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