ALSA: pcm_lib - return back hw_ptr_interrupt
[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->hw_ptr_interrupt = runtime->status->hw_ptr -
1256                 runtime->status->hw_ptr % runtime->period_size;
1257         runtime->silence_start = runtime->status->hw_ptr;
1258         runtime->silence_filled = 0;
1259         return 0;
1260 }
1261
1262 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1263 {
1264         struct snd_pcm_runtime *runtime = substream->runtime;
1265         runtime->control->appl_ptr = runtime->status->hw_ptr;
1266         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1267             runtime->silence_size > 0)
1268                 snd_pcm_playback_silence(substream, ULONG_MAX);
1269 }
1270
1271 static struct action_ops snd_pcm_action_reset = {
1272         .pre_action = snd_pcm_pre_reset,
1273         .do_action = snd_pcm_do_reset,
1274         .post_action = snd_pcm_post_reset
1275 };
1276
1277 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1278 {
1279         return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1280 }
1281
1282 /*
1283  * prepare ioctl
1284  */
1285 /* we use the second argument for updating f_flags */
1286 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1287                                int f_flags)
1288 {
1289         struct snd_pcm_runtime *runtime = substream->runtime;
1290         if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1291             runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1292                 return -EBADFD;
1293         if (snd_pcm_running(substream))
1294                 return -EBUSY;
1295         substream->f_flags = f_flags;
1296         return 0;
1297 }
1298
1299 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1300 {
1301         int err;
1302         err = substream->ops->prepare(substream);
1303         if (err < 0)
1304                 return err;
1305         return snd_pcm_do_reset(substream, 0);
1306 }
1307
1308 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1309 {
1310         struct snd_pcm_runtime *runtime = substream->runtime;
1311         runtime->control->appl_ptr = runtime->status->hw_ptr;
1312         runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1313 }
1314
1315 static struct action_ops snd_pcm_action_prepare = {
1316         .pre_action = snd_pcm_pre_prepare,
1317         .do_action = snd_pcm_do_prepare,
1318         .post_action = snd_pcm_post_prepare
1319 };
1320
1321 /**
1322  * snd_pcm_prepare - prepare the PCM substream to be triggerable
1323  * @substream: the PCM substream instance
1324  * @file: file to refer f_flags
1325  */
1326 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1327                            struct file *file)
1328 {
1329         int res;
1330         struct snd_card *card = substream->pcm->card;
1331         int f_flags;
1332
1333         if (file)
1334                 f_flags = file->f_flags;
1335         else
1336                 f_flags = substream->f_flags;
1337
1338         snd_power_lock(card);
1339         if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1340                 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1341                                                substream, f_flags);
1342         snd_power_unlock(card);
1343         return res;
1344 }
1345
1346 /*
1347  * drain ioctl
1348  */
1349
1350 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1351 {
1352         substream->runtime->trigger_master = substream;
1353         return 0;
1354 }
1355
1356 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1357 {
1358         struct snd_pcm_runtime *runtime = substream->runtime;
1359         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1360                 switch (runtime->status->state) {
1361                 case SNDRV_PCM_STATE_PREPARED:
1362                         /* start playback stream if possible */
1363                         if (! snd_pcm_playback_empty(substream)) {
1364                                 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1365                                 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1366                         }
1367                         break;
1368                 case SNDRV_PCM_STATE_RUNNING:
1369                         runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1370                         break;
1371                 default:
1372                         break;
1373                 }
1374         } else {
1375                 /* stop running stream */
1376                 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1377                         int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1378                                 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1379                         snd_pcm_do_stop(substream, new_state);
1380                         snd_pcm_post_stop(substream, new_state);
1381                 }
1382         }
1383         return 0;
1384 }
1385
1386 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1387 {
1388 }
1389
1390 static struct action_ops snd_pcm_action_drain_init = {
1391         .pre_action = snd_pcm_pre_drain_init,
1392         .do_action = snd_pcm_do_drain_init,
1393         .post_action = snd_pcm_post_drain_init
1394 };
1395
1396 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1397
1398 /*
1399  * Drain the stream(s).
1400  * When the substream is linked, sync until the draining of all playback streams
1401  * is finished.
1402  * After this call, all streams are supposed to be either SETUP or DRAINING
1403  * (capture only) state.
1404  */
1405 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1406                          struct file *file)
1407 {
1408         struct snd_card *card;
1409         struct snd_pcm_runtime *runtime;
1410         struct snd_pcm_substream *s;
1411         wait_queue_t wait;
1412         int result = 0;
1413         int nonblock = 0;
1414
1415         card = substream->pcm->card;
1416         runtime = substream->runtime;
1417
1418         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1419                 return -EBADFD;
1420
1421         snd_power_lock(card);
1422         if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1423                 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1424                 if (result < 0) {
1425                         snd_power_unlock(card);
1426                         return result;
1427                 }
1428         }
1429
1430         if (file) {
1431                 if (file->f_flags & O_NONBLOCK)
1432                         nonblock = 1;
1433         } else if (substream->f_flags & O_NONBLOCK)
1434                 nonblock = 1;
1435
1436         down_read(&snd_pcm_link_rwsem);
1437         snd_pcm_stream_lock_irq(substream);
1438         /* resume pause */
1439         if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1440                 snd_pcm_pause(substream, 0);
1441
1442         /* pre-start/stop - all running streams are changed to DRAINING state */
1443         result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1444         if (result < 0)
1445                 goto unlock;
1446         /* in non-blocking, we don't wait in ioctl but let caller poll */
1447         if (nonblock) {
1448                 result = -EAGAIN;
1449                 goto unlock;
1450         }
1451
1452         for (;;) {
1453                 long tout;
1454                 struct snd_pcm_runtime *to_check;
1455                 if (signal_pending(current)) {
1456                         result = -ERESTARTSYS;
1457                         break;
1458                 }
1459                 /* find a substream to drain */
1460                 to_check = NULL;
1461                 snd_pcm_group_for_each_entry(s, substream) {
1462                         if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1463                                 continue;
1464                         runtime = s->runtime;
1465                         if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1466                                 to_check = runtime;
1467                                 break;
1468                         }
1469                 }
1470                 if (!to_check)
1471                         break; /* all drained */
1472                 init_waitqueue_entry(&wait, current);
1473                 add_wait_queue(&to_check->sleep, &wait);
1474                 set_current_state(TASK_INTERRUPTIBLE);
1475                 snd_pcm_stream_unlock_irq(substream);
1476                 up_read(&snd_pcm_link_rwsem);
1477                 snd_power_unlock(card);
1478                 tout = schedule_timeout(10 * HZ);
1479                 snd_power_lock(card);
1480                 down_read(&snd_pcm_link_rwsem);
1481                 snd_pcm_stream_lock_irq(substream);
1482                 remove_wait_queue(&to_check->sleep, &wait);
1483                 if (tout == 0) {
1484                         if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1485                                 result = -ESTRPIPE;
1486                         else {
1487                                 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1488                                 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1489                                 result = -EIO;
1490                         }
1491                         break;
1492                 }
1493         }
1494
1495  unlock:
1496         snd_pcm_stream_unlock_irq(substream);
1497         up_read(&snd_pcm_link_rwsem);
1498         snd_power_unlock(card);
1499
1500         return result;
1501 }
1502
1503 /*
1504  * drop ioctl
1505  *
1506  * Immediately put all linked substreams into SETUP state.
1507  */
1508 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1509 {
1510         struct snd_pcm_runtime *runtime;
1511         struct snd_card *card;
1512         int result = 0;
1513         
1514         if (PCM_RUNTIME_CHECK(substream))
1515                 return -ENXIO;
1516         runtime = substream->runtime;
1517         card = substream->pcm->card;
1518
1519         if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1520             runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1521             runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1522                 return -EBADFD;
1523
1524         snd_pcm_stream_lock_irq(substream);
1525         /* resume pause */
1526         if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1527                 snd_pcm_pause(substream, 0);
1528
1529         snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1530         /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1531         snd_pcm_stream_unlock_irq(substream);
1532
1533         return result;
1534 }
1535
1536
1537 /* WARNING: Don't forget to fput back the file */
1538 static struct file *snd_pcm_file_fd(int fd)
1539 {
1540         struct file *file;
1541         struct inode *inode;
1542         unsigned int minor;
1543
1544         file = fget(fd);
1545         if (!file)
1546                 return NULL;
1547         inode = file->f_path.dentry->d_inode;
1548         if (!S_ISCHR(inode->i_mode) ||
1549             imajor(inode) != snd_major) {
1550                 fput(file);
1551                 return NULL;
1552         }
1553         minor = iminor(inode);
1554         if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1555             !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1556                 fput(file);
1557                 return NULL;
1558         }
1559         return file;
1560 }
1561
1562 /*
1563  * PCM link handling
1564  */
1565 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1566 {
1567         int res = 0;
1568         struct file *file;
1569         struct snd_pcm_file *pcm_file;
1570         struct snd_pcm_substream *substream1;
1571
1572         file = snd_pcm_file_fd(fd);
1573         if (!file)
1574                 return -EBADFD;
1575         pcm_file = file->private_data;
1576         substream1 = pcm_file->substream;
1577         down_write(&snd_pcm_link_rwsem);
1578         write_lock_irq(&snd_pcm_link_rwlock);
1579         if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1580             substream->runtime->status->state != substream1->runtime->status->state) {
1581                 res = -EBADFD;
1582                 goto _end;
1583         }
1584         if (snd_pcm_stream_linked(substream1)) {
1585                 res = -EALREADY;
1586                 goto _end;
1587         }
1588         if (!snd_pcm_stream_linked(substream)) {
1589                 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1590                 if (substream->group == NULL) {
1591                         res = -ENOMEM;
1592                         goto _end;
1593                 }
1594                 spin_lock_init(&substream->group->lock);
1595                 INIT_LIST_HEAD(&substream->group->substreams);
1596                 list_add_tail(&substream->link_list, &substream->group->substreams);
1597                 substream->group->count = 1;
1598         }
1599         list_add_tail(&substream1->link_list, &substream->group->substreams);
1600         substream->group->count++;
1601         substream1->group = substream->group;
1602  _end:
1603         write_unlock_irq(&snd_pcm_link_rwlock);
1604         up_write(&snd_pcm_link_rwsem);
1605         fput(file);
1606         return res;
1607 }
1608
1609 static void relink_to_local(struct snd_pcm_substream *substream)
1610 {
1611         substream->group = &substream->self_group;
1612         INIT_LIST_HEAD(&substream->self_group.substreams);
1613         list_add_tail(&substream->link_list, &substream->self_group.substreams);
1614 }
1615
1616 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1617 {
1618         struct snd_pcm_substream *s;
1619         int res = 0;
1620
1621         down_write(&snd_pcm_link_rwsem);
1622         write_lock_irq(&snd_pcm_link_rwlock);
1623         if (!snd_pcm_stream_linked(substream)) {
1624                 res = -EALREADY;
1625                 goto _end;
1626         }
1627         list_del(&substream->link_list);
1628         substream->group->count--;
1629         if (substream->group->count == 1) {     /* detach the last stream, too */
1630                 snd_pcm_group_for_each_entry(s, substream) {
1631                         relink_to_local(s);
1632                         break;
1633                 }
1634                 kfree(substream->group);
1635         }
1636         relink_to_local(substream);
1637        _end:
1638         write_unlock_irq(&snd_pcm_link_rwlock);
1639         up_write(&snd_pcm_link_rwsem);
1640         return res;
1641 }
1642
1643 /*
1644  * hw configurator
1645  */
1646 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1647                                struct snd_pcm_hw_rule *rule)
1648 {
1649         struct snd_interval t;
1650         snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1651                      hw_param_interval_c(params, rule->deps[1]), &t);
1652         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1653 }
1654
1655 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1656                                struct snd_pcm_hw_rule *rule)
1657 {
1658         struct snd_interval t;
1659         snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1660                      hw_param_interval_c(params, rule->deps[1]), &t);
1661         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1662 }
1663
1664 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1665                                    struct snd_pcm_hw_rule *rule)
1666 {
1667         struct snd_interval t;
1668         snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1669                          hw_param_interval_c(params, rule->deps[1]),
1670                          (unsigned long) rule->private, &t);
1671         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1672 }
1673
1674 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1675                                    struct snd_pcm_hw_rule *rule)
1676 {
1677         struct snd_interval t;
1678         snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1679                          (unsigned long) rule->private,
1680                          hw_param_interval_c(params, rule->deps[1]), &t);
1681         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1682 }
1683
1684 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1685                                   struct snd_pcm_hw_rule *rule)
1686 {
1687         unsigned int k;
1688         struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1689         struct snd_mask m;
1690         struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1691         snd_mask_any(&m);
1692         for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1693                 int bits;
1694                 if (! snd_mask_test(mask, k))
1695                         continue;
1696                 bits = snd_pcm_format_physical_width(k);
1697                 if (bits <= 0)
1698                         continue; /* ignore invalid formats */
1699                 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1700                         snd_mask_reset(&m, k);
1701         }
1702         return snd_mask_refine(mask, &m);
1703 }
1704
1705 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1706                                        struct snd_pcm_hw_rule *rule)
1707 {
1708         struct snd_interval t;
1709         unsigned int k;
1710         t.min = UINT_MAX;
1711         t.max = 0;
1712         t.openmin = 0;
1713         t.openmax = 0;
1714         for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1715                 int bits;
1716                 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1717                         continue;
1718                 bits = snd_pcm_format_physical_width(k);
1719                 if (bits <= 0)
1720                         continue; /* ignore invalid formats */
1721                 if (t.min > (unsigned)bits)
1722                         t.min = bits;
1723                 if (t.max < (unsigned)bits)
1724                         t.max = bits;
1725         }
1726         t.integer = 1;
1727         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1728 }
1729
1730 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1731 #error "Change this table"
1732 #endif
1733
1734 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1735                                  48000, 64000, 88200, 96000, 176400, 192000 };
1736
1737 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1738         .count = ARRAY_SIZE(rates),
1739         .list = rates,
1740 };
1741
1742 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1743                                 struct snd_pcm_hw_rule *rule)
1744 {
1745         struct snd_pcm_hardware *hw = rule->private;
1746         return snd_interval_list(hw_param_interval(params, rule->var),
1747                                  snd_pcm_known_rates.count,
1748                                  snd_pcm_known_rates.list, hw->rates);
1749 }               
1750
1751 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1752                                             struct snd_pcm_hw_rule *rule)
1753 {
1754         struct snd_interval t;
1755         struct snd_pcm_substream *substream = rule->private;
1756         t.min = 0;
1757         t.max = substream->buffer_bytes_max;
1758         t.openmin = 0;
1759         t.openmax = 0;
1760         t.integer = 1;
1761         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1762 }               
1763
1764 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1765 {
1766         struct snd_pcm_runtime *runtime = substream->runtime;
1767         struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1768         int k, err;
1769
1770         for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1771                 snd_mask_any(constrs_mask(constrs, k));
1772         }
1773
1774         for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1775                 snd_interval_any(constrs_interval(constrs, k));
1776         }
1777
1778         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1779         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1780         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1781         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1782         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1783
1784         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1785                                    snd_pcm_hw_rule_format, NULL,
1786                                    SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1787         if (err < 0)
1788                 return err;
1789         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 
1790                                   snd_pcm_hw_rule_sample_bits, NULL,
1791                                   SNDRV_PCM_HW_PARAM_FORMAT, 
1792                                   SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1793         if (err < 0)
1794                 return err;
1795         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 
1796                                   snd_pcm_hw_rule_div, NULL,
1797                                   SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1798         if (err < 0)
1799                 return err;
1800         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
1801                                   snd_pcm_hw_rule_mul, NULL,
1802                                   SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1803         if (err < 0)
1804                 return err;
1805         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
1806                                   snd_pcm_hw_rule_mulkdiv, (void*) 8,
1807                                   SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1808         if (err < 0)
1809                 return err;
1810         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
1811                                   snd_pcm_hw_rule_mulkdiv, (void*) 8,
1812                                   SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1813         if (err < 0)
1814                 return err;
1815         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 
1816                                   snd_pcm_hw_rule_div, NULL,
1817                                   SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1818         if (err < 0)
1819                 return err;
1820         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
1821                                   snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1822                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1823         if (err < 0)
1824                 return err;
1825         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
1826                                   snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1827                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1828         if (err < 0)
1829                 return err;
1830         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, 
1831                                   snd_pcm_hw_rule_div, NULL,
1832                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1833         if (err < 0)
1834                 return err;
1835         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
1836                                   snd_pcm_hw_rule_div, NULL,
1837                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1838         if (err < 0)
1839                 return err;
1840         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
1841                                   snd_pcm_hw_rule_mulkdiv, (void*) 8,
1842                                   SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1843         if (err < 0)
1844                 return err;
1845         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
1846                                   snd_pcm_hw_rule_muldivk, (void*) 1000000,
1847                                   SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1848         if (err < 0)
1849                 return err;
1850         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
1851                                   snd_pcm_hw_rule_mul, NULL,
1852                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1853         if (err < 0)
1854                 return err;
1855         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
1856                                   snd_pcm_hw_rule_mulkdiv, (void*) 8,
1857                                   SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1858         if (err < 0)
1859                 return err;
1860         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
1861                                   snd_pcm_hw_rule_muldivk, (void*) 1000000,
1862                                   SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1863         if (err < 0)
1864                 return err;
1865         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 
1866                                   snd_pcm_hw_rule_muldivk, (void*) 8,
1867                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1868         if (err < 0)
1869                 return err;
1870         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 
1871                                   snd_pcm_hw_rule_muldivk, (void*) 8,
1872                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1873         if (err < 0)
1874                 return err;
1875         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 
1876                                   snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1877                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1878         if (err < 0)
1879                 return err;
1880         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 
1881                                   snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1882                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1883         if (err < 0)
1884                 return err;
1885         return 0;
1886 }
1887
1888 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1889 {
1890         struct snd_pcm_runtime *runtime = substream->runtime;
1891         struct snd_pcm_hardware *hw = &runtime->hw;
1892         int err;
1893         unsigned int mask = 0;
1894
1895         if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1896                 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1897         if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1898                 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1899         if (hw->info & SNDRV_PCM_INFO_MMAP) {
1900                 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1901                         mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1902                 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1903                         mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1904                 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1905                         mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1906         }
1907         err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1908         if (err < 0)
1909                 return err;
1910
1911         err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1912         if (err < 0)
1913                 return err;
1914
1915         err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1916         if (err < 0)
1917                 return err;
1918
1919         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1920                                            hw->channels_min, hw->channels_max);
1921         if (err < 0)
1922                 return err;
1923
1924         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1925                                            hw->rate_min, hw->rate_max);
1926          if (err < 0)
1927                  return err;
1928
1929         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1930                                            hw->period_bytes_min, hw->period_bytes_max);
1931          if (err < 0)
1932                  return err;
1933
1934         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1935                                            hw->periods_min, hw->periods_max);
1936         if (err < 0)
1937                 return err;
1938
1939         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1940                                            hw->period_bytes_min, hw->buffer_bytes_max);
1941         if (err < 0)
1942                 return err;
1943
1944         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 
1945                                   snd_pcm_hw_rule_buffer_bytes_max, substream,
1946                                   SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1947         if (err < 0)
1948                 return err;
1949
1950         /* FIXME: remove */
1951         if (runtime->dma_bytes) {
1952                 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1953                 if (err < 0)
1954                         return -EINVAL;
1955         }
1956
1957         if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1958                 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
1959                                           snd_pcm_hw_rule_rate, hw,
1960                                           SNDRV_PCM_HW_PARAM_RATE, -1);
1961                 if (err < 0)
1962                         return err;
1963         }
1964
1965         /* FIXME: this belong to lowlevel */
1966         snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1967
1968         return 0;
1969 }
1970
1971 static void pcm_release_private(struct snd_pcm_substream *substream)
1972 {
1973         snd_pcm_unlink(substream);
1974 }
1975
1976 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1977 {
1978         substream->ref_count--;
1979         if (substream->ref_count > 0)
1980                 return;
1981
1982         snd_pcm_drop(substream);
1983         if (substream->hw_opened) {
1984                 if (substream->ops->hw_free != NULL)
1985                         substream->ops->hw_free(substream);
1986                 substream->ops->close(substream);
1987                 substream->hw_opened = 0;
1988         }
1989         if (substream->pcm_release) {
1990                 substream->pcm_release(substream);
1991                 substream->pcm_release = NULL;
1992         }
1993         snd_pcm_detach_substream(substream);
1994 }
1995
1996 EXPORT_SYMBOL(snd_pcm_release_substream);
1997
1998 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
1999                            struct file *file,
2000                            struct snd_pcm_substream **rsubstream)
2001 {
2002         struct snd_pcm_substream *substream;
2003         int err;
2004
2005         err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2006         if (err < 0)
2007                 return err;
2008         if (substream->ref_count > 1) {
2009                 *rsubstream = substream;
2010                 return 0;
2011         }
2012
2013         err = snd_pcm_hw_constraints_init(substream);
2014         if (err < 0) {
2015                 snd_printd("snd_pcm_hw_constraints_init failed\n");
2016                 goto error;
2017         }
2018
2019         if ((err = substream->ops->open(substream)) < 0)
2020                 goto error;
2021
2022         substream->hw_opened = 1;
2023
2024         err = snd_pcm_hw_constraints_complete(substream);
2025         if (err < 0) {
2026                 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2027                 goto error;
2028         }
2029
2030         *rsubstream = substream;
2031         return 0;
2032
2033  error:
2034         snd_pcm_release_substream(substream);
2035         return err;
2036 }
2037
2038 EXPORT_SYMBOL(snd_pcm_open_substream);
2039
2040 static int snd_pcm_open_file(struct file *file,
2041                              struct snd_pcm *pcm,
2042                              int stream,
2043                              struct snd_pcm_file **rpcm_file)
2044 {
2045         struct snd_pcm_file *pcm_file;
2046         struct snd_pcm_substream *substream;
2047         struct snd_pcm_str *str;
2048         int err;
2049
2050         if (rpcm_file)
2051                 *rpcm_file = NULL;
2052
2053         err = snd_pcm_open_substream(pcm, stream, file, &substream);
2054         if (err < 0)
2055                 return err;
2056
2057         pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2058         if (pcm_file == NULL) {
2059                 snd_pcm_release_substream(substream);
2060                 return -ENOMEM;
2061         }
2062         pcm_file->substream = substream;
2063         if (substream->ref_count == 1) {
2064                 str = substream->pstr;
2065                 substream->file = pcm_file;
2066                 substream->pcm_release = pcm_release_private;
2067         }
2068         file->private_data = pcm_file;
2069         if (rpcm_file)
2070                 *rpcm_file = pcm_file;
2071         return 0;
2072 }
2073
2074 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2075 {
2076         struct snd_pcm *pcm;
2077
2078         pcm = snd_lookup_minor_data(iminor(inode),
2079                                     SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2080         return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2081 }
2082
2083 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2084 {
2085         struct snd_pcm *pcm;
2086
2087         pcm = snd_lookup_minor_data(iminor(inode),
2088                                     SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2089         return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2090 }
2091
2092 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2093 {
2094         int err;
2095         struct snd_pcm_file *pcm_file;
2096         wait_queue_t wait;
2097
2098         if (pcm == NULL) {
2099                 err = -ENODEV;
2100                 goto __error1;
2101         }
2102         err = snd_card_file_add(pcm->card, file);
2103         if (err < 0)
2104                 goto __error1;
2105         if (!try_module_get(pcm->card->module)) {
2106                 err = -EFAULT;
2107                 goto __error2;
2108         }
2109         init_waitqueue_entry(&wait, current);
2110         add_wait_queue(&pcm->open_wait, &wait);
2111         mutex_lock(&pcm->open_mutex);
2112         while (1) {
2113                 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2114                 if (err >= 0)
2115                         break;
2116                 if (err == -EAGAIN) {
2117                         if (file->f_flags & O_NONBLOCK) {
2118                                 err = -EBUSY;
2119                                 break;
2120                         }
2121                 } else
2122                         break;
2123                 set_current_state(TASK_INTERRUPTIBLE);
2124                 mutex_unlock(&pcm->open_mutex);
2125                 schedule();
2126                 mutex_lock(&pcm->open_mutex);
2127                 if (signal_pending(current)) {
2128                         err = -ERESTARTSYS;
2129                         break;
2130                 }
2131         }
2132         remove_wait_queue(&pcm->open_wait, &wait);
2133         mutex_unlock(&pcm->open_mutex);
2134         if (err < 0)
2135                 goto __error;
2136         return err;
2137
2138       __error:
2139         module_put(pcm->card->module);
2140       __error2:
2141         snd_card_file_remove(pcm->card, file);
2142       __error1:
2143         return err;
2144 }
2145
2146 static int snd_pcm_release(struct inode *inode, struct file *file)
2147 {
2148         struct snd_pcm *pcm;
2149         struct snd_pcm_substream *substream;
2150         struct snd_pcm_file *pcm_file;
2151
2152         pcm_file = file->private_data;
2153         substream = pcm_file->substream;
2154         if (snd_BUG_ON(!substream))
2155                 return -ENXIO;
2156         pcm = substream->pcm;
2157         mutex_lock(&pcm->open_mutex);
2158         snd_pcm_release_substream(substream);
2159         kfree(pcm_file);
2160         mutex_unlock(&pcm->open_mutex);
2161         wake_up(&pcm->open_wait);
2162         module_put(pcm->card->module);
2163         snd_card_file_remove(pcm->card, file);
2164         return 0;
2165 }
2166
2167 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2168                                                  snd_pcm_uframes_t frames)
2169 {
2170         struct snd_pcm_runtime *runtime = substream->runtime;
2171         snd_pcm_sframes_t appl_ptr;
2172         snd_pcm_sframes_t ret;
2173         snd_pcm_sframes_t hw_avail;
2174
2175         if (frames == 0)
2176                 return 0;
2177
2178         snd_pcm_stream_lock_irq(substream);
2179         switch (runtime->status->state) {
2180         case SNDRV_PCM_STATE_PREPARED:
2181                 break;
2182         case SNDRV_PCM_STATE_DRAINING:
2183         case SNDRV_PCM_STATE_RUNNING:
2184                 if (snd_pcm_update_hw_ptr(substream) >= 0)
2185                         break;
2186                 /* Fall through */
2187         case SNDRV_PCM_STATE_XRUN:
2188                 ret = -EPIPE;
2189                 goto __end;
2190         case SNDRV_PCM_STATE_SUSPENDED:
2191                 ret = -ESTRPIPE;
2192                 goto __end;
2193         default:
2194                 ret = -EBADFD;
2195                 goto __end;
2196         }
2197
2198         hw_avail = snd_pcm_playback_hw_avail(runtime);
2199         if (hw_avail <= 0) {
2200                 ret = 0;
2201                 goto __end;
2202         }
2203         if (frames > (snd_pcm_uframes_t)hw_avail)
2204                 frames = hw_avail;
2205         appl_ptr = runtime->control->appl_ptr - frames;
2206         if (appl_ptr < 0)
2207                 appl_ptr += runtime->boundary;
2208         runtime->control->appl_ptr = appl_ptr;
2209         ret = frames;
2210  __end:
2211         snd_pcm_stream_unlock_irq(substream);
2212         return ret;
2213 }
2214
2215 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2216                                                 snd_pcm_uframes_t frames)
2217 {
2218         struct snd_pcm_runtime *runtime = substream->runtime;
2219         snd_pcm_sframes_t appl_ptr;
2220         snd_pcm_sframes_t ret;
2221         snd_pcm_sframes_t hw_avail;
2222
2223         if (frames == 0)
2224                 return 0;
2225
2226         snd_pcm_stream_lock_irq(substream);
2227         switch (runtime->status->state) {
2228         case SNDRV_PCM_STATE_PREPARED:
2229         case SNDRV_PCM_STATE_DRAINING:
2230                 break;
2231         case SNDRV_PCM_STATE_RUNNING:
2232                 if (snd_pcm_update_hw_ptr(substream) >= 0)
2233                         break;
2234                 /* Fall through */
2235         case SNDRV_PCM_STATE_XRUN:
2236                 ret = -EPIPE;
2237                 goto __end;
2238         case SNDRV_PCM_STATE_SUSPENDED:
2239                 ret = -ESTRPIPE;
2240                 goto __end;
2241         default:
2242                 ret = -EBADFD;
2243                 goto __end;
2244         }
2245
2246         hw_avail = snd_pcm_capture_hw_avail(runtime);
2247         if (hw_avail <= 0) {
2248                 ret = 0;
2249                 goto __end;
2250         }
2251         if (frames > (snd_pcm_uframes_t)hw_avail)
2252                 frames = hw_avail;
2253         appl_ptr = runtime->control->appl_ptr - frames;
2254         if (appl_ptr < 0)
2255                 appl_ptr += runtime->boundary;
2256         runtime->control->appl_ptr = appl_ptr;
2257         ret = frames;
2258  __end:
2259         snd_pcm_stream_unlock_irq(substream);
2260         return ret;
2261 }
2262
2263 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2264                                                   snd_pcm_uframes_t frames)
2265 {
2266         struct snd_pcm_runtime *runtime = substream->runtime;
2267         snd_pcm_sframes_t appl_ptr;
2268         snd_pcm_sframes_t ret;
2269         snd_pcm_sframes_t avail;
2270
2271         if (frames == 0)
2272                 return 0;
2273
2274         snd_pcm_stream_lock_irq(substream);
2275         switch (runtime->status->state) {
2276         case SNDRV_PCM_STATE_PREPARED:
2277         case SNDRV_PCM_STATE_PAUSED:
2278                 break;
2279         case SNDRV_PCM_STATE_DRAINING:
2280         case SNDRV_PCM_STATE_RUNNING:
2281                 if (snd_pcm_update_hw_ptr(substream) >= 0)
2282                         break;
2283                 /* Fall through */
2284         case SNDRV_PCM_STATE_XRUN:
2285                 ret = -EPIPE;
2286                 goto __end;
2287         case SNDRV_PCM_STATE_SUSPENDED:
2288                 ret = -ESTRPIPE;
2289                 goto __end;
2290         default:
2291                 ret = -EBADFD;
2292                 goto __end;
2293         }
2294
2295         avail = snd_pcm_playback_avail(runtime);
2296         if (avail <= 0) {
2297                 ret = 0;
2298                 goto __end;
2299         }
2300         if (frames > (snd_pcm_uframes_t)avail)
2301                 frames = avail;
2302         appl_ptr = runtime->control->appl_ptr + frames;
2303         if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2304                 appl_ptr -= runtime->boundary;
2305         runtime->control->appl_ptr = appl_ptr;
2306         ret = frames;
2307  __end:
2308         snd_pcm_stream_unlock_irq(substream);
2309         return ret;
2310 }
2311
2312 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2313                                                  snd_pcm_uframes_t frames)
2314 {
2315         struct snd_pcm_runtime *runtime = substream->runtime;
2316         snd_pcm_sframes_t appl_ptr;
2317         snd_pcm_sframes_t ret;
2318         snd_pcm_sframes_t avail;
2319
2320         if (frames == 0)
2321                 return 0;
2322
2323         snd_pcm_stream_lock_irq(substream);
2324         switch (runtime->status->state) {
2325         case SNDRV_PCM_STATE_PREPARED:
2326         case SNDRV_PCM_STATE_DRAINING:
2327         case SNDRV_PCM_STATE_PAUSED:
2328                 break;
2329         case SNDRV_PCM_STATE_RUNNING:
2330                 if (snd_pcm_update_hw_ptr(substream) >= 0)
2331                         break;
2332                 /* Fall through */
2333         case SNDRV_PCM_STATE_XRUN:
2334                 ret = -EPIPE;
2335                 goto __end;
2336         case SNDRV_PCM_STATE_SUSPENDED:
2337                 ret = -ESTRPIPE;
2338                 goto __end;
2339         default:
2340                 ret = -EBADFD;
2341                 goto __end;
2342         }
2343
2344         avail = snd_pcm_capture_avail(runtime);
2345         if (avail <= 0) {
2346                 ret = 0;
2347                 goto __end;
2348         }
2349         if (frames > (snd_pcm_uframes_t)avail)
2350                 frames = avail;
2351         appl_ptr = runtime->control->appl_ptr + frames;
2352         if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2353                 appl_ptr -= runtime->boundary;
2354         runtime->control->appl_ptr = appl_ptr;
2355         ret = frames;
2356  __end:
2357         snd_pcm_stream_unlock_irq(substream);
2358         return ret;
2359 }
2360
2361 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2362 {
2363         struct snd_pcm_runtime *runtime = substream->runtime;
2364         int err;
2365
2366         snd_pcm_stream_lock_irq(substream);
2367         switch (runtime->status->state) {
2368         case SNDRV_PCM_STATE_DRAINING:
2369                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2370                         goto __badfd;
2371         case SNDRV_PCM_STATE_RUNNING:
2372                 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2373                         break;
2374                 /* Fall through */
2375         case SNDRV_PCM_STATE_PREPARED:
2376         case SNDRV_PCM_STATE_SUSPENDED:
2377                 err = 0;
2378                 break;
2379         case SNDRV_PCM_STATE_XRUN:
2380                 err = -EPIPE;
2381                 break;
2382         default:
2383               __badfd:
2384                 err = -EBADFD;
2385                 break;
2386         }
2387         snd_pcm_stream_unlock_irq(substream);
2388         return err;
2389 }
2390                 
2391 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2392                          snd_pcm_sframes_t __user *res)
2393 {
2394         struct snd_pcm_runtime *runtime = substream->runtime;
2395         int err;
2396         snd_pcm_sframes_t n = 0;
2397
2398         snd_pcm_stream_lock_irq(substream);
2399         switch (runtime->status->state) {
2400         case SNDRV_PCM_STATE_DRAINING:
2401                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2402                         goto __badfd;
2403         case SNDRV_PCM_STATE_RUNNING:
2404                 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2405                         break;
2406                 /* Fall through */
2407         case SNDRV_PCM_STATE_PREPARED:
2408         case SNDRV_PCM_STATE_SUSPENDED:
2409                 err = 0;
2410                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2411                         n = snd_pcm_playback_hw_avail(runtime);
2412                 else
2413                         n = snd_pcm_capture_avail(runtime);
2414                 n += runtime->delay;
2415                 break;
2416         case SNDRV_PCM_STATE_XRUN:
2417                 err = -EPIPE;
2418                 break;
2419         default:
2420               __badfd:
2421                 err = -EBADFD;
2422                 break;
2423         }
2424         snd_pcm_stream_unlock_irq(substream);
2425         if (!err)
2426                 if (put_user(n, res))
2427                         err = -EFAULT;
2428         return err;
2429 }
2430                 
2431 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2432                             struct snd_pcm_sync_ptr __user *_sync_ptr)
2433 {
2434         struct snd_pcm_runtime *runtime = substream->runtime;
2435         struct snd_pcm_sync_ptr sync_ptr;
2436         volatile struct snd_pcm_mmap_status *status;
2437         volatile struct snd_pcm_mmap_control *control;
2438         int err;
2439
2440         memset(&sync_ptr, 0, sizeof(sync_ptr));
2441         if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2442                 return -EFAULT;
2443         if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2444                 return -EFAULT; 
2445         status = runtime->status;
2446         control = runtime->control;
2447         if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2448                 err = snd_pcm_hwsync(substream);
2449                 if (err < 0)
2450                         return err;
2451         }
2452         snd_pcm_stream_lock_irq(substream);
2453         if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2454                 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2455         else
2456                 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2457         if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2458                 control->avail_min = sync_ptr.c.control.avail_min;
2459         else
2460                 sync_ptr.c.control.avail_min = control->avail_min;
2461         sync_ptr.s.status.state = status->state;
2462         sync_ptr.s.status.hw_ptr = status->hw_ptr;
2463         sync_ptr.s.status.tstamp = status->tstamp;
2464         sync_ptr.s.status.suspended_state = status->suspended_state;
2465         snd_pcm_stream_unlock_irq(substream);
2466         if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2467                 return -EFAULT;
2468         return 0;
2469 }
2470
2471 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2472 {
2473         struct snd_pcm_runtime *runtime = substream->runtime;
2474         int arg;
2475         
2476         if (get_user(arg, _arg))
2477                 return -EFAULT;
2478         if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2479                 return -EINVAL;
2480         runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2481         if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2482                 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2483         return 0;
2484 }
2485                 
2486 static int snd_pcm_common_ioctl1(struct file *file,
2487                                  struct snd_pcm_substream *substream,
2488                                  unsigned int cmd, void __user *arg)
2489 {
2490         switch (cmd) {
2491         case SNDRV_PCM_IOCTL_PVERSION:
2492                 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2493         case SNDRV_PCM_IOCTL_INFO:
2494                 return snd_pcm_info_user(substream, arg);
2495         case SNDRV_PCM_IOCTL_TSTAMP:    /* just for compatibility */
2496                 return 0;
2497         case SNDRV_PCM_IOCTL_TTSTAMP:
2498                 return snd_pcm_tstamp(substream, arg);
2499         case SNDRV_PCM_IOCTL_HW_REFINE:
2500                 return snd_pcm_hw_refine_user(substream, arg);
2501         case SNDRV_PCM_IOCTL_HW_PARAMS:
2502                 return snd_pcm_hw_params_user(substream, arg);
2503         case SNDRV_PCM_IOCTL_HW_FREE:
2504                 return snd_pcm_hw_free(substream);
2505         case SNDRV_PCM_IOCTL_SW_PARAMS:
2506                 return snd_pcm_sw_params_user(substream, arg);
2507         case SNDRV_PCM_IOCTL_STATUS:
2508                 return snd_pcm_status_user(substream, arg);
2509         case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2510                 return snd_pcm_channel_info_user(substream, arg);
2511         case SNDRV_PCM_IOCTL_PREPARE:
2512                 return snd_pcm_prepare(substream, file);
2513         case SNDRV_PCM_IOCTL_RESET:
2514                 return snd_pcm_reset(substream);
2515         case SNDRV_PCM_IOCTL_START:
2516                 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2517         case SNDRV_PCM_IOCTL_LINK:
2518                 return snd_pcm_link(substream, (int)(unsigned long) arg);
2519         case SNDRV_PCM_IOCTL_UNLINK:
2520                 return snd_pcm_unlink(substream);
2521         case SNDRV_PCM_IOCTL_RESUME:
2522                 return snd_pcm_resume(substream);
2523         case SNDRV_PCM_IOCTL_XRUN:
2524                 return snd_pcm_xrun(substream);
2525         case SNDRV_PCM_IOCTL_HWSYNC:
2526                 return snd_pcm_hwsync(substream);
2527         case SNDRV_PCM_IOCTL_DELAY:
2528                 return snd_pcm_delay(substream, arg);
2529         case SNDRV_PCM_IOCTL_SYNC_PTR:
2530                 return snd_pcm_sync_ptr(substream, arg);
2531 #ifdef CONFIG_SND_SUPPORT_OLD_API
2532         case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2533                 return snd_pcm_hw_refine_old_user(substream, arg);
2534         case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2535                 return snd_pcm_hw_params_old_user(substream, arg);
2536 #endif
2537         case SNDRV_PCM_IOCTL_DRAIN:
2538                 return snd_pcm_drain(substream, file);
2539         case SNDRV_PCM_IOCTL_DROP:
2540                 return snd_pcm_drop(substream);
2541         case SNDRV_PCM_IOCTL_PAUSE:
2542         {
2543                 int res;
2544                 snd_pcm_stream_lock_irq(substream);
2545                 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2546                 snd_pcm_stream_unlock_irq(substream);
2547                 return res;
2548         }
2549         }
2550         snd_printd("unknown ioctl = 0x%x\n", cmd);
2551         return -ENOTTY;
2552 }
2553
2554 static int snd_pcm_playback_ioctl1(struct file *file,
2555                                    struct snd_pcm_substream *substream,
2556                                    unsigned int cmd, void __user *arg)
2557 {
2558         if (snd_BUG_ON(!substream))
2559                 return -ENXIO;
2560         if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2561                 return -EINVAL;
2562         switch (cmd) {
2563         case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2564         {
2565                 struct snd_xferi xferi;
2566                 struct snd_xferi __user *_xferi = arg;
2567                 struct snd_pcm_runtime *runtime = substream->runtime;
2568                 snd_pcm_sframes_t result;
2569                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2570                         return -EBADFD;
2571                 if (put_user(0, &_xferi->result))
2572                         return -EFAULT;
2573                 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2574                         return -EFAULT;
2575                 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2576                 __put_user(result, &_xferi->result);
2577                 return result < 0 ? result : 0;
2578         }
2579         case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2580         {
2581                 struct snd_xfern xfern;
2582                 struct snd_xfern __user *_xfern = arg;
2583                 struct snd_pcm_runtime *runtime = substream->runtime;
2584                 void __user **bufs;
2585                 snd_pcm_sframes_t result;
2586                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2587                         return -EBADFD;
2588                 if (runtime->channels > 128)
2589                         return -EINVAL;
2590                 if (put_user(0, &_xfern->result))
2591                         return -EFAULT;
2592                 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2593                         return -EFAULT;
2594
2595                 bufs = memdup_user(xfern.bufs,
2596                                    sizeof(void *) * runtime->channels);
2597                 if (IS_ERR(bufs))
2598                         return PTR_ERR(bufs);
2599                 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2600                 kfree(bufs);
2601                 __put_user(result, &_xfern->result);
2602                 return result < 0 ? result : 0;
2603         }
2604         case SNDRV_PCM_IOCTL_REWIND:
2605         {
2606                 snd_pcm_uframes_t frames;
2607                 snd_pcm_uframes_t __user *_frames = arg;
2608                 snd_pcm_sframes_t result;
2609                 if (get_user(frames, _frames))
2610                         return -EFAULT;
2611                 if (put_user(0, _frames))
2612                         return -EFAULT;
2613                 result = snd_pcm_playback_rewind(substream, frames);
2614                 __put_user(result, _frames);
2615                 return result < 0 ? result : 0;
2616         }
2617         case SNDRV_PCM_IOCTL_FORWARD:
2618         {
2619                 snd_pcm_uframes_t frames;
2620                 snd_pcm_uframes_t __user *_frames = arg;
2621                 snd_pcm_sframes_t result;
2622                 if (get_user(frames, _frames))
2623                         return -EFAULT;
2624                 if (put_user(0, _frames))
2625                         return -EFAULT;
2626                 result = snd_pcm_playback_forward(substream, frames);
2627                 __put_user(result, _frames);
2628                 return result < 0 ? result : 0;
2629         }
2630         }
2631         return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2632 }
2633
2634 static int snd_pcm_capture_ioctl1(struct file *file,
2635                                   struct snd_pcm_substream *substream,
2636                                   unsigned int cmd, void __user *arg)
2637 {
2638         if (snd_BUG_ON(!substream))
2639                 return -ENXIO;
2640         if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2641                 return -EINVAL;
2642         switch (cmd) {
2643         case SNDRV_PCM_IOCTL_READI_FRAMES:
2644         {
2645                 struct snd_xferi xferi;
2646                 struct snd_xferi __user *_xferi = arg;
2647                 struct snd_pcm_runtime *runtime = substream->runtime;
2648                 snd_pcm_sframes_t result;
2649                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2650                         return -EBADFD;
2651                 if (put_user(0, &_xferi->result))
2652                         return -EFAULT;
2653                 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2654                         return -EFAULT;
2655                 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2656                 __put_user(result, &_xferi->result);
2657                 return result < 0 ? result : 0;
2658         }
2659         case SNDRV_PCM_IOCTL_READN_FRAMES:
2660         {
2661                 struct snd_xfern xfern;
2662                 struct snd_xfern __user *_xfern = arg;
2663                 struct snd_pcm_runtime *runtime = substream->runtime;
2664                 void *bufs;
2665                 snd_pcm_sframes_t result;
2666                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2667                         return -EBADFD;
2668                 if (runtime->channels > 128)
2669                         return -EINVAL;
2670                 if (put_user(0, &_xfern->result))
2671                         return -EFAULT;
2672                 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2673                         return -EFAULT;
2674
2675                 bufs = memdup_user(xfern.bufs,
2676                                    sizeof(void *) * runtime->channels);
2677                 if (IS_ERR(bufs))
2678                         return PTR_ERR(bufs);
2679                 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2680                 kfree(bufs);
2681                 __put_user(result, &_xfern->result);
2682                 return result < 0 ? result : 0;
2683         }
2684         case SNDRV_PCM_IOCTL_REWIND:
2685         {
2686                 snd_pcm_uframes_t frames;
2687                 snd_pcm_uframes_t __user *_frames = arg;
2688                 snd_pcm_sframes_t result;
2689                 if (get_user(frames, _frames))
2690                         return -EFAULT;
2691                 if (put_user(0, _frames))
2692                         return -EFAULT;
2693                 result = snd_pcm_capture_rewind(substream, frames);
2694                 __put_user(result, _frames);
2695                 return result < 0 ? result : 0;
2696         }
2697         case SNDRV_PCM_IOCTL_FORWARD:
2698         {
2699                 snd_pcm_uframes_t frames;
2700                 snd_pcm_uframes_t __user *_frames = arg;
2701                 snd_pcm_sframes_t result;
2702                 if (get_user(frames, _frames))
2703                         return -EFAULT;
2704                 if (put_user(0, _frames))
2705                         return -EFAULT;
2706                 result = snd_pcm_capture_forward(substream, frames);
2707                 __put_user(result, _frames);
2708                 return result < 0 ? result : 0;
2709         }
2710         }
2711         return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2712 }
2713
2714 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2715                                    unsigned long arg)
2716 {
2717         struct snd_pcm_file *pcm_file;
2718
2719         pcm_file = file->private_data;
2720
2721         if (((cmd >> 8) & 0xff) != 'A')
2722                 return -ENOTTY;
2723
2724         return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2725                                        (void __user *)arg);
2726 }
2727
2728 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2729                                   unsigned long arg)
2730 {
2731         struct snd_pcm_file *pcm_file;
2732
2733         pcm_file = file->private_data;
2734
2735         if (((cmd >> 8) & 0xff) != 'A')
2736                 return -ENOTTY;
2737
2738         return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2739                                       (void __user *)arg);
2740 }
2741
2742 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2743                          unsigned int cmd, void *arg)
2744 {
2745         mm_segment_t fs;
2746         int result;
2747         
2748         fs = snd_enter_user();
2749         switch (substream->stream) {
2750         case SNDRV_PCM_STREAM_PLAYBACK:
2751                 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2752                                                  (void __user *)arg);
2753                 break;
2754         case SNDRV_PCM_STREAM_CAPTURE:
2755                 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2756                                                 (void __user *)arg);
2757                 break;
2758         default:
2759                 result = -EINVAL;
2760                 break;
2761         }
2762         snd_leave_user(fs);
2763         return result;
2764 }
2765
2766 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2767
2768 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2769                             loff_t * offset)
2770 {
2771         struct snd_pcm_file *pcm_file;
2772         struct snd_pcm_substream *substream;
2773         struct snd_pcm_runtime *runtime;
2774         snd_pcm_sframes_t result;
2775
2776         pcm_file = file->private_data;
2777         substream = pcm_file->substream;
2778         if (PCM_RUNTIME_CHECK(substream))
2779                 return -ENXIO;
2780         runtime = substream->runtime;
2781         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2782                 return -EBADFD;
2783         if (!frame_aligned(runtime, count))
2784                 return -EINVAL;
2785         count = bytes_to_frames(runtime, count);
2786         result = snd_pcm_lib_read(substream, buf, count);
2787         if (result > 0)
2788                 result = frames_to_bytes(runtime, result);
2789         return result;
2790 }
2791
2792 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2793                              size_t count, loff_t * offset)
2794 {
2795         struct snd_pcm_file *pcm_file;
2796         struct snd_pcm_substream *substream;
2797         struct snd_pcm_runtime *runtime;
2798         snd_pcm_sframes_t result;
2799
2800         pcm_file = file->private_data;
2801         substream = pcm_file->substream;
2802         if (PCM_RUNTIME_CHECK(substream))
2803                 return -ENXIO;
2804         runtime = substream->runtime;
2805         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2806                 return -EBADFD;
2807         if (!frame_aligned(runtime, count))
2808                 return -EINVAL;
2809         count = bytes_to_frames(runtime, count);
2810         result = snd_pcm_lib_write(substream, buf, count);
2811         if (result > 0)
2812                 result = frames_to_bytes(runtime, result);
2813         return result;
2814 }
2815
2816 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2817                              unsigned long nr_segs, loff_t pos)
2818
2819 {
2820         struct snd_pcm_file *pcm_file;
2821         struct snd_pcm_substream *substream;
2822         struct snd_pcm_runtime *runtime;
2823         snd_pcm_sframes_t result;
2824         unsigned long i;
2825         void __user **bufs;
2826         snd_pcm_uframes_t frames;
2827
2828         pcm_file = iocb->ki_filp->private_data;
2829         substream = pcm_file->substream;
2830         if (PCM_RUNTIME_CHECK(substream))
2831                 return -ENXIO;
2832         runtime = substream->runtime;
2833         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2834                 return -EBADFD;
2835         if (nr_segs > 1024 || nr_segs != runtime->channels)
2836                 return -EINVAL;
2837         if (!frame_aligned(runtime, iov->iov_len))
2838                 return -EINVAL;
2839         frames = bytes_to_samples(runtime, iov->iov_len);
2840         bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2841         if (bufs == NULL)
2842                 return -ENOMEM;
2843         for (i = 0; i < nr_segs; ++i)
2844                 bufs[i] = iov[i].iov_base;
2845         result = snd_pcm_lib_readv(substream, bufs, frames);
2846         if (result > 0)
2847                 result = frames_to_bytes(runtime, result);
2848         kfree(bufs);
2849         return result;
2850 }
2851
2852 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2853                               unsigned long nr_segs, loff_t pos)
2854 {
2855         struct snd_pcm_file *pcm_file;
2856         struct snd_pcm_substream *substream;
2857         struct snd_pcm_runtime *runtime;
2858         snd_pcm_sframes_t result;
2859         unsigned long i;
2860         void __user **bufs;
2861         snd_pcm_uframes_t frames;
2862
2863         pcm_file = iocb->ki_filp->private_data;
2864         substream = pcm_file->substream;
2865         if (PCM_RUNTIME_CHECK(substream))
2866                 return -ENXIO;
2867         runtime = substream->runtime;
2868         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2869                 return -EBADFD;
2870         if (nr_segs > 128 || nr_segs != runtime->channels ||
2871             !frame_aligned(runtime, iov->iov_len))
2872                 return -EINVAL;
2873         frames = bytes_to_samples(runtime, iov->iov_len);
2874         bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2875         if (bufs == NULL)
2876                 return -ENOMEM;
2877         for (i = 0; i < nr_segs; ++i)
2878                 bufs[i] = iov[i].iov_base;
2879         result = snd_pcm_lib_writev(substream, bufs, frames);
2880         if (result > 0)
2881                 result = frames_to_bytes(runtime, result);
2882         kfree(bufs);
2883         return result;
2884 }
2885
2886 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2887 {
2888         struct snd_pcm_file *pcm_file;
2889         struct snd_pcm_substream *substream;
2890         struct snd_pcm_runtime *runtime;
2891         unsigned int mask;
2892         snd_pcm_uframes_t avail;
2893
2894         pcm_file = file->private_data;
2895
2896         substream = pcm_file->substream;
2897         if (PCM_RUNTIME_CHECK(substream))
2898                 return -ENXIO;
2899         runtime = substream->runtime;
2900
2901         poll_wait(file, &runtime->sleep, wait);
2902
2903         snd_pcm_stream_lock_irq(substream);
2904         avail = snd_pcm_playback_avail(runtime);
2905         switch (runtime->status->state) {
2906         case SNDRV_PCM_STATE_RUNNING:
2907         case SNDRV_PCM_STATE_PREPARED:
2908         case SNDRV_PCM_STATE_PAUSED:
2909                 if (avail >= runtime->control->avail_min) {
2910                         mask = POLLOUT | POLLWRNORM;
2911                         break;
2912                 }
2913                 /* Fall through */
2914         case SNDRV_PCM_STATE_DRAINING:
2915                 mask = 0;
2916                 break;
2917         default:
2918                 mask = POLLOUT | POLLWRNORM | POLLERR;
2919                 break;
2920         }
2921         snd_pcm_stream_unlock_irq(substream);
2922         return mask;
2923 }
2924
2925 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2926 {
2927         struct snd_pcm_file *pcm_file;
2928         struct snd_pcm_substream *substream;
2929         struct snd_pcm_runtime *runtime;
2930         unsigned int mask;
2931         snd_pcm_uframes_t avail;
2932
2933         pcm_file = file->private_data;
2934
2935         substream = pcm_file->substream;
2936         if (PCM_RUNTIME_CHECK(substream))
2937                 return -ENXIO;
2938         runtime = substream->runtime;
2939
2940         poll_wait(file, &runtime->sleep, wait);
2941
2942         snd_pcm_stream_lock_irq(substream);
2943         avail = snd_pcm_capture_avail(runtime);
2944         switch (runtime->status->state) {
2945         case SNDRV_PCM_STATE_RUNNING:
2946         case SNDRV_PCM_STATE_PREPARED:
2947         case SNDRV_PCM_STATE_PAUSED:
2948                 if (avail >= runtime->control->avail_min) {
2949                         mask = POLLIN | POLLRDNORM;
2950                         break;
2951                 }
2952                 mask = 0;
2953                 break;
2954         case SNDRV_PCM_STATE_DRAINING:
2955                 if (avail > 0) {
2956                         mask = POLLIN | POLLRDNORM;
2957                         break;
2958                 }
2959                 /* Fall through */
2960         default:
2961                 mask = POLLIN | POLLRDNORM | POLLERR;
2962                 break;
2963         }
2964         snd_pcm_stream_unlock_irq(substream);
2965         return mask;
2966 }
2967
2968 /*
2969  * mmap support
2970  */
2971
2972 /*
2973  * Only on coherent architectures, we can mmap the status and the control records
2974  * for effcient data transfer.  On others, we have to use HWSYNC ioctl...
2975  */
2976 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2977 /*
2978  * mmap status record
2979  */
2980 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2981                                                 struct vm_fault *vmf)
2982 {
2983         struct snd_pcm_substream *substream = area->vm_private_data;
2984         struct snd_pcm_runtime *runtime;
2985         
2986         if (substream == NULL)
2987                 return VM_FAULT_SIGBUS;
2988         runtime = substream->runtime;
2989         vmf->page = virt_to_page(runtime->status);
2990         get_page(vmf->page);
2991         return 0;
2992 }
2993
2994 static const struct vm_operations_struct snd_pcm_vm_ops_status =
2995 {
2996         .fault =        snd_pcm_mmap_status_fault,
2997 };
2998
2999 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3000                                struct vm_area_struct *area)
3001 {
3002         struct snd_pcm_runtime *runtime;
3003         long size;
3004         if (!(area->vm_flags & VM_READ))
3005                 return -EINVAL;
3006         runtime = substream->runtime;
3007         size = area->vm_end - area->vm_start;
3008         if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3009                 return -EINVAL;
3010         area->vm_ops = &snd_pcm_vm_ops_status;
3011         area->vm_private_data = substream;
3012         area->vm_flags |= VM_RESERVED;
3013         return 0;
3014 }
3015
3016 /*
3017  * mmap control record
3018  */
3019 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3020                                                 struct vm_fault *vmf)
3021 {
3022         struct snd_pcm_substream *substream = area->vm_private_data;
3023         struct snd_pcm_runtime *runtime;
3024         
3025         if (substream == NULL)
3026                 return VM_FAULT_SIGBUS;
3027         runtime = substream->runtime;
3028         vmf->page = virt_to_page(runtime->control);
3029         get_page(vmf->page);
3030         return 0;
3031 }
3032
3033 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3034 {
3035         .fault =        snd_pcm_mmap_control_fault,
3036 };
3037
3038 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3039                                 struct vm_area_struct *area)
3040 {
3041         struct snd_pcm_runtime *runtime;
3042         long size;
3043         if (!(area->vm_flags & VM_READ))
3044                 return -EINVAL;
3045         runtime = substream->runtime;
3046         size = area->vm_end - area->vm_start;
3047         if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3048                 return -EINVAL;
3049         area->vm_ops = &snd_pcm_vm_ops_control;
3050         area->vm_private_data = substream;
3051         area->vm_flags |= VM_RESERVED;
3052         return 0;
3053 }
3054 #else /* ! coherent mmap */
3055 /*
3056  * don't support mmap for status and control records.
3057  */
3058 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3059                                struct vm_area_struct *area)
3060 {
3061         return -ENXIO;
3062 }
3063 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3064                                 struct vm_area_struct *area)
3065 {
3066         return -ENXIO;
3067 }
3068 #endif /* coherent mmap */
3069
3070 static inline struct page *
3071 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3072 {
3073         void *vaddr = substream->runtime->dma_area + ofs;
3074 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3075         if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3076                 return virt_to_page(CAC_ADDR(vaddr));
3077 #endif
3078 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3079         if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3080                 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3081                 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3082                 /* assume dma_handle set via pfn_to_phys() in
3083                  * mm/dma-noncoherent.c
3084                  */
3085                 return pfn_to_page(addr >> PAGE_SHIFT);
3086         }
3087 #endif
3088         return virt_to_page(vaddr);
3089 }
3090
3091 /*
3092  * fault callback for mmapping a RAM page
3093  */
3094 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3095                                                 struct vm_fault *vmf)
3096 {
3097         struct snd_pcm_substream *substream = area->vm_private_data;
3098         struct snd_pcm_runtime *runtime;
3099         unsigned long offset;
3100         struct page * page;
3101         size_t dma_bytes;
3102         
3103         if (substream == NULL)
3104                 return VM_FAULT_SIGBUS;
3105         runtime = substream->runtime;
3106         offset = vmf->pgoff << PAGE_SHIFT;
3107         dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3108         if (offset > dma_bytes - PAGE_SIZE)
3109                 return VM_FAULT_SIGBUS;
3110         if (substream->ops->page)
3111                 page = substream->ops->page(substream, offset);
3112         else
3113                 page = snd_pcm_default_page_ops(substream, offset);
3114         if (!page)
3115                 return VM_FAULT_SIGBUS;
3116         get_page(page);
3117         vmf->page = page;
3118         return 0;
3119 }
3120
3121 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3122         .open =         snd_pcm_mmap_data_open,
3123         .close =        snd_pcm_mmap_data_close,
3124 };
3125
3126 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3127         .open =         snd_pcm_mmap_data_open,
3128         .close =        snd_pcm_mmap_data_close,
3129         .fault =        snd_pcm_mmap_data_fault,
3130 };
3131
3132 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3133 /* This should be defined / handled globally! */
3134 #ifdef CONFIG_ARM
3135 #define ARCH_HAS_DMA_MMAP_COHERENT
3136 #endif
3137 #endif
3138
3139 /*
3140  * mmap the DMA buffer on RAM
3141  */
3142 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3143                                 struct vm_area_struct *area)
3144 {
3145         area->vm_flags |= VM_RESERVED;
3146 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3147         if (!substream->ops->page &&
3148             substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3149                 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3150                                          area,
3151                                          substream->runtime->dma_area,
3152                                          substream->runtime->dma_addr,
3153                                          area->vm_end - area->vm_start);
3154 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3155         /* mmap with fault handler */
3156         area->vm_ops = &snd_pcm_vm_ops_data_fault;
3157         return 0;
3158 }
3159
3160 /*
3161  * mmap the DMA buffer on I/O memory area
3162  */
3163 #if SNDRV_PCM_INFO_MMAP_IOMEM
3164 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3165                            struct vm_area_struct *area)
3166 {
3167         long size;
3168         unsigned long offset;
3169
3170 #ifdef pgprot_noncached
3171         area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3172 #endif
3173         area->vm_flags |= VM_IO;
3174         size = area->vm_end - area->vm_start;
3175         offset = area->vm_pgoff << PAGE_SHIFT;
3176         if (io_remap_pfn_range(area, area->vm_start,
3177                                 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3178                                 size, area->vm_page_prot))
3179                 return -EAGAIN;
3180         return 0;
3181 }
3182
3183 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3184 #endif /* SNDRV_PCM_INFO_MMAP */
3185
3186 /*
3187  * mmap DMA buffer
3188  */
3189 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3190                       struct vm_area_struct *area)
3191 {
3192         struct snd_pcm_runtime *runtime;
3193         long size;
3194         unsigned long offset;
3195         size_t dma_bytes;
3196         int err;
3197
3198         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3199                 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3200                         return -EINVAL;
3201         } else {
3202                 if (!(area->vm_flags & VM_READ))
3203                         return -EINVAL;
3204         }
3205         runtime = substream->runtime;
3206         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3207                 return -EBADFD;
3208         if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3209                 return -ENXIO;
3210         if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3211             runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3212                 return -EINVAL;
3213         size = area->vm_end - area->vm_start;
3214         offset = area->vm_pgoff << PAGE_SHIFT;
3215         dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3216         if ((size_t)size > dma_bytes)
3217                 return -EINVAL;
3218         if (offset > dma_bytes - size)
3219                 return -EINVAL;
3220
3221         area->vm_ops = &snd_pcm_vm_ops_data;
3222         area->vm_private_data = substream;
3223         if (substream->ops->mmap)
3224                 err = substream->ops->mmap(substream, area);
3225         else
3226                 err = snd_pcm_default_mmap(substream, area);
3227         if (!err)
3228                 atomic_inc(&substream->mmap_count);
3229         return err;
3230 }
3231
3232 EXPORT_SYMBOL(snd_pcm_mmap_data);
3233
3234 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3235 {
3236         struct snd_pcm_file * pcm_file;
3237         struct snd_pcm_substream *substream;    
3238         unsigned long offset;
3239         
3240         pcm_file = file->private_data;
3241         substream = pcm_file->substream;
3242         if (PCM_RUNTIME_CHECK(substream))
3243                 return -ENXIO;
3244
3245         offset = area->vm_pgoff << PAGE_SHIFT;
3246         switch (offset) {
3247         case SNDRV_PCM_MMAP_OFFSET_STATUS:
3248                 if (pcm_file->no_compat_mmap)
3249                         return -ENXIO;
3250                 return snd_pcm_mmap_status(substream, file, area);
3251         case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3252                 if (pcm_file->no_compat_mmap)
3253                         return -ENXIO;
3254                 return snd_pcm_mmap_control(substream, file, area);
3255         default:
3256                 return snd_pcm_mmap_data(substream, file, area);
3257         }
3258         return 0;
3259 }
3260
3261 static int snd_pcm_fasync(int fd, struct file * file, int on)
3262 {
3263         struct snd_pcm_file * pcm_file;
3264         struct snd_pcm_substream *substream;
3265         struct snd_pcm_runtime *runtime;
3266         int err = -ENXIO;
3267
3268         lock_kernel();
3269         pcm_file = file->private_data;
3270         substream = pcm_file->substream;
3271         if (PCM_RUNTIME_CHECK(substream))
3272                 goto out;
3273         runtime = substream->runtime;
3274         err = fasync_helper(fd, file, on, &runtime->fasync);
3275 out:
3276         unlock_kernel();
3277         return err;
3278 }
3279
3280 /*
3281  * ioctl32 compat
3282  */
3283 #ifdef CONFIG_COMPAT
3284 #include "pcm_compat.c"
3285 #else
3286 #define snd_pcm_ioctl_compat    NULL
3287 #endif
3288
3289 /*
3290  *  To be removed helpers to keep binary compatibility
3291  */
3292
3293 #ifdef CONFIG_SND_SUPPORT_OLD_API
3294 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3295 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3296
3297 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3298                                                struct snd_pcm_hw_params_old *oparams)
3299 {
3300         unsigned int i;
3301
3302         memset(params, 0, sizeof(*params));
3303         params->flags = oparams->flags;
3304         for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3305                 params->masks[i].bits[0] = oparams->masks[i];
3306         memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3307         params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3308         params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3309         params->info = oparams->info;
3310         params->msbits = oparams->msbits;
3311         params->rate_num = oparams->rate_num;
3312         params->rate_den = oparams->rate_den;
3313         params->fifo_size = oparams->fifo_size;
3314 }
3315
3316 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3317                                              struct snd_pcm_hw_params *params)
3318 {
3319         unsigned int i;
3320
3321         memset(oparams, 0, sizeof(*oparams));
3322         oparams->flags = params->flags;
3323         for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3324                 oparams->masks[i] = params->masks[i].bits[0];
3325         memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3326         oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3327         oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3328         oparams->info = params->info;
3329         oparams->msbits = params->msbits;
3330         oparams->rate_num = params->rate_num;
3331         oparams->rate_den = params->rate_den;
3332         oparams->fifo_size = params->fifo_size;
3333 }
3334
3335 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3336                                       struct snd_pcm_hw_params_old __user * _oparams)
3337 {
3338         struct snd_pcm_hw_params *params;
3339         struct snd_pcm_hw_params_old *oparams = NULL;
3340         int err;
3341
3342         params = kmalloc(sizeof(*params), GFP_KERNEL);
3343         if (!params)
3344                 return -ENOMEM;
3345
3346         oparams = memdup_user(_oparams, sizeof(*oparams));
3347         if (IS_ERR(oparams)) {
3348                 err = PTR_ERR(oparams);
3349                 goto out;
3350         }
3351         snd_pcm_hw_convert_from_old_params(params, oparams);
3352         err = snd_pcm_hw_refine(substream, params);
3353         snd_pcm_hw_convert_to_old_params(oparams, params);
3354         if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3355                 if (!err)
3356                         err = -EFAULT;
3357         }
3358
3359         kfree(oparams);
3360 out:
3361         kfree(params);
3362         return err;
3363 }
3364
3365 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3366                                       struct snd_pcm_hw_params_old __user * _oparams)
3367 {
3368         struct snd_pcm_hw_params *params;
3369         struct snd_pcm_hw_params_old *oparams = NULL;
3370         int err;
3371
3372         params = kmalloc(sizeof(*params), GFP_KERNEL);
3373         if (!params)
3374                 return -ENOMEM;
3375
3376         oparams = memdup_user(_oparams, sizeof(*oparams));
3377         if (IS_ERR(oparams)) {
3378                 err = PTR_ERR(oparams);
3379                 goto out;
3380         }
3381         snd_pcm_hw_convert_from_old_params(params, oparams);
3382         err = snd_pcm_hw_params(substream, params);
3383         snd_pcm_hw_convert_to_old_params(oparams, params);
3384         if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3385                 if (!err)
3386                         err = -EFAULT;
3387         }
3388
3389         kfree(oparams);
3390 out:
3391         kfree(params);
3392         return err;
3393 }
3394 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3395
3396 #ifndef CONFIG_MMU
3397 unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3398                                       unsigned long len, unsigned long pgoff,
3399                                       unsigned long flags)
3400 {
3401         return 0;
3402 }
3403 #else
3404 # define dummy_get_unmapped_area NULL
3405 #endif
3406
3407 /*
3408  *  Register section
3409  */
3410
3411 const struct file_operations snd_pcm_f_ops[2] = {
3412         {
3413                 .owner =                THIS_MODULE,
3414                 .write =                snd_pcm_write,
3415                 .aio_write =            snd_pcm_aio_write,
3416                 .open =                 snd_pcm_playback_open,
3417                 .release =              snd_pcm_release,
3418                 .poll =                 snd_pcm_playback_poll,
3419                 .unlocked_ioctl =       snd_pcm_playback_ioctl,
3420                 .compat_ioctl =         snd_pcm_ioctl_compat,
3421                 .mmap =                 snd_pcm_mmap,
3422                 .fasync =               snd_pcm_fasync,
3423                 .get_unmapped_area =    dummy_get_unmapped_area,
3424         },
3425         {
3426                 .owner =                THIS_MODULE,
3427                 .read =                 snd_pcm_read,
3428                 .aio_read =             snd_pcm_aio_read,
3429                 .open =                 snd_pcm_capture_open,
3430                 .release =              snd_pcm_release,
3431                 .poll =                 snd_pcm_capture_poll,
3432                 .unlocked_ioctl =       snd_pcm_capture_ioctl,
3433                 .compat_ioctl =         snd_pcm_ioctl_compat,
3434                 .mmap =                 snd_pcm_mmap,
3435                 .fasync =               snd_pcm_fasync,
3436                 .get_unmapped_area =    dummy_get_unmapped_area,
3437         }
3438 };