Coverage Report

Created: 2026-11-11 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/adhd/cras/src/server/cras_alsa_config.c
Line
Count
Source
1
/* Copyright 2023 The ChromiumOS Authors
2
 * Use of this source code is governed by a BSD-style license that can be
3
 * found in the LICENSE file.
4
 */
5
6
#include "cras/src/server/cras_alsa_config.h"
7
8
#include <alsa/asoundlib.h>
9
#include <stdbool.h>
10
#include <stdint.h>
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include <syslog.h>
15
16
#include "cras/src/common/blob_wrapper.h"
17
#include "cras/src/server/cras_alsa_card.h"
18
#include "third_party/strlcpy/strlcpy.h"
19
#include "third_party/utlist/utlist.h"
20
21
0
#define MAX_ALSA_CTL_ELEM_NAME_LENGTH 100
22
23
// ctl_elem is a handler for config read/write to given config control name.
24
struct ctl_elem {
25
  // ALSA sound card CTL handle.
26
  snd_ctl_t* handle;
27
  // Properties for ALSA CTL element for the config control.
28
  snd_ctl_elem_id_t* id;
29
  snd_ctl_elem_info_t* info;
30
  snd_ctl_elem_value_t* value;
31
32
  // The pointer of applied blob wrapper for byte-typed configuration.
33
  struct blob_wrapper* bw;
34
  // If true, the preliminary read is required for writing bytes.
35
  bool bw_update_needed;
36
37
  // The mixer control name.
38
  char name[MAX_ALSA_CTL_ELEM_NAME_LENGTH];
39
  // The sound card index it belongs.
40
  size_t card_index;
41
42
  struct ctl_elem *next, *prev;
43
};
44
45
struct ctl_elem* connected_ctl_elems;
46
47
0
static void ctl_elem_free(struct ctl_elem* ctl_elem) {
48
0
  if (!ctl_elem) {
49
0
    return;
50
0
  }
51
52
0
  snd_ctl_elem_value_free(ctl_elem->value);
53
0
  snd_ctl_elem_id_free(ctl_elem->id);
54
0
  snd_ctl_elem_info_free(ctl_elem->info);
55
0
  if (ctl_elem->handle != NULL) {
56
0
    snd_ctl_close(ctl_elem->handle);
57
0
  }
58
59
0
  free(ctl_elem->bw);
60
0
  free(ctl_elem);
61
0
}
62
63
0
static struct ctl_elem* ctl_elem_allocate() {
64
0
  struct ctl_elem* ctl_elem;
65
0
  int rc;
66
67
0
  ctl_elem = (struct ctl_elem*)calloc(1, sizeof(*ctl_elem));
68
0
  if (!ctl_elem) {
69
0
    return NULL;
70
0
  }
71
72
  /* Allocate buffers for pointers info, id, and value. */
73
0
  rc = snd_ctl_elem_info_malloc(&ctl_elem->info);
74
0
  if (rc) {
75
0
    syslog(LOG_ERR, "ctl_elem_allocate: Could not malloc elem_info: %d", rc);
76
0
    goto error;
77
0
  }
78
79
0
  rc = snd_ctl_elem_id_malloc(&ctl_elem->id);
80
0
  if (rc) {
81
0
    syslog(LOG_ERR, "ctl_elem_allocate: Could not malloc elem_id: %d", rc);
82
0
    goto error;
83
0
  }
84
85
0
  rc = snd_ctl_elem_value_malloc(&ctl_elem->value);
86
0
  if (rc) {
87
0
    syslog(LOG_ERR, "ctl_elem_allocate: Could not malloc elem_value: %d", rc);
88
0
    goto error;
89
0
  }
90
91
0
  return ctl_elem;
92
93
0
error:
94
0
  ctl_elem_free(ctl_elem);
95
0
  return NULL;
96
0
}
97
98
static int ctl_elem_create(size_t card_index,
99
                           const char* elem_name,
100
0
                           struct ctl_elem** ctl_elem_p) {
101
0
  struct ctl_elem* ctl_elem;
102
0
  alsa_card_name_t card_name = cras_alsa_card_get_name(card_index);
103
0
  int rc;
104
105
0
  ctl_elem = ctl_elem_allocate();
106
0
  if (!ctl_elem) {
107
0
    syslog(LOG_ERR, "ctl_elem_create: Failed allocating memory");
108
0
    return -ENOMEM;
109
0
  }
110
111
0
  rc = snd_ctl_open(&ctl_elem->handle, card_name.str, SND_CTL_NONBLOCK);
112
0
  if (rc) {
113
0
    syslog(LOG_ERR, "ctl_elem_create: Failed opening card %s.", card_name.str);
114
0
    goto error;
115
0
  }
116
117
  /* Parse elem id from the ascii control name. */
118
0
  rc = snd_ctl_ascii_elem_id_parse(ctl_elem->id, elem_name);
119
0
  if (rc) {
120
0
    syslog(LOG_ERR, "ctl_elem_create: Failed parsing id from %s.", elem_name);
121
0
    goto error;
122
0
  }
123
124
  /* Get element info from id. */
125
0
  snd_ctl_elem_info_set_id(ctl_elem->info, ctl_elem->id);
126
0
  rc = snd_ctl_elem_info(ctl_elem->handle, ctl_elem->info);
127
0
  if (rc) {
128
    /* snd_ctl_elem_info() returns -ENOENT when the control name is not found on
129
     * this card. */
130
0
    goto error;
131
0
  }
132
133
0
  *ctl_elem_p = ctl_elem;
134
0
  return 0;
135
136
0
error:
137
0
  ctl_elem_free(ctl_elem);
138
0
  return rc;
139
0
}
140
141
/* Create ctl_elem for the given control name. It will iterate over ALSA cards
142
 * until the target control is detected on one of them and ctl_elem is created
143
 * successfully. Or return with error code while reaching the iteration end.
144
 *
145
 * This should be used under the premise that the control name is unique over
146
 * all ALSA cards.
147
 */
