summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/audio/drv/audio810/audio810.c
blob: 9d186df9e09f4654d485103e970f93d1693f5c1f (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
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


/*
 * audio810 Audio Driver
 *
 * The driver is primarily targeted at providing audio support for the
 * W1100z and W2100z systems, which use the AMD 8111 audio core and
 * the Realtek ALC 655 codec. The ALC 655 chip supports only fixed 48k
 * sample rate. However, the audio core of AMD 8111 is completely
 * compatible to the Intel ICHx chips (Intel 8x0 chipsets), so the
 * driver can work for the ICHx.  We only support the 48k maximum
 * rate, since we only have a single PCM out channel.
 *
 * The AMD 8111 audio core, as an AC'97 controller, has independent
 * channels for PCM in, PCM out, mic in, modem in, and modem out.
 * The AC'97 controller is a PCI bus master with scatter/gather
 * support. Each channel has a DMA engine. Currently, we use only
 * the PCM in and PCM out channels. Each DMA engine uses one buffer
 * descriptor list. And the buffer descriptor list is an array of up
 * to 32 entries, each of which describes a data buffer. Each entry
 * contains a pointer to a data buffer, control bits, and the length
 * of the buffer being pointed to, where the length is expressed as
 * the number of samples. This, combined with the 16-bit sample size,
 * gives the actual physical length of the buffer.
 *
 * A workaround for the AD1980 and AD1985 codec:
 *	Most vendors connect the surr-out of the codecs to the line-out jack.
 *	So far we haven't found which vendors don't do that. So we assume that
 *	all vendors swap the surr-out and the line-out outputs. So we need swap
 *	the two outputs. But we still internally process the
 *	"ad198x-swap-output" property. If someday some vendors do not swap the
 *	outputs, we would set "ad198x-swap-output = 0" in the
 *	/kernel/drv/audio810.conf file, and unload and reload the audio810
 *	driver (or reboot).
 *
 * 	NOTE:
 * 	This driver depends on the drv/audio and misc/ac97
 * 	modules being loaded first.
 */
#include <sys/types.h>
#include <sys/modctl.h>
#include <sys/kmem.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/pci.h>
#include <sys/note.h>
#include <sys/audio/audio_driver.h>
#include <sys/audio/ac97.h>
#include "audio810.h"

/*
 * Module linkage routines for the kernel
 */
static int audio810_ddi_attach(dev_info_t *, ddi_attach_cmd_t);
static int audio810_ddi_detach(dev_info_t *, ddi_detach_cmd_t);
static int audio810_ddi_quiesce(dev_info_t *);

/*
 * Entry point routine prototypes
 */
static int audio810_open(void *, int, unsigned *, unsigned *, caddr_t *);
static void audio810_close(void *);
static int audio810_start(void *);
static void audio810_stop(void *);
static int audio810_format(void *);
static int audio810_channels(void *);
static int audio810_rate(void *);
static uint64_t audio810_count(void *);
static void audio810_sync(void *, unsigned);
static size_t audio810_qlen(void *);

static audio_engine_ops_t audio810_engine_ops = {
	AUDIO_ENGINE_VERSION,
	audio810_open,
	audio810_close,
	audio810_start,
	audio810_stop,
	audio810_count,
	audio810_format,
	audio810_channels,
	audio810_rate,
	audio810_sync,
	audio810_qlen
};

/*
 * interrupt handler
 */
static uint_t	audio810_intr(caddr_t);

/*
 * Local Routine Prototypes
 */
static int audio810_attach(dev_info_t *);
static int audio810_resume(dev_info_t *);
static int audio810_detach(dev_info_t *);
static int audio810_suspend(dev_info_t *);

static int audio810_alloc_port(audio810_state_t *, int, uint8_t);
static void audio810_start_port(audio810_port_t *);
static void audio810_stop_port(audio810_port_t *);
static void audio810_reset_port(audio810_port_t *);
static void audio810_update_port(audio810_port_t *);
static int audio810_codec_sync(audio810_state_t *);
static void audio810_write_ac97(void *, uint8_t, uint16_t);
static uint16_t audio810_read_ac97(void *, uint8_t);
static int audio810_map_regs(dev_info_t *, audio810_state_t *);
static void audio810_unmap_regs(audio810_state_t *);
static void audio810_stop_dma(audio810_state_t *);
static int audio810_chip_init(audio810_state_t *);
static void audio810_destroy(audio810_state_t *);

/*
 * Global variables, but used only by this file.
 */

/* driver name, so we don't have to call ddi_driver_name() or hard code strs */
static char	*audio810_name = I810_NAME;


/*
 * DDI Structures
 */

/* Device operations structure */
static struct dev_ops audio810_dev_ops = {
	DEVO_REV,		/* devo_rev */
	0,			/* devo_refcnt */
	NULL,			/* devo_getinfo */
	nulldev,		/* devo_identify - obsolete */
	nulldev,		/* devo_probe */
	audio810_ddi_attach,	/* devo_attach */
	audio810_ddi_detach,	/* devo_detach */
	nodev,			/* devo_reset */
	NULL,			/* devi_cb_ops */
	NULL,			/* devo_bus_ops */
	NULL,			/* devo_power */
	audio810_ddi_quiesce,	/* devo_quiesce */
};

/* Linkage structure for loadable drivers */
static struct modldrv audio810_modldrv = {
	&mod_driverops,		/* drv_modops */
	I810_MOD_NAME,		/* drv_linkinfo */
	&audio810_dev_ops,	/* drv_dev_ops */
};

/* Module linkage structure */
static struct modlinkage audio810_modlinkage = {
	MODREV_1,			/* ml_rev */
	(void *)&audio810_modldrv,	/* ml_linkage */
	NULL				/* NULL terminates the list */
};

/*
 * device access attributes for register mapping
 */
static struct ddi_device_acc_attr dev_attr = {
	DDI_DEVICE_ATTR_V0,
	DDI_STRUCTURE_LE_ACC,
	DDI_STRICTORDER_ACC
};

static struct ddi_device_acc_attr buf_attr = {
	DDI_DEVICE_ATTR_V0,
	DDI_STRUCTURE_LE_ACC,
	DDI_STRICTORDER_ACC
};

/*
 * DMA attributes of buffer descriptor list
 */
static ddi_dma_attr_t	bdlist_dma_attr = {
	DMA_ATTR_V0,	/* version */
	0,		/* addr_lo */
	0xffffffff,	/* addr_hi */
	0x0000ffff,	/* count_max */
	8,		/* align, BDL must be aligned on a 8-byte boundary */
	0x3c,		/* burstsize */
	8,		/* minxfer, set to the size of a BDlist entry */
	0x0000ffff,	/* maxxfer */
	0x00000fff,	/* seg, set to the RAM pagesize of intel platform */
	1,		/* sgllen, there's no scatter-gather list */
	8,		/* granular, set to the value of minxfer */
	0		/* flags, use virtual address */
};

/*
 * DMA attributes of buffers to be used to receive/send audio data
 */
static ddi_dma_attr_t	sample_buf_dma_attr = {
	DMA_ATTR_V0,
	0,		/* addr_lo */
	0xffffffff,	/* addr_hi */
	0x0001ffff,	/* count_max */
	4,		/* align, data buffer is aligned on a 4-byte boundary */
	0x3c,		/* burstsize */
	4,		/* minxfer, set to the size of a sample data */
	0x0001ffff,	/* maxxfer */
	0x0001ffff,	/* seg */
	1,		/* sgllen, no scatter-gather */
	4,		/* granular, set to the value of minxfer */
	0,		/* flags, use virtual address */
};

/*
 * _init()
 *
 * Description:
 *	Driver initialization, called when driver is first loaded.
 *	This is how access is initially given to all the static structures.
 *
 * Arguments:
 *	None
 *
 * Returns:
 *	mod_install() status, see mod_install(9f)
 */
int
_init(void)
{
	int	error;

	audio_init_ops(&audio810_dev_ops, I810_NAME);

	if ((error = mod_install(&audio810_modlinkage)) != 0) {
		audio_fini_ops(&audio810_dev_ops);
	}

	return (error);
}

/*
 * _fini()
 *
 * Description:
 *	Module de-initialization, called when the driver is to be unloaded.
 *
 * Arguments:
 *	None
 *
 * Returns:
 *	mod_remove() status, see mod_remove(9f)
 */
int
_fini(void)
{
	int		error;

	if ((error = mod_remove(&audio810_modlinkage)) != 0) {
		return (error);
	}

	/* clean up ops */
	audio_fini_ops(&audio810_dev_ops);

	return (0);
}

/*
 * _info()
 *
 * Description:
 *	Module information, returns information about the driver.
 *
 * Arguments:
 *	modinfo		*modinfop	Pointer to the opaque modinfo structure
 *
 * Returns:
 *	mod_info() status, see mod_info(9f)
 */
int
_info(struct modinfo *modinfop)
{
	return (mod_info(&audio810_modlinkage, modinfop));
}


/* ******************* Driver Entry Points ********************************* */

/*
 * audio810_ddi_attach()
 *
 * Description:
 *	Implements the DDI attach(9e) entry point.
 *
 * Arguments:
 *	dev_info_t	*dip	Pointer to the device's dev_info struct
 *	ddi_attach_cmd_t cmd	Attach command
 *
 * Returns:
 *	DDI_SUCCESS		The driver was initialized properly
 *	DDI_FAILURE		The driver couldn't be initialized properly
 */
static int
audio810_ddi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	switch (cmd) {
	case DDI_ATTACH:
		return (audio810_attach(dip));

	case DDI_RESUME:
		return (audio810_resume(dip));
	}
	return (DDI_FAILURE);
}

