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 remaining; 80 81 if (!ipc4_msg->header_u64 || !count || *ppos) 82 return 0; 83 84 remaining = sizeof(ipc4_msg->header_u64); 85 86 /* Only get large config have payload */ 87 if (SOF_IPC4_MSG_IS_MODULE_MSG(ipc4_msg->primary) && 88 (SOF_IPC4_MSG_TYPE_GET(ipc4_msg->primary) == SOF_IPC4_MOD_LARGE_CONFIG_GET)) 89 remaining += ipc4_msg->data_size; 90 91 if (count > remaining) 92 count = remaining; 93 94 /* copy the header first */ 95 if (copy_to_user(buffer, &ipc4_msg->header_u64, sizeof(ipc4_msg->header_u64))) 96 return -EFAULT; 97 98 *ppos += sizeof(ipc4_msg->header_u64); 99 remaining -= sizeof(ipc4_msg->header_u64); 100 101 if (!remaining) 102 return count; 103 104 if (remaining > ipc4_msg->data_size) 105 remaining = ipc4_msg->data_size; 106 107 /* Copy the payload */ 108 if (copy_to_user(buffer + *ppos, ipc4_msg->data_ptr, remaining)) 109 return -EFAULT; 110 111 *ppos += remaining; 112 return count; 113 } 114 115 static int sof_msg_inject_send_message(struct sof_client_dev *cdev) 116 { 117 struct sof_msg_inject_priv *priv = cdev->data; 118 struct device *dev = &cdev->auxdev.dev; 119 int ret, err; 120 121 ret = pm_runtime_resume_and_get(dev); 122 if (ret < 0 && ret != -EACCES) { 123 dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret); 124 return ret; 125 } 126 127 /* send the message */ 128 ret = sof_client_ipc_tx_message(cdev, priv->tx_buffer, priv->rx_buffer, 129 priv->max_msg_size); 130 if (ret) 131 dev_err(dev, "IPC message send failed: %d\n", ret); 132 133 pm_runtime_mark_last_busy(dev); 134 err = pm_runtime_put_autosuspend(dev); 135 if (err < 0) 136 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err); 137 138 return ret; 139 } 140 141 static ssize_t sof_msg_inject_dfs_write(struct file *file, const char __user *buffer, 142 size_t count, loff_t *ppos) 143 { 144 struct sof_client_dev *cdev = file->private_data; 145 struct sof_msg_inject_priv *priv = cdev->data; 146 size_t size; 147 int ret; 148 149 if (*ppos) 150 return 0; 151 152 size = simple_write_to_buffer(priv->tx_buffer, priv->max_msg_size, 153 ppos, buffer, count); 154 if (size != count) 155 return size > 0 ? -EFAULT : size; 156 157 memset(priv->rx_buffer, 0, priv->max_msg_size); 158 159 ret = sof_msg_inject_send_message(cdev); 160 161 /* return the error code if test failed */ 162 if (ret < 0) 163 size = ret; 164 165 return size; 166 }; 167 168 static ssize_t sof_msg_inject_ipc4_dfs_write(struct file *file, 169 const char __user *buffer, 170 size_t count, loff_t *ppos) 171 { 172 struct sof_client_dev *cdev = file->private_data; 173 struct sof_msg_inject_priv *priv = cdev->data; 174 struct sof_ipc4_msg *ipc4_msg = priv->tx_buffer; 175 size_t size; 176 int ret; 177 178 if (*ppos) 179 return 0; 180 181 if (count < sizeof(ipc4_msg->header_u64)) 182 return -EINVAL; 183 184 /* copy the header first */ 185 size = simple_write_to_buffer(&ipc4_msg->header_u64, 186 sizeof(ipc4_msg->header_u64), 187 ppos, buffer, count); 188 if (size != sizeof(ipc4_msg->header_u64)) 189 return size > 0 ? -EFAULT : size; 190 191 count -= size; 192 if (!count) { 193 /* Copy the payload */ 194 size = simple_write_to_buffer(ipc4_msg->data_ptr, 195 priv->max_msg_size, ppos, buffer, 196 count); 197 if (size != count) 198 return size > 0 ? -EFAULT : size; 199 } 200 201 ipc4_msg->data_size = count; 202 203 /* Initialize the reply storage */ 204 ipc4_msg = priv->rx_buffer; 205 ipc4_msg->header_u64 = 0; 206 ipc4_msg->data_size = priv->max_msg_size; 207 memset(ipc4_msg->data_ptr, 0, priv->max_msg_size); 208 209 ret = sof_msg_inject_send_message(cdev); 210 211 /* return the error code if test failed */ 212 if (ret < 0) 213 size = ret; 214 215 return size; 216 }; 217 218 static int sof_msg_inject_dfs_release(struct inode *inode, struct file *file) 219 { 220 debugfs_file_put(file->f_path.dentry); 221 222 return 0; 223 } 224 225 static const struct file_operations sof_msg_inject_fops = { 226 .open = sof_msg_inject_dfs_open, 227 .read = sof_msg_inject_dfs_read, 228 .write = sof_msg_inject_dfs_write, 229 .llseek = default_llseek, 230 .release = sof_msg_inject_dfs_release, 231 232 .owner = THIS_MODULE, 233 }; 234 235 static const struct file_operations sof_msg_inject_ipc4_fops = { 236 .open = sof_msg_inject_dfs_open, 237 .read = sof_msg_inject_ipc4_dfs_read, 238 .write = sof_msg_inject_ipc4_dfs_write, 239 .llseek = default_llseek, 240 .release = sof_msg_inject_dfs_release, 241 242 .owner = THIS_MODULE, 243 }; 244 245 static int sof_msg_inject_probe(struct auxiliary_device *auxdev, 246 const struct auxiliary_device_id *id) 247 { 248 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); 249 struct dentry *debugfs_root = sof_client_get_debugfs_root(cdev); 250 static const struct file_operations *fops; 251 struct device *dev = &auxdev->dev; 252 struct sof_msg_inject_priv *priv; 253 size_t alloc_size; 254 255 /* allocate memory for client data */ 256 priv = devm_kzalloc(&auxdev->dev, sizeof(*priv), GFP_KERNEL); 257 if (!priv) 258 return -ENOMEM; 259 260 priv->ipc_type = sof_client_get_ipc_type(cdev); 261 priv->max_msg_size = sof_client_get_ipc_max_payload_size(cdev); 262 alloc_size = priv->max_msg_size; 263 264 if (priv->ipc_type == SOF_INTEL_IPC4) 265 alloc_size += sizeof(struct sof_ipc4_msg); 266 267 priv->tx_buffer = devm_kmalloc(dev, alloc_size, GFP_KERNEL); 268 priv->rx_buffer = devm_kzalloc(dev, alloc_size, GFP_KERNEL); 269 if (!priv->tx_buffer || !priv->rx_buffer) 270 return -ENOMEM; 271 272 if (priv->ipc_type == SOF_INTEL_IPC4) { 273 struct sof_ipc4_msg *ipc4_msg; 274 275 ipc4_msg = priv->tx_buffer; 276 ipc4_msg->data_ptr = priv->tx_buffer + sizeof(struct sof_ipc4_msg); 277 278 ipc4_msg = priv->rx_buffer; 279 ipc4_msg->data_ptr = priv->rx_buffer + sizeof(struct sof_ipc4_msg); 280 281 fops = &sof_msg_inject_ipc4_fops; 282 } else { 283 fops = &sof_msg_inject_fops; 284 } 285 286 cdev->data = priv; 287 288 priv->dfs_file = debugfs_create_file("ipc_msg_inject", 0644, debugfs_root, 289 cdev, fops); 290 291 /* enable runtime PM */ 292 pm_runtime_set_autosuspend_delay(dev, SOF_IPC_CLIENT_SUSPEND_DELAY_MS); 293 pm_runtime_use_autosuspend(dev); 294 pm_runtime_enable(dev); 295 pm_runtime_mark_last_busy(dev); 296 pm_runtime_idle(dev); 297 298 return 0; 299 } 300 301 static void sof_msg_inject_remove(struct auxiliary_device *auxdev) 302 { 303 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); 304 struct sof_msg_inject_priv *priv = cdev->data; 305 306 pm_runtime_disable(&auxdev->dev); 307 308 debugfs_remove(priv->dfs_file); 309 } 310 311 static const struct auxiliary_device_id sof_msg_inject_client_id_table[] = { 312 { .name = "snd_sof.msg_injector" }, 313 {}, 314 }; 315 MODULE_DEVICE_TABLE(auxiliary, sof_msg_inject_client_id_table); 316 317 /* 318 * No need for driver pm_ops as the generic pm callbacks in the auxiliary bus 319 * type are enough to ensure that the parent SOF device resumes to bring the DSP 320 * back to D0. 321 * Driver name will be set based on KBUILD_MODNAME. 322 */ 323 static struct auxiliary_driver sof_msg_inject_client_drv = { 324 .probe = sof_msg_inject_probe, 325 .remove = sof_msg_inject_remove, 326 327 .id_table = sof_msg_inject_client_id_table, 328 }; 329 330 module_auxiliary_driver(sof_msg_inject_client_drv); 331 332 MODULE_DESCRIPTION("SOF IPC Message Injector Client Driver"); 333 MODULE_LICENSE("GPL"); 334 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT); 335