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