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