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