summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_alsa_io.c
blob: c972f3c129ba9827efdeb3549cdc7065e7e1f4d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <alsa/asoundlib.h>
#include <alsa/use-case.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <syslog.h>
#include <time.h>

#include "audio_thread.h"
#include "cras_alsa_helpers.h"
#include "cras_alsa_io.h"
#include "cras_alsa_jack.h"
#include "cras_alsa_mixer.h"
#include "cras_alsa_ucm.h"
#include "cras_audio_area.h"
#include "cras_config.h"
#include "cras_utf8.h"
#include "cras_iodev.h"
#include "cras_iodev_list.h"
#include "cras_messages.h"
#include "cras_rclient.h"
#include "cras_shm.h"
#include "cras_system_state.h"
#include "cras_types.h"
#include "cras_util.h"
#include "cras_volume_curve.h"
#include "sfh.h"
#include "softvol_curve.h"
#include "utlist.h"

#define MAX_ALSA_DEV_NAME_LENGTH 9 /* Alsa names "hw:XX,YY" + 1 for null. */
#define HOTWORD_DEV "Wake on Voice"
#define DEFAULT "(default)"
#define HDMI "HDMI"
#define INTERNAL_MICROPHONE "Internal Mic"
#define INTERNAL_SPEAKER "Speaker"
#define KEYBOARD_MIC "Keyboard Mic"
#define USB "USB"

/* For USB, pad the output buffer.  This avoids a situation where there isn't a
 * complete URB's worth of audio ready to be transmitted when it is requested.
 * The URB interval does track directly to the audio clock, making it hard to
 * predict the exact interval. */
#define USB_EXTRA_BUFFER_FRAMES 768

/* This extends cras_ionode to include alsa-specific information.
 * Members:
 *    mixer_output - From cras_alsa_mixer.
 *    jack_curve - In absense of a mixer output, holds a volume curve to use
 *        when this jack is plugged.
 *    jack - The jack associated with the jack_curve (if it exists).
 */
struct alsa_output_node {
	struct cras_ionode base;
	struct mixer_control *mixer_output;
	struct cras_volume_curve *jack_curve;
	const struct cras_alsa_jack *jack;
};

struct alsa_input_node {
	struct cras_ionode base;
	struct mixer_control* mixer_input;
	const struct cras_alsa_jack *jack;
};

/* Child of cras_iodev, alsa_io handles ALSA interaction for sound devices.
 * base - The cras_iodev structure "base class".
 * dev - String that names this device (e.g. "hw:0,0").
 * dev_name - value from snd_pcm_info_get_name
 * dev_id - value from snd_pcm_info_get_id
 * device_index - ALSA index of device, Y in "hw:X:Y".
 * next_ionode_index - The index we will give to the next ionode. Each ionode
 *     have a unique index within the iodev.
 * card_type - the type of the card this iodev belongs.
 * is_first - true if this is the first iodev on the card.
 * fully_specified - true if this device and it's nodes were fully specified.
 *     That is, don't automatically create nodes for it.
 * enable_htimestamp - True when the device's htimestamp is used.
 * handle - Handle to the opened ALSA device.
 * num_underruns - Number of times we have run out of data (playback only).
 * alsa_stream - Playback or capture type.
 * mixer - Alsa mixer used to control volume and mute of the device.
 * jack_list - List of alsa jack controls for this device.
 * ucm - ALSA use case manager, if configuration is found.
 * mmap_offset - offset returned from mmap_begin.
 * dsp_name_default - the default dsp name for the device. It can be overridden
 *     by the jack specific dsp name.
 * poll_fd - Descriptor used to block until data is ready.
 * dma_period_set_microsecs - If non-zero, the value to apply to the dma_period.
 * is_free_running - true if device is playing zeros in the buffer without
 *                   user filling meaningful data. The device buffer is filled
 *                   with zeros. In this state, appl_ptr remains the same
 *                   while hw_ptr keeps running ahead.
 * filled_zeros_for_draining - The number of zeros filled for draining.
 */
struct alsa_io {
	struct cras_iodev base;
	char *dev;
	char *dev_name;
	char *dev_id;
	uint32_t device_index;
	uint32_t next_ionode_index;
	enum CRAS_ALSA_CARD_TYPE card_type;
	int is_first;
	int fully_specified;
	int enable_htimestamp;
	snd_pcm_t *handle;
	unsigned int num_underruns;
	snd_pcm_stream_t alsa_stream;
	struct cras_alsa_mixer *mixer;
	struct cras_alsa_jack_list *jack_list;
	snd_use_case_mgr_t *ucm;
	snd_pcm_uframes_t mmap_offset;
	const char *dsp_name_default;
	int poll_fd;
	unsigned int dma_period_set_microsecs;
	int is_free_running;
	unsigned int filled_zeros_for_draining;
};

static void init_device_settings(struct alsa_io *aio);

static int alsa_iodev_set_active_node(struct cras_iodev *iodev,
				      struct cras_ionode *ionode,
				      unsigned dev_enabled);

/*
 * iodev callbacks.
 */

static int frames_queued(const struct cras_iodev *iodev,
			 struct timespec *tstamp)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	int rc;
	snd_pcm_uframes_t frames;

	rc = cras_alsa_get_avail_frames(aio->handle,
					aio->base.buffer_size,
					&frames, tstamp,
					&aio->num_underruns);
	if (rc < 0)
		return rc;
	if (!aio->enable_htimestamp)
		clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
	if (iodev->direction == CRAS_STREAM_INPUT)
		return (int)frames;

	/* For output, return number of frames that are used. */
	return iodev->buffer_size - frames;
}

static int delay_frames(const struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	snd_pcm_sframes_t delay;
	int rc;

	rc = cras_alsa_get_delay_frames(aio->handle,
					iodev->buffer_size,
					&delay);
	if (rc < 0)
		return rc;

	return (int)delay;
}

static int close_dev(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;

	if (aio->poll_fd >= 0)
		audio_thread_rm_callback(aio->poll_fd);
	if (!aio->handle)
		return 0;
	cras_alsa_pcm_close(aio->handle);
	aio->handle = NULL;
	aio->is_free_running = 0;
	aio->filled_zeros_for_draining = 0;
	cras_iodev_free_format(&aio->base);
	cras_iodev_free_audio_area(&aio->base);
	return 0;
}

static int dummy_hotword_cb(void *arg)
{
	/* Only need this once. */
	struct alsa_io *aio = (struct alsa_io *)arg;
	audio_thread_rm_callback(aio->poll_fd);
	aio->poll_fd = -1;
	return 0;
}

