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