/*
 * audio810_ddi_detach()
 *
 * Description:
 *	Implements the detach(9e) entry point.
 *
 * Arguments:
 *	dev_info_t		*dip	Pointer to the device's dev_info struct
 *	ddi_detach_cmd_t	cmd	Detach command
 *
 * Returns:
 *	DDI_SUCCESS	The driver was detached
 *	DDI_FAILURE	The driver couldn't be detached
 */
static int
audio810_ddi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	switch (cmd) {
	case DDI_DETACH:
		return (audio810_detach(dip));

	case DDI_SUSPEND:
		return (audio810_suspend(dip));
	}
	return (DDI_FAILURE);
}

/*
 * audio810_ddi_quiesce()
 *
 * Description:
 *	Implements the quiesce(9e) entry point.
 *
 * Arguments:
 *	dev_info_t		*dip	Pointer to the device's dev_info struct
 *
 * Returns:
 *	DDI_SUCCESS	The driver was quiesced
 *	DDI_FAILURE	The driver couldn't be quiesced
 */
static int
audio810_ddi_quiesce(dev_info_t *dip)
{
	audio810_state_t	*statep;

	if ((statep = ddi_get_driver_private(dip)) == NULL)
		return (DDI_FAILURE);

	audio810_stop_dma(statep);
	return (DDI_SUCCESS);
}

/*
 * audio810_intr()
 *
 * Description:
 *	Interrupt service routine for both play and record. For play we
 *	get the next buffers worth of audio. For record we send it on to
 *	the mixer.
 *
 *	Each of buffer descriptor has a field IOC(interrupt on completion)
 *	When both this and the IOC bit of correspondent dma control register
 *	is set, it means that the controller should issue an interrupt upon
 *	completion of this buffer.
 *	(AMD 8111 hypertransport I/O hub data sheet. 3.8.3 page 71)
 *
 * Arguments:
 *	caddr_t		arg	Pointer to the interrupting device's state
 *				structure
 *
 * Returns:
 *	DDI_INTR_CLAIMED	Interrupt claimed and processed
 *	DDI_INTR_UNCLAIMED	Interrupt not claimed, and thus ignored
 */
static uint_t
audio810_intr(caddr_t arg)
{
	audio810_state_t	*statep;
	uint16_t		gsr;

	statep = (void *)arg;
	mutex_enter(&statep->inst_lock);

	if (statep->suspended) {
		mutex_exit(&statep->inst_lock);
		return (DDI_INTR_UNCLAIMED);
	}

	gsr = I810_BM_GET32(I810_REG_GSR);

	/* check if device is interrupting */
	if ((gsr & I810_GSR_USE_INTR) == 0) {
		mutex_exit(&statep->inst_lock);
		return (DDI_INTR_UNCLAIMED);
	}

	for (int pnum = 0; pnum < I810_NUM_PORTS; pnum++) {
		audio810_port_t	*port;
		uint8_t		regoff, index;

		port = statep->ports[pnum];
		if (port == NULL) {
			continue;
		}
		regoff = port->regoff;

		if (!(I810_BM_GET8(port->stsoff) & I810_BM_SR_BCIS))
			continue;

		/* update the LVI -- we just set it to the current value - 1 */
		index = I810_BM_GET8(regoff + I810_OFFSET_CIV);
		index = (index - 1) % I810_BD_NUMS;

		I810_BM_PUT8(regoff + I810_OFFSET_LVI, index);

		/* clear any interrupt */
		I810_BM_PUT8(port->stsoff,
		    I810_BM_SR_LVBCI | I810_BM_SR_BCIS | I810_BM_SR_FIFOE);
	}

	/* update the kernel interrupt statistics */
	if (statep->ksp) {
		I810_KIOP(statep)->intrs[KSTAT_INTR_HARD]++;
	}

	mutex_exit(&statep->inst_lock);

	/* notify the framework */
	if (gsr & I810_GSR_INTR_PIN) {
		audio_engine_produce(statep->ports[I810_PCM_IN]->engine);
	}
	if (gsr & I810_GSR_INTR_POUT) {
		audio_engine_consume(statep->ports[I810_PCM_OUT]->engine);
	}

	return (DDI_INTR_CLAIMED);
}

/*
 * audio810_open()
 *
 * Description:
 *	Opens a DMA engine for use.
 *
 * Arguments:
 *	void		*arg		The DMA engine to set up
 *	int		flag		Open flags
 *	unsigned	*fragfrp	Receives number of frames per fragment
 *	unsigned	*nfragsp	Receives number of fragments
 *	caddr_t		*bufp		Receives kernel data buffer
 *
 * Returns:
 *	0	on success
 *	errno	on failure
 */
static int
audio810_open(void *arg, int flag, unsigned *fragfrp, unsigned *nfragsp,
    caddr_t *bufp)
{
	audio810_port_t	*port = arg;

	_NOTE(ARGUNUSED(flag));

	port->started = B_FALSE;
	port->count = 0;
	*fragfrp = port->fragfr;
	*nfragsp = port->nfrag;
	*bufp = port->samp_kaddr;

	mutex_enter(&port->statep->inst_lock);
	audio810_reset_port(port);
	mutex_exit(&port->statep->inst_lock);

	return (0);
}