static int open_dev(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	snd_pcm_t *handle;
	int period_wakeup;
	int rc;

	/* This is called after the first stream added so configure for it.
	 * format must be set before opening the device.
	 */
	if (iodev->format == NULL)
		return -EINVAL;
	aio->num_underruns = 0;
	aio->is_free_running = 0;
	aio->filled_zeros_for_draining = 0;
	cras_iodev_init_audio_area(iodev, iodev->format->num_channels);

	syslog(LOG_DEBUG, "Configure alsa device %s rate %zuHz, %zu channels",
	       aio->dev, iodev->format->frame_rate,
	       iodev->format->num_channels);
	handle = 0; /* Avoid unused warning. */
	rc = cras_alsa_pcm_open(&handle, aio->dev, aio->alsa_stream);
	if (rc < 0)
		return rc;

	/* If it's a wake on voice device, period_wakeups are required. */
	period_wakeup = (iodev->active_node->type == CRAS_NODE_TYPE_HOTWORD);

	rc = cras_alsa_set_hwparams(handle, iodev->format,
				    &iodev->buffer_size, period_wakeup,
				    aio->dma_period_set_microsecs);
	if (rc < 0) {
		cras_alsa_pcm_close(handle);
		return rc;
	}

	/* Set channel map to device */
	rc = cras_alsa_set_channel_map(handle,
				       iodev->format);
	if (rc < 0) {
		cras_alsa_pcm_close(handle);
		return rc;
	}

	/* Configure software params. */
	rc = cras_alsa_set_swparams(handle, &aio->enable_htimestamp);
	if (rc < 0) {
		cras_alsa_pcm_close(handle);
		return rc;
	}

	/* Assign pcm handle then initialize device settings. */
	aio->handle = handle;
	init_device_settings(aio);

	aio->poll_fd = -1;
	if (iodev->active_node->type == CRAS_NODE_TYPE_HOTWORD) {
		struct pollfd *ufds;
		int count, i;

		count = snd_pcm_poll_descriptors_count(handle);
		if (count <= 0) {
			syslog(LOG_ERR, "Invalid poll descriptors count\n");
			return count;
		}

		ufds = (struct pollfd *)malloc(sizeof(struct pollfd) * count);
		if (ufds == NULL)
			return -ENOMEM;

		rc = snd_pcm_poll_descriptors(handle, ufds, count);
		if (rc < 0) {
			syslog(LOG_ERR,
			       "Getting hotword poll descriptors: %s\n",
			       snd_strerror(rc));
			free(ufds);
			return rc;
		}

		for (i = 0; i < count; i++) {
			if (ufds[i].events & POLLIN) {
				aio->poll_fd = ufds[i].fd;
				break;
			}
		}
		free(ufds);

		if (aio->poll_fd >= 0)
			audio_thread_add_callback(aio->poll_fd,
						  dummy_hotword_cb,
						  aio);
	}

	/* Capture starts right away, playback will wait for samples. */
	if (aio->alsa_stream == SND_PCM_STREAM_CAPTURE)
		cras_alsa_pcm_start(aio->handle);

	return 0;
}

/* Check if ALSA device is opened by checking if handle is valid.
 * Note that to fully open a cras_iodev, ALSA device is opened first, then there
 * are some device init settings to be done in init_device_settings.
 * Therefore, when setting volume/mute/gain in init_device_settings,
 * cras_iodev is not in CRAS_IODEV_STATE_OPEN yet. We need to check if handle
 * is valid when setting those properties, instead of checking
 * cras_iodev_is_open.
 */
static int has_handle(const struct alsa_io *aio)
{
	return !!aio->handle;
}

static int start(const struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	snd_pcm_t *handle = aio->handle;
	int rc;

	if (snd_pcm_state(handle) == SND_PCM_STATE_RUNNING)
		return 0;

	if (snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED) {
		rc = cras_alsa_attempt_resume(handle);
		if (rc < 0) {
			syslog(LOG_ERR, "Resume error: %s", snd_strerror(rc));
			return rc;
		}
		cras_iodev_reset_rate_estimator(iodev);
	} else {
		rc = cras_alsa_pcm_start(handle);
		if (rc < 0) {
			syslog(LOG_ERR, "Start error: %s", snd_strerror(rc));
			return rc;
		}
	}

	return 0;
}

static int get_buffer(struct cras_iodev *iodev,
		      struct cras_audio_area **area,
		      unsigned *frames)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	snd_pcm_uframes_t nframes = *frames;
	uint8_t *dst = NULL;
	size_t format_bytes;
	int rc;

	aio->mmap_offset = 0;
	format_bytes = cras_get_format_bytes(iodev->format);

	rc = cras_alsa_mmap_begin(aio->handle,
				  format_bytes,
				  &dst,
				  &aio->mmap_offset,
				  &nframes,
				  &aio->num_underruns);

	iodev->area->frames = nframes;
	cras_audio_area_config_buf_pointers(iodev->area, iodev->format, dst);

	*area = iodev->area;
	*frames = nframes;

	return rc;
}

static int put_buffer(struct cras_iodev *iodev, unsigned nwritten)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;

	return cras_alsa_mmap_commit(aio->handle,
				     aio->mmap_offset,
				     nwritten,
				     &aio->num_underruns);
}

static int flush_buffer(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	snd_pcm_uframes_t nframes;

	if (iodev->direction == CRAS_STREAM_INPUT) {
		nframes = snd_pcm_forwardable(aio->handle);
		return snd_pcm_forward(aio->handle, nframes);
	}
	return 0;
}

 /* Gets the first plugged node in list. This is used as the
  * default node to set as active.
  */
static struct cras_ionode *first_plugged_node(struct cras_iodev *iodev)
{
	struct cras_ionode *n;

	/* When this is called at iodev creation, none of the nodes
	 * are selected. Just pick the first plugged one and let Chrome
	 * choose it later. */
	DL_FOREACH(iodev->nodes, n) {
		if (n->plugged)
			return n;
	}
	return iodev->nodes;
}

static void update_active_node(struct cras_iodev *iodev, unsigned node_idx,
			       unsigned dev_enabled)
{
	struct cras_ionode *n;

	/* If a node exists for node_idx, set it as active. */
	DL_FOREACH(iodev->nodes, n) {
		if (n->idx == node_idx) {
			alsa_iodev_set_active_node(iodev, n, dev_enabled);
			return;
		}
	}

	alsa_iodev_set_active_node(iodev, first_plugged_node(iodev),
				   dev_enabled);
}

static int update_channel_layout(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	snd_pcm_t *handle = NULL;
	snd_pcm_uframes_t buf_size = 0;
	int err = 0;

	err = cras_alsa_pcm_open(&handle, aio->dev, aio->alsa_stream);
	if (err < 0) {
		syslog(LOG_ERR, "snd_pcm_open_failed: %s", snd_strerror(err));
		return err;
	}

	/* Sets frame rate and channel count to alsa device before
	 * we test channel mapping. */
	err = cras_alsa_set_hwparams(handle, iodev->format, &buf_size, 0,
				     aio->dma_period_set_microsecs);
	if (err < 0) {
		cras_alsa_pcm_close(handle);
		return err;
	}

	err = cras_alsa_get_channel_map(handle, iodev->format);

	cras_alsa_pcm_close(handle);
	return err;
}

static int set_hotword_model(struct cras_iodev *iodev, const char *model_name)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	if (!aio->ucm)
		return -EINVAL;

	return ucm_set_hotword_model(aio->ucm, model_name);
}

static char *get_hotword_models(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	if (!aio->ucm)
		return NULL;

	return ucm_get_hotword_models(aio->ucm);
}

/*
 * Alsa helper functions.
 */

static struct alsa_output_node *get_active_output(const struct alsa_io *aio)
{
	return (struct alsa_output_node *)aio->base.active_node;
}

static struct alsa_input_node *get_active_input(const struct alsa_io *aio)
{
	return (struct alsa_input_node *)aio->base.active_node;
}

/* Gets the curve for the active output node by below priority:
 * 1. Jack's volume curve.
 * 2. Output mixer control's volume curve.
 * 3. Card mixer's default volume curve.
 */
static const struct cras_volume_curve *get_curve_for_output_node(
		const struct alsa_io *aio,
		const struct alsa_output_node *node)
{
	struct cras_volume_curve *curve = NULL;
	if (node) {
		if (node->jack_curve)
			return node->jack_curve;

		curve = cras_alsa_mixer_get_output_volume_curve(
				node->mixer_output);
		if (curve)
			return curve;
	}
	return cras_alsa_mixer_default_volume_curve(aio->mixer);
}

/* Gets the curve for the active output. */
static const struct cras_volume_curve *get_curve_for_active_output(
		const struct alsa_io *aio)
{
	struct alsa_output_node *node = get_active_output(aio);
	return get_curve_for_output_node(aio, node);
}

