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