1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <string.h>
9 #include <zephyr/drivers/i2s.h>
10
z_impl_i2s_buf_read(const struct device * dev,void * buf,size_t * size)11 int z_impl_i2s_buf_read(const struct device *dev, void *buf, size_t *size)
12 {
13 void *mem_block;
14 int ret;
15
16 ret = i2s_read((const struct device *)dev, &mem_block, size);
17
18 if (!ret) {
19 const struct i2s_config *rx_cfg;
20
21 rx_cfg = i2s_config_get((const struct device *)dev, I2S_DIR_RX);
22
23 memcpy(buf, mem_block, *size);
24 k_mem_slab_free(rx_cfg->mem_slab, mem_block);
25 }
26
27 return ret;
28 }
29
z_impl_i2s_buf_write(const struct device * dev,void * buf,size_t size)30 int z_impl_i2s_buf_write(const struct device *dev, void *buf, size_t size)
31 {
32 int ret;
33 const struct i2s_config *tx_cfg;
34 void *mem_block;
35
36 tx_cfg = i2s_config_get((const struct device *)dev, I2S_DIR_TX);
37 if (!tx_cfg) {
38 return -EIO;
39 }
40
41 if (size > tx_cfg->block_size) {
42 return -EINVAL;
43 }
44
45 ret = k_mem_slab_alloc(tx_cfg->mem_slab, &mem_block, K_FOREVER);
46 if (ret < 0) {
47 return -ENOMEM;
48 }
49
50 memcpy(mem_block, (void *)buf, size);
51
52 ret = i2s_write((const struct device *)dev, mem_block, size);
53 if (ret != 0) {
54 k_mem_slab_free(tx_cfg->mem_slab, mem_block);
55 }
56
57 return ret;
58 }
59