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