blob: 283936e07e67b06f18634238faa3bc42bc3d3430 [file] [log] [blame]
Haynes Mathew George5bc18842014-06-16 16:36:20 -07001/*
Haynes Mathew George569b7482017-05-08 14:44:27 -07002 * Copyright (C) 2014 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
vivek mehtaa68fea62017-06-08 19:04:02 -0700323 if (strcmp(attr[2], "mode")) {
324 ALOGE("%s: mode not found", __func__);
325 goto done;
326 }
327
328 if (strcmp(attr[4], "bit_width")) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800329 ALOGE("%s: bit_width not found", __func__);
330 goto done;
331 }
332
vivek mehtaa68fea62017-06-08 19:04:02 -0700333 if (strcmp(attr[6], "id")) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800334 ALOGE("%s: id not found", __func__);
335 goto done;
336 }
337
vivek mehtaa68fea62017-06-08 19:04:02 -0700338 if (strcmp(attr[8], "max_rate")) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800339 ALOGE("%s: max rate not found", __func__);
340 goto done;
341 }
342
vivek mehtaa68fea62017-06-08 19:04:02 -0700343 platform_add_app_type(attr[1], attr[3], atoi(attr[5]), atoi(attr[7]),
344 atoi(attr[9]));
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800345done:
346 return;
347}
348
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700349static void start_tag(void *userdata __unused, const XML_Char *tag_name,
350 const XML_Char **attr)
351{
352 const XML_Char *attr_name = NULL;
353 const XML_Char *attr_value = NULL;
354 unsigned int i;
355
Haynes Mathew George98c95622014-06-20 19:14:25 -0700356
vivek mehta0fb11312017-05-15 19:35:32 -0700357 if (my_data.do_full_parse) {
358 if (strcmp(tag_name, "acdb_ids") == 0) {
359 section = ACDB;
360 } else if (strcmp(tag_name, "pcm_ids") == 0) {
361 section = PCM_ID;
362 } else if (strcmp(tag_name, "backend_names") == 0) {
363 section = BACKEND_NAME;
364 } else if (strcmp(tag_name, "config_params") == 0) {
365 section = CONFIG_PARAMS;
366 } else if (strcmp(tag_name, "operator_specific") == 0) {
367 section = OPERATOR_SPECIFIC;
368 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
369 section = GAIN_LEVEL_MAPPING;
370 } else if (strcmp(tag_name, "app_types") == 0) {
371 section = APP_TYPE;
372 } else if (strcmp(tag_name, "device") == 0) {
373 if ((section != ACDB) && (section != BACKEND_NAME) && (section != OPERATOR_SPECIFIC)) {
374 ALOGE("device tag only supported for acdb/backend names");
375 return;
376 }
Haynes Mathew George98c95622014-06-20 19:14:25 -0700377
vivek mehta0fb11312017-05-15 19:35:32 -0700378 /* call into process function for the current section */
379 section_process_fn fn = section_table[section];
380 fn(attr);
381 } else if (strcmp(tag_name, "usecase") == 0) {
382 if (section != PCM_ID) {
383 ALOGE("usecase tag only supported with PCM_ID section");
384 return;
385 }
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700386
vivek mehta0fb11312017-05-15 19:35:32 -0700387 section_process_fn fn = section_table[PCM_ID];
388 fn(attr);
389 } else if (strcmp(tag_name, "param") == 0) {
390 if (section != CONFIG_PARAMS) {
391 ALOGE("param tag only supported with CONFIG_PARAMS section");
392 return;
393 }
vivek mehtaa8d7c922016-05-25 14:40:44 -0700394
vivek mehta0fb11312017-05-15 19:35:32 -0700395 section_process_fn fn = section_table[section];
396 fn(attr);
397 } else if (strcmp(tag_name, "gain_level_map") == 0) {
398 if (section != GAIN_LEVEL_MAPPING) {
399 ALOGE("usecase tag only supported with GAIN_LEVEL_MAPPING section");
400 return;
401 }
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800402
vivek mehta0fb11312017-05-15 19:35:32 -0700403 section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
404 fn(attr);
405 } else if (!strcmp(tag_name, "app")) {
406 if (section != APP_TYPE) {
407 ALOGE("app tag only valid in section APP_TYPE");
408 return;
409 }
410
411 section_process_fn fn = section_table[APP_TYPE];
412 fn(attr);
413 }
414 } else {
415 if(strcmp(tag_name, "config_params") == 0) {
416 section = CONFIG_PARAMS;
417 } else if (strcmp(tag_name, "param") == 0) {
418 if (section != CONFIG_PARAMS) {
419 ALOGE("param tag only supported with CONFIG_PARAMS section");
420 return;
421 }
422
423 section_process_fn fn = section_table[section];
424 fn(attr);
425 }
Haynes Mathew George98c95622014-06-20 19:14:25 -0700426 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700427
428 return;
429}
430
Haynes Mathew George98c95622014-06-20 19:14:25 -0700431static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700432{
Haynes Mathew George98c95622014-06-20 19:14:25 -0700433 if (strcmp(tag_name, "acdb_ids") == 0) {
434 section = ROOT;
435 } else if (strcmp(tag_name, "pcm_ids") == 0) {
436 section = ROOT;
437 } else if (strcmp(tag_name, "backend_names") == 0) {
438 section = ROOT;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700439 } else if (strcmp(tag_name, "config_params") == 0) {
440 section = ROOT;
keunhui.park2f7306a2015-07-16 16:48:06 +0900441 } else if (strcmp(tag_name, "operator_specific") == 0) {
442 section = ROOT;
vivek mehtaa8d7c922016-05-25 14:40:44 -0700443 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
444 section = ROOT;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800445 } else if (strcmp(tag_name, "app_types") == 0) {
446 section = ROOT;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700447 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700448}
449
vivek mehta0fb11312017-05-15 19:35:32 -0700450int snd_card_info_init(const char *filename, void *platform, set_parameters_fn fn)
451{
452 set_parameters = fn;
453 my_data.do_full_parse = false;
454 return platform_info_init(filename, platform);
455}
456
vivek mehtade4849c2016-03-03 17:23:38 -0800457int platform_info_init(const char *filename, void *platform)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700458{
459 XML_Parser parser;
460 FILE *file;
461 int ret = 0;
462 int bytes_read;
463 void *buf;
464 static const uint32_t kBufSize = 1024;
vivek mehtade4849c2016-03-03 17:23:38 -0800465 char platform_info_file_name[MIXER_PATH_MAX_LENGTH]= {0};
Haynes Mathew George98c95622014-06-20 19:14:25 -0700466 section = ROOT;
467
vivek mehtade4849c2016-03-03 17:23:38 -0800468 if (filename == NULL) {
469 strlcpy(platform_info_file_name, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
470 } else {
471 strlcpy(platform_info_file_name, filename, MIXER_PATH_MAX_LENGTH);
472 }
473
474 ALOGV("%s: platform info file name is %s", __func__, platform_info_file_name);
475
476 file = fopen(platform_info_file_name, "r");
477
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700478 if (!file) {
479 ALOGD("%s: Failed to open %s, using defaults.",
vivek mehtade4849c2016-03-03 17:23:38 -0800480 __func__, platform_info_file_name);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700481 ret = -ENODEV;
482 goto done;
483 }
484
485 parser = XML_ParserCreate(NULL);
486 if (!parser) {
487 ALOGE("%s: Failed to create XML parser!", __func__);
488 ret = -ENODEV;
489 goto err_close_file;
490 }
491
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700492 my_data.platform = platform;
493 my_data.kvpairs = str_parms_create();
494
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700495 XML_SetElementHandler(parser, start_tag, end_tag);
496
497 while (1) {
498 buf = XML_GetBuffer(parser, kBufSize);
499 if (buf == NULL) {
500 ALOGE("%s: XML_GetBuffer failed", __func__);
501 ret = -ENOMEM;
502 goto err_free_parser;
503 }
504
505 bytes_read = fread(buf, 1, kBufSize, file);
506 if (bytes_read < 0) {
507 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
508 ret = bytes_read;
509 goto err_free_parser;
510 }
511
512 if (XML_ParseBuffer(parser, bytes_read,
513 bytes_read == 0) == XML_STATUS_ERROR) {
514 ALOGE("%s: XML_ParseBuffer failed, for %s",
vivek mehtade4849c2016-03-03 17:23:38 -0800515 __func__, platform_info_file_name);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700516 ret = -EINVAL;
517 goto err_free_parser;
518 }
519
520 if (bytes_read == 0)
521 break;
522 }
523
vivek mehta0fb11312017-05-15 19:35:32 -0700524 set_parameters = &platform_set_parameters;
525 my_data.do_full_parse = true;
526
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700527err_free_parser:
528 XML_ParserFree(parser);
529err_close_file:
530 fclose(file);
531done:
532 return ret;
533}