/* Informs the system of the volume limits for this device. */
static void set_alsa_volume_limits(struct alsa_io *aio)
{
	const struct cras_volume_curve *curve;

	/* Only set the limits if the dev is active. */
	if (!has_handle(aio))
		return;

	curve = get_curve_for_active_output(aio);
	cras_system_set_volume_limits(
			curve->get_dBFS(curve, 1), /* min */
			curve->get_dBFS(curve, CRAS_MAX_SYSTEM_VOLUME));
}

/* Sets the alsa mute state for this iodev. */
static void set_alsa_mute(const struct alsa_io *aio, int muted)
{
	struct alsa_output_node *aout;

	if (!has_handle(aio))
		return;

	aout = get_active_output(aio);
	cras_alsa_mixer_set_mute(
		aio->mixer,
		muted,
		aout ? aout->mixer_output : NULL);
}

/* Sets the volume of the playback device to the specified level. Receives a
 * volume index from the system settings, ranging from 0 to 100, converts it to
 * dB using the volume curve, and sends the dB value to alsa. Handles mute and
 * unmute, including muting when volume is zero. */
static void set_alsa_volume(struct cras_iodev *iodev)
{
	const struct alsa_io *aio = (const struct alsa_io *)iodev;
	const struct cras_volume_curve *curve;
	size_t volume;
	int mute;
	struct alsa_output_node *aout;

	assert(aio);
	if (aio->mixer == NULL)
		return;

	/* Only set the volume if the dev is active. */
	if (!has_handle(aio))
		return;

	volume = cras_system_get_volume();
	mute = cras_system_get_mute();
	curve = get_curve_for_active_output(aio);
	if (curve == NULL)
		return;
	aout = get_active_output(aio);
	if (aout)
		volume = cras_iodev_adjust_node_volume(&aout->base, volume);

	/* Samples get scaled for devices using software volume, set alsa
	 * volume to 100. */
	if (cras_iodev_software_volume_needed(iodev))
		volume = 100;

	cras_alsa_mixer_set_dBFS(
		aio->mixer,
		curve->get_dBFS(curve, volume),
		aout ? aout->mixer_output : NULL);
	/* Mute for zero. */
	set_alsa_mute(aio, mute || (volume == 0));
}

/* Sets the capture gain to the current system input gain level, given in dBFS.
 * Set mute based on the system mute state.  This gain can be positive or
 * negative and might be adjusted often if and app is running an AGC. */
static void set_alsa_capture_gain(struct cras_iodev *iodev)
{
	const struct alsa_io *aio = (const struct alsa_io *)iodev;
	struct alsa_input_node *ain;
	long gain;

	assert(aio);
	if (aio->mixer == NULL)
		return;

	/* Only set the volume if the dev is active. */
	if (!has_handle(aio))
		return;

	gain = cras_system_get_capture_gain();
	ain = get_active_input(aio);
	if (ain)
		gain += ain->base.capture_gain;
	/* Set hardware gain to 0dB if software gain is needed. */
	if (cras_iodev_software_volume_needed(iodev))
		gain = 0;
	cras_alsa_mixer_set_capture_dBFS(
			aio->mixer,
			gain,
			ain ? ain->mixer_input : NULL);
	cras_alsa_mixer_set_capture_mute(aio->mixer,
					 cras_system_get_capture_mute(),
					 ain ? ain->mixer_input : NULL);
}

/* Swaps the left and right channels of the given node. */
static int set_alsa_node_swapped(struct cras_iodev *iodev,
				 struct cras_ionode *node, int enable)
{
	const struct alsa_io *aio = (const struct alsa_io *)iodev;
	assert(aio);
	return ucm_enable_swap_mode(aio->ucm, node->name, enable);
}

/* Initializes the device settings and registers for callbacks when system
 * settings have been changed.
 */
static void init_device_settings(struct alsa_io *aio)
{
	/* Register for volume/mute callback and set initial volume/mute for
	 * the device. */
	if (aio->base.direction == CRAS_STREAM_OUTPUT) {
		set_alsa_volume_limits(aio);
		set_alsa_volume(&aio->base);
	} else {
		struct mixer_control *mixer_input = NULL;
		struct alsa_input_node *ain = get_active_input(aio);
		long min_capture_gain, max_capture_gain;

		if (ain)
			mixer_input = ain->mixer_input;

		if (cras_iodev_software_volume_needed(&aio->base)) {
			min_capture_gain = DEFAULT_MIN_CAPTURE_GAIN;
			max_capture_gain = cras_iodev_maximum_software_gain(
					&aio->base);
		} else {
			min_capture_gain =
				cras_alsa_mixer_get_minimum_capture_gain(
						aio->mixer, mixer_input);
			max_capture_gain =
				cras_alsa_mixer_get_maximum_capture_gain(
						aio->mixer, mixer_input);
		}
		cras_system_set_capture_gain_limits(min_capture_gain,
						    max_capture_gain);
		set_alsa_capture_gain(&aio->base);
	}
}

/*
 * Functions run in the main server context.
 */

/* Frees resources used by the alsa iodev.
 * Args:
 *    iodev - the iodev to free the resources from.
 */
static void free_alsa_iodev_resources(struct alsa_io *aio)
{
	struct cras_ionode *node;
	struct alsa_output_node *aout;

	free(aio->base.supported_rates);
	free(aio->base.supported_channel_counts);
	free(aio->base.supported_formats);

	DL_FOREACH(aio->base.nodes, node) {
		if (aio->base.direction == CRAS_STREAM_OUTPUT) {
			aout = (struct alsa_output_node *)node;
			cras_volume_curve_destroy(aout->jack_curve);
		}
		cras_iodev_rm_node(&aio->base, node);
		free(node->softvol_scalers);
		free(node);
	}

	free((void *)aio->dsp_name_default);
	cras_iodev_free_resources(&aio->base);
	free(aio->dev);
	if (aio->dev_id)
		free(aio->dev_id);
	if (aio->dev_name)
		free(aio->dev_name);
}

/* Returns true if this is the first internal device */
static int first_internal_device(struct alsa_io *aio)
{
	return aio->is_first && aio->card_type == ALSA_CARD_TYPE_INTERNAL;
}

/* Returns true if there is already a node created with the given name */
static int has_node(struct alsa_io *aio, const char *name)
{
	struct cras_ionode *node;

	DL_FOREACH(aio->base.nodes, node)
		if (!strcmp(node->name, name))
			return 1;

	return 0;
}

/* Returns true if string s ends with the given suffix */
int endswith(const char *s, const char *suffix)
{
	size_t n = strlen(s);
	size_t m = strlen(suffix);
	return n >= m && !strcmp(s + (n - m), suffix);
}

/* Drop the node name and replace it with node type.  */
static void drop_node_name(struct cras_ionode *node)
{
	if (node->type == CRAS_NODE_TYPE_USB)
		strcpy(node->name, USB);
	else if (node->type == CRAS_NODE_TYPE_HDMI)
		strcpy(node->name, HDMI);
	else {
		/* Only HDMI or USB node might have invalid name to drop */
		syslog(LOG_ERR, "Unexpectedly drop node name for "
		       "node: %s, type: %d", node->name, node->type);
		strcpy(node->name, DEFAULT);
	}
}

/* Sets the initial plugged state and type of a node based on its
 * name. Chrome will assign priority to nodes base on node type.
 */
