aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/alsa/test-pcmtest-driver.c
blob: ca81afa4ee90c0560eb1aca30f8089eb1eef97e5 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * This is the test which covers PCM middle layer data transferring using
 * the virtual pcm test driver (snd-pcmtest).
 *
 * Copyright 2023 Ivan Orlov <ivan.orlov0322@gmail.com>
 */
#include <string.h>
#include <alsa/asoundlib.h>
#include "../kselftest_harness.h"

#define CH_NUM 4

struct pattern_buf {
	char buf[1024];
	int len;
};

struct pattern_buf patterns[CH_NUM];

struct pcmtest_test_params {
	unsigned long buffer_size;
	unsigned long period_size;
	unsigned long channels;
	unsigned int rate;
	snd_pcm_access_t access;
	size_t sec_buf_len;
	size_t sample_size;
	int time;
	snd_pcm_format_t format;
};

static int read_patterns(void)
{
	FILE *fp, *fpl;
	int i;
	char pf[64];
	char plf[64];

	for (i = 0; i < CH_NUM; i++) {
		sprintf(plf, "/sys/kernel/debug/pcmtest/fill_pattern%d_len", i);
		fpl = fopen(plf, "r");
		if (!fpl)
			return -1;
		fscanf(fpl, "%u", &patterns[i].len);
		fclose(fpl);

		sprintf(pf, "/sys/kernel/debug/pcmtest/fill_pattern%d", i);
		fp = fopen(pf, "r");
		if (!fp)
			return -1;
		fread(patterns[i].buf, 1, patterns[i].len, fp);
		fclose(fp);
	}

	return 0;
}

static int get_test_results(char *debug_name)
{
	int result;
	FILE *f;
	char fname[128];

	sprintf(fname, "/sys/kernel/debug/pcmtest/%s", debug_name);

	f = fopen(fname, "r");
	if (!f) {
		printf("Failed to open file\n");
		return -1;
	}
	fscanf(f, "%d", &result);
	fclose(f);

	return result;
}

static size_t get_sec_buf_len(unsigned int rate, unsigned long channels, snd_pcm_format_t format)
{
	return rate * channels * snd_pcm_format_physical_width(format) / 8;
}

static int setup_handle(snd_pcm_t **handle, snd_pcm_sw_params_t *swparams,
			snd_pcm_hw_params_t *hwparams, struct pcmtest_test_params *params,
			int card, snd_pcm_stream_t stream)
{
	char pcm_name[32];
	int err;

	sprintf(pcm_name, "hw:%d,0,0", card);
	err = snd_pcm_open(handle, pcm_name, stream, 0);
	if (err < 0)
		return err;
	snd_pcm_hw_params_any(*handle, hwparams);
	snd_pcm_hw_params_set_rate_resample(*handle, hwparams, 0);
	snd_pcm_hw_params_set_access(*handle, hwparams, params->access);
	snd_pcm_hw_params_set_format(*handle, hwparams, params->format);
	snd_pcm_hw_params_set_channels(*handle, hwparams, params->channels);
	snd_pcm_hw_params_set_rate_near(*handle, hwparams, &params->rate, 0);
	snd_pcm_hw_params_set_period_size_near(*handle, hwparams, &params->period_size, 0);
	snd_pcm_hw_params_set_buffer_size_near(*handle, hwparams, &params->buffer_size);
	snd_pcm_hw_params(*handle, hwparams);
	snd_pcm_sw_params_current(*handle, swparams);

	snd_pcm_hw_params_set_rate_resample(*handle, hwparams, 0);
	snd_pcm_sw_params_set_avail_min(*handle, swparams, params->period_size);
	snd_pcm_hw_params_set_buffer_size_near(*handle, hwparams, &params->buffer_size);
	snd_pcm_hw_params_set_period_size_near(*handle, hwparams, &params->period_size, 0);
	snd_pcm_sw_params(*handle, swparams);
	snd_pcm_hw_params(*handle, hwparams);

	return 0;
}