/*
 * audio810_close()
 *
 * Description:
 *	Closes an audio DMA engine that was previously opened.  Since
 *	nobody is using it, we take this opportunity to possibly power
 *	down the entire device.
 *
 * Arguments:
 *	void	*arg		The DMA engine to shut down
 */
static void
audio810_close(void *arg)
{
	audio810_port_t		*port = arg;
	audio810_state_t	*statep = port->statep;

	mutex_enter(&statep->inst_lock);
	audio810_stop_port(port);
	port->started = B_FALSE;
	mutex_exit(&statep->inst_lock);
}

/*
 * audio810_stop()
 *
 * Description:
 *	This is called by the framework to stop a port that is
 *	transferring data.
 *
 * Arguments:
 *	void	*arg		The DMA engine to stop
 */
static void
audio810_stop(void *arg)
{
	audio810_port_t		*port = arg;
	audio810_state_t	*statep = port->statep;

	mutex_enter(&statep->inst_lock);
	if (port->started) {
		audio810_stop_port(port);
	}
	port->started = B_FALSE;
	mutex_exit(&statep->inst_lock);
}

/*
 * audio810_start()
 *
 * Description:
 *	This is called by the framework to start a port transferring data.
 *
 * Arguments:
 *	void	*arg		The DMA engine to start
 *
 * Returns:
 *	0 	on success (never fails, errno if it did)
 */
static int
audio810_start(void *arg)
{
	audio810_port_t		*port = arg;
	audio810_state_t	*statep = port->statep;

	mutex_enter(&statep->inst_lock);
	if (!port->started) {
		audio810_start_port(port);
		port->started = B_TRUE;
	}
	mutex_exit(&statep->inst_lock);
	return (0);
}

/*
 * audio810_format()
 *
 * Description:
 *	This is called by the framework to query the format of the device.
 *
 * Arguments:
 *	void	*arg		The DMA engine to query
 *
 * Returns:
 *	Format of the device (fixed at AUDIO_FORMAT_S16_LE)
 */
static int
audio810_format(void *arg)
{
	_NOTE(ARGUNUSED(arg));

	return (AUDIO_FORMAT_S16_LE);
}

/*
 * audio810_channels()
 *
 * Description:
 *	This is called by the framework to query the num channels of
 *	the device.
 *
 * Arguments:
 *	void	*arg		The DMA engine to query
 *
 * Returns:
 *	0 number of channels for device
 */
static int
audio810_channels(void *arg)
{
	audio810_port_t	*port = arg;

	return (port->nchan);
}

/*
 * audio810_rate()
 *
 * Description:
 *	This is called by the framework to query the rate of the device.
 *
 * Arguments:
 *	void	*arg		The DMA engine to query
 *
 * Returns:
 *	Rate of device (fixed at 48000 Hz)
 */
static int
audio810_rate(void *arg)
{
	_NOTE(ARGUNUSED(arg));

	return (48000);
}

/*
 * audio810_count()
 *
 * Description:
 *	This is called by the framework to get the engine's frame counter
 *
 * Arguments:
 *	void	*arg		The DMA engine to query
 *
 * Returns:
 *	frame count for current engine
 */
static uint64_t
audio810_count(void *arg)
{
	audio810_port_t		*port = arg;
	audio810_state_t	*statep = port->statep;
	uint64_t		val;
	uint16_t		picb;
	uint64_t		count;
	uint8_t			nchan;

	mutex_enter(&statep->inst_lock);
	audio810_update_port(port);
	count = port->count;
	picb = port->picb;
	nchan = port->nchan;
	mutex_exit(&statep->inst_lock);

	if (statep->quirk == QUIRK_SIS7012) {
		val = count + picb / (2 * nchan);
	} else {
		val = count + (picb / nchan);
	}

	return (val);
}

/*
 * audio810_sync()
 *
 * Description:
 *	This is called by the framework to synchronize DMA caches.
 *
 * Arguments:
 *	void	*arg		The DMA engine to sync
 */
static void
audio810_sync(void *arg, unsigned nframes)
{
	audio810_port_t *port = arg;
	_NOTE(ARGUNUSED(nframes));

	(void) ddi_dma_sync(port->samp_dmah, 0, 0, port->sync_dir);
}

/*
 * audio810_qlen()
 *
 * Description:
 *	This is called by the framework to determine on-device queue length.
 *
 * Arguments:
 *	void	*arg		The DMA engine to query
 *
 * Returns:
 *	hardware queue length not reported by count (0 for this device)
 */
static size_t
audio810_qlen(void *arg)
{
	_NOTE(ARGUNUSED(arg));
	return (0);
}

/* *********************** Local Routines *************************** */

/*
 * audio810_start_port()
 *
 * Description:
 *	This routine starts the DMA engine.
 *
 * Arguments:
 *	audio810_port_t	*port		Port of DMA engine to start.
 */
static void
audio810_start_port(audio810_port_t *port)
{
	audio810_state_t	*statep = port->statep;
	uint8_t			cr;

	ASSERT(mutex_owned(&statep->inst_lock));

	/* if suspended, then do nothing else */
	if (statep->suspended) {
		return;
	}

	cr = I810_BM_GET8(port->regoff + I810_OFFSET_CR);
	cr |= I810_BM_CR_IOCE;
	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, cr);
	cr |= I810_BM_CR_RUN;
	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, cr);
}

/*
 * audio810_stop_port()
 *
 * Description:
 *	This routine stops the DMA engine.
 *
 * Arguments:
 *	audio810_port_t	*port		Port of DMA engine to stop.
 */
static void
audio810_stop_port(audio810_port_t *port)
{
	audio810_state_t	*statep = port->statep;
	uint8_t			cr;

	ASSERT(mutex_owned(&statep->inst_lock));

	/* if suspended, then do nothing else */
	if (statep->suspended) {
		return;
	}

	cr = I810_BM_GET8(port->regoff + I810_OFFSET_CR);
	cr &= ~I810_BM_CR_RUN;
	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, cr);
}

/*
 * audio810_reset_port()
 *
 * Description:
 *	This routine resets the DMA engine pareparing it for work.
 *
 * Arguments:
 *	audio810_port_t	*port		Port of DMA engine to reset.
 */
