blob: e6cc15d1e333a9deacb01cf76e322de280630562 [file] [log] [blame]
Ben Romberger55886882014-01-10 13:49:02 -08001/*
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002 * Copyright (c) 2014-2015, 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>
37#include <audio_hw.h>
Ben Romberger61764e32014-01-10 13:49:02 -080038#include "platform_api.h"
39#include <platform.h>
Ben Romberger55886882014-01-10 13:49:02 -080040
Ben Romberger55886882014-01-10 13:49:02 -080041#define BUF_SIZE 1024
42
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070043typedef enum {
44 ROOT,
45 ACDB,
Amit Shekhar5a39c912014-10-14 15:39:30 -070046 BITWIDTH,
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -080047 NATIVESUPPORT,
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,
Laxminath Kasam82252432015-05-29 18:37:11 +053051 TZ_NAME,
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);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -080058static void process_native_support(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);
Laxminath Kasam82252432015-05-29 18:37:11 +053062static void process_tz_name(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070063static void process_root(const XML_Char **attr);
64
65static section_process_fn section_table[] = {
66 [ROOT] = process_root,
67 [ACDB] = process_acdb_id,
Amit Shekhar5a39c912014-10-14 15:39:30 -070068 [BITWIDTH] = process_bit_width,
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -080069 [NATIVESUPPORT] = process_native_support,
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,
Laxminath Kasam82252432015-05-29 18:37:11 +053073 [TZ_NAME] = process_tz_name,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070074};
75
76static section_t section;
77
78/*
79 * <audio_platform_info>
80 * <acdb_ids>
81 * <device name="???" acdb_id="???"/>
82 * ...
83 * ...
84 * </acdb_ids>
85 * <backend_names>
86 * <device name="???" backend="???"/>
87 * ...
88 * ...
89 * </backend_names>
90 * <pcm_ids>
91 * <usecase name="???" type="in/out" id="???"/>
92 * ...
93 * ...
94 * </pcm_ids>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080095 * <interface_names>
Karthik Reddy Katta508eca42015-05-11 13:43:18 +053096 * <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 -080097 * ...
98 * ...
99 * </interface_names>
Laxminath Kasam82252432015-05-29 18:37:11 +0530100 * <tz_names>
101 * <device name="???" spkr_1_tz_name="???" spkr_2_tz_name="???"/>
102 * ...
103 * ...
104 * </tz_names>
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700105 * </audio_platform_info>
106 */
107
108static void process_root(const XML_Char **attr __unused)
109{
110}
111
112/* mapping from usecase to pcm dev id */
113static void process_pcm_id(const XML_Char **attr)
114{
115 int index;
116
117 if (strcmp(attr[0], "name") != 0) {
118 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
119 goto done;
120 }
121
122 index = platform_get_usecase_index((char *)attr[1]);
123 if (index < 0) {
124 ALOGE("%s: usecase %s not found!",
125 __func__, attr[1]);
126 goto done;
127 }
128
129 if (strcmp(attr[2], "type") != 0) {
130 ALOGE("%s: usecase type not mentioned", __func__);
131 goto done;
132 }
133
134 int type = -1;
135
136 if (!strcasecmp((char *)attr[3], "in")) {
137 type = 1;
138 } else if (!strcasecmp((char *)attr[3], "out")) {
139 type = 0;
140 } else {
141 ALOGE("%s: type must be IN or OUT", __func__);
142 goto done;
143 }
144
145 if (strcmp(attr[4], "id") != 0) {
146 ALOGE("%s: usecase id not mentioned", __func__);
147 goto done;
148 }
149
150 int id = atoi((char *)attr[5]);
151
152 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
153 ALOGE("%s: usecase %s type %d id %d was not set!",
154 __func__, attr[1], type, id);
155 goto done;
156 }
157
158done:
159 return;
160}
161
162/* backend to be used for a device */
163static void process_backend_name(const XML_Char **attr)
164{
165 int index;
166
167 if (strcmp(attr[0], "name") != 0) {
168 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
169 goto done;
170 }
171
172 index = platform_get_snd_device_index((char *)attr[1]);
173 if (index < 0) {
174 ALOGE("%s: Device %s not found, no ACDB ID set!",
175 __func__, attr[1]);
176 goto done;
177 }
178
179 if (strcmp(attr[2], "backend") != 0) {
180 ALOGE("%s: Device %s has no backend set!",
181 __func__, attr[1]);
182 goto done;
183 }
184
185 if (platform_set_snd_device_backend(index, attr[3]) < 0) {
186 ALOGE("%s: Device %s backend %s was not set!",
187 __func__, attr[1], attr[3]);
188 goto done;
189 }
190
191done:
192 return;
193}
194
195static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800196{
Ben Romberger61764e32014-01-10 13:49:02 -0800197 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800198
Ben Romberger61764e32014-01-10 13:49:02 -0800199 if (strcmp(attr[0], "name") != 0) {
200 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800201 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800202 }
Ben Romberger55886882014-01-10 13:49:02 -0800203
Ben Romberger61764e32014-01-10 13:49:02 -0800204 index = platform_get_snd_device_index((char *)attr[1]);
205 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800206 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
207 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800208 goto done;
209 }
210
211 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800212 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
213 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800214 goto done;
215 }
216
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700217 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
218 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800219 __func__, attr[1], atoi((char *)attr[3]));
Ben Romberger55886882014-01-10 13:49:02 -0800220 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800221 }
Ben Romberger55886882014-01-10 13:49:02 -0800222
Ben Romberger55886882014-01-10 13:49:02 -0800223done:
224 return;
225}
226
Amit Shekhar5a39c912014-10-14 15:39:30 -0700227static void process_bit_width(const XML_Char **attr)
228{
229 int index;
230
231 if (strcmp(attr[0], "name") != 0) {
232 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
233 goto done;
234 }
235
236 index = platform_get_snd_device_index((char *)attr[1]);
237 if (index < 0) {
238 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
239 __func__, attr[1]);
240 goto done;
241 }
242
243 if (strcmp(attr[2], "bit_width") != 0) {
244 ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!",
245 __func__, attr[1]);
246 goto done;
247 }
248
249 if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) {
250 ALOGE("%s: Device %s, ACDB ID %d was not set!",
251 __func__, attr[1], atoi((char *)attr[3]));
252 goto done;
253 }
254
255done:
256 return;
257}
258
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800259static void process_interface_name(const XML_Char **attr)
260{
261 int ret;
262
263 if (strcmp(attr[0], "name") != 0) {
264 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
265
266 goto done;
267 }
268
269 if (strcmp(attr[2], "interface") != 0) {
270 ALOGE("%s: Device %s has no Audio Interface set!",
271 __func__, attr[1]);
272
273 goto done;
274 }
275
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530276 if (strcmp(attr[4], "codec_type") != 0) {
277 ALOGE("%s: Device %s has no codec type set!",
278 __func__, attr[1]);
279
280 goto done;
281 }
282
283 ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3],
284 (char *)attr[5]);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800285 if (ret < 0) {
286 ALOGE("%s: Audio Interface not set!", __func__);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800287 goto done;
288 }
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800289
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800290done:
291 return;
292}
293
294static void process_native_support(const XML_Char **attr)
295{
296 int index;
297
298 if (strcmp(attr[0], "name") != 0) {
299 ALOGE("%s: 'name' not found, no NATIVE_AUDIO_44.1 set!", __func__);
300 goto done;
301 }
302
303 if (strcmp(attr[2], "codec_support") != 0) {
304 ALOGE("%s: NATIVE_AUDIO_44.1 in platform info xml has no codec_support set!",
305 __func__);
306 goto done;
307 }
308
309 if (platform_set_native_support(atoi((char *)attr[3])) < 0) {
310 ALOGE("%s: Device %s, ACDB ID %d was not set!",
311 __func__, attr[1], atoi((char *)attr[3]));
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800312 goto done;
313 }
314
315done:
316 return;
317}
318
Laxminath Kasam82252432015-05-29 18:37:11 +0530319static void process_tz_name(const XML_Char **attr)
320{
321 int ret, index;
322
323 if (strcmp(attr[0], "name") != 0) {
324 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
325 goto done;
326 }
327
328 index = platform_get_snd_device_index((char *)attr[1]);
329 if (index < 0) {
330 ALOGE("%s: Device %s not found, no snd device set!",
331 __func__, attr[1]);
332 goto done;
333 }
334
335 if (strcmp(attr[2], "spkr_1_tz_name") != 0) {
336 ALOGE("%s: Device %s has no spkr_1_tz_name set!",
337 __func__, attr[1]);
338 }
339
340 if (strcmp(attr[4], "spkr_2_tz_name") != 0) {
341 ALOGE("%s: Device %s has no spkr_2_tz_name set!",
342 __func__, attr[1]);
343 }
344
345 ret = platform_set_spkr_device_tz_names(index, (char *)attr[3], (char *)attr[5]);
346 if (ret < 0) {
347 ALOGE("%s: Audio Interface not set!", __func__);
348 goto done;
349 }
350
351done:
352 return;
353}
354
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700355static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800356 const XML_Char **attr)
357{
358 const XML_Char *attr_name = NULL;
359 const XML_Char *attr_value = NULL;
360 unsigned int i;
361
Amit Shekhar5a39c912014-10-14 15:39:30 -0700362 if (strcmp(tag_name, "bit_width_configs") == 0) {
363 section = BITWIDTH;
364 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700365 section = ACDB;
366 } else if (strcmp(tag_name, "pcm_ids") == 0) {
367 section = PCM_ID;
368 } else if (strcmp(tag_name, "backend_names") == 0) {
369 section = BACKEND_NAME;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800370 } else if (strcmp(tag_name, "interface_names") == 0) {
371 section = INTERFACE_NAME;
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800372 } else if (strcmp(tag_name, "native_configs") == 0) {
373 section = NATIVESUPPORT;
Laxminath Kasam82252432015-05-29 18:37:11 +0530374 } else if (strcmp(tag_name, "tz_names") == 0) {
375 section = TZ_NAME;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700376 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800377 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
Laxminath Kasam82252432015-05-29 18:37:11 +0530378 (section != INTERFACE_NAME) && (section != TZ_NAME)) {
379 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface/tz names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700380 return;
381 }
382
383 /* call into process function for the current section */
384 section_process_fn fn = section_table[section];
385 fn(attr);
386 } else if (strcmp(tag_name, "usecase") == 0) {
387 if (section != PCM_ID) {
388 ALOGE("usecase tag only supported with PCM_ID section");
389 return;
390 }
391
392 section_process_fn fn = section_table[PCM_ID];
393 fn(attr);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800394 } else if (strcmp(tag_name, "feature") == 0) {
395 if (section != NATIVESUPPORT) {
396 ALOGE("usecase tag only supported with NATIVESUPPORT section");
397 return;
398 }
399
400 section_process_fn fn = section_table[NATIVESUPPORT];
401 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700402 }
Ben Romberger55886882014-01-10 13:49:02 -0800403
404 return;
405}
406
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700407static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800408{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700409 if (strcmp(tag_name, "bit_width_configs") == 0) {
410 section = ROOT;
411 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700412 section = ROOT;
413 } else if (strcmp(tag_name, "pcm_ids") == 0) {
414 section = ROOT;
415 } else if (strcmp(tag_name, "backend_names") == 0) {
416 section = ROOT;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800417 } else if (strcmp(tag_name, "interface_names") == 0) {
418 section = ROOT;
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800419 } else if (strcmp(tag_name, "native_configs") == 0) {
420 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700421 }
Ben Romberger55886882014-01-10 13:49:02 -0800422}
423
Helen Zeng6a16ad72014-02-23 22:04:44 -0800424int platform_info_init(const char *filename)
Ben Romberger55886882014-01-10 13:49:02 -0800425{
426 XML_Parser parser;
427 FILE *file;
428 int ret = 0;
429 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800430 void *buf;
431
Helen Zeng6a16ad72014-02-23 22:04:44 -0800432 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700433 section = ROOT;
434
Ben Romberger55886882014-01-10 13:49:02 -0800435 if (!file) {
436 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800437 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800438 ret = -ENODEV;
439 goto done;
440 }
441
442 parser = XML_ParserCreate(NULL);
443 if (!parser) {
444 ALOGE("%s: Failed to create XML parser!", __func__);
445 ret = -ENODEV;
446 goto err_close_file;
447 }
448
Ben Romberger55886882014-01-10 13:49:02 -0800449 XML_SetElementHandler(parser, start_tag, end_tag);
450
451 while (1) {
452 buf = XML_GetBuffer(parser, BUF_SIZE);
453 if (buf == NULL) {
454 ALOGE("%s: XML_GetBuffer failed", __func__);
455 ret = -ENOMEM;
456 goto err_free_parser;
457 }
458
459 bytes_read = fread(buf, 1, BUF_SIZE, file);
460 if (bytes_read < 0) {
461 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
462 ret = bytes_read;
463 goto err_free_parser;
464 }
465
466 if (XML_ParseBuffer(parser, bytes_read,
467 bytes_read == 0) == XML_STATUS_ERROR) {
468 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800469 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800470 ret = -EINVAL;
471 goto err_free_parser;
472 }
473
474 if (bytes_read == 0)
475 break;
476 }
477
Ben Romberger55886882014-01-10 13:49:02 -0800478err_free_parser:
479 XML_ParserFree(parser);
480err_close_file:
481 fclose(file);
482done:
483 return ret;
484}