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