static void
audio810_reset_port(audio810_port_t *port)
{
	audio810_state_t	*statep = port->statep;
	uint32_t		gcr;

	ASSERT(mutex_owned(&statep->inst_lock));

	port->civ = 0;
	port->picb = 0;

	if (statep->suspended)
		return;

	/*
	 * Make sure we put once in stereo, to ensure we always start from
	 * front left.
	 */
	if (port->num == I810_PCM_OUT) {

		if (statep->quirk == QUIRK_SIS7012) {
			/*
			 * SiS 7012 needs its own special multichannel config.
			 */
			gcr = I810_BM_GET32(I810_REG_GCR);
			gcr &= ~I810_GCR_SIS_CHANNELS_MASK;
			I810_BM_PUT32(I810_REG_GCR, gcr);
			delay(drv_usectohz(50000));	/* 50 msec */

			switch (statep->maxch) {
			case 2:
				gcr |= I810_GCR_SIS_2_CHANNELS;
				break;
			case 4:
				gcr |= I810_GCR_SIS_4_CHANNELS;
				break;
			case 6:
				gcr |= I810_GCR_SIS_6_CHANNELS;
				break;
			}
			I810_BM_PUT32(I810_REG_GCR, gcr);
			delay(drv_usectohz(50000));	/* 50 msec */

			/*
			 * SiS 7012 has special unmute bit.
			 */
			I810_BM_PUT8(I810_REG_SISCTL, I810_SISCTL_UNMUTE);

		} else {

			/*
			 * All other devices work the same.
			 */
			gcr = I810_BM_GET32(I810_REG_GCR);
			gcr &= ~I810_GCR_CHANNELS_MASK;

			I810_BM_PUT32(I810_REG_GCR, gcr);
			delay(drv_usectohz(50000));	/* 50 msec */

			switch (statep->maxch) {
			case 2:
				gcr |= I810_GCR_2_CHANNELS;
				break;
			case 4:
				gcr |= I810_GCR_4_CHANNELS;
				break;
			case 6:
				gcr |= I810_GCR_6_CHANNELS;
				break;
			}
			I810_BM_PUT32(I810_REG_GCR, gcr);
			delay(drv_usectohz(50000));	/* 50 msec */
		}
	}

	/*
	 * Perform full reset of the engine, but leave it turned off.
	 */
	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, 0);
	I810_BM_PUT8(port->regoff + I810_OFFSET_CR, I810_BM_CR_RST);

	/* program the offset of the BD list */
	I810_BM_PUT32(port->regoff + I810_OFFSET_BD_BASE, port->bdl_paddr);

	/* we set the last index to the full count -- all buffers are valid */
	I810_BM_PUT8(port->regoff + I810_OFFSET_LVI, I810_BD_NUMS - 1);
}

/*
 * audio810_update_port()
 *
 * Description:
 *	This routine updates the ports frame counter from hardware, and
 *	gracefully handles wraps.
 *
 * Arguments:
 *	audio810_port_t	*port		The port to update.
 */
static void
audio810_update_port(audio810_port_t *port)
{
	audio810_state_t	*statep = port->statep;
	uint8_t			regoff = port->regoff;
	uint8_t			civ;
	uint16_t		picb;
	unsigned		n;

	if (statep->suspended) {
		civ = 0;
		picb = 0;
	} else {
		/*
		 * We read the position counters, but we're careful to avoid
		 * the situation where the position counter resets at the end
		 * of a buffer.
		 */
		for (int i = 0; i < 2; i++) {
			civ = I810_BM_GET8(regoff + I810_OFFSET_CIV);
			picb = I810_BM_GET16(port->picboff);
			if (I810_BM_GET8(regoff + I810_OFFSET_CIV) == civ) {
				/*
				 * Chip did not start a new index,
				 * so the picb is valid.
				 */
				break;
			}
		}

		if (civ >= port->civ) {
			n = civ - port->civ;
		} else {
			n = civ + (I810_BD_NUMS - port->civ);
		}
		port->count += (n * port->fragfr);
	}
	port->civ = civ;
	port->picb = picb;
}

/*
 * audio810_attach()
 *
 * Description:
 *	Attach an instance of the audio810 driver. This routine does the
 * 	device dependent attach tasks, and registers with the audio framework.
 *
 * Arguments:
 *	dev_info_t	*dip	Pointer to the device's dev_info struct
 *	ddi_attach_cmd_t cmd	Attach command
 *
 * Returns:
 *	DDI_SUCCESS		The driver was initialized properly
 *	DDI_FAILURE		The driver couldn't be initialized properly
 */
static int
audio810_attach(dev_info_t *dip)
{
	uint16_t		cmdreg;
	audio810_state_t	*statep;
	audio_dev_t		*adev;
	ddi_acc_handle_t	pcih;
	uint32_t		devid;
	uint32_t		gsr;
	const char		*name;
	const char		*vers;
	uint8_t			nch;
	int			maxch;

	/* we don't support high level interrupts in the driver */
	if (ddi_intr_hilevel(dip, 0) != 0) {
		cmn_err(CE_WARN, "!%s: unsupported high level interrupt",
		    audio810_name);
		return (DDI_FAILURE);
	}

	/* allocate the soft state structure */
	statep = kmem_zalloc(sizeof (*statep), KM_SLEEP);
	ddi_set_driver_private(dip, statep);

	/* get iblock cookie information */
	if (ddi_get_iblock_cookie(dip, 0, &statep->iblock) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "!%s: cannot get iblock cookie",
		    audio810_name);
		kmem_free(statep, sizeof (*statep));
		return (DDI_FAILURE);
	}
	mutex_init(&statep->inst_lock, NULL, MUTEX_DRIVER, statep->iblock);
	mutex_init(&statep->ac_lock, NULL, MUTEX_DRIVER, statep->iblock);

	if ((adev = audio_dev_alloc(dip, 0)) == NULL) {
		cmn_err(CE_WARN, "!%s: unable to allocate audio dev",
		    audio810_name);
		goto error;
	}
	statep->adev = adev;
	statep->dip = dip;

	/* map in the registers, allocate DMA buffers, etc. */
	if (audio810_map_regs(dip, statep) != DDI_SUCCESS) {
		audio_dev_warn(adev, "couldn't map registers");
		goto error;
	}

	/* set PCI command register */
	if (pci_config_setup(dip, &pcih) != DDI_SUCCESS) {
		audio_dev_warn(adev, "pci conf mapping failed");
		goto error;
	}
	cmdreg = pci_config_get16(pcih, PCI_CONF_COMM);
	pci_config_put16(pcih, PCI_CONF_COMM,
	    cmdreg | PCI_COMM_IO | PCI_COMM_MAE | PCI_COMM_ME);
	devid = pci_config_get16(pcih, PCI_CONF_VENID);
	devid <<= 16;
	devid |= pci_config_get16(pcih, PCI_CONF_DEVID);
	pci_config_teardown(&pcih);

	/*
	 * Override "max-channels" property to prevent configuration
	 * of 4 or 6 (or possibly even 8!) channel audio.  The default
	 * is to support as many channels as the hardware can do.
	 *
	 * (Hmmm... perhaps this should be driven in the common
	 * framework.  The framework could even offer simplistic upmix
	 * and downmix for various standard configs.)
	 */
	maxch = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "max-channels", 8);
	if (maxch < 2) {
		maxch = 2;
	}

	gsr = I810_BM_GET32(I810_REG_GSR);
	if (gsr & I810_GSR_CAP6CH) {
		nch = 6;
	} else if (gsr & I810_GSR_CAP4CH) {
		nch = 4;
	} else {
		nch = 2;
	}

	statep->maxch = (uint8_t)min(nch, maxch);
	statep->maxch &= ~1;
	name = "Unknown AC'97";
	vers = "";

	statep->quirk = QUIRK_NONE;
	switch (devid) {
	case 0x80862415:
		name = "Intel AC'97";
		vers = "ICH";
		break;
	case 0x80862425:
		name = "Intel AC'97";
		vers = "ICH0";
		break;
	case 0x80867195:
		name = "Intel AC'97";
		vers = "440MX";
		break;
	case 0x80862445:
		name = "Intel AC'97";
		vers = "ICH2";
		break;
	case 0x80862485:
		name = "Intel AC'97";
		vers = "ICH3";
		break;
	case 0x808624C5:
		name = "Intel AC'97";
		vers = "ICH4";
		break;
	case 0x808624D5:
		name = "Intel AC'97";
		vers = "ICH5";
		break;
	case 0x8086266E:
		name = "Intel AC'97";
		vers = "ICH6";
		break;
	case 0x808627DE:
		name = "Intel AC'97";
		vers = "ICH7";
		break;
	case 0x808625A6:
		name = "Intel AC'97";
		vers = "6300ESB";
		break;
	case 0x80862698:
		name = "Intel AC'97";
		vers = "ESB2";
		break;
	case 0x10397012:
		name = "SiS AC'97";
		vers = "7012";
		statep->quirk = QUIRK_SIS7012;
		break;
	case 0x10de01b1:	/* nForce */
		name = "NVIDIA AC'97";
		vers = "MCP1";
		break;
	case 0x10de006a:	/* nForce 2 */
		name = "NVIDIA AC'97";
		vers = "MCP2";
		break;
	case 0x10de00da:	/* nForce 3 */
		name = "NVIDIA AC'97";
		vers = "MCP3";
		break;
	case 0x10de00ea:
		name = "NVIDIA AC'97";
		vers = "CK8S";
		break;
	case 0x10de0059:
		name = "NVIDIA AC'97";
		vers = "CK804";
		break;
	case 0x10de008a:
		name = "NVIDIA AC'97";
		vers = "CK8";
		break;
	case 0x10de003a:	/* nForce 4 */
		name = "NVIDIA AC'97";
		vers = "MCP4";
		break;
	case 0x10de026b:
		name = "NVIDIA AC'97";
		vers = "MCP51";
		break;
	case 0x1022746d:
		name = "AMD AC'97";
		vers = "8111";
		break;
	case 0x10227445:
		name = "AMD AC'97";
		vers = "AMD768";
		break;
	}
	/* set device information */
	audio_dev_set_description(adev, name);
	audio_dev_set_version(adev, vers);

	/* allocate port structures */
	if ((audio810_alloc_port(statep, I810_PCM_OUT, statep->maxch) !=
	    DDI_SUCCESS) ||
	    (audio810_alloc_port(statep, I810_PCM_IN, 2) != DDI_SUCCESS)) {
		goto error;
	}

	/* allocate ac97 handle */
	statep->ac97 = ac97_alloc(dip, audio810_read_ac97, audio810_write_ac97,
	    statep);
	if (statep->ac97 == NULL) {
		audio_dev_warn(adev, "failed to allocate ac97 handle");
		goto error;
	}

	/* initialize audio controller and AC97 codec */
	if (audio810_chip_init(statep) != DDI_SUCCESS) {
		audio_dev_warn(adev, "failed to init chip");
		goto error;
	}

	/* initialize the AC'97 part */
	if (ac97_init(statep->ac97, adev) != DDI_SUCCESS) {
		audio_dev_warn(adev, "ac'97 initialization failed");
		goto error;
	}

	/* set up kernel statistics */
	if ((statep->ksp = kstat_create(I810_NAME, ddi_get_instance(dip),
	    I810_NAME, "controller", KSTAT_TYPE_INTR, 1,
	    KSTAT_FLAG_PERSISTENT)) != NULL) {
		kstat_install(statep->ksp);
	}

	/* set up the interrupt handler */
	if (ddi_add_intr(dip, 0, &statep->iblock,
	    NULL, audio810_intr, (caddr_t)statep) != DDI_SUCCESS) {
		audio_dev_warn(adev, "bad interrupt specification");
		goto error;
	}
	statep->intr_added = B_TRUE;

	if (audio_dev_register(adev) != DDI_SUCCESS) {
		audio_dev_warn(adev, "unable to register with framework");
		goto error;
	}

	ddi_report_dev(dip);

	return (DDI_SUCCESS);

