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