static void set_node_initial_state(struct cras_ionode *node,
				   enum CRAS_ALSA_CARD_TYPE card_type)
{
	static const struct {
		const char *name;
		int initial_plugged;
		enum CRAS_NODE_TYPE type;
	} node_defaults[] = {
		{ DEFAULT, 1, CRAS_NODE_TYPE_UNKNOWN},
		{ INTERNAL_SPEAKER, 1, CRAS_NODE_TYPE_INTERNAL_SPEAKER },
		{ INTERNAL_MICROPHONE, 1, CRAS_NODE_TYPE_INTERNAL_MIC },
		{ KEYBOARD_MIC, 1, CRAS_NODE_TYPE_KEYBOARD_MIC },
		{ HDMI, 0, CRAS_NODE_TYPE_HDMI },
		{ "IEC958", 0, CRAS_NODE_TYPE_HDMI },
		{ "Headphone", 0, CRAS_NODE_TYPE_HEADPHONE },
		{ "Front Headphone", 0, CRAS_NODE_TYPE_HEADPHONE },
		{ "Mic", 0, CRAS_NODE_TYPE_MIC },
		{ HOTWORD_DEV, 1, CRAS_NODE_TYPE_HOTWORD },
		{ "Haptic", 1, CRAS_NODE_TYPE_HAPTIC },
		{ "Rumbler", 1, CRAS_NODE_TYPE_HAPTIC },
		{ "Line Out", 0, CRAS_NODE_TYPE_LINEOUT},
	};
	unsigned i;

	node->volume = 100;
	node->type = CRAS_NODE_TYPE_UNKNOWN;
	/* Go through the known names */
	for (i = 0; i < ARRAY_SIZE(node_defaults); i++)
		if (!strncmp(node->name, node_defaults[i].name,
			     strlen(node_defaults[i].name))) {
			node->plugged = node_defaults[i].initial_plugged;
			node->type = node_defaults[i].type;
			if (node->plugged)
				gettimeofday(&node->plugged_time, NULL);
			break;
		}

	/* If we didn't find a matching name above, but the node is a jack node,
	 * set its type to headphone/mic. This matches node names like "DAISY-I2S Mic
	 * Jack".
	 * If HDMI is in the node name, set its type to HDMI. This matches node names
	 * like "Rockchip HDMI Jack".
	 */
	if (i == ARRAY_SIZE(node_defaults)) {
		if (endswith(node->name, "Jack")) {
			if (node->dev->direction == CRAS_STREAM_OUTPUT)
				node->type = CRAS_NODE_TYPE_HEADPHONE;
			else
				node->type = CRAS_NODE_TYPE_MIC;
		}
		if (strstr(node->name, HDMI) &&
		    node->dev->direction == CRAS_STREAM_OUTPUT)
			node->type = CRAS_NODE_TYPE_HDMI;
	}

	/* Regardless of the node name of a USB headset (it can be "Speaker"),
	 * set it's type to usb.
	 */
	if (card_type == ALSA_CARD_TYPE_USB)
		node->type = CRAS_NODE_TYPE_USB;

	if (!is_utf8_string(node->name))
		drop_node_name(node);
}

static int get_ucm_flag_integer(struct alsa_io *aio,
				const char *flag_name,
				int *result)
{
	char *value;
	int i;

	if (!aio->ucm)
		return -1;

	value = ucm_get_flag(aio->ucm, flag_name);
	if (!value)
		return -1;

	i = atoi(value);
	free(value);
	*result = i;
	return 0;
}

static int auto_unplug_input_node(struct alsa_io *aio)
{
	int result;
	if (get_ucm_flag_integer(aio, "AutoUnplugInputNode", &result))
		return 0;
	return result;
}

static int auto_unplug_output_node(struct alsa_io *aio)
{
	int result;
	if (get_ucm_flag_integer(aio, "AutoUnplugOutputNode", &result))
		return 0;
	return result;
}

static int no_create_default_input_node(struct alsa_io *aio)
{
	int result;
	if (get_ucm_flag_integer(aio, "NoCreateDefaultInputNode", &result))
		return 0;
	return result;
}

static int no_create_default_output_node(struct alsa_io *aio)
{
	int result;
	if (get_ucm_flag_integer(aio, "NoCreateDefaultOutputNode", &result))
		return 0;
	return result;
}

static void set_output_node_software_volume_needed(
	struct alsa_output_node *output, struct alsa_io *aio)
{

	struct cras_alsa_mixer *mixer = aio->mixer;
	long range = 0;

	if (aio->ucm && ucm_get_disable_software_volume(aio->ucm)) {
		output->base.software_volume_needed = 0;
		syslog(LOG_DEBUG, "Disable software volume for %s from ucm.",
		       output->base.name);
		return;
	}

	/* Use software volume for HDMI output and nodes without volume mixer
	 * control. */
	if ((output->base.type == CRAS_NODE_TYPE_HDMI) ||
	    (!cras_alsa_mixer_has_main_volume(mixer) &&
	     !cras_alsa_mixer_has_volume(output->mixer_output)))
		output->base.software_volume_needed = 1;

	/* Use software volume if the usb device's volume range is smaller
	 * than 40dB */
	if (output->base.type == CRAS_NODE_TYPE_USB) {
		range += cras_alsa_mixer_get_dB_range(mixer);
		range += cras_alsa_mixer_get_output_dB_range(
				output->mixer_output);
		if (range < 4000)
			output->base.software_volume_needed = 1;
	}
	if (output->base.software_volume_needed)
		syslog(LOG_DEBUG, "Use software volume for node: %s",
		       output->base.name);
}

static void set_input_node_software_volume_needed(
	struct alsa_input_node *input, struct alsa_io *aio)
{
	long max_software_gain;
	int rc;

	input->base.software_volume_needed = 0;
	input->base.max_software_gain = 0;

	/* Enable software gain only if max software gain is specified in UCM.*/
	if (!aio->ucm)
		return;

	rc = ucm_get_max_software_gain(aio->ucm, input->base.name,
	                               &max_software_gain);
	if (rc)
		return;

	input->base.software_volume_needed = 1;
	input->base.max_software_gain = max_software_gain;
	syslog(LOG_INFO,
	       "Use software gain for %s with max %ld because it is specified"
	       " in UCM", input->base.name, max_software_gain);
}

static void check_auto_unplug_output_node(struct alsa_io *aio,
					  struct cras_ionode *node,
					  int plugged)
{
	struct cras_ionode *tmp;

	if (!auto_unplug_output_node(aio))
		return;

	/* Auto unplug internal speaker if any output node has been created */
	if (!strcmp(node->name, INTERNAL_SPEAKER) && plugged) {
		DL_FOREACH(aio->base.nodes, tmp)
			if (tmp->plugged && (tmp != node))
				cras_iodev_set_node_attr(node,
							 IONODE_ATTR_PLUGGED,
							 0);
	} else {
		DL_FOREACH(aio->base.nodes, tmp) {
			if (!strcmp(tmp->name, INTERNAL_SPEAKER))
				cras_iodev_set_node_attr(tmp,
							 IONODE_ATTR_PLUGGED,
							 !plugged);
		}
	}
}

/* Callback for listing mixer outputs.  The mixer will call this once for each
 * output associated with this device.  Most commonly this is used to tell the
 * device it has Headphones and Speakers. */
static struct alsa_output_node *new_output(struct alsa_io *aio,
					   struct mixer_control *cras_output,
					   const char *name)
{
	struct alsa_output_node *output;
	syslog(LOG_DEBUG, "New output node for '%s'", name);
	if (aio == NULL) {
		syslog(LOG_ERR, "Invalid aio when listing outputs.");
		return NULL;
	}
	output = (struct alsa_output_node *)calloc(1, sizeof(*output));
	if (output == NULL) {
		syslog(LOG_ERR, "Out of memory when listing outputs.");
		return NULL;
	}
	output->base.dev = &aio->base;
	output->base.idx = aio->next_ionode_index++;
	output->base.stable_id = SuperFastHash(name,
					       sizeof(name),
					       aio->base.info.stable_id);
	output->mixer_output = cras_output;
	strncpy(output->base.name, name, sizeof(output->base.name) - 1);
	set_node_initial_state(&output->base, aio->card_type);
	set_output_node_software_volume_needed(output, aio);

	cras_iodev_add_node(&aio->base, &output->base);

	check_auto_unplug_output_node(aio, &output->base, output->base.plugged);
	return output;
}

