1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2019 Intel Corporation. All rights reserved. 7 // 8 // Authors: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com> 9 10 /* Generic SOF IPC code */ 11 12 #include <linux/device.h> 13 #include <linux/export.h> 14 #include <linux/module.h> 15 #include <linux/types.h> 16 17 #include <sound/pcm.h> 18 #include <sound/sof/stream.h> 19 20 #include "ops.h" 21 #include "sof-priv.h" 22 #include "sof-audio.h" 23 24 struct sof_stream { 25 size_t posn_offset; 26 }; 27 28 /* Mailbox-based Generic IPC implementation */ 29 int sof_ipc_msg_data(struct snd_sof_dev *sdev, 30 struct snd_sof_pcm_stream *sps, 31 void *p, size_t sz) 32 { 33 if (!sps || !sdev->stream_box.size) { 34 snd_sof_dsp_mailbox_read(sdev, sdev->dsp_box.offset, p, sz); 35 } else { 36 struct snd_pcm_substream *substream = sps->substream; 37 struct sof_stream *stream = substream->runtime->private_data; 38 39 /* The stream might already be closed */ 40 if (!stream) 41 return -ESTRPIPE; 42 43 snd_sof_dsp_mailbox_read(sdev, stream->posn_offset, p, sz); 44 } 45 46 return 0; 47 } 48 EXPORT_SYMBOL(sof_ipc_msg_data); 49 50 int sof_set_stream_data_offset(struct snd_sof_dev *sdev, 51 struct snd_pcm_substream *substream, 52 size_t posn_offset) 53 { 54 struct sof_stream *stream = substream->runtime->private_data; 55 56 /* check if offset is overflow or it is not aligned */ 57 if (posn_offset > sdev->stream_box.size || 58 posn_offset % sizeof(struct sof_ipc_stream_posn) != 0) 59 return -EINVAL; 60 61 stream->posn_offset = sdev->stream_box.offset + posn_offset; 62 63 dev_dbg(sdev->dev, "pcm: stream dir %d, posn mailbox offset is %zu", 64 substream->stream, stream->posn_offset); 65 66 return 0; 67 } 68 EXPORT_SYMBOL(sof_set_stream_data_offset); 69 70 int sof_stream_pcm_open(struct snd_sof_dev *sdev, 71 struct snd_pcm_substream *substream) 72 { 73 struct sof_stream *stream = kmalloc(sizeof(*stream), GFP_KERNEL); 74 75 if (!stream) 76 return -ENOMEM; 77 78 /* binding pcm substream to hda stream */ 79 substream->runtime->private_data = stream; 80 81 /* align to DMA minimum transfer size */ 82 snd_pcm_hw_constraint_step(substream->runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4); 83 84 /* avoid circular buffer wrap in middle of period */ 85 snd_pcm_hw_constraint_integer(substream->runtime, 86 SNDRV_PCM_HW_PARAM_PERIODS); 87 88 return 0; 89 } 90 EXPORT_SYMBOL(sof_stream_pcm_open); 91 92 int sof_stream_pcm_close(struct snd_sof_dev *sdev, 93 struct snd_pcm_substream *substream) 94 { 95 struct sof_stream *stream = substream->runtime->private_data; 96 97 substream->runtime->private_data = NULL; 98 kfree(stream); 99 100 return 0; 101 } 102 EXPORT_SYMBOL(sof_stream_pcm_close); 103 104 MODULE_LICENSE("Dual BSD/GPL"); 105