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