2017-11-07 13:58:41 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-01-13 21:07:48 +00:00
|
|
|
/*
|
|
|
|
* Greybus operations
|
|
|
|
*
|
|
|
|
* Copyright 2015-2016 Google Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/sysfs.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
2017-10-05 12:56:54 +00:00
|
|
|
#include <linux/spinlock.h>
|
2016-02-03 11:53:40 +00:00
|
|
|
#include <linux/idr.h>
|
2016-01-13 21:07:48 +00:00
|
|
|
|
|
|
|
#include "audio_manager.h"
|
|
|
|
#include "audio_manager_private.h"
|
|
|
|
|
|
|
|
static struct kset *manager_kset;
|
|
|
|
|
|
|
|
static LIST_HEAD(modules_list);
|
2016-05-04 10:59:19 +00:00
|
|
|
static DECLARE_RWSEM(modules_rwsem);
|
2016-01-26 00:52:17 +00:00
|
|
|
static DEFINE_IDA(module_id);
|
2016-01-13 21:07:48 +00:00
|
|
|
|
|
|
|
/* helpers */
|
|
|
|
static struct gb_audio_manager_module *gb_audio_manager_get_locked(int id)
|
|
|
|
{
|
|
|
|
struct gb_audio_manager_module *module;
|
|
|
|
|
|
|
|
if (id < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list_for_each_entry(module, &modules_list, list) {
|
|
|
|
if (module->id == id)
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public API */
|
|
|
|
int gb_audio_manager_add(struct gb_audio_manager_module_descriptor *desc)
|
|
|
|
{
|
|
|
|
struct gb_audio_manager_module *module;
|
2016-01-26 00:52:17 +00:00
|
|
|
int id;
|
2016-01-13 21:07:48 +00:00
|
|
|
int err;
|
|
|
|
|
2016-01-26 00:52:17 +00:00
|
|
|
id = ida_simple_get(&module_id, 0, 0, GFP_KERNEL);
|
2019-03-14 06:45:24 +00:00
|
|
|
if (id < 0)
|
|
|
|
return id;
|
|
|
|
|
2016-01-13 21:07:48 +00:00
|
|
|
err = gb_audio_manager_module_create(&module, manager_kset,
|
2016-01-26 00:52:17 +00:00
|
|
|
id, desc);
|
|
|
|
if (err) {
|
|
|
|
ida_simple_remove(&module_id, id);
|
2016-01-13 21:07:48 +00:00
|
|
|
return err;
|
2016-01-26 00:52:17 +00:00
|
|
|
}
|
2016-01-13 21:07:48 +00:00
|
|
|
|
|
|
|
/* Add it to the list */
|
2016-05-04 10:59:19 +00:00
|
|
|
down_write(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
list_add_tail(&module->list, &modules_list);
|
2016-05-04 10:59:19 +00:00
|
|
|
up_write(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
|
|
|
|
return module->id;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gb_audio_manager_add);
|
|
|
|
|
|
|
|
int gb_audio_manager_remove(int id)
|
|
|
|
{
|
|
|
|
struct gb_audio_manager_module *module;
|
|
|
|
|
2016-05-04 10:59:19 +00:00
|
|
|
down_write(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
|
|
|
|
module = gb_audio_manager_get_locked(id);
|
|
|
|
if (!module) {
|
2016-05-04 10:59:19 +00:00
|
|
|
up_write(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
list_del(&module->list);
|
|
|
|
kobject_put(&module->kobj);
|
2016-05-04 10:59:19 +00:00
|
|
|
up_write(&modules_rwsem);
|
2016-05-17 16:59:08 +00:00
|
|
|
ida_simple_remove(&module_id, id);
|
2016-01-13 21:07:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gb_audio_manager_remove);
|
|
|
|
|
|
|
|
void gb_audio_manager_remove_all(void)
|
|
|
|
{
|
|
|
|
struct gb_audio_manager_module *module, *next;
|
2019-07-04 13:30:31 +00:00
|
|
|
int is_empty;
|
2016-01-13 21:07:48 +00:00
|
|
|
|
2016-05-04 10:59:19 +00:00
|
|
|
down_write(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
|
|
|
|
list_for_each_entry_safe(module, next, &modules_list, list) {
|
|
|
|
list_del(&module->list);
|
2016-05-05 16:58:22 +00:00
|
|
|
ida_simple_remove(&module_id, module->id);
|
2020-02-05 12:32:17 +00:00
|
|
|
kobject_put(&module->kobj);
|
2016-01-13 21:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
is_empty = list_empty(&modules_list);
|
|
|
|
|
2016-05-04 10:59:19 +00:00
|
|
|
up_write(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
|
|
|
|
if (!is_empty)
|
|
|
|
pr_warn("Not all nodes were deleted\n");
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gb_audio_manager_remove_all);
|
|
|
|
|
|
|
|
struct gb_audio_manager_module *gb_audio_manager_get_module(int id)
|
|
|
|
{
|
|
|
|
struct gb_audio_manager_module *module;
|
|
|
|
|
2016-05-04 10:59:19 +00:00
|
|
|
down_read(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
module = gb_audio_manager_get_locked(id);
|
|
|
|
kobject_get(&module->kobj);
|
2016-05-04 10:59:19 +00:00
|
|
|
up_read(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
return module;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gb_audio_manager_get_module);
|
|
|
|
|
|
|
|
void gb_audio_manager_put_module(struct gb_audio_manager_module *module)
|
|
|
|
{
|
|
|
|
kobject_put(&module->kobj);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gb_audio_manager_put_module);
|
|
|
|
|
|
|
|
int gb_audio_manager_dump_module(int id)
|
|
|
|
{
|
|
|
|
struct gb_audio_manager_module *module;
|
|
|
|
|
2016-05-04 10:59:19 +00:00
|
|
|
down_read(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
module = gb_audio_manager_get_locked(id);
|
2016-05-04 10:59:19 +00:00
|
|
|
up_read(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
|
|
|
|
if (!module)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
gb_audio_manager_module_dump(module);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gb_audio_manager_dump_module);
|
|
|
|
|
|
|
|
void gb_audio_manager_dump_all(void)
|
|
|
|
{
|
|
|
|
struct gb_audio_manager_module *module;
|
|
|
|
int count = 0;
|
|
|
|
|
2016-05-04 10:59:19 +00:00
|
|
|
down_read(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
list_for_each_entry(module, &modules_list, list) {
|
|
|
|
gb_audio_manager_module_dump(module);
|
|
|
|
count++;
|
|
|
|
}
|
2016-05-04 10:59:19 +00:00
|
|
|
up_read(&modules_rwsem);
|
2016-01-13 21:07:48 +00:00
|
|
|
|
|
|
|
pr_info("Number of connected modules: %d\n", count);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gb_audio_manager_dump_all);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* module init/deinit
|
|
|
|
*/
|
|
|
|
static int __init manager_init(void)
|
|
|
|
{
|
|
|
|
manager_kset = kset_create_and_add(GB_AUDIO_MANAGER_NAME, NULL,
|
|
|
|
kernel_kobj);
|
|
|
|
if (!manager_kset)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
#ifdef GB_AUDIO_MANAGER_SYSFS
|
|
|
|
gb_audio_manager_sysfs_init(&manager_kset->kobj);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit manager_exit(void)
|
|
|
|
{
|
|
|
|
gb_audio_manager_remove_all();
|
|
|
|
kset_unregister(manager_kset);
|
2016-01-26 00:52:17 +00:00
|
|
|
ida_destroy(&module_id);
|
2016-01-13 21:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(manager_init);
|
|
|
|
module_exit(manager_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Svetlin Ankov <ankov_svetlin@projectara.com>");
|