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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Virtual ALSA driver for PCM testing/fuzzing
*
* Copyright 2023 Ivan Orlov <ivan.orlov0322@gmail.com>
*
* This is a simple virtual ALSA driver, which can be used for audio applications/PCM middle layer
* testing or fuzzing.
* It can:
* - Simulate 'playback' and 'capture' actions
* - Generate random or pattern-based capture data
* - Check playback buffer for containing looped template, and notify about the results
* through the debugfs entry
* - Inject delays into the playback and capturing processes. See 'inject_delay' parameter.
* - Inject errors during the PCM callbacks.
* - Register custom RESET ioctl and notify when it is called through the debugfs entry
* - Work in interleaved and non-interleaved modes
* - Support up to 8 substreams
* - Support up to 4 channels
* - Support framerates from 8 kHz to 48 kHz
*
* When driver works in the capture mode with multiple channels, it duplicates the looped
* pattern to each separate channel. For example, if we have 2 channels, format = U8, interleaved
* access mode and pattern 'abacaba', the DMA buffer will look like aabbccaabbaaaa..., so buffer for
* each channel will contain abacabaabacaba... Same for the non-interleaved mode.
*
* However, it may break the capturing on the higher framerates with small period size, so it is
* better to choose larger period sizes.
*
* You can find the corresponding selftest in the 'alsa' selftests folder.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <sound/pcm.h>
#include <sound/core.h>
#include <linux/dma-mapping.h>
#include <linux/platform_device.h>
#include <linux/timer.h>
#include <linux/random.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#define TIMER_PER_SEC 5
#define TIMER_INTERVAL (HZ / TIMER_PER_SEC)
#define DELAY_JIFFIES HZ
#define PLAYBACK_SUBSTREAM_CNT 8
#define CAPTURE_SUBSTREAM_CNT 8
#define MAX_CHANNELS_NUM 4
#define DEFAULT_PATTERN "abacaba"
#define DEFAULT_PATTERN_LEN 7
#define FILL_MODE_RAND 0
#define FILL_MODE_PAT 1
#define MAX_PATTERN_LEN 4096
static int index = -1;
static char *id = "pcmtest";
static bool enable = true;
static int inject_delay;
static bool inject_hwpars_err;
static bool inject_prepare_err;
static bool inject_trigger_err;
static bool inject_open_err;
static short fill_mode = FILL_MODE_PAT;
static u8 playback_capture_test;
static u8 ioctl_reset_test;
static struct dentry *driver_debug_dir;
module_param(index, int, 0444);
MODULE_PARM_DESC(index, "Index value for pcmtest soundcard");
module_param(id, charp, 0444);
MODULE_PARM_DESC(id, "ID string for pcmtest soundcard");
module_param(enable, bool, 0444);
MODULE_PARM_DESC(enable, "Enable pcmtest soundcard.");
module_param(fill_mode, short, 0600);
MODULE_PARM_DESC(fill_mode, "Buffer fill mode: rand(0) or pattern(1)");
module_param(inject_delay, int, 0600);
MODULE_PARM_DESC(inject_delay, "Inject delays during playback/capture (in jiffies)");
module_param(inject_hwpars_err, bool, 0600);
MODULE_PARM_DESC(inject_hwpars_err, "Inject EBUSY error in the 'hw_params' callback");
module_param(inject_prepare_err, bool, 0600);
MODULE_PARM_DESC(inject_prepare_err, "Inject EINVAL error in the 'prepare' callback");
module_param(inject_trigger_err, bool, 0600);
MODULE_PARM_DESC(inject_trigger_err, "Inject EINVAL error in the 'trigger' callback");
module_param(inject_open_err, bool, 0600);
MODULE_PARM_DESC(inject_open_err, "Inject EBUSY error in the 'open' callback");
struct pcmtst {
struct snd_pcm *pcm;
struct snd_card *card;
struct platform_device *pdev;
};
struct pcmtst_buf_iter {
size_t buf_pos; // position in the DMA buffer
size_t period_pos; // period-relative position
size_t b_rw; // Bytes to write on every timer tick
size_t s_rw_ch; // Samples to write to one channel on every tick
unsigned int sample_bytes; // sample_bits / 8
bool is_buf_corrupted; // playback test result indicator
size_t period_bytes; // bytes in a one period
bool interleaved; // Interleaved/Non-interleaved mode
size_t total_bytes; // Total bytes read/written
size_t chan_block; // Bytes in one channel buffer when non-interleaved
struct snd_pcm_substream *substream;
bool suspend; // We need to pause timer without shutting it down
struct timer_list timer_instance;
};
static struct snd_pcm_hardware snd_pcmtst_hw = {
.info = (SNDRV_PCM_INFO_INTERLEAVED |
SNDRV_PCM_INFO_BLOCK_TRANSFER |
SNDRV_PCM_INFO_NONINTERLEAVED |
SNDRV_PCM_INFO_MMAP_VALID |
SNDRV_PCM_INFO_PAUSE),
.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
.rates = SNDRV_PCM_RATE_8000_48000,
.rate_min = 8000,
.rate_max = 48000,
.channels_min = 1,
.channels_max = MAX_CHANNELS_NUM,
.buffer_bytes_max = 128 * 1024,
.period_bytes_min = 4096,
.period_bytes_max = 32768,
.periods_min = 1,
.periods_max = 1024,
};
struct pattern_buf {
char *buf;
u32 len;
};
static int buf_allocated;
static struct pattern_buf patt_bufs[MAX_CHANNELS_NUM];
static inline void inc_buf_pos(struct pcmtst_buf_iter *v_iter, size_t by, size_t bytes)
{
v_iter->total_bytes += by;
v_iter->buf_pos += by;
if (v_iter->buf_pos >= bytes)
v_iter->buf_pos %= bytes;
}
/*
* Position in the DMA buffer when we are in the non-interleaved mode. We increment buf_pos
* every time we write a byte to any channel, so the position in the current channel buffer is
* (position in the DMA buffer) / count_of_channels + size_of_channel_buf * current_channel
*/
static inline size_t buf_pos_n(struct pcmtst_buf_iter *v_iter, unsigned int channels,
unsigned int chan_num)
{
return v_iter->buf_pos / channels + v_iter->chan_block * chan_num;
}
/*
* Get the count of bytes written for the current channel in the interleaved mode.
* This is (count of samples written for the current channel) * bytes_in_sample +
* (relative position in the current sample)
*/
static inline size_t ch_pos_i(size_t b_total, unsigned int channels, unsigned int b_sample)
{
return b_total / channels / b_sample * b_sample + (b_total % b_sample);
}
static void check_buf_block_i(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
{
size_t i;
short ch_num;
u8 current_byte;
for (i = 0; i < v_iter->b_rw; i++) {
current_byte = runtime->dma_area[v_iter->buf_pos];
if (!current_byte)
break;
ch_num = (v_iter->total_bytes / v_iter->sample_bytes) % runtime->channels;
if (current_byte != patt_bufs[ch_num].buf[ch_pos_i(v_iter->total_bytes,
runtime->channels,
v_iter->sample_bytes)
% patt_bufs[ch_num].len]) {
v_iter->is_buf_corrupted = true;
break;
}
inc_buf_pos(v_iter, 1, runtime->dma_bytes);
}
// If we broke during the loop, add remaining bytes to the buffer position.
inc_buf_pos(v_iter, v_iter->b_rw - i, runtime->dma_bytes);
}
static void check_buf_block_ni(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
{
unsigned int channels = runtime->channels;
size_t i;
short ch_num;
u8 current_byte;
for (i = 0; i < v_iter->b_rw; i++) {
ch_num = i % channels;
current_byte = runtime->dma_area[buf_pos_n(v_iter, channels, ch_num)];
if (!current_byte)
break;
if (current_byte != patt_bufs[ch_num].buf[(v_iter->total_bytes / channels)
% patt_bufs[ch_num].len]) {
v_iter->is_buf_corrupted = true;
break;
}
inc_buf_pos(v_iter, 1, runtime->dma_bytes);
}
inc_buf_pos(v_iter, v_iter->b_rw - i, runtime->dma_bytes);
}
/*
* Check one block of the buffer. Here we iterate the buffer until we find '0'. This condition is
* necessary because we need to detect when the reading/writing ends, so we assume that the pattern
* doesn't contain zeros.
*/
static void check_buf_block(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
{
if (v_iter->interleaved)
check_buf_block_i(v_iter, runtime);
else
check_buf_block_ni(v_iter, runtime);
}
/*
* Fill buffer in the non-interleaved mode. The order of samples is C0, ..., C0, C1, ..., C1, C2...
* The channel buffers lay in the DMA buffer continuously (see default copy
* handlers in the pcm_lib.c file).
*
* Here we increment the DMA buffer position every time we write a byte to any channel 'buffer'.
* We need this to simulate the correct hardware pointer moving.
*/
static void fill_block_pattern_n(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
{
size_t i;
unsigned int channels = runtime->channels;
short ch_num;
for (i = 0; i < v_iter-&g
|