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