blob: 6a35be55d7a3a0dff4f5d42929e66fc39feb4d86 [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,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070047 PCM_ID,
48 BACKEND_NAME,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080049 INTERFACE_NAME,
Laxminath Kasam44f49402015-05-29 18:37:11 +053050 TZ_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);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070057static void process_pcm_id(const XML_Char **attr);
58static void process_backend_name(const XML_Char **attr);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080059static void process_interface_name(const XML_Char **attr);
Laxminath Kasam44f49402015-05-29 18:37:11 +053060static void process_tz_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,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070067 [PCM_ID] = process_pcm_id,
68 [BACKEND_NAME] = process_backend_name,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080069 [INTERFACE_NAME] = process_interface_name,
Laxminath Kasam44f49402015-05-29 18:37:11 +053070 [TZ_NAME] = process_tz_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 Kattaeef44a92015-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>
Laxminath Kasam44f49402015-05-29 18:37:11 +053097 * <tz_names>
98 * <device name="???" spkr_1_tz_name="???" spkr_2_tz_name="???"/>
99 * ...
100 * ...
101 * </tz_names>
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700102 * </audio_platform_info>
103 */
104
105static void process_root(const XML_Char **attr __unused)
106{
107}
108
109/* mapping from usecase to pcm dev id */
110static void process_pcm_id(const XML_Char **attr)
111{
112 int index;
113
114 if (strcmp(attr[0], "name") != 0) {
115 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
116 goto done;
117 }
118
119 index = platform_get_usecase_index((char *)attr[1]);
120 if (index < 0) {
121 ALOGE("%s: usecase %s not found!",
122 __func__, attr[1]);
123 goto done;
124 }
125
126 if (strcmp(attr[2], "type") != 0) {
127 ALOGE("%s: usecase type not mentioned", __func__);
128 goto done;
129 }
130
131 int type = -1;
132
133 if (!strcasecmp((char *)attr[3], "in")) {
134 type = 1;
135 } else if (!strcasecmp((char *)attr[3], "out")) {
136 type = 0;
137 } else {
138 ALOGE("%s: type must be IN or OUT", __func__);
139 goto done;
140 }
141
142 if (strcmp(attr[4], "id") != 0) {
143 ALOGE("%s: usecase id not mentioned", __func__);
144 goto done;
145 }
146
147 int id = atoi((char *)attr[5]);
148
149 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
150 ALOGE("%s: usecase %s type %d id %d was not set!",
151 __func__, attr[1], type, id);
152 goto done;
153 }
154
155done:
156 return;
157}
158
159/* backend to be used for a device */
160static void process_backend_name(const XML_Char **attr)
161{
162 int index;
163
164 if (strcmp(attr[0], "name") != 0) {
165 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
166 goto done;
167 }
168
169 index = platform_get_snd_device_index((char *)attr[1]);
170 if (index < 0) {
171 ALOGE("%s: Device %s not found, no ACDB ID set!",
172 __func__, attr[1]);
173 goto done;
174 }
175
176 if (strcmp(attr[2], "backend") != 0) {
177 ALOGE("%s: Device %s has no backend set!",
178 __func__, attr[1]);
179 goto done;
180 }
181
182 if (platform_set_snd_device_backend(index, attr[3]) < 0) {
183 ALOGE("%s: Device %s backend %s was not set!",
184 __func__, attr[1], attr[3]);
185 goto done;
186 }
187
188done:
189 return;
190}
191
192static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800193{
Ben Romberger61764e32014-01-10 13:49:02 -0800194 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800195
Ben Romberger61764e32014-01-10 13:49:02 -0800196 if (strcmp(attr[0], "name") != 0) {
197 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800198 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800199 }
Ben Romberger55886882014-01-10 13:49:02 -0800200
Ben Romberger61764e32014-01-10 13:49:02 -0800201 index = platform_get_snd_device_index((char *)attr[1]);
202 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800203 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
204 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800205 goto done;
206 }
207
208 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800209 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
210 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800211 goto done;
212 }
213
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700214 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
215 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800216 __func__, attr[1], atoi((char *)attr[3]));
Ben Romberger55886882014-01-10 13:49:02 -0800217 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800218 }
Ben Romberger55886882014-01-10 13:49:02 -0800219
Ben Romberger55886882014-01-10 13:49:02 -0800220done:
221 return;
222}
223
Amit Shekhar5a39c912014-10-14 15:39:30 -0700224static void process_bit_width(const XML_Char **attr)
225{
226 int index;
227
228 if (strcmp(attr[0], "name") != 0) {
229 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
230 goto done;
231 }
232
233 index = platform_get_snd_device_index((char *)attr[1]);
234 if (index < 0) {
235 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
236 __func__, attr[1]);
237 goto done;
238 }
239
240 if (strcmp(attr[2], "bit_width") != 0) {
241 ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!",
242 __func__, attr[1]);
243 goto done;
244 }
245
246 if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) {
247 ALOGE("%s: Device %s, ACDB ID %d was not set!",
248 __func__, attr[1], atoi((char *)attr[3]));
249 goto done;
250 }
251
252done:
253 return;
254}
255
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800256static void process_interface_name(const XML_Char **attr)
257{
258 int ret;
259
260 if (strcmp(attr[0], "name") != 0) {
261 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
262
263 goto done;
264 }
265
266 if (strcmp(attr[2], "interface") != 0) {
267 ALOGE("%s: Device %s has no Audio Interface set!",
268 __func__, attr[1]);
269
270 goto done;
271 }
272
Karthik Reddy Kattaeef44a92015-05-11 13:43:18 +0530273 if (strcmp(attr[4], "codec_type") != 0) {
274 ALOGE("%s: Device %s has no codec type set!",
275 __func__, attr[1]);
276
277 goto done;
278 }
279
280 ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3],
281 (char *)attr[5]);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800282 if (ret < 0) {
283 ALOGE("%s: Audio Interface not set!", __func__);
284
285 goto done;
286 }
287
288done:
289 return;
290}
291
Laxminath Kasam44f49402015-05-29 18:37:11 +0530292static void process_tz_name(const XML_Char **attr)
293{
294 int ret, index;
295
296 if (strcmp(attr[0], "name") != 0) {
297 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
298 goto done;
299 }
300
301 index = platform_get_snd_device_index((char *)attr[1]);
302 if (index < 0) {
303 ALOGE("%s: Device %s not found, no snd device set!",
304 __func__, attr[1]);
305 goto done;
306 }
307
308 if (strcmp(attr[2], "spkr_1_tz_name") != 0) {
309 ALOGE("%s: Device %s has no spkr_1_tz_name set!",
310 __func__, attr[1]);
311 }
312
313 if (strcmp(attr[4], "spkr_2_tz_name") != 0) {
314 ALOGE("%s: Device %s has no spkr_2_tz_name set!",
315 __func__, attr[1]);
316 }
317
318 ret = platform_set_spkr_device_tz_names(index, (char *)attr[3], (char *)attr[5]);
319 if (ret < 0) {
320 ALOGE("%s: Audio Interface not set!", __func__);
321 goto done;
322 }
323
324done:
325 return;
326}
327
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700328static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800329 const XML_Char **attr)
330{
331 const XML_Char *attr_name = NULL;
332 const XML_Char *attr_value = NULL;
333 unsigned int i;
334
Amit Shekhar5a39c912014-10-14 15:39:30 -0700335 if (strcmp(tag_name, "bit_width_configs") == 0) {
336 section = BITWIDTH;
337 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700338 section = ACDB;
339 } else if (strcmp(tag_name, "pcm_ids") == 0) {
340 section = PCM_ID;
341 } else if (strcmp(tag_name, "backend_names") == 0) {
342 section = BACKEND_NAME;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800343 } else if (strcmp(tag_name, "interface_names") == 0) {
344 section = INTERFACE_NAME;
Laxminath Kasam44f49402015-05-29 18:37:11 +0530345 } else if (strcmp(tag_name, "tz_names") == 0) {
346 section = TZ_NAME;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700347 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800348 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
Laxminath Kasam44f49402015-05-29 18:37:11 +0530349 (section != INTERFACE_NAME) && (section != TZ_NAME)) {
350 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface/tz names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700351 return;
352 }
353
354 /* call into process function for the current section */
355 section_process_fn fn = section_table[section];
356 fn(attr);
357 } else if (strcmp(tag_name, "usecase") == 0) {
358 if (section != PCM_ID) {
359 ALOGE("usecase tag only supported with PCM_ID section");
360 return;
361 }
362
363 section_process_fn fn = section_table[PCM_ID];
364 fn(attr);
365 }
Ben Romberger55886882014-01-10 13:49:02 -0800366
367 return;
368}
369
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700370static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800371{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700372 if (strcmp(tag_name, "bit_width_configs") == 0) {
373 section = ROOT;
374 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700375 section = ROOT;
376 } else if (strcmp(tag_name, "pcm_ids") == 0) {
377 section = ROOT;
378 } else if (strcmp(tag_name, "backend_names") == 0) {
379 section = ROOT;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800380 } else if (strcmp(tag_name, "interface_names") == 0) {
381 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700382 }
Ben Romberger55886882014-01-10 13:49:02 -0800383}
384
Helen Zeng6a16ad72014-02-23 22:04:44 -0800385int platform_info_init(const char *filename)
Ben Romberger55886882014-01-10 13:49:02 -0800386{
387 XML_Parser parser;
388 FILE *file;
389 int ret = 0;
390 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800391 void *buf;
392
Helen Zeng6a16ad72014-02-23 22:04:44 -0800393 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700394 section = ROOT;
395
Ben Romberger55886882014-01-10 13:49:02 -0800396 if (!file) {
397 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800398 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800399 ret = -ENODEV;
400 goto done;
401 }
402
403 parser = XML_ParserCreate(NULL);
404 if (!parser) {
405 ALOGE("%s: Failed to create XML parser!", __func__);
406 ret = -ENODEV;
407 goto err_close_file;
408 }
409
Ben Romberger55886882014-01-10 13:49:02 -0800410 XML_SetElementHandler(parser, start_tag, end_tag);
411
412 while (1) {
413 buf = XML_GetBuffer(parser, BUF_SIZE);
414 if (buf == NULL) {
415 ALOGE("%s: XML_GetBuffer failed", __func__);
416 ret = -ENOMEM;
417 goto err_free_parser;
418 }
419
420 bytes_read = fread(buf, 1, BUF_SIZE, file);
421 if (bytes_read < 0) {
422 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
423 ret = bytes_read;
424 goto err_free_parser;
425 }
426
427 if (XML_ParseBuffer(parser, bytes_read,
428 bytes_read == 0) == XML_STATUS_ERROR) {
429 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800430 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800431 ret = -EINVAL;
432 goto err_free_parser;
433 }
434
435 if (bytes_read == 0)
436 break;
437 }
438
Ben Romberger55886882014-01-10 13:49:02 -0800439err_free_parser:
440 XML_ParserFree(parser);
441err_close_file:
442 fclose(file);
443done:
444 return ret;
445}