148
static int ctl_elem_create_for_control_name(const char* name,
149
0
                                            struct ctl_elem** ctl_elem_p) {
150
0
  int card_index = -1;
151
0
  char elem_name[MAX_ALSA_CTL_ELEM_NAME_LENGTH];
152
0
  int rc;
153
154
0
  rc = snprintf(elem_name, MAX_ALSA_CTL_ELEM_NAME_LENGTH, "name='%s'", name);
155
0
  if (rc >= MAX_ALSA_CTL_ELEM_NAME_LENGTH) {
156
0
    syslog(LOG_ERR,
157
0
           "ctl_elem_create: elem_name truncated due to length exceeded");
158
0
    return -ERANGE;
159
0
  }
160
161
  /* snd_card_next() takes the index and sets it to the next index. To iterate
162
   * over cards, set the initial index to -1 and make repetitively calls until
163
   * the next index gets -1, e.g. -1 -> 0 -> 1 -> 2 -> -1.
164
   */
165
0
  rc = -EIO; /* error code for no available card */
166
0
  while (snd_card_next(&card_index) == 0 && card_index >= 0) {
167
0
    rc = ctl_elem_create(card_index, elem_name, ctl_elem_p);
168
0
    if (rc) {
169
      /* The error code -ENOENT is received when the control name is not
170
       * detected on the iterating card (reported by snd_crl_elem_info()), which
171
       * is our intention to try on the next card.
172
       *
173
       * Instead, other error codes might indicate the real problems once
174
       * received so they should not be waived here. (Now we only report them in
175
       * logs, may consider making assertions in the future.)
176
       */
177
0
      if (rc == -ENOENT) {
178
0
        syslog(LOG_DEBUG, "ctl_elem_create: %s not found on card %d", name,
179
0
               card_index);
180
0
      } else {
181
0
        syslog(LOG_ERR,
182
0
               "ctl_elem_create: Unexpected error code %d from creating %s on "
183
0
               "card %d",
184
0
               rc, elem_name, card_index);
185
0
      }
186
0
      continue;
187
0
    }
188
0
    syslog(LOG_DEBUG, "ctl_elem_create: %s found on card %d", name, card_index);
189
190
    /* Fill the matched keys, i.e. name and matched card index. */
191
0
    strlcpy((*ctl_elem_p)->name, name, MAX_ALSA_CTL_ELEM_NAME_LENGTH);
192
0
    (*ctl_elem_p)->card_index = card_index;
193
0
    return 0;
194
0
  }
195
0
  return rc;
196
0
}
197
198
0
static bool ctl_elem_is_bytes_type(struct ctl_elem* ctl_elem) {
199
0
  return snd_ctl_elem_info_get_type(ctl_elem->info) == SND_CTL_ELEM_TYPE_BYTES;
200
0
}
201
202
0
static bool ctl_elem_is_switch_type(struct ctl_elem* ctl_elem) {
203
0
  return snd_ctl_elem_info_get_type(ctl_elem->info) ==
204
0
         SND_CTL_ELEM_TYPE_BOOLEAN;
205
0
}
206
207
static int get_ctl_elem_by_name(const char* name,
208
0
                                struct ctl_elem** ctl_elem_p) {
209
0
  struct ctl_elem* ctl_elem;
210
0
  struct blob_wrapper* wrapper;
211
0
  int rc;
212
213
  /* Find control name in the list of connected control elements. */
214
0
  DL_FOREACH (connected_ctl_elems, ctl_elem) {
215
0
    if (!strcmp(ctl_elem->name, name)) {
216
0
      *ctl_elem_p = ctl_elem;
217
0
      return 0;
218
0
    }
219
0
  }
220
221
  /* Create the control element (and connect) if not matched in list. */
222
0
  rc = ctl_elem_create_for_control_name(name, &ctl_elem);
223
0
  if (rc) {
224
0
    syslog(LOG_WARNING, "get_ctl_elem_by_name: %s is not detected ", name);
225
0
    return rc;
226
0
  }
227
228
  /* Create the blob wrapper for bytes type. */
229
0
  if (ctl_elem_is_bytes_type(ctl_elem)) {
230
    /* Use SOF-typed blob wrapper by default.
231
     * TODO(b/292231234): revisit while more types are required for support.
232
     */
233
0
    wrapper = sof_blob_wrapper_create();
234
0
    if (!wrapper) {
235
0
      syslog(LOG_ERR, "get_ctl_elem_by_name: Failed creating wrapper");
236
0
      ctl_elem_free(ctl_elem);
237
0
      return -ENOMEM;
238
0
    }
239
0
    ctl_elem->bw = wrapper;
240
0
    ctl_elem->bw_update_needed = true;
241
0
  }
242
243
  /* Append the control element to the list. */
244
0
  DL_APPEND(connected_ctl_elems, ctl_elem);
245
246
0
  *ctl_elem_p = ctl_elem;
247
0
  return 0;
248
0
}
249
250
static int ctl_elem_get_blob_data(struct ctl_elem* ctl_elem,
251
                                  uint8_t* buf,
252
0
                                  size_t buf_size) {
253
0
  struct blob_wrapper* wrapper = ctl_elem->bw;
254
0
  uint8_t* read_buf;
255
0
  size_t read_buf_size;
256
0
  int rc;
257
258
0
  rc = blob_wrapper_get_wrapped_size(wrapper, buf, buf_size);
259
0
  if (rc < 0) {
260
0
    syslog(LOG_ERR, "ctl_elem_get_blob_data: Failed getting wrapped size");
261
0
    return rc;
262
0
  }
263
264
0
  read_buf_size = rc;
265
0
  read_buf = (uint8_t*)calloc(1, read_buf_size);
266
0
  if (!read_buf) {
267
0
    syslog(LOG_ERR,
268
0
           "cras_alsa_config_get_tlv_bytes_data: Failed allocating buffer");
269
0
    return -ENOMEM;
270
0
  }
271
272
  /* TLV read buffer needs to be formatted as the wrapped blob. */
273
0
  rc = blob_wrapper_wrap(wrapper, read_buf, read_buf_size, buf, buf_size);
274
0
  if (rc < 0) {
275
0
    syslog(LOG_ERR,
276
0
           "ctl_elem_get_blob_data: Failed wrapping blob for config read");
277
0
    goto exit;
278
0
  }
279
280
0
  rc = snd_ctl_elem_tlv_read(ctl_elem->handle, ctl_elem->id,
281
0
                             (uint32_t*)read_buf, read_buf_size);
282
0
  if (rc < 0) {
283
0
    syslog(LOG_ERR, "ctl_elem_get_blob_data: Failed TLV read");
284
0
    goto exit;
285
0
  }
286
287
0
  rc = blob_wrapper_unwrap(wrapper, buf, buf_size, read_buf, read_buf_size);
288
0
  if (rc < 0) {
289
0
    syslog(LOG_ERR, "ctl_elem_get_blob_data: Failed unwrapping blob");
290
0
    goto exit;
291
0
  }
292
293
0
  ctl_elem->bw_update_needed = false;
294
295
0
exit:
296
0
  free(read_buf);
297
0
  return rc;
298
0
}
299
300
0
static int ctl_elem_read_tlv_bytes_internal(struct ctl_elem* ctl_elem) {
301
0
  uint8_t* buf;
302
0
  size_t buf_size;
303
0
  int rc;
304
305
  /* Check if the control is readable.
306
   * The function returns 1 if readable; 0 otherwise.
307
   */
308
0
  if (!snd_ctl_elem_info_is_tlv_readable(ctl_elem->info)) {
309
0
    syslog(LOG_ERR, "ctl_elem_read_tlv_bytes_internal: Not a readable control");
310
0
    return -EACCES;
311
0
  }
312
313
  /* Get the biggest possible blob size (unwrapped) from info. */
314
0
  buf_size = snd_ctl_elem_info_get_count(ctl_elem->info);
315
316
0
  buf = (uint8_t*)calloc(1, buf_size);
317
0
  if (!buf) {
318
0
    syslog(LOG_ERR,
319
0
           "ctl_elem_read_tlv_bytes_internal: Could not allocate buffer");
320
0
    return -ENOMEM;
321
0
  }
322
323
0
  rc = ctl_elem_get_blob_data(ctl_elem, buf, buf_size);
324
0
  if (rc < 0) {
325
0
    syslog(LOG_ERR, "ctl_elem_read_tlv_bytes_internal: Failed TLV read");
326
0
  }
327
328
0
  free(buf);
329
0
  return rc;
330
0
}
331
332
/*
333
 * Exported Interface.
334
 */
