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