1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 // 2 // 3 // Copyright(c) 2023 Google Inc 3 // Copyright(c) 2023 Google Inc 4 // 4 // 5 // Author: Curtis Malainey <cujomalainey@chrom 5 // Author: Curtis Malainey <cujomalainey@chromium.org> 6 // 6 // 7 7 8 #include <linux/auxiliary_bus.h> 8 #include <linux/auxiliary_bus.h> 9 #include <linux/debugfs.h> 9 #include <linux/debugfs.h> 10 #include <linux/mod_devicetable.h> 10 #include <linux/mod_devicetable.h> 11 #include <linux/module.h> 11 #include <linux/module.h> 12 #include <linux/pm_runtime.h> 12 #include <linux/pm_runtime.h> 13 #include <sound/sof/header.h> 13 #include <sound/sof/header.h> 14 14 15 #include "sof-client.h" 15 #include "sof-client.h" 16 16 17 #define SOF_IPC_CLIENT_SUSPEND_DELAY_MS 3000 17 #define SOF_IPC_CLIENT_SUSPEND_DELAY_MS 3000 18 18 19 struct sof_msg_inject_priv { 19 struct sof_msg_inject_priv { 20 struct dentry *kernel_dfs_file; 20 struct dentry *kernel_dfs_file; 21 size_t max_msg_size; 21 size_t max_msg_size; 22 22 23 void *kernel_buffer; 23 void *kernel_buffer; 24 }; 24 }; 25 25 26 static int sof_msg_inject_dfs_open(struct inod 26 static int sof_msg_inject_dfs_open(struct inode *inode, struct file *file) 27 { 27 { 28 int ret = debugfs_file_get(file->f_pat 28 int ret = debugfs_file_get(file->f_path.dentry); 29 29 30 if (unlikely(ret)) 30 if (unlikely(ret)) 31 return ret; 31 return ret; 32 32 33 ret = simple_open(inode, file); 33 ret = simple_open(inode, file); 34 if (ret) 34 if (ret) 35 debugfs_file_put(file->f_path. 35 debugfs_file_put(file->f_path.dentry); 36 36 37 return ret; 37 return ret; 38 } 38 } 39 39 40 static ssize_t sof_kernel_msg_inject_dfs_write 40 static ssize_t sof_kernel_msg_inject_dfs_write(struct file *file, const char __user *buffer, 41 41 size_t count, loff_t *ppos) 42 { 42 { 43 struct sof_client_dev *cdev = file->pr 43 struct sof_client_dev *cdev = file->private_data; 44 struct sof_msg_inject_priv *priv = cde 44 struct sof_msg_inject_priv *priv = cdev->data; 45 struct sof_ipc_cmd_hdr *hdr = priv->ke 45 struct sof_ipc_cmd_hdr *hdr = priv->kernel_buffer; 46 struct device *dev = &cdev->auxdev.dev 46 struct device *dev = &cdev->auxdev.dev; 47 ssize_t size; 47 ssize_t size; 48 int ret; 48 int ret; 49 49 50 if (*ppos) 50 if (*ppos) 51 return 0; 51 return 0; 52 52 53 size = simple_write_to_buffer(priv->ke 53 size = simple_write_to_buffer(priv->kernel_buffer, priv->max_msg_size, 54 ppos, bu 54 ppos, buffer, count); 55 if (size < 0) 55 if (size < 0) 56 return size; 56 return size; 57 if (size != count) 57 if (size != count) 58 return -EFAULT; 58 return -EFAULT; 59 59 60 ret = pm_runtime_resume_and_get(dev); 60 ret = pm_runtime_resume_and_get(dev); 61 if (ret < 0 && ret != -EACCES) { 61 if (ret < 0 && ret != -EACCES) { 62 dev_err_ratelimited(dev, "debu 62 dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret); 63 return ret; 63 return ret; 64 } 64 } 65 65 66 sof_client_ipc_rx_message(cdev, hdr, p 66 sof_client_ipc_rx_message(cdev, hdr, priv->kernel_buffer); 67 67 68 pm_runtime_mark_last_busy(dev); 68 pm_runtime_mark_last_busy(dev); 69 ret = pm_runtime_put_autosuspend(dev); 69 ret = pm_runtime_put_autosuspend(dev); 70 if (ret < 0) 70 if (ret < 0) 71 dev_err_ratelimited(dev, "debu 71 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", ret); 72 72 73 return count; 73 return count; 74 }; 74 }; 75 75 76 static int sof_msg_inject_dfs_release(struct i 76 static int sof_msg_inject_dfs_release(struct inode *inode, struct file *file) 77 { 77 { 78 debugfs_file_put(file->f_path.dentry); 78 debugfs_file_put(file->f_path.dentry); 79 79 80 return 0; 80 return 0; 81 } 81 } 82 82 83 static const struct file_operations sof_kernel 83 static const struct file_operations sof_kernel_msg_inject_fops = { 84 .open = sof_msg_inject_dfs_open, 84 .open = sof_msg_inject_dfs_open, 85 .write = sof_kernel_msg_inject_dfs_wri 85 .write = sof_kernel_msg_inject_dfs_write, 86 .release = sof_msg_inject_dfs_release, 86 .release = sof_msg_inject_dfs_release, 87 87 88 .owner = THIS_MODULE, 88 .owner = THIS_MODULE, 89 }; 89 }; 90 90 91 static int sof_msg_inject_probe(struct auxilia 91 static int sof_msg_inject_probe(struct auxiliary_device *auxdev, 92 const struct a 92 const struct auxiliary_device_id *id) 93 { 93 { 94 struct sof_client_dev *cdev = auxiliar 94 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); 95 struct dentry *debugfs_root = sof_clie 95 struct dentry *debugfs_root = sof_client_get_debugfs_root(cdev); 96 struct device *dev = &auxdev->dev; 96 struct device *dev = &auxdev->dev; 97 struct sof_msg_inject_priv *priv; 97 struct sof_msg_inject_priv *priv; 98 size_t alloc_size; 98 size_t alloc_size; 99 99 100 /* allocate memory for client data */ 100 /* allocate memory for client data */ 101 priv = devm_kzalloc(&auxdev->dev, size 101 priv = devm_kzalloc(&auxdev->dev, sizeof(*priv), GFP_KERNEL); 102 if (!priv) 102 if (!priv) 103 return -ENOMEM; 103 return -ENOMEM; 104 104 105 priv->max_msg_size = sof_client_get_ip 105 priv->max_msg_size = sof_client_get_ipc_max_payload_size(cdev); 106 alloc_size = priv->max_msg_size; 106 alloc_size = priv->max_msg_size; 107 priv->kernel_buffer = devm_kmalloc(dev 107 priv->kernel_buffer = devm_kmalloc(dev, alloc_size, GFP_KERNEL); 108 108 109 if (!priv->kernel_buffer) 109 if (!priv->kernel_buffer) 110 return -ENOMEM; 110 return -ENOMEM; 111 111 112 cdev->data = priv; 112 cdev->data = priv; 113 113 114 priv->kernel_dfs_file = debugfs_create 114 priv->kernel_dfs_file = debugfs_create_file("kernel_ipc_msg_inject", 0644, 115 115 debugfs_root, cdev, 116 116 &sof_kernel_msg_inject_fops); 117 117 118 /* enable runtime PM */ 118 /* enable runtime PM */ 119 pm_runtime_set_autosuspend_delay(dev, 119 pm_runtime_set_autosuspend_delay(dev, SOF_IPC_CLIENT_SUSPEND_DELAY_MS); 120 pm_runtime_use_autosuspend(dev); 120 pm_runtime_use_autosuspend(dev); 121 pm_runtime_set_active(dev); 121 pm_runtime_set_active(dev); 122 pm_runtime_enable(dev); 122 pm_runtime_enable(dev); 123 pm_runtime_mark_last_busy(dev); 123 pm_runtime_mark_last_busy(dev); 124 pm_runtime_idle(dev); 124 pm_runtime_idle(dev); 125 125 126 return 0; 126 return 0; 127 } 127 } 128 128 129 static void sof_msg_inject_remove(struct auxil 129 static void sof_msg_inject_remove(struct auxiliary_device *auxdev) 130 { 130 { 131 struct sof_client_dev *cdev = auxiliar 131 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); 132 struct sof_msg_inject_priv *priv = cde 132 struct sof_msg_inject_priv *priv = cdev->data; 133 133 134 pm_runtime_disable(&auxdev->dev); 134 pm_runtime_disable(&auxdev->dev); 135 135 136 debugfs_remove(priv->kernel_dfs_file); 136 debugfs_remove(priv->kernel_dfs_file); 137 } 137 } 138 138 139 static const struct auxiliary_device_id sof_ms 139 static const struct auxiliary_device_id sof_msg_inject_client_id_table[] = { 140 { .name = "snd_sof.kernel_injector" }, 140 { .name = "snd_sof.kernel_injector" }, 141 {}, 141 {}, 142 }; 142 }; 143 MODULE_DEVICE_TABLE(auxiliary, sof_msg_inject_ 143 MODULE_DEVICE_TABLE(auxiliary, sof_msg_inject_client_id_table); 144 144 145 /* 145 /* 146 * No need for driver pm_ops as the generic pm 146 * No need for driver pm_ops as the generic pm callbacks in the auxiliary bus 147 * type are enough to ensure that the parent S 147 * type are enough to ensure that the parent SOF device resumes to bring the DSP 148 * back to D0. 148 * back to D0. 149 * Driver name will be set based on KBUILD_MOD 149 * Driver name will be set based on KBUILD_MODNAME. 150 */ 150 */ 151 static struct auxiliary_driver sof_msg_inject_ 151 static struct auxiliary_driver sof_msg_inject_client_drv = { 152 .probe = sof_msg_inject_probe, 152 .probe = sof_msg_inject_probe, 153 .remove = sof_msg_inject_remove, 153 .remove = sof_msg_inject_remove, 154 154 155 .id_table = sof_msg_inject_client_id_t 155 .id_table = sof_msg_inject_client_id_table, 156 }; 156 }; 157 157 158 module_auxiliary_driver(sof_msg_inject_client_ 158 module_auxiliary_driver(sof_msg_inject_client_drv); 159 159 160 MODULE_LICENSE("GPL"); 160 MODULE_LICENSE("GPL"); 161 MODULE_DESCRIPTION("SOF IPC Kernel Injector Cl 161 MODULE_DESCRIPTION("SOF IPC Kernel Injector Client Driver"); 162 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT); 162 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT); 163 163
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.