|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +// |
| 3 | +// Copyright(c) 2021 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 | + |
| 19 | +#include "sof-client.h" |
| 20 | + |
| 21 | +#define SOF_IPC_CLIENT_SUSPEND_DELAY_MS 3000 |
| 22 | + |
| 23 | +struct sof_msg_inject_priv { |
| 24 | + struct dentry *dfs_file; |
| 25 | + |
| 26 | + void *tx_buffer; |
| 27 | + void *rx_buffer; |
| 28 | +}; |
| 29 | + |
| 30 | +static int sof_msg_inject_dfs_open(struct inode *inode, struct file *file) |
| 31 | +{ |
| 32 | + struct sof_client_dev *cdev = inode->i_private; |
| 33 | + int ret; |
| 34 | + |
| 35 | + if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED) |
| 36 | + return -ENODEV; |
| 37 | + |
| 38 | + ret = debugfs_file_get(file->f_path.dentry); |
| 39 | + if (unlikely(ret)) |
| 40 | + return ret; |
| 41 | + |
| 42 | + ret = simple_open(inode, file); |
| 43 | + if (ret) |
| 44 | + debugfs_file_put(file->f_path.dentry); |
| 45 | + |
| 46 | + return ret; |
| 47 | +} |
| 48 | + |
| 49 | +static ssize_t sof_msg_inject_dfs_read(struct file *file, char __user *buffer, |
| 50 | + size_t count, loff_t *ppos) |
| 51 | +{ |
| 52 | + struct sof_client_dev *cdev = file->private_data; |
| 53 | + struct sof_msg_inject_priv *priv = cdev->data; |
| 54 | + struct sof_ipc_reply *rhdr = priv->rx_buffer; |
| 55 | + |
| 56 | + if (!rhdr->hdr.size || !count || *ppos) |
| 57 | + return 0; |
| 58 | + |
| 59 | + if (count > rhdr->hdr.size) |
| 60 | + count = rhdr->hdr.size; |
| 61 | + |
| 62 | + if (copy_to_user(buffer, priv->rx_buffer, count)) |
| 63 | + return -EFAULT; |
| 64 | + |
| 65 | + *ppos += count; |
| 66 | + return count; |
| 67 | +} |
| 68 | + |
| 69 | +static ssize_t sof_msg_inject_dfs_write(struct file *file, const char __user *buffer, |
| 70 | + size_t count, loff_t *ppos) |
| 71 | +{ |
| 72 | + struct sof_client_dev *cdev = file->private_data; |
| 73 | + struct sof_msg_inject_priv *priv = cdev->data; |
| 74 | + struct device *dev = &cdev->auxdev.dev; |
| 75 | + int ret, err; |
| 76 | + size_t size; |
| 77 | + |
| 78 | + if (*ppos) |
| 79 | + return 0; |
| 80 | + |
| 81 | + size = simple_write_to_buffer(priv->tx_buffer, SOF_IPC_MSG_MAX_SIZE, |
| 82 | + ppos, buffer, count); |
| 83 | + if (size != count) |
| 84 | + return size > 0 ? -EFAULT : size; |
| 85 | + |
| 86 | + ret = pm_runtime_get_sync(dev); |
| 87 | + if (ret < 0 && ret != -EACCES) { |
| 88 | + dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret); |
| 89 | + pm_runtime_put_noidle(dev); |
| 90 | + return ret; |
| 91 | + } |
| 92 | + |
| 93 | + /* send the message */ |
| 94 | + memset(priv->rx_buffer, 0, SOF_IPC_MSG_MAX_SIZE); |
| 95 | + ret = sof_client_ipc_tx_message(cdev, priv->tx_buffer, priv->rx_buffer, |
| 96 | + SOF_IPC_MSG_MAX_SIZE); |
| 97 | + pm_runtime_mark_last_busy(dev); |
| 98 | + err = pm_runtime_put_autosuspend(dev); |
| 99 | + if (err < 0) |
| 100 | + dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err); |
| 101 | + |
| 102 | + /* return size if test is successful */ |
| 103 | + if (ret >= 0) |
| 104 | + ret = size; |
| 105 | + |
| 106 | + return ret; |
| 107 | +}; |
| 108 | + |
| 109 | +static int sof_msg_inject_dfs_release(struct inode *inode, struct file *file) |
| 110 | +{ |
| 111 | + debugfs_file_put(file->f_path.dentry); |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
| 115 | + |
| 116 | +static const struct file_operations sof_msg_inject_fops = { |
| 117 | + .open = sof_msg_inject_dfs_open, |
| 118 | + .read = sof_msg_inject_dfs_read, |
| 119 | + .write = sof_msg_inject_dfs_write, |
| 120 | + .llseek = default_llseek, |
| 121 | + .release = sof_msg_inject_dfs_release, |
| 122 | + |
| 123 | + .owner = THIS_MODULE, |
| 124 | +}; |
| 125 | + |
| 126 | +static int sof_msg_inject_probe(struct auxiliary_device *auxdev, |
| 127 | + const struct auxiliary_device_id *id) |
| 128 | +{ |
| 129 | + struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); |
| 130 | + struct dentry *debugfs_root = sof_client_get_debugfs_root(cdev); |
| 131 | + struct device *dev = &auxdev->dev; |
| 132 | + struct sof_msg_inject_priv *priv; |
| 133 | + |
| 134 | + /* allocate memory for client data */ |
| 135 | + priv = devm_kzalloc(&auxdev->dev, sizeof(*priv), GFP_KERNEL); |
| 136 | + if (!priv) |
| 137 | + return -ENOMEM; |
| 138 | + |
| 139 | + priv->tx_buffer = devm_kmalloc(dev, SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL); |
| 140 | + priv->rx_buffer = devm_kmalloc(dev, SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL); |
| 141 | + if (!priv->tx_buffer || !priv->rx_buffer) |
| 142 | + return -ENOMEM; |
| 143 | + |
| 144 | + cdev->data = priv; |
| 145 | + |
| 146 | + priv->dfs_file = debugfs_create_file("ipc_msg_inject", 0644, debugfs_root, |
| 147 | + cdev, &sof_msg_inject_fops); |
| 148 | + |
| 149 | + /* enable runtime PM */ |
| 150 | + pm_runtime_set_autosuspend_delay(dev, SOF_IPC_CLIENT_SUSPEND_DELAY_MS); |
| 151 | + pm_runtime_use_autosuspend(dev); |
| 152 | + pm_runtime_enable(dev); |
| 153 | + pm_runtime_mark_last_busy(dev); |
| 154 | + pm_runtime_idle(dev); |
| 155 | + |
| 156 | + return 0; |
| 157 | +} |
| 158 | + |
| 159 | +static void sof_msg_inject_remove(struct auxiliary_device *auxdev) |
| 160 | +{ |
| 161 | + struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); |
| 162 | + struct sof_msg_inject_priv *priv = cdev->data; |
| 163 | + |
| 164 | + pm_runtime_disable(&auxdev->dev); |
| 165 | + |
| 166 | + debugfs_remove(priv->dfs_file); |
| 167 | +} |
| 168 | + |
| 169 | +static const struct auxiliary_device_id sof_msg_inject_client_id_table[] = { |
| 170 | + { .name = "snd_sof.msg_injector" }, |
| 171 | + {}, |
| 172 | +}; |
| 173 | +MODULE_DEVICE_TABLE(auxiliary, sof_msg_inject_client_id_table); |
| 174 | + |
| 175 | +/* |
| 176 | + * No need for driver pm_ops as the generic pm callbacks in the auxiliary bus |
| 177 | + * type are enough to ensure that the parent SOF device resumes to bring the DSP |
| 178 | + * back to D0. |
| 179 | + * Driver name will be set based on KBUILD_MODNAME. |
| 180 | + */ |
| 181 | +static struct auxiliary_driver sof_msg_inject_client_drv = { |
| 182 | + .probe = sof_msg_inject_probe, |
| 183 | + .remove = sof_msg_inject_remove, |
| 184 | + |
| 185 | + .id_table = sof_msg_inject_client_id_table, |
| 186 | +}; |
| 187 | + |
| 188 | +module_auxiliary_driver(sof_msg_inject_client_drv); |
| 189 | + |
| 190 | +MODULE_DESCRIPTION("SOF IPC Message Injector Client Driver"); |
| 191 | +MODULE_LICENSE("GPL"); |
| 192 | +MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT); |
0 commit comments