335
336
0
int cras_alsa_config_probe(const char* name) {
337
0
  struct ctl_elem* ctl_elem;
338
0
  return get_ctl_elem_by_name(name, &ctl_elem);
339
0
}
340
341
0
int cras_alsa_config_set_switch(const char* name, bool enabled) {
342
0
  struct ctl_elem* ctl_elem;
343
0
  int rc;
344
345
0
  syslog(LOG_DEBUG, "cras_alsa_config: Set switch %s to %d", name, enabled);
346
347
0
  rc = get_ctl_elem_by_name(name, &ctl_elem);
348
0
  if (rc) {
349
0
    syslog(LOG_ERR, "cras_alsa_config_set_switch: Failed creating ctl_elem");
350
0
    return rc;
351
0
  }
352
353
  /* Check if the control type has boolean support. */
354
0
  if (!ctl_elem_is_switch_type(ctl_elem)) {
355
0
    syslog(LOG_ERR,
356
0
           "cras_alsa_config_set_switch: Control type has no boolean support");
357
0
    return -EINVAL;
358
0
  }
359
360
  /* Set id and read from control for handle value. */
361
0
  snd_ctl_elem_value_set_id(ctl_elem->value, ctl_elem->id);
362
0
  rc = snd_ctl_elem_read(ctl_elem->handle, ctl_elem->value);
363
0
  if (rc) {
364
0
    syslog(LOG_ERR,
365
0
           "cras_alsa_config_set_switch: Failed to read control value");
366
0
    return rc;
367
0
  }
368
369
  /* Set switch boolean to handle value. */
370
0
  snd_ctl_elem_value_set_boolean(ctl_elem->value, 0, enabled);
371
372
  /* Write value to control. */
373
0
  rc = snd_ctl_elem_write(ctl_elem->handle, ctl_elem->value);
374
0
  if (rc < 0) {
375
0
    syslog(LOG_ERR,
376
0
           "cras_alsa_config_set_switch: Failed to write control value");
377
0
    return rc;
378
0
  }
379
380
0
  return 0;
381
0
}
382
383
0
int cras_alsa_config_get_switch(const char* name, bool* enabled) {
384
0
  struct ctl_elem* ctl_elem;
385
0
  int rc;
386
387
0
  syslog(LOG_DEBUG, "cras_alsa_config: Get switch %s", name);
388
389
0
  rc = get_ctl_elem_by_name(name, &ctl_elem);
390
0
  if (rc) {
391
0
    syslog(LOG_ERR, "cras_alsa_config_get_switch: Failed creating ctl_elem");
392
0
    return rc;
393
0
  }
394
395
  /* Check if the control type has boolean support. */
396
0
  if (!ctl_elem_is_switch_type(ctl_elem)) {
397
0
    syslog(LOG_ERR,
398
0
           "cras_alsa_config_get_switch: Control type has no boolean support");
399
0
    return -EINVAL;
400
0
  }
401
402
  /* Set id and read from control for handle value. */
403
0
  snd_ctl_elem_value_set_id(ctl_elem->value, ctl_elem->id);
404
0
  rc = snd_ctl_elem_read(ctl_elem->handle, ctl_elem->value);
405
0
  if (rc) {
406
0
    syslog(LOG_ERR,
407
0
           "cras_alsa_config_get_switch: Failed to read control value");
408
0
    return rc;
409
0
  }
410
411
  /* Get switch value. */
412
0
  *enabled = !!snd_ctl_elem_value_get_boolean(ctl_elem->value, 0);
413
0
  syslog(LOG_DEBUG, "cras_alsa_config: Got value %d", *enabled);
414
0
  return 0;
415
0
}
416
417
int cras_alsa_config_set_tlv_bytes(const char* name,
418
                                   const uint8_t* blob,
419
0
                                   size_t blob_size) {
420
0
  struct ctl_elem* ctl_elem;
421
0
  struct blob_wrapper* wrapper;
422
0
  uint8_t* buf = NULL;
423
0
  size_t buf_size;
424
0
  int rc;
425
426
0
  syslog(LOG_DEBUG, "cras_alsa_config: Set %s with blob size %zu", name,
427
0
         blob_size);
428
429
0
  rc = get_ctl_elem_by_name(name, &ctl_elem);
430
0
  if (rc) {
431
0
    syslog(LOG_ERR, "cras_alsa_config_set_tlv_bytes: Failed creating ctl_elem");
432
0
    return rc;
433
0
  }
434
435
  /* Check if the control type has bytes support. */
436
0
  if (!ctl_elem_is_bytes_type(ctl_elem)) {
437
0
    syslog(LOG_ERR,
438
0
           "cras_alsa_config_set_tlv_bytes: Control type has no bytes support");
439
0
    return -EINVAL;
440
0
  }
441
442
  /* Check if the control is writable.
443
   * The function returns 1 if writable; 0 otherwise.
444
   */
445
0
  if (!snd_ctl_elem_info_is_tlv_writable(ctl_elem->info)) {
446
0
    syslog(LOG_ERR, "cras_alsa_config_set_tlv_bytes: Not a writable control");
447
0
    return -EACCES;
448
0
  }
449
450
  /* Read the control configuration before write if needed. */
451
0
  if (ctl_elem->bw_update_needed) {
452
0
    rc = ctl_elem_read_tlv_bytes_internal(ctl_elem);
453
0
    if (rc < 0) {
454
0
      syslog(
455
0
          LOG_WARNING,
456
0
          "cras_alsa_config_set_tlv_bytes: Failed at preliminary read trial");
457
0
    }
458
0
  }
459
460
0
  wrapper = ctl_elem->bw;
461
462
0
  rc = blob_wrapper_get_wrapped_size(wrapper, blob, blob_size);
463
0
  if (rc < 0) {
464
0
    syslog(LOG_ERR,
465
0
           "cras_alsa_config_set_tlv_bytes: Failed getting wrapped size");
466
0
    return rc;
467
0
  }
468
469
0
  buf_size = rc;
470
0
  buf = (uint8_t*)calloc(1, buf_size);
471
0
  if (!buf) {
472
0
    syslog(LOG_ERR, "cras_alsa_config_set_tlv_bytes: Failed allocating buffer");
473
0
    rc = -ENOMEM;
474
0
    goto exit;
475
0
  }
476
477
0
  rc = blob_wrapper_wrap(wrapper, buf, buf_size, blob, blob_size);
478
0
  if (rc < 0) {
479
0
    syslog(LOG_ERR, "cras_alsa_config_set_tlv_bytes: Failed wrapping blob");
480
0
    goto exit;
481
0
  }
482
483
  /* Write TLV buffer to control. */
484
0
  rc = snd_ctl_elem_tlv_write(ctl_elem->handle, ctl_elem->id, (uint32_t*)buf);
485
0
  if (rc < 0) {
486
0
    syslog(LOG_ERR, "cras_alsa_config_set_tlv_bytes: Failed TLV write");
487
0
    goto exit;
488
0
  }
489
0
  rc = 0;
490
491
0
exit:
492
0
  free(buf);
493
0
  return rc;
494
0
}
495
496
0
int cras_alsa_config_get_tlv_bytes_maxcount(const char* name) {
497
0
  struct ctl_elem* ctl_elem;
498
0
  int rc;
499
500
0
  syslog(LOG_DEBUG, "cras_alsa_config: Get bytes count for control %s", name);
501
502
0
  rc = get_ctl_elem_by_name(name, &ctl_elem);
503
0
  if (rc) {
504
0
    syslog(LOG_ERR,
505
0
           "cras_alsa_config_get_tlv_bytes_maxcount: Failed creating ctl_elem");
506
0
    return rc;
507
0
  }
508
509
  /* Check if the control type has bytes support. */
510
0
  if (!ctl_elem_is_bytes_type(ctl_elem)) {
511
0
    syslog(LOG_ERR,
512
0
           "cras_alsa_config_get_tlv_bytes_maxcount: Control type has no bytes "
513
0
           "support");
514
0
    return -EINVAL;
515
0
  }
516
517
  /* Get the biggest possible blob size (unwrapped) from info. */
518
0
  rc = (int)snd_ctl_elem_info_get_count(ctl_elem->info);
519
0
  return rc;
520
0
}
521
522
int cras_alsa_config_get_tlv_bytes_data(const char* name,
523
                                        uint8_t* buf,
524
0
                                        size_t buf_size) {
525
0
  struct ctl_elem* ctl_elem;
526
0
  int rc;
527
528
0
  syslog(LOG_DEBUG, "cras_alsa_config: Get %s blob data", name);
529
530
0
  if (!buf) {
531
0
    syslog(
532
0
        LOG_ERR,
533
0
        "cras_alsa_config_get_tlv_bytes_data: Input buffer is not allocated");
534
0
    return -ENOMEM;
535
0
  }
536
537
0
  rc = get_ctl_elem_by_name(name, &ctl_elem);
538
0
  if (rc) {
539
0
    syslog(LOG_ERR,
540
0
           "cras_alsa_config_get_tlv_bytes_data: Failed creating ctl_elem");
541
0
    return rc;
542
0
  }
543
544
  /* Check if the control type has bytes support. */
545
0
  if (!ctl_elem_is_bytes_type(ctl_elem)) {
546
0
    syslog(LOG_ERR,
547
0
           "cras_alsa_config_get_tlv_bytes_data: Control type has no bytes "
548
0
           "support");
549
0
    return -EINVAL;
550
0
  }
551
552
  /* Check if the control is readable.
553
   * The function returns 1 if readable; 0 otherwise.
554
   */
555
0
  if (!snd_ctl_elem_info_is_tlv_readable(ctl_elem->info)) {
556
0
    syslog(LOG_ERR,
557
0
           "cras_alsa_config_get_tlv_bytes_data: not a readable control");
558
0
    return -EACCES;
559
0
  }
560
561
0
  rc = ctl_elem_get_blob_data(ctl_elem, buf, buf_size);
562
0
  if (rc < 0) {
563
0
    syslog(LOG_ERR,
564
0
           "cras_alsa_config_get_tlv_bytes_data: Failed to get blob data");
565
0
  }
566
567
0
  return rc;
568
0
}
569
570
0
void cras_alsa_config_release_controls_on_card(uint32_t card_index) {
571
0
  struct ctl_elem* ctl_elem;
572
573
0
  DL_FOREACH (connected_ctl_elems, ctl_elem) {
574
0
    if (ctl_elem->card_index == card_index) {
575
      DL_DELETE(connected_ctl_elems, ctl_elem);
576
0
      ctl_elem_free(ctl_elem);
577
0
    }
578
0
  }
579
0
}