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