Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/drivers/aloop.c
29266 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
* Loopback soundcard
4
*
5
* Original code:
6
* Copyright (c) by Jaroslav Kysela <[email protected]>
7
*
8
* More accurate positioning and full-duplex support:
9
* Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
10
*
11
* Major (almost complete) rewrite:
12
* Copyright (c) by Takashi Iwai <[email protected]>
13
*
14
* A next major update in 2010 (separate timers for playback and capture):
15
* Copyright (c) Jaroslav Kysela <[email protected]>
16
*/
17
18
#include <linux/init.h>
19
#include <linux/jiffies.h>
20
#include <linux/slab.h>
21
#include <linux/string.h>
22
#include <linux/time.h>
23
#include <linux/wait.h>
24
#include <linux/module.h>
25
#include <linux/platform_device.h>
26
#include <sound/core.h>
27
#include <sound/control.h>
28
#include <sound/pcm.h>
29
#include <sound/pcm_params.h>
30
#include <sound/info.h>
31
#include <sound/initval.h>
32
#include <sound/timer.h>
33
34
MODULE_AUTHOR("Jaroslav Kysela <[email protected]>");
35
MODULE_DESCRIPTION("A loopback soundcard");
36
MODULE_LICENSE("GPL");
37
38
#define MAX_PCM_SUBSTREAMS 8
39
40
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
41
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
42
static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
43
static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
44
static int pcm_notify[SNDRV_CARDS];
45
static char *timer_source[SNDRV_CARDS];
46
47
module_param_array(index, int, NULL, 0444);
48
MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
49
module_param_array(id, charp, NULL, 0444);
50
MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
51
module_param_array(enable, bool, NULL, 0444);
52
MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
53
module_param_array(pcm_substreams, int, NULL, 0444);
54
MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
55
module_param_array(pcm_notify, int, NULL, 0444);
56
MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
57
module_param_array(timer_source, charp, NULL, 0444);
58
MODULE_PARM_DESC(timer_source, "Sound card name or number and device/subdevice number of timer to be used. Empty string for jiffies timer [default].");
59
60
#define NO_PITCH 100000
61
62
#define CABLE_VALID_PLAYBACK BIT(SNDRV_PCM_STREAM_PLAYBACK)
63
#define CABLE_VALID_CAPTURE BIT(SNDRV_PCM_STREAM_CAPTURE)
64
#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE)
65
66
struct loopback_cable;
67
struct loopback_pcm;
68
69
struct loopback_ops {
70
/* optional
71
* call in loopback->cable_lock
72
*/
73
int (*open)(struct loopback_pcm *dpcm);
74
/* required
75
* call in cable->lock
76
*/
77
int (*start)(struct loopback_pcm *dpcm);
78
/* required
79
* call in cable->lock
80
*/
81
int (*stop)(struct loopback_pcm *dpcm);
82
/* optional */
83
int (*stop_sync)(struct loopback_pcm *dpcm);
84
/* optional */
85
int (*close_substream)(struct loopback_pcm *dpcm);
86
/* optional
87
* call in loopback->cable_lock
88
*/
89
int (*close_cable)(struct loopback_pcm *dpcm);
90
/* optional
91
* call in cable->lock
92
*/
93
unsigned int (*pos_update)(struct loopback_cable *cable);
94
/* optional */
95
void (*dpcm_info)(struct loopback_pcm *dpcm,
96
struct snd_info_buffer *buffer);
97
};
98
99
struct loopback_cable {
100
spinlock_t lock;
101
struct loopback_pcm *streams[2];
102
struct snd_pcm_hardware hw;
103
/* flags */
104
unsigned int valid;
105
unsigned int running;
106
unsigned int pause;
107
/* timer specific */
108
const struct loopback_ops *ops;
109
/* If sound timer is used */
110
struct {
111
int stream;
112
struct snd_timer_id id;
113
struct work_struct event_work;
114
struct snd_timer_instance *instance;
115
} snd_timer;
116
};
117
118
struct loopback_setup {
119
unsigned int notify: 1;
120
unsigned int rate_shift;
121
snd_pcm_format_t format;
122
unsigned int rate;
123
snd_pcm_access_t access;
124
unsigned int channels;
125
struct snd_ctl_elem_id active_id;
126
struct snd_ctl_elem_id format_id;
127
struct snd_ctl_elem_id rate_id;
128
struct snd_ctl_elem_id channels_id;
129
struct snd_ctl_elem_id access_id;
130
};
131
132
struct loopback {
133
struct snd_card *card;
134
struct mutex cable_lock;
135
struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
136
struct snd_pcm *pcm[2];
137
struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
138
const char *timer_source;
139
};
140
141
struct loopback_pcm {
142
struct loopback *loopback;
143
struct snd_pcm_substream *substream;
144
struct loopback_cable *cable;
145
unsigned int pcm_buffer_size;
146
unsigned int buf_pos; /* position in buffer */
147
unsigned int silent_size;
148
/* PCM parameters */
149
unsigned int pcm_period_size;
150
unsigned int pcm_bps; /* bytes per second */
151
unsigned int pcm_salign; /* bytes per sample * channels */
152
unsigned int pcm_rate_shift; /* rate shift value */
153
/* flags */
154
unsigned int period_update_pending :1;
155
/* timer stuff */
156
unsigned int irq_pos; /* fractional IRQ position in jiffies
157
* ticks
158
*/
159
unsigned int period_size_frac; /* period size in jiffies ticks */
160
unsigned int last_drift;
161
unsigned long last_jiffies;
162
/* If jiffies timer is used */
163
struct timer_list timer;
164
165
/* size of per channel buffer in case of non-interleaved access */
166
unsigned int channel_buf_n;
167
};
168
169
static struct platform_device *devices[SNDRV_CARDS];
170
171
static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
172
{
173
if (dpcm->pcm_rate_shift == NO_PITCH) {
174
x /= HZ;
175
} else {
176
x = div_u64(NO_PITCH * (unsigned long long)x,
177
HZ * (unsigned long long)dpcm->pcm_rate_shift);
178
}
179
return x - (x % dpcm->pcm_salign);
180
}
181
182
static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
183
{
184
if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
185
return x * HZ;
186
} else {
187
x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
188
NO_PITCH);
189
}
190
return x;
191
}
192
193
static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
194
{
195
int device = dpcm->substream->pstr->pcm->device;
196
197
if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
198
device ^= 1;
199
return &dpcm->loopback->setup[dpcm->substream->number][device];
200
}
201
202
static inline unsigned int get_notify(struct loopback_pcm *dpcm)
203
{
204
return get_setup(dpcm)->notify;
205
}
206
207
static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
208
{
209
return get_setup(dpcm)->rate_shift;
210
}
211
212
/* call in cable->lock */
213
static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm)
214
{
215
unsigned long tick;
216
unsigned int rate_shift = get_rate_shift(dpcm);
217
218
if (rate_shift != dpcm->pcm_rate_shift) {
219
dpcm->pcm_rate_shift = rate_shift;
220
dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
221
}
222
if (dpcm->period_size_frac <= dpcm->irq_pos) {
223
dpcm->irq_pos %= dpcm->period_size_frac;
224
dpcm->period_update_pending = 1;
225
}
226
tick = dpcm->period_size_frac - dpcm->irq_pos;
227
tick = DIV_ROUND_UP(tick, dpcm->pcm_bps);
228
mod_timer(&dpcm->timer, jiffies + tick);
229
230
return 0;
231
}
232
233
/* call in cable->lock */
234
static int loopback_snd_timer_start(struct loopback_pcm *dpcm)
235
{
236
struct loopback_cable *cable = dpcm->cable;
237
int err;
238
239
/* Loopback device has to use same period as timer card. Therefore
240
* wake up for each snd_pcm_period_elapsed() call of timer card.
241
*/
242
err = snd_timer_start(cable->snd_timer.instance, 1);
243
if (err < 0) {
244
/* do not report error if trying to start but already
245
* running. For example called by opposite substream
246
* of the same cable
247
*/
248
if (err == -EBUSY)
249
return 0;
250
251
pcm_err(dpcm->substream->pcm,
252
"snd_timer_start(%d,%d,%d) failed with %d",
253
cable->snd_timer.id.card,
254
cable->snd_timer.id.device,
255
cable->snd_timer.id.subdevice,
256
err);
257
}
258
259
return err;
260
}
261
262
/* call in cable->lock */
263
static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm)
264
{
265
timer_delete(&dpcm->timer);
266
dpcm->timer.expires = 0;
267
268
return 0;
269
}
270
271
/* call in cable->lock */
272
static int loopback_snd_timer_stop(struct loopback_pcm *dpcm)
273
{
274
struct loopback_cable *cable = dpcm->cable;
275
int err;
276
277
/* only stop if both devices (playback and capture) are not running */
278
if (cable->running ^ cable->pause)
279
return 0;
280
281
err = snd_timer_stop(cable->snd_timer.instance);
282
if (err < 0) {
283
pcm_err(dpcm->substream->pcm,
284
"snd_timer_stop(%d,%d,%d) failed with %d",
285
cable->snd_timer.id.card,
286
cable->snd_timer.id.device,
287
cable->snd_timer.id.subdevice,
288
err);
289
}
290
291
return err;
292
}
293
294
static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm)
295
{
296
timer_delete_sync(&dpcm->timer);
297
298
return 0;
299
}
300
301
/* call in loopback->cable_lock */
302
static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm)
303
{
304
struct loopback_cable *cable = dpcm->cable;
305
306
/* snd_timer was not opened */
307
if (!cable->snd_timer.instance)
308
return 0;
309
310
/* will only be called from free_cable() when other stream was
311
* already closed. Other stream cannot be reopened as long as
312
* loopback->cable_lock is locked. Therefore no need to lock
313
* cable->lock;
314
*/
315
snd_timer_close(cable->snd_timer.instance);
316
317
/* wait till drain work has finished if requested */
318
cancel_work_sync(&cable->snd_timer.event_work);
319
320
snd_timer_instance_free(cable->snd_timer.instance);
321
memset(&cable->snd_timer, 0, sizeof(cable->snd_timer));
322
323
return 0;
324
}
325
326
static bool is_access_interleaved(snd_pcm_access_t access)
327
{
328
switch (access) {
329
case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
330
case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
331
return true;
332
default:
333
return false;
334
}
335
};
336
337
static int loopback_check_format(struct loopback_cable *cable, int stream)
338
{
339
struct snd_pcm_runtime *runtime, *cruntime;
340
struct loopback_setup *setup;
341
struct snd_card *card;
342
int check;
343
344
if (cable->valid != CABLE_VALID_BOTH) {
345
if (stream == SNDRV_PCM_STREAM_PLAYBACK)
346
goto __notify;
347
return 0;
348
}
349
runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
350
substream->runtime;
351
cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
352
substream->runtime;
353
check = runtime->format != cruntime->format ||
354
runtime->rate != cruntime->rate ||
355
runtime->channels != cruntime->channels ||
356
is_access_interleaved(runtime->access) !=
357
is_access_interleaved(cruntime->access);
358
if (!check)
359
return 0;
360
if (stream == SNDRV_PCM_STREAM_CAPTURE) {
361
return -EIO;
362
} else {
363
snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
364
substream, SNDRV_PCM_STATE_DRAINING);
365
__notify:
366
runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
367
substream->runtime;
368
setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
369
card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
370
if (setup->format != runtime->format) {
371
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
372
&setup->format_id);
373
setup->format = runtime->format;
374
}
375
if (setup->rate != runtime->rate) {
376
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
377
&setup->rate_id);
378
setup->rate = runtime->rate;
379
}
380
if (setup->channels != runtime->channels) {
381
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
382
&setup->channels_id);
383
setup->channels = runtime->channels;
384
}
385
if (is_access_interleaved(setup->access) !=
386
is_access_interleaved(runtime->access)) {
387
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
388
&setup->access_id);
389
setup->access = runtime->access;
390
}
391
}
392
return 0;
393
}
394
395
static void loopback_active_notify(struct loopback_pcm *dpcm)
396
{
397
snd_ctl_notify(dpcm->loopback->card,
398
SNDRV_CTL_EVENT_MASK_VALUE,
399
&get_setup(dpcm)->active_id);
400
}
401
402
static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
403
{
404
struct snd_pcm_runtime *runtime = substream->runtime;
405
struct loopback_pcm *dpcm = runtime->private_data;
406
struct loopback_cable *cable = dpcm->cable;
407
int err = 0, stream = 1 << substream->stream;
408
409
switch (cmd) {
410
case SNDRV_PCM_TRIGGER_START:
411
err = loopback_check_format(cable, substream->stream);
412
if (err < 0)
413
return err;
414
dpcm->last_jiffies = jiffies;
415
dpcm->pcm_rate_shift = 0;
416
dpcm->last_drift = 0;
417
scoped_guard(spinlock, &cable->lock) {
418
cable->running |= stream;
419
cable->pause &= ~stream;
420
err = cable->ops->start(dpcm);
421
}
422
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
423
loopback_active_notify(dpcm);
424
break;
425
case SNDRV_PCM_TRIGGER_STOP:
426
scoped_guard(spinlock, &cable->lock) {
427
cable->running &= ~stream;
428
cable->pause &= ~stream;
429
err = cable->ops->stop(dpcm);
430
}
431
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
432
loopback_active_notify(dpcm);
433
break;
434
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
435
case SNDRV_PCM_TRIGGER_SUSPEND:
436
scoped_guard(spinlock, &cable->lock) {
437
cable->pause |= stream;
438
err = cable->ops->stop(dpcm);
439
}
440
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
441
loopback_active_notify(dpcm);
442
break;
443
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
444
case SNDRV_PCM_TRIGGER_RESUME:
445
scoped_guard(spinlock, &cable->lock) {
446
dpcm->last_jiffies = jiffies;
447
cable->pause &= ~stream;
448
err = cable->ops->start(dpcm);
449
}
450
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
451
loopback_active_notify(dpcm);
452
break;
453
default:
454
return -EINVAL;
455
}
456
return err;
457
}
458
459
static void params_change(struct snd_pcm_substream *substream)
460
{
461
struct snd_pcm_runtime *runtime = substream->runtime;
462
struct loopback_pcm *dpcm = runtime->private_data;
463
struct loopback_cable *cable = dpcm->cable;
464
465
cable->hw.formats = pcm_format_to_bits(runtime->format);
466
cable->hw.rate_min = runtime->rate;
467
cable->hw.rate_max = runtime->rate;
468
cable->hw.channels_min = runtime->channels;
469
cable->hw.channels_max = runtime->channels;
470
471
if (cable->snd_timer.instance) {
472
cable->hw.period_bytes_min =
473
frames_to_bytes(runtime, runtime->period_size);
474
cable->hw.period_bytes_max = cable->hw.period_bytes_min;
475
}
476
477
}
478
479
static int loopback_prepare(struct snd_pcm_substream *substream)
480
{
481
struct snd_pcm_runtime *runtime = substream->runtime;
482
struct loopback_pcm *dpcm = runtime->private_data;
483
struct loopback_cable *cable = dpcm->cable;
484
int err, bps, salign;
485
486
if (cable->ops->stop_sync) {
487
err = cable->ops->stop_sync(dpcm);
488
if (err < 0)
489
return err;
490
}
491
492
salign = (snd_pcm_format_physical_width(runtime->format) *
493
runtime->channels) / 8;
494
bps = salign * runtime->rate;
495
if (bps <= 0 || salign <= 0)
496
return -EINVAL;
497
498
dpcm->buf_pos = 0;
499
dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
500
dpcm->channel_buf_n = dpcm->pcm_buffer_size / runtime->channels;
501
if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
502
/* clear capture buffer */
503
dpcm->silent_size = dpcm->pcm_buffer_size;
504
snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
505
runtime->buffer_size * runtime->channels);
506
}
507
508
dpcm->irq_pos = 0;
509
dpcm->period_update_pending = 0;
510
dpcm->pcm_bps = bps;
511
dpcm->pcm_salign = salign;
512
dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
513
514
guard(mutex)(&dpcm->loopback->cable_lock);
515
if (!(cable->valid & ~(1 << substream->stream)) ||
516
(get_setup(dpcm)->notify &&
517
substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
518
params_change(substream);
519
cable->valid |= 1 << substream->stream;
520
521
return 0;
522
}
523
524
static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
525
{
526
struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
527
char *dst = runtime->dma_area;
528
unsigned int dst_off = dpcm->buf_pos;
529
530
if (dpcm->silent_size >= dpcm->pcm_buffer_size)
531
return;
532
if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
533
bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
534
535
for (;;) {
536
unsigned int size = bytes;
537
if (dst_off + size > dpcm->pcm_buffer_size)
538
size = dpcm->pcm_buffer_size - dst_off;
539
snd_pcm_format_set_silence(runtime->format, dst + dst_off,
540
bytes_to_frames(runtime, size) *
541
runtime->channels);
542
dpcm->silent_size += size;
543
bytes -= size;
544
if (!bytes)
545
break;
546
dst_off = 0;
547
}
548
}
549
550
static void copy_play_buf_part_n(struct loopback_pcm *play, struct loopback_pcm *capt,
551
unsigned int size, unsigned int src_off, unsigned int dst_off)
552
{
553
unsigned int channels = capt->substream->runtime->channels;
554
unsigned int size_p_ch = size / channels;
555
unsigned int src_off_ch = src_off / channels;
556
unsigned int dst_off_ch = dst_off / channels;
557
int i;
558
559
for (i = 0; i < channels; i++) {
560
memcpy(capt->substream->runtime->dma_area + capt->channel_buf_n * i + dst_off_ch,
561
play->substream->runtime->dma_area + play->channel_buf_n * i + src_off_ch,
562
size_p_ch);
563
}
564
}
565
566
static void copy_play_buf(struct loopback_pcm *play,
567
struct loopback_pcm *capt,
568
unsigned int bytes)
569
{
570
struct snd_pcm_runtime *runtime = play->substream->runtime;
571
char *src = runtime->dma_area;
572
char *dst = capt->substream->runtime->dma_area;
573
unsigned int src_off = play->buf_pos;
574
unsigned int dst_off = capt->buf_pos;
575
unsigned int clear_bytes = 0;
576
577
/* check if playback is draining, trim the capture copy size
578
* when our pointer is at the end of playback ring buffer */
579
if (runtime->state == SNDRV_PCM_STATE_DRAINING &&
580
snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
581
snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
582
appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
583
appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
584
appl_ptr1 += play->buf_pos / play->pcm_salign;
585
if (appl_ptr < appl_ptr1)
586
appl_ptr1 -= runtime->buffer_size;
587
diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
588
if (diff < bytes) {
589
clear_bytes = bytes - diff;
590
bytes = diff;
591
}
592
}
593
594
for (;;) {
595
unsigned int size = bytes;
596
if (src_off + size > play->pcm_buffer_size)
597
size = play->pcm_buffer_size - src_off;
598
if (dst_off + size > capt->pcm_buffer_size)
599
size = capt->pcm_buffer_size - dst_off;
600
if (!is_access_interleaved(runtime->access))
601
copy_play_buf_part_n(play, capt, size, src_off, dst_off);
602
else
603
memcpy(dst + dst_off, src + src_off, size);
604
capt->silent_size = 0;
605
bytes -= size;
606
if (!bytes)
607
break;
608
src_off = (src_off + size) % play->pcm_buffer_size;
609
dst_off = (dst_off + size) % capt->pcm_buffer_size;
610
}
611
612
if (clear_bytes > 0) {
613
clear_capture_buf(capt, clear_bytes);
614
capt->silent_size = 0;
615
}
616
}
617
618
static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
619
unsigned int jiffies_delta)
620
{
621
unsigned long last_pos;
622
unsigned int delta;
623
624
last_pos = byte_pos(dpcm, dpcm->irq_pos);
625
dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
626
delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
627
if (delta >= dpcm->last_drift)
628
delta -= dpcm->last_drift;
629
dpcm->last_drift = 0;
630
if (dpcm->irq_pos >= dpcm->period_size_frac) {
631
dpcm->irq_pos %= dpcm->period_size_frac;
632
dpcm->period_update_pending = 1;
633
}
634
return delta;
635
}
636
637
static inline void bytepos_finish(struct loopback_pcm *dpcm,
638
unsigned int delta)
639
{
640
dpcm->buf_pos += delta;
641
dpcm->buf_pos %= dpcm->pcm_buffer_size;
642
}
643
644
/* call in cable->lock */
645
static unsigned int loopback_jiffies_timer_pos_update
646
(struct loopback_cable *cable)
647
{
648
struct loopback_pcm *dpcm_play =
649
cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
650
struct loopback_pcm *dpcm_capt =
651
cable->streams[SNDRV_PCM_STREAM_CAPTURE];
652
unsigned long delta_play = 0, delta_capt = 0, cur_jiffies;
653
unsigned int running, count1, count2;
654
655
cur_jiffies = jiffies;
656
running = cable->running ^ cable->pause;
657
if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
658
delta_play = cur_jiffies - dpcm_play->last_jiffies;
659
dpcm_play->last_jiffies += delta_play;
660
}
661
662
if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
663
delta_capt = cur_jiffies - dpcm_capt->last_jiffies;
664
dpcm_capt->last_jiffies += delta_capt;
665
}
666
667
if (delta_play == 0 && delta_capt == 0)
668
goto unlock;
669
670
if (delta_play > delta_capt) {
671
count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
672
bytepos_finish(dpcm_play, count1);
673
delta_play = delta_capt;
674
} else if (delta_play < delta_capt) {
675
count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
676
clear_capture_buf(dpcm_capt, count1);
677
bytepos_finish(dpcm_capt, count1);
678
delta_capt = delta_play;
679
}
680
681
if (delta_play == 0 && delta_capt == 0)
682
goto unlock;
683
684
/* note delta_capt == delta_play at this moment */
685
count1 = bytepos_delta(dpcm_play, delta_play);
686
count2 = bytepos_delta(dpcm_capt, delta_capt);
687
if (count1 < count2) {
688
dpcm_capt->last_drift = count2 - count1;
689
count1 = count2;
690
} else if (count1 > count2) {
691
dpcm_play->last_drift = count1 - count2;
692
}
693
copy_play_buf(dpcm_play, dpcm_capt, count1);
694
bytepos_finish(dpcm_play, count1);
695
bytepos_finish(dpcm_capt, count1);
696
unlock:
697
return running;
698
}
699
700
static void loopback_jiffies_timer_function(struct timer_list *t)
701
{
702
struct loopback_pcm *dpcm = timer_container_of(dpcm, t, timer);
703
bool period_elapsed = false;
704
705
scoped_guard(spinlock_irqsave, &dpcm->cable->lock) {
706
if (loopback_jiffies_timer_pos_update(dpcm->cable) &
707
(1 << dpcm->substream->stream)) {
708
loopback_jiffies_timer_start(dpcm);
709
if (dpcm->period_update_pending) {
710
dpcm->period_update_pending = 0;
711
period_elapsed = true;
712
break;
713
}
714
}
715
}
716
717
if (period_elapsed)
718
snd_pcm_period_elapsed(dpcm->substream);
719
}
720
721
/* call in cable->lock */
722
static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime,
723
unsigned long resolution)
724
{
725
if (resolution != runtime->timer_resolution) {
726
struct loopback_pcm *dpcm = runtime->private_data;
727
struct loopback_cable *cable = dpcm->cable;
728
/* Worst case estimation of possible values for resolution
729
* resolution <= (512 * 1024) frames / 8kHz in nsec
730
* resolution <= 65.536.000.000 nsec
731
*
732
* period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz +
733
* 500.000
734
* period_size <= 12.582.912.000.000 <64bit
735
* / 1.000.000 usec/sec
736
*/
737
snd_pcm_uframes_t period_size_usec =
738
resolution / 1000 * runtime->rate;
739
/* round to nearest sample rate */
740
snd_pcm_uframes_t period_size =
741
(period_size_usec + 500 * 1000) / (1000 * 1000);
742
743
pcm_err(dpcm->substream->pcm,
744
"Period size (%lu frames) of loopback device is not corresponding to timer resolution (%lu nsec = %lu frames) of card timer %d,%d,%d. Use period size of %lu frames for loopback device.",
745
runtime->period_size, resolution, period_size,
746
cable->snd_timer.id.card,
747
cable->snd_timer.id.device,
748
cable->snd_timer.id.subdevice,
749
period_size);
750
return -EINVAL;
751
}
752
return 0;
753
}
754
755
static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,
756
int event,
757
unsigned long resolution)
758
{
759
struct loopback_pcm *dpcm_play, *dpcm_capt;
760
struct snd_pcm_substream *substream_play, *substream_capt;
761
struct snd_pcm_runtime *valid_runtime;
762
unsigned int running, elapsed_bytes;
763
bool xrun = false;
764
765
scoped_guard(spinlock_irqsave, &cable->lock) {
766
running = cable->running ^ cable->pause;
767
/* no need to do anything if no stream is running */
768
if (!running)
769
return;
770
771
dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
772
dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
773
774
if (event == SNDRV_TIMER_EVENT_MSTOP) {
775
if (!dpcm_play ||
776
dpcm_play->substream->runtime->state !=
777
SNDRV_PCM_STATE_DRAINING)
778
return;
779
}
780
781
substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
782
dpcm_play->substream : NULL;
783
substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
784
dpcm_capt->substream : NULL;
785
valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
786
dpcm_play->substream->runtime :
787
dpcm_capt->substream->runtime;
788
789
/* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
790
if (event == SNDRV_TIMER_EVENT_TICK) {
791
/* The hardware rules guarantee that playback and capture period
792
* are the same. Therefore only one device has to be checked
793
* here.
794
*/
795
if (loopback_snd_timer_check_resolution(valid_runtime,
796
resolution) < 0) {
797
xrun = true;
798
break;
799
}
800
}
801
802
elapsed_bytes = frames_to_bytes(valid_runtime,
803
valid_runtime->period_size);
804
/* The same timer interrupt is used for playback and capture device */
805
if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
806
(running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
807
copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
808
bytepos_finish(dpcm_play, elapsed_bytes);
809
bytepos_finish(dpcm_capt, elapsed_bytes);
810
} else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
811
bytepos_finish(dpcm_play, elapsed_bytes);
812
} else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
813
clear_capture_buf(dpcm_capt, elapsed_bytes);
814
bytepos_finish(dpcm_capt, elapsed_bytes);
815
}
816
}
817
818
if (xrun) {
819
if (substream_play)
820
snd_pcm_stop_xrun(substream_play);
821
if (substream_capt)
822
snd_pcm_stop_xrun(substream_capt);
823
return;
824
}
825
826
if (substream_play)
827
snd_pcm_period_elapsed(substream_play);
828
if (substream_capt)
829
snd_pcm_period_elapsed(substream_capt);
830
}
831
832
static void loopback_snd_timer_function(struct snd_timer_instance *timeri,
833
unsigned long resolution,
834
unsigned long ticks)
835
{
836
struct loopback_cable *cable = timeri->callback_data;
837
838
loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_TICK,
839
resolution);
840
}
841
842
static void loopback_snd_timer_work(struct work_struct *work)
843
{
844
struct loopback_cable *cable;
845
846
cable = container_of(work, struct loopback_cable, snd_timer.event_work);
847
loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_MSTOP, 0);
848
}
849
850
static void loopback_snd_timer_event(struct snd_timer_instance *timeri,
851
int event,
852
struct timespec64 *tstamp,
853
unsigned long resolution)
854
{
855
/* Do not lock cable->lock here because timer->lock is already hold.
856
* There are other functions which first lock cable->lock and than
857
* timer->lock e.g.
858
* loopback_trigger()
859
* spin_lock(&cable->lock)
860
* loopback_snd_timer_start()
861
* snd_timer_start()
862
* spin_lock(&timer->lock)
863
* Therefore when using the oposit order of locks here it could result
864
* in a deadlock.
865
*/
866
867
if (event == SNDRV_TIMER_EVENT_MSTOP) {
868
struct loopback_cable *cable = timeri->callback_data;
869
870
/* sound card of the timer was stopped. Therefore there will not
871
* be any further timer callbacks. Due to this forward audio
872
* data from here if in draining state. When still in running
873
* state the streaming will be aborted by the usual timeout. It
874
* should not be aborted here because may be the timer sound
875
* card does only a recovery and the timer is back soon.
876
* This work triggers loopback_snd_timer_work()
877
*/
878
schedule_work(&cable->snd_timer.event_work);
879
}
880
}
881
882
static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm,
883
struct snd_info_buffer *buffer)
884
{
885
snd_iprintf(buffer, " update_pending:\t%u\n",
886
dpcm->period_update_pending);
887
snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
888
snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
889
snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
890
dpcm->last_jiffies, jiffies);
891
snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
892
}
893
894
static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm,
895
struct snd_info_buffer *buffer)
896
{
897
struct loopback_cable *cable = dpcm->cable;
898
899
snd_iprintf(buffer, " sound timer:\thw:%d,%d,%d\n",
900
cable->snd_timer.id.card,
901
cable->snd_timer.id.device,
902
cable->snd_timer.id.subdevice);
903
snd_iprintf(buffer, " timer open:\t\t%s\n",
904
snd_pcm_direction_name(cable->snd_timer.stream));
905
}
906
907
static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
908
{
909
struct snd_pcm_runtime *runtime = substream->runtime;
910
struct loopback_pcm *dpcm = runtime->private_data;
911
snd_pcm_uframes_t pos;
912
913
guard(spinlock)(&dpcm->cable->lock);
914
if (dpcm->cable->ops->pos_update)
915
dpcm->cable->ops->pos_update(dpcm->cable);
916
pos = dpcm->buf_pos;
917
return bytes_to_frames(runtime, pos);
918
}
919
920
static const struct snd_pcm_hardware loopback_pcm_hardware =
921
{
922
.info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
923
SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
924
SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_NONINTERLEAVED),
925
.formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
926
SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
927
SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
928
SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
929
SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE |
930
SNDRV_PCM_FMTBIT_DSD_U8 |
931
SNDRV_PCM_FMTBIT_DSD_U16_LE | SNDRV_PCM_FMTBIT_DSD_U16_BE |
932
SNDRV_PCM_FMTBIT_DSD_U32_LE | SNDRV_PCM_FMTBIT_DSD_U32_BE),
933
.rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_768000,
934
.rate_min = 8000,
935
.rate_max = 768000,
936
.channels_min = 1,
937
.channels_max = 32,
938
.buffer_bytes_max = 2 * 1024 * 1024,
939
.period_bytes_min = 64,
940
/* note check overflow in frac_pos() using pcm_rate_shift before
941
changing period_bytes_max value */
942
.period_bytes_max = 1024 * 1024,
943
.periods_min = 1,
944
.periods_max = 1024,
945
.fifo_size = 0,
946
};
947
948
static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
949
{
950
struct loopback_pcm *dpcm = runtime->private_data;
951
kfree(dpcm);
952
}
953
954
static int loopback_hw_free(struct snd_pcm_substream *substream)
955
{
956
struct snd_pcm_runtime *runtime = substream->runtime;
957
struct loopback_pcm *dpcm = runtime->private_data;
958
struct loopback_cable *cable = dpcm->cable;
959
960
guard(mutex)(&dpcm->loopback->cable_lock);
961
cable->valid &= ~(1 << substream->stream);
962
return 0;
963
}
964
965
static unsigned int get_cable_index(struct snd_pcm_substream *substream)
966
{
967
if (!substream->pcm->device)
968
return substream->stream;
969
else
970
return !substream->stream;
971
}
972
973
static int rule_format(struct snd_pcm_hw_params *params,
974
struct snd_pcm_hw_rule *rule)
975
{
976
struct loopback_pcm *dpcm = rule->private;
977
struct loopback_cable *cable = dpcm->cable;
978
struct snd_mask m;
979
980
snd_mask_none(&m);
981
scoped_guard(mutex, &dpcm->loopback->cable_lock) {
982
m.bits[0] = (u_int32_t)cable->hw.formats;
983
m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
984
}
985
return snd_mask_refine(hw_param_mask(params, rule->var), &m);
986
}
987
988
static int rule_rate(struct snd_pcm_hw_params *params,
989
struct snd_pcm_hw_rule *rule)
990
{
991
struct loopback_pcm *dpcm = rule->private;
992
struct loopback_cable *cable = dpcm->cable;
993
struct snd_interval t;
994
995
scoped_guard(mutex, &dpcm->loopback->cable_lock) {
996
t.min = cable->hw.rate_min;
997
t.max = cable->hw.rate_max;
998
}
999
t.openmin = t.openmax = 0;
1000
t.integer = 0;
1001
return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1002
}
1003
1004
static int rule_channels(struct snd_pcm_hw_params *params,
1005
struct snd_pcm_hw_rule *rule)
1006
{
1007
struct loopback_pcm *dpcm = rule->private;
1008
struct loopback_cable *cable = dpcm->cable;
1009
struct snd_interval t;
1010
1011
scoped_guard(mutex, &dpcm->loopback->cable_lock) {
1012
t.min = cable->hw.channels_min;
1013
t.max = cable->hw.channels_max;
1014
}
1015
t.openmin = t.openmax = 0;
1016
t.integer = 0;
1017
return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1018
}
1019
1020
static int rule_period_bytes(struct snd_pcm_hw_params *params,
1021
struct snd_pcm_hw_rule *rule)
1022
{
1023
struct loopback_pcm *dpcm = rule->private;
1024
struct loopback_cable *cable = dpcm->cable;
1025
struct snd_interval t;
1026
1027
scoped_guard(mutex, &dpcm->loopback->cable_lock) {
1028
t.min = cable->hw.period_bytes_min;
1029
t.max = cable->hw.period_bytes_max;
1030
}
1031
t.openmin = 0;
1032
t.openmax = 0;
1033
t.integer = 0;
1034
return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1035
}
1036
1037
static void free_cable(struct snd_pcm_substream *substream)
1038
{
1039
struct loopback *loopback = substream->private_data;
1040
int dev = get_cable_index(substream);
1041
struct loopback_cable *cable;
1042
1043
cable = loopback->cables[substream->number][dev];
1044
if (!cable)
1045
return;
1046
if (cable->streams[!substream->stream]) {
1047
/* other stream is still alive */
1048
guard(spinlock_irq)(&cable->lock);
1049
cable->streams[substream->stream] = NULL;
1050
} else {
1051
struct loopback_pcm *dpcm = substream->runtime->private_data;
1052
1053
if (cable->ops && cable->ops->close_cable && dpcm)
1054
cable->ops->close_cable(dpcm);
1055
/* free the cable */
1056
loopback->cables[substream->number][dev] = NULL;
1057
kfree(cable);
1058
}
1059
}
1060
1061
static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm)
1062
{
1063
timer_setup(&dpcm->timer, loopback_jiffies_timer_function, 0);
1064
1065
return 0;
1066
}
1067
1068
static const struct loopback_ops loopback_jiffies_timer_ops = {
1069
.open = loopback_jiffies_timer_open,
1070
.start = loopback_jiffies_timer_start,
1071
.stop = loopback_jiffies_timer_stop,
1072
.stop_sync = loopback_jiffies_timer_stop_sync,
1073
.close_substream = loopback_jiffies_timer_stop_sync,
1074
.pos_update = loopback_jiffies_timer_pos_update,
1075
.dpcm_info = loopback_jiffies_timer_dpcm_info,
1076
};
1077
1078
static int loopback_parse_timer_id(const char *str,
1079
struct snd_timer_id *tid)
1080
{
1081
/* [<pref>:](<card name>|<card idx>)[{.,}<dev idx>[{.,}<subdev idx>]] */
1082
const char * const sep_dev = ".,";
1083
const char * const sep_pref = ":";
1084
const char *name = str;
1085
char *sep, save = '\0';
1086
int card_idx = 0, dev = 0, subdev = 0;
1087
int err;
1088
1089
sep = strpbrk(str, sep_pref);
1090
if (sep)
1091
name = sep + 1;
1092
sep = strpbrk(name, sep_dev);
1093
if (sep) {
1094
save = *sep;
1095
*sep = '\0';
1096
}
1097
err = kstrtoint(name, 0, &card_idx);
1098
if (err == -EINVAL) {
1099
/* Must be the name, not number */
1100
for (card_idx = 0; card_idx < snd_ecards_limit; card_idx++) {
1101
struct snd_card *card = snd_card_ref(card_idx);
1102
1103
if (card) {
1104
if (!strcmp(card->id, name))
1105
err = 0;
1106
snd_card_unref(card);
1107
}
1108
if (!err)
1109
break;
1110
}
1111
}
1112
if (sep) {
1113
*sep = save;
1114
if (!err) {
1115
char *sep2, save2 = '\0';
1116
1117
sep2 = strpbrk(sep + 1, sep_dev);
1118
if (sep2) {
1119
save2 = *sep2;
1120
*sep2 = '\0';
1121
}
1122
err = kstrtoint(sep + 1, 0, &dev);
1123
if (sep2) {
1124
*sep2 = save2;
1125
if (!err)
1126
err = kstrtoint(sep2 + 1, 0, &subdev);
1127
}
1128
}
1129
}
1130
if (card_idx == -1)
1131
tid->dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1132
if (!err && tid) {
1133
tid->card = card_idx;
1134
tid->device = dev;
1135
tid->subdevice = subdev;
1136
}
1137
return err;
1138
}
1139
1140
/* call in loopback->cable_lock */
1141
static int loopback_snd_timer_open(struct loopback_pcm *dpcm)
1142
{
1143
int err = 0;
1144
struct snd_timer_id tid = {
1145
.dev_class = SNDRV_TIMER_CLASS_PCM,
1146
.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION,
1147
};
1148
struct snd_timer_instance *timeri;
1149
struct loopback_cable *cable = dpcm->cable;
1150
1151
/* check if timer was already opened. It is only opened once
1152
* per playback and capture subdevice (aka cable).
1153
*/
1154
if (cable->snd_timer.instance)
1155
goto exit;
1156
1157
err = loopback_parse_timer_id(dpcm->loopback->timer_source, &tid);
1158
if (err < 0) {
1159
pcm_err(dpcm->substream->pcm,
1160
"Parsing timer source \'%s\' failed with %d",
1161
dpcm->loopback->timer_source, err);
1162
goto exit;
1163
}
1164
1165
cable->snd_timer.stream = dpcm->substream->stream;
1166
cable->snd_timer.id = tid;
1167
1168
timeri = snd_timer_instance_new(dpcm->loopback->card->id);
1169
if (!timeri) {
1170
err = -ENOMEM;
1171
goto exit;
1172
}
1173
/* The callback has to be called from another work. If
1174
* SNDRV_TIMER_IFLG_FAST is specified it will be called from the
1175
* snd_pcm_period_elapsed() call of the selected sound card.
1176
* snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave().
1177
* Due to our callback loopback_snd_timer_function() also calls
1178
* snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave().
1179
* This would end up in a dead lock.
1180
*/
1181
timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1182
timeri->callback = loopback_snd_timer_function;
1183
timeri->callback_data = (void *)cable;
1184
timeri->ccallback = loopback_snd_timer_event;
1185
1186
/* initialise a work used for draining */
1187
INIT_WORK(&cable->snd_timer.event_work, loopback_snd_timer_work);
1188
1189
/* The mutex loopback->cable_lock is kept locked.
1190
* Therefore snd_timer_open() cannot be called a second time
1191
* by the other device of the same cable.
1192
* Therefore the following issue cannot happen:
1193
* [proc1] Call loopback_timer_open() ->
1194
* Unlock cable->lock for snd_timer_close/open() call
1195
* [proc2] Call loopback_timer_open() -> snd_timer_open(),
1196
* snd_timer_start()
1197
* [proc1] Call snd_timer_open() and overwrite running timer
1198
* instance
1199
*/
1200
err = snd_timer_open(timeri, &cable->snd_timer.id, current->pid);
1201
if (err < 0) {
1202
pcm_err(dpcm->substream->pcm,
1203
"snd_timer_open (%d,%d,%d) failed with %d",
1204
cable->snd_timer.id.card,
1205
cable->snd_timer.id.device,
1206
cable->snd_timer.id.subdevice,
1207
err);
1208
snd_timer_instance_free(timeri);
1209
goto exit;
1210
}
1211
1212
cable->snd_timer.instance = timeri;
1213
1214
exit:
1215
return err;
1216
}
1217
1218
/* stop_sync() is not required for sound timer because it does not need to be
1219
* restarted in loopback_prepare() on Xrun recovery
1220
*/
1221
static const struct loopback_ops loopback_snd_timer_ops = {
1222
.open = loopback_snd_timer_open,
1223
.start = loopback_snd_timer_start,
1224
.stop = loopback_snd_timer_stop,
1225
.close_cable = loopback_snd_timer_close_cable,
1226
.dpcm_info = loopback_snd_timer_dpcm_info,
1227
};
1228
1229
static int loopback_open(struct snd_pcm_substream *substream)
1230
{
1231
struct snd_pcm_runtime *runtime = substream->runtime;
1232
struct loopback *loopback = substream->private_data;
1233
struct loopback_pcm *dpcm;
1234
struct loopback_cable *cable = NULL;
1235
int err = 0;
1236
int dev = get_cable_index(substream);
1237
1238
guard(mutex)(&loopback->cable_lock);
1239
dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1240
if (!dpcm)
1241
return -ENOMEM;
1242
dpcm->loopback = loopback;
1243
dpcm->substream = substream;
1244
1245
cable = loopback->cables[substream->number][dev];
1246
if (!cable) {
1247
cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1248
if (!cable) {
1249
err = -ENOMEM;
1250
goto unlock;
1251
}
1252
spin_lock_init(&cable->lock);
1253
cable->hw = loopback_pcm_hardware;
1254
if (loopback->timer_source)
1255
cable->ops = &loopback_snd_timer_ops;
1256
else
1257
cable->ops = &loopback_jiffies_timer_ops;
1258
loopback->cables[substream->number][dev] = cable;
1259
}
1260
dpcm->cable = cable;
1261
runtime->private_data = dpcm;
1262
1263
if (cable->ops->open) {
1264
err = cable->ops->open(dpcm);
1265
if (err < 0)
1266
goto unlock;
1267
}
1268
1269
snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1270
1271
/* use dynamic rules based on actual runtime->hw values */
1272
/* note that the default rules created in the PCM midlevel code */
1273
/* are cached -> they do not reflect the actual state */
1274
err = snd_pcm_hw_rule_add(runtime, 0,
1275
SNDRV_PCM_HW_PARAM_FORMAT,
1276
rule_format, dpcm,
1277
SNDRV_PCM_HW_PARAM_FORMAT, -1);
1278
if (err < 0)
1279
goto unlock;
1280
err = snd_pcm_hw_rule_add(runtime, 0,
1281
SNDRV_PCM_HW_PARAM_RATE,
1282
rule_rate, dpcm,
1283
SNDRV_PCM_HW_PARAM_RATE, -1);
1284
if (err < 0)
1285
goto unlock;
1286
err = snd_pcm_hw_rule_add(runtime, 0,
1287
SNDRV_PCM_HW_PARAM_CHANNELS,
1288
rule_channels, dpcm,
1289
SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1290
if (err < 0)
1291
goto unlock;
1292
1293
/* In case of sound timer the period time of both devices of the same
1294
* loop has to be the same.
1295
* This rule only takes effect if a sound timer was chosen
1296
*/
1297
if (cable->snd_timer.instance) {
1298
err = snd_pcm_hw_rule_add(runtime, 0,
1299
SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1300
rule_period_bytes, dpcm,
1301
SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1);
1302
if (err < 0)
1303
goto unlock;
1304
}
1305
1306
/* loopback_runtime_free() has not to be called if kfree(dpcm) was
1307
* already called here. Otherwise it will end up with a double free.
1308
*/
1309
runtime->private_free = loopback_runtime_free;
1310
if (get_notify(dpcm))
1311
runtime->hw = loopback_pcm_hardware;
1312
else
1313
runtime->hw = cable->hw;
1314
1315
scoped_guard(spinlock_irq, &cable->lock) {
1316
cable->streams[substream->stream] = dpcm;
1317
}
1318
1319
unlock:
1320
if (err < 0) {
1321
free_cable(substream);
1322
kfree(dpcm);
1323
}
1324
return err;
1325
}
1326
1327
static int loopback_close(struct snd_pcm_substream *substream)
1328
{
1329
struct loopback *loopback = substream->private_data;
1330
struct loopback_pcm *dpcm = substream->runtime->private_data;
1331
int err = 0;
1332
1333
if (dpcm->cable->ops->close_substream)
1334
err = dpcm->cable->ops->close_substream(dpcm);
1335
guard(mutex)(&loopback->cable_lock);
1336
free_cable(substream);
1337
return err;
1338
}
1339
1340
static const struct snd_pcm_ops loopback_pcm_ops = {
1341
.open = loopback_open,
1342
.close = loopback_close,
1343
.hw_free = loopback_hw_free,
1344
.prepare = loopback_prepare,
1345
.trigger = loopback_trigger,
1346
.pointer = loopback_pointer,
1347
};
1348
1349
static int loopback_pcm_new(struct loopback *loopback,
1350
int device, int substreams)
1351
{
1352
struct snd_pcm *pcm;
1353
int err;
1354
1355
err = snd_pcm_new(loopback->card, "Loopback PCM", device,
1356
substreams, substreams, &pcm);
1357
if (err < 0)
1358
return err;
1359
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops);
1360
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops);
1361
snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
1362
1363
pcm->private_data = loopback;
1364
pcm->info_flags = 0;
1365
strscpy(pcm->name, "Loopback PCM");
1366
1367
loopback->pcm[device] = pcm;
1368
return 0;
1369
}
1370
1371
static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
1372
struct snd_ctl_elem_info *uinfo)
1373
{
1374
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1375
uinfo->count = 1;
1376
uinfo->value.integer.min = 80000;
1377
uinfo->value.integer.max = 120000;
1378
uinfo->value.integer.step = 1;
1379
return 0;
1380
}
1381
1382
static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
1383
struct snd_ctl_elem_value *ucontrol)
1384
{
1385
struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1386
1387
guard(mutex)(&loopback->cable_lock);
1388
ucontrol->value.integer.value[0] =
1389
loopback->setup[kcontrol->id.subdevice]
1390
[kcontrol->id.device].rate_shift;
1391
return 0;
1392
}
1393
1394
static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
1395
struct snd_ctl_elem_value *ucontrol)
1396
{
1397
struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1398
unsigned int val;
1399
int change = 0;
1400
1401
val = ucontrol->value.integer.value[0];
1402
if (val < 80000)
1403
val = 80000;
1404
if (val > 120000)
1405
val = 120000;
1406
guard(mutex)(&loopback->cable_lock);
1407
if (val != loopback->setup[kcontrol->id.subdevice]
1408
[kcontrol->id.device].rate_shift) {
1409
loopback->setup[kcontrol->id.subdevice]
1410
[kcontrol->id.device].rate_shift = val;
1411
change = 1;
1412
}
1413
return change;
1414
}
1415
1416
static int loopback_notify_get(struct snd_kcontrol *kcontrol,
1417
struct snd_ctl_elem_value *ucontrol)
1418
{
1419
struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1420
1421
guard(mutex)(&loopback->cable_lock);
1422
ucontrol->value.integer.value[0] =
1423
loopback->setup[kcontrol->id.subdevice]
1424
[kcontrol->id.device].notify;
1425
return 0;
1426
}
1427
1428
static int loopback_notify_put(struct snd_kcontrol *kcontrol,
1429
struct snd_ctl_elem_value *ucontrol)
1430
{
1431
struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1432
unsigned int val;
1433
int change = 0;
1434
1435
val = ucontrol->value.integer.value[0] ? 1 : 0;
1436
guard(mutex)(&loopback->cable_lock);
1437
if (val != loopback->setup[kcontrol->id.subdevice]
1438
[kcontrol->id.device].notify) {
1439
loopback->setup[kcontrol->id.subdevice]
1440
[kcontrol->id.device].notify = val;
1441
change = 1;
1442
}
1443
return change;
1444
}
1445
1446
static int loopback_active_get(struct snd_kcontrol *kcontrol,
1447
struct snd_ctl_elem_value *ucontrol)
1448
{
1449
struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1450
struct loopback_cable *cable;
1451
1452
unsigned int val = 0;
1453
1454
guard(mutex)(&loopback->cable_lock);
1455
cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
1456
if (cable != NULL) {
1457
unsigned int running = cable->running ^ cable->pause;
1458
1459
val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
1460
}
1461
ucontrol->value.integer.value[0] = val;
1462
return 0;
1463
}
1464
1465
static int loopback_format_info(struct snd_kcontrol *kcontrol,
1466
struct snd_ctl_elem_info *uinfo)
1467
{
1468
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1469
uinfo->count = 1;
1470
uinfo->value.integer.min = 0;
1471
uinfo->value.integer.max = (__force int)SNDRV_PCM_FORMAT_LAST;
1472
uinfo->value.integer.step = 1;
1473
return 0;
1474
}
1475
1476
static int loopback_format_get(struct snd_kcontrol *kcontrol,
1477
struct snd_ctl_elem_value *ucontrol)
1478
{
1479
struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1480
1481
ucontrol->value.integer.value[0] =
1482
(__force int)loopback->setup[kcontrol->id.subdevice]
1483
[kcontrol->id.device].format;
1484
return 0;
1485
}
1486
1487
static int loopback_rate_info(struct snd_kcontrol *kcontrol,
1488
struct snd_ctl_elem_info *uinfo)
1489
{
1490
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1491
uinfo->count = 1;
1492
uinfo->value.integer.min = 0;
1493
uinfo->value.integer.max = 192000;
1494
uinfo->value.integer.step = 1;
1495
return 0;
1496
}
1497
1498
static int loopback_rate_get(struct snd_kcontrol *kcontrol,
1499
struct snd_ctl_elem_value *ucontrol)
1500
{
1501
struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1502
1503
guard(mutex)(&loopback->cable_lock);
1504
ucontrol->value.integer.value[0] =
1505
loopback->setup[kcontrol->id.subdevice]
1506
[kcontrol->id.device].rate;
1507
return 0;
1508
}
1509
1510
static int loopback_channels_info(struct snd_kcontrol *kcontrol,
1511
struct snd_ctl_elem_info *uinfo)
1512
{
1513
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1514
uinfo->count = 1;
1515
uinfo->value.integer.min = 1;
1516
uinfo->value.integer.max = 1024;
1517
uinfo->value.integer.step = 1;
1518
return 0;
1519
}
1520
1521
static int loopback_channels_get(struct snd_kcontrol *kcontrol,
1522
struct snd_ctl_elem_value *ucontrol)
1523
{
1524
struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1525
1526
guard(mutex)(&loopback->cable_lock);
1527
ucontrol->value.integer.value[0] =
1528
loopback->setup[kcontrol->id.subdevice]
1529
[kcontrol->id.device].channels;
1530
return 0;
1531
}
1532
1533
static int loopback_access_info(struct snd_kcontrol *kcontrol,
1534
struct snd_ctl_elem_info *uinfo)
1535
{
1536
const char * const texts[] = {"Interleaved", "Non-interleaved"};
1537
1538
return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
1539
}
1540
1541
static int loopback_access_get(struct snd_kcontrol *kcontrol,
1542
struct snd_ctl_elem_value *ucontrol)
1543
{
1544
struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1545
snd_pcm_access_t access;
1546
1547
guard(mutex)(&loopback->cable_lock);
1548
access = loopback->setup[kcontrol->id.subdevice][kcontrol->id.device].access;
1549
1550
ucontrol->value.enumerated.item[0] = !is_access_interleaved(access);
1551
1552
return 0;
1553
}
1554
1555
static const struct snd_kcontrol_new loopback_controls[] = {
1556
{
1557
.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1558
.name = "PCM Rate Shift 100000",
1559
.info = loopback_rate_shift_info,
1560
.get = loopback_rate_shift_get,
1561
.put = loopback_rate_shift_put,
1562
},
1563
{
1564
.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1565
.name = "PCM Notify",
1566
.info = snd_ctl_boolean_mono_info,
1567
.get = loopback_notify_get,
1568
.put = loopback_notify_put,
1569
},
1570
#define ACTIVE_IDX 2
1571
{
1572
.access = SNDRV_CTL_ELEM_ACCESS_READ,
1573
.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1574
.name = "PCM Slave Active",
1575
.info = snd_ctl_boolean_mono_info,
1576
.get = loopback_active_get,
1577
},
1578
#define FORMAT_IDX 3
1579
{
1580
.access = SNDRV_CTL_ELEM_ACCESS_READ,
1581
.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1582
.name = "PCM Slave Format",
1583
.info = loopback_format_info,
1584
.get = loopback_format_get
1585
},
1586
#define RATE_IDX 4
1587
{
1588
.access = SNDRV_CTL_ELEM_ACCESS_READ,
1589
.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1590
.name = "PCM Slave Rate",
1591
.info = loopback_rate_info,
1592
.get = loopback_rate_get
1593
},
1594
#define CHANNELS_IDX 5
1595
{
1596
.access = SNDRV_CTL_ELEM_ACCESS_READ,
1597
.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1598
.name = "PCM Slave Channels",
1599
.info = loopback_channels_info,
1600
.get = loopback_channels_get
1601
},
1602
#define ACCESS_IDX 6
1603
{
1604
.access = SNDRV_CTL_ELEM_ACCESS_READ,
1605
.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1606
.name = "PCM Slave Access Mode",
1607
.info = loopback_access_info,
1608
.get = loopback_access_get,
1609
},
1610
};
1611
1612
static int loopback_mixer_new(struct loopback *loopback, int notify)
1613
{
1614
struct snd_card *card = loopback->card;
1615
struct snd_pcm *pcm;
1616
struct snd_kcontrol *kctl;
1617
struct loopback_setup *setup;
1618
int err, dev, substr, substr_count, idx;
1619
1620
strscpy(card->mixername, "Loopback Mixer");
1621
for (dev = 0; dev < 2; dev++) {
1622
pcm = loopback->pcm[dev];
1623
substr_count =
1624
pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1625
for (substr = 0; substr < substr_count; substr++) {
1626
setup = &loopback->setup[substr][dev];
1627
setup->notify = notify;
1628
setup->rate_shift = NO_PITCH;
1629
setup->format = SNDRV_PCM_FORMAT_S16_LE;
1630
setup->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1631
setup->rate = 48000;
1632
setup->channels = 2;
1633
for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1634
idx++) {
1635
kctl = snd_ctl_new1(&loopback_controls[idx],
1636
loopback);
1637
if (!kctl)
1638
return -ENOMEM;
1639
kctl->id.device = dev;
1640
kctl->id.subdevice = substr;
1641
1642
/* Add the control before copying the id so that
1643
* the numid field of the id is set in the copy.
1644
*/
1645
err = snd_ctl_add(card, kctl);
1646
if (err < 0)
1647
return err;
1648
1649
switch (idx) {
1650
case ACTIVE_IDX:
1651
setup->active_id = kctl->id;
1652
break;
1653
case FORMAT_IDX:
1654
setup->format_id = kctl->id;
1655
break;
1656
case RATE_IDX:
1657
setup->rate_id = kctl->id;
1658
break;
1659
case CHANNELS_IDX:
1660
setup->channels_id = kctl->id;
1661
break;
1662
case ACCESS_IDX:
1663
setup->access_id = kctl->id;
1664
break;
1665
default:
1666
break;
1667
}
1668
}
1669
}
1670
}
1671
return 0;
1672
}
1673
1674
static void print_dpcm_info(struct snd_info_buffer *buffer,
1675
struct loopback_pcm *dpcm,
1676
const char *id)
1677
{
1678
snd_iprintf(buffer, " %s\n", id);
1679
if (dpcm == NULL) {
1680
snd_iprintf(buffer, " inactive\n");
1681
return;
1682
}
1683
snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1684
snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1685
snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1686
snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1687
snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1688
snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1689
snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1690
if (dpcm->cable->ops->dpcm_info)
1691
dpcm->cable->ops->dpcm_info(dpcm, buffer);
1692
}
1693
1694
static void print_substream_info(struct snd_info_buffer *buffer,
1695
struct loopback *loopback,
1696
int sub,
1697
int num)
1698
{
1699
struct loopback_cable *cable = loopback->cables[sub][num];
1700
1701
snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1702
if (cable == NULL) {
1703
snd_iprintf(buffer, " inactive\n");
1704
return;
1705
}
1706
snd_iprintf(buffer, " valid: %u\n", cable->valid);
1707
snd_iprintf(buffer, " running: %u\n", cable->running);
1708
snd_iprintf(buffer, " pause: %u\n", cable->pause);
1709
print_dpcm_info(buffer, cable->streams[0], "Playback");
1710
print_dpcm_info(buffer, cable->streams[1], "Capture");
1711
}
1712
1713
static void print_cable_info(struct snd_info_entry *entry,
1714
struct snd_info_buffer *buffer)
1715
{
1716
struct loopback *loopback = entry->private_data;
1717
int sub, num;
1718
1719
guard(mutex)(&loopback->cable_lock);
1720
num = entry->name[strlen(entry->name)-1];
1721
num = num == '0' ? 0 : 1;
1722
for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1723
print_substream_info(buffer, loopback, sub, num);
1724
}
1725
1726
static int loopback_cable_proc_new(struct loopback *loopback, int cidx)
1727
{
1728
char name[32];
1729
1730
snprintf(name, sizeof(name), "cable#%d", cidx);
1731
return snd_card_ro_proc_new(loopback->card, name, loopback,
1732
print_cable_info);
1733
}
1734
1735
static void loopback_set_timer_source(struct loopback *loopback,
1736
const char *value)
1737
{
1738
if (loopback->timer_source) {
1739
devm_kfree(loopback->card->dev, loopback->timer_source);
1740
loopback->timer_source = NULL;
1741
}
1742
if (value && *value)
1743
loopback->timer_source = devm_kstrdup(loopback->card->dev,
1744
value, GFP_KERNEL);
1745
}
1746
1747
static void print_timer_source_info(struct snd_info_entry *entry,
1748
struct snd_info_buffer *buffer)
1749
{
1750
struct loopback *loopback = entry->private_data;
1751
1752
guard(mutex)(&loopback->cable_lock);
1753
snd_iprintf(buffer, "%s\n",
1754
loopback->timer_source ? loopback->timer_source : "");
1755
}
1756
1757
static void change_timer_source_info(struct snd_info_entry *entry,
1758
struct snd_info_buffer *buffer)
1759
{
1760
struct loopback *loopback = entry->private_data;
1761
char line[64];
1762
1763
guard(mutex)(&loopback->cable_lock);
1764
if (!snd_info_get_line(buffer, line, sizeof(line)))
1765
loopback_set_timer_source(loopback, strim(line));
1766
}
1767
1768
static int loopback_timer_source_proc_new(struct loopback *loopback)
1769
{
1770
return snd_card_rw_proc_new(loopback->card, "timer_source", loopback,
1771
print_timer_source_info,
1772
change_timer_source_info);
1773
}
1774
1775
static int loopback_probe(struct platform_device *devptr)
1776
{
1777
struct snd_card *card;
1778
struct loopback *loopback;
1779
int dev = devptr->id;
1780
int err;
1781
1782
err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1783
sizeof(struct loopback), &card);
1784
if (err < 0)
1785
return err;
1786
loopback = card->private_data;
1787
1788
if (pcm_substreams[dev] < 1)
1789
pcm_substreams[dev] = 1;
1790
if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1791
pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1792
1793
loopback->card = card;
1794
loopback_set_timer_source(loopback, timer_source[dev]);
1795
1796
mutex_init(&loopback->cable_lock);
1797
1798
err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1799
if (err < 0)
1800
return err;
1801
err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1802
if (err < 0)
1803
return err;
1804
err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1805
if (err < 0)
1806
return err;
1807
loopback_cable_proc_new(loopback, 0);
1808
loopback_cable_proc_new(loopback, 1);
1809
loopback_timer_source_proc_new(loopback);
1810
strscpy(card->driver, "Loopback");
1811
strscpy(card->shortname, "Loopback");
1812
sprintf(card->longname, "Loopback %i", dev + 1);
1813
err = snd_card_register(card);
1814
if (err < 0)
1815
return err;
1816
platform_set_drvdata(devptr, card);
1817
return 0;
1818
}
1819
1820
static int loopback_suspend(struct device *pdev)
1821
{
1822
struct snd_card *card = dev_get_drvdata(pdev);
1823
1824
snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1825
return 0;
1826
}
1827
1828
static int loopback_resume(struct device *pdev)
1829
{
1830
struct snd_card *card = dev_get_drvdata(pdev);
1831
1832
snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1833
return 0;
1834
}
1835
1836
static DEFINE_SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1837
1838
#define SND_LOOPBACK_DRIVER "snd_aloop"
1839
1840
static struct platform_driver loopback_driver = {
1841
.probe = loopback_probe,
1842
.driver = {
1843
.name = SND_LOOPBACK_DRIVER,
1844
.pm = &loopback_pm,
1845
},
1846
};
1847
1848
static void loopback_unregister_all(void)
1849
{
1850
int i;
1851
1852
for (i = 0; i < ARRAY_SIZE(devices); ++i)
1853
platform_device_unregister(devices[i]);
1854
platform_driver_unregister(&loopback_driver);
1855
}
1856
1857
static int __init alsa_card_loopback_init(void)
1858
{
1859
int i, err, cards;
1860
1861
err = platform_driver_register(&loopback_driver);
1862
if (err < 0)
1863
return err;
1864
1865
1866
cards = 0;
1867
for (i = 0; i < SNDRV_CARDS; i++) {
1868
struct platform_device *device;
1869
if (!enable[i])
1870
continue;
1871
device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1872
i, NULL, 0);
1873
if (IS_ERR(device))
1874
continue;
1875
if (!platform_get_drvdata(device)) {
1876
platform_device_unregister(device);
1877
continue;
1878
}
1879
devices[i] = device;
1880
cards++;
1881
}
1882
if (!cards) {
1883
#ifdef MODULE
1884
pr_err("aloop: No loopback enabled\n");
1885
#endif
1886
loopback_unregister_all();
1887
return -ENODEV;
1888
}
1889
return 0;
1890
}
1891
1892
static void __exit alsa_card_loopback_exit(void)
1893
{
1894
loopback_unregister_all();
1895
}
1896
1897
module_init(alsa_card_loopback_init)
1898
module_exit(alsa_card_loopback_exit)
1899
1900