blob: 02f4988a81bb4ce1df2f7a79003762a5d661cac1 [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,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070051} section_t;
52
53typedef void (* section_process_fn)(const XML_Char **attr);
54
55static void process_acdb_id(const XML_Char **attr);
Amit Shekhar5a39c912014-10-14 15:39:30 -070056static void process_bit_width(const XML_Char **attr);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -080057static void process_native_support(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);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070061static void process_root(const XML_Char **attr);
62
63static section_process_fn section_table[] = {
64 [ROOT] = process_root,
65 [ACDB] = process_acdb_id,
Amit Shekhar5a39c912014-10-14 15:39:30 -070066 [BITWIDTH] = process_bit_width,
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -080067 [NATIVESUPPORT] = process_native_support,
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,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070071};
72
73static section_t section;
74
75/*
76 * <audio_platform_info>
77 * <acdb_ids>
78 * <device name="???" acdb_id="???"/>
79 * ...
80 * ...
81 * </acdb_ids>
82 * <backend_names>
83 * <device name="???" backend="???"/>
84 * ...
85 * ...
86 * </backend_names>
87 * <pcm_ids>
88 * <usecase name="???" type="in/out" id="???"/>
89 * ...
90 * ...
91 * </pcm_ids>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080092 * <interface_names>
Karthik Reddy Katta508eca42015-05-11 13:43:18 +053093 * <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 -080094 * ...
95 * ...
96 * </interface_names>
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070097 * </audio_platform_info>
98 */
99
100static void process_root(const XML_Char **attr __unused)
101{
102}
103
104/* mapping from usecase to pcm dev id */
105static void process_pcm_id(const XML_Char **attr)
106{
107 int index;
108
109 if (strcmp(attr[0], "name") != 0) {
110 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
111 goto done;
112 }
113
114 index = platform_get_usecase_index((char *)attr[1]);
115 if (index < 0) {
116 ALOGE("%s: usecase %s not found!",
117 __func__, attr[1]);
118 goto done;
119 }
120
121 if (strcmp(attr[2], "type") != 0) {
122 ALOGE("%s: usecase type not mentioned", __func__);
123 goto done;
124 }
125
126 int type = -1;
127
128 if (!strcasecmp((char *)attr[3], "in")) {
129 type = 1;
130 } else if (!strcasecmp((char *)attr[3], "out")) {
131 type = 0;
132 } else {
133 ALOGE("%s: type must be IN or OUT", __func__);
134 goto done;
135 }
136
137 if (strcmp(attr[4], "id") != 0) {
138 ALOGE("%s: usecase id not mentioned", __func__);
139 goto done;
140 }
141
142 int id = atoi((char *)attr[5]);
143
144 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
145 ALOGE("%s: usecase %s type %d id %d was not set!",
146 __func__, attr[1], type, id);
147 goto done;
148 }
149
150done:
151 return;
152}
153
154/* backend to be used for a device */
155static void process_backend_name(const XML_Char **attr)
156{
157 int index;
158
159 if (strcmp(attr[0], "name") != 0) {
160 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
161 goto done;
162 }
163
164 index = platform_get_snd_device_index((char *)attr[1]);
165 if (index < 0) {
166 ALOGE("%s: Device %s not found, no ACDB ID set!",
167 __func__, attr[1]);
168 goto done;
169 }
170
171 if (strcmp(attr[2], "backend") != 0) {
172 ALOGE("%s: Device %s has no backend set!",
173 __func__, attr[1]);
174 goto done;
175 }
176
177 if (platform_set_snd_device_backend(index, attr[3]) < 0) {
178 ALOGE("%s: Device %s backend %s was not set!",
179 __func__, attr[1], attr[3]);
180 goto done;
181 }
182
183done:
184 return;
185}
186
187static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800188{
Ben Romberger61764e32014-01-10 13:49:02 -0800189 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800190
Ben Romberger61764e32014-01-10 13:49:02 -0800191 if (strcmp(attr[0], "name") != 0) {
192 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800193 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800194 }
Ben Romberger55886882014-01-10 13:49:02 -0800195
Ben Romberger61764e32014-01-10 13:49:02 -0800196 index = platform_get_snd_device_index((char *)attr[1]);
197 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800198 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
199 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800200 goto done;
201 }
202
203 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800204 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
205 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800206 goto done;
207 }
208
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700209 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
210 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800211 __func__, attr[1], atoi((char *)attr[3]));
Ben Romberger55886882014-01-10 13:49:02 -0800212 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800213 }
Ben Romberger55886882014-01-10 13:49:02 -0800214
Ben Romberger55886882014-01-10 13:49:02 -0800215done:
216 return;
217}
218
Amit Shekhar5a39c912014-10-14 15:39:30 -0700219static void process_bit_width(const XML_Char **attr)
220{
221 int index;
222
223 if (strcmp(attr[0], "name") != 0) {
224 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
225 goto done;
226 }
227
228 index = platform_get_snd_device_index((char *)attr[1]);
229 if (index < 0) {
230 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
231 __func__, attr[1]);
232 goto done;
233 }
234
235 if (strcmp(attr[2], "bit_width") != 0) {
236 ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!",
237 __func__, attr[1]);
238 goto done;
239 }
240
241 if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) {
242 ALOGE("%s: Device %s, ACDB ID %d was not set!",
243 __func__, attr[1], atoi((char *)attr[3]));
244 goto done;
245 }
246
247done:
248 return;
249}
250
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800251static void process_interface_name(const XML_Char **attr)
252{
253 int ret;
254
255 if (strcmp(attr[0], "name") != 0) {
256 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
257
258 goto done;
259 }
260
261 if (strcmp(attr[2], "interface") != 0) {
262 ALOGE("%s: Device %s has no Audio Interface set!",
263 __func__, attr[1]);
264
265 goto done;
266 }
267
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530268 if (strcmp(attr[4], "codec_type") != 0) {
269 ALOGE("%s: Device %s has no codec type set!",
270 __func__, attr[1]);
271
272 goto done;
273 }
274
275 ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3],
276 (char *)attr[5]);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800277 if (ret < 0) {
278 ALOGE("%s: Audio Interface not set!", __func__);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800279 goto done;
280 }
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800281
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800282done:
283 return;
284}
285
286static void process_native_support(const XML_Char **attr)
287{
288 int index;
289
290 if (strcmp(attr[0], "name") != 0) {
291 ALOGE("%s: 'name' not found, no NATIVE_AUDIO_44.1 set!", __func__);
292 goto done;
293 }
294
295 if (strcmp(attr[2], "codec_support") != 0) {
296 ALOGE("%s: NATIVE_AUDIO_44.1 in platform info xml has no codec_support set!",
297 __func__);
298 goto done;
299 }
300
301 if (platform_set_native_support(atoi((char *)attr[3])) < 0) {
302 ALOGE("%s: Device %s, ACDB ID %d was not set!",
303 __func__, attr[1], atoi((char *)attr[3]));
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800304 goto done;
305 }
306
307done:
308 return;
309}
310
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700311static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800312 const XML_Char **attr)
313{
314 const XML_Char *attr_name = NULL;
315 const XML_Char *attr_value = NULL;
316 unsigned int i;
317
Amit Shekhar5a39c912014-10-14 15:39:30 -0700318 if (strcmp(tag_name, "bit_width_configs") == 0) {
319 section = BITWIDTH;
320 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700321 section = ACDB;
322 } else if (strcmp(tag_name, "pcm_ids") == 0) {
323 section = PCM_ID;
324 } else if (strcmp(tag_name, "backend_names") == 0) {
325 section = BACKEND_NAME;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800326 } else if (strcmp(tag_name, "interface_names") == 0) {
327 section = INTERFACE_NAME;
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800328 } else if (strcmp(tag_name, "native_configs") == 0) {
329 section = NATIVESUPPORT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700330 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800331 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
332 (section != INTERFACE_NAME)) {
333 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700334 return;
335 }
336
337 /* call into process function for the current section */
338 section_process_fn fn = section_table[section];
339 fn(attr);
340 } else if (strcmp(tag_name, "usecase") == 0) {
341 if (section != PCM_ID) {
342 ALOGE("usecase tag only supported with PCM_ID section");
343 return;
344 }
345
346 section_process_fn fn = section_table[PCM_ID];
347 fn(attr);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800348 } else if (strcmp(tag_name, "feature") == 0) {
349 if (section != NATIVESUPPORT) {
350 ALOGE("usecase tag only supported with NATIVESUPPORT section");
351 return;
352 }
353
354 section_process_fn fn = section_table[NATIVESUPPORT];
355 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700356 }
Ben Romberger55886882014-01-10 13:49:02 -0800357
358 return;
359}
360
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700361static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800362{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700363 if (strcmp(tag_name, "bit_width_configs") == 0) {
364 section = ROOT;
365 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700366 section = ROOT;
367 } else if (strcmp(tag_name, "pcm_ids") == 0) {
368 section = ROOT;
369 } else if (strcmp(tag_name, "backend_names") == 0) {
370 section = ROOT;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800371 } else if (strcmp(tag_name, "interface_names") == 0) {
372 section = ROOT;
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800373 } else if (strcmp(tag_name, "native_configs") == 0) {
374 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700375 }
Ben Romberger55886882014-01-10 13:49:02 -0800376}
377
Helen Zeng6a16ad72014-02-23 22:04:44 -0800378int platform_info_init(const char *filename)
Ben Romberger55886882014-01-10 13:49:02 -0800379{
380 XML_Parser parser;
381 FILE *file;
382 int ret = 0;
383 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800384 void *buf;
385
Helen Zeng6a16ad72014-02-23 22:04:44 -0800386 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700387 section = ROOT;
388
Ben Romberger55886882014-01-10 13:49:02 -0800389 if (!file) {
390 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800391 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800392 ret = -ENODEV;
393 goto done;
394 }
395
396 parser = XML_ParserCreate(NULL);
397 if (!parser) {
398 ALOGE("%s: Failed to create XML parser!", __func__);
399 ret = -ENODEV;
400 goto err_close_file;
401 }
402
Ben Romberger55886882014-01-10 13:49:02 -0800403 XML_SetElementHandler(parser, start_tag, end_tag);
404
405 while (1) {
406 buf = XML_GetBuffer(parser, BUF_SIZE);
407 if (buf == NULL) {
408 ALOGE("%s: XML_GetBuffer failed", __func__);
409 ret = -ENOMEM;
410 goto err_free_parser;
411 }
412
413 bytes_read = fread(buf, 1, BUF_SIZE, file);
414 if (bytes_read < 0) {
415 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
416 ret = bytes_read;
417 goto err_free_parser;
418 }
419
420 if (XML_ParseBuffer(parser, bytes_read,
421 bytes_read == 0) == XML_STATUS_ERROR) {
422 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800423 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800424 ret = -EINVAL;
425 goto err_free_parser;
426 }
427
428 if (bytes_read == 0)
429 break;
430 }
431
Ben Romberger55886882014-01-10 13:49:02 -0800432err_free_parser:
433 XML_ParserFree(parser);
434err_close_file:
435 fclose(file);
436done:
437 return ret;
438}