xref: /openbmc/linux/sound/core/pcm_native.c (revision b55f9fdc)
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/sched/signal.h>
27 #include <linux/time.h>
28 #include <linux/pm_qos.h>
29 #include <linux/io.h>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
38 #include <linux/uio.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  * spin until it gets the lock.
83  */
84 static inline void down_write_nonblock(struct rw_semaphore *lock)
85 {
86 	while (!down_write_trylock(lock))
87 		cond_resched();
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 	/* AB: FIXME!!! This is definitely nonsense */
218 	if (runtime) {
219 		info->sync = runtime->sync;
220 		substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
221 	}
222 	return 0;
223 }
224 
225 int snd_pcm_info_user(struct snd_pcm_substream *substream,
226 		      struct snd_pcm_info __user * _info)
227 {
228 	struct snd_pcm_info *info;
229 	int err;
230 
231 	info = kmalloc(sizeof(*info), GFP_KERNEL);
232 	if (! info)
233 		return -ENOMEM;
234 	err = snd_pcm_info(substream, info);
235 	if (err >= 0) {
236 		if (copy_to_user(_info, info, sizeof(*info)))
237 			err = -EFAULT;
238 	}
239 	kfree(info);
240 	return err;
241 }
242 
243 static bool hw_support_mmap(struct snd_pcm_substream *substream)
244 {
245 	if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
246 		return false;
247 	/* check architectures that return -EINVAL from dma_mmap_coherent() */
248 	/* FIXME: this should be some global flag */
249 #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
250 	defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
251 	if (!substream->ops->mmap &&
252 	    substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
253 		return false;
254 #endif
255 	return true;
256 }
257 
258 #undef RULES_DEBUG
259 
260 #ifdef RULES_DEBUG
261 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
262 static const char * const snd_pcm_hw_param_names[] = {
263 	HW_PARAM(ACCESS),
264 	HW_PARAM(FORMAT),
265 	HW_PARAM(SUBFORMAT),
266 	HW_PARAM(SAMPLE_BITS),
267 	HW_PARAM(FRAME_BITS),
268 	HW_PARAM(CHANNELS),
269 	HW_PARAM(RATE),
270 	HW_PARAM(PERIOD_TIME),
271 	HW_PARAM(PERIOD_SIZE),
272 	HW_PARAM(PERIOD_BYTES),
273 	HW_PARAM(PERIODS),
274 	HW_PARAM(BUFFER_TIME),
275 	HW_PARAM(BUFFER_SIZE),
276 	HW_PARAM(BUFFER_BYTES),
277 	HW_PARAM(TICK_TIME),
278 };
279 #endif
280 
281 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
282 		      struct snd_pcm_hw_params *params)
283 {
284 	unsigned int k;
285 	struct snd_pcm_hardware *hw;
286 	struct snd_interval *i = NULL;
287 	struct snd_mask *m = NULL;
288 	struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
289 	unsigned int rstamps[constrs->rules_num];
290 	unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
291 	unsigned int stamp = 2;
292 	int changed, again;
293 
294 	params->info = 0;
295 	params->fifo_size = 0;
296 	if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
297 		params->msbits = 0;
298 	if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
299 		params->rate_num = 0;
300 		params->rate_den = 0;
301 	}
302 
303 	for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
304 		m = hw_param_mask(params, k);
305 		if (snd_mask_empty(m))
306 			return -EINVAL;
307 		if (!(params->rmask & (1 << k)))
308 			continue;
309 #ifdef RULES_DEBUG
310 		pr_debug("%s = ", snd_pcm_hw_param_names[k]);
311 		pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
312 #endif
313 		changed = snd_mask_refine(m, constrs_mask(constrs, k));
314 #ifdef RULES_DEBUG
315 		pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
316 #endif
317 		if (changed)
318 			params->cmask |= 1 << k;
319 		if (changed < 0)
320 			return changed;
321 	}
322 
323 	for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
324 		i = hw_param_interval(params, k);
325 		if (snd_interval_empty(i))
326 			return -EINVAL;
327 		if (!(params->rmask & (1 << k)))
328 			continue;
329 #ifdef RULES_DEBUG
330 		pr_debug("%s = ", snd_pcm_hw_param_names[k]);
331 		if (i->empty)
332 			pr_cont("empty");
333 		else
334 			pr_cont("%c%u %u%c",
335 			       i->openmin ? '(' : '[', i->min,
336 			       i->max, i->openmax ? ')' : ']');
337 		pr_cont(" -> ");
338 #endif
339 		changed = snd_interval_refine(i, constrs_interval(constrs, k));
340 #ifdef RULES_DEBUG
341 		if (i->empty)
342 			pr_cont("empty\n");
343 		else
344 			pr_cont("%c%u %u%c\n",
345 			       i->openmin ? '(' : '[', i->min,
346 			       i->max, i->openmax ? ')' : ']');
347 #endif
348 		if (changed)
349 			params->cmask |= 1 << k;
350 		if (changed < 0)
351 			return changed;
352 	}
353 
354 	for (k = 0; k < constrs->rules_num; k++)
355 		rstamps[k] = 0;
356 	for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
357 		vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
358 	do {
359 		again = 0;
360 		for (k = 0; k < constrs->rules_num; k++) {
361 			struct snd_pcm_hw_rule *r = &constrs->rules[k];
362 			unsigned int d;
363 			int doit = 0;
364 			if (r->cond && !(r->cond & params->flags))
365 				continue;
366 			for (d = 0; r->deps[d] >= 0; d++) {
367 				if (vstamps[r->deps[d]] > rstamps[k]) {
368 					doit = 1;
369 					break;
370 				}
371 			}
372 			if (!doit)
373 				continue;
374 #ifdef RULES_DEBUG
375 			pr_debug("Rule %d [%p]: ", k, r->func);
376 			if (r->var >= 0) {
377 				pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
378 				if (hw_is_mask(r->var)) {
379 					m = hw_param_mask(params, r->var);
380 					pr_cont("%x", *m->bits);
381 				} else {
382 					i = hw_param_interval(params, r->var);
383 					if (i->empty)
384 						pr_cont("empty");
385 					else
386 						pr_cont("%c%u %u%c",
387 						       i->openmin ? '(' : '[', i->min,
388 						       i->max, i->openmax ? ')' : ']');
389 				}
390 			}
391 #endif
392 			changed = r->func(params, r);
393 #ifdef RULES_DEBUG
394 			if (r->var >= 0) {
395 				pr_cont(" -> ");
396 				if (hw_is_mask(r->var))
397 					pr_cont("%x", *m->bits);
398 				else {
399 					if (i->empty)
400 						pr_cont("empty");
401 					else
402 						pr_cont("%c%u %u%c",
403 						       i->openmin ? '(' : '[', i->min,
404 						       i->max, i->openmax ? ')' : ']');
405 				}
406 			}
407 			pr_cont("\n");
408 #endif
409 			rstamps[k] = stamp;
410 			if (changed && r->var >= 0) {
411 				params->cmask |= (1 << r->var);
412 				vstamps[r->var] = stamp;
413 				again = 1;
414 			}
415 			if (changed < 0)
416 				return changed;
417 			stamp++;
418 		}
419 	} while (again);
420 	if (!params->msbits) {
421 		i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
422 		if (snd_interval_single(i))
423 			params->msbits = snd_interval_value(i);
424 	}
425 
426 	if (!params->rate_den) {
427 		i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
428 		if (snd_interval_single(i)) {
429 			params->rate_num = snd_interval_value(i);
430 			params->rate_den = 1;
431 		}
432 	}
433 
434 	hw = &substream->runtime->hw;
435 	if (!params->info) {
436 		params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
437 					    SNDRV_PCM_INFO_DRAIN_TRIGGER);
438 		if (!hw_support_mmap(substream))
439 			params->info &= ~(SNDRV_PCM_INFO_MMAP |
440 					  SNDRV_PCM_INFO_MMAP_VALID);
441 	}
442 	if (!params->fifo_size) {
443 		m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
444 		i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
445 		if (snd_mask_min(m) == snd_mask_max(m) &&
446                     snd_interval_min(i) == snd_interval_max(i)) {
447 			changed = substream->ops->ioctl(substream,
448 					SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
449 			if (changed < 0)
450 				return changed;
451 		}
452 	}
453 	params->rmask = 0;
454 	return 0;
455 }
456 
457 EXPORT_SYMBOL(snd_pcm_hw_refine);
458 
459 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
460 				  struct snd_pcm_hw_params __user * _params)
461 {
462 	struct snd_pcm_hw_params *params;
463 	int err;
464 
465 	params = memdup_user(_params, sizeof(*params));
466 	if (IS_ERR(params))
467 		return PTR_ERR(params);
468 
469 	err = snd_pcm_hw_refine(substream, params);
470 	if (copy_to_user(_params, params, sizeof(*params))) {
471 		if (!err)
472 			err = -EFAULT;
473 	}
474 
475 	kfree(params);
476 	return err;
477 }
478 
479 static int period_to_usecs(struct snd_pcm_runtime *runtime)
480 {
481 	int usecs;
482 
483 	if (! runtime->rate)
484 		return -1; /* invalid */
485 
486 	/* take 75% of period time as the deadline */
487 	usecs = (750000 / runtime->rate) * runtime->period_size;
488 	usecs += ((750000 % runtime->rate) * runtime->period_size) /
489 		runtime->rate;
490 
491 	return usecs;
492 }
493 
494 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
495 {
496 	snd_pcm_stream_lock_irq(substream);
497 	if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
498 		substream->runtime->status->state = state;
499 	snd_pcm_stream_unlock_irq(substream);
500 }
501 
502 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
503 					int event)
504 {
505 #ifdef CONFIG_SND_PCM_TIMER
506 	if (substream->timer)
507 		snd_timer_notify(substream->timer, event,
508 					&substream->runtime->trigger_tstamp);
509 #endif
510 }
511 
512 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
513 			     struct snd_pcm_hw_params *params)
514 {
515 	struct snd_pcm_runtime *runtime;
516 	int err, usecs;
517 	unsigned int bits;
518 	snd_pcm_uframes_t frames;
519 
520 	if (PCM_RUNTIME_CHECK(substream))
521 		return -ENXIO;
522 	runtime = substream->runtime;
523 	snd_pcm_stream_lock_irq(substream);
524 	switch (runtime->status->state) {
525 	case SNDRV_PCM_STATE_OPEN:
526 	case SNDRV_PCM_STATE_SETUP:
527 	case SNDRV_PCM_STATE_PREPARED:
528 		break;
529 	default:
530 		snd_pcm_stream_unlock_irq(substream);
531 		return -EBADFD;
532 	}
533 	snd_pcm_stream_unlock_irq(substream);
534 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
535 	if (!substream->oss.oss)
536 #endif
537 		if (atomic_read(&substream->mmap_count))
538 			return -EBADFD;
539 
540 	params->rmask = ~0U;
541 	err = snd_pcm_hw_refine(substream, params);
542 	if (err < 0)
543 		goto _error;
544 
545 	err = snd_pcm_hw_params_choose(substream, params);
546 	if (err < 0)
547 		goto _error;
548 
549 	if (substream->ops->hw_params != NULL) {
550 		err = substream->ops->hw_params(substream, params);
551 		if (err < 0)
552 			goto _error;
553 	}
554 
555 	runtime->access = params_access(params);
556 	runtime->format = params_format(params);
557 	runtime->subformat = params_subformat(params);
558 	runtime->channels = params_channels(params);
559 	runtime->rate = params_rate(params);
560 	runtime->period_size = params_period_size(params);
561 	runtime->periods = params_periods(params);
562 	runtime->buffer_size = params_buffer_size(params);
563 	runtime->info = params->info;
564 	runtime->rate_num = params->rate_num;
565 	runtime->rate_den = params->rate_den;
566 	runtime->no_period_wakeup =
567 			(params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
568 			(params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
569 
570 	bits = snd_pcm_format_physical_width(runtime->format);
571 	runtime->sample_bits = bits;
572 	bits *= runtime->channels;
573 	runtime->frame_bits = bits;
574 	frames = 1;
575 	while (bits % 8 != 0) {
576 		bits *= 2;
577 		frames *= 2;
578 	}
579 	runtime->byte_align = bits / 8;
580 	runtime->min_align = frames;
581 
582 	/* Default sw params */
583 	runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
584 	runtime->period_step = 1;
585 	runtime->control->avail_min = runtime->period_size;
586 	runtime->start_threshold = 1;
587 	runtime->stop_threshold = runtime->buffer_size;
588 	runtime->silence_threshold = 0;
589 	runtime->silence_size = 0;
590 	runtime->boundary = runtime->buffer_size;
591 	while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
592 		runtime->boundary *= 2;
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 	if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1262 		return -EBUSY;
1263 	runtime->trigger_master = substream;
1264 	return 0;
1265 }
1266 
1267 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1268 {
1269 	struct snd_pcm_runtime *runtime = substream->runtime;
1270 	if (runtime->trigger_master != substream)
1271 		return 0;
1272 	if (! snd_pcm_running(substream))
1273 		return 0;
1274 	substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1275 	return 0; /* suspend unconditionally */
1276 }
1277 
1278 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1279 {
1280 	struct snd_pcm_runtime *runtime = substream->runtime;
1281 	snd_pcm_trigger_tstamp(substream);
1282 	runtime->status->suspended_state = runtime->status->state;
1283 	runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1284 	snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1285 	wake_up(&runtime->sleep);
1286 	wake_up(&runtime->tsleep);
1287 }
1288 
1289 static const struct action_ops snd_pcm_action_suspend = {
1290 	.pre_action = snd_pcm_pre_suspend,
1291 	.do_action = snd_pcm_do_suspend,
1292 	.post_action = snd_pcm_post_suspend
1293 };
1294 
1295 /**
1296  * snd_pcm_suspend - trigger SUSPEND to all linked streams
1297  * @substream: the PCM substream
1298  *
1299  * After this call, all streams are changed to SUSPENDED state.
1300  *
1301  * Return: Zero if successful (or @substream is %NULL), or a negative error
1302  * code.
1303  */
1304 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1305 {
1306 	int err;
1307 	unsigned long flags;
1308 
1309 	if (! substream)
1310 		return 0;
1311 
1312 	snd_pcm_stream_lock_irqsave(substream, flags);
1313 	err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1314 	snd_pcm_stream_unlock_irqrestore(substream, flags);
1315 	return err;
1316 }
1317 
1318 EXPORT_SYMBOL(snd_pcm_suspend);
1319 
1320 /**
1321  * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1322  * @pcm: the PCM instance
1323  *
1324  * After this call, all streams are changed to SUSPENDED state.
1325  *
1326  * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1327  */
1328 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1329 {
1330 	struct snd_pcm_substream *substream;
1331 	int stream, err = 0;
1332 
1333 	if (! pcm)
1334 		return 0;
1335 
1336 	for (stream = 0; stream < 2; stream++) {
1337 		for (substream = pcm->streams[stream].substream;
1338 		     substream; substream = substream->next) {
1339 			/* FIXME: the open/close code should lock this as well */
1340 			if (substream->runtime == NULL)
1341 				continue;
1342 			err = snd_pcm_suspend(substream);
1343 			if (err < 0 && err != -EBUSY)
1344 				return err;
1345 		}
1346 	}
1347 	return 0;
1348 }
1349 
1350 EXPORT_SYMBOL(snd_pcm_suspend_all);
1351 
1352 /* resume */
1353 
1354 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1355 {
1356 	struct snd_pcm_runtime *runtime = substream->runtime;
1357 	if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1358 		return -ENOSYS;
1359 	runtime->trigger_master = substream;
1360 	return 0;
1361 }
1362 
1363 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1364 {
1365 	struct snd_pcm_runtime *runtime = substream->runtime;
1366 	if (runtime->trigger_master != substream)
1367 		return 0;
1368 	/* DMA not running previously? */
1369 	if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1370 	    (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1371 	     substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1372 		return 0;
1373 	return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1374 }
1375 
1376 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1377 {
1378 	if (substream->runtime->trigger_master == substream &&
1379 	    snd_pcm_running(substream))
1380 		substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1381 }
1382 
1383 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1384 {
1385 	struct snd_pcm_runtime *runtime = substream->runtime;
1386 	snd_pcm_trigger_tstamp(substream);
1387 	runtime->status->state = runtime->status->suspended_state;
1388 	snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1389 }
1390 
1391 static const struct action_ops snd_pcm_action_resume = {
1392 	.pre_action = snd_pcm_pre_resume,
1393 	.do_action = snd_pcm_do_resume,
1394 	.undo_action = snd_pcm_undo_resume,
1395 	.post_action = snd_pcm_post_resume
1396 };
1397 
1398 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1399 {
1400 	struct snd_card *card = substream->pcm->card;
1401 	int res;
1402 
1403 	snd_power_lock(card);
1404 	if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1405 		res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1406 	snd_power_unlock(card);
1407 	return res;
1408 }
1409 
1410 #else
1411 
1412 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1413 {
1414 	return -ENOSYS;
1415 }
1416 
1417 #endif /* CONFIG_PM */
1418 
1419 /*
1420  * xrun ioctl
1421  *
1422  * Change the RUNNING stream(s) to XRUN state.
1423  */
1424 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1425 {
1426 	struct snd_card *card = substream->pcm->card;
1427 	struct snd_pcm_runtime *runtime = substream->runtime;
1428 	int result;
1429 
1430 	snd_power_lock(card);
1431 	if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1432 		result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1433 		if (result < 0)
1434 			goto _unlock;
1435 	}
1436 
1437 	snd_pcm_stream_lock_irq(substream);
1438 	switch (runtime->status->state) {
1439 	case SNDRV_PCM_STATE_XRUN:
1440 		result = 0;	/* already there */
1441 		break;
1442 	case SNDRV_PCM_STATE_RUNNING:
1443 		result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1444 		break;
1445 	default:
1446 		result = -EBADFD;
1447 	}
1448 	snd_pcm_stream_unlock_irq(substream);
1449  _unlock:
1450 	snd_power_unlock(card);
1451 	return result;
1452 }
1453 
1454 /*
1455  * reset ioctl
1456  */
1457 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1458 {
1459 	struct snd_pcm_runtime *runtime = substream->runtime;
1460 	switch (runtime->status->state) {
1461 	case SNDRV_PCM_STATE_RUNNING:
1462 	case SNDRV_PCM_STATE_PREPARED:
1463 	case SNDRV_PCM_STATE_PAUSED:
1464 	case SNDRV_PCM_STATE_SUSPENDED:
1465 		return 0;
1466 	default:
1467 		return -EBADFD;
1468 	}
1469 }
1470 
1471 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1472 {
1473 	struct snd_pcm_runtime *runtime = substream->runtime;
1474 	int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1475 	if (err < 0)
1476 		return err;
1477 	runtime->hw_ptr_base = 0;
1478 	runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1479 		runtime->status->hw_ptr % runtime->period_size;
1480 	runtime->silence_start = runtime->status->hw_ptr;
1481 	runtime->silence_filled = 0;
1482 	return 0;
1483 }
1484 
1485 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1486 {
1487 	struct snd_pcm_runtime *runtime = substream->runtime;
1488 	runtime->control->appl_ptr = runtime->status->hw_ptr;
1489 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1490 	    runtime->silence_size > 0)
1491 		snd_pcm_playback_silence(substream, ULONG_MAX);
1492 }
1493 
1494 static const struct action_ops snd_pcm_action_reset = {
1495 	.pre_action = snd_pcm_pre_reset,
1496 	.do_action = snd_pcm_do_reset,
1497 	.post_action = snd_pcm_post_reset
1498 };
1499 
1500 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1501 {
1502 	return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1503 }
1504 
1505 /*
1506  * prepare ioctl
1507  */
1508 /* we use the second argument for updating f_flags */
1509 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1510 			       int f_flags)
1511 {
1512 	struct snd_pcm_runtime *runtime = substream->runtime;
1513 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1514 	    runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1515 		return -EBADFD;
1516 	if (snd_pcm_running(substream))
1517 		return -EBUSY;
1518 	substream->f_flags = f_flags;
1519 	return 0;
1520 }
1521 
1522 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1523 {
1524 	int err;
1525 	err = substream->ops->prepare(substream);
1526 	if (err < 0)
1527 		return err;
1528 	return snd_pcm_do_reset(substream, 0);
1529 }
1530 
1531 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1532 {
1533 	struct snd_pcm_runtime *runtime = substream->runtime;
1534 	runtime->control->appl_ptr = runtime->status->hw_ptr;
1535 	snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1536 }
1537 
1538 static const struct action_ops snd_pcm_action_prepare = {
1539 	.pre_action = snd_pcm_pre_prepare,
1540 	.do_action = snd_pcm_do_prepare,
1541 	.post_action = snd_pcm_post_prepare
1542 };
1543 
1544 /**
1545  * snd_pcm_prepare - prepare the PCM substream to be triggerable
1546  * @substream: the PCM substream instance
1547  * @file: file to refer f_flags
1548  *
1549  * Return: Zero if successful, or a negative error code.
1550  */
1551 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1552 			   struct file *file)
1553 {
1554 	int res;
1555 	struct snd_card *card = substream->pcm->card;
1556 	int f_flags;
1557 
1558 	if (file)
1559 		f_flags = file->f_flags;
1560 	else
1561 		f_flags = substream->f_flags;
1562 
1563 	snd_power_lock(card);
1564 	if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1565 		res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1566 					       substream, f_flags);
1567 	snd_power_unlock(card);
1568 	return res;
1569 }
1570 
1571 /*
1572  * drain ioctl
1573  */
1574 
1575 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1576 {
1577 	struct snd_pcm_runtime *runtime = substream->runtime;
1578 	switch (runtime->status->state) {
1579 	case SNDRV_PCM_STATE_OPEN:
1580 	case SNDRV_PCM_STATE_DISCONNECTED:
1581 	case SNDRV_PCM_STATE_SUSPENDED:
1582 		return -EBADFD;
1583 	}
1584 	runtime->trigger_master = substream;
1585 	return 0;
1586 }
1587 
1588 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1589 {
1590 	struct snd_pcm_runtime *runtime = substream->runtime;
1591 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1592 		switch (runtime->status->state) {
1593 		case SNDRV_PCM_STATE_PREPARED:
1594 			/* start playback stream if possible */
1595 			if (! snd_pcm_playback_empty(substream)) {
1596 				snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1597 				snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1598 			} else {
1599 				runtime->status->state = SNDRV_PCM_STATE_SETUP;
1600 			}
1601 			break;
1602 		case SNDRV_PCM_STATE_RUNNING:
1603 			runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1604 			break;
1605 		case SNDRV_PCM_STATE_XRUN:
1606 			runtime->status->state = SNDRV_PCM_STATE_SETUP;
1607 			break;
1608 		default:
1609 			break;
1610 		}
1611 	} else {
1612 		/* stop running stream */
1613 		if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1614 			int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1615 				SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1616 			snd_pcm_do_stop(substream, new_state);
1617 			snd_pcm_post_stop(substream, new_state);
1618 		}
1619 	}
1620 
1621 	if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1622 	    runtime->trigger_master == substream &&
1623 	    (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1624 		return substream->ops->trigger(substream,
1625 					       SNDRV_PCM_TRIGGER_DRAIN);
1626 
1627 	return 0;
1628 }
1629 
1630 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1631 {
1632 }
1633 
1634 static const struct action_ops snd_pcm_action_drain_init = {
1635 	.pre_action = snd_pcm_pre_drain_init,
1636 	.do_action = snd_pcm_do_drain_init,
1637 	.post_action = snd_pcm_post_drain_init
1638 };
1639 
1640 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1641 
1642 /*
1643  * Drain the stream(s).
1644  * When the substream is linked, sync until the draining of all playback streams
1645  * is finished.
1646  * After this call, all streams are supposed to be either SETUP or DRAINING
1647  * (capture only) state.
1648  */
1649 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1650 			 struct file *file)
1651 {
1652 	struct snd_card *card;
1653 	struct snd_pcm_runtime *runtime;
1654 	struct snd_pcm_substream *s;
1655 	wait_queue_t wait;
1656 	int result = 0;
1657 	int nonblock = 0;
1658 
1659 	card = substream->pcm->card;
1660 	runtime = substream->runtime;
1661 
1662 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1663 		return -EBADFD;
1664 
1665 	snd_power_lock(card);
1666 	if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1667 		result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1668 		if (result < 0) {
1669 			snd_power_unlock(card);
1670 			return result;
1671 		}
1672 	}
1673 
1674 	if (file) {
1675 		if (file->f_flags & O_NONBLOCK)
1676 			nonblock = 1;
1677 	} else if (substream->f_flags & O_NONBLOCK)
1678 		nonblock = 1;
1679 
1680 	down_read(&snd_pcm_link_rwsem);
1681 	snd_pcm_stream_lock_irq(substream);
1682 	/* resume pause */
1683 	if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1684 		snd_pcm_pause(substream, 0);
1685 
1686 	/* pre-start/stop - all running streams are changed to DRAINING state */
1687 	result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1688 	if (result < 0)
1689 		goto unlock;
1690 	/* in non-blocking, we don't wait in ioctl but let caller poll */
1691 	if (nonblock) {
1692 		result = -EAGAIN;
1693 		goto unlock;
1694 	}
1695 
1696 	for (;;) {
1697 		long tout;
1698 		struct snd_pcm_runtime *to_check;
1699 		if (signal_pending(current)) {
1700 			result = -ERESTARTSYS;
1701 			break;
1702 		}
1703 		/* find a substream to drain */
1704 		to_check = NULL;
1705 		snd_pcm_group_for_each_entry(s, substream) {
1706 			if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1707 				continue;
1708 			runtime = s->runtime;
1709 			if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1710 				to_check = runtime;
1711 				break;
1712 			}
1713 		}
1714 		if (!to_check)
1715 			break; /* all drained */
1716 		init_waitqueue_entry(&wait, current);
1717 		add_wait_queue(&to_check->sleep, &wait);
1718 		snd_pcm_stream_unlock_irq(substream);
1719 		up_read(&snd_pcm_link_rwsem);
1720 		snd_power_unlock(card);
1721 		if (runtime->no_period_wakeup)
1722 			tout = MAX_SCHEDULE_TIMEOUT;
1723 		else {
1724 			tout = 10;
1725 			if (runtime->rate) {
1726 				long t = runtime->period_size * 2 / runtime->rate;
1727 				tout = max(t, tout);
1728 			}
1729 			tout = msecs_to_jiffies(tout * 1000);
1730 		}
1731 		tout = schedule_timeout_interruptible(tout);
1732 		snd_power_lock(card);
1733 		down_read(&snd_pcm_link_rwsem);
1734 		snd_pcm_stream_lock_irq(substream);
1735 		remove_wait_queue(&to_check->sleep, &wait);
1736 		if (card->shutdown) {
1737 			result = -ENODEV;
1738 			break;
1739 		}
1740 		if (tout == 0) {
1741 			if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1742 				result = -ESTRPIPE;
1743 			else {
1744 				dev_dbg(substream->pcm->card->dev,
1745 					"playback drain error (DMA or IRQ trouble?)\n");
1746 				snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1747 				result = -EIO;
1748 			}
1749 			break;
1750 		}
1751 	}
1752 
1753  unlock:
1754 	snd_pcm_stream_unlock_irq(substream);
1755 	up_read(&snd_pcm_link_rwsem);
1756 	snd_power_unlock(card);
1757 
1758 	return result;
1759 }
1760 
1761 /*
1762  * drop ioctl
1763  *
1764  * Immediately put all linked substreams into SETUP state.
1765  */
1766 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1767 {
1768 	struct snd_pcm_runtime *runtime;
1769 	int result = 0;
1770 
1771 	if (PCM_RUNTIME_CHECK(substream))
1772 		return -ENXIO;
1773 	runtime = substream->runtime;
1774 
1775 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1776 	    runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1777 	    runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1778 		return -EBADFD;
1779 
1780 	snd_pcm_stream_lock_irq(substream);
1781 	/* resume pause */
1782 	if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1783 		snd_pcm_pause(substream, 0);
1784 
1785 	snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1786 	/* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1787 	snd_pcm_stream_unlock_irq(substream);
1788 
1789 	return result;
1790 }
1791 
1792 
1793 static bool is_pcm_file(struct file *file)
1794 {
1795 	struct inode *inode = file_inode(file);
1796 	unsigned int minor;
1797 
1798 	if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1799 		return false;
1800 	minor = iminor(inode);
1801 	return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1802 		snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1803 }
1804 
1805 /*
1806  * PCM link handling
1807  */
1808 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1809 {
1810 	int res = 0;
1811 	struct snd_pcm_file *pcm_file;
1812 	struct snd_pcm_substream *substream1;
1813 	struct snd_pcm_group *group;
1814 	struct fd f = fdget(fd);
1815 
1816 	if (!f.file)
1817 		return -EBADFD;
1818 	if (!is_pcm_file(f.file)) {
1819 		res = -EBADFD;
1820 		goto _badf;
1821 	}
1822 	pcm_file = f.file->private_data;
1823 	substream1 = pcm_file->substream;
1824 	group = kmalloc(sizeof(*group), GFP_KERNEL);
1825 	if (!group) {
1826 		res = -ENOMEM;
1827 		goto _nolock;
1828 	}
1829 	down_write_nonblock(&snd_pcm_link_rwsem);
1830 	write_lock_irq(&snd_pcm_link_rwlock);
1831 	if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1832 	    substream->runtime->status->state != substream1->runtime->status->state ||
1833 	    substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1834 		res = -EBADFD;
1835 		goto _end;
1836 	}
1837 	if (snd_pcm_stream_linked(substream1)) {
1838 		res = -EALREADY;
1839 		goto _end;
1840 	}
1841 	if (!snd_pcm_stream_linked(substream)) {
1842 		substream->group = group;
1843 		group = NULL;
1844 		spin_lock_init(&substream->group->lock);
1845 		mutex_init(&substream->group->mutex);
1846 		INIT_LIST_HEAD(&substream->group->substreams);
1847 		list_add_tail(&substream->link_list, &substream->group->substreams);
1848 		substream->group->count = 1;
1849 	}
1850 	list_add_tail(&substream1->link_list, &substream->group->substreams);
1851 	substream->group->count++;
1852 	substream1->group = substream->group;
1853  _end:
1854 	write_unlock_irq(&snd_pcm_link_rwlock);
1855 	up_write(&snd_pcm_link_rwsem);
1856  _nolock:
1857 	snd_card_unref(substream1->pcm->card);
1858 	kfree(group);
1859  _badf:
1860 	fdput(f);
1861 	return res;
1862 }
1863 
1864 static void relink_to_local(struct snd_pcm_substream *substream)
1865 {
1866 	substream->group = &substream->self_group;
1867 	INIT_LIST_HEAD(&substream->self_group.substreams);
1868 	list_add_tail(&substream->link_list, &substream->self_group.substreams);
1869 }
1870 
1871 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1872 {
1873 	struct snd_pcm_substream *s;
1874 	int res = 0;
1875 
1876 	down_write_nonblock(&snd_pcm_link_rwsem);
1877 	write_lock_irq(&snd_pcm_link_rwlock);
1878 	if (!snd_pcm_stream_linked(substream)) {
1879 		res = -EALREADY;
1880 		goto _end;
1881 	}
1882 	list_del(&substream->link_list);
1883 	substream->group->count--;
1884 	if (substream->group->count == 1) {	/* detach the last stream, too */
1885 		snd_pcm_group_for_each_entry(s, substream) {
1886 			relink_to_local(s);
1887 			break;
1888 		}
1889 		kfree(substream->group);
1890 	}
1891 	relink_to_local(substream);
1892        _end:
1893 	write_unlock_irq(&snd_pcm_link_rwlock);
1894 	up_write(&snd_pcm_link_rwsem);
1895 	return res;
1896 }
1897 
1898 /*
1899  * hw configurator
1900  */
1901 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1902 			       struct snd_pcm_hw_rule *rule)
1903 {
1904 	struct snd_interval t;
1905 	snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1906 		     hw_param_interval_c(params, rule->deps[1]), &t);
1907 	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1908 }
1909 
1910 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1911 			       struct snd_pcm_hw_rule *rule)
1912 {
1913 	struct snd_interval t;
1914 	snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1915 		     hw_param_interval_c(params, rule->deps[1]), &t);
1916 	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1917 }
1918 
1919 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1920 				   struct snd_pcm_hw_rule *rule)
1921 {
1922 	struct snd_interval t;
1923 	snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1924 			 hw_param_interval_c(params, rule->deps[1]),
1925 			 (unsigned long) rule->private, &t);
1926 	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1927 }
1928 
1929 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1930 				   struct snd_pcm_hw_rule *rule)
1931 {
1932 	struct snd_interval t;
1933 	snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1934 			 (unsigned long) rule->private,
1935 			 hw_param_interval_c(params, rule->deps[1]), &t);
1936 	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1937 }
1938 
1939 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1940 				  struct snd_pcm_hw_rule *rule)
1941 {
1942 	unsigned int k;
1943 	const struct snd_interval *i =
1944 				hw_param_interval_c(params, rule->deps[0]);
1945 	struct snd_mask m;
1946 	struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1947 	snd_mask_any(&m);
1948 	for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1949 		int bits;
1950 		if (! snd_mask_test(mask, k))
1951 			continue;
1952 		bits = snd_pcm_format_physical_width(k);
1953 		if (bits <= 0)
1954 			continue; /* ignore invalid formats */
1955 		if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1956 			snd_mask_reset(&m, k);
1957 	}
1958 	return snd_mask_refine(mask, &m);
1959 }
1960 
1961 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1962 				       struct snd_pcm_hw_rule *rule)
1963 {
1964 	struct snd_interval t;
1965 	unsigned int k;
1966 	t.min = UINT_MAX;
1967 	t.max = 0;
1968 	t.openmin = 0;
1969 	t.openmax = 0;
1970 	for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1971 		int bits;
1972 		if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1973 			continue;
1974 		bits = snd_pcm_format_physical_width(k);
1975 		if (bits <= 0)
1976 			continue; /* ignore invalid formats */
1977 		if (t.min > (unsigned)bits)
1978 			t.min = bits;
1979 		if (t.max < (unsigned)bits)
1980 			t.max = bits;
1981 	}
1982 	t.integer = 1;
1983 	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1984 }
1985 
1986 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1987 #error "Change this table"
1988 #endif
1989 
1990 static const unsigned int rates[] = {
1991 	5512, 8000, 11025, 16000, 22050, 32000, 44100,
1992 	48000, 64000, 88200, 96000, 176400, 192000
1993 };
1994 
1995 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1996 	.count = ARRAY_SIZE(rates),
1997 	.list = rates,
1998 };
1999 
2000 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2001 				struct snd_pcm_hw_rule *rule)
2002 {
2003 	struct snd_pcm_hardware *hw = rule->private;
2004 	return snd_interval_list(hw_param_interval(params, rule->var),
2005 				 snd_pcm_known_rates.count,
2006 				 snd_pcm_known_rates.list, hw->rates);
2007 }
2008 
2009 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2010 					    struct snd_pcm_hw_rule *rule)
2011 {
2012 	struct snd_interval t;
2013 	struct snd_pcm_substream *substream = rule->private;
2014 	t.min = 0;
2015 	t.max = substream->buffer_bytes_max;
2016 	t.openmin = 0;
2017 	t.openmax = 0;
2018 	t.integer = 1;
2019 	return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2020 }
2021 
2022 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2023 {
2024 	struct snd_pcm_runtime *runtime = substream->runtime;
2025 	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2026 	int k, err;
2027 
2028 	for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2029 		snd_mask_any(constrs_mask(constrs, k));
2030 	}
2031 
2032 	for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2033 		snd_interval_any(constrs_interval(constrs, k));
2034 	}
2035 
2036 	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2037 	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2038 	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2039 	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2040 	snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2041 
2042 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2043 				   snd_pcm_hw_rule_format, NULL,
2044 				   SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2045 	if (err < 0)
2046 		return err;
2047 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2048 				  snd_pcm_hw_rule_sample_bits, NULL,
2049 				  SNDRV_PCM_HW_PARAM_FORMAT,
2050 				  SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2051 	if (err < 0)
2052 		return err;
2053 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2054 				  snd_pcm_hw_rule_div, NULL,
2055 				  SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2056 	if (err < 0)
2057 		return err;
2058 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2059 				  snd_pcm_hw_rule_mul, NULL,
2060 				  SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2061 	if (err < 0)
2062 		return err;
2063 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2064 				  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2065 				  SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2066 	if (err < 0)
2067 		return err;
2068 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2069 				  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2070 				  SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2071 	if (err < 0)
2072 		return err;
2073 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2074 				  snd_pcm_hw_rule_div, NULL,
2075 				  SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2076 	if (err < 0)
2077 		return err;
2078 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2079 				  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2080 				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2081 	if (err < 0)
2082 		return err;
2083 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2084 				  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2085 				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2086 	if (err < 0)
2087 		return err;
2088 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2089 				  snd_pcm_hw_rule_div, NULL,
2090 				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2091 	if (err < 0)
2092 		return err;
2093 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2094 				  snd_pcm_hw_rule_div, NULL,
2095 				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2096 	if (err < 0)
2097 		return err;
2098 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2099 				  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2100 				  SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2101 	if (err < 0)
2102 		return err;
2103 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2104 				  snd_pcm_hw_rule_muldivk, (void*) 1000000,
2105 				  SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2106 	if (err < 0)
2107 		return err;
2108 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2109 				  snd_pcm_hw_rule_mul, NULL,
2110 				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2111 	if (err < 0)
2112 		return err;
2113 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2114 				  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2115 				  SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2116 	if (err < 0)
2117 		return err;
2118 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2119 				  snd_pcm_hw_rule_muldivk, (void*) 1000000,
2120 				  SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2121 	if (err < 0)
2122 		return err;
2123 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2124 				  snd_pcm_hw_rule_muldivk, (void*) 8,
2125 				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2126 	if (err < 0)
2127 		return err;
2128 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2129 				  snd_pcm_hw_rule_muldivk, (void*) 8,
2130 				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2131 	if (err < 0)
2132 		return err;
2133 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2134 				  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2135 				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2136 	if (err < 0)
2137 		return err;
2138 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2139 				  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2140 				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2141 	if (err < 0)
2142 		return err;
2143 	return 0;
2144 }
2145 
2146 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2147 {
2148 	struct snd_pcm_runtime *runtime = substream->runtime;
2149 	struct snd_pcm_hardware *hw = &runtime->hw;
2150 	int err;
2151 	unsigned int mask = 0;
2152 
2153         if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2154 		mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2155         if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2156 		mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2157 	if (hw_support_mmap(substream)) {
2158 		if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2159 			mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2160 		if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2161 			mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2162 		if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2163 			mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2164 	}
2165 	err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2166 	if (err < 0)
2167 		return err;
2168 
2169 	err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2170 	if (err < 0)
2171 		return err;
2172 
2173 	err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2174 	if (err < 0)
2175 		return err;
2176 
2177 	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2178 					   hw->channels_min, hw->channels_max);
2179 	if (err < 0)
2180 		return err;
2181 
2182 	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2183 					   hw->rate_min, hw->rate_max);
2184 	if (err < 0)
2185 		return err;
2186 
2187 	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2188 					   hw->period_bytes_min, hw->period_bytes_max);
2189 	if (err < 0)
2190 		return err;
2191 
2192 	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2193 					   hw->periods_min, hw->periods_max);
2194 	if (err < 0)
2195 		return err;
2196 
2197 	err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2198 					   hw->period_bytes_min, hw->buffer_bytes_max);
2199 	if (err < 0)
2200 		return err;
2201 
2202 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2203 				  snd_pcm_hw_rule_buffer_bytes_max, substream,
2204 				  SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2205 	if (err < 0)
2206 		return err;
2207 
2208 	/* FIXME: remove */
2209 	if (runtime->dma_bytes) {
2210 		err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2211 		if (err < 0)
2212 			return err;
2213 	}
2214 
2215 	if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2216 		err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2217 					  snd_pcm_hw_rule_rate, hw,
2218 					  SNDRV_PCM_HW_PARAM_RATE, -1);
2219 		if (err < 0)
2220 			return err;
2221 	}
2222 
2223 	/* FIXME: this belong to lowlevel */
2224 	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2225 
2226 	return 0;
2227 }
2228 
2229 static void pcm_release_private(struct snd_pcm_substream *substream)
2230 {
2231 	snd_pcm_unlink(substream);
2232 }
2233 
2234 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2235 {
2236 	substream->ref_count--;
2237 	if (substream->ref_count > 0)
2238 		return;
2239 
2240 	snd_pcm_drop(substream);
2241 	if (substream->hw_opened) {
2242 		if (substream->ops->hw_free &&
2243 		    substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2244 			substream->ops->hw_free(substream);
2245 		substream->ops->close(substream);
2246 		substream->hw_opened = 0;
2247 	}
2248 	if (pm_qos_request_active(&substream->latency_pm_qos_req))
2249 		pm_qos_remove_request(&substream->latency_pm_qos_req);
2250 	if (substream->pcm_release) {
2251 		substream->pcm_release(substream);
2252 		substream->pcm_release = NULL;
2253 	}
2254 	snd_pcm_detach_substream(substream);
2255 }
2256 
2257 EXPORT_SYMBOL(snd_pcm_release_substream);
2258 
2259 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2260 			   struct file *file,
2261 			   struct snd_pcm_substream **rsubstream)
2262 {
2263 	struct snd_pcm_substream *substream;
2264 	int err;
2265 
2266 	err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2267 	if (err < 0)
2268 		return err;
2269 	if (substream->ref_count > 1) {
2270 		*rsubstream = substream;
2271 		return 0;
2272 	}
2273 
2274 	err = snd_pcm_hw_constraints_init(substream);
2275 	if (err < 0) {
2276 		pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2277 		goto error;
2278 	}
2279 
2280 	if ((err = substream->ops->open(substream)) < 0)
2281 		goto error;
2282 
2283 	substream->hw_opened = 1;
2284 
2285 	err = snd_pcm_hw_constraints_complete(substream);
2286 	if (err < 0) {
2287 		pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2288 		goto error;
2289 	}
2290 
2291 	*rsubstream = substream;
2292 	return 0;
2293 
2294  error:
2295 	snd_pcm_release_substream(substream);
2296 	return err;
2297 }
2298 
2299 EXPORT_SYMBOL(snd_pcm_open_substream);
2300 
2301 static int snd_pcm_open_file(struct file *file,
2302 			     struct snd_pcm *pcm,
2303 			     int stream)
2304 {
2305 	struct snd_pcm_file *pcm_file;
2306 	struct snd_pcm_substream *substream;
2307 	int err;
2308 
2309 	err = snd_pcm_open_substream(pcm, stream, file, &substream);
2310 	if (err < 0)
2311 		return err;
2312 
2313 	pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2314 	if (pcm_file == NULL) {
2315 		snd_pcm_release_substream(substream);
2316 		return -ENOMEM;
2317 	}
2318 	pcm_file->substream = substream;
2319 	if (substream->ref_count == 1) {
2320 		substream->file = pcm_file;
2321 		substream->pcm_release = pcm_release_private;
2322 	}
2323 	file->private_data = pcm_file;
2324 
2325 	return 0;
2326 }
2327 
2328 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2329 {
2330 	struct snd_pcm *pcm;
2331 	int err = nonseekable_open(inode, file);
2332 	if (err < 0)
2333 		return err;
2334 	pcm = snd_lookup_minor_data(iminor(inode),
2335 				    SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2336 	err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2337 	if (pcm)
2338 		snd_card_unref(pcm->card);
2339 	return err;
2340 }
2341 
2342 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2343 {
2344 	struct snd_pcm *pcm;
2345 	int err = nonseekable_open(inode, file);
2346 	if (err < 0)
2347 		return err;
2348 	pcm = snd_lookup_minor_data(iminor(inode),
2349 				    SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2350 	err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2351 	if (pcm)
2352 		snd_card_unref(pcm->card);
2353 	return err;
2354 }
2355 
2356 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2357 {
2358 	int err;
2359 	wait_queue_t wait;
2360 
2361 	if (pcm == NULL) {
2362 		err = -ENODEV;
2363 		goto __error1;
2364 	}
2365 	err = snd_card_file_add(pcm->card, file);
2366 	if (err < 0)
2367 		goto __error1;
2368 	if (!try_module_get(pcm->card->module)) {
2369 		err = -EFAULT;
2370 		goto __error2;
2371 	}
2372 	init_waitqueue_entry(&wait, current);
2373 	add_wait_queue(&pcm->open_wait, &wait);
2374 	mutex_lock(&pcm->open_mutex);
2375 	while (1) {
2376 		err = snd_pcm_open_file(file, pcm, stream);
2377 		if (err >= 0)
2378 			break;
2379 		if (err == -EAGAIN) {
2380 			if (file->f_flags & O_NONBLOCK) {
2381 				err = -EBUSY;
2382 				break;
2383 			}
2384 		} else
2385 			break;
2386 		set_current_state(TASK_INTERRUPTIBLE);
2387 		mutex_unlock(&pcm->open_mutex);
2388 		schedule();
2389 		mutex_lock(&pcm->open_mutex);
2390 		if (pcm->card->shutdown) {
2391 			err = -ENODEV;
2392 			break;
2393 		}
2394 		if (signal_pending(current)) {
2395 			err = -ERESTARTSYS;
2396 			break;
2397 		}
2398 	}
2399 	remove_wait_queue(&pcm->open_wait, &wait);
2400 	mutex_unlock(&pcm->open_mutex);
2401 	if (err < 0)
2402 		goto __error;
2403 	return err;
2404 
2405       __error:
2406 	module_put(pcm->card->module);
2407       __error2:
2408       	snd_card_file_remove(pcm->card, file);
2409       __error1:
2410       	return err;
2411 }
2412 
2413 static int snd_pcm_release(struct inode *inode, struct file *file)
2414 {
2415 	struct snd_pcm *pcm;
2416 	struct snd_pcm_substream *substream;
2417 	struct snd_pcm_file *pcm_file;
2418 
2419 	pcm_file = file->private_data;
2420 	substream = pcm_file->substream;
2421 	if (snd_BUG_ON(!substream))
2422 		return -ENXIO;
2423 	pcm = substream->pcm;
2424 	mutex_lock(&pcm->open_mutex);
2425 	snd_pcm_release_substream(substream);
2426 	kfree(pcm_file);
2427 	mutex_unlock(&pcm->open_mutex);
2428 	wake_up(&pcm->open_wait);
2429 	module_put(pcm->card->module);
2430 	snd_card_file_remove(pcm->card, file);
2431 	return 0;
2432 }
2433 
2434 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2435 						 snd_pcm_uframes_t frames)
2436 {
2437 	struct snd_pcm_runtime *runtime = substream->runtime;
2438 	snd_pcm_sframes_t appl_ptr;
2439 	snd_pcm_sframes_t ret;
2440 	snd_pcm_sframes_t hw_avail;
2441 
2442 	if (frames == 0)
2443 		return 0;
2444 
2445 	snd_pcm_stream_lock_irq(substream);
2446 	switch (runtime->status->state) {
2447 	case SNDRV_PCM_STATE_PREPARED:
2448 		break;
2449 	case SNDRV_PCM_STATE_DRAINING:
2450 	case SNDRV_PCM_STATE_RUNNING:
2451 		if (snd_pcm_update_hw_ptr(substream) >= 0)
2452 			break;
2453 		/* Fall through */
2454 	case SNDRV_PCM_STATE_XRUN:
2455 		ret = -EPIPE;
2456 		goto __end;
2457 	case SNDRV_PCM_STATE_SUSPENDED:
2458 		ret = -ESTRPIPE;
2459 		goto __end;
2460 	default:
2461 		ret = -EBADFD;
2462 		goto __end;
2463 	}
2464 
2465 	hw_avail = snd_pcm_playback_hw_avail(runtime);
2466 	if (hw_avail <= 0) {
2467 		ret = 0;
2468 		goto __end;
2469 	}
2470 	if (frames > (snd_pcm_uframes_t)hw_avail)
2471 		frames = hw_avail;
2472 	appl_ptr = runtime->control->appl_ptr - frames;
2473 	if (appl_ptr < 0)
2474 		appl_ptr += runtime->boundary;
2475 	runtime->control->appl_ptr = appl_ptr;
2476 	ret = frames;
2477  __end:
2478 	snd_pcm_stream_unlock_irq(substream);
2479 	return ret;
2480 }
2481 
2482 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2483 						snd_pcm_uframes_t frames)
2484 {
2485 	struct snd_pcm_runtime *runtime = substream->runtime;
2486 	snd_pcm_sframes_t appl_ptr;
2487 	snd_pcm_sframes_t ret;
2488 	snd_pcm_sframes_t hw_avail;
2489 
2490 	if (frames == 0)
2491 		return 0;
2492 
2493 	snd_pcm_stream_lock_irq(substream);
2494 	switch (runtime->status->state) {
2495 	case SNDRV_PCM_STATE_PREPARED:
2496 	case SNDRV_PCM_STATE_DRAINING:
2497 		break;
2498 	case SNDRV_PCM_STATE_RUNNING:
2499 		if (snd_pcm_update_hw_ptr(substream) >= 0)
2500 			break;
2501 		/* Fall through */
2502 	case SNDRV_PCM_STATE_XRUN:
2503 		ret = -EPIPE;
2504 		goto __end;
2505 	case SNDRV_PCM_STATE_SUSPENDED:
2506 		ret = -ESTRPIPE;
2507 		goto __end;
2508 	default:
2509 		ret = -EBADFD;
2510 		goto __end;
2511 	}
2512 
2513 	hw_avail = snd_pcm_capture_hw_avail(runtime);
2514 	if (hw_avail <= 0) {
2515 		ret = 0;
2516 		goto __end;
2517 	}
2518 	if (frames > (snd_pcm_uframes_t)hw_avail)
2519 		frames = hw_avail;
2520 	appl_ptr = runtime->control->appl_ptr - frames;
2521 	if (appl_ptr < 0)
2522 		appl_ptr += runtime->boundary;
2523 	runtime->control->appl_ptr = appl_ptr;
2524 	ret = frames;
2525  __end:
2526 	snd_pcm_stream_unlock_irq(substream);
2527 	return ret;
2528 }
2529 
2530 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2531 						  snd_pcm_uframes_t frames)
2532 {
2533 	struct snd_pcm_runtime *runtime = substream->runtime;
2534 	snd_pcm_sframes_t appl_ptr;
2535 	snd_pcm_sframes_t ret;
2536 	snd_pcm_sframes_t avail;
2537 
2538 	if (frames == 0)
2539 		return 0;
2540 
2541 	snd_pcm_stream_lock_irq(substream);
2542 	switch (runtime->status->state) {
2543 	case SNDRV_PCM_STATE_PREPARED:
2544 	case SNDRV_PCM_STATE_PAUSED:
2545 		break;
2546 	case SNDRV_PCM_STATE_DRAINING:
2547 	case SNDRV_PCM_STATE_RUNNING:
2548 		if (snd_pcm_update_hw_ptr(substream) >= 0)
2549 			break;
2550 		/* Fall through */
2551 	case SNDRV_PCM_STATE_XRUN:
2552 		ret = -EPIPE;
2553 		goto __end;
2554 	case SNDRV_PCM_STATE_SUSPENDED:
2555 		ret = -ESTRPIPE;
2556 		goto __end;
2557 	default:
2558 		ret = -EBADFD;
2559 		goto __end;
2560 	}
2561 
2562 	avail = snd_pcm_playback_avail(runtime);
2563 	if (avail <= 0) {
2564 		ret = 0;
2565 		goto __end;
2566 	}
2567 	if (frames > (snd_pcm_uframes_t)avail)
2568 		frames = avail;
2569 	appl_ptr = runtime->control->appl_ptr + frames;
2570 	if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2571 		appl_ptr -= runtime->boundary;
2572 	runtime->control->appl_ptr = appl_ptr;
2573 	ret = frames;
2574  __end:
2575 	snd_pcm_stream_unlock_irq(substream);
2576 	return ret;
2577 }
2578 
2579 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2580 						 snd_pcm_uframes_t frames)
2581 {
2582 	struct snd_pcm_runtime *runtime = substream->runtime;
2583 	snd_pcm_sframes_t appl_ptr;
2584 	snd_pcm_sframes_t ret;
2585 	snd_pcm_sframes_t avail;
2586 
2587 	if (frames == 0)
2588 		return 0;
2589 
2590 	snd_pcm_stream_lock_irq(substream);
2591 	switch (runtime->status->state) {
2592 	case SNDRV_PCM_STATE_PREPARED:
2593 	case SNDRV_PCM_STATE_DRAINING:
2594 	case SNDRV_PCM_STATE_PAUSED:
2595 		break;
2596 	case SNDRV_PCM_STATE_RUNNING:
2597 		if (snd_pcm_update_hw_ptr(substream) >= 0)
2598 			break;
2599 		/* Fall through */
2600 	case SNDRV_PCM_STATE_XRUN:
2601 		ret = -EPIPE;
2602 		goto __end;
2603 	case SNDRV_PCM_STATE_SUSPENDED:
2604 		ret = -ESTRPIPE;
2605 		goto __end;
2606 	default:
2607 		ret = -EBADFD;
2608 		goto __end;
2609 	}
2610 
2611 	avail = snd_pcm_capture_avail(runtime);
2612 	if (avail <= 0) {
2613 		ret = 0;
2614 		goto __end;
2615 	}
2616 	if (frames > (snd_pcm_uframes_t)avail)
2617 		frames = avail;
2618 	appl_ptr = runtime->control->appl_ptr + frames;
2619 	if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2620 		appl_ptr -= runtime->boundary;
2621 	runtime->control->appl_ptr = appl_ptr;
2622 	ret = frames;
2623  __end:
2624 	snd_pcm_stream_unlock_irq(substream);
2625 	return ret;
2626 }
2627 
2628 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2629 {
2630 	struct snd_pcm_runtime *runtime = substream->runtime;
2631 	int err;
2632 
2633 	snd_pcm_stream_lock_irq(substream);
2634 	switch (runtime->status->state) {
2635 	case SNDRV_PCM_STATE_DRAINING:
2636 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2637 			goto __badfd;
2638 		/* Fall through */
2639 	case SNDRV_PCM_STATE_RUNNING:
2640 		if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2641 			break;
2642 		/* Fall through */
2643 	case SNDRV_PCM_STATE_PREPARED:
2644 		err = 0;
2645 		break;
2646 	case SNDRV_PCM_STATE_SUSPENDED:
2647 		err = -ESTRPIPE;
2648 		break;
2649 	case SNDRV_PCM_STATE_XRUN:
2650 		err = -EPIPE;
2651 		break;
2652 	default:
2653 	      __badfd:
2654 		err = -EBADFD;
2655 		break;
2656 	}
2657 	snd_pcm_stream_unlock_irq(substream);
2658 	return err;
2659 }
2660 
2661 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2662 			 snd_pcm_sframes_t __user *res)
2663 {
2664 	struct snd_pcm_runtime *runtime = substream->runtime;
2665 	int err;
2666 	snd_pcm_sframes_t n = 0;
2667 
2668 	snd_pcm_stream_lock_irq(substream);
2669 	switch (runtime->status->state) {
2670 	case SNDRV_PCM_STATE_DRAINING:
2671 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2672 			goto __badfd;
2673 		/* Fall through */
2674 	case SNDRV_PCM_STATE_RUNNING:
2675 		if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2676 			break;
2677 		/* Fall through */
2678 	case SNDRV_PCM_STATE_PREPARED:
2679 	case SNDRV_PCM_STATE_SUSPENDED:
2680 		err = 0;
2681 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2682 			n = snd_pcm_playback_hw_avail(runtime);
2683 		else
2684 			n = snd_pcm_capture_avail(runtime);
2685 		n += runtime->delay;
2686 		break;
2687 	case SNDRV_PCM_STATE_XRUN:
2688 		err = -EPIPE;
2689 		break;
2690 	default:
2691 	      __badfd:
2692 		err = -EBADFD;
2693 		break;
2694 	}
2695 	snd_pcm_stream_unlock_irq(substream);
2696 	if (!err)
2697 		if (put_user(n, res))
2698 			err = -EFAULT;
2699 	return err;
2700 }
2701 
2702 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2703 			    struct snd_pcm_sync_ptr __user *_sync_ptr)
2704 {
2705 	struct snd_pcm_runtime *runtime = substream->runtime;
2706 	struct snd_pcm_sync_ptr sync_ptr;
2707 	volatile struct snd_pcm_mmap_status *status;
2708 	volatile struct snd_pcm_mmap_control *control;
2709 	int err;
2710 
2711 	memset(&sync_ptr, 0, sizeof(sync_ptr));
2712 	if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2713 		return -EFAULT;
2714 	if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2715 		return -EFAULT;
2716 	status = runtime->status;
2717 	control = runtime->control;
2718 	if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2719 		err = snd_pcm_hwsync(substream);
2720 		if (err < 0)
2721 			return err;
2722 	}
2723 	snd_pcm_stream_lock_irq(substream);
2724 	if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2725 		control->appl_ptr = sync_ptr.c.control.appl_ptr;
2726 	else
2727 		sync_ptr.c.control.appl_ptr = control->appl_ptr;
2728 	if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2729 		control->avail_min = sync_ptr.c.control.avail_min;
2730 	else
2731 		sync_ptr.c.control.avail_min = control->avail_min;
2732 	sync_ptr.s.status.state = status->state;
2733 	sync_ptr.s.status.hw_ptr = status->hw_ptr;
2734 	sync_ptr.s.status.tstamp = status->tstamp;
2735 	sync_ptr.s.status.suspended_state = status->suspended_state;
2736 	snd_pcm_stream_unlock_irq(substream);
2737 	if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2738 		return -EFAULT;
2739 	return 0;
2740 }
2741 
2742 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2743 {
2744 	struct snd_pcm_runtime *runtime = substream->runtime;
2745 	int arg;
2746 
2747 	if (get_user(arg, _arg))
2748 		return -EFAULT;
2749 	if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2750 		return -EINVAL;
2751 	runtime->tstamp_type = arg;
2752 	return 0;
2753 }
2754 
2755 static int snd_pcm_common_ioctl1(struct file *file,
2756 				 struct snd_pcm_substream *substream,
2757 				 unsigned int cmd, void __user *arg)
2758 {
2759 	switch (cmd) {
2760 	case SNDRV_PCM_IOCTL_PVERSION:
2761 		return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2762 	case SNDRV_PCM_IOCTL_INFO:
2763 		return snd_pcm_info_user(substream, arg);
2764 	case SNDRV_PCM_IOCTL_TSTAMP:	/* just for compatibility */
2765 		return 0;
2766 	case SNDRV_PCM_IOCTL_TTSTAMP:
2767 		return snd_pcm_tstamp(substream, arg);
2768 	case SNDRV_PCM_IOCTL_HW_REFINE:
2769 		return snd_pcm_hw_refine_user(substream, arg);
2770 	case SNDRV_PCM_IOCTL_HW_PARAMS:
2771 		return snd_pcm_hw_params_user(substream, arg);
2772 	case SNDRV_PCM_IOCTL_HW_FREE:
2773 		return snd_pcm_hw_free(substream);
2774 	case SNDRV_PCM_IOCTL_SW_PARAMS:
2775 		return snd_pcm_sw_params_user(substream, arg);
2776 	case SNDRV_PCM_IOCTL_STATUS:
2777 		return snd_pcm_status_user(substream, arg, false);
2778 	case SNDRV_PCM_IOCTL_STATUS_EXT:
2779 		return snd_pcm_status_user(substream, arg, true);
2780 	case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2781 		return snd_pcm_channel_info_user(substream, arg);
2782 	case SNDRV_PCM_IOCTL_PREPARE:
2783 		return snd_pcm_prepare(substream, file);
2784 	case SNDRV_PCM_IOCTL_RESET:
2785 		return snd_pcm_reset(substream);
2786 	case SNDRV_PCM_IOCTL_START:
2787 		return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2788 	case SNDRV_PCM_IOCTL_LINK:
2789 		return snd_pcm_link(substream, (int)(unsigned long) arg);
2790 	case SNDRV_PCM_IOCTL_UNLINK:
2791 		return snd_pcm_unlink(substream);
2792 	case SNDRV_PCM_IOCTL_RESUME:
2793 		return snd_pcm_resume(substream);
2794 	case SNDRV_PCM_IOCTL_XRUN:
2795 		return snd_pcm_xrun(substream);
2796 	case SNDRV_PCM_IOCTL_HWSYNC:
2797 		return snd_pcm_hwsync(substream);
2798 	case SNDRV_PCM_IOCTL_DELAY:
2799 		return snd_pcm_delay(substream, arg);
2800 	case SNDRV_PCM_IOCTL_SYNC_PTR:
2801 		return snd_pcm_sync_ptr(substream, arg);
2802 #ifdef CONFIG_SND_SUPPORT_OLD_API
2803 	case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2804 		return snd_pcm_hw_refine_old_user(substream, arg);
2805 	case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2806 		return snd_pcm_hw_params_old_user(substream, arg);
2807 #endif
2808 	case SNDRV_PCM_IOCTL_DRAIN:
2809 		return snd_pcm_drain(substream, file);
2810 	case SNDRV_PCM_IOCTL_DROP:
2811 		return snd_pcm_drop(substream);
2812 	case SNDRV_PCM_IOCTL_PAUSE:
2813 	{
2814 		int res;
2815 		snd_pcm_stream_lock_irq(substream);
2816 		res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2817 		snd_pcm_stream_unlock_irq(substream);
2818 		return res;
2819 	}
2820 	}
2821 	pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2822 	return -ENOTTY;
2823 }
2824 
2825 static int snd_pcm_playback_ioctl1(struct file *file,
2826 				   struct snd_pcm_substream *substream,
2827 				   unsigned int cmd, void __user *arg)
2828 {
2829 	if (snd_BUG_ON(!substream))
2830 		return -ENXIO;
2831 	if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2832 		return -EINVAL;
2833 	switch (cmd) {
2834 	case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2835 	{
2836 		struct snd_xferi xferi;
2837 		struct snd_xferi __user *_xferi = arg;
2838 		struct snd_pcm_runtime *runtime = substream->runtime;
2839 		snd_pcm_sframes_t result;
2840 		if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2841 			return -EBADFD;
2842 		if (put_user(0, &_xferi->result))
2843 			return -EFAULT;
2844 		if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2845 			return -EFAULT;
2846 		result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2847 		__put_user(result, &_xferi->result);
2848 		return result < 0 ? result : 0;
2849 	}
2850 	case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2851 	{
2852 		struct snd_xfern xfern;
2853 		struct snd_xfern __user *_xfern = arg;
2854 		struct snd_pcm_runtime *runtime = substream->runtime;
2855 		void __user **bufs;
2856 		snd_pcm_sframes_t result;
2857 		if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2858 			return -EBADFD;
2859 		if (runtime->channels > 128)
2860 			return -EINVAL;
2861 		if (put_user(0, &_xfern->result))
2862 			return -EFAULT;
2863 		if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2864 			return -EFAULT;
2865 
2866 		bufs = memdup_user(xfern.bufs,
2867 				   sizeof(void *) * runtime->channels);
2868 		if (IS_ERR(bufs))
2869 			return PTR_ERR(bufs);
2870 		result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2871 		kfree(bufs);
2872 		__put_user(result, &_xfern->result);
2873 		return result < 0 ? result : 0;
2874 	}
2875 	case SNDRV_PCM_IOCTL_REWIND:
2876 	{
2877 		snd_pcm_uframes_t frames;
2878 		snd_pcm_uframes_t __user *_frames = arg;
2879 		snd_pcm_sframes_t result;
2880 		if (get_user(frames, _frames))
2881 			return -EFAULT;
2882 		if (put_user(0, _frames))
2883 			return -EFAULT;
2884 		result = snd_pcm_playback_rewind(substream, frames);
2885 		__put_user(result, _frames);
2886 		return result < 0 ? result : 0;
2887 	}
2888 	case SNDRV_PCM_IOCTL_FORWARD:
2889 	{
2890 		snd_pcm_uframes_t frames;
2891 		snd_pcm_uframes_t __user *_frames = arg;
2892 		snd_pcm_sframes_t result;
2893 		if (get_user(frames, _frames))
2894 			return -EFAULT;
2895 		if (put_user(0, _frames))
2896 			return -EFAULT;
2897 		result = snd_pcm_playback_forward(substream, frames);
2898 		__put_user(result, _frames);
2899 		return result < 0 ? result : 0;
2900 	}
2901 	}
2902 	return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2903 }
2904 
2905 static int snd_pcm_capture_ioctl1(struct file *file,
2906 				  struct snd_pcm_substream *substream,
2907 				  unsigned int cmd, void __user *arg)
2908 {
2909 	if (snd_BUG_ON(!substream))
2910 		return -ENXIO;
2911 	if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2912 		return -EINVAL;
2913 	switch (cmd) {
2914 	case SNDRV_PCM_IOCTL_READI_FRAMES:
2915 	{
2916 		struct snd_xferi xferi;
2917 		struct snd_xferi __user *_xferi = arg;
2918 		struct snd_pcm_runtime *runtime = substream->runtime;
2919 		snd_pcm_sframes_t result;
2920 		if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2921 			return -EBADFD;
2922 		if (put_user(0, &_xferi->result))
2923 			return -EFAULT;
2924 		if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2925 			return -EFAULT;
2926 		result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2927 		__put_user(result, &_xferi->result);
2928 		return result < 0 ? result : 0;
2929 	}
2930 	case SNDRV_PCM_IOCTL_READN_FRAMES:
2931 	{
2932 		struct snd_xfern xfern;
2933 		struct snd_xfern __user *_xfern = arg;
2934 		struct snd_pcm_runtime *runtime = substream->runtime;
2935 		void *bufs;
2936 		snd_pcm_sframes_t result;
2937 		if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2938 			return -EBADFD;
2939 		if (runtime->channels > 128)
2940 			return -EINVAL;
2941 		if (put_user(0, &_xfern->result))
2942 			return -EFAULT;
2943 		if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2944 			return -EFAULT;
2945 
2946 		bufs = memdup_user(xfern.bufs,
2947 				   sizeof(void *) * runtime->channels);
2948 		if (IS_ERR(bufs))
2949 			return PTR_ERR(bufs);
2950 		result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2951 		kfree(bufs);
2952 		__put_user(result, &_xfern->result);
2953 		return result < 0 ? result : 0;
2954 	}
2955 	case SNDRV_PCM_IOCTL_REWIND:
2956 	{
2957 		snd_pcm_uframes_t frames;
2958 		snd_pcm_uframes_t __user *_frames = arg;
2959 		snd_pcm_sframes_t result;
2960 		if (get_user(frames, _frames))
2961 			return -EFAULT;
2962 		if (put_user(0, _frames))
2963 			return -EFAULT;
2964 		result = snd_pcm_capture_rewind(substream, frames);
2965 		__put_user(result, _frames);
2966 		return result < 0 ? result : 0;
2967 	}
2968 	case SNDRV_PCM_IOCTL_FORWARD:
2969 	{
2970 		snd_pcm_uframes_t frames;
2971 		snd_pcm_uframes_t __user *_frames = arg;
2972 		snd_pcm_sframes_t result;
2973 		if (get_user(frames, _frames))
2974 			return -EFAULT;
2975 		if (put_user(0, _frames))
2976 			return -EFAULT;
2977 		result = snd_pcm_capture_forward(substream, frames);
2978 		__put_user(result, _frames);
2979 		return result < 0 ? result : 0;
2980 	}
2981 	}
2982 	return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2983 }
2984 
2985 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2986 				   unsigned long arg)
2987 {
2988 	struct snd_pcm_file *pcm_file;
2989 
2990 	pcm_file = file->private_data;
2991 
2992 	if (((cmd >> 8) & 0xff) != 'A')
2993 		return -ENOTTY;
2994 
2995 	return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2996 				       (void __user *)arg);
2997 }
2998 
2999 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3000 				  unsigned long arg)
3001 {
3002 	struct snd_pcm_file *pcm_file;
3003 
3004 	pcm_file = file->private_data;
3005 
3006 	if (((cmd >> 8) & 0xff) != 'A')
3007 		return -ENOTTY;
3008 
3009 	return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3010 				      (void __user *)arg);
3011 }
3012 
3013 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3014 			 unsigned int cmd, void *arg)
3015 {
3016 	mm_segment_t fs;
3017 	int result;
3018 
3019 	fs = snd_enter_user();
3020 	switch (substream->stream) {
3021 	case SNDRV_PCM_STREAM_PLAYBACK:
3022 		result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3023 						 (void __user *)arg);
3024 		break;
3025 	case SNDRV_PCM_STREAM_CAPTURE:
3026 		result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3027 						(void __user *)arg);
3028 		break;
3029 	default:
3030 		result = -EINVAL;
3031 		break;
3032 	}
3033 	snd_leave_user(fs);
3034 	return result;
3035 }
3036 
3037 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3038 
3039 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3040 			    loff_t * offset)
3041 {
3042 	struct snd_pcm_file *pcm_file;
3043 	struct snd_pcm_substream *substream;
3044 	struct snd_pcm_runtime *runtime;
3045 	snd_pcm_sframes_t result;
3046 
3047 	pcm_file = file->private_data;
3048 	substream = pcm_file->substream;
3049 	if (PCM_RUNTIME_CHECK(substream))
3050 		return -ENXIO;
3051 	runtime = substream->runtime;
3052 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3053 		return -EBADFD;
3054 	if (!frame_aligned(runtime, count))
3055 		return -EINVAL;
3056 	count = bytes_to_frames(runtime, count);
3057 	result = snd_pcm_lib_read(substream, buf, count);
3058 	if (result > 0)
3059 		result = frames_to_bytes(runtime, result);
3060 	return result;
3061 }
3062 
3063 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3064 			     size_t count, loff_t * offset)
3065 {
3066 	struct snd_pcm_file *pcm_file;
3067 	struct snd_pcm_substream *substream;
3068 	struct snd_pcm_runtime *runtime;
3069 	snd_pcm_sframes_t result;
3070 
3071 	pcm_file = file->private_data;
3072 	substream = pcm_file->substream;
3073 	if (PCM_RUNTIME_CHECK(substream))
3074 		return -ENXIO;
3075 	runtime = substream->runtime;
3076 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3077 		return -EBADFD;
3078 	if (!frame_aligned(runtime, count))
3079 		return -EINVAL;
3080 	count = bytes_to_frames(runtime, count);
3081 	result = snd_pcm_lib_write(substream, buf, count);
3082 	if (result > 0)
3083 		result = frames_to_bytes(runtime, result);
3084 	return result;
3085 }
3086 
3087 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
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 	unsigned long i;
3094 	void __user **bufs;
3095 	snd_pcm_uframes_t frames;
3096 
3097 	pcm_file = iocb->ki_filp->private_data;
3098 	substream = pcm_file->substream;
3099 	if (PCM_RUNTIME_CHECK(substream))
3100 		return -ENXIO;
3101 	runtime = substream->runtime;
3102 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3103 		return -EBADFD;
3104 	if (!iter_is_iovec(to))
3105 		return -EINVAL;
3106 	if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3107 		return -EINVAL;
3108 	if (!frame_aligned(runtime, to->iov->iov_len))
3109 		return -EINVAL;
3110 	frames = bytes_to_samples(runtime, to->iov->iov_len);
3111 	bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
3112 	if (bufs == NULL)
3113 		return -ENOMEM;
3114 	for (i = 0; i < to->nr_segs; ++i)
3115 		bufs[i] = to->iov[i].iov_base;
3116 	result = snd_pcm_lib_readv(substream, bufs, frames);
3117 	if (result > 0)
3118 		result = frames_to_bytes(runtime, result);
3119 	kfree(bufs);
3120 	return result;
3121 }
3122 
3123 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3124 {
3125 	struct snd_pcm_file *pcm_file;
3126 	struct snd_pcm_substream *substream;
3127 	struct snd_pcm_runtime *runtime;
3128 	snd_pcm_sframes_t result;
3129 	unsigned long i;
3130 	void __user **bufs;
3131 	snd_pcm_uframes_t frames;
3132 
3133 	pcm_file = iocb->ki_filp->private_data;
3134 	substream = pcm_file->substream;
3135 	if (PCM_RUNTIME_CHECK(substream))
3136 		return -ENXIO;
3137 	runtime = substream->runtime;
3138 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3139 		return -EBADFD;
3140 	if (!iter_is_iovec(from))
3141 		return -EINVAL;
3142 	if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3143 	    !frame_aligned(runtime, from->iov->iov_len))
3144 		return -EINVAL;
3145 	frames = bytes_to_samples(runtime, from->iov->iov_len);
3146 	bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
3147 	if (bufs == NULL)
3148 		return -ENOMEM;
3149 	for (i = 0; i < from->nr_segs; ++i)
3150 		bufs[i] = from->iov[i].iov_base;
3151 	result = snd_pcm_lib_writev(substream, bufs, frames);
3152 	if (result > 0)
3153 		result = frames_to_bytes(runtime, result);
3154 	kfree(bufs);
3155 	return result;
3156 }
3157 
3158 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3159 {
3160 	struct snd_pcm_file *pcm_file;
3161 	struct snd_pcm_substream *substream;
3162 	struct snd_pcm_runtime *runtime;
3163         unsigned int mask;
3164 	snd_pcm_uframes_t avail;
3165 
3166 	pcm_file = file->private_data;
3167 
3168 	substream = pcm_file->substream;
3169 	if (PCM_RUNTIME_CHECK(substream))
3170 		return POLLOUT | POLLWRNORM | POLLERR;
3171 	runtime = substream->runtime;
3172 
3173 	poll_wait(file, &runtime->sleep, wait);
3174 
3175 	snd_pcm_stream_lock_irq(substream);
3176 	avail = snd_pcm_playback_avail(runtime);
3177 	switch (runtime->status->state) {
3178 	case SNDRV_PCM_STATE_RUNNING:
3179 	case SNDRV_PCM_STATE_PREPARED:
3180 	case SNDRV_PCM_STATE_PAUSED:
3181 		if (avail >= runtime->control->avail_min) {
3182 			mask = POLLOUT | POLLWRNORM;
3183 			break;
3184 		}
3185 		/* Fall through */
3186 	case SNDRV_PCM_STATE_DRAINING:
3187 		mask = 0;
3188 		break;
3189 	default:
3190 		mask = POLLOUT | POLLWRNORM | POLLERR;
3191 		break;
3192 	}
3193 	snd_pcm_stream_unlock_irq(substream);
3194 	return mask;
3195 }
3196 
3197 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3198 {
3199 	struct snd_pcm_file *pcm_file;
3200 	struct snd_pcm_substream *substream;
3201 	struct snd_pcm_runtime *runtime;
3202         unsigned int mask;
3203 	snd_pcm_uframes_t avail;
3204 
3205 	pcm_file = file->private_data;
3206 
3207 	substream = pcm_file->substream;
3208 	if (PCM_RUNTIME_CHECK(substream))
3209 		return POLLIN | POLLRDNORM | POLLERR;
3210 	runtime = substream->runtime;
3211 
3212 	poll_wait(file, &runtime->sleep, wait);
3213 
3214 	snd_pcm_stream_lock_irq(substream);
3215 	avail = snd_pcm_capture_avail(runtime);
3216 	switch (runtime->status->state) {
3217 	case SNDRV_PCM_STATE_RUNNING:
3218 	case SNDRV_PCM_STATE_PREPARED:
3219 	case SNDRV_PCM_STATE_PAUSED:
3220 		if (avail >= runtime->control->avail_min) {
3221 			mask = POLLIN | POLLRDNORM;
3222 			break;
3223 		}
3224 		mask = 0;
3225 		break;
3226 	case SNDRV_PCM_STATE_DRAINING:
3227 		if (avail > 0) {
3228 			mask = POLLIN | POLLRDNORM;
3229 			break;
3230 		}
3231 		/* Fall through */
3232 	default:
3233 		mask = POLLIN | POLLRDNORM | POLLERR;
3234 		break;
3235 	}
3236 	snd_pcm_stream_unlock_irq(substream);
3237 	return mask;
3238 }
3239 
3240 /*
3241  * mmap support
3242  */
3243 
3244 /*
3245  * Only on coherent architectures, we can mmap the status and the control records
3246  * for effcient data transfer.  On others, we have to use HWSYNC ioctl...
3247  */
3248 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3249 /*
3250  * mmap status record
3251  */
3252 static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3253 {
3254 	struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3255 	struct snd_pcm_runtime *runtime;
3256 
3257 	if (substream == NULL)
3258 		return VM_FAULT_SIGBUS;
3259 	runtime = substream->runtime;
3260 	vmf->page = virt_to_page(runtime->status);
3261 	get_page(vmf->page);
3262 	return 0;
3263 }
3264 
3265 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3266 {
3267 	.fault =	snd_pcm_mmap_status_fault,
3268 };
3269 
3270 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3271 			       struct vm_area_struct *area)
3272 {
3273 	long size;
3274 	if (!(area->vm_flags & VM_READ))
3275 		return -EINVAL;
3276 	size = area->vm_end - area->vm_start;
3277 	if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3278 		return -EINVAL;
3279 	area->vm_ops = &snd_pcm_vm_ops_status;
3280 	area->vm_private_data = substream;
3281 	area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3282 	return 0;
3283 }
3284 
3285 /*
3286  * mmap control record
3287  */
3288 static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3289 {
3290 	struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3291 	struct snd_pcm_runtime *runtime;
3292 
3293 	if (substream == NULL)
3294 		return VM_FAULT_SIGBUS;
3295 	runtime = substream->runtime;
3296 	vmf->page = virt_to_page(runtime->control);
3297 	get_page(vmf->page);
3298 	return 0;
3299 }
3300 
3301 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3302 {
3303 	.fault =	snd_pcm_mmap_control_fault,
3304 };
3305 
3306 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3307 				struct vm_area_struct *area)
3308 {
3309 	long size;
3310 	if (!(area->vm_flags & VM_READ))
3311 		return -EINVAL;
3312 	size = area->vm_end - area->vm_start;
3313 	if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3314 		return -EINVAL;
3315 	area->vm_ops = &snd_pcm_vm_ops_control;
3316 	area->vm_private_data = substream;
3317 	area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3318 	return 0;
3319 }
3320 #else /* ! coherent mmap */
3321 /*
3322  * don't support mmap for status and control records.
3323  */
3324 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3325 			       struct vm_area_struct *area)
3326 {
3327 	return -ENXIO;
3328 }
3329 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3330 				struct vm_area_struct *area)
3331 {
3332 	return -ENXIO;
3333 }
3334 #endif /* coherent mmap */
3335 
3336 static inline struct page *
3337 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3338 {
3339 	void *vaddr = substream->runtime->dma_area + ofs;
3340 	return virt_to_page(vaddr);
3341 }
3342 
3343 /*
3344  * fault callback for mmapping a RAM page
3345  */
3346 static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3347 {
3348 	struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3349 	struct snd_pcm_runtime *runtime;
3350 	unsigned long offset;
3351 	struct page * page;
3352 	size_t dma_bytes;
3353 
3354 	if (substream == NULL)
3355 		return VM_FAULT_SIGBUS;
3356 	runtime = substream->runtime;
3357 	offset = vmf->pgoff << PAGE_SHIFT;
3358 	dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3359 	if (offset > dma_bytes - PAGE_SIZE)
3360 		return VM_FAULT_SIGBUS;
3361 	if (substream->ops->page)
3362 		page = substream->ops->page(substream, offset);
3363 	else
3364 		page = snd_pcm_default_page_ops(substream, offset);
3365 	if (!page)
3366 		return VM_FAULT_SIGBUS;
3367 	get_page(page);
3368 	vmf->page = page;
3369 	return 0;
3370 }
3371 
3372 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3373 	.open =		snd_pcm_mmap_data_open,
3374 	.close =	snd_pcm_mmap_data_close,
3375 };
3376 
3377 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3378 	.open =		snd_pcm_mmap_data_open,
3379 	.close =	snd_pcm_mmap_data_close,
3380 	.fault =	snd_pcm_mmap_data_fault,
3381 };
3382 
3383 /*
3384  * mmap the DMA buffer on RAM
3385  */
3386 
3387 /**
3388  * snd_pcm_lib_default_mmap - Default PCM data mmap function
3389  * @substream: PCM substream
3390  * @area: VMA
3391  *
3392  * This is the default mmap handler for PCM data.  When mmap pcm_ops is NULL,
3393  * this function is invoked implicitly.
3394  */
3395 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3396 			     struct vm_area_struct *area)
3397 {
3398 	area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3399 #ifdef CONFIG_GENERIC_ALLOCATOR
3400 	if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3401 		area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3402 		return remap_pfn_range(area, area->vm_start,
3403 				substream->dma_buffer.addr >> PAGE_SHIFT,
3404 				area->vm_end - area->vm_start, area->vm_page_prot);
3405 	}
3406 #endif /* CONFIG_GENERIC_ALLOCATOR */
3407 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3408 	if (!substream->ops->page &&
3409 	    substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3410 		return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3411 					 area,
3412 					 substream->runtime->dma_area,
3413 					 substream->runtime->dma_addr,
3414 					 area->vm_end - area->vm_start);
3415 #endif /* CONFIG_X86 */
3416 	/* mmap with fault handler */
3417 	area->vm_ops = &snd_pcm_vm_ops_data_fault;
3418 	return 0;
3419 }
3420 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3421 
3422 /*
3423  * mmap the DMA buffer on I/O memory area
3424  */
3425 #if SNDRV_PCM_INFO_MMAP_IOMEM
3426 /**
3427  * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3428  * @substream: PCM substream
3429  * @area: VMA
3430  *
3431  * When your hardware uses the iomapped pages as the hardware buffer and
3432  * wants to mmap it, pass this function as mmap pcm_ops.  Note that this
3433  * is supposed to work only on limited architectures.
3434  */
3435 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3436 			   struct vm_area_struct *area)
3437 {
3438 	struct snd_pcm_runtime *runtime = substream->runtime;;
3439 
3440 	area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3441 	return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3442 }
3443 
3444 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3445 #endif /* SNDRV_PCM_INFO_MMAP */
3446 
3447 /*
3448  * mmap DMA buffer
3449  */
3450 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3451 		      struct vm_area_struct *area)
3452 {
3453 	struct snd_pcm_runtime *runtime;
3454 	long size;
3455 	unsigned long offset;
3456 	size_t dma_bytes;
3457 	int err;
3458 
3459 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3460 		if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3461 			return -EINVAL;
3462 	} else {
3463 		if (!(area->vm_flags & VM_READ))
3464 			return -EINVAL;
3465 	}
3466 	runtime = substream->runtime;
3467 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3468 		return -EBADFD;
3469 	if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3470 		return -ENXIO;
3471 	if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3472 	    runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3473 		return -EINVAL;
3474 	size = area->vm_end - area->vm_start;
3475 	offset = area->vm_pgoff << PAGE_SHIFT;
3476 	dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3477 	if ((size_t)size > dma_bytes)
3478 		return -EINVAL;
3479 	if (offset > dma_bytes - size)
3480 		return -EINVAL;
3481 
3482 	area->vm_ops = &snd_pcm_vm_ops_data;
3483 	area->vm_private_data = substream;
3484 	if (substream->ops->mmap)
3485 		err = substream->ops->mmap(substream, area);
3486 	else
3487 		err = snd_pcm_lib_default_mmap(substream, area);
3488 	if (!err)
3489 		atomic_inc(&substream->mmap_count);
3490 	return err;
3491 }
3492 
3493 EXPORT_SYMBOL(snd_pcm_mmap_data);
3494 
3495 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3496 {
3497 	struct snd_pcm_file * pcm_file;
3498 	struct snd_pcm_substream *substream;
3499 	unsigned long offset;
3500 
3501 	pcm_file = file->private_data;
3502 	substream = pcm_file->substream;
3503 	if (PCM_RUNTIME_CHECK(substream))
3504 		return -ENXIO;
3505 
3506 	offset = area->vm_pgoff << PAGE_SHIFT;
3507 	switch (offset) {
3508 	case SNDRV_PCM_MMAP_OFFSET_STATUS:
3509 		if (pcm_file->no_compat_mmap)
3510 			return -ENXIO;
3511 		return snd_pcm_mmap_status(substream, file, area);
3512 	case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3513 		if (pcm_file->no_compat_mmap)
3514 			return -ENXIO;
3515 		return snd_pcm_mmap_control(substream, file, area);
3516 	default:
3517 		return snd_pcm_mmap_data(substream, file, area);
3518 	}
3519 	return 0;
3520 }
3521 
3522 static int snd_pcm_fasync(int fd, struct file * file, int on)
3523 {
3524 	struct snd_pcm_file * pcm_file;
3525 	struct snd_pcm_substream *substream;
3526 	struct snd_pcm_runtime *runtime;
3527 
3528 	pcm_file = file->private_data;
3529 	substream = pcm_file->substream;
3530 	if (PCM_RUNTIME_CHECK(substream))
3531 		return -ENXIO;
3532 	runtime = substream->runtime;
3533 	return fasync_helper(fd, file, on, &runtime->fasync);
3534 }
3535 
3536 /*
3537  * ioctl32 compat
3538  */
3539 #ifdef CONFIG_COMPAT
3540 #include "pcm_compat.c"
3541 #else
3542 #define snd_pcm_ioctl_compat	NULL
3543 #endif
3544 
3545 /*
3546  *  To be removed helpers to keep binary compatibility
3547  */
3548 
3549 #ifdef CONFIG_SND_SUPPORT_OLD_API
3550 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3551 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3552 
3553 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3554 					       struct snd_pcm_hw_params_old *oparams)
3555 {
3556 	unsigned int i;
3557 
3558 	memset(params, 0, sizeof(*params));
3559 	params->flags = oparams->flags;
3560 	for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3561 		params->masks[i].bits[0] = oparams->masks[i];
3562 	memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3563 	params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3564 	params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3565 	params->info = oparams->info;
3566 	params->msbits = oparams->msbits;
3567 	params->rate_num = oparams->rate_num;
3568 	params->rate_den = oparams->rate_den;
3569 	params->fifo_size = oparams->fifo_size;
3570 }
3571 
3572 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3573 					     struct snd_pcm_hw_params *params)
3574 {
3575 	unsigned int i;
3576 
3577 	memset(oparams, 0, sizeof(*oparams));
3578 	oparams->flags = params->flags;
3579 	for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3580 		oparams->masks[i] = params->masks[i].bits[0];
3581 	memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3582 	oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3583 	oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3584 	oparams->info = params->info;
3585 	oparams->msbits = params->msbits;
3586 	oparams->rate_num = params->rate_num;
3587 	oparams->rate_den = params->rate_den;
3588 	oparams->fifo_size = params->fifo_size;
3589 }
3590 
3591 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3592 				      struct snd_pcm_hw_params_old __user * _oparams)
3593 {
3594 	struct snd_pcm_hw_params *params;
3595 	struct snd_pcm_hw_params_old *oparams = NULL;
3596 	int err;
3597 
3598 	params = kmalloc(sizeof(*params), GFP_KERNEL);
3599 	if (!params)
3600 		return -ENOMEM;
3601 
3602 	oparams = memdup_user(_oparams, sizeof(*oparams));
3603 	if (IS_ERR(oparams)) {
3604 		err = PTR_ERR(oparams);
3605 		goto out;
3606 	}
3607 	snd_pcm_hw_convert_from_old_params(params, oparams);
3608 	err = snd_pcm_hw_refine(substream, params);
3609 	snd_pcm_hw_convert_to_old_params(oparams, params);
3610 	if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3611 		if (!err)
3612 			err = -EFAULT;
3613 	}
3614 
3615 	kfree(oparams);
3616 out:
3617 	kfree(params);
3618 	return err;
3619 }
3620 
3621 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3622 				      struct snd_pcm_hw_params_old __user * _oparams)
3623 {
3624 	struct snd_pcm_hw_params *params;
3625 	struct snd_pcm_hw_params_old *oparams = NULL;
3626 	int err;
3627 
3628 	params = kmalloc(sizeof(*params), GFP_KERNEL);
3629 	if (!params)
3630 		return -ENOMEM;
3631 
3632 	oparams = memdup_user(_oparams, sizeof(*oparams));
3633 	if (IS_ERR(oparams)) {
3634 		err = PTR_ERR(oparams);
3635 		goto out;
3636 	}
3637 	snd_pcm_hw_convert_from_old_params(params, oparams);
3638 	err = snd_pcm_hw_params(substream, params);
3639 	snd_pcm_hw_convert_to_old_params(oparams, params);
3640 	if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3641 		if (!err)
3642 			err = -EFAULT;
3643 	}
3644 
3645 	kfree(oparams);
3646 out:
3647 	kfree(params);
3648 	return err;
3649 }
3650 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3651 
3652 #ifndef CONFIG_MMU
3653 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3654 					       unsigned long addr,
3655 					       unsigned long len,
3656 					       unsigned long pgoff,
3657 					       unsigned long flags)
3658 {
3659 	struct snd_pcm_file *pcm_file = file->private_data;
3660 	struct snd_pcm_substream *substream = pcm_file->substream;
3661 	struct snd_pcm_runtime *runtime = substream->runtime;
3662 	unsigned long offset = pgoff << PAGE_SHIFT;
3663 
3664 	switch (offset) {
3665 	case SNDRV_PCM_MMAP_OFFSET_STATUS:
3666 		return (unsigned long)runtime->status;
3667 	case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3668 		return (unsigned long)runtime->control;
3669 	default:
3670 		return (unsigned long)runtime->dma_area + offset;
3671 	}
3672 }
3673 #else
3674 # define snd_pcm_get_unmapped_area NULL
3675 #endif
3676 
3677 /*
3678  *  Register section
3679  */
3680 
3681 const struct file_operations snd_pcm_f_ops[2] = {
3682 	{
3683 		.owner =		THIS_MODULE,
3684 		.write =		snd_pcm_write,
3685 		.write_iter =		snd_pcm_writev,
3686 		.open =			snd_pcm_playback_open,
3687 		.release =		snd_pcm_release,
3688 		.llseek =		no_llseek,
3689 		.poll =			snd_pcm_playback_poll,
3690 		.unlocked_ioctl =	snd_pcm_playback_ioctl,
3691 		.compat_ioctl = 	snd_pcm_ioctl_compat,
3692 		.mmap =			snd_pcm_mmap,
3693 		.fasync =		snd_pcm_fasync,
3694 		.get_unmapped_area =	snd_pcm_get_unmapped_area,
3695 	},
3696 	{
3697 		.owner =		THIS_MODULE,
3698 		.read =			snd_pcm_read,
3699 		.read_iter =		snd_pcm_readv,
3700 		.open =			snd_pcm_capture_open,
3701 		.release =		snd_pcm_release,
3702 		.llseek =		no_llseek,
3703 		.poll =			snd_pcm_capture_poll,
3704 		.unlocked_ioctl =	snd_pcm_capture_ioctl,
3705 		.compat_ioctl = 	snd_pcm_ioctl_compat,
3706 		.mmap =			snd_pcm_mmap,
3707 		.fasync =		snd_pcm_fasync,
3708 		.get_unmapped_area =	snd_pcm_get_unmapped_area,
3709 	}
3710 };
3711