xref: /linux/sound/soc/sof/compress.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1858f7a5cSDaniel Baluta // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2858f7a5cSDaniel Baluta //
3858f7a5cSDaniel Baluta // Copyright 2021 NXP
4858f7a5cSDaniel Baluta //
5858f7a5cSDaniel Baluta // Author: Daniel Baluta <daniel.baluta@nxp.com>
6858f7a5cSDaniel Baluta 
7858f7a5cSDaniel Baluta #include <sound/soc.h>
8858f7a5cSDaniel Baluta #include <sound/sof.h>
9858f7a5cSDaniel Baluta #include <sound/compress_driver.h>
10858f7a5cSDaniel Baluta #include "sof-audio.h"
11858f7a5cSDaniel Baluta #include "sof-priv.h"
12ee844305SPeter Ujfalusi #include "sof-utils.h"
13a9737808SDaniel Baluta #include "ops.h"
14858f7a5cSDaniel Baluta 
sof_set_transferred_bytes(struct sof_compr_stream * sstream,u64 host_pos,u64 buffer_size)15e3091f0aSDaniel Baluta static void sof_set_transferred_bytes(struct sof_compr_stream *sstream,
166324cf90SDaniel Baluta 				      u64 host_pos, u64 buffer_size)
176324cf90SDaniel Baluta {
186324cf90SDaniel Baluta 	u64 prev_pos;
196324cf90SDaniel Baluta 	unsigned int copied;
206324cf90SDaniel Baluta 
21e3091f0aSDaniel Baluta 	div64_u64_rem(sstream->copied_total, buffer_size, &prev_pos);
226324cf90SDaniel Baluta 
236324cf90SDaniel Baluta 	if (host_pos < prev_pos)
246324cf90SDaniel Baluta 		copied = (buffer_size - prev_pos) + host_pos;
256324cf90SDaniel Baluta 	else
266324cf90SDaniel Baluta 		copied = host_pos - prev_pos;
276324cf90SDaniel Baluta 
28e3091f0aSDaniel Baluta 	sstream->copied_total += copied;
296324cf90SDaniel Baluta }
306324cf90SDaniel Baluta 
snd_sof_compr_fragment_elapsed_work(struct work_struct * work)31858f7a5cSDaniel Baluta static void snd_sof_compr_fragment_elapsed_work(struct work_struct *work)
32858f7a5cSDaniel Baluta {
33858f7a5cSDaniel Baluta 	struct snd_sof_pcm_stream *sps =
34858f7a5cSDaniel Baluta 		container_of(work, struct snd_sof_pcm_stream,
35858f7a5cSDaniel Baluta 			     period_elapsed_work);
36858f7a5cSDaniel Baluta 
37858f7a5cSDaniel Baluta 	snd_compr_fragment_elapsed(sps->cstream);
38858f7a5cSDaniel Baluta }
39858f7a5cSDaniel Baluta 
snd_sof_compr_init_elapsed_work(struct work_struct * work)40858f7a5cSDaniel Baluta void snd_sof_compr_init_elapsed_work(struct work_struct *work)
41858f7a5cSDaniel Baluta {
42858f7a5cSDaniel Baluta 	INIT_WORK(work, snd_sof_compr_fragment_elapsed_work);
43858f7a5cSDaniel Baluta }
44858f7a5cSDaniel Baluta 
45858f7a5cSDaniel Baluta /*
46858f7a5cSDaniel Baluta  * sof compr fragment elapse, this could be called in irq thread context
47858f7a5cSDaniel Baluta  */
snd_sof_compr_fragment_elapsed(struct snd_compr_stream * cstream)48858f7a5cSDaniel Baluta void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream)
49858f7a5cSDaniel Baluta {
507e4bfcf1SWeiguo Li 	struct snd_soc_pcm_runtime *rtd;
517e4bfcf1SWeiguo Li 	struct snd_compr_runtime *crtd;
52858f7a5cSDaniel Baluta 	struct snd_soc_component *component;
53e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream;
54858f7a5cSDaniel Baluta 	struct snd_sof_pcm *spcm;
55858f7a5cSDaniel Baluta 
56858f7a5cSDaniel Baluta 	if (!cstream)
57858f7a5cSDaniel Baluta 		return;
58858f7a5cSDaniel Baluta 
597e4bfcf1SWeiguo Li 	rtd = cstream->private_data;
607e4bfcf1SWeiguo Li 	crtd = cstream->runtime;
61e3091f0aSDaniel Baluta 	sstream = crtd->private_data;
62858f7a5cSDaniel Baluta 	component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
63858f7a5cSDaniel Baluta 
64858f7a5cSDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
65858f7a5cSDaniel Baluta 	if (!spcm) {
66858f7a5cSDaniel Baluta 		dev_err(component->dev,
67858f7a5cSDaniel Baluta 			"fragment elapsed called for unknown stream!\n");
68858f7a5cSDaniel Baluta 		return;
69858f7a5cSDaniel Baluta 	}
70858f7a5cSDaniel Baluta 
71e3091f0aSDaniel Baluta 	sof_set_transferred_bytes(sstream, spcm->stream[cstream->direction].posn.host_posn,
726324cf90SDaniel Baluta 				  crtd->buffer_size);
736324cf90SDaniel Baluta 
74858f7a5cSDaniel Baluta 	/* use the same workqueue-based solution as for PCM, cf. snd_sof_pcm_elapsed */
75858f7a5cSDaniel Baluta 	schedule_work(&spcm->stream[cstream->direction].period_elapsed_work);
76858f7a5cSDaniel Baluta }
776324cf90SDaniel Baluta 
create_page_table(struct snd_soc_component * component,struct snd_compr_stream * cstream,unsigned char * dma_area,size_t size)786324cf90SDaniel Baluta static int create_page_table(struct snd_soc_component *component,
796324cf90SDaniel Baluta 			     struct snd_compr_stream *cstream,
806324cf90SDaniel Baluta 			     unsigned char *dma_area, size_t size)
816324cf90SDaniel Baluta {
826324cf90SDaniel Baluta 	struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p;
836324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
846324cf90SDaniel Baluta 	int dir = cstream->direction;
856324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
866324cf90SDaniel Baluta 
876324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
886324cf90SDaniel Baluta 	if (!spcm)
896324cf90SDaniel Baluta 		return -EINVAL;
906324cf90SDaniel Baluta 
916324cf90SDaniel Baluta 	return snd_sof_create_page_table(component->dev, dmab,
926324cf90SDaniel Baluta 					 spcm->stream[dir].page_table.area, size);
936324cf90SDaniel Baluta }
946324cf90SDaniel Baluta 
sof_compr_open(struct snd_soc_component * component,struct snd_compr_stream * cstream)95e7c799e7SDaniel Baluta static int sof_compr_open(struct snd_soc_component *component,
966324cf90SDaniel Baluta 			  struct snd_compr_stream *cstream)
976324cf90SDaniel Baluta {
986324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
996324cf90SDaniel Baluta 	struct snd_compr_runtime *crtd = cstream->runtime;
100e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream;
1016324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
1026324cf90SDaniel Baluta 	int dir;
1036324cf90SDaniel Baluta 
104e3091f0aSDaniel Baluta 	sstream = kzalloc(sizeof(*sstream), GFP_KERNEL);
105e3091f0aSDaniel Baluta 	if (!sstream)
1066324cf90SDaniel Baluta 		return -ENOMEM;
1076324cf90SDaniel Baluta 
1086324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
1096324cf90SDaniel Baluta 	if (!spcm) {
110e3091f0aSDaniel Baluta 		kfree(sstream);
1116324cf90SDaniel Baluta 		return -EINVAL;
1126324cf90SDaniel Baluta 	}
1136324cf90SDaniel Baluta 
1146324cf90SDaniel Baluta 	dir = cstream->direction;
1156324cf90SDaniel Baluta 
1166324cf90SDaniel Baluta 	if (spcm->stream[dir].cstream) {
117e3091f0aSDaniel Baluta 		kfree(sstream);
1186324cf90SDaniel Baluta 		return -EBUSY;
1196324cf90SDaniel Baluta 	}
1206324cf90SDaniel Baluta 
1216324cf90SDaniel Baluta 	spcm->stream[dir].cstream = cstream;
1226324cf90SDaniel Baluta 	spcm->stream[dir].posn.host_posn = 0;
1236324cf90SDaniel Baluta 	spcm->stream[dir].posn.dai_posn = 0;
1246324cf90SDaniel Baluta 	spcm->prepared[dir] = false;
1256324cf90SDaniel Baluta 
126e3091f0aSDaniel Baluta 	crtd->private_data = sstream;
1276324cf90SDaniel Baluta 
1286324cf90SDaniel Baluta 	return 0;
1296324cf90SDaniel Baluta }
1306324cf90SDaniel Baluta 
sof_compr_free(struct snd_soc_component * component,struct snd_compr_stream * cstream)131e7c799e7SDaniel Baluta static int sof_compr_free(struct snd_soc_component *component,
1326324cf90SDaniel Baluta 			  struct snd_compr_stream *cstream)
1336324cf90SDaniel Baluta {
1346324cf90SDaniel Baluta 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
135e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream = cstream->runtime->private_data;
1366324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
1376324cf90SDaniel Baluta 	struct sof_ipc_stream stream;
1386324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
1396324cf90SDaniel Baluta 	int ret = 0;
1406324cf90SDaniel Baluta 
1416324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
1426324cf90SDaniel Baluta 	if (!spcm)
1436324cf90SDaniel Baluta 		return -EINVAL;
1446324cf90SDaniel Baluta 
1456324cf90SDaniel Baluta 	stream.hdr.size = sizeof(stream);
1466324cf90SDaniel Baluta 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
1476324cf90SDaniel Baluta 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
1486324cf90SDaniel Baluta 
1496324cf90SDaniel Baluta 	if (spcm->prepared[cstream->direction]) {
150*367fd6ffSCurtis Malainey 		ret = sof_ipc_tx_message_no_reply(sdev->ipc, &stream, sizeof(stream));
1516324cf90SDaniel Baluta 		if (!ret)
1526324cf90SDaniel Baluta 			spcm->prepared[cstream->direction] = false;
1536324cf90SDaniel Baluta 	}
1546324cf90SDaniel Baluta 
1556324cf90SDaniel Baluta 	cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work);
1566324cf90SDaniel Baluta 	spcm->stream[cstream->direction].cstream = NULL;
157e3091f0aSDaniel Baluta 	kfree(sstream);
1586324cf90SDaniel Baluta 
1596324cf90SDaniel Baluta 	return ret;
1606324cf90SDaniel Baluta }
1616324cf90SDaniel Baluta 
sof_compr_set_params(struct snd_soc_component * component,struct snd_compr_stream * cstream,struct snd_compr_params * params)162e7c799e7SDaniel Baluta static int sof_compr_set_params(struct snd_soc_component *component,
1636324cf90SDaniel Baluta 				struct snd_compr_stream *cstream, struct snd_compr_params *params)
1646324cf90SDaniel Baluta {
1656324cf90SDaniel Baluta 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
1666324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
1676324cf90SDaniel Baluta 	struct snd_compr_runtime *crtd = cstream->runtime;
1686324cf90SDaniel Baluta 	struct sof_ipc_pcm_params_reply ipc_params_reply;
169246b135fSDaniel Baluta 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
170246b135fSDaniel Baluta 	struct sof_ipc_fw_version *v = &ready->version;
171e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream;
172d5770daeSDaniel Baluta 	struct sof_ipc_pcm_params *pcm;
1736324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
1743f70c360SDaniel Baluta 	size_t ext_data_size;
1756324cf90SDaniel Baluta 	int ret;
1766324cf90SDaniel Baluta 
177246b135fSDaniel Baluta 	if (v->abi_version < SOF_ABI_VER(3, 22, 0)) {
178246b135fSDaniel Baluta 		dev_err(component->dev,
179246b135fSDaniel Baluta 			"Compress params not supported with FW ABI version %d:%d:%d\n",
180246b135fSDaniel Baluta 			SOF_ABI_VERSION_MAJOR(v->abi_version),
181246b135fSDaniel Baluta 			SOF_ABI_VERSION_MINOR(v->abi_version),
182246b135fSDaniel Baluta 			SOF_ABI_VERSION_PATCH(v->abi_version));
183246b135fSDaniel Baluta 		return -EINVAL;
184246b135fSDaniel Baluta 	}
185246b135fSDaniel Baluta 
186e3091f0aSDaniel Baluta 	sstream = crtd->private_data;
1876324cf90SDaniel Baluta 
1886324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
1896324cf90SDaniel Baluta 
1906324cf90SDaniel Baluta 	if (!spcm)
1916324cf90SDaniel Baluta 		return -EINVAL;
1926324cf90SDaniel Baluta 
1933f70c360SDaniel Baluta 	ext_data_size = sizeof(params->codec);
1943f70c360SDaniel Baluta 
1953f70c360SDaniel Baluta 	if (sizeof(*pcm) + ext_data_size > sdev->ipc->max_payload_size)
1963f70c360SDaniel Baluta 		return -EINVAL;
1973f70c360SDaniel Baluta 
1983f70c360SDaniel Baluta 	pcm = kzalloc(sizeof(*pcm) + ext_data_size, GFP_KERNEL);
199d5770daeSDaniel Baluta 	if (!pcm)
200d5770daeSDaniel Baluta 		return -ENOMEM;
201d5770daeSDaniel Baluta 
2026324cf90SDaniel Baluta 	cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
2036324cf90SDaniel Baluta 	cstream->dma_buffer.dev.dev = sdev->dev;
2046324cf90SDaniel Baluta 	ret = snd_compr_malloc_pages(cstream, crtd->buffer_size);
2056324cf90SDaniel Baluta 	if (ret < 0)
206d5770daeSDaniel Baluta 		goto out;
2076324cf90SDaniel Baluta 
2086324cf90SDaniel Baluta 	ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes);
2096324cf90SDaniel Baluta 	if (ret < 0)
210d5770daeSDaniel Baluta 		goto out;
2116324cf90SDaniel Baluta 
212d5770daeSDaniel Baluta 	pcm->params.buffer.pages = PFN_UP(crtd->dma_bytes);
2133f70c360SDaniel Baluta 	pcm->hdr.size = sizeof(*pcm) + ext_data_size;
214d5770daeSDaniel Baluta 	pcm->hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
2156324cf90SDaniel Baluta 
216d5770daeSDaniel Baluta 	pcm->comp_id = spcm->stream[cstream->direction].comp_id;
2173f70c360SDaniel Baluta 	pcm->params.hdr.size = sizeof(pcm->params) + ext_data_size;
218d5770daeSDaniel Baluta 	pcm->params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr;
219d5770daeSDaniel Baluta 	pcm->params.buffer.size = crtd->dma_bytes;
220d5770daeSDaniel Baluta 	pcm->params.direction = cstream->direction;
221d5770daeSDaniel Baluta 	pcm->params.channels = params->codec.ch_out;
222d5770daeSDaniel Baluta 	pcm->params.rate = params->codec.sample_rate;
223d5770daeSDaniel Baluta 	pcm->params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
224d5770daeSDaniel Baluta 	pcm->params.frame_fmt = SOF_IPC_FRAME_S32_LE;
225d5770daeSDaniel Baluta 	pcm->params.sample_container_bytes =
2266324cf90SDaniel Baluta 		snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3;
227d5770daeSDaniel Baluta 	pcm->params.host_period_bytes = params->buffer.fragment_size;
2283f70c360SDaniel Baluta 	pcm->params.ext_data_length = ext_data_size;
2296324cf90SDaniel Baluta 
2303f70c360SDaniel Baluta 	memcpy((u8 *)pcm->params.ext_data, &params->codec, ext_data_size);
2313f70c360SDaniel Baluta 
2323f70c360SDaniel Baluta 	ret = sof_ipc_tx_message(sdev->ipc, pcm, sizeof(*pcm) + ext_data_size,
2336324cf90SDaniel Baluta 				 &ipc_params_reply, sizeof(ipc_params_reply));
2346324cf90SDaniel Baluta 	if (ret < 0) {
2356324cf90SDaniel Baluta 		dev_err(component->dev, "error ipc failed\n");
236d5770daeSDaniel Baluta 		goto out;
2376324cf90SDaniel Baluta 	}
2386324cf90SDaniel Baluta 
239a9737808SDaniel Baluta 	ret = snd_sof_set_stream_data_offset(sdev, &spcm->stream[cstream->direction],
240a9737808SDaniel Baluta 					     ipc_params_reply.posn_offset);
241a9737808SDaniel Baluta 	if (ret < 0) {
242a9737808SDaniel Baluta 		dev_err(component->dev, "Invalid stream data offset for Compr %d\n",
243a9737808SDaniel Baluta 			spcm->pcm.pcm_id);
244a9737808SDaniel Baluta 		goto out;
245a9737808SDaniel Baluta 	}
246a9737808SDaniel Baluta 
247e3091f0aSDaniel Baluta 	sstream->sampling_rate = params->codec.sample_rate;
2483ccbe688SLaurentiu Mihalcea 	sstream->channels = params->codec.ch_out;
2493ccbe688SLaurentiu Mihalcea 	sstream->sample_container_bytes = pcm->params.sample_container_bytes;
2506324cf90SDaniel Baluta 
2516324cf90SDaniel Baluta 	spcm->prepared[cstream->direction] = true;
2526324cf90SDaniel Baluta 
253d5770daeSDaniel Baluta out:
254d5770daeSDaniel Baluta 	kfree(pcm);
255d5770daeSDaniel Baluta 
256d5770daeSDaniel Baluta 	return ret;
2576324cf90SDaniel Baluta }
2586324cf90SDaniel Baluta 
sof_compr_get_params(struct snd_soc_component * component,struct snd_compr_stream * cstream,struct snd_codec * params)259e7c799e7SDaniel Baluta static int sof_compr_get_params(struct snd_soc_component *component,
2606324cf90SDaniel Baluta 				struct snd_compr_stream *cstream, struct snd_codec *params)
2616324cf90SDaniel Baluta {
2626324cf90SDaniel Baluta 	/* TODO: we don't query the supported codecs for now, if the
2636324cf90SDaniel Baluta 	 * application asks for an unsupported codec the set_params() will fail.
2646324cf90SDaniel Baluta 	 */
2656324cf90SDaniel Baluta 	return 0;
2666324cf90SDaniel Baluta }
2676324cf90SDaniel Baluta 
sof_compr_trigger(struct snd_soc_component * component,struct snd_compr_stream * cstream,int cmd)268e7c799e7SDaniel Baluta static int sof_compr_trigger(struct snd_soc_component *component,
2696324cf90SDaniel Baluta 			     struct snd_compr_stream *cstream, int cmd)
2706324cf90SDaniel Baluta {
2716324cf90SDaniel Baluta 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
2726324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
2736324cf90SDaniel Baluta 	struct sof_ipc_stream stream;
2746324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
2756324cf90SDaniel Baluta 
2766324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
2776324cf90SDaniel Baluta 	if (!spcm)
2786324cf90SDaniel Baluta 		return -EINVAL;
2796324cf90SDaniel Baluta 
2806324cf90SDaniel Baluta 	stream.hdr.size = sizeof(stream);
2816324cf90SDaniel Baluta 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
2826324cf90SDaniel Baluta 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
2836324cf90SDaniel Baluta 
2846324cf90SDaniel Baluta 	switch (cmd) {
2856324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_START:
2866324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
2876324cf90SDaniel Baluta 		break;
2886324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_STOP:
2896324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
2906324cf90SDaniel Baluta 		break;
2916324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2926324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
2936324cf90SDaniel Baluta 		break;
2946324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2956324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
2966324cf90SDaniel Baluta 		break;
2976324cf90SDaniel Baluta 	default:
2986324cf90SDaniel Baluta 		dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd);
2996324cf90SDaniel Baluta 		break;
3006324cf90SDaniel Baluta 	}
3016324cf90SDaniel Baluta 
302*367fd6ffSCurtis Malainey 	return sof_ipc_tx_message_no_reply(sdev->ipc, &stream, sizeof(stream));
3036324cf90SDaniel Baluta }
3046324cf90SDaniel Baluta 
sof_compr_copy_playback(struct snd_compr_runtime * rtd,char __user * buf,size_t count)305272ff882SLaurentiu Mihalcea static int sof_compr_copy_playback(struct snd_compr_runtime *rtd,
3066324cf90SDaniel Baluta 				   char __user *buf, size_t count)
3076324cf90SDaniel Baluta {
3086324cf90SDaniel Baluta 	void *ptr;
309272ff882SLaurentiu Mihalcea 	unsigned int offset, n;
3106324cf90SDaniel Baluta 	int ret;
3116324cf90SDaniel Baluta 
3126324cf90SDaniel Baluta 	div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset);
3136324cf90SDaniel Baluta 	ptr = rtd->dma_area + offset;
3146324cf90SDaniel Baluta 	n = rtd->buffer_size - offset;
3156324cf90SDaniel Baluta 
3166324cf90SDaniel Baluta 	if (count < n) {
3176324cf90SDaniel Baluta 		ret = copy_from_user(ptr, buf, count);
3186324cf90SDaniel Baluta 	} else {
3196324cf90SDaniel Baluta 		ret = copy_from_user(ptr, buf, n);
3206324cf90SDaniel Baluta 		ret += copy_from_user(rtd->dma_area, buf + n, count - n);
3216324cf90SDaniel Baluta 	}
3226324cf90SDaniel Baluta 
3236324cf90SDaniel Baluta 	return count - ret;
3246324cf90SDaniel Baluta }
3256324cf90SDaniel Baluta 
sof_compr_copy_capture(struct snd_compr_runtime * rtd,char __user * buf,size_t count)3261a01e192SLaurentiu Mihalcea static int sof_compr_copy_capture(struct snd_compr_runtime *rtd,
3271a01e192SLaurentiu Mihalcea 				  char __user *buf, size_t count)
3281a01e192SLaurentiu Mihalcea {
3291a01e192SLaurentiu Mihalcea 	void *ptr;
3301a01e192SLaurentiu Mihalcea 	unsigned int offset, n;
3311a01e192SLaurentiu Mihalcea 	int ret;
3321a01e192SLaurentiu Mihalcea 
3331a01e192SLaurentiu Mihalcea 	div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
3341a01e192SLaurentiu Mihalcea 	ptr = rtd->dma_area + offset;
3351a01e192SLaurentiu Mihalcea 	n = rtd->buffer_size - offset;
3361a01e192SLaurentiu Mihalcea 
3371a01e192SLaurentiu Mihalcea 	if (count < n) {
3381a01e192SLaurentiu Mihalcea 		ret = copy_to_user(buf, ptr, count);
3391a01e192SLaurentiu Mihalcea 	} else {
3401a01e192SLaurentiu Mihalcea 		ret = copy_to_user(buf, ptr, n);
3411a01e192SLaurentiu Mihalcea 		ret += copy_to_user(buf + n, rtd->dma_area, count - n);
3421a01e192SLaurentiu Mihalcea 	}
3431a01e192SLaurentiu Mihalcea 
3441a01e192SLaurentiu Mihalcea 	return count - ret;
3451a01e192SLaurentiu Mihalcea }
3461a01e192SLaurentiu Mihalcea 
sof_compr_copy(struct snd_soc_component * component,struct snd_compr_stream * cstream,char __user * buf,size_t count)347272ff882SLaurentiu Mihalcea static int sof_compr_copy(struct snd_soc_component *component,
348272ff882SLaurentiu Mihalcea 			  struct snd_compr_stream *cstream,
349272ff882SLaurentiu Mihalcea 			  char __user *buf, size_t count)
350272ff882SLaurentiu Mihalcea {
351272ff882SLaurentiu Mihalcea 	struct snd_compr_runtime *rtd = cstream->runtime;
352272ff882SLaurentiu Mihalcea 
353272ff882SLaurentiu Mihalcea 	if (count > rtd->buffer_size)
354272ff882SLaurentiu Mihalcea 		count = rtd->buffer_size;
355272ff882SLaurentiu Mihalcea 
3561a01e192SLaurentiu Mihalcea 	if (cstream->direction == SND_COMPRESS_PLAYBACK)
357272ff882SLaurentiu Mihalcea 		return sof_compr_copy_playback(rtd, buf, count);
3581a01e192SLaurentiu Mihalcea 	else
3591a01e192SLaurentiu Mihalcea 		return sof_compr_copy_capture(rtd, buf, count);
360272ff882SLaurentiu Mihalcea }
361272ff882SLaurentiu Mihalcea 
sof_compr_pointer(struct snd_soc_component * component,struct snd_compr_stream * cstream,struct snd_compr_tstamp * tstamp)3626324cf90SDaniel Baluta static int sof_compr_pointer(struct snd_soc_component *component,
3636324cf90SDaniel Baluta 			     struct snd_compr_stream *cstream,
3646324cf90SDaniel Baluta 			     struct snd_compr_tstamp *tstamp)
3656324cf90SDaniel Baluta {
366c1a731c7SLaurentiu Mihalcea 	struct snd_sof_pcm *spcm;
367c1a731c7SLaurentiu Mihalcea 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
368e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream = cstream->runtime->private_data;
3696324cf90SDaniel Baluta 
370c1a731c7SLaurentiu Mihalcea 	spcm = snd_sof_find_spcm_dai(component, rtd);
371c1a731c7SLaurentiu Mihalcea 	if (!spcm)
372c1a731c7SLaurentiu Mihalcea 		return -EINVAL;
373c1a731c7SLaurentiu Mihalcea 
374e3091f0aSDaniel Baluta 	tstamp->sampling_rate = sstream->sampling_rate;
375e3091f0aSDaniel Baluta 	tstamp->copied_total = sstream->copied_total;
376c1a731c7SLaurentiu Mihalcea 	tstamp->pcm_io_frames = div_u64(spcm->stream[cstream->direction].posn.dai_posn,
377c1a731c7SLaurentiu Mihalcea 					sstream->channels * sstream->sample_container_bytes);
3786324cf90SDaniel Baluta 
3796324cf90SDaniel Baluta 	return 0;
3806324cf90SDaniel Baluta }
3816324cf90SDaniel Baluta 
3826324cf90SDaniel Baluta struct snd_compress_ops sof_compressed_ops = {
3836324cf90SDaniel Baluta 	.open		= sof_compr_open,
3846324cf90SDaniel Baluta 	.free		= sof_compr_free,
3856324cf90SDaniel Baluta 	.set_params	= sof_compr_set_params,
3866324cf90SDaniel Baluta 	.get_params	= sof_compr_get_params,
3876324cf90SDaniel Baluta 	.trigger	= sof_compr_trigger,
3886324cf90SDaniel Baluta 	.pointer	= sof_compr_pointer,
3896324cf90SDaniel Baluta 	.copy		= sof_compr_copy,
3906324cf90SDaniel Baluta };
3916324cf90SDaniel Baluta EXPORT_SYMBOL(sof_compressed_ops);
392