xref: /freebsd/sys/dev/sound/pci/hda/hdac.c (revision 70e0bbedef95258a4dadc996d641a9bebd3f107d)
1 /*-
2  * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3  * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4  * Copyright (c) 2008 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 (Controller) driver for FreeBSD. Be advised
31  * that this driver still in its early stage, and possible of rewrite are
32  * pretty much guaranteed. There are supposedly several distinct parent/child
33  * busses to make this "perfect", but as for now and for the sake of
34  * simplicity, everything is gobble up within single source.
35  *
36  * List of subsys:
37  *     1) HDA Controller support
38  *     2) HDA Codecs support, which may include
39  *        - HDA
40  *        - Modem
41  *     3) Widget parser - the real magic of why this driver works on so
42  *        many hardwares with minimal vendor specific quirk. The original
43  *        parser was written using Ruby and can be found at
44  *        http://people.freebsd.org/~ariff/HDA/parser.rb . This crude
45  *        ruby parser take the verbose dmesg dump as its input. Refer to
46  *        http://www.microsoft.com/whdc/device/audio/default.mspx for various
47  *        interesting documents, especially UAA (Universal Audio Architecture).
48  *     4) Possible vendor specific support.
49  *        (snd_hda_intel, snd_hda_ati, etc..)
50  *
51  * Thanks to Ahmad Ubaidah Omar @ Defenxis Sdn. Bhd. for the
52  * Compaq V3000 with Conexant HDA.
53  *
54  *    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
55  *    *                                                                 *
56  *    *        This driver is a collaborative effort made by:           *
57  *    *                                                                 *
58  *    *          Stephane E. Potvin <sepotvin@videotron.ca>             *
59  *    *               Andrea Bittau <a.bittau@cs.ucl.ac.uk>             *
60  *    *               Wesley Morgan <morganw@chemikals.org>             *
61  *    *              Daniel Eischen <deischen@FreeBSD.org>              *
62  *    *             Maxime Guillaud <bsd-ports@mguillaud.net>           *
63  *    *              Ariff Abdullah <ariff@FreeBSD.org>                 *
64  *    *             Alexander Motin <mav@FreeBSD.org>                   *
65  *    *                                                                 *
66  *    *   ....and various people from freebsd-multimedia@FreeBSD.org    *
67  *    *                                                                 *
68  *    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
69  */
70 
71 #ifdef HAVE_KERNEL_OPTION_HEADERS
72 #include "opt_snd.h"
73 #endif
74 
75 #include <dev/sound/pcm/sound.h>
76 #include <dev/pci/pcireg.h>
77 #include <dev/pci/pcivar.h>
78 
79 #include <sys/ctype.h>
80 #include <sys/taskqueue.h>
81 
82 #include <dev/sound/pci/hda/hdac_private.h>
83 #include <dev/sound/pci/hda/hdac_reg.h>
84 #include <dev/sound/pci/hda/hda_reg.h>
85 #include <dev/sound/pci/hda/hdac.h>
86 
87 #include "mixer_if.h"
88 
89 #define HDA_DRV_TEST_REV	"20100226_0142"
90 
91 SND_DECLARE_FILE("$FreeBSD$");
92 
93 #define HDA_BOOTVERBOSE(stmt)	do {			\
94 	if (bootverbose != 0 || snd_verbose > 3) {	\
95 		stmt					\
96 	}						\
97 } while (0)
98 
99 #define HDA_BOOTHVERBOSE(stmt)	do {			\
100 	if (snd_verbose > 3) {				\
101 		stmt					\
102 	}						\
103 } while (0)
104 
105 #if 1
106 #undef HDAC_INTR_EXTRA
107 #define HDAC_INTR_EXTRA		1
108 #endif
109 
110 #define hdac_lock(sc)		snd_mtxlock((sc)->lock)
111 #define hdac_unlock(sc)		snd_mtxunlock((sc)->lock)
112 #define hdac_lockassert(sc)	snd_mtxassert((sc)->lock)
113 #define hdac_lockowned(sc)	mtx_owned((sc)->lock)
114 
115 #define HDA_FLAG_MATCH(fl, v)	(((fl) & (v)) == (v))
116 #define HDA_DEV_MATCH(fl, v)	((fl) == (v) || \
117 				(fl) == 0xffffffff || \
118 				(((fl) & 0xffff0000) == 0xffff0000 && \
119 				((fl) & 0x0000ffff) == ((v) & 0x0000ffff)) || \
120 				(((fl) & 0x0000ffff) == 0x0000ffff && \
121 				((fl) & 0xffff0000) == ((v) & 0xffff0000)))
122 #define HDA_MATCH_ALL		0xffffffff
123 #define HDAC_INVALID		0xffffffff
124 
125 /* Default controller / jack sense poll: 250ms */
126 #define HDAC_POLL_INTERVAL	max(hz >> 2, 1)
127 
128 /*
129  * Make room for possible 4096 playback/record channels, in 100 years to come.
130  */
131 #define HDAC_TRIGGER_NONE	0x00000000
132 #define HDAC_TRIGGER_PLAY	0x00000fff
133 #define HDAC_TRIGGER_REC	0x00fff000
134 #define HDAC_TRIGGER_UNSOL	0x80000000
135 
136 #define HDA_MODEL_CONSTRUCT(vendor, model)	\
137 		(((uint32_t)(model) << 16) | ((vendor##_VENDORID) & 0xffff))
138 
139 /* Controller models */
140 
141 /* Intel */
142 #define INTEL_VENDORID		0x8086
143 #define HDA_INTEL_CPT		HDA_MODEL_CONSTRUCT(INTEL, 0x1c20)
144 #define HDA_INTEL_PATSBURG	HDA_MODEL_CONSTRUCT(INTEL, 0x1d20)
145 #define HDA_INTEL_PPT1		HDA_MODEL_CONSTRUCT(INTEL, 0x1e20)
146 #define HDA_INTEL_82801F	HDA_MODEL_CONSTRUCT(INTEL, 0x2668)
147 #define HDA_INTEL_63XXESB	HDA_MODEL_CONSTRUCT(INTEL, 0x269a)
148 #define HDA_INTEL_82801G	HDA_MODEL_CONSTRUCT(INTEL, 0x27d8)
149 #define HDA_INTEL_82801H	HDA_MODEL_CONSTRUCT(INTEL, 0x284b)
150 #define HDA_INTEL_82801I	HDA_MODEL_CONSTRUCT(INTEL, 0x293e)
151 #define HDA_INTEL_82801JI	HDA_MODEL_CONSTRUCT(INTEL, 0x3a3e)
152 #define HDA_INTEL_82801JD	HDA_MODEL_CONSTRUCT(INTEL, 0x3a6e)
153 #define HDA_INTEL_PCH		HDA_MODEL_CONSTRUCT(INTEL, 0x3b56)
154 #define HDA_INTEL_PCH2		HDA_MODEL_CONSTRUCT(INTEL, 0x3b57)
155 #define HDA_INTEL_SCH		HDA_MODEL_CONSTRUCT(INTEL, 0x811b)
156 #define HDA_INTEL_ALL		HDA_MODEL_CONSTRUCT(INTEL, 0xffff)
157 
158 /* Nvidia */
159 #define NVIDIA_VENDORID		0x10de
160 #define HDA_NVIDIA_MCP51	HDA_MODEL_CONSTRUCT(NVIDIA, 0x026c)
161 #define HDA_NVIDIA_MCP55	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0371)
162 #define HDA_NVIDIA_MCP61_1	HDA_MODEL_CONSTRUCT(NVIDIA, 0x03e4)
163 #define HDA_NVIDIA_MCP61_2	HDA_MODEL_CONSTRUCT(NVIDIA, 0x03f0)
164 #define HDA_NVIDIA_MCP65_1	HDA_MODEL_CONSTRUCT(NVIDIA, 0x044a)
165 #define HDA_NVIDIA_MCP65_2	HDA_MODEL_CONSTRUCT(NVIDIA, 0x044b)
166 #define HDA_NVIDIA_MCP67_1	HDA_MODEL_CONSTRUCT(NVIDIA, 0x055c)
167 #define HDA_NVIDIA_MCP67_2	HDA_MODEL_CONSTRUCT(NVIDIA, 0x055d)
168 #define HDA_NVIDIA_MCP78_1	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0774)
169 #define HDA_NVIDIA_MCP78_2	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0775)
170 #define HDA_NVIDIA_MCP78_3	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0776)
171 #define HDA_NVIDIA_MCP78_4	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0777)
172 #define HDA_NVIDIA_MCP73_1	HDA_MODEL_CONSTRUCT(NVIDIA, 0x07fc)
173 #define HDA_NVIDIA_MCP73_2	HDA_MODEL_CONSTRUCT(NVIDIA, 0x07fd)
174 #define HDA_NVIDIA_MCP79_1	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0ac0)
175 #define HDA_NVIDIA_MCP79_2	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0ac1)
176 #define HDA_NVIDIA_MCP79_3	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0ac2)
177 #define HDA_NVIDIA_MCP79_4	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0ac3)
178 #define HDA_NVIDIA_MCP89_1	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0d94)
179 #define HDA_NVIDIA_MCP89_2	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0d95)
180 #define HDA_NVIDIA_MCP89_3	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0d96)
181 #define HDA_NVIDIA_MCP89_4	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0d97)
182 #define HDA_NVIDIA_ALL		HDA_MODEL_CONSTRUCT(NVIDIA, 0xffff)
183 
184 /* ATI */
185 #define ATI_VENDORID		0x1002
186 #define HDA_ATI_SB450		HDA_MODEL_CONSTRUCT(ATI, 0x437b)
187 #define HDA_ATI_SB600		HDA_MODEL_CONSTRUCT(ATI, 0x4383)
188 #define HDA_ATI_RS600		HDA_MODEL_CONSTRUCT(ATI, 0x793b)
189 #define HDA_ATI_RS690		HDA_MODEL_CONSTRUCT(ATI, 0x7919)
190 #define HDA_ATI_RS780		HDA_MODEL_CONSTRUCT(ATI, 0x960f)
191 #define HDA_ATI_R600		HDA_MODEL_CONSTRUCT(ATI, 0xaa00)
192 #define HDA_ATI_RV630		HDA_MODEL_CONSTRUCT(ATI, 0xaa08)
193 #define HDA_ATI_RV610		HDA_MODEL_CONSTRUCT(ATI, 0xaa10)
194 #define HDA_ATI_RV670		HDA_MODEL_CONSTRUCT(ATI, 0xaa18)
195 #define HDA_ATI_RV635		HDA_MODEL_CONSTRUCT(ATI, 0xaa20)
196 #define HDA_ATI_RV620		HDA_MODEL_CONSTRUCT(ATI, 0xaa28)
197 #define HDA_ATI_RV770		HDA_MODEL_CONSTRUCT(ATI, 0xaa30)
198 #define HDA_ATI_RV730		HDA_MODEL_CONSTRUCT(ATI, 0xaa38)
199 #define HDA_ATI_RV710		HDA_MODEL_CONSTRUCT(ATI, 0xaa40)
200 #define HDA_ATI_RV740		HDA_MODEL_CONSTRUCT(ATI, 0xaa48)
201 #define HDA_ATI_ALL		HDA_MODEL_CONSTRUCT(ATI, 0xffff)
202 
203 /* RDC */
204 #define RDC_VENDORID		0x17f3
205 #define HDA_RDC_M3010		HDA_MODEL_CONSTRUCT(RDC, 0x3010)
206 
207 /* VIA */
208 #define VIA_VENDORID		0x1106
209 #define HDA_VIA_VT82XX		HDA_MODEL_CONSTRUCT(VIA, 0x3288)
210 #define HDA_VIA_ALL		HDA_MODEL_CONSTRUCT(VIA, 0xffff)
211 
212 /* SiS */
213 #define SIS_VENDORID		0x1039
214 #define HDA_SIS_966		HDA_MODEL_CONSTRUCT(SIS, 0x7502)
215 #define HDA_SIS_ALL		HDA_MODEL_CONSTRUCT(SIS, 0xffff)
216 
217 /* ULI */
218 #define ULI_VENDORID		0x10b9
219 #define HDA_ULI_M5461		HDA_MODEL_CONSTRUCT(ULI, 0x5461)
220 #define HDA_ULI_ALL		HDA_MODEL_CONSTRUCT(ULI, 0xffff)
221 
222 /* OEM/subvendors */
223 
224 /* Intel */
225 #define INTEL_D101GGC_SUBVENDOR	HDA_MODEL_CONSTRUCT(INTEL, 0xd600)
226 
227 /* HP/Compaq */
228 #define HP_VENDORID		0x103c
229 #define HP_V3000_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30b5)
230 #define HP_NX7400_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30a2)
231 #define HP_NX6310_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30aa)
232 #define HP_NX6325_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30b0)
233 #define HP_XW4300_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x3013)
234 #define HP_3010_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x3010)
235 #define HP_DV5000_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30a5)
236 #define HP_DC7700S_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x2801)
237 #define HP_DC7700_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x2802)
238 #define HP_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0xffff)
239 /* What is wrong with XN 2563 anyway? (Got the picture ?) */
240 #define HP_NX6325_SUBVENDORX	0x103c30b0
241 
242 /* Dell */
243 #define DELL_VENDORID		0x1028
244 #define DELL_D630_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x01f9)
245 #define DELL_D820_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x01cc)
246 #define DELL_V1400_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x0227)
247 #define DELL_V1500_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x0228)
248 #define DELL_I1300_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x01c9)
249 #define DELL_XPSM1210_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x01d7)
250 #define DELL_OPLX745_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x01da)
251 #define DELL_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0xffff)
252 
253 /* Clevo */
254 #define CLEVO_VENDORID		0x1558
255 #define CLEVO_D900T_SUBVENDOR	HDA_MODEL_CONSTRUCT(CLEVO, 0x0900)
256 #define CLEVO_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(CLEVO, 0xffff)
257 
258 /* Acer */
259 #define ACER_VENDORID		0x1025
260 #define ACER_A5050_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0x010f)
261 #define ACER_A4520_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0x0127)
262 #define ACER_A4710_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0x012f)
263 #define ACER_A4715_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0x0133)
264 #define ACER_3681WXM_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0x0110)
265 #define ACER_T6292_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0x011b)
266 #define ACER_T5320_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0x011f)
267 #define ACER_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0xffff)
268 
269 /* Asus */
270 #define ASUS_VENDORID		0x1043
271 #define ASUS_A8X_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1153)
272 #define ASUS_U5F_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1263)
273 #define ASUS_W6F_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1263)
274 #define ASUS_A7M_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1323)
275 #define ASUS_F3JC_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1338)
276 #define ASUS_G2K_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1339)
277 #define ASUS_A7T_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x13c2)
278 #define ASUS_W2J_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1971)
279 #define ASUS_M5200_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1993)
280 #define ASUS_P5PL2_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x817f)
281 #define ASUS_P1AH2_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x81cb)
282 #define ASUS_M2NPVMX_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x81cb)
283 #define ASUS_M2V_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x81e7)
284 #define ASUS_P5BWD_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x81ec)
285 #define ASUS_M2N_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x8234)
286 #define ASUS_A8NVMCSM_SUBVENDOR	HDA_MODEL_CONSTRUCT(NVIDIA, 0xcb84)
287 #define ASUS_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0xffff)
288 
289 /* IBM / Lenovo */
290 #define IBM_VENDORID		0x1014
291 #define IBM_M52_SUBVENDOR	HDA_MODEL_CONSTRUCT(IBM, 0x02f6)
292 #define IBM_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(IBM, 0xffff)
293 
294 /* Lenovo */
295 #define LENOVO_VENDORID		0x17aa
296 #define LENOVO_3KN100_SUBVENDOR	HDA_MODEL_CONSTRUCT(LENOVO, 0x2066)
297 #define LENOVO_3KN200_SUBVENDOR	HDA_MODEL_CONSTRUCT(LENOVO, 0x384e)
298 #define LENOVO_B450_SUBVENDOR HDA_MODEL_CONSTRUCT(LENOVO, 0x3a0d)
299 #define LENOVO_TCA55_SUBVENDOR	HDA_MODEL_CONSTRUCT(LENOVO, 0x1015)
300 #define LENOVO_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(LENOVO, 0xffff)
301 
302 /* Samsung */
303 #define SAMSUNG_VENDORID	0x144d
304 #define SAMSUNG_Q1_SUBVENDOR	HDA_MODEL_CONSTRUCT(SAMSUNG, 0xc027)
305 #define SAMSUNG_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(SAMSUNG, 0xffff)
306 
307 /* Medion ? */
308 #define MEDION_VENDORID			0x161f
309 #define MEDION_MD95257_SUBVENDOR	HDA_MODEL_CONSTRUCT(MEDION, 0x203d)
310 #define MEDION_ALL_SUBVENDOR		HDA_MODEL_CONSTRUCT(MEDION, 0xffff)
311 
312 /* Apple Computer Inc. */
313 #define APPLE_VENDORID		0x106b
314 #define APPLE_MB3_SUBVENDOR	HDA_MODEL_CONSTRUCT(APPLE, 0x00a1)
315 
316 /* Sony */
317 #define SONY_VENDORID		0x104d
318 #define SONY_S5_SUBVENDOR	HDA_MODEL_CONSTRUCT(SONY, 0x81cc)
319 #define SONY_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(SONY, 0xffff)
320 
321 /*
322  * Apple Intel MacXXXX seems using Sigmatel codec/vendor id
323  * instead of their own, which is beyond my comprehension
324  * (see HDA_CODEC_STAC9221 below).
325  */
326 #define APPLE_INTEL_MAC		0x76808384
327 #define APPLE_MACBOOKPRO55	0xcb7910de
328 
329 /* LG Electronics */
330 #define LG_VENDORID		0x1854
331 #define LG_LW20_SUBVENDOR	HDA_MODEL_CONSTRUCT(LG, 0x0018)
332 #define LG_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(LG, 0xffff)
333 
334 /* Fujitsu Siemens */
335 #define FS_VENDORID		0x1734
336 #define FS_PA1510_SUBVENDOR	HDA_MODEL_CONSTRUCT(FS, 0x10b8)
337 #define FS_SI1848_SUBVENDOR	HDA_MODEL_CONSTRUCT(FS, 0x10cd)
338 #define FS_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(FS, 0xffff)
339 
340 /* Fujitsu Limited */
341 #define FL_VENDORID		0x10cf
342 #define FL_S7020D_SUBVENDOR	HDA_MODEL_CONSTRUCT(FL, 0x1326)
343 #define FL_U1010_SUBVENDOR	HDA_MODEL_CONSTRUCT(FL, 0x142d)
344 #define FL_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(FL, 0xffff)
345 
346 /* Toshiba */
347 #define TOSHIBA_VENDORID	0x1179
348 #define TOSHIBA_U200_SUBVENDOR	HDA_MODEL_CONSTRUCT(TOSHIBA, 0x0001)
349 #define TOSHIBA_A135_SUBVENDOR	HDA_MODEL_CONSTRUCT(TOSHIBA, 0xff01)
350 #define TOSHIBA_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(TOSHIBA, 0xffff)
351 
352 /* Micro-Star International (MSI) */
353 #define MSI_VENDORID		0x1462
354 #define MSI_MS1034_SUBVENDOR	HDA_MODEL_CONSTRUCT(MSI, 0x0349)
355 #define MSI_MS034A_SUBVENDOR	HDA_MODEL_CONSTRUCT(MSI, 0x034a)
356 #define MSI_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(MSI, 0xffff)
357 
358 /* Giga-Byte Technology */
359 #define GB_VENDORID		0x1458
360 #define GB_G33S2H_SUBVENDOR	HDA_MODEL_CONSTRUCT(GB, 0xa022)
361 #define GP_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(GB, 0xffff)
362 
363 /* Uniwill ? */
364 #define UNIWILL_VENDORID	0x1584
365 #define UNIWILL_9075_SUBVENDOR	HDA_MODEL_CONSTRUCT(UNIWILL, 0x9075)
366 #define UNIWILL_9080_SUBVENDOR	HDA_MODEL_CONSTRUCT(UNIWILL, 0x9080)
367 
368 
369 /* Misc constants.. */
370 #define HDA_AMP_VOL_DEFAULT	(-1)
371 #define HDA_AMP_MUTE_DEFAULT	(0xffffffff)
372 #define HDA_AMP_MUTE_NONE	(0)
373 #define HDA_AMP_MUTE_LEFT	(1 << 0)
374 #define HDA_AMP_MUTE_RIGHT	(1 << 1)
375 #define HDA_AMP_MUTE_ALL	(HDA_AMP_MUTE_LEFT | HDA_AMP_MUTE_RIGHT)
376 
377 #define HDA_AMP_LEFT_MUTED(v)	((v) & (HDA_AMP_MUTE_LEFT))
378 #define HDA_AMP_RIGHT_MUTED(v)	(((v) & HDA_AMP_MUTE_RIGHT) >> 1)
379 
380 #define HDA_ADC_MONITOR		(1 << 0)
381 
382 #define HDA_CTL_OUT		1
383 #define HDA_CTL_IN		2
384 
385 #define HDA_GPIO_MAX		8
386 /* 0 - 7 = GPIO , 8 = Flush */
387 #define HDA_QUIRK_GPIO0		(1 << 0)
388 #define HDA_QUIRK_GPIO1		(1 << 1)
389 #define HDA_QUIRK_GPIO2		(1 << 2)
390 #define HDA_QUIRK_GPIO3		(1 << 3)
391 #define HDA_QUIRK_GPIO4		(1 << 4)
392 #define HDA_QUIRK_GPIO5		(1 << 5)
393 #define HDA_QUIRK_GPIO6		(1 << 6)
394 #define HDA_QUIRK_GPIO7		(1 << 7)
395 #define HDA_QUIRK_GPIOFLUSH	(1 << 8)
396 
397 /* 9 - 25 = anything else */
398 #define HDA_QUIRK_SOFTPCMVOL	(1 << 9)
399 #define HDA_QUIRK_FIXEDRATE	(1 << 10)
400 #define HDA_QUIRK_FORCESTEREO	(1 << 11)
401 #define HDA_QUIRK_EAPDINV	(1 << 12)
402 #define HDA_QUIRK_DMAPOS	(1 << 13)
403 #define HDA_QUIRK_SENSEINV	(1 << 14)
404 
405 /* 26 - 31 = vrefs */
406 #define HDA_QUIRK_IVREF50	(1 << 26)
407 #define HDA_QUIRK_IVREF80	(1 << 27)
408 #define HDA_QUIRK_IVREF100	(1 << 28)
409 #define HDA_QUIRK_OVREF50	(1 << 29)
410 #define HDA_QUIRK_OVREF80	(1 << 30)
411 #define HDA_QUIRK_OVREF100	(1 << 31)
412 
413 #define HDA_QUIRK_IVREF		(HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF80 | \
414 							HDA_QUIRK_IVREF100)
415 #define HDA_QUIRK_OVREF		(HDA_QUIRK_OVREF50 | HDA_QUIRK_OVREF80 | \
416 							HDA_QUIRK_OVREF100)
417 #define HDA_QUIRK_VREF		(HDA_QUIRK_IVREF | HDA_QUIRK_OVREF)
418 
419 #if __FreeBSD_version < 600000
420 #define taskqueue_drain(...)
421 #endif
422 
423 static const struct {
424 	char *key;
425 	uint32_t value;
426 } hdac_quirks_tab[] = {
427 	{ "gpio0", HDA_QUIRK_GPIO0 },
428 	{ "gpio1", HDA_QUIRK_GPIO1 },
429 	{ "gpio2", HDA_QUIRK_GPIO2 },
430 	{ "gpio3", HDA_QUIRK_GPIO3 },
431 	{ "gpio4", HDA_QUIRK_GPIO4 },
432 	{ "gpio5", HDA_QUIRK_GPIO5 },
433 	{ "gpio6", HDA_QUIRK_GPIO6 },
434 	{ "gpio7", HDA_QUIRK_GPIO7 },
435 	{ "gpioflush", HDA_QUIRK_GPIOFLUSH },
436 	{ "softpcmvol", HDA_QUIRK_SOFTPCMVOL },
437 	{ "fixedrate", HDA_QUIRK_FIXEDRATE },
438 	{ "forcestereo", HDA_QUIRK_FORCESTEREO },
439 	{ "eapdinv", HDA_QUIRK_EAPDINV },
440 	{ "dmapos", HDA_QUIRK_DMAPOS },
441 	{ "senseinv", HDA_QUIRK_SENSEINV },
442 	{ "ivref50", HDA_QUIRK_IVREF50 },
443 	{ "ivref80", HDA_QUIRK_IVREF80 },
444 	{ "ivref100", HDA_QUIRK_IVREF100 },
445 	{ "ovref50", HDA_QUIRK_OVREF50 },
446 	{ "ovref80", HDA_QUIRK_OVREF80 },
447 	{ "ovref100", HDA_QUIRK_OVREF100 },
448 	{ "ivref", HDA_QUIRK_IVREF },
449 	{ "ovref", HDA_QUIRK_OVREF },
450 	{ "vref", HDA_QUIRK_VREF },
451 };
452 #define HDAC_QUIRKS_TAB_LEN	\
453 		(sizeof(hdac_quirks_tab) / sizeof(hdac_quirks_tab[0]))
454 
455 #define HDA_BDL_MIN	2
456 #define HDA_BDL_MAX	256
457 #define HDA_BDL_DEFAULT	HDA_BDL_MIN
458 
459 #define HDA_BLK_MIN	HDAC_DMA_ALIGNMENT
460 #define HDA_BLK_ALIGN	(~(HDA_BLK_MIN - 1))
461 
462 #define HDA_BUFSZ_MIN		4096
463 #define HDA_BUFSZ_MAX		65536
464 #define HDA_BUFSZ_DEFAULT	16384
465 
466 #define HDA_PARSE_MAXDEPTH	10
467 
468 #define HDAC_UNSOLTAG_EVENT_HP		0x00
469 
470 MALLOC_DEFINE(M_HDAC, "hdac", "High Definition Audio Controller");
471 
472 const char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue", "Green", "Red",
473     "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B", "Res.C", "Res.D",
474     "White", "Other"};
475 
476 const char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD",
477     "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in",
478     "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"};
479 
480 const char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
481 
482 /* Default */
483 static uint32_t hdac_fmt[] = {
484 	SND_FORMAT(AFMT_S16_LE, 2, 0),
485 	0
486 };
487 
488 static struct pcmchan_caps hdac_caps = {48000, 48000, hdac_fmt, 0};
489 
490 #define HDAC_NO_MSI	1
491 #define HDAC_NO_64BIT	2
492 
493 static const struct {
494 	uint32_t	model;
495 	char		*desc;
496 	char		flags;
497 } hdac_devices[] = {
498 	{ HDA_INTEL_CPT,     "Intel Cougar Point",	0 },
499 	{ HDA_INTEL_PATSBURG,"Intel Patsburg",  0 },
500 	{ HDA_INTEL_PPT1,    "Intel Panther Point",	0 },
501 	{ HDA_INTEL_82801F,  "Intel 82801F",	0 },
502 	{ HDA_INTEL_63XXESB, "Intel 631x/632xESB",	0 },
503 	{ HDA_INTEL_82801G,  "Intel 82801G",	0 },
504 	{ HDA_INTEL_82801H,  "Intel 82801H",	0 },
505 	{ HDA_INTEL_82801I,  "Intel 82801I",	0 },
506 	{ HDA_INTEL_82801JI, "Intel 82801JI",	0 },
507 	{ HDA_INTEL_82801JD, "Intel 82801JD",	0 },
508 	{ HDA_INTEL_PCH,     "Intel 5 Series/3400 Series",	0 },
509 	{ HDA_INTEL_PCH2,    "Intel 5 Series/3400 Series",	0 },
510 	{ HDA_INTEL_SCH,     "Intel SCH",	0 },
511 	{ HDA_NVIDIA_MCP51,  "NVidia MCP51",	HDAC_NO_MSI },
512 	{ HDA_NVIDIA_MCP55,  "NVidia MCP55",	HDAC_NO_MSI },
513 	{ HDA_NVIDIA_MCP61_1, "NVidia MCP61",	0 },
514 	{ HDA_NVIDIA_MCP61_2, "NVidia MCP61",	0 },
515 	{ HDA_NVIDIA_MCP65_1, "NVidia MCP65",	0 },
516 	{ HDA_NVIDIA_MCP65_2, "NVidia MCP65",	0 },
517 	{ HDA_NVIDIA_MCP67_1, "NVidia MCP67",	0 },
518 	{ HDA_NVIDIA_MCP67_2, "NVidia MCP67",	0 },
519 	{ HDA_NVIDIA_MCP73_1, "NVidia MCP73",	0 },
520 	{ HDA_NVIDIA_MCP73_2, "NVidia MCP73",	0 },
521 	{ HDA_NVIDIA_MCP78_1, "NVidia MCP78",	HDAC_NO_64BIT },
522 	{ HDA_NVIDIA_MCP78_2, "NVidia MCP78",	HDAC_NO_64BIT },
523 	{ HDA_NVIDIA_MCP78_3, "NVidia MCP78",	HDAC_NO_64BIT },
524 	{ HDA_NVIDIA_MCP78_4, "NVidia MCP78",	HDAC_NO_64BIT },
525 	{ HDA_NVIDIA_MCP79_1, "NVidia MCP79",	0 },
526 	{ HDA_NVIDIA_MCP79_2, "NVidia MCP79",	0 },
527 	{ HDA_NVIDIA_MCP79_3, "NVidia MCP79",	0 },
528 	{ HDA_NVIDIA_MCP79_4, "NVidia MCP79",	0 },
529 	{ HDA_NVIDIA_MCP89_1, "NVidia MCP89",	0 },
530 	{ HDA_NVIDIA_MCP89_2, "NVidia MCP89",	0 },
531 	{ HDA_NVIDIA_MCP89_3, "NVidia MCP89",	0 },
532 	{ HDA_NVIDIA_MCP89_4, "NVidia MCP89",	0 },
533 	{ HDA_ATI_SB450,     "ATI SB450",	0 },
534 	{ HDA_ATI_SB600,     "ATI SB600",	0 },
535 	{ HDA_ATI_RS600,     "ATI RS600",	0 },
536 	{ HDA_ATI_RS690,     "ATI RS690",	0 },
537 	{ HDA_ATI_RS780,     "ATI RS780",	0 },
538 	{ HDA_ATI_R600,      "ATI R600",	0 },
539 	{ HDA_ATI_RV610,     "ATI RV610",	0 },
540 	{ HDA_ATI_RV620,     "ATI RV620",	0 },
541 	{ HDA_ATI_RV630,     "ATI RV630",	0 },
542 	{ HDA_ATI_RV635,     "ATI RV635",	0 },
543 	{ HDA_ATI_RV710,     "ATI RV710",	0 },
544 	{ HDA_ATI_RV730,     "ATI RV730",	0 },
545 	{ HDA_ATI_RV740,     "ATI RV740",	0 },
546 	{ HDA_ATI_RV770,     "ATI RV770",	0 },
547 	{ HDA_RDC_M3010,     "RDC M3010",	0 },
548 	{ HDA_VIA_VT82XX,    "VIA VT8251/8237A",0 },
549 	{ HDA_SIS_966,       "SiS 966",		0 },
550 	{ HDA_ULI_M5461,     "ULI M5461",	0 },
551 	/* Unknown */
552 	{ HDA_INTEL_ALL,  "Intel (Unknown)"  },
553 	{ HDA_NVIDIA_ALL, "NVidia (Unknown)" },
554 	{ HDA_ATI_ALL,    "ATI (Unknown)"    },
555 	{ HDA_VIA_ALL,    "VIA (Unknown)"    },
556 	{ HDA_SIS_ALL,    "SiS (Unknown)"    },
557 	{ HDA_ULI_ALL,    "ULI (Unknown)"    },
558 };
559 #define HDAC_DEVICES_LEN (sizeof(hdac_devices) / sizeof(hdac_devices[0]))
560 
561 static const struct {
562 	uint16_t vendor;
563 	uint8_t reg;
564 	uint8_t mask;
565 	uint8_t enable;
566 } hdac_pcie_snoop[] = {
567 	{  INTEL_VENDORID, 0x00, 0x00, 0x00 },
568 	{    ATI_VENDORID, 0x42, 0xf8, 0x02 },
569 	{ NVIDIA_VENDORID, 0x4e, 0xf0, 0x0f },
570 };
571 #define HDAC_PCIESNOOP_LEN	\
572 			(sizeof(hdac_pcie_snoop) / sizeof(hdac_pcie_snoop[0]))
573 
574 static const struct {
575 	uint32_t	rate;
576 	int		valid;
577 	uint16_t	base;
578 	uint16_t	mul;
579 	uint16_t	div;
580 } hda_rate_tab[] = {
581 	{   8000, 1, 0x0000, 0x0000, 0x0500 },	/* (48000 * 1) / 6 */
582 	{   9600, 0, 0x0000, 0x0000, 0x0400 },	/* (48000 * 1) / 5 */
583 	{  12000, 0, 0x0000, 0x0000, 0x0300 },	/* (48000 * 1) / 4 */
584 	{  16000, 1, 0x0000, 0x0000, 0x0200 },	/* (48000 * 1) / 3 */
585 	{  18000, 0, 0x0000, 0x1000, 0x0700 },	/* (48000 * 3) / 8 */
586 	{  19200, 0, 0x0000, 0x0800, 0x0400 },	/* (48000 * 2) / 5 */
587 	{  24000, 0, 0x0000, 0x0000, 0x0100 },	/* (48000 * 1) / 2 */
588 	{  28800, 0, 0x0000, 0x1000, 0x0400 },	/* (48000 * 3) / 5 */
589 	{  32000, 1, 0x0000, 0x0800, 0x0200 },	/* (48000 * 2) / 3 */
590 	{  36000, 0, 0x0000, 0x1000, 0x0300 },	/* (48000 * 3) / 4 */
591 	{  38400, 0, 0x0000, 0x1800, 0x0400 },	/* (48000 * 4) / 5 */
592 	{  48000, 1, 0x0000, 0x0000, 0x0000 },	/* (48000 * 1) / 1 */
593 	{  64000, 0, 0x0000, 0x1800, 0x0200 },	/* (48000 * 4) / 3 */
594 	{  72000, 0, 0x0000, 0x1000, 0x0100 },	/* (48000 * 3) / 2 */
595 	{  96000, 1, 0x0000, 0x0800, 0x0000 },	/* (48000 * 2) / 1 */
596 	{ 144000, 0, 0x0000, 0x1000, 0x0000 },	/* (48000 * 3) / 1 */
597 	{ 192000, 1, 0x0000, 0x1800, 0x0000 },	/* (48000 * 4) / 1 */
598 	{   8820, 0, 0x4000, 0x0000, 0x0400 },	/* (44100 * 1) / 5 */
599 	{  11025, 1, 0x4000, 0x0000, 0x0300 },	/* (44100 * 1) / 4 */
600 	{  12600, 0, 0x4000, 0x0800, 0x0600 },	/* (44100 * 2) / 7 */
601 	{  14700, 0, 0x4000, 0x0000, 0x0200 },	/* (44100 * 1) / 3 */
602 	{  17640, 0, 0x4000, 0x0800, 0x0400 },	/* (44100 * 2) / 5 */
603 	{  18900, 0, 0x4000, 0x1000, 0x0600 },	/* (44100 * 3) / 7 */
604 	{  22050, 1, 0x4000, 0x0000, 0x0100 },	/* (44100 * 1) / 2 */
605 	{  25200, 0, 0x4000, 0x1800, 0x0600 },	/* (44100 * 4) / 7 */
606 	{  26460, 0, 0x4000, 0x1000, 0x0400 },	/* (44100 * 3) / 5 */
607 	{  29400, 0, 0x4000, 0x0800, 0x0200 },	/* (44100 * 2) / 3 */
608 	{  33075, 0, 0x4000, 0x1000, 0x0300 },	/* (44100 * 3) / 4 */
609 	{  35280, 0, 0x4000, 0x1800, 0x0400 },	/* (44100 * 4) / 5 */
610 	{  44100, 1, 0x4000, 0x0000, 0x0000 },	/* (44100 * 1) / 1 */
611 	{  58800, 0, 0x4000, 0x1800, 0x0200 },	/* (44100 * 4) / 3 */
612 	{  66150, 0, 0x4000, 0x1000, 0x0100 },	/* (44100 * 3) / 2 */
613 	{  88200, 1, 0x4000, 0x0800, 0x0000 },	/* (44100 * 2) / 1 */
614 	{ 132300, 0, 0x4000, 0x1000, 0x0000 },	/* (44100 * 3) / 1 */
615 	{ 176400, 1, 0x4000, 0x1800, 0x0000 },	/* (44100 * 4) / 1 */
616 };
617 #define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
618 
619 /* All codecs you can eat... */
620 #define HDA_CODEC_CONSTRUCT(vendor, id) \
621 		(((uint32_t)(vendor##_VENDORID) << 16) | ((id) & 0xffff))
622 
623 /* Cirrus Logic */
624 #define CIRRUSLOGIC_VENDORID	0x1013
625 #define HDA_CODEC_CS4206	HDA_CODEC_CONSTRUCT(CIRRUSLOGIC, 0x4206)
626 #define HDA_CODEC_CS4207	HDA_CODEC_CONSTRUCT(CIRRUSLOGIC, 0x4207)
627 #define HDA_CODEC_CSXXXX	HDA_CODEC_CONSTRUCT(CIRRUSLOGIC, 0xffff)
628 
629 /* Realtek */
630 #define REALTEK_VENDORID	0x10ec
631 #define HDA_CODEC_ALC260	HDA_CODEC_CONSTRUCT(REALTEK, 0x0260)
632 #define HDA_CODEC_ALC262	HDA_CODEC_CONSTRUCT(REALTEK, 0x0262)
633 #define HDA_CODEC_ALC267	HDA_CODEC_CONSTRUCT(REALTEK, 0x0267)
634 #define HDA_CODEC_ALC268	HDA_CODEC_CONSTRUCT(REALTEK, 0x0268)
635 #define HDA_CODEC_ALC269	HDA_CODEC_CONSTRUCT(REALTEK, 0x0269)
636 #define HDA_CODEC_ALC270	HDA_CODEC_CONSTRUCT(REALTEK, 0x0270)
637 #define HDA_CODEC_ALC272	HDA_CODEC_CONSTRUCT(REALTEK, 0x0272)
638 #define HDA_CODEC_ALC273	HDA_CODEC_CONSTRUCT(REALTEK, 0x0273)
639 #define HDA_CODEC_ALC275	HDA_CODEC_CONSTRUCT(REALTEK, 0x0275)
640 #define HDA_CODEC_ALC660	HDA_CODEC_CONSTRUCT(REALTEK, 0x0660)
641 #define HDA_CODEC_ALC662	HDA_CODEC_CONSTRUCT(REALTEK, 0x0662)
642 #define HDA_CODEC_ALC663	HDA_CODEC_CONSTRUCT(REALTEK, 0x0663)
643 #define HDA_CODEC_ALC665	HDA_CODEC_CONSTRUCT(REALTEK, 0x0665)
644 #define HDA_CODEC_ALC861	HDA_CODEC_CONSTRUCT(REALTEK, 0x0861)
645 #define HDA_CODEC_ALC861VD	HDA_CODEC_CONSTRUCT(REALTEK, 0x0862)
646 #define HDA_CODEC_ALC880	HDA_CODEC_CONSTRUCT(REALTEK, 0x0880)
647 #define HDA_CODEC_ALC882	HDA_CODEC_CONSTRUCT(REALTEK, 0x0882)
648 #define HDA_CODEC_ALC883	HDA_CODEC_CONSTRUCT(REALTEK, 0x0883)
649 #define HDA_CODEC_ALC885	HDA_CODEC_CONSTRUCT(REALTEK, 0x0885)
650 #define HDA_CODEC_ALC887	HDA_CODEC_CONSTRUCT(REALTEK, 0x0887)
651 #define HDA_CODEC_ALC888	HDA_CODEC_CONSTRUCT(REALTEK, 0x0888)
652 #define HDA_CODEC_ALC889	HDA_CODEC_CONSTRUCT(REALTEK, 0x0889)
653 #define HDA_CODEC_ALC892	HDA_CODEC_CONSTRUCT(REALTEK, 0x0892)
654 #define HDA_CODEC_ALCXXXX	HDA_CODEC_CONSTRUCT(REALTEK, 0xffff)
655 
656 /* Analog Devices */
657 #define ANALOGDEVICES_VENDORID	0x11d4
658 #define HDA_CODEC_AD1884A	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x184a)
659 #define HDA_CODEC_AD1882	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1882)
660 #define HDA_CODEC_AD1883	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1883)
661 #define HDA_CODEC_AD1884	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1884)
662 #define HDA_CODEC_AD1984A	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x194a)
663 #define HDA_CODEC_AD1984B	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x194b)
664 #define HDA_CODEC_AD1981HD	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1981)
665 #define HDA_CODEC_AD1983	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1983)
666 #define HDA_CODEC_AD1984	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1984)
667 #define HDA_CODEC_AD1986A	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1986)
668 #define HDA_CODEC_AD1987	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1987)
669 #define HDA_CODEC_AD1988	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1988)
670 #define HDA_CODEC_AD1988B	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x198b)
671 #define HDA_CODEC_AD1882A	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x882a)
672 #define HDA_CODEC_AD1989B	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x989b)
673 #define HDA_CODEC_ADXXXX	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0xffff)
674 
675 /* CMedia */
676 #define CMEDIA_VENDORID		0x434d
677 #define HDA_CODEC_CMI9880	HDA_CODEC_CONSTRUCT(CMEDIA, 0x4980)
678 #define HDA_CODEC_CMIXXXX	HDA_CODEC_CONSTRUCT(CMEDIA, 0xffff)
679 
680 /* Sigmatel */
681 #define SIGMATEL_VENDORID	0x8384
682 #define HDA_CODEC_STAC9230X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7612)
683 #define HDA_CODEC_STAC9230D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7613)
684 #define HDA_CODEC_STAC9229X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7614)
685 #define HDA_CODEC_STAC9229D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7615)
686 #define HDA_CODEC_STAC9228X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7616)
687 #define HDA_CODEC_STAC9228D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7617)
688 #define HDA_CODEC_STAC9227X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7618)
689 #define HDA_CODEC_STAC9227D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7619)
690 #define HDA_CODEC_STAC9274	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7620)
691 #define HDA_CODEC_STAC9274D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7621)
692 #define HDA_CODEC_STAC9273X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7622)
693 #define HDA_CODEC_STAC9273D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7623)
694 #define HDA_CODEC_STAC9272X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7624)
695 #define HDA_CODEC_STAC9272D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7625)
696 #define HDA_CODEC_STAC9271X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7626)
697 #define HDA_CODEC_STAC9271D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7627)
698 #define HDA_CODEC_STAC9274X5NH	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7628)
699 #define HDA_CODEC_STAC9274D5NH	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7629)
700 #define HDA_CODEC_STAC9250	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7634)
701 #define HDA_CODEC_STAC9251	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7636)
702 #define HDA_CODEC_IDT92HD700X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7638)
703 #define HDA_CODEC_IDT92HD700D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7639)
704 #define HDA_CODEC_IDT92HD206X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7645)
705 #define HDA_CODEC_IDT92HD206D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7646)
706 #define HDA_CODEC_CXD9872RDK	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7661)
707 #define HDA_CODEC_STAC9872AK	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7662)
708 #define HDA_CODEC_CXD9872AKD	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7664)
709 #define HDA_CODEC_STAC9221	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7680)
710 #define HDA_CODEC_STAC922XD	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7681)
711 #define HDA_CODEC_STAC9221_A2	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7682)
712 #define HDA_CODEC_STAC9221D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7683)
713 #define HDA_CODEC_STAC9220	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7690)
714 #define HDA_CODEC_STAC9200D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7691)
715 #define HDA_CODEC_IDT92HD005	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7698)
716 #define HDA_CODEC_IDT92HD005D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7699)
717 #define HDA_CODEC_STAC9205X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x76a0)
718 #define HDA_CODEC_STAC9205D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x76a1)
719 #define HDA_CODEC_STAC9204X	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x76a2)
720 #define HDA_CODEC_STAC9204D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x76a3)
721 #define HDA_CODEC_STAC9220_A2	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7880)
722 #define HDA_CODEC_STAC9220_A1	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7882)
723 #define HDA_CODEC_STACXXXX	HDA_CODEC_CONSTRUCT(SIGMATEL, 0xffff)
724 
725 /* IDT */
726 #define IDT_VENDORID		0x111d
727 #define HDA_CODEC_IDT92HD75BX	HDA_CODEC_CONSTRUCT(IDT, 0x7603)
728 #define HDA_CODEC_IDT92HD83C1X	HDA_CODEC_CONSTRUCT(IDT, 0x7604)
729 #define HDA_CODEC_IDT92HD81B1X	HDA_CODEC_CONSTRUCT(IDT, 0x7605)
730 #define HDA_CODEC_IDT92HD75B3	HDA_CODEC_CONSTRUCT(IDT, 0x7608)
731 #define HDA_CODEC_IDT92HD73D1	HDA_CODEC_CONSTRUCT(IDT, 0x7674)
732 #define HDA_CODEC_IDT92HD73C1	HDA_CODEC_CONSTRUCT(IDT, 0x7675)
733 #define HDA_CODEC_IDT92HD73E1	HDA_CODEC_CONSTRUCT(IDT, 0x7676)
734 #define HDA_CODEC_IDT92HD71B8	HDA_CODEC_CONSTRUCT(IDT, 0x76b0)
735 #define HDA_CODEC_IDT92HD71B7	HDA_CODEC_CONSTRUCT(IDT, 0x76b2)
736 #define HDA_CODEC_IDT92HD71B5	HDA_CODEC_CONSTRUCT(IDT, 0x76b6)
737 #define HDA_CODEC_IDT92HD83C1C	HDA_CODEC_CONSTRUCT(IDT, 0x76d4)
738 #define HDA_CODEC_IDT92HD81B1C	HDA_CODEC_CONSTRUCT(IDT, 0x76d5)
739 #define HDA_CODEC_IDTXXXX	HDA_CODEC_CONSTRUCT(IDT, 0xffff)
740 
741 /* Silicon Image */
742 #define SII_VENDORID	0x1095
743 #define HDA_CODEC_SII1390	HDA_CODEC_CONSTRUCT(SII, 0x1390)
744 #define HDA_CODEC_SII1392	HDA_CODEC_CONSTRUCT(SII, 0x1392)
745 #define HDA_CODEC_SIIXXXX	HDA_CODEC_CONSTRUCT(SII, 0xffff)
746 
747 /* Lucent/Agere */
748 #define AGERE_VENDORID	0x11c1
749 #define HDA_CODEC_AGEREXXXX	HDA_CODEC_CONSTRUCT(AGERE, 0xffff)
750 
751 /* Conexant */
752 #define CONEXANT_VENDORID	0x14f1
753 #define HDA_CODEC_CX20549	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5045)
754 #define HDA_CODEC_CX20551	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5047)
755 #define HDA_CODEC_CX20561	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5051)
756 #define HDA_CODEC_CX20582	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5066)
757 #define HDA_CODEC_CX20583	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5067)
758 #define HDA_CODEC_CX20584	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5068)
759 #define HDA_CODEC_CX20585	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5069)
760 #define HDA_CODEC_CX20590	HDA_CODEC_CONSTRUCT(CONEXANT, 0x506e)
761 #define HDA_CODEC_CX20631	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5097)
762 #define HDA_CODEC_CX20632	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5098)
763 #define HDA_CODEC_CX20641	HDA_CODEC_CONSTRUCT(CONEXANT, 0x50a1)
764 #define HDA_CODEC_CX20642	HDA_CODEC_CONSTRUCT(CONEXANT, 0x50a2)
765 #define HDA_CODEC_CX20651	HDA_CODEC_CONSTRUCT(CONEXANT, 0x50ab)
766 #define HDA_CODEC_CX20652	HDA_CODEC_CONSTRUCT(CONEXANT, 0x50ac)
767 #define HDA_CODEC_CX20664	HDA_CODEC_CONSTRUCT(CONEXANT, 0x50b8)
768 #define HDA_CODEC_CX20665	HDA_CODEC_CONSTRUCT(CONEXANT, 0x50b9)
769 #define HDA_CODEC_CXXXXX	HDA_CODEC_CONSTRUCT(CONEXANT, 0xffff)
770 
771 /* VIA */
772 #define HDA_CODEC_VT1708_8	HDA_CODEC_CONSTRUCT(VIA, 0x1708)
773 #define HDA_CODEC_VT1708_9	HDA_CODEC_CONSTRUCT(VIA, 0x1709)
774 #define HDA_CODEC_VT1708_A	HDA_CODEC_CONSTRUCT(VIA, 0x170a)
775 #define HDA_CODEC_VT1708_B	HDA_CODEC_CONSTRUCT(VIA, 0x170b)
776 #define HDA_CODEC_VT1709_0	HDA_CODEC_CONSTRUCT(VIA, 0xe710)
777 #define HDA_CODEC_VT1709_1	HDA_CODEC_CONSTRUCT(VIA, 0xe711)
778 #define HDA_CODEC_VT1709_2	HDA_CODEC_CONSTRUCT(VIA, 0xe712)
779 #define HDA_CODEC_VT1709_3	HDA_CODEC_CONSTRUCT(VIA, 0xe713)
780 #define HDA_CODEC_VT1709_4	HDA_CODEC_CONSTRUCT(VIA, 0xe714)
781 #define HDA_CODEC_VT1709_5	HDA_CODEC_CONSTRUCT(VIA, 0xe715)
782 #define HDA_CODEC_VT1709_6	HDA_CODEC_CONSTRUCT(VIA, 0xe716)
783 #define HDA_CODEC_VT1709_7	HDA_CODEC_CONSTRUCT(VIA, 0xe717)
784 #define HDA_CODEC_VT1708B_0	HDA_CODEC_CONSTRUCT(VIA, 0xe720)
785 #define HDA_CODEC_VT1708B_1	HDA_CODEC_CONSTRUCT(VIA, 0xe721)
786 #define HDA_CODEC_VT1708B_2	HDA_CODEC_CONSTRUCT(VIA, 0xe722)
787 #define HDA_CODEC_VT1708B_3	HDA_CODEC_CONSTRUCT(VIA, 0xe723)
788 #define HDA_CODEC_VT1708B_4	HDA_CODEC_CONSTRUCT(VIA, 0xe724)
789 #define HDA_CODEC_VT1708B_5	HDA_CODEC_CONSTRUCT(VIA, 0xe725)
790 #define HDA_CODEC_VT1708B_6	HDA_CODEC_CONSTRUCT(VIA, 0xe726)
791 #define HDA_CODEC_VT1708B_7	HDA_CODEC_CONSTRUCT(VIA, 0xe727)
792 #define HDA_CODEC_VT1708S_0	HDA_CODEC_CONSTRUCT(VIA, 0x0397)
793 #define HDA_CODEC_VT1708S_1	HDA_CODEC_CONSTRUCT(VIA, 0x1397)
794 #define HDA_CODEC_VT1708S_2	HDA_CODEC_CONSTRUCT(VIA, 0x2397)
795 #define HDA_CODEC_VT1708S_3	HDA_CODEC_CONSTRUCT(VIA, 0x3397)
796 #define HDA_CODEC_VT1708S_4	HDA_CODEC_CONSTRUCT(VIA, 0x4397)
797 #define HDA_CODEC_VT1708S_5	HDA_CODEC_CONSTRUCT(VIA, 0x5397)
798 #define HDA_CODEC_VT1708S_6	HDA_CODEC_CONSTRUCT(VIA, 0x6397)
799 #define HDA_CODEC_VT1708S_7	HDA_CODEC_CONSTRUCT(VIA, 0x7397)
800 #define HDA_CODEC_VT1702_0	HDA_CODEC_CONSTRUCT(VIA, 0x0398)
801 #define HDA_CODEC_VT1702_1	HDA_CODEC_CONSTRUCT(VIA, 0x1398)
802 #define HDA_CODEC_VT1702_2	HDA_CODEC_CONSTRUCT(VIA, 0x2398)
803 #define HDA_CODEC_VT1702_3	HDA_CODEC_CONSTRUCT(VIA, 0x3398)
804 #define HDA_CODEC_VT1702_4	HDA_CODEC_CONSTRUCT(VIA, 0x4398)
805 #define HDA_CODEC_VT1702_5	HDA_CODEC_CONSTRUCT(VIA, 0x5398)
806 #define HDA_CODEC_VT1702_6	HDA_CODEC_CONSTRUCT(VIA, 0x6398)
807 #define HDA_CODEC_VT1702_7	HDA_CODEC_CONSTRUCT(VIA, 0x7398)
808 #define HDA_CODEC_VT1716S_0	HDA_CODEC_CONSTRUCT(VIA, 0x0433)
809 #define HDA_CODEC_VT1716S_1	HDA_CODEC_CONSTRUCT(VIA, 0xa721)
810 #define HDA_CODEC_VT1718S_0	HDA_CODEC_CONSTRUCT(VIA, 0x0428)
811 #define HDA_CODEC_VT1718S_1	HDA_CODEC_CONSTRUCT(VIA, 0x4428)
812 #define HDA_CODEC_VT1812	HDA_CODEC_CONSTRUCT(VIA, 0x0448)
813 #define HDA_CODEC_VT1818S	HDA_CODEC_CONSTRUCT(VIA, 0x0440)
814 #define HDA_CODEC_VT1828S	HDA_CODEC_CONSTRUCT(VIA, 0x4441)
815 #define HDA_CODEC_VT2002P_0	HDA_CODEC_CONSTRUCT(VIA, 0x0438)
816 #define HDA_CODEC_VT2002P_1	HDA_CODEC_CONSTRUCT(VIA, 0x4438)
817 #define HDA_CODEC_VT2020	HDA_CODEC_CONSTRUCT(VIA, 0x0441)
818 #define HDA_CODEC_VTXXXX	HDA_CODEC_CONSTRUCT(VIA, 0xffff)
819 
820 /* ATI */
821 #define HDA_CODEC_ATIRS600_1	HDA_CODEC_CONSTRUCT(ATI, 0x793c)
822 #define HDA_CODEC_ATIRS600_2	HDA_CODEC_CONSTRUCT(ATI, 0x7919)
823 #define HDA_CODEC_ATIRS690	HDA_CODEC_CONSTRUCT(ATI, 0x791a)
824 #define HDA_CODEC_ATIR6XX	HDA_CODEC_CONSTRUCT(ATI, 0xaa01)
825 #define HDA_CODEC_ATIXXXX	HDA_CODEC_CONSTRUCT(ATI, 0xffff)
826 
827 /* NVIDIA */
828 #define HDA_CODEC_NVIDIAMCP78	HDA_CODEC_CONSTRUCT(NVIDIA, 0x0002)
829 #define HDA_CODEC_NVIDIAMCP78_2	HDA_CODEC_CONSTRUCT(NVIDIA, 0x0006)
830 #define HDA_CODEC_NVIDIAMCP7A	HDA_CODEC_CONSTRUCT(NVIDIA, 0x0007)
831 #define HDA_CODEC_NVIDIAGT220	HDA_CODEC_CONSTRUCT(NVIDIA, 0x000a)
832 #define HDA_CODEC_NVIDIAGT21X	HDA_CODEC_CONSTRUCT(NVIDIA, 0x000b)
833 #define HDA_CODEC_NVIDIAMCP89	HDA_CODEC_CONSTRUCT(NVIDIA, 0x000c)
834 #define HDA_CODEC_NVIDIAGT240	HDA_CODEC_CONSTRUCT(NVIDIA, 0x000d)
835 #define HDA_CODEC_NVIDIAMCP67	HDA_CODEC_CONSTRUCT(NVIDIA, 0x0067)
836 #define HDA_CODEC_NVIDIAMCP73	HDA_CODEC_CONSTRUCT(NVIDIA, 0x8001)
837 #define HDA_CODEC_NVIDIAXXXX	HDA_CODEC_CONSTRUCT(NVIDIA, 0xffff)
838 
839 /* INTEL */
840 #define HDA_CODEC_INTELIP	HDA_CODEC_CONSTRUCT(INTEL, 0x0054)
841 #define HDA_CODEC_INTELBL	HDA_CODEC_CONSTRUCT(INTEL, 0x2801)
842 #define HDA_CODEC_INTELCA	HDA_CODEC_CONSTRUCT(INTEL, 0x2802)
843 #define HDA_CODEC_INTELEL	HDA_CODEC_CONSTRUCT(INTEL, 0x2803)
844 #define HDA_CODEC_INTELIP2	HDA_CODEC_CONSTRUCT(INTEL, 0x2804)
845 #define HDA_CODEC_INTELCPT	HDA_CODEC_CONSTRUCT(INTEL, 0x2805)
846 #define HDA_CODEC_INTELCL	HDA_CODEC_CONSTRUCT(INTEL, 0x29fb)
847 #define HDA_CODEC_INTELXXXX	HDA_CODEC_CONSTRUCT(INTEL, 0xffff)
848 
849 /* Codecs */
850 static const struct {
851 	uint32_t id;
852 	char *name;
853 } hdac_codecs[] = {
854 	{ HDA_CODEC_CS4206,    "Cirrus Logic CS4206" },
855 	{ HDA_CODEC_CS4207,    "Cirrus Logic CS4207" },
856 	{ HDA_CODEC_ALC260,    "Realtek ALC260" },
857 	{ HDA_CODEC_ALC262,    "Realtek ALC262" },
858 	{ HDA_CODEC_ALC267,    "Realtek ALC267" },
859 	{ HDA_CODEC_ALC268,    "Realtek ALC268" },
860 	{ HDA_CODEC_ALC269,    "Realtek ALC269" },
861 	{ HDA_CODEC_ALC270,    "Realtek ALC270" },
862 	{ HDA_CODEC_ALC272,    "Realtek ALC272" },
863 	{ HDA_CODEC_ALC273,    "Realtek ALC273" },
864 	{ HDA_CODEC_ALC275,    "Realtek ALC275" },
865 	{ HDA_CODEC_ALC660,    "Realtek ALC660" },
866 	{ HDA_CODEC_ALC662,    "Realtek ALC662" },
867 	{ HDA_CODEC_ALC663,    "Realtek ALC663" },
868 	{ HDA_CODEC_ALC665,    "Realtek ALC665" },
869 	{ HDA_CODEC_ALC861,    "Realtek ALC861" },
870 	{ HDA_CODEC_ALC861VD,  "Realtek ALC861-VD" },
871 	{ HDA_CODEC_ALC880,    "Realtek ALC880" },
872 	{ HDA_CODEC_ALC882,    "Realtek ALC882" },
873 	{ HDA_CODEC_ALC883,    "Realtek ALC883" },
874 	{ HDA_CODEC_ALC885,    "Realtek ALC885" },
875 	{ HDA_CODEC_ALC887,    "Realtek ALC887" },
876 	{ HDA_CODEC_ALC888,    "Realtek ALC888" },
877 	{ HDA_CODEC_ALC889,    "Realtek ALC889" },
878 	{ HDA_CODEC_ALC892,    "Realtek ALC892" },
879 	{ HDA_CODEC_AD1882,    "Analog Devices AD1882" },
880 	{ HDA_CODEC_AD1882A,   "Analog Devices AD1882A" },
881 	{ HDA_CODEC_AD1883,    "Analog Devices AD1883" },
882 	{ HDA_CODEC_AD1884,    "Analog Devices AD1884" },
883 	{ HDA_CODEC_AD1884A,   "Analog Devices AD1884A" },
884 	{ HDA_CODEC_AD1981HD,  "Analog Devices AD1981HD" },
885 	{ HDA_CODEC_AD1983,    "Analog Devices AD1983" },
886 	{ HDA_CODEC_AD1984,    "Analog Devices AD1984" },
887 	{ HDA_CODEC_AD1984A,   "Analog Devices AD1984A" },
888 	{ HDA_CODEC_AD1984B,   "Analog Devices AD1984B" },
889 	{ HDA_CODEC_AD1986A,   "Analog Devices AD1986A" },
890 	{ HDA_CODEC_AD1987,    "Analog Devices AD1987" },
891 	{ HDA_CODEC_AD1988,    "Analog Devices AD1988A" },
892 	{ HDA_CODEC_AD1988B,   "Analog Devices AD1988B" },
893 	{ HDA_CODEC_AD1989B,   "Analog Devices AD1989B" },
894 	{ HDA_CODEC_CMI9880,   "CMedia CMI9880" },
895 	{ HDA_CODEC_CXD9872RDK, "Sigmatel CXD9872RD/K" },
896 	{ HDA_CODEC_CXD9872AKD, "Sigmatel CXD9872AKD" },
897 	{ HDA_CODEC_STAC9200D, "Sigmatel STAC9200D" },
898 	{ HDA_CODEC_STAC9204X, "Sigmatel STAC9204X" },
899 	{ HDA_CODEC_STAC9204D, "Sigmatel STAC9204D" },
900 	{ HDA_CODEC_STAC9205X, "Sigmatel STAC9205X" },
901 	{ HDA_CODEC_STAC9205D, "Sigmatel STAC9205D" },
902 	{ HDA_CODEC_STAC9220,  "Sigmatel STAC9220" },
903 	{ HDA_CODEC_STAC9220_A1, "Sigmatel STAC9220_A1" },
904 	{ HDA_CODEC_STAC9220_A2, "Sigmatel STAC9220_A2" },
905 	{ HDA_CODEC_STAC9221,  "Sigmatel STAC9221" },
906 	{ HDA_CODEC_STAC9221_A2, "Sigmatel STAC9221_A2" },
907 	{ HDA_CODEC_STAC9221D, "Sigmatel STAC9221D" },
908 	{ HDA_CODEC_STAC922XD, "Sigmatel STAC9220D/9223D" },
909 	{ HDA_CODEC_STAC9227X, "Sigmatel STAC9227X" },
910 	{ HDA_CODEC_STAC9227D, "Sigmatel STAC9227D" },
911 	{ HDA_CODEC_STAC9228X, "Sigmatel STAC9228X" },
912 	{ HDA_CODEC_STAC9228D, "Sigmatel STAC9228D" },
913 	{ HDA_CODEC_STAC9229X, "Sigmatel STAC9229X" },
914 	{ HDA_CODEC_STAC9229D, "Sigmatel STAC9229D" },
915 	{ HDA_CODEC_STAC9230X, "Sigmatel STAC9230X" },
916 	{ HDA_CODEC_STAC9230D, "Sigmatel STAC9230D" },
917 	{ HDA_CODEC_STAC9250,  "Sigmatel STAC9250" },
918 	{ HDA_CODEC_STAC9251,  "Sigmatel STAC9251" },
919 	{ HDA_CODEC_STAC9271X, "Sigmatel STAC9271X" },
920 	{ HDA_CODEC_STAC9271D, "Sigmatel STAC9271D" },
921 	{ HDA_CODEC_STAC9272X, "Sigmatel STAC9272X" },
922 	{ HDA_CODEC_STAC9272D, "Sigmatel STAC9272D" },
923 	{ HDA_CODEC_STAC9273X, "Sigmatel STAC9273X" },
924 	{ HDA_CODEC_STAC9273D, "Sigmatel STAC9273D" },
925 	{ HDA_CODEC_STAC9274,  "Sigmatel STAC9274" },
926 	{ HDA_CODEC_STAC9274D, "Sigmatel STAC9274D" },
927 	{ HDA_CODEC_STAC9274X5NH, "Sigmatel STAC9274X5NH" },
928 	{ HDA_CODEC_STAC9274D5NH, "Sigmatel STAC9274D5NH" },
929 	{ HDA_CODEC_STAC9872AK, "Sigmatel STAC9872AK" },
930 	{ HDA_CODEC_IDT92HD005, "IDT 92HD005" },
931 	{ HDA_CODEC_IDT92HD005D, "IDT 92HD005D" },
932 	{ HDA_CODEC_IDT92HD206X, "IDT 92HD206X" },
933 	{ HDA_CODEC_IDT92HD206D, "IDT 92HD206D" },
934 	{ HDA_CODEC_IDT92HD700X, "IDT 92HD700X" },
935 	{ HDA_CODEC_IDT92HD700D, "IDT 92HD700D" },
936 	{ HDA_CODEC_IDT92HD71B5, "IDT 92HD71B5" },
937 	{ HDA_CODEC_IDT92HD71B7, "IDT 92HD71B7" },
938 	{ HDA_CODEC_IDT92HD71B8, "IDT 92HD71B8" },
939 	{ HDA_CODEC_IDT92HD73C1, "IDT 92HD73C1" },
940 	{ HDA_CODEC_IDT92HD73D1, "IDT 92HD73D1" },
941 	{ HDA_CODEC_IDT92HD73E1, "IDT 92HD73E1" },
942 	{ HDA_CODEC_IDT92HD75B3, "IDT 92HD75B3" },
943 	{ HDA_CODEC_IDT92HD75BX, "IDT 92HD75BX" },
944 	{ HDA_CODEC_IDT92HD81B1C, "IDT 92HD81B1C" },
945 	{ HDA_CODEC_IDT92HD81B1X, "IDT 92HD81B1X" },
946 	{ HDA_CODEC_IDT92HD83C1C, "IDT 92HD83C1C" },
947 	{ HDA_CODEC_IDT92HD83C1X, "IDT 92HD83C1X" },
948 	{ HDA_CODEC_CX20549,   "Conexant CX20549 (Venice)" },
949 	{ HDA_CODEC_CX20551,   "Conexant CX20551 (Waikiki)" },
950 	{ HDA_CODEC_CX20561,   "Conexant CX20561 (Hermosa)" },
951 	{ HDA_CODEC_CX20582,   "Conexant CX20582 (Pebble)" },
952 	{ HDA_CODEC_CX20583,   "Conexant CX20583 (Pebble HSF)" },
953 	{ HDA_CODEC_CX20584,   "Conexant CX20584" },
954 	{ HDA_CODEC_CX20585,   "Conexant CX20585" },
955 	{ HDA_CODEC_CX20590,   "Conexant CX20590" },
956 	{ HDA_CODEC_CX20631,   "Conexant CX20631" },
957 	{ HDA_CODEC_CX20632,   "Conexant CX20632" },
958 	{ HDA_CODEC_CX20641,   "Conexant CX20641" },
959 	{ HDA_CODEC_CX20642,   "Conexant CX20642" },
960 	{ HDA_CODEC_CX20651,   "Conexant CX20651" },
961 	{ HDA_CODEC_CX20652,   "Conexant CX20652" },
962 	{ HDA_CODEC_CX20664,   "Conexant CX20664" },
963 	{ HDA_CODEC_CX20665,   "Conexant CX20665" },
964 	{ HDA_CODEC_VT1708_8,  "VIA VT1708_8" },
965 	{ HDA_CODEC_VT1708_9,  "VIA VT1708_9" },
966 	{ HDA_CODEC_VT1708_A,  "VIA VT1708_A" },
967 	{ HDA_CODEC_VT1708_B,  "VIA VT1708_B" },
968 	{ HDA_CODEC_VT1709_0,  "VIA VT1709_0" },
969 	{ HDA_CODEC_VT1709_1,  "VIA VT1709_1" },
970 	{ HDA_CODEC_VT1709_2,  "VIA VT1709_2" },
971 	{ HDA_CODEC_VT1709_3,  "VIA VT1709_3" },
972 	{ HDA_CODEC_VT1709_4,  "VIA VT1709_4" },
973 	{ HDA_CODEC_VT1709_5,  "VIA VT1709_5" },
974 	{ HDA_CODEC_VT1709_6,  "VIA VT1709_6" },
975 	{ HDA_CODEC_VT1709_7,  "VIA VT1709_7" },
976 	{ HDA_CODEC_VT1708B_0, "VIA VT1708B_0" },
977 	{ HDA_CODEC_VT1708B_1, "VIA VT1708B_1" },
978 	{ HDA_CODEC_VT1708B_2, "VIA VT1708B_2" },
979 	{ HDA_CODEC_VT1708B_3, "VIA VT1708B_3" },
980 	{ HDA_CODEC_VT1708B_4, "VIA VT1708B_4" },
981 	{ HDA_CODEC_VT1708B_5, "VIA VT1708B_5" },
982 	{ HDA_CODEC_VT1708B_6, "VIA VT1708B_6" },
983 	{ HDA_CODEC_VT1708B_7, "VIA VT1708B_7" },
984 	{ HDA_CODEC_VT1708S_0, "VIA VT1708S_0" },
985 	{ HDA_CODEC_VT1708S_1, "VIA VT1708S_1" },
986 	{ HDA_CODEC_VT1708S_2, "VIA VT1708S_2" },
987 	{ HDA_CODEC_VT1708S_3, "VIA VT1708S_3" },
988 	{ HDA_CODEC_VT1708S_4, "VIA VT1708S_4" },
989 	{ HDA_CODEC_VT1708S_5, "VIA VT1708S_5" },
990 	{ HDA_CODEC_VT1708S_6, "VIA VT1708S_6" },
991 	{ HDA_CODEC_VT1708S_7, "VIA VT1708S_7" },
992 	{ HDA_CODEC_VT1702_0, "VIA VT1702_0" },
993 	{ HDA_CODEC_VT1702_1, "VIA VT1702_1" },
994 	{ HDA_CODEC_VT1702_2, "VIA VT1702_2" },
995 	{ HDA_CODEC_VT1702_3, "VIA VT1702_3" },
996 	{ HDA_CODEC_VT1702_4, "VIA VT1702_4" },
997 	{ HDA_CODEC_VT1702_5, "VIA VT1702_5" },
998 	{ HDA_CODEC_VT1702_6, "VIA VT1702_6" },
999 	{ HDA_CODEC_VT1702_7, "VIA VT1702_7" },
1000 	{ HDA_CODEC_VT1716S_0, "VIA VT1716S_0" },
1001 	{ HDA_CODEC_VT1716S_1, "VIA VT1716S_1" },
1002 	{ HDA_CODEC_VT1718S_0, "VIA VT1718S_0" },
1003 	{ HDA_CODEC_VT1718S_1, "VIA VT1718S_1" },
1004 	{ HDA_CODEC_VT1812, "VIA VT1812" },
1005 	{ HDA_CODEC_VT1818S, "VIA VT1818S" },
1006 	{ HDA_CODEC_VT1828S, "VIA VT1828S" },
1007 	{ HDA_CODEC_VT2002P_0, "VIA VT2002P_0" },
1008 	{ HDA_CODEC_VT2002P_1, "VIA VT2002P_1" },
1009 	{ HDA_CODEC_VT2020, "VIA VT2020" },
1010 	{ HDA_CODEC_ATIRS600_1,"ATI RS600 HDMI" },
1011 	{ HDA_CODEC_ATIRS600_2,"ATI RS600 HDMI" },
1012 	{ HDA_CODEC_ATIRS690,  "ATI RS690/780 HDMI" },
1013 	{ HDA_CODEC_ATIR6XX,   "ATI R6xx HDMI" },
1014 	{ HDA_CODEC_NVIDIAMCP67, "NVidia MCP67 HDMI" },
1015 	{ HDA_CODEC_NVIDIAMCP73, "NVidia MCP73 HDMI" },
1016 	{ HDA_CODEC_NVIDIAMCP78, "NVidia MCP78 HDMI" },
1017 	{ HDA_CODEC_NVIDIAMCP78_2, "NVidia MCP78 HDMI" },
1018 	{ HDA_CODEC_NVIDIAMCP7A, "NVidia MCP7A HDMI" },
1019 	{ HDA_CODEC_NVIDIAGT220, "NVidia GT220 HDMI" },
1020 	{ HDA_CODEC_NVIDIAGT21X, "NVidia GT21x HDMI" },
1021 	{ HDA_CODEC_NVIDIAMCP89, "NVidia MCP89 HDMI" },
1022 	{ HDA_CODEC_NVIDIAGT240, "NVidia GT240 HDMI" },
1023 	{ HDA_CODEC_INTELIP,   "Intel Ibex Peak HDMI" },
1024 	{ HDA_CODEC_INTELBL,   "Intel Bearlake HDMI" },
1025 	{ HDA_CODEC_INTELCA,   "Intel Cantiga HDMI" },
1026 	{ HDA_CODEC_INTELEL,   "Intel Eaglelake HDMI" },
1027 	{ HDA_CODEC_INTELIP2,  "Intel Ibex Peak HDMI" },
1028 	{ HDA_CODEC_INTELCPT,  "Intel Cougar Point HDMI" },
1029 	{ HDA_CODEC_INTELCL,   "Intel Crestline HDMI" },
1030 	{ HDA_CODEC_SII1390,   "Silicon Image SiI1390 HDMI" },
1031 	{ HDA_CODEC_SII1392,   "Silicon Image SiI1392 HDMI" },
1032 	/* Unknown codec */
1033 	{ HDA_CODEC_ALCXXXX,   "Realtek (Unknown)" },
1034 	{ HDA_CODEC_ADXXXX,    "Analog Devices (Unknown)" },
1035 	{ HDA_CODEC_CSXXXX,    "Cirrus Logic (Unknown)" },
1036 	{ HDA_CODEC_CMIXXXX,   "CMedia (Unknown)" },
1037 	{ HDA_CODEC_STACXXXX,  "Sigmatel (Unknown)" },
1038 	{ HDA_CODEC_SIIXXXX,   "Silicon Image (Unknown)" },
1039 	{ HDA_CODEC_AGEREXXXX, "Lucent/Agere Systems (Unknown)" },
1040 	{ HDA_CODEC_CXXXXX,    "Conexant (Unknown)" },
1041 	{ HDA_CODEC_VTXXXX,    "VIA (Unknown)" },
1042 	{ HDA_CODEC_ATIXXXX,   "ATI (Unknown)" },
1043 	{ HDA_CODEC_NVIDIAXXXX,"NVidia (Unknown)" },
1044 	{ HDA_CODEC_INTELXXXX, "Intel (Unknown)" },
1045 	{ HDA_CODEC_IDTXXXX,   "IDT (Unknown)" },
1046 };
1047 #define HDAC_CODECS_LEN	(sizeof(hdac_codecs) / sizeof(hdac_codecs[0]))
1048 
1049 
1050 /****************************************************************************
1051  * Function prototypes
1052  ****************************************************************************/
1053 static void	hdac_intr_handler(void *);
1054 static int	hdac_reset(struct hdac_softc *, int);
1055 static int	hdac_get_capabilities(struct hdac_softc *);
1056 static void	hdac_dma_cb(void *, bus_dma_segment_t *, int, int);
1057 static int	hdac_dma_alloc(struct hdac_softc *,
1058 					struct hdac_dma *, bus_size_t);
1059 static void	hdac_dma_free(struct hdac_softc *, struct hdac_dma *);
1060 static int	hdac_mem_alloc(struct hdac_softc *);
1061 static void	hdac_mem_free(struct hdac_softc *);
1062 static int	hdac_irq_alloc(struct hdac_softc *);
1063 static void	hdac_irq_free(struct hdac_softc *);
1064 static void	hdac_corb_init(struct hdac_softc *);
1065 static void	hdac_rirb_init(struct hdac_softc *);
1066 static void	hdac_corb_start(struct hdac_softc *);
1067 static void	hdac_rirb_start(struct hdac_softc *);
1068 static void	hdac_scan_codecs(struct hdac_softc *);
1069 static void	hdac_probe_codec(struct hdac_codec *);
1070 static void	hdac_probe_function(struct hdac_codec *, nid_t);
1071 static int	hdac_pcmchannel_setup(struct hdac_chan *);
1072 
1073 static void	hdac_attach2(void *);
1074 
1075 static uint32_t	hdac_command_sendone_internal(struct hdac_softc *,
1076 							uint32_t, int);
1077 static void	hdac_command_send_internal(struct hdac_softc *,
1078 					struct hdac_command_list *, int);
1079 
1080 static int	hdac_probe(device_t);
1081 static int	hdac_attach(device_t);
1082 static int	hdac_detach(device_t);
1083 static int	hdac_suspend(device_t);
1084 static int	hdac_resume(device_t);
1085 static void	hdac_widget_connection_select(struct hdac_widget *, uint8_t);
1086 static void	hdac_audio_ctl_amp_set(struct hdac_audio_ctl *,
1087 						uint32_t, int, int);
1088 static struct	hdac_audio_ctl *hdac_audio_ctl_amp_get(struct hdac_devinfo *,
1089 							nid_t, int, int, int);
1090 static void	hdac_audio_ctl_amp_set_internal(struct hdac_softc *,
1091 				nid_t, nid_t, int, int, int, int, int, int);
1092 static struct	hdac_widget *hdac_widget_get(struct hdac_devinfo *, nid_t);
1093 
1094 static int	hdac_rirb_flush(struct hdac_softc *sc);
1095 static int	hdac_unsolq_flush(struct hdac_softc *sc);
1096 
1097 static void	hdac_dump_pin_config(struct hdac_widget *w, uint32_t conf);
1098 
1099 #define hdac_command(a1, a2, a3)	\
1100 		hdac_command_sendone_internal(a1, a2, a3)
1101 
1102 #define hdac_codec_id(c)							\
1103 		((uint32_t)((c == NULL) ? 0x00000000 :	\
1104 		((((uint32_t)(c)->vendor_id & 0x0000ffff) << 16) |	\
1105 		((uint32_t)(c)->device_id & 0x0000ffff))))
1106 
1107 static char *
1108 hdac_codec_name(struct hdac_codec *codec)
1109 {
1110 	uint32_t id;
1111 	int i;
1112 
1113 	id = hdac_codec_id(codec);
1114 
1115 	for (i = 0; i < HDAC_CODECS_LEN; i++) {
1116 		if (HDA_DEV_MATCH(hdac_codecs[i].id, id))
1117 			return (hdac_codecs[i].name);
1118 	}
1119 
1120 	return ((id == 0x00000000) ? "NULL Codec" : "Unknown Codec");
1121 }
1122 
1123 static char *
1124 hdac_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
1125 {
1126 	static char *ossname[] = SOUND_DEVICE_NAMES;
1127 	int i, first = 1;
1128 
1129 	bzero(buf, len);
1130 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1131 		if (mask & (1 << i)) {
1132 			if (first == 0)
1133 				strlcat(buf, ", ", len);
1134 			strlcat(buf, ossname[i], len);
1135 			first = 0;
1136 		}
1137 	}
1138 	return (buf);
1139 }
1140 
1141 static struct hdac_audio_ctl *
1142 hdac_audio_ctl_each(struct hdac_devinfo *devinfo, int *index)
1143 {
1144 	if (devinfo == NULL ||
1145 	    devinfo->node_type != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO ||
1146 	    index == NULL || devinfo->function.audio.ctl == NULL ||
1147 	    devinfo->function.audio.ctlcnt < 1 ||
1148 	    *index < 0 || *index >= devinfo->function.audio.ctlcnt)
1149 		return (NULL);
1150 	return (&devinfo->function.audio.ctl[(*index)++]);
1151 }
1152 
1153 static struct hdac_audio_ctl *
1154 hdac_audio_ctl_amp_get(struct hdac_devinfo *devinfo, nid_t nid, int dir,
1155 						int index, int cnt)
1156 {
1157 	struct hdac_audio_ctl *ctl;
1158 	int i, found = 0;
1159 
1160 	if (devinfo == NULL || devinfo->function.audio.ctl == NULL)
1161 		return (NULL);
1162 
1163 	i = 0;
1164 	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
1165 		if (ctl->enable == 0)
1166 			continue;
1167 		if (ctl->widget->nid != nid)
1168 			continue;
1169 		if (dir && ctl->ndir != dir)
1170 			continue;
1171 		if (index >= 0 && ctl->ndir == HDA_CTL_IN &&
1172 		    ctl->dir == ctl->ndir && ctl->index != index)
1173 			continue;
1174 		found++;
1175 		if (found == cnt || cnt <= 0)
1176 			return (ctl);
1177 	}
1178 
1179 	return (NULL);
1180 }
1181 
1182 /*
1183  * Jack detection (Speaker/HP redirection) event handler.
1184  */
1185 static void
1186 hdac_hp_switch_handler(struct hdac_devinfo *devinfo)
1187 {
1188 	struct hdac_audio_as *as;
1189 	struct hdac_softc *sc;
1190 	struct hdac_widget *w;
1191 	struct hdac_audio_ctl *ctl;
1192 	uint32_t val, res;
1193 	int i, j;
1194 	nid_t cad;
1195 
1196 	if (devinfo == NULL || devinfo->codec == NULL ||
1197 	    devinfo->codec->sc == NULL)
1198 		return;
1199 
1200 	sc = devinfo->codec->sc;
1201 	cad = devinfo->codec->cad;
1202 	as = devinfo->function.audio.as;
1203 	for (i = 0; i < devinfo->function.audio.ascnt; i++) {
1204 		if (as[i].hpredir < 0)
1205 			continue;
1206 
1207 		w = hdac_widget_get(devinfo, as[i].pins[15]);
1208 		if (w == NULL || w->enable == 0 || w->type !=
1209 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
1210 			continue;
1211 
1212 		res = hdac_command(sc,
1213 		    HDA_CMD_GET_PIN_SENSE(cad, as[i].pins[15]), cad);
1214 
1215 		HDA_BOOTVERBOSE(
1216 			device_printf(sc->dev,
1217 			    "Pin sense: nid=%d res=0x%08x\n",
1218 			    as[i].pins[15], res);
1219 		);
1220 
1221 		res = HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT(res);
1222 		if (devinfo->function.audio.quirks & HDA_QUIRK_SENSEINV)
1223 			res ^= 1;
1224 
1225 		/* (Un)Mute headphone pin. */
1226 		ctl = hdac_audio_ctl_amp_get(devinfo,
1227 		    as[i].pins[15], HDA_CTL_IN, -1, 1);
1228 		if (ctl != NULL && ctl->mute) {
1229 			/* If pin has muter - use it. */
1230 			val = (res != 0) ? 0 : 1;
1231 			if (val != ctl->forcemute) {
1232 				ctl->forcemute = val;
1233 				hdac_audio_ctl_amp_set(ctl,
1234 				    HDA_AMP_MUTE_DEFAULT,
1235 				    HDA_AMP_VOL_DEFAULT, HDA_AMP_VOL_DEFAULT);
1236 			}
1237 		} else {
1238 			/* If there is no muter - disable pin output. */
1239 			w = hdac_widget_get(devinfo, as[i].pins[15]);
1240 			if (w != NULL && w->type ==
1241 			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1242 				if (res != 0)
1243 					val = w->wclass.pin.ctrl |
1244 					    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
1245 				else
1246 					val = w->wclass.pin.ctrl &
1247 					    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
1248 				if (val != w->wclass.pin.ctrl) {
1249 					w->wclass.pin.ctrl = val;
1250 					hdac_command(sc,
1251 					    HDA_CMD_SET_PIN_WIDGET_CTRL(cad,
1252 					    w->nid, w->wclass.pin.ctrl), cad);
1253 				}
1254 			}
1255 		}
1256 		/* (Un)Mute other pins. */
1257 		for (j = 0; j < 15; j++) {
1258 			if (as[i].pins[j] <= 0)
1259 				continue;
1260 			ctl = hdac_audio_ctl_amp_get(devinfo,
1261 			    as[i].pins[j], HDA_CTL_IN, -1, 1);
1262 			if (ctl != NULL && ctl->mute) {
1263 				/* If pin has muter - use it. */
1264 				val = (res != 0) ? 1 : 0;
1265 				if (val == ctl->forcemute)
1266 					continue;
1267 				ctl->forcemute = val;
1268 				hdac_audio_ctl_amp_set(ctl,
1269 				    HDA_AMP_MUTE_DEFAULT,
1270 				    HDA_AMP_VOL_DEFAULT, HDA_AMP_VOL_DEFAULT);
1271 				continue;
1272 			}
1273 			/* If there is no muter - disable pin output. */
1274 			w = hdac_widget_get(devinfo, as[i].pins[j]);
1275 			if (w != NULL && w->type ==
1276 			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1277 				if (res != 0)
1278 					val = w->wclass.pin.ctrl &
1279 					    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
1280 				else
1281 					val = w->wclass.pin.ctrl |
1282 					    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
1283 				if (val != w->wclass.pin.ctrl) {
1284 					w->wclass.pin.ctrl = val;
1285 					hdac_command(sc,
1286 					    HDA_CMD_SET_PIN_WIDGET_CTRL(cad,
1287 					    w->nid, w->wclass.pin.ctrl), cad);
1288 				}
1289 			}
1290 		}
1291 	}
1292 }
1293 
1294 /*
1295  * Callback for poll based jack detection.
1296  */
1297 static void
1298 hdac_jack_poll_callback(void *arg)
1299 {
1300 	struct hdac_devinfo *devinfo = arg;
1301 	struct hdac_softc *sc;
1302 
1303 	if (devinfo == NULL || devinfo->codec == NULL ||
1304 	    devinfo->codec->sc == NULL)
1305 		return;
1306 	sc = devinfo->codec->sc;
1307 	hdac_lock(sc);
1308 	if (sc->poll_ival == 0) {
1309 		hdac_unlock(sc);
1310 		return;
1311 	}
1312 	hdac_hp_switch_handler(devinfo);
1313 	callout_reset(&sc->poll_jack, sc->poll_ival,
1314 	    hdac_jack_poll_callback, devinfo);
1315 	hdac_unlock(sc);
1316 }
1317 
1318 /*
1319  * Jack detection initializer.
1320  */
1321 static void
1322 hdac_hp_switch_init(struct hdac_devinfo *devinfo)
1323 {
1324         struct hdac_softc *sc = devinfo->codec->sc;
1325 	struct hdac_audio_as *as = devinfo->function.audio.as;
1326         struct hdac_widget *w;
1327         uint32_t id;
1328         int i, enable = 0, poll = 0;
1329         nid_t cad;
1330 
1331 	id = hdac_codec_id(devinfo->codec);
1332 	cad = devinfo->codec->cad;
1333 	for (i = 0; i < devinfo->function.audio.ascnt; i++) {
1334 		if (as[i].hpredir < 0)
1335 			continue;
1336 
1337 		w = hdac_widget_get(devinfo, as[i].pins[15]);
1338 		if (w == NULL || w->enable == 0 || w->type !=
1339 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
1340 			continue;
1341 		if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
1342 		    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0) {
1343 			device_printf(sc->dev,
1344 			    "No jack detection support at pin %d\n",
1345 			    as[i].pins[15]);
1346 			continue;
1347 		}
1348 		enable = 1;
1349 		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap)) {
1350 			hdac_command(sc,
1351 			    HDA_CMD_SET_UNSOLICITED_RESPONSE(cad, w->nid,
1352 			    HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE |
1353 			    HDAC_UNSOLTAG_EVENT_HP), cad);
1354 		} else
1355 			poll = 1;
1356 		HDA_BOOTVERBOSE(
1357 			device_printf(sc->dev,
1358 			    "Enabling headphone/speaker "
1359 			    "audio routing switching:\n");
1360 			device_printf(sc->dev, "\tas=%d sense nid=%d [%s]\n",
1361 			    i, w->nid, (poll != 0) ? "POLL" : "UNSOL");
1362 		);
1363 	}
1364 	if (enable) {
1365 		hdac_hp_switch_handler(devinfo);
1366 		if (poll) {
1367 			callout_reset(&sc->poll_jack, 1,
1368 			    hdac_jack_poll_callback, devinfo);
1369 		}
1370 	}
1371 }
1372 
1373 /*
1374  * Unsolicited messages handler.
1375  */
1376 static void
1377 hdac_unsolicited_handler(struct hdac_codec *codec, uint32_t tag)
1378 {
1379 	struct hdac_softc *sc;
1380 	struct hdac_devinfo *devinfo = NULL;
1381 	int i;
1382 
1383 	if (codec == NULL || codec->sc == NULL)
1384 		return;
1385 
1386 	sc = codec->sc;
1387 
1388 	HDA_BOOTVERBOSE(
1389 		device_printf(sc->dev, "Unsol Tag: 0x%08x\n", tag);
1390 	);
1391 
1392 	for (i = 0; i < codec->num_fgs; i++) {
1393 		if (codec->fgs[i].node_type ==
1394 		    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) {
1395 			devinfo = &codec->fgs[i];
1396 			break;
1397 		}
1398 	}
1399 
1400 	if (devinfo == NULL)
1401 		return;
1402 
1403 	switch (tag) {
1404 	case HDAC_UNSOLTAG_EVENT_HP:
1405 		hdac_hp_switch_handler(devinfo);
1406 		break;
1407 	default:
1408 		device_printf(sc->dev, "Unknown unsol tag: 0x%08x!\n", tag);
1409 		break;
1410 	}
1411 }
1412 
1413 static int
1414 hdac_stream_intr(struct hdac_softc *sc, struct hdac_chan *ch)
1415 {
1416 	/* XXX to be removed */
1417 #ifdef HDAC_INTR_EXTRA
1418 	uint32_t res;
1419 #endif
1420 
1421 	if (!(ch->flags & HDAC_CHN_RUNNING))
1422 		return (0);
1423 
1424 	/* XXX to be removed */
1425 #ifdef HDAC_INTR_EXTRA
1426 	res = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDSTS);
1427 #endif
1428 
1429 	/* XXX to be removed */
1430 #ifdef HDAC_INTR_EXTRA
1431 	HDA_BOOTVERBOSE(
1432 		if (res & (HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE))
1433 			device_printf(ch->pdevinfo->dev,
1434 			    "PCMDIR_%s intr triggered beyond stream boundary:"
1435 			    "%08x\n",
1436 			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC", res);
1437 	);
1438 #endif
1439 
1440 	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDSTS,
1441 	    HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE | HDAC_SDSTS_BCIS );
1442 
1443 	/* XXX to be removed */
1444 #ifdef HDAC_INTR_EXTRA
1445 	if (res & HDAC_SDSTS_BCIS) {
1446 #endif
1447 		return (1);
1448 	/* XXX to be removed */
1449 #ifdef HDAC_INTR_EXTRA
1450 	}
1451 #endif
1452 
1453 	return (0);
1454 }
1455 
1456 /****************************************************************************
1457  * void hdac_intr_handler(void *)
1458  *
1459  * Interrupt handler. Processes interrupts received from the hdac.
1460  ****************************************************************************/
1461 static void
1462 hdac_intr_handler(void *context)
1463 {
1464 	struct hdac_softc *sc;
1465 	uint32_t intsts;
1466 	uint8_t rirbsts;
1467 	struct hdac_rirb *rirb_base;
1468 	uint32_t trigger;
1469 	int i;
1470 
1471 	sc = (struct hdac_softc *)context;
1472 
1473 	hdac_lock(sc);
1474 	if (sc->polling != 0) {
1475 		hdac_unlock(sc);
1476 		return;
1477 	}
1478 
1479 	/* Do we have anything to do? */
1480 	intsts = HDAC_READ_4(&sc->mem, HDAC_INTSTS);
1481 	if (!HDA_FLAG_MATCH(intsts, HDAC_INTSTS_GIS)) {
1482 		hdac_unlock(sc);
1483 		return;
1484 	}
1485 
1486 	trigger = 0;
1487 
1488 	/* Was this a controller interrupt? */
1489 	if (HDA_FLAG_MATCH(intsts, HDAC_INTSTS_CIS)) {
1490 		rirb_base = (struct hdac_rirb *)sc->rirb_dma.dma_vaddr;
1491 		rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
1492 		/* Get as many responses that we can */
1493 		while (HDA_FLAG_MATCH(rirbsts, HDAC_RIRBSTS_RINTFL)) {
1494 			HDAC_WRITE_1(&sc->mem,
1495 			    HDAC_RIRBSTS, HDAC_RIRBSTS_RINTFL);
1496 			if (hdac_rirb_flush(sc) != 0)
1497 				trigger |= HDAC_TRIGGER_UNSOL;
1498 			rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
1499 		}
1500 		/* XXX to be removed */
1501 		/* Clear interrupt and exit */
1502 #ifdef HDAC_INTR_EXTRA
1503 		HDAC_WRITE_4(&sc->mem, HDAC_INTSTS, HDAC_INTSTS_CIS);
1504 #endif
1505 	}
1506 
1507 	if (intsts & HDAC_INTSTS_SIS_MASK) {
1508 		for (i = 0; i < sc->num_chans; i++) {
1509 			if ((intsts & (1 << (sc->chans[i].off >> 5))) &&
1510 			    hdac_stream_intr(sc, &sc->chans[i]) != 0)
1511 				trigger |= (1 << i);
1512 		}
1513 		/* XXX to be removed */
1514 #ifdef HDAC_INTR_EXTRA
1515 		HDAC_WRITE_4(&sc->mem, HDAC_INTSTS, intsts &
1516 		    HDAC_INTSTS_SIS_MASK);
1517 #endif
1518 	}
1519 
1520 	hdac_unlock(sc);
1521 
1522 	for (i = 0; i < sc->num_chans; i++) {
1523 		if (trigger & (1 << i))
1524 			chn_intr(sc->chans[i].c);
1525 	}
1526 	if (trigger & HDAC_TRIGGER_UNSOL)
1527 		taskqueue_enqueue(taskqueue_thread, &sc->unsolq_task);
1528 }
1529 
1530 /****************************************************************************
1531  * int hdac_reset(hdac_softc *, int)
1532  *
1533  * Reset the hdac to a quiescent and known state.
1534  ****************************************************************************/
1535 static int
1536 hdac_reset(struct hdac_softc *sc, int wakeup)
1537 {
1538 	uint32_t gctl;
1539 	int count, i;
1540 
1541 	/*
1542 	 * Stop all Streams DMA engine
1543 	 */
1544 	for (i = 0; i < sc->num_iss; i++)
1545 		HDAC_WRITE_4(&sc->mem, HDAC_ISDCTL(sc, i), 0x0);
1546 	for (i = 0; i < sc->num_oss; i++)
1547 		HDAC_WRITE_4(&sc->mem, HDAC_OSDCTL(sc, i), 0x0);
1548 	for (i = 0; i < sc->num_bss; i++)
1549 		HDAC_WRITE_4(&sc->mem, HDAC_BSDCTL(sc, i), 0x0);
1550 
1551 	/*
1552 	 * Stop Control DMA engines.
1553 	 */
1554 	HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, 0x0);
1555 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, 0x0);
1556 
1557 	/*
1558 	 * Reset DMA position buffer.
1559 	 */
1560 	HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE, 0x0);
1561 	HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, 0x0);
1562 
1563 	/*
1564 	 * Reset the controller. The reset must remain asserted for
1565 	 * a minimum of 100us.
1566 	 */
1567 	gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
1568 	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl & ~HDAC_GCTL_CRST);
1569 	count = 10000;
1570 	do {
1571 		gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
1572 		if (!(gctl & HDAC_GCTL_CRST))
1573 			break;
1574 		DELAY(10);
1575 	} while	(--count);
1576 	if (gctl & HDAC_GCTL_CRST) {
1577 		device_printf(sc->dev, "Unable to put hdac in reset\n");
1578 		return (ENXIO);
1579 	}
1580 
1581 	/* If wakeup is not requested - leave the controller in reset state. */
1582 	if (!wakeup)
1583 		return (0);
1584 
1585 	DELAY(100);
1586 	gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
1587 	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl | HDAC_GCTL_CRST);
1588 	count = 10000;
1589 	do {
1590 		gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
1591 		if (gctl & HDAC_GCTL_CRST)
1592 			break;
1593 		DELAY(10);
1594 	} while (--count);
1595 	if (!(gctl & HDAC_GCTL_CRST)) {
1596 		device_printf(sc->dev, "Device stuck in reset\n");
1597 		return (ENXIO);
1598 	}
1599 
1600 	/*
1601 	 * Wait for codecs to finish their own reset sequence. The delay here
1602 	 * should be of 250us but for some reasons, on it's not enough on my
1603 	 * computer. Let's use twice as much as necessary to make sure that
1604 	 * it's reset properly.
1605 	 */
1606 	DELAY(1000);
1607 
1608 	return (0);
1609 }
1610 
1611 
1612 /****************************************************************************
1613  * int hdac_get_capabilities(struct hdac_softc *);
1614  *
1615  * Retreive the general capabilities of the hdac;
1616  *	Number of Input Streams
1617  *	Number of Output Streams
1618  *	Number of bidirectional Streams
1619  *	64bit ready
1620  *	CORB and RIRB sizes
1621  ****************************************************************************/
1622 static int
1623 hdac_get_capabilities(struct hdac_softc *sc)
1624 {
1625 	uint16_t gcap;
1626 	uint8_t corbsize, rirbsize;
1627 
1628 	gcap = HDAC_READ_2(&sc->mem, HDAC_GCAP);
1629 	sc->num_iss = HDAC_GCAP_ISS(gcap);
1630 	sc->num_oss = HDAC_GCAP_OSS(gcap);
1631 	sc->num_bss = HDAC_GCAP_BSS(gcap);
1632 	sc->num_sdo = HDAC_GCAP_NSDO(gcap);
1633 	sc->support_64bit = HDA_FLAG_MATCH(gcap, HDAC_GCAP_64OK);
1634 
1635 	corbsize = HDAC_READ_1(&sc->mem, HDAC_CORBSIZE);
1636 	if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_256) ==
1637 	    HDAC_CORBSIZE_CORBSZCAP_256)
1638 		sc->corb_size = 256;
1639 	else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_16) ==
1640 	    HDAC_CORBSIZE_CORBSZCAP_16)
1641 		sc->corb_size = 16;
1642 	else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_2) ==
1643 	    HDAC_CORBSIZE_CORBSZCAP_2)
1644 		sc->corb_size = 2;
1645 	else {
1646 		device_printf(sc->dev, "%s: Invalid corb size (%x)\n",
1647 		    __func__, corbsize);
1648 		return (ENXIO);
1649 	}
1650 
1651 	rirbsize = HDAC_READ_1(&sc->mem, HDAC_RIRBSIZE);
1652 	if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_256) ==
1653 	    HDAC_RIRBSIZE_RIRBSZCAP_256)
1654 		sc->rirb_size = 256;
1655 	else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_16) ==
1656 	    HDAC_RIRBSIZE_RIRBSZCAP_16)
1657 		sc->rirb_size = 16;
1658 	else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_2) ==
1659 	    HDAC_RIRBSIZE_RIRBSZCAP_2)
1660 		sc->rirb_size = 2;
1661 	else {
1662 		device_printf(sc->dev, "%s: Invalid rirb size (%x)\n",
1663 		    __func__, rirbsize);
1664 		return (ENXIO);
1665 	}
1666 
1667 	HDA_BOOTVERBOSE(
1668 		device_printf(sc->dev, "Caps: OSS %d, ISS %d, BSS %d, "
1669 		    "NSDO %d%s, CORB %d, RIRB %d\n",
1670 		    sc->num_oss, sc->num_iss, sc->num_bss, 1 << sc->num_sdo,
1671 		    sc->support_64bit ? ", 64bit" : "",
1672 		    sc->corb_size, sc->rirb_size);
1673 	);
1674 
1675 	return (0);
1676 }
1677 
1678 
1679 /****************************************************************************
1680  * void hdac_dma_cb
1681  *
1682  * This function is called by bus_dmamap_load when the mapping has been
1683  * established. We just record the physical address of the mapping into
1684  * the struct hdac_dma passed in.
1685  ****************************************************************************/
1686 static void
1687 hdac_dma_cb(void *callback_arg, bus_dma_segment_t *segs, int nseg, int error)
1688 {
1689 	struct hdac_dma *dma;
1690 
1691 	if (error == 0) {
1692 		dma = (struct hdac_dma *)callback_arg;
1693 		dma->dma_paddr = segs[0].ds_addr;
1694 	}
1695 }
1696 
1697 
1698 /****************************************************************************
1699  * int hdac_dma_alloc
1700  *
1701  * This function allocate and setup a dma region (struct hdac_dma).
1702  * It must be freed by a corresponding hdac_dma_free.
1703  ****************************************************************************/
1704 static int
1705 hdac_dma_alloc(struct hdac_softc *sc, struct hdac_dma *dma, bus_size_t size)
1706 {
1707 	bus_size_t roundsz;
1708 	int result;
1709 
1710 	roundsz = roundup2(size, HDAC_DMA_ALIGNMENT);
1711 	bzero(dma, sizeof(*dma));
1712 
1713 	/*
1714 	 * Create a DMA tag
1715 	 */
1716 	result = bus_dma_tag_create(
1717 	    bus_get_dma_tag(sc->dev),		/* parent */
1718 	    HDAC_DMA_ALIGNMENT,			/* alignment */
1719 	    0,					/* boundary */
1720 	    (sc->support_64bit) ? BUS_SPACE_MAXADDR :
1721 		BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1722 	    BUS_SPACE_MAXADDR,			/* highaddr */
1723 	    NULL,				/* filtfunc */
1724 	    NULL,				/* fistfuncarg */
1725 	    roundsz, 				/* maxsize */
1726 	    1,					/* nsegments */
1727 	    roundsz, 				/* maxsegsz */
1728 	    0,					/* flags */
1729 	    NULL,				/* lockfunc */
1730 	    NULL,				/* lockfuncarg */
1731 	    &dma->dma_tag);			/* dmat */
1732 	if (result != 0) {
1733 		device_printf(sc->dev, "%s: bus_dma_tag_create failed (%x)\n",
1734 		    __func__, result);
1735 		goto hdac_dma_alloc_fail;
1736 	}
1737 
1738 	/*
1739 	 * Allocate DMA memory
1740 	 */
1741 	result = bus_dmamem_alloc(dma->dma_tag, (void **)&dma->dma_vaddr,
1742 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO |
1743 	    ((sc->flags & HDAC_F_DMA_NOCACHE) ? BUS_DMA_NOCACHE : 0),
1744 	    &dma->dma_map);
1745 	if (result != 0) {
1746 		device_printf(sc->dev, "%s: bus_dmamem_alloc failed (%x)\n",
1747 		    __func__, result);
1748 		goto hdac_dma_alloc_fail;
1749 	}
1750 
1751 	dma->dma_size = roundsz;
1752 
1753 	/*
1754 	 * Map the memory
1755 	 */
1756 	result = bus_dmamap_load(dma->dma_tag, dma->dma_map,
1757 	    (void *)dma->dma_vaddr, roundsz, hdac_dma_cb, (void *)dma, 0);
1758 	if (result != 0 || dma->dma_paddr == 0) {
1759 		if (result == 0)
1760 			result = ENOMEM;
1761 		device_printf(sc->dev, "%s: bus_dmamem_load failed (%x)\n",
1762 		    __func__, result);
1763 		goto hdac_dma_alloc_fail;
1764 	}
1765 
1766 	HDA_BOOTHVERBOSE(
1767 		device_printf(sc->dev, "%s: size=%ju -> roundsz=%ju\n",
1768 		    __func__, (uintmax_t)size, (uintmax_t)roundsz);
1769 	);
1770 
1771 	return (0);
1772 
1773 hdac_dma_alloc_fail:
1774 	hdac_dma_free(sc, dma);
1775 
1776 	return (result);
1777 }
1778 
1779 
1780 /****************************************************************************
1781  * void hdac_dma_free(struct hdac_softc *, struct hdac_dma *)
1782  *
1783  * Free a struct dhac_dma that has been previously allocated via the
1784  * hdac_dma_alloc function.
1785  ****************************************************************************/
1786 static void
1787 hdac_dma_free(struct hdac_softc *sc, struct hdac_dma *dma)
1788 {
1789 	if (dma->dma_map != NULL) {
1790 #if 0
1791 		/* Flush caches */
1792 		bus_dmamap_sync(dma->dma_tag, dma->dma_map,
1793 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1794 #endif
1795 		bus_dmamap_unload(dma->dma_tag, dma->dma_map);
1796 	}
1797 	if (dma->dma_vaddr != NULL) {
1798 		bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1799 		dma->dma_vaddr = NULL;
1800 	}
1801 	dma->dma_map = NULL;
1802 	if (dma->dma_tag != NULL) {
1803 		bus_dma_tag_destroy(dma->dma_tag);
1804 		dma->dma_tag = NULL;
1805 	}
1806 	dma->dma_size = 0;
1807 }
1808 
1809 /****************************************************************************
1810  * int hdac_mem_alloc(struct hdac_softc *)
1811  *
1812  * Allocate all the bus resources necessary to speak with the physical
1813  * controller.
1814  ****************************************************************************/
1815 static int
1816 hdac_mem_alloc(struct hdac_softc *sc)
1817 {
1818 	struct hdac_mem *mem;
1819 
1820 	mem = &sc->mem;
1821 	mem->mem_rid = PCIR_BAR(0);
1822 	mem->mem_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
1823 	    &mem->mem_rid, RF_ACTIVE);
1824 	if (mem->mem_res == NULL) {
1825 		device_printf(sc->dev,
1826 		    "%s: Unable to allocate memory resource\n", __func__);
1827 		return (ENOMEM);
1828 	}
1829 	mem->mem_tag = rman_get_bustag(mem->mem_res);
1830 	mem->mem_handle = rman_get_bushandle(mem->mem_res);
1831 
1832 	return (0);
1833 }
1834 
1835 /****************************************************************************
1836  * void hdac_mem_free(struct hdac_softc *)
1837  *
1838  * Free up resources previously allocated by hdac_mem_alloc.
1839  ****************************************************************************/
1840 static void
1841 hdac_mem_free(struct hdac_softc *sc)
1842 {
1843 	struct hdac_mem *mem;
1844 
1845 	mem = &sc->mem;
1846 	if (mem->mem_res != NULL)
1847 		bus_release_resource(sc->dev, SYS_RES_MEMORY, mem->mem_rid,
1848 		    mem->mem_res);
1849 	mem->mem_res = NULL;
1850 }
1851 
1852 /****************************************************************************
1853  * int hdac_irq_alloc(struct hdac_softc *)
1854  *
1855  * Allocate and setup the resources necessary for interrupt handling.
1856  ****************************************************************************/
1857 static int
1858 hdac_irq_alloc(struct hdac_softc *sc)
1859 {
1860 	struct hdac_irq *irq;
1861 	int result;
1862 
1863 	irq = &sc->irq;
1864 	irq->irq_rid = 0x0;
1865 
1866 	if ((sc->flags & HDAC_F_MSI) &&
1867 	    (result = pci_msi_count(sc->dev)) == 1 &&
1868 	    pci_alloc_msi(sc->dev, &result) == 0)
1869 		irq->irq_rid = 0x1;
1870 	else
1871 		sc->flags &= ~HDAC_F_MSI;
1872 
1873 	irq->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
1874 	    &irq->irq_rid, RF_SHAREABLE | RF_ACTIVE);
1875 	if (irq->irq_res == NULL) {
1876 		device_printf(sc->dev, "%s: Unable to allocate irq\n",
1877 		    __func__);
1878 		goto hdac_irq_alloc_fail;
1879 	}
1880 	result = bus_setup_intr(sc->dev, irq->irq_res, INTR_MPSAFE | INTR_TYPE_AV,
1881 	    NULL, hdac_intr_handler, sc, &irq->irq_handle);
1882 	if (result != 0) {
1883 		device_printf(sc->dev,
1884 		    "%s: Unable to setup interrupt handler (%x)\n",
1885 		    __func__, result);
1886 		goto hdac_irq_alloc_fail;
1887 	}
1888 
1889 	return (0);
1890 
1891 hdac_irq_alloc_fail:
1892 	hdac_irq_free(sc);
1893 
1894 	return (ENXIO);
1895 }
1896 
1897 /****************************************************************************
1898  * void hdac_irq_free(struct hdac_softc *)
1899  *
1900  * Free up resources previously allocated by hdac_irq_alloc.
1901  ****************************************************************************/
1902 static void
1903 hdac_irq_free(struct hdac_softc *sc)
1904 {
1905 	struct hdac_irq *irq;
1906 
1907 	irq = &sc->irq;
1908 	if (irq->irq_res != NULL && irq->irq_handle != NULL)
1909 		bus_teardown_intr(sc->dev, irq->irq_res, irq->irq_handle);
1910 	if (irq->irq_res != NULL)
1911 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->irq_rid,
1912 		    irq->irq_res);
1913 	if (irq->irq_rid == 0x1)
1914 		pci_release_msi(sc->dev);
1915 	irq->irq_handle = NULL;
1916 	irq->irq_res = NULL;
1917 	irq->irq_rid = 0x0;
1918 }
1919 
1920 /****************************************************************************
1921  * void hdac_corb_init(struct hdac_softc *)
1922  *
1923  * Initialize the corb registers for operations but do not start it up yet.
1924  * The CORB engine must not be running when this function is called.
1925  ****************************************************************************/
1926 static void
1927 hdac_corb_init(struct hdac_softc *sc)
1928 {
1929 	uint8_t corbsize;
1930 	uint64_t corbpaddr;
1931 
1932 	/* Setup the CORB size. */
1933 	switch (sc->corb_size) {
1934 	case 256:
1935 		corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_256);
1936 		break;
1937 	case 16:
1938 		corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_16);
1939 		break;
1940 	case 2:
1941 		corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_2);
1942 		break;
1943 	default:
1944 		panic("%s: Invalid CORB size (%x)\n", __func__, sc->corb_size);
1945 	}
1946 	HDAC_WRITE_1(&sc->mem, HDAC_CORBSIZE, corbsize);
1947 
1948 	/* Setup the CORB Address in the hdac */
1949 	corbpaddr = (uint64_t)sc->corb_dma.dma_paddr;
1950 	HDAC_WRITE_4(&sc->mem, HDAC_CORBLBASE, (uint32_t)corbpaddr);
1951 	HDAC_WRITE_4(&sc->mem, HDAC_CORBUBASE, (uint32_t)(corbpaddr >> 32));
1952 
1953 	/* Set the WP and RP */
1954 	sc->corb_wp = 0;
1955 	HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
1956 	HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, HDAC_CORBRP_CORBRPRST);
1957 	/*
1958 	 * The HDA specification indicates that the CORBRPRST bit will always
1959 	 * read as zero. Unfortunately, it seems that at least the 82801G
1960 	 * doesn't reset the bit to zero, which stalls the corb engine.
1961 	 * manually reset the bit to zero before continuing.
1962 	 */
1963 	HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, 0x0);
1964 
1965 	/* Enable CORB error reporting */
1966 #if 0
1967 	HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, HDAC_CORBCTL_CMEIE);
1968 #endif
1969 }
1970 
1971 /****************************************************************************
1972  * void hdac_rirb_init(struct hdac_softc *)
1973  *
1974  * Initialize the rirb registers for operations but do not start it up yet.
1975  * The RIRB engine must not be running when this function is called.
1976  ****************************************************************************/
1977 static void
1978 hdac_rirb_init(struct hdac_softc *sc)
1979 {
1980 	uint8_t rirbsize;
1981 	uint64_t rirbpaddr;
1982 
1983 	/* Setup the RIRB size. */
1984 	switch (sc->rirb_size) {
1985 	case 256:
1986 		rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_256);
1987 		break;
1988 	case 16:
1989 		rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_16);
1990 		break;
1991 	case 2:
1992 		rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_2);
1993 		break;
1994 	default:
1995 		panic("%s: Invalid RIRB size (%x)\n", __func__, sc->rirb_size);
1996 	}
1997 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBSIZE, rirbsize);
1998 
1999 	/* Setup the RIRB Address in the hdac */
2000 	rirbpaddr = (uint64_t)sc->rirb_dma.dma_paddr;
2001 	HDAC_WRITE_4(&sc->mem, HDAC_RIRBLBASE, (uint32_t)rirbpaddr);
2002 	HDAC_WRITE_4(&sc->mem, HDAC_RIRBUBASE, (uint32_t)(rirbpaddr >> 32));
2003 
2004 	/* Setup the WP and RP */
2005 	sc->rirb_rp = 0;
2006 	HDAC_WRITE_2(&sc->mem, HDAC_RIRBWP, HDAC_RIRBWP_RIRBWPRST);
2007 
2008 	/* Setup the interrupt threshold */
2009 	HDAC_WRITE_2(&sc->mem, HDAC_RINTCNT, sc->rirb_size / 2);
2010 
2011 	/* Enable Overrun and response received reporting */
2012 #if 0
2013 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL,
2014 	    HDAC_RIRBCTL_RIRBOIC | HDAC_RIRBCTL_RINTCTL);
2015 #else
2016 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, HDAC_RIRBCTL_RINTCTL);
2017 #endif
2018 
2019 #if 0
2020 	/*
2021 	 * Make sure that the Host CPU cache doesn't contain any dirty
2022 	 * cache lines that falls in the rirb. If I understood correctly, it
2023 	 * should be sufficient to do this only once as the rirb is purely
2024 	 * read-only from now on.
2025 	 */
2026 	bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
2027 	    BUS_DMASYNC_PREREAD);
2028 #endif
2029 }
2030 
2031 /****************************************************************************
2032  * void hdac_corb_start(hdac_softc *)
2033  *
2034  * Startup the corb DMA engine
2035  ****************************************************************************/
2036 static void
2037 hdac_corb_start(struct hdac_softc *sc)
2038 {
2039 	uint32_t corbctl;
2040 
2041 	corbctl = HDAC_READ_1(&sc->mem, HDAC_CORBCTL);
2042 	corbctl |= HDAC_CORBCTL_CORBRUN;
2043 	HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, corbctl);
2044 }
2045 
2046 /****************************************************************************
2047  * void hdac_rirb_start(hdac_softc *)
2048  *
2049  * Startup the rirb DMA engine
2050  ****************************************************************************/
2051 static void
2052 hdac_rirb_start(struct hdac_softc *sc)
2053 {
2054 	uint32_t rirbctl;
2055 
2056 	rirbctl = HDAC_READ_1(&sc->mem, HDAC_RIRBCTL);
2057 	rirbctl |= HDAC_RIRBCTL_RIRBDMAEN;
2058 	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, rirbctl);
2059 }
2060 
2061 
2062 /****************************************************************************
2063  * void hdac_scan_codecs(struct hdac_softc *, int)
2064  *
2065  * Scan the bus for available codecs, starting with num.
2066  ****************************************************************************/
2067 static void
2068 hdac_scan_codecs(struct hdac_softc *sc)
2069 {
2070 	struct hdac_codec *codec;
2071 	int i;
2072 	uint16_t statests;
2073 
2074 	statests = HDAC_READ_2(&sc->mem, HDAC_STATESTS);
2075 	for (i = 0; i < HDAC_CODEC_MAX; i++) {
2076 		if (HDAC_STATESTS_SDIWAKE(statests, i)) {
2077 			/* We have found a codec. */
2078 			codec = (struct hdac_codec *)malloc(sizeof(*codec),
2079 			    M_HDAC, M_ZERO | M_NOWAIT);
2080 			if (codec == NULL) {
2081 				device_printf(sc->dev,
2082 				    "Unable to allocate memory for codec\n");
2083 				continue;
2084 			}
2085 			codec->commands = NULL;
2086 			codec->responses_received = 0;
2087 			codec->verbs_sent = 0;
2088 			codec->sc = sc;
2089 			codec->cad = i;
2090 			sc->codecs[i] = codec;
2091 			hdac_probe_codec(codec);
2092 		}
2093 	}
2094 	/* All codecs have been probed, now try to attach drivers to them */
2095 	/* bus_generic_attach(sc->dev); */
2096 }
2097 
2098 /****************************************************************************
2099  * void hdac_probe_codec(struct hdac_softc *, int)
2100  *
2101  * Probe a the given codec_id for available function groups.
2102  ****************************************************************************/
2103 static void
2104 hdac_probe_codec(struct hdac_codec *codec)
2105 {
2106 	struct hdac_softc *sc = codec->sc;
2107 	uint32_t vendorid, revisionid, subnode;
2108 	int startnode;
2109 	int endnode;
2110 	int i;
2111 	nid_t cad = codec->cad;
2112 
2113 	HDA_BOOTVERBOSE(
2114 		device_printf(sc->dev, "Probing codec #%d...\n", cad);
2115 	);
2116 	vendorid = hdac_command(sc,
2117 	    HDA_CMD_GET_PARAMETER(cad, 0x0, HDA_PARAM_VENDOR_ID),
2118 	    cad);
2119 	revisionid = hdac_command(sc,
2120 	    HDA_CMD_GET_PARAMETER(cad, 0x0, HDA_PARAM_REVISION_ID),
2121 	    cad);
2122 	codec->vendor_id = HDA_PARAM_VENDOR_ID_VENDOR_ID(vendorid);
2123 	codec->device_id = HDA_PARAM_VENDOR_ID_DEVICE_ID(vendorid);
2124 	codec->revision_id = HDA_PARAM_REVISION_ID_REVISION_ID(revisionid);
2125 	codec->stepping_id = HDA_PARAM_REVISION_ID_STEPPING_ID(revisionid);
2126 
2127 	if (vendorid == HDAC_INVALID && revisionid == HDAC_INVALID) {
2128 		device_printf(sc->dev, "Codec #%d is not responding!"
2129 		    " Probing aborted.\n", cad);
2130 		return;
2131 	}
2132 
2133 	device_printf(sc->dev, "HDA Codec #%d: %s\n",
2134 	    cad, hdac_codec_name(codec));
2135 	HDA_BOOTVERBOSE(
2136 		device_printf(sc->dev, " HDA Codec ID: 0x%08x\n",
2137 		    hdac_codec_id(codec));
2138 		device_printf(sc->dev, "       Vendor: 0x%04x\n",
2139 		    codec->vendor_id);
2140 		device_printf(sc->dev, "       Device: 0x%04x\n",
2141 		    codec->device_id);
2142 		device_printf(sc->dev, "     Revision: 0x%02x\n",
2143 		    codec->revision_id);
2144 		device_printf(sc->dev, "     Stepping: 0x%02x\n",
2145 		    codec->stepping_id);
2146 		device_printf(sc->dev, "PCI Subvendor: 0x%08x\n",
2147 		    sc->pci_subvendor);
2148 	);
2149 	subnode = hdac_command(sc,
2150 	    HDA_CMD_GET_PARAMETER(cad, 0x0, HDA_PARAM_SUB_NODE_COUNT),
2151 	    cad);
2152 	startnode = HDA_PARAM_SUB_NODE_COUNT_START(subnode);
2153 	endnode = startnode + HDA_PARAM_SUB_NODE_COUNT_TOTAL(subnode);
2154 
2155 	HDA_BOOTHVERBOSE(
2156 		device_printf(sc->dev, "\tstartnode=%d endnode=%d\n",
2157 		    startnode, endnode);
2158 	);
2159 
2160 	codec->fgs = (struct hdac_devinfo *)malloc(sizeof(struct hdac_devinfo) *
2161 	    (endnode - startnode), M_HDAC, M_NOWAIT | M_ZERO);
2162 	if (codec->fgs == NULL) {
2163 		device_printf(sc->dev, "%s: Unable to allocate function groups\n",
2164 		    __func__);
2165 		return;
2166 	}
2167 
2168 	for (i = startnode; i < endnode; i++)
2169 		hdac_probe_function(codec, i);
2170 	return;
2171 }
2172 
2173 /*
2174  * Probe codec function and add it to the list.
2175  */
2176 static void
2177 hdac_probe_function(struct hdac_codec *codec, nid_t nid)
2178 {
2179 	struct hdac_softc *sc = codec->sc;
2180 	struct hdac_devinfo *devinfo = &codec->fgs[codec->num_fgs];
2181 	uint32_t fctgrptype;
2182 	uint32_t res;
2183 	nid_t cad = codec->cad;
2184 
2185 	fctgrptype = HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE(hdac_command(sc,
2186 	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_FCT_GRP_TYPE), cad));
2187 
2188 	devinfo->nid = nid;
2189 	devinfo->node_type = fctgrptype;
2190 	devinfo->codec = codec;
2191 
2192 	res = hdac_command(sc,
2193 	    HDA_CMD_GET_PARAMETER(cad , nid, HDA_PARAM_SUB_NODE_COUNT), cad);
2194 
2195 	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
2196 	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
2197 	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
2198 
2199 	HDA_BOOTVERBOSE(
2200 		device_printf(sc->dev,
2201 		    "\tFound %s FG nid=%d startnode=%d endnode=%d total=%d\n",
2202 		    (fctgrptype == HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) ? "audio":
2203 		    (fctgrptype == HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_MODEM) ? "modem":
2204 		    "unknown", nid, devinfo->startnode, devinfo->endnode,
2205 		    devinfo->nodecnt);
2206 	);
2207 
2208 	if (devinfo->nodecnt > 0)
2209 		devinfo->widget = (struct hdac_widget *)malloc(
2210 		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAC,
2211 		    M_NOWAIT | M_ZERO);
2212 	else
2213 		devinfo->widget = NULL;
2214 
2215 	if (devinfo->widget == NULL) {
2216 		device_printf(sc->dev, "unable to allocate widgets!\n");
2217 		devinfo->endnode = devinfo->startnode;
2218 		devinfo->nodecnt = 0;
2219 		return;
2220 	}
2221 
2222 	codec->num_fgs++;
2223 }
2224 
2225 static void
2226 hdac_widget_connection_parse(struct hdac_widget *w)
2227 {
2228 	struct hdac_softc *sc = w->devinfo->codec->sc;
2229 	uint32_t res;
2230 	int i, j, max, ents, entnum;
2231 	nid_t cad = w->devinfo->codec->cad;
2232 	nid_t nid = w->nid;
2233 	nid_t cnid, addcnid, prevcnid;
2234 
2235 	w->nconns = 0;
2236 
2237 	res = hdac_command(sc,
2238 	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_CONN_LIST_LENGTH), cad);
2239 
2240 	ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res);
2241 
2242 	if (ents < 1)
2243 		return;
2244 
2245 	entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4;
2246 	max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1;
2247 	prevcnid = 0;
2248 
2249 #define CONN_RMASK(e)		(1 << ((32 / (e)) - 1))
2250 #define CONN_NMASK(e)		(CONN_RMASK(e) - 1)
2251 #define CONN_RESVAL(r, e, n)	((r) >> ((32 / (e)) * (n)))
2252 #define CONN_RANGE(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_RMASK(e))
2253 #define CONN_CNID(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_NMASK(e))
2254 
2255 	for (i = 0; i < ents; i += entnum) {
2256 		res = hdac_command(sc,
2257 		    HDA_CMD_GET_CONN_LIST_ENTRY(cad, nid, i), cad);
2258 		for (j = 0; j < entnum; j++) {
2259 			cnid = CONN_CNID(res, entnum, j);
2260 			if (cnid == 0) {
2261 				if (w->nconns < ents)
2262 					device_printf(sc->dev,
2263 					    "%s: nid=%d WARNING: zero cnid "
2264 					    "entnum=%d j=%d index=%d "
2265 					    "entries=%d found=%d res=0x%08x\n",
2266 					    __func__, nid, entnum, j, i,
2267 					    ents, w->nconns, res);
2268 				else
2269 					goto getconns_out;
2270 			}
2271 			if (cnid < w->devinfo->startnode ||
2272 			    cnid >= w->devinfo->endnode) {
2273 				HDA_BOOTVERBOSE(
2274 					device_printf(sc->dev,
2275 					    "GHOST: nid=%d j=%d "
2276 					    "entnum=%d index=%d res=0x%08x\n",
2277 					    nid, j, entnum, i, res);
2278 				);
2279 			}
2280 			if (CONN_RANGE(res, entnum, j) == 0)
2281 				addcnid = cnid;
2282 			else if (prevcnid == 0 || prevcnid >= cnid) {
2283 				device_printf(sc->dev,
2284 				    "%s: WARNING: Invalid child range "
2285 				    "nid=%d index=%d j=%d entnum=%d "
2286 				    "prevcnid=%d cnid=%d res=0x%08x\n",
2287 				    __func__, nid, i, j, entnum, prevcnid,
2288 				    cnid, res);
2289 				addcnid = cnid;
2290 			} else
2291 				addcnid = prevcnid + 1;
2292 			while (addcnid <= cnid) {
2293 				if (w->nconns > max) {
2294 					device_printf(sc->dev,
2295 					    "Adding %d (nid=%d): "
2296 					    "Max connection reached! max=%d\n",
2297 					    addcnid, nid, max + 1);
2298 					goto getconns_out;
2299 				}
2300 				w->connsenable[w->nconns] = 1;
2301 				w->conns[w->nconns++] = addcnid++;
2302 			}
2303 			prevcnid = cnid;
2304 		}
2305 	}
2306 
2307 getconns_out:
2308 	return;
2309 }
2310 
2311 static uint32_t
2312 hdac_widget_pin_patch(uint32_t config, const char *str)
2313 {
2314 	char buf[256];
2315 	char *key, *value, *rest, *bad;
2316 	int ival, i;
2317 
2318 	strlcpy(buf, str, sizeof(buf));
2319 	rest = buf;
2320 	while ((key = strsep(&rest, "=")) != NULL) {
2321 		value = strsep(&rest, " \t");
2322 		if (value == NULL)
2323 			break;
2324 		ival = strtol(value, &bad, 10);
2325 		if (strcmp(key, "seq") == 0) {
2326 			config &= ~HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK;
2327 			config |= ((ival << HDA_CONFIG_DEFAULTCONF_SEQUENCE_SHIFT) &
2328 			    HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK);
2329 		} else if (strcmp(key, "as") == 0) {
2330 			config &= ~HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK;
2331 			config |= ((ival << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT) &
2332 			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK);
2333 		} else if (strcmp(key, "misc") == 0) {
2334 			config &= ~HDA_CONFIG_DEFAULTCONF_MISC_MASK;
2335 			config |= ((ival << HDA_CONFIG_DEFAULTCONF_MISC_SHIFT) &
2336 			    HDA_CONFIG_DEFAULTCONF_MISC_MASK);
2337 		} else if (strcmp(key, "color") == 0) {
2338 			config &= ~HDA_CONFIG_DEFAULTCONF_COLOR_MASK;
2339 			if (bad[0] == 0) {
2340 				config |= ((ival << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT) &
2341 				    HDA_CONFIG_DEFAULTCONF_COLOR_MASK);
2342 			};
2343 			for (i = 0; i < 16; i++) {
2344 				if (strcasecmp(HDA_COLORS[i], value) == 0) {
2345 					config |= (i << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT);
2346 					break;
2347 				}
2348 			}
2349 		} else if (strcmp(key, "ctype") == 0) {
2350 			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK;
2351 			config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT) &
2352 			    HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK);
2353 		} else if (strcmp(key, "device") == 0) {
2354 			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2355 			if (bad[0] == 0) {
2356 				config |= ((ival << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT) &
2357 				    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK);
2358 				continue;
2359 			};
2360 			for (i = 0; i < 16; i++) {
2361 				if (strcasecmp(HDA_DEVS[i], value) == 0) {
2362 					config |= (i << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT);
2363 					break;
2364 				}
2365 			}
2366 		} else if (strcmp(key, "loc") == 0) {
2367 			config &= ~HDA_CONFIG_DEFAULTCONF_LOCATION_MASK;
2368 			config |= ((ival << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT) &
2369 			    HDA_CONFIG_DEFAULTCONF_LOCATION_MASK);
2370 		} else if (strcmp(key, "conn") == 0) {
2371 			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
2372 			if (bad[0] == 0) {
2373 				config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT) &
2374 				    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2375 				continue;
2376 			};
2377 			for (i = 0; i < 4; i++) {
2378 				if (strcasecmp(HDA_CONNS[i], value) == 0) {
2379 					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT);
2380 					break;
2381 				}
2382 			}
2383 		}
2384 	}
2385 	return (config);
2386 }
2387 
2388 static uint32_t
2389 hdac_widget_pin_getconfig(struct hdac_widget *w)
2390 {
2391 	struct hdac_softc *sc;
2392 	uint32_t config, orig, id;
2393 	nid_t cad, nid;
2394 	char buf[32];
2395 	const char *res = NULL, *patch = NULL;
2396 
2397 	sc = w->devinfo->codec->sc;
2398 	cad = w->devinfo->codec->cad;
2399 	nid = w->nid;
2400 	id = hdac_codec_id(w->devinfo->codec);
2401 
2402 	config = hdac_command(sc,
2403 	    HDA_CMD_GET_CONFIGURATION_DEFAULT(cad, nid),
2404 	    cad);
2405 	orig = config;
2406 
2407 	HDA_BOOTVERBOSE(
2408 		hdac_dump_pin_config(w, orig);
2409 	);
2410 
2411 	/* XXX: Old patches require complete review.
2412 	 * Now they may create more problem then solve due to
2413 	 * incorrect associations.
2414 	 */
2415 	if (id == HDA_CODEC_ALC880 && sc->pci_subvendor == LG_LW20_SUBVENDOR) {
2416 		switch (nid) {
2417 		case 26:
2418 			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2419 			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN;
2420 			break;
2421 		case 27:
2422 			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2423 			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT;
2424 			break;
2425 		default:
2426 			break;
2427 		}
2428 	} else if (id == HDA_CODEC_ALC880 &&
2429 	    (sc->pci_subvendor == CLEVO_D900T_SUBVENDOR ||
2430 	    sc->pci_subvendor == ASUS_M5200_SUBVENDOR)) {
2431 		/*
2432 		 * Super broken BIOS
2433 		 */
2434 		switch (nid) {
2435 		case 24:	/* MIC1 */
2436 			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2437 			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN;
2438 			break;
2439 		case 25:	/* XXX MIC2 */
2440 			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2441 			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN;
2442 			break;
2443 		case 26:	/* LINE1 */
2444 			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2445 			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN;
2446 			break;
2447 		case 27:	/* XXX LINE2 */
2448 			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2449 			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN;
2450 			break;
2451 		case 28:	/* CD */
2452 			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2453 			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_CD;
2454 			break;
2455 		}
2456 	} else if (id == HDA_CODEC_ALC883 &&
2457 	    (sc->pci_subvendor == MSI_MS034A_SUBVENDOR ||
2458 	    HDA_DEV_MATCH(ACER_ALL_SUBVENDOR, sc->pci_subvendor))) {
2459 		switch (nid) {
2460 		case 25:
2461 			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2462 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2463 			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN |
2464 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
2465 			break;
2466 		case 28:
2467 			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2468 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2469 			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_CD |
2470 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
2471 			break;
2472 		}
2473 	} else if (id == HDA_CODEC_CX20549 && sc->pci_subvendor ==
2474 	    HP_V3000_SUBVENDOR) {
2475 		switch (nid) {
2476 		case 18:
2477 			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
2478 			config |= HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE;
2479 			break;
2480 		case 20:
2481 			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2482 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2483 			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN |
2484 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
2485 			break;
2486 		case 21:
2487 			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2488 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2489 			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_CD |
2490 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
2491 			break;
2492 		}
2493 	} else if (id == HDA_CODEC_CX20551 && sc->pci_subvendor ==
2494 	    HP_DV5000_SUBVENDOR) {
2495 		switch (nid) {
2496 		case 20:
2497 		case 21:
2498 			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
2499 			config |= HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE;
2500 			break;
2501 		}
2502 	} else if (id == HDA_CODEC_ALC861 && sc->pci_subvendor ==
2503 	    ASUS_W6F_SUBVENDOR) {
2504 		switch (nid) {
2505 		case 11:
2506 			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2507 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2508 			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT |
2509 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
2510 			break;
2511 		case 12:
2512 		case 14:
2513 		case 16:
2514 		case 31:
2515 		case 32:
2516 			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2517 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2518 			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN |
2519 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
2520 			break;
2521 		case 15:
2522 			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2523 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2524 			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT |
2525 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK);
2526 			break;
2527 		}
2528 	} else if (id == HDA_CODEC_ALC861 && sc->pci_subvendor ==
2529 	    UNIWILL_9075_SUBVENDOR) {
2530 		switch (nid) {
2531 		case 15:
2532 			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2533 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2534 			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT |
2535 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK);
2536 			break;
2537 		}
2538 	}
2539 
2540 	/* New patches */
2541 	if (id == HDA_CODEC_AD1986A &&
2542 	    (sc->pci_subvendor == ASUS_M2NPVMX_SUBVENDOR ||
2543 	    sc->pci_subvendor == ASUS_A8NVMCSM_SUBVENDOR ||
2544 	    sc->pci_subvendor == ASUS_P5PL2_SUBVENDOR)) {
2545 		switch (nid) {
2546 		case 26: /* Headphones with redirection */
2547 			patch = "as=1 seq=15";
2548 			break;
2549 		case 28: /* 5.1 out => 2.0 out + 1 input */
2550 			patch = "device=Line-in as=8 seq=1";
2551 			break;
2552 		case 29: /* Can't use this as input, as the only available mic
2553 			  * preamplifier is busy by front panel mic (nid 31).
2554 			  * If you want to use this rear connector as mic input,
2555 			  * you have to disable the front panel one. */
2556 			patch = "as=0";
2557 			break;
2558 		case 31: /* Lot of inputs configured with as=15 and unusable */
2559 			patch = "as=8 seq=3";
2560 			break;
2561 		case 32:
2562 			patch = "as=8 seq=4";
2563 			break;
2564 		case 34:
2565 			patch = "as=8 seq=5";
2566 			break;
2567 		case 36:
2568 			patch = "as=8 seq=6";
2569 			break;
2570 		}
2571 	} else if (id == HDA_CODEC_ALC260 &&
2572 	    HDA_DEV_MATCH(SONY_S5_SUBVENDOR, sc->pci_subvendor)) {
2573 		switch (nid) {
2574 		case 16:
2575 			patch = "seq=15 device=Headphones";
2576 			break;
2577 		}
2578 	} else if (id == HDA_CODEC_ALC268) {
2579 	    if (sc->pci_subvendor == ACER_T5320_SUBVENDOR) {
2580 		switch (nid) {
2581 		case 20: /* Headphones Jack */
2582 			patch = "as=1 seq=15";
2583 			break;
2584 		}
2585 	    }
2586 	} else if (id == HDA_CODEC_CX20561 &&
2587 	    sc->pci_subvendor == LENOVO_B450_SUBVENDOR) {
2588 		switch (nid) {
2589 		case 22:
2590 			patch = "as=1 seq=15";
2591 			break;
2592 		}
2593 	}
2594 
2595 	if (patch != NULL)
2596 		config = hdac_widget_pin_patch(config, patch);
2597 
2598 	snprintf(buf, sizeof(buf), "cad%u.nid%u.config", cad, nid);
2599 	if (resource_string_value(device_get_name(sc->dev),
2600 	    device_get_unit(sc->dev), buf, &res) == 0) {
2601 		if (strncmp(res, "0x", 2) == 0) {
2602 			config = strtol(res + 2, NULL, 16);
2603 		} else {
2604 			config = hdac_widget_pin_patch(config, res);
2605 		}
2606 	}
2607 
2608 	HDA_BOOTVERBOSE(
2609 		if (config != orig)
2610 			device_printf(sc->dev,
2611 			    "Patching pin config nid=%u 0x%08x -> 0x%08x\n",
2612 			    nid, orig, config);
2613 	);
2614 
2615 	return (config);
2616 }
2617 
2618 static uint32_t
2619 hdac_widget_pin_getcaps(struct hdac_widget *w)
2620 {
2621 	struct hdac_softc *sc;
2622 	uint32_t caps, orig, id;
2623 	nid_t cad, nid;
2624 
2625 	sc = w->devinfo->codec->sc;
2626 	cad = w->devinfo->codec->cad;
2627 	nid = w->nid;
2628 	id = hdac_codec_id(w->devinfo->codec);
2629 
2630 	caps = hdac_command(sc,
2631 	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_PIN_CAP), cad);
2632 	orig = caps;
2633 
2634 	HDA_BOOTVERBOSE(
2635 		if (caps != orig)
2636 			device_printf(sc->dev,
2637 			    "Patching pin caps nid=%u 0x%08x -> 0x%08x\n",
2638 			    nid, orig, caps);
2639 	);
2640 
2641 	return (caps);
2642 }
2643 
2644 static void
2645 hdac_widget_pin_parse(struct hdac_widget *w)
2646 {
2647 	struct hdac_softc *sc = w->devinfo->codec->sc;
2648 	uint32_t config, pincap;
2649 	const char *devstr;
2650 	nid_t cad = w->devinfo->codec->cad;
2651 	nid_t nid = w->nid;
2652 	int conn, color;
2653 
2654 	config = hdac_widget_pin_getconfig(w);
2655 	w->wclass.pin.config = config;
2656 
2657 	pincap = hdac_widget_pin_getcaps(w);
2658 	w->wclass.pin.cap = pincap;
2659 
2660 	w->wclass.pin.ctrl = hdac_command(sc,
2661 	    HDA_CMD_GET_PIN_WIDGET_CTRL(cad, nid), cad);
2662 
2663 	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)) {
2664 		w->param.eapdbtl = hdac_command(sc,
2665 		    HDA_CMD_GET_EAPD_BTL_ENABLE(cad, nid), cad);
2666 		w->param.eapdbtl &= 0x7;
2667 		w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2668 	} else
2669 		w->param.eapdbtl = HDAC_INVALID;
2670 
2671 	devstr = HDA_DEVS[(config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) >>
2672 	    HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT];
2673 
2674 	conn = (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) >>
2675 	    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT;
2676 	color = (config & HDA_CONFIG_DEFAULTCONF_COLOR_MASK) >>
2677 	    HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT;
2678 
2679 	strlcat(w->name, ": ", sizeof(w->name));
2680 	strlcat(w->name, devstr, sizeof(w->name));
2681 	strlcat(w->name, " (", sizeof(w->name));
2682 	if (conn == 0 && color != 0 && color != 15) {
2683 		strlcat(w->name, HDA_COLORS[color], sizeof(w->name));
2684 		strlcat(w->name, " ", sizeof(w->name));
2685 	}
2686 	strlcat(w->name, HDA_CONNS[conn], sizeof(w->name));
2687 	strlcat(w->name, ")", sizeof(w->name));
2688 }
2689 
2690 static uint32_t
2691 hdac_widget_getcaps(struct hdac_widget *w, int *waspin)
2692 {
2693 	struct hdac_softc *sc;
2694 	uint32_t caps, orig, id;
2695 	nid_t cad, nid, beeper = -1;
2696 
2697 	sc = w->devinfo->codec->sc;
2698 	cad = w->devinfo->codec->cad;
2699 	nid = w->nid;
2700 	id = hdac_codec_id(w->devinfo->codec);
2701 
2702 	caps = hdac_command(sc,
2703 	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_AUDIO_WIDGET_CAP),
2704 	    cad);
2705 	orig = caps;
2706 
2707 	/* On some codecs beeper is an input pin, but it is not recordable
2708 	   alone. Also most of BIOSes does not declare beeper pin.
2709 	   Change beeper pin node type to beeper to help parser. */
2710 	*waspin = 0;
2711 	switch (id) {
2712 	case HDA_CODEC_AD1882:
2713 	case HDA_CODEC_AD1883:
2714 	case HDA_CODEC_AD1984:
2715 	case HDA_CODEC_AD1984A:
2716 	case HDA_CODEC_AD1984B:
2717 	case HDA_CODEC_AD1987:
2718 	case HDA_CODEC_AD1988:
2719 	case HDA_CODEC_AD1988B:
2720 	case HDA_CODEC_AD1989B:
2721 		beeper = 26;
2722 		break;
2723 	case HDA_CODEC_ALC260:
2724 		beeper = 23;
2725 		break;
2726 	case HDA_CODEC_ALC262:
2727 	case HDA_CODEC_ALC268:
2728 	case HDA_CODEC_ALC880:
2729 	case HDA_CODEC_ALC882:
2730 	case HDA_CODEC_ALC883:
2731 	case HDA_CODEC_ALC885:
2732 	case HDA_CODEC_ALC888:
2733 	case HDA_CODEC_ALC889:
2734 		beeper = 29;
2735 		break;
2736 	}
2737 	if (nid == beeper) {
2738 		caps &= ~HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_MASK;
2739 		caps |= HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET <<
2740 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_SHIFT;
2741 		*waspin = 1;
2742 	}
2743 
2744 	HDA_BOOTVERBOSE(
2745 		if (caps != orig) {
2746 			device_printf(sc->dev,
2747 			    "Patching widget caps nid=%u 0x%08x -> 0x%08x\n",
2748 			    nid, orig, caps);
2749 		}
2750 	);
2751 
2752 	return (caps);
2753 }
2754 
2755 static void
2756 hdac_widget_parse(struct hdac_widget *w)
2757 {
2758 	struct hdac_softc *sc = w->devinfo->codec->sc;
2759 	uint32_t wcap, cap;
2760 	char *typestr;
2761 	nid_t cad = w->devinfo->codec->cad;
2762 	nid_t nid = w->nid;
2763 
2764 	wcap = hdac_widget_getcaps(w, &w->waspin);
2765 
2766 	w->param.widget_cap = wcap;
2767 	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap);
2768 
2769 	switch (w->type) {
2770 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
2771 		typestr = "audio output";
2772 		break;
2773 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
2774 		typestr = "audio input";
2775 		break;
2776 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2777 		typestr = "audio mixer";
2778 		break;
2779 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2780 		typestr = "audio selector";
2781 		break;
2782 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
2783 		typestr = "pin";
2784 		break;
2785 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET:
2786 		typestr = "power widget";
2787 		break;
2788 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET:
2789 		typestr = "volume widget";
2790 		break;
2791 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
2792 		typestr = "beep widget";
2793 		break;
2794 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET:
2795 		typestr = "vendor widget";
2796 		break;
2797 	default:
2798 		typestr = "unknown type";
2799 		break;
2800 	}
2801 
2802 	strlcpy(w->name, typestr, sizeof(w->name));
2803 
2804 	hdac_widget_connection_parse(w);
2805 
2806 	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) {
2807 		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
2808 			w->param.outamp_cap =
2809 			    hdac_command(sc,
2810 			    HDA_CMD_GET_PARAMETER(cad, nid,
2811 			    HDA_PARAM_OUTPUT_AMP_CAP), cad);
2812 		else
2813 			w->param.outamp_cap =
2814 			    w->devinfo->function.audio.outamp_cap;
2815 	} else
2816 		w->param.outamp_cap = 0;
2817 
2818 	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) {
2819 		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
2820 			w->param.inamp_cap =
2821 			    hdac_command(sc,
2822 			    HDA_CMD_GET_PARAMETER(cad, nid,
2823 			    HDA_PARAM_INPUT_AMP_CAP), cad);
2824 		else
2825 			w->param.inamp_cap =
2826 			    w->devinfo->function.audio.inamp_cap;
2827 	} else
2828 		w->param.inamp_cap = 0;
2829 
2830 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
2831 	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2832 		if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) {
2833 			cap = hdac_command(sc,
2834 			    HDA_CMD_GET_PARAMETER(cad, nid,
2835 			    HDA_PARAM_SUPP_STREAM_FORMATS), cad);
2836 			w->param.supp_stream_formats = (cap != 0) ? cap :
2837 			    w->devinfo->function.audio.supp_stream_formats;
2838 			cap = hdac_command(sc,
2839 			    HDA_CMD_GET_PARAMETER(cad, nid,
2840 			    HDA_PARAM_SUPP_PCM_SIZE_RATE), cad);
2841 			w->param.supp_pcm_size_rate = (cap != 0) ? cap :
2842 			    w->devinfo->function.audio.supp_pcm_size_rate;
2843 		} else {
2844 			w->param.supp_stream_formats =
2845 			    w->devinfo->function.audio.supp_stream_formats;
2846 			w->param.supp_pcm_size_rate =
2847 			    w->devinfo->function.audio.supp_pcm_size_rate;
2848 		}
2849 	} else {
2850 		w->param.supp_stream_formats = 0;
2851 		w->param.supp_pcm_size_rate = 0;
2852 	}
2853 
2854 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2855 		hdac_widget_pin_parse(w);
2856 }
2857 
2858 static struct hdac_widget *
2859 hdac_widget_get(struct hdac_devinfo *devinfo, nid_t nid)
2860 {
2861 	if (devinfo == NULL || devinfo->widget == NULL ||
2862 		    nid < devinfo->startnode || nid >= devinfo->endnode)
2863 		return (NULL);
2864 	return (&devinfo->widget[nid - devinfo->startnode]);
2865 }
2866 
2867 static __inline int
2868 hda_poll_channel(struct hdac_chan *ch)
2869 {
2870 	uint32_t sz, delta;
2871 	volatile uint32_t ptr;
2872 
2873 	if (!(ch->flags & HDAC_CHN_RUNNING))
2874 		return (0);
2875 
2876 	sz = ch->blksz * ch->blkcnt;
2877 	if (ch->dmapos != NULL)
2878 		ptr = *(ch->dmapos);
2879 	else
2880 		ptr = HDAC_READ_4(&ch->devinfo->codec->sc->mem,
2881 		    ch->off + HDAC_SDLPIB);
2882 	ch->ptr = ptr;
2883 	ptr %= sz;
2884 	ptr &= ~(ch->blksz - 1);
2885 	delta = (sz + ptr - ch->prevptr) % sz;
2886 
2887 	if (delta < ch->blksz)
2888 		return (0);
2889 
2890 	ch->prevptr = ptr;
2891 
2892 	return (1);
2893 }
2894 
2895 static void
2896 hda_poll_callback(void *arg)
2897 {
2898 	struct hdac_softc *sc = arg;
2899 	uint32_t trigger;
2900 	int i, active = 0;
2901 
2902 	if (sc == NULL)
2903 		return;
2904 
2905 	hdac_lock(sc);
2906 	if (sc->polling == 0) {
2907 		hdac_unlock(sc);
2908 		return;
2909 	}
2910 
2911 	trigger = 0;
2912 	for (i = 0; i < sc->num_chans; i++) {
2913 		if ((sc->chans[i].flags & HDAC_CHN_RUNNING) == 0)
2914 		    continue;
2915 		active = 1;
2916 		if (hda_poll_channel(&sc->chans[i]))
2917 		    trigger |= (1 << i);
2918 	}
2919 
2920 	/* XXX */
2921 	if (active)
2922 		callout_reset(&sc->poll_hda, sc->poll_ticks,
2923 		    hda_poll_callback, sc);
2924 
2925 	hdac_unlock(sc);
2926 
2927 	for (i = 0; i < sc->num_chans; i++) {
2928 		if (trigger & (1 << i))
2929 			chn_intr(sc->chans[i].c);
2930 	}
2931 }
2932 
2933 static int
2934 hdac_rirb_flush(struct hdac_softc *sc)
2935 {
2936 	struct hdac_rirb *rirb_base, *rirb;
2937 	struct hdac_codec *codec;
2938 	struct hdac_command_list *commands;
2939 	nid_t cad;
2940 	uint32_t resp;
2941 	uint8_t rirbwp;
2942 	int ret;
2943 
2944 	rirb_base = (struct hdac_rirb *)sc->rirb_dma.dma_vaddr;
2945 	rirbwp = HDAC_READ_1(&sc->mem, HDAC_RIRBWP);
2946 #if 0
2947 	bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
2948 	    BUS_DMASYNC_POSTREAD);
2949 #endif
2950 
2951 	ret = 0;
2952 
2953 	while (sc->rirb_rp != rirbwp) {
2954 		sc->rirb_rp++;
2955 		sc->rirb_rp %= sc->rirb_size;
2956 		rirb = &rirb_base[sc->rirb_rp];
2957 		cad = HDAC_RIRB_RESPONSE_EX_SDATA_IN(rirb->response_ex);
2958 		if (cad < 0 || cad >= HDAC_CODEC_MAX ||
2959 		    sc->codecs[cad] == NULL)
2960 			continue;
2961 		resp = rirb->response;
2962 		codec = sc->codecs[cad];
2963 		commands = codec->commands;
2964 		if (rirb->response_ex & HDAC_RIRB_RESPONSE_EX_UNSOLICITED) {
2965 			sc->unsolq[sc->unsolq_wp++] = (cad << 16) |
2966 			    ((resp >> 26) & 0xffff);
2967 			sc->unsolq_wp %= HDAC_UNSOLQ_MAX;
2968 		} else if (commands != NULL && commands->num_commands > 0 &&
2969 		    codec->responses_received < commands->num_commands)
2970 			commands->responses[codec->responses_received++] =
2971 			    resp;
2972 		ret++;
2973 	}
2974 
2975 	return (ret);
2976 }
2977 
2978 static int
2979 hdac_unsolq_flush(struct hdac_softc *sc)
2980 {
2981 	nid_t cad;
2982 	uint32_t tag;
2983 	int ret = 0;
2984 
2985 	if (sc->unsolq_st == HDAC_UNSOLQ_READY) {
2986 		sc->unsolq_st = HDAC_UNSOLQ_BUSY;
2987 		while (sc->unsolq_rp != sc->unsolq_wp) {
2988 			cad = sc->unsolq[sc->unsolq_rp] >> 16;
2989 			tag = sc->unsolq[sc->unsolq_rp++] & 0xffff;
2990 			sc->unsolq_rp %= HDAC_UNSOLQ_MAX;
2991 			hdac_unsolicited_handler(sc->codecs[cad], tag);
2992 			ret++;
2993 		}
2994 		sc->unsolq_st = HDAC_UNSOLQ_READY;
2995 	}
2996 
2997 	return (ret);
2998 }
2999 
3000 static void
3001 hdac_poll_callback(void *arg)
3002 {
3003 	struct hdac_softc *sc = arg;
3004 	if (sc == NULL)
3005 		return;
3006 
3007 	hdac_lock(sc);
3008 	if (sc->polling == 0 || sc->poll_ival == 0) {
3009 		hdac_unlock(sc);
3010 		return;
3011 	}
3012 	if (hdac_rirb_flush(sc) != 0)
3013 		hdac_unsolq_flush(sc);
3014 	callout_reset(&sc->poll_hdac, sc->poll_ival, hdac_poll_callback, sc);
3015 	hdac_unlock(sc);
3016 }
3017 
3018 static void
3019 hdac_poll_reinit(struct hdac_softc *sc)
3020 {
3021 	int i, pollticks, min = 1000000;
3022 	struct hdac_chan *ch;
3023 
3024 	for (i = 0; i < sc->num_chans; i++) {
3025 		if ((sc->chans[i].flags & HDAC_CHN_RUNNING) == 0)
3026 			continue;
3027 		ch = &sc->chans[i];
3028 		pollticks = ((uint64_t)hz * ch->blksz) /
3029 		    ((uint64_t)sndbuf_getalign(ch->b) * sndbuf_getspd(ch->b));
3030 		pollticks >>= 1;
3031 		if (pollticks > hz)
3032 			pollticks = hz;
3033 		if (pollticks < 1) {
3034 			HDA_BOOTVERBOSE(
3035 				device_printf(sc->dev,
3036 				    "%s: pollticks=%d < 1 !\n",
3037 				    __func__, pollticks);
3038 			);
3039 			pollticks = 1;
3040 		}
3041 		if (min > pollticks)
3042 			min = pollticks;
3043 	}
3044 	HDA_BOOTVERBOSE(
3045 		device_printf(sc->dev,
3046 		    "%s: pollticks %d -> %d\n",
3047 		    __func__, sc->poll_ticks, min);
3048 	);
3049 	sc->poll_ticks = min;
3050 	if (min == 1000000)
3051 		callout_stop(&sc->poll_hda);
3052 	else
3053 		callout_reset(&sc->poll_hda, 1, hda_poll_callback, sc);
3054 }
3055 
3056 static void
3057 hdac_stream_stop(struct hdac_chan *ch)
3058 {
3059 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3060 	uint32_t ctl;
3061 
3062 	ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
3063 	ctl &= ~(HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
3064 	    HDAC_SDCTL_RUN);
3065 	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL0, ctl);
3066 
3067 	ch->flags &= ~HDAC_CHN_RUNNING;
3068 
3069 	if (sc->polling != 0)
3070 		hdac_poll_reinit(sc);
3071 
3072 	ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
3073 	ctl &= ~(1 << (ch->off >> 5));
3074 	HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
3075 }
3076 
3077 static void
3078 hdac_stream_start(struct hdac_chan *ch)
3079 {
3080 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3081 	uint32_t ctl;
3082 
3083 	ch->flags |= HDAC_CHN_RUNNING;
3084 
3085 	if (sc->polling != 0)
3086 		hdac_poll_reinit(sc);
3087 
3088 	ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
3089 	ctl |= 1 << (ch->off >> 5);
3090 	HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
3091 
3092 	ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
3093 	ctl |= HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
3094 	    HDAC_SDCTL_RUN;
3095 	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL0, ctl);
3096 }
3097 
3098 static void
3099 hdac_stream_reset(struct hdac_chan *ch)
3100 {
3101 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3102 	int timeout = 1000;
3103 	int to = timeout;
3104 	uint32_t ctl;
3105 
3106 	ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
3107 	ctl |= HDAC_SDCTL_SRST;
3108 	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL0, ctl);
3109 	do {
3110 		ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
3111 		if (ctl & HDAC_SDCTL_SRST)
3112 			break;
3113 		DELAY(10);
3114 	} while (--to);
3115 	if (!(ctl & HDAC_SDCTL_SRST)) {
3116 		device_printf(sc->dev, "timeout in reset\n");
3117 	}
3118 	ctl &= ~HDAC_SDCTL_SRST;
3119 	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL0, ctl);
3120 	to = timeout;
3121 	do {
3122 		ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
3123 		if (!(ctl & HDAC_SDCTL_SRST))
3124 			break;
3125 		DELAY(10);
3126 	} while (--to);
3127 	if (ctl & HDAC_SDCTL_SRST)
3128 		device_printf(sc->dev, "can't reset!\n");
3129 }
3130 
3131 static void
3132 hdac_stream_setid(struct hdac_chan *ch)
3133 {
3134 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3135 	uint32_t ctl;
3136 
3137 	ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL2);
3138 	ctl &= ~HDAC_SDCTL2_STRM_MASK;
3139 	ctl |= ch->sid << HDAC_SDCTL2_STRM_SHIFT;
3140 	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL2, ctl);
3141 }
3142 
3143 static void
3144 hdac_bdl_setup(struct hdac_chan *ch)
3145 {
3146 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3147 	struct hdac_bdle *bdle;
3148 	uint64_t addr;
3149 	uint32_t blksz, blkcnt;
3150 	int i;
3151 
3152 	addr = (uint64_t)sndbuf_getbufaddr(ch->b);
3153 	bdle = (struct hdac_bdle *)ch->bdl_dma.dma_vaddr;
3154 
3155 	blksz = ch->blksz;
3156 	blkcnt = ch->blkcnt;
3157 
3158 	for (i = 0; i < blkcnt; i++, bdle++) {
3159 		bdle->addrl = (uint32_t)addr;
3160 		bdle->addrh = (uint32_t)(addr >> 32);
3161 		bdle->len = blksz;
3162 		bdle->ioc = 1;
3163 		addr += blksz;
3164 	}
3165 
3166 	HDAC_WRITE_4(&sc->mem, ch->off + HDAC_SDCBL, blksz * blkcnt);
3167 	HDAC_WRITE_2(&sc->mem, ch->off + HDAC_SDLVI, blkcnt - 1);
3168 	addr = ch->bdl_dma.dma_paddr;
3169 	HDAC_WRITE_4(&sc->mem, ch->off + HDAC_SDBDPL, (uint32_t)addr);
3170 	HDAC_WRITE_4(&sc->mem, ch->off + HDAC_SDBDPU, (uint32_t)(addr >> 32));
3171 	if (ch->dmapos != NULL &&
3172 	    !(HDAC_READ_4(&sc->mem, HDAC_DPIBLBASE) & 0x00000001)) {
3173 		addr = sc->pos_dma.dma_paddr;
3174 		HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE,
3175 		    ((uint32_t)addr & HDAC_DPLBASE_DPLBASE_MASK) | 0x00000001);
3176 		HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, (uint32_t)(addr >> 32));
3177 	}
3178 }
3179 
3180 static int
3181 hdac_bdl_alloc(struct hdac_chan *ch)
3182 {
3183 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3184 	int rc;
3185 
3186 	rc = hdac_dma_alloc(sc, &ch->bdl_dma,
3187 	    sizeof(struct hdac_bdle) * HDA_BDL_MAX);
3188 	if (rc) {
3189 		device_printf(sc->dev, "can't alloc bdl\n");
3190 		return (rc);
3191 	}
3192 
3193 	return (0);
3194 }
3195 
3196 static void
3197 hdac_audio_ctl_amp_set_internal(struct hdac_softc *sc, nid_t cad, nid_t nid,
3198 					int index, int lmute, int rmute,
3199 					int left, int right, int dir)
3200 {
3201 	uint16_t v = 0;
3202 
3203 	if (sc == NULL)
3204 		return;
3205 
3206 	if (left != right || lmute != rmute) {
3207 		v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
3208 		    (lmute << 7) | left;
3209 		hdac_command(sc,
3210 		    HDA_CMD_SET_AMP_GAIN_MUTE(cad, nid, v), cad);
3211 		v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
3212 		    (rmute << 7) | right;
3213 	} else
3214 		v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
3215 		    (lmute << 7) | left;
3216 
3217 	hdac_command(sc,
3218 	    HDA_CMD_SET_AMP_GAIN_MUTE(cad, nid, v), cad);
3219 }
3220 
3221 static void
3222 hdac_audio_ctl_amp_set(struct hdac_audio_ctl *ctl, uint32_t mute,
3223 						int left, int right)
3224 {
3225 	struct hdac_softc *sc;
3226 	nid_t nid, cad;
3227 	int lmute, rmute;
3228 
3229 	sc = ctl->widget->devinfo->codec->sc;
3230 	cad = ctl->widget->devinfo->codec->cad;
3231 	nid = ctl->widget->nid;
3232 
3233 	/* Save new values if valid. */
3234 	if (mute != HDA_AMP_MUTE_DEFAULT)
3235 		ctl->muted = mute;
3236 	if (left != HDA_AMP_VOL_DEFAULT)
3237 		ctl->left = left;
3238 	if (right != HDA_AMP_VOL_DEFAULT)
3239 		ctl->right = right;
3240 	/* Prepare effective values */
3241 	if (ctl->forcemute) {
3242 		lmute = 1;
3243 		rmute = 1;
3244 		left = 0;
3245 		right = 0;
3246 	} else {
3247 		lmute = HDA_AMP_LEFT_MUTED(ctl->muted);
3248 		rmute = HDA_AMP_RIGHT_MUTED(ctl->muted);
3249 		left = ctl->left;
3250 		right = ctl->right;
3251 	}
3252 	/* Apply effective values */
3253 	if (ctl->dir & HDA_CTL_OUT)
3254 		hdac_audio_ctl_amp_set_internal(sc, cad, nid, ctl->index,
3255 		    lmute, rmute, left, right, 0);
3256 	if (ctl->dir & HDA_CTL_IN)
3257     		hdac_audio_ctl_amp_set_internal(sc, cad, nid, ctl->index,
3258 		    lmute, rmute, left, right, 1);
3259 }
3260 
3261 static void
3262 hdac_widget_connection_select(struct hdac_widget *w, uint8_t index)
3263 {
3264 	if (w == NULL || w->nconns < 1 || index > (w->nconns - 1))
3265 		return;
3266 	hdac_command(w->devinfo->codec->sc,
3267 	    HDA_CMD_SET_CONNECTION_SELECT_CONTROL(w->devinfo->codec->cad,
3268 	    w->nid, index), w->devinfo->codec->cad);
3269 	w->selconn = index;
3270 }
3271 
3272 
3273 /****************************************************************************
3274  * uint32_t hdac_command_sendone_internal
3275  *
3276  * Wrapper function that sends only one command to a given codec
3277  ****************************************************************************/
3278 static uint32_t
3279 hdac_command_sendone_internal(struct hdac_softc *sc, uint32_t verb, nid_t cad)
3280 {
3281 	struct hdac_command_list cl;
3282 	uint32_t response = HDAC_INVALID;
3283 
3284 	if (!hdac_lockowned(sc))
3285 		device_printf(sc->dev, "WARNING!!!! mtx not owned!!!!\n");
3286 	cl.num_commands = 1;
3287 	cl.verbs = &verb;
3288 	cl.responses = &response;
3289 
3290 	hdac_command_send_internal(sc, &cl, cad);
3291 
3292 	return (response);
3293 }
3294 
3295 /****************************************************************************
3296  * hdac_command_send_internal
3297  *
3298  * Send a command list to the codec via the corb. We queue as much verbs as
3299  * we can and msleep on the codec. When the interrupt get the responses
3300  * back from the rirb, it will wake us up so we can queue the remaining verbs
3301  * if any.
3302  ****************************************************************************/
3303 static void
3304 hdac_command_send_internal(struct hdac_softc *sc,
3305 			struct hdac_command_list *commands, nid_t cad)
3306 {
3307 	struct hdac_codec *codec;
3308 	int corbrp;
3309 	uint32_t *corb;
3310 	int timeout;
3311 	int retry = 10;
3312 	struct hdac_rirb *rirb_base;
3313 
3314 	if (sc == NULL || sc->codecs[cad] == NULL || commands == NULL ||
3315 	    commands->num_commands < 1)
3316 		return;
3317 
3318 	codec = sc->codecs[cad];
3319 	codec->commands = commands;
3320 	codec->responses_received = 0;
3321 	codec->verbs_sent = 0;
3322 	corb = (uint32_t *)sc->corb_dma.dma_vaddr;
3323 	rirb_base = (struct hdac_rirb *)sc->rirb_dma.dma_vaddr;
3324 
3325 	do {
3326 		if (codec->verbs_sent != commands->num_commands) {
3327 			/* Queue as many verbs as possible */
3328 			corbrp = HDAC_READ_2(&sc->mem, HDAC_CORBRP);
3329 #if 0
3330 			bus_dmamap_sync(sc->corb_dma.dma_tag,
3331 			    sc->corb_dma.dma_map, BUS_DMASYNC_PREWRITE);
3332 #endif
3333 			while (codec->verbs_sent != commands->num_commands &&
3334 			    ((sc->corb_wp + 1) % sc->corb_size) != corbrp) {
3335 				sc->corb_wp++;
3336 				sc->corb_wp %= sc->corb_size;
3337 				corb[sc->corb_wp] =
3338 				    commands->verbs[codec->verbs_sent++];
3339 			}
3340 
3341 			/* Send the verbs to the codecs */
3342 #if 0
3343 			bus_dmamap_sync(sc->corb_dma.dma_tag,
3344 			    sc->corb_dma.dma_map, BUS_DMASYNC_POSTWRITE);
3345 #endif
3346 			HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
3347 		}
3348 
3349 		timeout = 1000;
3350 		while (hdac_rirb_flush(sc) == 0 && --timeout)
3351 			DELAY(10);
3352 	} while ((codec->verbs_sent != commands->num_commands ||
3353 	    codec->responses_received != commands->num_commands) && --retry);
3354 
3355 	if (retry == 0)
3356 		device_printf(sc->dev,
3357 		    "%s: TIMEOUT numcmd=%d, sent=%d, received=%d\n",
3358 		    __func__, commands->num_commands, codec->verbs_sent,
3359 		    codec->responses_received);
3360 
3361 	codec->commands = NULL;
3362 	codec->responses_received = 0;
3363 	codec->verbs_sent = 0;
3364 
3365 	hdac_unsolq_flush(sc);
3366 }
3367 
3368 
3369 /****************************************************************************
3370  * Device Methods
3371  ****************************************************************************/
3372 
3373 /****************************************************************************
3374  * int hdac_probe(device_t)
3375  *
3376  * Probe for the presence of an hdac. If none is found, check for a generic
3377  * match using the subclass of the device.
3378  ****************************************************************************/
3379 static int
3380 hdac_probe(device_t dev)
3381 {
3382 	int i, result;
3383 	uint32_t model;
3384 	uint16_t class, subclass;
3385 	char desc[64];
3386 
3387 	model = (uint32_t)pci_get_device(dev) << 16;
3388 	model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
3389 	class = pci_get_class(dev);
3390 	subclass = pci_get_subclass(dev);
3391 
3392 	bzero(desc, sizeof(desc));
3393 	result = ENXIO;
3394 	for (i = 0; i < HDAC_DEVICES_LEN; i++) {
3395 		if (hdac_devices[i].model == model) {
3396 		    	strlcpy(desc, hdac_devices[i].desc, sizeof(desc));
3397 		    	result = BUS_PROBE_DEFAULT;
3398 			break;
3399 		}
3400 		if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
3401 		    class == PCIC_MULTIMEDIA &&
3402 		    subclass == PCIS_MULTIMEDIA_HDA) {
3403 		    	strlcpy(desc, hdac_devices[i].desc, sizeof(desc));
3404 		    	result = BUS_PROBE_GENERIC;
3405 			break;
3406 		}
3407 	}
3408 	if (result == ENXIO && class == PCIC_MULTIMEDIA &&
3409 	    subclass == PCIS_MULTIMEDIA_HDA) {
3410 		strlcpy(desc, "Generic", sizeof(desc));
3411 	    	result = BUS_PROBE_GENERIC;
3412 	}
3413 	if (result != ENXIO) {
3414 		strlcat(desc, " High Definition Audio Controller",
3415 		    sizeof(desc));
3416 		device_set_desc_copy(dev, desc);
3417 	}
3418 
3419 	return (result);
3420 }
3421 
3422 static void *
3423 hdac_channel_init(kobj_t obj, void *data, struct snd_dbuf *b,
3424 					struct pcm_channel *c, int dir)
3425 {
3426 	struct hdac_pcm_devinfo *pdevinfo = data;
3427 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
3428 	struct hdac_softc *sc = devinfo->codec->sc;
3429 	struct hdac_chan *ch;
3430 	int i, ord = 0, chid;
3431 
3432 	hdac_lock(sc);
3433 
3434 	chid = (dir == PCMDIR_PLAY)?pdevinfo->play:pdevinfo->rec;
3435 	ch = &sc->chans[chid];
3436 	for (i = 0; i < sc->num_chans && i < chid; i++) {
3437 		if (ch->dir == sc->chans[i].dir)
3438 			ord++;
3439 	}
3440 	if (dir == PCMDIR_PLAY) {
3441 		ch->off = (sc->num_iss + ord) << 5;
3442 	} else {
3443 		ch->off = ord << 5;
3444 	}
3445 
3446 	if (devinfo->function.audio.quirks & HDA_QUIRK_FIXEDRATE) {
3447 		ch->caps.minspeed = ch->caps.maxspeed = 48000;
3448 		ch->pcmrates[0] = 48000;
3449 		ch->pcmrates[1] = 0;
3450 	}
3451 	if (sc->pos_dma.dma_vaddr != NULL)
3452 		ch->dmapos = (uint32_t *)(sc->pos_dma.dma_vaddr +
3453 		    (sc->streamcnt * 8));
3454 	else
3455 		ch->dmapos = NULL;
3456 	ch->sid = ++sc->streamcnt;
3457 	ch->dir = dir;
3458 	ch->b = b;
3459 	ch->c = c;
3460 	ch->blksz = pdevinfo->chan_size / pdevinfo->chan_blkcnt;
3461 	ch->blkcnt = pdevinfo->chan_blkcnt;
3462 	hdac_unlock(sc);
3463 
3464 	if (hdac_bdl_alloc(ch) != 0) {
3465 		ch->blkcnt = 0;
3466 		return (NULL);
3467 	}
3468 
3469 	if (sndbuf_alloc(ch->b, sc->chan_dmat,
3470 	    (sc->flags & HDAC_F_DMA_NOCACHE) ? BUS_DMA_NOCACHE : 0,
3471 	    pdevinfo->chan_size) != 0)
3472 		return (NULL);
3473 
3474 	return (ch);
3475 }
3476 
3477 static int
3478 hdac_channel_setformat(kobj_t obj, void *data, uint32_t format)
3479 {
3480 	struct hdac_chan *ch = data;
3481 	int i;
3482 
3483 	for (i = 0; ch->caps.fmtlist[i] != 0; i++) {
3484 		if (format == ch->caps.fmtlist[i]) {
3485 			ch->fmt = format;
3486 			return (0);
3487 		}
3488 	}
3489 
3490 	return (EINVAL);
3491 }
3492 
3493 static uint32_t
3494 hdac_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
3495 {
3496 	struct hdac_chan *ch = data;
3497 	uint32_t spd = 0, threshold;
3498 	int i;
3499 
3500 	for (i = 0; ch->pcmrates[i] != 0; i++) {
3501 		spd = ch->pcmrates[i];
3502 		threshold = spd + ((ch->pcmrates[i + 1] != 0) ?
3503 		    ((ch->pcmrates[i + 1] - spd) >> 1) : 0);
3504 		if (speed < threshold)
3505 			break;
3506 	}
3507 
3508 	if (spd == 0)	/* impossible */
3509 		ch->spd = 48000;
3510 	else
3511 		ch->spd = spd;
3512 
3513 	return (ch->spd);
3514 }
3515 
3516 static void
3517 hdac_stream_setup(struct hdac_chan *ch)
3518 {
3519 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3520 	struct hdac_audio_as *as = &ch->devinfo->function.audio.as[ch->as];
3521 	struct hdac_widget *w;
3522 	int i, chn, totalchn, c;
3523 	nid_t cad = ch->devinfo->codec->cad;
3524 	uint16_t fmt, dfmt;
3525 	uint16_t chmap[2][5] = {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x0231 }, /* 5.1 */
3526 				{ 0x0010, 0x0001, 0x2001, 0x2031, 0x2431 }};/* 7.1 */
3527 	int map = -1;
3528 
3529 	totalchn = AFMT_CHANNEL(ch->fmt);
3530 	HDA_BOOTHVERBOSE(
3531 		device_printf(ch->pdevinfo->dev,
3532 		    "PCMDIR_%s: Stream setup fmt=%08x speed=%d\n",
3533 		    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
3534 		    ch->fmt, ch->spd);
3535 	);
3536 	fmt = 0;
3537 	if (ch->fmt & AFMT_S16_LE)
3538 		fmt |= ch->bit16 << 4;
3539 	else if (ch->fmt & AFMT_S32_LE)
3540 		fmt |= ch->bit32 << 4;
3541 	else
3542 		fmt |= 1 << 4;
3543 	for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
3544 		if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
3545 			fmt |= hda_rate_tab[i].base;
3546 			fmt |= hda_rate_tab[i].mul;
3547 			fmt |= hda_rate_tab[i].div;
3548 			break;
3549 		}
3550 	}
3551 	fmt |= (totalchn - 1);
3552 
3553 	/* Set channel mapping for known speaker setups. */
3554 	if (as->pinset == 0x0007 || as->pinset == 0x0013) /* Standard 5.1 */
3555 		map = 0;
3556 	else if (as->pinset == 0x0017) /* Standard 7.1 */
3557 		map = 1;
3558 
3559 	HDAC_WRITE_2(&sc->mem, ch->off + HDAC_SDFMT, fmt);
3560 
3561 	dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN;
3562 	if (ch->fmt & AFMT_AC3)
3563 		dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO;
3564 
3565 	chn = 0;
3566 	for (i = 0; ch->io[i] != -1; i++) {
3567 		w = hdac_widget_get(ch->devinfo, ch->io[i]);
3568 		if (w == NULL)
3569 			continue;
3570 
3571 		/* If HP redirection is enabled, but failed to use same
3572 		   DAC, make last DAC to duplicate first one. */
3573 		if (as->fakeredir && i == (as->pincnt - 1)) {
3574 			c = (ch->sid << 4);
3575 		} else {
3576 			if (map >= 0) /* Map known speaker setups. */
3577 				chn = (((chmap[map][totalchn / 2] >> i * 4) &
3578 				    0xf) - 1) * 2;
3579 			if (chn < 0 || chn >= totalchn) {
3580 				c = 0;
3581 			} else {
3582 				c = (ch->sid << 4) | chn;
3583 			}
3584 		}
3585 		HDA_BOOTHVERBOSE(
3586 			device_printf(ch->pdevinfo->dev,
3587 			    "PCMDIR_%s: Stream setup nid=%d: "
3588 			    "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x\n",
3589 			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
3590 			    ch->io[i], fmt, dfmt, c);
3591 		);
3592 		hdac_command(sc,
3593 		    HDA_CMD_SET_CONV_FMT(cad, ch->io[i], fmt), cad);
3594 		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
3595 			hdac_command(sc,
3596 			    HDA_CMD_SET_DIGITAL_CONV_FMT1(cad, ch->io[i], dfmt),
3597 			    cad);
3598 		}
3599 		hdac_command(sc,
3600 		    HDA_CMD_SET_CONV_STREAM_CHAN(cad, ch->io[i], c), cad);
3601 #if 0
3602 		hdac_command(sc,
3603 		    HDA_CMD_SET_CONV_CHAN_COUNT(cad, ch->io[i], 1), cad);
3604 		hdac_command(sc,
3605 		    HDA_CMD_SET_HDMI_CHAN_SLOT(cad, ch->io[i], 0x00), cad);
3606 		hdac_command(sc,
3607 		    HDA_CMD_SET_HDMI_CHAN_SLOT(cad, ch->io[i], 0x11), cad);
3608 #endif
3609 		chn += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
3610 	}
3611 }
3612 
3613 /*
3614  * Greatest Common Divisor.
3615  */
3616 static unsigned
3617 gcd(unsigned a, unsigned b)
3618 {
3619 	u_int c;
3620 
3621 	while (b != 0) {
3622 		c = a;
3623 		a = b;
3624 		b = (c % b);
3625 	}
3626 	return (a);
3627 }
3628 
3629 /*
3630  * Least Common Multiple.
3631  */
3632 static unsigned
3633 lcm(unsigned a, unsigned b)
3634 {
3635 
3636 	return ((a * b) / gcd(a, b));
3637 }
3638 
3639 static int
3640 hdac_channel_setfragments(kobj_t obj, void *data,
3641 					uint32_t blksz, uint32_t blkcnt)
3642 {
3643 	struct hdac_chan *ch = data;
3644 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3645 
3646 	blksz -= blksz % lcm(HDAC_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
3647 
3648 	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
3649 		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
3650 	if (blksz < HDA_BLK_MIN)
3651 		blksz = HDA_BLK_MIN;
3652 	if (blkcnt > HDA_BDL_MAX)
3653 		blkcnt = HDA_BDL_MAX;
3654 	if (blkcnt < HDA_BDL_MIN)
3655 		blkcnt = HDA_BDL_MIN;
3656 
3657 	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
3658 		if ((blkcnt >> 1) >= HDA_BDL_MIN)
3659 			blkcnt >>= 1;
3660 		else if ((blksz >> 1) >= HDA_BLK_MIN)
3661 			blksz >>= 1;
3662 		else
3663 			break;
3664 	}
3665 
3666 	if ((sndbuf_getblksz(ch->b) != blksz ||
3667 	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
3668 	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
3669 		device_printf(sc->dev, "%s: failed blksz=%u blkcnt=%u\n",
3670 		    __func__, blksz, blkcnt);
3671 
3672 	ch->blksz = sndbuf_getblksz(ch->b);
3673 	ch->blkcnt = sndbuf_getblkcnt(ch->b);
3674 
3675 	return (0);
3676 }
3677 
3678 static uint32_t
3679 hdac_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
3680 {
3681 	struct hdac_chan *ch = data;
3682 
3683 	hdac_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
3684 
3685 	return (ch->blksz);
3686 }
3687 
3688 static void
3689 hdac_channel_stop(struct hdac_softc *sc, struct hdac_chan *ch)
3690 {
3691 	struct hdac_devinfo *devinfo = ch->devinfo;
3692 	struct hdac_widget *w;
3693 	nid_t cad = devinfo->codec->cad;
3694 	int i;
3695 
3696 	hdac_stream_stop(ch);
3697 
3698 	for (i = 0; ch->io[i] != -1; i++) {
3699 		w = hdac_widget_get(ch->devinfo, ch->io[i]);
3700 		if (w == NULL)
3701 			continue;
3702 		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
3703 			hdac_command(sc,
3704 			    HDA_CMD_SET_DIGITAL_CONV_FMT1(cad, ch->io[i], 0),
3705 			    cad);
3706 		}
3707 		hdac_command(sc,
3708 		    HDA_CMD_SET_CONV_STREAM_CHAN(cad, ch->io[i],
3709 		    0), cad);
3710 	}
3711 }
3712 
3713 static void
3714 hdac_channel_start(struct hdac_softc *sc, struct hdac_chan *ch)
3715 {
3716 	ch->ptr = 0;
3717 	ch->prevptr = 0;
3718 	hdac_stream_stop(ch);
3719 	hdac_stream_reset(ch);
3720 	hdac_bdl_setup(ch);
3721 	hdac_stream_setid(ch);
3722 	hdac_stream_setup(ch);
3723 	hdac_stream_start(ch);
3724 }
3725 
3726 static int
3727 hdac_channel_trigger(kobj_t obj, void *data, int go)
3728 {
3729 	struct hdac_chan *ch = data;
3730 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3731 
3732 	if (!PCMTRIG_COMMON(go))
3733 		return (0);
3734 
3735 	hdac_lock(sc);
3736 	switch (go) {
3737 	case PCMTRIG_START:
3738 		hdac_channel_start(sc, ch);
3739 		break;
3740 	case PCMTRIG_STOP:
3741 	case PCMTRIG_ABORT:
3742 		hdac_channel_stop(sc, ch);
3743 		break;
3744 	default:
3745 		break;
3746 	}
3747 	hdac_unlock(sc);
3748 
3749 	return (0);
3750 }
3751 
3752 static uint32_t
3753 hdac_channel_getptr(kobj_t obj, void *data)
3754 {
3755 	struct hdac_chan *ch = data;
3756 	struct hdac_softc *sc = ch->devinfo->codec->sc;
3757 	uint32_t ptr;
3758 
3759 	hdac_lock(sc);
3760 	if (sc->polling != 0)
3761 		ptr = ch->ptr;
3762 	else if (ch->dmapos != NULL)
3763 		ptr = *(ch->dmapos);
3764 	else
3765 		ptr = HDAC_READ_4(&sc->mem, ch->off + HDAC_SDLPIB);
3766 	hdac_unlock(sc);
3767 
3768 	/*
3769 	 * Round to available space and force 128 bytes aligment.
3770 	 */
3771 	ptr %= ch->blksz * ch->blkcnt;
3772 	ptr &= HDA_BLK_ALIGN;
3773 
3774 	return (ptr);
3775 }
3776 
3777 static struct pcmchan_caps *
3778 hdac_channel_getcaps(kobj_t obj, void *data)
3779 {
3780 	return (&((struct hdac_chan *)data)->caps);
3781 }
3782 
3783 static kobj_method_t hdac_channel_methods[] = {
3784 	KOBJMETHOD(channel_init,		hdac_channel_init),
3785 	KOBJMETHOD(channel_setformat,		hdac_channel_setformat),
3786 	KOBJMETHOD(channel_setspeed,		hdac_channel_setspeed),
3787 	KOBJMETHOD(channel_setblocksize,	hdac_channel_setblocksize),
3788 	KOBJMETHOD(channel_setfragments,	hdac_channel_setfragments),
3789 	KOBJMETHOD(channel_trigger,		hdac_channel_trigger),
3790 	KOBJMETHOD(channel_getptr,		hdac_channel_getptr),
3791 	KOBJMETHOD(channel_getcaps,		hdac_channel_getcaps),
3792 	KOBJMETHOD_END
3793 };
3794 CHANNEL_DECLARE(hdac_channel);
3795 
3796 static int
3797 hdac_audio_ctl_ossmixer_init(struct snd_mixer *m)
3798 {
3799 	struct hdac_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
3800 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
3801 	struct hdac_softc *sc = devinfo->codec->sc;
3802 	struct hdac_widget *w, *cw;
3803 	struct hdac_audio_ctl *ctl;
3804 	uint32_t mask, recmask, id;
3805 	int i, j, softpcmvol;
3806 
3807 	hdac_lock(sc);
3808 
3809 	/* Make sure that in case of soft volume it won't stay muted. */
3810 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
3811 		pdevinfo->left[i] = 100;
3812 		pdevinfo->right[i] = 100;
3813 	}
3814 
3815 	mask = 0;
3816 	recmask = 0;
3817 	id = hdac_codec_id(devinfo->codec);
3818 
3819 	/* Declate EAPD as ogain control. */
3820 	if (pdevinfo->play >= 0) {
3821 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3822 			w = hdac_widget_get(devinfo, i);
3823 			if (w == NULL || w->enable == 0)
3824 				continue;
3825 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3826 			    w->param.eapdbtl == HDAC_INVALID ||
3827 			    w->bindas != sc->chans[pdevinfo->play].as)
3828 				continue;
3829 			mask |= SOUND_MASK_OGAIN;
3830 			break;
3831 		}
3832 	}
3833 
3834 	/* Declare volume controls assigned to this association. */
3835 	i = 0;
3836 	ctl = NULL;
3837 	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
3838 		if (ctl->enable == 0)
3839 			continue;
3840 		if ((pdevinfo->play >= 0 &&
3841 		    ctl->widget->bindas == sc->chans[pdevinfo->play].as) ||
3842 		    (pdevinfo->rec >= 0 &&
3843 		    ctl->widget->bindas == sc->chans[pdevinfo->rec].as) ||
3844 		    (ctl->widget->bindas == -2 && pdevinfo->index == 0))
3845 			mask |= ctl->ossmask;
3846 	}
3847 
3848 	/* Declare record sources available to this association. */
3849 	if (pdevinfo->rec >= 0) {
3850 		struct hdac_chan *ch = &sc->chans[pdevinfo->rec];
3851 		for (i = 0; ch->io[i] != -1; i++) {
3852 			w = hdac_widget_get(devinfo, ch->io[i]);
3853 			if (w == NULL || w->enable == 0)
3854 				continue;
3855 			for (j = 0; j < w->nconns; j++) {
3856 				if (w->connsenable[j] == 0)
3857 					continue;
3858 				cw = hdac_widget_get(devinfo, w->conns[j]);
3859 				if (cw == NULL || cw->enable == 0)
3860 					continue;
3861 				if (cw->bindas != sc->chans[pdevinfo->rec].as &&
3862 				    cw->bindas != -2)
3863 					continue;
3864 				recmask |= cw->ossmask;
3865 			}
3866 		}
3867 	}
3868 
3869 	/* Declare soft PCM volume if needed. */
3870 	if (pdevinfo->play >= 0) {
3871 		ctl = NULL;
3872 		if ((mask & SOUND_MASK_PCM) == 0 ||
3873 		    (devinfo->function.audio.quirks & HDA_QUIRK_SOFTPCMVOL)) {
3874 			softpcmvol = 1;
3875 			mask |= SOUND_MASK_PCM;
3876 		} else {
3877 			softpcmvol = 0;
3878 			i = 0;
3879 			while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
3880 				if (ctl->enable == 0)
3881 					continue;
3882 				if (ctl->widget->bindas != sc->chans[pdevinfo->play].as &&
3883 				    (ctl->widget->bindas != -2 || pdevinfo->index != 0))
3884 					continue;
3885 				if (!(ctl->ossmask & SOUND_MASK_PCM))
3886 					continue;
3887 				if (ctl->step > 0)
3888 					break;
3889 			}
3890 		}
3891 
3892 		if (softpcmvol == 1 || ctl == NULL) {
3893 			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
3894 			HDA_BOOTVERBOSE(
3895 				device_printf(pdevinfo->dev,
3896 				    "%s Soft PCM volume\n",
3897 				    (softpcmvol == 1) ? "Forcing" : "Enabling");
3898 			);
3899 		}
3900 	}
3901 
3902 	/* Declare master volume if needed. */
3903 	if (pdevinfo->play >= 0) {
3904 		if ((mask & (SOUND_MASK_VOLUME | SOUND_MASK_PCM)) ==
3905 		    SOUND_MASK_PCM) {
3906 			mask |= SOUND_MASK_VOLUME;
3907 			mix_setparentchild(m, SOUND_MIXER_VOLUME,
3908 			    SOUND_MASK_PCM);
3909 			mix_setrealdev(m, SOUND_MIXER_VOLUME,
3910 			    SOUND_MIXER_NONE);
3911 			HDA_BOOTVERBOSE(
3912 				device_printf(pdevinfo->dev,
3913 				    "Forcing master volume with PCM\n");
3914 			);
3915 		}
3916 	}
3917 
3918 	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
3919 	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
3920 
3921 	mix_setrecdevs(m, recmask);
3922 	mix_setdevs(m, mask);
3923 
3924 	hdac_unlock(sc);
3925 
3926 	return (0);
3927 }
3928 
3929 static int
3930 hdac_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
3931 					unsigned left, unsigned right)
3932 {
3933 	struct hdac_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
3934 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
3935 	struct hdac_softc *sc = devinfo->codec->sc;
3936 	struct hdac_widget *w;
3937 	struct hdac_audio_ctl *ctl;
3938 	uint32_t mute;
3939 	int lvol, rvol;
3940 	int i, j;
3941 
3942 	hdac_lock(sc);
3943 	/* Save new values. */
3944 	pdevinfo->left[dev] = left;
3945 	pdevinfo->right[dev] = right;
3946 
3947 	/* 'ogain' is the special case implemented with EAPD. */
3948 	if (dev == SOUND_MIXER_OGAIN) {
3949 		uint32_t orig;
3950 		w = NULL;
3951 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3952 			w = hdac_widget_get(devinfo, i);
3953 			if (w == NULL || w->enable == 0)
3954 				continue;
3955 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3956 			    w->param.eapdbtl == HDAC_INVALID)
3957 				continue;
3958 			break;
3959 		}
3960 		if (i >= devinfo->endnode) {
3961 			hdac_unlock(sc);
3962 			return (-1);
3963 		}
3964 		orig = w->param.eapdbtl;
3965 		if (left == 0)
3966 			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
3967 		else
3968 			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
3969 		if (orig != w->param.eapdbtl) {
3970 			uint32_t val;
3971 
3972 			val = w->param.eapdbtl;
3973 			if (devinfo->function.audio.quirks & HDA_QUIRK_EAPDINV)
3974 				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
3975 			hdac_command(sc,
3976 			    HDA_CMD_SET_EAPD_BTL_ENABLE(devinfo->codec->cad,
3977 			    w->nid, val), devinfo->codec->cad);
3978 		}
3979 		hdac_unlock(sc);
3980 		return (left | (left << 8));
3981 	}
3982 
3983 	/* Recalculate all controls related to this OSS device. */
3984 	i = 0;
3985 	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
3986 		if (ctl->enable == 0 ||
3987 		    !(ctl->ossmask & (1 << dev)))
3988 			continue;
3989 		if (!((pdevinfo->play >= 0 &&
3990 		    ctl->widget->bindas == sc->chans[pdevinfo->play].as) ||
3991 		    (pdevinfo->rec >= 0 &&
3992 		    ctl->widget->bindas == sc->chans[pdevinfo->rec].as) ||
3993 		    ctl->widget->bindas == -2))
3994 			continue;
3995 
3996 		lvol = 100;
3997 		rvol = 100;
3998 		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
3999 			if (ctl->ossmask & (1 << j)) {
4000 				lvol = lvol * pdevinfo->left[j] / 100;
4001 				rvol = rvol * pdevinfo->right[j] / 100;
4002 			}
4003 		}
4004 		mute = (lvol == 0) ? HDA_AMP_MUTE_LEFT : 0;
4005 		mute |= (rvol == 0) ? HDA_AMP_MUTE_RIGHT : 0;
4006 		lvol = (lvol * ctl->step + 50) / 100;
4007 		rvol = (rvol * ctl->step + 50) / 100;
4008 		hdac_audio_ctl_amp_set(ctl, mute, lvol, rvol);
4009 	}
4010 	hdac_unlock(sc);
4011 
4012 	return (left | (right << 8));
4013 }
4014 
4015 /*
4016  * Commutate specified record source.
4017  */
4018 static uint32_t
4019 hdac_audio_ctl_recsel_comm(struct hdac_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
4020 {
4021 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
4022 	struct hdac_widget *w, *cw;
4023 	struct hdac_audio_ctl *ctl;
4024 	char buf[64];
4025 	int i, muted;
4026 	uint32_t res = 0;
4027 
4028 	if (depth > HDA_PARSE_MAXDEPTH)
4029 		return (0);
4030 
4031 	w = hdac_widget_get(devinfo, nid);
4032 	if (w == NULL || w->enable == 0)
4033 		return (0);
4034 
4035 	for (i = 0; i < w->nconns; i++) {
4036 		if (w->connsenable[i] == 0)
4037 			continue;
4038 		cw = hdac_widget_get(devinfo, w->conns[i]);
4039 		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
4040 			continue;
4041 		/* Call recursively to trace signal to it's source if needed. */
4042 		if ((src & cw->ossmask) != 0) {
4043 			if (cw->ossdev < 0) {
4044 				res |= hdac_audio_ctl_recsel_comm(pdevinfo, src,
4045 				    w->conns[i], depth + 1);
4046 			} else {
4047 				res |= cw->ossmask;
4048 			}
4049 		}
4050 		/* We have two special cases: mixers and others (selectors). */
4051 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
4052 			ctl = hdac_audio_ctl_amp_get(devinfo,
4053 			    w->nid, HDA_CTL_IN, i, 1);
4054 			if (ctl == NULL)
4055 				continue;
4056 			/* If we have input control on this node mute them
4057 			 * according to requested sources. */
4058 			muted = (src & cw->ossmask) ? 0 : 1;
4059 	    		if (muted != ctl->forcemute) {
4060 				ctl->forcemute = muted;
4061 				hdac_audio_ctl_amp_set(ctl,
4062 				    HDA_AMP_MUTE_DEFAULT,
4063 				    HDA_AMP_VOL_DEFAULT, HDA_AMP_VOL_DEFAULT);
4064 			}
4065 			HDA_BOOTHVERBOSE(
4066 				device_printf(pdevinfo->dev,
4067 				    "Recsel (%s): nid %d source %d %s\n",
4068 				    hdac_audio_ctl_ossmixer_mask2allname(
4069 				    src, buf, sizeof(buf)),
4070 				    nid, i, muted?"mute":"unmute");
4071 			);
4072 		} else {
4073 			if (w->nconns == 1)
4074 				break;
4075 			if ((src & cw->ossmask) == 0)
4076 				continue;
4077 			/* If we found requested source - select it and exit. */
4078 			hdac_widget_connection_select(w, i);
4079 			HDA_BOOTHVERBOSE(
4080 				device_printf(pdevinfo->dev,
4081 				    "Recsel (%s): nid %d source %d select\n",
4082 				    hdac_audio_ctl_ossmixer_mask2allname(
4083 			    	    src, buf, sizeof(buf)),
4084 				    nid, i);
4085 			);
4086 			break;
4087 		}
4088 	}
4089 	return (res);
4090 }
4091 
4092 static uint32_t
4093 hdac_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
4094 {
4095 	struct hdac_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
4096 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
4097 	struct hdac_widget *w;
4098 	struct hdac_softc *sc = devinfo->codec->sc;
4099 	struct hdac_chan *ch;
4100 	int i;
4101 	uint32_t ret = 0xffffffff;
4102 
4103 	hdac_lock(sc);
4104 
4105 	/* Commutate requested recsrc for each ADC. */
4106 	ch = &sc->chans[pdevinfo->rec];
4107 	for (i = 0; ch->io[i] != -1; i++) {
4108 		w = hdac_widget_get(devinfo, ch->io[i]);
4109 		if (w == NULL || w->enable == 0)
4110 			continue;
4111 		ret &= hdac_audio_ctl_recsel_comm(pdevinfo, src, ch->io[i], 0);
4112 	}
4113 
4114 	hdac_unlock(sc);
4115 
4116 	return ((ret == 0xffffffff)? 0 : ret);
4117 }
4118 
4119 static kobj_method_t hdac_audio_ctl_ossmixer_methods[] = {
4120 	KOBJMETHOD(mixer_init,		hdac_audio_ctl_ossmixer_init),
4121 	KOBJMETHOD(mixer_set,		hdac_audio_ctl_ossmixer_set),
4122 	KOBJMETHOD(mixer_setrecsrc,	hdac_audio_ctl_ossmixer_setrecsrc),
4123 	KOBJMETHOD_END
4124 };
4125 MIXER_DECLARE(hdac_audio_ctl_ossmixer);
4126 
4127 static void
4128 hdac_unsolq_task(void *context, int pending)
4129 {
4130 	struct hdac_softc *sc;
4131 
4132 	sc = (struct hdac_softc *)context;
4133 
4134 	hdac_lock(sc);
4135 	hdac_unsolq_flush(sc);
4136 	hdac_unlock(sc);
4137 }
4138 
4139 /****************************************************************************
4140  * int hdac_attach(device_t)
4141  *
4142  * Attach the device into the kernel. Interrupts usually won't be enabled
4143  * when this function is called. Setup everything that doesn't require
4144  * interrupts and defer probing of codecs until interrupts are enabled.
4145  ****************************************************************************/
4146 static int
4147 hdac_attach(device_t dev)
4148 {
4149 	struct hdac_softc *sc;
4150 	int result;
4151 	int i, devid = -1;
4152 	uint32_t model;
4153 	uint16_t class, subclass;
4154 	uint16_t vendor;
4155 	uint8_t v;
4156 
4157 	HDA_BOOTVERBOSE(
4158 		device_printf(dev, "HDA Driver Revision: %s\n",
4159 		    HDA_DRV_TEST_REV);
4160 	);
4161 
4162 	model = (uint32_t)pci_get_device(dev) << 16;
4163 	model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
4164 	class = pci_get_class(dev);
4165 	subclass = pci_get_subclass(dev);
4166 
4167 	for (i = 0; i < HDAC_DEVICES_LEN; i++) {
4168 		if (hdac_devices[i].model == model) {
4169 			devid = i;
4170 			break;
4171 		}
4172 		if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
4173 		    class == PCIC_MULTIMEDIA &&
4174 		    subclass == PCIS_MULTIMEDIA_HDA) {
4175 			devid = i;
4176 			break;
4177 		}
4178 	}
4179 
4180 	sc = device_get_softc(dev);
4181 	sc->lock = snd_mtxcreate(device_get_nameunit(dev), HDAC_MTX_NAME);
4182 	sc->dev = dev;
4183 	sc->pci_subvendor = (uint32_t)pci_get_subdevice(sc->dev) << 16;
4184 	sc->pci_subvendor |= (uint32_t)pci_get_subvendor(sc->dev) & 0x0000ffff;
4185 	vendor = pci_get_vendor(dev);
4186 
4187 	if (sc->pci_subvendor == HP_NX6325_SUBVENDORX) {
4188 		/* Screw nx6325 - subdevice/subvendor swapped */
4189 		sc->pci_subvendor = HP_NX6325_SUBVENDOR;
4190 	}
4191 
4192 	callout_init(&sc->poll_hda, CALLOUT_MPSAFE);
4193 	callout_init(&sc->poll_hdac, CALLOUT_MPSAFE);
4194 	callout_init(&sc->poll_jack, CALLOUT_MPSAFE);
4195 
4196 	TASK_INIT(&sc->unsolq_task, 0, hdac_unsolq_task, sc);
4197 
4198 	sc->poll_ticks = 1000000;
4199 	sc->poll_ival = HDAC_POLL_INTERVAL;
4200 	if (resource_int_value(device_get_name(dev),
4201 	    device_get_unit(dev), "polling", &i) == 0 && i != 0)
4202 		sc->polling = 1;
4203 	else
4204 		sc->polling = 0;
4205 
4206 	sc->hdabus = NULL;
4207 	for (i = 0; i < HDAC_CODEC_MAX; i++)
4208 		sc->codecs[i] = NULL;
4209 
4210 	pci_enable_busmaster(dev);
4211 
4212 	if (vendor == INTEL_VENDORID) {
4213 		/* TCSEL -> TC0 */
4214 		v = pci_read_config(dev, 0x44, 1);
4215 		pci_write_config(dev, 0x44, v & 0xf8, 1);
4216 		HDA_BOOTHVERBOSE(
4217 			device_printf(dev, "TCSEL: 0x%02d -> 0x%02d\n", v,
4218 			    pci_read_config(dev, 0x44, 1));
4219 		);
4220 	}
4221 
4222 	if (devid >= 0 && (hdac_devices[devid].flags & HDAC_NO_MSI))
4223 		sc->flags &= ~HDAC_F_MSI;
4224 	else
4225 		sc->flags |= HDAC_F_MSI;
4226 	if (resource_int_value(device_get_name(dev),
4227 	    device_get_unit(dev), "msi", &i) == 0) {
4228 		if (i == 0)
4229 			sc->flags &= ~HDAC_F_MSI;
4230 		else
4231 			sc->flags |= HDAC_F_MSI;
4232 	}
4233 
4234 #if defined(__i386__) || defined(__amd64__)
4235 	sc->flags |= HDAC_F_DMA_NOCACHE;
4236 
4237 	if (resource_int_value(device_get_name(dev),
4238 	    device_get_unit(dev), "snoop", &i) == 0 && i != 0) {
4239 #else
4240 	sc->flags &= ~HDAC_F_DMA_NOCACHE;
4241 #endif
4242 		/*
4243 		 * Try to enable PCIe snoop to avoid messing around with
4244 		 * uncacheable DMA attribute. Since PCIe snoop register
4245 		 * config is pretty much vendor specific, there are no
4246 		 * general solutions on how to enable it, forcing us (even
4247 		 * Microsoft) to enable uncacheable or write combined DMA
4248 		 * by default.
4249 		 *
4250 		 * http://msdn2.microsoft.com/en-us/library/ms790324.aspx
4251 		 */
4252 		for (i = 0; i < HDAC_PCIESNOOP_LEN; i++) {
4253 			if (hdac_pcie_snoop[i].vendor != vendor)
4254 				continue;
4255 			sc->flags &= ~HDAC_F_DMA_NOCACHE;
4256 			if (hdac_pcie_snoop[i].reg == 0x00)
4257 				break;
4258 			v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
4259 			if ((v & hdac_pcie_snoop[i].enable) ==
4260 			    hdac_pcie_snoop[i].enable)
4261 				break;
4262 			v &= hdac_pcie_snoop[i].mask;
4263 			v |= hdac_pcie_snoop[i].enable;
4264 			pci_write_config(dev, hdac_pcie_snoop[i].reg, v, 1);
4265 			v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
4266 			if ((v & hdac_pcie_snoop[i].enable) !=
4267 			    hdac_pcie_snoop[i].enable) {
4268 				HDA_BOOTVERBOSE(
4269 					device_printf(dev,
4270 					    "WARNING: Failed to enable PCIe "
4271 					    "snoop!\n");
4272 				);
4273 #if defined(__i386__) || defined(__amd64__)
4274 				sc->flags |= HDAC_F_DMA_NOCACHE;
4275 #endif
4276 			}
4277 			break;
4278 		}
4279 #if defined(__i386__) || defined(__amd64__)
4280 	}
4281 #endif
4282 
4283 	HDA_BOOTHVERBOSE(
4284 		device_printf(dev, "DMA Coherency: %s / vendor=0x%04x\n",
4285 		    (sc->flags & HDAC_F_DMA_NOCACHE) ?
4286 		    "Uncacheable" : "PCIe snoop", vendor);
4287 	);
4288 
4289 	/* Allocate resources */
4290 	result = hdac_mem_alloc(sc);
4291 	if (result != 0)
4292 		goto hdac_attach_fail;
4293 	result = hdac_irq_alloc(sc);
4294 	if (result != 0)
4295 		goto hdac_attach_fail;
4296 
4297 	/* Get Capabilities */
4298 	result = hdac_get_capabilities(sc);
4299 	if (result != 0)
4300 		goto hdac_attach_fail;
4301 
4302 	if (devid >= 0 && (hdac_devices[devid].flags & HDAC_NO_64BIT))
4303 		sc->support_64bit = 0;
4304 
4305 	/* Allocate CORB and RIRB dma memory */
4306 	result = hdac_dma_alloc(sc, &sc->corb_dma,
4307 	    sc->corb_size * sizeof(uint32_t));
4308 	if (result != 0)
4309 		goto hdac_attach_fail;
4310 	result = hdac_dma_alloc(sc, &sc->rirb_dma,
4311 	    sc->rirb_size * sizeof(struct hdac_rirb));
4312 	if (result != 0)
4313 		goto hdac_attach_fail;
4314 
4315 	result = bus_dma_tag_create(
4316 	    bus_get_dma_tag(sc->dev),		/* parent */
4317 	    HDAC_DMA_ALIGNMENT,			/* alignment */
4318 	    0,					/* boundary */
4319 	    (sc->support_64bit) ? BUS_SPACE_MAXADDR :
4320 		BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
4321 	    BUS_SPACE_MAXADDR,			/* highaddr */
4322 	    NULL,				/* filtfunc */
4323 	    NULL,				/* fistfuncarg */
4324 	    HDA_BUFSZ_MAX, 			/* maxsize */
4325 	    1,					/* nsegments */
4326 	    HDA_BUFSZ_MAX, 			/* maxsegsz */
4327 	    0,					/* flags */
4328 	    NULL,				/* lockfunc */
4329 	    NULL,				/* lockfuncarg */
4330 	    &sc->chan_dmat);			/* dmat */
4331 	if (result != 0) {
4332 		device_printf(dev, "%s: bus_dma_tag_create failed (%x)\n",
4333 		     __func__, result);
4334 		goto hdac_attach_fail;
4335 	}
4336 
4337 	/* Quiesce everything */
4338 	HDA_BOOTHVERBOSE(
4339 		device_printf(dev, "Reset controller...\n");
4340 	);
4341 	hdac_reset(sc, 1);
4342 
4343 	/* Initialize the CORB and RIRB */
4344 	hdac_corb_init(sc);
4345 	hdac_rirb_init(sc);
4346 
4347 	/* Defer remaining of initialization until interrupts are enabled */
4348 	sc->intrhook.ich_func = hdac_attach2;
4349 	sc->intrhook.ich_arg = (void *)sc;
4350 	if (cold == 0 || config_intrhook_establish(&sc->intrhook) != 0) {
4351 		sc->intrhook.ich_func = NULL;
4352 		hdac_attach2((void *)sc);
4353 	}
4354 
4355 	return (0);
4356 
4357 hdac_attach_fail:
4358 	hdac_irq_free(sc);
4359 	hdac_dma_free(sc, &sc->rirb_dma);
4360 	hdac_dma_free(sc, &sc->corb_dma);
4361 	hdac_mem_free(sc);
4362 	snd_mtxfree(sc->lock);
4363 
4364 	return (ENXIO);
4365 }
4366 
4367 static void
4368 hdac_audio_parse(struct hdac_devinfo *devinfo)
4369 {
4370 	struct hdac_codec *codec = devinfo->codec;
4371 	struct hdac_softc *sc = codec->sc;
4372 	struct hdac_widget *w;
4373 	uint32_t res;
4374 	int i;
4375 	nid_t cad, nid;
4376 
4377 	cad = devinfo->codec->cad;
4378 	nid = devinfo->nid;
4379 
4380 	res = hdac_command(sc,
4381 	    HDA_CMD_GET_PARAMETER(cad , nid, HDA_PARAM_GPIO_COUNT), cad);
4382 	devinfo->function.audio.gpio = res;
4383 
4384 	HDA_BOOTVERBOSE(
4385 		device_printf(sc->dev, "GPIO: 0x%08x "
4386 		    "NumGPIO=%d NumGPO=%d "
4387 		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
4388 		    devinfo->function.audio.gpio,
4389 		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->function.audio.gpio),
4390 		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->function.audio.gpio),
4391 		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->function.audio.gpio),
4392 		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->function.audio.gpio),
4393 		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->function.audio.gpio));
4394 	);
4395 
4396 	res = hdac_command(sc,
4397 	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_SUPP_STREAM_FORMATS),
4398 	    cad);
4399 	devinfo->function.audio.supp_stream_formats = res;
4400 
4401 	res = hdac_command(sc,
4402 	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE),
4403 	    cad);
4404 	devinfo->function.audio.supp_pcm_size_rate = res;
4405 
4406 	res = hdac_command(sc,
4407 	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_OUTPUT_AMP_CAP),
4408 	    cad);
4409 	devinfo->function.audio.outamp_cap = res;
4410 
4411 	res = hdac_command(sc,
4412 	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_INPUT_AMP_CAP),
4413 	    cad);
4414 	devinfo->function.audio.inamp_cap = res;
4415 
4416 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4417 		w = hdac_widget_get(devinfo, i);
4418 		if (w == NULL)
4419 			device_printf(sc->dev, "Ghost widget! nid=%d!\n", i);
4420 		else {
4421 			w->devinfo = devinfo;
4422 			w->nid = i;
4423 			w->enable = 1;
4424 			w->selconn = -1;
4425 			w->pflags = 0;
4426 			w->ossdev = -1;
4427 			w->bindas = -1;
4428 			w->param.eapdbtl = HDAC_INVALID;
4429 			hdac_widget_parse(w);
4430 		}
4431 	}
4432 }
4433 
4434 static void
4435 hdac_audio_ctl_parse(struct hdac_devinfo *devinfo)
4436 {
4437 	struct hdac_softc *sc = devinfo->codec->sc;
4438 	struct hdac_audio_ctl *ctls;
4439 	struct hdac_widget *w, *cw;
4440 	int i, j, cnt, max, ocap, icap;
4441 	int mute, offset, step, size;
4442 
4443 	/* XXX This is redundant */
4444 	max = 0;
4445 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4446 		w = hdac_widget_get(devinfo, i);
4447 		if (w == NULL || w->enable == 0)
4448 			continue;
4449 		if (w->param.outamp_cap != 0)
4450 			max++;
4451 		if (w->param.inamp_cap != 0) {
4452 			switch (w->type) {
4453 			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
4454 			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
4455 				for (j = 0; j < w->nconns; j++) {
4456 					cw = hdac_widget_get(devinfo,
4457 					    w->conns[j]);
4458 					if (cw == NULL || cw->enable == 0)
4459 						continue;
4460 					max++;
4461 				}
4462 				break;
4463 			default:
4464 				max++;
4465 				break;
4466 			}
4467 		}
4468 	}
4469 
4470 	devinfo->function.audio.ctlcnt = max;
4471 
4472 	if (max < 1)
4473 		return;
4474 
4475 	ctls = (struct hdac_audio_ctl *)malloc(
4476 	    sizeof(*ctls) * max, M_HDAC, M_ZERO | M_NOWAIT);
4477 
4478 	if (ctls == NULL) {
4479 		/* Blekh! */
4480 		device_printf(sc->dev, "unable to allocate ctls!\n");
4481 		devinfo->function.audio.ctlcnt = 0;
4482 		return;
4483 	}
4484 
4485 	cnt = 0;
4486 	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
4487 		if (cnt >= max) {
4488 			device_printf(sc->dev, "%s: Ctl overflow!\n",
4489 			    __func__);
4490 			break;
4491 		}
4492 		w = hdac_widget_get(devinfo, i);
4493 		if (w == NULL || w->enable == 0)
4494 			continue;
4495 		ocap = w->param.outamp_cap;
4496 		icap = w->param.inamp_cap;
4497 		if (ocap != 0) {
4498 			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
4499 			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
4500 			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
4501 			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
4502 			/*if (offset > step) {
4503 				HDA_BOOTVERBOSE(
4504 					device_printf(sc->dev,
4505 					    "BUGGY outamp: nid=%d "
4506 					    "[offset=%d > step=%d]\n",
4507 					    w->nid, offset, step);
4508 				);
4509 				offset = step;
4510 			}*/
4511 			ctls[cnt].enable = 1;
4512 			ctls[cnt].widget = w;
4513 			ctls[cnt].mute = mute;
4514 			ctls[cnt].step = step;
4515 			ctls[cnt].size = size;
4516 			ctls[cnt].offset = offset;
4517 			ctls[cnt].left = offset;
4518 			ctls[cnt].right = offset;
4519 			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
4520 			    w->waspin)
4521 				ctls[cnt].ndir = HDA_CTL_IN;
4522 			else
4523 				ctls[cnt].ndir = HDA_CTL_OUT;
4524 			ctls[cnt++].dir = HDA_CTL_OUT;
4525 		}
4526 
4527 		if (icap != 0) {
4528 			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
4529 			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
4530 			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
4531 			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
4532 			/*if (offset > step) {
4533 				HDA_BOOTVERBOSE(
4534 					device_printf(sc->dev,
4535 					    "BUGGY inamp: nid=%d "
4536 					    "[offset=%d > step=%d]\n",
4537 					    w->nid, offset, step);
4538 				);
4539 				offset = step;
4540 			}*/
4541 			switch (w->type) {
4542 			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
4543 			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
4544 				for (j = 0; j < w->nconns; j++) {
4545 					if (cnt >= max) {
4546 						device_printf(sc->dev,
4547 						    "%s: Ctl overflow!\n",
4548 						    __func__);
4549 						break;
4550 					}
4551 					cw = hdac_widget_get(devinfo,
4552 					    w->conns[j]);
4553 					if (cw == NULL || cw->enable == 0)
4554 						continue;
4555 					ctls[cnt].enable = 1;
4556 					ctls[cnt].widget = w;
4557 					ctls[cnt].childwidget = cw;
4558 					ctls[cnt].index = j;
4559 					ctls[cnt].mute = mute;
4560 					ctls[cnt].step = step;
4561 					ctls[cnt].size = size;
4562 					ctls[cnt].offset = offset;
4563 					ctls[cnt].left = offset;
4564 					ctls[cnt].right = offset;
4565 	    				ctls[cnt].ndir = HDA_CTL_IN;
4566 					ctls[cnt++].dir = HDA_CTL_IN;
4567 				}
4568 				break;
4569 			default:
4570 				if (cnt >= max) {
4571 					device_printf(sc->dev,
4572 					    "%s: Ctl overflow!\n",
4573 					    __func__);
4574 					break;
4575 				}
4576 				ctls[cnt].enable = 1;
4577 				ctls[cnt].widget = w;
4578 				ctls[cnt].mute = mute;
4579 				ctls[cnt].step = step;
4580 				ctls[cnt].size = size;
4581 				ctls[cnt].offset = offset;
4582 				ctls[cnt].left = offset;
4583 				ctls[cnt].right = offset;
4584 				if (w->type ==
4585 				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4586 					ctls[cnt].ndir = HDA_CTL_OUT;
4587 				else
4588 					ctls[cnt].ndir = HDA_CTL_IN;
4589 				ctls[cnt++].dir = HDA_CTL_IN;
4590 				break;
4591 			}
4592 		}
4593 	}
4594 
4595 	devinfo->function.audio.ctl = ctls;
4596 }
4597 
4598 static void
4599 hdac_audio_as_parse(struct hdac_devinfo *devinfo)
4600 {
4601 	struct hdac_softc *sc = devinfo->codec->sc;
4602 	struct hdac_audio_as *as;
4603 	struct hdac_widget *w;
4604 	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
4605 
4606 	/* Count present associations */
4607 	max = 0;
4608 	for (j = 1; j < 16; j++) {
4609 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4610 			w = hdac_widget_get(devinfo, i);
4611 			if (w == NULL || w->enable == 0)
4612 				continue;
4613 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4614 				continue;
4615 			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
4616 			    != j)
4617 				continue;
4618 			max++;
4619 			if (j != 15)  /* There could be many 1-pin assocs #15 */
4620 				break;
4621 		}
4622 	}
4623 
4624 	devinfo->function.audio.ascnt = max;
4625 
4626 	if (max < 1)
4627 		return;
4628 
4629 	as = (struct hdac_audio_as *)malloc(
4630 	    sizeof(*as) * max, M_HDAC, M_ZERO | M_NOWAIT);
4631 
4632 	if (as == NULL) {
4633 		/* Blekh! */
4634 		device_printf(sc->dev, "unable to allocate assocs!\n");
4635 		devinfo->function.audio.ascnt = 0;
4636 		return;
4637 	}
4638 
4639 	for (i = 0; i < max; i++) {
4640 		as[i].hpredir = -1;
4641 		as[i].chan = -1;
4642 		as[i].digital = 0;
4643 	}
4644 
4645 	/* Scan associations skipping as=0. */
4646 	cnt = 0;
4647 	for (j = 1; j < 16; j++) {
4648 		first = 16;
4649 		hpredir = 0;
4650 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4651 			w = hdac_widget_get(devinfo, i);
4652 			if (w == NULL || w->enable == 0)
4653 				continue;
4654 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4655 				continue;
4656 			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
4657 			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
4658 			if (assoc != j) {
4659 				continue;
4660 			}
4661 			KASSERT(cnt < max,
4662 			    ("%s: Associations owerflow (%d of %d)",
4663 			    __func__, cnt, max));
4664 			type = w->wclass.pin.config &
4665 			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
4666 			/* Get pin direction. */
4667 			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
4668 			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
4669 			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
4670 			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
4671 			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
4672 				dir = HDA_CTL_OUT;
4673 			else
4674 				dir = HDA_CTL_IN;
4675 			/* If this is a first pin - create new association. */
4676 			if (as[cnt].pincnt == 0) {
4677 				as[cnt].enable = 1;
4678 				as[cnt].index = j;
4679 				as[cnt].dir = dir;
4680 			}
4681 			if (seq < first)
4682 				first = seq;
4683 			/* Check association correctness. */
4684 			if (as[cnt].pins[seq] != 0) {
4685 				device_printf(sc->dev, "%s: Duplicate pin %d (%d) "
4686 				    "in association %d! Disabling association.\n",
4687 				    __func__, seq, w->nid, j);
4688 				as[cnt].enable = 0;
4689 			}
4690 			if (dir != as[cnt].dir) {
4691 				device_printf(sc->dev, "%s: Pin %d has wrong "
4692 				    "direction for association %d! Disabling "
4693 				    "association.\n",
4694 				    __func__, w->nid, j);
4695 				as[cnt].enable = 0;
4696 			}
4697 			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
4698 				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
4699 					as[cnt].digital = 3;
4700 				else if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
4701 					as[cnt].digital = 2;
4702 				else
4703 					as[cnt].digital = 1;
4704 			}
4705 			/* Headphones with seq=15 may mean redirection. */
4706 			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
4707 			    seq == 15)
4708 				hpredir = 1;
4709 			as[cnt].pins[seq] = w->nid;
4710 			as[cnt].pincnt++;
4711 			/* Association 15 is a multiple unassociated pins. */
4712 			if (j == 15)
4713 				cnt++;
4714 		}
4715 		if (j != 15 && as[cnt].pincnt > 0) {
4716 			if (hpredir && as[cnt].pincnt > 1)
4717 				as[cnt].hpredir = first;
4718 			cnt++;
4719 		}
4720 	}
4721 	HDA_BOOTVERBOSE(
4722 		device_printf(sc->dev,
4723 		    "%d associations found:\n", max);
4724 		for (i = 0; i < max; i++) {
4725 			device_printf(sc->dev,
4726 			    "Association %d (%d) %s%s:\n",
4727 			    i, as[i].index, (as[i].dir == HDA_CTL_IN)?"in":"out",
4728 			    as[i].enable?"":" (disabled)");
4729 			for (j = 0; j < 16; j++) {
4730 				if (as[i].pins[j] == 0)
4731 					continue;
4732 				device_printf(sc->dev,
4733 				    " Pin nid=%d seq=%d\n",
4734 				    as[i].pins[j], j);
4735 			}
4736 		}
4737 	);
4738 
4739 	devinfo->function.audio.as = as;
4740 }
4741 
4742 static const struct {
4743 	uint32_t model;
4744 	uint32_t id;
4745 	uint32_t set, unset;
4746 } hdac_quirks[] = {
4747 	/*
4748 	 * XXX Force stereo quirk. Monoural recording / playback
4749 	 *     on few codecs (especially ALC880) seems broken or
4750 	 *     perhaps unsupported.
4751 	 */
4752 	{ HDA_MATCH_ALL, HDA_MATCH_ALL,
4753 	    HDA_QUIRK_FORCESTEREO | HDA_QUIRK_IVREF, 0 },
4754 	{ ACER_ALL_SUBVENDOR, HDA_MATCH_ALL,
4755 	    HDA_QUIRK_GPIO0, 0 },
4756 	{ ASUS_G2K_SUBVENDOR, HDA_CODEC_ALC660,
4757 	    HDA_QUIRK_GPIO0, 0 },
4758 	{ ASUS_M5200_SUBVENDOR, HDA_CODEC_ALC880,
4759 	    HDA_QUIRK_GPIO0, 0 },
4760 	{ ASUS_A7M_SUBVENDOR, HDA_CODEC_ALC880,
4761 	    HDA_QUIRK_GPIO0, 0 },
4762 	{ ASUS_A7T_SUBVENDOR, HDA_CODEC_ALC882,
4763 	    HDA_QUIRK_GPIO0, 0 },
4764 	{ ASUS_W2J_SUBVENDOR, HDA_CODEC_ALC882,
4765 	    HDA_QUIRK_GPIO0, 0 },
4766 	{ ASUS_U5F_SUBVENDOR, HDA_CODEC_AD1986A,
4767 	    HDA_QUIRK_EAPDINV, 0 },
4768 	{ ASUS_A8X_SUBVENDOR, HDA_CODEC_AD1986A,
4769 	    HDA_QUIRK_EAPDINV, 0 },
4770 	{ ASUS_F3JC_SUBVENDOR, HDA_CODEC_ALC861,
4771 	    HDA_QUIRK_OVREF, 0 },
4772 	{ UNIWILL_9075_SUBVENDOR, HDA_CODEC_ALC861,
4773 	    HDA_QUIRK_OVREF, 0 },
4774 	/*{ ASUS_M2N_SUBVENDOR, HDA_CODEC_AD1988,
4775 	    HDA_QUIRK_IVREF80, HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF100 },*/
4776 	{ MEDION_MD95257_SUBVENDOR, HDA_CODEC_ALC880,
4777 	    HDA_QUIRK_GPIO1, 0 },
4778 	{ LENOVO_3KN100_SUBVENDOR, HDA_CODEC_AD1986A,
4779 	    HDA_QUIRK_EAPDINV | HDA_QUIRK_SENSEINV, 0 },
4780 	{ SAMSUNG_Q1_SUBVENDOR, HDA_CODEC_AD1986A,
4781 	    HDA_QUIRK_EAPDINV, 0 },
4782 	{ APPLE_MB3_SUBVENDOR, HDA_CODEC_ALC885,
4783 	    HDA_QUIRK_GPIO0 | HDA_QUIRK_OVREF50, 0},
4784 	{ APPLE_INTEL_MAC, HDA_CODEC_STAC9221,
4785 	    HDA_QUIRK_GPIO0 | HDA_QUIRK_GPIO1, 0 },
4786 	{ APPLE_MACBOOKPRO55, HDA_CODEC_CS4206,
4787 	    HDA_QUIRK_GPIO1 | HDA_QUIRK_GPIO3, 0 },
4788 	{ DELL_D630_SUBVENDOR, HDA_CODEC_STAC9205X,
4789 	    HDA_QUIRK_GPIO0, 0 },
4790 	{ DELL_V1400_SUBVENDOR, HDA_CODEC_STAC9228X,
4791 	    HDA_QUIRK_GPIO2, 0 },
4792 	{ DELL_V1500_SUBVENDOR, HDA_CODEC_STAC9205X,
4793 	    HDA_QUIRK_GPIO0, 0 },
4794 	{ HDA_MATCH_ALL, HDA_CODEC_AD1988,
4795 	    HDA_QUIRK_IVREF80, HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF100 },
4796 	{ HDA_MATCH_ALL, HDA_CODEC_AD1988B,
4797 	    HDA_QUIRK_IVREF80, HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF100 },
4798 	{ HDA_MATCH_ALL, HDA_CODEC_CX20549,
4799 	    0, HDA_QUIRK_FORCESTEREO }
4800 };
4801 #define HDAC_QUIRKS_LEN (sizeof(hdac_quirks) / sizeof(hdac_quirks[0]))
4802 
4803 static void
4804 hdac_vendor_patch_parse(struct hdac_devinfo *devinfo)
4805 {
4806 	struct hdac_widget *w;
4807 	uint32_t id, subvendor;
4808 	int i;
4809 
4810 	id = hdac_codec_id(devinfo->codec);
4811 	subvendor = devinfo->codec->sc->pci_subvendor;
4812 
4813 	/*
4814 	 * Quirks
4815 	 */
4816 	for (i = 0; i < HDAC_QUIRKS_LEN; i++) {
4817 		if (!(HDA_DEV_MATCH(hdac_quirks[i].model, subvendor) &&
4818 		    HDA_DEV_MATCH(hdac_quirks[i].id, id)))
4819 			continue;
4820 		if (hdac_quirks[i].set != 0)
4821 			devinfo->function.audio.quirks |=
4822 			    hdac_quirks[i].set;
4823 		if (hdac_quirks[i].unset != 0)
4824 			devinfo->function.audio.quirks &=
4825 			    ~(hdac_quirks[i].unset);
4826 	}
4827 
4828 	switch (id) {
4829 	case HDA_CODEC_AD1983:
4830 		/*
4831 		 * This codec has several possible usages, but none
4832 		 * fit the parser best. Help parser to choose better.
4833 		 */
4834 		/* Disable direct unmixed playback to get pcm volume. */
4835 		w = hdac_widget_get(devinfo, 5);
4836 		if (w != NULL)
4837 			w->connsenable[0] = 0;
4838 		w = hdac_widget_get(devinfo, 6);
4839 		if (w != NULL)
4840 			w->connsenable[0] = 0;
4841 		w = hdac_widget_get(devinfo, 11);
4842 		if (w != NULL)
4843 			w->connsenable[0] = 0;
4844 		/* Disable mic and line selectors. */
4845 		w = hdac_widget_get(devinfo, 12);
4846 		if (w != NULL)
4847 			w->connsenable[1] = 0;
4848 		w = hdac_widget_get(devinfo, 13);
4849 		if (w != NULL)
4850 			w->connsenable[1] = 0;
4851 		/* Disable recording from mono playback mix. */
4852 		w = hdac_widget_get(devinfo, 20);
4853 		if (w != NULL)
4854 			w->connsenable[3] = 0;
4855 		break;
4856 	case HDA_CODEC_AD1986A:
4857 		/*
4858 		 * This codec has overcomplicated input mixing.
4859 		 * Make some cleaning there.
4860 		 */
4861 		/* Disable input mono mixer. Not needed and not supported. */
4862 		w = hdac_widget_get(devinfo, 43);
4863 		if (w != NULL)
4864 			w->enable = 0;
4865 		/* Disable any with any input mixing mesh. Use separately. */
4866 		w = hdac_widget_get(devinfo, 39);
4867 		if (w != NULL)
4868 			w->enable = 0;
4869 		w = hdac_widget_get(devinfo, 40);
4870 		if (w != NULL)
4871 			w->enable = 0;
4872 		w = hdac_widget_get(devinfo, 41);
4873 		if (w != NULL)
4874 			w->enable = 0;
4875 		w = hdac_widget_get(devinfo, 42);
4876 		if (w != NULL)
4877 			w->enable = 0;
4878 		/* Disable duplicate mixer node connector. */
4879 		w = hdac_widget_get(devinfo, 15);
4880 		if (w != NULL)
4881 			w->connsenable[3] = 0;
4882 		/* There is only one mic preamplifier, use it effectively. */
4883 		w = hdac_widget_get(devinfo, 31);
4884 		if (w != NULL) {
4885 			if ((w->wclass.pin.config &
4886 			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
4887 			    HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN) {
4888 				w = hdac_widget_get(devinfo, 16);
4889 				if (w != NULL)
4890 				    w->connsenable[2] = 0;
4891 			} else {
4892 				w = hdac_widget_get(devinfo, 15);
4893 				if (w != NULL)
4894 				    w->connsenable[0] = 0;
4895 			}
4896 		}
4897 		w = hdac_widget_get(devinfo, 32);
4898 		if (w != NULL) {
4899 			if ((w->wclass.pin.config &
4900 			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
4901 			    HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN) {
4902 				w = hdac_widget_get(devinfo, 16);
4903 				if (w != NULL)
4904 				    w->connsenable[0] = 0;
4905 			} else {
4906 				w = hdac_widget_get(devinfo, 15);
4907 				if (w != NULL)
4908 				    w->connsenable[1] = 0;
4909 			}
4910 		}
4911 
4912 		if (subvendor == ASUS_A8X_SUBVENDOR) {
4913 			/*
4914 			 * This is just plain ridiculous.. There
4915 			 * are several A8 series that share the same
4916 			 * pci id but works differently (EAPD).
4917 			 */
4918 			w = hdac_widget_get(devinfo, 26);
4919 			if (w != NULL && w->type ==
4920 			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4921 			    (w->wclass.pin.config &
4922 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) !=
4923 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE)
4924 				devinfo->function.audio.quirks &=
4925 				    ~HDA_QUIRK_EAPDINV;
4926 		}
4927 		break;
4928 	case HDA_CODEC_AD1981HD:
4929 		/*
4930 		 * This codec has very unusual design with several
4931 		 * points inappropriate for the present parser.
4932 		 */
4933 		/* Disable recording from mono playback mix. */
4934 		w = hdac_widget_get(devinfo, 21);
4935 		if (w != NULL)
4936 			w->connsenable[3] = 0;
4937 		/* Disable rear to front mic mixer, use separately. */
4938 		w = hdac_widget_get(devinfo, 31);
4939 		if (w != NULL)
4940 			w->enable = 0;
4941 		/* Disable direct playback, use mixer. */
4942 		w = hdac_widget_get(devinfo, 5);
4943 		if (w != NULL)
4944 			w->connsenable[0] = 0;
4945 		w = hdac_widget_get(devinfo, 6);
4946 		if (w != NULL)
4947 			w->connsenable[0] = 0;
4948 		w = hdac_widget_get(devinfo, 9);
4949 		if (w != NULL)
4950 			w->connsenable[0] = 0;
4951 		w = hdac_widget_get(devinfo, 24);
4952 		if (w != NULL)
4953 			w->connsenable[0] = 0;
4954 		break;
4955 	case HDA_CODEC_CX20582:
4956 	case HDA_CODEC_CX20583:
4957 	case HDA_CODEC_CX20584:
4958 	case HDA_CODEC_CX20585:
4959 	case HDA_CODEC_CX20590:
4960 		/*
4961 		 * These codecs have extra connectivity on record side
4962 		 * too reach for the present parser.
4963 		 */
4964 		w = hdac_widget_get(devinfo, 20);
4965 		if (w != NULL)
4966 			w->connsenable[1] = 0;
4967 		w = hdac_widget_get(devinfo, 21);
4968 		if (w != NULL)
4969 			w->connsenable[1] = 0;
4970 		w = hdac_widget_get(devinfo, 22);
4971 		if (w != NULL)
4972 			w->connsenable[0] = 0;
4973 		break;
4974 	case HDA_CODEC_VT1708S_0:
4975 	case HDA_CODEC_VT1708S_1:
4976 	case HDA_CODEC_VT1708S_2:
4977 	case HDA_CODEC_VT1708S_3:
4978 	case HDA_CODEC_VT1708S_4:
4979 	case HDA_CODEC_VT1708S_5:
4980 	case HDA_CODEC_VT1708S_6:
4981 	case HDA_CODEC_VT1708S_7:
4982 		/*
4983 		 * These codecs have hidden mic boost controls.
4984 		 */
4985 		w = hdac_widget_get(devinfo, 26);
4986 		if (w != NULL)
4987 			w->param.inamp_cap =
4988 			    (40 << HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE_SHIFT) |
4989 			    (3 << HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS_SHIFT) |
4990 			    (0 << HDA_PARAM_OUTPUT_AMP_CAP_OFFSET_SHIFT);
4991 		w = hdac_widget_get(devinfo, 30);
4992 		if (w != NULL)
4993 			w->param.inamp_cap =
4994 			    (40 << HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE_SHIFT) |
4995 			    (3 << HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS_SHIFT) |
4996 			    (0 << HDA_PARAM_OUTPUT_AMP_CAP_OFFSET_SHIFT);
4997 		break;
4998 	}
4999 }
5000 
5001 /*
5002  * Trace path from DAC to pin.
5003  */
5004 static nid_t
5005 hdac_audio_trace_dac(struct hdac_devinfo *devinfo, int as, int seq, nid_t nid,
5006     int dupseq, int min, int only, int depth)
5007 {
5008 	struct hdac_widget *w;
5009 	int i, im = -1;
5010 	nid_t m = 0, ret;
5011 
5012 	if (depth > HDA_PARSE_MAXDEPTH)
5013 		return (0);
5014 	w = hdac_widget_get(devinfo, nid);
5015 	if (w == NULL || w->enable == 0)
5016 		return (0);
5017 	HDA_BOOTHVERBOSE(
5018 		if (!only) {
5019 			device_printf(devinfo->codec->sc->dev,
5020 			    " %*stracing via nid %d\n",
5021 				depth + 1, "", w->nid);
5022 		}
5023 	);
5024 	/* Use only unused widgets */
5025 	if (w->bindas >= 0 && w->bindas != as) {
5026 		HDA_BOOTHVERBOSE(
5027 			if (!only) {
5028 				device_printf(devinfo->codec->sc->dev,
5029 				    " %*snid %d busy by association %d\n",
5030 					depth + 1, "", w->nid, w->bindas);
5031 			}
5032 		);
5033 		return (0);
5034 	}
5035 	if (dupseq < 0) {
5036 		if (w->bindseqmask != 0) {
5037 			HDA_BOOTHVERBOSE(
5038 				if (!only) {
5039 					device_printf(devinfo->codec->sc->dev,
5040 					    " %*snid %d busy by seqmask %x\n",
5041 						depth + 1, "", w->nid, w->bindseqmask);
5042 				}
5043 			);
5044 			return (0);
5045 		}
5046 	} else {
5047 		/* If this is headphones - allow duplicate first pin. */
5048 		if (w->bindseqmask != 0 &&
5049 		    (w->bindseqmask & (1 << dupseq)) == 0) {
5050 			HDA_BOOTHVERBOSE(
5051 				device_printf(devinfo->codec->sc->dev,
5052 				    " %*snid %d busy by seqmask %x\n",
5053 					depth + 1, "", w->nid, w->bindseqmask);
5054 			);
5055 			return (0);
5056 		}
5057 	}
5058 
5059 	switch (w->type) {
5060 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
5061 		/* Do not traverse input. AD1988 has digital monitor
5062 		for which we are not ready. */
5063 		break;
5064 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
5065 		/* If we are tracing HP take only dac of first pin. */
5066 		if ((only == 0 || only == w->nid) &&
5067 		    (w->nid >= min) && (dupseq < 0 || w->nid ==
5068 		    devinfo->function.audio.as[as].dacs[dupseq]))
5069 			m = w->nid;
5070 		break;
5071 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
5072 		if (depth > 0)
5073 			break;
5074 		/* Fall */
5075 	default:
5076 		/* Find reachable DACs with smallest nid respecting constraints. */
5077 		for (i = 0; i < w->nconns; i++) {
5078 			if (w->connsenable[i] == 0)
5079 				continue;
5080 			if (w->selconn != -1 && w->selconn != i)
5081 				continue;
5082 			if ((ret = hdac_audio_trace_dac(devinfo, as, seq,
5083 			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
5084 				if (m == 0 || ret < m) {
5085 					m = ret;
5086 					im = i;
5087 				}
5088 				if (only || dupseq >= 0)
5089 					break;
5090 			}
5091 		}
5092 		if (m && only && ((w->nconns > 1 &&
5093 		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
5094 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
5095 			w->selconn = im;
5096 		break;
5097 	}
5098 	if (m && only) {
5099 		w->bindas = as;
5100 		w->bindseqmask |= (1 << seq);
5101 	}
5102 	HDA_BOOTHVERBOSE(
5103 		if (!only) {
5104 			device_printf(devinfo->codec->sc->dev,
5105 			    " %*snid %d returned %d\n",
5106 				depth + 1, "", w->nid, m);
5107 		}
5108 	);
5109 	return (m);
5110 }
5111 
5112 /*
5113  * Trace path from widget to ADC.
5114  */
5115 static nid_t
5116 hdac_audio_trace_adc(struct hdac_devinfo *devinfo, int as, int seq, nid_t nid,
5117     int only, int depth)
5118 {
5119 	struct hdac_widget *w, *wc;
5120 	int i, j;
5121 	nid_t res = 0;
5122 
5123 	if (depth > HDA_PARSE_MAXDEPTH)
5124 		return (0);
5125 	w = hdac_widget_get(devinfo, nid);
5126 	if (w == NULL || w->enable == 0)
5127 		return (0);
5128 	HDA_BOOTHVERBOSE(
5129 		device_printf(devinfo->codec->sc->dev,
5130 		    " %*stracing via nid %d\n",
5131 			depth + 1, "", w->nid);
5132 	);
5133 	/* Use only unused widgets */
5134 	if (w->bindas >= 0 && w->bindas != as) {
5135 		HDA_BOOTHVERBOSE(
5136 			device_printf(devinfo->codec->sc->dev,
5137 			    " %*snid %d busy by association %d\n",
5138 				depth + 1, "", w->nid, w->bindas);
5139 		);
5140 		return (0);
5141 	}
5142 
5143 	switch (w->type) {
5144 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
5145 		/* If we are tracing HP take only dac of first pin. */
5146 		if (only == w->nid)
5147 			res = 1;
5148 		break;
5149 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
5150 		if (depth > 0)
5151 			break;
5152 		/* Fall */
5153 	default:
5154 		/* Try to find reachable ADCs with specified nid. */
5155 		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5156 			wc = hdac_widget_get(devinfo, j);
5157 			if (wc == NULL || wc->enable == 0)
5158 				continue;
5159 			for (i = 0; i < wc->nconns; i++) {
5160 				if (wc->connsenable[i] == 0)
5161 					continue;
5162 				if (wc->conns[i] != nid)
5163 					continue;
5164 				if (hdac_audio_trace_adc(devinfo, as, seq,
5165 				    j, only, depth + 1) != 0) {
5166 					res = 1;
5167 					if (((wc->nconns > 1 &&
5168 					    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
5169 					    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR) &&
5170 					    wc->selconn == -1)
5171 						wc->selconn = i;
5172 				}
5173 			}
5174 		}
5175 		break;
5176 	}
5177 	if (res) {
5178 		w->bindas = as;
5179 		w->bindseqmask |= (1 << seq);
5180 	}
5181 	HDA_BOOTHVERBOSE(
5182 		device_printf(devinfo->codec->sc->dev,
5183 		    " %*snid %d returned %d\n",
5184 			depth + 1, "", w->nid, res);
5185 	);
5186 	return (res);
5187 }
5188 
5189 /*
5190  * Erase trace path of the specified association.
5191  */
5192 static void
5193 hdac_audio_undo_trace(struct hdac_devinfo *devinfo, int as, int seq)
5194 {
5195 	struct hdac_widget *w;
5196 	int i;
5197 
5198 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5199 		w = hdac_widget_get(devinfo, i);
5200 		if (w == NULL || w->enable == 0)
5201 			continue;
5202 		if (w->bindas == as) {
5203 			if (seq >= 0) {
5204 				w->bindseqmask &= ~(1 << seq);
5205 				if (w->bindseqmask == 0) {
5206 					w->bindas = -1;
5207 					w->selconn = -1;
5208 				}
5209 			} else {
5210 				w->bindas = -1;
5211 				w->bindseqmask = 0;
5212 				w->selconn = -1;
5213 			}
5214 		}
5215 	}
5216 }
5217 
5218 /*
5219  * Trace association path from DAC to output
5220  */
5221 static int
5222 hdac_audio_trace_as_out(struct hdac_devinfo *devinfo, int as, int seq)
5223 {
5224 	struct hdac_audio_as *ases = devinfo->function.audio.as;
5225 	int i, hpredir;
5226 	nid_t min, res;
5227 
5228 	/* Find next pin */
5229 	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
5230 		;
5231 	/* Check if there is no any left. If so - we succeeded. */
5232 	if (i == 16)
5233 		return (1);
5234 
5235 	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
5236 	min = 0;
5237 	res = 0;
5238 	do {
5239 		HDA_BOOTHVERBOSE(
5240 			device_printf(devinfo->codec->sc->dev,
5241 			    " Tracing pin %d with min nid %d",
5242 			    ases[as].pins[i], min);
5243 			if (hpredir >= 0)
5244 				printf(" and hpredir %d", hpredir);
5245 			printf("\n");
5246 		);
5247 		/* Trace this pin taking min nid into account. */
5248 		res = hdac_audio_trace_dac(devinfo, as, i,
5249 		    ases[as].pins[i], hpredir, min, 0, 0);
5250 		if (res == 0) {
5251 			/* If we failed - return to previous and redo it. */
5252 			HDA_BOOTVERBOSE(
5253 				device_printf(devinfo->codec->sc->dev,
5254 				    " Unable to trace pin %d seq %d with min "
5255 				    "nid %d",
5256 				    ases[as].pins[i], i, min);
5257 				if (hpredir >= 0)
5258 					printf(" and hpredir %d", hpredir);
5259 				printf("\n");
5260 			);
5261 			return (0);
5262 		}
5263 		HDA_BOOTVERBOSE(
5264 			device_printf(devinfo->codec->sc->dev,
5265 			    " Pin %d traced to DAC %d",
5266 			    ases[as].pins[i], res);
5267 			if (hpredir >= 0)
5268 				printf(" and hpredir %d", hpredir);
5269 			if (ases[as].fakeredir)
5270 				printf(" with fake redirection");
5271 			printf("\n");
5272 		);
5273 		/* Trace again to mark the path */
5274 		hdac_audio_trace_dac(devinfo, as, i,
5275 		    ases[as].pins[i], hpredir, min, res, 0);
5276 		ases[as].dacs[i] = res;
5277 		/* We succeeded, so call next. */
5278 		if (hdac_audio_trace_as_out(devinfo, as, i + 1))
5279 			return (1);
5280 		/* If next failed, we should retry with next min */
5281 		hdac_audio_undo_trace(devinfo, as, i);
5282 		ases[as].dacs[i] = 0;
5283 		min = res + 1;
5284 	} while (1);
5285 }
5286 
5287 /*
5288  * Trace association path from input to ADC
5289  */
5290 static int
5291 hdac_audio_trace_as_in(struct hdac_devinfo *devinfo, int as)
5292 {
5293 	struct hdac_audio_as *ases = devinfo->function.audio.as;
5294 	struct hdac_widget *w;
5295 	int i, j, k;
5296 
5297 	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5298 		w = hdac_widget_get(devinfo, j);
5299 		if (w == NULL || w->enable == 0)
5300 			continue;
5301 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
5302 			continue;
5303 		if (w->bindas >= 0 && w->bindas != as)
5304 			continue;
5305 
5306 		/* Find next pin */
5307 		for (i = 0; i < 16; i++) {
5308 			if (ases[as].pins[i] == 0)
5309 				continue;
5310 
5311 			HDA_BOOTHVERBOSE(
5312 				device_printf(devinfo->codec->sc->dev,
5313 				    " Tracing pin %d to ADC %d\n",
5314 				    ases[as].pins[i], j);
5315 			);
5316 			/* Trace this pin taking goal into account. */
5317 			if (hdac_audio_trace_adc(devinfo, as, i,
5318 			    ases[as].pins[i], j, 0) == 0) {
5319 				/* If we failed - return to previous and redo it. */
5320 				HDA_BOOTVERBOSE(
5321 					device_printf(devinfo->codec->sc->dev,
5322 					    " Unable to trace pin %d to ADC %d, undo traces\n",
5323 					    ases[as].pins[i], j);
5324 				);
5325 				hdac_audio_undo_trace(devinfo, as, -1);
5326 				for (k = 0; k < 16; k++)
5327 					ases[as].dacs[k] = 0;
5328 				break;
5329 			}
5330 			HDA_BOOTVERBOSE(
5331 				device_printf(devinfo->codec->sc->dev,
5332 				    " Pin %d traced to ADC %d\n",
5333 				    ases[as].pins[i], j);
5334 			);
5335 			ases[as].dacs[i] = j;
5336 		}
5337 		if (i == 16)
5338 			return (1);
5339 	}
5340 	return (0);
5341 }
5342 
5343 /*
5344  * Trace input monitor path from mixer to output association.
5345  */
5346 static int
5347 hdac_audio_trace_to_out(struct hdac_devinfo *devinfo, nid_t nid, int depth)
5348 {
5349 	struct hdac_audio_as *ases = devinfo->function.audio.as;
5350 	struct hdac_widget *w, *wc;
5351 	int i, j;
5352 	nid_t res = 0;
5353 
5354 	if (depth > HDA_PARSE_MAXDEPTH)
5355 		return (0);
5356 	w = hdac_widget_get(devinfo, nid);
5357 	if (w == NULL || w->enable == 0)
5358 		return (0);
5359 	HDA_BOOTHVERBOSE(
5360 		device_printf(devinfo->codec->sc->dev,
5361 		    " %*stracing via nid %d\n",
5362 			depth + 1, "", w->nid);
5363 	);
5364 	/* Use only unused widgets */
5365 	if (depth > 0 && w->bindas != -1) {
5366 		if (w->bindas < 0 || ases[w->bindas].dir == HDA_CTL_OUT) {
5367 			HDA_BOOTHVERBOSE(
5368 				device_printf(devinfo->codec->sc->dev,
5369 				    " %*snid %d found output association %d\n",
5370 					depth + 1, "", w->nid, w->bindas);
5371 			);
5372 			if (w->bindas >= 0)
5373 				w->pflags |= HDA_ADC_MONITOR;
5374 			return (1);
5375 		} else {
5376 			HDA_BOOTHVERBOSE(
5377 				device_printf(devinfo->codec->sc->dev,
5378 				    " %*snid %d busy by input association %d\n",
5379 					depth + 1, "", w->nid, w->bindas);
5380 			);
5381 			return (0);
5382 		}
5383 	}
5384 
5385 	switch (w->type) {
5386 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
5387 		/* Do not traverse input. AD1988 has digital monitor
5388 		for which we are not ready. */
5389 		break;
5390 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
5391 		if (depth > 0)
5392 			break;
5393 		/* Fall */
5394 	default:
5395 		/* Try to find reachable ADCs with specified nid. */
5396 		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5397 			wc = hdac_widget_get(devinfo, j);
5398 			if (wc == NULL || wc->enable == 0)
5399 				continue;
5400 			for (i = 0; i < wc->nconns; i++) {
5401 				if (wc->connsenable[i] == 0)
5402 					continue;
5403 				if (wc->conns[i] != nid)
5404 					continue;
5405 				if (hdac_audio_trace_to_out(devinfo,
5406 				    j, depth + 1) != 0) {
5407 					res = 1;
5408 					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
5409 					    wc->selconn == -1)
5410 						wc->selconn = i;
5411 				}
5412 			}
5413 		}
5414 		break;
5415 	}
5416 	if (res && w->bindas == -1)
5417 		w->bindas = -2;
5418 
5419 	HDA_BOOTHVERBOSE(
5420 		device_printf(devinfo->codec->sc->dev,
5421 		    " %*snid %d returned %d\n",
5422 			depth + 1, "", w->nid, res);
5423 	);
5424 	return (res);
5425 }
5426 
5427 /*
5428  * Trace extra associations (beeper, monitor)
5429  */
5430 static void
5431 hdac_audio_trace_as_extra(struct hdac_devinfo *devinfo)
5432 {
5433 	struct hdac_audio_as *as = devinfo->function.audio.as;
5434 	struct hdac_widget *w;
5435 	int j;
5436 
5437 	/* Input monitor */
5438 	/* Find mixer associated with input, but supplying signal
5439 	   for output associations. Hope it will be input monitor. */
5440 	HDA_BOOTVERBOSE(
5441 		device_printf(devinfo->codec->sc->dev,
5442 		    "Tracing input monitor\n");
5443 	);
5444 	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5445 		w = hdac_widget_get(devinfo, j);
5446 		if (w == NULL || w->enable == 0)
5447 			continue;
5448 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5449 			continue;
5450 		if (w->bindas < 0 || as[w->bindas].dir != HDA_CTL_IN)
5451 			continue;
5452 		HDA_BOOTVERBOSE(
5453 			device_printf(devinfo->codec->sc->dev,
5454 			    " Tracing nid %d to out\n",
5455 			    j);
5456 		);
5457 		if (hdac_audio_trace_to_out(devinfo, w->nid, 0)) {
5458 			HDA_BOOTVERBOSE(
5459 				device_printf(devinfo->codec->sc->dev,
5460 				    " nid %d is input monitor\n",
5461 					w->nid);
5462 			);
5463 			w->ossdev = SOUND_MIXER_IMIX;
5464 		}
5465 	}
5466 
5467 	/* Other inputs monitor */
5468 	/* Find input pins supplying signal for output associations.
5469 	   Hope it will be input monitoring. */
5470 	HDA_BOOTVERBOSE(
5471 		device_printf(devinfo->codec->sc->dev,
5472 		    "Tracing other input monitors\n");
5473 	);
5474 	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5475 		w = hdac_widget_get(devinfo, j);
5476 		if (w == NULL || w->enable == 0)
5477 			continue;
5478 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5479 			continue;
5480 		if (w->bindas < 0 || as[w->bindas].dir != HDA_CTL_IN)
5481 			continue;
5482 		HDA_BOOTVERBOSE(
5483 			device_printf(devinfo->codec->sc->dev,
5484 			    " Tracing nid %d to out\n",
5485 			    j);
5486 		);
5487 		if (hdac_audio_trace_to_out(devinfo, w->nid, 0)) {
5488 			HDA_BOOTVERBOSE(
5489 				device_printf(devinfo->codec->sc->dev,
5490 				    " nid %d is input monitor\n",
5491 					w->nid);
5492 			);
5493 		}
5494 	}
5495 
5496 	/* Beeper */
5497 	HDA_BOOTVERBOSE(
5498 		device_printf(devinfo->codec->sc->dev,
5499 		    "Tracing beeper\n");
5500 	);
5501 	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5502 		w = hdac_widget_get(devinfo, j);
5503 		if (w == NULL || w->enable == 0)
5504 			continue;
5505 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
5506 			continue;
5507 		HDA_BOOTHVERBOSE(
5508 			device_printf(devinfo->codec->sc->dev,
5509 			    " Tracing nid %d to out\n",
5510 			    j);
5511 		);
5512 		if (hdac_audio_trace_to_out(devinfo, w->nid, 0)) {
5513 			HDA_BOOTVERBOSE(
5514 				device_printf(devinfo->codec->sc->dev,
5515 				    " nid %d traced to out\n",
5516 				    j);
5517 			);
5518 		}
5519 		w->bindas = -2;
5520 	}
5521 }
5522 
5523 /*
5524  * Bind assotiations to PCM channels
5525  */
5526 static void
5527 hdac_audio_bind_as(struct hdac_devinfo *devinfo)
5528 {
5529 	struct hdac_softc *sc = devinfo->codec->sc;
5530 	struct hdac_audio_as *as = devinfo->function.audio.as;
5531 	int j, cnt = 0, free;
5532 
5533 	for (j = 0; j < devinfo->function.audio.ascnt; j++) {
5534 		if (as[j].enable)
5535 			cnt++;
5536 	}
5537 	if (sc->num_chans == 0) {
5538 		sc->chans = (struct hdac_chan *)malloc(
5539 		    sizeof(struct hdac_chan) * cnt,
5540 		    M_HDAC, M_ZERO | M_NOWAIT);
5541 		if (sc->chans == NULL) {
5542 			device_printf(sc->dev,
5543 			    "Channels memory allocation failed!\n");
5544 			return;
5545 		}
5546 	} else {
5547 		sc->chans = (struct hdac_chan *)realloc(sc->chans,
5548 		    sizeof(struct hdac_chan) * (sc->num_chans + cnt),
5549 		    M_HDAC, M_ZERO | M_NOWAIT);
5550 		if (sc->chans == NULL) {
5551 			sc->num_chans = 0;
5552 			device_printf(sc->dev,
5553 			    "Channels memory allocation failed!\n");
5554 			return;
5555 		}
5556 		/* Fixup relative pointers after realloc */
5557 		for (j = 0; j < sc->num_chans; j++)
5558 			sc->chans[j].caps.fmtlist = sc->chans[j].fmtlist;
5559 	}
5560 	free = sc->num_chans;
5561 	sc->num_chans += cnt;
5562 
5563 	for (j = free; j < free + cnt; j++) {
5564 		sc->chans[j].devinfo = devinfo;
5565 		sc->chans[j].as = -1;
5566 	}
5567 
5568 	/* Assign associations in order of their numbers, */
5569 	for (j = 0; j < devinfo->function.audio.ascnt; j++) {
5570 		if (as[j].enable == 0)
5571 			continue;
5572 
5573 		as[j].chan = free;
5574 		sc->chans[free].as = j;
5575 		sc->chans[free].dir =
5576 		    (as[j].dir == HDA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
5577 		hdac_pcmchannel_setup(&sc->chans[free]);
5578 		free++;
5579 	}
5580 }
5581 
5582 static void
5583 hdac_audio_disable_nonaudio(struct hdac_devinfo *devinfo)
5584 {
5585 	struct hdac_widget *w;
5586 	int i;
5587 
5588 	/* Disable power and volume widgets. */
5589 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5590 		w = hdac_widget_get(devinfo, i);
5591 		if (w == NULL || w->enable == 0)
5592 			continue;
5593 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
5594 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
5595 			w->enable = 0;
5596 			HDA_BOOTHVERBOSE(
5597 				device_printf(devinfo->codec->sc->dev,
5598 				    " Disabling nid %d due to it's"
5599 				    " non-audio type.\n",
5600 				    w->nid);
5601 			);
5602 		}
5603 	}
5604 }
5605 
5606 static void
5607 hdac_audio_disable_useless(struct hdac_devinfo *devinfo)
5608 {
5609 	struct hdac_widget *w, *cw;
5610 	struct hdac_audio_ctl *ctl;
5611 	int done, found, i, j, k;
5612 
5613 	/* Disable useless pins. */
5614 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5615 		w = hdac_widget_get(devinfo, i);
5616 		if (w == NULL || w->enable == 0)
5617 			continue;
5618 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
5619 			if ((w->wclass.pin.config &
5620 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
5621 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
5622 				w->enable = 0;
5623 				HDA_BOOTHVERBOSE(
5624 					device_printf(devinfo->codec->sc->dev,
5625 					    " Disabling pin nid %d due"
5626 					    " to None connectivity.\n",
5627 					    w->nid);
5628 				);
5629 			} else if ((w->wclass.pin.config &
5630 			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
5631 				w->enable = 0;
5632 				HDA_BOOTHVERBOSE(
5633 					device_printf(devinfo->codec->sc->dev,
5634 					    " Disabling unassociated"
5635 					    " pin nid %d.\n",
5636 					    w->nid);
5637 				);
5638 			}
5639 		}
5640 	}
5641 	do {
5642 		done = 1;
5643 		/* Disable and mute controls for disabled widgets. */
5644 		i = 0;
5645 		while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
5646 			if (ctl->enable == 0)
5647 				continue;
5648 			if (ctl->widget->enable == 0 ||
5649 			    (ctl->childwidget != NULL &&
5650 			    ctl->childwidget->enable == 0)) {
5651 				ctl->forcemute = 1;
5652 				ctl->muted = HDA_AMP_MUTE_ALL;
5653 				ctl->left = 0;
5654 				ctl->right = 0;
5655 				ctl->enable = 0;
5656 				if (ctl->ndir == HDA_CTL_IN)
5657 					ctl->widget->connsenable[ctl->index] = 0;
5658 				done = 0;
5659 				HDA_BOOTHVERBOSE(
5660 					device_printf(devinfo->codec->sc->dev,
5661 					    " Disabling ctl %d nid %d cnid %d due"
5662 					    " to disabled widget.\n", i,
5663 					    ctl->widget->nid,
5664 					    (ctl->childwidget != NULL)?
5665 					    ctl->childwidget->nid:-1);
5666 				);
5667 			}
5668 		}
5669 		/* Disable useless widgets. */
5670 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5671 			w = hdac_widget_get(devinfo, i);
5672 			if (w == NULL || w->enable == 0)
5673 				continue;
5674 			/* Disable inputs with disabled child widgets. */
5675 			for (j = 0; j < w->nconns; j++) {
5676 				if (w->connsenable[j]) {
5677 					cw = hdac_widget_get(devinfo, w->conns[j]);
5678 					if (cw == NULL || cw->enable == 0) {
5679 						w->connsenable[j] = 0;
5680 						HDA_BOOTHVERBOSE(
5681 							device_printf(devinfo->codec->sc->dev,
5682 							    " Disabling nid %d connection %d due"
5683 							    " to disabled child widget.\n",
5684 							    i, j);
5685 						);
5686 					}
5687 				}
5688 			}
5689 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
5690 			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5691 				continue;
5692 			/* Disable mixers and selectors without inputs. */
5693 			found = 0;
5694 			for (j = 0; j < w->nconns; j++) {
5695 				if (w->connsenable[j]) {
5696 					found = 1;
5697 					break;
5698 				}
5699 			}
5700 			if (found == 0) {
5701 				w->enable = 0;
5702 				done = 0;
5703 				HDA_BOOTHVERBOSE(
5704 					device_printf(devinfo->codec->sc->dev,
5705 					    " Disabling nid %d due to all it's"
5706 					    " inputs disabled.\n", w->nid);
5707 				);
5708 			}
5709 			/* Disable nodes without consumers. */
5710 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
5711 			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5712 				continue;
5713 			found = 0;
5714 			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
5715 				cw = hdac_widget_get(devinfo, k);
5716 				if (cw == NULL || cw->enable == 0)
5717 					continue;
5718 				for (j = 0; j < cw->nconns; j++) {
5719 					if (cw->connsenable[j] && cw->conns[j] == i) {
5720 						found = 1;
5721 						break;
5722 					}
5723 				}
5724 			}
5725 			if (found == 0) {
5726 				w->enable = 0;
5727 				done = 0;
5728 				HDA_BOOTHVERBOSE(
5729 					device_printf(devinfo->codec->sc->dev,
5730 					    " Disabling nid %d due to all it's"
5731 					    " consumers disabled.\n", w->nid);
5732 				);
5733 			}
5734 		}
5735 	} while (done == 0);
5736 
5737 }
5738 
5739 static void
5740 hdac_audio_disable_unas(struct hdac_devinfo *devinfo)
5741 {
5742 	struct hdac_audio_as *as = devinfo->function.audio.as;
5743 	struct hdac_widget *w, *cw;
5744 	struct hdac_audio_ctl *ctl;
5745 	int i, j, k;
5746 
5747 	/* Disable unassosiated widgets. */
5748 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5749 		w = hdac_widget_get(devinfo, i);
5750 		if (w == NULL || w->enable == 0)
5751 			continue;
5752 		if (w->bindas == -1) {
5753 			w->enable = 0;
5754 			HDA_BOOTHVERBOSE(
5755 				device_printf(devinfo->codec->sc->dev,
5756 				    " Disabling unassociated nid %d.\n",
5757 				    w->nid);
5758 			);
5759 		}
5760 	}
5761 	/* Disable input connections on input pin and
5762 	 * output on output. */
5763 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5764 		w = hdac_widget_get(devinfo, i);
5765 		if (w == NULL || w->enable == 0)
5766 			continue;
5767 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5768 			continue;
5769 		if (w->bindas < 0)
5770 			continue;
5771 		if (as[w->bindas].dir == HDA_CTL_IN) {
5772 			for (j = 0; j < w->nconns; j++) {
5773 				if (w->connsenable[j] == 0)
5774 					continue;
5775 				w->connsenable[j] = 0;
5776 				HDA_BOOTHVERBOSE(
5777 					device_printf(devinfo->codec->sc->dev,
5778 					    " Disabling connection to input pin "
5779 					    "nid %d conn %d.\n",
5780 					    i, j);
5781 				);
5782 			}
5783 			ctl = hdac_audio_ctl_amp_get(devinfo, w->nid,
5784 			    HDA_CTL_IN, -1, 1);
5785 			if (ctl && ctl->enable) {
5786 				ctl->forcemute = 1;
5787 				ctl->muted = HDA_AMP_MUTE_ALL;
5788 				ctl->left = 0;
5789 				ctl->right = 0;
5790 				ctl->enable = 0;
5791 			}
5792 		} else {
5793 			ctl = hdac_audio_ctl_amp_get(devinfo, w->nid,
5794 			    HDA_CTL_OUT, -1, 1);
5795 			if (ctl && ctl->enable) {
5796 				ctl->forcemute = 1;
5797 				ctl->muted = HDA_AMP_MUTE_ALL;
5798 				ctl->left = 0;
5799 				ctl->right = 0;
5800 				ctl->enable = 0;
5801 			}
5802 			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
5803 				cw = hdac_widget_get(devinfo, k);
5804 				if (cw == NULL || cw->enable == 0)
5805 					continue;
5806 				for (j = 0; j < cw->nconns; j++) {
5807 					if (cw->connsenable[j] && cw->conns[j] == i) {
5808 						cw->connsenable[j] = 0;
5809 						HDA_BOOTHVERBOSE(
5810 							device_printf(devinfo->codec->sc->dev,
5811 							    " Disabling connection from output pin "
5812 							    "nid %d conn %d cnid %d.\n",
5813 							    k, j, i);
5814 						);
5815 						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
5816 						    cw->nconns > 1)
5817 							continue;
5818 						ctl = hdac_audio_ctl_amp_get(devinfo, k,
5819 		    				    HDA_CTL_IN, j, 1);
5820 						if (ctl && ctl->enable) {
5821 							ctl->forcemute = 1;
5822 							ctl->muted = HDA_AMP_MUTE_ALL;
5823 							ctl->left = 0;
5824 							ctl->right = 0;
5825 							ctl->enable = 0;
5826 						}
5827 					}
5828 				}
5829 			}
5830 		}
5831 	}
5832 }
5833 
5834 static void
5835 hdac_audio_disable_notselected(struct hdac_devinfo *devinfo)
5836 {
5837 	struct hdac_audio_as *as = devinfo->function.audio.as;
5838 	struct hdac_widget *w;
5839 	int i, j;
5840 
5841 	/* On playback path we can safely disable all unseleted inputs. */
5842 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5843 		w = hdac_widget_get(devinfo, i);
5844 		if (w == NULL || w->enable == 0)
5845 			continue;
5846 		if (w->nconns <= 1)
5847 			continue;
5848 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5849 			continue;
5850 		if (w->bindas < 0 || as[w->bindas].dir == HDA_CTL_IN)
5851 			continue;
5852 		for (j = 0; j < w->nconns; j++) {
5853 			if (w->connsenable[j] == 0)
5854 				continue;
5855 			if (w->selconn < 0 || w->selconn == j)
5856 				continue;
5857 			w->connsenable[j] = 0;
5858 			HDA_BOOTHVERBOSE(
5859 				device_printf(devinfo->codec->sc->dev,
5860 				    " Disabling unselected connection "
5861 				    "nid %d conn %d.\n",
5862 				    i, j);
5863 			);
5864 		}
5865 	}
5866 }
5867 
5868 static void
5869 hdac_audio_disable_crossas(struct hdac_devinfo *devinfo)
5870 {
5871 	struct hdac_audio_as *ases = devinfo->function.audio.as;
5872 	struct hdac_widget *w, *cw;
5873 	struct hdac_audio_ctl *ctl;
5874 	int i, j;
5875 
5876 	/* Disable crossassociatement and unwanted crosschannel connections. */
5877 	/* ... using selectors */
5878 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5879 		w = hdac_widget_get(devinfo, i);
5880 		if (w == NULL || w->enable == 0)
5881 			continue;
5882 		if (w->nconns <= 1)
5883 			continue;
5884 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5885 			continue;
5886 		if (w->bindas == -2)
5887 			continue;
5888 		for (j = 0; j < w->nconns; j++) {
5889 			if (w->connsenable[j] == 0)
5890 				continue;
5891 			cw = hdac_widget_get(devinfo, w->conns[j]);
5892 			if (cw == NULL || w->enable == 0)
5893 				continue;
5894 			if (cw->bindas == -2 ||
5895 			    ((w->pflags & HDA_ADC_MONITOR) &&
5896 			     cw->bindas >= 0 &&
5897 			     ases[cw->bindas].dir == HDA_CTL_IN))
5898 				continue;
5899 			if (w->bindas == cw->bindas &&
5900 			    (w->bindseqmask & cw->bindseqmask) != 0)
5901 				continue;
5902 			w->connsenable[j] = 0;
5903 			HDA_BOOTHVERBOSE(
5904 				device_printf(devinfo->codec->sc->dev,
5905 				    " Disabling crossassociatement connection "
5906 				    "nid %d conn %d cnid %d.\n",
5907 				    i, j, cw->nid);
5908 			);
5909 		}
5910 	}
5911 	/* ... using controls */
5912 	i = 0;
5913 	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
5914 		if (ctl->enable == 0 || ctl->childwidget == NULL)
5915 			continue;
5916 		if (ctl->widget->bindas == -2)
5917 			continue;
5918 		if (ctl->childwidget->bindas == -2 ||
5919 		    ((ctl->widget->pflags & HDA_ADC_MONITOR) &&
5920 		     ctl->childwidget->bindas >= 0 &&
5921 		     ases[ctl->childwidget->bindas].dir == HDA_CTL_IN))
5922 			continue;
5923 		if (ctl->widget->bindas != ctl->childwidget->bindas ||
5924 		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) == 0) {
5925 			ctl->forcemute = 1;
5926 			ctl->muted = HDA_AMP_MUTE_ALL;
5927 			ctl->left = 0;
5928 			ctl->right = 0;
5929 			ctl->enable = 0;
5930 			if (ctl->ndir == HDA_CTL_IN)
5931 				ctl->widget->connsenable[ctl->index] = 0;
5932 			HDA_BOOTHVERBOSE(
5933 				device_printf(devinfo->codec->sc->dev,
5934 				    " Disabling crossassociatement connection "
5935 				    "ctl %d nid %d cnid %d.\n", i,
5936 				    ctl->widget->nid,
5937 				    ctl->childwidget->nid);
5938 			);
5939 		}
5940 	}
5941 
5942 }
5943 
5944 #define HDA_CTL_GIVE(ctl)	((ctl)->step?1:0)
5945 
5946 /*
5947  * Find controls to control amplification for source.
5948  */
5949 static int
5950 hdac_audio_ctl_source_amp(struct hdac_devinfo *devinfo, nid_t nid, int index,
5951     int ossdev, int ctlable, int depth, int need)
5952 {
5953 	struct hdac_widget *w, *wc;
5954 	struct hdac_audio_ctl *ctl;
5955 	int i, j, conns = 0, rneed;
5956 
5957 	if (depth > HDA_PARSE_MAXDEPTH)
5958 		return (need);
5959 
5960 	w = hdac_widget_get(devinfo, nid);
5961 	if (w == NULL || w->enable == 0)
5962 		return (need);
5963 
5964 	/* Count number of active inputs. */
5965 	if (depth > 0) {
5966 		for (j = 0; j < w->nconns; j++) {
5967 			if (w->connsenable[j])
5968 				conns++;
5969 		}
5970 	}
5971 
5972 	/* If this is not a first step - use input mixer.
5973 	   Pins have common input ctl so care must be taken. */
5974 	if (depth > 0 && ctlable && (conns == 1 ||
5975 	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
5976 		ctl = hdac_audio_ctl_amp_get(devinfo, w->nid, HDA_CTL_IN,
5977 		    index, 1);
5978 		if (ctl) {
5979 			if (HDA_CTL_GIVE(ctl) & need)
5980 				ctl->ossmask |= (1 << ossdev);
5981 			else
5982 				ctl->possmask |= (1 << ossdev);
5983 			need &= ~HDA_CTL_GIVE(ctl);
5984 		}
5985 	}
5986 
5987 	/* If widget has own ossdev - not traverse it.
5988 	   It will be traversed on it's own. */
5989 	if (w->ossdev >= 0 && depth > 0)
5990 		return (need);
5991 
5992 	/* We must not traverse pin */
5993 	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
5994 	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
5995 	    depth > 0)
5996 		return (need);
5997 
5998 	/* record that this widget exports such signal, */
5999 	w->ossmask |= (1 << ossdev);
6000 
6001 	/* If signals mixed, we can't assign controls farther.
6002 	 * Ignore this on depth zero. Caller must knows why.
6003 	 * Ignore this for static selectors if this input selected.
6004 	 */
6005 	if (conns > 1)
6006 		ctlable = 0;
6007 
6008 	if (ctlable) {
6009 		ctl = hdac_audio_ctl_amp_get(devinfo, w->nid, HDA_CTL_OUT, -1, 1);
6010 		if (ctl) {
6011 			if (HDA_CTL_GIVE(ctl) & need)
6012 				ctl->ossmask |= (1 << ossdev);
6013 			else
6014 				ctl->possmask |= (1 << ossdev);
6015 			need &= ~HDA_CTL_GIVE(ctl);
6016 		}
6017 	}
6018 
6019 	rneed = 0;
6020 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6021 		wc = hdac_widget_get(devinfo, i);
6022 		if (wc == NULL || wc->enable == 0)
6023 			continue;
6024 		for (j = 0; j < wc->nconns; j++) {
6025 			if (wc->connsenable[j] && wc->conns[j] == nid) {
6026 				rneed |= hdac_audio_ctl_source_amp(devinfo,
6027 				    wc->nid, j, ossdev, ctlable, depth + 1, need);
6028 			}
6029 		}
6030 	}
6031 	rneed &= need;
6032 
6033 	return (rneed);
6034 }
6035 
6036 /*
6037  * Find controls to control amplification for destination.
6038  */
6039 static void
6040 hdac_audio_ctl_dest_amp(struct hdac_devinfo *devinfo, nid_t nid, int index,
6041     int ossdev, int depth, int need)
6042 {
6043 	struct hdac_audio_as *as = devinfo->function.audio.as;
6044 	struct hdac_widget *w, *wc;
6045 	struct hdac_audio_ctl *ctl;
6046 	int i, j, consumers;
6047 
6048 	if (depth > HDA_PARSE_MAXDEPTH)
6049 		return;
6050 
6051 	w = hdac_widget_get(devinfo, nid);
6052 	if (w == NULL || w->enable == 0)
6053 		return;
6054 
6055 	if (depth > 0) {
6056 		/* If this node produce output for several consumers,
6057 		   we can't touch it. */
6058 		consumers = 0;
6059 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6060 			wc = hdac_widget_get(devinfo, i);
6061 			if (wc == NULL || wc->enable == 0)
6062 				continue;
6063 			for (j = 0; j < wc->nconns; j++) {
6064 				if (wc->connsenable[j] && wc->conns[j] == nid)
6065 					consumers++;
6066 			}
6067 		}
6068 		/* The only exception is if real HP redirection is configured
6069 		   and this is a duplication point.
6070 		   XXX: Actually exception is not completely correct.
6071 		   XXX: Duplication point check is not perfect. */
6072 		if ((consumers == 2 && (w->bindas < 0 ||
6073 		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
6074 		    (w->bindseqmask & (1 << 15)) == 0)) ||
6075 		    consumers > 2)
6076 			return;
6077 
6078 		/* Else use it's output mixer. */
6079 		ctl = hdac_audio_ctl_amp_get(devinfo, w->nid,
6080 		    HDA_CTL_OUT, -1, 1);
6081 		if (ctl) {
6082 			if (HDA_CTL_GIVE(ctl) & need)
6083 				ctl->ossmask |= (1 << ossdev);
6084 			else
6085 				ctl->possmask |= (1 << ossdev);
6086 			need &= ~HDA_CTL_GIVE(ctl);
6087 		}
6088 	}
6089 
6090 	/* We must not traverse pin */
6091 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
6092 	    depth > 0)
6093 		return;
6094 
6095 	for (i = 0; i < w->nconns; i++) {
6096 		int tneed = need;
6097 		if (w->connsenable[i] == 0)
6098 			continue;
6099 		if (index >= 0 && i != index)
6100 			continue;
6101 		ctl = hdac_audio_ctl_amp_get(devinfo, w->nid,
6102 		    HDA_CTL_IN, i, 1);
6103 		if (ctl) {
6104 			if (HDA_CTL_GIVE(ctl) & tneed)
6105 				ctl->ossmask |= (1 << ossdev);
6106 			else
6107 				ctl->possmask |= (1 << ossdev);
6108 			tneed &= ~HDA_CTL_GIVE(ctl);
6109 		}
6110 		hdac_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
6111 		    depth + 1, tneed);
6112 	}
6113 }
6114 
6115 /*
6116  * Assign OSS names to sound sources
6117  */
6118 static void
6119 hdac_audio_assign_names(struct hdac_devinfo *devinfo)
6120 {
6121 	struct hdac_audio_as *as = devinfo->function.audio.as;
6122 	struct hdac_widget *w;
6123 	int i, j;
6124 	int type = -1, use, used = 0;
6125 	static const int types[7][13] = {
6126 	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
6127 	      SOUND_MIXER_LINE3, -1 },	/* line */
6128 	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
6129 	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
6130 	    { SOUND_MIXER_CD, -1 },	/* cd */
6131 	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
6132 	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
6133 	      -1 },	/* digital */
6134 	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
6135 	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
6136 	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
6137 	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
6138 	      -1 }	/* others */
6139 	};
6140 
6141 	/* Surely known names */
6142 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6143 		w = hdac_widget_get(devinfo, i);
6144 		if (w == NULL || w->enable == 0)
6145 			continue;
6146 		if (w->bindas == -1)
6147 			continue;
6148 		use = -1;
6149 		switch (w->type) {
6150 		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
6151 			if (as[w->bindas].dir == HDA_CTL_OUT)
6152 				break;
6153 			type = -1;
6154 			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
6155 			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
6156 				type = 0;
6157 				break;
6158 			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
6159 				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
6160 				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
6161 					break;
6162 				type = 1;
6163 				break;
6164 			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
6165 				type = 3;
6166 				break;
6167 			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
6168 				type = 4;
6169 				break;
6170 			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
6171 			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
6172 				type = 5;
6173 				break;
6174 			}
6175 			if (type == -1)
6176 				break;
6177 			j = 0;
6178 			while (types[type][j] >= 0 &&
6179 			    (used & (1 << types[type][j])) != 0) {
6180 				j++;
6181 			}
6182 			if (types[type][j] >= 0)
6183 				use = types[type][j];
6184 			break;
6185 		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
6186 			use = SOUND_MIXER_PCM;
6187 			break;
6188 		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
6189 			use = SOUND_MIXER_SPEAKER;
6190 			break;
6191 		default:
6192 			break;
6193 		}
6194 		if (use >= 0) {
6195 			w->ossdev = use;
6196 			used |= (1 << use);
6197 		}
6198 	}
6199 	/* Semi-known names */
6200 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6201 		w = hdac_widget_get(devinfo, i);
6202 		if (w == NULL || w->enable == 0)
6203 			continue;
6204 		if (w->ossdev >= 0)
6205 			continue;
6206 		if (w->bindas == -1)
6207 			continue;
6208 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6209 			continue;
6210 		if (as[w->bindas].dir == HDA_CTL_OUT)
6211 			continue;
6212 		type = -1;
6213 		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
6214 		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
6215 		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
6216 		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
6217 		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
6218 			type = 0;
6219 			break;
6220 		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
6221 			type = 2;
6222 			break;
6223 		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
6224 		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
6225 			type = 5;
6226 			break;
6227 		}
6228 		if (type == -1)
6229 			break;
6230 		j = 0;
6231 		while (types[type][j] >= 0 &&
6232 		    (used & (1 << types[type][j])) != 0) {
6233 			j++;
6234 		}
6235 		if (types[type][j] >= 0) {
6236 			w->ossdev = types[type][j];
6237 			used |= (1 << types[type][j]);
6238 		}
6239 	}
6240 	/* Others */
6241 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6242 		w = hdac_widget_get(devinfo, i);
6243 		if (w == NULL || w->enable == 0)
6244 			continue;
6245 		if (w->ossdev >= 0)
6246 			continue;
6247 		if (w->bindas == -1)
6248 			continue;
6249 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6250 			continue;
6251 		if (as[w->bindas].dir == HDA_CTL_OUT)
6252 			continue;
6253 		j = 0;
6254 		while (types[6][j] >= 0 &&
6255 		    (used & (1 << types[6][j])) != 0) {
6256 			j++;
6257 		}
6258 		if (types[6][j] >= 0) {
6259 			w->ossdev = types[6][j];
6260 			used |= (1 << types[6][j]);
6261 		}
6262 	}
6263 }
6264 
6265 static void
6266 hdac_audio_build_tree(struct hdac_devinfo *devinfo)
6267 {
6268 	struct hdac_audio_as *as = devinfo->function.audio.as;
6269 	int j, res;
6270 
6271 	/* Trace all associations in order of their numbers, */
6272 	for (j = 0; j < devinfo->function.audio.ascnt; j++) {
6273 		if (as[j].enable == 0)
6274 			continue;
6275 		HDA_BOOTVERBOSE(
6276 			device_printf(devinfo->codec->sc->dev,
6277 			    "Tracing association %d (%d)\n", j, as[j].index);
6278 		);
6279 		if (as[j].dir == HDA_CTL_OUT) {
6280 retry:
6281 			res = hdac_audio_trace_as_out(devinfo, j, 0);
6282 			if (res == 0 && as[j].hpredir >= 0 &&
6283 			    as[j].fakeredir == 0) {
6284 				/* If codec can't do analog HP redirection
6285 				   try to make it using one more DAC. */
6286 				as[j].fakeredir = 1;
6287 				goto retry;
6288 			}
6289 		} else {
6290 			res = hdac_audio_trace_as_in(devinfo, j);
6291 		}
6292 		if (res) {
6293 			HDA_BOOTVERBOSE(
6294 				device_printf(devinfo->codec->sc->dev,
6295 				    "Association %d (%d) trace succeeded\n",
6296 				    j, as[j].index);
6297 			);
6298 		} else {
6299 			HDA_BOOTVERBOSE(
6300 				device_printf(devinfo->codec->sc->dev,
6301 				    "Association %d (%d) trace failed\n",
6302 				    j, as[j].index);
6303 			);
6304 			as[j].enable = 0;
6305 		}
6306 	}
6307 
6308 	/* Trace mixer and beeper pseudo associations. */
6309 	hdac_audio_trace_as_extra(devinfo);
6310 }
6311 
6312 static void
6313 hdac_audio_assign_mixers(struct hdac_devinfo *devinfo)
6314 {
6315 	struct hdac_audio_as *as = devinfo->function.audio.as;
6316 	struct hdac_audio_ctl *ctl;
6317 	struct hdac_widget *w, *cw;
6318 	int i, j;
6319 
6320 	/* Assign mixers to the tree. */
6321 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6322 		w = hdac_widget_get(devinfo, i);
6323 		if (w == NULL || w->enable == 0)
6324 			continue;
6325 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
6326 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
6327 		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
6328 		    as[w->bindas].dir == HDA_CTL_IN)) {
6329 			if (w->ossdev < 0)
6330 				continue;
6331 			hdac_audio_ctl_source_amp(devinfo, w->nid, -1,
6332 			    w->ossdev, 1, 0, 1);
6333 		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
6334 			hdac_audio_ctl_dest_amp(devinfo, w->nid, -1,
6335 			    SOUND_MIXER_RECLEV, 0, 1);
6336 		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
6337 		    as[w->bindas].dir == HDA_CTL_OUT) {
6338 			hdac_audio_ctl_dest_amp(devinfo, w->nid, -1,
6339 			    SOUND_MIXER_VOLUME, 0, 1);
6340 		}
6341 		if (w->ossdev == SOUND_MIXER_IMIX) {
6342 			if (hdac_audio_ctl_source_amp(devinfo, w->nid, -1,
6343 			    w->ossdev, 1, 0, 1)) {
6344 				/* If we are unable to control input monitor
6345 				   as source - try to control it as destination. */
6346 				hdac_audio_ctl_dest_amp(devinfo, w->nid, -1,
6347 				    w->ossdev, 0, 1);
6348 			}
6349 		}
6350 		if (w->pflags & HDA_ADC_MONITOR) {
6351 			for (j = 0; j < w->nconns; j++) {
6352 				if (!w->connsenable[j])
6353 				    continue;
6354 				cw = hdac_widget_get(devinfo, w->conns[j]);
6355 				if (cw == NULL || cw->enable == 0)
6356 				    continue;
6357 				if (cw->bindas == -1)
6358 				    continue;
6359 				if (cw->bindas >= 0 &&
6360 				    as[cw->bindas].dir != HDA_CTL_IN)
6361 					continue;
6362 				hdac_audio_ctl_dest_amp(devinfo,
6363 				    w->nid, j, SOUND_MIXER_IGAIN, 0, 1);
6364 			}
6365 		}
6366 	}
6367 	/* Treat unrequired as possible. */
6368 	i = 0;
6369 	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
6370 		if (ctl->ossmask == 0)
6371 			ctl->ossmask = ctl->possmask;
6372 	}
6373 }
6374 
6375 static void
6376 hdac_audio_prepare_pin_ctrl(struct hdac_devinfo *devinfo)
6377 {
6378 	struct hdac_audio_as *as = devinfo->function.audio.as;
6379 	struct hdac_widget *w;
6380 	uint32_t pincap;
6381 	int i;
6382 
6383 	for (i = 0; i < devinfo->nodecnt; i++) {
6384 		w = &devinfo->widget[i];
6385 		if (w == NULL)
6386 			continue;
6387 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6388 			continue;
6389 
6390 		pincap = w->wclass.pin.cap;
6391 
6392 		/* Disable everything. */
6393 		w->wclass.pin.ctrl &= ~(
6394 		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
6395 		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
6396 		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
6397 		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
6398 
6399 		if (w->enable == 0 ||
6400 		    w->bindas < 0 || as[w->bindas].enable == 0) {
6401 			/* Pin is unused so left it disabled. */
6402 			continue;
6403 		} else if (as[w->bindas].dir == HDA_CTL_IN) {
6404 			/* Input pin, configure for input. */
6405 			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
6406 				w->wclass.pin.ctrl |=
6407 				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
6408 
6409 			if ((devinfo->function.audio.quirks & HDA_QUIRK_IVREF100) &&
6410 			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
6411 				w->wclass.pin.ctrl |=
6412 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6413 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
6414 			else if ((devinfo->function.audio.quirks & HDA_QUIRK_IVREF80) &&
6415 			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
6416 				w->wclass.pin.ctrl |=
6417 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6418 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
6419 			else if ((devinfo->function.audio.quirks & HDA_QUIRK_IVREF50) &&
6420 			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
6421 				w->wclass.pin.ctrl |=
6422 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6423 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
6424 		} else {
6425 			/* Output pin, configure for output. */
6426 			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
6427 				w->wclass.pin.ctrl |=
6428 				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
6429 
6430 			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
6431 			    (w->wclass.pin.config &
6432 			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
6433 			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
6434 				w->wclass.pin.ctrl |=
6435 				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
6436 
6437 			if ((devinfo->function.audio.quirks & HDA_QUIRK_OVREF100) &&
6438 			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
6439 				w->wclass.pin.ctrl |=
6440 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6441 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
6442 			else if ((devinfo->function.audio.quirks & HDA_QUIRK_OVREF80) &&
6443 			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
6444 				w->wclass.pin.ctrl |=
6445 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6446 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
6447 			else if ((devinfo->function.audio.quirks & HDA_QUIRK_OVREF50) &&
6448 			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
6449 				w->wclass.pin.ctrl |=
6450 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6451 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
6452 		}
6453 	}
6454 }
6455 
6456 static void
6457 hdac_audio_ctl_commit(struct hdac_devinfo *devinfo)
6458 {
6459 	struct hdac_audio_ctl *ctl;
6460 	int i, z;
6461 
6462 	i = 0;
6463 	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
6464 		if (ctl->enable == 0 || ctl->ossmask != 0) {
6465 			/* Mute disabled and mixer controllable controls.
6466 			 * Last will be initialized by mixer_init().
6467 			 * This expected to reduce click on startup. */
6468 			hdac_audio_ctl_amp_set(ctl, HDA_AMP_MUTE_ALL, 0, 0);
6469 			continue;
6470 		}
6471 		/* Init fixed controls to 0dB amplification. */
6472 		z = ctl->offset;
6473 		if (z > ctl->step)
6474 			z = ctl->step;
6475 		hdac_audio_ctl_amp_set(ctl, HDA_AMP_MUTE_NONE, z, z);
6476 	}
6477 }
6478 
6479 static void
6480 hdac_audio_commit(struct hdac_devinfo *devinfo)
6481 {
6482 	struct hdac_softc *sc = devinfo->codec->sc;
6483 	struct hdac_widget *w;
6484 	nid_t cad;
6485 	uint32_t id, gdata, gmask, gdir;
6486 	int commitgpio, numgpio;
6487 	int i;
6488 
6489 	cad = devinfo->codec->cad;
6490 
6491 	if (sc->pci_subvendor == APPLE_INTEL_MAC)
6492 		hdac_command(sc, HDA_CMD_12BIT(cad, devinfo->nid,
6493 		    0x7e7, 0), cad);
6494 	id = hdac_codec_id(devinfo->codec);
6495 	switch (id) {
6496 	case HDA_CODEC_VT1708S_0:
6497 	case HDA_CODEC_VT1708S_1:
6498 	case HDA_CODEC_VT1708S_2:
6499 	case HDA_CODEC_VT1708S_3:
6500 	case HDA_CODEC_VT1708S_4:
6501 	case HDA_CODEC_VT1708S_5:
6502 	case HDA_CODEC_VT1708S_6:
6503 	case HDA_CODEC_VT1708S_7:
6504 		/* Enable Mic Boost Volume controls. */
6505 		hdac_command(sc, HDA_CMD_12BIT(cad, devinfo->nid,
6506 		    0xf98, 0x01), cad);
6507 		/* Don't bypass mixer. */
6508 		hdac_command(sc, HDA_CMD_12BIT(cad, devinfo->nid,
6509 		    0xf88, 0xc0), cad);
6510 		break;
6511 	}
6512 
6513 	/* Commit controls. */
6514 	hdac_audio_ctl_commit(devinfo);
6515 
6516 	/* Commit selectors, pins and EAPD. */
6517 	for (i = 0; i < devinfo->nodecnt; i++) {
6518 		w = &devinfo->widget[i];
6519 		if (w == NULL)
6520 			continue;
6521 		if (w->selconn == -1)
6522 			w->selconn = 0;
6523 		if (w->nconns > 0)
6524 			hdac_widget_connection_select(w, w->selconn);
6525 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
6526 			hdac_command(sc,
6527 			    HDA_CMD_SET_PIN_WIDGET_CTRL(cad, w->nid,
6528 			    w->wclass.pin.ctrl), cad);
6529 		}
6530 		if (w->param.eapdbtl != HDAC_INVALID) {
6531 		    	uint32_t val;
6532 
6533 			val = w->param.eapdbtl;
6534 			if (devinfo->function.audio.quirks &
6535 			    HDA_QUIRK_EAPDINV)
6536 				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
6537 			hdac_command(sc,
6538 			    HDA_CMD_SET_EAPD_BTL_ENABLE(cad, w->nid,
6539 			    val), cad);
6540 		}
6541 	}
6542 
6543 	/* Commit GPIOs. */
6544 	gdata = 0;
6545 	gmask = 0;
6546 	gdir = 0;
6547 	commitgpio = 0;
6548 	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(
6549 	    devinfo->function.audio.gpio);
6550 
6551 	if (devinfo->function.audio.quirks & HDA_QUIRK_GPIOFLUSH)
6552 		commitgpio = (numgpio > 0) ? 1 : 0;
6553 	else {
6554 		for (i = 0; i < numgpio && i < HDA_GPIO_MAX; i++) {
6555 			if (!(devinfo->function.audio.quirks &
6556 			    (1 << i)))
6557 				continue;
6558 			if (commitgpio == 0) {
6559 				commitgpio = 1;
6560 				HDA_BOOTVERBOSE(
6561 					gdata = hdac_command(sc,
6562 					    HDA_CMD_GET_GPIO_DATA(cad,
6563 					    devinfo->nid), cad);
6564 					gmask = hdac_command(sc,
6565 					    HDA_CMD_GET_GPIO_ENABLE_MASK(cad,
6566 					    devinfo->nid), cad);
6567 					gdir = hdac_command(sc,
6568 					    HDA_CMD_GET_GPIO_DIRECTION(cad,
6569 					    devinfo->nid), cad);
6570 					device_printf(sc->dev,
6571 					    "GPIO init: data=0x%08x "
6572 					    "mask=0x%08x dir=0x%08x\n",
6573 					    gdata, gmask, gdir);
6574 					gdata = 0;
6575 					gmask = 0;
6576 					gdir = 0;
6577 				);
6578 			}
6579 			gdata |= 1 << i;
6580 			gmask |= 1 << i;
6581 			gdir |= 1 << i;
6582 		}
6583 	}
6584 
6585 	if (commitgpio != 0) {
6586 		HDA_BOOTVERBOSE(
6587 			device_printf(sc->dev,
6588 			    "GPIO commit: data=0x%08x mask=0x%08x "
6589 			    "dir=0x%08x\n",
6590 			    gdata, gmask, gdir);
6591 		);
6592 		hdac_command(sc,
6593 		    HDA_CMD_SET_GPIO_ENABLE_MASK(cad, devinfo->nid,
6594 		    gmask), cad);
6595 		hdac_command(sc,
6596 		    HDA_CMD_SET_GPIO_DIRECTION(cad, devinfo->nid,
6597 		    gdir), cad);
6598 		hdac_command(sc,
6599 		    HDA_CMD_SET_GPIO_DATA(cad, devinfo->nid,
6600 		    gdata), cad);
6601 	}
6602 }
6603 
6604 static void
6605 hdac_powerup(struct hdac_devinfo *devinfo)
6606 {
6607 	struct hdac_softc *sc = devinfo->codec->sc;
6608 	nid_t cad = devinfo->codec->cad;
6609 	int i;
6610 
6611 	hdac_command(sc,
6612 	    HDA_CMD_SET_POWER_STATE(cad,
6613 	    devinfo->nid, HDA_CMD_POWER_STATE_D0),
6614 	    cad);
6615 	DELAY(100);
6616 
6617 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6618 		hdac_command(sc,
6619 		    HDA_CMD_SET_POWER_STATE(cad,
6620 		    i, HDA_CMD_POWER_STATE_D0),
6621 		    cad);
6622 	}
6623 	DELAY(1000);
6624 }
6625 
6626 static int
6627 hdac_pcmchannel_setup(struct hdac_chan *ch)
6628 {
6629 	struct hdac_devinfo *devinfo = ch->devinfo;
6630 	struct hdac_audio_as *as = devinfo->function.audio.as;
6631 	struct hdac_widget *w;
6632 	uint32_t cap, fmtcap, pcmcap;
6633 	int i, j, ret, channels, onlystereo;
6634 	uint16_t pinset;
6635 
6636 	ch->caps = hdac_caps;
6637 	ch->caps.fmtlist = ch->fmtlist;
6638 	ch->bit16 = 1;
6639 	ch->bit32 = 0;
6640 	ch->pcmrates[0] = 48000;
6641 	ch->pcmrates[1] = 0;
6642 
6643 	ret = 0;
6644 	channels = 0;
6645 	onlystereo = 1;
6646 	pinset = 0;
6647 	fmtcap = devinfo->function.audio.supp_stream_formats;
6648 	pcmcap = devinfo->function.audio.supp_pcm_size_rate;
6649 
6650 	for (i = 0; i < 16; i++) {
6651 		/* Check as is correct */
6652 		if (ch->as < 0)
6653 			break;
6654 		/* Cound only present DACs */
6655 		if (as[ch->as].dacs[i] <= 0)
6656 			continue;
6657 		/* Ignore duplicates */
6658 		for (j = 0; j < ret; j++) {
6659 			if (ch->io[j] == as[ch->as].dacs[i])
6660 				break;
6661 		}
6662 		if (j < ret)
6663 			continue;
6664 
6665 		w = hdac_widget_get(devinfo, as[ch->as].dacs[i]);
6666 		if (w == NULL || w->enable == 0)
6667 			continue;
6668 		cap = w->param.supp_stream_formats;
6669 		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
6670 		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
6671 			continue;
6672 		/* Many CODECs does not declare AC3 support on SPDIF.
6673 		   I don't beleave that they doesn't support it! */
6674 		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
6675 			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
6676 		if (ret == 0) {
6677 			fmtcap = cap;
6678 			pcmcap = w->param.supp_pcm_size_rate;
6679 		} else {
6680 			fmtcap &= cap;
6681 			pcmcap &= w->param.supp_pcm_size_rate;
6682 		}
6683 		ch->io[ret++] = as[ch->as].dacs[i];
6684 		/* Do not count redirection pin/dac channels. */
6685 		if (i == 15 && as[ch->as].hpredir >= 0)
6686 			continue;
6687 		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
6688 		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
6689 			onlystereo = 0;
6690 		pinset |= (1 << i);
6691 	}
6692 	ch->io[ret] = -1;
6693 
6694 	if (as[ch->as].fakeredir)
6695 		ret--;
6696 	/* Standard speaks only about stereo pins and playback, ... */
6697 	if ((!onlystereo) || as[ch->as].dir != HDA_CTL_OUT)
6698 		pinset = 0;
6699 	/* ..., but there it gives us info about speakers layout. */
6700 	as[ch->as].pinset = pinset;
6701 
6702 	ch->supp_stream_formats = fmtcap;
6703 	ch->supp_pcm_size_rate = pcmcap;
6704 
6705 	/*
6706 	 *  8bit = 0
6707 	 * 16bit = 1
6708 	 * 20bit = 2
6709 	 * 24bit = 3
6710 	 * 32bit = 4
6711 	 */
6712 	if (ret > 0) {
6713 		i = 0;
6714 		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
6715 			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
6716 				ch->bit16 = 1;
6717 			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
6718 				ch->bit16 = 0;
6719 			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6720 				ch->bit32 = 4;
6721 			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6722 				ch->bit32 = 3;
6723 			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6724 				ch->bit32 = 2;
6725 			if (!(devinfo->function.audio.quirks & HDA_QUIRK_FORCESTEREO)) {
6726 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
6727 				if (ch->bit32)
6728 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
6729 			}
6730 			if (channels >= 2) {
6731 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
6732 				if (ch->bit32)
6733 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
6734 			}
6735 			if (channels == 4 || /* Any 4-channel */
6736 			    pinset == 0x0007 || /* 5.1 */
6737 			    pinset == 0x0013 || /* 5.1 */
6738 			    pinset == 0x0017) {  /* 7.1 */
6739 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
6740 				if (ch->bit32)
6741 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
6742 			}
6743 			if (channels == 6 || /* Any 6-channel */
6744 			    pinset == 0x0017) {  /* 7.1 */
6745 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
6746 				if (ch->bit32)
6747 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
6748 			}
6749 			if (channels == 8) { /* Any 8-channel */
6750 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
6751 				if (ch->bit32)
6752 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
6753 			}
6754 		}
6755 		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
6756 			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
6757 		}
6758 		ch->fmtlist[i] = 0;
6759 		i = 0;
6760 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
6761 			ch->pcmrates[i++] = 8000;
6762 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
6763 			ch->pcmrates[i++] = 11025;
6764 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
6765 			ch->pcmrates[i++] = 16000;
6766 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
6767 			ch->pcmrates[i++] = 22050;
6768 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
6769 			ch->pcmrates[i++] = 32000;
6770 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
6771 			ch->pcmrates[i++] = 44100;
6772 		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
6773 		ch->pcmrates[i++] = 48000;
6774 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
6775 			ch->pcmrates[i++] = 88200;
6776 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
6777 			ch->pcmrates[i++] = 96000;
6778 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
6779 			ch->pcmrates[i++] = 176400;
6780 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
6781 			ch->pcmrates[i++] = 192000;
6782 		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
6783 		ch->pcmrates[i] = 0;
6784 		if (i > 0) {
6785 			ch->caps.minspeed = ch->pcmrates[0];
6786 			ch->caps.maxspeed = ch->pcmrates[i - 1];
6787 		}
6788 	}
6789 
6790 	return (ret);
6791 }
6792 
6793 static void
6794 hdac_create_pcms(struct hdac_devinfo *devinfo)
6795 {
6796 	struct hdac_softc *sc = devinfo->codec->sc;
6797 	struct hdac_audio_as *as = devinfo->function.audio.as;
6798 	int i, j, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
6799 
6800 	for (i = 0; i < devinfo->function.audio.ascnt; i++) {
6801 		if (as[i].enable == 0)
6802 			continue;
6803 		if (as[i].dir == HDA_CTL_IN) {
6804 			if (as[i].digital)
6805 				drdev++;
6806 			else
6807 				ardev++;
6808 		} else {
6809 			if (as[i].digital)
6810 				dpdev++;
6811 			else
6812 				apdev++;
6813 		}
6814 	}
6815 	devinfo->function.audio.num_devs =
6816 	    max(ardev, apdev) + max(drdev, dpdev);
6817 	devinfo->function.audio.devs =
6818 	    (struct hdac_pcm_devinfo *)malloc(
6819 	    devinfo->function.audio.num_devs * sizeof(struct hdac_pcm_devinfo),
6820 	    M_HDAC, M_ZERO | M_NOWAIT);
6821 	if (devinfo->function.audio.devs == NULL) {
6822 		device_printf(sc->dev,
6823 		    "Unable to allocate memory for devices\n");
6824 		return;
6825 	}
6826 	for (i = 0; i < devinfo->function.audio.num_devs; i++) {
6827 		devinfo->function.audio.devs[i].index = i;
6828 		devinfo->function.audio.devs[i].devinfo = devinfo;
6829 		devinfo->function.audio.devs[i].play = -1;
6830 		devinfo->function.audio.devs[i].rec = -1;
6831 		devinfo->function.audio.devs[i].digital = 255;
6832 	}
6833 	for (i = 0; i < devinfo->function.audio.ascnt; i++) {
6834 		if (as[i].enable == 0)
6835 			continue;
6836 		for (j = 0; j < devinfo->function.audio.num_devs; j++) {
6837 			if (devinfo->function.audio.devs[j].digital != 255 &&
6838 			    (!devinfo->function.audio.devs[j].digital) !=
6839 			    (!as[i].digital))
6840 				continue;
6841 			if (as[i].dir == HDA_CTL_IN) {
6842 				if (devinfo->function.audio.devs[j].rec >= 0)
6843 					continue;
6844 				devinfo->function.audio.devs[j].rec
6845 				    = as[i].chan;
6846 			} else {
6847 				if (devinfo->function.audio.devs[j].play >= 0)
6848 					continue;
6849 				devinfo->function.audio.devs[j].play
6850 				    = as[i].chan;
6851 			}
6852 			sc->chans[as[i].chan].pdevinfo =
6853 			    &devinfo->function.audio.devs[j];
6854 			devinfo->function.audio.devs[j].digital =
6855 			    as[i].digital;
6856 			break;
6857 		}
6858 	}
6859 	for (i = 0; i < devinfo->function.audio.num_devs; i++) {
6860 		struct hdac_pcm_devinfo *pdevinfo =
6861 		    &devinfo->function.audio.devs[i];
6862 		pdevinfo->dev =
6863 		    device_add_child(sc->dev, "pcm", -1);
6864 		device_set_ivars(pdevinfo->dev,
6865 		     (void *)pdevinfo);
6866 	}
6867 }
6868 
6869 static void
6870 hdac_dump_ctls(struct hdac_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
6871 {
6872 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
6873 	struct hdac_audio_ctl *ctl;
6874 	struct hdac_softc *sc = devinfo->codec->sc;
6875 	char buf[64];
6876 	int i, j, printed;
6877 
6878 	if (flag == 0) {
6879 		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
6880 		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
6881 		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
6882 		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
6883 	}
6884 
6885 	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
6886 		if ((flag & (1 << j)) == 0)
6887 			continue;
6888 		i = 0;
6889 		printed = 0;
6890 		while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
6891 			if (ctl->enable == 0 ||
6892 			    ctl->widget->enable == 0)
6893 				continue;
6894 			if (!((pdevinfo->play >= 0 &&
6895 			    ctl->widget->bindas == sc->chans[pdevinfo->play].as) ||
6896 			    (pdevinfo->rec >= 0 &&
6897 			    ctl->widget->bindas == sc->chans[pdevinfo->rec].as) ||
6898 			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
6899 				continue;
6900 			if ((ctl->ossmask & (1 << j)) == 0)
6901 				continue;
6902 
6903 	    		if (printed == 0) {
6904 				device_printf(pdevinfo->dev, "\n");
6905 				if (banner != NULL) {
6906 					device_printf(pdevinfo->dev, "%s", banner);
6907 				} else {
6908 					device_printf(pdevinfo->dev, "Unknown Ctl");
6909 				}
6910 				printf(" (OSS: %s)\n",
6911 				    hdac_audio_ctl_ossmixer_mask2allname(1 << j,
6912 				    buf, sizeof(buf)));
6913 				device_printf(pdevinfo->dev, "   |\n");
6914 				printed = 1;
6915 			}
6916 			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
6917 				ctl->widget->nid,
6918 				(ctl->ndir == HDA_CTL_IN)?"in ":"out");
6919 			if (ctl->ndir == HDA_CTL_IN && ctl->ndir == ctl->dir)
6920 				printf(" %2d): ", ctl->index);
6921 			else
6922 				printf("):    ");
6923 			if (ctl->step > 0) {
6924 				printf("%+d/%+ddB (%d steps)%s\n",
6925 			    	    (0 - ctl->offset) * (ctl->size + 1) / 4,
6926 				    (ctl->step - ctl->offset) * (ctl->size + 1) / 4,
6927 				    ctl->step + 1,
6928 				    ctl->mute?" + mute":"");
6929 			} else
6930 				printf("%s\n", ctl->mute?"mute":"");
6931 		}
6932 	}
6933 }
6934 
6935 static void
6936 hdac_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
6937 {
6938 	uint32_t cap;
6939 
6940 	cap = fcap;
6941 	if (cap != 0) {
6942 		device_printf(dev, "     Stream cap: 0x%08x\n", cap);
6943 		device_printf(dev, "                ");
6944 		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
6945 			printf(" AC3");
6946 		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
6947 			printf(" FLOAT32");
6948 		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
6949 			printf(" PCM");
6950 		printf("\n");
6951 	}
6952 	cap = pcmcap;
6953 	if (cap != 0) {
6954 		device_printf(dev, "        PCM cap: 0x%08x\n", cap);
6955 		device_printf(dev, "                ");
6956 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
6957 			printf(" 8");
6958 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
6959 			printf(" 16");
6960 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
6961 			printf(" 20");
6962 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
6963 			printf(" 24");
6964 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
6965 			printf(" 32");
6966 		printf(" bits,");
6967 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
6968 			printf(" 8");
6969 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
6970 			printf(" 11");
6971 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
6972 			printf(" 16");
6973 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
6974 			printf(" 22");
6975 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
6976 			printf(" 32");
6977 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
6978 			printf(" 44");
6979 		printf(" 48");
6980 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
6981 			printf(" 88");
6982 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
6983 			printf(" 96");
6984 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
6985 			printf(" 176");
6986 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
6987 			printf(" 192");
6988 		printf(" KHz\n");
6989 	}
6990 }
6991 
6992 static void
6993 hdac_dump_pin(struct hdac_softc *sc, struct hdac_widget *w)
6994 {
6995 	uint32_t pincap;
6996 
6997 	pincap = w->wclass.pin.cap;
6998 
6999 	device_printf(sc->dev, "        Pin cap: 0x%08x\n", pincap);
7000 	device_printf(sc->dev, "                ");
7001 	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
7002 		printf(" ISC");
7003 	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
7004 		printf(" TRQD");
7005 	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
7006 		printf(" PDC");
7007 	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
7008 		printf(" HP");
7009 	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
7010 		printf(" OUT");
7011 	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
7012 		printf(" IN");
7013 	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
7014 		printf(" BAL");
7015 	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
7016 		printf(" HDMI");
7017 	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
7018 		printf(" VREF[");
7019 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
7020 			printf(" 50");
7021 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
7022 			printf(" 80");
7023 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
7024 			printf(" 100");
7025 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
7026 			printf(" GROUND");
7027 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
7028 			printf(" HIZ");
7029 		printf(" ]");
7030 	}
7031 	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
7032 		printf(" EAPD");
7033 	if (HDA_PARAM_PIN_CAP_DP(pincap))
7034 		printf(" DP");
7035 	if (HDA_PARAM_PIN_CAP_HBR(pincap))
7036 		printf(" HBR");
7037 	printf("\n");
7038 	device_printf(sc->dev, "     Pin config: 0x%08x\n",
7039 	    w->wclass.pin.config);
7040 	device_printf(sc->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
7041 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
7042 		printf(" HP");
7043 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
7044 		printf(" IN");
7045 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
7046 		printf(" OUT");
7047 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK)
7048 		printf(" VREFs");
7049 	printf("\n");
7050 }
7051 
7052 static void
7053 hdac_dump_pin_config(struct hdac_widget *w, uint32_t conf)
7054 {
7055 	struct hdac_softc *sc = w->devinfo->codec->sc;
7056 
7057 	device_printf(sc->dev, " nid %d 0x%08x as %2d seq %2d %13s %5s "
7058 	    "jack %2d loc %2d color %7s misc %d%s\n",
7059 	    w->nid, conf,
7060 	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
7061 	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
7062 	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
7063 	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
7064 	    HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf),
7065 	    HDA_CONFIG_DEFAULTCONF_LOCATION(conf),
7066 	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
7067 	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
7068 	    (w->enable == 0)?" [DISABLED]":"");
7069 }
7070 
7071 static void
7072 hdac_dump_pin_configs(struct hdac_devinfo *devinfo)
7073 {
7074 	struct hdac_widget *w;
7075 	int i;
7076 
7077 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
7078 		w = hdac_widget_get(devinfo, i);
7079 		if (w == NULL)
7080 			continue;
7081 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
7082 			continue;
7083 		hdac_dump_pin_config(w, w->wclass.pin.config);
7084 	}
7085 }
7086 
7087 static void
7088 hdac_dump_amp(struct hdac_softc *sc, uint32_t cap, char *banner)
7089 {
7090 	device_printf(sc->dev, "     %s amp: 0x%08x\n", banner, cap);
7091 	device_printf(sc->dev, "                 "
7092 	    "mute=%d step=%d size=%d offset=%d\n",
7093 	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
7094 	    HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap),
7095 	    HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap),
7096 	    HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap));
7097 }
7098 
7099 static void
7100 hdac_dump_nodes(struct hdac_devinfo *devinfo)
7101 {
7102 	struct hdac_softc *sc = devinfo->codec->sc;
7103 	static char *ossname[] = SOUND_DEVICE_NAMES;
7104 	struct hdac_widget *w, *cw;
7105 	char buf[64];
7106 	int i, j;
7107 
7108 	device_printf(sc->dev, "\n");
7109 	device_printf(sc->dev, "Default Parameter\n");
7110 	device_printf(sc->dev, "-----------------\n");
7111 	hdac_dump_audio_formats(sc->dev,
7112 	    devinfo->function.audio.supp_stream_formats,
7113 	    devinfo->function.audio.supp_pcm_size_rate);
7114 	device_printf(sc->dev, "         IN amp: 0x%08x\n",
7115 	    devinfo->function.audio.inamp_cap);
7116 	device_printf(sc->dev, "        OUT amp: 0x%08x\n",
7117 	    devinfo->function.audio.outamp_cap);
7118 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
7119 		w = hdac_widget_get(devinfo, i);
7120 		if (w == NULL) {
7121 			device_printf(sc->dev, "Ghost widget nid=%d\n", i);
7122 			continue;
7123 		}
7124 		device_printf(sc->dev, "\n");
7125 		device_printf(sc->dev, "            nid: %d%s\n", w->nid,
7126 		    (w->enable == 0) ? " [DISABLED]" : "");
7127 		device_printf(sc->dev, "           Name: %s\n", w->name);
7128 		device_printf(sc->dev, "     Widget cap: 0x%08x\n",
7129 		    w->param.widget_cap);
7130 		if (w->param.widget_cap & 0x0ee1) {
7131 			device_printf(sc->dev, "                ");
7132 			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
7133 			    printf(" LRSWAP");
7134 			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
7135 			    printf(" PWR");
7136 			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
7137 			    printf(" DIGITAL");
7138 			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
7139 			    printf(" UNSOL");
7140 			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
7141 			    printf(" PROC");
7142 			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
7143 			    printf(" STRIPE");
7144 			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
7145 			if (j == 1)
7146 			    printf(" STEREO");
7147 			else if (j > 1)
7148 			    printf(" %dCH", j + 1);
7149 			printf("\n");
7150 		}
7151 		if (w->bindas != -1) {
7152 			device_printf(sc->dev, "    Association: %d (0x%08x)\n",
7153 			    w->bindas, w->bindseqmask);
7154 		}
7155 		if (w->ossmask != 0 || w->ossdev >= 0) {
7156 			device_printf(sc->dev, "            OSS: %s",
7157 			    hdac_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
7158 			if (w->ossdev >= 0)
7159 			    printf(" (%s)", ossname[w->ossdev]);
7160 			printf("\n");
7161 		}
7162 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
7163 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
7164 			hdac_dump_audio_formats(sc->dev,
7165 			    w->param.supp_stream_formats,
7166 			    w->param.supp_pcm_size_rate);
7167 		} else if (w->type ==
7168 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
7169 			hdac_dump_pin(sc, w);
7170 		if (w->param.eapdbtl != HDAC_INVALID)
7171 			device_printf(sc->dev, "           EAPD: 0x%08x\n",
7172 			    w->param.eapdbtl);
7173 		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
7174 		    w->param.outamp_cap != 0)
7175 			hdac_dump_amp(sc, w->param.outamp_cap, "Output");
7176 		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
7177 		    w->param.inamp_cap != 0)
7178 			hdac_dump_amp(sc, w->param.inamp_cap, " Input");
7179 		if (w->nconns > 0) {
7180 			device_printf(sc->dev, "    connections: %d\n", w->nconns);
7181 			device_printf(sc->dev, "          |\n");
7182 		}
7183 		for (j = 0; j < w->nconns; j++) {
7184 			cw = hdac_widget_get(devinfo, w->conns[j]);
7185 			device_printf(sc->dev, "          + %s<- nid=%d [%s]",
7186 			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
7187 			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
7188 			if (cw == NULL)
7189 				printf(" [UNKNOWN]");
7190 			else if (cw->enable == 0)
7191 				printf(" [DISABLED]");
7192 			if (w->nconns > 1 && w->selconn == j && w->type !=
7193 			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
7194 				printf(" (selected)");
7195 			printf("\n");
7196 		}
7197 	}
7198 
7199 }
7200 
7201 static void
7202 hdac_dump_dst_nid(struct hdac_pcm_devinfo *pdevinfo, nid_t nid, int depth)
7203 {
7204 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
7205 	struct hdac_widget *w, *cw;
7206 	char buf[64];
7207 	int i, printed = 0;
7208 
7209 	if (depth > HDA_PARSE_MAXDEPTH)
7210 		return;
7211 
7212 	w = hdac_widget_get(devinfo, nid);
7213 	if (w == NULL || w->enable == 0)
7214 		return;
7215 
7216 	if (depth == 0)
7217 		device_printf(pdevinfo->dev, "%*s", 4, "");
7218 	else
7219 		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
7220 	printf("nid=%d [%s]", w->nid, w->name);
7221 
7222 	if (depth > 0) {
7223 		if (w->ossmask == 0) {
7224 			printf("\n");
7225 			return;
7226 		}
7227 		printf(" [src: %s]",
7228 		    hdac_audio_ctl_ossmixer_mask2allname(
7229 			w->ossmask, buf, sizeof(buf)));
7230 		if (w->ossdev >= 0) {
7231 			printf("\n");
7232 			return;
7233 		}
7234 	}
7235 	printf("\n");
7236 
7237 	for (i = 0; i < w->nconns; i++) {
7238 		if (w->connsenable[i] == 0)
7239 			continue;
7240 		cw = hdac_widget_get(devinfo, w->conns[i]);
7241 		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
7242 			continue;
7243 		if (printed == 0) {
7244 			device_printf(pdevinfo->dev, "%*s  |\n", 4 + (depth) * 7, "");
7245 			printed = 1;
7246 		}
7247 		hdac_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
7248 	}
7249 
7250 }
7251 
7252 static void
7253 hdac_dump_dac(struct hdac_pcm_devinfo *pdevinfo)
7254 {
7255 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
7256 	struct hdac_softc *sc = devinfo->codec->sc;
7257 	struct hdac_audio_as *as;
7258 	struct hdac_widget *w;
7259 	int i, printed = 0;
7260 
7261 	if (pdevinfo->play < 0)
7262 		return;
7263 
7264 	as = &devinfo->function.audio.as[sc->chans[pdevinfo->play].as];
7265 	for (i = 0; i < 16; i++) {
7266 		if (as->pins[i] <= 0)
7267 			continue;
7268 		w = hdac_widget_get(devinfo, as->pins[i]);
7269 		if (w == NULL || w->enable == 0)
7270 			continue;
7271 		if (printed == 0) {
7272 			printed = 1;
7273 			device_printf(pdevinfo->dev, "\n");
7274 			device_printf(pdevinfo->dev, "Playback:\n");
7275 		}
7276 		device_printf(pdevinfo->dev, "\n");
7277 		hdac_dump_dst_nid(pdevinfo, as->pins[i], 0);
7278 	}
7279 }
7280 
7281 static void
7282 hdac_dump_adc(struct hdac_pcm_devinfo *pdevinfo)
7283 {
7284 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
7285 	struct hdac_softc *sc = devinfo->codec->sc;
7286 	struct hdac_widget *w;
7287 	int i;
7288 	int printed = 0;
7289 
7290 	if (pdevinfo->rec < 0)
7291 		return;
7292 
7293 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
7294 		w = hdac_widget_get(devinfo, i);
7295 		if (w == NULL || w->enable == 0)
7296 			continue;
7297 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
7298 			continue;
7299 		if (w->bindas != sc->chans[pdevinfo->rec].as)
7300 			continue;
7301 		if (printed == 0) {
7302 			printed = 1;
7303 			device_printf(pdevinfo->dev, "\n");
7304 			device_printf(pdevinfo->dev, "Record:\n");
7305 		}
7306 		device_printf(pdevinfo->dev, "\n");
7307 		hdac_dump_dst_nid(pdevinfo, i, 0);
7308 	}
7309 }
7310 
7311 static void
7312 hdac_dump_mix(struct hdac_pcm_devinfo *pdevinfo)
7313 {
7314 	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
7315 	struct hdac_widget *w;
7316 	int i;
7317 	int printed = 0;
7318 
7319 	if (pdevinfo->index != 0)
7320 		return;
7321 
7322 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
7323 		w = hdac_widget_get(devinfo, i);
7324 		if (w == NULL || w->enable == 0)
7325 			continue;
7326 		if (w->ossdev != SOUND_MIXER_IMIX)
7327 			continue;
7328 		if (printed == 0) {
7329 			printed = 1;
7330 			device_printf(pdevinfo->dev, "\n");
7331 			device_printf(pdevinfo->dev, "Input Mix:\n");
7332 		}
7333 		device_printf(pdevinfo->dev, "\n");
7334 		hdac_dump_dst_nid(pdevinfo, i, 0);
7335 	}
7336 }
7337 
7338 static void
7339 hdac_dump_pcmchannels(struct hdac_pcm_devinfo *pdevinfo)
7340 {
7341 	struct hdac_softc *sc = pdevinfo->devinfo->codec->sc;
7342 	nid_t *nids;
7343 	int i;
7344 
7345 	if (pdevinfo->play >= 0) {
7346 		i = pdevinfo->play;
7347 		device_printf(pdevinfo->dev, "\n");
7348 		device_printf(pdevinfo->dev, "Playback:\n");
7349 		device_printf(pdevinfo->dev, "\n");
7350 		hdac_dump_audio_formats(pdevinfo->dev, sc->chans[i].supp_stream_formats,
7351 		    sc->chans[i].supp_pcm_size_rate);
7352 		device_printf(pdevinfo->dev, "            DAC:");
7353 		for (nids = sc->chans[i].io; *nids != -1; nids++)
7354 			printf(" %d", *nids);
7355 		printf("\n");
7356 	}
7357 	if (pdevinfo->rec >= 0) {
7358 		i = pdevinfo->rec;
7359 		device_printf(pdevinfo->dev, "\n");
7360 		device_printf(pdevinfo->dev, "Record:\n");
7361 		device_printf(pdevinfo->dev, "\n");
7362 		hdac_dump_audio_formats(pdevinfo->dev, sc->chans[i].supp_stream_formats,
7363 		    sc->chans[i].supp_pcm_size_rate);
7364 		device_printf(pdevinfo->dev, "            ADC:");
7365 		for (nids = sc->chans[i].io; *nids != -1; nids++)
7366 			printf(" %d", *nids);
7367 		printf("\n");
7368 	}
7369 }
7370 
7371 static void
7372 hdac_release_resources(struct hdac_softc *sc)
7373 {
7374         int i, j;
7375 
7376 	if (sc == NULL)
7377 		return;
7378 
7379 	hdac_lock(sc);
7380 	sc->polling = 0;
7381 	sc->poll_ival = 0;
7382 	callout_stop(&sc->poll_hda);
7383 	callout_stop(&sc->poll_hdac);
7384 	callout_stop(&sc->poll_jack);
7385 	hdac_reset(sc, 0);
7386 	hdac_unlock(sc);
7387 	taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
7388 	callout_drain(&sc->poll_hda);
7389 	callout_drain(&sc->poll_hdac);
7390 	callout_drain(&sc->poll_jack);
7391 
7392 	hdac_irq_free(sc);
7393 
7394 	for (i = 0; i < HDAC_CODEC_MAX; i++) {
7395 		if (sc->codecs[i] == NULL)
7396 			continue;
7397 		for (j = 0; j < sc->codecs[i]->num_fgs; j++) {
7398 			free(sc->codecs[i]->fgs[j].widget, M_HDAC);
7399 			if (sc->codecs[i]->fgs[j].node_type ==
7400 			    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) {
7401 				free(sc->codecs[i]->fgs[j].function.audio.ctl,
7402 				    M_HDAC);
7403 				free(sc->codecs[i]->fgs[j].function.audio.as,
7404 				    M_HDAC);
7405 				free(sc->codecs[i]->fgs[j].function.audio.devs,
7406 				    M_HDAC);
7407 			}
7408 		}
7409 		free(sc->codecs[i]->fgs, M_HDAC);
7410 		free(sc->codecs[i], M_HDAC);
7411 		sc->codecs[i] = NULL;
7412 	}
7413 
7414 	hdac_dma_free(sc, &sc->pos_dma);
7415 	hdac_dma_free(sc, &sc->rirb_dma);
7416 	hdac_dma_free(sc, &sc->corb_dma);
7417 	for (i = 0; i < sc->num_chans; i++) {
7418     		if (sc->chans[i].blkcnt > 0)
7419     			hdac_dma_free(sc, &sc->chans[i].bdl_dma);
7420 	}
7421 	free(sc->chans, M_HDAC);
7422 	if (sc->chan_dmat != NULL) {
7423 		bus_dma_tag_destroy(sc->chan_dmat);
7424 		sc->chan_dmat = NULL;
7425 	}
7426 	hdac_mem_free(sc);
7427 	snd_mtxfree(sc->lock);
7428 }
7429 
7430 /* This function surely going to make its way into upper level someday. */
7431 static void
7432 hdac_config_fetch(struct hdac_softc *sc, uint32_t *on, uint32_t *off)
7433 {
7434 	const char *res = NULL;
7435 	int i = 0, j, k, len, inv;
7436 
7437 	if (on != NULL)
7438 		*on = 0;
7439 	if (off != NULL)
7440 		*off = 0;
7441 	if (sc == NULL)
7442 		return;
7443 	if (resource_string_value(device_get_name(sc->dev),
7444 	    device_get_unit(sc->dev), "config", &res) != 0)
7445 		return;
7446 	if (!(res != NULL && strlen(res) > 0))
7447 		return;
7448 	HDA_BOOTVERBOSE(
7449 		device_printf(sc->dev, "HDA Config:");
7450 	);
7451 	for (;;) {
7452 		while (res[i] != '\0' &&
7453 		    (res[i] == ',' || isspace(res[i]) != 0))
7454 			i++;
7455 		if (res[i] == '\0') {
7456 			HDA_BOOTVERBOSE(
7457 				printf("\n");
7458 			);
7459 			return;
7460 		}
7461 		j = i;
7462 		while (res[j] != '\0' &&
7463 		    !(res[j] == ',' || isspace(res[j]) != 0))
7464 			j++;
7465 		len = j - i;
7466 		if (len > 2 && strncmp(res + i, "no", 2) == 0)
7467 			inv = 2;
7468 		else
7469 			inv = 0;
7470 		for (k = 0; len > inv && k < HDAC_QUIRKS_TAB_LEN; k++) {
7471 			if (strncmp(res + i + inv,
7472 			    hdac_quirks_tab[k].key, len - inv) != 0)
7473 				continue;
7474 			if (len - inv != strlen(hdac_quirks_tab[k].key))
7475 				continue;
7476 			HDA_BOOTVERBOSE(
7477 				printf(" %s%s", (inv != 0) ? "no" : "",
7478 				    hdac_quirks_tab[k].key);
7479 			);
7480 			if (inv == 0 && on != NULL)
7481 				*on |= hdac_quirks_tab[k].value;
7482 			else if (inv != 0 && off != NULL)
7483 				*off |= hdac_quirks_tab[k].value;
7484 			break;
7485 		}
7486 		i = j;
7487 	}
7488 }
7489 
7490 static int
7491 sysctl_hdac_polling(SYSCTL_HANDLER_ARGS)
7492 {
7493 	struct hdac_softc *sc;
7494 	device_t dev;
7495 	uint32_t ctl;
7496 	int err, val;
7497 
7498 	dev = oidp->oid_arg1;
7499 	sc = device_get_softc(dev);
7500 	if (sc == NULL)
7501 		return (EINVAL);
7502 	hdac_lock(sc);
7503 	val = sc->polling;
7504 	hdac_unlock(sc);
7505 	err = sysctl_handle_int(oidp, &val, 0, req);
7506 
7507 	if (err != 0 || req->newptr == NULL)
7508 		return (err);
7509 	if (val < 0 || val > 1)
7510 		return (EINVAL);
7511 
7512 	hdac_lock(sc);
7513 	if (val != sc->polling) {
7514 		if (val == 0) {
7515 			callout_stop(&sc->poll_hda);
7516 			callout_stop(&sc->poll_hdac);
7517 			hdac_unlock(sc);
7518 			callout_drain(&sc->poll_hda);
7519 			callout_drain(&sc->poll_hdac);
7520 			hdac_lock(sc);
7521 			sc->polling = 0;
7522 			ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
7523 			ctl |= HDAC_INTCTL_GIE;
7524 			HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
7525 		} else {
7526 			ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
7527 			ctl &= ~HDAC_INTCTL_GIE;
7528 			HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
7529 			hdac_unlock(sc);
7530 			taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
7531 			hdac_lock(sc);
7532 			sc->polling = 1;
7533 			hdac_poll_reinit(sc);
7534 			callout_reset(&sc->poll_hdac, 1, hdac_poll_callback, sc);
7535 		}
7536 	}
7537 	hdac_unlock(sc);
7538 
7539 	return (err);
7540 }
7541 
7542 static int
7543 sysctl_hdac_polling_interval(SYSCTL_HANDLER_ARGS)
7544 {
7545 	struct hdac_softc *sc;
7546 	device_t dev;
7547 	int err, val;
7548 
7549 	dev = oidp->oid_arg1;
7550 	sc = device_get_softc(dev);
7551 	if (sc == NULL)
7552 		return (EINVAL);
7553 	hdac_lock(sc);
7554 	val = ((uint64_t)sc->poll_ival * 1000) / hz;
7555 	hdac_unlock(sc);
7556 	err = sysctl_handle_int(oidp, &val, 0, req);
7557 
7558 	if (err != 0 || req->newptr == NULL)
7559 		return (err);
7560 
7561 	if (val < 1)
7562 		val = 1;
7563 	if (val > 5000)
7564 		val = 5000;
7565 	val = ((uint64_t)val * hz) / 1000;
7566 	if (val < 1)
7567 		val = 1;
7568 	if (val > (hz * 5))
7569 		val = hz * 5;
7570 
7571 	hdac_lock(sc);
7572 	sc->poll_ival = val;
7573 	hdac_unlock(sc);
7574 
7575 	return (err);
7576 }
7577 
7578 static int
7579 sysctl_hdac_pindump(SYSCTL_HANDLER_ARGS)
7580 {
7581 	struct hdac_softc *sc;
7582 	struct hdac_codec *codec;
7583 	struct hdac_devinfo *devinfo;
7584 	struct hdac_widget *w;
7585 	device_t dev;
7586 	uint32_t res, pincap, delay;
7587 	int codec_index, fg_index;
7588 	int i, err, val;
7589 	nid_t cad;
7590 
7591 	dev = oidp->oid_arg1;
7592 	sc = device_get_softc(dev);
7593 	if (sc == NULL)
7594 		return (EINVAL);
7595 	val = 0;
7596 	err = sysctl_handle_int(oidp, &val, 0, req);
7597 	if (err != 0 || req->newptr == NULL || val == 0)
7598 		return (err);
7599 
7600 	/* XXX: Temporary. For debugging. */
7601 	if (val == 100) {
7602 		hdac_suspend(dev);
7603 		return (0);
7604 	} else if (val == 101) {
7605 		hdac_resume(dev);
7606 		return (0);
7607 	}
7608 
7609 	hdac_lock(sc);
7610 	for (codec_index = 0; codec_index < HDAC_CODEC_MAX; codec_index++) {
7611 		codec = sc->codecs[codec_index];
7612 		if (codec == NULL)
7613 			continue;
7614 		cad = codec->cad;
7615 		for (fg_index = 0; fg_index < codec->num_fgs; fg_index++) {
7616 			devinfo = &codec->fgs[fg_index];
7617 			if (devinfo->node_type !=
7618 			    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
7619 				continue;
7620 
7621 			device_printf(dev, "Dumping AFG cad=%d nid=%d pins:\n",
7622 			    codec_index, devinfo->nid);
7623 			for (i = devinfo->startnode; i < devinfo->endnode; i++) {
7624 					w = hdac_widget_get(devinfo, i);
7625 				if (w == NULL || w->type !=
7626 				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
7627 					continue;
7628 				hdac_dump_pin_config(w, w->wclass.pin.config);
7629 				pincap = w->wclass.pin.cap;
7630 				device_printf(dev, "       Caps: %2s %3s %2s %4s %4s",
7631 				    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
7632 				    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
7633 				    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
7634 				    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
7635 				    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
7636 				if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
7637 				    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
7638 					if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
7639 						delay = 0;
7640 						hdac_command(sc,
7641 						    HDA_CMD_SET_PIN_SENSE(cad, w->nid, 0), cad);
7642 						do {
7643 							res = hdac_command(sc,
7644 							    HDA_CMD_GET_PIN_SENSE(cad, w->nid), cad);
7645 							if (res != 0x7fffffff && res != 0xffffffff)
7646 								break;
7647 							DELAY(10);
7648 						} while (++delay < 10000);
7649 					} else {
7650 						delay = 0;
7651 						res = hdac_command(sc, HDA_CMD_GET_PIN_SENSE(cad,
7652 						    w->nid), cad);
7653 					}
7654 					printf(" Sense: 0x%08x", res);
7655 					if (delay > 0)
7656 						printf(" delay %dus", delay * 10);
7657 				}
7658 				printf("\n");
7659 			}
7660 			device_printf(dev,
7661 			    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
7662 			    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->function.audio.gpio),
7663 			    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->function.audio.gpio),
7664 			    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->function.audio.gpio),
7665 			    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->function.audio.gpio),
7666 			    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->function.audio.gpio));
7667 			if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->function.audio.gpio) > 0) {
7668 				device_printf(dev, " GPI:");
7669 				res = hdac_command(sc,
7670 				    HDA_CMD_GET_GPI_DATA(cad, devinfo->nid), cad);
7671 				printf(" data=0x%08x", res);
7672 				res = hdac_command(sc,
7673 				    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(cad, devinfo->nid),
7674 				    cad);
7675 				printf(" wake=0x%08x", res);
7676 				res = hdac_command(sc,
7677 				    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(cad, devinfo->nid),
7678 				    cad);
7679 				printf(" unsol=0x%08x", res);
7680 				res = hdac_command(sc,
7681 				    HDA_CMD_GET_GPI_STICKY_MASK(cad, devinfo->nid), cad);
7682 				printf(" sticky=0x%08x\n", res);
7683 			}
7684 			if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->function.audio.gpio) > 0) {
7685 				device_printf(dev, " GPO:");
7686 				res = hdac_command(sc,
7687 				    HDA_CMD_GET_GPO_DATA(cad, devinfo->nid), cad);
7688 				printf(" data=0x%08x\n", res);
7689 			}
7690 			if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->function.audio.gpio) > 0) {
7691 				device_printf(dev, "GPIO:");
7692 				res = hdac_command(sc,
7693 				    HDA_CMD_GET_GPIO_DATA(cad, devinfo->nid), cad);
7694 				printf(" data=0x%08x", res);
7695 				res = hdac_command(sc,
7696 				    HDA_CMD_GET_GPIO_ENABLE_MASK(cad, devinfo->nid), cad);
7697 				printf(" enable=0x%08x", res);
7698 				res = hdac_command(sc,
7699 				    HDA_CMD_GET_GPIO_DIRECTION(cad, devinfo->nid), cad);
7700 				printf(" direction=0x%08x\n", res);
7701 				res = hdac_command(sc,
7702 				    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(cad, devinfo->nid), cad);
7703 				device_printf(dev, "      wake=0x%08x", res);
7704 				res = hdac_command(sc,
7705 				    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(cad, devinfo->nid),
7706 				    cad);
7707 				printf("  unsol=0x%08x", res);
7708 				res = hdac_command(sc,
7709 				    HDA_CMD_GET_GPIO_STICKY_MASK(cad, devinfo->nid), cad);
7710 				printf("    sticky=0x%08x\n", res);
7711 			}
7712 		}
7713 	}
7714 	hdac_unlock(sc);
7715 	return (0);
7716 }
7717 
7718 static void
7719 hdac_attach2(void *arg)
7720 {
7721 	struct hdac_codec *codec;
7722 	struct hdac_softc *sc;
7723 	struct hdac_audio_ctl *ctl;
7724 	uint32_t quirks_on, quirks_off;
7725 	int codec_index, fg_index;
7726 	int i, dmaalloc = 0;
7727 	struct hdac_devinfo *devinfo;
7728 
7729 	sc = (struct hdac_softc *)arg;
7730 
7731 	hdac_config_fetch(sc, &quirks_on, &quirks_off);
7732 
7733 	HDA_BOOTHVERBOSE(
7734 		device_printf(sc->dev, "HDA Config: on=0x%08x off=0x%08x\n",
7735 		    quirks_on, quirks_off);
7736 	);
7737 
7738 	hdac_lock(sc);
7739 
7740 	/* Remove ourselves from the config hooks */
7741 	if (sc->intrhook.ich_func != NULL) {
7742 		config_intrhook_disestablish(&sc->intrhook);
7743 		sc->intrhook.ich_func = NULL;
7744 	}
7745 
7746 	/* Start the corb and rirb engines */
7747 	HDA_BOOTHVERBOSE(
7748 		device_printf(sc->dev, "Starting CORB Engine...\n");
7749 	);
7750 	hdac_corb_start(sc);
7751 	HDA_BOOTHVERBOSE(
7752 		device_printf(sc->dev, "Starting RIRB Engine...\n");
7753 	);
7754 	hdac_rirb_start(sc);
7755 
7756 	HDA_BOOTHVERBOSE(
7757 		device_printf(sc->dev,
7758 		    "Enabling controller interrupt...\n");
7759 	);
7760 	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
7761 	    HDAC_GCTL_UNSOL);
7762 	if (sc->polling == 0) {
7763 		HDAC_WRITE_4(&sc->mem, HDAC_INTCTL,
7764 		    HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
7765 	} else {
7766 		callout_reset(&sc->poll_hdac, 1, hdac_poll_callback, sc);
7767 	}
7768 	DELAY(1000);
7769 
7770 	HDA_BOOTHVERBOSE(
7771 		device_printf(sc->dev,
7772 		    "Scanning HDA codecs ...\n");
7773 	);
7774 	hdac_scan_codecs(sc);
7775 
7776 	for (codec_index = 0; codec_index < HDAC_CODEC_MAX; codec_index++) {
7777 		codec = sc->codecs[codec_index];
7778 		if (codec == NULL)
7779 			continue;
7780 		for (fg_index = 0; fg_index < codec->num_fgs; fg_index++) {
7781 			devinfo = &codec->fgs[fg_index];
7782 			HDA_BOOTVERBOSE(
7783 				device_printf(sc->dev, "\n");
7784 				device_printf(sc->dev,
7785 				    "Processing %s FG cad=%d nid=%d...\n",
7786 				    (devinfo->node_type == HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) ? "audio":
7787 				    (devinfo->node_type == HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_MODEM) ? "modem":
7788 				    "unknown",
7789 				    devinfo->codec->cad, devinfo->nid);
7790 			);
7791 			if (devinfo->node_type !=
7792 			    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) {
7793 				HDA_BOOTHVERBOSE(
7794 					device_printf(sc->dev,
7795 					    "Powering down...\n");
7796 				);
7797 				hdac_command(sc,
7798 				    HDA_CMD_SET_POWER_STATE(codec->cad,
7799 				    devinfo->nid, HDA_CMD_POWER_STATE_D3),
7800 				    codec->cad);
7801 				continue;
7802 			}
7803 
7804 			HDA_BOOTHVERBOSE(
7805 				device_printf(sc->dev, "Powering up...\n");
7806 			);
7807 			hdac_powerup(devinfo);
7808 			HDA_BOOTHVERBOSE(
7809 				device_printf(sc->dev, "Parsing audio FG...\n");
7810 			);
7811 			hdac_audio_parse(devinfo);
7812 			HDA_BOOTHVERBOSE(
7813 				device_printf(sc->dev, "Parsing vendor patch...\n");
7814 			);
7815 			hdac_vendor_patch_parse(devinfo);
7816 			devinfo->function.audio.quirks |= quirks_on;
7817 			devinfo->function.audio.quirks &= ~quirks_off;
7818 
7819 			HDA_BOOTHVERBOSE(
7820 				device_printf(sc->dev, "Parsing Ctls...\n");
7821 			);
7822 			hdac_audio_ctl_parse(devinfo);
7823 			HDA_BOOTHVERBOSE(
7824 				device_printf(sc->dev, "Disabling nonaudio...\n");
7825 			);
7826 			hdac_audio_disable_nonaudio(devinfo);
7827 			HDA_BOOTHVERBOSE(
7828 				device_printf(sc->dev, "Disabling useless...\n");
7829 			);
7830 			hdac_audio_disable_useless(devinfo);
7831 			HDA_BOOTVERBOSE(
7832 				device_printf(sc->dev, "Patched pins configuration:\n");
7833 				hdac_dump_pin_configs(devinfo);
7834 			);
7835 			HDA_BOOTHVERBOSE(
7836 				device_printf(sc->dev, "Parsing pin associations...\n");
7837 			);
7838 			hdac_audio_as_parse(devinfo);
7839 			HDA_BOOTHVERBOSE(
7840 				device_printf(sc->dev, "Building AFG tree...\n");
7841 			);
7842 			hdac_audio_build_tree(devinfo);
7843 			HDA_BOOTHVERBOSE(
7844 				device_printf(sc->dev, "Disabling unassociated "
7845 				    "widgets...\n");
7846 			);
7847 			hdac_audio_disable_unas(devinfo);
7848 			HDA_BOOTHVERBOSE(
7849 				device_printf(sc->dev, "Disabling nonselected "
7850 				    "inputs...\n");
7851 			);
7852 			hdac_audio_disable_notselected(devinfo);
7853 			HDA_BOOTHVERBOSE(
7854 				device_printf(sc->dev, "Disabling useless...\n");
7855 			);
7856 			hdac_audio_disable_useless(devinfo);
7857 			HDA_BOOTHVERBOSE(
7858 				device_printf(sc->dev, "Disabling "
7859 				    "crossassociatement connections...\n");
7860 			);
7861 			hdac_audio_disable_crossas(devinfo);
7862 			HDA_BOOTHVERBOSE(
7863 				device_printf(sc->dev, "Disabling useless...\n");
7864 			);
7865 			hdac_audio_disable_useless(devinfo);
7866 			HDA_BOOTHVERBOSE(
7867 				device_printf(sc->dev, "Binding associations to channels...\n");
7868 			);
7869 			hdac_audio_bind_as(devinfo);
7870 			HDA_BOOTHVERBOSE(
7871 				device_printf(sc->dev, "Assigning names to signal sources...\n");
7872 			);
7873 			hdac_audio_assign_names(devinfo);
7874 			HDA_BOOTHVERBOSE(
7875 				device_printf(sc->dev, "Assigning mixers to the tree...\n");
7876 			);
7877 			hdac_audio_assign_mixers(devinfo);
7878 			HDA_BOOTHVERBOSE(
7879 				device_printf(sc->dev, "Preparing pin controls...\n");
7880 			);
7881 			hdac_audio_prepare_pin_ctrl(devinfo);
7882 			HDA_BOOTHVERBOSE(
7883 				device_printf(sc->dev, "AFG commit...\n");
7884 		    	);
7885 			hdac_audio_commit(devinfo);
7886 		    	HDA_BOOTHVERBOSE(
7887 				device_printf(sc->dev, "HP switch init...\n");
7888 			);
7889 			hdac_hp_switch_init(devinfo);
7890 
7891 			if ((devinfo->function.audio.quirks & HDA_QUIRK_DMAPOS) &&
7892 			    dmaalloc == 0) {
7893 				if (hdac_dma_alloc(sc, &sc->pos_dma,
7894 				    (sc->num_iss + sc->num_oss + sc->num_bss) * 8) != 0) {
7895 					HDA_BOOTVERBOSE(
7896 						device_printf(sc->dev, "Failed to "
7897 						    "allocate DMA pos buffer "
7898 						    "(non-fatal)\n");
7899 					);
7900 				} else
7901 					dmaalloc = 1;
7902 			}
7903 
7904 		    	HDA_BOOTHVERBOSE(
7905 				device_printf(sc->dev, "Creating PCM devices...\n");
7906 			);
7907 			hdac_create_pcms(devinfo);
7908 
7909 			HDA_BOOTVERBOSE(
7910 				if (devinfo->function.audio.quirks != 0) {
7911 					device_printf(sc->dev, "FG config/quirks:");
7912 					for (i = 0; i < HDAC_QUIRKS_TAB_LEN; i++) {
7913 						if ((devinfo->function.audio.quirks &
7914 						    hdac_quirks_tab[i].value) ==
7915 						    hdac_quirks_tab[i].value)
7916 							printf(" %s", hdac_quirks_tab[i].key);
7917 					}
7918 					printf("\n");
7919 				}
7920 
7921 				device_printf(sc->dev, "\n");
7922 				device_printf(sc->dev, "+-------------------+\n");
7923 				device_printf(sc->dev, "| DUMPING HDA NODES |\n");
7924 				device_printf(sc->dev, "+-------------------+\n");
7925 				hdac_dump_nodes(devinfo);
7926 			);
7927 
7928 			HDA_BOOTHVERBOSE(
7929 				device_printf(sc->dev, "\n");
7930 				device_printf(sc->dev, "+------------------------+\n");
7931 				device_printf(sc->dev, "| DUMPING HDA AMPLIFIERS |\n");
7932 				device_printf(sc->dev, "+------------------------+\n");
7933 				device_printf(sc->dev, "\n");
7934 				i = 0;
7935 				while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
7936 					device_printf(sc->dev, "%3d: nid %3d %s (%s) index %d", i,
7937 					    (ctl->widget != NULL) ? ctl->widget->nid : -1,
7938 					    (ctl->ndir == HDA_CTL_IN)?"in ":"out",
7939 					    (ctl->dir == HDA_CTL_IN)?"in ":"out",
7940 					    ctl->index);
7941 					if (ctl->childwidget != NULL)
7942 						printf(" cnid %3d", ctl->childwidget->nid);
7943 					else
7944 						printf("         ");
7945 					printf(" ossmask=0x%08x\n",
7946 					    ctl->ossmask);
7947 					device_printf(sc->dev,
7948 					    "       mute: %d step: %3d size: %3d off: %3d%s\n",
7949 					    ctl->mute, ctl->step, ctl->size, ctl->offset,
7950 					    (ctl->enable == 0) ? " [DISABLED]" :
7951 					    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
7952 				}
7953 			);
7954 		}
7955 	}
7956 	hdac_unlock(sc);
7957 
7958 	HDA_BOOTVERBOSE(
7959 		device_printf(sc->dev, "\n");
7960 	);
7961 
7962 	bus_generic_attach(sc->dev);
7963 
7964 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
7965 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
7966 	    "polling", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
7967 	    sysctl_hdac_polling, "I", "Enable polling mode");
7968 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
7969 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
7970 	    "polling_interval", CTLTYPE_INT | CTLFLAG_RW, sc->dev,
7971 	    sizeof(sc->dev), sysctl_hdac_polling_interval, "I",
7972 	    "Controller/Jack Sense polling interval (1-1000 ms)");
7973 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
7974 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
7975 	    "pindump", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
7976 	    sysctl_hdac_pindump, "I", "Dump pin states/data");
7977 }
7978 
7979 /****************************************************************************
7980  * int hdac_suspend(device_t)
7981  *
7982  * Suspend and power down HDA bus and codecs.
7983  ****************************************************************************/
7984 static int
7985 hdac_suspend(device_t dev)
7986 {
7987 	struct hdac_softc *sc;
7988 	struct hdac_codec *codec;
7989 	struct hdac_devinfo *devinfo;
7990 	int codec_index, fg_index, i;
7991 
7992 	HDA_BOOTHVERBOSE(
7993 		device_printf(dev, "Suspend...\n");
7994 	);
7995 
7996 	sc = device_get_softc(dev);
7997 	hdac_lock(sc);
7998 
7999 	HDA_BOOTHVERBOSE(
8000 		device_printf(dev, "Stop streams...\n");
8001 	);
8002 	for (i = 0; i < sc->num_chans; i++) {
8003 		if (sc->chans[i].flags & HDAC_CHN_RUNNING) {
8004 			sc->chans[i].flags |= HDAC_CHN_SUSPEND;
8005 			hdac_channel_stop(sc, &sc->chans[i]);
8006 		}
8007 	}
8008 
8009 	for (codec_index = 0; codec_index < HDAC_CODEC_MAX; codec_index++) {
8010 		codec = sc->codecs[codec_index];
8011 		if (codec == NULL)
8012 			continue;
8013 		for (fg_index = 0; fg_index < codec->num_fgs; fg_index++) {
8014 			devinfo = &codec->fgs[fg_index];
8015 			HDA_BOOTHVERBOSE(
8016 				device_printf(dev,
8017 				    "Power down FG"
8018 				    " cad=%d nid=%d to the D3 state...\n",
8019 				    codec->cad, devinfo->nid);
8020 			);
8021 			hdac_command(sc,
8022 			    HDA_CMD_SET_POWER_STATE(codec->cad,
8023 			    devinfo->nid, HDA_CMD_POWER_STATE_D3),
8024 			    codec->cad);
8025 		}
8026 	}
8027 
8028 	HDA_BOOTHVERBOSE(
8029 		device_printf(dev, "Reset controller...\n");
8030 	);
8031 	callout_stop(&sc->poll_hda);
8032 	callout_stop(&sc->poll_hdac);
8033 	callout_stop(&sc->poll_jack);
8034 	hdac_reset(sc, 0);
8035 	hdac_unlock(sc);
8036 	taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
8037 	callout_drain(&sc->poll_hda);
8038 	callout_drain(&sc->poll_hdac);
8039 	callout_drain(&sc->poll_jack);
8040 
8041 	HDA_BOOTHVERBOSE(
8042 		device_printf(dev, "Suspend done\n");
8043 	);
8044 
8045 	return (0);
8046 }
8047 
8048 /****************************************************************************
8049  * int hdac_resume(device_t)
8050  *
8051  * Powerup and restore HDA bus and codecs state.
8052  ****************************************************************************/
8053 static int
8054 hdac_resume(device_t dev)
8055 {
8056 	struct hdac_softc *sc;
8057 	struct hdac_codec *codec;
8058 	struct hdac_devinfo *devinfo;
8059 	int codec_index, fg_index, i;
8060 
8061 	HDA_BOOTHVERBOSE(
8062 		device_printf(dev, "Resume...\n");
8063 	);
8064 
8065 	sc = device_get_softc(dev);
8066 	hdac_lock(sc);
8067 
8068 	/* Quiesce everything */
8069 	HDA_BOOTHVERBOSE(
8070 		device_printf(dev, "Reset controller...\n");
8071 	);
8072 	hdac_reset(sc, 1);
8073 
8074 	/* Initialize the CORB and RIRB */
8075 	hdac_corb_init(sc);
8076 	hdac_rirb_init(sc);
8077 
8078 	/* Start the corb and rirb engines */
8079 	HDA_BOOTHVERBOSE(
8080 		device_printf(dev, "Starting CORB Engine...\n");
8081 	);
8082 	hdac_corb_start(sc);
8083 	HDA_BOOTHVERBOSE(
8084 		device_printf(dev, "Starting RIRB Engine...\n");
8085 	);
8086 	hdac_rirb_start(sc);
8087 
8088 	HDA_BOOTHVERBOSE(
8089 		device_printf(dev,
8090 		    "Enabling controller interrupt...\n");
8091 	);
8092 	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
8093 	    HDAC_GCTL_UNSOL);
8094 	if (sc->polling == 0) {
8095 		HDAC_WRITE_4(&sc->mem, HDAC_INTCTL,
8096 		    HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
8097 	} else {
8098 		callout_reset(&sc->poll_hdac, 1, hdac_poll_callback, sc);
8099 	}
8100 	DELAY(1000);
8101 
8102 	for (codec_index = 0; codec_index < HDAC_CODEC_MAX; codec_index++) {
8103 		codec = sc->codecs[codec_index];
8104 		if (codec == NULL)
8105 			continue;
8106 		for (fg_index = 0; fg_index < codec->num_fgs; fg_index++) {
8107 			devinfo = &codec->fgs[fg_index];
8108 			if (devinfo->node_type !=
8109 			    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) {
8110 				HDA_BOOTHVERBOSE(
8111 					device_printf(dev,
8112 					    "Power down unsupported non-audio FG"
8113 					    " cad=%d nid=%d to the D3 state...\n",
8114 					    codec->cad, devinfo->nid);
8115 				);
8116 				hdac_command(sc,
8117 				    HDA_CMD_SET_POWER_STATE(codec->cad,
8118 				    devinfo->nid, HDA_CMD_POWER_STATE_D3),
8119 				    codec->cad);
8120 				continue;
8121 			}
8122 
8123 			HDA_BOOTHVERBOSE(
8124 				device_printf(dev,
8125 				    "Power up audio FG cad=%d nid=%d...\n",
8126 				    devinfo->codec->cad, devinfo->nid);
8127 			);
8128 			hdac_powerup(devinfo);
8129 			HDA_BOOTHVERBOSE(
8130 				device_printf(dev, "AFG commit...\n");
8131 		    	);
8132 			hdac_audio_commit(devinfo);
8133 		    	HDA_BOOTHVERBOSE(
8134 				device_printf(dev, "HP switch init...\n");
8135 			);
8136 			hdac_hp_switch_init(devinfo);
8137 
8138 			hdac_unlock(sc);
8139 			for (i = 0; i < devinfo->function.audio.num_devs; i++) {
8140 				struct hdac_pcm_devinfo *pdevinfo =
8141 				    &devinfo->function.audio.devs[i];
8142 				HDA_BOOTHVERBOSE(
8143 					device_printf(pdevinfo->dev,
8144 					    "OSS mixer reinitialization...\n");
8145 				);
8146 				if (mixer_reinit(pdevinfo->dev) == -1)
8147 					device_printf(pdevinfo->dev,
8148 					    "unable to reinitialize the mixer\n");
8149 			}
8150 			hdac_lock(sc);
8151 		}
8152 	}
8153 
8154 	HDA_BOOTHVERBOSE(
8155 		device_printf(dev, "Start streams...\n");
8156 	);
8157 	for (i = 0; i < sc->num_chans; i++) {
8158 		if (sc->chans[i].flags & HDAC_CHN_SUSPEND) {
8159 			sc->chans[i].flags &= ~HDAC_CHN_SUSPEND;
8160 			hdac_channel_start(sc, &sc->chans[i]);
8161 		}
8162 	}
8163 
8164 	hdac_unlock(sc);
8165 
8166 	HDA_BOOTHVERBOSE(
8167 		device_printf(dev, "Resume done\n");
8168 	);
8169 
8170 	return (0);
8171 }
8172 /****************************************************************************
8173  * int hdac_detach(device_t)
8174  *
8175  * Detach and free up resources utilized by the hdac device.
8176  ****************************************************************************/
8177 static int
8178 hdac_detach(device_t dev)
8179 {
8180 	struct hdac_softc *sc;
8181 	device_t *devlist;
8182 	int i, devcount, error;
8183 
8184 	if ((error = device_get_children(dev, &devlist, &devcount)) != 0)
8185 		return (error);
8186 	for (i = 0; i < devcount; i++) {
8187 		if ((error = device_delete_child(dev, devlist[i])) != 0) {
8188 			free(devlist, M_TEMP);
8189 			return (error);
8190 		}
8191 	}
8192 	free(devlist, M_TEMP);
8193 
8194 	sc = device_get_softc(dev);
8195 	hdac_release_resources(sc);
8196 
8197 	return (0);
8198 }
8199 
8200 static int
8201 hdac_print_child(device_t dev, device_t child)
8202 {
8203 	struct hdac_pcm_devinfo *pdevinfo =
8204 	    (struct hdac_pcm_devinfo *)device_get_ivars(child);
8205 	int retval;
8206 
8207 	retval = bus_print_child_header(dev, child);
8208 	retval += printf(" at cad %d nid %d",
8209 	    pdevinfo->devinfo->codec->cad, pdevinfo->devinfo->nid);
8210 	retval += bus_print_child_footer(dev, child);
8211 
8212 	return (retval);
8213 }
8214 
8215 static device_method_t hdac_methods[] = {
8216 	/* device interface */
8217 	DEVMETHOD(device_probe,		hdac_probe),
8218 	DEVMETHOD(device_attach,	hdac_attach),
8219 	DEVMETHOD(device_detach,	hdac_detach),
8220 	DEVMETHOD(device_suspend,	hdac_suspend),
8221 	DEVMETHOD(device_resume,	hdac_resume),
8222 	/* Bus interface */
8223 	DEVMETHOD(bus_print_child,	hdac_print_child),
8224 	{ 0, 0 }
8225 };
8226 
8227 static driver_t hdac_driver = {
8228 	"hdac",
8229 	hdac_methods,
8230 	sizeof(struct hdac_softc),
8231 };
8232 
8233 static devclass_t hdac_devclass;
8234 
8235 DRIVER_MODULE(snd_hda, pci, hdac_driver, hdac_devclass, 0, 0);
8236 MODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
8237 MODULE_VERSION(snd_hda, 1);
8238 
8239 static int
8240 hdac_pcm_probe(device_t dev)
8241 {
8242 	struct hdac_pcm_devinfo *pdevinfo =
8243 	    (struct hdac_pcm_devinfo *)device_get_ivars(dev);
8244 	char buf[128];
8245 
8246 	snprintf(buf, sizeof(buf), "HDA %s PCM #%d %s",
8247 	    hdac_codec_name(pdevinfo->devinfo->codec),
8248 	    pdevinfo->index,
8249 	    (pdevinfo->digital == 3)?"DisplayPort":
8250 	    ((pdevinfo->digital == 2)?"HDMI":
8251 	    ((pdevinfo->digital)?"Digital":"Analog")));
8252 	device_set_desc_copy(dev, buf);
8253 	return (0);
8254 }
8255 
8256 static int
8257 hdac_pcm_attach(device_t dev)
8258 {
8259 	struct hdac_pcm_devinfo *pdevinfo =
8260 	    (struct hdac_pcm_devinfo *)device_get_ivars(dev);
8261 	struct hdac_softc *sc = pdevinfo->devinfo->codec->sc;
8262 	char status[SND_STATUSLEN];
8263 	int i;
8264 
8265 	pdevinfo->chan_size = pcm_getbuffersize(dev,
8266 	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
8267 
8268 	HDA_BOOTVERBOSE(
8269 		device_printf(dev, "+--------------------------------------+\n");
8270 		device_printf(dev, "| DUMPING PCM Playback/Record Channels |\n");
8271 		device_printf(dev, "+--------------------------------------+\n");
8272 		hdac_dump_pcmchannels(pdevinfo);
8273 		device_printf(dev, "\n");
8274 		device_printf(dev, "+-------------------------------+\n");
8275 		device_printf(dev, "| DUMPING Playback/Record Paths |\n");
8276 		device_printf(dev, "+-------------------------------+\n");
8277 		hdac_dump_dac(pdevinfo);
8278 		hdac_dump_adc(pdevinfo);
8279 		hdac_dump_mix(pdevinfo);
8280 		device_printf(dev, "\n");
8281 		device_printf(dev, "+-------------------------+\n");
8282 		device_printf(dev, "| DUMPING Volume Controls |\n");
8283 		device_printf(dev, "+-------------------------+\n");
8284 		hdac_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
8285 		hdac_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
8286 		hdac_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
8287 		hdac_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
8288 		hdac_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
8289 		hdac_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
8290 		hdac_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
8291 		hdac_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
8292 		hdac_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
8293 		hdac_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
8294 		hdac_dump_ctls(pdevinfo, NULL, 0);
8295 		device_printf(dev, "\n");
8296 	);
8297 
8298 	if (resource_int_value(device_get_name(dev),
8299 	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
8300 		i &= HDA_BLK_ALIGN;
8301 		if (i < HDA_BLK_MIN)
8302 			i = HDA_BLK_MIN;
8303 		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
8304 		i = 0;
8305 		while (pdevinfo->chan_blkcnt >> i)
8306 			i++;
8307 		pdevinfo->chan_blkcnt = 1 << (i - 1);
8308 		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
8309 			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
8310 		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
8311 			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
8312 	} else
8313 		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
8314 
8315 	/*
8316 	 * We don't register interrupt handler with snd_setup_intr
8317 	 * in pcm device. Mark pcm device as MPSAFE manually.
8318 	 */
8319 	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
8320 
8321 	HDA_BOOTHVERBOSE(
8322 		device_printf(dev, "OSS mixer initialization...\n");
8323 	);
8324 	if (mixer_init(dev, &hdac_audio_ctl_ossmixer_class, pdevinfo) != 0)
8325 		device_printf(dev, "Can't register mixer\n");
8326 
8327 	HDA_BOOTHVERBOSE(
8328 		device_printf(dev, "Registering PCM channels...\n");
8329 	);
8330 	if (pcm_register(dev, pdevinfo, (pdevinfo->play >= 0)?1:0,
8331 	    (pdevinfo->rec >= 0)?1:0) != 0)
8332 		device_printf(dev, "Can't register PCM\n");
8333 
8334 	pdevinfo->registered++;
8335 
8336 	if (pdevinfo->play >= 0)
8337 		pcm_addchan(dev, PCMDIR_PLAY, &hdac_channel_class, pdevinfo);
8338 	if (pdevinfo->rec >= 0)
8339 		pcm_addchan(dev, PCMDIR_REC, &hdac_channel_class, pdevinfo);
8340 
8341 	snprintf(status, SND_STATUSLEN, "at cad %d nid %d on %s %s",
8342 	    pdevinfo->devinfo->codec->cad, pdevinfo->devinfo->nid,
8343 	    device_get_nameunit(sc->dev), PCM_KLDSTRING(snd_hda));
8344 	pcm_setstatus(dev, status);
8345 
8346 	return (0);
8347 }
8348 
8349 static int
8350 hdac_pcm_detach(device_t dev)
8351 {
8352 	struct hdac_pcm_devinfo *pdevinfo =
8353 	    (struct hdac_pcm_devinfo *)device_get_ivars(dev);
8354 	int err;
8355 
8356 	if (pdevinfo->registered > 0) {
8357 		err = pcm_unregister(dev);
8358 		if (err != 0)
8359 			return (err);
8360 	}
8361 
8362 	return (0);
8363 }
8364 
8365 static device_method_t hdac_pcm_methods[] = {
8366 	/* device interface */
8367 	DEVMETHOD(device_probe,		hdac_pcm_probe),
8368 	DEVMETHOD(device_attach,	hdac_pcm_attach),
8369 	DEVMETHOD(device_detach,	hdac_pcm_detach),
8370 	{ 0, 0 }
8371 };
8372 
8373 static driver_t hdac_pcm_driver = {
8374 	"pcm",
8375 	hdac_pcm_methods,
8376 	PCM_SOFTC_SIZE,
8377 };
8378 
8379 DRIVER_MODULE(snd_hda_pcm, hdac, hdac_pcm_driver, pcm_devclass, 0, 0);
8380 
8381