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