1 /* 2 * Copyright (c) 2018 The Linux Foundation. All rights reserved. 3 * 4 * Permission to use, copy, modify, and/or distribute this software for 5 * any purpose with or without fee is hereby granted, provided that the 6 * above copyright notice and this permission notice appear in all 7 * copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 * PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /** 20 * DOC: UCFG APIs for the configuration component. 21 * 22 * Logically, configuration exists at the psoc level. This means, each psoc can 23 * have its own custom configuration, and calls to lookup configuration take a 24 * psoc parameter for reference. E.g. 25 * 26 * int32_t value = cfg_get(psoc, WLAN_SOME_INTEGER_CONFIG_ID); 27 * 28 * Configuration is cascading, and lookups happen in this order: 29 * 30 * 1) use psoc value, if configured 31 * 2) use global value, if configured 32 * 3) fallback to the default value for the configuration item 33 * 34 * This means a psoc configuration is a specialization of the global 35 * configuration, and does not need to explicitly set the same values if they 36 * would match the global config. 37 * 38 * In order to load and parse the global config, call cfg_parse(). In order to 39 * load and parse psoc configs, call cfg_psoc_parse(). cfg_parse() MUST be 40 * called before cfg_psoc_parse(), as global configuration will be consulted 41 * during the psoc parsing process. 42 * 43 * There are two basic lifecycles supported: 44 * 45 * 1) The type and number of psocs is *not* known at load time 46 * 47 * // driver is loading 48 * cfg_parse("/path/to/config"); 49 * 50 * ... 51 * 52 * // a psoc has just been created 53 * cfg_psoc_parse(psoc, "/path/to/psoc/config"); 54 * 55 * ... 56 * 57 * // driver is unloading 58 * cfg_release(); 59 * 60 * 2) The type and number of psocs *is* known at load time 61 * 62 * // driver is loading 63 * cfg_parse("/path/to/config"); 64 * 65 * ... 66 * 67 * // for each psoc 68 * cfg_psoc_parse(psoc, "/path/to/psoc/config"); 69 * 70 * // no further psocs will be created after this point 71 * cfg_release(); 72 * 73 * ... 74 * 75 * // driver is unloaded later 76 * 77 * Each configuration store is reference counted to reduce memory footprint, and 78 * the configuration component itself will hold one ref count on the global 79 * config store. All psocs for which psoc-specific configurations have *not* 80 * been provided will reference the global config store. Psocs for which psoc- 81 * specific configurations *have* been provded will check for existings stores 82 * with a matching path to use, before parsing the specified configuration file. 83 * 84 * If, at some point in time, it is known that no further psocs will ever be 85 * created, a call to cfg_release() will release the global ref count held by 86 * the configuration component. For systems which specify psoc-specific configs 87 * for all psocs, this will release the unnecessary memory used by the global 88 * config store. Otherwise, calling cfg_release() at unload time will ensure 89 * the global config store is properly freed. 90 */ 91 92 #ifndef __CFG_UCFG_H 93 #define __CFG_UCFG_H 94 95 #include "cfg_all.h" 96 #include "cfg_define.h" 97 #include "i_cfg.h" 98 #include "qdf_status.h" 99 #include "qdf_str.h" 100 #include "qdf_types.h" 101 #include "wlan_objmgr_psoc_obj.h" 102 103 /** 104 * cfg_parse() - parse an ini file, and populate the global config storei 105 * @path: The full file path of the ini file to parse 106 * 107 * Note: A matching cfg_release() call is required to release allocated 108 * resources. 109 * 110 * The *.ini file format is a simple format consiting of a list of key/value 111 * pairs, separated by an '=' character. e.g. 112 * 113 * gConfigItem1=some string value 114 * gConfigItem2=0xabc 115 * 116 * Comments are also supported, initiated with the '#' character: 117 * 118 * # This is a comment. It will be ignored by the *.ini parser 119 * gConfigItem3=aa:bb:cc:dd:ee:ff # this is also a comment 120 * 121 * Several datatypes are natively supported: 122 * 123 * gInt=-123 # bin (0b), octal (0o), hex (0x), and decimal supported 124 * gUint=123 # a non-negative integer value 125 * gBool=y # (1, Y, y) -> true; (0, N, n) -> false 126 * gString=any string # strings are useful for representing complex types 127 * gMacAddr=aa:bb:cc:dd:ee:ff # colons are optional, upper and lower case 128 * gIpv4Addr=127.0.0.1 # uses typical dot-decimal notation 129 * gIpv6Addr=::1 # typical notation, supporting zero-compression 130 * 131 * Return: QDF_STATUS 132 */ 133 QDF_STATUS cfg_parse(const char *path); 134 135 /** 136 * cfg_release() - release the global configuration store 137 * 138 * This API releases the configuration component's reference to the global 139 * config store. 140 * 141 * See also: this file's DOC section. 142 * 143 * Return: None 144 */ 145 void cfg_release(void); 146 147 /** 148 * cfg_psoc_parse() - specialize the config store for @psoc by parsing @path 149 * @psoc: The psoc whose config store should be specialized 150 * @path: The full file path of the ini file to parse 151 * 152 * See also: cfg_parse(), and this file's DOC section. 153 * 154 * Return: QDF_STATUS 155 */ 156 QDF_STATUS cfg_psoc_parse(struct wlan_objmgr_psoc *psoc, const char *path); 157 158 /** 159 * cfg_get() - lookup the configured value for @id from @psoc 160 * @psoc: The psoc from which to lookup the configured value 161 * @id: The id of the configured value to lookup 162 * 163 * E.g. 164 * 165 * int32_t value = cfg_get(psoc, WLAN_SOME_INTEGER_CONFIG_ID); 166 * 167 * Return: The configured value 168 */ 169 #define cfg_get(psoc, id) __cfg_get(psoc, __##id) 170 171 /* Configuration Access APIs */ 172 #define __do_call(op, args...) op(args) 173 #define do_call(op, args) __do_call(op, rm_parens args) 174 175 #define cfg_id(id) #id 176 177 #define __cfg_mtype(mtype, ctype, name, min, max, fallback, desc, def...) mtype 178 #define cfg_mtype(id) do_call(__cfg_mtype, id) 179 180 #define __cfg_type(mtype, ctype, name, min, max, fallback, desc, def...) ctype 181 #define cfg_type(id) do_call(__cfg_type, id) 182 183 #define __cfg_name(mtype, ctype, name, min, max, fallback, desc, def...) name 184 #define cfg_name(id) do_call(__cfg_name, id) 185 186 #define __cfg_min(mtype, ctype, name, min, max, fallback, desc, def...) min 187 #define cfg_min(id) do_call(__cfg_min, id) 188 189 #define __cfg_max(mtype, ctype, name, min, max, fallback, desc, def...) max 190 #define cfg_max(id) do_call(__cfg_max, id) 191 192 #define __cfg_fb(mtype, ctype, name, min, max, fallback, desc, def...) fallback 193 #define cfg_fallback(id) do_call(__cfg_fb, id) 194 195 #define __cfg_desc(mtype, ctype, name, min, max, fallback, desc, def...) desc 196 #define cfg_description(id) do_call(__cfg_desc, id) 197 198 #define __cfg_def(mtype, ctype, name, min, max, fallback, desc, def...) def 199 #define cfg_default(id) do_call(__cfg_def, id) 200 201 #define __cfg_str(id...) #id 202 #define cfg_str(id) #id __cfg_str(id) 203 204 /* validate APIs */ 205 static inline bool 206 cfg_string_in_range(const char *value, qdf_size_t min_len, qdf_size_t max_len) 207 { 208 qdf_size_t len = qdf_str_len(value); 209 210 return len >= min_len && len <= max_len; 211 } 212 213 #define __cfg_int_in_range(value, min, max) (value >= min && value <= max) 214 #define __cfg_uint_in_range(value, min, max) (value >= min && value <= max) 215 #define __cfg_string_in_range(value, min_len, max_len) \ 216 cfg_string_in_range(value, min_len, max_len) 217 218 #define __cfg_in_range(id, value, mtype) \ 219 __cfg_ ## mtype ## _in_range(value, cfg_min(id), cfg_max(id)) 220 221 /* this may look redundant, but forces @mtype to be expanded */ 222 #define __cfg_in_range_type(id, value, mtype) \ 223 __cfg_is_range(id, value, mtype) 224 225 #define cfg_in_range(id, value) __cfg_in_range_type(id, value, cfg_mtype(id)) 226 227 /* Value-or-Default APIs */ 228 #define __cfg_value_or_default(id, value, def) \ 229 (cfg_in_range(id, value) ? value : def) 230 231 #define cfg_value_or_default(id, value) \ 232 __cfg_value_or_default(id, value, cfg_default(id)) 233 234 /* Value-or-Clamped APIs */ 235 #define __cfg_clamp(val, min, max) (val < min ? min : (val > max ? max : val)) 236 #define cfg_clamp(id, value) __cfg_clamp(value, cfg_min(id), cfg_max(id)) 237 238 #endif /* __CFG_UCFG_H */ 239 240