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