blob: 973d4c4237109f5575f79683ec643d2ce4713f67 [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>
Ben Romberger55886882014-01-10 13:49:02 -080041
Ben Romberger55886882014-01-10 13:49:02 -080042#define BUF_SIZE 1024
43
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070044typedef enum {
45 ROOT,
46 ACDB,
Amit Shekhar5a39c912014-10-14 15:39:30 -070047 BITWIDTH,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070048 PCM_ID,
49 BACKEND_NAME,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080050 INTERFACE_NAME,
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070051 CONFIG_PARAMS,
Laxminath Kasam44f49402015-05-29 18:37:11 +053052 TZ_NAME,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070053} section_t;
54
55typedef void (* section_process_fn)(const XML_Char **attr);
56
57static void process_acdb_id(const XML_Char **attr);
Amit Shekhar5a39c912014-10-14 15:39:30 -070058static void process_bit_width(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070059static void process_pcm_id(const XML_Char **attr);
60static void process_backend_name(const XML_Char **attr);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080061static void process_interface_name(const XML_Char **attr);
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070062static void process_config_params(const XML_Char **attr);
Laxminath Kasam44f49402015-05-29 18:37:11 +053063static void process_tz_name(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070064static void process_root(const XML_Char **attr);
65
66static section_process_fn section_table[] = {
67 [ROOT] = process_root,
68 [ACDB] = process_acdb_id,
Amit Shekhar5a39c912014-10-14 15:39:30 -070069 [BITWIDTH] = process_bit_width,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070070 [PCM_ID] = process_pcm_id,
71 [BACKEND_NAME] = process_backend_name,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080072 [INTERFACE_NAME] = process_interface_name,
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070073 [CONFIG_PARAMS] = process_config_params,
Laxminath Kasam44f49402015-05-29 18:37:11 +053074 [TZ_NAME] = process_tz_name,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070075};
76
77static section_t section;
78
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070079struct platform_info {
80 void *platform;
81 struct str_parms *kvpairs;
82};
83
84static struct platform_info my_data;
85
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070086/*
87 * <audio_platform_info>
88 * <acdb_ids>
89 * <device name="???" acdb_id="???"/>
90 * ...
91 * ...
92 * </acdb_ids>
93 * <backend_names>
94 * <device name="???" backend="???"/>
95 * ...
96 * ...
97 * </backend_names>
98 * <pcm_ids>
99 * <usecase name="???" type="in/out" id="???"/>
100 * ...
101 * ...
102 * </pcm_ids>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800103 * <interface_names>
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530104 * <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 -0800105 * ...
106 * ...
107 * </interface_names>
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700108 * <config_params>
109 * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
110 * ...
111 * ...
112 * </config_params>
113 *
Laxminath Kasam44f49402015-05-29 18:37:11 +0530114 * <tz_names>
115 * <device name="???" spkr_1_tz_name="???" spkr_2_tz_name="???"/>
116 * ...
117 * ...
118 * </tz_names>
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700119 * </audio_platform_info>
120 */
121
122static void process_root(const XML_Char **attr __unused)
123{
124}
125
126/* mapping from usecase to pcm dev id */
127static void process_pcm_id(const XML_Char **attr)
128{
129 int index;
130
131 if (strcmp(attr[0], "name") != 0) {
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700132 ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700133 goto done;
134 }
135
136 index = platform_get_usecase_index((char *)attr[1]);
137 if (index < 0) {
138 ALOGE("%s: usecase %s not found!",
139 __func__, attr[1]);
140 goto done;
141 }
142
143 if (strcmp(attr[2], "type") != 0) {
144 ALOGE("%s: usecase type not mentioned", __func__);
145 goto done;
146 }
147
148 int type = -1;
149
150 if (!strcasecmp((char *)attr[3], "in")) {
151 type = 1;
152 } else if (!strcasecmp((char *)attr[3], "out")) {
153 type = 0;
154 } else {
155 ALOGE("%s: type must be IN or OUT", __func__);
156 goto done;
157 }
158
159 if (strcmp(attr[4], "id") != 0) {
160 ALOGE("%s: usecase id not mentioned", __func__);
161 goto done;
162 }
163
164 int id = atoi((char *)attr[5]);
165
166 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
167 ALOGE("%s: usecase %s type %d id %d was not set!",
168 __func__, attr[1], type, id);
169 goto done;
170 }
171
172done:
173 return;
174}
175
176/* backend to be used for a device */
177static void process_backend_name(const XML_Char **attr)
178{
179 int index;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530180 char *hw_interface = NULL;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700181
182 if (strcmp(attr[0], "name") != 0) {
183 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
184 goto done;
185 }
186
187 index = platform_get_snd_device_index((char *)attr[1]);
188 if (index < 0) {
189 ALOGE("%s: Device %s not found, no ACDB ID set!",
190 __func__, attr[1]);
191 goto done;
192 }
193
194 if (strcmp(attr[2], "backend") != 0) {
195 ALOGE("%s: Device %s has no backend set!",
196 __func__, attr[1]);
197 goto done;
198 }
199
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530200 if (attr[4] != NULL) {
201 if (strcmp(attr[4], "interface") != 0) {
202 hw_interface = NULL;
203 } else {
204 hw_interface = (char *)attr[5];
205 }
206 }
207
208 if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700209 ALOGE("%s: Device %s backend %s was not set!",
210 __func__, attr[1], attr[3]);
211 goto done;
212 }
213
214done:
215 return;
216}
217
218static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800219{
Ben Romberger61764e32014-01-10 13:49:02 -0800220 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800221
Ben Romberger61764e32014-01-10 13:49:02 -0800222 if (strcmp(attr[0], "name") != 0) {
223 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800224 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800225 }
Ben Romberger55886882014-01-10 13:49:02 -0800226
Ben Romberger61764e32014-01-10 13:49:02 -0800227 index = platform_get_snd_device_index((char *)attr[1]);
228 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800229 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
230 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800231 goto done;
232 }
233
234 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800235 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
236 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800237 goto done;
238 }
239
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700240 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
241 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800242 __func__, attr[1], atoi((char *)attr[3]));
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 Romberger55886882014-01-10 13:49:02 -0800246done:
247 return;
248}
249
Amit Shekhar5a39c912014-10-14 15:39:30 -0700250static void process_bit_width(const XML_Char **attr)
251{
252 int index;
253
254 if (strcmp(attr[0], "name") != 0) {
255 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
256 goto done;
257 }
258
259 index = platform_get_snd_device_index((char *)attr[1]);
260 if (index < 0) {
261 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
262 __func__, attr[1]);
263 goto done;
264 }
265
266 if (strcmp(attr[2], "bit_width") != 0) {
267 ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!",
268 __func__, attr[1]);
269 goto done;
270 }
271
272 if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) {
273 ALOGE("%s: Device %s, ACDB ID %d was not set!",
274 __func__, attr[1], atoi((char *)attr[3]));
275 goto done;
276 }
277
278done:
279 return;
280}
281
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800282static void process_interface_name(const XML_Char **attr)
283{
284 int ret;
285
286 if (strcmp(attr[0], "name") != 0) {
287 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
288
289 goto done;
290 }
291
292 if (strcmp(attr[2], "interface") != 0) {
293 ALOGE("%s: Device %s has no Audio Interface set!",
294 __func__, attr[1]);
295
296 goto done;
297 }
298
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530299 if (strcmp(attr[4], "codec_type") != 0) {
300 ALOGE("%s: Device %s has no codec type set!",
301 __func__, attr[1]);
302
303 goto done;
304 }
305
306 ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3],
307 (char *)attr[5]);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800308 if (ret < 0) {
309 ALOGE("%s: Audio Interface not set!", __func__);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800310 goto done;
311 }
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800312
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800313done:
314 return;
315}
Laxminath Kasam44f49402015-05-29 18:37:11 +0530316static void process_tz_name(const XML_Char **attr)
317{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530318 int index;
Laxminath Kasam44f49402015-05-29 18:37:11 +0530319
320 if (strcmp(attr[0], "name") != 0) {
321 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
322 goto done;
323 }
324
325 index = platform_get_snd_device_index((char *)attr[1]);
326 if (index < 0) {
327 ALOGE("%s: Device %s not found, no snd device set!",
328 __func__, attr[1]);
329 goto done;
330 }
331
332 if (strcmp(attr[2], "spkr_1_tz_name") != 0) {
333 ALOGE("%s: Device %s has no spkr_1_tz_name set!",
334 __func__, attr[1]);
335 }
336
337 if (strcmp(attr[4], "spkr_2_tz_name") != 0) {
338 ALOGE("%s: Device %s has no spkr_2_tz_name set!",
339 __func__, attr[1]);
340 }
341
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +0530342 /* ret = platform_set_spkr_device_tz_names(index, (char *)attr[3], (char *)attr[5]);
Laxminath Kasam44f49402015-05-29 18:37:11 +0530343 if (ret < 0) {
344 ALOGE("%s: Audio Interface not set!", __func__);
345 goto done;
346 }
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +0530347 */
Laxminath Kasam44f49402015-05-29 18:37:11 +0530348
349done:
350 return;
351}
352
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700353static void process_config_params(const XML_Char **attr)
354{
355 if (strcmp(attr[0], "key") != 0) {
356 ALOGE("%s: 'key' not found", __func__);
357 goto done;
358 }
359
360 if (strcmp(attr[2], "value") != 0) {
361 ALOGE("%s: 'value' not found", __func__);
362 goto done;
363 }
364
365 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
366done:
367 return;
368}
369
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700370static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800371 const XML_Char **attr)
372{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700373 if (strcmp(tag_name, "bit_width_configs") == 0) {
374 section = BITWIDTH;
375 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700376 section = ACDB;
377 } else if (strcmp(tag_name, "pcm_ids") == 0) {
378 section = PCM_ID;
379 } else if (strcmp(tag_name, "backend_names") == 0) {
380 section = BACKEND_NAME;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700381 } else if (strcmp(tag_name, "config_params") == 0) {
382 section = CONFIG_PARAMS;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800383 } else if (strcmp(tag_name, "interface_names") == 0) {
384 section = INTERFACE_NAME;
Laxminath Kasam44f49402015-05-29 18:37:11 +0530385 } else if (strcmp(tag_name, "tz_names") == 0) {
386 section = TZ_NAME;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700387 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800388 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
Laxminath Kasam44f49402015-05-29 18:37:11 +0530389 (section != INTERFACE_NAME) && (section != TZ_NAME)) {
390 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface/tz names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700391 return;
392 }
393
394 /* call into process function for the current section */
395 section_process_fn fn = section_table[section];
396 fn(attr);
397 } else if (strcmp(tag_name, "usecase") == 0) {
398 if (section != PCM_ID) {
399 ALOGE("usecase tag only supported with PCM_ID section");
400 return;
401 }
402
403 section_process_fn fn = section_table[PCM_ID];
404 fn(attr);
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700405 } else if (strcmp(tag_name, "param") == 0) {
406 if (section != CONFIG_PARAMS) {
407 ALOGE("param tag only supported with CONFIG_PARAMS section");
408 return;
409 }
410
411 section_process_fn fn = section_table[section];
412 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700413 }
Ben Romberger55886882014-01-10 13:49:02 -0800414
415 return;
416}
417
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700418static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800419{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700420 if (strcmp(tag_name, "bit_width_configs") == 0) {
421 section = ROOT;
422 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700423 section = ROOT;
424 } else if (strcmp(tag_name, "pcm_ids") == 0) {
425 section = ROOT;
426 } else if (strcmp(tag_name, "backend_names") == 0) {
427 section = ROOT;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700428 } else if (strcmp(tag_name, "config_params") == 0) {
429 section = ROOT;
430 platform_set_parameters(my_data.platform, my_data.kvpairs);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800431 } else if (strcmp(tag_name, "interface_names") == 0) {
432 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700433 }
Ben Romberger55886882014-01-10 13:49:02 -0800434}
435
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700436int platform_info_init(const char *filename, void *platform)
Ben Romberger55886882014-01-10 13:49:02 -0800437{
438 XML_Parser parser;
439 FILE *file;
440 int ret = 0;
441 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800442 void *buf;
443
Helen Zeng6a16ad72014-02-23 22:04:44 -0800444 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700445 section = ROOT;
446
Ben Romberger55886882014-01-10 13:49:02 -0800447 if (!file) {
448 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800449 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800450 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 Alamanda14b0f2d2015-06-28 21:04:09 -0700461 my_data.platform = platform;
462 my_data.kvpairs = str_parms_create();
463
Ben Romberger55886882014-01-10 13:49:02 -0800464 XML_SetElementHandler(parser, start_tag, end_tag);
465
466 while (1) {
467 buf = XML_GetBuffer(parser, BUF_SIZE);
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, BUF_SIZE, 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",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800484 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800485 ret = -EINVAL;
486 goto err_free_parser;
487 }
488
489 if (bytes_read == 0)
490 break;
491 }
492
Ben Romberger55886882014-01-10 13:49:02 -0800493err_free_parser:
494 XML_ParserFree(parser);
495err_close_file:
496 fclose(file);
497done:
498 return ret;
499}