hdac.c revision 204351
1/*-
2 * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3 * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4 * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * Intel High Definition Audio (Controller) driver for FreeBSD. Be advised
31 * that this driver still in its early stage, and possible of rewrite are
32 * pretty much guaranteed. There are supposedly several distinct parent/child
33 * busses to make this "perfect", but as for now and for the sake of
34 * simplicity, everything is gobble up within single source.
35 *
36 * List of subsys:
37 *     1) HDA Controller support
38 *     2) HDA Codecs support, which may include
39 *        - HDA
40 *        - Modem
41 *     3) Widget parser - the real magic of why this driver works on so
42 *        many hardwares with minimal vendor specific quirk. The original
43 *        parser was written using Ruby and can be found at
44 *        http://people.freebsd.org/~ariff/HDA/parser.rb . This crude
45 *        ruby parser take the verbose dmesg dump as its input. Refer to
46 *        http://www.microsoft.com/whdc/device/audio/default.mspx for various
47 *        interesting documents, especially UAA (Universal Audio Architecture).
48 *     4) Possible vendor specific support.
49 *        (snd_hda_intel, snd_hda_ati, etc..)
50 *
51 * Thanks to Ahmad Ubaidah Omar @ Defenxis Sdn. Bhd. for the
52 * Compaq V3000 with Conexant HDA.
53 *
54 *    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
55 *    *                                                                 *
56 *    *        This driver is a collaborative effort made by:           *
57 *    *                                                                 *
58 *    *          Stephane E. Potvin <sepotvin@videotron.ca>             *
59 *    *               Andrea Bittau <a.bittau@cs.ucl.ac.uk>             *
60 *    *               Wesley Morgan <morganw@chemikals.org>             *
61 *    *              Daniel Eischen <deischen@FreeBSD.org>              *
62 *    *             Maxime Guillaud <bsd-ports@mguillaud.net>           *
63 *    *              Ariff Abdullah <ariff@FreeBSD.org>                 *
64 *    *             Alexander Motin <mav@FreeBSD.org>                   *
65 *    *                                                                 *
66 *    *   ....and various people from freebsd-multimedia@FreeBSD.org    *
67 *    *                                                                 *
68 *    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
69 */
70
71#ifdef HAVE_KERNEL_OPTION_HEADERS
72#include "opt_snd.h"
73#endif
74
75#include <dev/sound/pcm/sound.h>
76#include <dev/pci/pcireg.h>
77#include <dev/pci/pcivar.h>
78
79#include <sys/ctype.h>
80#include <sys/taskqueue.h>
81
82#include <dev/sound/pci/hda/hdac_private.h>
83#include <dev/sound/pci/hda/hdac_reg.h>
84#include <dev/sound/pci/hda/hda_reg.h>
85#include <dev/sound/pci/hda/hdac.h>
86
87#include "mixer_if.h"
88
89#define HDA_DRV_TEST_REV	"20100226_0142"
90
91SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/hda/hdac.c 204351 2010-02-26 09:31:50Z 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	uint16_t chmap[2][5] = {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x0231 }, /* 5.1 */
3459				{ 0x0010, 0x0001, 0x2001, 0x2031, 0x2431 }};/* 7.1 */
3460	int map = -1;
3461
3462	totalchn = AFMT_CHANNEL(ch->fmt);
3463	HDA_BOOTHVERBOSE(
3464		device_printf(ch->pdevinfo->dev,
3465		    "PCMDIR_%s: Stream setup fmt=%08x speed=%d\n",
3466		    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
3467		    ch->fmt, ch->spd);
3468	);
3469	fmt = 0;
3470	if (ch->fmt & AFMT_S16_LE)
3471		fmt |= ch->bit16 << 4;
3472	else if (ch->fmt & AFMT_S32_LE)
3473		fmt |= ch->bit32 << 4;
3474	else
3475		fmt |= 1 << 4;
3476	for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
3477		if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
3478			fmt |= hda_rate_tab[i].base;
3479			fmt |= hda_rate_tab[i].mul;
3480			fmt |= hda_rate_tab[i].div;
3481			break;
3482		}
3483	}
3484	fmt |= (totalchn - 1);
3485
3486	/* Set channel mapping for known speaker setups. */
3487	if (as->pinset == 0x0007 || as->pinset == 0x0013) /* Standard 5.1 */
3488		map = 0;
3489	else if (as->pinset == 0x0017) /* Standard 7.1 */
3490		map = 1;
3491
3492	HDAC_WRITE_2(&sc->mem, ch->off + HDAC_SDFMT, fmt);
3493
3494	dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN;
3495	if (ch->fmt & AFMT_AC3)
3496		dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO;
3497
3498	chn = 0;
3499	for (i = 0; ch->io[i] != -1; i++) {
3500		w = hdac_widget_get(ch->devinfo, ch->io[i]);
3501		if (w == NULL)
3502			continue;
3503
3504		/* If HP redirection is enabled, but failed to use same
3505		   DAC, make last DAC to duplicate first one. */
3506		if (as->fakeredir && i == (as->pincnt - 1)) {
3507			c = (ch->sid << 4);
3508		} else {
3509			if (map >= 0) /* Map known speaker setups. */
3510				chn = (((chmap[map][totalchn / 2] >> i * 4) &
3511				    0xf) - 1) * 2;
3512			if (chn < 0 || chn >= totalchn) {
3513				c = 0;
3514			} else {
3515				c = (ch->sid << 4) | chn;
3516			}
3517		}
3518		HDA_BOOTHVERBOSE(
3519			device_printf(ch->pdevinfo->dev,
3520			    "PCMDIR_%s: Stream setup nid=%d: "
3521			    "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x\n",
3522			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
3523			    ch->io[i], fmt, dfmt, c);
3524		);
3525		hdac_command(sc,
3526		    HDA_CMD_SET_CONV_FMT(cad, ch->io[i], fmt), cad);
3527		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
3528			hdac_command(sc,
3529			    HDA_CMD_SET_DIGITAL_CONV_FMT1(cad, ch->io[i], dfmt),
3530			    cad);
3531		}
3532		hdac_command(sc,
3533		    HDA_CMD_SET_CONV_STREAM_CHAN(cad, ch->io[i], c), cad);
3534#if 0
3535		hdac_command(sc,
3536		    HDA_CMD_SET_CONV_CHAN_COUNT(cad, ch->io[i], 1), cad);
3537		hdac_command(sc,
3538		    HDA_CMD_SET_HDMI_CHAN_SLOT(cad, ch->io[i], 0x00), cad);
3539		hdac_command(sc,
3540		    HDA_CMD_SET_HDMI_CHAN_SLOT(cad, ch->io[i], 0x11), cad);
3541#endif
3542		chn += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
3543	}
3544}
3545
3546/*
3547 * Greatest Common Divisor.
3548 */
3549static unsigned
3550gcd(unsigned a, unsigned b)
3551{
3552	u_int c;
3553
3554	while (b != 0) {
3555		c = a;
3556		a = b;
3557		b = (c % b);
3558	}
3559	return (a);
3560}
3561
3562/*
3563 * Least Common Multiple.
3564 */
3565static unsigned
3566lcm(unsigned a, unsigned b)
3567{
3568
3569	return ((a * b) / gcd(a, b));
3570}
3571
3572static int
3573hdac_channel_setfragments(kobj_t obj, void *data,
3574					uint32_t blksz, uint32_t blkcnt)
3575{
3576	struct hdac_chan *ch = data;
3577	struct hdac_softc *sc = ch->devinfo->codec->sc;
3578
3579	blksz -= blksz % lcm(HDAC_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
3580
3581	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
3582		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
3583	if (blksz < HDA_BLK_MIN)
3584		blksz = HDA_BLK_MIN;
3585	if (blkcnt > HDA_BDL_MAX)
3586		blkcnt = HDA_BDL_MAX;
3587	if (blkcnt < HDA_BDL_MIN)
3588		blkcnt = HDA_BDL_MIN;
3589
3590	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
3591		if ((blkcnt >> 1) >= HDA_BDL_MIN)
3592			blkcnt >>= 1;
3593		else if ((blksz >> 1) >= HDA_BLK_MIN)
3594			blksz >>= 1;
3595		else
3596			break;
3597	}
3598
3599	if ((sndbuf_getblksz(ch->b) != blksz ||
3600	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
3601	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
3602		device_printf(sc->dev, "%s: failed blksz=%u blkcnt=%u\n",
3603		    __func__, blksz, blkcnt);
3604
3605	ch->blksz = sndbuf_getblksz(ch->b);
3606	ch->blkcnt = sndbuf_getblkcnt(ch->b);
3607
3608	return (0);
3609}
3610
3611static uint32_t
3612hdac_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
3613{
3614	struct hdac_chan *ch = data;
3615
3616	hdac_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
3617
3618	return (ch->blksz);
3619}
3620
3621static void
3622hdac_channel_stop(struct hdac_softc *sc, struct hdac_chan *ch)
3623{
3624	struct hdac_devinfo *devinfo = ch->devinfo;
3625	struct hdac_widget *w;
3626	nid_t cad = devinfo->codec->cad;
3627	int i;
3628
3629	hdac_stream_stop(ch);
3630
3631	for (i = 0; ch->io[i] != -1; i++) {
3632		w = hdac_widget_get(ch->devinfo, ch->io[i]);
3633		if (w == NULL)
3634			continue;
3635		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
3636			hdac_command(sc,
3637			    HDA_CMD_SET_DIGITAL_CONV_FMT1(cad, ch->io[i], 0),
3638			    cad);
3639		}
3640		hdac_command(sc,
3641		    HDA_CMD_SET_CONV_STREAM_CHAN(cad, ch->io[i],
3642		    0), cad);
3643	}
3644}
3645
3646static void
3647hdac_channel_start(struct hdac_softc *sc, struct hdac_chan *ch)
3648{
3649	ch->ptr = 0;
3650	ch->prevptr = 0;
3651	hdac_stream_stop(ch);
3652	hdac_stream_reset(ch);
3653	hdac_bdl_setup(ch);
3654	hdac_stream_setid(ch);
3655	hdac_stream_setup(ch);
3656	hdac_stream_start(ch);
3657}
3658
3659static int
3660hdac_channel_trigger(kobj_t obj, void *data, int go)
3661{
3662	struct hdac_chan *ch = data;
3663	struct hdac_softc *sc = ch->devinfo->codec->sc;
3664
3665	if (!PCMTRIG_COMMON(go))
3666		return (0);
3667
3668	hdac_lock(sc);
3669	switch (go) {
3670	case PCMTRIG_START:
3671		hdac_channel_start(sc, ch);
3672		break;
3673	case PCMTRIG_STOP:
3674	case PCMTRIG_ABORT:
3675		hdac_channel_stop(sc, ch);
3676		break;
3677	default:
3678		break;
3679	}
3680	hdac_unlock(sc);
3681
3682	return (0);
3683}
3684
3685static uint32_t
3686hdac_channel_getptr(kobj_t obj, void *data)
3687{
3688	struct hdac_chan *ch = data;
3689	struct hdac_softc *sc = ch->devinfo->codec->sc;
3690	uint32_t ptr;
3691
3692	hdac_lock(sc);
3693	if (sc->polling != 0)
3694		ptr = ch->ptr;
3695	else if (ch->dmapos != NULL)
3696		ptr = *(ch->dmapos);
3697	else
3698		ptr = HDAC_READ_4(&sc->mem, ch->off + HDAC_SDLPIB);
3699	hdac_unlock(sc);
3700
3701	/*
3702	 * Round to available space and force 128 bytes aligment.
3703	 */
3704	ptr %= ch->blksz * ch->blkcnt;
3705	ptr &= HDA_BLK_ALIGN;
3706
3707	return (ptr);
3708}
3709
3710static struct pcmchan_caps *
3711hdac_channel_getcaps(kobj_t obj, void *data)
3712{
3713	return (&((struct hdac_chan *)data)->caps);
3714}
3715
3716static kobj_method_t hdac_channel_methods[] = {
3717	KOBJMETHOD(channel_init,		hdac_channel_init),
3718	KOBJMETHOD(channel_setformat,		hdac_channel_setformat),
3719	KOBJMETHOD(channel_setspeed,		hdac_channel_setspeed),
3720	KOBJMETHOD(channel_setblocksize,	hdac_channel_setblocksize),
3721	KOBJMETHOD(channel_setfragments,	hdac_channel_setfragments),
3722	KOBJMETHOD(channel_trigger,		hdac_channel_trigger),
3723	KOBJMETHOD(channel_getptr,		hdac_channel_getptr),
3724	KOBJMETHOD(channel_getcaps,		hdac_channel_getcaps),
3725	KOBJMETHOD_END
3726};
3727CHANNEL_DECLARE(hdac_channel);
3728
3729static int
3730hdac_audio_ctl_ossmixer_init(struct snd_mixer *m)
3731{
3732	struct hdac_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
3733	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
3734	struct hdac_softc *sc = devinfo->codec->sc;
3735	struct hdac_widget *w, *cw;
3736	struct hdac_audio_ctl *ctl;
3737	uint32_t mask, recmask, id;
3738	int i, j, softpcmvol;
3739
3740	hdac_lock(sc);
3741
3742	/* Make sure that in case of soft volume it won't stay muted. */
3743	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
3744		pdevinfo->left[i] = 100;
3745		pdevinfo->right[i] = 100;
3746	}
3747
3748	mask = 0;
3749	recmask = 0;
3750	id = hdac_codec_id(devinfo->codec);
3751
3752	/* Declate EAPD as ogain control. */
3753	if (pdevinfo->play >= 0) {
3754		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3755			w = hdac_widget_get(devinfo, i);
3756			if (w == NULL || w->enable == 0)
3757				continue;
3758			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3759			    w->param.eapdbtl == HDAC_INVALID ||
3760			    w->bindas != sc->chans[pdevinfo->play].as)
3761				continue;
3762			mask |= SOUND_MASK_OGAIN;
3763			break;
3764		}
3765	}
3766
3767	/* Declare volume controls assigned to this association. */
3768	i = 0;
3769	ctl = NULL;
3770	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
3771		if (ctl->enable == 0)
3772			continue;
3773		if ((pdevinfo->play >= 0 &&
3774		    ctl->widget->bindas == sc->chans[pdevinfo->play].as) ||
3775		    (pdevinfo->rec >= 0 &&
3776		    ctl->widget->bindas == sc->chans[pdevinfo->rec].as) ||
3777		    (ctl->widget->bindas == -2 && pdevinfo->index == 0))
3778			mask |= ctl->ossmask;
3779	}
3780
3781	/* Declare record sources available to this association. */
3782	if (pdevinfo->rec >= 0) {
3783		struct hdac_chan *ch = &sc->chans[pdevinfo->rec];
3784		for (i = 0; ch->io[i] != -1; i++) {
3785			w = hdac_widget_get(devinfo, ch->io[i]);
3786			if (w == NULL || w->enable == 0)
3787				continue;
3788			for (j = 0; j < w->nconns; j++) {
3789				if (w->connsenable[j] == 0)
3790					continue;
3791				cw = hdac_widget_get(devinfo, w->conns[j]);
3792				if (cw == NULL || cw->enable == 0)
3793					continue;
3794				if (cw->bindas != sc->chans[pdevinfo->rec].as &&
3795				    cw->bindas != -2)
3796					continue;
3797				recmask |= cw->ossmask;
3798			}
3799		}
3800	}
3801
3802	/* Declare soft PCM volume if needed. */
3803	if (pdevinfo->play >= 0) {
3804		ctl = NULL;
3805		if ((mask & SOUND_MASK_PCM) == 0 ||
3806		    (devinfo->function.audio.quirks & HDA_QUIRK_SOFTPCMVOL)) {
3807			softpcmvol = 1;
3808			mask |= SOUND_MASK_PCM;
3809		} else {
3810			softpcmvol = 0;
3811			i = 0;
3812			while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
3813				if (ctl->enable == 0)
3814					continue;
3815				if (ctl->widget->bindas != sc->chans[pdevinfo->play].as &&
3816				    (ctl->widget->bindas != -2 || pdevinfo->index != 0))
3817					continue;
3818				if (!(ctl->ossmask & SOUND_MASK_PCM))
3819					continue;
3820				if (ctl->step > 0)
3821					break;
3822			}
3823		}
3824
3825		if (softpcmvol == 1 || ctl == NULL) {
3826			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
3827			HDA_BOOTVERBOSE(
3828				device_printf(pdevinfo->dev,
3829				    "%s Soft PCM volume\n",
3830				    (softpcmvol == 1) ? "Forcing" : "Enabling");
3831			);
3832		}
3833	}
3834
3835	/* Declare master volume if needed. */
3836	if (pdevinfo->play >= 0) {
3837		if ((mask & (SOUND_MASK_VOLUME | SOUND_MASK_PCM)) ==
3838		    SOUND_MASK_PCM) {
3839			mask |= SOUND_MASK_VOLUME;
3840			mix_setparentchild(m, SOUND_MIXER_VOLUME,
3841			    SOUND_MASK_PCM);
3842			mix_setrealdev(m, SOUND_MIXER_VOLUME,
3843			    SOUND_MIXER_NONE);
3844			HDA_BOOTVERBOSE(
3845				device_printf(pdevinfo->dev,
3846				    "Forcing master volume with PCM\n");
3847			);
3848		}
3849	}
3850
3851	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
3852	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
3853
3854	mix_setrecdevs(m, recmask);
3855	mix_setdevs(m, mask);
3856
3857	hdac_unlock(sc);
3858
3859	return (0);
3860}
3861
3862static int
3863hdac_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
3864					unsigned left, unsigned right)
3865{
3866	struct hdac_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
3867	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
3868	struct hdac_softc *sc = devinfo->codec->sc;
3869	struct hdac_widget *w;
3870	struct hdac_audio_ctl *ctl;
3871	uint32_t mute;
3872	int lvol, rvol;
3873	int i, j;
3874
3875	hdac_lock(sc);
3876	/* Save new values. */
3877	pdevinfo->left[dev] = left;
3878	pdevinfo->right[dev] = right;
3879
3880	/* 'ogain' is the special case implemented with EAPD. */
3881	if (dev == SOUND_MIXER_OGAIN) {
3882		uint32_t orig;
3883		w = NULL;
3884		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3885			w = hdac_widget_get(devinfo, i);
3886			if (w == NULL || w->enable == 0)
3887				continue;
3888			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3889			    w->param.eapdbtl == HDAC_INVALID)
3890				continue;
3891			break;
3892		}
3893		if (i >= devinfo->endnode) {
3894			hdac_unlock(sc);
3895			return (-1);
3896		}
3897		orig = w->param.eapdbtl;
3898		if (left == 0)
3899			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
3900		else
3901			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
3902		if (orig != w->param.eapdbtl) {
3903			uint32_t val;
3904
3905			val = w->param.eapdbtl;
3906			if (devinfo->function.audio.quirks & HDA_QUIRK_EAPDINV)
3907				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
3908			hdac_command(sc,
3909			    HDA_CMD_SET_EAPD_BTL_ENABLE(devinfo->codec->cad,
3910			    w->nid, val), devinfo->codec->cad);
3911		}
3912		hdac_unlock(sc);
3913		return (left | (left << 8));
3914	}
3915
3916	/* Recalculate all controls related to this OSS device. */
3917	i = 0;
3918	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
3919		if (ctl->enable == 0 ||
3920		    !(ctl->ossmask & (1 << dev)))
3921			continue;
3922		if (!((pdevinfo->play >= 0 &&
3923		    ctl->widget->bindas == sc->chans[pdevinfo->play].as) ||
3924		    (pdevinfo->rec >= 0 &&
3925		    ctl->widget->bindas == sc->chans[pdevinfo->rec].as) ||
3926		    ctl->widget->bindas == -2))
3927			continue;
3928
3929		lvol = 100;
3930		rvol = 100;
3931		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
3932			if (ctl->ossmask & (1 << j)) {
3933				lvol = lvol * pdevinfo->left[j] / 100;
3934				rvol = rvol * pdevinfo->right[j] / 100;
3935			}
3936		}
3937		mute = (lvol == 0) ? HDA_AMP_MUTE_LEFT : 0;
3938		mute |= (rvol == 0) ? HDA_AMP_MUTE_RIGHT : 0;
3939		lvol = (lvol * ctl->step + 50) / 100;
3940		rvol = (rvol * ctl->step + 50) / 100;
3941		hdac_audio_ctl_amp_set(ctl, mute, lvol, rvol);
3942	}
3943	hdac_unlock(sc);
3944
3945	return (left | (right << 8));
3946}
3947
3948/*
3949 * Commutate specified record source.
3950 */
3951static uint32_t
3952hdac_audio_ctl_recsel_comm(struct hdac_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
3953{
3954	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
3955	struct hdac_widget *w, *cw;
3956	struct hdac_audio_ctl *ctl;
3957	char buf[64];
3958	int i, muted;
3959	uint32_t res = 0;
3960
3961	if (depth > HDA_PARSE_MAXDEPTH)
3962		return (0);
3963
3964	w = hdac_widget_get(devinfo, nid);
3965	if (w == NULL || w->enable == 0)
3966		return (0);
3967
3968	for (i = 0; i < w->nconns; i++) {
3969		if (w->connsenable[i] == 0)
3970			continue;
3971		cw = hdac_widget_get(devinfo, w->conns[i]);
3972		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
3973			continue;
3974		/* Call recursively to trace signal to it's source if needed. */
3975		if ((src & cw->ossmask) != 0) {
3976			if (cw->ossdev < 0) {
3977				res |= hdac_audio_ctl_recsel_comm(pdevinfo, src,
3978				    w->conns[i], depth + 1);
3979			} else {
3980				res |= cw->ossmask;
3981			}
3982		}
3983		/* We have two special cases: mixers and others (selectors). */
3984		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
3985			ctl = hdac_audio_ctl_amp_get(devinfo,
3986			    w->nid, HDA_CTL_IN, i, 1);
3987			if (ctl == NULL)
3988				continue;
3989			/* If we have input control on this node mute them
3990			 * according to requested sources. */
3991			muted = (src & cw->ossmask) ? 0 : 1;
3992	    		if (muted != ctl->forcemute) {
3993				ctl->forcemute = muted;
3994				hdac_audio_ctl_amp_set(ctl,
3995				    HDA_AMP_MUTE_DEFAULT,
3996				    HDA_AMP_VOL_DEFAULT, HDA_AMP_VOL_DEFAULT);
3997			}
3998			HDA_BOOTHVERBOSE(
3999				device_printf(pdevinfo->dev,
4000				    "Recsel (%s): nid %d source %d %s\n",
4001				    hdac_audio_ctl_ossmixer_mask2allname(
4002				    src, buf, sizeof(buf)),
4003				    nid, i, muted?"mute":"unmute");
4004			);
4005		} else {
4006			if (w->nconns == 1)
4007				break;
4008			if ((src & cw->ossmask) == 0)
4009				continue;
4010			/* If we found requested source - select it and exit. */
4011			hdac_widget_connection_select(w, i);
4012			HDA_BOOTHVERBOSE(
4013				device_printf(pdevinfo->dev,
4014				    "Recsel (%s): nid %d source %d select\n",
4015				    hdac_audio_ctl_ossmixer_mask2allname(
4016			    	    src, buf, sizeof(buf)),
4017				    nid, i);
4018			);
4019			break;
4020		}
4021	}
4022	return (res);
4023}
4024
4025static uint32_t
4026hdac_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
4027{
4028	struct hdac_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
4029	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
4030	struct hdac_widget *w;
4031	struct hdac_softc *sc = devinfo->codec->sc;
4032	struct hdac_chan *ch;
4033	int i;
4034	uint32_t ret = 0xffffffff;
4035
4036	hdac_lock(sc);
4037
4038	/* Commutate requested recsrc for each ADC. */
4039	ch = &sc->chans[pdevinfo->rec];
4040	for (i = 0; ch->io[i] != -1; i++) {
4041		w = hdac_widget_get(devinfo, ch->io[i]);
4042		if (w == NULL || w->enable == 0)
4043			continue;
4044		ret &= hdac_audio_ctl_recsel_comm(pdevinfo, src, ch->io[i], 0);
4045	}
4046
4047	hdac_unlock(sc);
4048
4049	return ((ret == 0xffffffff)? 0 : ret);
4050}
4051
4052static kobj_method_t hdac_audio_ctl_ossmixer_methods[] = {
4053	KOBJMETHOD(mixer_init,		hdac_audio_ctl_ossmixer_init),
4054	KOBJMETHOD(mixer_set,		hdac_audio_ctl_ossmixer_set),
4055	KOBJMETHOD(mixer_setrecsrc,	hdac_audio_ctl_ossmixer_setrecsrc),
4056	KOBJMETHOD_END
4057};
4058MIXER_DECLARE(hdac_audio_ctl_ossmixer);
4059
4060static void
4061hdac_unsolq_task(void *context, int pending)
4062{
4063	struct hdac_softc *sc;
4064
4065	sc = (struct hdac_softc *)context;
4066
4067	hdac_lock(sc);
4068	hdac_unsolq_flush(sc);
4069	hdac_unlock(sc);
4070}
4071
4072/****************************************************************************
4073 * int hdac_attach(device_t)
4074 *
4075 * Attach the device into the kernel. Interrupts usually won't be enabled
4076 * when this function is called. Setup everything that doesn't require
4077 * interrupts and defer probing of codecs until interrupts are enabled.
4078 ****************************************************************************/
4079static int
4080hdac_attach(device_t dev)
4081{
4082	struct hdac_softc *sc;
4083	int result;
4084	int i, devid = -1;
4085	uint32_t model;
4086	uint16_t class, subclass;
4087	uint16_t vendor;
4088	uint8_t v;
4089
4090	device_printf(dev, "HDA Driver Revision: %s\n", HDA_DRV_TEST_REV);
4091
4092	model = (uint32_t)pci_get_device(dev) << 16;
4093	model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
4094	class = pci_get_class(dev);
4095	subclass = pci_get_subclass(dev);
4096
4097	for (i = 0; i < HDAC_DEVICES_LEN; i++) {
4098		if (hdac_devices[i].model == model) {
4099			devid = i;
4100			break;
4101		}
4102		if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
4103		    class == PCIC_MULTIMEDIA &&
4104		    subclass == PCIS_MULTIMEDIA_HDA) {
4105			devid = i;
4106			break;
4107		}
4108	}
4109
4110	sc = device_get_softc(dev);
4111	sc->lock = snd_mtxcreate(device_get_nameunit(dev), HDAC_MTX_NAME);
4112	sc->dev = dev;
4113	sc->pci_subvendor = (uint32_t)pci_get_subdevice(sc->dev) << 16;
4114	sc->pci_subvendor |= (uint32_t)pci_get_subvendor(sc->dev) & 0x0000ffff;
4115	vendor = pci_get_vendor(dev);
4116
4117	if (sc->pci_subvendor == HP_NX6325_SUBVENDORX) {
4118		/* Screw nx6325 - subdevice/subvendor swapped */
4119		sc->pci_subvendor = HP_NX6325_SUBVENDOR;
4120	}
4121
4122	callout_init(&sc->poll_hda, CALLOUT_MPSAFE);
4123	callout_init(&sc->poll_hdac, CALLOUT_MPSAFE);
4124	callout_init(&sc->poll_jack, CALLOUT_MPSAFE);
4125
4126	TASK_INIT(&sc->unsolq_task, 0, hdac_unsolq_task, sc);
4127
4128	sc->poll_ticks = 1000000;
4129	sc->poll_ival = HDAC_POLL_INTERVAL;
4130	if (resource_int_value(device_get_name(dev),
4131	    device_get_unit(dev), "polling", &i) == 0 && i != 0)
4132		sc->polling = 1;
4133	else
4134		sc->polling = 0;
4135
4136	sc->hdabus = NULL;
4137	for (i = 0; i < HDAC_CODEC_MAX; i++)
4138		sc->codecs[i] = NULL;
4139
4140	pci_enable_busmaster(dev);
4141
4142	if (vendor == INTEL_VENDORID) {
4143		/* TCSEL -> TC0 */
4144		v = pci_read_config(dev, 0x44, 1);
4145		pci_write_config(dev, 0x44, v & 0xf8, 1);
4146		HDA_BOOTHVERBOSE(
4147			device_printf(dev, "TCSEL: 0x%02d -> 0x%02d\n", v,
4148			    pci_read_config(dev, 0x44, 1));
4149		);
4150	}
4151
4152	if (devid >= 0 && (hdac_devices[devid].flags & HDAC_NO_MSI))
4153		sc->flags &= ~HDAC_F_MSI;
4154	else
4155		sc->flags |= HDAC_F_MSI;
4156	if (resource_int_value(device_get_name(dev),
4157	    device_get_unit(dev), "msi", &i) == 0) {
4158		if (i == 0)
4159			sc->flags &= ~HDAC_F_MSI;
4160		else
4161			sc->flags |= HDAC_F_MSI;
4162	}
4163
4164#if defined(__i386__) || defined(__amd64__)
4165	sc->flags |= HDAC_F_DMA_NOCACHE;
4166
4167	if (resource_int_value(device_get_name(dev),
4168	    device_get_unit(dev), "snoop", &i) == 0 && i != 0) {
4169#else
4170	sc->flags &= ~HDAC_F_DMA_NOCACHE;
4171#endif
4172		/*
4173		 * Try to enable PCIe snoop to avoid messing around with
4174		 * uncacheable DMA attribute. Since PCIe snoop register
4175		 * config is pretty much vendor specific, there are no
4176		 * general solutions on how to enable it, forcing us (even
4177		 * Microsoft) to enable uncacheable or write combined DMA
4178		 * by default.
4179		 *
4180		 * http://msdn2.microsoft.com/en-us/library/ms790324.aspx
4181		 */
4182		for (i = 0; i < HDAC_PCIESNOOP_LEN; i++) {
4183			if (hdac_pcie_snoop[i].vendor != vendor)
4184				continue;
4185			sc->flags &= ~HDAC_F_DMA_NOCACHE;
4186			if (hdac_pcie_snoop[i].reg == 0x00)
4187				break;
4188			v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
4189			if ((v & hdac_pcie_snoop[i].enable) ==
4190			    hdac_pcie_snoop[i].enable)
4191				break;
4192			v &= hdac_pcie_snoop[i].mask;
4193			v |= hdac_pcie_snoop[i].enable;
4194			pci_write_config(dev, hdac_pcie_snoop[i].reg, v, 1);
4195			v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
4196			if ((v & hdac_pcie_snoop[i].enable) !=
4197			    hdac_pcie_snoop[i].enable) {
4198				HDA_BOOTVERBOSE(
4199					device_printf(dev,
4200					    "WARNING: Failed to enable PCIe "
4201					    "snoop!\n");
4202				);
4203#if defined(__i386__) || defined(__amd64__)
4204				sc->flags |= HDAC_F_DMA_NOCACHE;
4205#endif
4206			}
4207			break;
4208		}
4209#if defined(__i386__) || defined(__amd64__)
4210	}
4211#endif
4212
4213	HDA_BOOTHVERBOSE(
4214		device_printf(dev, "DMA Coherency: %s / vendor=0x%04x\n",
4215		    (sc->flags & HDAC_F_DMA_NOCACHE) ?
4216		    "Uncacheable" : "PCIe snoop", vendor);
4217	);
4218
4219	/* Allocate resources */
4220	result = hdac_mem_alloc(sc);
4221	if (result != 0)
4222		goto hdac_attach_fail;
4223	result = hdac_irq_alloc(sc);
4224	if (result != 0)
4225		goto hdac_attach_fail;
4226
4227	/* Get Capabilities */
4228	result = hdac_get_capabilities(sc);
4229	if (result != 0)
4230		goto hdac_attach_fail;
4231
4232	if (devid >= 0 && (hdac_devices[devid].flags & HDAC_NO_64BIT))
4233		sc->support_64bit = 0;
4234
4235	/* Allocate CORB and RIRB dma memory */
4236	result = hdac_dma_alloc(sc, &sc->corb_dma,
4237	    sc->corb_size * sizeof(uint32_t));
4238	if (result != 0)
4239		goto hdac_attach_fail;
4240	result = hdac_dma_alloc(sc, &sc->rirb_dma,
4241	    sc->rirb_size * sizeof(struct hdac_rirb));
4242	if (result != 0)
4243		goto hdac_attach_fail;
4244
4245	result = bus_dma_tag_create(
4246	    bus_get_dma_tag(sc->dev),		/* parent */
4247	    HDAC_DMA_ALIGNMENT,			/* alignment */
4248	    0,					/* boundary */
4249	    (sc->support_64bit) ? BUS_SPACE_MAXADDR :
4250		BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
4251	    BUS_SPACE_MAXADDR,			/* highaddr */
4252	    NULL,				/* filtfunc */
4253	    NULL,				/* fistfuncarg */
4254	    HDA_BUFSZ_MAX, 			/* maxsize */
4255	    1,					/* nsegments */
4256	    HDA_BUFSZ_MAX, 			/* maxsegsz */
4257	    0,					/* flags */
4258	    NULL,				/* lockfunc */
4259	    NULL,				/* lockfuncarg */
4260	    &sc->chan_dmat);			/* dmat */
4261	if (result != 0) {
4262		device_printf(dev, "%s: bus_dma_tag_create failed (%x)\n",
4263		     __func__, result);
4264		goto hdac_attach_fail;
4265	}
4266
4267	/* Quiesce everything */
4268	HDA_BOOTHVERBOSE(
4269		device_printf(dev, "Reset controller...\n");
4270	);
4271	hdac_reset(sc, 1);
4272
4273	/* Initialize the CORB and RIRB */
4274	hdac_corb_init(sc);
4275	hdac_rirb_init(sc);
4276
4277	/* Defer remaining of initialization until interrupts are enabled */
4278	sc->intrhook.ich_func = hdac_attach2;
4279	sc->intrhook.ich_arg = (void *)sc;
4280	if (cold == 0 || config_intrhook_establish(&sc->intrhook) != 0) {
4281		sc->intrhook.ich_func = NULL;
4282		hdac_attach2((void *)sc);
4283	}
4284
4285	return (0);
4286
4287hdac_attach_fail:
4288	hdac_irq_free(sc);
4289	hdac_dma_free(sc, &sc->rirb_dma);
4290	hdac_dma_free(sc, &sc->corb_dma);
4291	hdac_mem_free(sc);
4292	snd_mtxfree(sc->lock);
4293	free(sc, M_DEVBUF);
4294
4295	return (ENXIO);
4296}
4297
4298static void
4299hdac_audio_parse(struct hdac_devinfo *devinfo)
4300{
4301	struct hdac_codec *codec = devinfo->codec;
4302	struct hdac_softc *sc = codec->sc;
4303	struct hdac_widget *w;
4304	uint32_t res;
4305	int i;
4306	nid_t cad, nid;
4307
4308	cad = devinfo->codec->cad;
4309	nid = devinfo->nid;
4310
4311	res = hdac_command(sc,
4312	    HDA_CMD_GET_PARAMETER(cad , nid, HDA_PARAM_GPIO_COUNT), cad);
4313	devinfo->function.audio.gpio = res;
4314
4315	HDA_BOOTVERBOSE(
4316		device_printf(sc->dev, "GPIO: 0x%08x "
4317		    "NumGPIO=%d NumGPO=%d "
4318		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
4319		    devinfo->function.audio.gpio,
4320		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->function.audio.gpio),
4321		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->function.audio.gpio),
4322		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->function.audio.gpio),
4323		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->function.audio.gpio),
4324		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->function.audio.gpio));
4325	);
4326
4327	res = hdac_command(sc,
4328	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_SUPP_STREAM_FORMATS),
4329	    cad);
4330	devinfo->function.audio.supp_stream_formats = res;
4331
4332	res = hdac_command(sc,
4333	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE),
4334	    cad);
4335	devinfo->function.audio.supp_pcm_size_rate = res;
4336
4337	res = hdac_command(sc,
4338	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_OUTPUT_AMP_CAP),
4339	    cad);
4340	devinfo->function.audio.outamp_cap = res;
4341
4342	res = hdac_command(sc,
4343	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_INPUT_AMP_CAP),
4344	    cad);
4345	devinfo->function.audio.inamp_cap = res;
4346
4347	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4348		w = hdac_widget_get(devinfo, i);
4349		if (w == NULL)
4350			device_printf(sc->dev, "Ghost widget! nid=%d!\n", i);
4351		else {
4352			w->devinfo = devinfo;
4353			w->nid = i;
4354			w->enable = 1;
4355			w->selconn = -1;
4356			w->pflags = 0;
4357			w->ossdev = -1;
4358			w->bindas = -1;
4359			w->param.eapdbtl = HDAC_INVALID;
4360			hdac_widget_parse(w);
4361		}
4362	}
4363}
4364
4365static void
4366hdac_audio_ctl_parse(struct hdac_devinfo *devinfo)
4367{
4368	struct hdac_softc *sc = devinfo->codec->sc;
4369	struct hdac_audio_ctl *ctls;
4370	struct hdac_widget *w, *cw;
4371	int i, j, cnt, max, ocap, icap;
4372	int mute, offset, step, size;
4373
4374	/* XXX This is redundant */
4375	max = 0;
4376	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4377		w = hdac_widget_get(devinfo, i);
4378		if (w == NULL || w->enable == 0)
4379			continue;
4380		if (w->param.outamp_cap != 0)
4381			max++;
4382		if (w->param.inamp_cap != 0) {
4383			switch (w->type) {
4384			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
4385			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
4386				for (j = 0; j < w->nconns; j++) {
4387					cw = hdac_widget_get(devinfo,
4388					    w->conns[j]);
4389					if (cw == NULL || cw->enable == 0)
4390						continue;
4391					max++;
4392				}
4393				break;
4394			default:
4395				max++;
4396				break;
4397			}
4398		}
4399	}
4400
4401	devinfo->function.audio.ctlcnt = max;
4402
4403	if (max < 1)
4404		return;
4405
4406	ctls = (struct hdac_audio_ctl *)malloc(
4407	    sizeof(*ctls) * max, M_HDAC, M_ZERO | M_NOWAIT);
4408
4409	if (ctls == NULL) {
4410		/* Blekh! */
4411		device_printf(sc->dev, "unable to allocate ctls!\n");
4412		devinfo->function.audio.ctlcnt = 0;
4413		return;
4414	}
4415
4416	cnt = 0;
4417	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
4418		if (cnt >= max) {
4419			device_printf(sc->dev, "%s: Ctl overflow!\n",
4420			    __func__);
4421			break;
4422		}
4423		w = hdac_widget_get(devinfo, i);
4424		if (w == NULL || w->enable == 0)
4425			continue;
4426		ocap = w->param.outamp_cap;
4427		icap = w->param.inamp_cap;
4428		if (ocap != 0) {
4429			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
4430			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
4431			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
4432			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
4433			/*if (offset > step) {
4434				HDA_BOOTVERBOSE(
4435					device_printf(sc->dev,
4436					    "BUGGY outamp: nid=%d "
4437					    "[offset=%d > step=%d]\n",
4438					    w->nid, offset, step);
4439				);
4440				offset = step;
4441			}*/
4442			ctls[cnt].enable = 1;
4443			ctls[cnt].widget = w;
4444			ctls[cnt].mute = mute;
4445			ctls[cnt].step = step;
4446			ctls[cnt].size = size;
4447			ctls[cnt].offset = offset;
4448			ctls[cnt].left = offset;
4449			ctls[cnt].right = offset;
4450			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
4451			    w->waspin)
4452				ctls[cnt].ndir = HDA_CTL_IN;
4453			else
4454				ctls[cnt].ndir = HDA_CTL_OUT;
4455			ctls[cnt++].dir = HDA_CTL_OUT;
4456		}
4457
4458		if (icap != 0) {
4459			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
4460			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
4461			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
4462			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
4463			/*if (offset > step) {
4464				HDA_BOOTVERBOSE(
4465					device_printf(sc->dev,
4466					    "BUGGY inamp: nid=%d "
4467					    "[offset=%d > step=%d]\n",
4468					    w->nid, offset, step);
4469				);
4470				offset = step;
4471			}*/
4472			switch (w->type) {
4473			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
4474			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
4475				for (j = 0; j < w->nconns; j++) {
4476					if (cnt >= max) {
4477						device_printf(sc->dev,
4478						    "%s: Ctl overflow!\n",
4479						    __func__);
4480						break;
4481					}
4482					cw = hdac_widget_get(devinfo,
4483					    w->conns[j]);
4484					if (cw == NULL || cw->enable == 0)
4485						continue;
4486					ctls[cnt].enable = 1;
4487					ctls[cnt].widget = w;
4488					ctls[cnt].childwidget = cw;
4489					ctls[cnt].index = j;
4490					ctls[cnt].mute = mute;
4491					ctls[cnt].step = step;
4492					ctls[cnt].size = size;
4493					ctls[cnt].offset = offset;
4494					ctls[cnt].left = offset;
4495					ctls[cnt].right = offset;
4496	    				ctls[cnt].ndir = HDA_CTL_IN;
4497					ctls[cnt++].dir = HDA_CTL_IN;
4498				}
4499				break;
4500			default:
4501				if (cnt >= max) {
4502					device_printf(sc->dev,
4503					    "%s: Ctl overflow!\n",
4504					    __func__);
4505					break;
4506				}
4507				ctls[cnt].enable = 1;
4508				ctls[cnt].widget = w;
4509				ctls[cnt].mute = mute;
4510				ctls[cnt].step = step;
4511				ctls[cnt].size = size;
4512				ctls[cnt].offset = offset;
4513				ctls[cnt].left = offset;
4514				ctls[cnt].right = offset;
4515				if (w->type ==
4516				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4517					ctls[cnt].ndir = HDA_CTL_OUT;
4518				else
4519					ctls[cnt].ndir = HDA_CTL_IN;
4520				ctls[cnt++].dir = HDA_CTL_IN;
4521				break;
4522			}
4523		}
4524	}
4525
4526	devinfo->function.audio.ctl = ctls;
4527}
4528
4529static void
4530hdac_audio_as_parse(struct hdac_devinfo *devinfo)
4531{
4532	struct hdac_softc *sc = devinfo->codec->sc;
4533	struct hdac_audio_as *as;
4534	struct hdac_widget *w;
4535	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
4536
4537	/* Count present associations */
4538	max = 0;
4539	for (j = 1; j < 16; j++) {
4540		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4541			w = hdac_widget_get(devinfo, i);
4542			if (w == NULL || w->enable == 0)
4543				continue;
4544			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4545				continue;
4546			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
4547			    != j)
4548				continue;
4549			max++;
4550			if (j != 15)  /* There could be many 1-pin assocs #15 */
4551				break;
4552		}
4553	}
4554
4555	devinfo->function.audio.ascnt = max;
4556
4557	if (max < 1)
4558		return;
4559
4560	as = (struct hdac_audio_as *)malloc(
4561	    sizeof(*as) * max, M_HDAC, M_ZERO | M_NOWAIT);
4562
4563	if (as == NULL) {
4564		/* Blekh! */
4565		device_printf(sc->dev, "unable to allocate assocs!\n");
4566		devinfo->function.audio.ascnt = 0;
4567		return;
4568	}
4569
4570	for (i = 0; i < max; i++) {
4571		as[i].hpredir = -1;
4572		as[i].chan = -1;
4573		as[i].digital = 0;
4574	}
4575
4576	/* Scan associations skipping as=0. */
4577	cnt = 0;
4578	for (j = 1; j < 16; j++) {
4579		first = 16;
4580		hpredir = 0;
4581		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4582			w = hdac_widget_get(devinfo, i);
4583			if (w == NULL || w->enable == 0)
4584				continue;
4585			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4586				continue;
4587			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
4588			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
4589			if (assoc != j) {
4590				continue;
4591			}
4592			KASSERT(cnt < max,
4593			    ("%s: Associations owerflow (%d of %d)",
4594			    __func__, cnt, max));
4595			type = w->wclass.pin.config &
4596			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
4597			/* Get pin direction. */
4598			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
4599			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
4600			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
4601			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
4602			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
4603				dir = HDA_CTL_OUT;
4604			else
4605				dir = HDA_CTL_IN;
4606			/* If this is a first pin - create new association. */
4607			if (as[cnt].pincnt == 0) {
4608				as[cnt].enable = 1;
4609				as[cnt].index = j;
4610				as[cnt].dir = dir;
4611			}
4612			if (seq < first)
4613				first = seq;
4614			/* Check association correctness. */
4615			if (as[cnt].pins[seq] != 0) {
4616				device_printf(sc->dev, "%s: Duplicate pin %d (%d) "
4617				    "in association %d! Disabling association.\n",
4618				    __func__, seq, w->nid, j);
4619				as[cnt].enable = 0;
4620			}
4621			if (dir != as[cnt].dir) {
4622				device_printf(sc->dev, "%s: Pin %d has wrong "
4623				    "direction for association %d! Disabling "
4624				    "association.\n",
4625				    __func__, w->nid, j);
4626				as[cnt].enable = 0;
4627			}
4628			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
4629				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
4630					as[cnt].digital = 3;
4631				else if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
4632					as[cnt].digital = 2;
4633				else
4634					as[cnt].digital = 1;
4635			}
4636			/* Headphones with seq=15 may mean redirection. */
4637			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
4638			    seq == 15)
4639				hpredir = 1;
4640			as[cnt].pins[seq] = w->nid;
4641			as[cnt].pincnt++;
4642			/* Association 15 is a multiple unassociated pins. */
4643			if (j == 15)
4644				cnt++;
4645		}
4646		if (j != 15 && as[cnt].pincnt > 0) {
4647			if (hpredir && as[cnt].pincnt > 1)
4648				as[cnt].hpredir = first;
4649			cnt++;
4650		}
4651	}
4652	HDA_BOOTVERBOSE(
4653		device_printf(sc->dev,
4654		    "%d associations found:\n", max);
4655		for (i = 0; i < max; i++) {
4656			device_printf(sc->dev,
4657			    "Association %d (%d) %s%s:\n",
4658			    i, as[i].index, (as[i].dir == HDA_CTL_IN)?"in":"out",
4659			    as[i].enable?"":" (disabled)");
4660			for (j = 0; j < 16; j++) {
4661				if (as[i].pins[j] == 0)
4662					continue;
4663				device_printf(sc->dev,
4664				    " Pin nid=%d seq=%d\n",
4665				    as[i].pins[j], j);
4666			}
4667		}
4668	);
4669
4670	devinfo->function.audio.as = as;
4671}
4672
4673static const struct {
4674	uint32_t model;
4675	uint32_t id;
4676	uint32_t set, unset;
4677} hdac_quirks[] = {
4678	/*
4679	 * XXX Force stereo quirk. Monoural recording / playback
4680	 *     on few codecs (especially ALC880) seems broken or
4681	 *     perhaps unsupported.
4682	 */
4683	{ HDA_MATCH_ALL, HDA_MATCH_ALL,
4684	    HDA_QUIRK_FORCESTEREO | HDA_QUIRK_IVREF, 0 },
4685	{ ACER_ALL_SUBVENDOR, HDA_MATCH_ALL,
4686	    HDA_QUIRK_GPIO0, 0 },
4687	{ ASUS_G2K_SUBVENDOR, HDA_CODEC_ALC660,
4688	    HDA_QUIRK_GPIO0, 0 },
4689	{ ASUS_M5200_SUBVENDOR, HDA_CODEC_ALC880,
4690	    HDA_QUIRK_GPIO0, 0 },
4691	{ ASUS_A7M_SUBVENDOR, HDA_CODEC_ALC880,
4692	    HDA_QUIRK_GPIO0, 0 },
4693	{ ASUS_A7T_SUBVENDOR, HDA_CODEC_ALC882,
4694	    HDA_QUIRK_GPIO0, 0 },
4695	{ ASUS_W2J_SUBVENDOR, HDA_CODEC_ALC882,
4696	    HDA_QUIRK_GPIO0, 0 },
4697	{ ASUS_U5F_SUBVENDOR, HDA_CODEC_AD1986A,
4698	    HDA_QUIRK_EAPDINV, 0 },
4699	{ ASUS_A8X_SUBVENDOR, HDA_CODEC_AD1986A,
4700	    HDA_QUIRK_EAPDINV, 0 },
4701	{ ASUS_F3JC_SUBVENDOR, HDA_CODEC_ALC861,
4702	    HDA_QUIRK_OVREF, 0 },
4703	{ UNIWILL_9075_SUBVENDOR, HDA_CODEC_ALC861,
4704	    HDA_QUIRK_OVREF, 0 },
4705	/*{ ASUS_M2N_SUBVENDOR, HDA_CODEC_AD1988,
4706	    HDA_QUIRK_IVREF80, HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF100 },*/
4707	{ MEDION_MD95257_SUBVENDOR, HDA_CODEC_ALC880,
4708	    HDA_QUIRK_GPIO1, 0 },
4709	{ LENOVO_3KN100_SUBVENDOR, HDA_CODEC_AD1986A,
4710	    HDA_QUIRK_EAPDINV | HDA_QUIRK_SENSEINV, 0 },
4711	{ SAMSUNG_Q1_SUBVENDOR, HDA_CODEC_AD1986A,
4712	    HDA_QUIRK_EAPDINV, 0 },
4713	{ APPLE_MB3_SUBVENDOR, HDA_CODEC_ALC885,
4714	    HDA_QUIRK_GPIO0 | HDA_QUIRK_OVREF50, 0},
4715	{ APPLE_INTEL_MAC, HDA_CODEC_STAC9221,
4716	    HDA_QUIRK_GPIO0 | HDA_QUIRK_GPIO1, 0 },
4717	{ APPLE_MACBOOKPRO55, HDA_CODEC_CS4206,
4718	    HDA_QUIRK_GPIO1 | HDA_QUIRK_GPIO3, 0 },
4719	{ DELL_D630_SUBVENDOR, HDA_CODEC_STAC9205X,
4720	    HDA_QUIRK_GPIO0, 0 },
4721	{ DELL_V1400_SUBVENDOR, HDA_CODEC_STAC9228X,
4722	    HDA_QUIRK_GPIO2, 0 },
4723	{ DELL_V1500_SUBVENDOR, HDA_CODEC_STAC9205X,
4724	    HDA_QUIRK_GPIO0, 0 },
4725	{ HDA_MATCH_ALL, HDA_CODEC_AD1988,
4726	    HDA_QUIRK_IVREF80, HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF100 },
4727	{ HDA_MATCH_ALL, HDA_CODEC_AD1988B,
4728	    HDA_QUIRK_IVREF80, HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF100 },
4729	{ HDA_MATCH_ALL, HDA_CODEC_CX20549,
4730	    0, HDA_QUIRK_FORCESTEREO }
4731};
4732#define HDAC_QUIRKS_LEN (sizeof(hdac_quirks) / sizeof(hdac_quirks[0]))
4733
4734static void
4735hdac_vendor_patch_parse(struct hdac_devinfo *devinfo)
4736{
4737	struct hdac_widget *w;
4738	uint32_t id, subvendor;
4739	int i;
4740
4741	id = hdac_codec_id(devinfo->codec);
4742	subvendor = devinfo->codec->sc->pci_subvendor;
4743
4744	/*
4745	 * Quirks
4746	 */
4747	for (i = 0; i < HDAC_QUIRKS_LEN; i++) {
4748		if (!(HDA_DEV_MATCH(hdac_quirks[i].model, subvendor) &&
4749		    HDA_DEV_MATCH(hdac_quirks[i].id, id)))
4750			continue;
4751		if (hdac_quirks[i].set != 0)
4752			devinfo->function.audio.quirks |=
4753			    hdac_quirks[i].set;
4754		if (hdac_quirks[i].unset != 0)
4755			devinfo->function.audio.quirks &=
4756			    ~(hdac_quirks[i].unset);
4757	}
4758
4759	switch (id) {
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 direct playback, use mixer. */
4873		w = hdac_widget_get(devinfo, 5);
4874		if (w != NULL)
4875			w->connsenable[0] = 0;
4876		w = hdac_widget_get(devinfo, 6);
4877		if (w != NULL)
4878			w->connsenable[0] = 0;
4879		w = hdac_widget_get(devinfo, 9);
4880		if (w != NULL)
4881			w->connsenable[0] = 0;
4882		w = hdac_widget_get(devinfo, 24);
4883		if (w != NULL)
4884			w->connsenable[0] = 0;
4885		break;
4886	}
4887}
4888
4889/*
4890 * Trace path from DAC to pin.
4891 */
4892static nid_t
4893hdac_audio_trace_dac(struct hdac_devinfo *devinfo, int as, int seq, nid_t nid,
4894    int dupseq, int min, int only, int depth)
4895{
4896	struct hdac_widget *w;
4897	int i, im = -1;
4898	nid_t m = 0, ret;
4899
4900	if (depth > HDA_PARSE_MAXDEPTH)
4901		return (0);
4902	w = hdac_widget_get(devinfo, nid);
4903	if (w == NULL || w->enable == 0)
4904		return (0);
4905	HDA_BOOTHVERBOSE(
4906		if (!only) {
4907			device_printf(devinfo->codec->sc->dev,
4908			    " %*stracing via nid %d\n",
4909				depth + 1, "", w->nid);
4910		}
4911	);
4912	/* Use only unused widgets */
4913	if (w->bindas >= 0 && w->bindas != as) {
4914		HDA_BOOTHVERBOSE(
4915			if (!only) {
4916				device_printf(devinfo->codec->sc->dev,
4917				    " %*snid %d busy by association %d\n",
4918					depth + 1, "", w->nid, w->bindas);
4919			}
4920		);
4921		return (0);
4922	}
4923	if (dupseq < 0) {
4924		if (w->bindseqmask != 0) {
4925			HDA_BOOTHVERBOSE(
4926				if (!only) {
4927					device_printf(devinfo->codec->sc->dev,
4928					    " %*snid %d busy by seqmask %x\n",
4929						depth + 1, "", w->nid, w->bindseqmask);
4930				}
4931			);
4932			return (0);
4933		}
4934	} else {
4935		/* If this is headphones - allow duplicate first pin. */
4936		if (w->bindseqmask != 0 &&
4937		    (w->bindseqmask & (1 << dupseq)) == 0) {
4938			HDA_BOOTHVERBOSE(
4939				device_printf(devinfo->codec->sc->dev,
4940				    " %*snid %d busy by seqmask %x\n",
4941					depth + 1, "", w->nid, w->bindseqmask);
4942			);
4943			return (0);
4944		}
4945	}
4946
4947	switch (w->type) {
4948	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
4949		/* Do not traverse input. AD1988 has digital monitor
4950		for which we are not ready. */
4951		break;
4952	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4953		/* If we are tracing HP take only dac of first pin. */
4954		if ((only == 0 || only == w->nid) &&
4955		    (w->nid >= min) && (dupseq < 0 || w->nid ==
4956		    devinfo->function.audio.as[as].dacs[dupseq]))
4957			m = w->nid;
4958		break;
4959	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4960		if (depth > 0)
4961			break;
4962		/* Fall */
4963	default:
4964		/* Find reachable DACs with smallest nid respecting constraints. */
4965		for (i = 0; i < w->nconns; i++) {
4966			if (w->connsenable[i] == 0)
4967				continue;
4968			if (w->selconn != -1 && w->selconn != i)
4969				continue;
4970			if ((ret = hdac_audio_trace_dac(devinfo, as, seq,
4971			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
4972				if (m == 0 || ret < m) {
4973					m = ret;
4974					im = i;
4975				}
4976				if (only || dupseq >= 0)
4977					break;
4978			}
4979		}
4980		if (m && only && ((w->nconns > 1 &&
4981		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
4982		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
4983			w->selconn = im;
4984		break;
4985	}
4986	if (m && only) {
4987		w->bindas = as;
4988		w->bindseqmask |= (1 << seq);
4989	}
4990	HDA_BOOTHVERBOSE(
4991		if (!only) {
4992			device_printf(devinfo->codec->sc->dev,
4993			    " %*snid %d returned %d\n",
4994				depth + 1, "", w->nid, m);
4995		}
4996	);
4997	return (m);
4998}
4999
5000/*
5001 * Trace path from widget to ADC.
5002 */
5003static nid_t
5004hdac_audio_trace_adc(struct hdac_devinfo *devinfo, int as, int seq, nid_t nid,
5005    int only, int depth)
5006{
5007	struct hdac_widget *w, *wc;
5008	int i, j;
5009	nid_t res = 0;
5010
5011	if (depth > HDA_PARSE_MAXDEPTH)
5012		return (0);
5013	w = hdac_widget_get(devinfo, nid);
5014	if (w == NULL || w->enable == 0)
5015		return (0);
5016	HDA_BOOTHVERBOSE(
5017		device_printf(devinfo->codec->sc->dev,
5018		    " %*stracing via nid %d\n",
5019			depth + 1, "", w->nid);
5020	);
5021	/* Use only unused widgets */
5022	if (w->bindas >= 0 && w->bindas != as) {
5023		HDA_BOOTHVERBOSE(
5024			device_printf(devinfo->codec->sc->dev,
5025			    " %*snid %d busy by association %d\n",
5026				depth + 1, "", w->nid, w->bindas);
5027		);
5028		return (0);
5029	}
5030
5031	switch (w->type) {
5032	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
5033		/* If we are tracing HP take only dac of first pin. */
5034		if (only == w->nid)
5035			res = 1;
5036		break;
5037	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
5038		if (depth > 0)
5039			break;
5040		/* Fall */
5041	default:
5042		/* Try to find reachable ADCs with specified nid. */
5043		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5044			wc = hdac_widget_get(devinfo, j);
5045			if (wc == NULL || wc->enable == 0)
5046				continue;
5047			for (i = 0; i < wc->nconns; i++) {
5048				if (wc->connsenable[i] == 0)
5049					continue;
5050				if (wc->conns[i] != nid)
5051					continue;
5052				if (hdac_audio_trace_adc(devinfo, as, seq,
5053				    j, only, depth + 1) != 0) {
5054					res = 1;
5055					if (((wc->nconns > 1 &&
5056					    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
5057					    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR) &&
5058					    wc->selconn == -1)
5059						wc->selconn = i;
5060				}
5061			}
5062		}
5063		break;
5064	}
5065	if (res) {
5066		w->bindas = as;
5067		w->bindseqmask |= (1 << seq);
5068	}
5069	HDA_BOOTHVERBOSE(
5070		device_printf(devinfo->codec->sc->dev,
5071		    " %*snid %d returned %d\n",
5072			depth + 1, "", w->nid, res);
5073	);
5074	return (res);
5075}
5076
5077/*
5078 * Erase trace path of the specified association.
5079 */
5080static void
5081hdac_audio_undo_trace(struct hdac_devinfo *devinfo, int as, int seq)
5082{
5083	struct hdac_widget *w;
5084	int i;
5085
5086	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5087		w = hdac_widget_get(devinfo, i);
5088		if (w == NULL || w->enable == 0)
5089			continue;
5090		if (w->bindas == as) {
5091			if (seq >= 0) {
5092				w->bindseqmask &= ~(1 << seq);
5093				if (w->bindseqmask == 0) {
5094					w->bindas = -1;
5095					w->selconn = -1;
5096				}
5097			} else {
5098				w->bindas = -1;
5099				w->bindseqmask = 0;
5100				w->selconn = -1;
5101			}
5102		}
5103	}
5104}
5105
5106/*
5107 * Trace association path from DAC to output
5108 */
5109static int
5110hdac_audio_trace_as_out(struct hdac_devinfo *devinfo, int as, int seq)
5111{
5112	struct hdac_audio_as *ases = devinfo->function.audio.as;
5113	int i, hpredir;
5114	nid_t min, res;
5115
5116	/* Find next pin */
5117	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
5118		;
5119	/* Check if there is no any left. If so - we succeeded. */
5120	if (i == 16)
5121		return (1);
5122
5123	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
5124	min = 0;
5125	res = 0;
5126	do {
5127		HDA_BOOTHVERBOSE(
5128			device_printf(devinfo->codec->sc->dev,
5129			    " Tracing pin %d with min nid %d",
5130			    ases[as].pins[i], min);
5131			if (hpredir >= 0)
5132				printf(" and hpredir %d", hpredir);
5133			printf("\n");
5134		);
5135		/* Trace this pin taking min nid into account. */
5136		res = hdac_audio_trace_dac(devinfo, as, i,
5137		    ases[as].pins[i], hpredir, min, 0, 0);
5138		if (res == 0) {
5139			/* If we failed - return to previous and redo it. */
5140			HDA_BOOTVERBOSE(
5141				device_printf(devinfo->codec->sc->dev,
5142				    " Unable to trace pin %d seq %d with min "
5143				    "nid %d",
5144				    ases[as].pins[i], i, min);
5145				if (hpredir >= 0)
5146					printf(" and hpredir %d", hpredir);
5147				printf("\n");
5148			);
5149			return (0);
5150		}
5151		HDA_BOOTVERBOSE(
5152			device_printf(devinfo->codec->sc->dev,
5153			    " Pin %d traced to DAC %d",
5154			    ases[as].pins[i], res);
5155			if (hpredir >= 0)
5156				printf(" and hpredir %d", hpredir);
5157			if (ases[as].fakeredir)
5158				printf(" with fake redirection");
5159			printf("\n");
5160		);
5161		/* Trace again to mark the path */
5162		hdac_audio_trace_dac(devinfo, as, i,
5163		    ases[as].pins[i], hpredir, min, res, 0);
5164		ases[as].dacs[i] = res;
5165		/* We succeeded, so call next. */
5166		if (hdac_audio_trace_as_out(devinfo, as, i + 1))
5167			return (1);
5168		/* If next failed, we should retry with next min */
5169		hdac_audio_undo_trace(devinfo, as, i);
5170		ases[as].dacs[i] = 0;
5171		min = res + 1;
5172	} while (1);
5173}
5174
5175/*
5176 * Trace association path from input to ADC
5177 */
5178static int
5179hdac_audio_trace_as_in(struct hdac_devinfo *devinfo, int as)
5180{
5181	struct hdac_audio_as *ases = devinfo->function.audio.as;
5182	struct hdac_widget *w;
5183	int i, j, k;
5184
5185	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5186		w = hdac_widget_get(devinfo, j);
5187		if (w == NULL || w->enable == 0)
5188			continue;
5189		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
5190			continue;
5191		if (w->bindas >= 0 && w->bindas != as)
5192			continue;
5193
5194		/* Find next pin */
5195		for (i = 0; i < 16; i++) {
5196			if (ases[as].pins[i] == 0)
5197				continue;
5198
5199			HDA_BOOTHVERBOSE(
5200				device_printf(devinfo->codec->sc->dev,
5201				    " Tracing pin %d to ADC %d\n",
5202				    ases[as].pins[i], j);
5203			);
5204			/* Trace this pin taking goal into account. */
5205			if (hdac_audio_trace_adc(devinfo, as, i,
5206			    ases[as].pins[i], j, 0) == 0) {
5207				/* If we failed - return to previous and redo it. */
5208				HDA_BOOTVERBOSE(
5209					device_printf(devinfo->codec->sc->dev,
5210					    " Unable to trace pin %d to ADC %d, undo traces\n",
5211					    ases[as].pins[i], j);
5212				);
5213				hdac_audio_undo_trace(devinfo, as, -1);
5214				for (k = 0; k < 16; k++)
5215					ases[as].dacs[k] = 0;
5216				break;
5217			}
5218			HDA_BOOTVERBOSE(
5219				device_printf(devinfo->codec->sc->dev,
5220				    " Pin %d traced to ADC %d\n",
5221				    ases[as].pins[i], j);
5222			);
5223			ases[as].dacs[i] = j;
5224		}
5225		if (i == 16)
5226			return (1);
5227	}
5228	return (0);
5229}
5230
5231/*
5232 * Trace input monitor path from mixer to output association.
5233 */
5234static int
5235hdac_audio_trace_to_out(struct hdac_devinfo *devinfo, nid_t nid, int depth)
5236{
5237	struct hdac_audio_as *ases = devinfo->function.audio.as;
5238	struct hdac_widget *w, *wc;
5239	int i, j;
5240	nid_t res = 0;
5241
5242	if (depth > HDA_PARSE_MAXDEPTH)
5243		return (0);
5244	w = hdac_widget_get(devinfo, nid);
5245	if (w == NULL || w->enable == 0)
5246		return (0);
5247	HDA_BOOTHVERBOSE(
5248		device_printf(devinfo->codec->sc->dev,
5249		    " %*stracing via nid %d\n",
5250			depth + 1, "", w->nid);
5251	);
5252	/* Use only unused widgets */
5253	if (depth > 0 && w->bindas != -1) {
5254		if (w->bindas < 0 || ases[w->bindas].dir == HDA_CTL_OUT) {
5255			HDA_BOOTHVERBOSE(
5256				device_printf(devinfo->codec->sc->dev,
5257				    " %*snid %d found output association %d\n",
5258					depth + 1, "", w->nid, w->bindas);
5259			);
5260			if (w->bindas >= 0)
5261				w->pflags |= HDA_ADC_MONITOR;
5262			return (1);
5263		} else {
5264			HDA_BOOTHVERBOSE(
5265				device_printf(devinfo->codec->sc->dev,
5266				    " %*snid %d busy by input association %d\n",
5267					depth + 1, "", w->nid, w->bindas);
5268			);
5269			return (0);
5270		}
5271	}
5272
5273	switch (w->type) {
5274	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
5275		/* Do not traverse input. AD1988 has digital monitor
5276		for which we are not ready. */
5277		break;
5278	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
5279		if (depth > 0)
5280			break;
5281		/* Fall */
5282	default:
5283		/* Try to find reachable ADCs with specified nid. */
5284		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5285			wc = hdac_widget_get(devinfo, j);
5286			if (wc == NULL || wc->enable == 0)
5287				continue;
5288			for (i = 0; i < wc->nconns; i++) {
5289				if (wc->connsenable[i] == 0)
5290					continue;
5291				if (wc->conns[i] != nid)
5292					continue;
5293				if (hdac_audio_trace_to_out(devinfo,
5294				    j, depth + 1) != 0) {
5295					res = 1;
5296					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
5297					    wc->selconn == -1)
5298						wc->selconn = i;
5299				}
5300			}
5301		}
5302		break;
5303	}
5304	if (res && w->bindas == -1)
5305		w->bindas = -2;
5306
5307	HDA_BOOTHVERBOSE(
5308		device_printf(devinfo->codec->sc->dev,
5309		    " %*snid %d returned %d\n",
5310			depth + 1, "", w->nid, res);
5311	);
5312	return (res);
5313}
5314
5315/*
5316 * Trace extra associations (beeper, monitor)
5317 */
5318static void
5319hdac_audio_trace_as_extra(struct hdac_devinfo *devinfo)
5320{
5321	struct hdac_audio_as *as = devinfo->function.audio.as;
5322	struct hdac_widget *w;
5323	int j;
5324
5325	/* Input monitor */
5326	/* Find mixer associated with input, but supplying signal
5327	   for output associations. Hope it will be input monitor. */
5328	HDA_BOOTVERBOSE(
5329		device_printf(devinfo->codec->sc->dev,
5330		    "Tracing input monitor\n");
5331	);
5332	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5333		w = hdac_widget_get(devinfo, j);
5334		if (w == NULL || w->enable == 0)
5335			continue;
5336		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5337			continue;
5338		if (w->bindas < 0 || as[w->bindas].dir != HDA_CTL_IN)
5339			continue;
5340		HDA_BOOTVERBOSE(
5341			device_printf(devinfo->codec->sc->dev,
5342			    " Tracing nid %d to out\n",
5343			    j);
5344		);
5345		if (hdac_audio_trace_to_out(devinfo, w->nid, 0)) {
5346			HDA_BOOTVERBOSE(
5347				device_printf(devinfo->codec->sc->dev,
5348				    " nid %d is input monitor\n",
5349					w->nid);
5350			);
5351			w->ossdev = SOUND_MIXER_IMIX;
5352		}
5353	}
5354
5355	/* Other inputs monitor */
5356	/* Find input pins supplying signal for output associations.
5357	   Hope it will be input monitoring. */
5358	HDA_BOOTVERBOSE(
5359		device_printf(devinfo->codec->sc->dev,
5360		    "Tracing other input monitors\n");
5361	);
5362	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5363		w = hdac_widget_get(devinfo, j);
5364		if (w == NULL || w->enable == 0)
5365			continue;
5366		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5367			continue;
5368		if (w->bindas < 0 || as[w->bindas].dir != HDA_CTL_IN)
5369			continue;
5370		HDA_BOOTVERBOSE(
5371			device_printf(devinfo->codec->sc->dev,
5372			    " Tracing nid %d to out\n",
5373			    j);
5374		);
5375		if (hdac_audio_trace_to_out(devinfo, w->nid, 0)) {
5376			HDA_BOOTVERBOSE(
5377				device_printf(devinfo->codec->sc->dev,
5378				    " nid %d is input monitor\n",
5379					w->nid);
5380			);
5381		}
5382	}
5383
5384	/* Beeper */
5385	HDA_BOOTVERBOSE(
5386		device_printf(devinfo->codec->sc->dev,
5387		    "Tracing beeper\n");
5388	);
5389	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
5390		w = hdac_widget_get(devinfo, j);
5391		if (w == NULL || w->enable == 0)
5392			continue;
5393		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
5394			continue;
5395		HDA_BOOTHVERBOSE(
5396			device_printf(devinfo->codec->sc->dev,
5397			    " Tracing nid %d to out\n",
5398			    j);
5399		);
5400		if (hdac_audio_trace_to_out(devinfo, w->nid, 0)) {
5401			HDA_BOOTVERBOSE(
5402				device_printf(devinfo->codec->sc->dev,
5403				    " nid %d traced to out\n",
5404				    j);
5405			);
5406		}
5407		w->bindas = -2;
5408	}
5409}
5410
5411/*
5412 * Bind assotiations to PCM channels
5413 */
5414static void
5415hdac_audio_bind_as(struct hdac_devinfo *devinfo)
5416{
5417	struct hdac_softc *sc = devinfo->codec->sc;
5418	struct hdac_audio_as *as = devinfo->function.audio.as;
5419	int j, cnt = 0, free;
5420
5421	for (j = 0; j < devinfo->function.audio.ascnt; j++) {
5422		if (as[j].enable)
5423			cnt++;
5424	}
5425	if (sc->num_chans == 0) {
5426		sc->chans = (struct hdac_chan *)malloc(
5427		    sizeof(struct hdac_chan) * cnt,
5428		    M_HDAC, M_ZERO | M_NOWAIT);
5429		if (sc->chans == NULL) {
5430			device_printf(sc->dev,
5431			    "Channels memory allocation failed!\n");
5432			return;
5433		}
5434	} else {
5435		sc->chans = (struct hdac_chan *)realloc(sc->chans,
5436		    sizeof(struct hdac_chan) * (sc->num_chans + cnt),
5437		    M_HDAC, M_ZERO | M_NOWAIT);
5438		if (sc->chans == NULL) {
5439			sc->num_chans = 0;
5440			device_printf(sc->dev,
5441			    "Channels memory allocation failed!\n");
5442			return;
5443		}
5444		/* Fixup relative pointers after realloc */
5445		for (j = 0; j < sc->num_chans; j++)
5446			sc->chans[j].caps.fmtlist = sc->chans[j].fmtlist;
5447	}
5448	free = sc->num_chans;
5449	sc->num_chans += cnt;
5450
5451	for (j = free; j < free + cnt; j++) {
5452		sc->chans[j].devinfo = devinfo;
5453		sc->chans[j].as = -1;
5454	}
5455
5456	/* Assign associations in order of their numbers, */
5457	for (j = 0; j < devinfo->function.audio.ascnt; j++) {
5458		if (as[j].enable == 0)
5459			continue;
5460
5461		as[j].chan = free;
5462		sc->chans[free].as = j;
5463		sc->chans[free].dir =
5464		    (as[j].dir == HDA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
5465		hdac_pcmchannel_setup(&sc->chans[free]);
5466		free++;
5467	}
5468}
5469
5470static void
5471hdac_audio_disable_nonaudio(struct hdac_devinfo *devinfo)
5472{
5473	struct hdac_widget *w;
5474	int i;
5475
5476	/* Disable power and volume widgets. */
5477	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5478		w = hdac_widget_get(devinfo, i);
5479		if (w == NULL || w->enable == 0)
5480			continue;
5481		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
5482		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
5483			w->enable = 0;
5484			HDA_BOOTHVERBOSE(
5485				device_printf(devinfo->codec->sc->dev,
5486				    " Disabling nid %d due to it's"
5487				    " non-audio type.\n",
5488				    w->nid);
5489			);
5490		}
5491	}
5492}
5493
5494static void
5495hdac_audio_disable_useless(struct hdac_devinfo *devinfo)
5496{
5497	struct hdac_widget *w, *cw;
5498	struct hdac_audio_ctl *ctl;
5499	int done, found, i, j, k;
5500
5501	/* Disable useless pins. */
5502	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5503		w = hdac_widget_get(devinfo, i);
5504		if (w == NULL || w->enable == 0)
5505			continue;
5506		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
5507			if ((w->wclass.pin.config &
5508			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
5509			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
5510				w->enable = 0;
5511				HDA_BOOTHVERBOSE(
5512					device_printf(devinfo->codec->sc->dev,
5513					    " Disabling pin nid %d due"
5514					    " to None connectivity.\n",
5515					    w->nid);
5516				);
5517			} else if ((w->wclass.pin.config &
5518			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
5519				w->enable = 0;
5520				HDA_BOOTHVERBOSE(
5521					device_printf(devinfo->codec->sc->dev,
5522					    " Disabling unassociated"
5523					    " pin nid %d.\n",
5524					    w->nid);
5525				);
5526			}
5527		}
5528	}
5529	do {
5530		done = 1;
5531		/* Disable and mute controls for disabled widgets. */
5532		i = 0;
5533		while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
5534			if (ctl->enable == 0)
5535				continue;
5536			if (ctl->widget->enable == 0 ||
5537			    (ctl->childwidget != NULL &&
5538			    ctl->childwidget->enable == 0)) {
5539				ctl->forcemute = 1;
5540				ctl->muted = HDA_AMP_MUTE_ALL;
5541				ctl->left = 0;
5542				ctl->right = 0;
5543				ctl->enable = 0;
5544				if (ctl->ndir == HDA_CTL_IN)
5545					ctl->widget->connsenable[ctl->index] = 0;
5546				done = 0;
5547				HDA_BOOTHVERBOSE(
5548					device_printf(devinfo->codec->sc->dev,
5549					    " Disabling ctl %d nid %d cnid %d due"
5550					    " to disabled widget.\n", i,
5551					    ctl->widget->nid,
5552					    (ctl->childwidget != NULL)?
5553					    ctl->childwidget->nid:-1);
5554				);
5555			}
5556		}
5557		/* Disable useless widgets. */
5558		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5559			w = hdac_widget_get(devinfo, i);
5560			if (w == NULL || w->enable == 0)
5561				continue;
5562			/* Disable inputs with disabled child widgets. */
5563			for (j = 0; j < w->nconns; j++) {
5564				if (w->connsenable[j]) {
5565					cw = hdac_widget_get(devinfo, w->conns[j]);
5566					if (cw == NULL || cw->enable == 0) {
5567						w->connsenable[j] = 0;
5568						HDA_BOOTHVERBOSE(
5569							device_printf(devinfo->codec->sc->dev,
5570							    " Disabling nid %d connection %d due"
5571							    " to disabled child widget.\n",
5572							    i, j);
5573						);
5574					}
5575				}
5576			}
5577			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
5578			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5579				continue;
5580			/* Disable mixers and selectors without inputs. */
5581			found = 0;
5582			for (j = 0; j < w->nconns; j++) {
5583				if (w->connsenable[j]) {
5584					found = 1;
5585					break;
5586				}
5587			}
5588			if (found == 0) {
5589				w->enable = 0;
5590				done = 0;
5591				HDA_BOOTHVERBOSE(
5592					device_printf(devinfo->codec->sc->dev,
5593					    " Disabling nid %d due to all it's"
5594					    " inputs disabled.\n", w->nid);
5595				);
5596			}
5597			/* Disable nodes without consumers. */
5598			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
5599			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5600				continue;
5601			found = 0;
5602			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
5603				cw = hdac_widget_get(devinfo, k);
5604				if (cw == NULL || cw->enable == 0)
5605					continue;
5606				for (j = 0; j < cw->nconns; j++) {
5607					if (cw->connsenable[j] && cw->conns[j] == i) {
5608						found = 1;
5609						break;
5610					}
5611				}
5612			}
5613			if (found == 0) {
5614				w->enable = 0;
5615				done = 0;
5616				HDA_BOOTHVERBOSE(
5617					device_printf(devinfo->codec->sc->dev,
5618					    " Disabling nid %d due to all it's"
5619					    " consumers disabled.\n", w->nid);
5620				);
5621			}
5622		}
5623	} while (done == 0);
5624
5625}
5626
5627static void
5628hdac_audio_disable_unas(struct hdac_devinfo *devinfo)
5629{
5630	struct hdac_audio_as *as = devinfo->function.audio.as;
5631	struct hdac_widget *w, *cw;
5632	struct hdac_audio_ctl *ctl;
5633	int i, j, k;
5634
5635	/* Disable unassosiated widgets. */
5636	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5637		w = hdac_widget_get(devinfo, i);
5638		if (w == NULL || w->enable == 0)
5639			continue;
5640		if (w->bindas == -1) {
5641			w->enable = 0;
5642			HDA_BOOTHVERBOSE(
5643				device_printf(devinfo->codec->sc->dev,
5644				    " Disabling unassociated nid %d.\n",
5645				    w->nid);
5646			);
5647		}
5648	}
5649	/* Disable input connections on input pin and
5650	 * output on output. */
5651	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5652		w = hdac_widget_get(devinfo, i);
5653		if (w == NULL || w->enable == 0)
5654			continue;
5655		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5656			continue;
5657		if (w->bindas < 0)
5658			continue;
5659		if (as[w->bindas].dir == HDA_CTL_IN) {
5660			for (j = 0; j < w->nconns; j++) {
5661				if (w->connsenable[j] == 0)
5662					continue;
5663				w->connsenable[j] = 0;
5664				HDA_BOOTHVERBOSE(
5665					device_printf(devinfo->codec->sc->dev,
5666					    " Disabling connection to input pin "
5667					    "nid %d conn %d.\n",
5668					    i, j);
5669				);
5670			}
5671			ctl = hdac_audio_ctl_amp_get(devinfo, w->nid,
5672			    HDA_CTL_IN, -1, 1);
5673			if (ctl && ctl->enable) {
5674				ctl->forcemute = 1;
5675				ctl->muted = HDA_AMP_MUTE_ALL;
5676				ctl->left = 0;
5677				ctl->right = 0;
5678				ctl->enable = 0;
5679			}
5680		} else {
5681			ctl = hdac_audio_ctl_amp_get(devinfo, w->nid,
5682			    HDA_CTL_OUT, -1, 1);
5683			if (ctl && ctl->enable) {
5684				ctl->forcemute = 1;
5685				ctl->muted = HDA_AMP_MUTE_ALL;
5686				ctl->left = 0;
5687				ctl->right = 0;
5688				ctl->enable = 0;
5689			}
5690			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
5691				cw = hdac_widget_get(devinfo, k);
5692				if (cw == NULL || cw->enable == 0)
5693					continue;
5694				for (j = 0; j < cw->nconns; j++) {
5695					if (cw->connsenable[j] && cw->conns[j] == i) {
5696						cw->connsenable[j] = 0;
5697						HDA_BOOTHVERBOSE(
5698							device_printf(devinfo->codec->sc->dev,
5699							    " Disabling connection from output pin "
5700							    "nid %d conn %d cnid %d.\n",
5701							    k, j, i);
5702						);
5703						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
5704						    cw->nconns > 1)
5705							continue;
5706						ctl = hdac_audio_ctl_amp_get(devinfo, k,
5707		    				    HDA_CTL_IN, j, 1);
5708						if (ctl && ctl->enable) {
5709							ctl->forcemute = 1;
5710							ctl->muted = HDA_AMP_MUTE_ALL;
5711							ctl->left = 0;
5712							ctl->right = 0;
5713							ctl->enable = 0;
5714						}
5715					}
5716				}
5717			}
5718		}
5719	}
5720}
5721
5722static void
5723hdac_audio_disable_notselected(struct hdac_devinfo *devinfo)
5724{
5725	struct hdac_audio_as *as = devinfo->function.audio.as;
5726	struct hdac_widget *w;
5727	int i, j;
5728
5729	/* On playback path we can safely disable all unseleted inputs. */
5730	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5731		w = hdac_widget_get(devinfo, i);
5732		if (w == NULL || w->enable == 0)
5733			continue;
5734		if (w->nconns <= 1)
5735			continue;
5736		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5737			continue;
5738		if (w->bindas < 0 || as[w->bindas].dir == HDA_CTL_IN)
5739			continue;
5740		for (j = 0; j < w->nconns; j++) {
5741			if (w->connsenable[j] == 0)
5742				continue;
5743			if (w->selconn < 0 || w->selconn == j)
5744				continue;
5745			w->connsenable[j] = 0;
5746			HDA_BOOTHVERBOSE(
5747				device_printf(devinfo->codec->sc->dev,
5748				    " Disabling unselected connection "
5749				    "nid %d conn %d.\n",
5750				    i, j);
5751			);
5752		}
5753	}
5754}
5755
5756static void
5757hdac_audio_disable_crossas(struct hdac_devinfo *devinfo)
5758{
5759	struct hdac_audio_as *ases = devinfo->function.audio.as;
5760	struct hdac_widget *w, *cw;
5761	struct hdac_audio_ctl *ctl;
5762	int i, j;
5763
5764	/* Disable crossassociatement and unwanted crosschannel connections. */
5765	/* ... using selectors */
5766	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5767		w = hdac_widget_get(devinfo, i);
5768		if (w == NULL || w->enable == 0)
5769			continue;
5770		if (w->nconns <= 1)
5771			continue;
5772		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5773			continue;
5774		if (w->bindas == -2)
5775			continue;
5776		for (j = 0; j < w->nconns; j++) {
5777			if (w->connsenable[j] == 0)
5778				continue;
5779			cw = hdac_widget_get(devinfo, w->conns[j]);
5780			if (cw == NULL || w->enable == 0)
5781				continue;
5782			if (cw->bindas == -2 ||
5783			    ((w->pflags & HDA_ADC_MONITOR) &&
5784			     cw->bindas >= 0 &&
5785			     ases[cw->bindas].dir == HDA_CTL_IN))
5786				continue;
5787			if (w->bindas == cw->bindas &&
5788			    (w->bindseqmask & cw->bindseqmask) != 0)
5789				continue;
5790			w->connsenable[j] = 0;
5791			HDA_BOOTHVERBOSE(
5792				device_printf(devinfo->codec->sc->dev,
5793				    " Disabling crossassociatement connection "
5794				    "nid %d conn %d cnid %d.\n",
5795				    i, j, cw->nid);
5796			);
5797		}
5798	}
5799	/* ... using controls */
5800	i = 0;
5801	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
5802		if (ctl->enable == 0 || ctl->childwidget == NULL)
5803			continue;
5804		if (ctl->widget->bindas == -2)
5805			continue;
5806		if (ctl->childwidget->bindas == -2 ||
5807		    ((ctl->widget->pflags & HDA_ADC_MONITOR) &&
5808		     ctl->childwidget->bindas >= 0 &&
5809		     ases[ctl->childwidget->bindas].dir == HDA_CTL_IN))
5810			continue;
5811		if (ctl->widget->bindas != ctl->childwidget->bindas ||
5812		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) == 0) {
5813			ctl->forcemute = 1;
5814			ctl->muted = HDA_AMP_MUTE_ALL;
5815			ctl->left = 0;
5816			ctl->right = 0;
5817			ctl->enable = 0;
5818			if (ctl->ndir == HDA_CTL_IN)
5819				ctl->widget->connsenable[ctl->index] = 0;
5820			HDA_BOOTHVERBOSE(
5821				device_printf(devinfo->codec->sc->dev,
5822				    " Disabling crossassociatement connection "
5823				    "ctl %d nid %d cnid %d.\n", i,
5824				    ctl->widget->nid,
5825				    ctl->childwidget->nid);
5826			);
5827		}
5828	}
5829
5830}
5831
5832#define HDA_CTL_GIVE(ctl)	((ctl)->step?1:0)
5833
5834/*
5835 * Find controls to control amplification for source.
5836 */
5837static int
5838hdac_audio_ctl_source_amp(struct hdac_devinfo *devinfo, nid_t nid, int index,
5839    int ossdev, int ctlable, int depth, int need)
5840{
5841	struct hdac_widget *w, *wc;
5842	struct hdac_audio_ctl *ctl;
5843	int i, j, conns = 0, rneed;
5844
5845	if (depth > HDA_PARSE_MAXDEPTH)
5846		return (need);
5847
5848	w = hdac_widget_get(devinfo, nid);
5849	if (w == NULL || w->enable == 0)
5850		return (need);
5851
5852	/* Count number of active inputs. */
5853	if (depth > 0) {
5854		for (j = 0; j < w->nconns; j++) {
5855			if (w->connsenable[j])
5856				conns++;
5857		}
5858	}
5859
5860	/* If this is not a first step - use input mixer.
5861	   Pins have common input ctl so care must be taken. */
5862	if (depth > 0 && ctlable && (conns == 1 ||
5863	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
5864		ctl = hdac_audio_ctl_amp_get(devinfo, w->nid, HDA_CTL_IN,
5865		    index, 1);
5866		if (ctl) {
5867			if (HDA_CTL_GIVE(ctl) & need)
5868				ctl->ossmask |= (1 << ossdev);
5869			else
5870				ctl->possmask |= (1 << ossdev);
5871			need &= ~HDA_CTL_GIVE(ctl);
5872		}
5873	}
5874
5875	/* If widget has own ossdev - not traverse it.
5876	   It will be traversed on it's own. */
5877	if (w->ossdev >= 0 && depth > 0)
5878		return (need);
5879
5880	/* We must not traverse pin */
5881	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
5882	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
5883	    depth > 0)
5884		return (need);
5885
5886	/* record that this widget exports such signal, */
5887	w->ossmask |= (1 << ossdev);
5888
5889	/* If signals mixed, we can't assign controls farther.
5890	 * Ignore this on depth zero. Caller must knows why.
5891	 * Ignore this for static selectors if this input selected.
5892	 */
5893	if (conns > 1)
5894		ctlable = 0;
5895
5896	if (ctlable) {
5897		ctl = hdac_audio_ctl_amp_get(devinfo, w->nid, HDA_CTL_OUT, -1, 1);
5898		if (ctl) {
5899			if (HDA_CTL_GIVE(ctl) & need)
5900				ctl->ossmask |= (1 << ossdev);
5901			else
5902				ctl->possmask |= (1 << ossdev);
5903			need &= ~HDA_CTL_GIVE(ctl);
5904		}
5905	}
5906
5907	rneed = 0;
5908	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5909		wc = hdac_widget_get(devinfo, i);
5910		if (wc == NULL || wc->enable == 0)
5911			continue;
5912		for (j = 0; j < wc->nconns; j++) {
5913			if (wc->connsenable[j] && wc->conns[j] == nid) {
5914				rneed |= hdac_audio_ctl_source_amp(devinfo,
5915				    wc->nid, j, ossdev, ctlable, depth + 1, need);
5916			}
5917		}
5918	}
5919	rneed &= need;
5920
5921	return (rneed);
5922}
5923
5924/*
5925 * Find controls to control amplification for destination.
5926 */
5927static void
5928hdac_audio_ctl_dest_amp(struct hdac_devinfo *devinfo, nid_t nid, int index,
5929    int ossdev, int depth, int need)
5930{
5931	struct hdac_audio_as *as = devinfo->function.audio.as;
5932	struct hdac_widget *w, *wc;
5933	struct hdac_audio_ctl *ctl;
5934	int i, j, consumers;
5935
5936	if (depth > HDA_PARSE_MAXDEPTH)
5937		return;
5938
5939	w = hdac_widget_get(devinfo, nid);
5940	if (w == NULL || w->enable == 0)
5941		return;
5942
5943	if (depth > 0) {
5944		/* If this node produce output for several consumers,
5945		   we can't touch it. */
5946		consumers = 0;
5947		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5948			wc = hdac_widget_get(devinfo, i);
5949			if (wc == NULL || wc->enable == 0)
5950				continue;
5951			for (j = 0; j < wc->nconns; j++) {
5952				if (wc->connsenable[j] && wc->conns[j] == nid)
5953					consumers++;
5954			}
5955		}
5956		/* The only exception is if real HP redirection is configured
5957		   and this is a duplication point.
5958		   XXX: Actually exception is not completely correct.
5959		   XXX: Duplication point check is not perfect. */
5960		if ((consumers == 2 && (w->bindas < 0 ||
5961		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
5962		    (w->bindseqmask & (1 << 15)) == 0)) ||
5963		    consumers > 2)
5964			return;
5965
5966		/* Else use it's output mixer. */
5967		ctl = hdac_audio_ctl_amp_get(devinfo, w->nid,
5968		    HDA_CTL_OUT, -1, 1);
5969		if (ctl) {
5970			if (HDA_CTL_GIVE(ctl) & need)
5971				ctl->ossmask |= (1 << ossdev);
5972			else
5973				ctl->possmask |= (1 << ossdev);
5974			need &= ~HDA_CTL_GIVE(ctl);
5975		}
5976	}
5977
5978	/* We must not traverse pin */
5979	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
5980	    depth > 0)
5981		return;
5982
5983	for (i = 0; i < w->nconns; i++) {
5984		int tneed = need;
5985		if (w->connsenable[i] == 0)
5986			continue;
5987		if (index >= 0 && i != index)
5988			continue;
5989		ctl = hdac_audio_ctl_amp_get(devinfo, w->nid,
5990		    HDA_CTL_IN, i, 1);
5991		if (ctl) {
5992			if (HDA_CTL_GIVE(ctl) & tneed)
5993				ctl->ossmask |= (1 << ossdev);
5994			else
5995				ctl->possmask |= (1 << ossdev);
5996			tneed &= ~HDA_CTL_GIVE(ctl);
5997		}
5998		hdac_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
5999		    depth + 1, tneed);
6000	}
6001}
6002
6003/*
6004 * Assign OSS names to sound sources
6005 */
6006static void
6007hdac_audio_assign_names(struct hdac_devinfo *devinfo)
6008{
6009	struct hdac_audio_as *as = devinfo->function.audio.as;
6010	struct hdac_widget *w;
6011	int i, j;
6012	int type = -1, use, used = 0;
6013	static const int types[7][13] = {
6014	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
6015	      SOUND_MIXER_LINE3, -1 },	/* line */
6016	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
6017	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
6018	    { SOUND_MIXER_CD, -1 },	/* cd */
6019	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
6020	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
6021	      -1 },	/* digital */
6022	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
6023	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
6024	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
6025	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
6026	      -1 }	/* others */
6027	};
6028
6029	/* Surely known names */
6030	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6031		w = hdac_widget_get(devinfo, i);
6032		if (w == NULL || w->enable == 0)
6033			continue;
6034		if (w->bindas == -1)
6035			continue;
6036		use = -1;
6037		switch (w->type) {
6038		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
6039			if (as[w->bindas].dir == HDA_CTL_OUT)
6040				break;
6041			type = -1;
6042			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
6043			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
6044				type = 0;
6045				break;
6046			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
6047				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
6048				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
6049					break;
6050				type = 1;
6051				break;
6052			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
6053				type = 3;
6054				break;
6055			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
6056				type = 4;
6057				break;
6058			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
6059			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
6060				type = 5;
6061				break;
6062			}
6063			if (type == -1)
6064				break;
6065			j = 0;
6066			while (types[type][j] >= 0 &&
6067			    (used & (1 << types[type][j])) != 0) {
6068				j++;
6069			}
6070			if (types[type][j] >= 0)
6071				use = types[type][j];
6072			break;
6073		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
6074			use = SOUND_MIXER_PCM;
6075			break;
6076		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
6077			use = SOUND_MIXER_SPEAKER;
6078			break;
6079		default:
6080			break;
6081		}
6082		if (use >= 0) {
6083			w->ossdev = use;
6084			used |= (1 << use);
6085		}
6086	}
6087	/* Semi-known names */
6088	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6089		w = hdac_widget_get(devinfo, i);
6090		if (w == NULL || w->enable == 0)
6091			continue;
6092		if (w->ossdev >= 0)
6093			continue;
6094		if (w->bindas == -1)
6095			continue;
6096		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6097			continue;
6098		if (as[w->bindas].dir == HDA_CTL_OUT)
6099			continue;
6100		type = -1;
6101		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
6102		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
6103		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
6104		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
6105		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
6106			type = 0;
6107			break;
6108		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
6109			type = 2;
6110			break;
6111		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
6112		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
6113			type = 5;
6114			break;
6115		}
6116		if (type == -1)
6117			break;
6118		j = 0;
6119		while (types[type][j] >= 0 &&
6120		    (used & (1 << types[type][j])) != 0) {
6121			j++;
6122		}
6123		if (types[type][j] >= 0) {
6124			w->ossdev = types[type][j];
6125			used |= (1 << types[type][j]);
6126		}
6127	}
6128	/* Others */
6129	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6130		w = hdac_widget_get(devinfo, i);
6131		if (w == NULL || w->enable == 0)
6132			continue;
6133		if (w->ossdev >= 0)
6134			continue;
6135		if (w->bindas == -1)
6136			continue;
6137		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6138			continue;
6139		if (as[w->bindas].dir == HDA_CTL_OUT)
6140			continue;
6141		j = 0;
6142		while (types[6][j] >= 0 &&
6143		    (used & (1 << types[6][j])) != 0) {
6144			j++;
6145		}
6146		if (types[6][j] >= 0) {
6147			w->ossdev = types[6][j];
6148			used |= (1 << types[6][j]);
6149		}
6150	}
6151}
6152
6153static void
6154hdac_audio_build_tree(struct hdac_devinfo *devinfo)
6155{
6156	struct hdac_audio_as *as = devinfo->function.audio.as;
6157	int j, res;
6158
6159	/* Trace all associations in order of their numbers, */
6160	for (j = 0; j < devinfo->function.audio.ascnt; j++) {
6161		if (as[j].enable == 0)
6162			continue;
6163		HDA_BOOTVERBOSE(
6164			device_printf(devinfo->codec->sc->dev,
6165			    "Tracing association %d (%d)\n", j, as[j].index);
6166		);
6167		if (as[j].dir == HDA_CTL_OUT) {
6168retry:
6169			res = hdac_audio_trace_as_out(devinfo, j, 0);
6170			if (res == 0 && as[j].hpredir >= 0 &&
6171			    as[j].fakeredir == 0) {
6172				/* If codec can't do analog HP redirection
6173				   try to make it using one more DAC. */
6174				as[j].fakeredir = 1;
6175				goto retry;
6176			}
6177		} else {
6178			res = hdac_audio_trace_as_in(devinfo, j);
6179		}
6180		if (res) {
6181			HDA_BOOTVERBOSE(
6182				device_printf(devinfo->codec->sc->dev,
6183				    "Association %d (%d) trace succeeded\n",
6184				    j, as[j].index);
6185			);
6186		} else {
6187			HDA_BOOTVERBOSE(
6188				device_printf(devinfo->codec->sc->dev,
6189				    "Association %d (%d) trace failed\n",
6190				    j, as[j].index);
6191			);
6192			as[j].enable = 0;
6193		}
6194	}
6195
6196	/* Trace mixer and beeper pseudo associations. */
6197	hdac_audio_trace_as_extra(devinfo);
6198}
6199
6200static void
6201hdac_audio_assign_mixers(struct hdac_devinfo *devinfo)
6202{
6203	struct hdac_audio_as *as = devinfo->function.audio.as;
6204	struct hdac_audio_ctl *ctl;
6205	struct hdac_widget *w, *cw;
6206	int i, j;
6207
6208	/* Assign mixers to the tree. */
6209	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6210		w = hdac_widget_get(devinfo, i);
6211		if (w == NULL || w->enable == 0)
6212			continue;
6213		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
6214		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
6215		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
6216		    as[w->bindas].dir == HDA_CTL_IN)) {
6217			if (w->ossdev < 0)
6218				continue;
6219			hdac_audio_ctl_source_amp(devinfo, w->nid, -1,
6220			    w->ossdev, 1, 0, 1);
6221		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
6222			hdac_audio_ctl_dest_amp(devinfo, w->nid, -1,
6223			    SOUND_MIXER_RECLEV, 0, 1);
6224		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
6225		    as[w->bindas].dir == HDA_CTL_OUT) {
6226			hdac_audio_ctl_dest_amp(devinfo, w->nid, -1,
6227			    SOUND_MIXER_VOLUME, 0, 1);
6228		}
6229		if (w->ossdev == SOUND_MIXER_IMIX) {
6230			if (hdac_audio_ctl_source_amp(devinfo, w->nid, -1,
6231			    w->ossdev, 1, 0, 1)) {
6232				/* If we are unable to control input monitor
6233				   as source - try to control it as destination. */
6234				hdac_audio_ctl_dest_amp(devinfo, w->nid, -1,
6235				    w->ossdev, 0, 1);
6236			}
6237		}
6238		if (w->pflags & HDA_ADC_MONITOR) {
6239			for (j = 0; j < w->nconns; j++) {
6240				if (!w->connsenable[j])
6241				    continue;
6242				cw = hdac_widget_get(devinfo, w->conns[j]);
6243				if (cw == NULL || cw->enable == 0)
6244				    continue;
6245				if (cw->bindas == -1)
6246				    continue;
6247				if (cw->bindas >= 0 &&
6248				    as[cw->bindas].dir != HDA_CTL_IN)
6249					continue;
6250				hdac_audio_ctl_dest_amp(devinfo,
6251				    w->nid, j, SOUND_MIXER_IGAIN, 0, 1);
6252			}
6253		}
6254	}
6255	/* Treat unrequired as possible. */
6256	i = 0;
6257	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
6258		if (ctl->ossmask == 0)
6259			ctl->ossmask = ctl->possmask;
6260	}
6261}
6262
6263static void
6264hdac_audio_prepare_pin_ctrl(struct hdac_devinfo *devinfo)
6265{
6266	struct hdac_audio_as *as = devinfo->function.audio.as;
6267	struct hdac_widget *w;
6268	uint32_t pincap;
6269	int i;
6270
6271	for (i = 0; i < devinfo->nodecnt; i++) {
6272		w = &devinfo->widget[i];
6273		if (w == NULL)
6274			continue;
6275		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6276			continue;
6277
6278		pincap = w->wclass.pin.cap;
6279
6280		/* Disable everything. */
6281		w->wclass.pin.ctrl &= ~(
6282		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
6283		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
6284		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
6285		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
6286
6287		if (w->enable == 0 ||
6288		    w->bindas < 0 || as[w->bindas].enable == 0) {
6289			/* Pin is unused so left it disabled. */
6290			continue;
6291		} else if (as[w->bindas].dir == HDA_CTL_IN) {
6292			/* Input pin, configure for input. */
6293			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
6294				w->wclass.pin.ctrl |=
6295				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
6296
6297			if ((devinfo->function.audio.quirks & HDA_QUIRK_IVREF100) &&
6298			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
6299				w->wclass.pin.ctrl |=
6300				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6301				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
6302			else if ((devinfo->function.audio.quirks & HDA_QUIRK_IVREF80) &&
6303			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
6304				w->wclass.pin.ctrl |=
6305				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6306				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
6307			else if ((devinfo->function.audio.quirks & HDA_QUIRK_IVREF50) &&
6308			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
6309				w->wclass.pin.ctrl |=
6310				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6311				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
6312		} else {
6313			/* Output pin, configure for output. */
6314			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
6315				w->wclass.pin.ctrl |=
6316				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
6317
6318			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
6319			    (w->wclass.pin.config &
6320			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
6321			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
6322				w->wclass.pin.ctrl |=
6323				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
6324
6325			if ((devinfo->function.audio.quirks & HDA_QUIRK_OVREF100) &&
6326			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
6327				w->wclass.pin.ctrl |=
6328				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6329				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
6330			else if ((devinfo->function.audio.quirks & HDA_QUIRK_OVREF80) &&
6331			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
6332				w->wclass.pin.ctrl |=
6333				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6334				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
6335			else if ((devinfo->function.audio.quirks & HDA_QUIRK_OVREF50) &&
6336			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
6337				w->wclass.pin.ctrl |=
6338				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
6339				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
6340		}
6341	}
6342}
6343
6344static void
6345hdac_audio_ctl_commit(struct hdac_devinfo *devinfo)
6346{
6347	struct hdac_audio_ctl *ctl;
6348	int i, z;
6349
6350	i = 0;
6351	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
6352		if (ctl->enable == 0 || ctl->ossmask != 0) {
6353			/* Mute disabled and mixer controllable controls.
6354			 * Last will be initialized by mixer_init().
6355			 * This expected to reduce click on startup. */
6356			hdac_audio_ctl_amp_set(ctl, HDA_AMP_MUTE_ALL, 0, 0);
6357			continue;
6358		}
6359		/* Init fixed controls to 0dB amplification. */
6360		z = ctl->offset;
6361		if (z > ctl->step)
6362			z = ctl->step;
6363		hdac_audio_ctl_amp_set(ctl, HDA_AMP_MUTE_NONE, z, z);
6364	}
6365}
6366
6367static void
6368hdac_audio_commit(struct hdac_devinfo *devinfo)
6369{
6370	struct hdac_softc *sc = devinfo->codec->sc;
6371	struct hdac_widget *w;
6372	nid_t cad;
6373	uint32_t gdata, gmask, gdir;
6374	int commitgpio, numgpio;
6375	int i;
6376
6377	cad = devinfo->codec->cad;
6378
6379	if (sc->pci_subvendor == APPLE_INTEL_MAC)
6380		hdac_command(sc, HDA_CMD_12BIT(cad, devinfo->nid,
6381		    0x7e7, 0), cad);
6382
6383	/* Commit controls. */
6384	hdac_audio_ctl_commit(devinfo);
6385
6386	/* Commit selectors, pins and EAPD. */
6387	for (i = 0; i < devinfo->nodecnt; i++) {
6388		w = &devinfo->widget[i];
6389		if (w == NULL)
6390			continue;
6391		if (w->selconn == -1)
6392			w->selconn = 0;
6393		if (w->nconns > 0)
6394			hdac_widget_connection_select(w, w->selconn);
6395		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
6396			hdac_command(sc,
6397			    HDA_CMD_SET_PIN_WIDGET_CTRL(cad, w->nid,
6398			    w->wclass.pin.ctrl), cad);
6399		}
6400		if (w->param.eapdbtl != HDAC_INVALID) {
6401		    	uint32_t val;
6402
6403			val = w->param.eapdbtl;
6404			if (devinfo->function.audio.quirks &
6405			    HDA_QUIRK_EAPDINV)
6406				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
6407			hdac_command(sc,
6408			    HDA_CMD_SET_EAPD_BTL_ENABLE(cad, w->nid,
6409			    val), cad);
6410		}
6411	}
6412
6413	/* Commit GPIOs. */
6414	gdata = 0;
6415	gmask = 0;
6416	gdir = 0;
6417	commitgpio = 0;
6418	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(
6419	    devinfo->function.audio.gpio);
6420
6421	if (devinfo->function.audio.quirks & HDA_QUIRK_GPIOFLUSH)
6422		commitgpio = (numgpio > 0) ? 1 : 0;
6423	else {
6424		for (i = 0; i < numgpio && i < HDA_GPIO_MAX; i++) {
6425			if (!(devinfo->function.audio.quirks &
6426			    (1 << i)))
6427				continue;
6428			if (commitgpio == 0) {
6429				commitgpio = 1;
6430				HDA_BOOTVERBOSE(
6431					gdata = hdac_command(sc,
6432					    HDA_CMD_GET_GPIO_DATA(cad,
6433					    devinfo->nid), cad);
6434					gmask = hdac_command(sc,
6435					    HDA_CMD_GET_GPIO_ENABLE_MASK(cad,
6436					    devinfo->nid), cad);
6437					gdir = hdac_command(sc,
6438					    HDA_CMD_GET_GPIO_DIRECTION(cad,
6439					    devinfo->nid), cad);
6440					device_printf(sc->dev,
6441					    "GPIO init: data=0x%08x "
6442					    "mask=0x%08x dir=0x%08x\n",
6443					    gdata, gmask, gdir);
6444					gdata = 0;
6445					gmask = 0;
6446					gdir = 0;
6447				);
6448			}
6449			gdata |= 1 << i;
6450			gmask |= 1 << i;
6451			gdir |= 1 << i;
6452		}
6453	}
6454
6455	if (commitgpio != 0) {
6456		HDA_BOOTVERBOSE(
6457			device_printf(sc->dev,
6458			    "GPIO commit: data=0x%08x mask=0x%08x "
6459			    "dir=0x%08x\n",
6460			    gdata, gmask, gdir);
6461		);
6462		hdac_command(sc,
6463		    HDA_CMD_SET_GPIO_ENABLE_MASK(cad, devinfo->nid,
6464		    gmask), cad);
6465		hdac_command(sc,
6466		    HDA_CMD_SET_GPIO_DIRECTION(cad, devinfo->nid,
6467		    gdir), cad);
6468		hdac_command(sc,
6469		    HDA_CMD_SET_GPIO_DATA(cad, devinfo->nid,
6470		    gdata), cad);
6471	}
6472}
6473
6474static void
6475hdac_powerup(struct hdac_devinfo *devinfo)
6476{
6477	struct hdac_softc *sc = devinfo->codec->sc;
6478	nid_t cad = devinfo->codec->cad;
6479	int i;
6480
6481	hdac_command(sc,
6482	    HDA_CMD_SET_POWER_STATE(cad,
6483	    devinfo->nid, HDA_CMD_POWER_STATE_D0),
6484	    cad);
6485	DELAY(100);
6486
6487	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6488		hdac_command(sc,
6489		    HDA_CMD_SET_POWER_STATE(cad,
6490		    i, HDA_CMD_POWER_STATE_D0),
6491		    cad);
6492	}
6493	DELAY(1000);
6494}
6495
6496static int
6497hdac_pcmchannel_setup(struct hdac_chan *ch)
6498{
6499	struct hdac_devinfo *devinfo = ch->devinfo;
6500	struct hdac_audio_as *as = devinfo->function.audio.as;
6501	struct hdac_widget *w;
6502	uint32_t cap, fmtcap, pcmcap;
6503	int i, j, ret, channels, onlystereo;
6504	uint16_t pinset;
6505
6506	ch->caps = hdac_caps;
6507	ch->caps.fmtlist = ch->fmtlist;
6508	ch->bit16 = 1;
6509	ch->bit32 = 0;
6510	ch->pcmrates[0] = 48000;
6511	ch->pcmrates[1] = 0;
6512
6513	ret = 0;
6514	channels = 0;
6515	onlystereo = 1;
6516	pinset = 0;
6517	fmtcap = devinfo->function.audio.supp_stream_formats;
6518	pcmcap = devinfo->function.audio.supp_pcm_size_rate;
6519
6520	for (i = 0; i < 16; i++) {
6521		/* Check as is correct */
6522		if (ch->as < 0)
6523			break;
6524		/* Cound only present DACs */
6525		if (as[ch->as].dacs[i] <= 0)
6526			continue;
6527		/* Ignore duplicates */
6528		for (j = 0; j < ret; j++) {
6529			if (ch->io[j] == as[ch->as].dacs[i])
6530				break;
6531		}
6532		if (j < ret)
6533			continue;
6534
6535		w = hdac_widget_get(devinfo, as[ch->as].dacs[i]);
6536		if (w == NULL || w->enable == 0)
6537			continue;
6538		cap = w->param.supp_stream_formats;
6539		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
6540		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
6541			continue;
6542		/* Many CODECs does not declare AC3 support on SPDIF.
6543		   I don't beleave that they doesn't support it! */
6544		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
6545			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
6546		if (ret == 0) {
6547			fmtcap = cap;
6548			pcmcap = w->param.supp_pcm_size_rate;
6549		} else {
6550			fmtcap &= cap;
6551			pcmcap &= w->param.supp_pcm_size_rate;
6552		}
6553		ch->io[ret++] = as[ch->as].dacs[i];
6554		/* Do not count redirection pin/dac channels. */
6555		if (i == 15 && as[ch->as].hpredir >= 0)
6556			continue;
6557		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
6558		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
6559			onlystereo = 0;
6560		pinset |= (1 << i);
6561	}
6562	ch->io[ret] = -1;
6563
6564	if (as[ch->as].fakeredir)
6565		ret--;
6566	/* Standard speaks only about stereo pins and playback, ... */
6567	if ((!onlystereo) || as[ch->as].dir != HDA_CTL_OUT)
6568		pinset = 0;
6569	/* ..., but there it gives us info about speakers layout. */
6570	as[ch->as].pinset = pinset;
6571
6572	ch->supp_stream_formats = fmtcap;
6573	ch->supp_pcm_size_rate = pcmcap;
6574
6575	/*
6576	 *  8bit = 0
6577	 * 16bit = 1
6578	 * 20bit = 2
6579	 * 24bit = 3
6580	 * 32bit = 4
6581	 */
6582	if (ret > 0) {
6583		i = 0;
6584		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
6585			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
6586				ch->bit16 = 1;
6587			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
6588				ch->bit16 = 0;
6589			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6590				ch->bit32 = 4;
6591			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6592				ch->bit32 = 3;
6593			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6594				ch->bit32 = 2;
6595			if (!(devinfo->function.audio.quirks & HDA_QUIRK_FORCESTEREO)) {
6596				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
6597				if (ch->bit32)
6598					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
6599			}
6600			if (channels >= 2) {
6601				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
6602				if (ch->bit32)
6603					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
6604			}
6605			if (channels == 4 || /* Any 4-channel */
6606			    pinset == 0x0007 || /* 5.1 */
6607			    pinset == 0x0013 || /* 5.1 */
6608			    pinset == 0x0017) {  /* 7.1 */
6609				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
6610				if (ch->bit32)
6611					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
6612			}
6613			if (channels == 6 || /* Any 6-channel */
6614			    pinset == 0x0017) {  /* 7.1 */
6615				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
6616				if (ch->bit32)
6617					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
6618			}
6619			if (channels == 8) { /* Any 8-channel */
6620				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
6621				if (ch->bit32)
6622					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
6623			}
6624		}
6625		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
6626			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
6627		}
6628		ch->fmtlist[i] = 0;
6629		i = 0;
6630		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
6631			ch->pcmrates[i++] = 8000;
6632		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
6633			ch->pcmrates[i++] = 11025;
6634		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
6635			ch->pcmrates[i++] = 16000;
6636		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
6637			ch->pcmrates[i++] = 22050;
6638		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
6639			ch->pcmrates[i++] = 32000;
6640		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
6641			ch->pcmrates[i++] = 44100;
6642		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
6643		ch->pcmrates[i++] = 48000;
6644		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
6645			ch->pcmrates[i++] = 88200;
6646		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
6647			ch->pcmrates[i++] = 96000;
6648		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
6649			ch->pcmrates[i++] = 176400;
6650		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
6651			ch->pcmrates[i++] = 192000;
6652		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
6653		ch->pcmrates[i] = 0;
6654		if (i > 0) {
6655			ch->caps.minspeed = ch->pcmrates[0];
6656			ch->caps.maxspeed = ch->pcmrates[i - 1];
6657		}
6658	}
6659
6660	return (ret);
6661}
6662
6663static void
6664hdac_create_pcms(struct hdac_devinfo *devinfo)
6665{
6666	struct hdac_softc *sc = devinfo->codec->sc;
6667	struct hdac_audio_as *as = devinfo->function.audio.as;
6668	int i, j, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
6669
6670	for (i = 0; i < devinfo->function.audio.ascnt; i++) {
6671		if (as[i].enable == 0)
6672			continue;
6673		if (as[i].dir == HDA_CTL_IN) {
6674			if (as[i].digital)
6675				drdev++;
6676			else
6677				ardev++;
6678		} else {
6679			if (as[i].digital)
6680				dpdev++;
6681			else
6682				apdev++;
6683		}
6684	}
6685	devinfo->function.audio.num_devs =
6686	    max(ardev, apdev) + max(drdev, dpdev);
6687	devinfo->function.audio.devs =
6688	    (struct hdac_pcm_devinfo *)malloc(
6689	    devinfo->function.audio.num_devs * sizeof(struct hdac_pcm_devinfo),
6690	    M_HDAC, M_ZERO | M_NOWAIT);
6691	if (devinfo->function.audio.devs == NULL) {
6692		device_printf(sc->dev,
6693		    "Unable to allocate memory for devices\n");
6694		return;
6695	}
6696	for (i = 0; i < devinfo->function.audio.num_devs; i++) {
6697		devinfo->function.audio.devs[i].index = i;
6698		devinfo->function.audio.devs[i].devinfo = devinfo;
6699		devinfo->function.audio.devs[i].play = -1;
6700		devinfo->function.audio.devs[i].rec = -1;
6701		devinfo->function.audio.devs[i].digital = 255;
6702	}
6703	for (i = 0; i < devinfo->function.audio.ascnt; i++) {
6704		if (as[i].enable == 0)
6705			continue;
6706		for (j = 0; j < devinfo->function.audio.num_devs; j++) {
6707			if (devinfo->function.audio.devs[j].digital != 255 &&
6708			    (!devinfo->function.audio.devs[j].digital) !=
6709			    (!as[i].digital))
6710				continue;
6711			if (as[i].dir == HDA_CTL_IN) {
6712				if (devinfo->function.audio.devs[j].rec >= 0)
6713					continue;
6714				devinfo->function.audio.devs[j].rec
6715				    = as[i].chan;
6716			} else {
6717				if (devinfo->function.audio.devs[j].play >= 0)
6718					continue;
6719				devinfo->function.audio.devs[j].play
6720				    = as[i].chan;
6721			}
6722			sc->chans[as[i].chan].pdevinfo =
6723			    &devinfo->function.audio.devs[j];
6724			devinfo->function.audio.devs[j].digital =
6725			    as[i].digital;
6726			break;
6727		}
6728	}
6729	for (i = 0; i < devinfo->function.audio.num_devs; i++) {
6730		struct hdac_pcm_devinfo *pdevinfo =
6731		    &devinfo->function.audio.devs[i];
6732		pdevinfo->dev =
6733		    device_add_child(sc->dev, "pcm", -1);
6734		device_set_ivars(pdevinfo->dev,
6735		     (void *)pdevinfo);
6736	}
6737}
6738
6739static void
6740hdac_dump_ctls(struct hdac_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
6741{
6742	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
6743	struct hdac_audio_ctl *ctl;
6744	struct hdac_softc *sc = devinfo->codec->sc;
6745	char buf[64];
6746	int i, j, printed;
6747
6748	if (flag == 0) {
6749		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
6750		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
6751		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
6752		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
6753	}
6754
6755	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
6756		if ((flag & (1 << j)) == 0)
6757			continue;
6758		i = 0;
6759		printed = 0;
6760		while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
6761			if (ctl->enable == 0 ||
6762			    ctl->widget->enable == 0)
6763				continue;
6764			if (!((pdevinfo->play >= 0 &&
6765			    ctl->widget->bindas == sc->chans[pdevinfo->play].as) ||
6766			    (pdevinfo->rec >= 0 &&
6767			    ctl->widget->bindas == sc->chans[pdevinfo->rec].as) ||
6768			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
6769				continue;
6770			if ((ctl->ossmask & (1 << j)) == 0)
6771				continue;
6772
6773	    		if (printed == 0) {
6774				device_printf(pdevinfo->dev, "\n");
6775				if (banner != NULL) {
6776					device_printf(pdevinfo->dev, "%s", banner);
6777				} else {
6778					device_printf(pdevinfo->dev, "Unknown Ctl");
6779				}
6780				printf(" (OSS: %s)\n",
6781				    hdac_audio_ctl_ossmixer_mask2allname(1 << j,
6782				    buf, sizeof(buf)));
6783				device_printf(pdevinfo->dev, "   |\n");
6784				printed = 1;
6785			}
6786			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
6787				ctl->widget->nid,
6788				(ctl->ndir == HDA_CTL_IN)?"in ":"out");
6789			if (ctl->ndir == HDA_CTL_IN && ctl->ndir == ctl->dir)
6790				printf(" %2d): ", ctl->index);
6791			else
6792				printf("):    ");
6793			if (ctl->step > 0) {
6794				printf("%+d/%+ddB (%d steps)%s\n",
6795			    	    (0 - ctl->offset) * (ctl->size + 1) / 4,
6796				    (ctl->step - ctl->offset) * (ctl->size + 1) / 4,
6797				    ctl->step + 1,
6798				    ctl->mute?" + mute":"");
6799			} else
6800				printf("%s\n", ctl->mute?"mute":"");
6801		}
6802	}
6803}
6804
6805static void
6806hdac_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
6807{
6808	uint32_t cap;
6809
6810	cap = fcap;
6811	if (cap != 0) {
6812		device_printf(dev, "     Stream cap: 0x%08x\n", cap);
6813		device_printf(dev, "                ");
6814		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
6815			printf(" AC3");
6816		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
6817			printf(" FLOAT32");
6818		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
6819			printf(" PCM");
6820		printf("\n");
6821	}
6822	cap = pcmcap;
6823	if (cap != 0) {
6824		device_printf(dev, "        PCM cap: 0x%08x\n", cap);
6825		device_printf(dev, "                ");
6826		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
6827			printf(" 8");
6828		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
6829			printf(" 16");
6830		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
6831			printf(" 20");
6832		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
6833			printf(" 24");
6834		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
6835			printf(" 32");
6836		printf(" bits,");
6837		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
6838			printf(" 8");
6839		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
6840			printf(" 11");
6841		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
6842			printf(" 16");
6843		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
6844			printf(" 22");
6845		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
6846			printf(" 32");
6847		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
6848			printf(" 44");
6849		printf(" 48");
6850		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
6851			printf(" 88");
6852		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
6853			printf(" 96");
6854		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
6855			printf(" 176");
6856		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
6857			printf(" 192");
6858		printf(" KHz\n");
6859	}
6860}
6861
6862static void
6863hdac_dump_pin(struct hdac_softc *sc, struct hdac_widget *w)
6864{
6865	uint32_t pincap;
6866
6867	pincap = w->wclass.pin.cap;
6868
6869	device_printf(sc->dev, "        Pin cap: 0x%08x\n", pincap);
6870	device_printf(sc->dev, "                ");
6871	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
6872		printf(" ISC");
6873	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
6874		printf(" TRQD");
6875	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
6876		printf(" PDC");
6877	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
6878		printf(" HP");
6879	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
6880		printf(" OUT");
6881	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
6882		printf(" IN");
6883	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
6884		printf(" BAL");
6885	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
6886		printf(" HDMI");
6887	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
6888		printf(" VREF[");
6889		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
6890			printf(" 50");
6891		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
6892			printf(" 80");
6893		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
6894			printf(" 100");
6895		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
6896			printf(" GROUND");
6897		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
6898			printf(" HIZ");
6899		printf(" ]");
6900	}
6901	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
6902		printf(" EAPD");
6903	if (HDA_PARAM_PIN_CAP_DP(pincap))
6904		printf(" DP");
6905	if (HDA_PARAM_PIN_CAP_HBR(pincap))
6906		printf(" HBR");
6907	printf("\n");
6908	device_printf(sc->dev, "     Pin config: 0x%08x\n",
6909	    w->wclass.pin.config);
6910	device_printf(sc->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
6911	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
6912		printf(" HP");
6913	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
6914		printf(" IN");
6915	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
6916		printf(" OUT");
6917	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK)
6918		printf(" VREFs");
6919	printf("\n");
6920}
6921
6922static void
6923hdac_dump_pin_config(struct hdac_widget *w, uint32_t conf)
6924{
6925	struct hdac_softc *sc = w->devinfo->codec->sc;
6926
6927	device_printf(sc->dev, " nid %d 0x%08x as %2d seq %2d %13s %5s "
6928	    "jack %2d loc %2d color %7s misc %d%s\n",
6929	    w->nid, conf,
6930	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
6931	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
6932	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
6933	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
6934	    HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf),
6935	    HDA_CONFIG_DEFAULTCONF_LOCATION(conf),
6936	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
6937	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
6938	    (w->enable == 0)?" [DISABLED]":"");
6939}
6940
6941static void
6942hdac_dump_pin_configs(struct hdac_devinfo *devinfo)
6943{
6944	struct hdac_widget *w;
6945	int i;
6946
6947	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6948		w = hdac_widget_get(devinfo, i);
6949		if (w == NULL)
6950			continue;
6951		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6952			continue;
6953		hdac_dump_pin_config(w, w->wclass.pin.config);
6954	}
6955}
6956
6957static void
6958hdac_dump_amp(struct hdac_softc *sc, uint32_t cap, char *banner)
6959{
6960	device_printf(sc->dev, "     %s amp: 0x%08x\n", banner, cap);
6961	device_printf(sc->dev, "                 "
6962	    "mute=%d step=%d size=%d offset=%d\n",
6963	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
6964	    HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap),
6965	    HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap),
6966	    HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap));
6967}
6968
6969static void
6970hdac_dump_nodes(struct hdac_devinfo *devinfo)
6971{
6972	struct hdac_softc *sc = devinfo->codec->sc;
6973	static char *ossname[] = SOUND_DEVICE_NAMES;
6974	struct hdac_widget *w, *cw;
6975	char buf[64];
6976	int i, j;
6977
6978	device_printf(sc->dev, "\n");
6979	device_printf(sc->dev, "Default Parameter\n");
6980	device_printf(sc->dev, "-----------------\n");
6981	hdac_dump_audio_formats(sc->dev,
6982	    devinfo->function.audio.supp_stream_formats,
6983	    devinfo->function.audio.supp_pcm_size_rate);
6984	device_printf(sc->dev, "         IN amp: 0x%08x\n",
6985	    devinfo->function.audio.inamp_cap);
6986	device_printf(sc->dev, "        OUT amp: 0x%08x\n",
6987	    devinfo->function.audio.outamp_cap);
6988	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6989		w = hdac_widget_get(devinfo, i);
6990		if (w == NULL) {
6991			device_printf(sc->dev, "Ghost widget nid=%d\n", i);
6992			continue;
6993		}
6994		device_printf(sc->dev, "\n");
6995		device_printf(sc->dev, "            nid: %d%s\n", w->nid,
6996		    (w->enable == 0) ? " [DISABLED]" : "");
6997		device_printf(sc->dev, "           Name: %s\n", w->name);
6998		device_printf(sc->dev, "     Widget cap: 0x%08x\n",
6999		    w->param.widget_cap);
7000		if (w->param.widget_cap & 0x0ee1) {
7001			device_printf(sc->dev, "                ");
7002			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
7003			    printf(" LRSWAP");
7004			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
7005			    printf(" PWR");
7006			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
7007			    printf(" DIGITAL");
7008			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
7009			    printf(" UNSOL");
7010			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
7011			    printf(" PROC");
7012			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
7013			    printf(" STRIPE");
7014			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
7015			if (j == 1)
7016			    printf(" STEREO");
7017			else if (j > 1)
7018			    printf(" %dCH", j + 1);
7019			printf("\n");
7020		}
7021		if (w->bindas != -1) {
7022			device_printf(sc->dev, "    Association: %d (0x%08x)\n",
7023			    w->bindas, w->bindseqmask);
7024		}
7025		if (w->ossmask != 0 || w->ossdev >= 0) {
7026			device_printf(sc->dev, "            OSS: %s",
7027			    hdac_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
7028			if (w->ossdev >= 0)
7029			    printf(" (%s)", ossname[w->ossdev]);
7030			printf("\n");
7031		}
7032		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
7033		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
7034			hdac_dump_audio_formats(sc->dev,
7035			    w->param.supp_stream_formats,
7036			    w->param.supp_pcm_size_rate);
7037		} else if (w->type ==
7038		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
7039			hdac_dump_pin(sc, w);
7040		if (w->param.eapdbtl != HDAC_INVALID)
7041			device_printf(sc->dev, "           EAPD: 0x%08x\n",
7042			    w->param.eapdbtl);
7043		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
7044		    w->param.outamp_cap != 0)
7045			hdac_dump_amp(sc, w->param.outamp_cap, "Output");
7046		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
7047		    w->param.inamp_cap != 0)
7048			hdac_dump_amp(sc, w->param.inamp_cap, " Input");
7049		if (w->nconns > 0) {
7050			device_printf(sc->dev, "    connections: %d\n", w->nconns);
7051			device_printf(sc->dev, "          |\n");
7052		}
7053		for (j = 0; j < w->nconns; j++) {
7054			cw = hdac_widget_get(devinfo, w->conns[j]);
7055			device_printf(sc->dev, "          + %s<- nid=%d [%s]",
7056			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
7057			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
7058			if (cw == NULL)
7059				printf(" [UNKNOWN]");
7060			else if (cw->enable == 0)
7061				printf(" [DISABLED]");
7062			if (w->nconns > 1 && w->selconn == j && w->type !=
7063			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
7064				printf(" (selected)");
7065			printf("\n");
7066		}
7067	}
7068
7069}
7070
7071static void
7072hdac_dump_dst_nid(struct hdac_pcm_devinfo *pdevinfo, nid_t nid, int depth)
7073{
7074	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
7075	struct hdac_widget *w, *cw;
7076	char buf[64];
7077	int i, printed = 0;
7078
7079	if (depth > HDA_PARSE_MAXDEPTH)
7080		return;
7081
7082	w = hdac_widget_get(devinfo, nid);
7083	if (w == NULL || w->enable == 0)
7084		return;
7085
7086	if (depth == 0)
7087		device_printf(pdevinfo->dev, "%*s", 4, "");
7088	else
7089		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
7090	printf("nid=%d [%s]", w->nid, w->name);
7091
7092	if (depth > 0) {
7093		if (w->ossmask == 0) {
7094			printf("\n");
7095			return;
7096		}
7097		printf(" [src: %s]",
7098		    hdac_audio_ctl_ossmixer_mask2allname(
7099			w->ossmask, buf, sizeof(buf)));
7100		if (w->ossdev >= 0) {
7101			printf("\n");
7102			return;
7103		}
7104	}
7105	printf("\n");
7106
7107	for (i = 0; i < w->nconns; i++) {
7108		if (w->connsenable[i] == 0)
7109			continue;
7110		cw = hdac_widget_get(devinfo, w->conns[i]);
7111		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
7112			continue;
7113		if (printed == 0) {
7114			device_printf(pdevinfo->dev, "%*s  |\n", 4 + (depth) * 7, "");
7115			printed = 1;
7116		}
7117		hdac_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
7118	}
7119
7120}
7121
7122static void
7123hdac_dump_dac(struct hdac_pcm_devinfo *pdevinfo)
7124{
7125	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
7126	struct hdac_softc *sc = devinfo->codec->sc;
7127	struct hdac_audio_as *as;
7128	struct hdac_widget *w;
7129	int i, printed = 0;
7130
7131	if (pdevinfo->play < 0)
7132		return;
7133
7134	as = &devinfo->function.audio.as[sc->chans[pdevinfo->play].as];
7135	for (i = 0; i < 16; i++) {
7136		if (as->pins[i] <= 0)
7137			continue;
7138		w = hdac_widget_get(devinfo, as->pins[i]);
7139		if (w == NULL || w->enable == 0)
7140			continue;
7141		if (printed == 0) {
7142			printed = 1;
7143			device_printf(pdevinfo->dev, "\n");
7144			device_printf(pdevinfo->dev, "Playback:\n");
7145		}
7146		device_printf(pdevinfo->dev, "\n");
7147		hdac_dump_dst_nid(pdevinfo, as->pins[i], 0);
7148	}
7149}
7150
7151static void
7152hdac_dump_adc(struct hdac_pcm_devinfo *pdevinfo)
7153{
7154	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
7155	struct hdac_softc *sc = devinfo->codec->sc;
7156	struct hdac_widget *w;
7157	int i;
7158	int printed = 0;
7159
7160	if (pdevinfo->rec < 0)
7161		return;
7162
7163	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
7164		w = hdac_widget_get(devinfo, i);
7165		if (w == NULL || w->enable == 0)
7166			continue;
7167		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
7168			continue;
7169		if (w->bindas != sc->chans[pdevinfo->rec].as)
7170			continue;
7171		if (printed == 0) {
7172			printed = 1;
7173			device_printf(pdevinfo->dev, "\n");
7174			device_printf(pdevinfo->dev, "Record:\n");
7175		}
7176		device_printf(pdevinfo->dev, "\n");
7177		hdac_dump_dst_nid(pdevinfo, i, 0);
7178	}
7179}
7180
7181static void
7182hdac_dump_mix(struct hdac_pcm_devinfo *pdevinfo)
7183{
7184	struct hdac_devinfo *devinfo = pdevinfo->devinfo;
7185	struct hdac_widget *w;
7186	int i;
7187	int printed = 0;
7188
7189	if (pdevinfo->index != 0)
7190		return;
7191
7192	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
7193		w = hdac_widget_get(devinfo, i);
7194		if (w == NULL || w->enable == 0)
7195			continue;
7196		if (w->ossdev != SOUND_MIXER_IMIX)
7197			continue;
7198		if (printed == 0) {
7199			printed = 1;
7200			device_printf(pdevinfo->dev, "\n");
7201			device_printf(pdevinfo->dev, "Input Mix:\n");
7202		}
7203		device_printf(pdevinfo->dev, "\n");
7204		hdac_dump_dst_nid(pdevinfo, i, 0);
7205	}
7206}
7207
7208static void
7209hdac_dump_pcmchannels(struct hdac_pcm_devinfo *pdevinfo)
7210{
7211	struct hdac_softc *sc = pdevinfo->devinfo->codec->sc;
7212	nid_t *nids;
7213	int i;
7214
7215	if (pdevinfo->play >= 0) {
7216		i = pdevinfo->play;
7217		device_printf(pdevinfo->dev, "\n");
7218		device_printf(pdevinfo->dev, "Playback:\n");
7219		device_printf(pdevinfo->dev, "\n");
7220		hdac_dump_audio_formats(pdevinfo->dev, sc->chans[i].supp_stream_formats,
7221		    sc->chans[i].supp_pcm_size_rate);
7222		device_printf(pdevinfo->dev, "            DAC:");
7223		for (nids = sc->chans[i].io; *nids != -1; nids++)
7224			printf(" %d", *nids);
7225		printf("\n");
7226	}
7227	if (pdevinfo->rec >= 0) {
7228		i = pdevinfo->rec;
7229		device_printf(pdevinfo->dev, "\n");
7230		device_printf(pdevinfo->dev, "Record:\n");
7231		device_printf(pdevinfo->dev, "\n");
7232		hdac_dump_audio_formats(pdevinfo->dev, sc->chans[i].supp_stream_formats,
7233		    sc->chans[i].supp_pcm_size_rate);
7234		device_printf(pdevinfo->dev, "            ADC:");
7235		for (nids = sc->chans[i].io; *nids != -1; nids++)
7236			printf(" %d", *nids);
7237		printf("\n");
7238	}
7239}
7240
7241static void
7242hdac_release_resources(struct hdac_softc *sc)
7243{
7244        int i, j;
7245
7246	if (sc == NULL)
7247		return;
7248
7249	hdac_lock(sc);
7250	sc->polling = 0;
7251	sc->poll_ival = 0;
7252	callout_stop(&sc->poll_hda);
7253	callout_stop(&sc->poll_hdac);
7254	callout_stop(&sc->poll_jack);
7255	hdac_reset(sc, 0);
7256	hdac_unlock(sc);
7257	taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
7258	callout_drain(&sc->poll_hda);
7259	callout_drain(&sc->poll_hdac);
7260	callout_drain(&sc->poll_jack);
7261
7262	hdac_irq_free(sc);
7263
7264	for (i = 0; i < HDAC_CODEC_MAX; i++) {
7265		if (sc->codecs[i] == NULL)
7266			continue;
7267		for (j = 0; j < sc->codecs[i]->num_fgs; j++) {
7268			free(sc->codecs[i]->fgs[j].widget, M_HDAC);
7269			if (sc->codecs[i]->fgs[j].node_type ==
7270			    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) {
7271				free(sc->codecs[i]->fgs[j].function.audio.ctl,
7272				    M_HDAC);
7273				free(sc->codecs[i]->fgs[j].function.audio.as,
7274				    M_HDAC);
7275				free(sc->codecs[i]->fgs[j].function.audio.devs,
7276				    M_HDAC);
7277			}
7278		}
7279		free(sc->codecs[i]->fgs, M_HDAC);
7280		free(sc->codecs[i], M_HDAC);
7281		sc->codecs[i] = NULL;
7282	}
7283
7284	hdac_dma_free(sc, &sc->pos_dma);
7285	hdac_dma_free(sc, &sc->rirb_dma);
7286	hdac_dma_free(sc, &sc->corb_dma);
7287	for (i = 0; i < sc->num_chans; i++) {
7288    		if (sc->chans[i].blkcnt > 0)
7289    			hdac_dma_free(sc, &sc->chans[i].bdl_dma);
7290	}
7291	free(sc->chans, M_HDAC);
7292	if (sc->chan_dmat != NULL) {
7293		bus_dma_tag_destroy(sc->chan_dmat);
7294		sc->chan_dmat = NULL;
7295	}
7296	hdac_mem_free(sc);
7297	snd_mtxfree(sc->lock);
7298}
7299
7300/* This function surely going to make its way into upper level someday. */
7301static void
7302hdac_config_fetch(struct hdac_softc *sc, uint32_t *on, uint32_t *off)
7303{
7304	const char *res = NULL;
7305	int i = 0, j, k, len, inv;
7306
7307	if (on != NULL)
7308		*on = 0;
7309	if (off != NULL)
7310		*off = 0;
7311	if (sc == NULL)
7312		return;
7313	if (resource_string_value(device_get_name(sc->dev),
7314	    device_get_unit(sc->dev), "config", &res) != 0)
7315		return;
7316	if (!(res != NULL && strlen(res) > 0))
7317		return;
7318	HDA_BOOTVERBOSE(
7319		device_printf(sc->dev, "HDA Config:");
7320	);
7321	for (;;) {
7322		while (res[i] != '\0' &&
7323		    (res[i] == ',' || isspace(res[i]) != 0))
7324			i++;
7325		if (res[i] == '\0') {
7326			HDA_BOOTVERBOSE(
7327				printf("\n");
7328			);
7329			return;
7330		}
7331		j = i;
7332		while (res[j] != '\0' &&
7333		    !(res[j] == ',' || isspace(res[j]) != 0))
7334			j++;
7335		len = j - i;
7336		if (len > 2 && strncmp(res + i, "no", 2) == 0)
7337			inv = 2;
7338		else
7339			inv = 0;
7340		for (k = 0; len > inv && k < HDAC_QUIRKS_TAB_LEN; k++) {
7341			if (strncmp(res + i + inv,
7342			    hdac_quirks_tab[k].key, len - inv) != 0)
7343				continue;
7344			if (len - inv != strlen(hdac_quirks_tab[k].key))
7345				continue;
7346			HDA_BOOTVERBOSE(
7347				printf(" %s%s", (inv != 0) ? "no" : "",
7348				    hdac_quirks_tab[k].key);
7349			);
7350			if (inv == 0 && on != NULL)
7351				*on |= hdac_quirks_tab[k].value;
7352			else if (inv != 0 && off != NULL)
7353				*off |= hdac_quirks_tab[k].value;
7354			break;
7355		}
7356		i = j;
7357	}
7358}
7359
7360static int
7361sysctl_hdac_polling(SYSCTL_HANDLER_ARGS)
7362{
7363	struct hdac_softc *sc;
7364	device_t dev;
7365	uint32_t ctl;
7366	int err, val;
7367
7368	dev = oidp->oid_arg1;
7369	sc = device_get_softc(dev);
7370	if (sc == NULL)
7371		return (EINVAL);
7372	hdac_lock(sc);
7373	val = sc->polling;
7374	hdac_unlock(sc);
7375	err = sysctl_handle_int(oidp, &val, 0, req);
7376
7377	if (err != 0 || req->newptr == NULL)
7378		return (err);
7379	if (val < 0 || val > 1)
7380		return (EINVAL);
7381
7382	hdac_lock(sc);
7383	if (val != sc->polling) {
7384		if (val == 0) {
7385			callout_stop(&sc->poll_hda);
7386			callout_stop(&sc->poll_hdac);
7387			hdac_unlock(sc);
7388			callout_drain(&sc->poll_hda);
7389			callout_drain(&sc->poll_hdac);
7390			hdac_lock(sc);
7391			sc->polling = 0;
7392			ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
7393			ctl |= HDAC_INTCTL_GIE;
7394			HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
7395		} else {
7396			ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
7397			ctl &= ~HDAC_INTCTL_GIE;
7398			HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
7399			hdac_unlock(sc);
7400			taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
7401			hdac_lock(sc);
7402			sc->polling = 1;
7403			hdac_poll_reinit(sc);
7404			callout_reset(&sc->poll_hdac, 1, hdac_poll_callback, sc);
7405		}
7406	}
7407	hdac_unlock(sc);
7408
7409	return (err);
7410}
7411
7412static int
7413sysctl_hdac_polling_interval(SYSCTL_HANDLER_ARGS)
7414{
7415	struct hdac_softc *sc;
7416	device_t dev;
7417	int err, val;
7418
7419	dev = oidp->oid_arg1;
7420	sc = device_get_softc(dev);
7421	if (sc == NULL)
7422		return (EINVAL);
7423	hdac_lock(sc);
7424	val = ((uint64_t)sc->poll_ival * 1000) / hz;
7425	hdac_unlock(sc);
7426	err = sysctl_handle_int(oidp, &val, 0, req);
7427
7428	if (err != 0 || req->newptr == NULL)
7429		return (err);
7430
7431	if (val < 1)
7432		val = 1;
7433	if (val > 5000)
7434		val = 5000;
7435	val = ((uint64_t)val * hz) / 1000;
7436	if (val < 1)
7437		val = 1;
7438	if (val > (hz * 5))
7439		val = hz * 5;
7440
7441	hdac_lock(sc);
7442	sc->poll_ival = val;
7443	hdac_unlock(sc);
7444
7445	return (err);
7446}
7447
7448static int
7449sysctl_hdac_pindump(SYSCTL_HANDLER_ARGS)
7450{
7451	struct hdac_softc *sc;
7452	struct hdac_codec *codec;
7453	struct hdac_devinfo *devinfo;
7454	struct hdac_widget *w;
7455	device_t dev;
7456	uint32_t res, pincap, delay;
7457	int codec_index, fg_index;
7458	int i, err, val;
7459	nid_t cad;
7460
7461	dev = oidp->oid_arg1;
7462	sc = device_get_softc(dev);
7463	if (sc == NULL)
7464		return (EINVAL);
7465	val = 0;
7466	err = sysctl_handle_int(oidp, &val, 0, req);
7467	if (err != 0 || req->newptr == NULL || val == 0)
7468		return (err);
7469
7470	/* XXX: Temporary. For debugging. */
7471	if (val == 100) {
7472		hdac_suspend(dev);
7473		return (0);
7474	} else if (val == 101) {
7475		hdac_resume(dev);
7476		return (0);
7477	}
7478
7479	hdac_lock(sc);
7480	for (codec_index = 0; codec_index < HDAC_CODEC_MAX; codec_index++) {
7481		codec = sc->codecs[codec_index];
7482		if (codec == NULL)
7483			continue;
7484		cad = codec->cad;
7485		for (fg_index = 0; fg_index < codec->num_fgs; fg_index++) {
7486			devinfo = &codec->fgs[fg_index];
7487			if (devinfo->node_type !=
7488			    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
7489				continue;
7490
7491			device_printf(dev, "Dumping AFG cad=%d nid=%d pins:\n",
7492			    codec_index, devinfo->nid);
7493			for (i = devinfo->startnode; i < devinfo->endnode; i++) {
7494					w = hdac_widget_get(devinfo, i);
7495				if (w == NULL || w->type !=
7496				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
7497					continue;
7498				hdac_dump_pin_config(w, w->wclass.pin.config);
7499				pincap = w->wclass.pin.cap;
7500				device_printf(dev, "       Caps: %2s %3s %2s %4s %4s",
7501				    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
7502				    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
7503				    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
7504				    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
7505				    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
7506				if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
7507				    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
7508					if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
7509						delay = 0;
7510						hdac_command(sc,
7511						    HDA_CMD_SET_PIN_SENSE(cad, w->nid, 0), cad);
7512						do {
7513							res = hdac_command(sc,
7514							    HDA_CMD_GET_PIN_SENSE(cad, w->nid), cad);
7515							if (res != 0x7fffffff && res != 0xffffffff)
7516								break;
7517							DELAY(10);
7518						} while (++delay < 10000);
7519					} else {
7520						delay = 0;
7521						res = hdac_command(sc, HDA_CMD_GET_PIN_SENSE(cad,
7522						    w->nid), cad);
7523					}
7524					printf(" Sense: 0x%08x", res);
7525					if (delay > 0)
7526						printf(" delay %dus", delay * 10);
7527				}
7528				printf("\n");
7529			}
7530			device_printf(dev,
7531			    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
7532			    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->function.audio.gpio),
7533			    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->function.audio.gpio),
7534			    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->function.audio.gpio),
7535			    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->function.audio.gpio),
7536			    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->function.audio.gpio));
7537			if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->function.audio.gpio) > 0) {
7538				device_printf(dev, " GPI:");
7539				res = hdac_command(sc,
7540				    HDA_CMD_GET_GPI_DATA(cad, devinfo->nid), cad);
7541				printf(" data=0x%08x", res);
7542				res = hdac_command(sc,
7543				    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(cad, devinfo->nid),
7544				    cad);
7545				printf(" wake=0x%08x", res);
7546				res = hdac_command(sc,
7547				    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(cad, devinfo->nid),
7548				    cad);
7549				printf(" unsol=0x%08x", res);
7550				res = hdac_command(sc,
7551				    HDA_CMD_GET_GPI_STICKY_MASK(cad, devinfo->nid), cad);
7552				printf(" sticky=0x%08x\n", res);
7553			}
7554			if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->function.audio.gpio) > 0) {
7555				device_printf(dev, " GPO:");
7556				res = hdac_command(sc,
7557				    HDA_CMD_GET_GPO_DATA(cad, devinfo->nid), cad);
7558				printf(" data=0x%08x\n", res);
7559			}
7560			if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->function.audio.gpio) > 0) {
7561				device_printf(dev, "GPIO:");
7562				res = hdac_command(sc,
7563				    HDA_CMD_GET_GPIO_DATA(cad, devinfo->nid), cad);
7564				printf(" data=0x%08x", res);
7565				res = hdac_command(sc,
7566				    HDA_CMD_GET_GPIO_ENABLE_MASK(cad, devinfo->nid), cad);
7567				printf(" enable=0x%08x", res);
7568				res = hdac_command(sc,
7569				    HDA_CMD_GET_GPIO_DIRECTION(cad, devinfo->nid), cad);
7570				printf(" direction=0x%08x\n", res);
7571				res = hdac_command(sc,
7572				    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(cad, devinfo->nid), cad);
7573				device_printf(dev, "      wake=0x%08x", res);
7574				res = hdac_command(sc,
7575				    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(cad, devinfo->nid),
7576				    cad);
7577				printf("  unsol=0x%08x", res);
7578				res = hdac_command(sc,
7579				    HDA_CMD_GET_GPIO_STICKY_MASK(cad, devinfo->nid), cad);
7580				printf("    sticky=0x%08x\n", res);
7581			}
7582		}
7583	}
7584	hdac_unlock(sc);
7585	return (0);
7586}
7587
7588static void
7589hdac_attach2(void *arg)
7590{
7591	struct hdac_codec *codec;
7592	struct hdac_softc *sc;
7593	struct hdac_audio_ctl *ctl;
7594	uint32_t quirks_on, quirks_off;
7595	int codec_index, fg_index;
7596	int i, dmaalloc = 0;
7597	struct hdac_devinfo *devinfo;
7598
7599	sc = (struct hdac_softc *)arg;
7600
7601	hdac_config_fetch(sc, &quirks_on, &quirks_off);
7602
7603	HDA_BOOTHVERBOSE(
7604		device_printf(sc->dev, "HDA Config: on=0x%08x off=0x%08x\n",
7605		    quirks_on, quirks_off);
7606	);
7607
7608	hdac_lock(sc);
7609
7610	/* Remove ourselves from the config hooks */
7611	if (sc->intrhook.ich_func != NULL) {
7612		config_intrhook_disestablish(&sc->intrhook);
7613		sc->intrhook.ich_func = NULL;
7614	}
7615
7616	/* Start the corb and rirb engines */
7617	HDA_BOOTHVERBOSE(
7618		device_printf(sc->dev, "Starting CORB Engine...\n");
7619	);
7620	hdac_corb_start(sc);
7621	HDA_BOOTHVERBOSE(
7622		device_printf(sc->dev, "Starting RIRB Engine...\n");
7623	);
7624	hdac_rirb_start(sc);
7625
7626	HDA_BOOTHVERBOSE(
7627		device_printf(sc->dev,
7628		    "Enabling controller interrupt...\n");
7629	);
7630	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
7631	    HDAC_GCTL_UNSOL);
7632	if (sc->polling == 0) {
7633		HDAC_WRITE_4(&sc->mem, HDAC_INTCTL,
7634		    HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
7635	} else {
7636		callout_reset(&sc->poll_hdac, 1, hdac_poll_callback, sc);
7637	}
7638	DELAY(1000);
7639
7640	HDA_BOOTHVERBOSE(
7641		device_printf(sc->dev,
7642		    "Scanning HDA codecs ...\n");
7643	);
7644	hdac_scan_codecs(sc);
7645
7646	for (codec_index = 0; codec_index < HDAC_CODEC_MAX; codec_index++) {
7647		codec = sc->codecs[codec_index];
7648		if (codec == NULL)
7649			continue;
7650		for (fg_index = 0; fg_index < codec->num_fgs; fg_index++) {
7651			devinfo = &codec->fgs[fg_index];
7652			HDA_BOOTVERBOSE(
7653				device_printf(sc->dev, "\n");
7654				device_printf(sc->dev,
7655				    "Processing %s FG cad=%d nid=%d...\n",
7656				    (devinfo->node_type == HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) ? "audio":
7657				    (devinfo->node_type == HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_MODEM) ? "modem":
7658				    "unknown",
7659				    devinfo->codec->cad, devinfo->nid);
7660			);
7661			if (devinfo->node_type !=
7662			    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) {
7663				HDA_BOOTHVERBOSE(
7664					device_printf(sc->dev,
7665					    "Powering down...\n");
7666				);
7667				hdac_command(sc,
7668				    HDA_CMD_SET_POWER_STATE(codec->cad,
7669				    devinfo->nid, HDA_CMD_POWER_STATE_D3),
7670				    codec->cad);
7671				continue;
7672			}
7673
7674			HDA_BOOTHVERBOSE(
7675				device_printf(sc->dev, "Powering up...\n");
7676			);
7677			hdac_powerup(devinfo);
7678			HDA_BOOTHVERBOSE(
7679				device_printf(sc->dev, "Parsing audio FG...\n");
7680			);
7681			hdac_audio_parse(devinfo);
7682			HDA_BOOTHVERBOSE(
7683				device_printf(sc->dev, "Parsing Ctls...\n");
7684			);
7685		    	hdac_audio_ctl_parse(devinfo);
7686			HDA_BOOTHVERBOSE(
7687				device_printf(sc->dev, "Parsing vendor patch...\n");
7688			);
7689			hdac_vendor_patch_parse(devinfo);
7690			devinfo->function.audio.quirks |= quirks_on;
7691			devinfo->function.audio.quirks &= ~quirks_off;
7692
7693			HDA_BOOTHVERBOSE(
7694				device_printf(sc->dev, "Disabling nonaudio...\n");
7695			);
7696			hdac_audio_disable_nonaudio(devinfo);
7697			HDA_BOOTHVERBOSE(
7698				device_printf(sc->dev, "Disabling useless...\n");
7699			);
7700			hdac_audio_disable_useless(devinfo);
7701			HDA_BOOTVERBOSE(
7702				device_printf(sc->dev, "Patched pins configuration:\n");
7703				hdac_dump_pin_configs(devinfo);
7704			);
7705			HDA_BOOTHVERBOSE(
7706				device_printf(sc->dev, "Parsing pin associations...\n");
7707			);
7708			hdac_audio_as_parse(devinfo);
7709			HDA_BOOTHVERBOSE(
7710				device_printf(sc->dev, "Building AFG tree...\n");
7711			);
7712			hdac_audio_build_tree(devinfo);
7713			HDA_BOOTHVERBOSE(
7714				device_printf(sc->dev, "Disabling unassociated "
7715				    "widgets...\n");
7716			);
7717			hdac_audio_disable_unas(devinfo);
7718			HDA_BOOTHVERBOSE(
7719				device_printf(sc->dev, "Disabling nonselected "
7720				    "inputs...\n");
7721			);
7722			hdac_audio_disable_notselected(devinfo);
7723			HDA_BOOTHVERBOSE(
7724				device_printf(sc->dev, "Disabling useless...\n");
7725			);
7726			hdac_audio_disable_useless(devinfo);
7727			HDA_BOOTHVERBOSE(
7728				device_printf(sc->dev, "Disabling "
7729				    "crossassociatement connections...\n");
7730			);
7731			hdac_audio_disable_crossas(devinfo);
7732			HDA_BOOTHVERBOSE(
7733				device_printf(sc->dev, "Disabling useless...\n");
7734			);
7735			hdac_audio_disable_useless(devinfo);
7736			HDA_BOOTHVERBOSE(
7737				device_printf(sc->dev, "Binding associations to channels...\n");
7738			);
7739			hdac_audio_bind_as(devinfo);
7740			HDA_BOOTHVERBOSE(
7741				device_printf(sc->dev, "Assigning names to signal sources...\n");
7742			);
7743			hdac_audio_assign_names(devinfo);
7744			HDA_BOOTHVERBOSE(
7745				device_printf(sc->dev, "Assigning mixers to the tree...\n");
7746			);
7747			hdac_audio_assign_mixers(devinfo);
7748			HDA_BOOTHVERBOSE(
7749				device_printf(sc->dev, "Preparing pin controls...\n");
7750			);
7751			hdac_audio_prepare_pin_ctrl(devinfo);
7752			HDA_BOOTHVERBOSE(
7753				device_printf(sc->dev, "AFG commit...\n");
7754		    	);
7755			hdac_audio_commit(devinfo);
7756		    	HDA_BOOTHVERBOSE(
7757				device_printf(sc->dev, "HP switch init...\n");
7758			);
7759			hdac_hp_switch_init(devinfo);
7760
7761			if ((devinfo->function.audio.quirks & HDA_QUIRK_DMAPOS) &&
7762			    dmaalloc == 0) {
7763				if (hdac_dma_alloc(sc, &sc->pos_dma,
7764				    (sc->num_iss + sc->num_oss + sc->num_bss) * 8) != 0) {
7765					HDA_BOOTVERBOSE(
7766						device_printf(sc->dev, "Failed to "
7767						    "allocate DMA pos buffer "
7768						    "(non-fatal)\n");
7769					);
7770				} else
7771					dmaalloc = 1;
7772			}
7773
7774		    	HDA_BOOTHVERBOSE(
7775				device_printf(sc->dev, "Creating PCM devices...\n");
7776			);
7777			hdac_create_pcms(devinfo);
7778
7779			HDA_BOOTVERBOSE(
7780				if (devinfo->function.audio.quirks != 0) {
7781					device_printf(sc->dev, "FG config/quirks:");
7782					for (i = 0; i < HDAC_QUIRKS_TAB_LEN; i++) {
7783						if ((devinfo->function.audio.quirks &
7784						    hdac_quirks_tab[i].value) ==
7785						    hdac_quirks_tab[i].value)
7786							printf(" %s", hdac_quirks_tab[i].key);
7787					}
7788					printf("\n");
7789				}
7790
7791				device_printf(sc->dev, "\n");
7792				device_printf(sc->dev, "+-------------------+\n");
7793				device_printf(sc->dev, "| DUMPING HDA NODES |\n");
7794				device_printf(sc->dev, "+-------------------+\n");
7795				hdac_dump_nodes(devinfo);
7796			);
7797
7798			HDA_BOOTHVERBOSE(
7799				device_printf(sc->dev, "\n");
7800				device_printf(sc->dev, "+------------------------+\n");
7801				device_printf(sc->dev, "| DUMPING HDA AMPLIFIERS |\n");
7802				device_printf(sc->dev, "+------------------------+\n");
7803				device_printf(sc->dev, "\n");
7804				i = 0;
7805				while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
7806					device_printf(sc->dev, "%3d: nid %3d %s (%s) index %d", i,
7807					    (ctl->widget != NULL) ? ctl->widget->nid : -1,
7808					    (ctl->ndir == HDA_CTL_IN)?"in ":"out",
7809					    (ctl->dir == HDA_CTL_IN)?"in ":"out",
7810					    ctl->index);
7811					if (ctl->childwidget != NULL)
7812						printf(" cnid %3d", ctl->childwidget->nid);
7813					else
7814						printf("         ");
7815					printf(" ossmask=0x%08x\n",
7816					    ctl->ossmask);
7817					device_printf(sc->dev,
7818					    "       mute: %d step: %3d size: %3d off: %3d%s\n",
7819					    ctl->mute, ctl->step, ctl->size, ctl->offset,
7820					    (ctl->enable == 0) ? " [DISABLED]" :
7821					    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
7822				}
7823			);
7824		}
7825	}
7826	hdac_unlock(sc);
7827
7828	HDA_BOOTVERBOSE(
7829		device_printf(sc->dev, "\n");
7830	);
7831
7832	bus_generic_attach(sc->dev);
7833
7834	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
7835	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
7836	    "polling", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
7837	    sysctl_hdac_polling, "I", "Enable polling mode");
7838	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
7839	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
7840	    "polling_interval", CTLTYPE_INT | CTLFLAG_RW, sc->dev,
7841	    sizeof(sc->dev), sysctl_hdac_polling_interval, "I",
7842	    "Controller/Jack Sense polling interval (1-1000 ms)");
7843	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
7844	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
7845	    "pindump", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
7846	    sysctl_hdac_pindump, "I", "Dump pin states/data");
7847}
7848
7849/****************************************************************************
7850 * int hdac_suspend(device_t)
7851 *
7852 * Suspend and power down HDA bus and codecs.
7853 ****************************************************************************/
7854static int
7855hdac_suspend(device_t dev)
7856{
7857	struct hdac_softc *sc;
7858	struct hdac_codec *codec;
7859	struct hdac_devinfo *devinfo;
7860	int codec_index, fg_index, i;
7861
7862	HDA_BOOTHVERBOSE(
7863		device_printf(dev, "Suspend...\n");
7864	);
7865
7866	sc = device_get_softc(dev);
7867	hdac_lock(sc);
7868
7869	HDA_BOOTHVERBOSE(
7870		device_printf(dev, "Stop streams...\n");
7871	);
7872	for (i = 0; i < sc->num_chans; i++) {
7873		if (sc->chans[i].flags & HDAC_CHN_RUNNING) {
7874			sc->chans[i].flags |= HDAC_CHN_SUSPEND;
7875			hdac_channel_stop(sc, &sc->chans[i]);
7876		}
7877	}
7878
7879	for (codec_index = 0; codec_index < HDAC_CODEC_MAX; codec_index++) {
7880		codec = sc->codecs[codec_index];
7881		if (codec == NULL)
7882			continue;
7883		for (fg_index = 0; fg_index < codec->num_fgs; fg_index++) {
7884			devinfo = &codec->fgs[fg_index];
7885			HDA_BOOTHVERBOSE(
7886				device_printf(dev,
7887				    "Power down 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		}
7896	}
7897
7898	HDA_BOOTHVERBOSE(
7899		device_printf(dev, "Reset controller...\n");
7900	);
7901	callout_stop(&sc->poll_hda);
7902	callout_stop(&sc->poll_hdac);
7903	callout_stop(&sc->poll_jack);
7904	hdac_reset(sc, 0);
7905	hdac_unlock(sc);
7906	taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
7907	callout_drain(&sc->poll_hda);
7908	callout_drain(&sc->poll_hdac);
7909	callout_drain(&sc->poll_jack);
7910
7911	HDA_BOOTHVERBOSE(
7912		device_printf(dev, "Suspend done\n");
7913	);
7914
7915	return (0);
7916}
7917
7918/****************************************************************************
7919 * int hdac_resume(device_t)
7920 *
7921 * Powerup and restore HDA bus and codecs state.
7922 ****************************************************************************/
7923static int
7924hdac_resume(device_t dev)
7925{
7926	struct hdac_softc *sc;
7927	struct hdac_codec *codec;
7928	struct hdac_devinfo *devinfo;
7929	int codec_index, fg_index, i;
7930
7931	HDA_BOOTHVERBOSE(
7932		device_printf(dev, "Resume...\n");
7933	);
7934
7935	sc = device_get_softc(dev);
7936	hdac_lock(sc);
7937
7938	/* Quiesce everything */
7939	HDA_BOOTHVERBOSE(
7940		device_printf(dev, "Reset controller...\n");
7941	);
7942	hdac_reset(sc, 1);
7943
7944	/* Initialize the CORB and RIRB */
7945	hdac_corb_init(sc);
7946	hdac_rirb_init(sc);
7947
7948	/* Start the corb and rirb engines */
7949	HDA_BOOTHVERBOSE(
7950		device_printf(dev, "Starting CORB Engine...\n");
7951	);
7952	hdac_corb_start(sc);
7953	HDA_BOOTHVERBOSE(
7954		device_printf(dev, "Starting RIRB Engine...\n");
7955	);
7956	hdac_rirb_start(sc);
7957
7958	HDA_BOOTHVERBOSE(
7959		device_printf(dev,
7960		    "Enabling controller interrupt...\n");
7961	);
7962	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
7963	    HDAC_GCTL_UNSOL);
7964	if (sc->polling == 0) {
7965		HDAC_WRITE_4(&sc->mem, HDAC_INTCTL,
7966		    HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
7967	} else {
7968		callout_reset(&sc->poll_hdac, 1, hdac_poll_callback, sc);
7969	}
7970	DELAY(1000);
7971
7972	for (codec_index = 0; codec_index < HDAC_CODEC_MAX; codec_index++) {
7973		codec = sc->codecs[codec_index];
7974		if (codec == NULL)
7975			continue;
7976		for (fg_index = 0; fg_index < codec->num_fgs; fg_index++) {
7977			devinfo = &codec->fgs[fg_index];
7978			if (devinfo->node_type !=
7979			    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) {
7980				HDA_BOOTHVERBOSE(
7981					device_printf(dev,
7982					    "Power down unsupported non-audio FG"
7983					    " cad=%d nid=%d to the D3 state...\n",
7984					    codec->cad, devinfo->nid);
7985				);
7986				hdac_command(sc,
7987				    HDA_CMD_SET_POWER_STATE(codec->cad,
7988				    devinfo->nid, HDA_CMD_POWER_STATE_D3),
7989				    codec->cad);
7990				continue;
7991			}
7992
7993			HDA_BOOTHVERBOSE(
7994				device_printf(dev,
7995				    "Power up audio FG cad=%d nid=%d...\n",
7996				    devinfo->codec->cad, devinfo->nid);
7997			);
7998			hdac_powerup(devinfo);
7999			HDA_BOOTHVERBOSE(
8000				device_printf(dev, "AFG commit...\n");
8001		    	);
8002			hdac_audio_commit(devinfo);
8003		    	HDA_BOOTHVERBOSE(
8004				device_printf(dev, "HP switch init...\n");
8005			);
8006			hdac_hp_switch_init(devinfo);
8007
8008			hdac_unlock(sc);
8009			for (i = 0; i < devinfo->function.audio.num_devs; i++) {
8010				struct hdac_pcm_devinfo *pdevinfo =
8011				    &devinfo->function.audio.devs[i];
8012				HDA_BOOTHVERBOSE(
8013					device_printf(pdevinfo->dev,
8014					    "OSS mixer reinitialization...\n");
8015				);
8016				if (mixer_reinit(pdevinfo->dev) == -1)
8017					device_printf(pdevinfo->dev,
8018					    "unable to reinitialize the mixer\n");
8019			}
8020			hdac_lock(sc);
8021		}
8022	}
8023
8024	HDA_BOOTHVERBOSE(
8025		device_printf(dev, "Start streams...\n");
8026	);
8027	for (i = 0; i < sc->num_chans; i++) {
8028		if (sc->chans[i].flags & HDAC_CHN_SUSPEND) {
8029			sc->chans[i].flags &= ~HDAC_CHN_SUSPEND;
8030			hdac_channel_start(sc, &sc->chans[i]);
8031		}
8032	}
8033
8034	hdac_unlock(sc);
8035
8036	HDA_BOOTHVERBOSE(
8037		device_printf(dev, "Resume done\n");
8038	);
8039
8040	return (0);
8041}
8042/****************************************************************************
8043 * int hdac_detach(device_t)
8044 *
8045 * Detach and free up resources utilized by the hdac device.
8046 ****************************************************************************/
8047static int
8048hdac_detach(device_t dev)
8049{
8050	struct hdac_softc *sc;
8051	device_t *devlist;
8052	int i, devcount, error;
8053
8054	if ((error = device_get_children(dev, &devlist, &devcount)) != 0)
8055		return (error);
8056	for (i = 0; i < devcount; i++) {
8057		if ((error = device_delete_child(dev, devlist[i])) != 0) {
8058			free(devlist, M_TEMP);
8059			return (error);
8060		}
8061	}
8062	free(devlist, M_TEMP);
8063
8064	sc = device_get_softc(dev);
8065	hdac_release_resources(sc);
8066
8067	return (0);
8068}
8069
8070static int
8071hdac_print_child(device_t dev, device_t child)
8072{
8073	struct hdac_pcm_devinfo *pdevinfo =
8074	    (struct hdac_pcm_devinfo *)device_get_ivars(child);
8075	int retval;
8076
8077	retval = bus_print_child_header(dev, child);
8078	retval += printf(" at cad %d nid %d",
8079	    pdevinfo->devinfo->codec->cad, pdevinfo->devinfo->nid);
8080	retval += bus_print_child_footer(dev, child);
8081
8082	return (retval);
8083}
8084
8085static device_method_t hdac_methods[] = {
8086	/* device interface */
8087	DEVMETHOD(device_probe,		hdac_probe),
8088	DEVMETHOD(device_attach,	hdac_attach),
8089	DEVMETHOD(device_detach,	hdac_detach),
8090	DEVMETHOD(device_suspend,	hdac_suspend),
8091	DEVMETHOD(device_resume,	hdac_resume),
8092	/* Bus interface */
8093	DEVMETHOD(bus_print_child,	hdac_print_child),
8094	{ 0, 0 }
8095};
8096
8097static driver_t hdac_driver = {
8098	"hdac",
8099	hdac_methods,
8100	sizeof(struct hdac_softc),
8101};
8102
8103static devclass_t hdac_devclass;
8104
8105DRIVER_MODULE(snd_hda, pci, hdac_driver, hdac_devclass, 0, 0);
8106MODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
8107MODULE_VERSION(snd_hda, 1);
8108
8109static int
8110hdac_pcm_probe(device_t dev)
8111{
8112	struct hdac_pcm_devinfo *pdevinfo =
8113	    (struct hdac_pcm_devinfo *)device_get_ivars(dev);
8114	char buf[128];
8115
8116	snprintf(buf, sizeof(buf), "HDA %s PCM #%d %s",
8117	    hdac_codec_name(pdevinfo->devinfo->codec),
8118	    pdevinfo->index,
8119	    (pdevinfo->digital == 3)?"DisplayPort":
8120	    ((pdevinfo->digital == 2)?"HDMI":
8121	    ((pdevinfo->digital)?"Digital":"Analog")));
8122	device_set_desc_copy(dev, buf);
8123	return (0);
8124}
8125
8126static int
8127hdac_pcm_attach(device_t dev)
8128{
8129	struct hdac_pcm_devinfo *pdevinfo =
8130	    (struct hdac_pcm_devinfo *)device_get_ivars(dev);
8131	struct hdac_softc *sc = pdevinfo->devinfo->codec->sc;
8132	char status[SND_STATUSLEN];
8133	int i;
8134
8135	pdevinfo->chan_size = pcm_getbuffersize(dev,
8136	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
8137
8138	HDA_BOOTVERBOSE(
8139		device_printf(dev, "+--------------------------------------+\n");
8140		device_printf(dev, "| DUMPING PCM Playback/Record Channels |\n");
8141		device_printf(dev, "+--------------------------------------+\n");
8142		hdac_dump_pcmchannels(pdevinfo);
8143		device_printf(dev, "\n");
8144		device_printf(dev, "+-------------------------------+\n");
8145		device_printf(dev, "| DUMPING Playback/Record Paths |\n");
8146		device_printf(dev, "+-------------------------------+\n");
8147		hdac_dump_dac(pdevinfo);
8148		hdac_dump_adc(pdevinfo);
8149		hdac_dump_mix(pdevinfo);
8150		device_printf(dev, "\n");
8151		device_printf(dev, "+-------------------------+\n");
8152		device_printf(dev, "| DUMPING Volume Controls |\n");
8153		device_printf(dev, "+-------------------------+\n");
8154		hdac_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
8155		hdac_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
8156		hdac_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
8157		hdac_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
8158		hdac_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
8159		hdac_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
8160		hdac_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
8161		hdac_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
8162		hdac_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
8163		hdac_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
8164		hdac_dump_ctls(pdevinfo, NULL, 0);
8165		device_printf(dev, "\n");
8166	);
8167
8168	if (resource_int_value(device_get_name(dev),
8169	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
8170		i &= HDA_BLK_ALIGN;
8171		if (i < HDA_BLK_MIN)
8172			i = HDA_BLK_MIN;
8173		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
8174		i = 0;
8175		while (pdevinfo->chan_blkcnt >> i)
8176			i++;
8177		pdevinfo->chan_blkcnt = 1 << (i - 1);
8178		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
8179			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
8180		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
8181			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
8182	} else
8183		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
8184
8185	/*
8186	 * We don't register interrupt handler with snd_setup_intr
8187	 * in pcm device. Mark pcm device as MPSAFE manually.
8188	 */
8189	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
8190
8191	HDA_BOOTHVERBOSE(
8192		device_printf(dev, "OSS mixer initialization...\n");
8193	);
8194	if (mixer_init(dev, &hdac_audio_ctl_ossmixer_class, pdevinfo) != 0)
8195		device_printf(dev, "Can't register mixer\n");
8196
8197	HDA_BOOTHVERBOSE(
8198		device_printf(dev, "Registering PCM channels...\n");
8199	);
8200	if (pcm_register(dev, pdevinfo, (pdevinfo->play >= 0)?1:0,
8201	    (pdevinfo->rec >= 0)?1:0) != 0)
8202		device_printf(dev, "Can't register PCM\n");
8203
8204	pdevinfo->registered++;
8205
8206	if (pdevinfo->play >= 0)
8207		pcm_addchan(dev, PCMDIR_PLAY, &hdac_channel_class, pdevinfo);
8208	if (pdevinfo->rec >= 0)
8209		pcm_addchan(dev, PCMDIR_REC, &hdac_channel_class, pdevinfo);
8210
8211	snprintf(status, SND_STATUSLEN, "at cad %d nid %d on %s %s",
8212	    pdevinfo->devinfo->codec->cad, pdevinfo->devinfo->nid,
8213	    device_get_nameunit(sc->dev), PCM_KLDSTRING(snd_hda));
8214	pcm_setstatus(dev, status);
8215
8216	return (0);
8217}
8218
8219static int
8220hdac_pcm_detach(device_t dev)
8221{
8222	struct hdac_pcm_devinfo *pdevinfo =
8223	    (struct hdac_pcm_devinfo *)device_get_ivars(dev);
8224	int err;
8225
8226	if (pdevinfo->registered > 0) {
8227		err = pcm_unregister(dev);
8228		if (err != 0)
8229			return (err);
8230	}
8231
8232	return (0);
8233}
8234
8235static device_method_t hdac_pcm_methods[] = {
8236	/* device interface */
8237	DEVMETHOD(device_probe,		hdac_pcm_probe),
8238	DEVMETHOD(device_attach,	hdac_pcm_attach),
8239	DEVMETHOD(device_detach,	hdac_pcm_detach),
8240	{ 0, 0 }
8241};
8242
8243static driver_t hdac_pcm_driver = {
8244	"pcm",
8245	hdac_pcm_methods,
8246	PCM_SOFTC_SIZE,
8247};
8248
8249DRIVER_MODULE(snd_hda_pcm, hdac, hdac_pcm_driver, pcm_devclass, 0, 0);
8250
8251