Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "platform_info" |
| 18 | #define LOG_NDDEBUG 0 |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdio.h> |
| 22 | #include <expat.h> |
| 23 | #include <cutils/log.h> |
| 24 | #include <audio_hw.h> |
| 25 | #include "platform_api.h" |
| 26 | #include <platform.h> |
| 27 | |
| 28 | #define PLATFORM_INFO_XML_PATH "/system/etc/audio_platform_info.xml" |
| 29 | |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 30 | typedef enum { |
| 31 | ROOT, |
| 32 | ACDB, |
| 33 | PCM_ID, |
| 34 | BACKEND_NAME, |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 35 | CONFIG_PARAMS, |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 36 | OPERATOR_SPECIFIC, |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 37 | } section_t; |
| 38 | |
| 39 | typedef void (* section_process_fn)(const XML_Char **attr); |
| 40 | |
| 41 | static void process_acdb_id(const XML_Char **attr); |
| 42 | static void process_pcm_id(const XML_Char **attr); |
| 43 | static void process_backend_name(const XML_Char **attr); |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 44 | static void process_config_params(const XML_Char **attr); |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 45 | static void process_root(const XML_Char **attr); |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 46 | static void process_operator_specific(const XML_Char **attr); |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 47 | |
| 48 | static section_process_fn section_table[] = { |
| 49 | [ROOT] = process_root, |
| 50 | [ACDB] = process_acdb_id, |
| 51 | [PCM_ID] = process_pcm_id, |
| 52 | [BACKEND_NAME] = process_backend_name, |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 53 | [CONFIG_PARAMS] = process_config_params, |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 54 | [OPERATOR_SPECIFIC] = process_operator_specific, |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static section_t section; |
| 58 | |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 59 | struct platform_info { |
| 60 | void *platform; |
| 61 | struct str_parms *kvpairs; |
| 62 | }; |
| 63 | |
| 64 | static struct platform_info my_data; |
| 65 | |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 66 | /* |
| 67 | * <audio_platform_info> |
| 68 | * <acdb_ids> |
| 69 | * <device name="???" acdb_id="???"/> |
| 70 | * ... |
| 71 | * ... |
| 72 | * </acdb_ids> |
| 73 | * <backend_names> |
| 74 | * <device name="???" backend="???"/> |
| 75 | * ... |
| 76 | * ... |
| 77 | * </backend_names> |
| 78 | * <pcm_ids> |
| 79 | * <usecase name="???" type="in/out" id="???"/> |
| 80 | * ... |
| 81 | * ... |
| 82 | * </pcm_ids> |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 83 | * <config_params> |
| 84 | * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/> |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 85 | * <param key="operator_info" value="tmus;aa;bb;cc"/> |
| 86 | * <param key="operator_info" value="sprint;xx;yy;zz"/> |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 87 | * ... |
| 88 | * ... |
| 89 | * </config_params> |
| 90 | * |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 91 | * <operator_specific> |
| 92 | * <device name="???" operator="???" mixer_path="???" acdb_id="???"/> |
| 93 | * ... |
| 94 | * ... |
| 95 | * </operator_specific> |
| 96 | * |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 97 | * </audio_platform_info> |
| 98 | */ |
| 99 | |
| 100 | static void process_root(const XML_Char **attr __unused) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | /* mapping from usecase to pcm dev id */ |
| 105 | static void process_pcm_id(const XML_Char **attr) |
| 106 | { |
| 107 | int index; |
| 108 | |
| 109 | if (strcmp(attr[0], "name") != 0) { |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 110 | ALOGE("%s: 'name' not found, no pcm_id set!", __func__); |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 111 | goto done; |
| 112 | } |
| 113 | |
| 114 | index = platform_get_usecase_index((char *)attr[1]); |
| 115 | if (index < 0) { |
| 116 | ALOGE("%s: usecase %s in %s not found!", |
| 117 | __func__, attr[1], PLATFORM_INFO_XML_PATH); |
| 118 | goto done; |
| 119 | } |
| 120 | |
| 121 | if (strcmp(attr[2], "type") != 0) { |
| 122 | ALOGE("%s: usecase type not mentioned", __func__); |
| 123 | goto done; |
| 124 | } |
| 125 | |
| 126 | int type = -1; |
| 127 | |
| 128 | if (!strcasecmp((char *)attr[3], "in")) { |
| 129 | type = 1; |
| 130 | } else if (!strcasecmp((char *)attr[3], "out")) { |
| 131 | type = 0; |
| 132 | } else { |
| 133 | ALOGE("%s: type must be IN or OUT", __func__); |
| 134 | goto done; |
| 135 | } |
| 136 | |
| 137 | if (strcmp(attr[4], "id") != 0) { |
| 138 | ALOGE("%s: usecase id not mentioned", __func__); |
| 139 | goto done; |
| 140 | } |
| 141 | |
| 142 | int id = atoi((char *)attr[5]); |
| 143 | |
| 144 | if (platform_set_usecase_pcm_id(index, type, id) < 0) { |
| 145 | ALOGE("%s: usecase %s in %s, type %d id %d was not set!", |
| 146 | __func__, attr[1], PLATFORM_INFO_XML_PATH, type, id); |
| 147 | goto done; |
| 148 | } |
| 149 | |
| 150 | done: |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | /* backend to be used for a device */ |
| 155 | static void process_backend_name(const XML_Char **attr) |
| 156 | { |
| 157 | int index; |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 158 | char *hw_interface = NULL; |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 159 | |
| 160 | if (strcmp(attr[0], "name") != 0) { |
| 161 | ALOGE("%s: 'name' not found, no ACDB ID set!", __func__); |
| 162 | goto done; |
| 163 | } |
| 164 | |
| 165 | index = platform_get_snd_device_index((char *)attr[1]); |
| 166 | if (index < 0) { |
| 167 | ALOGE("%s: Device %s in %s not found, no ACDB ID set!", |
| 168 | __func__, attr[1], PLATFORM_INFO_XML_PATH); |
| 169 | goto done; |
| 170 | } |
| 171 | |
| 172 | if (strcmp(attr[2], "backend") != 0) { |
| 173 | ALOGE("%s: Device %s in %s has no backed set!", |
| 174 | __func__, attr[1], PLATFORM_INFO_XML_PATH); |
| 175 | goto done; |
| 176 | } |
| 177 | |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 178 | if (attr[4] != NULL) { |
| 179 | if (strcmp(attr[4], "interface") != 0) { |
| 180 | hw_interface = NULL; |
| 181 | } else { |
| 182 | hw_interface = (char *)attr[5]; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 0) { |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 187 | ALOGE("%s: Device %s in %s, backend %s was not set!", |
| 188 | __func__, attr[1], PLATFORM_INFO_XML_PATH, attr[3]); |
| 189 | goto done; |
| 190 | } |
| 191 | |
| 192 | done: |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | static void process_acdb_id(const XML_Char **attr) |
Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 197 | { |
| 198 | int index; |
| 199 | |
| 200 | if (strcmp(attr[0], "name") != 0) { |
| 201 | ALOGE("%s: 'name' not found, no ACDB ID set!", __func__); |
| 202 | goto done; |
| 203 | } |
| 204 | |
| 205 | index = platform_get_snd_device_index((char *)attr[1]); |
| 206 | if (index < 0) { |
| 207 | ALOGE("%s: Device %s in %s not found, no ACDB ID set!", |
| 208 | __func__, attr[1], PLATFORM_INFO_XML_PATH); |
| 209 | goto done; |
| 210 | } |
| 211 | |
| 212 | if (strcmp(attr[2], "acdb_id") != 0) { |
| 213 | ALOGE("%s: Device %s in %s has no acdb_id, no ACDB ID set!", |
| 214 | __func__, attr[1], PLATFORM_INFO_XML_PATH); |
| 215 | goto done; |
| 216 | } |
| 217 | |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 218 | if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) { |
Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 219 | ALOGE("%s: Device %s in %s, ACDB ID %d was not set!", |
| 220 | __func__, attr[1], PLATFORM_INFO_XML_PATH, atoi((char *)attr[3])); |
| 221 | goto done; |
| 222 | } |
| 223 | |
| 224 | done: |
| 225 | return; |
| 226 | } |
| 227 | |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 228 | |
| 229 | static void process_operator_specific(const XML_Char **attr) |
| 230 | { |
| 231 | snd_device_t snd_device = SND_DEVICE_NONE; |
| 232 | |
| 233 | if (strcmp(attr[0], "name") != 0) { |
| 234 | ALOGE("%s: 'name' not found", __func__); |
| 235 | goto done; |
| 236 | } |
| 237 | |
| 238 | snd_device = platform_get_snd_device_index((char *)attr[1]); |
| 239 | if (snd_device < 0) { |
| 240 | ALOGE("%s: Device %s in %s not found, no ACDB ID set!", |
| 241 | __func__, (char *)attr[3], PLATFORM_INFO_XML_PATH); |
| 242 | goto done; |
| 243 | } |
| 244 | |
| 245 | if (strcmp(attr[2], "operator") != 0) { |
| 246 | ALOGE("%s: 'operator' not found", __func__); |
| 247 | goto done; |
| 248 | } |
| 249 | |
| 250 | if (strcmp(attr[4], "mixer_path") != 0) { |
| 251 | ALOGE("%s: 'mixer_path' not found", __func__); |
| 252 | goto done; |
| 253 | } |
| 254 | |
| 255 | if (strcmp(attr[6], "acdb_id") != 0) { |
| 256 | ALOGE("%s: 'acdb_id' not found", __func__); |
| 257 | goto done; |
| 258 | } |
| 259 | |
| 260 | platform_add_operator_specific_device(snd_device, (char *)attr[3], (char *)attr[5], atoi((char *)attr[7])); |
| 261 | |
| 262 | done: |
| 263 | return; |
| 264 | } |
| 265 | |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 266 | /* platform specific configuration key-value pairs */ |
| 267 | static void process_config_params(const XML_Char **attr) |
| 268 | { |
| 269 | if (strcmp(attr[0], "key") != 0) { |
| 270 | ALOGE("%s: 'key' not found", __func__); |
| 271 | goto done; |
| 272 | } |
| 273 | |
| 274 | if (strcmp(attr[2], "value") != 0) { |
| 275 | ALOGE("%s: 'value' not found", __func__); |
| 276 | goto done; |
| 277 | } |
| 278 | |
| 279 | str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]); |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 280 | platform_set_parameters(my_data.platform, my_data.kvpairs); |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 281 | done: |
| 282 | return; |
| 283 | } |
| 284 | |
Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 285 | static void start_tag(void *userdata __unused, const XML_Char *tag_name, |
| 286 | const XML_Char **attr) |
| 287 | { |
| 288 | const XML_Char *attr_name = NULL; |
| 289 | const XML_Char *attr_value = NULL; |
| 290 | unsigned int i; |
| 291 | |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 292 | if (strcmp(tag_name, "acdb_ids") == 0) { |
| 293 | section = ACDB; |
| 294 | } else if (strcmp(tag_name, "pcm_ids") == 0) { |
| 295 | section = PCM_ID; |
| 296 | } else if (strcmp(tag_name, "backend_names") == 0) { |
| 297 | section = BACKEND_NAME; |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 298 | } else if (strcmp(tag_name, "config_params") == 0) { |
| 299 | section = CONFIG_PARAMS; |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 300 | } else if (strcmp(tag_name, "operator_specific") == 0) { |
| 301 | section = OPERATOR_SPECIFIC; |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 302 | } else if (strcmp(tag_name, "device") == 0) { |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 303 | if ((section != ACDB) && (section != BACKEND_NAME) && (section != OPERATOR_SPECIFIC)) { |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 304 | ALOGE("device tag only supported for acdb/backend names"); |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | /* call into process function for the current section */ |
| 309 | section_process_fn fn = section_table[section]; |
| 310 | fn(attr); |
| 311 | } else if (strcmp(tag_name, "usecase") == 0) { |
| 312 | if (section != PCM_ID) { |
| 313 | ALOGE("usecase tag only supported with PCM_ID section"); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | section_process_fn fn = section_table[PCM_ID]; |
| 318 | fn(attr); |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 319 | } else if (strcmp(tag_name, "param") == 0) { |
| 320 | if (section != CONFIG_PARAMS) { |
| 321 | ALOGE("param tag only supported with CONFIG_PARAMS section"); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | section_process_fn fn = section_table[section]; |
| 326 | fn(attr); |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 327 | } |
Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 328 | |
| 329 | return; |
| 330 | } |
| 331 | |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 332 | static void end_tag(void *userdata __unused, const XML_Char *tag_name) |
Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 333 | { |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 334 | if (strcmp(tag_name, "acdb_ids") == 0) { |
| 335 | section = ROOT; |
| 336 | } else if (strcmp(tag_name, "pcm_ids") == 0) { |
| 337 | section = ROOT; |
| 338 | } else if (strcmp(tag_name, "backend_names") == 0) { |
| 339 | section = ROOT; |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 340 | } else if (strcmp(tag_name, "config_params") == 0) { |
| 341 | section = ROOT; |
keunhui.park | 2f7306a | 2015-07-16 16:48:06 +0900 | [diff] [blame^] | 342 | } else if (strcmp(tag_name, "operator_specific") == 0) { |
| 343 | section = ROOT; |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 344 | } |
Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 347 | int platform_info_init(void *platform) |
Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 348 | { |
| 349 | XML_Parser parser; |
| 350 | FILE *file; |
| 351 | int ret = 0; |
| 352 | int bytes_read; |
| 353 | void *buf; |
| 354 | static const uint32_t kBufSize = 1024; |
| 355 | |
Haynes Mathew George | 98c9562 | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 356 | section = ROOT; |
| 357 | |
Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 358 | file = fopen(PLATFORM_INFO_XML_PATH, "r"); |
| 359 | if (!file) { |
| 360 | ALOGD("%s: Failed to open %s, using defaults.", |
| 361 | __func__, PLATFORM_INFO_XML_PATH); |
| 362 | ret = -ENODEV; |
| 363 | goto done; |
| 364 | } |
| 365 | |
| 366 | parser = XML_ParserCreate(NULL); |
| 367 | if (!parser) { |
| 368 | ALOGE("%s: Failed to create XML parser!", __func__); |
| 369 | ret = -ENODEV; |
| 370 | goto err_close_file; |
| 371 | } |
| 372 | |
Ravi Kumar Alamanda | c4f5731 | 2015-06-26 17:41:02 -0700 | [diff] [blame] | 373 | my_data.platform = platform; |
| 374 | my_data.kvpairs = str_parms_create(); |
| 375 | |
Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame] | 376 | XML_SetElementHandler(parser, start_tag, end_tag); |
| 377 | |
| 378 | while (1) { |
| 379 | buf = XML_GetBuffer(parser, kBufSize); |
| 380 | if (buf == NULL) { |
| 381 | ALOGE("%s: XML_GetBuffer failed", __func__); |
| 382 | ret = -ENOMEM; |
| 383 | goto err_free_parser; |
| 384 | } |
| 385 | |
| 386 | bytes_read = fread(buf, 1, kBufSize, file); |
| 387 | if (bytes_read < 0) { |
| 388 | ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read); |
| 389 | ret = bytes_read; |
| 390 | goto err_free_parser; |
| 391 | } |
| 392 | |
| 393 | if (XML_ParseBuffer(parser, bytes_read, |
| 394 | bytes_read == 0) == XML_STATUS_ERROR) { |
| 395 | ALOGE("%s: XML_ParseBuffer failed, for %s", |
| 396 | __func__, PLATFORM_INFO_XML_PATH); |
| 397 | ret = -EINVAL; |
| 398 | goto err_free_parser; |
| 399 | } |
| 400 | |
| 401 | if (bytes_read == 0) |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | err_free_parser: |
| 406 | XML_ParserFree(parser); |
| 407 | err_close_file: |
| 408 | fclose(file); |
| 409 | done: |
| 410 | return ret; |
| 411 | } |