1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
4 //
5 // Author: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
6 //
7 
8 #include <linux/auxiliary_bus.h>
9 #include <linux/completion.h>
10 #include <linux/debugfs.h>
11 #include <linux/ktime.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/slab.h>
16 #include <linux/uaccess.h>
17 #include <sound/sof/header.h>
18 #include <sound/sof/ipc4/header.h>
19 
20 #include "sof-client.h"
21 
22 #define SOF_IPC_CLIENT_SUSPEND_DELAY_MS	3000
23 
24 struct sof_msg_inject_priv {
25 	struct dentry *dfs_file;
26 	size_t max_msg_size;
27 	enum sof_ipc_type ipc_type;
28 
29 	void *tx_buffer;
30 	void *rx_buffer;
31 };
32 
33 static int sof_msg_inject_dfs_open(struct inode *inode, struct file *file)
34 {
35 	struct sof_client_dev *cdev = inode->i_private;
36 	int ret;
37 
38 	if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED)
39 		return -ENODEV;
40 
41 	ret = debugfs_file_get(file->f_path.dentry);
42 	if (unlikely(ret))
43 		return ret;
44 
45 	ret = simple_open(inode, file);
46 	if (ret)
47 		debugfs_file_put(file->f_path.dentry);
48 
49 	return ret;
50 }
51 
52 static ssize_t sof_msg_inject_dfs_read(struct file *file, char __user *buffer,
53 				       size_t count, loff_t *ppos)
54 {
55 	struct sof_client_dev *cdev = file->private_data;
56 	struct sof_msg_inject_priv *priv = cdev->data;
57 	struct sof_ipc_reply *rhdr = priv->rx_buffer;
58 
59 	if (!rhdr->hdr.size || !count || *ppos)
60 		return 0;
61 
62 	if (count > rhdr->hdr.size)
63 		count = rhdr->hdr.size;
64 
65 	if (copy_to_user(buffer, priv->rx_buffer, count))
66 		return -EFAULT;
67 
68 	*ppos += count;
69 	return count;
70 }
71 
72 static ssize_t sof_msg_inject_ipc4_dfs_read(struct file *file,
73 					    char __user *buffer,
74 					    size_t count, loff_t *ppos)
75 {
76 	struct sof_client_dev *cdev = file->private_data;
77 	struct sof_msg_inject_priv *priv = cdev->data;
78 	struct sof_ipc4_msg *ipc4_msg = priv->rx_buffer;
79 	size_t header_size = sizeof(ipc4_msg->header_u64);
80 	size_t remaining;
81 
82 	if (!ipc4_msg->header_u64 || !count || *ppos)
83 		return 0;
84 
85 	/* we need space for the header at minimum (u64) */
86 	if (count < header_size)
87 		return -ENOSPC;
88 
89 	remaining = header_size;
90 
91 	/* Only get large config have payload */
92 	if (SOF_IPC4_MSG_IS_MODULE_MSG(ipc4_msg->primary) &&
93 	    (SOF_IPC4_MSG_TYPE_GET(ipc4_msg->primary) == SOF_IPC4_MOD_LARGE_CONFIG_GET))
94 		remaining += ipc4_msg->data_size;
95 
96 	if (count > remaining)
97 		count = remaining;
98 	else if (count < remaining)
99 		remaining = count;
100 
101 	/* copy the header first */
102 	if (copy_to_user(buffer, &ipc4_msg->header_u64, header_size))
103 		return -EFAULT;
104 
105 	*ppos += header_size;
106 	remaining -= header_size;
107 
108 	if (!remaining)
109 		return count;
110 
111 	if (remaining > ipc4_msg->data_size)
112 		remaining = ipc4_msg->data_size;
113 
114 	/* Copy the payload */
115 	if (copy_to_user(buffer + *ppos, ipc4_msg->data_ptr, remaining))
116 		return -EFAULT;
117 
118 	*ppos += remaining;
119 	return count;
120 }
121 
122 static int sof_msg_inject_send_message(struct sof_client_dev *cdev)
123 {
124 	struct sof_msg_inject_priv *priv = cdev->data;
125 	struct device *dev = &cdev->auxdev.dev;
126 	int ret, err;
127 
128 	ret = pm_runtime_resume_and_get(dev);
129 	if (ret < 0 && ret != -EACCES) {
130 		dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
131 		return ret;
132 	}
133 
134 	/* send the message */
135 	ret = sof_client_ipc_tx_message(cdev, priv->tx_buffer, priv->rx_buffer,
136 					priv->max_msg_size);
137 	if (ret)
138 		dev_err(dev, "IPC message send failed: %d\n", ret);
139 
140 	pm_runtime_mark_last_busy(dev);
141 	err = pm_runtime_put_autosuspend(dev);
142 	if (err < 0)
143 		dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
144 
145 	return ret;
146 }
147 
148 static ssize_t sof_msg_inject_dfs_write(struct file *file, const char __user *buffer,
149 					size_t count, loff_t *ppos)
150 {
151 	struct sof_client_dev *cdev = file->private_data;
152 	struct sof_msg_inject_priv *priv = cdev->data;
153 	size_t size;
154 	int ret;
155 
156 	if (*ppos)
157 		return 0;
158 
159 	size = simple_write_to_buffer(priv->tx_buffer, priv->max_msg_size,
160 				      ppos, buffer, count);
161 	if (size != count)
162 		return size > 0 ? -EFAULT : size;
163 
164 	memset(priv->rx_buffer, 0, priv->max_msg_size);
165 
166 	ret = sof_msg_inject_send_message(cdev);
167 
168 	/* return the error code if test failed */
169 	if (ret < 0)
170 		size = ret;
171 
172 	return size;
173 };
174 
175 static ssize_t sof_msg_inject_ipc4_dfs_write(struct file *file,
176 					     const char __user *buffer,
177 					     size_t count, loff_t *ppos)
178 {
179 	struct sof_client_dev *cdev = file->private_data;
180 	struct sof_msg_inject_priv *priv = cdev->data;
181 	struct sof_ipc4_msg *ipc4_msg = priv->tx_buffer;
182 	size_t size;
183 	int ret;
184 
185 	if (*ppos)
186 		return 0;
187 
188 	if (count < sizeof(ipc4_msg->header_u64))
189 		return -EINVAL;
190 
191 	/* copy the header first */
192 	size = simple_write_to_buffer(&ipc4_msg->header_u64,
193 				      sizeof(ipc4_msg->header_u64),
194 				      ppos, buffer, count);
195 	if (size != sizeof(ipc4_msg->header_u64))
196 		return size > 0 ? -EFAULT : size;
197 
198 	count -= size;
199 	if (!count) {
200 		/* Copy the payload */
201 		size = simple_write_to_buffer(ipc4_msg->data_ptr,
202 					      priv->max_msg_size, ppos, buffer,
203 					      count);
204 		if (size != count)
205 			return size > 0 ? -EFAULT : size;
206 	}
207 
208 	ipc4_msg->data_size = count;
209 
210 	/* Initialize the reply storage */
211 	ipc4_msg = priv->rx_buffer;
212 	ipc4_msg->header_u64 = 0;
213 	ipc4_msg->data_size = priv->max_msg_size;
214 	memset(ipc4_msg->data_ptr, 0, priv->max_msg_size);
215 
216 	ret = sof_msg_inject_send_message(cdev);
217 
218 	/* return the error code if test failed */
219 	if (ret < 0)
220 		size = ret;
221 
222 	return size;
223 };
224 
225 static int sof_msg_inject_dfs_release(struct inode *inode, struct file *file)
226 {
227 	debugfs_file_put(file->f_path.dentry);
228 
229 	return 0;
230 }
231 
232 static const struct file_operations sof_msg_inject_fops = {
233 	.open = sof_msg_inject_dfs_open,
234 	.read = sof_msg_inject_dfs_read,
235 	.write = sof_msg_inject_dfs_write,
236 	.llseek = default_llseek,
237 	.release = sof_msg_inject_dfs_release,
238 
239 	.owner = THIS_MODULE,
240 };
241 
242 static const struct file_operations sof_msg_inject_ipc4_fops = {
243 	.open = sof_msg_inject_dfs_open,
244 	.read = sof_msg_inject_ipc4_dfs_read,
245 	.write = sof_msg_inject_ipc4_dfs_write,
246 	.llseek = default_llseek,
247 	.release = sof_msg_inject_dfs_release,
248 
249 	.owner = THIS_MODULE,
250 };
251 
252 static int sof_msg_inject_probe(struct auxiliary_device *auxdev,
253 				const struct auxiliary_device_id *id)
254 {
255 	struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
256 	struct dentry *debugfs_root = sof_client_get_debugfs_root(cdev);
257 	static const struct file_operations *fops;
258 	struct device *dev = &auxdev->dev;
259 	struct sof_msg_inject_priv *priv;
260 	size_t alloc_size;
261 
262 	/* allocate memory for client data */
263 	priv = devm_kzalloc(&auxdev->dev, sizeof(*priv), GFP_KERNEL);
264 	if (!priv)
265 		return -ENOMEM;
266 
267 	priv->ipc_type = sof_client_get_ipc_type(cdev);
268 	priv->max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
269 	alloc_size = priv->max_msg_size;
270 
271 	if (priv->ipc_type == SOF_INTEL_IPC4)
272 		alloc_size += sizeof(struct sof_ipc4_msg);
273 
274 	priv->tx_buffer = devm_kmalloc(dev, alloc_size, GFP_KERNEL);
275 	priv->rx_buffer = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
276 	if (!priv->tx_buffer || !priv->rx_buffer)
277 		return -ENOMEM;
278 
279 	if (priv->ipc_type == SOF_INTEL_IPC4) {
280 		struct sof_ipc4_msg *ipc4_msg;
281 
282 		ipc4_msg = priv->tx_buffer;
283 		ipc4_msg->data_ptr = priv->tx_buffer + sizeof(struct sof_ipc4_msg);
284 
285 		ipc4_msg = priv->rx_buffer;
286 		ipc4_msg->data_ptr = priv->rx_buffer + sizeof(struct sof_ipc4_msg);
287 
288 		fops = &sof_msg_inject_ipc4_fops;
289 	} else {
290 		fops = &sof_msg_inject_fops;
291 	}
292 
293 	cdev->data = priv;
294 
295 	priv->dfs_file = debugfs_create_file("ipc_msg_inject", 0644, debugfs_root,
296 					     cdev, fops);
297 
298 	/* enable runtime PM */
299 	pm_runtime_set_autosuspend_delay(dev, SOF_IPC_CLIENT_SUSPEND_DELAY_MS);
300 	pm_runtime_use_autosuspend(dev);
301 	pm_runtime_enable(dev);
302 	pm_runtime_mark_last_busy(dev);
303 	pm_runtime_idle(dev);
304 
305 	return 0;
306 }
307 
308 static void sof_msg_inject_remove(struct auxiliary_device *auxdev)
309 {
310 	struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
311 	struct sof_msg_inject_priv *priv = cdev->data;
312 
313 	pm_runtime_disable(&auxdev->dev);
314 
315 	debugfs_remove(priv->dfs_file);
316 }
317 
318 static const struct auxiliary_device_id sof_msg_inject_client_id_table[] = {
319 	{ .name = "snd_sof.msg_injector" },
320 	{},
321 };
322 MODULE_DEVICE_TABLE(auxiliary, sof_msg_inject_client_id_table);
323 
324 /*
325  * No need for driver pm_ops as the generic pm callbacks in the auxiliary bus
326  * type are enough to ensure that the parent SOF device resumes to bring the DSP
327  * back to D0.
328  * Driver name will be set based on KBUILD_MODNAME.
329  */
330 static struct auxiliary_driver sof_msg_inject_client_drv = {
331 	.probe = sof_msg_inject_probe,
332 	.remove = sof_msg_inject_remove,
333 
334 	.id_table = sof_msg_inject_client_id_table,
335 };
336 
337 module_auxiliary_driver(sof_msg_inject_client_drv);
338 
339 MODULE_DESCRIPTION("SOF IPC Message Injector Client Driver");
340 MODULE_LICENSE("GPL");
341 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);
342