error:
	audio810_destroy(statep);

	return (DDI_FAILURE);
}


/*
 * audio810_resume()
 *
 * Description:
 *	Resume operation of the device after sleeping or hibernating.
 *	Note that this should never fail, even if hardware goes wonky,
 *	because the current PM framework will panic if it does.
 *
 * Arguments:
 *	dev_info_t	*dip	Pointer to the device's dev_info struct
 *
 * Returns:
 *	DDI_SUCCESS		The driver was resumed.
 */
static int
audio810_resume(dev_info_t *dip)
{
	audio810_state_t	*statep;
	audio_dev_t		*adev;

	/* this should always be valid */
	statep = ddi_get_driver_private(dip);
	adev = statep->adev;

	ASSERT(statep != NULL);
	ASSERT(dip == statep->dip);

	/* Restore the audio810 chip's state */
	if (audio810_chip_init(statep) != DDI_SUCCESS) {
		/*
		 * Note that PM gurus say we should return
		 * success here.  Failure of audio shouldn't
		 * be considered FATAL to the system.  The
		 * upshot is that audio will not progress.
		 */
		audio_dev_warn(adev, "DDI_RESUME failed to init chip");
		return (DDI_SUCCESS);
	}

	/* allow ac97 operations again */
	ac97_resume(statep->ac97);

	mutex_enter(&statep->inst_lock);

	ASSERT(statep->suspended);
	statep->suspended = B_FALSE;

	for (int i = 0; i < I810_NUM_PORTS; i++) {

		audio810_port_t *port = statep->ports[i];

		if (port != NULL) {
			/* reset framework DMA engine buffer */
			if (port->engine != NULL) {
				audio_engine_reset(port->engine);
			}

			/* reset and initialize hardware ports */
			audio810_reset_port(port);
			if (port->started) {
				audio810_start_port(port);
			} else {
				audio810_stop_port(port);
			}
		}
	}
	mutex_exit(&statep->inst_lock);

	return (DDI_SUCCESS);
}

/*
 * audio810_detach()
 *
 * Description:
 *	Detach an instance of the audio810 driver.
 *
 * Arguments:
 *	dev_info_t		*dip	Pointer to the device's dev_info struct
 *
 * Returns:
 *	DDI_SUCCESS	The driver was detached
 *	DDI_FAILURE	The driver couldn't be detached
 */
