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