forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aloop.c
1836 lines (1627 loc) · 51.6 KB
/
aloop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Loopback soundcard
*
* Original code:
* Copyright (c) by Jaroslav Kysela <[email protected]>
*
* More accurate positioning and full-duplex support:
* Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
*
* Major (almost complete) rewrite:
* Copyright (c) by Takashi Iwai <[email protected]>
*
* A next major update in 2010 (separate timers for playback and capture):
* Copyright (c) Jaroslav Kysela <[email protected]>
*/
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/wait.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <sound/core.h>
#include <sound/control.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/info.h>
#include <sound/initval.h>
#include <sound/timer.h>
MODULE_AUTHOR("Jaroslav Kysela <[email protected]>");
MODULE_DESCRIPTION("A loopback soundcard");
MODULE_LICENSE("GPL");
#define MAX_PCM_SUBSTREAMS 8
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
static int pcm_notify[SNDRV_CARDS];
static char *timer_source[SNDRV_CARDS];
module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
module_param_array(pcm_substreams, int, NULL, 0444);
MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
module_param_array(pcm_notify, int, NULL, 0444);
MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
module_param_array(timer_source, charp, NULL, 0444);
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].");
#define NO_PITCH 100000
#define CABLE_VALID_PLAYBACK BIT(SNDRV_PCM_STREAM_PLAYBACK)
#define CABLE_VALID_CAPTURE BIT(SNDRV_PCM_STREAM_CAPTURE)
#define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE)
struct loopback_cable;
struct loopback_pcm;
struct loopback_ops {
/* optional
* call in loopback->cable_lock
*/
int (*open)(struct loopback_pcm *dpcm);
/* required
* call in cable->lock
*/
int (*start)(struct loopback_pcm *dpcm);
/* required
* call in cable->lock
*/
int (*stop)(struct loopback_pcm *dpcm);
/* optional */
int (*stop_sync)(struct loopback_pcm *dpcm);
/* optional */
int (*close_substream)(struct loopback_pcm *dpcm);
/* optional
* call in loopback->cable_lock
*/
int (*close_cable)(struct loopback_pcm *dpcm);
/* optional
* call in cable->lock
*/
unsigned int (*pos_update)(struct loopback_cable *cable);
/* optional */
void (*dpcm_info)(struct loopback_pcm *dpcm,
struct snd_info_buffer *buffer);
};
struct loopback_cable {
spinlock_t lock;
struct loopback_pcm *streams[2];
struct snd_pcm_hardware hw;
/* flags */
unsigned int valid;
unsigned int running;
unsigned int pause;
/* timer specific */
const struct loopback_ops *ops;
/* If sound timer is used */
struct {
int stream;
struct snd_timer_id id;
struct work_struct event_work;
struct snd_timer_instance *instance;
} snd_timer;
};
struct loopback_setup {
unsigned int notify: 1;
unsigned int rate_shift;
snd_pcm_format_t format;
unsigned int rate;
unsigned int channels;
struct snd_ctl_elem_id active_id;
struct snd_ctl_elem_id format_id;
struct snd_ctl_elem_id rate_id;
struct snd_ctl_elem_id channels_id;
};
struct loopback {
struct snd_card *card;
struct mutex cable_lock;
struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
struct snd_pcm *pcm[2];
struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
const char *timer_source;
};
struct loopback_pcm {
struct loopback *loopback;
struct snd_pcm_substream *substream;
struct loopback_cable *cable;
unsigned int pcm_buffer_size;
unsigned int buf_pos; /* position in buffer */
unsigned int silent_size;
/* PCM parameters */
unsigned int pcm_period_size;
unsigned int pcm_bps; /* bytes per second */
unsigned int pcm_salign; /* bytes per sample * channels */
unsigned int pcm_rate_shift; /* rate shift value */
/* flags */
unsigned int period_update_pending :1;
/* timer stuff */
unsigned int irq_pos; /* fractional IRQ position in jiffies
* ticks
*/
unsigned int period_size_frac; /* period size in jiffies ticks */
unsigned int last_drift;
unsigned long last_jiffies;
/* If jiffies timer is used */
struct timer_list timer;
};
static struct platform_device *devices[SNDRV_CARDS];
static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
{
if (dpcm->pcm_rate_shift == NO_PITCH) {
x /= HZ;
} else {
x = div_u64(NO_PITCH * (unsigned long long)x,
HZ * (unsigned long long)dpcm->pcm_rate_shift);
}
return x - (x % dpcm->pcm_salign);
}
static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
{
if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
return x * HZ;
} else {
x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
NO_PITCH);
}
return x;
}
static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
{
int device = dpcm->substream->pstr->pcm->device;
if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
device ^= 1;
return &dpcm->loopback->setup[dpcm->substream->number][device];
}
static inline unsigned int get_notify(struct loopback_pcm *dpcm)
{
return get_setup(dpcm)->notify;
}
static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
{
return get_setup(dpcm)->rate_shift;
}
/* call in cable->lock */
static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm)
{
unsigned long tick;
unsigned int rate_shift = get_rate_shift(dpcm);
if (rate_shift != dpcm->pcm_rate_shift) {
dpcm->pcm_rate_shift = rate_shift;
dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
}
if (dpcm->period_size_frac <= dpcm->irq_pos) {
dpcm->irq_pos %= dpcm->period_size_frac;
dpcm->period_update_pending = 1;
}
tick = dpcm->period_size_frac - dpcm->irq_pos;
tick = DIV_ROUND_UP(tick, dpcm->pcm_bps);
mod_timer(&dpcm->timer, jiffies + tick);
return 0;
}
/* call in cable->lock */
static int loopback_snd_timer_start(struct loopback_pcm *dpcm)
{
struct loopback_cable *cable = dpcm->cable;
int err;
/* Loopback device has to use same period as timer card. Therefore
* wake up for each snd_pcm_period_elapsed() call of timer card.
*/
err = snd_timer_start(cable->snd_timer.instance, 1);
if (err < 0) {
/* do not report error if trying to start but already
* running. For example called by opposite substream
* of the same cable
*/
if (err == -EBUSY)
return 0;
pcm_err(dpcm->substream->pcm,
"snd_timer_start(%d,%d,%d) failed with %d",
cable->snd_timer.id.card,
cable->snd_timer.id.device,
cable->snd_timer.id.subdevice,
err);
}
return err;
}
/* call in cable->lock */
static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm)
{
del_timer(&dpcm->timer);
dpcm->timer.expires = 0;
return 0;
}
/* call in cable->lock */
static int loopback_snd_timer_stop(struct loopback_pcm *dpcm)
{
struct loopback_cable *cable = dpcm->cable;
int err;
/* only stop if both devices (playback and capture) are not running */
if (cable->running ^ cable->pause)
return 0;
err = snd_timer_stop(cable->snd_timer.instance);
if (err < 0) {
pcm_err(dpcm->substream->pcm,
"snd_timer_stop(%d,%d,%d) failed with %d",
cable->snd_timer.id.card,
cable->snd_timer.id.device,
cable->snd_timer.id.subdevice,
err);
}
return err;
}
static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm)
{
del_timer_sync(&dpcm->timer);
return 0;
}
/* call in loopback->cable_lock */
static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm)
{
struct loopback_cable *cable = dpcm->cable;
/* snd_timer was not opened */
if (!cable->snd_timer.instance)
return 0;
/* will only be called from free_cable() when other stream was
* already closed. Other stream cannot be reopened as long as
* loopback->cable_lock is locked. Therefore no need to lock
* cable->lock;
*/
snd_timer_close(cable->snd_timer.instance);
/* wait till drain work has finished if requested */
cancel_work_sync(&cable->snd_timer.event_work);
snd_timer_instance_free(cable->snd_timer.instance);
memset(&cable->snd_timer, 0, sizeof(cable->snd_timer));
return 0;
}
static int loopback_check_format(struct loopback_cable *cable, int stream)
{
struct snd_pcm_runtime *runtime, *cruntime;
struct loopback_setup *setup;
struct snd_card *card;
int check;
if (cable->valid != CABLE_VALID_BOTH) {
if (stream == SNDRV_PCM_STREAM_PLAYBACK)
goto __notify;
return 0;
}
runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
substream->runtime;
cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
substream->runtime;
check = runtime->format != cruntime->format ||
runtime->rate != cruntime->rate ||
runtime->channels != cruntime->channels;
if (!check)
return 0;
if (stream == SNDRV_PCM_STREAM_CAPTURE) {
return -EIO;
} else {
snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
substream, SNDRV_PCM_STATE_DRAINING);
__notify:
runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
substream->runtime;
setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
if (setup->format != runtime->format) {
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
&setup->format_id);
setup->format = runtime->format;
}
if (setup->rate != runtime->rate) {
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
&setup->rate_id);
setup->rate = runtime->rate;
}
if (setup->channels != runtime->channels) {
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
&setup->channels_id);
setup->channels = runtime->channels;
}
}
return 0;
}
static void loopback_active_notify(struct loopback_pcm *dpcm)
{
snd_ctl_notify(dpcm->loopback->card,
SNDRV_CTL_EVENT_MASK_VALUE,
&get_setup(dpcm)->active_id);
}
static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct loopback_pcm *dpcm = runtime->private_data;
struct loopback_cable *cable = dpcm->cable;
int err = 0, stream = 1 << substream->stream;
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
err = loopback_check_format(cable, substream->stream);
if (err < 0)
return err;
dpcm->last_jiffies = jiffies;
dpcm->pcm_rate_shift = 0;
dpcm->last_drift = 0;
spin_lock(&cable->lock);
cable->running |= stream;
cable->pause &= ~stream;
err = cable->ops->start(dpcm);
spin_unlock(&cable->lock);
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
loopback_active_notify(dpcm);
break;
case SNDRV_PCM_TRIGGER_STOP:
spin_lock(&cable->lock);
cable->running &= ~stream;
cable->pause &= ~stream;
err = cable->ops->stop(dpcm);
spin_unlock(&cable->lock);
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
loopback_active_notify(dpcm);
break;
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
case SNDRV_PCM_TRIGGER_SUSPEND:
spin_lock(&cable->lock);
cable->pause |= stream;
err = cable->ops->stop(dpcm);
spin_unlock(&cable->lock);
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
loopback_active_notify(dpcm);
break;
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
case SNDRV_PCM_TRIGGER_RESUME:
spin_lock(&cable->lock);
dpcm->last_jiffies = jiffies;
cable->pause &= ~stream;
err = cable->ops->start(dpcm);
spin_unlock(&cable->lock);
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
loopback_active_notify(dpcm);
break;
default:
return -EINVAL;
}
return err;
}
static void params_change(struct snd_pcm_substream *substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct loopback_pcm *dpcm = runtime->private_data;
struct loopback_cable *cable = dpcm->cable;
cable->hw.formats = pcm_format_to_bits(runtime->format);
cable->hw.rate_min = runtime->rate;
cable->hw.rate_max = runtime->rate;
cable->hw.channels_min = runtime->channels;
cable->hw.channels_max = runtime->channels;
if (cable->snd_timer.instance) {
cable->hw.period_bytes_min =
frames_to_bytes(runtime, runtime->period_size);
cable->hw.period_bytes_max = cable->hw.period_bytes_min;
}
}
static int loopback_prepare(struct snd_pcm_substream *substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct loopback_pcm *dpcm = runtime->private_data;
struct loopback_cable *cable = dpcm->cable;
int err, bps, salign;
if (cable->ops->stop_sync) {
err = cable->ops->stop_sync(dpcm);
if (err < 0)
return err;
}
salign = (snd_pcm_format_physical_width(runtime->format) *
runtime->channels) / 8;
bps = salign * runtime->rate;
if (bps <= 0 || salign <= 0)
return -EINVAL;
dpcm->buf_pos = 0;
dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
/* clear capture buffer */
dpcm->silent_size = dpcm->pcm_buffer_size;
snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
runtime->buffer_size * runtime->channels);
}
dpcm->irq_pos = 0;
dpcm->period_update_pending = 0;
dpcm->pcm_bps = bps;
dpcm->pcm_salign = salign;
dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
mutex_lock(&dpcm->loopback->cable_lock);
if (!(cable->valid & ~(1 << substream->stream)) ||
(get_setup(dpcm)->notify &&
substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
params_change(substream);
cable->valid |= 1 << substream->stream;
mutex_unlock(&dpcm->loopback->cable_lock);
return 0;
}
static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
{
struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
char *dst = runtime->dma_area;
unsigned int dst_off = dpcm->buf_pos;
if (dpcm->silent_size >= dpcm->pcm_buffer_size)
return;
if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
for (;;) {
unsigned int size = bytes;
if (dst_off + size > dpcm->pcm_buffer_size)
size = dpcm->pcm_buffer_size - dst_off;
snd_pcm_format_set_silence(runtime->format, dst + dst_off,
bytes_to_frames(runtime, size) *
runtime->channels);
dpcm->silent_size += size;
bytes -= size;
if (!bytes)
break;
dst_off = 0;
}
}
static void copy_play_buf(struct loopback_pcm *play,
struct loopback_pcm *capt,
unsigned int bytes)
{
struct snd_pcm_runtime *runtime = play->substream->runtime;
char *src = runtime->dma_area;
char *dst = capt->substream->runtime->dma_area;
unsigned int src_off = play->buf_pos;
unsigned int dst_off = capt->buf_pos;
unsigned int clear_bytes = 0;
/* check if playback is draining, trim the capture copy size
* when our pointer is at the end of playback ring buffer */
if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
appl_ptr1 += play->buf_pos / play->pcm_salign;
if (appl_ptr < appl_ptr1)
appl_ptr1 -= runtime->buffer_size;
diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
if (diff < bytes) {
clear_bytes = bytes - diff;
bytes = diff;
}
}
for (;;) {
unsigned int size = bytes;
if (src_off + size > play->pcm_buffer_size)
size = play->pcm_buffer_size - src_off;
if (dst_off + size > capt->pcm_buffer_size)
size = capt->pcm_buffer_size - dst_off;
memcpy(dst + dst_off, src + src_off, size);
capt->silent_size = 0;
bytes -= size;
if (!bytes)
break;
src_off = (src_off + size) % play->pcm_buffer_size;
dst_off = (dst_off + size) % capt->pcm_buffer_size;
}
if (clear_bytes > 0) {
clear_capture_buf(capt, clear_bytes);
capt->silent_size = 0;
}
}
static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
unsigned int jiffies_delta)
{
unsigned long last_pos;
unsigned int delta;
last_pos = byte_pos(dpcm, dpcm->irq_pos);
dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
if (delta >= dpcm->last_drift)
delta -= dpcm->last_drift;
dpcm->last_drift = 0;
if (dpcm->irq_pos >= dpcm->period_size_frac) {
dpcm->irq_pos %= dpcm->period_size_frac;
dpcm->period_update_pending = 1;
}
return delta;
}
static inline void bytepos_finish(struct loopback_pcm *dpcm,
unsigned int delta)
{
dpcm->buf_pos += delta;
dpcm->buf_pos %= dpcm->pcm_buffer_size;
}
/* call in cable->lock */
static unsigned int loopback_jiffies_timer_pos_update
(struct loopback_cable *cable)
{
struct loopback_pcm *dpcm_play =
cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
struct loopback_pcm *dpcm_capt =
cable->streams[SNDRV_PCM_STREAM_CAPTURE];
unsigned long delta_play = 0, delta_capt = 0;
unsigned int running, count1, count2;
running = cable->running ^ cable->pause;
if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
delta_play = jiffies - dpcm_play->last_jiffies;
dpcm_play->last_jiffies += delta_play;
}
if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
delta_capt = jiffies - dpcm_capt->last_jiffies;
dpcm_capt->last_jiffies += delta_capt;
}
if (delta_play == 0 && delta_capt == 0)
goto unlock;
if (delta_play > delta_capt) {
count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
bytepos_finish(dpcm_play, count1);
delta_play = delta_capt;
} else if (delta_play < delta_capt) {
count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
clear_capture_buf(dpcm_capt, count1);
bytepos_finish(dpcm_capt, count1);
delta_capt = delta_play;
}
if (delta_play == 0 && delta_capt == 0)
goto unlock;
/* note delta_capt == delta_play at this moment */
count1 = bytepos_delta(dpcm_play, delta_play);
count2 = bytepos_delta(dpcm_capt, delta_capt);
if (count1 < count2) {
dpcm_capt->last_drift = count2 - count1;
count1 = count2;
} else if (count1 > count2) {
dpcm_play->last_drift = count1 - count2;
}
copy_play_buf(dpcm_play, dpcm_capt, count1);
bytepos_finish(dpcm_play, count1);
bytepos_finish(dpcm_capt, count1);
unlock:
return running;
}
static void loopback_jiffies_timer_function(struct timer_list *t)
{
struct loopback_pcm *dpcm = from_timer(dpcm, t, timer);
unsigned long flags;
spin_lock_irqsave(&dpcm->cable->lock, flags);
if (loopback_jiffies_timer_pos_update(dpcm->cable) &
(1 << dpcm->substream->stream)) {
loopback_jiffies_timer_start(dpcm);
if (dpcm->period_update_pending) {
dpcm->period_update_pending = 0;
spin_unlock_irqrestore(&dpcm->cable->lock, flags);
/* need to unlock before calling below */
snd_pcm_period_elapsed(dpcm->substream);
return;
}
}
spin_unlock_irqrestore(&dpcm->cable->lock, flags);
}
/* call in cable->lock */
static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime,
unsigned long resolution)
{
if (resolution != runtime->timer_resolution) {
struct loopback_pcm *dpcm = runtime->private_data;
struct loopback_cable *cable = dpcm->cable;
/* Worst case estimation of possible values for resolution
* resolution <= (512 * 1024) frames / 8kHz in nsec
* resolution <= 65.536.000.000 nsec
*
* period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz +
* 500.000
* period_size <= 12.582.912.000.000 <64bit
* / 1.000.000 usec/sec
*/
snd_pcm_uframes_t period_size_usec =
resolution / 1000 * runtime->rate;
/* round to nearest sample rate */
snd_pcm_uframes_t period_size =
(period_size_usec + 500 * 1000) / (1000 * 1000);
pcm_err(dpcm->substream->pcm,
"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.",
runtime->period_size, resolution, period_size,
cable->snd_timer.id.card,
cable->snd_timer.id.device,
cable->snd_timer.id.subdevice,
period_size);
return -EINVAL;
}
return 0;
}
static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,
int event,
unsigned long resolution)
{
struct loopback_pcm *dpcm_play, *dpcm_capt;
struct snd_pcm_substream *substream_play, *substream_capt;
struct snd_pcm_runtime *valid_runtime;
unsigned int running, elapsed_bytes;
unsigned long flags;
spin_lock_irqsave(&cable->lock, flags);
running = cable->running ^ cable->pause;
/* no need to do anything if no stream is running */
if (!running) {
spin_unlock_irqrestore(&cable->lock, flags);
return;
}
dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
if (event == SNDRV_TIMER_EVENT_MSTOP) {
if (!dpcm_play ||
dpcm_play->substream->runtime->status->state !=
SNDRV_PCM_STATE_DRAINING) {
spin_unlock_irqrestore(&cable->lock, flags);
return;
}
}
substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
dpcm_play->substream : NULL;
substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
dpcm_capt->substream : NULL;
valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
dpcm_play->substream->runtime :
dpcm_capt->substream->runtime;
/* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
if (event == SNDRV_TIMER_EVENT_TICK) {
/* The hardware rules guarantee that playback and capture period
* are the same. Therefore only one device has to be checked
* here.
*/
if (loopback_snd_timer_check_resolution(valid_runtime,
resolution) < 0) {
spin_unlock_irqrestore(&cable->lock, flags);
if (substream_play)
snd_pcm_stop_xrun(substream_play);
if (substream_capt)
snd_pcm_stop_xrun(substream_capt);
return;
}
}
elapsed_bytes = frames_to_bytes(valid_runtime,
valid_runtime->period_size);
/* The same timer interrupt is used for playback and capture device */
if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
(running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
bytepos_finish(dpcm_play, elapsed_bytes);
bytepos_finish(dpcm_capt, elapsed_bytes);
} else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
bytepos_finish(dpcm_play, elapsed_bytes);
} else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
clear_capture_buf(dpcm_capt, elapsed_bytes);
bytepos_finish(dpcm_capt, elapsed_bytes);
}
spin_unlock_irqrestore(&cable->lock, flags);
if (substream_play)
snd_pcm_period_elapsed(substream_play);
if (substream_capt)
snd_pcm_period_elapsed(substream_capt);
}
static void loopback_snd_timer_function(struct snd_timer_instance *timeri,
unsigned long resolution,
unsigned long ticks)
{
struct loopback_cable *cable = timeri->callback_data;
loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_TICK,
resolution);
}
static void loopback_snd_timer_work(struct work_struct *work)
{
struct loopback_cable *cable;
cable = container_of(work, struct loopback_cable, snd_timer.event_work);
loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_MSTOP, 0);
}
static void loopback_snd_timer_event(struct snd_timer_instance *timeri,
int event,
struct timespec64 *tstamp,
unsigned long resolution)
{
/* Do not lock cable->lock here because timer->lock is already hold.
* There are other functions which first lock cable->lock and than
* timer->lock e.g.
* loopback_trigger()
* spin_lock(&cable->lock)
* loopback_snd_timer_start()
* snd_timer_start()
* spin_lock(&timer->lock)
* Therefore when using the oposit order of locks here it could result
* in a deadlock.
*/
if (event == SNDRV_TIMER_EVENT_MSTOP) {
struct loopback_cable *cable = timeri->callback_data;
/* sound card of the timer was stopped. Therefore there will not
* be any further timer callbacks. Due to this forward audio
* data from here if in draining state. When still in running
* state the streaming will be aborted by the usual timeout. It
* should not be aborted here because may be the timer sound
* card does only a recovery and the timer is back soon.
* This work triggers loopback_snd_timer_work()
*/
schedule_work(&cable->snd_timer.event_work);
}
}
static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm,
struct snd_info_buffer *buffer)
{
snd_iprintf(buffer, " update_pending:\t%u\n",
dpcm->period_update_pending);
snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
dpcm->last_jiffies, jiffies);
snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
}
static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm,
struct snd_info_buffer *buffer)
{
struct loopback_cable *cable = dpcm->cable;
snd_iprintf(buffer, " sound timer:\thw:%d,%d,%d\n",
cable->snd_timer.id.card,
cable->snd_timer.id.device,
cable->snd_timer.id.subdevice);
snd_iprintf(buffer, " timer open:\t\t%s\n",
(cable->snd_timer.stream == SNDRV_PCM_STREAM_CAPTURE) ?
"capture" : "playback");
}
static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct loopback_pcm *dpcm = runtime->private_data;
snd_pcm_uframes_t pos;
spin_lock(&dpcm->cable->lock);
if (dpcm->cable->ops->pos_update)
dpcm->cable->ops->pos_update(dpcm->cable);
pos = dpcm->buf_pos;
spin_unlock(&dpcm->cable->lock);
return bytes_to_frames(runtime, pos);
}
static const struct snd_pcm_hardware loopback_pcm_hardware =
{
.info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
SNDRV_PCM_INFO_RESUME),
.formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
.rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
.rate_min = 8000,
.rate_max = 192000,
.channels_min = 1,
.channels_max = 32,
.buffer_bytes_max = 2 * 1024 * 1024,
.period_bytes_min = 64,
/* note check overflow in frac_pos() using pcm_rate_shift before
changing period_bytes_max value */
.period_bytes_max = 1024 * 1024,
.periods_min = 1,
.periods_max = 1024,
.fifo_size = 0,
};
static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
{
struct loopback_pcm *dpcm = runtime->private_data;
kfree(dpcm);
}
static int loopback_hw_free(struct snd_pcm_substream *substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct loopback_pcm *dpcm = runtime->private_data;
struct loopback_cable *cable = dpcm->cable;
mutex_lock(&dpcm->loopback->cable_lock);
cable->valid &= ~(1 << substream->stream);
mutex_unlock(&dpcm->loopback->cable_lock);
return 0;
}
static unsigned int get_cable_index(struct snd_pcm_substream *substream)
{
if (!substream->pcm->device)
return substream->stream;
else
return !substream->stream;
}
static int rule_format(struct snd_pcm_hw_params *params,
struct snd_pcm_hw_rule *rule)
{
struct loopback_pcm *dpcm = rule->private;
struct loopback_cable *cable = dpcm->cable;
struct snd_mask m;
snd_mask_none(&m);
mutex_lock(&dpcm->loopback->cable_lock);
m.bits[0] = (u_int32_t)cable->hw.formats;
m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
mutex_unlock(&dpcm->loopback->cable_lock);
return snd_mask_refine(hw_param_mask(params, rule->var), &m);
}
static int rule_rate(struct snd_pcm_hw_params *params,
struct snd_pcm_hw_rule *rule)
{
struct loopback_pcm *dpcm = rule->private;
struct loopback_cable *cable = dpcm->cable;
struct snd_interval t;
mutex_lock(&dpcm->loopback->cable_lock);
t.min = cable->hw.rate_min;
t.max = cable->hw.rate_max;
mutex_unlock(&dpcm->loopback->cable_lock);
t.openmin = t.openmax = 0;
t.integer = 0;
return snd_interval_refine(hw_param_interval(params, rule->var), &t);
}
static int rule_channels(struct snd_pcm_hw_params *params,
struct snd_pcm_hw_rule *rule)
{
struct loopback_pcm *dpcm = rule->private;
struct loopback_cable *cable = dpcm->cable;
struct snd_interval t;
mutex_lock(&dpcm->loopback->cable_lock);
t.min = cable->hw.channels_min;
t.max = cable->hw.channels_max;
mutex_unlock(&dpcm->loopback->cable_lock);
t.openmin = t.openmax = 0;
t.integer = 0;
return snd_interval_refine(hw_param_interval(params, rule->var), &t);
}
static int rule_period_bytes(struct snd_pcm_hw_params *params,
struct snd_pcm_hw_rule *rule)
{
struct loopback_pcm *dpcm = rule->private;
struct loopback_cable *cable = dpcm->cable;
struct snd_interval t;
mutex_lock(&dpcm->loopback->cable_lock);
t.min = cable->hw.period_bytes_min;
t.max = cable->hw.period_bytes_max;
mutex_unlock(&dpcm->loopback->cable_lock);
t.openmin = 0;
t.openmax = 0;
t.integer = 0;
return snd_interval_refine(hw_param_interval(params, rule->var), &t);
}
static void free_cable(struct snd_pcm_substream *substream)
{
struct loopback *loopback = substream->private_data;
int dev = get_cable_index(substream);
struct loopback_cable *cable;
cable = loopback->cables[substream->number][dev];
if (!cable)
return;
if (cable->streams[!substream->stream]) {