static void new_output_by_mixer_control(struct mixer_control *cras_output,
				        void *callback_arg)
{
	struct alsa_io *aio = (struct alsa_io *)callback_arg;
	char node_name[CRAS_IODEV_NAME_BUFFER_SIZE];
	const char *ctl_name;

	ctl_name = cras_alsa_mixer_get_control_name(cras_output);
	if (!ctl_name)
	        return;

	if (aio->card_type == ALSA_CARD_TYPE_USB) {
		snprintf(node_name, sizeof(node_name), "%s: %s",
			aio->base.info.name, ctl_name);
		new_output(aio, cras_output, node_name);
	} else {
		new_output(aio, cras_output, ctl_name);
	}
}

static void check_auto_unplug_input_node(struct alsa_io *aio,
					 struct cras_ionode *node,
					 int plugged)
{
	struct cras_ionode *tmp;
	if (!auto_unplug_input_node(aio))
		return;

	/* Auto unplug internal mic if any input node has already
	 * been created */
	if (!strcmp(node->name, INTERNAL_MICROPHONE) && plugged) {
		DL_FOREACH(aio->base.nodes, tmp)
			if (tmp->plugged && (tmp != node))
				cras_iodev_set_node_attr(node,
							 IONODE_ATTR_PLUGGED,
							 0);
	} else {
		DL_FOREACH(aio->base.nodes, tmp)
			if (!strcmp(tmp->name, INTERNAL_MICROPHONE))
				cras_iodev_set_node_attr(tmp,
							 IONODE_ATTR_PLUGGED,
							 !plugged);
	}
}

static struct alsa_input_node *new_input(struct alsa_io *aio,
		struct mixer_control *cras_input, const char *name)
{
	struct alsa_input_node *input;
	char *mic_positions;

	input = (struct alsa_input_node *)calloc(1, sizeof(*input));
	if (input == NULL) {
		syslog(LOG_ERR, "Out of memory when listing inputs.");
		return NULL;
	}
	input->base.dev = &aio->base;
	input->base.idx = aio->next_ionode_index++;
	input->base.stable_id = SuperFastHash(name,
					      sizeof(name),
					      aio->base.info.stable_id);
	input->mixer_input = cras_input;
	strncpy(input->base.name, name, sizeof(input->base.name) - 1);
	set_node_initial_state(&input->base, aio->card_type);
	set_input_node_software_volume_needed(input, aio);

	/* Check mic positions only for internal mic. */
	if (aio->ucm && input->base.type == CRAS_NODE_TYPE_INTERNAL_MIC) {
		mic_positions = ucm_get_mic_positions(aio->ucm);
		if (mic_positions) {
			strncpy(input->base.mic_positions, mic_positions,
				sizeof(input->base.mic_positions) - 1);
			free(mic_positions);
		}
	}

	cras_iodev_add_node(&aio->base, &input->base);
	check_auto_unplug_input_node(aio, &input->base,
				     input->base.plugged);
	return input;
}

static void new_input_by_mixer_control(struct mixer_control *cras_input,
				       void *callback_arg)
{
	struct alsa_io *aio = (struct alsa_io *)callback_arg;
	char node_name[CRAS_IODEV_NAME_BUFFER_SIZE];
	const char *ctl_name = cras_alsa_mixer_get_control_name(cras_input);

	if (aio->card_type == ALSA_CARD_TYPE_USB) {
		snprintf(node_name , sizeof(node_name), "%s: %s",
			 aio->base.info.name, ctl_name);
		new_input(aio, cras_input, node_name);
	} else {
		new_input(aio, cras_input, ctl_name);
	}
}

/* Finds the output node associated with the jack. Returns NULL if not found. */
static struct alsa_output_node *get_output_node_from_jack(
		struct alsa_io *aio, const struct cras_alsa_jack *jack)
{
	struct mixer_control *mixer_output;
	struct cras_ionode *node = NULL;
	struct alsa_output_node *aout = NULL;

	/* Search by jack first. */
	DL_SEARCH_SCALAR_WITH_CAST(aio->base.nodes, node, aout,
				   jack, jack);
	if (aout)
		return aout;

	/* Search by mixer control next. */
	mixer_output = cras_alsa_jack_get_mixer_output(jack);
	if (mixer_output == NULL)
		return NULL;

	DL_SEARCH_SCALAR_WITH_CAST(aio->base.nodes, node, aout,
				   mixer_output, mixer_output);
	return aout;
}

static struct alsa_input_node *get_input_node_from_jack(
		struct alsa_io *aio, const struct cras_alsa_jack *jack)
{
	struct mixer_control *mixer_input;
	struct cras_ionode *node = NULL;
	struct alsa_input_node *ain = NULL;

	mixer_input = cras_alsa_jack_get_mixer_input(jack);
	if (mixer_input == NULL) {
		DL_SEARCH_SCALAR_WITH_CAST(aio->base.nodes, node, ain,
					   jack, jack);
		return ain;
	}

	DL_SEARCH_SCALAR_WITH_CAST(aio->base.nodes, node, ain,
				   mixer_input, mixer_input);
	return ain;
}

/* Returns the dsp name specified in the ucm config. If there is a dsp
 * name specified for the jack of the active node, use that. Otherwise
 * use the default dsp name for the alsa_io device. */
static const char *get_active_dsp_name(struct alsa_io *aio)
{
	struct cras_ionode *node = aio->base.active_node;
	const struct cras_alsa_jack *jack;

	if (node == NULL)
		return NULL;

	if (aio->base.direction == CRAS_STREAM_OUTPUT)
		jack = ((struct alsa_output_node *) node)->jack;
	else
		jack = ((struct alsa_input_node *) node)->jack;

	return cras_alsa_jack_get_dsp_name(jack) ? : aio->dsp_name_default;
}

/* Creates volume curve for the node associated with given jack. */
static struct cras_volume_curve *create_volume_curve_for_jack(
		const struct cras_alsa_mixer *mixer,
		const struct cras_alsa_jack *jack)
{
	struct cras_volume_curve *curve;
	const char *name;

	/* Use jack's UCM device name as key to get volume curve. */
	name = cras_alsa_jack_get_ucm_device(jack);
	curve = cras_alsa_mixer_create_volume_curve_for_name(mixer, name);
	if (curve)
		return curve;

	/* Use alsa jack's name as key to get volume curve. */
	name = cras_alsa_jack_get_name(jack);
	curve = cras_alsa_mixer_create_volume_curve_for_name(mixer, name);
	if (curve)
		return curve;

	return NULL;
}

/* Callback that is called when an output jack is plugged or unplugged. */
static void jack_output_plug_event(const struct cras_alsa_jack *jack,
				    int plugged,
				    void *arg)
{
	struct alsa_io *aio;
	struct alsa_output_node *node;
	const char *jack_name;

	if (arg == NULL)
		return;

	aio = (struct alsa_io *)arg;
	node = get_output_node_from_jack(aio, jack);
	jack_name = cras_alsa_jack_get_name(jack);
	if (!strcmp(jack_name, "Speaker Phantom Jack"))
		jack_name = INTERNAL_SPEAKER;

	/* If there isn't a node for this jack, create one. */
	if (node == NULL) {
		if (aio->fully_specified) {
			/* When fully specified, can't have new nodes. */
			syslog(LOG_ERR, "No matching output node for jack %s!",
			       jack_name);
			return;
		}
		node = new_output(aio, NULL, jack_name);
		if (node == NULL)
			return;

		cras_alsa_jack_update_node_type(jack, &(node->base.type));
	}

	if (!node->jack) {
		if (aio->fully_specified)
			syslog(LOG_ERR,
			       "Jack '%s' was found to match output node '%s'."
			       " Please fix your UCM configuration to match.",
			       jack_name, node->base.name);

		/* If we already have the node, associate with the jack. */
		node->jack_curve = create_volume_curve_for_jack(aio->mixer,
								jack);
		node->jack = jack;
	}

	syslog(LOG_DEBUG, "%s plugged: %d, %s", jack_name, plugged,
	       cras_alsa_mixer_get_control_name(node->mixer_output));

	cras_alsa_jack_update_monitor_name(jack, node->base.name,
					   sizeof(node->base.name));
	/* The name got from jack might be an invalid UTF8 string. */
	if (!is_utf8_string(node->base.name))
		drop_node_name(&node->base);

	cras_iodev_set_node_attr(&node->base, IONODE_ATTR_PLUGGED, plugged);

	check_auto_unplug_output_node(aio, &node->base, plugged);
}

