Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Greybus operations |
| 3 | * |
| 4 | * Copyright 2015-2016 Google Inc. |
| 5 | * |
| 6 | * Released under the GPLv2 only. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/string.h> |
| 10 | #include <linux/sysfs.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/rwlock.h> |
Bartosz Golaszewski | ec41356 | 2016-02-03 12:53:40 +0100 | [diff] [blame] | 14 | #include <linux/idr.h> |
Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 15 | |
| 16 | #include "audio_manager.h" |
| 17 | #include "audio_manager_private.h" |
| 18 | |
| 19 | static struct kset *manager_kset; |
| 20 | |
| 21 | static LIST_HEAD(modules_list); |
| 22 | static DEFINE_RWLOCK(modules_lock); |
Greg Kroah-Hartman | 3c5de59 | 2016-01-25 16:52:17 -0800 | [diff] [blame] | 23 | static DEFINE_IDA(module_id); |
Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 24 | |
| 25 | /* helpers */ |
| 26 | static struct gb_audio_manager_module *gb_audio_manager_get_locked(int id) |
| 27 | { |
| 28 | struct gb_audio_manager_module *module; |
| 29 | |
| 30 | if (id < 0) |
| 31 | return NULL; |
| 32 | |
| 33 | list_for_each_entry(module, &modules_list, list) { |
| 34 | if (module->id == id) |
| 35 | return module; |
| 36 | } |
| 37 | |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | /* public API */ |
| 42 | int gb_audio_manager_add(struct gb_audio_manager_module_descriptor *desc) |
| 43 | { |
| 44 | struct gb_audio_manager_module *module; |
| 45 | unsigned long flags; |
Greg Kroah-Hartman | 3c5de59 | 2016-01-25 16:52:17 -0800 | [diff] [blame] | 46 | int id; |
Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 47 | int err; |
| 48 | |
Greg Kroah-Hartman | 3c5de59 | 2016-01-25 16:52:17 -0800 | [diff] [blame] | 49 | id = ida_simple_get(&module_id, 0, 0, GFP_KERNEL); |
Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 50 | err = gb_audio_manager_module_create(&module, manager_kset, |
Greg Kroah-Hartman | 3c5de59 | 2016-01-25 16:52:17 -0800 | [diff] [blame] | 51 | id, desc); |
| 52 | if (err) { |
| 53 | ida_simple_remove(&module_id, id); |
Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 54 | return err; |
Greg Kroah-Hartman | 3c5de59 | 2016-01-25 16:52:17 -0800 | [diff] [blame] | 55 | } |
Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 56 | |
| 57 | /* Add it to the list */ |
| 58 | write_lock_irqsave(&modules_lock, flags); |
| 59 | list_add_tail(&module->list, &modules_list); |
| 60 | write_unlock_irqrestore(&modules_lock, flags); |
| 61 | |
| 62 | return module->id; |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(gb_audio_manager_add); |
| 65 | |
| 66 | int gb_audio_manager_remove(int id) |
| 67 | { |
| 68 | struct gb_audio_manager_module *module; |
| 69 | unsigned long flags; |
| 70 | |
| 71 | write_lock_irqsave(&modules_lock, flags); |
| 72 | |
| 73 | module = gb_audio_manager_get_locked(id); |
| 74 | if (!module) { |
| 75 | write_unlock_irqrestore(&modules_lock, flags); |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
Greg Kroah-Hartman | 3c5de59 | 2016-01-25 16:52:17 -0800 | [diff] [blame] | 79 | ida_simple_remove(&module_id, module->id); |
Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 80 | list_del(&module->list); |
| 81 | kobject_put(&module->kobj); |
| 82 | write_unlock_irqrestore(&modules_lock, flags); |
| 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(gb_audio_manager_remove); |
| 86 | |
| 87 | void gb_audio_manager_remove_all(void) |
| 88 | { |
| 89 | struct gb_audio_manager_module *module, *next; |
| 90 | int is_empty = 1; |
| 91 | unsigned long flags; |
| 92 | |
| 93 | write_lock_irqsave(&modules_lock, flags); |
| 94 | |
| 95 | list_for_each_entry_safe(module, next, &modules_list, list) { |
| 96 | list_del(&module->list); |
| 97 | kobject_put(&module->kobj); |
| 98 | } |
| 99 | |
| 100 | is_empty = list_empty(&modules_list); |
| 101 | |
| 102 | write_unlock_irqrestore(&modules_lock, flags); |
| 103 | |
| 104 | if (!is_empty) |
| 105 | pr_warn("Not all nodes were deleted\n"); |
| 106 | } |
| 107 | EXPORT_SYMBOL_GPL(gb_audio_manager_remove_all); |
| 108 | |
| 109 | struct gb_audio_manager_module *gb_audio_manager_get_module(int id) |
| 110 | { |
| 111 | struct gb_audio_manager_module *module; |
| 112 | unsigned long flags; |
| 113 | |
| 114 | read_lock_irqsave(&modules_lock, flags); |
| 115 | module = gb_audio_manager_get_locked(id); |
| 116 | kobject_get(&module->kobj); |
| 117 | read_unlock_irqrestore(&modules_lock, flags); |
| 118 | return module; |
| 119 | } |
| 120 | EXPORT_SYMBOL_GPL(gb_audio_manager_get_module); |
| 121 | |
| 122 | void gb_audio_manager_put_module(struct gb_audio_manager_module *module) |
| 123 | { |
| 124 | kobject_put(&module->kobj); |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(gb_audio_manager_put_module); |
| 127 | |
| 128 | int gb_audio_manager_dump_module(int id) |
| 129 | { |
| 130 | struct gb_audio_manager_module *module; |
| 131 | unsigned long flags; |
| 132 | |
| 133 | read_lock_irqsave(&modules_lock, flags); |
| 134 | module = gb_audio_manager_get_locked(id); |
| 135 | read_unlock_irqrestore(&modules_lock, flags); |
| 136 | |
| 137 | if (!module) |
| 138 | return -EINVAL; |
| 139 | |
| 140 | gb_audio_manager_module_dump(module); |
| 141 | return 0; |
| 142 | } |
| 143 | EXPORT_SYMBOL_GPL(gb_audio_manager_dump_module); |
| 144 | |
| 145 | void gb_audio_manager_dump_all(void) |
| 146 | { |
| 147 | struct gb_audio_manager_module *module; |
| 148 | int count = 0; |
| 149 | unsigned long flags; |
| 150 | |
| 151 | read_lock_irqsave(&modules_lock, flags); |
| 152 | list_for_each_entry(module, &modules_list, list) { |
| 153 | gb_audio_manager_module_dump(module); |
| 154 | count++; |
| 155 | } |
| 156 | read_unlock_irqrestore(&modules_lock, flags); |
| 157 | |
| 158 | pr_info("Number of connected modules: %d\n", count); |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(gb_audio_manager_dump_all); |
| 161 | |
| 162 | /* |
| 163 | * module init/deinit |
| 164 | */ |
| 165 | static int __init manager_init(void) |
| 166 | { |
| 167 | manager_kset = kset_create_and_add(GB_AUDIO_MANAGER_NAME, NULL, |
| 168 | kernel_kobj); |
| 169 | if (!manager_kset) |
| 170 | return -ENOMEM; |
| 171 | |
| 172 | #ifdef GB_AUDIO_MANAGER_SYSFS |
| 173 | gb_audio_manager_sysfs_init(&manager_kset->kobj); |
| 174 | #endif |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static void __exit manager_exit(void) |
| 180 | { |
| 181 | gb_audio_manager_remove_all(); |
| 182 | kset_unregister(manager_kset); |
Greg Kroah-Hartman | 3c5de59 | 2016-01-25 16:52:17 -0800 | [diff] [blame] | 183 | ida_destroy(&module_id); |
Svetlin Ankov | 8db0073 | 2016-01-13 14:07:48 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | module_init(manager_init); |
| 187 | module_exit(manager_exit); |
| 188 | |
| 189 | MODULE_LICENSE("GPL"); |
| 190 | MODULE_AUTHOR("Svetlin Ankov <ankov_svetlin@projectara.com>"); |