blob: c63f244fe204ff9ce53ec7648f444da60396afce [file] [log] [blame]
Haynes Mathew George5bc18842014-06-16 16:36:20 -07001/*
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -08002 * Copyright (C) 2014-2017 The Android Open Source Project
Haynes Mathew George5bc18842014-06-16 16:36:20 -07003 *
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>
vivek mehtaa8d7c922016-05-25 14:40:44 -070027#include <math.h>
Haynes Mathew George5bc18842014-06-16 16:36:20 -070028
Haynes Mathew George98c95622014-06-20 19:14:25 -070029typedef enum {
30 ROOT,
31 ACDB,
32 PCM_ID,
33 BACKEND_NAME,
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070034 CONFIG_PARAMS,
keunhui.park2f7306a2015-07-16 16:48:06 +090035 OPERATOR_SPECIFIC,
vivek mehtaa8d7c922016-05-25 14:40:44 -070036 GAIN_LEVEL_MAPPING,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080037 APP_TYPE,
Haynes Mathew George98c95622014-06-20 19:14:25 -070038} section_t;
39
40typedef void (* section_process_fn)(const XML_Char **attr);
41
42static void process_acdb_id(const XML_Char **attr);
43static void process_pcm_id(const XML_Char **attr);
44static void process_backend_name(const XML_Char **attr);
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070045static void process_config_params(const XML_Char **attr);
Haynes Mathew George98c95622014-06-20 19:14:25 -070046static void process_root(const XML_Char **attr);
keunhui.park2f7306a2015-07-16 16:48:06 +090047static void process_operator_specific(const XML_Char **attr);
vivek mehtaa8d7c922016-05-25 14:40:44 -070048static void process_gain_db_to_level_map(const XML_Char **attr);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080049static void process_app_type(const XML_Char **attr);
Haynes Mathew George98c95622014-06-20 19:14:25 -070050
51static section_process_fn section_table[] = {
52 [ROOT] = process_root,
53 [ACDB] = process_acdb_id,
54 [PCM_ID] = process_pcm_id,
55 [BACKEND_NAME] = process_backend_name,
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070056 [CONFIG_PARAMS] = process_config_params,
keunhui.park2f7306a2015-07-16 16:48:06 +090057 [OPERATOR_SPECIFIC] = process_operator_specific,
vivek mehtaa8d7c922016-05-25 14:40:44 -070058 [GAIN_LEVEL_MAPPING] = process_gain_db_to_level_map,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080059 [APP_TYPE] = process_app_type,
Haynes Mathew George98c95622014-06-20 19:14:25 -070060};
61
vivek mehta0fb11312017-05-15 19:35:32 -070062static set_parameters_fn set_parameters = &platform_set_parameters;
63
Haynes Mathew George98c95622014-06-20 19:14:25 -070064static section_t section;
65
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070066struct platform_info {
vivek mehta0fb11312017-05-15 19:35:32 -070067 bool do_full_parse;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070068 void *platform;
69 struct str_parms *kvpairs;
70};
71
vivek mehta0fb11312017-05-15 19:35:32 -070072static struct platform_info my_data = {true, NULL, NULL};
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070073
Haynes Mathew George98c95622014-06-20 19:14:25 -070074/*
75 * <audio_platform_info>
76 * <acdb_ids>
77 * <device name="???" acdb_id="???"/>
78 * ...
79 * ...
80 * </acdb_ids>
81 * <backend_names>
82 * <device name="???" backend="???"/>
83 * ...
84 * ...
85 * </backend_names>
86 * <pcm_ids>
87 * <usecase name="???" type="in/out" id="???"/>
88 * ...
89 * ...
90 * </pcm_ids>
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070091 * <config_params>
92 * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
keunhui.park2f7306a2015-07-16 16:48:06 +090093 * <param key="operator_info" value="tmus;aa;bb;cc"/>
94 * <param key="operator_info" value="sprint;xx;yy;zz"/>
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070095 * ...
96 * ...
97 * </config_params>
98 *
keunhui.park2f7306a2015-07-16 16:48:06 +090099 * <operator_specific>
100 * <device name="???" operator="???" mixer_path="???" acdb_id="???"/>
101 * ...
102 * ...
103 * </operator_specific>
104 *
Haynes Mathew George98c95622014-06-20 19:14:25 -0700105 * </audio_platform_info>
106 */
107
108static void process_root(const XML_Char **attr __unused)
109{
110}
111
112/* mapping from usecase to pcm dev id */
113static void process_pcm_id(const XML_Char **attr)
114{
115 int index;
116
117 if (strcmp(attr[0], "name") != 0) {
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700118 ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
Haynes Mathew George98c95622014-06-20 19:14:25 -0700119 goto done;
120 }
121
122 index = platform_get_usecase_index((char *)attr[1]);
123 if (index < 0) {
124 ALOGE("%s: usecase %s in %s not found!",
125 __func__, attr[1], PLATFORM_INFO_XML_PATH);
126 goto done;
127 }
128
129 if (strcmp(attr[2], "type") != 0) {
130 ALOGE("%s: usecase type not mentioned", __func__);
131 goto done;
132 }
133
134 int type = -1;
135
136 if (!strcasecmp((char *)attr[3], "in")) {
137 type = 1;
138 } else if (!strcasecmp((char *)attr[3], "out")) {
139 type = 0;
140 } else {
141 ALOGE("%s: type must be IN or OUT", __func__);
142 goto done;
143 }
144
145 if (strcmp(attr[4], "id") != 0) {
146 ALOGE("%s: usecase id not mentioned", __func__);
147 goto done;
148 }
149
150 int id = atoi((char *)attr[5]);
151
152 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
153 ALOGE("%s: usecase %s in %s, type %d id %d was not set!",
154 __func__, attr[1], PLATFORM_INFO_XML_PATH, type, id);
155 goto done;
156 }
157
158done:
159 return;
160}
161
162/* backend to be used for a device */
163static void process_backend_name(const XML_Char **attr)
164{
165 int index;
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700166 char *hw_interface = NULL;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700167
168 if (strcmp(attr[0], "name") != 0) {
169 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
170 goto done;
171 }
172
173 index = platform_get_snd_device_index((char *)attr[1]);
174 if (index < 0) {
175 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
176 __func__, attr[1], PLATFORM_INFO_XML_PATH);
177 goto done;
178 }
179
180 if (strcmp(attr[2], "backend") != 0) {
181 ALOGE("%s: Device %s in %s has no backed set!",
182 __func__, attr[1], PLATFORM_INFO_XML_PATH);
183 goto done;
184 }
185
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700186 if (attr[4] != NULL) {
187 if (strcmp(attr[4], "interface") != 0) {
188 hw_interface = NULL;
189 } else {
190 hw_interface = (char *)attr[5];
191 }
192 }
193
194 if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 0) {
Haynes Mathew George98c95622014-06-20 19:14:25 -0700195 ALOGE("%s: Device %s in %s, backend %s was not set!",
196 __func__, attr[1], PLATFORM_INFO_XML_PATH, attr[3]);
197 goto done;
198 }
199
200done:
201 return;
202}
203
vivek mehtaa8d7c922016-05-25 14:40:44 -0700204static void process_gain_db_to_level_map(const XML_Char **attr)
205{
206 struct amp_db_and_gain_table tbl_entry;
207
208 if ((strcmp(attr[0], "db") != 0) ||
209 (strcmp(attr[2], "level") != 0)) {
210 ALOGE("%s: invalid attribute passed %s %sexpected amp db level",
211 __func__, attr[0], attr[2]);
212 goto done;
213 }
214
215 tbl_entry.db = atof(attr[1]);
216 tbl_entry.amp = exp(tbl_entry.db * 0.115129f);
217 tbl_entry.level = atoi(attr[3]);
218
219 ALOGV("%s: amp [%f] db [%f] level [%d]", __func__,
220 tbl_entry.amp, tbl_entry.db, tbl_entry.level);
221 platform_add_gain_level_mapping(&tbl_entry);
222
223done:
224 return;
225}
226
Haynes Mathew George98c95622014-06-20 19:14:25 -0700227static void process_acdb_id(const XML_Char **attr)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700228{
229 int index;
230
231 if (strcmp(attr[0], "name") != 0) {
232 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
233 goto done;
234 }
235
236 index = platform_get_snd_device_index((char *)attr[1]);
237 if (index < 0) {
238 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
239 __func__, attr[1], PLATFORM_INFO_XML_PATH);
240 goto done;
241 }
242
243 if (strcmp(attr[2], "acdb_id") != 0) {
244 ALOGE("%s: Device %s in %s has no acdb_id, no ACDB ID set!",
245 __func__, attr[1], PLATFORM_INFO_XML_PATH);
246 goto done;
247 }
248
Haynes Mathew George98c95622014-06-20 19:14:25 -0700249 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700250 ALOGE("%s: Device %s in %s, ACDB ID %d was not set!",
251 __func__, attr[1], PLATFORM_INFO_XML_PATH, atoi((char *)attr[3]));
252 goto done;
253 }
254
255done:
256 return;
257}
258
keunhui.park2f7306a2015-07-16 16:48:06 +0900259
260static void process_operator_specific(const XML_Char **attr)
261{
262 snd_device_t snd_device = SND_DEVICE_NONE;
263
264 if (strcmp(attr[0], "name") != 0) {
265 ALOGE("%s: 'name' not found", __func__);
266 goto done;
267 }
268
269 snd_device = platform_get_snd_device_index((char *)attr[1]);
270 if (snd_device < 0) {
271 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
272 __func__, (char *)attr[3], PLATFORM_INFO_XML_PATH);
273 goto done;
274 }
275
276 if (strcmp(attr[2], "operator") != 0) {
277 ALOGE("%s: 'operator' not found", __func__);
278 goto done;
279 }
280
281 if (strcmp(attr[4], "mixer_path") != 0) {
282 ALOGE("%s: 'mixer_path' not found", __func__);
283 goto done;
284 }
285
286 if (strcmp(attr[6], "acdb_id") != 0) {
287 ALOGE("%s: 'acdb_id' not found", __func__);
288 goto done;
289 }
290
291 platform_add_operator_specific_device(snd_device, (char *)attr[3], (char *)attr[5], atoi((char *)attr[7]));
292
293done:
294 return;
295}
296
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700297/* platform specific configuration key-value pairs */
298static void process_config_params(const XML_Char **attr)
299{
300 if (strcmp(attr[0], "key") != 0) {
301 ALOGE("%s: 'key' not found", __func__);
302 goto done;
303 }
304
305 if (strcmp(attr[2], "value") != 0) {
306 ALOGE("%s: 'value' not found", __func__);
307 goto done;
308 }
309
310 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
vivek mehta0fb11312017-05-15 19:35:32 -0700311 set_parameters(my_data.platform, my_data.kvpairs);
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700312done:
313 return;
314}
315
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800316static void process_app_type(const XML_Char **attr)
317{
318 if (strcmp(attr[0], "uc_type")) {
319 ALOGE("%s: uc_type not found", __func__);
320 goto done;
321 }
322
323 if (strcmp(attr[2], "bit_width")) {
324 ALOGE("%s: bit_width not found", __func__);
325 goto done;
326 }
327
328 if (strcmp(attr[4], "id")) {
329 ALOGE("%s: id not found", __func__);
330 goto done;
331 }
332
333 if (strcmp(attr[6], "max_rate")) {
334 ALOGE("%s: max rate not found", __func__);
335 goto done;
336 }
337
338 platform_add_app_type(atoi(attr[3]), attr[1], atoi(attr[5]), atoi(attr[7]));
339done:
340 return;
341}
342
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700343static void start_tag(void *userdata __unused, const XML_Char *tag_name,
344 const XML_Char **attr)
345{
346 const XML_Char *attr_name = NULL;
347 const XML_Char *attr_value = NULL;
348 unsigned int i;
349
Haynes Mathew George98c95622014-06-20 19:14:25 -0700350
vivek mehta0fb11312017-05-15 19:35:32 -0700351 if (my_data.do_full_parse) {
352 if (strcmp(tag_name, "acdb_ids") == 0) {
353 section = ACDB;
354 } else if (strcmp(tag_name, "pcm_ids") == 0) {
355 section = PCM_ID;
356 } else if (strcmp(tag_name, "backend_names") == 0) {
357 section = BACKEND_NAME;
358 } else if (strcmp(tag_name, "config_params") == 0) {
359 section = CONFIG_PARAMS;
360 } else if (strcmp(tag_name, "operator_specific") == 0) {
361 section = OPERATOR_SPECIFIC;
362 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
363 section = GAIN_LEVEL_MAPPING;
364 } else if (strcmp(tag_name, "app_types") == 0) {
365 section = APP_TYPE;
366 } else if (strcmp(tag_name, "device") == 0) {
367 if ((section != ACDB) && (section != BACKEND_NAME) && (section != OPERATOR_SPECIFIC)) {
368 ALOGE("device tag only supported for acdb/backend names");
369 return;
370 }
Haynes Mathew George98c95622014-06-20 19:14:25 -0700371
vivek mehta0fb11312017-05-15 19:35:32 -0700372 /* call into process function for the current section */
373 section_process_fn fn = section_table[section];
374 fn(attr);
375 } else if (strcmp(tag_name, "usecase") == 0) {
376 if (section != PCM_ID) {
377 ALOGE("usecase tag only supported with PCM_ID section");
378 return;
379 }
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700380
vivek mehta0fb11312017-05-15 19:35:32 -0700381 section_process_fn fn = section_table[PCM_ID];
382 fn(attr);
383 } else if (strcmp(tag_name, "param") == 0) {
384 if (section != CONFIG_PARAMS) {
385 ALOGE("param tag only supported with CONFIG_PARAMS section");
386 return;
387 }
vivek mehtaa8d7c922016-05-25 14:40:44 -0700388
vivek mehta0fb11312017-05-15 19:35:32 -0700389 section_process_fn fn = section_table[section];
390 fn(attr);
391 } else if (strcmp(tag_name, "gain_level_map") == 0) {
392 if (section != GAIN_LEVEL_MAPPING) {
393 ALOGE("usecase tag only supported with GAIN_LEVEL_MAPPING section");
394 return;
395 }
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800396
vivek mehta0fb11312017-05-15 19:35:32 -0700397 section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
398 fn(attr);
399 } else if (!strcmp(tag_name, "app")) {
400 if (section != APP_TYPE) {
401 ALOGE("app tag only valid in section APP_TYPE");
402 return;
403 }
404
405 section_process_fn fn = section_table[APP_TYPE];
406 fn(attr);
407 }
408 } else {
409 if(strcmp(tag_name, "config_params") == 0) {
410 section = CONFIG_PARAMS;
411 } else if (strcmp(tag_name, "param") == 0) {
412 if (section != CONFIG_PARAMS) {
413 ALOGE("param tag only supported with CONFIG_PARAMS section");
414 return;
415 }
416
417 section_process_fn fn = section_table[section];
418 fn(attr);
419 }
Haynes Mathew George98c95622014-06-20 19:14:25 -0700420 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700421
422 return;
423}
424
Haynes Mathew George98c95622014-06-20 19:14:25 -0700425static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700426{
Haynes Mathew George98c95622014-06-20 19:14:25 -0700427 if (strcmp(tag_name, "acdb_ids") == 0) {
428 section = ROOT;
429 } else if (strcmp(tag_name, "pcm_ids") == 0) {
430 section = ROOT;
431 } else if (strcmp(tag_name, "backend_names") == 0) {
432 section = ROOT;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700433 } else if (strcmp(tag_name, "config_params") == 0) {
434 section = ROOT;
keunhui.park2f7306a2015-07-16 16:48:06 +0900435 } else if (strcmp(tag_name, "operator_specific") == 0) {
436 section = ROOT;
vivek mehtaa8d7c922016-05-25 14:40:44 -0700437 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
438 section = ROOT;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800439 } else if (strcmp(tag_name, "app_types") == 0) {
440 section = ROOT;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700441 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700442}
443
vivek mehta0fb11312017-05-15 19:35:32 -0700444int snd_card_info_init(const char *filename, void *platform, set_parameters_fn fn)
445{
446 set_parameters = fn;
447 my_data.do_full_parse = false;
448 return platform_info_init(filename, platform);
449}
450
vivek mehtade4849c2016-03-03 17:23:38 -0800451int platform_info_init(const char *filename, void *platform)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700452{
453 XML_Parser parser;
454 FILE *file;
455 int ret = 0;
456 int bytes_read;
457 void *buf;
458 static const uint32_t kBufSize = 1024;
vivek mehtade4849c2016-03-03 17:23:38 -0800459 char platform_info_file_name[MIXER_PATH_MAX_LENGTH]= {0};
Haynes Mathew George98c95622014-06-20 19:14:25 -0700460 section = ROOT;
461
vivek mehtade4849c2016-03-03 17:23:38 -0800462 if (filename == NULL) {
463 strlcpy(platform_info_file_name, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
464 } else {
465 strlcpy(platform_info_file_name, filename, MIXER_PATH_MAX_LENGTH);
466 }
467
468 ALOGV("%s: platform info file name is %s", __func__, platform_info_file_name);
469
470 file = fopen(platform_info_file_name, "r");
471
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700472 if (!file) {
473 ALOGD("%s: Failed to open %s, using defaults.",
vivek mehtade4849c2016-03-03 17:23:38 -0800474 __func__, platform_info_file_name);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700475 ret = -ENODEV;
476 goto done;
477 }
478
479 parser = XML_ParserCreate(NULL);
480 if (!parser) {
481 ALOGE("%s: Failed to create XML parser!", __func__);
482 ret = -ENODEV;
483 goto err_close_file;
484 }
485
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700486 my_data.platform = platform;
487 my_data.kvpairs = str_parms_create();
488
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700489 XML_SetElementHandler(parser, start_tag, end_tag);
490
491 while (1) {
492 buf = XML_GetBuffer(parser, kBufSize);
493 if (buf == NULL) {
494 ALOGE("%s: XML_GetBuffer failed", __func__);
495 ret = -ENOMEM;
496 goto err_free_parser;
497 }
498
499 bytes_read = fread(buf, 1, kBufSize, file);
500 if (bytes_read < 0) {
501 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
502 ret = bytes_read;
503 goto err_free_parser;
504 }
505
506 if (XML_ParseBuffer(parser, bytes_read,
507 bytes_read == 0) == XML_STATUS_ERROR) {
508 ALOGE("%s: XML_ParseBuffer failed, for %s",
vivek mehtade4849c2016-03-03 17:23:38 -0800509 __func__, platform_info_file_name);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700510 ret = -EINVAL;
511 goto err_free_parser;
512 }
513
514 if (bytes_read == 0)
515 break;
516 }
517
vivek mehta0fb11312017-05-15 19:35:32 -0700518 set_parameters = &platform_set_parameters;
519 my_data.do_full_parse = true;
520
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700521err_free_parser:
522 XML_ParserFree(parser);
523err_close_file:
524 fclose(file);
525done:
526 return ret;
527}