FIXTURE(pcmtest) {
	int card;
	snd_pcm_sw_params_t *swparams;
	snd_pcm_hw_params_t *hwparams;
	struct pcmtest_test_params params;
};

FIXTURE_TEARDOWN(pcmtest) {
}

FIXTURE_SETUP(pcmtest) {
	char *card_name;
	int err;

	if (geteuid())
		SKIP(return, "This test needs root to run!");

	err = read_patterns();
	if (err)
		SKIP(return, "Can't read patterns. Probably, module isn't loaded");

	card_name = malloc(127);
	ASSERT_NE(card_name, NULL);
	self->params.buffer_size = 16384;
	self->params.period_size = 4096;
	self->params.channels = CH_NUM;
	self->params.rate = 8000;
	self->params.access = SND_PCM_ACCESS_RW_INTERLEAVED;
	self->params.format = SND_PCM_FORMAT_S16_LE;
	self->card = -1;
	self->params.sample_size = snd_pcm_format_physical_width(self->params.format) / 8;

	self->params.sec_buf_len = get_sec_buf_len(self->params.rate, self->params.channels,
						   self->params.format);
	self->params.time = 4;

	while (snd_card_next(&self->card) >= 0) {
		if (self->card == -1)
			break;
		snd_card_get_name(self->card, &card_name);
		if (!strcmp(card_name, "PCM-Test"))
			break;
	}
	free(card_name);
	ASSERT_NE(self->card, -1);
}

/*
 * Here we are trying to send the looped monotonically increasing sequence of bytes to the driver.
 * If our data isn't corrupted, the driver will set the content of 'pc_test' debugfs file to '1'
 */
TEST_F(pcmtest, playback) {
	snd_pcm_t *handle;
	unsigned char *it;
	size_t write_res;
	int test_results;
	int i, cur_ch, pos_in_ch;
	void *samples;
	struct pcmtest_test_params *params = &self->params;

	samples = calloc(self->params.sec_buf_len * self->params.time, 1);
	ASSERT_NE(samples, NULL);

	snd_pcm_sw_params_alloca(&self->swparams);
	snd_pcm_hw_params_alloca(&self->hwparams);

	ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams, params,
			       self->card, SND_PCM_STREAM_PLAYBACK), 0);
	snd_pcm_format_set_silence(params->format, samples,
				   params->rate * params->channels * params->time);
	it = samples;
	for (i = 0; i < self->params.sec_buf_len * params->time; i++) {
		cur_ch = (i / params->sample_size) % CH_NUM;
		pos_in_ch = i / params->sample_size / CH_NUM * params->sample_size
			    + (i % params->sample_size);
		it[i] = patterns[cur_ch].buf[pos_in_ch % patterns[cur_ch].len];
	}
	write_res = snd_pcm_writei(handle, samples, params->rate * params->time);
	ASSERT_GE(write_res, 0);

	snd_pcm_close(handle);
	free(samples);
	test_results = get_test_results("pc_test");
	ASSERT_EQ(test_results, 1);
}

/*
 * Here we test that the virtual alsa driver returns looped and monotonically increasing sequence
 * of bytes. In the interleaved mode the buffer will contain samples in the following order:
 * C0, C1, C2, C3, C0, C1, ...
 */
TEST_F(pcmtest, capture) {
	snd_pcm_t *handle;
	unsigned char *it;
	size_t read_res;
	int i, cur_ch, pos_in_ch;
	void *samples;
	struct pcmtest_test_params *params = &self->params;

	samples = calloc(self->params.sec_buf_len * self->params.time, 1);
	ASSERT_NE(samples, NULL);

	snd_pcm_sw_params_alloca(&self->swparams);
	snd_pcm_hw_params_alloca(&self->hwparams);

	ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams,
			       params, self->card, SND_PCM_STREAM_CAPTURE), 0);
	snd_pcm_format_set_silence(params->format, samples,
				   params->rate * params->channels * params->time);
	read_res = snd_pcm_readi(handle, samples, params->rate * params->time);
	ASSERT_GE(read_res, 0);
	snd_pcm_close(handle);
	it = (unsigned char *)samples;
	for (i = 0; i < self->params.sec_buf_len * self->params.time; i++) {
		cur_ch = (i / params->sample_size) % CH_NUM;
		pos_in_ch = i / params->sample_size / CH_NUM * params->sample_size
			    + (i % params->sample_size);
		ASSERT_EQ(it[i], patterns[cur_ch].buf[pos_in_ch % patterns[cur_ch].len]);
	}
	free(samples);
}

