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