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