// SPDX-License-Identifier: GPL-2.0 /* * awe_test - userspace PCM hammer for SOC-MR-05/07 verification. * * modes: * cycle [sleep_ms] : n times { open pcmC*D1p -> hw_params -> * prepare -> START -> sleep -> close } * MR-07: close() path snd_pcm_drop -> trigger * STOP -> stop_voice()'s non-sync * timer_delete() races the in-flight softirq * emu8k_pcm_timer_func() while * emu8k_pcm_close() kfrees rec. * hold : one open->params->prepare->START, keep * HWSYNC/DELAY queries in a loop (fd stays * alive across modprobe -r snd-emu8000-synth) * then close - MR-05: snd_emu8000_remove -> * snd_device_free(hw->pcm) races the in-flight * fd operations and the final release. * idle : open then sleep (fd held, no PCM activity). * still : 200x open/close, never hw_params/START * (timer never armed) - MR-07 negative control. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include /* system uapi header, verified identical to linux-7.3 */ static const char *dev_pcm = "/dev/snd/pcmC1D1p"; static const char *dev; static int msleep(int ms) { struct timespec ts = { .tv_sec = ms / 1000, .tv_nsec = (long)(ms % 1000) * 1000000L }; return nanosleep(&ts, NULL); } static int do_open(void) { int fd = open(dev, O_WRONLY); if (fd < 0) { fprintf(stderr, "open %s failed: %s\n", dev, strerror(errno)); return -1; } return fd; } /* fill "any" masks then fix rate/channels/buffer/period */ static int setup_params(int fd) { struct snd_pcm_hw_params *p; unsigned int i, rate = 8000, ch = 1, psize = 1024; char *e; if ((e = getenv("PCM_RATE"))) rate = (unsigned)atoi(e); if ((e = getenv("PCM_CHANS"))) ch = (unsigned)atoi(e); if ((e = getenv("PCM_PSIZE"))) psize = (unsigned)atoi(e); p = calloc(1, sizeof(*p)); if (!p) return -1; /* masks: ACCESS=RW_INTERLEAVED, FORMAT=S16_LE, SUBFORMAT=STD */ for (i = 0; i < SNDRV_PCM_HW_PARAM_LAST_MASK - SNDRV_PCM_HW_PARAM_FIRST_MASK + 1; i++) memset(&p->masks[i].bits[0], 0xff, sizeof(p->masks[i].bits)); p->masks[SNDRV_PCM_HW_PARAM_ACCESS].bits[0] &= 1U << SNDRV_PCM_ACCESS_RW_INTERLEAVED; p->masks[SNDRV_PCM_HW_PARAM_FORMAT].bits[0] &= 1U << SNDRV_PCM_FORMAT_S16_LE; p->masks[SNDRV_PCM_HW_PARAM_SUBFORMAT].bits[0] &= 1U << SNDRV_PCM_SUBFORMAT_STD; /* intervals: allow wide ranges, REFINE will pick */ for (i = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; i <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; i++) { p->intervals[i - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].min = 0; p->intervals[i - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].max = ~0U; } p->rmask = ~0U; p->cmask = 0; p->info = ~0U; if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, p) < 0) { perror("HW_REFINE"); free(p); return -1; } /* nail rate/channels/period for a stable config */ p->intervals[SNDRV_PCM_HW_PARAM_RATE - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].min = rate; p->intervals[SNDRV_PCM_HW_PARAM_RATE - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].max = rate; p->intervals[SNDRV_PCM_HW_PARAM_CHANNELS - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].min = ch; p->intervals[SNDRV_PCM_HW_PARAM_CHANNELS - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].max = ch; p->intervals[SNDRV_PCM_HW_PARAM_PERIOD_SIZE - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].min = psize; p->intervals[SNDRV_PCM_HW_PARAM_PERIOD_SIZE - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].max = psize; /* emu8k hw_params allocates a memhdr block of buffer_bytes+32 out of * the 512K fake DRAM; an unbounded REFINE picks the 512K-24 maximum * and overflows it -> -ENOMEM. Keep the buffer small. */ p->intervals[SNDRV_PCM_HW_PARAM_BUFFER_SIZE - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].min = 4096; p->intervals[SNDRV_PCM_HW_PARAM_BUFFER_SIZE - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].max = 16384; p->rmask = (1U << (SNDRV_PCM_HW_PARAM_RATE - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL)) | (1U << (SNDRV_PCM_HW_PARAM_CHANNELS - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL)) | (1U << (SNDRV_PCM_HW_PARAM_PERIOD_SIZE - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL)) | (1U << (SNDRV_PCM_HW_PARAM_BUFFER_SIZE - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL)); if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, p) < 0) { perror("HW_REFINE(fixed)"); free(p); return -1; } if (ioctl(fd, SNDRV_PCM_IOCTL_HW_PARAMS, p) < 0) { perror("HW_PARAMS"); free(p); return -1; } free(p); /* stop_threshold >= boundary makes snd_pcm_playback_data() always * report data present, so SNDRV_PCM_IOCTL_START succeeds on an * empty buffer (start_voice() arms the rec->timer - the race * counterpart we need alive). */ { struct snd_pcm_sw_params *s = calloc(1, sizeof(*s)); if (s) { s->avail_min = 1024; s->start_threshold = 1024; s->stop_threshold = 0x7fffffffU; if (ioctl(fd, SNDRV_PCM_IOCTL_SW_PARAMS, s) < 0) perror("SW_PARAMS"); free(s); } } if (ioctl(fd, SNDRV_PCM_IOCTL_PREPARE) < 0) { perror("PREPARE"); return -1; } if (ioctl(fd, SNDRV_PCM_IOCTL_START) < 0) { perror("START"); return -1; } return 0; } static int cycle(int n, int sleep_ms) { int i, fd; for (i = 0; i < n; i++) { fd = do_open(); if (fd < 0) return 1; if (setup_params(fd) == 0) msleep(sleep_ms); /* RUNNING: timer re-armed * every jiffie */ close(fd); /* drop -> trigger STOP -> timer_delete * (non-sync) -> hw_free -> close kfree(rec) */ if ((i % 25) == 0) printf("cycle %d/%d done\n", i + 1, n); } printf("cycle: %d rounds done\n", n); return 0; } static int hold(int seconds) { int fd, k; snd_pcm_sframes_t delay = 0; fd = do_open(); if (fd < 0) return 1; if (setup_params(fd)) return 1; printf("hold: RUNNING on fd=%d, querying for %ds (keep fd alive across rmmod)\n", fd, seconds); for (k = 0; k < seconds * 100; k++) { if (ioctl(fd, SNDRV_PCM_IOCTL_HWSYNC) < 0) break; ioctl(fd, SNDRV_PCM_IOCTL_DELAY, &delay); msleep(10); } printf("hold: closing fd after %d queries (release vs device free)\n", k); close(fd); printf("hold: done\n"); return 0; } static int idle(int seconds) { int fd = do_open(); if (fd < 0) return 1; printf("idle: fd=%d sleeping %ds\n", fd, seconds); sleep(seconds); close(fd); printf("idle: done\n"); return 0; } static int still(void) { int i, fd; for (i = 0; i < 200; i++) { fd = do_open(); if (fd < 0) return 1; close(fd); /* state stays OPEN -> drop returns * -EBADFD, no trigger, timer never armed */ } printf("still: 200 open/close done (timer never armed)\n"); return 0; } int main(int argc, char **argv) { dev = getenv("PCMDEV"); if (!dev) dev = dev_pcm; if (argc >= 3 && !strcmp(argv[1], "cycle")) return cycle(atoi(argv[2]), argc >= 4 ? atoi(argv[3]) : 150); if (argc >= 3 && !strcmp(argv[1], "arm")) { int fd, s = atoi(argv[2]); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); printf("arm: stepping: open\n"); fd = do_open(); if (fd < 0) return 1; printf("arm: stepping: hw_params\n"); if (setup_params(fd)) return 1; printf("arm: RUNNING fd=%d, will close after %ds\n", fd, s); sleep(s); close(fd); printf("arm: closed\n"); return 0; } if (argc >= 3 && !strcmp(argv[1], "hold")) return hold(atoi(argv[2])); if (argc >= 3 && !strcmp(argv[1], "idle")) return idle(atoi(argv[2])); if (argc >= 2 && !strcmp(argv[1], "still")) return still(); fprintf(stderr, "usage: %s cycle [sleep_ms] | hold | idle | still\n", argv[0]); return 2; }