static int
audio810_detach(dev_info_t *dip)
{
	audio810_state_t	*statep;

	statep = ddi_get_driver_private(dip);
	ASSERT(statep != NULL);

	/* don't detach us if we are still in use */
	if (audio_dev_unregister(statep->adev) != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	audio810_destroy(statep);
	return (DDI_SUCCESS);
}

/*
 * audio810_suspend()
 *
 * Description:
 *	Suspend an instance of the audio810 driver, in preparation for
 *	sleep or hibernation.
 *
 * Arguments:
 *	dev_info_t		*dip	Pointer to the device's dev_info struct
 *
 * Returns:
 *	DDI_SUCCESS	The driver was suspended
 */
static int
audio810_suspend(dev_info_t *dip)
{
	audio810_state_t	*statep;

	statep = ddi_get_driver_private(dip);
	ASSERT(statep != NULL);

	ac97_suspend(statep->ac97);

	mutex_enter(&statep->inst_lock);

	ASSERT(statep->suspended == B_FALSE);

	statep->suspended = B_TRUE; /* stop new ops */

	/* stop DMA engines */
	audio810_stop_dma(statep);

	mutex_exit(&statep->inst_lock);

	return (DDI_SUCCESS);
}

/*
 * audio810_alloc_port()
 *
 * Description:
 *	This routine allocates the DMA handles and the memory for the
 *	DMA engines to use.  It also configures the BDL lists properly
 *	for use.
 *
 * Arguments:
 *	dev_info_t	*dip	Pointer to the device's devinfo
 *
 * Returns:
 *	DDI_SUCCESS		Registers successfully mapped
 *	DDI_FAILURE		Registers not successfully mapped
 */
static int
audio810_alloc_port(audio810_state_t *statep, int num, uint8_t nchan)
{
	ddi_dma_cookie_t	cookie;
	uint_t			count;
	int			dir;
	unsigned		caps;
	char			*prop;
	char			*nfprop;
	audio_dev_t		*adev;
	audio810_port_t		*port;
	uint32_t		paddr;
	int			rc;
	dev_info_t		*dip;
	i810_bd_entry_t		*bdentry;

	adev = statep->adev;
	dip = statep->dip;

	port = kmem_zalloc(sizeof (*port), KM_SLEEP);
	statep->ports[num] = port;
	port->statep = statep;
	port->started = B_FALSE;
	port->nchan = nchan;
	port->num = num;

	switch (num) {
	case I810_PCM_IN:
		prop = "record-interrupts";
		nfprop = "record-fragments";
		dir = DDI_DMA_READ;
		caps = ENGINE_INPUT_CAP;
		port->sync_dir = DDI_DMA_SYNC_FORKERNEL;
		port->regoff = I810_BASE_PCM_IN;
		break;
	case I810_PCM_OUT:
		prop = "play-interrupts";
		nfprop = "play-fragments";
		dir = DDI_DMA_WRITE;
		caps = ENGINE_OUTPUT_CAP;
		port->sync_dir = DDI_DMA_SYNC_FORDEV;
		port->regoff = I810_BASE_PCM_OUT;
		break;
	default:
		audio_dev_warn(adev, "bad port number (%d)!", num);
		return (DDI_FAILURE);
	}

	/*
	 * SiS 7012 swaps status and picb registers.
	 */
	if (statep->quirk == QUIRK_SIS7012) {
		port->stsoff = port->regoff + I810_OFFSET_PICB;
		port->picboff = port->regoff + I810_OFFSET_SR;
	} else {
		port->stsoff = port->regoff + I810_OFFSET_SR;
		port->picboff = port->regoff + I810_OFFSET_PICB;
	}

	port->intrs = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, prop, I810_INTS);

	/* make sure the values are good */
	if (port->intrs < I810_MIN_INTS) {
		audio_dev_warn(adev, "%s too low, %d, resetting to %d",
		    prop, port->intrs, I810_INTS);
		port->intrs = I810_INTS;
	} else if (port->intrs > I810_MAX_INTS) {
		audio_dev_warn(adev, "%s too high, %d, resetting to %d",
		    prop, port->intrs, I810_INTS);
		port->intrs = I810_INTS;
	}

	port->nfrag = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, nfprop, I810_NFRAGS);

	/*
	 * Note that fragments must divide evenly into I810_BD_NUMS (32).
	 */
	if (port->nfrag <= 4) {
		port->nfrag = 4;
	} else if (port->nfrag <= 8) {
		port->nfrag = 8;
	} else if (port->nfrag <= 16) {
		port->nfrag = 16;
	} else {
		port->nfrag = I810_BD_NUMS;
	}

	/*
	 * Figure out how much space we need.  Sample rate is 48kHz, and
	 * we need to store 32 chunks.  (Note that this means that low
	 * interrupt frequencies will require more RAM.  We could probably
	 * do some cleverness to use a shorter BD list.)
	 */
	port->fragfr = 48000 / port->intrs;
	port->fragfr = I810_ROUNDUP(port->fragfr, I810_MOD_SIZE);
	port->fragsz = port->fragfr * port->nchan * 2;
	port->samp_size = port->fragsz * port->nfrag;

	/* allocate dma handle */
	rc = ddi_dma_alloc_handle(dip, &sample_buf_dma_attr, DDI_DMA_SLEEP,
	    NULL, &port->samp_dmah);
	if (rc != DDI_SUCCESS) {
		audio_dev_warn(adev, "ddi_dma_alloc_handle failed: %d", rc);
		return (DDI_FAILURE);
	}
	/* allocate DMA buffer */
	rc = ddi_dma_mem_alloc(port->samp_dmah, port->samp_size, &buf_attr,
	    DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &port->samp_kaddr,
	    &port->samp_size, &port->samp_acch);
	if (rc == DDI_FAILURE) {
		audio_dev_warn(adev, "dma_mem_alloc (%d) failed: %d",
		    port->samp_size, rc);
		return (DDI_FAILURE);
	}

	/* bind DMA buffer */
	rc = ddi_dma_addr_bind_handle(port->samp_dmah, NULL,
	    port->samp_kaddr, port->samp_size, dir|DDI_DMA_CONSISTENT,
	    DDI_DMA_SLEEP, NULL, &cookie, &count);
	if ((rc != DDI_DMA_MAPPED) || (count != 1)) {
		audio_dev_warn(adev,
		    "ddi_dma_addr_bind_handle failed: %d", rc);
		return (DDI_FAILURE);
	}
	port->samp_paddr = cookie.dmac_address;

	/*
	 * now, from here we allocate DMA memory for buffer descriptor list.
	 * we allocate adjacent DMA memory for all DMA engines.
	 */
	rc = ddi_dma_alloc_handle(dip, &bdlist_dma_attr, DDI_DMA_SLEEP,
	    NULL, &port->bdl_dmah);
	if (rc != DDI_SUCCESS) {
		audio_dev_warn(adev, "ddi_dma_alloc_handle(bdlist) failed");
		return (DDI_FAILURE);
	}

	/*
	 * we allocate all buffer descriptors lists in continuous dma memory.
	 */
	port->bdl_size = sizeof (i810_bd_entry_t) * I810_BD_NUMS;
	rc = ddi_dma_mem_alloc(port->bdl_dmah, port->bdl_size,
	    &dev_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
	    &port->bdl_kaddr, &port->bdl_size, &port->bdl_acch);
	if (rc != DDI_SUCCESS) {
		audio_dev_warn(adev, "ddi_dma_mem_alloc(bdlist) failed");
		return (DDI_FAILURE);
	}

	rc = ddi_dma_addr_bind_handle(port->bdl_dmah, NULL, port->bdl_kaddr,
	    port->bdl_size, DDI_DMA_WRITE|DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
	    NULL, &cookie, &count);
	if ((rc != DDI_DMA_MAPPED) || (count != 1)) {
		audio_dev_warn(adev, "addr_bind_handle failed");
		return (DDI_FAILURE);
	}
	port->bdl_paddr = cookie.dmac_address;

	/*
	 * Wire up the BD list.
	 */
	paddr = port->samp_paddr;
	bdentry = (void *)port->bdl_kaddr;
	for (int i = 0; i < I810_BD_NUMS; i++) {

		/* set base address of buffer */
		ddi_put32(port->bdl_acch, &bdentry->buf_base, paddr);
		/*
		 * SiS 7012 counts samples in bytes, all other count
		 * in words.
		 */
		ddi_put16(port->bdl_acch, &bdentry->buf_len,
		    statep->quirk == QUIRK_SIS7012 ? port->fragsz :
		    port->fragsz / 2);
		ddi_put16(port->bdl_acch, &bdentry->buf_cmd,
		    BUF_CMD_IOC | BUF_CMD_BUP);
		paddr += port->fragsz;
		if ((i % port->nfrag) == (port->nfrag - 1)) {
			/* handle wrap */
			paddr = port->samp_paddr;
		}
		bdentry++;
	}
	(void) ddi_dma_sync(port->bdl_dmah, 0, 0, DDI_DMA_SYNC_FORDEV);

	port->engine = audio_engine_alloc(&audio810_engine_ops, caps);
	if (port->engine == NULL) {
		audio_dev_warn(adev, "audio_engine_alloc failed");
		return (DDI_FAILURE);
	}

	audio_engine_set_private(port->engine, port);
	audio_dev_add_engine(adev, port->engine);

	return (DDI_SUCCESS);
}

