blob: 952fc684b3ac25f51bf765016b17ae8cabcf20fe [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
62static section_t section;
63
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070064struct platform_info {
65 void *platform;
66 struct str_parms *kvpairs;
67};
68
69static struct platform_info my_data;
70
Haynes Mathew George98c95622014-06-20 19:14:25 -070071/*
72 * <audio_platform_info>
73 * <acdb_ids>
74 * <device name="???" acdb_id="???"/>
75 * ...
76 * ...
77 * </acdb_ids>
78 * <backend_names>
79 * <device name="???" backend="???"/>
80 * ...
81 * ...
82 * </backend_names>
83 * <pcm_ids>
84 * <usecase name="???" type="in/out" id="???"/>
85 * ...
86 * ...
87 * </pcm_ids>
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070088 * <config_params>
89 * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
keunhui.park2f7306a2015-07-16 16:48:06 +090090 * <param key="operator_info" value="tmus;aa;bb;cc"/>
91 * <param key="operator_info" value="sprint;xx;yy;zz"/>
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070092 * ...
93 * ...
94 * </config_params>
95 *
keunhui.park2f7306a2015-07-16 16:48:06 +090096 * <operator_specific>
97 * <device name="???" operator="???" mixer_path="???" acdb_id="???"/>
98 * ...
99 * ...
100 * </operator_specific>
101 *
Haynes Mathew George98c95622014-06-20 19:14:25 -0700102 * </audio_platform_info>
103 */
104
105static void process_root(const XML_Char **attr __unused)
106{
107}
108
109/* mapping from usecase to pcm dev id */
110static void process_pcm_id(const XML_Char **attr)
111{
112 int index;
113
114 if (strcmp(attr[0], "name") != 0) {
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700115 ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
Haynes Mathew George98c95622014-06-20 19:14:25 -0700116 goto done;
117 }
118
119 index = platform_get_usecase_index((char *)attr[1]);
120 if (index < 0) {
121 ALOGE("%s: usecase %s in %s not found!",
122 __func__, attr[1], PLATFORM_INFO_XML_PATH);
123 goto done;
124 }
125
126 if (strcmp(attr[2], "type") != 0) {
127 ALOGE("%s: usecase type not mentioned", __func__);
128 goto done;
129 }
130
131 int type = -1;
132
133 if (!strcasecmp((char *)attr[3], "in")) {
134 type = 1;
135 } else if (!strcasecmp((char *)attr[3], "out")) {
136 type = 0;
137 } else {
138 ALOGE("%s: type must be IN or OUT", __func__);
139 goto done;
140 }
141
142 if (strcmp(attr[4], "id") != 0) {
143 ALOGE("%s: usecase id not mentioned", __func__);
144 goto done;
145 }
146
147 int id = atoi((char *)attr[5]);
148
149 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
150 ALOGE("%s: usecase %s in %s, type %d id %d was not set!",
151 __func__, attr[1], PLATFORM_INFO_XML_PATH, type, id);
152 goto done;
153 }
154
155done:
156 return;
157}
158
159/* backend to be used for a device */
160static void process_backend_name(const XML_Char **attr)
161{
162 int index;
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700163 char *hw_interface = NULL;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700164
165 if (strcmp(attr[0], "name") != 0) {
166 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
167 goto done;
168 }
169
170 index = platform_get_snd_device_index((char *)attr[1]);
171 if (index < 0) {
172 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
173 __func__, attr[1], PLATFORM_INFO_XML_PATH);
174 goto done;
175 }
176
177 if (strcmp(attr[2], "backend") != 0) {
178 ALOGE("%s: Device %s in %s has no backed set!",
179 __func__, attr[1], PLATFORM_INFO_XML_PATH);
180 goto done;
181 }
182
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700183 if (attr[4] != NULL) {
184 if (strcmp(attr[4], "interface") != 0) {
185 hw_interface = NULL;
186 } else {
187 hw_interface = (char *)attr[5];
188 }
189 }
190
191 if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 0) {
Haynes Mathew George98c95622014-06-20 19:14:25 -0700192 ALOGE("%s: Device %s in %s, backend %s was not set!",
193 __func__, attr[1], PLATFORM_INFO_XML_PATH, attr[3]);
194 goto done;
195 }
196
197done:
198 return;
199}
200
vivek mehtaa8d7c922016-05-25 14:40:44 -0700201static void process_gain_db_to_level_map(const XML_Char **attr)
202{
203 struct amp_db_and_gain_table tbl_entry;
204
205 if ((strcmp(attr[0], "db") != 0) ||
206 (strcmp(attr[2], "level") != 0)) {
207 ALOGE("%s: invalid attribute passed %s %sexpected amp db level",
208 __func__, attr[0], attr[2]);
209 goto done;
210 }
211
212 tbl_entry.db = atof(attr[1]);
213 tbl_entry.amp = exp(tbl_entry.db * 0.115129f);
214 tbl_entry.level = atoi(attr[3]);
215
216 ALOGV("%s: amp [%f] db [%f] level [%d]", __func__,
217 tbl_entry.amp, tbl_entry.db, tbl_entry.level);
218 platform_add_gain_level_mapping(&tbl_entry);
219
220done:
221 return;
222}
223
Haynes Mathew George98c95622014-06-20 19:14:25 -0700224static void process_acdb_id(const XML_Char **attr)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700225{
226 int index;
227
228 if (strcmp(attr[0], "name") != 0) {
229 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
230 goto done;
231 }
232
233 index = platform_get_snd_device_index((char *)attr[1]);
234 if (index < 0) {
235 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
236 __func__, attr[1], PLATFORM_INFO_XML_PATH);
237 goto done;
238 }
239
240 if (strcmp(attr[2], "acdb_id") != 0) {
241 ALOGE("%s: Device %s in %s has no acdb_id, no ACDB ID set!",
242 __func__, attr[1], PLATFORM_INFO_XML_PATH);
243 goto done;
244 }
245
Haynes Mathew George98c95622014-06-20 19:14:25 -0700246 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700247 ALOGE("%s: Device %s in %s, ACDB ID %d was not set!",
248 __func__, attr[1], PLATFORM_INFO_XML_PATH, atoi((char *)attr[3]));
249 goto done;
250 }
251
252done:
253 return;
254}
255
keunhui.park2f7306a2015-07-16 16:48:06 +0900256
257static void process_operator_specific(const XML_Char **attr)
258{
259 snd_device_t snd_device = SND_DEVICE_NONE;
260
261 if (strcmp(attr[0], "name") != 0) {
262 ALOGE("%s: 'name' not found", __func__);
263 goto done;
264 }
265
266 snd_device = platform_get_snd_device_index((char *)attr[1]);
267 if (snd_device < 0) {
268 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
269 __func__, (char *)attr[3], PLATFORM_INFO_XML_PATH);
270 goto done;
271 }
272
273 if (strcmp(attr[2], "operator") != 0) {
274 ALOGE("%s: 'operator' not found", __func__);
275 goto done;
276 }
277
278 if (strcmp(attr[4], "mixer_path") != 0) {
279 ALOGE("%s: 'mixer_path' not found", __func__);
280 goto done;
281 }
282
283 if (strcmp(attr[6], "acdb_id") != 0) {
284 ALOGE("%s: 'acdb_id' not found", __func__);
285 goto done;
286 }
287
288 platform_add_operator_specific_device(snd_device, (char *)attr[3], (char *)attr[5], atoi((char *)attr[7]));
289
290done:
291 return;
292}
293
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700294/* platform specific configuration key-value pairs */
295static void process_config_params(const XML_Char **attr)
296{
297 if (strcmp(attr[0], "key") != 0) {
298 ALOGE("%s: 'key' not found", __func__);
299 goto done;
300 }
301
302 if (strcmp(attr[2], "value") != 0) {
303 ALOGE("%s: 'value' not found", __func__);
304 goto done;
305 }
306
307 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
keunhui.park2f7306a2015-07-16 16:48:06 +0900308 platform_set_parameters(my_data.platform, my_data.kvpairs);
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700309done:
310 return;
311}
312
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800313static void process_app_type(const XML_Char **attr)
314{
315 if (strcmp(attr[0], "uc_type")) {
316 ALOGE("%s: uc_type not found", __func__);
317 goto done;
318 }
319
320 if (strcmp(attr[2], "bit_width")) {
321 ALOGE("%s: bit_width not found", __func__);
322 goto done;
323 }
324
325 if (strcmp(attr[4], "id")) {
326 ALOGE("%s: id not found", __func__);
327 goto done;
328 }
329
330 if (strcmp(attr[6], "max_rate")) {
331 ALOGE("%s: max rate not found", __func__);
332 goto done;
333 }
334
335 platform_add_app_type(atoi(attr[3]), attr[1], atoi(attr[5]), atoi(attr[7]));
336done:
337 return;
338}
339
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700340static void start_tag(void *userdata __unused, const XML_Char *tag_name,
341 const XML_Char **attr)
342{
343 const XML_Char *attr_name = NULL;
344 const XML_Char *attr_value = NULL;
345 unsigned int i;
346
Haynes Mathew George98c95622014-06-20 19:14:25 -0700347 if (strcmp(tag_name, "acdb_ids") == 0) {
348 section = ACDB;
349 } else if (strcmp(tag_name, "pcm_ids") == 0) {
350 section = PCM_ID;
351 } else if (strcmp(tag_name, "backend_names") == 0) {
352 section = BACKEND_NAME;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700353 } else if (strcmp(tag_name, "config_params") == 0) {
354 section = CONFIG_PARAMS;
keunhui.park2f7306a2015-07-16 16:48:06 +0900355 } else if (strcmp(tag_name, "operator_specific") == 0) {
356 section = OPERATOR_SPECIFIC;
vivek mehtaa8d7c922016-05-25 14:40:44 -0700357 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
358 section = GAIN_LEVEL_MAPPING;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800359 } else if (strcmp(tag_name, "app_types") == 0) {
360 section = APP_TYPE;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700361 } else if (strcmp(tag_name, "device") == 0) {
keunhui.park2f7306a2015-07-16 16:48:06 +0900362 if ((section != ACDB) && (section != BACKEND_NAME) && (section != OPERATOR_SPECIFIC)) {
Haynes Mathew George98c95622014-06-20 19:14:25 -0700363 ALOGE("device tag only supported for acdb/backend names");
364 return;
365 }
366
367 /* call into process function for the current section */
368 section_process_fn fn = section_table[section];
369 fn(attr);
370 } else if (strcmp(tag_name, "usecase") == 0) {
371 if (section != PCM_ID) {
372 ALOGE("usecase tag only supported with PCM_ID section");
373 return;
374 }
375
376 section_process_fn fn = section_table[PCM_ID];
377 fn(attr);
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700378 } else if (strcmp(tag_name, "param") == 0) {
379 if (section != CONFIG_PARAMS) {
380 ALOGE("param tag only supported with CONFIG_PARAMS section");
381 return;
382 }
383
384 section_process_fn fn = section_table[section];
385 fn(attr);
vivek mehtaa8d7c922016-05-25 14:40:44 -0700386 } else if (strcmp(tag_name, "gain_level_map") == 0) {
387 if (section != GAIN_LEVEL_MAPPING) {
388 ALOGE("usecase tag only supported with GAIN_LEVEL_MAPPING section");
389 return;
390 }
391
392 section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
393 fn(attr);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800394 } else if (!strcmp(tag_name, "app")) {
395 if (section != APP_TYPE) {
396 ALOGE("app tag only valid in section APP_TYPE");
397 return;
398 }
399
400 section_process_fn fn = section_table[APP_TYPE];
401 fn(attr);
Haynes Mathew George98c95622014-06-20 19:14:25 -0700402 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700403
404 return;
405}
406
Haynes Mathew George98c95622014-06-20 19:14:25 -0700407static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700408{
Haynes Mathew George98c95622014-06-20 19:14:25 -0700409 if (strcmp(tag_name, "acdb_ids") == 0) {
410 section = ROOT;
411 } else if (strcmp(tag_name, "pcm_ids") == 0) {
412 section = ROOT;
413 } else if (strcmp(tag_name, "backend_names") == 0) {
414 section = ROOT;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700415 } else if (strcmp(tag_name, "config_params") == 0) {
416 section = ROOT;
keunhui.park2f7306a2015-07-16 16:48:06 +0900417 } else if (strcmp(tag_name, "operator_specific") == 0) {
418 section = ROOT;
vivek mehtaa8d7c922016-05-25 14:40:44 -0700419 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
420 section = ROOT;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800421 } else if (strcmp(tag_name, "app_types") == 0) {
422 section = ROOT;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700423 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700424}
425
vivek mehtade4849c2016-03-03 17:23:38 -0800426int platform_info_init(const char *filename, void *platform)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700427{
428 XML_Parser parser;
429 FILE *file;
430 int ret = 0;
431 int bytes_read;
432 void *buf;
433 static const uint32_t kBufSize = 1024;
vivek mehtade4849c2016-03-03 17:23:38 -0800434 char platform_info_file_name[MIXER_PATH_MAX_LENGTH]= {0};
Haynes Mathew George98c95622014-06-20 19:14:25 -0700435 section = ROOT;
436
vivek mehtade4849c2016-03-03 17:23:38 -0800437 if (filename == NULL) {
438 strlcpy(platform_info_file_name, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
439 } else {
440 strlcpy(platform_info_file_name, filename, MIXER_PATH_MAX_LENGTH);
441 }
442
443 ALOGV("%s: platform info file name is %s", __func__, platform_info_file_name);
444
445 file = fopen(platform_info_file_name, "r");
446
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700447 if (!file) {
448 ALOGD("%s: Failed to open %s, using defaults.",
vivek mehtade4849c2016-03-03 17:23:38 -0800449 __func__, platform_info_file_name);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700450 ret = -ENODEV;
451 goto done;
452 }
453
454 parser = XML_ParserCreate(NULL);
455 if (!parser) {
456 ALOGE("%s: Failed to create XML parser!", __func__);
457 ret = -ENODEV;
458 goto err_close_file;
459 }
460
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700461 my_data.platform = platform;
462 my_data.kvpairs = str_parms_create();
463
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700464 XML_SetElementHandler(parser, start_tag, end_tag);
465
466 while (1) {
467 buf = XML_GetBuffer(parser, kBufSize);
468 if (buf == NULL) {
469 ALOGE("%s: XML_GetBuffer failed", __func__);
470 ret = -ENOMEM;
471 goto err_free_parser;
472 }
473
474 bytes_read = fread(buf, 1, kBufSize, file);
475 if (bytes_read < 0) {
476 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
477 ret = bytes_read;
478 goto err_free_parser;
479 }
480
481 if (XML_ParseBuffer(parser, bytes_read,
482 bytes_read == 0) == XML_STATUS_ERROR) {
483 ALOGE("%s: XML_ParseBuffer failed, for %s",
vivek mehtade4849c2016-03-03 17:23:38 -0800484 __func__, platform_info_file_name);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700485 ret = -EINVAL;
486 goto err_free_parser;
487 }
488
489 if (bytes_read == 0)
490 break;
491 }
492
493err_free_parser:
494 XML_ParserFree(parser);
495err_close_file:
496 fclose(file);
497done:
498 return ret;
499}