pcmtest.c 22 KB

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