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