Skip to content

Commit 949d6d4

Browse files
committed
audio: don't override the "const" attribute
Using a type-cast to override a "const" attribute and modify read- only data is unsafe. Add a writable pointer to struct comp_driver_info instead and use it when creating component drivers dynamically. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent f694d20 commit 949d6d4

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/audio/component.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <rtos/alloc.h>
1414
#include <rtos/cache.h>
1515
#include <sof/lib/memory.h> /* for SHARED_DATA */
16+
#include <sof/lib/uuid.h>
1617
#include <sof/list.h>
1718
#include <rtos/sof.h>
1819
#include <rtos/string.h>
@@ -63,6 +64,25 @@ void comp_unregister(struct comp_driver_info *drv)
6364
k_spin_unlock(&drivers->lock, key);
6465
}
6566

67+
int comp_set_adapter_ops(const struct comp_driver *drv, const struct module_interface *ops)
68+
{
69+
struct comp_driver_list *drivers = comp_drivers_get();
70+
struct list_item *clist;
71+
72+
/* The list is only modified in IPC context, and we're in IPC context too */
73+
list_for_item(clist, &drivers->list) {
74+
struct comp_driver_info *info = container_of(clist, struct comp_driver_info, list);
75+
76+
if (!memcmp(info->drv->uid, drv->uid, UUID_SIZE)) {
77+
tr_dbg(&comp_tr, "update uuid %pU", info->drv->tctx->uuid_p);
78+
info->dyn_drv->adapter_ops = ops;
79+
return 0;
80+
}
81+
}
82+
83+
return -ENODEV;
84+
}
85+
6686
/* NOTE: Keep the component state diagram up to date:
6787
* sof-docs/developer_guides/firmware/components/images/comp-dev-states.pu
6888
*/

src/include/sof/audio/component.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ struct comp_driver {
595595
/** \brief Holds constant pointer to component driver */
596596
struct comp_driver_info {
597597
const struct comp_driver *drv; /**< pointer to component driver */
598+
struct comp_driver *dyn_drv; /**< pointer to dynamically created component driver -
599+
* same value as drv, but writable
600+
*/
598601
struct list_item list; /**< list of component drivers */
599602
};
600603

@@ -968,6 +971,15 @@ int comp_register(struct comp_driver_info *drv);
968971
*/
969972
void comp_unregister(struct comp_driver_info *drv);
970973

974+
/**
975+
* Set adapter ops for a dynamically created driver.
976+
*
977+
* @param drv Component driver to update.
978+
* @param ops Module interface operations.
979+
* @return 0 or a negative error code
980+
*/
981+
int comp_set_adapter_ops(const struct comp_driver *drv, const struct module_interface *ops);
982+
971983
/** @}*/
972984

973985
/**

src/library_manager/lib_manager.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,19 @@ static struct comp_dev *lib_manager_module_create(const struct comp_driver *drv,
518518
/* Intel modules expects DW size here */
519519
mod_cfg.size = args->size >> 2;
520520

521-
((struct comp_driver *)drv)->adapter_ops = native_system_agent_start(module_entry_point,
522-
module_id, instance_id,
523-
0, log_handle,
524-
&mod_cfg);
521+
const struct module_interface *ops = native_system_agent_start(module_entry_point,
522+
module_id, instance_id,
523+
0, log_handle, &mod_cfg);
525524

526-
if (!drv->adapter_ops) {
525+
if (!ops) {
527526
lib_manager_free_module(module_id);
528527
tr_err(&lib_manager_tr, "native_system_agent_start failed!");
529528
return NULL;
530529
}
531530

531+
if (comp_set_adapter_ops(drv, ops) < 0)
532+
return NULL;
533+
532534
dev = module_adapter_new(drv, config, spec);
533535
if (!dev)
534536
lib_manager_free_module(module_id);
@@ -662,6 +664,7 @@ int lib_manager_register_module(const uint32_t component_id)
662664

663665
/* Fill the new_drv_info structure with already known parameters */
664666
new_drv_info->drv = drv;
667+
new_drv_info->dyn_drv = drv;
665668

666669
/* Register new driver in the list */
667670
ret = comp_register(new_drv_info);

0 commit comments

Comments
 (0)