xref: /linux-6.15/sound/soc/sof/compress.c (revision e145e9af)
1 // SPDX-License-Identifier: (GPL-2.0 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-2020 Intel Corporation. All rights reserved.
7 //
8 // Author: Cezary Rojewski <[email protected]>
9 //
10 
11 #include <sound/soc.h>
12 #include "compress.h"
13 #include "ops.h"
14 #include "probe.h"
15 
16 int sof_probe_compr_open(struct snd_compr_stream *cstream,
17 		struct snd_soc_dai *dai)
18 {
19 	struct snd_sof_dev *sdev =
20 				snd_soc_component_get_drvdata(dai->component);
21 	int ret;
22 
23 	ret = snd_sof_probe_compr_assign(sdev, cstream, dai);
24 	if (ret < 0) {
25 		dev_err(dai->dev, "Failed to assign probe stream: %d\n", ret);
26 		return ret;
27 	}
28 
29 	sdev->extractor_stream_tag = ret;
30 	return 0;
31 }
32 EXPORT_SYMBOL(sof_probe_compr_open);
33 
34 int sof_probe_compr_free(struct snd_compr_stream *cstream,
35 		struct snd_soc_dai *dai)
36 {
37 	struct snd_sof_dev *sdev =
38 				snd_soc_component_get_drvdata(dai->component);
39 	struct sof_probe_point_desc *desc;
40 	size_t num_desc;
41 	int i, ret;
42 
43 	/* disconnect all probe points */
44 	ret = sof_ipc_probe_points_info(sdev, &desc, &num_desc);
45 	if (ret < 0) {
46 		dev_err(dai->dev, "Failed to get probe points: %d\n", ret);
47 		goto exit;
48 	}
49 
50 	for (i = 0; i < num_desc; i++)
51 		sof_ipc_probe_points_remove(sdev, &desc[i].buffer_id, 1);
52 	kfree(desc);
53 
54 exit:
55 	ret = sof_ipc_probe_deinit(sdev);
56 	if (ret < 0)
57 		dev_err(dai->dev, "Failed to deinit probe: %d\n", ret);
58 
59 	sdev->extractor_stream_tag = SOF_PROBE_INVALID_NODE_ID;
60 	snd_compr_free_pages(cstream);
61 
62 	return snd_sof_probe_compr_free(sdev, cstream, dai);
63 }
64 EXPORT_SYMBOL(sof_probe_compr_free);
65 
66 int sof_probe_compr_set_params(struct snd_compr_stream *cstream,
67 		struct snd_compr_params *params, struct snd_soc_dai *dai)
68 {
69 	struct snd_compr_runtime *rtd = cstream->runtime;
70 	struct snd_sof_dev *sdev =
71 				snd_soc_component_get_drvdata(dai->component);
72 	int ret;
73 
74 	cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
75 	cstream->dma_buffer.dev.dev = sdev->dev;
76 	ret = snd_compr_malloc_pages(cstream, rtd->buffer_size);
77 	if (ret < 0)
78 		return ret;
79 
80 	ret = snd_sof_probe_compr_set_params(sdev, cstream, params, dai);
81 	if (ret < 0)
82 		return ret;
83 
84 	ret = sof_ipc_probe_init(sdev, sdev->extractor_stream_tag,
85 				 rtd->dma_bytes);
86 	if (ret < 0) {
87 		dev_err(dai->dev, "Failed to init probe: %d\n", ret);
88 		return ret;
89 	}
90 
91 	return 0;
92 }
93 EXPORT_SYMBOL(sof_probe_compr_set_params);
94 
95 int sof_probe_compr_trigger(struct snd_compr_stream *cstream, int cmd,
96 		struct snd_soc_dai *dai)
97 {
98 	struct snd_sof_dev *sdev =
99 				snd_soc_component_get_drvdata(dai->component);
100 
101 	return snd_sof_probe_compr_trigger(sdev, cstream, cmd, dai);
102 }
103 EXPORT_SYMBOL(sof_probe_compr_trigger);
104 
105 int sof_probe_compr_pointer(struct snd_compr_stream *cstream,
106 		struct snd_compr_tstamp *tstamp, struct snd_soc_dai *dai)
107 {
108 	struct snd_sof_dev *sdev =
109 				snd_soc_component_get_drvdata(dai->component);
110 
111 	return snd_sof_probe_compr_pointer(sdev, cstream, tstamp, dai);
112 }
113 EXPORT_SYMBOL(sof_probe_compr_pointer);
114 
115 int sof_probe_compr_copy(struct snd_compr_stream *cstream,
116 		char __user *buf, size_t count)
117 {
118 	struct snd_compr_runtime *rtd = cstream->runtime;
119 	unsigned int offset, n;
120 	void *ptr;
121 	int ret;
122 
123 	if (count > rtd->buffer_size)
124 		count = rtd->buffer_size;
125 
126 	div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
127 	ptr = rtd->dma_area + offset;
128 	n = rtd->buffer_size - offset;
129 
130 	if (count < n) {
131 		ret = copy_to_user(buf, ptr, count);
132 	} else {
133 		ret = copy_to_user(buf, ptr, n);
134 		ret += copy_to_user(buf + n, rtd->dma_area, count - n);
135 	}
136 
137 	if (ret)
138 		return count - ret;
139 	return count;
140 }
141 EXPORT_SYMBOL(sof_probe_compr_copy);
142