/*
 * audio810_free_port()
 *
 * Description:
 *	This routine unbinds the DMA cookies, frees the DMA buffers,
 *	deallocates the DMA handles.
 *
 * Arguments:
 *	audio810_port_t	*port	The port structure for a DMA engine.
 */
static void
audio810_free_port(audio810_port_t *port)
{
	if (port == NULL)
		return;

	if (port->engine) {
		audio_dev_remove_engine(port->statep->adev, port->engine);
		audio_engine_free(port->engine);
	}
	if (port->bdl_paddr) {
		(void) ddi_dma_unbind_handle(port->bdl_dmah);
	}
	if (port->bdl_acch) {
		ddi_dma_mem_free(&port->bdl_acch);
	}
	if (port->bdl_dmah) {
		ddi_dma_free_handle(&port->bdl_dmah);
	}
	if (port->samp_paddr) {
		(void) ddi_dma_unbind_handle(port->samp_dmah);
	}
	if (port->samp_acch) {
		ddi_dma_mem_free(&port->samp_acch);
	}
	if (port->samp_dmah) {
		ddi_dma_free_handle(&port->samp_dmah);
	}
	kmem_free(port, sizeof (*port));
}

/*
 * audio810_map_regs()
 *
 * Description:
 *	The registers are mapped in.
 *
 * Arguments:
 *	dev_info_t	*dip	Pointer to the device's devinfo
 *
 * Returns:
 *	DDI_SUCCESS		Registers successfully mapped
 *	DDI_FAILURE		Registers not successfully mapped
 */
static int
audio810_map_regs(dev_info_t *dip, audio810_state_t *statep)
{
	uint_t			nregs = 0;
	int			*regs_list;
	int			i;
	int			pciBar1 = 0;
	int			pciBar2 = 0;
	int			pciBar3 = 0;
	int			pciBar4 = 0;

	/* check the "reg" property to get the length of memory-mapped I/O */
	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
	    "reg", (int **)&regs_list, &nregs) != DDI_PROP_SUCCESS) {
		audio_dev_warn(statep->adev, "inquire regs property failed");
		goto error;
	}
	/*
	 * Some hardwares, such as Intel ICH0/ICH and AMD 8111, use PCI 0x10
	 * and 0x14 BAR separately for native audio mixer BAR and native bus
	 * mastering BAR. More advanced hardwares, such as Intel ICH4 and ICH5,
	 * support PCI memory BAR, via PCI 0x18 and 0x1C BAR, that allows for
	 * higher performance access to the controller register. All features
	 * can be accessed via this BAR making the I/O BAR (PCI 0x10 and 0x14
	 * BAR) capabilities obsolete. However, these controller maintain the
	 * I/O BAR capability to allow for the reuse of legacy code maintaining
	 * backward compatibility. The I/O BAR is disabled unless system BIOS
	 * enables the simultaneous backward compatible capability on the 0x41
	 * register.
	 *
	 * When I/O BAR is enabled, the value of "reg" property should be like
	 * this,
	 *	phys_hi   phys_mid  phys_lo   size_hi   size_lo
	 * --------------------------------------------------------
	 *	0000fd00  00000000  00000000  00000000  00000000
	 *	0100fd10  00000000  00000000  00000000  00000100
	 *	0100fd14  00000000  00000000  00000000  00000040
	 *	0200fd18  00000000  00000000  00000000  00000200
	 *	0200fd1c  00000000  00000000  00000000  00000100
	 *
	 * When I/O BAR is disabled, the "reg" property of the device node does
	 * not consist of the description for the I/O BAR. The following example
	 * illustrates the vaule of "reg" property,
	 *
	 *	phys_hi   phys_mid  phys_lo   size_hi   size_lo
	 * --------------------------------------------------------
	 *	0000fd00  00000000  00000000  00000000  00000000
	 *	0200fd18  00000000  00000000  00000000  00000200
	 *	0200fd1c  00000000  00000000  00000000  00000100
	 *
	 * If the hardware has memory-mapped I/O access, first try to use
	 * this facility, otherwise we will try I/O access.
	 */
	for (i = 1; i < nregs/I810_INTS_PER_REG_PROP; i++) {
		switch (regs_list[I810_INTS_PER_REG_PROP * i] & 0x000000ff) {
			case 0x10:
				pciBar1 = i;
				break;
			case 0x14:
				pciBar2 = i;
				break;
			case 0x18:
				pciBar3 = i;
				break;
			case 0x1c:
				pciBar4 = i;
				break;
			default:	/* we don't care others */
				break;
		}
	}

	if ((pciBar3 != 0) && (pciBar4 != 0)) {
		/* map audio mixer registers */
		if ((ddi_regs_map_setup(dip, pciBar3, &statep->am_regs_base, 0,
		    0, &dev_attr, &statep->am_regs_handle)) != DDI_SUCCESS) {
			audio_dev_warn(statep->adev,
			    "memory am mapping failed");
			goto error;
		}

		/* map bus master register */
		if ((ddi_regs_map_setup(dip, pciBar4, &statep->bm_regs_base, 0,
		    0, &dev_attr, &statep->bm_regs_handle)) != DDI_SUCCESS) {
			audio_dev_warn(statep->adev,
			    "memory bm mapping failed");
			goto error;
		}

	} else if ((pciBar1 != 0) && (pciBar2 != 0)) {
		/* map audio mixer registers */
		if ((ddi_regs_map_setup(dip, pciBar1, &statep->am_regs_base, 0,
		    0, &dev_attr, &statep->am_regs_handle)) != DDI_SUCCESS) {
			audio_dev_warn(statep->adev, "I/O am mapping failed");
			goto error;
		}

		/* map bus master register */
		if ((ddi_regs_map_setup(dip, pciBar2, &statep->bm_regs_base, 0,
		    0, &dev_attr, &statep->bm_regs_handle)) != DDI_SUCCESS) {
			audio_dev_warn(statep->adev, "I/O bm mapping failed");
			goto error;
		}
	} else {
		audio_dev_warn(statep->adev, "map_regs() pci BAR error");
		goto error;
	}

	ddi_prop_free(regs_list);

	return (DDI_SUCCESS);

error:
	if (nregs > 0) {
		ddi_prop_free(regs_list);
	}
	audio810_unmap_regs(statep);

	return (DDI_FAILURE);
}

/*
 * audio810_unmap_regs()
 *
 * Description:
 *	This routine unmaps control registers.
 *
 * Arguments:
 *	audio810_state_t	*state	The device's state structure
 */
static void
audio810_unmap_regs(audio810_state_t *statep)
{
	if (statep->bm_regs_handle) {
		ddi_regs_map_free(&statep->bm_regs_handle);
	}

	if (statep->am_regs_handle) {
		ddi_regs_map_free(&statep->am_regs_handle);
	}
}

