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