blob: 2be66c48720080285486d6ce968a8ea02de9f4ec [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;
180
181 if (strcmp(attr[0], "name") != 0) {
182 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
183 goto done;
184 }
185
186 index = platform_get_snd_device_index((char *)attr[1]);
187 if (index < 0) {
188 ALOGE("%s: Device %s not found, no ACDB ID set!",
189 __func__, attr[1]);
190 goto done;
191 }
192
193 if (strcmp(attr[2], "backend") != 0) {
194 ALOGE("%s: Device %s has no backend set!",
195 __func__, attr[1]);
196 goto done;
197 }
198
Sidipotu Ashokeaec6392016-04-19 09:37:57 +0530199 if (platform_set_snd_device_backend(index, attr[3]) < 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700200 ALOGE("%s: Device %s backend %s was not set!",
201 __func__, attr[1], attr[3]);
202 goto done;
203 }
204
205done:
206 return;
207}
208
209static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800210{
Ben Romberger61764e32014-01-10 13:49:02 -0800211 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800212
Ben Romberger61764e32014-01-10 13:49:02 -0800213 if (strcmp(attr[0], "name") != 0) {
214 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800215 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800216 }
Ben Romberger55886882014-01-10 13:49:02 -0800217
Ben Romberger61764e32014-01-10 13:49:02 -0800218 index = platform_get_snd_device_index((char *)attr[1]);
219 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800220 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
221 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800222 goto done;
223 }
224
225 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800226 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
227 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800228 goto done;
229 }
230
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700231 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
232 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800233 __func__, attr[1], atoi((char *)attr[3]));
Ben Romberger55886882014-01-10 13:49:02 -0800234 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800235 }
Ben Romberger55886882014-01-10 13:49:02 -0800236
Ben Romberger55886882014-01-10 13:49:02 -0800237done:
238 return;
239}
240
Amit Shekhar5a39c912014-10-14 15:39:30 -0700241static void process_bit_width(const XML_Char **attr)
242{
243 int index;
244
245 if (strcmp(attr[0], "name") != 0) {
246 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
247 goto done;
248 }
249
250 index = platform_get_snd_device_index((char *)attr[1]);
251 if (index < 0) {
252 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
253 __func__, attr[1]);
254 goto done;
255 }
256
257 if (strcmp(attr[2], "bit_width") != 0) {
258 ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!",
259 __func__, attr[1]);
260 goto done;
261 }
262
263 if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) {
264 ALOGE("%s: Device %s, ACDB ID %d was not set!",
265 __func__, attr[1], atoi((char *)attr[3]));
266 goto done;
267 }
268
269done:
270 return;
271}
272
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800273static void process_interface_name(const XML_Char **attr)
274{
275 int ret;
276
277 if (strcmp(attr[0], "name") != 0) {
278 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
279
280 goto done;
281 }
282
283 if (strcmp(attr[2], "interface") != 0) {
284 ALOGE("%s: Device %s has no Audio Interface set!",
285 __func__, attr[1]);
286
287 goto done;
288 }
289
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530290 if (strcmp(attr[4], "codec_type") != 0) {
291 ALOGE("%s: Device %s has no codec type set!",
292 __func__, attr[1]);
293
294 goto done;
295 }
296
297 ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3],
298 (char *)attr[5]);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800299 if (ret < 0) {
300 ALOGE("%s: Audio Interface not set!", __func__);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800301 goto done;
302 }
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800303
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800304done:
305 return;
306}
Laxminath Kasam44f49402015-05-29 18:37:11 +0530307static void process_tz_name(const XML_Char **attr)
308{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530309 int index;
Laxminath Kasam44f49402015-05-29 18:37:11 +0530310
311 if (strcmp(attr[0], "name") != 0) {
312 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
313 goto done;
314 }
315
316 index = platform_get_snd_device_index((char *)attr[1]);
317 if (index < 0) {
318 ALOGE("%s: Device %s not found, no snd device set!",
319 __func__, attr[1]);
320 goto done;
321 }
322
323 if (strcmp(attr[2], "spkr_1_tz_name") != 0) {
324 ALOGE("%s: Device %s has no spkr_1_tz_name set!",
325 __func__, attr[1]);
326 }
327
328 if (strcmp(attr[4], "spkr_2_tz_name") != 0) {
329 ALOGE("%s: Device %s has no spkr_2_tz_name set!",
330 __func__, attr[1]);
331 }
332
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +0530333 /* ret = platform_set_spkr_device_tz_names(index, (char *)attr[3], (char *)attr[5]);
Laxminath Kasam44f49402015-05-29 18:37:11 +0530334 if (ret < 0) {
335 ALOGE("%s: Audio Interface not set!", __func__);
336 goto done;
337 }
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +0530338 */
Laxminath Kasam44f49402015-05-29 18:37:11 +0530339
340done:
341 return;
342}
343
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700344static void process_config_params(const XML_Char **attr)
345{
346 if (strcmp(attr[0], "key") != 0) {
347 ALOGE("%s: 'key' not found", __func__);
348 goto done;
349 }
350
351 if (strcmp(attr[2], "value") != 0) {
352 ALOGE("%s: 'value' not found", __func__);
353 goto done;
354 }
355
356 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
357done:
358 return;
359}
360
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700361static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800362 const XML_Char **attr)
363{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700364 if (strcmp(tag_name, "bit_width_configs") == 0) {
365 section = BITWIDTH;
366 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700367 section = ACDB;
368 } else if (strcmp(tag_name, "pcm_ids") == 0) {
369 section = PCM_ID;
370 } else if (strcmp(tag_name, "backend_names") == 0) {
371 section = BACKEND_NAME;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700372 } else if (strcmp(tag_name, "config_params") == 0) {
373 section = CONFIG_PARAMS;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800374 } else if (strcmp(tag_name, "interface_names") == 0) {
375 section = INTERFACE_NAME;
Laxminath Kasam44f49402015-05-29 18:37:11 +0530376 } else if (strcmp(tag_name, "tz_names") == 0) {
377 section = TZ_NAME;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700378 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800379 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
Laxminath Kasam44f49402015-05-29 18:37:11 +0530380 (section != INTERFACE_NAME) && (section != TZ_NAME)) {
381 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface/tz names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700382 return;
383 }
384
385 /* call into process function for the current section */
386 section_process_fn fn = section_table[section];
387 fn(attr);
388 } 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;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700424 }
Ben Romberger55886882014-01-10 13:49:02 -0800425}
426
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700427int platform_info_init(const char *filename, void *platform)
Ben Romberger55886882014-01-10 13:49:02 -0800428{
429 XML_Parser parser;
430 FILE *file;
431 int ret = 0;
432 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800433 void *buf;
434
Helen Zeng6a16ad72014-02-23 22:04:44 -0800435 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700436 section = ROOT;
437
Ben Romberger55886882014-01-10 13:49:02 -0800438 if (!file) {
439 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800440 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800441 ret = -ENODEV;
442 goto done;
443 }
444
445 parser = XML_ParserCreate(NULL);
446 if (!parser) {
447 ALOGE("%s: Failed to create XML parser!", __func__);
448 ret = -ENODEV;
449 goto err_close_file;
450 }
451
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700452 my_data.platform = platform;
453 my_data.kvpairs = str_parms_create();
454
Ben Romberger55886882014-01-10 13:49:02 -0800455 XML_SetElementHandler(parser, start_tag, end_tag);
456
457 while (1) {
458 buf = XML_GetBuffer(parser, BUF_SIZE);
459 if (buf == NULL) {
460 ALOGE("%s: XML_GetBuffer failed", __func__);
461 ret = -ENOMEM;
462 goto err_free_parser;
463 }
464
465 bytes_read = fread(buf, 1, BUF_SIZE, file);
466 if (bytes_read < 0) {
467 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
468 ret = bytes_read;
469 goto err_free_parser;
470 }
471
472 if (XML_ParseBuffer(parser, bytes_read,
473 bytes_read == 0) == XML_STATUS_ERROR) {
474 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800475 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800476 ret = -EINVAL;
477 goto err_free_parser;
478 }
479
480 if (bytes_read == 0)
481 break;
482 }
483
Ben Romberger55886882014-01-10 13:49:02 -0800484err_free_parser:
485 XML_ParserFree(parser);
486err_close_file:
487 fclose(file);
488done:
489 return ret;
490}