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