/* Callback that is called when an input jack is plugged or unplugged. */
static void jack_input_plug_event(const struct cras_alsa_jack *jack,
				  int plugged,
				  void *arg)
{
	struct alsa_io *aio;
	struct alsa_input_node *node;
	struct mixer_control *cras_input;
	const char *jack_name;

	if (arg == NULL)
		return;
	aio = (struct alsa_io *)arg;
	node = get_input_node_from_jack(aio, jack);
	jack_name = cras_alsa_jack_get_name(jack);

	/* If there isn't a node for this jack, create one. */
	if (node == NULL) {
		if (aio->fully_specified) {
			/* When fully specified, can't have new nodes. */
			syslog(LOG_ERR, "No matching input node for jack %s!",
			       jack_name);
			return;
		}
		cras_input = cras_alsa_jack_get_mixer_input(jack);
		node = new_input(aio, cras_input, jack_name);
		if (node == NULL)
			return;
	}

	syslog(LOG_DEBUG, "%s plugged: %d, %s", jack_name, plugged,
	       cras_alsa_mixer_get_control_name(node->mixer_input));

	/* If we already have the node, associate with the jack. */
	if (!node->jack) {
		if (aio->fully_specified)
			syslog(LOG_ERR,
			       "Jack '%s' was found to match input node '%s'."
			       " Please fix your UCM configuration to match.",
			       jack_name, node->base.name);
		node->jack = jack;
	}

	cras_iodev_set_node_attr(&node->base, IONODE_ATTR_PLUGGED, plugged);

	check_auto_unplug_input_node(aio, &node->base, plugged);
}

/* Sets the name of the given iodev, using the name and index of the card
 * combined with the device index and direction */
static void set_iodev_name(struct cras_iodev *dev,
			   const char *card_name,
			   const char *dev_name,
			   size_t card_index,
			   size_t device_index,
			   enum CRAS_ALSA_CARD_TYPE card_type,
			   size_t usb_vid,
			   size_t usb_pid)
{
	snprintf(dev->info.name,
		 sizeof(dev->info.name),
		 "%s: %s:%zu,%zu",
		 card_name,
		 dev_name,
		 card_index,
		 device_index);
	dev->info.name[ARRAY_SIZE(dev->info.name) - 1] = '\0';
	syslog(LOG_DEBUG, "Add device name=%s", dev->info.name);

	dev->info.stable_id = SuperFastHash(card_name,
					    strlen(card_name),
					    strlen(card_name));
	dev->info.stable_id = SuperFastHash(dev_name,
					    strlen(dev_name),
					    dev->info.stable_id);

	switch (card_type) {
	case ALSA_CARD_TYPE_INTERNAL:
		dev->info.stable_id = SuperFastHash((const char *)&device_index,
						    sizeof(device_index),
						    dev->info.stable_id);
		break;
	case ALSA_CARD_TYPE_USB:
		dev->info.stable_id = SuperFastHash((const char *)&usb_vid,
						    sizeof(usb_vid),
						    dev->info.stable_id);
		dev->info.stable_id = SuperFastHash((const char *)&usb_pid,
						    sizeof(usb_pid),
						    dev->info.stable_id);
		break;
	}
	syslog(LOG_DEBUG, "Stable ID=%08x", dev->info.stable_id);
}

/* Updates the supported sample rates and channel counts. */
static int update_supported_formats(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	int err;

	free(iodev->supported_rates);
	iodev->supported_rates = NULL;
	free(iodev->supported_channel_counts);
	iodev->supported_channel_counts = NULL;
	free(iodev->supported_formats);
	iodev->supported_formats = NULL;

	err = cras_alsa_fill_properties(aio->dev, aio->alsa_stream,
					&iodev->supported_rates,
					&iodev->supported_channel_counts,
					&iodev->supported_formats);
	return err;
}

/* Builds software volume scalers for output nodes in the device. */
static void build_softvol_scalers(struct alsa_io *aio)
{
	struct cras_ionode *ionode;

	DL_FOREACH(aio->base.nodes, ionode) {
		struct alsa_output_node *aout;
		const struct cras_volume_curve *curve;

		aout = (struct alsa_output_node *)ionode;
		curve = get_curve_for_output_node(aio, aout);

		ionode->softvol_scalers = softvol_build_from_curve(curve);
	}
}

static void enable_active_ucm(struct alsa_io *aio, int plugged)
{
	const struct cras_alsa_jack *jack;
	const char *name;

	if (aio->base.direction == CRAS_STREAM_OUTPUT) {
		struct alsa_output_node *active = get_active_output(aio);
		if (!active)
			return;
		name = active->base.name;
		jack = active->jack;
	} else {
		struct alsa_input_node *active = get_active_input(aio);
		if (!active)
			return;
		name = active->base.name;
		jack = active->jack;
	}

	if (jack)
		cras_alsa_jack_enable_ucm(jack, plugged);
	else if (aio->ucm)
		ucm_set_enabled(aio->ucm, name, plugged);
}

static int fill_whole_buffer_with_zeros(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	int rc;
	uint8_t *dst = NULL;
	size_t format_bytes;

	/* Fill whole buffer with zeros. */
	rc = cras_alsa_mmap_get_whole_buffer(
			aio->handle, &dst, &aio->num_underruns);

	if (rc < 0) {
		syslog(LOG_ERR, "Failed to get whole buffer: %s",
		       snd_strerror(rc));
		return rc;
	}

	format_bytes = cras_get_format_bytes(iodev->format);
	memset(dst, 0, iodev->buffer_size * format_bytes);

	return 0;
}

static int possibly_enter_free_run(struct cras_iodev *odev)
{
	struct alsa_io *aio = (struct alsa_io *)odev;
	int rc;
	unsigned int hw_level, fr_to_write;
	unsigned int target_hw_level = odev->min_cb_level * 2;
	struct timespec hw_tstamp;

	if (aio->is_free_running)
		return 0;

	/* Check if all valid samples are played.
	 * If all valid samples are played, fill whole buffer with zeros. */
	rc = cras_iodev_frames_queued(odev, &hw_tstamp);
	if (rc < 0)
		return rc;
	hw_level = rc;

	if (hw_level < aio->filled_zeros_for_draining || hw_level == 0) {
		rc = fill_whole_buffer_with_zeros(odev);
		if (rc < 0)
			return rc;
		aio->is_free_running = 1;
		return 0;
	}

	/* Fill some zeros to drain valid samples. */
	fr_to_write = cras_iodev_buffer_avail(odev, hw_level);

	if (hw_level <= target_hw_level) {
		fr_to_write = MIN(target_hw_level - hw_level, fr_to_write);
		rc = cras_iodev_fill_odev_zeros(odev, fr_to_write);
		if (rc)
			return rc;
		aio->filled_zeros_for_draining += fr_to_write;
	}

	return 0;
}

