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