/*
 * audio810_chip_init()
 *
 * Description:
 *	This routine initializes the AMD 8111 audio controller.
 *	codec.
 *
 * Arguments:
 *	audio810_state_t	*state		The device's state structure
 *
 * Returns:
 *	DDI_SUCCESS	The hardware was initialized properly
 *	DDI_FAILURE	The hardware couldn't be initialized properly
 */
static int
audio810_chip_init(audio810_state_t *statep)
{
	uint32_t	gcr;
	uint32_t	gsr;
	uint32_t	codec_ready;
	int 		loop;
	clock_t		ticks;

	gcr = I810_BM_GET32(I810_REG_GCR);
	ticks = drv_usectohz(100);

	/*
	 * Clear the channels bits for now.  We'll set them later in
	 * reset port.
	 */
	if (statep->quirk == QUIRK_SIS7012) {
		gcr &= ~(I810_GCR_ACLINK_OFF | I810_GCR_SIS_CHANNELS_MASK);
	} else {
		gcr &= ~(I810_GCR_ACLINK_OFF | I810_GCR_CHANNELS_MASK);
	}

	/*
	 * Datasheet(ICH5, document number of Intel: 252751-001):
	 * 3.6.5.5(page 37)
	 * 	if reset bit(bit1) is "0", driver must set it
	 * 	to "1" to de-assert the AC_RESET# signal in AC
	 * 	link, thus completing a cold reset. But if the
	 * 	bit is "1", then a warm reset is required.
	 */
	gcr |= (gcr & I810_GCR_COLD_RST) == 0 ?
	    I810_GCR_COLD_RST:I810_GCR_WARM_RST;
	I810_BM_PUT32(I810_REG_GCR, gcr);

	/* according AC'97 spec, wait for codec reset */
	for (loop = 6000; --loop >= 0; ) {
		delay(ticks);
		gcr = I810_BM_GET32(I810_REG_GCR);
		if ((gcr & I810_GCR_WARM_RST) == 0) {
			break;
		}
	}

	/* codec reset failed */
	if (loop < 0) {
		audio_dev_warn(statep->adev, "Failed to reset codec");
		return (DDI_FAILURE);
	}

	/*
	 * Wait for codec ready. The hardware can provide the state of
	 * codec ready bit on SDATA_IN[0], SDATA_IN[1] or SDATA_IN[2]
	 */
	codec_ready =
	    I810_GSR_PRI_READY | I810_GSR_SEC_READY | I810_GSR_TRI_READY;
	for (loop = 7000; --loop >= 0; ) {
		delay(ticks);
		gsr = I810_BM_GET32(I810_REG_GSR);
		if ((gsr & codec_ready) != 0) {
			break;
		}
	}
	if (loop < 0) {
		audio_dev_warn(statep->adev, "No codec ready signal received");
		return (DDI_FAILURE);
	}

	/*
	 * put the audio controller into quiet state, everything off
	 */
	audio810_stop_dma(statep);

	return (DDI_SUCCESS);
}

/*
 * audio810_stop_dma()
 *
 * Description:
 *	This routine is used to put each DMA engine into the quiet state.
 *
 * Arguments:
 *	audio810_state_t	*state		The device's state structure
 */
static void
audio810_stop_dma(audio810_state_t *statep)
{
	if (statep->bm_regs_handle == NULL) {
		return;
	}
	/* pause bus master (needed for the following reset register) */
	I810_BM_PUT8(I810_BASE_PCM_IN + I810_OFFSET_CR, 0x0);
	I810_BM_PUT8(I810_BASE_PCM_OUT + I810_OFFSET_CR, 0x0);
	I810_BM_PUT8(I810_BASE_MIC + I810_OFFSET_CR, 0x0);

	/* and then reset the bus master registers for a three DMA engines */
	I810_BM_PUT8(I810_BASE_PCM_IN + I810_OFFSET_CR, I810_BM_CR_RST);
	I810_BM_PUT8(I810_BASE_PCM_OUT + I810_OFFSET_CR, I810_BM_CR_RST);
	I810_BM_PUT8(I810_BASE_MIC + I810_OFFSET_CR, I810_BM_CR_RST);
}


/*
 * audio810_codec_sync()
 *
 * Description:
 *	Serialize access to the AC97 audio mixer registers.
 *
 * Arguments:
 *	audio810_state_t	*state		The device's state structure
 *
 * Returns:
 *	DDI_SUCCESS		Ready for an I/O access to the codec
 *	DDI_FAILURE		An I/O access is currently in progress, can't
 *				perform another I/O access.
 */
static int
audio810_codec_sync(audio810_state_t *statep)
{
	int 		i;
	uint16_t	casr;

	for (i = 0; i < 300; i++) {
		casr = I810_BM_GET8(I810_REG_CASR);
		if ((casr & 1) == 0) {
			return (DDI_SUCCESS);
		}
		drv_usecwait(10);
	}

	return (DDI_FAILURE);
}

/*
 * audio810_write_ac97()
 *
 * Description:
 *	Set the specific AC97 Codec register.
 *
 * Arguments:
 *	void		*arg		The device's state structure
 *	uint8_t		reg		AC97 register number
 *	uint16_t	data		The data want to be set
 */
static void
audio810_write_ac97(void *arg, uint8_t reg, uint16_t data)
{
	audio810_state_t	*statep = arg;

	mutex_enter(&statep->ac_lock);
	if (audio810_codec_sync(statep) == DDI_SUCCESS) {
		I810_AM_PUT16(reg, data);
	}
	mutex_exit(&statep->ac_lock);

	(void) audio810_read_ac97(statep, reg);
}

/*
 * audio810_read_ac97()
 *
 * Description:
 *	Get the specific AC97 Codec register.
 *
 * Arguments:
 *	void		*arg		The device's state structure
 *	uint8_t		reg		AC97 register number
 *
 * Returns:
 *	The register value.
 */
static uint16_t
audio810_read_ac97(void *arg, uint8_t reg)
{
	audio810_state_t	*statep = arg;
	uint16_t		val = 0xffff;

	mutex_enter(&statep->ac_lock);
	if (audio810_codec_sync(statep) == DDI_SUCCESS) {
		val = I810_AM_GET16(reg);
	}
	mutex_exit(&statep->ac_lock);
	return (val);
}

/*
 * audio810_destroy()
 *
 * Description:
 *	This routine releases all resources held by the device instance,
 *	as part of either detach or a failure in attach.
 *
 * Arguments:
 *	audio810_state_t	*state	The device soft state.
 */
void
audio810_destroy(audio810_state_t *statep)
{
	/* stop DMA engines */
	audio810_stop_dma(statep);

	if (statep->intr_added) {
		ddi_remove_intr(statep->dip, 0, NULL);
	}

	if (statep->ksp) {
		kstat_delete(statep->ksp);
	}

	for (int i = 0; i < I810_NUM_PORTS; i++) {
		audio810_free_port(statep->ports[i]);
	}

	audio810_unmap_regs(statep);

	if (statep->ac97)
		ac97_free(statep->ac97);

	if (statep->adev)
		audio_dev_free(statep->adev);

	mutex_destroy(&statep->inst_lock);
	mutex_destroy(&statep->ac_lock);
	kmem_free(statep, sizeof (*statep));
}