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