// Test capture in the non-interleaved access mode. The are buffers for each recorded channel
TEST_F(pcmtest, ni_capture) {
	snd_pcm_t *handle;
	struct pcmtest_test_params params = self->params;
	char **chan_samples;
	size_t i, j, read_res;

	chan_samples = calloc(CH_NUM, sizeof(*chan_samples));
	ASSERT_NE(chan_samples, NULL);

	snd_pcm_sw_params_alloca(&self->swparams);
	snd_pcm_hw_params_alloca(&self->hwparams);

	params.access = SND_PCM_ACCESS_RW_NONINTERLEAVED;

	ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams,
			       &params, self->card, SND_PCM_STREAM_CAPTURE), 0);

	for (i = 0; i < CH_NUM; i++)
		chan_samples[i] = calloc(params.sec_buf_len * params.time, 1);

	for (i = 0; i < 1; i++) {
		read_res = snd_pcm_readn(handle, (void **)chan_samples, params.rate * params.time);
		ASSERT_GE(read_res, 0);
	}
	snd_pcm_close(handle);

	for (i = 0; i < CH_NUM; i++) {
		for (j = 0; j < params.rate * params.time; j++)
			ASSERT_EQ(chan_samples[i][j], patterns[i].buf[j % patterns[i].len]);
		free(chan_samples[i]);
	}
	free(chan_samples);
}

TEST_F(pcmtest, ni_playback) {
	snd_pcm_t *handle;
	struct pcmtest_test_params params = self->params;
	char **chan_samples;
	size_t i, j, read_res;
	int test_res;

	chan_samples = calloc(CH_NUM, sizeof(*chan_samples));
	ASSERT_NE(chan_samples, NULL);

	snd_pcm_sw_params_alloca(&self->swparams);
	snd_pcm_hw_params_alloca(&self->hwparams);

	params.access = SND_PCM_ACCESS_RW_NONINTERLEAVED;

	ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams,
			       &params, self->card, SND_PCM_STREAM_PLAYBACK), 0);

	for (i = 0; i < CH_NUM; i++) {
		chan_samples[i] = calloc(params.sec_buf_len * params.time, 1);
		for (j = 0; j < params.sec_buf_len * params.time; j++)
			chan_samples[i][j] = patterns[i].buf[j % patterns[i].len];
	}

	for (i = 0; i < 1; i++) {
		read_res = snd_pcm_writen(handle, (void **)chan_samples, params.rate * params.time);
		ASSERT_GE(read_res, 0);
	}

	snd_pcm_close(handle);
	test_res = get_test_results("pc_test");
	ASSERT_EQ(test_res, 1);

	for (i = 0; i < CH_NUM; i++)
		free(chan_samples[i]);
	free(chan_samples);
}

/*
 * Here we are testing the custom ioctl definition inside the virtual driver. If it triggers
 * successfully, the driver sets the content of 'ioctl_test' debugfs file to '1'.
 */
TEST_F(pcmtest, reset_ioctl) {
	snd_pcm_t *handle;
	int test_res;
	struct pcmtest_test_params *params = &self->params;

	snd_pcm_sw_params_alloca(&self->swparams);
	snd_pcm_hw_params_alloca(&self->hwparams);

	ASSERT_EQ(setup_handle(&handle, self->swparams, self->hwparams, params,
			       self->card, SND_PCM_STREAM_CAPTURE), 0);
	snd_pcm_reset(handle);
	test_res = get_test_results("ioctl_test");
	ASSERT_EQ(test_res, 1);
	snd_pcm_close(handle);
}

TEST_HARNESS_MAIN