xref: /linux/sound/drivers/pcmtest.c (revision ca141fe31df06fd5769e3d1d41e2a7b68b308243)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Virtual ALSA driver for PCM testing/fuzzing
4  *
5  * Copyright 2023 Ivan Orlov <ivan.orlov0322@gmail.com>
6  *
7  * This is a simple virtual ALSA driver, which can be used for audio applications/PCM middle layer
8  * testing or fuzzing.
9  * It can:
10  *	- Simulate 'playback' and 'capture' actions
11  *	- Generate random or pattern-based capture data
12  *	- Check playback buffer for containing looped template, and notify about the results
13  *	through the debugfs entry
14  *	- Inject delays into the playback and capturing processes. See 'inject_delay' parameter.
15  *	- Inject errors during the PCM callbacks.
16  *	- Register custom RESET ioctl and notify when it is called through the debugfs entry
17  *	- Work in interleaved and non-interleaved modes
18  *	- Support up to 8 substreams
19  *	- Support up to 4 channels
20  *	- Support framerates from 8 kHz to 48 kHz
21  *
22  * When driver works in the capture mode with multiple channels, it duplicates the looped
23  * pattern to each separate channel. For example, if we have 2 channels, format = U8, interleaved
24  * access mode and pattern 'abacaba', the DMA buffer will look like aabbccaabbaaaa..., so buffer for
25  * each channel will contain abacabaabacaba... Same for the non-interleaved mode.
26  *
27  * However, it may break the capturing on the higher framerates with small period size, so it is
28  * better to choose larger period sizes.
29  *
30  * You can find the corresponding selftest in the 'alsa' selftests folder.
31  */
32 
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <sound/pcm.h>
36 #include <sound/core.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/platform_device.h>
39 #include <linux/timer.h>
40 #include <linux/random.h>
41 #include <linux/debugfs.h>
42 #include <linux/delay.h>
43 
44 #define DEVNAME "pcmtestd"
45 #define CARD_NAME "pcm-test-card"
46 #define TIMER_PER_SEC 5
47 #define TIMER_INTERVAL (HZ / TIMER_PER_SEC)
48 #define DELAY_JIFFIES HZ
49 #define PLAYBACK_SUBSTREAM_CNT	8
50 #define CAPTURE_SUBSTREAM_CNT	8
51 #define MAX_CHANNELS_NUM	4
52 
53 #define DEFAULT_PATTERN		"abacaba"
54 #define DEFAULT_PATTERN_LEN	7
55 
56 #define FILL_MODE_RAND	0
57 #define FILL_MODE_PAT	1
58 
59 #define MAX_PATTERN_LEN 4096
60 
61 static int index = -1;
62 static char *id = "pcmtest";
63 static bool enable = true;
64 static int inject_delay;
65 static bool inject_hwpars_err;
66 static bool inject_prepare_err;
67 static bool inject_trigger_err;
68 static bool inject_open_err;
69 
70 static short fill_mode = FILL_MODE_PAT;
71 
72 static u8 playback_capture_test;
73 static u8 ioctl_reset_test;
74 static struct dentry *driver_debug_dir;
75 
76 module_param(index, int, 0444);
77 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard");
78 module_param(id, charp, 0444);
79 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard");
80 module_param(enable, bool, 0444);
81 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
82 module_param(fill_mode, short, 0600);
83 MODULE_PARM_DESC(fill_mode, "Buffer fill mode: rand(0) or pattern(1)");
84 module_param(inject_delay, int, 0600);
85 MODULE_PARM_DESC(inject_delay, "Inject delays during playback/capture (in jiffies)");
86 module_param(inject_hwpars_err, bool, 0600);
87 MODULE_PARM_DESC(inject_hwpars_err, "Inject EBUSY error in the 'hw_params' callback");
88 module_param(inject_prepare_err, bool, 0600);
89 MODULE_PARM_DESC(inject_prepare_err, "Inject EINVAL error in the 'prepare' callback");
90 module_param(inject_trigger_err, bool, 0600);
91 MODULE_PARM_DESC(inject_trigger_err, "Inject EINVAL error in the 'trigger' callback");
92 module_param(inject_open_err, bool, 0600);
93 MODULE_PARM_DESC(inject_open_err, "Inject EBUSY error in the 'open' callback");
94 
95 
96 struct pcmtst {
97 	struct snd_pcm *pcm;
98 	struct snd_card *card;
99 	struct platform_device *pdev;
100 };
101 
102 struct pcmtst_buf_iter {
103 	size_t buf_pos;				// position in the DMA buffer
104 	size_t period_pos;			// period-relative position
105 	size_t b_rw;				// Bytes to write on every timer tick
106 	size_t s_rw_ch;				// Samples to write to one channel on every tick
107 	unsigned int sample_bytes;		// sample_bits / 8
108 	bool is_buf_corrupted;			// playback test result indicator
109 	size_t period_bytes;			// bytes in a one period
110 	bool interleaved;			// Interleaved/Non-interleaved mode
111 	size_t total_bytes;			// Total bytes read/written
112 	size_t chan_block;			// Bytes in one channel buffer when non-interleaved
113 	struct snd_pcm_substream *substream;
114 	struct timer_list timer_instance;
115 };
116 
117 static struct snd_pcm_hardware snd_pcmtst_hw = {
118 	.info = (SNDRV_PCM_INFO_INTERLEAVED |
119 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
120 		 SNDRV_PCM_INFO_NONINTERLEAVED |
121 		 SNDRV_PCM_INFO_MMAP_VALID),
122 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
123 	.rates =		SNDRV_PCM_RATE_8000_48000,
124 	.rate_min =		8000,
125 	.rate_max =		48000,
126 	.channels_min =		1,
127 	.channels_max =		MAX_CHANNELS_NUM,
128 	.buffer_bytes_max =	128 * 1024,
129 	.period_bytes_min =	4096,
130 	.period_bytes_max =	32768,
131 	.periods_min =		1,
132 	.periods_max =		1024,
133 };
134 
135 struct pattern_buf {
136 	char *buf;
137 	u32 len;
138 };
139 
140 static int buf_allocated;
141 static struct pattern_buf patt_bufs[MAX_CHANNELS_NUM];
142 
143 static inline void inc_buf_pos(struct pcmtst_buf_iter *v_iter, size_t by, size_t bytes)
144 {
145 	v_iter->total_bytes += by;
146 	v_iter->buf_pos += by;
147 	if (v_iter->buf_pos >= bytes)
148 		v_iter->buf_pos %= bytes;
149 }
150 
151 /*
152  * Position in the DMA buffer when we are in the non-interleaved mode. We increment buf_pos
153  * every time we write a byte to any channel, so the position in the current channel buffer is
154  * (position in the DMA buffer) / count_of_channels + size_of_channel_buf * current_channel
155  */
156 static inline size_t buf_pos_n(struct pcmtst_buf_iter *v_iter, unsigned int channels,
157 			       unsigned int chan_num)
158 {
159 	return v_iter->buf_pos / channels + v_iter->chan_block * chan_num;
160 }
161 
162 /*
163  * Get the count of bytes written for the current channel in the interleaved mode.
164  * This is (count of samples written for the current channel) * bytes_in_sample +
165  * (relative position in the current sample)
166  */
167 static inline size_t ch_pos_i(size_t b_total, unsigned int channels, unsigned int b_sample)
168 {
169 	return b_total / channels / b_sample * b_sample + (b_total % b_sample);
170 }
171 
172 static void check_buf_block_i(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
173 {
174 	size_t i;
175 	short ch_num;
176 	u8 current_byte;
177 
178 	for (i = 0; i < v_iter->b_rw; i++) {
179 		current_byte = runtime->dma_area[v_iter->buf_pos];
180 		if (!current_byte)
181 			break;
182 		ch_num = (v_iter->total_bytes / v_iter->sample_bytes) % runtime->channels;
183 		if (current_byte != patt_bufs[ch_num].buf[ch_pos_i(v_iter->total_bytes,
184 								   runtime->channels,
185 								   v_iter->sample_bytes)
186 							  % patt_bufs[ch_num].len]) {
187 			v_iter->is_buf_corrupted = true;
188 			break;
189 		}
190 		inc_buf_pos(v_iter, 1, runtime->dma_bytes);
191 	}
192 	// If we broke during the loop, add remaining bytes to the buffer position.
193 	inc_buf_pos(v_iter, v_iter->b_rw - i, runtime->dma_bytes);
194 }
195 
196 static void check_buf_block_ni(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
197 {
198 	unsigned int channels = runtime->channels;
199 	size_t i;
200 	short ch_num;
201 	u8 current_byte;
202 
203 	for (i = 0; i < v_iter->b_rw; i++) {
204 		ch_num = i % channels;
205 		current_byte = runtime->dma_area[buf_pos_n(v_iter, channels, ch_num)];
206 		if (!current_byte)
207 			break;
208 		if (current_byte != patt_bufs[ch_num].buf[(v_iter->total_bytes / channels)
209 							  % patt_bufs[ch_num].len]) {
210 			v_iter->is_buf_corrupted = true;
211 			break;
212 		}
213 		inc_buf_pos(v_iter, 1, runtime->dma_bytes);
214 	}
215 	inc_buf_pos(v_iter, v_iter->b_rw - i, runtime->dma_bytes);
216 }
217 
218 /*
219  * Check one block of the buffer. Here we iterate the buffer until we find '0'. This condition is
220  * necessary because we need to detect when the reading/writing ends, so we assume that the pattern
221  * doesn't contain zeros.
222  */
223 static void check_buf_block(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
224 {
225 	if (v_iter->interleaved)
226 		check_buf_block_i(v_iter, runtime);
227 	else
228 		check_buf_block_ni(v_iter, runtime);
229 }
230 
231 /*
232  * Fill buffer in the non-interleaved mode. The order of samples is C0, ..., C0, C1, ..., C1, C2...
233  * The channel buffers lay in the DMA buffer continuously (see default copy_user and copy_kernel
234  * handlers in the pcm_lib.c file).
235  *
236  * Here we increment the DMA buffer position every time we write a byte to any channel 'buffer'.
237  * We need this to simulate the correct hardware pointer moving.
238  */
239 static void fill_block_pattern_n(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
240 {
241 	size_t i;
242 	unsigned int channels = runtime->channels;
243 	short ch_num;
244 
245 	for (i = 0; i < v_iter->b_rw; i++) {
246 		ch_num = i % channels;
247 		runtime->dma_area[buf_pos_n(v_iter, channels, ch_num)] =
248 			patt_bufs[ch_num].buf[(v_iter->total_bytes / channels)
249 					      % patt_bufs[ch_num].len];
250 		inc_buf_pos(v_iter, 1, runtime->dma_bytes);
251 	}
252 }
253 
254 // Fill buffer in the interleaved mode. The order of samples is C0, C1, C2, C0, C1, C2, ...
255 static void fill_block_pattern_i(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
256 {
257 	size_t sample;
258 	size_t pos_in_ch, pos_pattern;
259 	short ch, pos_sample;
260 
261 	pos_in_ch = ch_pos_i(v_iter->total_bytes, runtime->channels, v_iter->sample_bytes);
262 
263 	for (sample = 0; sample < v_iter->s_rw_ch; sample++) {
264 		for (ch = 0; ch < runtime->channels; ch++) {
265 			for (pos_sample = 0; pos_sample < v_iter->sample_bytes; pos_sample++) {
266 				pos_pattern = (pos_in_ch + sample * v_iter->sample_bytes
267 					      + pos_sample) % patt_bufs[ch].len;
268 				runtime->dma_area[v_iter->buf_pos] = patt_bufs[ch].buf[pos_pattern];
269 				inc_buf_pos(v_iter, 1, runtime->dma_bytes);
270 			}
271 		}
272 	}
273 }
274 
275 static void fill_block_pattern(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
276 {
277 	if (v_iter->interleaved)
278 		fill_block_pattern_i(v_iter, runtime);
279 	else
280 		fill_block_pattern_n(v_iter, runtime);
281 }
282 
283 static void fill_block_rand_n(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
284 {
285 	unsigned int channels = runtime->channels;
286 	// Remaining space in all channel buffers
287 	size_t bytes_remain = runtime->dma_bytes - v_iter->buf_pos;
288 	unsigned int i;
289 
290 	for (i = 0; i < channels; i++) {
291 		if (v_iter->b_rw <= bytes_remain) {
292 			//b_rw - count of bytes must be written for all channels at each timer tick
293 			get_random_bytes(runtime->dma_area + buf_pos_n(v_iter, channels, i),
294 					 v_iter->b_rw / channels);
295 		} else {
296 			// Write to the end of buffer and start from the beginning of it
297 			get_random_bytes(runtime->dma_area + buf_pos_n(v_iter, channels, i),
298 					 bytes_remain / channels);
299 			get_random_bytes(runtime->dma_area + v_iter->chan_block * i,
300 					 (v_iter->b_rw - bytes_remain) / channels);
301 		}
302 	}
303 	inc_buf_pos(v_iter, v_iter->b_rw, runtime->dma_bytes);
304 }
305 
306 static void fill_block_rand_i(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
307 {
308 	size_t in_cur_block = runtime->dma_bytes - v_iter->buf_pos;
309 
310 	if (v_iter->b_rw <= in_cur_block) {
311 		get_random_bytes(&runtime->dma_area[v_iter->buf_pos], v_iter->b_rw);
312 	} else {
313 		get_random_bytes(&runtime->dma_area[v_iter->buf_pos], in_cur_block);
314 		get_random_bytes(runtime->dma_area, v_iter->b_rw - in_cur_block);
315 	}
316 	inc_buf_pos(v_iter, v_iter->b_rw, runtime->dma_bytes);
317 }
318 
319 static void fill_block_random(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
320 {
321 	if (v_iter->interleaved)
322 		fill_block_rand_i(v_iter, runtime);
323 	else
324 		fill_block_rand_n(v_iter, runtime);
325 }
326 
327 static void fill_block(struct pcmtst_buf_iter *v_iter, struct snd_pcm_runtime *runtime)
328 {
329 	switch (fill_mode) {
330 	case FILL_MODE_RAND:
331 		fill_block_random(v_iter, runtime);
332 		break;
333 	case FILL_MODE_PAT:
334 		fill_block_pattern(v_iter, runtime);
335 		break;
336 	}
337 }
338 
339 /*
340  * Here we iterate through the buffer by (buffer_size / iterates_per_second) bytes.
341  * The driver uses timer to simulate the hardware pointer moving, and notify the PCM middle layer
342  * about period elapsed.
343  */
344 static void timer_timeout(struct timer_list *data)
345 {
346 	struct pcmtst_buf_iter *v_iter;
347 	struct snd_pcm_substream *substream;
348 
349 	v_iter = from_timer(v_iter, data, timer_instance);
350 	substream = v_iter->substream;
351 
352 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !v_iter->is_buf_corrupted)
353 		check_buf_block(v_iter, substream->runtime);
354 	else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
355 		fill_block(v_iter, substream->runtime);
356 	else
357 		inc_buf_pos(v_iter, v_iter->b_rw, substream->runtime->dma_bytes);
358 
359 	v_iter->period_pos += v_iter->b_rw;
360 	if (v_iter->period_pos >= v_iter->period_bytes) {
361 		v_iter->period_pos %= v_iter->period_bytes;
362 		snd_pcm_period_elapsed(substream);
363 	}
364 	mod_timer(&v_iter->timer_instance, jiffies + TIMER_INTERVAL + inject_delay);
365 }
366 
367 static int snd_pcmtst_pcm_open(struct snd_pcm_substream *substream)
368 {
369 	struct snd_pcm_runtime *runtime = substream->runtime;
370 	struct pcmtst_buf_iter *v_iter;
371 
372 	if (inject_open_err)
373 		return -EBUSY;
374 
375 	v_iter = kzalloc(sizeof(*v_iter), GFP_KERNEL);
376 	if (!v_iter)
377 		return -ENOMEM;
378 
379 	runtime->hw = snd_pcmtst_hw;
380 	runtime->private_data = v_iter;
381 	v_iter->substream = substream;
382 	v_iter->buf_pos = 0;
383 	v_iter->is_buf_corrupted = false;
384 	v_iter->period_pos = 0;
385 	v_iter->total_bytes = 0;
386 
387 	playback_capture_test = 0;
388 	ioctl_reset_test = 0;
389 
390 	timer_setup(&v_iter->timer_instance, timer_timeout, 0);
391 	mod_timer(&v_iter->timer_instance, jiffies + TIMER_INTERVAL);
392 	return 0;
393 }
394 
395 static int snd_pcmtst_pcm_close(struct snd_pcm_substream *substream)
396 {
397 	struct pcmtst_buf_iter *v_iter = substream->runtime->private_data;
398 
399 	timer_shutdown_sync(&v_iter->timer_instance);
400 	v_iter->substream = NULL;
401 	playback_capture_test = !v_iter->is_buf_corrupted;
402 	kfree(v_iter);
403 	return 0;
404 }
405 
406 static int snd_pcmtst_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
407 {
408 	struct snd_pcm_runtime *runtime = substream->runtime;
409 	struct pcmtst_buf_iter *v_iter = runtime->private_data;
410 
411 	if (inject_trigger_err)
412 		return -EINVAL;
413 
414 	v_iter->sample_bytes = runtime->sample_bits / 8;
415 	v_iter->period_bytes = frames_to_bytes(runtime, runtime->period_size);
416 	if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED ||
417 	    runtime->access == SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED) {
418 		v_iter->chan_block = runtime->dma_bytes / runtime->channels;
419 		v_iter->interleaved = false;
420 	} else {
421 		v_iter->interleaved = true;
422 	}
423 	// We want to record RATE * ch_cnt samples per sec, it is rate * sample_bytes * ch_cnt bytes
424 	v_iter->s_rw_ch = runtime->rate / TIMER_PER_SEC;
425 	v_iter->b_rw = v_iter->s_rw_ch * v_iter->sample_bytes * runtime->channels;
426 
427 	return 0;
428 }
429 
430 static snd_pcm_uframes_t snd_pcmtst_pcm_pointer(struct snd_pcm_substream *substream)
431 {
432 	struct pcmtst_buf_iter *v_iter = substream->runtime->private_data;
433 
434 	return bytes_to_frames(substream->runtime, v_iter->buf_pos);
435 }
436 
437 static int snd_pcmtst_free(struct pcmtst *pcmtst)
438 {
439 	if (!pcmtst)
440 		return 0;
441 	kfree(pcmtst);
442 	return 0;
443 }
444 
445 // These callbacks are required, but empty - all freeing occurs in pdev_remove
446 static int snd_pcmtst_dev_free(struct snd_device *device)
447 {
448 	return 0;
449 }
450 
451 static void pcmtst_pdev_release(struct device *dev)
452 {
453 }
454 
455 static int snd_pcmtst_pcm_prepare(struct snd_pcm_substream *substream)
456 {
457 	if (inject_prepare_err)
458 		return -EINVAL;
459 	return 0;
460 }
461 
462 static int snd_pcmtst_pcm_hw_params(struct snd_pcm_substream *substream,
463 				    struct snd_pcm_hw_params *params)
464 {
465 	if (inject_hwpars_err)
466 		return -EBUSY;
467 	return 0;
468 }
469 
470 static int snd_pcmtst_pcm_hw_free(struct snd_pcm_substream *substream)
471 {
472 	return 0;
473 }
474 
475 static int snd_pcmtst_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg)
476 {
477 	switch (cmd) {
478 	case SNDRV_PCM_IOCTL1_RESET:
479 		ioctl_reset_test = 1;
480 		break;
481 	}
482 	return snd_pcm_lib_ioctl(substream, cmd, arg);
483 }
484 
485 static const struct snd_pcm_ops snd_pcmtst_playback_ops = {
486 	.open =		snd_pcmtst_pcm_open,
487 	.close =	snd_pcmtst_pcm_close,
488 	.trigger =	snd_pcmtst_pcm_trigger,
489 	.hw_params =	snd_pcmtst_pcm_hw_params,
490 	.ioctl =	snd_pcmtst_ioctl,
491 	.hw_free =	snd_pcmtst_pcm_hw_free,
492 	.prepare =	snd_pcmtst_pcm_prepare,
493 	.pointer =	snd_pcmtst_pcm_pointer,
494 };
495 
496 static const struct snd_pcm_ops snd_pcmtst_capture_ops = {
497 	.open =		snd_pcmtst_pcm_open,
498 	.close =	snd_pcmtst_pcm_close,
499 	.trigger =	snd_pcmtst_pcm_trigger,
500 	.hw_params =	snd_pcmtst_pcm_hw_params,
501 	.hw_free =	snd_pcmtst_pcm_hw_free,
502 	.ioctl =	snd_pcmtst_ioctl,
503 	.prepare =	snd_pcmtst_pcm_prepare,
504 	.pointer =	snd_pcmtst_pcm_pointer,
505 };
506 
507 static int snd_pcmtst_new_pcm(struct pcmtst *pcmtst)
508 {
509 	struct snd_pcm *pcm;
510 	int err;
511 
512 	err = snd_pcm_new(pcmtst->card, "PCMTest", 0, PLAYBACK_SUBSTREAM_CNT,
513 			  CAPTURE_SUBSTREAM_CNT, &pcm);
514 	if (err < 0)
515 		return err;
516 	pcm->private_data = pcmtst;
517 	strcpy(pcm->name, "PCMTest");
518 	pcmtst->pcm = pcm;
519 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pcmtst_playback_ops);
520 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pcmtst_capture_ops);
521 
522 	err = snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &pcmtst->pdev->dev,
523 					     0, 128 * 1024);
524 	return err;
525 }
526 
527 static int snd_pcmtst_create(struct snd_card *card, struct platform_device *pdev,
528 			     struct pcmtst **r_pcmtst)
529 {
530 	struct pcmtst *pcmtst;
531 	int err;
532 	static const struct snd_device_ops ops = {
533 		.dev_free = snd_pcmtst_dev_free,
534 	};
535 
536 	pcmtst = kzalloc(sizeof(*pcmtst), GFP_KERNEL);
537 	if (!pcmtst)
538 		return -ENOMEM;
539 	pcmtst->card = card;
540 	pcmtst->pdev = pdev;
541 
542 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, pcmtst, &ops);
543 	if (err < 0)
544 		goto _err_free_chip;
545 
546 	err = snd_pcmtst_new_pcm(pcmtst);
547 	if (err < 0)
548 		goto _err_free_chip;
549 
550 	*r_pcmtst = pcmtst;
551 	return 0;
552 
553 _err_free_chip:
554 	snd_pcmtst_free(pcmtst);
555 	return err;
556 }
557 
558 static int pcmtst_probe(struct platform_device *pdev)
559 {
560 	struct snd_card *card;
561 	struct pcmtst *pcmtst;
562 	int err;
563 
564 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
565 	if (err)
566 		return err;
567 
568 	err = snd_devm_card_new(&pdev->dev, index, id, THIS_MODULE, 0, &card);
569 	if (err < 0)
570 		return err;
571 	err = snd_pcmtst_create(card, pdev, &pcmtst);
572 	if (err < 0)
573 		return err;
574 
575 	strcpy(card->driver, "PCM-TEST Driver");
576 	strcpy(card->shortname, "PCM-Test");
577 	strcpy(card->longname, "PCM-Test virtual driver");
578 
579 	err = snd_card_register(card);
580 	if (err < 0)
581 		return err;
582 
583 	platform_set_drvdata(pdev, pcmtst);
584 
585 	return 0;
586 }
587 
588 static void pdev_remove(struct platform_device *pdev)
589 {
590 	struct pcmtst *pcmtst = platform_get_drvdata(pdev);
591 
592 	snd_pcmtst_free(pcmtst);
593 }
594 
595 static struct platform_device pcmtst_pdev = {
596 	.name =		"pcmtest",
597 	.dev.release =	pcmtst_pdev_release,
598 };
599 
600 static struct platform_driver pcmtst_pdrv = {
601 	.probe =	pcmtst_probe,
602 	.remove_new =	pdev_remove,
603 	.driver =	{
604 		.name = "pcmtest",
605 	},
606 };
607 
608 static ssize_t pattern_write(struct file *file, const char __user *u_buff, size_t len, loff_t *off)
609 {
610 	struct pattern_buf *patt_buf = file->f_inode->i_private;
611 	ssize_t to_write = len;
612 
613 	if (*off + to_write > MAX_PATTERN_LEN)
614 		to_write = MAX_PATTERN_LEN - *off;
615 
616 	// Crop silently everything over the buffer
617 	if (to_write <= 0)
618 		return len;
619 
620 	if (copy_from_user(patt_buf->buf + *off, u_buff, to_write))
621 		return -EFAULT;
622 
623 	patt_buf->len = *off + to_write;
624 	*off += to_write;
625 
626 	return to_write;
627 }
628 
629 static ssize_t pattern_read(struct file *file, char __user *u_buff, size_t len, loff_t *off)
630 {
631 	struct pattern_buf *patt_buf = file->f_inode->i_private;
632 	ssize_t to_read = len;
633 
634 	if (*off + to_read >= MAX_PATTERN_LEN)
635 		to_read = MAX_PATTERN_LEN - *off;
636 	if (to_read <= 0)
637 		return 0;
638 
639 	if (copy_to_user(u_buff, patt_buf->buf + *off, to_read))
640 		to_read = 0;
641 	else
642 		*off += to_read;
643 
644 	return to_read;
645 }
646 
647 static const struct file_operations fill_pattern_fops = {
648 	.read = pattern_read,
649 	.write = pattern_write,
650 };
651 
652 static int setup_patt_bufs(void)
653 {
654 	size_t i;
655 
656 	for (i = 0; i < ARRAY_SIZE(patt_bufs); i++) {
657 		patt_bufs[i].buf = kzalloc(MAX_PATTERN_LEN, GFP_KERNEL);
658 		if (!patt_bufs[i].buf)
659 			break;
660 		strcpy(patt_bufs[i].buf, DEFAULT_PATTERN);
661 		patt_bufs[i].len = DEFAULT_PATTERN_LEN;
662 	}
663 
664 	return i;
665 }
666 
667 static const char * const pattern_files[] = { "fill_pattern0", "fill_pattern1",
668 					      "fill_pattern2", "fill_pattern3"};
669 static int init_debug_files(int buf_count)
670 {
671 	size_t i;
672 	char len_file_name[32];
673 
674 	driver_debug_dir = debugfs_create_dir("pcmtest", NULL);
675 	if (IS_ERR(driver_debug_dir))
676 		return PTR_ERR(driver_debug_dir);
677 	debugfs_create_u8("pc_test", 0444, driver_debug_dir, &playback_capture_test);
678 	debugfs_create_u8("ioctl_test", 0444, driver_debug_dir, &ioctl_reset_test);
679 
680 	for (i = 0; i < buf_count; i++) {
681 		debugfs_create_file(pattern_files[i], 0600, driver_debug_dir,
682 				    &patt_bufs[i], &fill_pattern_fops);
683 		snprintf(len_file_name, sizeof(len_file_name), "%s_len", pattern_files[i]);
684 		debugfs_create_u32(len_file_name, 0444, driver_debug_dir, &patt_bufs[i].len);
685 	}
686 
687 	return 0;
688 }
689 
690 static void free_pattern_buffers(void)
691 {
692 	int i;
693 
694 	for (i = 0; i < buf_allocated; i++)
695 		kfree(patt_bufs[i].buf);
696 }
697 
698 static void clear_debug_files(void)
699 {
700 	debugfs_remove_recursive(driver_debug_dir);
701 }
702 
703 static int __init mod_init(void)
704 {
705 	int err = 0;
706 
707 	buf_allocated = setup_patt_bufs();
708 	if (!buf_allocated)
709 		return -ENOMEM;
710 
711 	snd_pcmtst_hw.channels_max = buf_allocated;
712 
713 	err = init_debug_files(buf_allocated);
714 	if (err)
715 		return err;
716 	err = platform_device_register(&pcmtst_pdev);
717 	if (err)
718 		return err;
719 	err = platform_driver_register(&pcmtst_pdrv);
720 	if (err)
721 		platform_device_unregister(&pcmtst_pdev);
722 	return err;
723 }
724 
725 static void __exit mod_exit(void)
726 {
727 	clear_debug_files();
728 	free_pattern_buffers();
729 
730 	platform_driver_unregister(&pcmtst_pdrv);
731 	platform_device_unregister(&pcmtst_pdev);
732 }
733 
734 MODULE_LICENSE("GPL");
735 MODULE_AUTHOR("Ivan Orlov");
736 module_init(mod_init);
737 module_exit(mod_exit);
738