blob: 50e19c62eea117ec44de793ca1bda34bf47bf058 [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,
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +053047 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 Kasam44f49402015-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);
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +053058static 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 Kasam44f49402015-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,
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +053069 [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 Kasam44f49402015-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 Kattaeef44a92015-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 Kasam44f49402015-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 Kattaeef44a92015-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__);
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530287 goto done;
288 }
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800289
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530290done:
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: NATIVE_AUDIO_44 was not set!", __func__);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800311 goto done;
312 }
313
314done:
315 return;
316}
317
Laxminath Kasam44f49402015-05-29 18:37:11 +0530318static void process_tz_name(const XML_Char **attr)
319{
320 int ret, index;
321
322 if (strcmp(attr[0], "name") != 0) {
323 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
324 goto done;
325 }
326
327 index = platform_get_snd_device_index((char *)attr[1]);
328 if (index < 0) {
329 ALOGE("%s: Device %s not found, no snd device set!",
330 __func__, attr[1]);
331 goto done;
332 }
333
334 if (strcmp(attr[2], "spkr_1_tz_name") != 0) {
335 ALOGE("%s: Device %s has no spkr_1_tz_name set!",
336 __func__, attr[1]);
337 }
338
339 if (strcmp(attr[4], "spkr_2_tz_name") != 0) {
340 ALOGE("%s: Device %s has no spkr_2_tz_name set!",
341 __func__, attr[1]);
342 }
343
344 ret = platform_set_spkr_device_tz_names(index, (char *)attr[3], (char *)attr[5]);
345 if (ret < 0) {
346 ALOGE("%s: Audio Interface not set!", __func__);
347 goto done;
348 }
349
350done:
351 return;
352}
353
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700354static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800355 const XML_Char **attr)
356{
357 const XML_Char *attr_name = NULL;
358 const XML_Char *attr_value = NULL;
359 unsigned int i;
360
Amit Shekhar5a39c912014-10-14 15:39:30 -0700361 if (strcmp(tag_name, "bit_width_configs") == 0) {
362 section = BITWIDTH;
363 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700364 section = ACDB;
365 } else if (strcmp(tag_name, "pcm_ids") == 0) {
366 section = PCM_ID;
367 } else if (strcmp(tag_name, "backend_names") == 0) {
368 section = BACKEND_NAME;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800369 } else if (strcmp(tag_name, "interface_names") == 0) {
370 section = INTERFACE_NAME;
Laxminath Kasam44f49402015-05-29 18:37:11 +0530371 } else if (strcmp(tag_name, "tz_names") == 0) {
372 section = TZ_NAME;
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530373 } else if (strcmp(tag_name, "native_configs") == 0) {
374 section = NATIVESUPPORT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700375 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800376 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
Laxminath Kasam44f49402015-05-29 18:37:11 +0530377 (section != INTERFACE_NAME) && (section != TZ_NAME)) {
378 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface/tz names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700379 return;
380 }
381
382 /* call into process function for the current section */
383 section_process_fn fn = section_table[section];
384 fn(attr);
385 } else if (strcmp(tag_name, "usecase") == 0) {
386 if (section != PCM_ID) {
387 ALOGE("usecase tag only supported with PCM_ID section");
388 return;
389 }
390
391 section_process_fn fn = section_table[PCM_ID];
392 fn(attr);
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530393 } else if (strcmp(tag_name, "feature") == 0) {
394 if (section != NATIVESUPPORT) {
395 ALOGE("usecase tag only supported with NATIVESUPPORT section");
396 return;
397 }
398
399 section_process_fn fn = section_table[NATIVESUPPORT];
400 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700401 }
Ben Romberger55886882014-01-10 13:49:02 -0800402
403 return;
404}
405
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700406static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800407{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700408 if (strcmp(tag_name, "bit_width_configs") == 0) {
409 section = ROOT;
410 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700411 section = ROOT;
412 } else if (strcmp(tag_name, "pcm_ids") == 0) {
413 section = ROOT;
414 } else if (strcmp(tag_name, "backend_names") == 0) {
415 section = ROOT;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800416 } else if (strcmp(tag_name, "interface_names") == 0) {
417 section = ROOT;
Preetam Singh Ranawat36f3d372015-06-18 22:59:53 +0530418 } else if (strcmp(tag_name, "native_configs") == 0) {
419 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700420 }
Ben Romberger55886882014-01-10 13:49:02 -0800421}
422
Helen Zeng6a16ad72014-02-23 22:04:44 -0800423int platform_info_init(const char *filename)
Ben Romberger55886882014-01-10 13:49:02 -0800424{
425 XML_Parser parser;
426 FILE *file;
427 int ret = 0;
428 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800429 void *buf;
430
Helen Zeng6a16ad72014-02-23 22:04:44 -0800431 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700432 section = ROOT;
433
Ben Romberger55886882014-01-10 13:49:02 -0800434 if (!file) {
435 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800436 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800437 ret = -ENODEV;
438 goto done;
439 }
440
441 parser = XML_ParserCreate(NULL);
442 if (!parser) {
443 ALOGE("%s: Failed to create XML parser!", __func__);
444 ret = -ENODEV;
445 goto err_close_file;
446 }
447
Ben Romberger55886882014-01-10 13:49:02 -0800448 XML_SetElementHandler(parser, start_tag, end_tag);
449
450 while (1) {
451 buf = XML_GetBuffer(parser, BUF_SIZE);
452 if (buf == NULL) {
453 ALOGE("%s: XML_GetBuffer failed", __func__);
454 ret = -ENOMEM;
455 goto err_free_parser;
456 }
457
458 bytes_read = fread(buf, 1, BUF_SIZE, file);
459 if (bytes_read < 0) {
460 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
461 ret = bytes_read;
462 goto err_free_parser;
463 }
464
465 if (XML_ParseBuffer(parser, bytes_read,
466 bytes_read == 0) == XML_STATUS_ERROR) {
467 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800468 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800469 ret = -EINVAL;
470 goto err_free_parser;
471 }
472
473 if (bytes_read == 0)
474 break;
475 }
476
Ben Romberger55886882014-01-10 13:49:02 -0800477err_free_parser:
478 XML_ParserFree(parser);
479err_close_file:
480 fclose(file);
481done:
482 return ret;
483}