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