static int leave_free_run(struct cras_iodev *odev)
{
	struct alsa_io *aio = (struct alsa_io *)odev;
	int rc;

	if (!aio->is_free_running)
		return 0;

	/* Move appl_ptr to min_buffer_level + min_cb_level frames ahead of
	 * hw_ptr when resuming from free run. */
	rc = cras_alsa_resume_appl_ptr(
			aio->handle,
			odev->min_buffer_level + odev->min_cb_level);
	if (rc) {
		syslog(LOG_ERR, "device %s failed to leave free run, rc = %d",
		       odev->info.name, rc);
		return rc;
	}
	aio->is_free_running = 0;
	aio->filled_zeros_for_draining = 0;

	return 0;
}

/* Free run state is the optimization of no_stream playback on alsa_io.
 * The whole buffer will be filled with zeros. Device can play these zeros
 * indefinitely. When there is new meaningful sample, appl_ptr should be
 * resumed to some distance ahead of hw_ptr. */
static int no_stream(struct cras_iodev *odev, int enable)
{
	if (enable)
		return possibly_enter_free_run(odev);
	else
		return leave_free_run(odev);
}

static int output_should_wake(const struct cras_iodev *odev)
{
	struct alsa_io *aio = (struct alsa_io *)odev;
	if (aio->is_free_running)
		return 0;
	else
		return ((cras_iodev_state(odev) ==
					CRAS_IODEV_STATE_NO_STREAM_RUN) ||
		        (cras_iodev_state(odev) ==
					CRAS_IODEV_STATE_NORMAL_RUN));
}

static unsigned int get_num_underruns(const struct cras_iodev *iodev)
{
	const struct alsa_io *aio = (const struct alsa_io *)iodev;
	return aio->num_underruns;
}

/*
 * Exported Interface.
 */

struct cras_iodev *alsa_iodev_create(size_t card_index,
				     const char *card_name,
				     size_t device_index,
				     const char *dev_name,
				     const char *dev_id,
				     enum CRAS_ALSA_CARD_TYPE card_type,
				     int is_first,
				     struct cras_alsa_mixer *mixer,
				     snd_use_case_mgr_t *ucm,
				     snd_hctl_t *hctl,
				     enum CRAS_STREAM_DIRECTION direction,
				     size_t usb_vid,
				     size_t usb_pid)
{
	struct alsa_io *aio;
	struct cras_iodev *iodev;
	int err;

	if (direction != CRAS_STREAM_INPUT && direction != CRAS_STREAM_OUTPUT)
		return NULL;

	aio = (struct alsa_io *)calloc(1, sizeof(*aio));
	if (!aio)
		return NULL;
	iodev = &aio->base;
	iodev->direction = direction;

	aio->device_index = device_index;
	aio->card_type = card_type;
	aio->is_first = is_first;
	aio->handle = NULL;
	if (dev_name) {
		aio->dev_name = strdup(dev_name);
		if (!aio->dev_name)
			goto cleanup_iodev;
	}
	if (dev_id) {
		aio->dev_id = strdup(dev_id);
		if (!aio->dev_id)
			goto cleanup_iodev;
	}
	aio->is_free_running = 0;
	aio->filled_zeros_for_draining = 0;
	aio->dev = (char *)malloc(MAX_ALSA_DEV_NAME_LENGTH);
	if (aio->dev == NULL)
		goto cleanup_iodev;
	snprintf(aio->dev,
		 MAX_ALSA_DEV_NAME_LENGTH,
		 "hw:%zu,%zu",
		 card_index,
		 device_index);

	if (direction == CRAS_STREAM_INPUT) {
		aio->alsa_stream = SND_PCM_STREAM_CAPTURE;
		aio->base.set_capture_gain = set_alsa_capture_gain;
		aio->base.set_capture_mute = set_alsa_capture_gain;
	} else {
		aio->alsa_stream = SND_PCM_STREAM_PLAYBACK;
		aio->base.set_volume = set_alsa_volume;
		aio->base.set_mute = set_alsa_volume;
	}
	iodev->open_dev = open_dev;
	iodev->close_dev = close_dev;
	iodev->update_supported_formats = update_supported_formats;
	iodev->frames_queued = frames_queued;
	iodev->delay_frames = delay_frames;
	iodev->get_buffer = get_buffer;
	iodev->put_buffer = put_buffer;
	iodev->flush_buffer = flush_buffer;
	iodev->start = start;
	iodev->update_active_node = update_active_node;
	iodev->update_channel_layout = update_channel_layout;
	iodev->set_hotword_model = set_hotword_model;
	iodev->get_hotword_models = get_hotword_models;
	iodev->no_stream = cras_iodev_default_no_stream_playback;
	iodev->get_num_underruns = get_num_underruns;

	if (card_type == ALSA_CARD_TYPE_USB)
		iodev->min_buffer_level = USB_EXTRA_BUFFER_FRAMES;

	err = cras_alsa_fill_properties(aio->dev, aio->alsa_stream,
					&iodev->supported_rates,
					&iodev->supported_channel_counts,
					&iodev->supported_formats);
	if (err < 0 || iodev->supported_rates[0] == 0 ||
	    iodev->supported_channel_counts[0] == 0 ||
	    iodev->supported_formats[0] == 0) {
		syslog(LOG_ERR, "cras_alsa_fill_properties: %s", strerror(err));
		goto cleanup_iodev;
	}

	aio->mixer = mixer;
	aio->ucm = ucm;
	if (ucm) {
		unsigned int level;

		aio->dsp_name_default = ucm_get_dsp_name_default(ucm,
								 direction);
		/* Set callback for swap mode if it is supported
		 * in ucm modifier. */
		if (ucm_swap_mode_exists(ucm))
			aio->base.set_swap_mode_for_node =
				set_alsa_node_swapped;

		level = ucm_get_min_buffer_level(ucm);
		if (level && direction == CRAS_STREAM_OUTPUT)
			iodev->min_buffer_level = level;

		if (ucm_get_optimize_no_stream_flag(ucm) &&
		    direction == CRAS_STREAM_OUTPUT) {
			syslog(LOG_DEBUG, "Use no_stream ops on %s:%s",
			       card_name, dev_name);
			iodev->no_stream = no_stream;
			iodev->output_should_wake = output_should_wake;
		}

		aio->enable_htimestamp =
			ucm_get_enable_htimestamp_flag(ucm);
        }

	set_iodev_name(iodev, card_name, dev_name, card_index, device_index,
		       card_type, usb_vid, usb_pid);

	aio->jack_list =
		cras_alsa_jack_list_create(
			card_index,
			card_name,
			device_index,
			is_first,
			mixer,
			ucm,
			hctl,
			direction,
			direction == CRAS_STREAM_OUTPUT ?
				     jack_output_plug_event :
				     jack_input_plug_event,
			aio);
	if (!aio->jack_list)
		goto cleanup_iodev;

	/* HDMI outputs don't have volume adjustment, do it in software. */
	if (direction == CRAS_STREAM_OUTPUT && strstr(dev_name, HDMI))
		iodev->software_volume_needed = 1;

	/* Add this now so that cleanup of the iodev (in case of error or card
	 * card removal will function as expected. */
	if (direction == CRAS_STREAM_OUTPUT)
		cras_iodev_list_add_output(&aio->base);
	else
		cras_iodev_list_add_input(&aio->base);
	return &aio->base;

cleanup_iodev:
	free_alsa_iodev_resources(aio);
	free(aio);
	return NULL;
}

