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