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