blob: 8e12dd627c30cacd9d5df729ed4b7c1647b836d2 [file] [log] [blame]
Ben Romberger55886882014-01-10 13:49:02 -08001/*
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302 * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
Ben Romberger55886882014-01-10 13:49:02 -08003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Ben Romberger61764e32014-01-10 13:49:02 -080030#define LOG_TAG "platform_info"
Ben Romberger55886882014-01-10 13:49:02 -080031#define LOG_NDDEBUG 0
32
33#include <errno.h>
34#include <stdio.h>
35#include <expat.h>
36#include <cutils/log.h>
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070037#include <cutils/str_parms.h>
Ben Romberger55886882014-01-10 13:49:02 -080038#include <audio_hw.h>
Ben Romberger61764e32014-01-10 13:49:02 -080039#include "platform_api.h"
40#include <platform.h>
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070041#include <math.h>
Ben Romberger55886882014-01-10 13:49:02 -080042
Ben Romberger55886882014-01-10 13:49:02 -080043#define BUF_SIZE 1024
44
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070045typedef enum {
46 ROOT,
47 ACDB,
Amit Shekhar5a39c912014-10-14 15:39:30 -070048 BITWIDTH,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070049 PCM_ID,
50 BACKEND_NAME,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080051 INTERFACE_NAME,
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070052 CONFIG_PARAMS,
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070053 GAIN_LEVEL_MAPPING,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070054} section_t;
55
56typedef void (* section_process_fn)(const XML_Char **attr);
57
58static void process_acdb_id(const XML_Char **attr);
Amit Shekhar5a39c912014-10-14 15:39:30 -070059static void process_bit_width(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070060static void process_pcm_id(const XML_Char **attr);
61static void process_backend_name(const XML_Char **attr);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080062static void process_interface_name(const XML_Char **attr);
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070063static void process_config_params(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070064static void process_root(const XML_Char **attr);
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070065static void process_gain_db_to_level_map(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070066
67static section_process_fn section_table[] = {
68 [ROOT] = process_root,
69 [ACDB] = process_acdb_id,
Amit Shekhar5a39c912014-10-14 15:39:30 -070070 [BITWIDTH] = process_bit_width,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070071 [PCM_ID] = process_pcm_id,
72 [BACKEND_NAME] = process_backend_name,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080073 [INTERFACE_NAME] = process_interface_name,
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070074 [CONFIG_PARAMS] = process_config_params,
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070075 [GAIN_LEVEL_MAPPING] = process_gain_db_to_level_map,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070076};
77
78static section_t section;
79
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070080struct platform_info {
81 void *platform;
82 struct str_parms *kvpairs;
83};
84
85static struct platform_info my_data;
86
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070087/*
88 * <audio_platform_info>
89 * <acdb_ids>
90 * <device name="???" acdb_id="???"/>
91 * ...
92 * ...
93 * </acdb_ids>
94 * <backend_names>
95 * <device name="???" backend="???"/>
96 * ...
97 * ...
98 * </backend_names>
99 * <pcm_ids>
100 * <usecase name="???" type="in/out" id="???"/>
101 * ...
102 * ...
103 * </pcm_ids>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800104 * <interface_names>
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530105 * <device name="Use audio device name here, not sound device name" interface="PRIMARY_I2S" codec_type="external/internal"/>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800106 * ...
107 * ...
108 * </interface_names>
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700109 * <config_params>
110 * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
111 * ...
112 * ...
113 * </config_params>
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700114 * </audio_platform_info>
115 */
116
117static void process_root(const XML_Char **attr __unused)
118{
119}
120
121/* mapping from usecase to pcm dev id */
122static void process_pcm_id(const XML_Char **attr)
123{
124 int index;
125
126 if (strcmp(attr[0], "name") != 0) {
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700127 ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700128 goto done;
129 }
130
131 index = platform_get_usecase_index((char *)attr[1]);
132 if (index < 0) {
133 ALOGE("%s: usecase %s not found!",
134 __func__, attr[1]);
135 goto done;
136 }
137
138 if (strcmp(attr[2], "type") != 0) {
139 ALOGE("%s: usecase type not mentioned", __func__);
140 goto done;
141 }
142
143 int type = -1;
144
145 if (!strcasecmp((char *)attr[3], "in")) {
146 type = 1;
147 } else if (!strcasecmp((char *)attr[3], "out")) {
148 type = 0;
149 } else {
150 ALOGE("%s: type must be IN or OUT", __func__);
151 goto done;
152 }
153
154 if (strcmp(attr[4], "id") != 0) {
155 ALOGE("%s: usecase id not mentioned", __func__);
156 goto done;
157 }
158
159 int id = atoi((char *)attr[5]);
160
161 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
162 ALOGE("%s: usecase %s type %d id %d was not set!",
163 __func__, attr[1], type, id);
164 goto done;
165 }
166
167done:
168 return;
169}
170
171/* backend to be used for a device */
172static void process_backend_name(const XML_Char **attr)
173{
174 int index;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530175 char *hw_interface = NULL;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700176
177 if (strcmp(attr[0], "name") != 0) {
178 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
179 goto done;
180 }
181
182 index = platform_get_snd_device_index((char *)attr[1]);
183 if (index < 0) {
184 ALOGE("%s: Device %s not found, no ACDB ID set!",
185 __func__, attr[1]);
186 goto done;
187 }
188
189 if (strcmp(attr[2], "backend") != 0) {
190 ALOGE("%s: Device %s has no backend set!",
191 __func__, attr[1]);
192 goto done;
193 }
194
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530195 if (attr[4] != NULL) {
196 if (strcmp(attr[4], "interface") != 0) {
197 hw_interface = NULL;
198 } else {
199 hw_interface = (char *)attr[5];
200 }
201 }
202
203 if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700204 ALOGE("%s: Device %s backend %s was not set!",
205 __func__, attr[1], attr[3]);
206 goto done;
207 }
208
209done:
210 return;
211}
212
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700213static void process_gain_db_to_level_map(const XML_Char **attr)
214{
215 struct amp_db_and_gain_table tbl_entry;
216
217 if ((strcmp(attr[0], "db") != 0) ||
218 (strcmp(attr[2], "level") != 0)) {
219 ALOGE("%s: invalid attribute passed %s %sexpected amp db level",
220 __func__, attr[0], attr[2]);
221 goto done;
222 }
223
224 tbl_entry.db = atof(attr[1]);
225 tbl_entry.amp = exp(tbl_entry.db * 0.115129f);
226 tbl_entry.level = atoi(attr[3]);
227
228 ALOGV("%s: amp [%f] db [%f] level [%d]", __func__,
229 tbl_entry.amp, tbl_entry.db, tbl_entry.level);
230 platform_add_gain_level_mapping(&tbl_entry);
231
232done:
233 return;
234}
235
236
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700237static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800238{
Ben Romberger61764e32014-01-10 13:49:02 -0800239 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800240
Ben Romberger61764e32014-01-10 13:49:02 -0800241 if (strcmp(attr[0], "name") != 0) {
242 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800243 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800244 }
Ben Romberger55886882014-01-10 13:49:02 -0800245
Ben Romberger61764e32014-01-10 13:49:02 -0800246 index = platform_get_snd_device_index((char *)attr[1]);
247 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800248 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
249 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800250 goto done;
251 }
252
253 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800254 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
255 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800256 goto done;
257 }
258
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700259 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
260 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800261 __func__, attr[1], atoi((char *)attr[3]));
Ben Romberger55886882014-01-10 13:49:02 -0800262 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800263 }
Ben Romberger55886882014-01-10 13:49:02 -0800264
Ben Romberger55886882014-01-10 13:49:02 -0800265done:
266 return;
267}
268
Amit Shekhar5a39c912014-10-14 15:39:30 -0700269static void process_bit_width(const XML_Char **attr)
270{
271 int index;
272
273 if (strcmp(attr[0], "name") != 0) {
274 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
275 goto done;
276 }
277
278 index = platform_get_snd_device_index((char *)attr[1]);
279 if (index < 0) {
280 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
281 __func__, attr[1]);
282 goto done;
283 }
284
285 if (strcmp(attr[2], "bit_width") != 0) {
286 ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!",
287 __func__, attr[1]);
288 goto done;
289 }
290
291 if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) {
292 ALOGE("%s: Device %s, ACDB ID %d was not set!",
293 __func__, attr[1], atoi((char *)attr[3]));
294 goto done;
295 }
296
297done:
298 return;
299}
300
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800301static void process_interface_name(const XML_Char **attr)
302{
303 int ret;
304
305 if (strcmp(attr[0], "name") != 0) {
306 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
307
308 goto done;
309 }
310
311 if (strcmp(attr[2], "interface") != 0) {
312 ALOGE("%s: Device %s has no Audio Interface set!",
313 __func__, attr[1]);
314
315 goto done;
316 }
317
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530318 if (strcmp(attr[4], "codec_type") != 0) {
319 ALOGE("%s: Device %s has no codec type set!",
320 __func__, attr[1]);
321
322 goto done;
323 }
324
325 ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3],
326 (char *)attr[5]);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800327 if (ret < 0) {
328 ALOGE("%s: Audio Interface not set!", __func__);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800329 goto done;
330 }
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800331
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800332done:
333 return;
334}
Laxminath Kasam44f49402015-05-29 18:37:11 +0530335
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700336static void process_config_params(const XML_Char **attr)
337{
338 if (strcmp(attr[0], "key") != 0) {
339 ALOGE("%s: 'key' not found", __func__);
340 goto done;
341 }
342
343 if (strcmp(attr[2], "value") != 0) {
344 ALOGE("%s: 'value' not found", __func__);
345 goto done;
346 }
347
348 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
349done:
350 return;
351}
352
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700353static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800354 const XML_Char **attr)
355{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700356 if (strcmp(tag_name, "bit_width_configs") == 0) {
357 section = BITWIDTH;
358 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700359 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;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700364 } else if (strcmp(tag_name, "config_params") == 0) {
365 section = CONFIG_PARAMS;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800366 } else if (strcmp(tag_name, "interface_names") == 0) {
367 section = INTERFACE_NAME;
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700368 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
369 section = GAIN_LEVEL_MAPPING;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700370 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800371 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
Laxminath Kasam167ade52016-05-23 17:46:51 +0530372 (section != INTERFACE_NAME)) {
373 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700374 return;
375 }
376
377 /* call into process function for the current section */
378 section_process_fn fn = section_table[section];
379 fn(attr);
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700380 } else if (strcmp(tag_name, "gain_level_map") == 0) {
381 if (section != GAIN_LEVEL_MAPPING) {
382 ALOGE("usecase tag only supported with GAIN_LEVEL_MAPPING section");
383 return;
384 }
385
386 section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
387 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700388 } else if (strcmp(tag_name, "usecase") == 0) {
389 if (section != PCM_ID) {
390 ALOGE("usecase tag only supported with PCM_ID section");
391 return;
392 }
393
394 section_process_fn fn = section_table[PCM_ID];
395 fn(attr);
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700396 } else if (strcmp(tag_name, "param") == 0) {
397 if (section != CONFIG_PARAMS) {
398 ALOGE("param tag only supported with CONFIG_PARAMS section");
399 return;
400 }
401
402 section_process_fn fn = section_table[section];
403 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700404 }
Ben Romberger55886882014-01-10 13:49:02 -0800405
406 return;
407}
408
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700409static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800410{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700411 if (strcmp(tag_name, "bit_width_configs") == 0) {
412 section = ROOT;
413 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700414 section = ROOT;
415 } else if (strcmp(tag_name, "pcm_ids") == 0) {
416 section = ROOT;
417 } else if (strcmp(tag_name, "backend_names") == 0) {
418 section = ROOT;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700419 } else if (strcmp(tag_name, "config_params") == 0) {
420 section = ROOT;
421 platform_set_parameters(my_data.platform, my_data.kvpairs);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800422 } else if (strcmp(tag_name, "interface_names") == 0) {
423 section = ROOT;
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700424 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
425 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700426 }
Ben Romberger55886882014-01-10 13:49:02 -0800427}
428
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700429int platform_info_init(const char *filename, void *platform)
Ben Romberger55886882014-01-10 13:49:02 -0800430{
431 XML_Parser parser;
432 FILE *file;
433 int ret = 0;
434 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800435 void *buf;
436
Helen Zeng6a16ad72014-02-23 22:04:44 -0800437 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700438 section = ROOT;
439
Ben Romberger55886882014-01-10 13:49:02 -0800440 if (!file) {
441 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800442 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800443 ret = -ENODEV;
444 goto done;
445 }
446
447 parser = XML_ParserCreate(NULL);
448 if (!parser) {
449 ALOGE("%s: Failed to create XML parser!", __func__);
450 ret = -ENODEV;
451 goto err_close_file;
452 }
453
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700454 my_data.platform = platform;
455 my_data.kvpairs = str_parms_create();
456
Ben Romberger55886882014-01-10 13:49:02 -0800457 XML_SetElementHandler(parser, start_tag, end_tag);
458
459 while (1) {
460 buf = XML_GetBuffer(parser, BUF_SIZE);
461 if (buf == NULL) {
462 ALOGE("%s: XML_GetBuffer failed", __func__);
463 ret = -ENOMEM;
464 goto err_free_parser;
465 }
466
467 bytes_read = fread(buf, 1, BUF_SIZE, file);
468 if (bytes_read < 0) {
469 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
470 ret = bytes_read;
471 goto err_free_parser;
472 }
473
474 if (XML_ParseBuffer(parser, bytes_read,
475 bytes_read == 0) == XML_STATUS_ERROR) {
476 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800477 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800478 ret = -EINVAL;
479 goto err_free_parser;
480 }
481
482 if (bytes_read == 0)
483 break;
484 }
485
Ben Romberger55886882014-01-10 13:49:02 -0800486err_free_parser:
487 XML_ParserFree(parser);
488err_close_file:
489 fclose(file);
490done:
491 return ret;
492}