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