blob: 13a314ebad9258cabcedebb89c989a18b753c43f [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,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070050} section_t;
51
52typedef void (* section_process_fn)(const XML_Char **attr);
53
54static void process_acdb_id(const XML_Char **attr);
Amit Shekhar5a39c912014-10-14 15:39:30 -070055static void process_bit_width(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070056static void process_pcm_id(const XML_Char **attr);
57static void process_backend_name(const XML_Char **attr);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080058static void process_interface_name(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070059static void process_root(const XML_Char **attr);
60
61static section_process_fn section_table[] = {
62 [ROOT] = process_root,
63 [ACDB] = process_acdb_id,
Amit Shekhar5a39c912014-10-14 15:39:30 -070064 [BITWIDTH] = process_bit_width,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070065 [PCM_ID] = process_pcm_id,
66 [BACKEND_NAME] = process_backend_name,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080067 [INTERFACE_NAME] = process_interface_name,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070068};
69
70static section_t section;
71
72/*
73 * <audio_platform_info>
74 * <acdb_ids>
75 * <device name="???" acdb_id="???"/>
76 * ...
77 * ...
78 * </acdb_ids>
79 * <backend_names>
80 * <device name="???" backend="???"/>
81 * ...
82 * ...
83 * </backend_names>
84 * <pcm_ids>
85 * <usecase name="???" type="in/out" id="???"/>
86 * ...
87 * ...
88 * </pcm_ids>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080089 * <interface_names>
90 * <device name="Use audio device name here, not sound device name" interface="PRIMARY_I2S"/>
91 * ...
92 * ...
93 * </interface_names>
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070094 * </audio_platform_info>
95 */
96
97static void process_root(const XML_Char **attr __unused)
98{
99}
100
101/* mapping from usecase to pcm dev id */
102static void process_pcm_id(const XML_Char **attr)
103{
104 int index;
105
106 if (strcmp(attr[0], "name") != 0) {
107 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
108 goto done;
109 }
110
111 index = platform_get_usecase_index((char *)attr[1]);
112 if (index < 0) {
113 ALOGE("%s: usecase %s not found!",
114 __func__, attr[1]);
115 goto done;
116 }
117
118 if (strcmp(attr[2], "type") != 0) {
119 ALOGE("%s: usecase type not mentioned", __func__);
120 goto done;
121 }
122
123 int type = -1;
124
125 if (!strcasecmp((char *)attr[3], "in")) {
126 type = 1;
127 } else if (!strcasecmp((char *)attr[3], "out")) {
128 type = 0;
129 } else {
130 ALOGE("%s: type must be IN or OUT", __func__);
131 goto done;
132 }
133
134 if (strcmp(attr[4], "id") != 0) {
135 ALOGE("%s: usecase id not mentioned", __func__);
136 goto done;
137 }
138
139 int id = atoi((char *)attr[5]);
140
141 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
142 ALOGE("%s: usecase %s type %d id %d was not set!",
143 __func__, attr[1], type, id);
144 goto done;
145 }
146
147done:
148 return;
149}
150
151/* backend to be used for a device */
152static void process_backend_name(const XML_Char **attr)
153{
154 int index;
155
156 if (strcmp(attr[0], "name") != 0) {
157 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
158 goto done;
159 }
160
161 index = platform_get_snd_device_index((char *)attr[1]);
162 if (index < 0) {
163 ALOGE("%s: Device %s not found, no ACDB ID set!",
164 __func__, attr[1]);
165 goto done;
166 }
167
168 if (strcmp(attr[2], "backend") != 0) {
169 ALOGE("%s: Device %s has no backend set!",
170 __func__, attr[1]);
171 goto done;
172 }
173
174 if (platform_set_snd_device_backend(index, attr[3]) < 0) {
175 ALOGE("%s: Device %s backend %s was not set!",
176 __func__, attr[1], attr[3]);
177 goto done;
178 }
179
180done:
181 return;
182}
183
184static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800185{
Ben Romberger61764e32014-01-10 13:49:02 -0800186 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800187
Ben Romberger61764e32014-01-10 13:49:02 -0800188 if (strcmp(attr[0], "name") != 0) {
189 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800190 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800191 }
Ben Romberger55886882014-01-10 13:49:02 -0800192
Ben Romberger61764e32014-01-10 13:49:02 -0800193 index = platform_get_snd_device_index((char *)attr[1]);
194 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800195 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
196 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800197 goto done;
198 }
199
200 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800201 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
202 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800203 goto done;
204 }
205
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700206 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
207 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800208 __func__, attr[1], atoi((char *)attr[3]));
Ben Romberger55886882014-01-10 13:49:02 -0800209 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800210 }
Ben Romberger55886882014-01-10 13:49:02 -0800211
Ben Romberger55886882014-01-10 13:49:02 -0800212done:
213 return;
214}
215
Amit Shekhar5a39c912014-10-14 15:39:30 -0700216static void process_bit_width(const XML_Char **attr)
217{
218 int index;
219
220 if (strcmp(attr[0], "name") != 0) {
221 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
222 goto done;
223 }
224
225 index = platform_get_snd_device_index((char *)attr[1]);
226 if (index < 0) {
227 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
228 __func__, attr[1]);
229 goto done;
230 }
231
232 if (strcmp(attr[2], "bit_width") != 0) {
233 ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!",
234 __func__, attr[1]);
235 goto done;
236 }
237
238 if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) {
239 ALOGE("%s: Device %s, ACDB ID %d was not set!",
240 __func__, attr[1], atoi((char *)attr[3]));
241 goto done;
242 }
243
244done:
245 return;
246}
247
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800248static void process_interface_name(const XML_Char **attr)
249{
250 int ret;
251
252 if (strcmp(attr[0], "name") != 0) {
253 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
254
255 goto done;
256 }
257
258 if (strcmp(attr[2], "interface") != 0) {
259 ALOGE("%s: Device %s has no Audio Interface set!",
260 __func__, attr[1]);
261
262 goto done;
263 }
264
265 ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3]);
266 if (ret < 0) {
267 ALOGE("%s: Audio Interface not set!", __func__);
268
269 goto done;
270 }
271
272done:
273 return;
274}
275
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700276static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800277 const XML_Char **attr)
278{
279 const XML_Char *attr_name = NULL;
280 const XML_Char *attr_value = NULL;
281 unsigned int i;
282
Amit Shekhar5a39c912014-10-14 15:39:30 -0700283 if (strcmp(tag_name, "bit_width_configs") == 0) {
284 section = BITWIDTH;
285 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700286 section = ACDB;
287 } else if (strcmp(tag_name, "pcm_ids") == 0) {
288 section = PCM_ID;
289 } else if (strcmp(tag_name, "backend_names") == 0) {
290 section = BACKEND_NAME;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800291 } else if (strcmp(tag_name, "interface_names") == 0) {
292 section = INTERFACE_NAME;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700293 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800294 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
295 (section != INTERFACE_NAME)) {
296 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700297 return;
298 }
299
300 /* call into process function for the current section */
301 section_process_fn fn = section_table[section];
302 fn(attr);
303 } else if (strcmp(tag_name, "usecase") == 0) {
304 if (section != PCM_ID) {
305 ALOGE("usecase tag only supported with PCM_ID section");
306 return;
307 }
308
309 section_process_fn fn = section_table[PCM_ID];
310 fn(attr);
311 }
Ben Romberger55886882014-01-10 13:49:02 -0800312
313 return;
314}
315
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700316static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800317{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700318 if (strcmp(tag_name, "bit_width_configs") == 0) {
319 section = ROOT;
320 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700321 section = ROOT;
322 } else if (strcmp(tag_name, "pcm_ids") == 0) {
323 section = ROOT;
324 } else if (strcmp(tag_name, "backend_names") == 0) {
325 section = ROOT;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800326 } else if (strcmp(tag_name, "interface_names") == 0) {
327 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700328 }
Ben Romberger55886882014-01-10 13:49:02 -0800329}
330
Helen Zeng6a16ad72014-02-23 22:04:44 -0800331int platform_info_init(const char *filename)
Ben Romberger55886882014-01-10 13:49:02 -0800332{
333 XML_Parser parser;
334 FILE *file;
335 int ret = 0;
336 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800337 void *buf;
338
Helen Zeng6a16ad72014-02-23 22:04:44 -0800339 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700340 section = ROOT;
341
Ben Romberger55886882014-01-10 13:49:02 -0800342 if (!file) {
343 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800344 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800345 ret = -ENODEV;
346 goto done;
347 }
348
349 parser = XML_ParserCreate(NULL);
350 if (!parser) {
351 ALOGE("%s: Failed to create XML parser!", __func__);
352 ret = -ENODEV;
353 goto err_close_file;
354 }
355
Ben Romberger55886882014-01-10 13:49:02 -0800356 XML_SetElementHandler(parser, start_tag, end_tag);
357
358 while (1) {
359 buf = XML_GetBuffer(parser, BUF_SIZE);
360 if (buf == NULL) {
361 ALOGE("%s: XML_GetBuffer failed", __func__);
362 ret = -ENOMEM;
363 goto err_free_parser;
364 }
365
366 bytes_read = fread(buf, 1, BUF_SIZE, file);
367 if (bytes_read < 0) {
368 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
369 ret = bytes_read;
370 goto err_free_parser;
371 }
372
373 if (XML_ParseBuffer(parser, bytes_read,
374 bytes_read == 0) == XML_STATUS_ERROR) {
375 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800376 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800377 ret = -EINVAL;
378 goto err_free_parser;
379 }
380
381 if (bytes_read == 0)
382 break;
383 }
384
Ben Romberger55886882014-01-10 13:49:02 -0800385err_free_parser:
386 XML_ParserFree(parser);
387err_close_file:
388 fclose(file);
389done:
390 return ret;
391}