GNU Linux-libre 4.9.330-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         snd_pcm_stream_lock_irq(substream);
1493         runtime->hw_ptr_base = 0;
1494         runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1495                 runtime->status->hw_ptr % runtime->period_size;
1496         runtime->silence_start = runtime->status->hw_ptr;
1497         runtime->silence_filled = 0;
1498         snd_pcm_stream_unlock_irq(substream);
1499         return 0;
1500 }
1501
1502 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1503 {
1504         struct snd_pcm_runtime *runtime = substream->runtime;
1505         snd_pcm_stream_lock_irq(substream);
1506         runtime->control->appl_ptr = runtime->status->hw_ptr;
1507         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1508             runtime->silence_size > 0)
1509                 snd_pcm_playback_silence(substream, ULONG_MAX);
1510         snd_pcm_stream_unlock_irq(substream);
1511 }
1512
1513 static const struct action_ops snd_pcm_action_reset = {
1514         .pre_action = snd_pcm_pre_reset,
1515         .do_action = snd_pcm_do_reset,
1516         .post_action = snd_pcm_post_reset
1517 };
1518
1519 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1520 {
1521         return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1522 }
1523
1524 /*
1525  * prepare ioctl
1526  */
1527 /* we use the second argument for updating f_flags */
1528 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1529                                int f_flags)
1530 {
1531         struct snd_pcm_runtime *runtime = substream->runtime;
1532         if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1533             runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1534                 return -EBADFD;
1535         if (snd_pcm_running(substream))
1536                 return -EBUSY;
1537         substream->f_flags = f_flags;
1538         return 0;
1539 }
1540
1541 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1542 {
1543         int err;
1544         err = substream->ops->prepare(substream);
1545         if (err < 0)
1546                 return err;
1547         return snd_pcm_do_reset(substream, 0);
1548 }
1549
1550 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1551 {
1552         struct snd_pcm_runtime *runtime = substream->runtime;
1553         runtime->control->appl_ptr = runtime->status->hw_ptr;
1554         snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1555 }
1556
1557 static const struct action_ops snd_pcm_action_prepare = {
1558         .pre_action = snd_pcm_pre_prepare,
1559         .do_action = snd_pcm_do_prepare,
1560         .post_action = snd_pcm_post_prepare
1561 };
1562
1563 /**
1564  * snd_pcm_prepare - prepare the PCM substream to be triggerable
1565  * @substream: the PCM substream instance
1566  * @file: file to refer f_flags
1567  *
1568  * Return: Zero if successful, or a negative error code.
1569  */
1570 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1571                            struct file *file)
1572 {
1573         int res;
1574         struct snd_card *card = substream->pcm->card;
1575         int f_flags;
1576
1577         if (file)
1578                 f_flags = file->f_flags;
1579         else
1580                 f_flags = substream->f_flags;
1581
1582         snd_power_lock(card);
1583         if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1584                 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1585                                                substream, f_flags);
1586         snd_power_unlock(card);
1587         return res;
1588 }
1589
1590 /*
1591  * drain ioctl
1592  */
1593
1594 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1595 {
1596         struct snd_pcm_runtime *runtime = substream->runtime;
1597         switch (runtime->status->state) {
1598         case SNDRV_PCM_STATE_OPEN:
1599         case SNDRV_PCM_STATE_DISCONNECTED:
1600         case SNDRV_PCM_STATE_SUSPENDED:
1601                 return -EBADFD;
1602         }
1603         runtime->trigger_master = substream;
1604         return 0;
1605 }
1606
1607 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1608 {
1609         struct snd_pcm_runtime *runtime = substream->runtime;
1610         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1611                 switch (runtime->status->state) {
1612                 case SNDRV_PCM_STATE_PREPARED:
1613                         /* start playback stream if possible */
1614                         if (! snd_pcm_playback_empty(substream)) {
1615                                 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1616                                 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1617                         } else {
1618                                 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1619                         }
1620                         break;
1621                 case SNDRV_PCM_STATE_RUNNING:
1622                         runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1623                         break;
1624                 case SNDRV_PCM_STATE_XRUN:
1625                         runtime->status->state = SNDRV_PCM_STATE_SETUP;
1626                         break;
1627                 default:
1628                         break;
1629                 }
1630         } else {
1631                 /* stop running stream */
1632                 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1633                         int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1634                                 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1635                         snd_pcm_do_stop(substream, new_state);
1636                         snd_pcm_post_stop(substream, new_state);
1637                 }
1638         }
1639
1640         if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1641             runtime->trigger_master == substream &&
1642             (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1643                 return substream->ops->trigger(substream,
1644                                                SNDRV_PCM_TRIGGER_DRAIN);
1645
1646         return 0;
1647 }
1648
1649 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1650 {
1651 }
1652
1653 static const struct action_ops snd_pcm_action_drain_init = {
1654         .pre_action = snd_pcm_pre_drain_init,
1655         .do_action = snd_pcm_do_drain_init,
1656         .post_action = snd_pcm_post_drain_init
1657 };
1658
1659 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1660
1661 /*
1662  * Drain the stream(s).
1663  * When the substream is linked, sync until the draining of all playback streams
1664  * is finished.
1665  * After this call, all streams are supposed to be either SETUP or DRAINING
1666  * (capture only) state.
1667  */
1668 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1669                          struct file *file)
1670 {
1671         struct snd_card *card;
1672         struct snd_pcm_runtime *runtime;
1673         struct snd_pcm_substream *s;
1674         wait_queue_t wait;
1675         int result = 0;
1676         int nonblock = 0;
1677
1678         card = substream->pcm->card;
1679         runtime = substream->runtime;
1680
1681         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1682                 return -EBADFD;
1683
1684         snd_power_lock(card);
1685         if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1686                 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1687                 if (result < 0) {
1688                         snd_power_unlock(card);
1689                         return result;
1690                 }
1691         }
1692
1693         if (file) {
1694                 if (file->f_flags & O_NONBLOCK)
1695                         nonblock = 1;
1696         } else if (substream->f_flags & O_NONBLOCK)
1697                 nonblock = 1;
1698
1699         down_read(&snd_pcm_link_rwsem);
1700         snd_pcm_stream_lock_irq(substream);
1701         /* resume pause */
1702         if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1703                 snd_pcm_pause(substream, 0);
1704
1705         /* pre-start/stop - all running streams are changed to DRAINING state */
1706         result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1707         if (result < 0)
1708                 goto unlock;
1709         /* in non-blocking, we don't wait in ioctl but let caller poll */
1710         if (nonblock) {
1711                 result = -EAGAIN;
1712                 goto unlock;
1713         }
1714
1715         for (;;) {
1716                 long tout;
1717                 struct snd_pcm_runtime *to_check;
1718                 if (signal_pending(current)) {
1719                         result = -ERESTARTSYS;
1720                         break;
1721                 }
1722                 /* find a substream to drain */
1723                 to_check = NULL;
1724                 snd_pcm_group_for_each_entry(s, substream) {
1725                         if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1726                                 continue;
1727                         runtime = s->runtime;
1728                         if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1729                                 to_check = runtime;
1730                                 break;
1731                         }
1732                 }
1733                 if (!to_check)
1734                         break; /* all drained */
1735                 init_waitqueue_entry(&wait, current);
1736                 add_wait_queue(&to_check->sleep, &wait);
1737                 snd_pcm_stream_unlock_irq(substream);
1738                 up_read(&snd_pcm_link_rwsem);
1739                 snd_power_unlock(card);
1740                 if (runtime->no_period_wakeup)
1741                         tout = MAX_SCHEDULE_TIMEOUT;
1742                 else {
1743                         tout = 10;
1744                         if (runtime->rate) {
1745                                 long t = runtime->period_size * 2 / runtime->rate;
1746                                 tout = max(t, tout);
1747                         }
1748                         tout = msecs_to_jiffies(tout * 1000);
1749                 }
1750                 tout = schedule_timeout_interruptible(tout);
1751                 snd_power_lock(card);
1752                 down_read(&snd_pcm_link_rwsem);
1753                 snd_pcm_stream_lock_irq(substream);
1754                 remove_wait_queue(&to_check->sleep, &wait);
1755                 if (card->shutdown) {
1756                         result = -ENODEV;
1757                         break;
1758                 }
1759                 if (tout == 0) {
1760                         if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1761                                 result = -ESTRPIPE;
1762                         else {
1763                                 dev_dbg(substream->pcm->card->dev,
1764                                         "playback drain error (DMA or IRQ trouble?)\n");
1765                                 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1766                                 result = -EIO;
1767                         }
1768                         break;
1769                 }
1770         }
1771
1772  unlock:
1773         snd_pcm_stream_unlock_irq(substream);
1774         up_read(&snd_pcm_link_rwsem);
1775         snd_power_unlock(card);
1776
1777         return result;
1778 }
1779
1780 /*
1781  * drop ioctl
1782  *
1783  * Immediately put all linked substreams into SETUP state.
1784  */
1785 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1786 {
1787         struct snd_pcm_runtime *runtime;
1788         int result = 0;
1789         
1790         if (PCM_RUNTIME_CHECK(substream))
1791                 return -ENXIO;
1792         runtime = substream->runtime;
1793
1794         if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1795             runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1796             runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1797                 return -EBADFD;
1798
1799         snd_pcm_stream_lock_irq(substream);
1800         /* resume pause */
1801         if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1802                 snd_pcm_pause(substream, 0);
1803
1804         snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1805         /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1806         snd_pcm_stream_unlock_irq(substream);
1807
1808         return result;
1809 }
1810
1811
1812 static bool is_pcm_file(struct file *file)
1813 {
1814         struct inode *inode = file_inode(file);
1815         unsigned int minor;
1816
1817         if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1818                 return false;
1819         minor = iminor(inode);
1820         return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1821                 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1822 }
1823
1824 /*
1825  * PCM link handling
1826  */
1827 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1828 {
1829         int res = 0;
1830         struct snd_pcm_file *pcm_file;
1831         struct snd_pcm_substream *substream1;
1832         struct snd_pcm_group *group;
1833         struct fd f = fdget(fd);
1834
1835         if (!f.file)
1836                 return -EBADFD;
1837         if (!is_pcm_file(f.file)) {
1838                 res = -EBADFD;
1839                 goto _badf;
1840         }
1841         pcm_file = f.file->private_data;
1842         substream1 = pcm_file->substream;
1843         if (substream == substream1) {
1844                 res = -EINVAL;
1845                 goto _badf;
1846         }
1847
1848         group = kmalloc(sizeof(*group), GFP_KERNEL);
1849         if (!group) {
1850                 res = -ENOMEM;
1851                 goto _nolock;
1852         }
1853         down_write_nonfifo(&snd_pcm_link_rwsem);
1854         write_lock_irq(&snd_pcm_link_rwlock);
1855         if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1856             substream->runtime->status->state != substream1->runtime->status->state ||
1857             substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1858                 res = -EBADFD;
1859                 goto _end;
1860         }
1861         if (snd_pcm_stream_linked(substream1)) {
1862                 res = -EALREADY;
1863                 goto _end;
1864         }
1865         if (!snd_pcm_stream_linked(substream)) {
1866                 substream->group = group;
1867                 group = NULL;
1868                 spin_lock_init(&substream->group->lock);
1869                 mutex_init(&substream->group->mutex);
1870                 INIT_LIST_HEAD(&substream->group->substreams);
1871                 list_add_tail(&substream->link_list, &substream->group->substreams);
1872                 substream->group->count = 1;
1873         }
1874         list_add_tail(&substream1->link_list, &substream->group->substreams);
1875         substream->group->count++;
1876         substream1->group = substream->group;
1877  _end:
1878         write_unlock_irq(&snd_pcm_link_rwlock);
1879         up_write(&snd_pcm_link_rwsem);
1880  _nolock:
1881         snd_card_unref(substream1->pcm->card);
1882         kfree(group);
1883  _badf:
1884         fdput(f);
1885         return res;
1886 }
1887
1888 static void relink_to_local(struct snd_pcm_substream *substream)
1889 {
1890         substream->group = &substream->self_group;
1891         INIT_LIST_HEAD(&substream->self_group.substreams);
1892         list_add_tail(&substream->link_list, &substream->self_group.substreams);
1893 }
1894
1895 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1896 {
1897         struct snd_pcm_substream *s;
1898         int res = 0;
1899
1900         down_write_nonfifo(&snd_pcm_link_rwsem);
1901         write_lock_irq(&snd_pcm_link_rwlock);
1902         if (!snd_pcm_stream_linked(substream)) {
1903                 res = -EALREADY;
1904                 goto _end;
1905         }
1906         list_del(&substream->link_list);
1907         substream->group->count--;
1908         if (substream->group->count == 1) {     /* detach the last stream, too */
1909                 snd_pcm_group_for_each_entry(s, substream) {
1910                         relink_to_local(s);
1911                         break;
1912                 }
1913                 kfree(substream->group);
1914         }
1915         relink_to_local(substream);
1916        _end:
1917         write_unlock_irq(&snd_pcm_link_rwlock);
1918         up_write(&snd_pcm_link_rwsem);
1919         return res;
1920 }
1921
1922 /*
1923  * hw configurator
1924  */
1925 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1926                                struct snd_pcm_hw_rule *rule)
1927 {
1928         struct snd_interval t;
1929         snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1930                      hw_param_interval_c(params, rule->deps[1]), &t);
1931         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1932 }
1933
1934 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1935                                struct snd_pcm_hw_rule *rule)
1936 {
1937         struct snd_interval t;
1938         snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1939                      hw_param_interval_c(params, rule->deps[1]), &t);
1940         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1941 }
1942
1943 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1944                                    struct snd_pcm_hw_rule *rule)
1945 {
1946         struct snd_interval t;
1947         snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1948                          hw_param_interval_c(params, rule->deps[1]),
1949                          (unsigned long) rule->private, &t);
1950         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1951 }
1952
1953 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1954                                    struct snd_pcm_hw_rule *rule)
1955 {
1956         struct snd_interval t;
1957         snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1958                          (unsigned long) rule->private,
1959                          hw_param_interval_c(params, rule->deps[1]), &t);
1960         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1961 }
1962
1963 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1964                                   struct snd_pcm_hw_rule *rule)
1965 {
1966         unsigned int k;
1967         struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1968         struct snd_mask m;
1969         struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1970         snd_mask_any(&m);
1971         for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1972                 int bits;
1973                 if (! snd_mask_test(mask, k))
1974                         continue;
1975                 bits = snd_pcm_format_physical_width(k);
1976                 if (bits <= 0)
1977                         continue; /* ignore invalid formats */
1978                 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1979                         snd_mask_reset(&m, k);
1980         }
1981         return snd_mask_refine(mask, &m);
1982 }
1983
1984 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1985                                        struct snd_pcm_hw_rule *rule)
1986 {
1987         struct snd_interval t;
1988         unsigned int k;
1989         t.min = UINT_MAX;
1990         t.max = 0;
1991         t.openmin = 0;
1992         t.openmax = 0;
1993         for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1994                 int bits;
1995                 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1996                         continue;
1997                 bits = snd_pcm_format_physical_width(k);
1998                 if (bits <= 0)
1999                         continue; /* ignore invalid formats */
2000                 if (t.min > (unsigned)bits)
2001                         t.min = bits;
2002                 if (t.max < (unsigned)bits)
2003                         t.max = bits;
2004         }
2005         t.integer = 1;
2006         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2007 }
2008
2009 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2010 #error "Change this table"
2011 #endif
2012
2013 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2014                                  48000, 64000, 88200, 96000, 176400, 192000 };
2015
2016 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2017         .count = ARRAY_SIZE(rates),
2018         .list = rates,
2019 };
2020
2021 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2022                                 struct snd_pcm_hw_rule *rule)
2023 {
2024         struct snd_pcm_hardware *hw = rule->private;
2025         return snd_interval_list(hw_param_interval(params, rule->var),
2026                                  snd_pcm_known_rates.count,
2027                                  snd_pcm_known_rates.list, hw->rates);
2028 }               
2029
2030 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2031                                             struct snd_pcm_hw_rule *rule)
2032 {
2033         struct snd_interval t;
2034         struct snd_pcm_substream *substream = rule->private;
2035         t.min = 0;
2036         t.max = substream->buffer_bytes_max;
2037         t.openmin = 0;
2038         t.openmax = 0;
2039         t.integer = 1;
2040         return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2041 }               
2042
2043 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2044 {
2045         struct snd_pcm_runtime *runtime = substream->runtime;
2046         struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2047         int k, err;
2048
2049         for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2050                 snd_mask_any(constrs_mask(constrs, k));
2051         }
2052
2053         for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2054                 snd_interval_any(constrs_interval(constrs, k));
2055         }
2056
2057         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2058         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2059         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2060         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2061         snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2062
2063         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2064                                    snd_pcm_hw_rule_format, NULL,
2065                                    SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2066         if (err < 0)
2067                 return err;
2068         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 
2069                                   snd_pcm_hw_rule_sample_bits, NULL,
2070                                   SNDRV_PCM_HW_PARAM_FORMAT, 
2071                                   SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2072         if (err < 0)
2073                 return err;
2074         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 
2075                                   snd_pcm_hw_rule_div, NULL,
2076                                   SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2077         if (err < 0)
2078                 return err;
2079         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
2080                                   snd_pcm_hw_rule_mul, NULL,
2081                                   SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2082         if (err < 0)
2083                 return err;
2084         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
2085                                   snd_pcm_hw_rule_mulkdiv, (void*) 8,
2086                                   SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2087         if (err < 0)
2088                 return err;
2089         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
2090                                   snd_pcm_hw_rule_mulkdiv, (void*) 8,
2091                                   SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2092         if (err < 0)
2093                 return err;
2094         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 
2095                                   snd_pcm_hw_rule_div, NULL,
2096                                   SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2097         if (err < 0)
2098                 return err;
2099         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
2100                                   snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2101                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2102         if (err < 0)
2103                 return err;
2104         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
2105                                   snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2106                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2107         if (err < 0)
2108                 return err;
2109         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, 
2110                                   snd_pcm_hw_rule_div, NULL,
2111                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2112         if (err < 0)
2113                 return err;
2114         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
2115                                   snd_pcm_hw_rule_div, NULL,
2116                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2117         if (err < 0)
2118                 return err;
2119         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
2120                                   snd_pcm_hw_rule_mulkdiv, (void*) 8,
2121                                   SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2122         if (err < 0)
2123                 return err;
2124         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
2125                                   snd_pcm_hw_rule_muldivk, (void*) 1000000,
2126                                   SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2127         if (err < 0)
2128                 return err;
2129         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
2130                                   snd_pcm_hw_rule_mul, NULL,
2131                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2132         if (err < 0)
2133                 return err;
2134         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
2135                                   snd_pcm_hw_rule_mulkdiv, (void*) 8,
2136                                   SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2137         if (err < 0)
2138                 return err;
2139         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
2140                                   snd_pcm_hw_rule_muldivk, (void*) 1000000,
2141                                   SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2142         if (err < 0)
2143                 return err;
2144         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 
2145                                   snd_pcm_hw_rule_muldivk, (void*) 8,
2146                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2147         if (err < 0)
2148                 return err;
2149         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 
2150                                   snd_pcm_hw_rule_muldivk, (void*) 8,
2151                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2152         if (err < 0)
2153                 return err;
2154         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 
2155                                   snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2156                                   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2157         if (err < 0)
2158                 return err;
2159         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 
2160                                   snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2161                                   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2162         if (err < 0)
2163                 return err;
2164         return 0;
2165 }
2166
2167 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2168 {
2169         struct snd_pcm_runtime *runtime = substream->runtime;
2170         struct snd_pcm_hardware *hw = &runtime->hw;
2171         int err;
2172         unsigned int mask = 0;
2173
2174         if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2175                 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2176         if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2177                 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2178         if (hw_support_mmap(substream)) {
2179                 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2180                         mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2181                 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2182                         mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2183                 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2184                         mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2185         }
2186         err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2187         if (err < 0)
2188                 return err;
2189
2190         err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2191         if (err < 0)
2192                 return err;
2193
2194         err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2195         if (err < 0)
2196                 return err;
2197
2198         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2199                                            hw->channels_min, hw->channels_max);
2200         if (err < 0)
2201                 return err;
2202
2203         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2204                                            hw->rate_min, hw->rate_max);
2205         if (err < 0)
2206                 return err;
2207
2208         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2209                                            hw->period_bytes_min, hw->period_bytes_max);
2210         if (err < 0)
2211                 return err;
2212
2213         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2214                                            hw->periods_min, hw->periods_max);
2215         if (err < 0)
2216                 return err;
2217
2218         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2219                                            hw->period_bytes_min, hw->buffer_bytes_max);
2220         if (err < 0)
2221                 return err;
2222
2223         err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 
2224                                   snd_pcm_hw_rule_buffer_bytes_max, substream,
2225                                   SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2226         if (err < 0)
2227                 return err;
2228
2229         /* FIXME: remove */
2230         if (runtime->dma_bytes) {
2231                 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2232                 if (err < 0)
2233                         return err;
2234         }
2235
2236         if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2237                 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
2238                                           snd_pcm_hw_rule_rate, hw,
2239                                           SNDRV_PCM_HW_PARAM_RATE, -1);
2240                 if (err < 0)
2241                         return err;
2242         }
2243
2244         /* FIXME: this belong to lowlevel */
2245         snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2246
2247         return 0;
2248 }
2249
2250 static void pcm_release_private(struct snd_pcm_substream *substream)
2251 {
2252         if (snd_pcm_stream_linked(substream))
2253                 snd_pcm_unlink(substream);
2254 }
2255
2256 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2257 {
2258         substream->ref_count--;
2259         if (substream->ref_count > 0)
2260                 return;
2261
2262         snd_pcm_drop(substream);
2263         if (substream->hw_opened) {
2264                 if (substream->ops->hw_free &&
2265                     substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2266                         substream->ops->hw_free(substream);
2267                 substream->ops->close(substream);
2268                 substream->hw_opened = 0;
2269         }
2270         if (pm_qos_request_active(&substream->latency_pm_qos_req))
2271                 pm_qos_remove_request(&substream->latency_pm_qos_req);
2272         if (substream->pcm_release) {
2273                 substream->pcm_release(substream);
2274                 substream->pcm_release = NULL;
2275         }
2276         snd_pcm_detach_substream(substream);
2277 }
2278
2279 EXPORT_SYMBOL(snd_pcm_release_substream);
2280
2281 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2282                            struct file *file,
2283                            struct snd_pcm_substream **rsubstream)
2284 {
2285         struct snd_pcm_substream *substream;
2286         int err;
2287
2288         err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2289         if (err < 0)
2290                 return err;
2291         if (substream->ref_count > 1) {
2292                 *rsubstream = substream;
2293                 return 0;
2294         }
2295
2296         err = snd_pcm_hw_constraints_init(substream);
2297         if (err < 0) {
2298                 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2299                 goto error;
2300         }
2301
2302         if ((err = substream->ops->open(substream)) < 0)
2303                 goto error;
2304
2305         substream->hw_opened = 1;
2306
2307         err = snd_pcm_hw_constraints_complete(substream);
2308         if (err < 0) {
2309                 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2310                 goto error;
2311         }
2312
2313         *rsubstream = substream;
2314         return 0;
2315
2316  error:
2317         snd_pcm_release_substream(substream);
2318         return err;
2319 }
2320
2321 EXPORT_SYMBOL(snd_pcm_open_substream);
2322
2323 static int snd_pcm_open_file(struct file *file,
2324                              struct snd_pcm *pcm,
2325                              int stream)
2326 {
2327         struct snd_pcm_file *pcm_file;
2328         struct snd_pcm_substream *substream;
2329         int err;
2330
2331         err = snd_pcm_open_substream(pcm, stream, file, &substream);
2332         if (err < 0)
2333                 return err;
2334
2335         pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2336         if (pcm_file == NULL) {
2337                 snd_pcm_release_substream(substream);
2338                 return -ENOMEM;
2339         }
2340         pcm_file->substream = substream;
2341         if (substream->ref_count == 1) {
2342                 substream->file = pcm_file;
2343                 substream->pcm_release = pcm_release_private;
2344         }
2345         file->private_data = pcm_file;
2346
2347         return 0;
2348 }
2349
2350 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2351 {
2352         struct snd_pcm *pcm;
2353         int err = nonseekable_open(inode, file);
2354         if (err < 0)
2355                 return err;
2356         pcm = snd_lookup_minor_data(iminor(inode),
2357                                     SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2358         err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2359         if (pcm)
2360                 snd_card_unref(pcm->card);
2361         return err;
2362 }
2363
2364 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2365 {
2366         struct snd_pcm *pcm;
2367         int err = nonseekable_open(inode, file);
2368         if (err < 0)
2369                 return err;
2370         pcm = snd_lookup_minor_data(iminor(inode),
2371                                     SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2372         err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2373         if (pcm)
2374                 snd_card_unref(pcm->card);
2375         return err;
2376 }
2377
2378 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2379 {
2380         int err;
2381         wait_queue_t wait;
2382
2383         if (pcm == NULL) {
2384                 err = -ENODEV;
2385                 goto __error1;
2386         }
2387         err = snd_card_file_add(pcm->card, file);
2388         if (err < 0)
2389                 goto __error1;
2390         if (!try_module_get(pcm->card->module)) {
2391                 err = -EFAULT;
2392                 goto __error2;
2393         }
2394         init_waitqueue_entry(&wait, current);
2395         add_wait_queue(&pcm->open_wait, &wait);
2396         mutex_lock(&pcm->open_mutex);
2397         while (1) {
2398                 err = snd_pcm_open_file(file, pcm, stream);
2399                 if (err >= 0)
2400                         break;
2401                 if (err == -EAGAIN) {
2402                         if (file->f_flags & O_NONBLOCK) {
2403                                 err = -EBUSY;
2404                                 break;
2405                         }
2406                 } else
2407                         break;
2408                 set_current_state(TASK_INTERRUPTIBLE);
2409                 mutex_unlock(&pcm->open_mutex);
2410                 schedule();
2411                 mutex_lock(&pcm->open_mutex);
2412                 if (pcm->card->shutdown) {
2413                         err = -ENODEV;
2414                         break;
2415                 }
2416                 if (signal_pending(current)) {
2417                         err = -ERESTARTSYS;
2418                         break;
2419                 }
2420         }
2421         remove_wait_queue(&pcm->open_wait, &wait);
2422         mutex_unlock(&pcm->open_mutex);
2423         if (err < 0)
2424                 goto __error;
2425         return err;
2426
2427       __error:
2428         module_put(pcm->card->module);
2429       __error2:
2430         snd_card_file_remove(pcm->card, file);
2431       __error1:
2432         return err;
2433 }
2434
2435 static int snd_pcm_release(struct inode *inode, struct file *file)
2436 {
2437         struct snd_pcm *pcm;
2438         struct snd_pcm_substream *substream;
2439         struct snd_pcm_file *pcm_file;
2440
2441         pcm_file = file->private_data;
2442         substream = pcm_file->substream;
2443         if (snd_BUG_ON(!substream))
2444                 return -ENXIO;
2445         pcm = substream->pcm;
2446         mutex_lock(&pcm->open_mutex);
2447         snd_pcm_release_substream(substream);
2448         kfree(pcm_file);
2449         mutex_unlock(&pcm->open_mutex);
2450         wake_up(&pcm->open_wait);
2451         module_put(pcm->card->module);
2452         snd_card_file_remove(pcm->card, file);
2453         return 0;
2454 }
2455
2456 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2457                                                  snd_pcm_uframes_t frames)
2458 {
2459         struct snd_pcm_runtime *runtime = substream->runtime;
2460         snd_pcm_sframes_t appl_ptr;
2461         snd_pcm_sframes_t ret;
2462         snd_pcm_sframes_t hw_avail;
2463
2464         if (frames == 0)
2465                 return 0;
2466
2467         snd_pcm_stream_lock_irq(substream);
2468         switch (runtime->status->state) {
2469         case SNDRV_PCM_STATE_PREPARED:
2470                 break;
2471         case SNDRV_PCM_STATE_DRAINING:
2472         case SNDRV_PCM_STATE_RUNNING:
2473                 if (snd_pcm_update_hw_ptr(substream) >= 0)
2474                         break;
2475                 /* Fall through */
2476         case SNDRV_PCM_STATE_XRUN:
2477                 ret = -EPIPE;
2478                 goto __end;
2479         case SNDRV_PCM_STATE_SUSPENDED:
2480                 ret = -ESTRPIPE;
2481                 goto __end;
2482         default:
2483                 ret = -EBADFD;
2484                 goto __end;
2485         }
2486
2487         hw_avail = snd_pcm_playback_hw_avail(runtime);
2488         if (hw_avail <= 0) {
2489                 ret = 0;
2490                 goto __end;
2491         }
2492         if (frames > (snd_pcm_uframes_t)hw_avail)
2493                 frames = hw_avail;
2494         appl_ptr = runtime->control->appl_ptr - frames;
2495         if (appl_ptr < 0)
2496                 appl_ptr += runtime->boundary;
2497         runtime->control->appl_ptr = appl_ptr;
2498         ret = frames;
2499  __end:
2500         snd_pcm_stream_unlock_irq(substream);
2501         return ret;
2502 }
2503
2504 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2505                                                 snd_pcm_uframes_t frames)
2506 {
2507         struct snd_pcm_runtime *runtime = substream->runtime;
2508         snd_pcm_sframes_t appl_ptr;
2509         snd_pcm_sframes_t ret;
2510         snd_pcm_sframes_t hw_avail;
2511
2512         if (frames == 0)
2513                 return 0;
2514
2515         snd_pcm_stream_lock_irq(substream);
2516         switch (runtime->status->state) {
2517         case SNDRV_PCM_STATE_PREPARED:
2518         case SNDRV_PCM_STATE_DRAINING:
2519                 break;
2520         case SNDRV_PCM_STATE_RUNNING:
2521                 if (snd_pcm_update_hw_ptr(substream) >= 0)
2522                         break;
2523                 /* Fall through */
2524         case SNDRV_PCM_STATE_XRUN:
2525                 ret = -EPIPE;
2526                 goto __end;
2527         case SNDRV_PCM_STATE_SUSPENDED:
2528                 ret = -ESTRPIPE;
2529                 goto __end;
2530         default:
2531                 ret = -EBADFD;
2532                 goto __end;
2533         }
2534
2535         hw_avail = snd_pcm_capture_hw_avail(runtime);
2536         if (hw_avail <= 0) {
2537                 ret = 0;
2538                 goto __end;
2539         }
2540         if (frames > (snd_pcm_uframes_t)hw_avail)
2541                 frames = hw_avail;
2542         appl_ptr = runtime->control->appl_ptr - frames;
2543         if (appl_ptr < 0)
2544                 appl_ptr += runtime->boundary;
2545         runtime->control->appl_ptr = appl_ptr;
2546         ret = frames;
2547  __end:
2548         snd_pcm_stream_unlock_irq(substream);
2549         return ret;
2550 }
2551
2552 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2553                                                   snd_pcm_uframes_t frames)
2554 {
2555         struct snd_pcm_runtime *runtime = substream->runtime;
2556         snd_pcm_sframes_t appl_ptr;
2557         snd_pcm_sframes_t ret;
2558         snd_pcm_sframes_t avail;
2559
2560         if (frames == 0)
2561                 return 0;
2562
2563         snd_pcm_stream_lock_irq(substream);
2564         switch (runtime->status->state) {
2565         case SNDRV_PCM_STATE_PREPARED:
2566         case SNDRV_PCM_STATE_PAUSED:
2567                 break;
2568         case SNDRV_PCM_STATE_DRAINING:
2569         case SNDRV_PCM_STATE_RUNNING:
2570                 if (snd_pcm_update_hw_ptr(substream) >= 0)
2571                         break;
2572                 /* Fall through */
2573         case SNDRV_PCM_STATE_XRUN:
2574                 ret = -EPIPE;
2575                 goto __end;
2576         case SNDRV_PCM_STATE_SUSPENDED:
2577                 ret = -ESTRPIPE;
2578                 goto __end;
2579         default:
2580                 ret = -EBADFD;
2581                 goto __end;
2582         }
2583
2584         avail = snd_pcm_playback_avail(runtime);
2585         if (avail <= 0) {
2586                 ret = 0;
2587                 goto __end;
2588         }
2589         if (frames > (snd_pcm_uframes_t)avail)
2590                 frames = avail;
2591         appl_ptr = runtime->control->appl_ptr + frames;
2592         if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2593                 appl_ptr -= runtime->boundary;
2594         runtime->control->appl_ptr = appl_ptr;
2595         ret = frames;
2596  __end:
2597         snd_pcm_stream_unlock_irq(substream);
2598         return ret;
2599 }
2600
2601 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2602                                                  snd_pcm_uframes_t frames)
2603 {
2604         struct snd_pcm_runtime *runtime = substream->runtime;
2605         snd_pcm_sframes_t appl_ptr;
2606         snd_pcm_sframes_t ret;
2607         snd_pcm_sframes_t avail;
2608
2609         if (frames == 0)
2610                 return 0;
2611
2612         snd_pcm_stream_lock_irq(substream);
2613         switch (runtime->status->state) {
2614         case SNDRV_PCM_STATE_PREPARED:
2615         case SNDRV_PCM_STATE_DRAINING:
2616         case SNDRV_PCM_STATE_PAUSED:
2617                 break;
2618         case SNDRV_PCM_STATE_RUNNING:
2619                 if (snd_pcm_update_hw_ptr(substream) >= 0)
2620                         break;
2621                 /* Fall through */
2622         case SNDRV_PCM_STATE_XRUN:
2623                 ret = -EPIPE;
2624                 goto __end;
2625         case SNDRV_PCM_STATE_SUSPENDED:
2626                 ret = -ESTRPIPE;
2627                 goto __end;
2628         default:
2629                 ret = -EBADFD;
2630                 goto __end;
2631         }
2632
2633         avail = snd_pcm_capture_avail(runtime);
2634         if (avail <= 0) {
2635                 ret = 0;
2636                 goto __end;
2637         }
2638         if (frames > (snd_pcm_uframes_t)avail)
2639                 frames = avail;
2640         appl_ptr = runtime->control->appl_ptr + frames;
2641         if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2642                 appl_ptr -= runtime->boundary;
2643         runtime->control->appl_ptr = appl_ptr;
2644         ret = frames;
2645  __end:
2646         snd_pcm_stream_unlock_irq(substream);
2647         return ret;
2648 }
2649
2650 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2651 {
2652         struct snd_pcm_runtime *runtime = substream->runtime;
2653         int err;
2654
2655         snd_pcm_stream_lock_irq(substream);
2656         switch (runtime->status->state) {
2657         case SNDRV_PCM_STATE_DRAINING:
2658                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2659                         goto __badfd;
2660                 /* Fall through */
2661         case SNDRV_PCM_STATE_RUNNING:
2662                 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2663                         break;
2664                 /* Fall through */
2665         case SNDRV_PCM_STATE_PREPARED:
2666                 err = 0;
2667                 break;
2668         case SNDRV_PCM_STATE_SUSPENDED:
2669                 err = -ESTRPIPE;
2670                 break;
2671         case SNDRV_PCM_STATE_XRUN:
2672                 err = -EPIPE;
2673                 break;
2674         default:
2675               __badfd:
2676                 err = -EBADFD;
2677                 break;
2678         }
2679         snd_pcm_stream_unlock_irq(substream);
2680         return err;
2681 }
2682                 
2683 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2684                          snd_pcm_sframes_t __user *res)
2685 {
2686         struct snd_pcm_runtime *runtime = substream->runtime;
2687         int err;
2688         snd_pcm_sframes_t n = 0;
2689
2690         snd_pcm_stream_lock_irq(substream);
2691         switch (runtime->status->state) {
2692         case SNDRV_PCM_STATE_DRAINING:
2693                 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2694                         goto __badfd;
2695                 /* Fall through */
2696         case SNDRV_PCM_STATE_RUNNING:
2697                 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2698                         break;
2699                 /* Fall through */
2700         case SNDRV_PCM_STATE_PREPARED:
2701         case SNDRV_PCM_STATE_SUSPENDED:
2702                 err = 0;
2703                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2704                         n = snd_pcm_playback_hw_avail(runtime);
2705                 else
2706                         n = snd_pcm_capture_avail(runtime);
2707                 n += runtime->delay;
2708                 break;
2709         case SNDRV_PCM_STATE_XRUN:
2710                 err = -EPIPE;
2711                 break;
2712         default:
2713               __badfd:
2714                 err = -EBADFD;
2715                 break;
2716         }
2717         snd_pcm_stream_unlock_irq(substream);
2718         if (!err)
2719                 if (put_user(n, res))
2720                         err = -EFAULT;
2721         return err;
2722 }
2723                 
2724 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2725                             struct snd_pcm_sync_ptr __user *_sync_ptr)
2726 {
2727         struct snd_pcm_runtime *runtime = substream->runtime;
2728         struct snd_pcm_sync_ptr sync_ptr;
2729         volatile struct snd_pcm_mmap_status *status;
2730         volatile struct snd_pcm_mmap_control *control;
2731         int err;
2732
2733         memset(&sync_ptr, 0, sizeof(sync_ptr));
2734         if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2735                 return -EFAULT;
2736         if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2737                 return -EFAULT; 
2738         status = runtime->status;
2739         control = runtime->control;
2740         if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2741                 err = snd_pcm_hwsync(substream);
2742                 if (err < 0)
2743                         return err;
2744         }
2745         snd_pcm_stream_lock_irq(substream);
2746         if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2747                 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2748         else
2749                 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2750         if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2751                 control->avail_min = sync_ptr.c.control.avail_min;
2752         else
2753                 sync_ptr.c.control.avail_min = control->avail_min;
2754         sync_ptr.s.status.state = status->state;
2755         sync_ptr.s.status.hw_ptr = status->hw_ptr;
2756         sync_ptr.s.status.tstamp = status->tstamp;
2757         sync_ptr.s.status.suspended_state = status->suspended_state;
2758         sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2759         snd_pcm_stream_unlock_irq(substream);
2760         if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2761                 return -EFAULT;
2762         return 0;
2763 }
2764
2765 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2766 {
2767         struct snd_pcm_runtime *runtime = substream->runtime;
2768         int arg;
2769         
2770         if (get_user(arg, _arg))
2771                 return -EFAULT;
2772         if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2773                 return -EINVAL;
2774         runtime->tstamp_type = arg;
2775         return 0;
2776 }
2777                 
2778 static int snd_pcm_common_ioctl1(struct file *file,
2779                                  struct snd_pcm_substream *substream,
2780                                  unsigned int cmd, void __user *arg)
2781 {
2782         switch (cmd) {
2783         case SNDRV_PCM_IOCTL_PVERSION:
2784                 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2785         case SNDRV_PCM_IOCTL_INFO:
2786                 return snd_pcm_info_user(substream, arg);
2787         case SNDRV_PCM_IOCTL_TSTAMP:    /* just for compatibility */
2788                 return 0;
2789         case SNDRV_PCM_IOCTL_TTSTAMP:
2790                 return snd_pcm_tstamp(substream, arg);
2791         case SNDRV_PCM_IOCTL_HW_REFINE:
2792                 return snd_pcm_hw_refine_user(substream, arg);
2793         case SNDRV_PCM_IOCTL_HW_PARAMS:
2794                 return snd_pcm_hw_params_user(substream, arg);
2795         case SNDRV_PCM_IOCTL_HW_FREE:
2796                 return snd_pcm_hw_free(substream);
2797         case SNDRV_PCM_IOCTL_SW_PARAMS:
2798                 return snd_pcm_sw_params_user(substream, arg);
2799         case SNDRV_PCM_IOCTL_STATUS:
2800                 return snd_pcm_status_user(substream, arg, false);
2801         case SNDRV_PCM_IOCTL_STATUS_EXT:
2802                 return snd_pcm_status_user(substream, arg, true);
2803         case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2804                 return snd_pcm_channel_info_user(substream, arg);
2805         case SNDRV_PCM_IOCTL_PREPARE:
2806                 return snd_pcm_prepare(substream, file);
2807         case SNDRV_PCM_IOCTL_RESET:
2808                 return snd_pcm_reset(substream);
2809         case SNDRV_PCM_IOCTL_START:
2810                 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2811         case SNDRV_PCM_IOCTL_LINK:
2812                 return snd_pcm_link(substream, (int)(unsigned long) arg);
2813         case SNDRV_PCM_IOCTL_UNLINK:
2814                 return snd_pcm_unlink(substream);
2815         case SNDRV_PCM_IOCTL_RESUME:
2816                 return snd_pcm_resume(substream);
2817         case SNDRV_PCM_IOCTL_XRUN:
2818                 return snd_pcm_xrun(substream);
2819         case SNDRV_PCM_IOCTL_HWSYNC:
2820                 return snd_pcm_hwsync(substream);
2821         case SNDRV_PCM_IOCTL_DELAY:
2822                 return snd_pcm_delay(substream, arg);
2823         case SNDRV_PCM_IOCTL_SYNC_PTR:
2824                 return snd_pcm_sync_ptr(substream, arg);
2825 #ifdef CONFIG_SND_SUPPORT_OLD_API
2826         case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2827                 return snd_pcm_hw_refine_old_user(substream, arg);
2828         case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2829                 return snd_pcm_hw_params_old_user(substream, arg);
2830 #endif
2831         case SNDRV_PCM_IOCTL_DRAIN:
2832                 return snd_pcm_drain(substream, file);
2833         case SNDRV_PCM_IOCTL_DROP:
2834                 return snd_pcm_drop(substream);
2835         case SNDRV_PCM_IOCTL_PAUSE:
2836         {
2837                 int res;
2838                 snd_pcm_stream_lock_irq(substream);
2839                 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2840                 snd_pcm_stream_unlock_irq(substream);
2841                 return res;
2842         }
2843         }
2844         pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2845         return -ENOTTY;
2846 }
2847
2848 static int snd_pcm_playback_ioctl1(struct file *file,
2849                                    struct snd_pcm_substream *substream,
2850                                    unsigned int cmd, void __user *arg)
2851 {
2852         if (snd_BUG_ON(!substream))
2853                 return -ENXIO;
2854         if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2855                 return -EINVAL;
2856         switch (cmd) {
2857         case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2858         {
2859                 struct snd_xferi xferi;
2860                 struct snd_xferi __user *_xferi = arg;
2861                 struct snd_pcm_runtime *runtime = substream->runtime;
2862                 snd_pcm_sframes_t result;
2863                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2864                         return -EBADFD;
2865                 if (put_user(0, &_xferi->result))
2866                         return -EFAULT;
2867                 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2868                         return -EFAULT;
2869                 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2870                 __put_user(result, &_xferi->result);
2871                 return result < 0 ? result : 0;
2872         }
2873         case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2874         {
2875                 struct snd_xfern xfern;
2876                 struct snd_xfern __user *_xfern = arg;
2877                 struct snd_pcm_runtime *runtime = substream->runtime;
2878                 void __user **bufs;
2879                 snd_pcm_sframes_t result;
2880                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2881                         return -EBADFD;
2882                 if (runtime->channels > 128)
2883                         return -EINVAL;
2884                 if (put_user(0, &_xfern->result))
2885                         return -EFAULT;
2886                 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2887                         return -EFAULT;
2888
2889                 bufs = memdup_user(xfern.bufs,
2890                                    sizeof(void *) * runtime->channels);
2891                 if (IS_ERR(bufs))
2892                         return PTR_ERR(bufs);
2893                 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2894                 kfree(bufs);
2895                 __put_user(result, &_xfern->result);
2896                 return result < 0 ? result : 0;
2897         }
2898         case SNDRV_PCM_IOCTL_REWIND:
2899         {
2900                 snd_pcm_uframes_t frames;
2901                 snd_pcm_uframes_t __user *_frames = arg;
2902                 snd_pcm_sframes_t result;
2903                 if (get_user(frames, _frames))
2904                         return -EFAULT;
2905                 if (put_user(0, _frames))
2906                         return -EFAULT;
2907                 result = snd_pcm_playback_rewind(substream, frames);
2908                 __put_user(result, _frames);
2909                 return result < 0 ? result : 0;
2910         }
2911         case SNDRV_PCM_IOCTL_FORWARD:
2912         {
2913                 snd_pcm_uframes_t frames;
2914                 snd_pcm_uframes_t __user *_frames = arg;
2915                 snd_pcm_sframes_t result;
2916                 if (get_user(frames, _frames))
2917                         return -EFAULT;
2918                 if (put_user(0, _frames))
2919                         return -EFAULT;
2920                 result = snd_pcm_playback_forward(substream, frames);
2921                 __put_user(result, _frames);
2922                 return result < 0 ? result : 0;
2923         }
2924         }
2925         return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2926 }
2927
2928 static int snd_pcm_capture_ioctl1(struct file *file,
2929                                   struct snd_pcm_substream *substream,
2930                                   unsigned int cmd, void __user *arg)
2931 {
2932         if (snd_BUG_ON(!substream))
2933                 return -ENXIO;
2934         if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2935                 return -EINVAL;
2936         switch (cmd) {
2937         case SNDRV_PCM_IOCTL_READI_FRAMES:
2938         {
2939                 struct snd_xferi xferi;
2940                 struct snd_xferi __user *_xferi = arg;
2941                 struct snd_pcm_runtime *runtime = substream->runtime;
2942                 snd_pcm_sframes_t result;
2943                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2944                         return -EBADFD;
2945                 if (put_user(0, &_xferi->result))
2946                         return -EFAULT;
2947                 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2948                         return -EFAULT;
2949                 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2950                 __put_user(result, &_xferi->result);
2951                 return result < 0 ? result : 0;
2952         }
2953         case SNDRV_PCM_IOCTL_READN_FRAMES:
2954         {
2955                 struct snd_xfern xfern;
2956                 struct snd_xfern __user *_xfern = arg;
2957                 struct snd_pcm_runtime *runtime = substream->runtime;
2958                 void *bufs;
2959                 snd_pcm_sframes_t result;
2960                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2961                         return -EBADFD;
2962                 if (runtime->channels > 128)
2963                         return -EINVAL;
2964                 if (put_user(0, &_xfern->result))
2965                         return -EFAULT;
2966                 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2967                         return -EFAULT;
2968
2969                 bufs = memdup_user(xfern.bufs,
2970                                    sizeof(void *) * runtime->channels);
2971                 if (IS_ERR(bufs))
2972                         return PTR_ERR(bufs);
2973                 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2974                 kfree(bufs);
2975                 __put_user(result, &_xfern->result);
2976                 return result < 0 ? result : 0;
2977         }
2978         case SNDRV_PCM_IOCTL_REWIND:
2979         {
2980                 snd_pcm_uframes_t frames;
2981                 snd_pcm_uframes_t __user *_frames = arg;
2982                 snd_pcm_sframes_t result;
2983                 if (get_user(frames, _frames))
2984                         return -EFAULT;
2985                 if (put_user(0, _frames))
2986                         return -EFAULT;
2987                 result = snd_pcm_capture_rewind(substream, frames);
2988                 __put_user(result, _frames);
2989                 return result < 0 ? result : 0;
2990         }
2991         case SNDRV_PCM_IOCTL_FORWARD:
2992         {
2993                 snd_pcm_uframes_t frames;
2994                 snd_pcm_uframes_t __user *_frames = arg;
2995                 snd_pcm_sframes_t result;
2996                 if (get_user(frames, _frames))
2997                         return -EFAULT;
2998                 if (put_user(0, _frames))
2999                         return -EFAULT;
3000                 result = snd_pcm_capture_forward(substream, frames);
3001                 __put_user(result, _frames);
3002                 return result < 0 ? result : 0;
3003         }
3004         }
3005         return snd_pcm_common_ioctl1(file, substream, cmd, arg);
3006 }
3007
3008 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
3009                                    unsigned long arg)
3010 {
3011         struct snd_pcm_file *pcm_file;
3012
3013         pcm_file = file->private_data;
3014
3015         if (((cmd >> 8) & 0xff) != 'A')
3016                 return -ENOTTY;
3017
3018         return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3019                                        (void __user *)arg);
3020 }
3021
3022 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3023                                   unsigned long arg)
3024 {
3025         struct snd_pcm_file *pcm_file;
3026
3027         pcm_file = file->private_data;
3028
3029         if (((cmd >> 8) & 0xff) != 'A')
3030                 return -ENOTTY;
3031
3032         return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3033                                       (void __user *)arg);
3034 }
3035
3036 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3037                          unsigned int cmd, void *arg)
3038 {
3039         mm_segment_t fs;
3040         int result;
3041         
3042         fs = snd_enter_user();
3043         switch (substream->stream) {
3044         case SNDRV_PCM_STREAM_PLAYBACK:
3045                 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3046                                                  (void __user *)arg);
3047                 break;
3048         case SNDRV_PCM_STREAM_CAPTURE:
3049                 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3050                                                 (void __user *)arg);
3051                 break;
3052         default:
3053                 result = -EINVAL;
3054                 break;
3055         }
3056         snd_leave_user(fs);
3057         return result;
3058 }
3059
3060 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3061
3062 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3063                             loff_t * offset)
3064 {
3065         struct snd_pcm_file *pcm_file;
3066         struct snd_pcm_substream *substream;
3067         struct snd_pcm_runtime *runtime;
3068         snd_pcm_sframes_t result;
3069
3070         pcm_file = file->private_data;
3071         substream = pcm_file->substream;
3072         if (PCM_RUNTIME_CHECK(substream))
3073                 return -ENXIO;
3074         runtime = substream->runtime;
3075         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3076                 return -EBADFD;
3077         if (!frame_aligned(runtime, count))
3078                 return -EINVAL;
3079         count = bytes_to_frames(runtime, count);
3080         result = snd_pcm_lib_read(substream, buf, count);
3081         if (result > 0)
3082                 result = frames_to_bytes(runtime, result);
3083         return result;
3084 }
3085
3086 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3087                              size_t count, loff_t * offset)
3088 {
3089         struct snd_pcm_file *pcm_file;
3090         struct snd_pcm_substream *substream;
3091         struct snd_pcm_runtime *runtime;
3092         snd_pcm_sframes_t result;
3093
3094         pcm_file = file->private_data;
3095         substream = pcm_file->substream;
3096         if (PCM_RUNTIME_CHECK(substream))
3097                 return -ENXIO;
3098         runtime = substream->runtime;
3099         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3100                 return -EBADFD;
3101         if (!frame_aligned(runtime, count))
3102                 return -EINVAL;
3103         count = bytes_to_frames(runtime, count);
3104         result = snd_pcm_lib_write(substream, buf, count);
3105         if (result > 0)
3106                 result = frames_to_bytes(runtime, result);
3107         return result;
3108 }
3109
3110 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3111 {
3112         struct snd_pcm_file *pcm_file;
3113         struct snd_pcm_substream *substream;
3114         struct snd_pcm_runtime *runtime;
3115         snd_pcm_sframes_t result;
3116         unsigned long i;
3117         void __user **bufs;
3118         snd_pcm_uframes_t frames;
3119
3120         pcm_file = iocb->ki_filp->private_data;
3121         substream = pcm_file->substream;
3122         if (PCM_RUNTIME_CHECK(substream))
3123                 return -ENXIO;
3124         runtime = substream->runtime;
3125         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3126                 return -EBADFD;
3127         if (!iter_is_iovec(to))
3128                 return -EINVAL;
3129         if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3130                 return -EINVAL;
3131         if (!frame_aligned(runtime, to->iov->iov_len))
3132                 return -EINVAL;
3133         frames = bytes_to_samples(runtime, to->iov->iov_len);
3134         bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
3135         if (bufs == NULL)
3136                 return -ENOMEM;
3137         for (i = 0; i < to->nr_segs; ++i)
3138                 bufs[i] = to->iov[i].iov_base;
3139         result = snd_pcm_lib_readv(substream, bufs, frames);
3140         if (result > 0)
3141                 result = frames_to_bytes(runtime, result);
3142         kfree(bufs);
3143         return result;
3144 }
3145
3146 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3147 {
3148         struct snd_pcm_file *pcm_file;
3149         struct snd_pcm_substream *substream;
3150         struct snd_pcm_runtime *runtime;
3151         snd_pcm_sframes_t result;
3152         unsigned long i;
3153         void __user **bufs;
3154         snd_pcm_uframes_t frames;
3155
3156         pcm_file = iocb->ki_filp->private_data;
3157         substream = pcm_file->substream;
3158         if (PCM_RUNTIME_CHECK(substream))
3159                 return -ENXIO;
3160         runtime = substream->runtime;
3161         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3162                 return -EBADFD;
3163         if (!iter_is_iovec(from))
3164                 return -EINVAL;
3165         if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3166             !frame_aligned(runtime, from->iov->iov_len))
3167                 return -EINVAL;
3168         frames = bytes_to_samples(runtime, from->iov->iov_len);
3169         bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
3170         if (bufs == NULL)
3171                 return -ENOMEM;
3172         for (i = 0; i < from->nr_segs; ++i)
3173                 bufs[i] = from->iov[i].iov_base;
3174         result = snd_pcm_lib_writev(substream, bufs, frames);
3175         if (result > 0)
3176                 result = frames_to_bytes(runtime, result);
3177         kfree(bufs);
3178         return result;
3179 }
3180
3181 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3182 {
3183         struct snd_pcm_file *pcm_file;
3184         struct snd_pcm_substream *substream;
3185         struct snd_pcm_runtime *runtime;
3186         unsigned int mask;
3187         snd_pcm_uframes_t avail;
3188
3189         pcm_file = file->private_data;
3190
3191         substream = pcm_file->substream;
3192         if (PCM_RUNTIME_CHECK(substream))
3193                 return POLLOUT | POLLWRNORM | POLLERR;
3194         runtime = substream->runtime;
3195
3196         poll_wait(file, &runtime->sleep, wait);
3197
3198         snd_pcm_stream_lock_irq(substream);
3199         avail = snd_pcm_playback_avail(runtime);
3200         switch (runtime->status->state) {
3201         case SNDRV_PCM_STATE_RUNNING:
3202         case SNDRV_PCM_STATE_PREPARED:
3203         case SNDRV_PCM_STATE_PAUSED:
3204                 if (avail >= runtime->control->avail_min) {
3205                         mask = POLLOUT | POLLWRNORM;
3206                         break;
3207                 }
3208                 /* Fall through */
3209         case SNDRV_PCM_STATE_DRAINING:
3210                 mask = 0;
3211                 break;
3212         default:
3213                 mask = POLLOUT | POLLWRNORM | POLLERR;
3214                 break;
3215         }
3216         snd_pcm_stream_unlock_irq(substream);
3217         return mask;
3218 }
3219
3220 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3221 {
3222         struct snd_pcm_file *pcm_file;
3223         struct snd_pcm_substream *substream;
3224         struct snd_pcm_runtime *runtime;
3225         unsigned int mask;
3226         snd_pcm_uframes_t avail;
3227
3228         pcm_file = file->private_data;
3229
3230         substream = pcm_file->substream;
3231         if (PCM_RUNTIME_CHECK(substream))
3232                 return POLLIN | POLLRDNORM | POLLERR;
3233         runtime = substream->runtime;
3234
3235         poll_wait(file, &runtime->sleep, wait);
3236
3237         snd_pcm_stream_lock_irq(substream);
3238         avail = snd_pcm_capture_avail(runtime);
3239         switch (runtime->status->state) {
3240         case SNDRV_PCM_STATE_RUNNING:
3241         case SNDRV_PCM_STATE_PREPARED:
3242         case SNDRV_PCM_STATE_PAUSED:
3243                 if (avail >= runtime->control->avail_min) {
3244                         mask = POLLIN | POLLRDNORM;
3245                         break;
3246                 }
3247                 mask = 0;
3248                 break;
3249         case SNDRV_PCM_STATE_DRAINING:
3250                 if (avail > 0) {
3251                         mask = POLLIN | POLLRDNORM;
3252                         break;
3253                 }
3254                 /* Fall through */
3255         default:
3256                 mask = POLLIN | POLLRDNORM | POLLERR;
3257                 break;
3258         }
3259         snd_pcm_stream_unlock_irq(substream);
3260         return mask;
3261 }
3262
3263 /*
3264  * mmap support
3265  */
3266
3267 /*
3268  * Only on coherent architectures, we can mmap the status and the control records
3269  * for effcient data transfer.  On others, we have to use HWSYNC ioctl...
3270  */
3271 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3272 /*
3273  * mmap status record
3274  */
3275 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3276                                                 struct vm_fault *vmf)
3277 {
3278         struct snd_pcm_substream *substream = area->vm_private_data;
3279         struct snd_pcm_runtime *runtime;
3280         
3281         if (substream == NULL)
3282                 return VM_FAULT_SIGBUS;
3283         runtime = substream->runtime;
3284         vmf->page = virt_to_page(runtime->status);
3285         get_page(vmf->page);
3286         return 0;
3287 }
3288
3289 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3290 {
3291         .fault =        snd_pcm_mmap_status_fault,
3292 };
3293
3294 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3295                                struct vm_area_struct *area)
3296 {
3297         long size;
3298         if (!(area->vm_flags & VM_READ))
3299                 return -EINVAL;
3300         size = area->vm_end - area->vm_start;
3301         if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3302                 return -EINVAL;
3303         area->vm_ops = &snd_pcm_vm_ops_status;
3304         area->vm_private_data = substream;
3305         area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3306         return 0;
3307 }
3308
3309 /*
3310  * mmap control record
3311  */
3312 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3313                                                 struct vm_fault *vmf)
3314 {
3315         struct snd_pcm_substream *substream = area->vm_private_data;
3316         struct snd_pcm_runtime *runtime;
3317         
3318         if (substream == NULL)
3319                 return VM_FAULT_SIGBUS;
3320         runtime = substream->runtime;
3321         vmf->page = virt_to_page(runtime->control);
3322         get_page(vmf->page);
3323         return 0;
3324 }
3325
3326 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3327 {
3328         .fault =        snd_pcm_mmap_control_fault,
3329 };
3330
3331 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3332                                 struct vm_area_struct *area)
3333 {
3334         long size;
3335         if (!(area->vm_flags & VM_READ))
3336                 return -EINVAL;
3337         size = area->vm_end - area->vm_start;
3338         if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3339                 return -EINVAL;
3340         area->vm_ops = &snd_pcm_vm_ops_control;
3341         area->vm_private_data = substream;
3342         area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3343         return 0;
3344 }
3345 #else /* ! coherent mmap */
3346 /*
3347  * don't support mmap for status and control records.
3348  */
3349 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3350                                struct vm_area_struct *area)
3351 {
3352         return -ENXIO;
3353 }
3354 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3355                                 struct vm_area_struct *area)
3356 {
3357         return -ENXIO;
3358 }
3359 #endif /* coherent mmap */
3360
3361 static inline struct page *
3362 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3363 {
3364         void *vaddr = substream->runtime->dma_area + ofs;
3365         return virt_to_page(vaddr);
3366 }
3367
3368 /*
3369  * fault callback for mmapping a RAM page
3370  */
3371 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3372                                                 struct vm_fault *vmf)
3373 {
3374         struct snd_pcm_substream *substream = area->vm_private_data;
3375         struct snd_pcm_runtime *runtime;
3376         unsigned long offset;
3377         struct page * page;
3378         size_t dma_bytes;
3379         
3380         if (substream == NULL)
3381                 return VM_FAULT_SIGBUS;
3382         runtime = substream->runtime;
3383         offset = vmf->pgoff << PAGE_SHIFT;
3384         dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3385         if (offset > dma_bytes - PAGE_SIZE)
3386                 return VM_FAULT_SIGBUS;
3387         if (substream->ops->page)
3388                 page = substream->ops->page(substream, offset);
3389         else
3390                 page = snd_pcm_default_page_ops(substream, offset);
3391         if (!page)
3392                 return VM_FAULT_SIGBUS;
3393         get_page(page);
3394         vmf->page = page;
3395         return 0;
3396 }
3397
3398 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3399         .open =         snd_pcm_mmap_data_open,
3400         .close =        snd_pcm_mmap_data_close,
3401 };
3402
3403 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3404         .open =         snd_pcm_mmap_data_open,
3405         .close =        snd_pcm_mmap_data_close,
3406         .fault =        snd_pcm_mmap_data_fault,
3407 };
3408
3409 /*
3410  * mmap the DMA buffer on RAM
3411  */
3412
3413 /**
3414  * snd_pcm_lib_default_mmap - Default PCM data mmap function
3415  * @substream: PCM substream
3416  * @area: VMA
3417  *
3418  * This is the default mmap handler for PCM data.  When mmap pcm_ops is NULL,
3419  * this function is invoked implicitly.
3420  */
3421 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3422                              struct vm_area_struct *area)
3423 {
3424         area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3425 #ifdef CONFIG_GENERIC_ALLOCATOR
3426         if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3427                 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3428                 return remap_pfn_range(area, area->vm_start,
3429                                 substream->dma_buffer.addr >> PAGE_SHIFT,
3430                                 area->vm_end - area->vm_start, area->vm_page_prot);
3431         }
3432 #endif /* CONFIG_GENERIC_ALLOCATOR */
3433 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3434         if (!substream->ops->page &&
3435             substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3436                 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3437                                          area,
3438                                          substream->runtime->dma_area,
3439                                          substream->runtime->dma_addr,
3440                                          substream->runtime->dma_bytes);
3441 #endif /* CONFIG_X86 */
3442         /* mmap with fault handler */
3443         area->vm_ops = &snd_pcm_vm_ops_data_fault;
3444         return 0;
3445 }
3446 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3447
3448 /*
3449  * mmap the DMA buffer on I/O memory area
3450  */
3451 #if SNDRV_PCM_INFO_MMAP_IOMEM
3452 /**
3453  * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3454  * @substream: PCM substream
3455  * @area: VMA
3456  *
3457  * When your hardware uses the iomapped pages as the hardware buffer and
3458  * wants to mmap it, pass this function as mmap pcm_ops.  Note that this
3459  * is supposed to work only on limited architectures.
3460  */
3461 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3462                            struct vm_area_struct *area)
3463 {
3464         struct snd_pcm_runtime *runtime = substream->runtime;;
3465
3466         area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3467         return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3468 }
3469
3470 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3471 #endif /* SNDRV_PCM_INFO_MMAP */
3472
3473 /*
3474  * mmap DMA buffer
3475  */
3476 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3477                       struct vm_area_struct *area)
3478 {
3479         struct snd_pcm_runtime *runtime;
3480         long size;
3481         unsigned long offset;
3482         size_t dma_bytes;
3483         int err;
3484
3485         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3486                 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3487                         return -EINVAL;
3488         } else {
3489                 if (!(area->vm_flags & VM_READ))
3490                         return -EINVAL;
3491         }
3492         runtime = substream->runtime;
3493         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3494                 return -EBADFD;
3495         if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3496                 return -ENXIO;
3497         if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3498             runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3499                 return -EINVAL;
3500         size = area->vm_end - area->vm_start;
3501         offset = area->vm_pgoff << PAGE_SHIFT;
3502         dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3503         if ((size_t)size > dma_bytes)
3504                 return -EINVAL;
3505         if (offset > dma_bytes - size)
3506                 return -EINVAL;
3507
3508         area->vm_ops = &snd_pcm_vm_ops_data;
3509         area->vm_private_data = substream;
3510         if (substream->ops->mmap)
3511                 err = substream->ops->mmap(substream, area);
3512         else
3513                 err = snd_pcm_lib_default_mmap(substream, area);
3514         if (!err)
3515                 atomic_inc(&substream->mmap_count);
3516         return err;
3517 }
3518
3519 EXPORT_SYMBOL(snd_pcm_mmap_data);
3520
3521 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3522 {
3523         struct snd_pcm_file * pcm_file;
3524         struct snd_pcm_substream *substream;    
3525         unsigned long offset;
3526         
3527         pcm_file = file->private_data;
3528         substream = pcm_file->substream;
3529         if (PCM_RUNTIME_CHECK(substream))
3530                 return -ENXIO;
3531
3532         offset = area->vm_pgoff << PAGE_SHIFT;
3533         switch (offset) {
3534         case SNDRV_PCM_MMAP_OFFSET_STATUS:
3535                 if (pcm_file->no_compat_mmap)
3536                         return -ENXIO;
3537                 return snd_pcm_mmap_status(substream, file, area);
3538         case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3539                 if (pcm_file->no_compat_mmap)
3540                         return -ENXIO;
3541                 return snd_pcm_mmap_control(substream, file, area);
3542         default:
3543                 return snd_pcm_mmap_data(substream, file, area);
3544         }
3545         return 0;
3546 }
3547
3548 static int snd_pcm_fasync(int fd, struct file * file, int on)
3549 {
3550         struct snd_pcm_file * pcm_file;
3551         struct snd_pcm_substream *substream;
3552         struct snd_pcm_runtime *runtime;
3553
3554         pcm_file = file->private_data;
3555         substream = pcm_file->substream;
3556         if (PCM_RUNTIME_CHECK(substream))
3557                 return -ENXIO;
3558         runtime = substream->runtime;
3559         return fasync_helper(fd, file, on, &runtime->fasync);
3560 }
3561
3562 /*
3563  * ioctl32 compat
3564  */
3565 #ifdef CONFIG_COMPAT
3566 #include "pcm_compat.c"
3567 #else
3568 #define snd_pcm_ioctl_compat    NULL
3569 #endif
3570
3571 /*
3572  *  To be removed helpers to keep binary compatibility
3573  */
3574
3575 #ifdef CONFIG_SND_SUPPORT_OLD_API
3576 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3577 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3578
3579 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3580                                                struct snd_pcm_hw_params_old *oparams)
3581 {
3582         unsigned int i;
3583
3584         memset(params, 0, sizeof(*params));
3585         params->flags = oparams->flags;
3586         for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3587                 params->masks[i].bits[0] = oparams->masks[i];
3588         memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3589         params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3590         params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3591         params->info = oparams->info;
3592         params->msbits = oparams->msbits;
3593         params->rate_num = oparams->rate_num;
3594         params->rate_den = oparams->rate_den;
3595         params->fifo_size = oparams->fifo_size;
3596 }
3597
3598 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3599                                              struct snd_pcm_hw_params *params)
3600 {
3601         unsigned int i;
3602
3603         memset(oparams, 0, sizeof(*oparams));
3604         oparams->flags = params->flags;
3605         for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3606                 oparams->masks[i] = params->masks[i].bits[0];
3607         memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3608         oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3609         oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3610         oparams->info = params->info;
3611         oparams->msbits = params->msbits;
3612         oparams->rate_num = params->rate_num;
3613         oparams->rate_den = params->rate_den;
3614         oparams->fifo_size = params->fifo_size;
3615 }
3616
3617 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3618                                       struct snd_pcm_hw_params_old __user * _oparams)
3619 {
3620         struct snd_pcm_hw_params *params;
3621         struct snd_pcm_hw_params_old *oparams = NULL;
3622         int err;
3623
3624         params = kmalloc(sizeof(*params), GFP_KERNEL);
3625         if (!params)
3626                 return -ENOMEM;
3627
3628         oparams = memdup_user(_oparams, sizeof(*oparams));
3629         if (IS_ERR(oparams)) {
3630                 err = PTR_ERR(oparams);
3631                 goto out;
3632         }
3633         snd_pcm_hw_convert_from_old_params(params, oparams);
3634         err = snd_pcm_hw_refine(substream, params);
3635         snd_pcm_hw_convert_to_old_params(oparams, params);
3636         if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3637                 if (!err)
3638                         err = -EFAULT;
3639         }
3640
3641         kfree(oparams);
3642 out:
3643         kfree(params);
3644         return err;
3645 }
3646
3647 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3648                                       struct snd_pcm_hw_params_old __user * _oparams)
3649 {
3650         struct snd_pcm_hw_params *params;
3651         struct snd_pcm_hw_params_old *oparams = NULL;
3652         int err;
3653
3654         params = kmalloc(sizeof(*params), GFP_KERNEL);
3655         if (!params)
3656                 return -ENOMEM;
3657
3658         oparams = memdup_user(_oparams, sizeof(*oparams));
3659         if (IS_ERR(oparams)) {
3660                 err = PTR_ERR(oparams);
3661                 goto out;
3662         }
3663         snd_pcm_hw_convert_from_old_params(params, oparams);
3664         err = snd_pcm_hw_params(substream, params);
3665         snd_pcm_hw_convert_to_old_params(oparams, params);
3666         if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3667                 if (!err)
3668                         err = -EFAULT;
3669         }
3670
3671         kfree(oparams);
3672 out:
3673         kfree(params);
3674         return err;
3675 }
3676 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3677
3678 #ifndef CONFIG_MMU
3679 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3680                                                unsigned long addr,
3681                                                unsigned long len,
3682                                                unsigned long pgoff,
3683                                                unsigned long flags)
3684 {
3685         struct snd_pcm_file *pcm_file = file->private_data;
3686         struct snd_pcm_substream *substream = pcm_file->substream;
3687         struct snd_pcm_runtime *runtime = substream->runtime;
3688         unsigned long offset = pgoff << PAGE_SHIFT;
3689
3690         switch (offset) {
3691         case SNDRV_PCM_MMAP_OFFSET_STATUS:
3692                 return (unsigned long)runtime->status;
3693         case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3694                 return (unsigned long)runtime->control;
3695         default:
3696                 return (unsigned long)runtime->dma_area + offset;
3697         }
3698 }
3699 #else
3700 # define snd_pcm_get_unmapped_area NULL
3701 #endif
3702
3703 /*
3704  *  Register section
3705  */
3706
3707 const struct file_operations snd_pcm_f_ops[2] = {
3708         {
3709                 .owner =                THIS_MODULE,
3710                 .write =                snd_pcm_write,
3711                 .write_iter =           snd_pcm_writev,
3712                 .open =                 snd_pcm_playback_open,
3713                 .release =              snd_pcm_release,
3714                 .llseek =               no_llseek,
3715                 .poll =                 snd_pcm_playback_poll,
3716                 .unlocked_ioctl =       snd_pcm_playback_ioctl,
3717                 .compat_ioctl =         snd_pcm_ioctl_compat,
3718                 .mmap =                 snd_pcm_mmap,
3719                 .fasync =               snd_pcm_fasync,
3720                 .get_unmapped_area =    snd_pcm_get_unmapped_area,
3721         },
3722         {
3723                 .owner =                THIS_MODULE,
3724                 .read =                 snd_pcm_read,
3725                 .read_iter =            snd_pcm_readv,
3726                 .open =                 snd_pcm_capture_open,
3727                 .release =              snd_pcm_release,
3728                 .llseek =               no_llseek,
3729                 .poll =                 snd_pcm_capture_poll,
3730                 .unlocked_ioctl =       snd_pcm_capture_ioctl,
3731                 .compat_ioctl =         snd_pcm_ioctl_compat,
3732                 .mmap =                 snd_pcm_mmap,
3733                 .fasync =               snd_pcm_fasync,
3734                 .get_unmapped_area =    snd_pcm_get_unmapped_area,
3735         }
3736 };