OSDN Git Service

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