| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | // |
| 3 | // Copyright(c) 2022 Intel Corporation |
| 4 | // |
| 5 | // Authors: Ranjani Sridharan <ranjani.sridharan@linux.intel.com> |
| 6 | // Peter Ujfalusi <peter.ujfalusi@linux.intel.com> |
| 7 | // |
| 8 | |
| 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <sound/sof/ipc4/header.h> |
| 16 | #include "ops.h" |
| 17 | #include "sof-client.h" |
| 18 | #include "sof-priv.h" |
| 19 | #include "ipc3-priv.h" |
| 20 | #include "ipc4-priv.h" |
| 21 | |
| 22 | /** |
| 23 | * struct sof_ipc_event_entry - IPC client event description |
| 24 | * @ipc_msg_type: IPC msg type of the event the client is interested |
| 25 | * @cdev: sof_client_dev of the requesting client |
| 26 | * @callback: Callback function of the client |
| 27 | * @list: item in SOF core client event list |
| 28 | */ |
| 29 | struct sof_ipc_event_entry { |
| 30 | u32 ipc_msg_type; |
| 31 | struct sof_client_dev *cdev; |
| 32 | sof_client_event_callback callback; |
| 33 | struct list_head list; |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * struct sof_state_event_entry - DSP panic event subscription entry |
| 38 | * @cdev: sof_client_dev of the requesting client |
| 39 | * @callback: Callback function of the client |
| 40 | * @list: item in SOF core client event list |
| 41 | */ |
| 42 | struct sof_state_event_entry { |
| 43 | struct sof_client_dev *cdev; |
| 44 | sof_client_fw_state_callback callback; |
| 45 | struct list_head list; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * struct sof_client_dev_entry - client device entry for internal management use |
| 50 | * @sdev: pointer to SOF core device struct |
| 51 | * @list: item in SOF core client dev list |
| 52 | * @client_dev: SOF client device |
| 53 | */ |
| 54 | struct sof_client_dev_entry { |
| 55 | struct snd_sof_dev *sdev; |
| 56 | struct list_head list; |
| 57 | |
| 58 | struct sof_client_dev client_dev; |
| 59 | }; |
| 60 | |
| 61 | #define cdev_to_centry(cdev) \ |
| 62 | container_of(cdev, struct sof_client_dev_entry, client_dev) |
| 63 | |
| 64 | static void sof_client_auxdev_release(struct device *dev) |
| 65 | { |
| 66 | struct auxiliary_device *auxdev = to_auxiliary_dev(dev); |
| 67 | struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); |
| 68 | struct sof_client_dev_entry *centry = cdev_to_centry(cdev); |
| 69 | |
| 70 | kfree(objp: cdev->auxdev.dev.platform_data); |
| 71 | kfree(objp: centry); |
| 72 | } |
| 73 | |
| 74 | static int sof_client_dev_add_data(struct sof_client_dev *cdev, const void *data, |
| 75 | size_t size) |
| 76 | { |
| 77 | void *d = NULL; |
| 78 | |
| 79 | if (data) { |
| 80 | d = kmemdup(data, size, GFP_KERNEL); |
| 81 | if (!d) |
| 82 | return -ENOMEM; |
| 83 | } |
| 84 | |
| 85 | cdev->auxdev.dev.platform_data = d; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST) |
| 90 | static int sof_register_ipc_flood_test(struct snd_sof_dev *sdev) |
| 91 | { |
| 92 | int ret = 0; |
| 93 | int i; |
| 94 | |
| 95 | if (sdev->pdata->ipc_type != SOF_IPC_TYPE_3) |
| 96 | return 0; |
| 97 | |
| 98 | for (i = 0; i < CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST_NUM; i++) { |
| 99 | ret = sof_client_dev_register(sdev, name: "ipc_flood" , id: i, NULL, size: 0); |
| 100 | if (ret < 0) |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | if (ret) { |
| 105 | for (; i >= 0; --i) |
| 106 | sof_client_dev_unregister(sdev, name: "ipc_flood" , id: i); |
| 107 | } |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static void sof_unregister_ipc_flood_test(struct snd_sof_dev *sdev) |
| 113 | { |
| 114 | int i; |
| 115 | |
| 116 | for (i = 0; i < CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST_NUM; i++) |
| 117 | sof_client_dev_unregister(sdev, name: "ipc_flood" , id: i); |
| 118 | } |
| 119 | #else |
| 120 | static inline int sof_register_ipc_flood_test(struct snd_sof_dev *sdev) |
| 121 | { |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static inline void sof_unregister_ipc_flood_test(struct snd_sof_dev *sdev) {} |
| 126 | #endif /* CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST */ |
| 127 | |
| 128 | #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_MSG_INJECTOR) |
| 129 | static int sof_register_ipc_msg_injector(struct snd_sof_dev *sdev) |
| 130 | { |
| 131 | return sof_client_dev_register(sdev, name: "msg_injector" , id: 0, NULL, size: 0); |
| 132 | } |
| 133 | |
| 134 | static void sof_unregister_ipc_msg_injector(struct snd_sof_dev *sdev) |
| 135 | { |
| 136 | sof_client_dev_unregister(sdev, name: "msg_injector" , id: 0); |
| 137 | } |
| 138 | #else |
| 139 | static inline int sof_register_ipc_msg_injector(struct snd_sof_dev *sdev) |
| 140 | { |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static inline void sof_unregister_ipc_msg_injector(struct snd_sof_dev *sdev) {} |
| 145 | #endif /* CONFIG_SND_SOC_SOF_DEBUG_IPC_MSG_INJECTOR */ |
| 146 | |
| 147 | #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_KERNEL_INJECTOR) |
| 148 | static int sof_register_ipc_kernel_injector(struct snd_sof_dev *sdev) |
| 149 | { |
| 150 | /* Only IPC3 supported right now */ |
| 151 | if (sdev->pdata->ipc_type != SOF_IPC_TYPE_3) |
| 152 | return 0; |
| 153 | |
| 154 | return sof_client_dev_register(sdev, name: "kernel_injector" , id: 0, NULL, size: 0); |
| 155 | } |
| 156 | |
| 157 | static void sof_unregister_ipc_kernel_injector(struct snd_sof_dev *sdev) |
| 158 | { |
| 159 | sof_client_dev_unregister(sdev, name: "kernel_injector" , id: 0); |
| 160 | } |
| 161 | #else |
| 162 | static inline int sof_register_ipc_kernel_injector(struct snd_sof_dev *sdev) |
| 163 | { |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static inline void sof_unregister_ipc_kernel_injector(struct snd_sof_dev *sdev) {} |
| 168 | #endif /* CONFIG_SND_SOC_SOF_DEBUG_IPC_KERNEL_INJECTOR */ |
| 169 | |
| 170 | int sof_register_clients(struct snd_sof_dev *sdev) |
| 171 | { |
| 172 | int ret; |
| 173 | |
| 174 | if (sdev->dspless_mode_selected) |
| 175 | return 0; |
| 176 | |
| 177 | /* Register platform independent client devices */ |
| 178 | ret = sof_register_ipc_flood_test(sdev); |
| 179 | if (ret) { |
| 180 | dev_err(sdev->dev, "IPC flood test client registration failed\n" ); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | ret = sof_register_ipc_msg_injector(sdev); |
| 185 | if (ret) { |
| 186 | dev_err(sdev->dev, "IPC message injector client registration failed\n" ); |
| 187 | goto err_msg_injector; |
| 188 | } |
| 189 | |
| 190 | ret = sof_register_ipc_kernel_injector(sdev); |
| 191 | if (ret) { |
| 192 | dev_err(sdev->dev, "IPC kernel injector client registration failed\n" ); |
| 193 | goto err_kernel_injector; |
| 194 | } |
| 195 | |
| 196 | /* Platform dependent client device registration */ |
| 197 | |
| 198 | if (sof_ops(sdev) && sof_ops(sdev)->register_ipc_clients) |
| 199 | ret = sof_ops(sdev)->register_ipc_clients(sdev); |
| 200 | |
| 201 | if (!ret) |
| 202 | return 0; |
| 203 | |
| 204 | sof_unregister_ipc_kernel_injector(sdev); |
| 205 | |
| 206 | err_kernel_injector: |
| 207 | sof_unregister_ipc_msg_injector(sdev); |
| 208 | |
| 209 | err_msg_injector: |
| 210 | sof_unregister_ipc_flood_test(sdev); |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | void sof_unregister_clients(struct snd_sof_dev *sdev) |
| 216 | { |
| 217 | if (sof_ops(sdev) && sof_ops(sdev)->unregister_ipc_clients) |
| 218 | sof_ops(sdev)->unregister_ipc_clients(sdev); |
| 219 | |
| 220 | sof_unregister_ipc_kernel_injector(sdev); |
| 221 | sof_unregister_ipc_msg_injector(sdev); |
| 222 | sof_unregister_ipc_flood_test(sdev); |
| 223 | } |
| 224 | |
| 225 | int sof_client_dev_register(struct snd_sof_dev *sdev, const char *name, u32 id, |
| 226 | const void *data, size_t size) |
| 227 | { |
| 228 | struct sof_client_dev_entry *centry; |
| 229 | struct auxiliary_device *auxdev; |
| 230 | struct sof_client_dev *cdev; |
| 231 | int ret; |
| 232 | |
| 233 | centry = kzalloc(sizeof(*centry), GFP_KERNEL); |
| 234 | if (!centry) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | cdev = ¢ry->client_dev; |
| 238 | |
| 239 | centry->sdev = sdev; |
| 240 | auxdev = &cdev->auxdev; |
| 241 | auxdev->name = name; |
| 242 | auxdev->dev.parent = sdev->dev; |
| 243 | auxdev->dev.release = sof_client_auxdev_release; |
| 244 | auxdev->id = id; |
| 245 | |
| 246 | ret = sof_client_dev_add_data(cdev, data, size); |
| 247 | if (ret < 0) |
| 248 | goto err_dev_add_data; |
| 249 | |
| 250 | ret = auxiliary_device_init(auxdev); |
| 251 | if (ret < 0) { |
| 252 | dev_err(sdev->dev, "failed to initialize client dev %s.%d\n" , name, id); |
| 253 | goto err_dev_init; |
| 254 | } |
| 255 | |
| 256 | ret = auxiliary_device_add(&cdev->auxdev); |
| 257 | if (ret < 0) { |
| 258 | dev_err(sdev->dev, "failed to add client dev %s.%d\n" , name, id); |
| 259 | /* |
| 260 | * sof_client_auxdev_release() will be invoked to free up memory |
| 261 | * allocations through put_device() |
| 262 | */ |
| 263 | auxiliary_device_uninit(auxdev: &cdev->auxdev); |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | /* add to list of SOF client devices */ |
| 268 | mutex_lock(&sdev->ipc_client_mutex); |
| 269 | list_add(new: ¢ry->list, head: &sdev->ipc_client_list); |
| 270 | mutex_unlock(lock: &sdev->ipc_client_mutex); |
| 271 | |
| 272 | return 0; |
| 273 | |
| 274 | err_dev_init: |
| 275 | kfree(objp: cdev->auxdev.dev.platform_data); |
| 276 | |
| 277 | err_dev_add_data: |
| 278 | kfree(objp: centry); |
| 279 | |
| 280 | return ret; |
| 281 | } |
| 282 | EXPORT_SYMBOL_NS_GPL(sof_client_dev_register, "SND_SOC_SOF_CLIENT" ); |
| 283 | |
| 284 | void sof_client_dev_unregister(struct snd_sof_dev *sdev, const char *name, u32 id) |
| 285 | { |
| 286 | struct sof_client_dev_entry *centry; |
| 287 | |
| 288 | mutex_lock(&sdev->ipc_client_mutex); |
| 289 | |
| 290 | /* |
| 291 | * sof_client_auxdev_release() will be invoked to free up memory |
| 292 | * allocations through put_device() |
| 293 | */ |
| 294 | list_for_each_entry(centry, &sdev->ipc_client_list, list) { |
| 295 | struct sof_client_dev *cdev = ¢ry->client_dev; |
| 296 | |
| 297 | if (!strcmp(cdev->auxdev.name, name) && cdev->auxdev.id == id) { |
| 298 | list_del(entry: ¢ry->list); |
| 299 | auxiliary_device_delete(auxdev: &cdev->auxdev); |
| 300 | auxiliary_device_uninit(auxdev: &cdev->auxdev); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | mutex_unlock(lock: &sdev->ipc_client_mutex); |
| 306 | } |
| 307 | EXPORT_SYMBOL_NS_GPL(sof_client_dev_unregister, "SND_SOC_SOF_CLIENT" ); |
| 308 | |
| 309 | int sof_client_ipc_tx_message(struct sof_client_dev *cdev, void *ipc_msg, |
| 310 | void *reply_data, size_t reply_bytes) |
| 311 | { |
| 312 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 313 | |
| 314 | if (sdev->pdata->ipc_type == SOF_IPC_TYPE_3) { |
| 315 | struct sof_ipc_cmd_hdr *hdr = ipc_msg; |
| 316 | |
| 317 | return sof_ipc_tx_message(ipc: sdev->ipc, msg_data: ipc_msg, msg_bytes: hdr->size, |
| 318 | reply_data, reply_bytes); |
| 319 | } else if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { |
| 320 | struct sof_ipc4_msg *msg = ipc_msg; |
| 321 | |
| 322 | return sof_ipc_tx_message(ipc: sdev->ipc, msg_data: ipc_msg, msg_bytes: msg->data_size, |
| 323 | reply_data, reply_bytes); |
| 324 | } |
| 325 | |
| 326 | return -EINVAL; |
| 327 | } |
| 328 | EXPORT_SYMBOL_NS_GPL(sof_client_ipc_tx_message, "SND_SOC_SOF_CLIENT" ); |
| 329 | |
| 330 | int sof_client_ipc_rx_message(struct sof_client_dev *cdev, void *ipc_msg, void *msg_buf) |
| 331 | { |
| 332 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 333 | |
| 334 | if (IS_ENABLED(CONFIG_SND_SOC_SOF_IPC3) && |
| 335 | sdev->pdata->ipc_type == SOF_IPC_TYPE_3) { |
| 336 | struct sof_ipc_cmd_hdr *hdr = ipc_msg; |
| 337 | |
| 338 | if (hdr->size < sizeof(hdr)) { |
| 339 | dev_err(sdev->dev, "The received message size is invalid\n" ); |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | |
| 343 | sof_ipc3_do_rx_work(sdev, hdr: ipc_msg, msg_buf); |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | return -EOPNOTSUPP; |
| 348 | } |
| 349 | EXPORT_SYMBOL_NS_GPL(sof_client_ipc_rx_message, "SND_SOC_SOF_CLIENT" ); |
| 350 | |
| 351 | int sof_client_ipc_set_get_data(struct sof_client_dev *cdev, void *ipc_msg, |
| 352 | bool set) |
| 353 | { |
| 354 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 355 | |
| 356 | if (sdev->pdata->ipc_type == SOF_IPC_TYPE_3) { |
| 357 | struct sof_ipc_cmd_hdr *hdr = ipc_msg; |
| 358 | |
| 359 | return sof_ipc_set_get_data(ipc: sdev->ipc, msg_data: ipc_msg, msg_bytes: hdr->size, set); |
| 360 | } else if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { |
| 361 | struct sof_ipc4_msg *msg = ipc_msg; |
| 362 | |
| 363 | return sof_ipc_set_get_data(ipc: sdev->ipc, msg_data: ipc_msg, msg_bytes: msg->data_size, |
| 364 | set); |
| 365 | } |
| 366 | |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | EXPORT_SYMBOL_NS_GPL(sof_client_ipc_set_get_data, "SND_SOC_SOF_CLIENT" ); |
| 370 | |
| 371 | #ifdef CONFIG_SND_SOC_SOF_IPC4 |
| 372 | struct sof_ipc4_fw_module *sof_client_ipc4_find_module(struct sof_client_dev *c, const guid_t *uuid) |
| 373 | { |
| 374 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev: c); |
| 375 | |
| 376 | if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) |
| 377 | return sof_ipc4_find_module_by_uuid(sdev, uuid); |
| 378 | dev_err(sdev->dev, "Only supported with IPC4\n" ); |
| 379 | |
| 380 | return NULL; |
| 381 | } |
| 382 | EXPORT_SYMBOL_NS_GPL(sof_client_ipc4_find_module, "SND_SOC_SOF_CLIENT" ); |
| 383 | |
| 384 | struct snd_sof_widget *sof_client_ipc4_find_swidget_by_id(struct sof_client_dev *cdev, |
| 385 | u32 module_id, int instance_id) |
| 386 | { |
| 387 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 388 | |
| 389 | if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) |
| 390 | return sof_ipc4_find_swidget_by_ids(sdev, module_id, instance_id); |
| 391 | dev_err(sdev->dev, "Only supported with IPC4\n" ); |
| 392 | |
| 393 | return NULL; |
| 394 | } |
| 395 | EXPORT_SYMBOL_NS_GPL(sof_client_ipc4_find_swidget_by_id, "SND_SOC_SOF_CLIENT" ); |
| 396 | #endif |
| 397 | |
| 398 | int sof_suspend_clients(struct snd_sof_dev *sdev, pm_message_t state) |
| 399 | { |
| 400 | const struct auxiliary_driver *adrv; |
| 401 | struct sof_client_dev_entry *centry; |
| 402 | |
| 403 | mutex_lock(&sdev->ipc_client_mutex); |
| 404 | |
| 405 | list_for_each_entry(centry, &sdev->ipc_client_list, list) { |
| 406 | struct sof_client_dev *cdev = ¢ry->client_dev; |
| 407 | |
| 408 | /* Skip devices without loaded driver */ |
| 409 | if (!cdev->auxdev.dev.driver) |
| 410 | continue; |
| 411 | |
| 412 | adrv = to_auxiliary_drv(drv: cdev->auxdev.dev.driver); |
| 413 | if (adrv->suspend) |
| 414 | adrv->suspend(&cdev->auxdev, state); |
| 415 | } |
| 416 | |
| 417 | mutex_unlock(lock: &sdev->ipc_client_mutex); |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | EXPORT_SYMBOL_NS_GPL(sof_suspend_clients, "SND_SOC_SOF_CLIENT" ); |
| 422 | |
| 423 | int sof_resume_clients(struct snd_sof_dev *sdev) |
| 424 | { |
| 425 | const struct auxiliary_driver *adrv; |
| 426 | struct sof_client_dev_entry *centry; |
| 427 | |
| 428 | mutex_lock(&sdev->ipc_client_mutex); |
| 429 | |
| 430 | list_for_each_entry(centry, &sdev->ipc_client_list, list) { |
| 431 | struct sof_client_dev *cdev = ¢ry->client_dev; |
| 432 | |
| 433 | /* Skip devices without loaded driver */ |
| 434 | if (!cdev->auxdev.dev.driver) |
| 435 | continue; |
| 436 | |
| 437 | adrv = to_auxiliary_drv(drv: cdev->auxdev.dev.driver); |
| 438 | if (adrv->resume) |
| 439 | adrv->resume(&cdev->auxdev); |
| 440 | } |
| 441 | |
| 442 | mutex_unlock(lock: &sdev->ipc_client_mutex); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | EXPORT_SYMBOL_NS_GPL(sof_resume_clients, "SND_SOC_SOF_CLIENT" ); |
| 447 | |
| 448 | struct dentry *sof_client_get_debugfs_root(struct sof_client_dev *cdev) |
| 449 | { |
| 450 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 451 | |
| 452 | return sdev->debugfs_root; |
| 453 | } |
| 454 | EXPORT_SYMBOL_NS_GPL(sof_client_get_debugfs_root, "SND_SOC_SOF_CLIENT" ); |
| 455 | |
| 456 | /* DMA buffer allocation in client drivers must use the core SOF device */ |
| 457 | struct device *sof_client_get_dma_dev(struct sof_client_dev *cdev) |
| 458 | { |
| 459 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 460 | |
| 461 | return sdev->dev; |
| 462 | } |
| 463 | EXPORT_SYMBOL_NS_GPL(sof_client_get_dma_dev, "SND_SOC_SOF_CLIENT" ); |
| 464 | |
| 465 | const struct sof_ipc_fw_version *sof_client_get_fw_version(struct sof_client_dev *cdev) |
| 466 | { |
| 467 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 468 | |
| 469 | return &sdev->fw_ready.version; |
| 470 | } |
| 471 | EXPORT_SYMBOL_NS_GPL(sof_client_get_fw_version, "SND_SOC_SOF_CLIENT" ); |
| 472 | |
| 473 | size_t sof_client_get_ipc_max_payload_size(struct sof_client_dev *cdev) |
| 474 | { |
| 475 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 476 | |
| 477 | return sdev->ipc->max_payload_size; |
| 478 | } |
| 479 | EXPORT_SYMBOL_NS_GPL(sof_client_get_ipc_max_payload_size, "SND_SOC_SOF_CLIENT" ); |
| 480 | |
| 481 | enum sof_ipc_type sof_client_get_ipc_type(struct sof_client_dev *cdev) |
| 482 | { |
| 483 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 484 | |
| 485 | return sdev->pdata->ipc_type; |
| 486 | } |
| 487 | EXPORT_SYMBOL_NS_GPL(sof_client_get_ipc_type, "SND_SOC_SOF_CLIENT" ); |
| 488 | |
| 489 | /* module refcount management of SOF core */ |
| 490 | int sof_client_core_module_get(struct sof_client_dev *cdev) |
| 491 | { |
| 492 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 493 | |
| 494 | if (!try_module_get(module: sdev->dev->driver->owner)) |
| 495 | return -ENODEV; |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | EXPORT_SYMBOL_NS_GPL(sof_client_core_module_get, "SND_SOC_SOF_CLIENT" ); |
| 500 | |
| 501 | void sof_client_core_module_put(struct sof_client_dev *cdev) |
| 502 | { |
| 503 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 504 | |
| 505 | module_put(module: sdev->dev->driver->owner); |
| 506 | } |
| 507 | EXPORT_SYMBOL_NS_GPL(sof_client_core_module_put, "SND_SOC_SOF_CLIENT" ); |
| 508 | |
| 509 | /* IPC event handling */ |
| 510 | void sof_client_ipc_rx_dispatcher(struct snd_sof_dev *sdev, void *msg_buf) |
| 511 | { |
| 512 | struct sof_ipc_event_entry *event; |
| 513 | u32 msg_type; |
| 514 | |
| 515 | if (sdev->pdata->ipc_type == SOF_IPC_TYPE_3) { |
| 516 | struct sof_ipc_cmd_hdr *hdr = msg_buf; |
| 517 | |
| 518 | msg_type = hdr->cmd & SOF_GLB_TYPE_MASK; |
| 519 | } else if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { |
| 520 | struct sof_ipc4_msg *msg = msg_buf; |
| 521 | |
| 522 | msg_type = SOF_IPC4_NOTIFICATION_TYPE_GET(msg->primary); |
| 523 | } else { |
| 524 | dev_dbg_once(sdev->dev, "Not supported IPC version: %d\n" , |
| 525 | sdev->pdata->ipc_type); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | mutex_lock(&sdev->client_event_handler_mutex); |
| 530 | |
| 531 | list_for_each_entry(event, &sdev->ipc_rx_handler_list, list) { |
| 532 | if (event->ipc_msg_type == msg_type) |
| 533 | event->callback(event->cdev, msg_buf); |
| 534 | } |
| 535 | |
| 536 | mutex_unlock(lock: &sdev->client_event_handler_mutex); |
| 537 | } |
| 538 | |
| 539 | int sof_client_register_ipc_rx_handler(struct sof_client_dev *cdev, |
| 540 | u32 ipc_msg_type, |
| 541 | sof_client_event_callback callback) |
| 542 | { |
| 543 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 544 | struct sof_ipc_event_entry *event; |
| 545 | |
| 546 | if (!callback) |
| 547 | return -EINVAL; |
| 548 | |
| 549 | if (sdev->pdata->ipc_type == SOF_IPC_TYPE_3) { |
| 550 | if (!(ipc_msg_type & SOF_GLB_TYPE_MASK)) |
| 551 | return -EINVAL; |
| 552 | } else if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { |
| 553 | if (!(ipc_msg_type & SOF_IPC4_NOTIFICATION_TYPE_MASK)) |
| 554 | return -EINVAL; |
| 555 | } else { |
| 556 | dev_warn(sdev->dev, "%s: Not supported IPC version: %d\n" , |
| 557 | __func__, sdev->pdata->ipc_type); |
| 558 | return -EINVAL; |
| 559 | } |
| 560 | |
| 561 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 562 | if (!event) |
| 563 | return -ENOMEM; |
| 564 | |
| 565 | event->ipc_msg_type = ipc_msg_type; |
| 566 | event->cdev = cdev; |
| 567 | event->callback = callback; |
| 568 | |
| 569 | /* add to list of SOF client devices */ |
| 570 | mutex_lock(&sdev->client_event_handler_mutex); |
| 571 | list_add(new: &event->list, head: &sdev->ipc_rx_handler_list); |
| 572 | mutex_unlock(lock: &sdev->client_event_handler_mutex); |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | EXPORT_SYMBOL_NS_GPL(sof_client_register_ipc_rx_handler, "SND_SOC_SOF_CLIENT" ); |
| 577 | |
| 578 | void sof_client_unregister_ipc_rx_handler(struct sof_client_dev *cdev, |
| 579 | u32 ipc_msg_type) |
| 580 | { |
| 581 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 582 | struct sof_ipc_event_entry *event; |
| 583 | |
| 584 | mutex_lock(&sdev->client_event_handler_mutex); |
| 585 | |
| 586 | list_for_each_entry(event, &sdev->ipc_rx_handler_list, list) { |
| 587 | if (event->cdev == cdev && event->ipc_msg_type == ipc_msg_type) { |
| 588 | list_del(entry: &event->list); |
| 589 | kfree(objp: event); |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | mutex_unlock(lock: &sdev->client_event_handler_mutex); |
| 595 | } |
| 596 | EXPORT_SYMBOL_NS_GPL(sof_client_unregister_ipc_rx_handler, "SND_SOC_SOF_CLIENT" ); |
| 597 | |
| 598 | /*DSP state notification and query */ |
| 599 | void sof_client_fw_state_dispatcher(struct snd_sof_dev *sdev) |
| 600 | { |
| 601 | struct sof_state_event_entry *event; |
| 602 | |
| 603 | mutex_lock(&sdev->client_event_handler_mutex); |
| 604 | |
| 605 | list_for_each_entry(event, &sdev->fw_state_handler_list, list) |
| 606 | event->callback(event->cdev, sdev->fw_state); |
| 607 | |
| 608 | mutex_unlock(lock: &sdev->client_event_handler_mutex); |
| 609 | } |
| 610 | |
| 611 | int sof_client_register_fw_state_handler(struct sof_client_dev *cdev, |
| 612 | sof_client_fw_state_callback callback) |
| 613 | { |
| 614 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 615 | struct sof_state_event_entry *event; |
| 616 | |
| 617 | if (!callback) |
| 618 | return -EINVAL; |
| 619 | |
| 620 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 621 | if (!event) |
| 622 | return -ENOMEM; |
| 623 | |
| 624 | event->cdev = cdev; |
| 625 | event->callback = callback; |
| 626 | |
| 627 | /* add to list of SOF client devices */ |
| 628 | mutex_lock(&sdev->client_event_handler_mutex); |
| 629 | list_add(new: &event->list, head: &sdev->fw_state_handler_list); |
| 630 | mutex_unlock(lock: &sdev->client_event_handler_mutex); |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | EXPORT_SYMBOL_NS_GPL(sof_client_register_fw_state_handler, "SND_SOC_SOF_CLIENT" ); |
| 635 | |
| 636 | void sof_client_unregister_fw_state_handler(struct sof_client_dev *cdev) |
| 637 | { |
| 638 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 639 | struct sof_state_event_entry *event; |
| 640 | |
| 641 | mutex_lock(&sdev->client_event_handler_mutex); |
| 642 | |
| 643 | list_for_each_entry(event, &sdev->fw_state_handler_list, list) { |
| 644 | if (event->cdev == cdev) { |
| 645 | list_del(entry: &event->list); |
| 646 | kfree(objp: event); |
| 647 | break; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | mutex_unlock(lock: &sdev->client_event_handler_mutex); |
| 652 | } |
| 653 | EXPORT_SYMBOL_NS_GPL(sof_client_unregister_fw_state_handler, "SND_SOC_SOF_CLIENT" ); |
| 654 | |
| 655 | enum sof_fw_state sof_client_get_fw_state(struct sof_client_dev *cdev) |
| 656 | { |
| 657 | struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); |
| 658 | |
| 659 | return sdev->fw_state; |
| 660 | } |
| 661 | EXPORT_SYMBOL_NS_GPL(sof_client_get_fw_state, "SND_SOC_SOF_CLIENT" ); |
| 662 | |
| 663 | struct snd_sof_dev *sof_client_dev_to_sof_dev(struct sof_client_dev *cdev) |
| 664 | { |
| 665 | struct sof_client_dev_entry *centry = cdev_to_centry(cdev); |
| 666 | |
| 667 | return centry->sdev; |
| 668 | } |
| 669 | EXPORT_SYMBOL_NS_GPL(sof_client_dev_to_sof_dev, "SND_SOC_SOF_CLIENT" ); |
| 670 | |