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