int alsa_iodev_legacy_complete_init(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	const char *dev_name;
	const char *dev_id;
	enum CRAS_STREAM_DIRECTION direction;
	int err;
	int is_first;
	struct cras_alsa_mixer *mixer;

	if (!aio)
		return -EINVAL;
	direction = iodev->direction;
	dev_name = aio->dev_name;
	dev_id = aio->dev_id;
	is_first = aio->is_first;
	mixer = aio->mixer;

	/* Create output nodes for mixer controls, such as Headphone
	 * and Speaker, only for the first device. */
	if (direction == CRAS_STREAM_OUTPUT && is_first)
		cras_alsa_mixer_list_outputs(mixer,
				new_output_by_mixer_control, aio);
	else if (direction == CRAS_STREAM_INPUT && is_first)
		cras_alsa_mixer_list_inputs(mixer,
				new_input_by_mixer_control, aio);

	err = cras_alsa_jack_list_find_jacks_by_name_matching(aio->jack_list);
	if (err)
		return err;

	/* Create nodes for jacks that aren't associated with an
	 * already existing node. Get an initial read of the jacks for
	 * this device. */
	cras_alsa_jack_list_report(aio->jack_list);

	/* Make a default node if there is still no node for this
	 * device, or we still don't have the "Speaker"/"Internal Mic"
	 * node for the first internal device. Note that the default
	 * node creation can be supressed by UCM flags for platforms
	 * which really don't have an internal device. */
	if ((direction == CRAS_STREAM_OUTPUT) &&
			!no_create_default_output_node(aio)) {
		if (first_internal_device(aio) &&
		    !has_node(aio, INTERNAL_SPEAKER) &&
		    !has_node(aio, HDMI)) {
			if (strstr(aio->base.info.name, HDMI))
				new_output(aio, NULL, HDMI);
			else
				new_output(aio, NULL, INTERNAL_SPEAKER);
		} else if (!aio->base.nodes) {
			new_output(aio, NULL, DEFAULT);
		}
	} else if ((direction == CRAS_STREAM_INPUT) &&
			!no_create_default_input_node(aio)) {
		if (first_internal_device(aio) &&
		    !has_node(aio, INTERNAL_MICROPHONE))
			new_input(aio, NULL, INTERNAL_MICROPHONE);
		else if (strstr(dev_name, KEYBOARD_MIC))
			new_input(aio, NULL, KEYBOARD_MIC);
		else if (dev_id && strstr(dev_id, HOTWORD_DEV))
			new_input(aio, NULL, HOTWORD_DEV);
		else if (!aio->base.nodes)
			new_input(aio, NULL, DEFAULT);
	}

	/* Build software volume scalers. */
	if (direction == CRAS_STREAM_OUTPUT)
		build_softvol_scalers(aio);

	/* Set the active node as the best node we have now. */
	alsa_iodev_set_active_node(&aio->base,
				   first_plugged_node(&aio->base),
				   0);

	/* Set plugged for the first USB device per card when it appears. */
	if (aio->card_type == ALSA_CARD_TYPE_USB && is_first)
		cras_iodev_set_node_attr(iodev->active_node,
					 IONODE_ATTR_PLUGGED, 1);
	return 0;
}

int alsa_iodev_ucm_add_nodes_and_jacks(struct cras_iodev *iodev,
				       struct ucm_section *section)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	struct mixer_control *control;
	struct alsa_input_node *input_node = NULL;
	struct cras_alsa_jack *jack;
	struct alsa_output_node *output_node = NULL;
	int rc;

	if (!aio || !section)
		return -EINVAL;
	if ((uint32_t)section->dev_idx != aio->device_index)
		return -EINVAL;

	/* This iodev is fully specified. Avoid automatic node creation. */
	aio->fully_specified = 1;

	/* Check here in case the DmaPeriodMicrosecs flag has only been
	 * specified on one of many device entries with the same PCM. */
	if (!aio->dma_period_set_microsecs)
		aio->dma_period_set_microsecs =
			ucm_get_dma_period_for_dev(aio->ucm, section->name);

	/* Create a node matching this section. If there is a matching
	 * control use that, otherwise make a node without a control. */
	control = cras_alsa_mixer_get_control_for_section(aio->mixer, section);
	if (iodev->direction == CRAS_STREAM_OUTPUT) {
		output_node = new_output(aio, control, section->name);
		if (!output_node)
			return -ENOMEM;
	} else if (iodev->direction == CRAS_STREAM_INPUT) {
		input_node = new_input(aio, control, section->name);
		if (!input_node)
			return -ENOMEM;
	}

	/* Find any jack controls for this device. */
	rc = cras_alsa_jack_list_add_jack_for_section(
					aio->jack_list, section, &jack);
	if (rc)
		return rc;

	/* Associated the jack with the node. */
	if (jack) {
		if (output_node) {
			output_node->jack = jack;
			output_node->jack_curve =
				create_volume_curve_for_jack(aio->mixer, jack);
		} else if (input_node) {
			input_node->jack = jack;
		}
	}
	return 0;
}

void alsa_iodev_ucm_complete_init(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;

	if (!iodev)
		return;

	/* Get an initial read of the jacks for this device. */
	cras_alsa_jack_list_report(aio->jack_list);

	/* Build software volume scaler. */
	if (iodev->direction == CRAS_STREAM_OUTPUT)
		build_softvol_scalers(aio);

	/* Set the active node as the best node we have now. */
	alsa_iodev_set_active_node(&aio->base,
				   first_plugged_node(&aio->base),
				   0);

	/* Set plugged for the first USB device per card when it appears. */
	if (aio->card_type == ALSA_CARD_TYPE_USB && aio->is_first)
		cras_iodev_set_node_attr(iodev->active_node,
					 IONODE_ATTR_PLUGGED, 1);
}

void alsa_iodev_destroy(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	int rc;

	cras_alsa_jack_list_destroy(aio->jack_list);
	if (iodev->direction == CRAS_STREAM_INPUT)
		rc = cras_iodev_list_rm_input(iodev);
	else
		rc = cras_iodev_list_rm_output(iodev);

	if (rc == -EBUSY) {
		syslog(LOG_ERR, "Failed to remove iodev %s", iodev->info.name);
		return;
	}

	/* Free resources when device successfully removed. */
	free_alsa_iodev_resources(aio);
	free(iodev);
}

unsigned alsa_iodev_index(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	return aio->device_index;
}

int alsa_iodev_has_hctl_jacks(struct cras_iodev *iodev)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;
	return cras_alsa_jack_list_has_hctl_jacks(aio->jack_list);
}

static void alsa_iodev_unmute_node(struct alsa_io *aio,
				   struct cras_ionode *ionode)
{
	struct alsa_output_node *active = (struct alsa_output_node *)ionode;
	struct mixer_control *mixer = active->mixer_output;
	struct alsa_output_node *output;
	struct cras_ionode *node;

	/* If this node is associated with mixer output, unmute the
	 * active mixer output and mute all others, otherwise just set
	 * the node as active and set the volume curve. */
	if (mixer) {
		set_alsa_mute(aio, 1);
		/* Unmute the active mixer output, mute all others. */
		DL_FOREACH(aio->base.nodes, node) {
			output = (struct alsa_output_node *)node;
			if (output->mixer_output)
				cras_alsa_mixer_set_output_active_state(
					output->mixer_output, node == ionode);
		}
	}
}

static int alsa_iodev_set_active_node(struct cras_iodev *iodev,
				      struct cras_ionode *ionode,
				      unsigned dev_enabled)
{
	struct alsa_io *aio = (struct alsa_io *)iodev;

	if (iodev->active_node == ionode) {
		enable_active_ucm(aio, dev_enabled);
		return 0;
	}

	/* Disable jack ucm before switching node. */
	enable_active_ucm(aio, 0);
	if (dev_enabled && (iodev->direction == CRAS_STREAM_OUTPUT))
		alsa_iodev_unmute_node(aio, ionode);

	cras_iodev_set_active_node(iodev, ionode);
	aio->base.dsp_name = get_active_dsp_name(aio);
	cras_iodev_update_dsp(iodev);
	enable_active_ucm(aio, dev_enabled);
	/* Setting the volume will also unmute if the system isn't muted. */
	init_device_settings(aio);
	return 0;
}