Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 1 | /* |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 2 | * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 3 | * |
| 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 Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 30 | #define LOG_TAG "platform_info" |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 31 | #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 Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 38 | #include "platform_api.h" |
| 39 | #include <platform.h> |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 40 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 41 | #define BUF_SIZE 1024 |
| 42 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 43 | typedef enum { |
| 44 | ROOT, |
| 45 | ACDB, |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 46 | BITWIDTH, |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 47 | PCM_ID, |
| 48 | BACKEND_NAME, |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 49 | INTERFACE_NAME, |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 50 | } section_t; |
| 51 | |
| 52 | typedef void (* section_process_fn)(const XML_Char **attr); |
| 53 | |
| 54 | static void process_acdb_id(const XML_Char **attr); |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 55 | static void process_bit_width(const XML_Char **attr); |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 56 | static void process_pcm_id(const XML_Char **attr); |
| 57 | static void process_backend_name(const XML_Char **attr); |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 58 | static void process_interface_name(const XML_Char **attr); |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 59 | static void process_root(const XML_Char **attr); |
| 60 | |
| 61 | static section_process_fn section_table[] = { |
| 62 | [ROOT] = process_root, |
| 63 | [ACDB] = process_acdb_id, |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 64 | [BITWIDTH] = process_bit_width, |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 65 | [PCM_ID] = process_pcm_id, |
| 66 | [BACKEND_NAME] = process_backend_name, |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 67 | [INTERFACE_NAME] = process_interface_name, |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static 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 Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 89 | * <interface_names> |
| 90 | * <device name="Use audio device name here, not sound device name" interface="PRIMARY_I2S"/> |
| 91 | * ... |
| 92 | * ... |
| 93 | * </interface_names> |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 94 | * </audio_platform_info> |
| 95 | */ |
| 96 | |
| 97 | static void process_root(const XML_Char **attr __unused) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | /* mapping from usecase to pcm dev id */ |
| 102 | static 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 | |
| 147 | done: |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | /* backend to be used for a device */ |
| 152 | static 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 | |
| 180 | done: |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | static void process_acdb_id(const XML_Char **attr) |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 185 | { |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 186 | int index; |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 187 | |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 188 | if (strcmp(attr[0], "name") != 0) { |
| 189 | ALOGE("%s: 'name' not found, no ACDB ID set!", __func__); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 190 | goto done; |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 191 | } |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 192 | |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 193 | index = platform_get_snd_device_index((char *)attr[1]); |
| 194 | if (index < 0) { |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 195 | ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!", |
| 196 | __func__, attr[1]); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 197 | goto done; |
| 198 | } |
| 199 | |
| 200 | if (strcmp(attr[2], "acdb_id") != 0) { |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 201 | ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!", |
| 202 | __func__, attr[1]); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 203 | goto done; |
| 204 | } |
| 205 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 206 | 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 Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 208 | __func__, attr[1], atoi((char *)attr[3])); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 209 | goto done; |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 210 | } |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 211 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 212 | done: |
| 213 | return; |
| 214 | } |
| 215 | |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 216 | static 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 | |
| 244 | done: |
| 245 | return; |
| 246 | } |
| 247 | |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 248 | static 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 | |
| 272 | done: |
| 273 | return; |
| 274 | } |
| 275 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 276 | static void start_tag(void *userdata __unused, const XML_Char *tag_name, |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 277 | 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 Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 283 | if (strcmp(tag_name, "bit_width_configs") == 0) { |
| 284 | section = BITWIDTH; |
| 285 | } else if (strcmp(tag_name, "acdb_ids") == 0) { |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 286 | 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 Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 291 | } else if (strcmp(tag_name, "interface_names") == 0) { |
| 292 | section = INTERFACE_NAME; |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 293 | } else if (strcmp(tag_name, "device") == 0) { |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 294 | 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 George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 297 | 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 Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 312 | |
| 313 | return; |
| 314 | } |
| 315 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 316 | static void end_tag(void *userdata __unused, const XML_Char *tag_name) |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 317 | { |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 318 | if (strcmp(tag_name, "bit_width_configs") == 0) { |
| 319 | section = ROOT; |
| 320 | } else if (strcmp(tag_name, "acdb_ids") == 0) { |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 321 | 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 Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 326 | } else if (strcmp(tag_name, "interface_names") == 0) { |
| 327 | section = ROOT; |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 328 | } |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 331 | int platform_info_init(const char *filename) |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 332 | { |
| 333 | XML_Parser parser; |
| 334 | FILE *file; |
| 335 | int ret = 0; |
| 336 | int bytes_read; |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 337 | void *buf; |
| 338 | |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 339 | file = fopen(filename, "r"); |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 340 | section = ROOT; |
| 341 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 342 | if (!file) { |
| 343 | ALOGD("%s: Failed to open %s, using defaults.", |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 344 | __func__, filename); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 345 | 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 Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 356 | 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 Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 376 | __func__, filename); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 377 | ret = -EINVAL; |
| 378 | goto err_free_parser; |
| 379 | } |
| 380 | |
| 381 | if (bytes_read == 0) |
| 382 | break; |
| 383 | } |
| 384 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 385 | err_free_parser: |
| 386 | XML_ParserFree(parser); |
| 387 | err_close_file: |
| 388 | fclose(file); |
| 389 | done: |
| 390 | return ret; |
| 391 | } |