xref: /freebsd/sys/dev/sound/pci/hda/hdacc.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
1 /*-
2  * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3  * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4  * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Intel High Definition Audio (CODEC) driver for FreeBSD.
31  */
32 
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
34 #include "opt_snd.h"
35 #endif
36 
37 #include <dev/sound/pcm/sound.h>
38 
39 #include <sys/ctype.h>
40 
41 #include <dev/sound/pci/hda/hda_reg.h>
42 #include <dev/sound/pci/hda/hdac.h>
43 
44 SND_DECLARE_FILE("$FreeBSD$");
45 
46 struct hdacc_fg {
47 	device_t	dev;
48 	nid_t		nid;
49 	uint8_t		type;
50 	uint32_t	subsystem_id;
51 };
52 
53 struct hdacc_softc {
54 	device_t	dev;
55 	struct mtx	*lock;
56 	nid_t		cad;
57 	device_t	streams[2][16];
58 	device_t	tags[64];
59 	int		fgcnt;
60 	struct hdacc_fg	*fgs;
61 };
62 
63 #define hdacc_lock(codec)	snd_mtxlock((codec)->lock)
64 #define hdacc_unlock(codec)	snd_mtxunlock((codec)->lock)
65 #define hdacc_lockassert(codec)	snd_mtxassert((codec)->lock)
66 #define hdacc_lockowned(codec)	mtx_owned((codec)->lock)
67 
68 MALLOC_DEFINE(M_HDACC, "hdacc", "HDA CODEC");
69 
70 /* CODECs */
71 static const struct {
72 	uint32_t id;
73 	uint16_t revid;
74 	char *name;
75 } hdacc_codecs[] = {
76 	{ HDA_CODEC_CS4206, 0,		"Cirrus Logic CS4206" },
77 	{ HDA_CODEC_CS4207, 0,		"Cirrus Logic CS4207" },
78 	{ HDA_CODEC_CS4210, 0,		"Cirrus Logic CS4210" },
79 	{ HDA_CODEC_ALC221, 0,		"Realtek ALC221" },
80 	{ HDA_CODEC_ALC260, 0,		"Realtek ALC260" },
81 	{ HDA_CODEC_ALC262, 0,		"Realtek ALC262" },
82 	{ HDA_CODEC_ALC267, 0,		"Realtek ALC267" },
83 	{ HDA_CODEC_ALC268, 0,		"Realtek ALC268" },
84 	{ HDA_CODEC_ALC269, 0,		"Realtek ALC269" },
85 	{ HDA_CODEC_ALC270, 0,		"Realtek ALC270" },
86 	{ HDA_CODEC_ALC272, 0,		"Realtek ALC272" },
87 	{ HDA_CODEC_ALC273, 0,		"Realtek ALC273" },
88 	{ HDA_CODEC_ALC275, 0,		"Realtek ALC275" },
89 	{ HDA_CODEC_ALC276, 0,		"Realtek ALC276" },
90 	{ HDA_CODEC_ALC660, 0,		"Realtek ALC660-VD" },
91 	{ HDA_CODEC_ALC662, 0x0002,	"Realtek ALC662 rev2" },
92 	{ HDA_CODEC_ALC662, 0,		"Realtek ALC662" },
93 	{ HDA_CODEC_ALC663, 0,		"Realtek ALC663" },
94 	{ HDA_CODEC_ALC665, 0,		"Realtek ALC665" },
95 	{ HDA_CODEC_ALC670, 0,		"Realtek ALC670" },
96 	{ HDA_CODEC_ALC680, 0,		"Realtek ALC680" },
97 	{ HDA_CODEC_ALC861, 0x0340,	"Realtek ALC660" },
98 	{ HDA_CODEC_ALC861, 0,		"Realtek ALC861" },
99 	{ HDA_CODEC_ALC861VD, 0,	"Realtek ALC861-VD" },
100 	{ HDA_CODEC_ALC880, 0,		"Realtek ALC880" },
101 	{ HDA_CODEC_ALC882, 0,		"Realtek ALC882" },
102 	{ HDA_CODEC_ALC883, 0,		"Realtek ALC883" },
103 	{ HDA_CODEC_ALC885, 0x0101,	"Realtek ALC889A" },
104 	{ HDA_CODEC_ALC885, 0x0103,	"Realtek ALC889A" },
105 	{ HDA_CODEC_ALC885, 0,		"Realtek ALC885" },
106 	{ HDA_CODEC_ALC887, 0,		"Realtek ALC887" },
107 	{ HDA_CODEC_ALC888, 0x0101,	"Realtek ALC1200" },
108 	{ HDA_CODEC_ALC888, 0,		"Realtek ALC888" },
109 	{ HDA_CODEC_ALC889, 0,		"Realtek ALC889" },
110 	{ HDA_CODEC_ALC892, 0,		"Realtek ALC892" },
111 	{ HDA_CODEC_ALC899, 0,		"Realtek ALC899" },
112 	{ HDA_CODEC_AD1882, 0,		"Analog Devices AD1882" },
113 	{ HDA_CODEC_AD1882A, 0,		"Analog Devices AD1882A" },
114 	{ HDA_CODEC_AD1883, 0,		"Analog Devices AD1883" },
115 	{ HDA_CODEC_AD1884, 0,		"Analog Devices AD1884" },
116 	{ HDA_CODEC_AD1884A, 0,		"Analog Devices AD1884A" },
117 	{ HDA_CODEC_AD1981HD, 0,	"Analog Devices AD1981HD" },
118 	{ HDA_CODEC_AD1983, 0,		"Analog Devices AD1983" },
119 	{ HDA_CODEC_AD1984, 0,		"Analog Devices AD1984" },
120 	{ HDA_CODEC_AD1984A, 0,		"Analog Devices AD1984A" },
121 	{ HDA_CODEC_AD1984B, 0,		"Analog Devices AD1984B" },
122 	{ HDA_CODEC_AD1986A, 0,		"Analog Devices AD1986A" },
123 	{ HDA_CODEC_AD1987, 0,		"Analog Devices AD1987" },
124 	{ HDA_CODEC_AD1988, 0,		"Analog Devices AD1988A" },
125 	{ HDA_CODEC_AD1988B, 0,		"Analog Devices AD1988B" },
126 	{ HDA_CODEC_AD1989A, 0,		"Analog Devices AD1989A" },
127 	{ HDA_CODEC_AD1989B, 0,		"Analog Devices AD1989B" },
128 	{ HDA_CODEC_CA0110, 0,		"Creative CA0110-IBG" },
129 	{ HDA_CODEC_CA0110_2, 0,	"Creative CA0110-IBG" },
130 	{ HDA_CODEC_CA0132, 0,		"Creative CA0132" },
131 	{ HDA_CODEC_SB0880, 0,		"Creative SB0880 X-Fi" },
132 	{ HDA_CODEC_CMI9880, 0,		"CMedia CMI9880" },
133 	{ HDA_CODEC_CMI98802, 0,	"CMedia CMI9880" },
134 	{ HDA_CODEC_CXD9872RDK, 0,	"Sigmatel CXD9872RD/K" },
135 	{ HDA_CODEC_CXD9872AKD, 0,	"Sigmatel CXD9872AKD" },
136 	{ HDA_CODEC_STAC9200D, 0,	"Sigmatel STAC9200D" },
137 	{ HDA_CODEC_STAC9204X, 0,	"Sigmatel STAC9204X" },
138 	{ HDA_CODEC_STAC9204D, 0,	"Sigmatel STAC9204D" },
139 	{ HDA_CODEC_STAC9205X, 0,	"Sigmatel STAC9205X" },
140 	{ HDA_CODEC_STAC9205D, 0,	"Sigmatel STAC9205D" },
141 	{ HDA_CODEC_STAC9220, 0,	"Sigmatel STAC9220" },
142 	{ HDA_CODEC_STAC9220_A1, 0,	"Sigmatel STAC9220_A1" },
143 	{ HDA_CODEC_STAC9220_A2, 0,	"Sigmatel STAC9220_A2" },
144 	{ HDA_CODEC_STAC9221, 0,	"Sigmatel STAC9221" },
145 	{ HDA_CODEC_STAC9221_A2, 0,	"Sigmatel STAC9221_A2" },
146 	{ HDA_CODEC_STAC9221D, 0,	"Sigmatel STAC9221D" },
147 	{ HDA_CODEC_STAC922XD, 0,	"Sigmatel STAC9220D/9223D" },
148 	{ HDA_CODEC_STAC9227X, 0,	"Sigmatel STAC9227X" },
149 	{ HDA_CODEC_STAC9227D, 0,	"Sigmatel STAC9227D" },
150 	{ HDA_CODEC_STAC9228X, 0,	"Sigmatel STAC9228X" },
151 	{ HDA_CODEC_STAC9228D, 0,	"Sigmatel STAC9228D" },
152 	{ HDA_CODEC_STAC9229X, 0,	"Sigmatel STAC9229X" },
153 	{ HDA_CODEC_STAC9229D, 0,	"Sigmatel STAC9229D" },
154 	{ HDA_CODEC_STAC9230X, 0,	"Sigmatel STAC9230X" },
155 	{ HDA_CODEC_STAC9230D, 0,	"Sigmatel STAC9230D" },
156 	{ HDA_CODEC_STAC9250, 0, 	"Sigmatel STAC9250" },
157 	{ HDA_CODEC_STAC9251, 0, 	"Sigmatel STAC9251" },
158 	{ HDA_CODEC_STAC9255, 0, 	"Sigmatel STAC9255" },
159 	{ HDA_CODEC_STAC9255D, 0, 	"Sigmatel STAC9255D" },
160 	{ HDA_CODEC_STAC9254, 0, 	"Sigmatel STAC9254" },
161 	{ HDA_CODEC_STAC9254D, 0, 	"Sigmatel STAC9254D" },
162 	{ HDA_CODEC_STAC9271X, 0,	"Sigmatel STAC9271X" },
163 	{ HDA_CODEC_STAC9271D, 0,	"Sigmatel STAC9271D" },
164 	{ HDA_CODEC_STAC9272X, 0,	"Sigmatel STAC9272X" },
165 	{ HDA_CODEC_STAC9272D, 0,	"Sigmatel STAC9272D" },
166 	{ HDA_CODEC_STAC9273X, 0,	"Sigmatel STAC9273X" },
167 	{ HDA_CODEC_STAC9273D, 0,	"Sigmatel STAC9273D" },
168 	{ HDA_CODEC_STAC9274, 0, 	"Sigmatel STAC9274" },
169 	{ HDA_CODEC_STAC9274D, 0,	"Sigmatel STAC9274D" },
170 	{ HDA_CODEC_STAC9274X5NH, 0,	"Sigmatel STAC9274X5NH" },
171 	{ HDA_CODEC_STAC9274D5NH, 0,	"Sigmatel STAC9274D5NH" },
172 	{ HDA_CODEC_STAC9872AK, 0,	"Sigmatel STAC9872AK" },
173 	{ HDA_CODEC_IDT92HD005, 0,	"IDT 92HD005" },
174 	{ HDA_CODEC_IDT92HD005D, 0,	"IDT 92HD005D" },
175 	{ HDA_CODEC_IDT92HD206X, 0,	"IDT 92HD206X" },
176 	{ HDA_CODEC_IDT92HD206D, 0,	"IDT 92HD206D" },
177 	{ HDA_CODEC_IDT92HD66B1X5, 0,	"IDT 92HD66B1X5" },
178 	{ HDA_CODEC_IDT92HD66B2X5, 0,	"IDT 92HD66B2X5" },
179 	{ HDA_CODEC_IDT92HD66B3X5, 0,	"IDT 92HD66B3X5" },
180 	{ HDA_CODEC_IDT92HD66C1X5, 0,	"IDT 92HD66C1X5" },
181 	{ HDA_CODEC_IDT92HD66C2X5, 0,	"IDT 92HD66C2X5" },
182 	{ HDA_CODEC_IDT92HD66C3X5, 0,	"IDT 92HD66C3X5" },
183 	{ HDA_CODEC_IDT92HD66B1X3, 0,	"IDT 92HD66B1X3" },
184 	{ HDA_CODEC_IDT92HD66B2X3, 0,	"IDT 92HD66B2X3" },
185 	{ HDA_CODEC_IDT92HD66B3X3, 0,	"IDT 92HD66B3X3" },
186 	{ HDA_CODEC_IDT92HD66C1X3, 0,	"IDT 92HD66C1X3" },
187 	{ HDA_CODEC_IDT92HD66C2X3, 0,	"IDT 92HD66C2X3" },
188 	{ HDA_CODEC_IDT92HD66C3_65, 0,	"IDT 92HD66C3_65" },
189 	{ HDA_CODEC_IDT92HD700X, 0,	"IDT 92HD700X" },
190 	{ HDA_CODEC_IDT92HD700D, 0,	"IDT 92HD700D" },
191 	{ HDA_CODEC_IDT92HD71B5, 0,	"IDT 92HD71B5" },
192 	{ HDA_CODEC_IDT92HD71B5_2, 0,	"IDT 92HD71B5" },
193 	{ HDA_CODEC_IDT92HD71B6, 0,	"IDT 92HD71B6" },
194 	{ HDA_CODEC_IDT92HD71B6_2, 0,	"IDT 92HD71B6" },
195 	{ HDA_CODEC_IDT92HD71B7, 0,	"IDT 92HD71B7" },
196 	{ HDA_CODEC_IDT92HD71B7_2, 0,	"IDT 92HD71B7" },
197 	{ HDA_CODEC_IDT92HD71B8, 0,	"IDT 92HD71B8" },
198 	{ HDA_CODEC_IDT92HD71B8_2, 0,	"IDT 92HD71B8" },
199 	{ HDA_CODEC_IDT92HD73C1, 0,	"IDT 92HD73C1" },
200 	{ HDA_CODEC_IDT92HD73D1, 0,	"IDT 92HD73D1" },
201 	{ HDA_CODEC_IDT92HD73E1, 0,	"IDT 92HD73E1" },
202 	{ HDA_CODEC_IDT92HD75B3, 0,	"IDT 92HD75B3" },
203 	{ HDA_CODEC_IDT92HD75BX, 0,	"IDT 92HD75BX" },
204 	{ HDA_CODEC_IDT92HD81B1C, 0,	"IDT 92HD81B1C" },
205 	{ HDA_CODEC_IDT92HD81B1X, 0,	"IDT 92HD81B1X" },
206 	{ HDA_CODEC_IDT92HD83C1C, 0,	"IDT 92HD83C1C" },
207 	{ HDA_CODEC_IDT92HD83C1X, 0,	"IDT 92HD83C1X" },
208 	{ HDA_CODEC_IDT92HD87B1_3, 0,	"IDT 92HD87B1/3" },
209 	{ HDA_CODEC_IDT92HD87B2_4, 0,	"IDT 92HD87B2/4" },
210 	{ HDA_CODEC_IDT92HD89C3, 0,	"IDT 92HD89C3" },
211 	{ HDA_CODEC_IDT92HD89C2, 0,	"IDT 92HD89C2" },
212 	{ HDA_CODEC_IDT92HD89C1, 0,	"IDT 92HD89C1" },
213 	{ HDA_CODEC_IDT92HD89B3, 0,	"IDT 92HD89B3" },
214 	{ HDA_CODEC_IDT92HD89B2, 0,	"IDT 92HD89B2" },
215 	{ HDA_CODEC_IDT92HD89B1, 0,	"IDT 92HD89B1" },
216 	{ HDA_CODEC_IDT92HD89E3, 0,	"IDT 92HD89E3" },
217 	{ HDA_CODEC_IDT92HD89E2, 0,	"IDT 92HD89E2" },
218 	{ HDA_CODEC_IDT92HD89E1, 0,	"IDT 92HD89E1" },
219 	{ HDA_CODEC_IDT92HD89D3, 0,	"IDT 92HD89D3" },
220 	{ HDA_CODEC_IDT92HD89D2, 0,	"IDT 92HD89D2" },
221 	{ HDA_CODEC_IDT92HD89D1, 0,	"IDT 92HD89D1" },
222 	{ HDA_CODEC_IDT92HD89F3, 0,	"IDT 92HD89F3" },
223 	{ HDA_CODEC_IDT92HD89F2, 0,	"IDT 92HD89F2" },
224 	{ HDA_CODEC_IDT92HD89F1, 0,	"IDT 92HD89F1" },
225 	{ HDA_CODEC_IDT92HD90BXX, 0,	"IDT 92HD90BXX" },
226 	{ HDA_CODEC_IDT92HD91BXX, 0,	"IDT 92HD91BXX" },
227 	{ HDA_CODEC_IDT92HD93BXX, 0,	"IDT 92HD93BXX" },
228 	{ HDA_CODEC_IDT92HD98BXX, 0,	"IDT 92HD98BXX" },
229 	{ HDA_CODEC_IDT92HD99BXX, 0,	"IDT 92HD99BXX" },
230 	{ HDA_CODEC_CX20549, 0,		"Conexant CX20549 (Venice)" },
231 	{ HDA_CODEC_CX20551, 0,		"Conexant CX20551 (Waikiki)" },
232 	{ HDA_CODEC_CX20561, 0,		"Conexant CX20561 (Hermosa)" },
233 	{ HDA_CODEC_CX20582, 0,		"Conexant CX20582 (Pebble)" },
234 	{ HDA_CODEC_CX20583, 0,		"Conexant CX20583 (Pebble HSF)" },
235 	{ HDA_CODEC_CX20584, 0,		"Conexant CX20584" },
236 	{ HDA_CODEC_CX20585, 0,		"Conexant CX20585" },
237 	{ HDA_CODEC_CX20588, 0,		"Conexant CX20588" },
238 	{ HDA_CODEC_CX20590, 0,		"Conexant CX20590" },
239 	{ HDA_CODEC_CX20631, 0,		"Conexant CX20631" },
240 	{ HDA_CODEC_CX20632, 0,		"Conexant CX20632" },
241 	{ HDA_CODEC_CX20641, 0,		"Conexant CX20641" },
242 	{ HDA_CODEC_CX20642, 0,		"Conexant CX20642" },
243 	{ HDA_CODEC_CX20651, 0,		"Conexant CX20651" },
244 	{ HDA_CODEC_CX20652, 0,		"Conexant CX20652" },
245 	{ HDA_CODEC_CX20664, 0,		"Conexant CX20664" },
246 	{ HDA_CODEC_CX20665, 0,		"Conexant CX20665" },
247 	{ HDA_CODEC_VT1708_8, 0,	"VIA VT1708_8" },
248 	{ HDA_CODEC_VT1708_9, 0,	"VIA VT1708_9" },
249 	{ HDA_CODEC_VT1708_A, 0,	"VIA VT1708_A" },
250 	{ HDA_CODEC_VT1708_B, 0,	"VIA VT1708_B" },
251 	{ HDA_CODEC_VT1709_0, 0,	"VIA VT1709_0" },
252 	{ HDA_CODEC_VT1709_1, 0,	"VIA VT1709_1" },
253 	{ HDA_CODEC_VT1709_2, 0,	"VIA VT1709_2" },
254 	{ HDA_CODEC_VT1709_3, 0,	"VIA VT1709_3" },
255 	{ HDA_CODEC_VT1709_4, 0,	"VIA VT1709_4" },
256 	{ HDA_CODEC_VT1709_5, 0,	"VIA VT1709_5" },
257 	{ HDA_CODEC_VT1709_6, 0,	"VIA VT1709_6" },
258 	{ HDA_CODEC_VT1709_7, 0,	"VIA VT1709_7" },
259 	{ HDA_CODEC_VT1708B_0, 0,	"VIA VT1708B_0" },
260 	{ HDA_CODEC_VT1708B_1, 0,	"VIA VT1708B_1" },
261 	{ HDA_CODEC_VT1708B_2, 0,	"VIA VT1708B_2" },
262 	{ HDA_CODEC_VT1708B_3, 0,	"VIA VT1708B_3" },
263 	{ HDA_CODEC_VT1708B_4, 0,	"VIA VT1708B_4" },
264 	{ HDA_CODEC_VT1708B_5, 0,	"VIA VT1708B_5" },
265 	{ HDA_CODEC_VT1708B_6, 0,	"VIA VT1708B_6" },
266 	{ HDA_CODEC_VT1708B_7, 0,	"VIA VT1708B_7" },
267 	{ HDA_CODEC_VT1708S_0, 0,	"VIA VT1708S_0" },
268 	{ HDA_CODEC_VT1708S_1, 0,	"VIA VT1708S_1" },
269 	{ HDA_CODEC_VT1708S_2, 0,	"VIA VT1708S_2" },
270 	{ HDA_CODEC_VT1708S_3, 0,	"VIA VT1708S_3" },
271 	{ HDA_CODEC_VT1708S_4, 0,	"VIA VT1708S_4" },
272 	{ HDA_CODEC_VT1708S_5, 0,	"VIA VT1708S_5" },
273 	{ HDA_CODEC_VT1708S_6, 0,	"VIA VT1708S_6" },
274 	{ HDA_CODEC_VT1708S_7, 0,	"VIA VT1708S_7" },
275 	{ HDA_CODEC_VT1702_0, 0,	"VIA VT1702_0" },
276 	{ HDA_CODEC_VT1702_1, 0,	"VIA VT1702_1" },
277 	{ HDA_CODEC_VT1702_2, 0,	"VIA VT1702_2" },
278 	{ HDA_CODEC_VT1702_3, 0,	"VIA VT1702_3" },
279 	{ HDA_CODEC_VT1702_4, 0,	"VIA VT1702_4" },
280 	{ HDA_CODEC_VT1702_5, 0,	"VIA VT1702_5" },
281 	{ HDA_CODEC_VT1702_6, 0,	"VIA VT1702_6" },
282 	{ HDA_CODEC_VT1702_7, 0,	"VIA VT1702_7" },
283 	{ HDA_CODEC_VT1716S_0, 0,	"VIA VT1716S_0" },
284 	{ HDA_CODEC_VT1716S_1, 0,	"VIA VT1716S_1" },
285 	{ HDA_CODEC_VT1718S_0, 0,	"VIA VT1718S_0" },
286 	{ HDA_CODEC_VT1718S_1, 0,	"VIA VT1718S_1" },
287 	{ HDA_CODEC_VT1802_0, 0,	"VIA VT1802_0" },
288 	{ HDA_CODEC_VT1802_1, 0,	"VIA VT1802_1" },
289 	{ HDA_CODEC_VT1812, 0,		"VIA VT1812" },
290 	{ HDA_CODEC_VT1818S, 0,		"VIA VT1818S" },
291 	{ HDA_CODEC_VT1828S, 0,		"VIA VT1828S" },
292 	{ HDA_CODEC_VT2002P_0, 0,	"VIA VT2002P_0" },
293 	{ HDA_CODEC_VT2002P_1, 0,	"VIA VT2002P_1" },
294 	{ HDA_CODEC_VT2020, 0,		"VIA VT2020" },
295 	{ HDA_CODEC_ATIRS600_1, 0,	"ATI RS600" },
296 	{ HDA_CODEC_ATIRS600_2, 0,	"ATI RS600" },
297 	{ HDA_CODEC_ATIRS690, 0,	"ATI RS690/780" },
298 	{ HDA_CODEC_ATIR6XX, 0,		"ATI R6xx" },
299 	{ HDA_CODEC_NVIDIAMCP67, 0,	"NVIDIA MCP67" },
300 	{ HDA_CODEC_NVIDIAMCP73, 0,	"NVIDIA MCP73" },
301 	{ HDA_CODEC_NVIDIAMCP78, 0,	"NVIDIA MCP78" },
302 	{ HDA_CODEC_NVIDIAMCP78_2, 0,	"NVIDIA MCP78" },
303 	{ HDA_CODEC_NVIDIAMCP78_3, 0,	"NVIDIA MCP78" },
304 	{ HDA_CODEC_NVIDIAMCP78_4, 0,	"NVIDIA MCP78" },
305 	{ HDA_CODEC_NVIDIAMCP7A, 0,	"NVIDIA MCP7A" },
306 	{ HDA_CODEC_NVIDIAGT220, 0,	"NVIDIA GT220" },
307 	{ HDA_CODEC_NVIDIAGT21X, 0,	"NVIDIA GT21x" },
308 	{ HDA_CODEC_NVIDIAMCP89, 0,	"NVIDIA MCP89" },
309 	{ HDA_CODEC_NVIDIAGT240, 0,	"NVIDIA GT240" },
310 	{ HDA_CODEC_NVIDIAGTS450, 0,	"NVIDIA GTS450" },
311 	{ HDA_CODEC_NVIDIAGT440, 0,	"NVIDIA GT440" },
312 	{ HDA_CODEC_NVIDIAGTX550, 0,	"NVIDIA GTX550" },
313 	{ HDA_CODEC_NVIDIAGTX570, 0,	"NVIDIA GTX570" },
314 	{ HDA_CODEC_INTELIP, 0,		"Intel Ibex Peak" },
315 	{ HDA_CODEC_INTELBL, 0,		"Intel Bearlake" },
316 	{ HDA_CODEC_INTELCA, 0,		"Intel Cantiga" },
317 	{ HDA_CODEC_INTELEL, 0,		"Intel Eaglelake" },
318 	{ HDA_CODEC_INTELIP2, 0,	"Intel Ibex Peak" },
319 	{ HDA_CODEC_INTELCPT, 0,	"Intel Cougar Point" },
320 	{ HDA_CODEC_INTELPPT, 0,	"Intel Panther Point" },
321 	{ HDA_CODEC_INTELHSW, 0,	"Intel Haswell" },
322 	{ HDA_CODEC_INTELCL, 0,		"Intel Crestline" },
323 	{ HDA_CODEC_SII1390, 0,		"Silicon Image SiI1390" },
324 	{ HDA_CODEC_SII1392, 0,		"Silicon Image SiI1392" },
325 	/* Unknown CODECs */
326 	{ HDA_CODEC_ADXXXX, 0,		"Analog Devices" },
327 	{ HDA_CODEC_AGEREXXXX, 0,	"Lucent/Agere Systems" },
328 	{ HDA_CODEC_ALCXXXX, 0,		"Realtek" },
329 	{ HDA_CODEC_ATIXXXX, 0,		"ATI" },
330 	{ HDA_CODEC_CAXXXX, 0,		"Creative" },
331 	{ HDA_CODEC_CMIXXXX, 0,		"CMedia" },
332 	{ HDA_CODEC_CMIXXXX2, 0,	"CMedia" },
333 	{ HDA_CODEC_CSXXXX, 0,		"Cirrus Logic" },
334 	{ HDA_CODEC_CXXXXX, 0,		"Conexant" },
335 	{ HDA_CODEC_CHXXXX, 0,		"Chrontel" },
336 	{ HDA_CODEC_IDTXXXX, 0,		"IDT" },
337 	{ HDA_CODEC_INTELXXXX, 0,	"Intel" },
338 	{ HDA_CODEC_MOTOXXXX, 0,	"Motorola" },
339 	{ HDA_CODEC_NVIDIAXXXX, 0,	"NVIDIA" },
340 	{ HDA_CODEC_SIIXXXX, 0,		"Silicon Image" },
341 	{ HDA_CODEC_STACXXXX, 0,	"Sigmatel" },
342 	{ HDA_CODEC_VTXXXX, 0,		"VIA" },
343 };
344 #define HDACC_CODECS_LEN	(sizeof(hdacc_codecs) / sizeof(hdacc_codecs[0]))
345 
346 static int
347 hdacc_suspend(device_t dev)
348 {
349 
350 	HDA_BOOTHVERBOSE(
351 		device_printf(dev, "Suspend...\n");
352 	);
353 	bus_generic_suspend(dev);
354 	HDA_BOOTHVERBOSE(
355 		device_printf(dev, "Suspend done\n");
356 	);
357 	return (0);
358 }
359 
360 static int
361 hdacc_resume(device_t dev)
362 {
363 
364 	HDA_BOOTHVERBOSE(
365 		device_printf(dev, "Resume...\n");
366 	);
367 	bus_generic_resume(dev);
368 	HDA_BOOTHVERBOSE(
369 		device_printf(dev, "Resume done\n");
370 	);
371 	return (0);
372 }
373 
374 static int
375 hdacc_probe(device_t dev)
376 {
377 	uint32_t id, revid;
378 	char buf[128];
379 	int i;
380 
381 	id = ((uint32_t)hda_get_vendor_id(dev) << 16) + hda_get_device_id(dev);
382 	revid = ((uint32_t)hda_get_revision_id(dev) << 8) + hda_get_stepping_id(dev);
383 
384 	for (i = 0; i < HDACC_CODECS_LEN; i++) {
385 		if (!HDA_DEV_MATCH(hdacc_codecs[i].id, id))
386 			continue;
387 		if (hdacc_codecs[i].revid != 0 &&
388 		    hdacc_codecs[i].revid != revid)
389 			continue;
390 		break;
391 	}
392 	if (i < HDACC_CODECS_LEN) {
393 		if ((hdacc_codecs[i].id & 0xffff) != 0xffff)
394 			strlcpy(buf, hdacc_codecs[i].name, sizeof(buf));
395 		else
396 			snprintf(buf, sizeof(buf), "%s (0x%04x)",
397 			    hdacc_codecs[i].name, hda_get_device_id(dev));
398 	} else
399 		snprintf(buf, sizeof(buf), "Generic (0x%04x)", id);
400 	strlcat(buf, " HDA CODEC", sizeof(buf));
401 	device_set_desc_copy(dev, buf);
402 	return (BUS_PROBE_DEFAULT);
403 }
404 
405 static int
406 hdacc_attach(device_t dev)
407 {
408 	struct hdacc_softc *codec = device_get_softc(dev);
409 	device_t child;
410 	int cad = (intptr_t)device_get_ivars(dev);
411 	uint32_t subnode;
412 	int startnode;
413 	int endnode;
414 	int i, n;
415 
416 	codec->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
417 	codec->dev = dev;
418 	codec->cad = cad;
419 
420 	hdacc_lock(codec);
421 	subnode = hda_command(dev,
422 	    HDA_CMD_GET_PARAMETER(0, 0x0, HDA_PARAM_SUB_NODE_COUNT));
423 	hdacc_unlock(codec);
424 	if (subnode == HDA_INVALID)
425 		return (EIO);
426 	codec->fgcnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(subnode);
427 	startnode = HDA_PARAM_SUB_NODE_COUNT_START(subnode);
428 	endnode = startnode + codec->fgcnt;
429 
430 	HDA_BOOTHVERBOSE(
431 		device_printf(dev,
432 		    "Root Node at nid=0: %d subnodes %d-%d\n",
433 		    HDA_PARAM_SUB_NODE_COUNT_TOTAL(subnode),
434 		    startnode, endnode - 1);
435 	);
436 
437 	codec->fgs = malloc(sizeof(struct hdacc_fg) * codec->fgcnt,
438 	    M_HDACC, M_ZERO | M_WAITOK);
439 	for (i = startnode, n = 0; i < endnode; i++, n++) {
440 		codec->fgs[n].nid = i;
441 		hdacc_lock(codec);
442 		codec->fgs[n].type =
443 		    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE(hda_command(dev,
444 		    HDA_CMD_GET_PARAMETER(0, i, HDA_PARAM_FCT_GRP_TYPE)));
445 		codec->fgs[n].subsystem_id = hda_command(dev,
446 		    HDA_CMD_GET_SUBSYSTEM_ID(0, i));
447 		hdacc_unlock(codec);
448 		codec->fgs[n].dev = child = device_add_child(dev, NULL, -1);
449 		if (child == NULL) {
450 			device_printf(dev, "Failed to add function device\n");
451 			continue;
452 		}
453 		device_set_ivars(child, &codec->fgs[n]);
454 	}
455 
456 	bus_generic_attach(dev);
457 
458 	return (0);
459 }
460 
461 static int
462 hdacc_detach(device_t dev)
463 {
464 	struct hdacc_softc *codec = device_get_softc(dev);
465 	int error;
466 
467 	error = device_delete_children(dev);
468 	free(codec->fgs, M_HDACC);
469 	return (error);
470 }
471 
472 static int
473 hdacc_child_location_str(device_t dev, device_t child, char *buf,
474     size_t buflen)
475 {
476 	struct hdacc_fg *fg = device_get_ivars(child);
477 
478 	snprintf(buf, buflen, "nid=%d", fg->nid);
479 	return (0);
480 }
481 
482 static int
483 hdacc_child_pnpinfo_str_method(device_t dev, device_t child, char *buf,
484     size_t buflen)
485 {
486 	struct hdacc_fg *fg = device_get_ivars(child);
487 
488 	snprintf(buf, buflen, "type=0x%02x subsystem=0x%08x",
489 	    fg->type, fg->subsystem_id);
490 	return (0);
491 }
492 
493 static int
494 hdacc_print_child(device_t dev, device_t child)
495 {
496 	struct hdacc_fg *fg = device_get_ivars(child);
497 	int retval;
498 
499 	retval = bus_print_child_header(dev, child);
500 	retval += printf(" at nid %d", fg->nid);
501 	retval += bus_print_child_footer(dev, child);
502 
503 	return (retval);
504 }
505 
506 static void
507 hdacc_probe_nomatch(device_t dev, device_t child)
508 {
509 	struct hdacc_softc *codec = device_get_softc(dev);
510 	struct hdacc_fg *fg = device_get_ivars(child);
511 
512 	device_printf(child, "<%s %s Function Group> at nid %d on %s "
513 	    "(no driver attached)\n",
514 	    device_get_desc(dev),
515 	    fg->type == HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO ? "Audio" :
516 	    (fg->type == HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_MODEM ? "Modem" :
517 	    "Unknown"), fg->nid, device_get_nameunit(dev));
518 	HDA_BOOTVERBOSE(
519 		device_printf(dev, "Subsystem ID: 0x%08x\n",
520 		    hda_get_subsystem_id(dev));
521 	);
522 	HDA_BOOTHVERBOSE(
523 		device_printf(dev, "Power down FG nid=%d to the D3 state...\n",
524 		    fg->nid);
525 	);
526 	hdacc_lock(codec);
527 	hda_command(dev, HDA_CMD_SET_POWER_STATE(0,
528 	    fg->nid, HDA_CMD_POWER_STATE_D3));
529 	hdacc_unlock(codec);
530 }
531 
532 static int
533 hdacc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
534 {
535 	struct hdacc_fg *fg = device_get_ivars(child);
536 
537 	switch (which) {
538 	case HDA_IVAR_NODE_ID:
539 		*result = fg->nid;
540 		break;
541 	case HDA_IVAR_NODE_TYPE:
542 		*result = fg->type;
543 		break;
544 	case HDA_IVAR_SUBSYSTEM_ID:
545 		*result = fg->subsystem_id;
546 		break;
547 	default:
548 		return(BUS_READ_IVAR(device_get_parent(dev), dev,
549 		    which, result));
550 	}
551 	return (0);
552 }
553 
554 static struct mtx *
555 hdacc_get_mtx(device_t dev, device_t child)
556 {
557 	struct hdacc_softc *codec = device_get_softc(dev);
558 
559 	return (codec->lock);
560 }
561 
562 static uint32_t
563 hdacc_codec_command(device_t dev, device_t child, uint32_t verb)
564 {
565 
566 	return (HDAC_CODEC_COMMAND(device_get_parent(dev), dev, verb));
567 }
568 
569 static int
570 hdacc_stream_alloc(device_t dev, device_t child, int dir, int format,
571     int stripe, uint32_t **dmapos)
572 {
573 	struct hdacc_softc *codec = device_get_softc(dev);
574 	int stream;
575 
576 	stream = HDAC_STREAM_ALLOC(device_get_parent(dev), dev,
577 	    dir, format, stripe, dmapos);
578 	if (stream > 0)
579 		codec->streams[dir][stream] = child;
580 	return (stream);
581 }
582 
583 static void
584 hdacc_stream_free(device_t dev, device_t child, int dir, int stream)
585 {
586 	struct hdacc_softc *codec = device_get_softc(dev);
587 
588 	codec->streams[dir][stream] = NULL;
589 	HDAC_STREAM_FREE(device_get_parent(dev), dev, dir, stream);
590 }
591 
592 static int
593 hdacc_stream_start(device_t dev, device_t child,
594     int dir, int stream, bus_addr_t buf, int blksz, int blkcnt)
595 {
596 
597 	return (HDAC_STREAM_START(device_get_parent(dev), dev,
598 	    dir, stream, buf, blksz, blkcnt));
599 }
600 
601 static void
602 hdacc_stream_stop(device_t dev, device_t child, int dir, int stream)
603 {
604 
605 	HDAC_STREAM_STOP(device_get_parent(dev), dev, dir, stream);
606 }
607 
608 static void
609 hdacc_stream_reset(device_t dev, device_t child, int dir, int stream)
610 {
611 
612 	HDAC_STREAM_RESET(device_get_parent(dev), dev, dir, stream);
613 }
614 
615 static uint32_t
616 hdacc_stream_getptr(device_t dev, device_t child, int dir, int stream)
617 {
618 
619 	return (HDAC_STREAM_GETPTR(device_get_parent(dev), dev, dir, stream));
620 }
621 
622 static void
623 hdacc_stream_intr(device_t dev, int dir, int stream)
624 {
625 	struct hdacc_softc *codec = device_get_softc(dev);
626 	device_t child;
627 
628 	if ((child = codec->streams[dir][stream]) != NULL)
629 		HDAC_STREAM_INTR(child, dir, stream);
630 }
631 
632 static int
633 hdacc_unsol_alloc(device_t dev, device_t child, int wanted)
634 {
635 	struct hdacc_softc *codec = device_get_softc(dev);
636 	int tag;
637 
638 	wanted &= 0x3f;
639 	tag = wanted;
640 	do {
641 		if (codec->tags[tag] == NULL) {
642 			codec->tags[tag] = child;
643 			HDAC_UNSOL_ALLOC(device_get_parent(dev), dev, tag);
644 			return (tag);
645 		}
646 		tag++;
647 		tag &= 0x3f;
648 	} while (tag != wanted);
649 	return (-1);
650 }
651 
652 static void
653 hdacc_unsol_free(device_t dev, device_t child, int tag)
654 {
655 	struct hdacc_softc *codec = device_get_softc(dev);
656 
657 	KASSERT(tag >= 0 && tag <= 0x3f, ("Wrong tag value %d\n", tag));
658 	codec->tags[tag] = NULL;
659 	HDAC_UNSOL_FREE(device_get_parent(dev), dev, tag);
660 }
661 
662 static void
663 hdacc_unsol_intr(device_t dev, uint32_t resp)
664 {
665 	struct hdacc_softc *codec = device_get_softc(dev);
666 	device_t child;
667 	int tag;
668 
669 	tag = resp >> 26;
670 	if ((child = codec->tags[tag]) != NULL)
671 		HDAC_UNSOL_INTR(child, resp);
672 	else
673 		device_printf(codec->dev, "Unexpected unsolicited "
674 		    "response with tag %d: %08x\n", tag, resp);
675 }
676 
677 static void
678 hdacc_pindump(device_t dev)
679 {
680 	device_t *devlist;
681 	int devcount, i;
682 
683 	if (device_get_children(dev, &devlist, &devcount) != 0)
684 		return;
685 	for (i = 0; i < devcount; i++)
686 		HDAC_PINDUMP(devlist[i]);
687 	free(devlist, M_TEMP);
688 }
689 
690 static device_method_t hdacc_methods[] = {
691 	/* device interface */
692 	DEVMETHOD(device_probe,		hdacc_probe),
693 	DEVMETHOD(device_attach,	hdacc_attach),
694 	DEVMETHOD(device_detach,	hdacc_detach),
695 	DEVMETHOD(device_suspend,	hdacc_suspend),
696 	DEVMETHOD(device_resume,	hdacc_resume),
697 	/* Bus interface */
698 	DEVMETHOD(bus_child_location_str, hdacc_child_location_str),
699 	DEVMETHOD(bus_child_pnpinfo_str, hdacc_child_pnpinfo_str_method),
700 	DEVMETHOD(bus_print_child,	hdacc_print_child),
701 	DEVMETHOD(bus_probe_nomatch,	hdacc_probe_nomatch),
702 	DEVMETHOD(bus_read_ivar,	hdacc_read_ivar),
703 	DEVMETHOD(hdac_get_mtx,		hdacc_get_mtx),
704 	DEVMETHOD(hdac_codec_command,	hdacc_codec_command),
705 	DEVMETHOD(hdac_stream_alloc,	hdacc_stream_alloc),
706 	DEVMETHOD(hdac_stream_free,	hdacc_stream_free),
707 	DEVMETHOD(hdac_stream_start,	hdacc_stream_start),
708 	DEVMETHOD(hdac_stream_stop,	hdacc_stream_stop),
709 	DEVMETHOD(hdac_stream_reset,	hdacc_stream_reset),
710 	DEVMETHOD(hdac_stream_getptr,	hdacc_stream_getptr),
711 	DEVMETHOD(hdac_stream_intr,	hdacc_stream_intr),
712 	DEVMETHOD(hdac_unsol_alloc,	hdacc_unsol_alloc),
713 	DEVMETHOD(hdac_unsol_free,	hdacc_unsol_free),
714 	DEVMETHOD(hdac_unsol_intr,	hdacc_unsol_intr),
715 	DEVMETHOD(hdac_pindump,		hdacc_pindump),
716 	{ 0, 0 }
717 };
718 
719 static driver_t hdacc_driver = {
720 	"hdacc",
721 	hdacc_methods,
722 	sizeof(struct hdacc_softc),
723 };
724 
725 static devclass_t hdacc_devclass;
726 
727 DRIVER_MODULE(snd_hda, hdac, hdacc_driver, hdacc_devclass, 0, 0);
728