1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
5 // Author: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
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>
20 #include "sof-client.h"
22 #define SOF_IPC_CLIENT_SUSPEND_DELAY_MS 3000
24 struct sof_msg_inject_priv {
25 struct dentry *dfs_file;
27 enum sof_ipc_type ipc_type;
33 static int sof_msg_inject_dfs_open(struct inode *inode, struct file *file)
35 struct sof_client_dev *cdev = inode->i_private;
38 if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED)
41 ret = debugfs_file_get(file->f_path.dentry);
45 ret = simple_open(inode, file);
47 debugfs_file_put(file->f_path.dentry);
52 static ssize_t sof_msg_inject_dfs_read(struct file *file, char __user *buffer,
53 size_t count, loff_t *ppos)
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;
59 if (!rhdr->hdr.size || !count || *ppos)
62 if (count > rhdr->hdr.size)
63 count = rhdr->hdr.size;
65 if (copy_to_user(buffer, priv->rx_buffer, count))
72 static ssize_t sof_msg_inject_ipc4_dfs_read(struct file *file,
74 size_t count, loff_t *ppos)
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);
82 if (!ipc4_msg->header_u64 || !count || *ppos)
85 /* we need space for the header at minimum (u64) */
86 if (count < header_size)
89 remaining = header_size;
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;
96 if (count > remaining)
98 else if (count < remaining)
101 /* copy the header first */
102 if (copy_to_user(buffer, &ipc4_msg->header_u64, header_size))
105 *ppos += header_size;
106 remaining -= header_size;
111 if (remaining > ipc4_msg->data_size)
112 remaining = ipc4_msg->data_size;
114 /* Copy the payload */
115 if (copy_to_user(buffer + *ppos, ipc4_msg->data_ptr, remaining))
122 static int sof_msg_inject_send_message(struct sof_client_dev *cdev)
124 struct sof_msg_inject_priv *priv = cdev->data;
125 struct device *dev = &cdev->auxdev.dev;
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);
134 /* send the message */
135 ret = sof_client_ipc_tx_message(cdev, priv->tx_buffer, priv->rx_buffer,
138 dev_err(dev, "IPC message send failed: %d\n", ret);
140 pm_runtime_mark_last_busy(dev);
141 err = pm_runtime_put_autosuspend(dev);
143 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
148 static ssize_t sof_msg_inject_dfs_write(struct file *file, const char __user *buffer,
149 size_t count, loff_t *ppos)
151 struct sof_client_dev *cdev = file->private_data;
152 struct sof_msg_inject_priv *priv = cdev->data;
159 size = simple_write_to_buffer(priv->tx_buffer, priv->max_msg_size,
160 ppos, buffer, count);
166 memset(priv->rx_buffer, 0, priv->max_msg_size);
168 ret = sof_msg_inject_send_message(cdev);
170 /* return the error code if test failed */
177 static ssize_t sof_msg_inject_ipc4_dfs_write(struct file *file,
178 const char __user *buffer,
179 size_t count, loff_t *ppos)
181 struct sof_client_dev *cdev = file->private_data;
182 struct sof_msg_inject_priv *priv = cdev->data;
183 struct sof_ipc4_msg *ipc4_msg = priv->tx_buffer;
190 if (count < sizeof(ipc4_msg->header_u64))
193 /* copy the header first */
194 size = simple_write_to_buffer(&ipc4_msg->header_u64,
195 sizeof(ipc4_msg->header_u64),
196 ppos, buffer, count);
199 if (size != sizeof(ipc4_msg->header_u64))
203 /* Copy the payload */
204 size = simple_write_to_buffer(ipc4_msg->data_ptr,
205 priv->max_msg_size, ppos, buffer,
212 ipc4_msg->data_size = count;
214 /* Initialize the reply storage */
215 ipc4_msg = priv->rx_buffer;
216 ipc4_msg->header_u64 = 0;
217 ipc4_msg->data_size = priv->max_msg_size;
218 memset(ipc4_msg->data_ptr, 0, priv->max_msg_size);
220 ret = sof_msg_inject_send_message(cdev);
222 /* return the error code if test failed */
229 static int sof_msg_inject_dfs_release(struct inode *inode, struct file *file)
231 debugfs_file_put(file->f_path.dentry);
236 static const struct file_operations sof_msg_inject_fops = {
237 .open = sof_msg_inject_dfs_open,
238 .read = sof_msg_inject_dfs_read,
239 .write = sof_msg_inject_dfs_write,
240 .llseek = default_llseek,
241 .release = sof_msg_inject_dfs_release,
243 .owner = THIS_MODULE,
246 static const struct file_operations sof_msg_inject_ipc4_fops = {
247 .open = sof_msg_inject_dfs_open,
248 .read = sof_msg_inject_ipc4_dfs_read,
249 .write = sof_msg_inject_ipc4_dfs_write,
250 .llseek = default_llseek,
251 .release = sof_msg_inject_dfs_release,
253 .owner = THIS_MODULE,
256 static int sof_msg_inject_probe(struct auxiliary_device *auxdev,
257 const struct auxiliary_device_id *id)
259 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
260 struct dentry *debugfs_root = sof_client_get_debugfs_root(cdev);
261 static const struct file_operations *fops;
262 struct device *dev = &auxdev->dev;
263 struct sof_msg_inject_priv *priv;
266 /* allocate memory for client data */
267 priv = devm_kzalloc(&auxdev->dev, sizeof(*priv), GFP_KERNEL);
271 priv->ipc_type = sof_client_get_ipc_type(cdev);
272 priv->max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
273 alloc_size = priv->max_msg_size;
275 if (priv->ipc_type == SOF_INTEL_IPC4)
276 alloc_size += sizeof(struct sof_ipc4_msg);
278 priv->tx_buffer = devm_kmalloc(dev, alloc_size, GFP_KERNEL);
279 priv->rx_buffer = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
280 if (!priv->tx_buffer || !priv->rx_buffer)
283 if (priv->ipc_type == SOF_INTEL_IPC4) {
284 struct sof_ipc4_msg *ipc4_msg;
286 ipc4_msg = priv->tx_buffer;
287 ipc4_msg->data_ptr = priv->tx_buffer + sizeof(struct sof_ipc4_msg);
289 ipc4_msg = priv->rx_buffer;
290 ipc4_msg->data_ptr = priv->rx_buffer + sizeof(struct sof_ipc4_msg);
292 fops = &sof_msg_inject_ipc4_fops;
294 fops = &sof_msg_inject_fops;
299 priv->dfs_file = debugfs_create_file("ipc_msg_inject", 0644, debugfs_root,
302 /* enable runtime PM */
303 pm_runtime_set_autosuspend_delay(dev, SOF_IPC_CLIENT_SUSPEND_DELAY_MS);
304 pm_runtime_use_autosuspend(dev);
305 pm_runtime_enable(dev);
306 pm_runtime_mark_last_busy(dev);
307 pm_runtime_idle(dev);
312 static void sof_msg_inject_remove(struct auxiliary_device *auxdev)
314 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
315 struct sof_msg_inject_priv *priv = cdev->data;
317 pm_runtime_disable(&auxdev->dev);
319 debugfs_remove(priv->dfs_file);
322 static const struct auxiliary_device_id sof_msg_inject_client_id_table[] = {
323 { .name = "snd_sof.msg_injector" },
326 MODULE_DEVICE_TABLE(auxiliary, sof_msg_inject_client_id_table);
329 * No need for driver pm_ops as the generic pm callbacks in the auxiliary bus
330 * type are enough to ensure that the parent SOF device resumes to bring the DSP
332 * Driver name will be set based on KBUILD_MODNAME.
334 static struct auxiliary_driver sof_msg_inject_client_drv = {
335 .probe = sof_msg_inject_probe,
336 .remove = sof_msg_inject_remove,
338 .id_table = sof_msg_inject_client_id_table,
341 module_auxiliary_driver(sof_msg_inject_client_drv);
343 MODULE_DESCRIPTION("SOF IPC Message Injector Client Driver");
344 MODULE_LICENSE("GPL");
345 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);