blob: 4ccb89c1d6df9dd65ad87bc568b8a2933788b4ba [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,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070052} section_t;
53
54typedef void (* section_process_fn)(const XML_Char **attr);
55
56static void process_acdb_id(const XML_Char **attr);
Amit Shekhar5a39c912014-10-14 15:39:30 -070057static void process_bit_width(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070058static void process_pcm_id(const XML_Char **attr);
59static void process_backend_name(const XML_Char **attr);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080060static void process_interface_name(const XML_Char **attr);
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070061static void process_config_params(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070062static void process_root(const XML_Char **attr);
63
64static section_process_fn section_table[] = {
65 [ROOT] = process_root,
66 [ACDB] = process_acdb_id,
Amit Shekhar5a39c912014-10-14 15:39:30 -070067 [BITWIDTH] = process_bit_width,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070068 [PCM_ID] = process_pcm_id,
69 [BACKEND_NAME] = process_backend_name,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080070 [INTERFACE_NAME] = process_interface_name,
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070071 [CONFIG_PARAMS] = process_config_params,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070072};
73
74static section_t section;
75
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070076struct platform_info {
77 void *platform;
78 struct str_parms *kvpairs;
79};
80
81static struct platform_info my_data;
82
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070083/*
84 * <audio_platform_info>
85 * <acdb_ids>
86 * <device name="???" acdb_id="???"/>
87 * ...
88 * ...
89 * </acdb_ids>
90 * <backend_names>
91 * <device name="???" backend="???"/>
92 * ...
93 * ...
94 * </backend_names>
95 * <pcm_ids>
96 * <usecase name="???" type="in/out" id="???"/>
97 * ...
98 * ...
99 * </pcm_ids>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800100 * <interface_names>
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530101 * <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 -0800102 * ...
103 * ...
104 * </interface_names>
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700105 * <config_params>
106 * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
107 * ...
108 * ...
109 * </config_params>
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700110 * </audio_platform_info>
111 */
112
113static void process_root(const XML_Char **attr __unused)
114{
115}
116
117/* mapping from usecase to pcm dev id */
118static void process_pcm_id(const XML_Char **attr)
119{
120 int index;
121
122 if (strcmp(attr[0], "name") != 0) {
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700123 ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700124 goto done;
125 }
126
127 index = platform_get_usecase_index((char *)attr[1]);
128 if (index < 0) {
129 ALOGE("%s: usecase %s not found!",
130 __func__, attr[1]);
131 goto done;
132 }
133
134 if (strcmp(attr[2], "type") != 0) {
135 ALOGE("%s: usecase type not mentioned", __func__);
136 goto done;
137 }
138
139 int type = -1;
140
141 if (!strcasecmp((char *)attr[3], "in")) {
142 type = 1;
143 } else if (!strcasecmp((char *)attr[3], "out")) {
144 type = 0;
145 } else {
146 ALOGE("%s: type must be IN or OUT", __func__);
147 goto done;
148 }
149
150 if (strcmp(attr[4], "id") != 0) {
151 ALOGE("%s: usecase id not mentioned", __func__);
152 goto done;
153 }
154
155 int id = atoi((char *)attr[5]);
156
157 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
158 ALOGE("%s: usecase %s type %d id %d was not set!",
159 __func__, attr[1], type, id);
160 goto done;
161 }
162
163done:
164 return;
165}
166
167/* backend to be used for a device */
168static void process_backend_name(const XML_Char **attr)
169{
170 int index;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530171 char *hw_interface = NULL;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700172
173 if (strcmp(attr[0], "name") != 0) {
174 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
175 goto done;
176 }
177
178 index = platform_get_snd_device_index((char *)attr[1]);
179 if (index < 0) {
180 ALOGE("%s: Device %s not found, no ACDB ID set!",
181 __func__, attr[1]);
182 goto done;
183 }
184
185 if (strcmp(attr[2], "backend") != 0) {
186 ALOGE("%s: Device %s has no backend set!",
187 __func__, attr[1]);
188 goto done;
189 }
190
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530191 if (attr[4] != NULL) {
192 if (strcmp(attr[4], "interface") != 0) {
193 hw_interface = NULL;
194 } else {
195 hw_interface = (char *)attr[5];
196 }
197 }
198
199 if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 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 +0530307
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700308static void process_config_params(const XML_Char **attr)
309{
310 if (strcmp(attr[0], "key") != 0) {
311 ALOGE("%s: 'key' not found", __func__);
312 goto done;
313 }
314
315 if (strcmp(attr[2], "value") != 0) {
316 ALOGE("%s: 'value' not found", __func__);
317 goto done;
318 }
319
320 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
321done:
322 return;
323}
324
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700325static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800326 const XML_Char **attr)
327{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700328 if (strcmp(tag_name, "bit_width_configs") == 0) {
329 section = BITWIDTH;
330 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700331 section = ACDB;
332 } else if (strcmp(tag_name, "pcm_ids") == 0) {
333 section = PCM_ID;
334 } else if (strcmp(tag_name, "backend_names") == 0) {
335 section = BACKEND_NAME;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700336 } else if (strcmp(tag_name, "config_params") == 0) {
337 section = CONFIG_PARAMS;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800338 } else if (strcmp(tag_name, "interface_names") == 0) {
339 section = INTERFACE_NAME;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700340 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800341 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
Laxminath Kasam167ade52016-05-23 17:46:51 +0530342 (section != INTERFACE_NAME)) {
343 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700344 return;
345 }
346
347 /* call into process function for the current section */
348 section_process_fn fn = section_table[section];
349 fn(attr);
350 } else if (strcmp(tag_name, "usecase") == 0) {
351 if (section != PCM_ID) {
352 ALOGE("usecase tag only supported with PCM_ID section");
353 return;
354 }
355
356 section_process_fn fn = section_table[PCM_ID];
357 fn(attr);
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700358 } else if (strcmp(tag_name, "param") == 0) {
359 if (section != CONFIG_PARAMS) {
360 ALOGE("param tag only supported with CONFIG_PARAMS section");
361 return;
362 }
363
364 section_process_fn fn = section_table[section];
365 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700366 }
Ben Romberger55886882014-01-10 13:49:02 -0800367
368 return;
369}
370
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700371static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800372{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700373 if (strcmp(tag_name, "bit_width_configs") == 0) {
374 section = ROOT;
375 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700376 section = ROOT;
377 } else if (strcmp(tag_name, "pcm_ids") == 0) {
378 section = ROOT;
379 } else if (strcmp(tag_name, "backend_names") == 0) {
380 section = ROOT;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700381 } else if (strcmp(tag_name, "config_params") == 0) {
382 section = ROOT;
383 platform_set_parameters(my_data.platform, my_data.kvpairs);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800384 } else if (strcmp(tag_name, "interface_names") == 0) {
385 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700386 }
Ben Romberger55886882014-01-10 13:49:02 -0800387}
388
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700389int platform_info_init(const char *filename, void *platform)
Ben Romberger55886882014-01-10 13:49:02 -0800390{
391 XML_Parser parser;
392 FILE *file;
393 int ret = 0;
394 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800395 void *buf;
396
Helen Zeng6a16ad72014-02-23 22:04:44 -0800397 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700398 section = ROOT;
399
Ben Romberger55886882014-01-10 13:49:02 -0800400 if (!file) {
401 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800402 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800403 ret = -ENODEV;
404 goto done;
405 }
406
407 parser = XML_ParserCreate(NULL);
408 if (!parser) {
409 ALOGE("%s: Failed to create XML parser!", __func__);
410 ret = -ENODEV;
411 goto err_close_file;
412 }
413
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700414 my_data.platform = platform;
415 my_data.kvpairs = str_parms_create();
416
Ben Romberger55886882014-01-10 13:49:02 -0800417 XML_SetElementHandler(parser, start_tag, end_tag);
418
419 while (1) {
420 buf = XML_GetBuffer(parser, BUF_SIZE);
421 if (buf == NULL) {
422 ALOGE("%s: XML_GetBuffer failed", __func__);
423 ret = -ENOMEM;
424 goto err_free_parser;
425 }
426
427 bytes_read = fread(buf, 1, BUF_SIZE, file);
428 if (bytes_read < 0) {
429 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
430 ret = bytes_read;
431 goto err_free_parser;
432 }
433
434 if (XML_ParseBuffer(parser, bytes_read,
435 bytes_read == 0) == XML_STATUS_ERROR) {
436 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800437 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800438 ret = -EINVAL;
439 goto err_free_parser;
440 }
441
442 if (bytes_read == 0)
443 break;
444 }
445
Ben Romberger55886882014-01-10 13:49:02 -0800446err_free_parser:
447 XML_ParserFree(parser);
448err_close_file:
449 fclose(file);
450done:
451 return ret;
452}