hdac.c revision 170289
1/*-
2 * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3 * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28/*
29 * Intel High Definition Audio (Controller) driver for FreeBSD. Be advised
30 * that this driver still in its early stage, and possible of rewrite are
31 * pretty much guaranteed. There are supposedly several distinct parent/child
32 * busses to make this "perfect", but as for now and for the sake of
33 * simplicity, everything is gobble up within single source.
34 *
35 * List of subsys:
36 *     1) HDA Controller support
37 *     2) HDA Codecs support, which may include
38 *        - HDA
39 *        - Modem
40 *        - HDMI
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 *    *                                                                 *
65 *    *   ....and various people from freebsd-multimedia@FreeBSD.org    *
66 *    *                                                                 *
67 *    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
68 */
69
70#include <sys/ctype.h>
71
72#include <dev/sound/pcm/sound.h>
73#include <dev/pci/pcireg.h>
74#include <dev/pci/pcivar.h>
75
76#include <dev/sound/pci/hda/hdac_private.h>
77#include <dev/sound/pci/hda/hdac_reg.h>
78#include <dev/sound/pci/hda/hda_reg.h>
79#include <dev/sound/pci/hda/hdac.h>
80
81#include "mixer_if.h"
82
83#define HDA_DRV_TEST_REV	"20070505_0044"
84#define HDA_WIDGET_PARSER_REV	1
85
86SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/hda/hdac.c 170289 2007-06-04 18:25:08Z dwmalone $");
87
88#define HDA_BOOTVERBOSE(stmt)	do {			\
89	if (bootverbose != 0 || snd_verbose > 3) {	\
90		stmt					\
91	}						\
92} while(0)
93
94#if 1
95#undef HDAC_INTR_EXTRA
96#define HDAC_INTR_EXTRA		1
97#endif
98
99#define hdac_lock(sc)		snd_mtxlock((sc)->lock)
100#define hdac_unlock(sc)		snd_mtxunlock((sc)->lock)
101#define hdac_lockassert(sc)	snd_mtxassert((sc)->lock)
102#define hdac_lockowned(sc)	mtx_owned((sc)->lock)
103
104#define HDA_FLAG_MATCH(fl, v)	(((fl) & (v)) == (v))
105#define HDA_DEV_MATCH(fl, v)	((fl) == (v) || \
106				(fl) == 0xffffffff || \
107				(((fl) & 0xffff0000) == 0xffff0000 && \
108				((fl) & 0x0000ffff) == ((v) & 0x0000ffff)) || \
109				(((fl) & 0x0000ffff) == 0x0000ffff && \
110				((fl) & 0xffff0000) == ((v) & 0xffff0000)))
111#define HDA_MATCH_ALL		0xffffffff
112#define HDAC_INVALID		0xffffffff
113
114/* Default controller / jack sense poll: 250ms */
115#define HDAC_POLL_INTERVAL	max(hz >> 2, 1)
116
117#define HDA_MODEL_CONSTRUCT(vendor, model)	\
118		(((uint32_t)(model) << 16) | ((vendor##_VENDORID) & 0xffff))
119
120/* Controller models */
121
122/* Intel */
123#define INTEL_VENDORID		0x8086
124#define HDA_INTEL_82801F	HDA_MODEL_CONSTRUCT(INTEL, 0x2668)
125#define HDA_INTEL_82801G	HDA_MODEL_CONSTRUCT(INTEL, 0x27d8)
126#define HDA_INTEL_82801H	HDA_MODEL_CONSTRUCT(INTEL, 0x284b)
127#define HDA_INTEL_63XXESB	HDA_MODEL_CONSTRUCT(INTEL, 0x269a)
128#define HDA_INTEL_ALL		HDA_MODEL_CONSTRUCT(INTEL, 0xffff)
129
130/* Nvidia */
131#define NVIDIA_VENDORID		0x10de
132#define HDA_NVIDIA_MCP51	HDA_MODEL_CONSTRUCT(NVIDIA, 0x026c)
133#define HDA_NVIDIA_MCP55	HDA_MODEL_CONSTRUCT(NVIDIA, 0x0371)
134#define HDA_NVIDIA_MCP61A	HDA_MODEL_CONSTRUCT(NVIDIA, 0x03e4)
135#define HDA_NVIDIA_MCP61B	HDA_MODEL_CONSTRUCT(NVIDIA, 0x03f0)
136#define HDA_NVIDIA_MCP65A	HDA_MODEL_CONSTRUCT(NVIDIA, 0x044a)
137#define HDA_NVIDIA_MCP65B	HDA_MODEL_CONSTRUCT(NVIDIA, 0x044b)
138#define HDA_NVIDIA_ALL		HDA_MODEL_CONSTRUCT(NVIDIA, 0xffff)
139
140/* ATI */
141#define ATI_VENDORID		0x1002
142#define HDA_ATI_SB450		HDA_MODEL_CONSTRUCT(ATI, 0x437b)
143#define HDA_ATI_SB600		HDA_MODEL_CONSTRUCT(ATI, 0x4383)
144#define HDA_ATI_ALL		HDA_MODEL_CONSTRUCT(ATI, 0xffff)
145
146/* VIA */
147#define VIA_VENDORID		0x1106
148#define HDA_VIA_VT82XX		HDA_MODEL_CONSTRUCT(VIA, 0x3288)
149#define HDA_VIA_ALL		HDA_MODEL_CONSTRUCT(VIA, 0xffff)
150
151/* SiS */
152#define SIS_VENDORID		0x1039
153#define HDA_SIS_966		HDA_MODEL_CONSTRUCT(SIS, 0x7502)
154#define HDA_SIS_ALL		HDA_MODEL_CONSTRUCT(SIS, 0xffff)
155
156/* OEM/subvendors */
157
158/* Intel */
159#define INTEL_D101GGC_SUBVENDOR	HDA_MODEL_CONSTRUCT(INTEL, 0xd600)
160
161/* HP/Compaq */
162#define HP_VENDORID		0x103c
163#define HP_V3000_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30b5)
164#define HP_NX7400_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30a2)
165#define HP_NX6310_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30aa)
166#define HP_NX6325_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30b0)
167#define HP_XW4300_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x3013)
168#define HP_3010_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x3010)
169#define HP_DV5000_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0x30a5)
170#define HP_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(HP, 0xffff)
171/* What is wrong with XN 2563 anyway? (Got the picture ?) */
172#define HP_NX6325_SUBVENDORX	0x103c30b0
173
174/* Dell */
175#define DELL_VENDORID		0x1028
176#define DELL_D820_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x01cc)
177#define DELL_I1300_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x01c9)
178#define DELL_XPSM1210_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x01d7)
179#define DELL_OPLX745_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0x01da)
180#define DELL_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(DELL, 0xffff)
181
182/* Clevo */
183#define CLEVO_VENDORID		0x1558
184#define CLEVO_D900T_SUBVENDOR	HDA_MODEL_CONSTRUCT(CLEVO, 0x0900)
185#define CLEVO_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(CLEVO, 0xffff)
186
187/* Acer */
188#define ACER_VENDORID		0x1025
189#define ACER_A5050_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0x010f)
190#define ACER_3681WXM_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0x0110)
191#define ACER_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(ACER, 0xffff)
192
193/* Asus */
194#define ASUS_VENDORID		0x1043
195#define ASUS_M5200_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1993)
196#define ASUS_U5F_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1263)
197#define ASUS_A8JC_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1153)
198#define ASUS_P1AH2_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x81cb)
199#define ASUS_A7M_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1323)
200#define ASUS_A7T_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x13c2)
201#define ASUS_W6F_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1263)
202#define ASUS_W2J_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1971)
203#define ASUS_F3JC_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x1338)
204#define ASUS_M2N_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x8234)
205#define ASUS_M2NPVMX_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0x81cb)
206#define ASUS_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(ASUS, 0xffff)
207
208/* IBM / Lenovo */
209#define IBM_VENDORID		0x1014
210#define IBM_M52_SUBVENDOR	HDA_MODEL_CONSTRUCT(IBM, 0x02f6)
211#define IBM_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(IBM, 0xffff)
212
213/* Lenovo */
214#define LENOVO_VENDORID		0x17aa
215#define LENOVO_3KN100_SUBVENDOR	HDA_MODEL_CONSTRUCT(LENOVO, 0x2066)
216#define LENOVO_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(LENOVO, 0xffff)
217
218/* Samsung */
219#define SAMSUNG_VENDORID	0x144d
220#define SAMSUNG_Q1_SUBVENDOR	HDA_MODEL_CONSTRUCT(SAMSUNG, 0xc027)
221#define SAMSUNG_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(SAMSUNG, 0xffff)
222
223/* Medion ? */
224#define MEDION_VENDORID			0x161f
225#define MEDION_MD95257_SUBVENDOR	HDA_MODEL_CONSTRUCT(MEDION, 0x203d)
226#define MEDION_ALL_SUBVENDOR		HDA_MODEL_CONSTRUCT(MEDION, 0xffff)
227
228/*
229 * Apple Intel MacXXXX seems using Sigmatel codec/vendor id
230 * instead of their own, which is beyond my comprehension
231 * (see HDA_CODEC_STAC9221 below).
232 */
233#define APPLE_INTEL_MAC		0x76808384
234
235/* LG Electronics */
236#define LG_VENDORID		0x1854
237#define LG_LW20_SUBVENDOR	HDA_MODEL_CONSTRUCT(LG, 0x0018)
238#define LG_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(LG, 0xffff)
239
240/* Fujitsu Siemens */
241#define FS_VENDORID		0x1734
242#define FS_PA1510_SUBVENDOR	HDA_MODEL_CONSTRUCT(FS, 0x10b8)
243#define FS_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(FS, 0xffff)
244
245/* Toshiba */
246#define TOSHIBA_VENDORID	0x1179
247#define TOSHIBA_U200_SUBVENDOR	HDA_MODEL_CONSTRUCT(TOSHIBA, 0x0001)
248#define TOSHIBA_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(TOSHIBA, 0xffff)
249
250/* Micro-Star International (MSI) */
251#define MSI_VENDORID		0x1462
252#define MSI_MS1034_SUBVENDOR	HDA_MODEL_CONSTRUCT(MSI, 0x0349)
253#define MSI_ALL_SUBVENDOR	HDA_MODEL_CONSTRUCT(MSI, 0xffff)
254
255/* Uniwill ? */
256#define UNIWILL_VENDORID	0x1584
257#define UNIWILL_9075_SUBVENDOR	HDA_MODEL_CONSTRUCT(UNIWILL, 0x9075)
258
259
260/* Misc constants.. */
261#define HDA_AMP_MUTE_DEFAULT	(0xffffffff)
262#define HDA_AMP_MUTE_NONE	(0)
263#define HDA_AMP_MUTE_LEFT	(1 << 0)
264#define HDA_AMP_MUTE_RIGHT	(1 << 1)
265#define HDA_AMP_MUTE_ALL	(HDA_AMP_MUTE_LEFT | HDA_AMP_MUTE_RIGHT)
266
267#define HDA_AMP_LEFT_MUTED(v)	((v) & (HDA_AMP_MUTE_LEFT))
268#define HDA_AMP_RIGHT_MUTED(v)	(((v) & HDA_AMP_MUTE_RIGHT) >> 1)
269
270#define HDA_DAC_PATH	(1 << 0)
271#define HDA_ADC_PATH	(1 << 1)
272#define HDA_ADC_RECSEL	(1 << 2)
273
274#define HDA_DAC_LOCKED	(1 << 3)
275#define HDA_ADC_LOCKED	(1 << 4)
276
277#define HDA_CTL_OUT	(1 << 0)
278#define HDA_CTL_IN	(1 << 1)
279#define HDA_CTL_BOTH	(HDA_CTL_IN | HDA_CTL_OUT)
280
281#define HDA_GPIO_MAX		8
282/* 0 - 7 = GPIO , 8 = Flush */
283#define HDA_QUIRK_GPIO0		(1 << 0)
284#define HDA_QUIRK_GPIO1		(1 << 1)
285#define HDA_QUIRK_GPIO2		(1 << 2)
286#define HDA_QUIRK_GPIO3		(1 << 3)
287#define HDA_QUIRK_GPIO4		(1 << 4)
288#define HDA_QUIRK_GPIO5		(1 << 5)
289#define HDA_QUIRK_GPIO6		(1 << 6)
290#define HDA_QUIRK_GPIO7		(1 << 7)
291#define HDA_QUIRK_GPIOFLUSH	(1 << 8)
292
293/* 9 - 25 = anything else */
294#define HDA_QUIRK_SOFTPCMVOL	(1 << 9)
295#define HDA_QUIRK_FIXEDRATE	(1 << 10)
296#define HDA_QUIRK_FORCESTEREO	(1 << 11)
297#define HDA_QUIRK_EAPDINV	(1 << 12)
298#define HDA_QUIRK_DMAPOS	(1 << 13)
299
300/* 26 - 31 = vrefs */
301#define HDA_QUIRK_IVREF50	(1 << 26)
302#define HDA_QUIRK_IVREF80	(1 << 27)
303#define HDA_QUIRK_IVREF100	(1 << 28)
304#define HDA_QUIRK_OVREF50	(1 << 29)
305#define HDA_QUIRK_OVREF80	(1 << 30)
306#define HDA_QUIRK_OVREF100	(1 << 31)
307
308#define HDA_QUIRK_IVREF		(HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF80 | \
309							HDA_QUIRK_IVREF100)
310#define HDA_QUIRK_OVREF		(HDA_QUIRK_OVREF50 | HDA_QUIRK_OVREF80 | \
311							HDA_QUIRK_OVREF100)
312#define HDA_QUIRK_VREF		(HDA_QUIRK_IVREF | HDA_QUIRK_OVREF)
313
314#define SOUND_MASK_SKIP		(1 << 30)
315#define SOUND_MASK_DISABLE	(1 << 31)
316
317static const struct {
318	char *key;
319	uint32_t value;
320} hdac_quirks_tab[] = {
321	{ "gpio0", HDA_QUIRK_GPIO0 },
322	{ "gpio1", HDA_QUIRK_GPIO1 },
323	{ "gpio2", HDA_QUIRK_GPIO2 },
324	{ "gpio3", HDA_QUIRK_GPIO3 },
325	{ "gpio4", HDA_QUIRK_GPIO4 },
326	{ "gpio5", HDA_QUIRK_GPIO5 },
327	{ "gpio6", HDA_QUIRK_GPIO6 },
328	{ "gpio7", HDA_QUIRK_GPIO7 },
329	{ "gpioflush", HDA_QUIRK_GPIOFLUSH },
330	{ "softpcmvol", HDA_QUIRK_SOFTPCMVOL },
331	{ "fixedrate", HDA_QUIRK_FIXEDRATE },
332	{ "forcestereo", HDA_QUIRK_FORCESTEREO },
333	{ "eapdinv", HDA_QUIRK_EAPDINV },
334	{ "dmapos", HDA_QUIRK_DMAPOS },
335	{ "ivref50", HDA_QUIRK_IVREF50 },
336	{ "ivref80", HDA_QUIRK_IVREF80 },
337	{ "ivref100", HDA_QUIRK_IVREF100 },
338	{ "ovref50", HDA_QUIRK_OVREF50 },
339	{ "ovref80", HDA_QUIRK_OVREF80 },
340	{ "ovref100", HDA_QUIRK_OVREF100 },
341	{ "ivref", HDA_QUIRK_IVREF },
342	{ "ovref", HDA_QUIRK_OVREF },
343	{ "vref", HDA_QUIRK_VREF },
344};
345#define HDAC_QUIRKS_TAB_LEN	\
346		(sizeof(hdac_quirks_tab) / sizeof(hdac_quirks_tab[0]))
347
348#define HDA_BDL_MIN	2
349#define HDA_BDL_MAX	256
350#define HDA_BDL_DEFAULT	HDA_BDL_MIN
351
352#define HDA_BLK_MIN	HDAC_DMA_ALIGNMENT
353#define HDA_BLK_ALIGN	(~(HDA_BLK_MIN - 1))
354
355#define HDA_BUFSZ_MIN		4096
356#define HDA_BUFSZ_MAX		65536
357#define HDA_BUFSZ_DEFAULT	16384
358
359#define HDA_PARSE_MAXDEPTH	10
360
361#define HDAC_UNSOLTAG_EVENT_HP		0x00
362#define HDAC_UNSOLTAG_EVENT_TEST	0x01
363
364MALLOC_DEFINE(M_HDAC, "hdac", "High Definition Audio Controller");
365
366enum {
367	HDA_PARSE_MIXER,
368	HDA_PARSE_DIRECT
369};
370
371/* Default */
372static uint32_t hdac_fmt[] = {
373	AFMT_STEREO | AFMT_S16_LE,
374	0
375};
376
377static struct pcmchan_caps hdac_caps = {48000, 48000, hdac_fmt, 0};
378
379static const struct {
380	uint32_t	model;
381	char		*desc;
382} hdac_devices[] = {
383	{ HDA_INTEL_82801F,  "Intel 82801F" },
384	{ HDA_INTEL_82801G,  "Intel 82801G" },
385	{ HDA_INTEL_82801H,  "Intel 82801H" },
386	{ HDA_INTEL_63XXESB, "Intel 631x/632xESB" },
387	{ HDA_NVIDIA_MCP51,  "NVidia MCP51" },
388	{ HDA_NVIDIA_MCP55,  "NVidia MCP55" },
389	{ HDA_NVIDIA_MCP61A, "NVidia MCP61A" },
390	{ HDA_NVIDIA_MCP61B, "NVidia MCP61B" },
391	{ HDA_NVIDIA_MCP65A, "NVidia MCP65A" },
392	{ HDA_NVIDIA_MCP65B, "NVidia MCP65B" },
393	{ HDA_ATI_SB450,     "ATI SB450"    },
394	{ HDA_ATI_SB600,     "ATI SB600"    },
395	{ HDA_VIA_VT82XX,    "VIA VT8251/8237A" },
396	{ HDA_SIS_966,       "SiS 966" },
397	/* Unknown */
398	{ HDA_INTEL_ALL,  "Intel (Unknown)"  },
399	{ HDA_NVIDIA_ALL, "NVidia (Unknown)" },
400	{ HDA_ATI_ALL,    "ATI (Unknown)"    },
401	{ HDA_VIA_ALL,    "VIA (Unknown)"    },
402	{ HDA_SIS_ALL,    "SiS (Unknown)"    },
403};
404#define HDAC_DEVICES_LEN (sizeof(hdac_devices) / sizeof(hdac_devices[0]))
405
406static const struct {
407	uint16_t vendor;
408	uint8_t reg;
409	uint8_t mask;
410	uint8_t enable;
411} hdac_pcie_snoop[] = {
412	{  INTEL_VENDORID, 0x00, 0x00, 0x00 },
413	{    ATI_VENDORID, 0x42, 0xf8, 0x02 },
414	{ NVIDIA_VENDORID, 0x4e, 0xf0, 0x0f },
415};
416#define HDAC_PCIESNOOP_LEN	\
417			(sizeof(hdac_pcie_snoop) / sizeof(hdac_pcie_snoop[0]))
418
419static const struct {
420	uint32_t	rate;
421	int		valid;
422	uint16_t	base;
423	uint16_t	mul;
424	uint16_t	div;
425} hda_rate_tab[] = {
426	{   8000, 1, 0x0000, 0x0000, 0x0500 },	/* (48000 * 1) / 6 */
427	{   9600, 0, 0x0000, 0x0000, 0x0400 },	/* (48000 * 1) / 5 */
428	{  12000, 0, 0x0000, 0x0000, 0x0300 },	/* (48000 * 1) / 4 */
429	{  16000, 1, 0x0000, 0x0000, 0x0200 },	/* (48000 * 1) / 3 */
430	{  18000, 0, 0x0000, 0x1000, 0x0700 },	/* (48000 * 3) / 8 */
431	{  19200, 0, 0x0000, 0x0800, 0x0400 },	/* (48000 * 2) / 5 */
432	{  24000, 0, 0x0000, 0x0000, 0x0100 },	/* (48000 * 1) / 2 */
433	{  28800, 0, 0x0000, 0x1000, 0x0400 },	/* (48000 * 3) / 5 */
434	{  32000, 1, 0x0000, 0x0800, 0x0200 },	/* (48000 * 2) / 3 */
435	{  36000, 0, 0x0000, 0x1000, 0x0300 },	/* (48000 * 3) / 4 */
436	{  38400, 0, 0x0000, 0x1800, 0x0400 },	/* (48000 * 4) / 5 */
437	{  48000, 1, 0x0000, 0x0000, 0x0000 },	/* (48000 * 1) / 1 */
438	{  64000, 0, 0x0000, 0x1800, 0x0200 },	/* (48000 * 4) / 3 */
439	{  72000, 0, 0x0000, 0x1000, 0x0100 },	/* (48000 * 3) / 2 */
440	{  96000, 1, 0x0000, 0x0800, 0x0000 },	/* (48000 * 2) / 1 */
441	{ 144000, 0, 0x0000, 0x1000, 0x0000 },	/* (48000 * 3) / 1 */
442	{ 192000, 1, 0x0000, 0x1800, 0x0000 },	/* (48000 * 4) / 1 */
443	{   8820, 0, 0x4000, 0x0000, 0x0400 },	/* (44100 * 1) / 5 */
444	{  11025, 1, 0x4000, 0x0000, 0x0300 },	/* (44100 * 1) / 4 */
445	{  12600, 0, 0x4000, 0x0800, 0x0600 },	/* (44100 * 2) / 7 */
446	{  14700, 0, 0x4000, 0x0000, 0x0200 },	/* (44100 * 1) / 3 */
447	{  17640, 0, 0x4000, 0x0800, 0x0400 },	/* (44100 * 2) / 5 */
448	{  18900, 0, 0x4000, 0x1000, 0x0600 },	/* (44100 * 3) / 7 */
449	{  22050, 1, 0x4000, 0x0000, 0x0100 },	/* (44100 * 1) / 2 */
450	{  25200, 0, 0x4000, 0x1800, 0x0600 },	/* (44100 * 4) / 7 */
451	{  26460, 0, 0x4000, 0x1000, 0x0400 },	/* (44100 * 3) / 5 */
452	{  29400, 0, 0x4000, 0x0800, 0x0200 },	/* (44100 * 2) / 3 */
453	{  33075, 0, 0x4000, 0x1000, 0x0300 },	/* (44100 * 3) / 4 */
454	{  35280, 0, 0x4000, 0x1800, 0x0400 },	/* (44100 * 4) / 5 */
455	{  44100, 1, 0x4000, 0x0000, 0x0000 },	/* (44100 * 1) / 1 */
456	{  58800, 0, 0x4000, 0x1800, 0x0200 },	/* (44100 * 4) / 3 */
457	{  66150, 0, 0x4000, 0x1000, 0x0100 },	/* (44100 * 3) / 2 */
458	{  88200, 1, 0x4000, 0x0800, 0x0000 },	/* (44100 * 2) / 1 */
459	{ 132300, 0, 0x4000, 0x1000, 0x0000 },	/* (44100 * 3) / 1 */
460	{ 176400, 1, 0x4000, 0x1800, 0x0000 },	/* (44100 * 4) / 1 */
461};
462#define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
463
464/* All codecs you can eat... */
465#define HDA_CODEC_CONSTRUCT(vendor, id) \
466		(((uint32_t)(vendor##_VENDORID) << 16) | ((id) & 0xffff))
467
468/* Realtek */
469#define REALTEK_VENDORID	0x10ec
470#define HDA_CODEC_ALC260	HDA_CODEC_CONSTRUCT(REALTEK, 0x0260)
471#define HDA_CODEC_ALC262	HDA_CODEC_CONSTRUCT(REALTEK, 0x0262)
472#define HDA_CODEC_ALC861	HDA_CODEC_CONSTRUCT(REALTEK, 0x0861)
473#define HDA_CODEC_ALC861VD	HDA_CODEC_CONSTRUCT(REALTEK, 0x0862)
474#define HDA_CODEC_ALC880	HDA_CODEC_CONSTRUCT(REALTEK, 0x0880)
475#define HDA_CODEC_ALC882	HDA_CODEC_CONSTRUCT(REALTEK, 0x0882)
476#define HDA_CODEC_ALC883	HDA_CODEC_CONSTRUCT(REALTEK, 0x0883)
477#define HDA_CODEC_ALC885	HDA_CODEC_CONSTRUCT(REALTEK, 0x0885)
478#define HDA_CODEC_ALC888	HDA_CODEC_CONSTRUCT(REALTEK, 0x0888)
479#define HDA_CODEC_ALCXXXX	HDA_CODEC_CONSTRUCT(REALTEK, 0xffff)
480
481/* Analog Devices */
482#define ANALOGDEVICES_VENDORID	0x11d4
483#define HDA_CODEC_AD1981HD	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1981)
484#define HDA_CODEC_AD1983	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1983)
485#define HDA_CODEC_AD1986A	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1986)
486#define HDA_CODEC_AD1988	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0x1988)
487#define HDA_CODEC_ADXXXX	HDA_CODEC_CONSTRUCT(ANALOGDEVICES, 0xffff)
488
489/* CMedia */
490#define CMEDIA_VENDORID		0x434d
491#define HDA_CODEC_CMI9880	HDA_CODEC_CONSTRUCT(CMEDIA, 0x4980)
492#define HDA_CODEC_CMIXXXX	HDA_CODEC_CONSTRUCT(CMEDIA, 0xffff)
493
494/* Sigmatel */
495#define SIGMATEL_VENDORID	0x8384
496#define HDA_CODEC_STAC9221	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7680)
497#define HDA_CODEC_STAC9221D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7683)
498#define HDA_CODEC_STAC9220	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7690)
499#define HDA_CODEC_STAC922XD	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7681)
500#define HDA_CODEC_STAC9227	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7618)
501#define HDA_CODEC_STAC9271D	HDA_CODEC_CONSTRUCT(SIGMATEL, 0x7627)
502#define HDA_CODEC_STACXXXX	HDA_CODEC_CONSTRUCT(SIGMATEL, 0xffff)
503
504/*
505 * Conexant
506 *
507 * Ok, the truth is, I don't have any idea at all whether
508 * it is "Venice" or "Waikiki" or other unnamed CXyadayada. The only
509 * place that tell me it is "Venice" is from its Windows driver INF.
510 *
511 *  Venice - CX?????
512 * Waikiki - CX20551-22
513 */
514#define CONEXANT_VENDORID	0x14f1
515#define HDA_CODEC_CXVENICE	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5045)
516#define HDA_CODEC_CXWAIKIKI	HDA_CODEC_CONSTRUCT(CONEXANT, 0x5047)
517#define HDA_CODEC_CXXXXX	HDA_CODEC_CONSTRUCT(CONEXANT, 0xffff)
518
519/* VIA */
520#define HDA_CODEC_VT1708_8	HDA_CODEC_CONSTRUCT(VIA, 0x1708)
521#define HDA_CODEC_VT1708_9	HDA_CODEC_CONSTRUCT(VIA, 0x1709)
522#define HDA_CODEC_VT1708_A	HDA_CODEC_CONSTRUCT(VIA, 0x170a)
523#define HDA_CODEC_VT1708_B	HDA_CODEC_CONSTRUCT(VIA, 0x170b)
524#define HDA_CODEC_VT1709_0	HDA_CODEC_CONSTRUCT(VIA, 0xe710)
525#define HDA_CODEC_VT1709_1	HDA_CODEC_CONSTRUCT(VIA, 0xe711)
526#define HDA_CODEC_VT1709_2	HDA_CODEC_CONSTRUCT(VIA, 0xe712)
527#define HDA_CODEC_VT1709_3	HDA_CODEC_CONSTRUCT(VIA, 0xe713)
528#define HDA_CODEC_VT1709_4	HDA_CODEC_CONSTRUCT(VIA, 0xe714)
529#define HDA_CODEC_VT1709_5	HDA_CODEC_CONSTRUCT(VIA, 0xe715)
530#define HDA_CODEC_VT1709_6	HDA_CODEC_CONSTRUCT(VIA, 0xe716)
531#define HDA_CODEC_VT1709_7	HDA_CODEC_CONSTRUCT(VIA, 0xe717)
532#define HDA_CODEC_VTXXXX	HDA_CODEC_CONSTRUCT(VIA, 0xffff)
533
534
535/* Codecs */
536static const struct {
537	uint32_t id;
538	char *name;
539} hdac_codecs[] = {
540	{ HDA_CODEC_ALC260,    "Realtek ALC260" },
541	{ HDA_CODEC_ALC262,    "Realtek ALC262" },
542	{ HDA_CODEC_ALC861,    "Realtek ALC861" },
543	{ HDA_CODEC_ALC861VD,  "Realtek ALC861-VD" },
544	{ HDA_CODEC_ALC880,    "Realtek ALC880" },
545	{ HDA_CODEC_ALC882,    "Realtek ALC882" },
546	{ HDA_CODEC_ALC883,    "Realtek ALC883" },
547	{ HDA_CODEC_ALC885,    "Realtek ALC885" },
548	{ HDA_CODEC_ALC888,    "Realtek ALC888" },
549	{ HDA_CODEC_AD1981HD,  "Analog Devices AD1981HD" },
550	{ HDA_CODEC_AD1983,    "Analog Devices AD1983" },
551	{ HDA_CODEC_AD1986A,   "Analog Devices AD1986A" },
552	{ HDA_CODEC_AD1988,    "Analog Devices AD1988" },
553	{ HDA_CODEC_CMI9880,   "CMedia CMI9880" },
554	{ HDA_CODEC_STAC9221,  "Sigmatel STAC9221" },
555	{ HDA_CODEC_STAC9221D, "Sigmatel STAC9221D" },
556	{ HDA_CODEC_STAC9220,  "Sigmatel STAC9220" },
557	{ HDA_CODEC_STAC922XD, "Sigmatel STAC9220D/9223D" },
558	{ HDA_CODEC_STAC9227,  "Sigmatel STAC9227" },
559	{ HDA_CODEC_STAC9271D, "Sigmatel STAC9271D" },
560	{ HDA_CODEC_CXVENICE,  "Conexant Venice" },
561	{ HDA_CODEC_CXWAIKIKI, "Conexant Waikiki" },
562	{ HDA_CODEC_VT1708_8,  "VIA VT1708_8" },
563	{ HDA_CODEC_VT1708_9,  "VIA VT1708_9" },
564	{ HDA_CODEC_VT1708_A,  "VIA VT1708_A" },
565	{ HDA_CODEC_VT1708_B,  "VIA VT1708_B" },
566	{ HDA_CODEC_VT1709_0,  "VIA VT1709_0" },
567	{ HDA_CODEC_VT1709_1,  "VIA VT1709_1" },
568	{ HDA_CODEC_VT1709_2,  "VIA VT1709_2" },
569	{ HDA_CODEC_VT1709_3,  "VIA VT1709_3" },
570	{ HDA_CODEC_VT1709_4,  "VIA VT1709_4" },
571	{ HDA_CODEC_VT1709_5,  "VIA VT1709_5" },
572	{ HDA_CODEC_VT1709_6,  "VIA VT1709_6" },
573	{ HDA_CODEC_VT1709_7,  "VIA VT1709_7" },
574	/* Unknown codec */
575	{ HDA_CODEC_ALCXXXX,   "Realtek (Unknown)" },
576	{ HDA_CODEC_ADXXXX,    "Analog Devices (Unknown)" },
577	{ HDA_CODEC_CMIXXXX,   "CMedia (Unknown)" },
578	{ HDA_CODEC_STACXXXX,  "Sigmatel (Unknown)" },
579	{ HDA_CODEC_CXXXXX,    "Conexant (Unknown)" },
580	{ HDA_CODEC_VTXXXX,    "VIA (Unknown)" },
581};
582#define HDAC_CODECS_LEN	(sizeof(hdac_codecs) / sizeof(hdac_codecs[0]))
583
584enum {
585	HDAC_HP_SWITCH_CTL,
586	HDAC_HP_SWITCH_CTRL,
587	HDAC_HP_SWITCH_DEBUG
588};
589
590static const struct {
591	uint32_t model;
592	uint32_t id;
593	int type;
594	int inverted;
595	int polling;
596	int execsense;
597	nid_t hpnid;
598	nid_t spkrnid[8];
599	nid_t eapdnid;
600} hdac_hp_switch[] = {
601	/* Specific OEM models */
602	{ HP_V3000_SUBVENDOR, HDA_CODEC_CXVENICE, HDAC_HP_SWITCH_CTL,
603	    0, 0, -1, 17, { 16, -1 }, 16 },
604	/* { HP_XW4300_SUBVENDOR, HDA_CODEC_ALC260, HDAC_HP_SWITCH_CTL,
605	    0, 0, -1, 21, { 16, 17, -1 }, -1 } */
606	/*{ HP_3010_SUBVENDOR,  HDA_CODEC_ALC260, HDAC_HP_SWITCH_DEBUG,
607	    0, 1, 0, 16, { 15, 18, 19, 20, 21, -1 }, -1 },*/
608	{ HP_NX7400_SUBVENDOR, HDA_CODEC_AD1981HD, HDAC_HP_SWITCH_CTL,
609	    0, 0, -1, 6, { 5, -1 }, 5 },
610	{ HP_NX6310_SUBVENDOR, HDA_CODEC_AD1981HD, HDAC_HP_SWITCH_CTL,
611	    0, 0, -1, 6, { 5, -1 }, 5 },
612	{ HP_NX6325_SUBVENDOR, HDA_CODEC_AD1981HD, HDAC_HP_SWITCH_CTL,
613	    0, 0, -1, 6, { 5, -1 }, 5 },
614	{ TOSHIBA_U200_SUBVENDOR, HDA_CODEC_AD1981HD, HDAC_HP_SWITCH_CTL,
615	    0, 0, -1, 6, { 5, -1 }, -1 },
616	{ DELL_D820_SUBVENDOR, HDA_CODEC_STAC9220, HDAC_HP_SWITCH_CTRL,
617	    0, 0, -1, 13, { 14, -1 }, -1 },
618	{ DELL_I1300_SUBVENDOR, HDA_CODEC_STAC9220, HDAC_HP_SWITCH_CTRL,
619	    0, 0, -1, 13, { 14, -1 }, -1 },
620	{ DELL_OPLX745_SUBVENDOR, HDA_CODEC_AD1983, HDAC_HP_SWITCH_CTL,
621	    0, 0, -1, 6, { 5, 7, -1 }, -1 },
622	{ APPLE_INTEL_MAC, HDA_CODEC_STAC9221, HDAC_HP_SWITCH_CTRL,
623	    0, 0, -1, 10, { 13, -1 }, -1 },
624	{ LENOVO_3KN100_SUBVENDOR, HDA_CODEC_AD1986A, HDAC_HP_SWITCH_CTL,
625	    1, 0, -1, 26, { 27, -1 }, -1 },
626	{ LG_LW20_SUBVENDOR, HDA_CODEC_ALC880, HDAC_HP_SWITCH_CTL,
627	    0, 0, -1, 27, { 20, -1 }, -1 },
628	{ ACER_A5050_SUBVENDOR, HDA_CODEC_ALC883, HDAC_HP_SWITCH_CTL,
629	    0, 0, -1, 20, { 21, -1 }, -1 },
630	{ ACER_3681WXM_SUBVENDOR, HDA_CODEC_ALC883, HDAC_HP_SWITCH_CTL,
631	    0, 0, -1, 20, { 21, -1 }, -1 },
632	{ MSI_MS1034_SUBVENDOR, HDA_CODEC_ALC883, HDAC_HP_SWITCH_CTL,
633	    0, 0, -1, 20, { 27, -1 }, -1 },
634	/*
635	 * All models that at least come from the same vendor with
636	 * simmilar codec.
637	 */
638	{ HP_ALL_SUBVENDOR, HDA_CODEC_CXVENICE, HDAC_HP_SWITCH_CTL,
639	    0, 0, -1, 17, { 16, -1 }, 16 },
640	{ HP_ALL_SUBVENDOR, HDA_CODEC_AD1981HD, HDAC_HP_SWITCH_CTL,
641	    0, 0, -1, 6, { 5, -1 }, 5 },
642	{ TOSHIBA_ALL_SUBVENDOR, HDA_CODEC_AD1981HD, HDAC_HP_SWITCH_CTL,
643	    0, 0, -1, 6, { 5, -1 }, -1 },
644	{ DELL_ALL_SUBVENDOR, HDA_CODEC_STAC9220, HDAC_HP_SWITCH_CTRL,
645	    0, 0, -1, 13, { 14, -1 }, -1 },
646	{ LENOVO_ALL_SUBVENDOR, HDA_CODEC_AD1986A, HDAC_HP_SWITCH_CTL,
647	    1, 0, -1, 26, { 27, -1 }, -1 },
648#if 0
649	{ ACER_ALL_SUBVENDOR, HDA_CODEC_ALC883, HDAC_HP_SWITCH_CTL,
650	    0, 0, -1, 20, { 21, -1 }, -1 },
651#endif
652};
653#define HDAC_HP_SWITCH_LEN	\
654		(sizeof(hdac_hp_switch) / sizeof(hdac_hp_switch[0]))
655
656static const struct {
657	uint32_t model;
658	uint32_t id;
659	nid_t eapdnid;
660	int hp_switch;
661} hdac_eapd_switch[] = {
662	{ HP_V3000_SUBVENDOR, HDA_CODEC_CXVENICE, 16, 1 },
663	{ HP_NX7400_SUBVENDOR, HDA_CODEC_AD1981HD, 5, 1 },
664	{ HP_NX6310_SUBVENDOR, HDA_CODEC_AD1981HD, 5, 1 },
665};
666#define HDAC_EAPD_SWITCH_LEN	\
667		(sizeof(hdac_eapd_switch) / sizeof(hdac_eapd_switch[0]))
668
669/****************************************************************************
670 * Function prototypes
671 ****************************************************************************/
672static void	hdac_intr_handler(void *);
673static int	hdac_reset(struct hdac_softc *);
674static int	hdac_get_capabilities(struct hdac_softc *);
675static void	hdac_dma_cb(void *, bus_dma_segment_t *, int, int);
676static int	hdac_dma_alloc(struct hdac_softc *,
677					struct hdac_dma *, bus_size_t);
678static void	hdac_dma_free(struct hdac_softc *, struct hdac_dma *);
679static int	hdac_mem_alloc(struct hdac_softc *);
680static void	hdac_mem_free(struct hdac_softc *);
681static int	hdac_irq_alloc(struct hdac_softc *);
682static void	hdac_irq_free(struct hdac_softc *);
683static void	hdac_corb_init(struct hdac_softc *);
684static void	hdac_rirb_init(struct hdac_softc *);
685static void	hdac_corb_start(struct hdac_softc *);
686static void	hdac_rirb_start(struct hdac_softc *);
687static void	hdac_scan_codecs(struct hdac_softc *);
688static int	hdac_probe_codec(struct hdac_codec *);
689static struct	hdac_devinfo *hdac_probe_function(struct hdac_codec *, nid_t);
690static void	hdac_add_child(struct hdac_softc *, struct hdac_devinfo *);
691
692static void	hdac_attach2(void *);
693
694static uint32_t	hdac_command_sendone_internal(struct hdac_softc *,
695							uint32_t, int);
696static void	hdac_command_send_internal(struct hdac_softc *,
697					struct hdac_command_list *, int);
698
699static int	hdac_probe(device_t);
700static int	hdac_attach(device_t);
701static int	hdac_detach(device_t);
702static void	hdac_widget_connection_select(struct hdac_widget *, uint8_t);
703static void	hdac_audio_ctl_amp_set(struct hdac_audio_ctl *,
704						uint32_t, int, int);
705static struct	hdac_audio_ctl *hdac_audio_ctl_amp_get(struct hdac_devinfo *,
706							nid_t, int, int);
707static void	hdac_audio_ctl_amp_set_internal(struct hdac_softc *,
708				nid_t, nid_t, int, int, int, int, int, int);
709static int	hdac_audio_ctl_ossmixer_getnextdev(struct hdac_devinfo *);
710static struct	hdac_widget *hdac_widget_get(struct hdac_devinfo *, nid_t);
711
712static int	hdac_rirb_flush(struct hdac_softc *sc);
713static int	hdac_unsolq_flush(struct hdac_softc *sc);
714
715#define hdac_command(a1, a2, a3)	\
716		hdac_command_sendone_internal(a1, a2, a3)
717
718#define hdac_codec_id(d)						\
719		((uint32_t)((d == NULL) ? 0x00000000 :			\
720		((((uint32_t)(d)->vendor_id & 0x0000ffff) << 16) |	\
721		((uint32_t)(d)->device_id & 0x0000ffff))))
722
723static char *
724hdac_codec_name(struct hdac_devinfo *devinfo)
725{
726	uint32_t id;
727	int i;
728
729	id = hdac_codec_id(devinfo);
730
731	for (i = 0; i < HDAC_CODECS_LEN; i++) {
732		if (HDA_DEV_MATCH(hdac_codecs[i].id, id))
733			return (hdac_codecs[i].name);
734	}
735
736	return ((id == 0x00000000) ? "NULL Codec" : "Unknown Codec");
737}
738
739static char *
740hdac_audio_ctl_ossmixer_mask2name(uint32_t devmask)
741{
742	static char *ossname[] = SOUND_DEVICE_NAMES;
743	static char *unknown = "???";
744	int i;
745
746	for (i = SOUND_MIXER_NRDEVICES - 1; i >= 0; i--) {
747		if (devmask & (1 << i))
748			return (ossname[i]);
749	}
750	return (unknown);
751}
752
753static void
754hdac_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
755{
756	static char *ossname[] = SOUND_DEVICE_NAMES;
757	int i, first = 1;
758
759	bzero(buf, len);
760	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
761		if (mask & (1 << i)) {
762			if (first == 0)
763				strlcat(buf, ", ", len);
764			strlcat(buf, ossname[i], len);
765			first = 0;
766		}
767	}
768}
769
770static struct hdac_audio_ctl *
771hdac_audio_ctl_each(struct hdac_devinfo *devinfo, int *index)
772{
773	if (devinfo == NULL ||
774	    devinfo->node_type != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO ||
775	    index == NULL || devinfo->function.audio.ctl == NULL ||
776	    devinfo->function.audio.ctlcnt < 1 ||
777	    *index < 0 || *index >= devinfo->function.audio.ctlcnt)
778		return (NULL);
779	return (&devinfo->function.audio.ctl[(*index)++]);
780}
781
782static struct hdac_audio_ctl *
783hdac_audio_ctl_amp_get(struct hdac_devinfo *devinfo, nid_t nid,
784						int index, int cnt)
785{
786	struct hdac_audio_ctl *ctl, *retctl = NULL;
787	int i, at, atindex, found = 0;
788
789	if (devinfo == NULL || devinfo->function.audio.ctl == NULL)
790		return (NULL);
791
792	at = cnt;
793	if (at == 0)
794		at = 1;
795	else if (at < 0)
796		at = -1;
797	atindex = index;
798	if (atindex < 0)
799		atindex = -1;
800
801	i = 0;
802	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
803		if (ctl->enable == 0 || ctl->widget == NULL)
804			continue;
805		if (!(ctl->widget->nid == nid && (atindex == -1 ||
806		    ctl->index == atindex)))
807			continue;
808		found++;
809		if (found == cnt)
810			return (ctl);
811		retctl = ctl;
812	}
813
814	return ((at == -1) ? retctl : NULL);
815}
816
817static void
818hdac_hp_switch_handler(struct hdac_devinfo *devinfo)
819{
820	struct hdac_softc *sc;
821	struct hdac_widget *w;
822	struct hdac_audio_ctl *ctl;
823	uint32_t val, id, res;
824	int i = 0, j, forcemute;
825	nid_t cad;
826
827	if (devinfo == NULL || devinfo->codec == NULL ||
828	    devinfo->codec->sc == NULL)
829		return;
830
831	sc = devinfo->codec->sc;
832	cad = devinfo->codec->cad;
833	id = hdac_codec_id(devinfo);
834	for (i = 0; i < HDAC_HP_SWITCH_LEN; i++) {
835		if (HDA_DEV_MATCH(hdac_hp_switch[i].model,
836		    sc->pci_subvendor) &&
837		    hdac_hp_switch[i].id == id)
838			break;
839	}
840
841	if (i >= HDAC_HP_SWITCH_LEN)
842		return;
843
844	forcemute = 0;
845	if (hdac_hp_switch[i].eapdnid != -1) {
846		w = hdac_widget_get(devinfo, hdac_hp_switch[i].eapdnid);
847		if (w != NULL && w->param.eapdbtl != HDAC_INVALID)
848			forcemute = (w->param.eapdbtl &
849			    HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD) ? 0 : 1;
850	}
851
852	if (hdac_hp_switch[i].execsense != -1)
853		hdac_command(sc,
854		    HDA_CMD_SET_PIN_SENSE(cad, hdac_hp_switch[i].hpnid,
855		    hdac_hp_switch[i].execsense), cad);
856	res = hdac_command(sc,
857	    HDA_CMD_GET_PIN_SENSE(cad, hdac_hp_switch[i].hpnid), cad);
858	HDA_BOOTVERBOSE(
859		device_printf(sc->dev,
860		    "HDA_DEBUG: Pin sense: nid=%d res=0x%08x\n",
861		    hdac_hp_switch[i].hpnid, res);
862	);
863	res = HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT(res);
864	res ^= hdac_hp_switch[i].inverted;
865
866	switch (hdac_hp_switch[i].type) {
867	case HDAC_HP_SWITCH_CTL:
868		ctl = hdac_audio_ctl_amp_get(devinfo,
869		    hdac_hp_switch[i].hpnid, 0, 1);
870		if (ctl != NULL) {
871			val = (res != 0 && forcemute == 0) ?
872			    HDA_AMP_MUTE_NONE : HDA_AMP_MUTE_ALL;
873			if (val != ctl->muted) {
874				ctl->muted = val;
875				hdac_audio_ctl_amp_set(ctl,
876				    HDA_AMP_MUTE_DEFAULT, ctl->left,
877				    ctl->right);
878			}
879		}
880		for (j = 0; hdac_hp_switch[i].spkrnid[j] != -1; j++) {
881			ctl = hdac_audio_ctl_amp_get(devinfo,
882			    hdac_hp_switch[i].spkrnid[j], 0, 1);
883			if (ctl == NULL)
884				continue;
885			val = (res != 0 || forcemute == 1) ?
886			    HDA_AMP_MUTE_ALL : HDA_AMP_MUTE_NONE;
887			if (val == ctl->muted)
888				continue;
889			ctl->muted = val;
890			hdac_audio_ctl_amp_set(ctl, HDA_AMP_MUTE_DEFAULT,
891			    ctl->left, ctl->right);
892		}
893		break;
894	case HDAC_HP_SWITCH_CTRL:
895		if (res != 0) {
896			/* HP in */
897			w = hdac_widget_get(devinfo, hdac_hp_switch[i].hpnid);
898			if (w != NULL && w->type ==
899			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
900				if (forcemute == 0)
901					val = w->wclass.pin.ctrl |
902					    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
903				else
904					val = w->wclass.pin.ctrl &
905					    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
906				if (val != w->wclass.pin.ctrl) {
907					w->wclass.pin.ctrl = val;
908					hdac_command(sc,
909					    HDA_CMD_SET_PIN_WIDGET_CTRL(cad,
910					    w->nid, w->wclass.pin.ctrl), cad);
911				}
912			}
913			for (j = 0; hdac_hp_switch[i].spkrnid[j] != -1; j++) {
914				w = hdac_widget_get(devinfo,
915				    hdac_hp_switch[i].spkrnid[j]);
916				if (w == NULL || w->type !=
917				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
918					continue;
919				val = w->wclass.pin.ctrl &
920				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
921				if (val == w->wclass.pin.ctrl)
922					continue;
923				w->wclass.pin.ctrl = val;
924				hdac_command(sc, HDA_CMD_SET_PIN_WIDGET_CTRL(
925				    cad, w->nid, w->wclass.pin.ctrl), cad);
926			}
927		} else {
928			/* HP out */
929			w = hdac_widget_get(devinfo, hdac_hp_switch[i].hpnid);
930			if (w != NULL && w->type ==
931			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
932				val = w->wclass.pin.ctrl &
933				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
934				if (val != w->wclass.pin.ctrl) {
935					w->wclass.pin.ctrl = val;
936					hdac_command(sc,
937					    HDA_CMD_SET_PIN_WIDGET_CTRL(cad,
938					    w->nid, w->wclass.pin.ctrl), cad);
939				}
940			}
941			for (j = 0; hdac_hp_switch[i].spkrnid[j] != -1; j++) {
942				w = hdac_widget_get(devinfo,
943				    hdac_hp_switch[i].spkrnid[j]);
944				if (w == NULL || w->type !=
945				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
946					continue;
947				if (forcemute == 0)
948					val = w->wclass.pin.ctrl |
949					    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
950				else
951					val = w->wclass.pin.ctrl &
952					    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
953				if (val == w->wclass.pin.ctrl)
954					continue;
955				w->wclass.pin.ctrl = val;
956				hdac_command(sc, HDA_CMD_SET_PIN_WIDGET_CTRL(
957				    cad, w->nid, w->wclass.pin.ctrl), cad);
958			}
959		}
960		break;
961	case HDAC_HP_SWITCH_DEBUG:
962		if (hdac_hp_switch[i].execsense != -1)
963			hdac_command(sc,
964			    HDA_CMD_SET_PIN_SENSE(cad, hdac_hp_switch[i].hpnid,
965			    hdac_hp_switch[i].execsense), cad);
966		res = hdac_command(sc,
967		    HDA_CMD_GET_PIN_SENSE(cad, hdac_hp_switch[i].hpnid), cad);
968		device_printf(sc->dev,
969		    "[ 0] HDA_DEBUG: Pin sense: nid=%d res=0x%08x\n",
970		    hdac_hp_switch[i].hpnid, res);
971		for (j = 0; hdac_hp_switch[i].spkrnid[j] != -1; j++) {
972			w = hdac_widget_get(devinfo,
973			    hdac_hp_switch[i].spkrnid[j]);
974			if (w == NULL || w->type !=
975			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
976				continue;
977			if (hdac_hp_switch[i].execsense != -1)
978				hdac_command(sc,
979				    HDA_CMD_SET_PIN_SENSE(cad, w->nid,
980				    hdac_hp_switch[i].execsense), cad);
981			res = hdac_command(sc,
982			    HDA_CMD_GET_PIN_SENSE(cad, w->nid), cad);
983			device_printf(sc->dev,
984			    "[%2d] HDA_DEBUG: Pin sense: nid=%d res=0x%08x\n",
985			    j + 1, w->nid, res);
986		}
987		break;
988	default:
989		break;
990	}
991}
992
993static void
994hdac_unsolicited_handler(struct hdac_codec *codec, uint32_t tag)
995{
996	struct hdac_softc *sc;
997	struct hdac_devinfo *devinfo = NULL;
998	device_t *devlist = NULL;
999	int devcount, i;
1000
1001	if (codec == NULL || codec->sc == NULL)
1002		return;
1003
1004	sc = codec->sc;
1005
1006	HDA_BOOTVERBOSE(
1007		device_printf(sc->dev, "HDA_DEBUG: Unsol Tag: 0x%08x\n", tag);
1008	);
1009
1010	device_get_children(sc->dev, &devlist, &devcount);
1011	for (i = 0; devlist != NULL && i < devcount; i++) {
1012		devinfo = (struct hdac_devinfo *)device_get_ivars(devlist[i]);
1013		if (devinfo != NULL && devinfo->node_type ==
1014		    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO &&
1015		    devinfo->codec != NULL &&
1016		    devinfo->codec->cad == codec->cad) {
1017			break;
1018		} else
1019			devinfo = NULL;
1020	}
1021	if (devlist != NULL)
1022		free(devlist, M_TEMP);
1023
1024	if (devinfo == NULL)
1025		return;
1026
1027	switch (tag) {
1028	case HDAC_UNSOLTAG_EVENT_HP:
1029		hdac_hp_switch_handler(devinfo);
1030		break;
1031	case HDAC_UNSOLTAG_EVENT_TEST:
1032		device_printf(sc->dev, "Unsol Test!\n");
1033		break;
1034	default:
1035		break;
1036	}
1037}
1038
1039static int
1040hdac_stream_intr(struct hdac_softc *sc, struct hdac_chan *ch)
1041{
1042	/* XXX to be removed */
1043#ifdef HDAC_INTR_EXTRA
1044	uint32_t res;
1045#endif
1046
1047	if (ch->blkcnt == 0)
1048		return (0);
1049
1050	/* XXX to be removed */
1051#ifdef HDAC_INTR_EXTRA
1052	res = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDSTS);
1053#endif
1054
1055	/* XXX to be removed */
1056#ifdef HDAC_INTR_EXTRA
1057	HDA_BOOTVERBOSE(
1058		if (res & (HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE))
1059			device_printf(sc->dev,
1060			    "PCMDIR_%s intr triggered beyond stream boundary:"
1061			    "%08x\n",
1062			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC", res);
1063	);
1064#endif
1065
1066	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDSTS,
1067	    HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE | HDAC_SDSTS_BCIS );
1068
1069	/* XXX to be removed */
1070#ifdef HDAC_INTR_EXTRA
1071	if (res & HDAC_SDSTS_BCIS) {
1072#endif
1073		return (1);
1074	/* XXX to be removed */
1075#ifdef HDAC_INTR_EXTRA
1076	}
1077#endif
1078
1079	return (0);
1080}
1081
1082/****************************************************************************
1083 * void hdac_intr_handler(void *)
1084 *
1085 * Interrupt handler. Processes interrupts received from the hdac.
1086 ****************************************************************************/
1087static void
1088hdac_intr_handler(void *context)
1089{
1090	struct hdac_softc *sc;
1091	uint32_t intsts;
1092	uint8_t rirbsts;
1093	struct hdac_rirb *rirb_base;
1094	uint32_t trigger = 0;
1095
1096	sc = (struct hdac_softc *)context;
1097
1098	hdac_lock(sc);
1099	if (sc->polling != 0) {
1100		hdac_unlock(sc);
1101		return;
1102	}
1103	/* Do we have anything to do? */
1104	intsts = HDAC_READ_4(&sc->mem, HDAC_INTSTS);
1105	if (!HDA_FLAG_MATCH(intsts, HDAC_INTSTS_GIS)) {
1106		hdac_unlock(sc);
1107		return;
1108	}
1109
1110	/* Was this a controller interrupt? */
1111	if (HDA_FLAG_MATCH(intsts, HDAC_INTSTS_CIS)) {
1112		rirb_base = (struct hdac_rirb *)sc->rirb_dma.dma_vaddr;
1113		rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
1114		/* Get as many responses that we can */
1115		while (HDA_FLAG_MATCH(rirbsts, HDAC_RIRBSTS_RINTFL)) {
1116			HDAC_WRITE_1(&sc->mem,
1117			    HDAC_RIRBSTS, HDAC_RIRBSTS_RINTFL);
1118			hdac_rirb_flush(sc);
1119			rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
1120		}
1121		/* XXX to be removed */
1122		/* Clear interrupt and exit */
1123#ifdef HDAC_INTR_EXTRA
1124		HDAC_WRITE_4(&sc->mem, HDAC_INTSTS, HDAC_INTSTS_CIS);
1125#endif
1126	}
1127
1128	hdac_unsolq_flush(sc);
1129
1130	if (intsts & HDAC_INTSTS_SIS_MASK) {
1131		if ((intsts & (1 << sc->num_iss)) &&
1132		    hdac_stream_intr(sc, &sc->play) != 0)
1133			trigger |= 1;
1134		if ((intsts & (1 << 0)) &&
1135		    hdac_stream_intr(sc, &sc->rec) != 0)
1136			trigger |= 2;
1137		/* XXX to be removed */
1138#ifdef HDAC_INTR_EXTRA
1139		HDAC_WRITE_4(&sc->mem, HDAC_INTSTS, intsts &
1140		    HDAC_INTSTS_SIS_MASK);
1141#endif
1142	}
1143
1144	hdac_unlock(sc);
1145
1146	if (trigger & 1)
1147		chn_intr(sc->play.c);
1148	if (trigger & 2)
1149		chn_intr(sc->rec.c);
1150}
1151
1152/****************************************************************************
1153 * int hdac_reset(hdac_softc *)
1154 *
1155 * Reset the hdac to a quiescent and known state.
1156 ****************************************************************************/
1157static int
1158hdac_reset(struct hdac_softc *sc)
1159{
1160	uint32_t gctl;
1161	int count, i;
1162
1163	/*
1164	 * Stop all Streams DMA engine
1165	 */
1166	for (i = 0; i < sc->num_iss; i++)
1167		HDAC_WRITE_4(&sc->mem, HDAC_ISDCTL(sc, i), 0x0);
1168	for (i = 0; i < sc->num_oss; i++)
1169		HDAC_WRITE_4(&sc->mem, HDAC_OSDCTL(sc, i), 0x0);
1170	for (i = 0; i < sc->num_bss; i++)
1171		HDAC_WRITE_4(&sc->mem, HDAC_BSDCTL(sc, i), 0x0);
1172
1173	/*
1174	 * Stop Control DMA engines.
1175	 */
1176	HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, 0x0);
1177	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, 0x0);
1178
1179	/*
1180	 * Reset DMA position buffer.
1181	 */
1182	HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE, 0x0);
1183	HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, 0x0);
1184
1185	/*
1186	 * Reset the controller. The reset must remain asserted for
1187	 * a minimum of 100us.
1188	 */
1189	gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
1190	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl & ~HDAC_GCTL_CRST);
1191	count = 10000;
1192	do {
1193		gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
1194		if (!(gctl & HDAC_GCTL_CRST))
1195			break;
1196		DELAY(10);
1197	} while	(--count);
1198	if (gctl & HDAC_GCTL_CRST) {
1199		device_printf(sc->dev, "Unable to put hdac in reset\n");
1200		return (ENXIO);
1201	}
1202	DELAY(100);
1203	gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
1204	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl | HDAC_GCTL_CRST);
1205	count = 10000;
1206	do {
1207		gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
1208		if (gctl & HDAC_GCTL_CRST)
1209			break;
1210		DELAY(10);
1211	} while (--count);
1212	if (!(gctl & HDAC_GCTL_CRST)) {
1213		device_printf(sc->dev, "Device stuck in reset\n");
1214		return (ENXIO);
1215	}
1216
1217	/*
1218	 * Wait for codecs to finish their own reset sequence. The delay here
1219	 * should be of 250us but for some reasons, on it's not enough on my
1220	 * computer. Let's use twice as much as necessary to make sure that
1221	 * it's reset properly.
1222	 */
1223	DELAY(1000);
1224
1225	return (0);
1226}
1227
1228
1229/****************************************************************************
1230 * int hdac_get_capabilities(struct hdac_softc *);
1231 *
1232 * Retreive the general capabilities of the hdac;
1233 *	Number of Input Streams
1234 *	Number of Output Streams
1235 *	Number of bidirectional Streams
1236 *	64bit ready
1237 *	CORB and RIRB sizes
1238 ****************************************************************************/
1239static int
1240hdac_get_capabilities(struct hdac_softc *sc)
1241{
1242	uint16_t gcap;
1243	uint8_t corbsize, rirbsize;
1244
1245	gcap = HDAC_READ_2(&sc->mem, HDAC_GCAP);
1246	sc->num_iss = HDAC_GCAP_ISS(gcap);
1247	sc->num_oss = HDAC_GCAP_OSS(gcap);
1248	sc->num_bss = HDAC_GCAP_BSS(gcap);
1249
1250	sc->support_64bit = HDA_FLAG_MATCH(gcap, HDAC_GCAP_64OK);
1251
1252	corbsize = HDAC_READ_1(&sc->mem, HDAC_CORBSIZE);
1253	if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_256) ==
1254	    HDAC_CORBSIZE_CORBSZCAP_256)
1255		sc->corb_size = 256;
1256	else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_16) ==
1257	    HDAC_CORBSIZE_CORBSZCAP_16)
1258		sc->corb_size = 16;
1259	else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_2) ==
1260	    HDAC_CORBSIZE_CORBSZCAP_2)
1261		sc->corb_size = 2;
1262	else {
1263		device_printf(sc->dev, "%s: Invalid corb size (%x)\n",
1264		    __func__, corbsize);
1265		return (ENXIO);
1266	}
1267
1268	rirbsize = HDAC_READ_1(&sc->mem, HDAC_RIRBSIZE);
1269	if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_256) ==
1270	    HDAC_RIRBSIZE_RIRBSZCAP_256)
1271		sc->rirb_size = 256;
1272	else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_16) ==
1273	    HDAC_RIRBSIZE_RIRBSZCAP_16)
1274		sc->rirb_size = 16;
1275	else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_2) ==
1276	    HDAC_RIRBSIZE_RIRBSZCAP_2)
1277		sc->rirb_size = 2;
1278	else {
1279		device_printf(sc->dev, "%s: Invalid rirb size (%x)\n",
1280		    __func__, rirbsize);
1281		return (ENXIO);
1282	}
1283
1284	return (0);
1285}
1286
1287
1288/****************************************************************************
1289 * void hdac_dma_cb
1290 *
1291 * This function is called by bus_dmamap_load when the mapping has been
1292 * established. We just record the physical address of the mapping into
1293 * the struct hdac_dma passed in.
1294 ****************************************************************************/
1295static void
1296hdac_dma_cb(void *callback_arg, bus_dma_segment_t *segs, int nseg, int error)
1297{
1298	struct hdac_dma *dma;
1299
1300	if (error == 0) {
1301		dma = (struct hdac_dma *)callback_arg;
1302		dma->dma_paddr = segs[0].ds_addr;
1303	}
1304}
1305
1306
1307/****************************************************************************
1308 * int hdac_dma_alloc
1309 *
1310 * This function allocate and setup a dma region (struct hdac_dma).
1311 * It must be freed by a corresponding hdac_dma_free.
1312 ****************************************************************************/
1313static int
1314hdac_dma_alloc(struct hdac_softc *sc, struct hdac_dma *dma, bus_size_t size)
1315{
1316	bus_size_t roundsz;
1317	int result;
1318	int lowaddr;
1319
1320	roundsz = roundup2(size, HDAC_DMA_ALIGNMENT);
1321	lowaddr = (sc->support_64bit) ? BUS_SPACE_MAXADDR :
1322	    BUS_SPACE_MAXADDR_32BIT;
1323	bzero(dma, sizeof(*dma));
1324
1325	/*
1326	 * Create a DMA tag
1327	 */
1328	result = bus_dma_tag_create(NULL,	/* parent */
1329	    HDAC_DMA_ALIGNMENT,			/* alignment */
1330	    0,					/* boundary */
1331	    lowaddr,				/* lowaddr */
1332	    BUS_SPACE_MAXADDR,			/* highaddr */
1333	    NULL,				/* filtfunc */
1334	    NULL,				/* fistfuncarg */
1335	    roundsz, 				/* maxsize */
1336	    1,					/* nsegments */
1337	    roundsz, 				/* maxsegsz */
1338	    0,					/* flags */
1339	    NULL,				/* lockfunc */
1340	    NULL,				/* lockfuncarg */
1341	    &dma->dma_tag);			/* dmat */
1342	if (result != 0) {
1343		device_printf(sc->dev, "%s: bus_dma_tag_create failed (%x)\n",
1344		    __func__, result);
1345		goto hdac_dma_alloc_fail;
1346	}
1347
1348	/*
1349	 * Allocate DMA memory
1350	 */
1351	result = bus_dmamem_alloc(dma->dma_tag, (void **)&dma->dma_vaddr,
1352	    BUS_DMA_NOWAIT | BUS_DMA_ZERO |
1353	    ((sc->nocache != 0) ? BUS_DMA_NOCACHE : 0), &dma->dma_map);
1354	if (result != 0) {
1355		device_printf(sc->dev, "%s: bus_dmamem_alloc failed (%x)\n",
1356		    __func__, result);
1357		goto hdac_dma_alloc_fail;
1358	}
1359
1360	dma->dma_size = roundsz;
1361
1362	/*
1363	 * Map the memory
1364	 */
1365	result = bus_dmamap_load(dma->dma_tag, dma->dma_map,
1366	    (void *)dma->dma_vaddr, roundsz, hdac_dma_cb, (void *)dma, 0);
1367	if (result != 0 || dma->dma_paddr == 0) {
1368		if (result == 0)
1369			result = ENOMEM;
1370		device_printf(sc->dev, "%s: bus_dmamem_load failed (%x)\n",
1371		    __func__, result);
1372		goto hdac_dma_alloc_fail;
1373	}
1374
1375	HDA_BOOTVERBOSE(
1376		device_printf(sc->dev, "%s: size=%ju -> roundsz=%ju\n",
1377		    __func__, (uintmax_t)size, (uintmax_t)roundsz);
1378	);
1379
1380	return (0);
1381
1382hdac_dma_alloc_fail:
1383	hdac_dma_free(sc, dma);
1384
1385	return (result);
1386}
1387
1388
1389/****************************************************************************
1390 * void hdac_dma_free(struct hdac_softc *, struct hdac_dma *)
1391 *
1392 * Free a struct dhac_dma that has been previously allocated via the
1393 * hdac_dma_alloc function.
1394 ****************************************************************************/
1395static void
1396hdac_dma_free(struct hdac_softc *sc, struct hdac_dma *dma)
1397{
1398	if (dma->dma_map != NULL) {
1399#if 0
1400		/* Flush caches */
1401		bus_dmamap_sync(dma->dma_tag, dma->dma_map,
1402		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1403#endif
1404		bus_dmamap_unload(dma->dma_tag, dma->dma_map);
1405	}
1406	if (dma->dma_vaddr != NULL) {
1407		bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1408		dma->dma_vaddr = NULL;
1409	}
1410	dma->dma_map = NULL;
1411	if (dma->dma_tag != NULL) {
1412		bus_dma_tag_destroy(dma->dma_tag);
1413		dma->dma_tag = NULL;
1414	}
1415	dma->dma_size = 0;
1416}
1417
1418/****************************************************************************
1419 * int hdac_mem_alloc(struct hdac_softc *)
1420 *
1421 * Allocate all the bus resources necessary to speak with the physical
1422 * controller.
1423 ****************************************************************************/
1424static int
1425hdac_mem_alloc(struct hdac_softc *sc)
1426{
1427	struct hdac_mem *mem;
1428
1429	mem = &sc->mem;
1430	mem->mem_rid = PCIR_BAR(0);
1431	mem->mem_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
1432	    &mem->mem_rid, RF_ACTIVE);
1433	if (mem->mem_res == NULL) {
1434		device_printf(sc->dev,
1435		    "%s: Unable to allocate memory resource\n", __func__);
1436		return (ENOMEM);
1437	}
1438	mem->mem_tag = rman_get_bustag(mem->mem_res);
1439	mem->mem_handle = rman_get_bushandle(mem->mem_res);
1440
1441	return (0);
1442}
1443
1444/****************************************************************************
1445 * void hdac_mem_free(struct hdac_softc *)
1446 *
1447 * Free up resources previously allocated by hdac_mem_alloc.
1448 ****************************************************************************/
1449static void
1450hdac_mem_free(struct hdac_softc *sc)
1451{
1452	struct hdac_mem *mem;
1453
1454	mem = &sc->mem;
1455	if (mem->mem_res != NULL)
1456		bus_release_resource(sc->dev, SYS_RES_MEMORY, mem->mem_rid,
1457		    mem->mem_res);
1458	mem->mem_res = NULL;
1459}
1460
1461/****************************************************************************
1462 * int hdac_irq_alloc(struct hdac_softc *)
1463 *
1464 * Allocate and setup the resources necessary for interrupt handling.
1465 ****************************************************************************/
1466static int
1467hdac_irq_alloc(struct hdac_softc *sc)
1468{
1469	struct hdac_irq *irq;
1470	int result;
1471
1472	irq = &sc->irq;
1473	irq->irq_rid = 0x0;
1474	irq->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
1475	    &irq->irq_rid, RF_SHAREABLE | RF_ACTIVE);
1476	if (irq->irq_res == NULL) {
1477		device_printf(sc->dev, "%s: Unable to allocate irq\n",
1478		    __func__);
1479		goto hdac_irq_alloc_fail;
1480	}
1481	result = snd_setup_intr(sc->dev, irq->irq_res, INTR_MPSAFE,
1482	    hdac_intr_handler, sc, &irq->irq_handle);
1483	if (result != 0) {
1484		device_printf(sc->dev,
1485		    "%s: Unable to setup interrupt handler (%x)\n",
1486		    __func__, result);
1487		goto hdac_irq_alloc_fail;
1488	}
1489
1490	return (0);
1491
1492hdac_irq_alloc_fail:
1493	hdac_irq_free(sc);
1494
1495	return (ENXIO);
1496}
1497
1498/****************************************************************************
1499 * void hdac_irq_free(struct hdac_softc *)
1500 *
1501 * Free up resources previously allocated by hdac_irq_alloc.
1502 ****************************************************************************/
1503static void
1504hdac_irq_free(struct hdac_softc *sc)
1505{
1506	struct hdac_irq *irq;
1507
1508	irq = &sc->irq;
1509	if (irq->irq_res != NULL && irq->irq_handle != NULL)
1510		bus_teardown_intr(sc->dev, irq->irq_res, irq->irq_handle);
1511	if (irq->irq_res != NULL)
1512		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->irq_rid,
1513		    irq->irq_res);
1514	irq->irq_handle = NULL;
1515	irq->irq_res = NULL;
1516}
1517
1518/****************************************************************************
1519 * void hdac_corb_init(struct hdac_softc *)
1520 *
1521 * Initialize the corb registers for operations but do not start it up yet.
1522 * The CORB engine must not be running when this function is called.
1523 ****************************************************************************/
1524static void
1525hdac_corb_init(struct hdac_softc *sc)
1526{
1527	uint8_t corbsize;
1528	uint64_t corbpaddr;
1529
1530	/* Setup the CORB size. */
1531	switch (sc->corb_size) {
1532	case 256:
1533		corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_256);
1534		break;
1535	case 16:
1536		corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_16);
1537		break;
1538	case 2:
1539		corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_2);
1540		break;
1541	default:
1542		panic("%s: Invalid CORB size (%x)\n", __func__, sc->corb_size);
1543	}
1544	HDAC_WRITE_1(&sc->mem, HDAC_CORBSIZE, corbsize);
1545
1546	/* Setup the CORB Address in the hdac */
1547	corbpaddr = (uint64_t)sc->corb_dma.dma_paddr;
1548	HDAC_WRITE_4(&sc->mem, HDAC_CORBLBASE, (uint32_t)corbpaddr);
1549	HDAC_WRITE_4(&sc->mem, HDAC_CORBUBASE, (uint32_t)(corbpaddr >> 32));
1550
1551	/* Set the WP and RP */
1552	sc->corb_wp = 0;
1553	HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
1554	HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, HDAC_CORBRP_CORBRPRST);
1555	/*
1556	 * The HDA specification indicates that the CORBRPRST bit will always
1557	 * read as zero. Unfortunately, it seems that at least the 82801G
1558	 * doesn't reset the bit to zero, which stalls the corb engine.
1559	 * manually reset the bit to zero before continuing.
1560	 */
1561	HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, 0x0);
1562
1563	/* Enable CORB error reporting */
1564#if 0
1565	HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, HDAC_CORBCTL_CMEIE);
1566#endif
1567}
1568
1569/****************************************************************************
1570 * void hdac_rirb_init(struct hdac_softc *)
1571 *
1572 * Initialize the rirb registers for operations but do not start it up yet.
1573 * The RIRB engine must not be running when this function is called.
1574 ****************************************************************************/
1575static void
1576hdac_rirb_init(struct hdac_softc *sc)
1577{
1578	uint8_t rirbsize;
1579	uint64_t rirbpaddr;
1580
1581	/* Setup the RIRB size. */
1582	switch (sc->rirb_size) {
1583	case 256:
1584		rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_256);
1585		break;
1586	case 16:
1587		rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_16);
1588		break;
1589	case 2:
1590		rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_2);
1591		break;
1592	default:
1593		panic("%s: Invalid RIRB size (%x)\n", __func__, sc->rirb_size);
1594	}
1595	HDAC_WRITE_1(&sc->mem, HDAC_RIRBSIZE, rirbsize);
1596
1597	/* Setup the RIRB Address in the hdac */
1598	rirbpaddr = (uint64_t)sc->rirb_dma.dma_paddr;
1599	HDAC_WRITE_4(&sc->mem, HDAC_RIRBLBASE, (uint32_t)rirbpaddr);
1600	HDAC_WRITE_4(&sc->mem, HDAC_RIRBUBASE, (uint32_t)(rirbpaddr >> 32));
1601
1602	/* Setup the WP and RP */
1603	sc->rirb_rp = 0;
1604	HDAC_WRITE_2(&sc->mem, HDAC_RIRBWP, HDAC_RIRBWP_RIRBWPRST);
1605
1606	if (sc->polling == 0) {
1607		/* Setup the interrupt threshold */
1608		HDAC_WRITE_2(&sc->mem, HDAC_RINTCNT, sc->rirb_size / 2);
1609
1610		/* Enable Overrun and response received reporting */
1611#if 0
1612		HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL,
1613		    HDAC_RIRBCTL_RIRBOIC | HDAC_RIRBCTL_RINTCTL);
1614#else
1615		HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, HDAC_RIRBCTL_RINTCTL);
1616#endif
1617	}
1618
1619#if 0
1620	/*
1621	 * Make sure that the Host CPU cache doesn't contain any dirty
1622	 * cache lines that falls in the rirb. If I understood correctly, it
1623	 * should be sufficient to do this only once as the rirb is purely
1624	 * read-only from now on.
1625	 */
1626	bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
1627	    BUS_DMASYNC_PREREAD);
1628#endif
1629}
1630
1631/****************************************************************************
1632 * void hdac_corb_start(hdac_softc *)
1633 *
1634 * Startup the corb DMA engine
1635 ****************************************************************************/
1636static void
1637hdac_corb_start(struct hdac_softc *sc)
1638{
1639	uint32_t corbctl;
1640
1641	corbctl = HDAC_READ_1(&sc->mem, HDAC_CORBCTL);
1642	corbctl |= HDAC_CORBCTL_CORBRUN;
1643	HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, corbctl);
1644}
1645
1646/****************************************************************************
1647 * void hdac_rirb_start(hdac_softc *)
1648 *
1649 * Startup the rirb DMA engine
1650 ****************************************************************************/
1651static void
1652hdac_rirb_start(struct hdac_softc *sc)
1653{
1654	uint32_t rirbctl;
1655
1656	rirbctl = HDAC_READ_1(&sc->mem, HDAC_RIRBCTL);
1657	rirbctl |= HDAC_RIRBCTL_RIRBDMAEN;
1658	HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, rirbctl);
1659}
1660
1661
1662/****************************************************************************
1663 * void hdac_scan_codecs(struct hdac_softc *)
1664 *
1665 * Scan the bus for available codecs.
1666 ****************************************************************************/
1667static void
1668hdac_scan_codecs(struct hdac_softc *sc)
1669{
1670	struct hdac_codec *codec;
1671	int i;
1672	uint16_t statests;
1673
1674	statests = HDAC_READ_2(&sc->mem, HDAC_STATESTS);
1675	for (i = 0; i < HDAC_CODEC_MAX; i++) {
1676		if (HDAC_STATESTS_SDIWAKE(statests, i)) {
1677			/* We have found a codec. */
1678			codec = (struct hdac_codec *)malloc(sizeof(*codec),
1679			    M_HDAC, M_ZERO | M_NOWAIT);
1680			if (codec == NULL) {
1681				device_printf(sc->dev,
1682				    "Unable to allocate memory for codec\n");
1683				continue;
1684			}
1685			codec->commands = NULL;
1686			codec->responses_received = 0;
1687			codec->verbs_sent = 0;
1688			codec->sc = sc;
1689			codec->cad = i;
1690			sc->codecs[i] = codec;
1691			if (hdac_probe_codec(codec) != 0)
1692				break;
1693		}
1694	}
1695	/* All codecs have been probed, now try to attach drivers to them */
1696	/* bus_generic_attach(sc->dev); */
1697}
1698
1699/****************************************************************************
1700 * void hdac_probe_codec(struct hdac_softc *, int)
1701 *
1702 * Probe a the given codec_id for available function groups.
1703 ****************************************************************************/
1704static int
1705hdac_probe_codec(struct hdac_codec *codec)
1706{
1707	struct hdac_softc *sc = codec->sc;
1708	struct hdac_devinfo *devinfo;
1709	uint32_t vendorid, revisionid, subnode;
1710	int startnode;
1711	int endnode;
1712	int i;
1713	nid_t cad = codec->cad;
1714
1715	HDA_BOOTVERBOSE(
1716		device_printf(sc->dev, "HDA_DEBUG: Probing codec: %d\n", cad);
1717	);
1718	vendorid = hdac_command(sc,
1719	    HDA_CMD_GET_PARAMETER(cad, 0x0, HDA_PARAM_VENDOR_ID),
1720	    cad);
1721	revisionid = hdac_command(sc,
1722	    HDA_CMD_GET_PARAMETER(cad, 0x0, HDA_PARAM_REVISION_ID),
1723	    cad);
1724	subnode = hdac_command(sc,
1725	    HDA_CMD_GET_PARAMETER(cad, 0x0, HDA_PARAM_SUB_NODE_COUNT),
1726	    cad);
1727	startnode = HDA_PARAM_SUB_NODE_COUNT_START(subnode);
1728	endnode = startnode + HDA_PARAM_SUB_NODE_COUNT_TOTAL(subnode);
1729
1730	HDA_BOOTVERBOSE(
1731		device_printf(sc->dev, "HDA_DEBUG: \tstartnode=%d endnode=%d\n",
1732		    startnode, endnode);
1733	);
1734	for (i = startnode; i < endnode; i++) {
1735		devinfo = hdac_probe_function(codec, i);
1736		if (devinfo != NULL) {
1737			/* XXX Ignore other FG. */
1738			devinfo->vendor_id =
1739			    HDA_PARAM_VENDOR_ID_VENDOR_ID(vendorid);
1740			devinfo->device_id =
1741			    HDA_PARAM_VENDOR_ID_DEVICE_ID(vendorid);
1742			devinfo->revision_id =
1743			    HDA_PARAM_REVISION_ID_REVISION_ID(revisionid);
1744			devinfo->stepping_id =
1745			    HDA_PARAM_REVISION_ID_STEPPING_ID(revisionid);
1746			HDA_BOOTVERBOSE(
1747				device_printf(sc->dev,
1748				    "HDA_DEBUG: \tFound AFG nid=%d "
1749				    "[startnode=%d endnode=%d]\n",
1750				    devinfo->nid, startnode, endnode);
1751			);
1752			return (1);
1753		}
1754	}
1755
1756	HDA_BOOTVERBOSE(
1757		device_printf(sc->dev, "HDA_DEBUG: \tAFG not found\n");
1758	);
1759	return (0);
1760}
1761
1762static struct hdac_devinfo *
1763hdac_probe_function(struct hdac_codec *codec, nid_t nid)
1764{
1765	struct hdac_softc *sc = codec->sc;
1766	struct hdac_devinfo *devinfo;
1767	uint32_t fctgrptype;
1768	nid_t cad = codec->cad;
1769
1770	fctgrptype = HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE(hdac_command(sc,
1771	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_FCT_GRP_TYPE), cad));
1772
1773	/* XXX For now, ignore other FG. */
1774	if (fctgrptype != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
1775		return (NULL);
1776
1777	devinfo = (struct hdac_devinfo *)malloc(sizeof(*devinfo), M_HDAC,
1778	    M_NOWAIT | M_ZERO);
1779	if (devinfo == NULL) {
1780		device_printf(sc->dev, "%s: Unable to allocate ivar\n",
1781		    __func__);
1782		return (NULL);
1783	}
1784
1785	devinfo->nid = nid;
1786	devinfo->node_type = fctgrptype;
1787	devinfo->codec = codec;
1788
1789	hdac_add_child(sc, devinfo);
1790
1791	return (devinfo);
1792}
1793
1794static void
1795hdac_add_child(struct hdac_softc *sc, struct hdac_devinfo *devinfo)
1796{
1797	devinfo->dev = device_add_child(sc->dev, NULL, -1);
1798	device_set_ivars(devinfo->dev, (void *)devinfo);
1799	/* XXX - Print more information when booting verbose??? */
1800}
1801
1802static void
1803hdac_widget_connection_parse(struct hdac_widget *w)
1804{
1805	struct hdac_softc *sc = w->devinfo->codec->sc;
1806	uint32_t res;
1807	int i, j, max, ents, entnum;
1808	nid_t cad = w->devinfo->codec->cad;
1809	nid_t nid = w->nid;
1810	nid_t cnid, addcnid, prevcnid;
1811
1812	w->nconns = 0;
1813
1814	res = hdac_command(sc,
1815	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_CONN_LIST_LENGTH), cad);
1816
1817	ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res);
1818
1819	if (ents < 1)
1820		return;
1821
1822	entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4;
1823	max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1;
1824	prevcnid = 0;
1825
1826#define CONN_RMASK(e)		(1 << ((32 / (e)) - 1))
1827#define CONN_NMASK(e)		(CONN_RMASK(e) - 1)
1828#define CONN_RESVAL(r, e, n)	((r) >> ((32 / (e)) * (n)))
1829#define CONN_RANGE(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_RMASK(e))
1830#define CONN_CNID(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_NMASK(e))
1831
1832	for (i = 0; i < ents; i += entnum) {
1833		res = hdac_command(sc,
1834		    HDA_CMD_GET_CONN_LIST_ENTRY(cad, nid, i), cad);
1835		for (j = 0; j < entnum; j++) {
1836			cnid = CONN_CNID(res, entnum, j);
1837			if (cnid == 0) {
1838				if (w->nconns < ents)
1839					device_printf(sc->dev,
1840					    "%s: nid=%d WARNING: zero cnid "
1841					    "entnum=%d j=%d index=%d "
1842					    "entries=%d found=%d res=0x%08x\n",
1843					    __func__, nid, entnum, j, i,
1844					    ents, w->nconns, res);
1845				else
1846					goto getconns_out;
1847			}
1848			if (cnid < w->devinfo->startnode ||
1849			    cnid >= w->devinfo->endnode) {
1850				HDA_BOOTVERBOSE(
1851					device_printf(sc->dev,
1852					    "%s: GHOST: nid=%d j=%d "
1853					    "entnum=%d index=%d res=0x%08x\n",
1854					    __func__, nid, j, entnum, i, res);
1855				);
1856			}
1857			if (CONN_RANGE(res, entnum, j) == 0)
1858				addcnid = cnid;
1859			else if (prevcnid == 0 || prevcnid >= cnid) {
1860				device_printf(sc->dev,
1861				    "%s: WARNING: Invalid child range "
1862				    "nid=%d index=%d j=%d entnum=%d "
1863				    "prevcnid=%d cnid=%d res=0x%08x\n",
1864				    __func__, nid, i, j, entnum, prevcnid,
1865				    cnid, res);
1866				addcnid = cnid;
1867			} else
1868				addcnid = prevcnid + 1;
1869			while (addcnid <= cnid) {
1870				if (w->nconns > max) {
1871					device_printf(sc->dev,
1872					    "%s: nid=%d: Adding %d: "
1873					    "Max connection reached! max=%d\n",
1874					    __func__, nid, addcnid, max + 1);
1875					goto getconns_out;
1876				}
1877				w->conns[w->nconns++] = addcnid++;
1878			}
1879			prevcnid = cnid;
1880		}
1881	}
1882
1883getconns_out:
1884	HDA_BOOTVERBOSE(
1885		device_printf(sc->dev,
1886		    "HDA_DEBUG: %s: nid=%d entries=%d found=%d\n",
1887		    __func__, nid, ents, w->nconns);
1888	);
1889	return;
1890}
1891
1892static uint32_t
1893hdac_widget_pin_getconfig(struct hdac_widget *w)
1894{
1895	struct hdac_softc *sc;
1896	uint32_t config, orig, id;
1897	nid_t cad, nid;
1898
1899	sc = w->devinfo->codec->sc;
1900	cad = w->devinfo->codec->cad;
1901	nid = w->nid;
1902	id = hdac_codec_id(w->devinfo);
1903
1904	config = hdac_command(sc,
1905	    HDA_CMD_GET_CONFIGURATION_DEFAULT(cad, nid),
1906	    cad);
1907	orig = config;
1908
1909	/*
1910	 * XXX REWRITE!!!! Don't argue!
1911	 */
1912	if (id == HDA_CODEC_ALC880 && sc->pci_subvendor == LG_LW20_SUBVENDOR) {
1913		switch (nid) {
1914		case 26:
1915			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
1916			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN;
1917			break;
1918		case 27:
1919			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
1920			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT;
1921			break;
1922		default:
1923			break;
1924		}
1925	} else if (id == HDA_CODEC_ALC880 &&
1926	    (sc->pci_subvendor == CLEVO_D900T_SUBVENDOR ||
1927	    sc->pci_subvendor == ASUS_M5200_SUBVENDOR)) {
1928		/*
1929		 * Super broken BIOS
1930		 */
1931		switch (nid) {
1932		case 20:
1933			break;
1934		case 21:
1935			break;
1936		case 22:
1937			break;
1938		case 23:
1939			break;
1940		case 24:	/* MIC1 */
1941			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
1942			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN;
1943			break;
1944		case 25:	/* XXX MIC2 */
1945			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
1946			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN;
1947			break;
1948		case 26:	/* LINE1 */
1949			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
1950			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN;
1951			break;
1952		case 27:	/* XXX LINE2 */
1953			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
1954			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN;
1955			break;
1956		case 28:	/* CD */
1957			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
1958			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_CD;
1959			break;
1960		case 30:
1961			break;
1962		case 31:
1963			break;
1964		default:
1965			break;
1966		}
1967	} else if (id == HDA_CODEC_ALC883 &&
1968	    HDA_DEV_MATCH(ACER_ALL_SUBVENDOR, sc->pci_subvendor)) {
1969		switch (nid) {
1970		case 25:
1971			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
1972			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
1973			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN |
1974			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
1975			break;
1976		case 28:
1977			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
1978			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
1979			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_CD |
1980			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
1981			break;
1982		default:
1983			break;
1984		}
1985	} else if (id == HDA_CODEC_CXVENICE && sc->pci_subvendor ==
1986	    HP_V3000_SUBVENDOR) {
1987		switch (nid) {
1988		case 18:
1989			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
1990			config |= HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE;
1991			break;
1992		case 20:
1993			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
1994			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
1995			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN |
1996			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
1997			break;
1998		case 21:
1999			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2000			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2001			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_CD |
2002			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
2003			break;
2004		default:
2005			break;
2006		}
2007	} else if (id == HDA_CODEC_CXWAIKIKI && sc->pci_subvendor ==
2008	    HP_DV5000_SUBVENDOR) {
2009		switch (nid) {
2010		case 20:
2011		case 21:
2012			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
2013			config |= HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE;
2014			break;
2015		default:
2016			break;
2017		}
2018	} else if (id == HDA_CODEC_ALC861 && sc->pci_subvendor ==
2019	    ASUS_W6F_SUBVENDOR) {
2020		switch (nid) {
2021		case 11:
2022			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2023			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2024			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT |
2025			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED);
2026			break;
2027		case 15:
2028			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2029			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2030			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT |
2031			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK);
2032			break;
2033		default:
2034			break;
2035		}
2036	} else if (id == HDA_CODEC_ALC861 && sc->pci_subvendor ==
2037	    UNIWILL_9075_SUBVENDOR) {
2038		switch (nid) {
2039		case 15:
2040			config &= ~(HDA_CONFIG_DEFAULTCONF_DEVICE_MASK |
2041			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
2042			config |= (HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT |
2043			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK);
2044			break;
2045		default:
2046			break;
2047		}
2048	} else if (id == HDA_CODEC_AD1986A && sc->pci_subvendor ==
2049	    ASUS_M2NPVMX_SUBVENDOR) {
2050		switch (nid) {
2051		case 28:	/* LINE */
2052			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2053			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN;
2054			break;
2055		case 29:	/* MIC */
2056			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2057			config |= HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN;
2058			break;
2059		default:
2060			break;
2061		}
2062	}
2063
2064	HDA_BOOTVERBOSE(
2065		if (config != orig)
2066			device_printf(sc->dev,
2067			    "HDA_DEBUG: Pin config nid=%u 0x%08x -> 0x%08x\n",
2068			    nid, orig, config);
2069	);
2070
2071	return (config);
2072}
2073
2074static uint32_t
2075hdac_widget_pin_getcaps(struct hdac_widget *w)
2076{
2077	struct hdac_softc *sc;
2078	uint32_t caps, orig, id;
2079	nid_t cad, nid;
2080
2081	sc = w->devinfo->codec->sc;
2082	cad = w->devinfo->codec->cad;
2083	nid = w->nid;
2084	id = hdac_codec_id(w->devinfo);
2085
2086	caps = hdac_command(sc,
2087	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_PIN_CAP), cad);
2088	orig = caps;
2089
2090	HDA_BOOTVERBOSE(
2091		if (caps != orig)
2092			device_printf(sc->dev,
2093			    "HDA_DEBUG: Pin caps nid=%u 0x%08x -> 0x%08x\n",
2094			    nid, orig, caps);
2095	);
2096
2097	return (caps);
2098}
2099
2100static void
2101hdac_widget_pin_parse(struct hdac_widget *w)
2102{
2103	struct hdac_softc *sc = w->devinfo->codec->sc;
2104	uint32_t config, pincap;
2105	char *devstr, *connstr;
2106	nid_t cad = w->devinfo->codec->cad;
2107	nid_t nid = w->nid;
2108
2109	config = hdac_widget_pin_getconfig(w);
2110	w->wclass.pin.config = config;
2111
2112	pincap = hdac_widget_pin_getcaps(w);
2113	w->wclass.pin.cap = pincap;
2114
2115	w->wclass.pin.ctrl = hdac_command(sc,
2116	    HDA_CMD_GET_PIN_WIDGET_CTRL(cad, nid), cad) &
2117	    ~(HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
2118	    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
2119	    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
2120	    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
2121
2122	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
2123		w->wclass.pin.ctrl |= HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
2124	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
2125		w->wclass.pin.ctrl |= HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
2126	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
2127		w->wclass.pin.ctrl |= HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
2128	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)) {
2129		w->param.eapdbtl = hdac_command(sc,
2130		    HDA_CMD_GET_EAPD_BTL_ENABLE(cad, nid), cad);
2131		w->param.eapdbtl &= 0x7;
2132		w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2133	} else
2134		w->param.eapdbtl = HDAC_INVALID;
2135
2136	switch (config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
2137	case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
2138		devstr = "line out";
2139		break;
2140	case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
2141		devstr = "speaker";
2142		break;
2143	case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
2144		devstr = "headphones out";
2145		break;
2146	case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
2147		devstr = "CD";
2148		break;
2149	case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
2150		devstr = "SPDIF out";
2151		break;
2152	case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
2153		devstr = "digital (other) out";
2154		break;
2155	case HDA_CONFIG_DEFAULTCONF_DEVICE_MODEM_LINE:
2156		devstr = "modem, line side";
2157		break;
2158	case HDA_CONFIG_DEFAULTCONF_DEVICE_MODEM_HANDSET:
2159		devstr = "modem, handset side";
2160		break;
2161	case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
2162		devstr = "line in";
2163		break;
2164	case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
2165		devstr = "AUX";
2166		break;
2167	case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
2168		devstr = "Mic in";
2169		break;
2170	case HDA_CONFIG_DEFAULTCONF_DEVICE_TELEPHONY:
2171		devstr = "telephony";
2172		break;
2173	case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
2174		devstr = "SPDIF in";
2175		break;
2176	case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
2177		devstr = "digital (other) in";
2178		break;
2179	case HDA_CONFIG_DEFAULTCONF_DEVICE_OTHER:
2180		devstr = "other";
2181		break;
2182	default:
2183		devstr = "unknown";
2184		break;
2185	}
2186
2187	switch (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) {
2188	case HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK:
2189		connstr = "jack";
2190		break;
2191	case HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE:
2192		connstr = "none";
2193		break;
2194	case HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_FIXED:
2195		connstr = "fixed";
2196		break;
2197	case HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_BOTH:
2198		connstr = "jack / fixed";
2199		break;
2200	default:
2201		connstr = "unknown";
2202		break;
2203	}
2204
2205	strlcat(w->name, ": ", sizeof(w->name));
2206	strlcat(w->name, devstr, sizeof(w->name));
2207	strlcat(w->name, " (", sizeof(w->name));
2208	strlcat(w->name, connstr, sizeof(w->name));
2209	strlcat(w->name, ")", sizeof(w->name));
2210}
2211
2212static void
2213hdac_widget_parse(struct hdac_widget *w)
2214{
2215	struct hdac_softc *sc = w->devinfo->codec->sc;
2216	uint32_t wcap, cap;
2217	char *typestr;
2218	nid_t cad = w->devinfo->codec->cad;
2219	nid_t nid = w->nid;
2220
2221	wcap = hdac_command(sc,
2222	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_AUDIO_WIDGET_CAP),
2223	    cad);
2224	w->param.widget_cap = wcap;
2225	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap);
2226
2227	switch (w->type) {
2228	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
2229		typestr = "audio output";
2230		break;
2231	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
2232		typestr = "audio input";
2233		break;
2234	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2235		typestr = "audio mixer";
2236		break;
2237	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2238		typestr = "audio selector";
2239		break;
2240	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
2241		typestr = "pin";
2242		break;
2243	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET:
2244		typestr = "power widget";
2245		break;
2246	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET:
2247		typestr = "volume widget";
2248		break;
2249	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
2250		typestr = "beep widget";
2251		break;
2252	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET:
2253		typestr = "vendor widget";
2254		break;
2255	default:
2256		typestr = "unknown type";
2257		break;
2258	}
2259
2260	strlcpy(w->name, typestr, sizeof(w->name));
2261
2262	if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(wcap)) {
2263		hdac_command(sc,
2264		    HDA_CMD_SET_POWER_STATE(cad, nid, HDA_CMD_POWER_STATE_D0),
2265		    cad);
2266		DELAY(1000);
2267	}
2268
2269	hdac_widget_connection_parse(w);
2270
2271	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) {
2272		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
2273			w->param.outamp_cap =
2274			    hdac_command(sc,
2275			    HDA_CMD_GET_PARAMETER(cad, nid,
2276			    HDA_PARAM_OUTPUT_AMP_CAP), cad);
2277		else
2278			w->param.outamp_cap =
2279			    w->devinfo->function.audio.outamp_cap;
2280	} else
2281		w->param.outamp_cap = 0;
2282
2283	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) {
2284		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
2285			w->param.inamp_cap =
2286			    hdac_command(sc,
2287			    HDA_CMD_GET_PARAMETER(cad, nid,
2288			    HDA_PARAM_INPUT_AMP_CAP), cad);
2289		else
2290			w->param.inamp_cap =
2291			    w->devinfo->function.audio.inamp_cap;
2292	} else
2293		w->param.inamp_cap = 0;
2294
2295	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
2296	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2297		if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) {
2298			cap = hdac_command(sc,
2299			    HDA_CMD_GET_PARAMETER(cad, nid,
2300			    HDA_PARAM_SUPP_STREAM_FORMATS), cad);
2301			w->param.supp_stream_formats = (cap != 0) ? cap :
2302			    w->devinfo->function.audio.supp_stream_formats;
2303			cap = hdac_command(sc,
2304			    HDA_CMD_GET_PARAMETER(cad, nid,
2305			    HDA_PARAM_SUPP_PCM_SIZE_RATE), cad);
2306			w->param.supp_pcm_size_rate = (cap != 0) ? cap :
2307			    w->devinfo->function.audio.supp_pcm_size_rate;
2308		} else {
2309			w->param.supp_stream_formats =
2310			    w->devinfo->function.audio.supp_stream_formats;
2311			w->param.supp_pcm_size_rate =
2312			    w->devinfo->function.audio.supp_pcm_size_rate;
2313		}
2314	} else {
2315		w->param.supp_stream_formats = 0;
2316		w->param.supp_pcm_size_rate = 0;
2317	}
2318
2319	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2320		hdac_widget_pin_parse(w);
2321}
2322
2323static struct hdac_widget *
2324hdac_widget_get(struct hdac_devinfo *devinfo, nid_t nid)
2325{
2326	if (devinfo == NULL || devinfo->widget == NULL ||
2327		    nid < devinfo->startnode || nid >= devinfo->endnode)
2328		return (NULL);
2329	return (&devinfo->widget[nid - devinfo->startnode]);
2330}
2331
2332static __inline int
2333hda_poll_channel(struct hdac_chan *ch)
2334{
2335	uint32_t sz, delta;
2336	volatile uint32_t ptr;
2337
2338	if (ch->active == 0)
2339		return (0);
2340
2341	sz = ch->blksz * ch->blkcnt;
2342	if (ch->dmapos != NULL)
2343		ptr = *(ch->dmapos);
2344	else
2345		ptr = HDAC_READ_4(&ch->devinfo->codec->sc->mem,
2346		    ch->off + HDAC_SDLPIB);
2347	ch->ptr = ptr;
2348	ptr %= sz;
2349	ptr &= ~(ch->blksz - 1);
2350	delta = (sz + ptr - ch->prevptr) % sz;
2351
2352	if (delta < ch->blksz)
2353		return (0);
2354
2355	ch->prevptr = ptr;
2356
2357	return (1);
2358}
2359
2360#define hda_chan_active(sc)	((sc)->play.active + (sc)->rec.active)
2361
2362static void
2363hda_poll_callback(void *arg)
2364{
2365	struct hdac_softc *sc = arg;
2366	uint32_t trigger = 0;
2367
2368	if (sc == NULL)
2369		return;
2370
2371	hdac_lock(sc);
2372	if (sc->polling == 0 || hda_chan_active(sc) == 0) {
2373		hdac_unlock(sc);
2374		return;
2375	}
2376
2377	trigger |= (hda_poll_channel(&sc->play) != 0) ? 1 : 0;
2378	trigger |= (hda_poll_channel(&sc->rec) != 0) ? 2 : 0;
2379
2380	/* XXX */
2381	callout_reset(&sc->poll_hda, 1/*sc->poll_ticks*/,
2382	    hda_poll_callback, sc);
2383
2384	hdac_unlock(sc);
2385
2386	if (trigger & 1)
2387		chn_intr(sc->play.c);
2388	if (trigger & 2)
2389		chn_intr(sc->rec.c);
2390}
2391
2392static int
2393hdac_rirb_flush(struct hdac_softc *sc)
2394{
2395	struct hdac_rirb *rirb_base, *rirb;
2396	struct hdac_codec *codec;
2397	struct hdac_command_list *commands;
2398	nid_t cad;
2399	uint32_t resp;
2400	uint8_t rirbwp;
2401	int ret = 0;
2402
2403	rirb_base = (struct hdac_rirb *)sc->rirb_dma.dma_vaddr;
2404	rirbwp = HDAC_READ_1(&sc->mem, HDAC_RIRBWP);
2405#if 0
2406	bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
2407	    BUS_DMASYNC_POSTREAD);
2408#endif
2409
2410	while (sc->rirb_rp != rirbwp) {
2411		sc->rirb_rp++;
2412		sc->rirb_rp %= sc->rirb_size;
2413		rirb = &rirb_base[sc->rirb_rp];
2414		cad = HDAC_RIRB_RESPONSE_EX_SDATA_IN(rirb->response_ex);
2415		if (cad < 0 || cad >= HDAC_CODEC_MAX ||
2416		    sc->codecs[cad] == NULL)
2417			continue;
2418		resp = rirb->response;
2419		codec = sc->codecs[cad];
2420		commands = codec->commands;
2421		if (rirb->response_ex & HDAC_RIRB_RESPONSE_EX_UNSOLICITED) {
2422			sc->unsolq[sc->unsolq_wp++] = (cad << 16) |
2423			    ((resp >> 26) & 0xffff);
2424			sc->unsolq_wp %= HDAC_UNSOLQ_MAX;
2425		} else if (commands != NULL && commands->num_commands > 0 &&
2426		    codec->responses_received < commands->num_commands)
2427			commands->responses[codec->responses_received++] =
2428			    resp;
2429		ret++;
2430	}
2431
2432	return (ret);
2433}
2434
2435static int
2436hdac_unsolq_flush(struct hdac_softc *sc)
2437{
2438	nid_t cad;
2439	uint32_t tag;
2440	int ret = 0;
2441
2442	if (sc->unsolq_st == HDAC_UNSOLQ_READY) {
2443		sc->unsolq_st = HDAC_UNSOLQ_BUSY;
2444		while (sc->unsolq_rp != sc->unsolq_wp) {
2445			cad = sc->unsolq[sc->unsolq_rp] >> 16;
2446			tag = sc->unsolq[sc->unsolq_rp++] & 0xffff;
2447			sc->unsolq_rp %= HDAC_UNSOLQ_MAX;
2448			hdac_unsolicited_handler(sc->codecs[cad], tag);
2449			ret++;
2450		}
2451		sc->unsolq_st = HDAC_UNSOLQ_READY;
2452	}
2453
2454	return (ret);
2455}
2456
2457static void
2458hdac_poll_callback(void *arg)
2459{
2460	struct hdac_softc *sc = arg;
2461	if (sc == NULL)
2462		return;
2463
2464	hdac_lock(sc);
2465	if (sc->polling == 0 || sc->poll_ival == 0) {
2466		hdac_unlock(sc);
2467		return;
2468	}
2469	hdac_rirb_flush(sc);
2470	hdac_unsolq_flush(sc);
2471	callout_reset(&sc->poll_hdac, sc->poll_ival, hdac_poll_callback, sc);
2472	hdac_unlock(sc);
2473}
2474
2475static void
2476hdac_stream_stop(struct hdac_chan *ch)
2477{
2478	struct hdac_softc *sc = ch->devinfo->codec->sc;
2479	uint32_t ctl;
2480
2481	ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
2482	ctl &= ~(HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
2483	    HDAC_SDCTL_RUN);
2484	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL0, ctl);
2485
2486	ch->active = 0;
2487
2488	if (sc->polling != 0) {
2489		int pollticks;
2490
2491		if (hda_chan_active(sc) == 0) {
2492			callout_stop(&sc->poll_hda);
2493			sc->poll_ticks = 1;
2494		} else {
2495			if (sc->play.active != 0)
2496				ch = &sc->play;
2497			else
2498				ch = &sc->rec;
2499			pollticks = ((uint64_t)hz * ch->blksz) /
2500			    ((uint64_t)sndbuf_getbps(ch->b) *
2501			    sndbuf_getspd(ch->b));
2502			pollticks >>= 2;
2503			if (pollticks > hz)
2504				pollticks = hz;
2505			if (pollticks < 1) {
2506				HDA_BOOTVERBOSE(
2507					device_printf(sc->dev,
2508					    "%s: pollticks=%d < 1 !\n",
2509					    __func__, pollticks);
2510				);
2511				pollticks = 1;
2512			}
2513			if (pollticks > sc->poll_ticks) {
2514				HDA_BOOTVERBOSE(
2515					device_printf(sc->dev,
2516					    "%s: pollticks %d -> %d\n",
2517					    __func__, sc->poll_ticks,
2518					    pollticks);
2519				);
2520				sc->poll_ticks = pollticks;
2521				callout_reset(&sc->poll_hda, 1,
2522				    hda_poll_callback, sc);
2523			}
2524		}
2525	} else {
2526		ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
2527		ctl &= ~(1 << (ch->off >> 5));
2528		HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
2529	}
2530}
2531
2532static void
2533hdac_stream_start(struct hdac_chan *ch)
2534{
2535	struct hdac_softc *sc = ch->devinfo->codec->sc;
2536	uint32_t ctl;
2537
2538	if (sc->polling != 0) {
2539		int pollticks;
2540
2541		pollticks = ((uint64_t)hz * ch->blksz) /
2542		    ((uint64_t)sndbuf_getbps(ch->b) * sndbuf_getspd(ch->b));
2543		pollticks >>= 2;
2544		if (pollticks > hz)
2545			pollticks = hz;
2546		if (pollticks < 1) {
2547			HDA_BOOTVERBOSE(
2548				device_printf(sc->dev,
2549				    "%s: pollticks=%d < 1 !\n",
2550				    __func__, pollticks);
2551			);
2552			pollticks = 1;
2553		}
2554		if (hda_chan_active(sc) == 0 || pollticks < sc->poll_ticks) {
2555			HDA_BOOTVERBOSE(
2556				if (hda_chan_active(sc) == 0) {
2557					device_printf(sc->dev,
2558					    "%s: pollticks=%d\n",
2559					    __func__, pollticks);
2560				} else {
2561					device_printf(sc->dev,
2562					    "%s: pollticks %d -> %d\n",
2563					    __func__, sc->poll_ticks,
2564					    pollticks);
2565				}
2566			);
2567			sc->poll_ticks = pollticks;
2568			callout_reset(&sc->poll_hda, 1, hda_poll_callback,
2569			    sc);
2570		}
2571		ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
2572		ctl |= HDAC_SDCTL_RUN;
2573	} else {
2574		ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
2575		ctl |= 1 << (ch->off >> 5);
2576		HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
2577		ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
2578		ctl |= HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
2579		    HDAC_SDCTL_RUN;
2580	}
2581	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL0, ctl);
2582
2583	ch->active = 1;
2584}
2585
2586static void
2587hdac_stream_reset(struct hdac_chan *ch)
2588{
2589	struct hdac_softc *sc = ch->devinfo->codec->sc;
2590	int timeout = 1000;
2591	int to = timeout;
2592	uint32_t ctl;
2593
2594	ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
2595	ctl |= HDAC_SDCTL_SRST;
2596	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL0, ctl);
2597	do {
2598		ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
2599		if (ctl & HDAC_SDCTL_SRST)
2600			break;
2601		DELAY(10);
2602	} while (--to);
2603	if (!(ctl & HDAC_SDCTL_SRST)) {
2604		device_printf(sc->dev, "timeout in reset\n");
2605	}
2606	ctl &= ~HDAC_SDCTL_SRST;
2607	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL0, ctl);
2608	to = timeout;
2609	do {
2610		ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL0);
2611		if (!(ctl & HDAC_SDCTL_SRST))
2612			break;
2613		DELAY(10);
2614	} while (--to);
2615	if (ctl & HDAC_SDCTL_SRST)
2616		device_printf(sc->dev, "can't reset!\n");
2617}
2618
2619static void
2620hdac_stream_setid(struct hdac_chan *ch)
2621{
2622	struct hdac_softc *sc = ch->devinfo->codec->sc;
2623	uint32_t ctl;
2624
2625	ctl = HDAC_READ_1(&sc->mem, ch->off + HDAC_SDCTL2);
2626	ctl &= ~HDAC_SDCTL2_STRM_MASK;
2627	ctl |= ch->sid << HDAC_SDCTL2_STRM_SHIFT;
2628	HDAC_WRITE_1(&sc->mem, ch->off + HDAC_SDCTL2, ctl);
2629}
2630
2631static void
2632hdac_bdl_setup(struct hdac_chan *ch)
2633{
2634	struct hdac_softc *sc = ch->devinfo->codec->sc;
2635	struct hdac_bdle *bdle;
2636	uint64_t addr;
2637	uint32_t blksz, blkcnt;
2638	int i;
2639
2640	addr = (uint64_t)sndbuf_getbufaddr(ch->b);
2641	bdle = (struct hdac_bdle *)ch->bdl_dma.dma_vaddr;
2642
2643	if (sc->polling != 0) {
2644		blksz = ch->blksz * ch->blkcnt;
2645		blkcnt = 1;
2646	} else {
2647		blksz = ch->blksz;
2648		blkcnt = ch->blkcnt;
2649	}
2650
2651	for (i = 0; i < blkcnt; i++, bdle++) {
2652		bdle->addrl = (uint32_t)addr;
2653		bdle->addrh = (uint32_t)(addr >> 32);
2654		bdle->len = blksz;
2655		bdle->ioc = 1 ^ sc->polling;
2656		addr += blksz;
2657	}
2658
2659	HDAC_WRITE_4(&sc->mem, ch->off + HDAC_SDCBL, blksz * blkcnt);
2660	HDAC_WRITE_2(&sc->mem, ch->off + HDAC_SDLVI, blkcnt - 1);
2661	addr = ch->bdl_dma.dma_paddr;
2662	HDAC_WRITE_4(&sc->mem, ch->off + HDAC_SDBDPL, (uint32_t)addr);
2663	HDAC_WRITE_4(&sc->mem, ch->off + HDAC_SDBDPU, (uint32_t)(addr >> 32));
2664	if (ch->dmapos != NULL &&
2665	    !(HDAC_READ_4(&sc->mem, HDAC_DPIBLBASE) & 0x00000001)) {
2666		addr = sc->pos_dma.dma_paddr;
2667		HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE,
2668		    ((uint32_t)addr & HDAC_DPLBASE_DPLBASE_MASK) | 0x00000001);
2669		HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, (uint32_t)(addr >> 32));
2670	}
2671}
2672
2673static int
2674hdac_bdl_alloc(struct hdac_chan *ch)
2675{
2676	struct hdac_softc *sc = ch->devinfo->codec->sc;
2677	int rc;
2678
2679	rc = hdac_dma_alloc(sc, &ch->bdl_dma,
2680	    sizeof(struct hdac_bdle) * HDA_BDL_MAX);
2681	if (rc) {
2682		device_printf(sc->dev, "can't alloc bdl\n");
2683		return (rc);
2684	}
2685
2686	return (0);
2687}
2688
2689static void
2690hdac_audio_ctl_amp_set_internal(struct hdac_softc *sc, nid_t cad, nid_t nid,
2691					int index, int lmute, int rmute,
2692					int left, int right, int dir)
2693{
2694	uint16_t v = 0;
2695
2696	if (sc == NULL)
2697		return;
2698
2699	if (left != right || lmute != rmute) {
2700		v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
2701		    (lmute << 7) | left;
2702		hdac_command(sc,
2703		    HDA_CMD_SET_AMP_GAIN_MUTE(cad, nid, v), cad);
2704		v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
2705		    (rmute << 7) | right;
2706	} else
2707		v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
2708		    (lmute << 7) | left;
2709
2710	hdac_command(sc,
2711	    HDA_CMD_SET_AMP_GAIN_MUTE(cad, nid, v), cad);
2712}
2713
2714static void
2715hdac_audio_ctl_amp_set(struct hdac_audio_ctl *ctl, uint32_t mute,
2716						int left, int right)
2717{
2718	struct hdac_softc *sc;
2719	nid_t nid, cad;
2720	int lmute, rmute;
2721
2722	if (ctl == NULL || ctl->widget == NULL ||
2723	    ctl->widget->devinfo == NULL ||
2724	    ctl->widget->devinfo->codec == NULL ||
2725	    ctl->widget->devinfo->codec->sc == NULL)
2726		return;
2727
2728	sc = ctl->widget->devinfo->codec->sc;
2729	cad = ctl->widget->devinfo->codec->cad;
2730	nid = ctl->widget->nid;
2731
2732	if (mute == HDA_AMP_MUTE_DEFAULT) {
2733		lmute = HDA_AMP_LEFT_MUTED(ctl->muted);
2734		rmute = HDA_AMP_RIGHT_MUTED(ctl->muted);
2735	} else {
2736		lmute = HDA_AMP_LEFT_MUTED(mute);
2737		rmute = HDA_AMP_RIGHT_MUTED(mute);
2738	}
2739
2740	if (ctl->dir & HDA_CTL_OUT)
2741		hdac_audio_ctl_amp_set_internal(sc, cad, nid, ctl->index,
2742		    lmute, rmute, left, right, 0);
2743	if (ctl->dir & HDA_CTL_IN)
2744		hdac_audio_ctl_amp_set_internal(sc, cad, nid, ctl->index,
2745		    lmute, rmute, left, right, 1);
2746	ctl->left = left;
2747	ctl->right = right;
2748}
2749
2750static void
2751hdac_widget_connection_select(struct hdac_widget *w, uint8_t index)
2752{
2753	if (w == NULL || w->nconns < 1 || index > (w->nconns - 1))
2754		return;
2755	hdac_command(w->devinfo->codec->sc,
2756	    HDA_CMD_SET_CONNECTION_SELECT_CONTROL(w->devinfo->codec->cad,
2757	    w->nid, index), w->devinfo->codec->cad);
2758	w->selconn = index;
2759}
2760
2761
2762/****************************************************************************
2763 * uint32_t hdac_command_sendone_internal
2764 *
2765 * Wrapper function that sends only one command to a given codec
2766 ****************************************************************************/
2767static uint32_t
2768hdac_command_sendone_internal(struct hdac_softc *sc, uint32_t verb, nid_t cad)
2769{
2770	struct hdac_command_list cl;
2771	uint32_t response = HDAC_INVALID;
2772
2773	if (!hdac_lockowned(sc))
2774		device_printf(sc->dev, "WARNING!!!! mtx not owned!!!!\n");
2775	cl.num_commands = 1;
2776	cl.verbs = &verb;
2777	cl.responses = &response;
2778
2779	hdac_command_send_internal(sc, &cl, cad);
2780
2781	return (response);
2782}
2783
2784/****************************************************************************
2785 * hdac_command_send_internal
2786 *
2787 * Send a command list to the codec via the corb. We queue as much verbs as
2788 * we can and msleep on the codec. When the interrupt get the responses
2789 * back from the rirb, it will wake us up so we can queue the remaining verbs
2790 * if any.
2791 ****************************************************************************/
2792static void
2793hdac_command_send_internal(struct hdac_softc *sc,
2794			struct hdac_command_list *commands, nid_t cad)
2795{
2796	struct hdac_codec *codec;
2797	int corbrp;
2798	uint32_t *corb;
2799	int timeout;
2800	int retry = 10;
2801	struct hdac_rirb *rirb_base;
2802
2803	if (sc == NULL || sc->codecs[cad] == NULL || commands == NULL ||
2804	    commands->num_commands < 1)
2805		return;
2806
2807	codec = sc->codecs[cad];
2808	codec->commands = commands;
2809	codec->responses_received = 0;
2810	codec->verbs_sent = 0;
2811	corb = (uint32_t *)sc->corb_dma.dma_vaddr;
2812	rirb_base = (struct hdac_rirb *)sc->rirb_dma.dma_vaddr;
2813
2814	do {
2815		if (codec->verbs_sent != commands->num_commands) {
2816			/* Queue as many verbs as possible */
2817			corbrp = HDAC_READ_2(&sc->mem, HDAC_CORBRP);
2818#if 0
2819			bus_dmamap_sync(sc->corb_dma.dma_tag,
2820			    sc->corb_dma.dma_map, BUS_DMASYNC_PREWRITE);
2821#endif
2822			while (codec->verbs_sent != commands->num_commands &&
2823			    ((sc->corb_wp + 1) % sc->corb_size) != corbrp) {
2824				sc->corb_wp++;
2825				sc->corb_wp %= sc->corb_size;
2826				corb[sc->corb_wp] =
2827				    commands->verbs[codec->verbs_sent++];
2828			}
2829
2830			/* Send the verbs to the codecs */
2831#if 0
2832			bus_dmamap_sync(sc->corb_dma.dma_tag,
2833			    sc->corb_dma.dma_map, BUS_DMASYNC_POSTWRITE);
2834#endif
2835			HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
2836		}
2837
2838		timeout = 1000;
2839		while (hdac_rirb_flush(sc) == 0 && --timeout)
2840			DELAY(10);
2841	} while ((codec->verbs_sent != commands->num_commands ||
2842	    codec->responses_received != commands->num_commands) && --retry);
2843
2844	if (retry == 0)
2845		device_printf(sc->dev,
2846		    "%s: TIMEOUT numcmd=%d, sent=%d, received=%d\n",
2847		    __func__, commands->num_commands, codec->verbs_sent,
2848		    codec->responses_received);
2849
2850	codec->commands = NULL;
2851	codec->responses_received = 0;
2852	codec->verbs_sent = 0;
2853
2854	hdac_unsolq_flush(sc);
2855}
2856
2857
2858/****************************************************************************
2859 * Device Methods
2860 ****************************************************************************/
2861
2862/****************************************************************************
2863 * int hdac_probe(device_t)
2864 *
2865 * Probe for the presence of an hdac. If none is found, check for a generic
2866 * match using the subclass of the device.
2867 ****************************************************************************/
2868static int
2869hdac_probe(device_t dev)
2870{
2871	int i, result;
2872	uint32_t model;
2873	uint16_t class, subclass;
2874	char desc[64];
2875
2876	model = (uint32_t)pci_get_device(dev) << 16;
2877	model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
2878	class = pci_get_class(dev);
2879	subclass = pci_get_subclass(dev);
2880
2881	bzero(desc, sizeof(desc));
2882	result = ENXIO;
2883	for (i = 0; i < HDAC_DEVICES_LEN; i++) {
2884		if (hdac_devices[i].model == model) {
2885		    	strlcpy(desc, hdac_devices[i].desc, sizeof(desc));
2886		    	result = BUS_PROBE_DEFAULT;
2887			break;
2888		}
2889		if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
2890		    class == PCIC_MULTIMEDIA &&
2891		    subclass == PCIS_MULTIMEDIA_HDA) {
2892		    	strlcpy(desc, hdac_devices[i].desc, sizeof(desc));
2893		    	result = BUS_PROBE_GENERIC;
2894			break;
2895		}
2896	}
2897	if (result == ENXIO && class == PCIC_MULTIMEDIA &&
2898	    subclass == PCIS_MULTIMEDIA_HDA) {
2899		strlcpy(desc, "Generic", sizeof(desc));
2900	    	result = BUS_PROBE_GENERIC;
2901	}
2902	if (result != ENXIO) {
2903		strlcat(desc, " High Definition Audio Controller",
2904		    sizeof(desc));
2905		device_set_desc_copy(dev, desc);
2906	}
2907
2908	return (result);
2909}
2910
2911static void *
2912hdac_channel_init(kobj_t obj, void *data, struct snd_dbuf *b,
2913					struct pcm_channel *c, int dir)
2914{
2915	struct hdac_devinfo *devinfo = data;
2916	struct hdac_softc *sc = devinfo->codec->sc;
2917	struct hdac_chan *ch;
2918
2919	hdac_lock(sc);
2920	if (dir == PCMDIR_PLAY) {
2921		ch = &sc->play;
2922		ch->off = (sc->num_iss + devinfo->function.audio.playcnt) << 5;
2923		devinfo->function.audio.playcnt++;
2924	} else {
2925		ch = &sc->rec;
2926		ch->off = devinfo->function.audio.reccnt << 5;
2927		devinfo->function.audio.reccnt++;
2928	}
2929	if (devinfo->function.audio.quirks & HDA_QUIRK_FIXEDRATE) {
2930		ch->caps.minspeed = ch->caps.maxspeed = 48000;
2931		ch->pcmrates[0] = 48000;
2932		ch->pcmrates[1] = 0;
2933	}
2934	if (sc->pos_dma.dma_vaddr != NULL)
2935		ch->dmapos = (uint32_t *)(sc->pos_dma.dma_vaddr +
2936		    (sc->streamcnt * 8));
2937	else
2938		ch->dmapos = NULL;
2939	ch->sid = ++sc->streamcnt;
2940	ch->dir = dir;
2941	ch->b = b;
2942	ch->c = c;
2943	ch->devinfo = devinfo;
2944	ch->blksz = sc->chan_size / sc->chan_blkcnt;
2945	ch->blkcnt = sc->chan_blkcnt;
2946	hdac_unlock(sc);
2947
2948	if (hdac_bdl_alloc(ch) != 0) {
2949		ch->blkcnt = 0;
2950		return (NULL);
2951	}
2952
2953	if (sndbuf_alloc(ch->b, sc->chan_dmat,
2954	    (sc->nocache != 0) ? BUS_DMA_NOCACHE : 0, sc->chan_size) != 0)
2955		return (NULL);
2956
2957	return (ch);
2958}
2959
2960static int
2961hdac_channel_setformat(kobj_t obj, void *data, uint32_t format)
2962{
2963	struct hdac_chan *ch = data;
2964	int i;
2965
2966	for (i = 0; ch->caps.fmtlist[i] != 0; i++) {
2967		if (format == ch->caps.fmtlist[i]) {
2968			ch->fmt = format;
2969			return (0);
2970		}
2971	}
2972
2973	return (EINVAL);
2974}
2975
2976static int
2977hdac_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
2978{
2979	struct hdac_chan *ch = data;
2980	uint32_t spd = 0, threshold;
2981	int i;
2982
2983	for (i = 0; ch->pcmrates[i] != 0; i++) {
2984		spd = ch->pcmrates[i];
2985		threshold = spd + ((ch->pcmrates[i + 1] != 0) ?
2986		    ((ch->pcmrates[i + 1] - spd) >> 1) : 0);
2987		if (speed < threshold)
2988			break;
2989	}
2990
2991	if (spd == 0)	/* impossible */
2992		ch->spd = 48000;
2993	else
2994		ch->spd = spd;
2995
2996	return (ch->spd);
2997}
2998
2999static void
3000hdac_stream_setup(struct hdac_chan *ch)
3001{
3002	struct hdac_softc *sc = ch->devinfo->codec->sc;
3003	int i;
3004	nid_t cad = ch->devinfo->codec->cad;
3005	uint16_t fmt;
3006
3007	fmt = 0;
3008	if (ch->fmt & AFMT_S16_LE)
3009		fmt |= ch->bit16 << 4;
3010	else if (ch->fmt & AFMT_S32_LE)
3011		fmt |= ch->bit32 << 4;
3012	else
3013		fmt |= 1 << 4;
3014
3015	for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
3016		if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
3017			fmt |= hda_rate_tab[i].base;
3018			fmt |= hda_rate_tab[i].mul;
3019			fmt |= hda_rate_tab[i].div;
3020			break;
3021		}
3022	}
3023
3024	if (ch->fmt & AFMT_STEREO)
3025		fmt |= 1;
3026
3027	HDAC_WRITE_2(&sc->mem, ch->off + HDAC_SDFMT, fmt);
3028
3029	for (i = 0; ch->io[i] != -1; i++) {
3030		HDA_BOOTVERBOSE(
3031			device_printf(sc->dev,
3032			    "HDA_DEBUG: PCMDIR_%s: Stream setup nid=%d "
3033			    "fmt=0x%08x\n",
3034			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
3035			    ch->io[i], fmt);
3036		);
3037		hdac_command(sc,
3038		    HDA_CMD_SET_CONV_FMT(cad, ch->io[i], fmt), cad);
3039		hdac_command(sc,
3040		    HDA_CMD_SET_CONV_STREAM_CHAN(cad, ch->io[i],
3041		    ch->sid << 4), cad);
3042	}
3043}
3044
3045static int
3046hdac_channel_setfragments(kobj_t obj, void *data,
3047					uint32_t blksz, uint32_t blkcnt)
3048{
3049	struct hdac_chan *ch = data;
3050	struct hdac_softc *sc = ch->devinfo->codec->sc;
3051
3052	blksz &= HDA_BLK_ALIGN;
3053
3054	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
3055		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
3056	if (blksz < HDA_BLK_MIN)
3057		blksz = HDA_BLK_MIN;
3058	if (blkcnt > HDA_BDL_MAX)
3059		blkcnt = HDA_BDL_MAX;
3060	if (blkcnt < HDA_BDL_MIN)
3061		blkcnt = HDA_BDL_MIN;
3062
3063	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
3064		if ((blkcnt >> 1) >= HDA_BDL_MIN)
3065			blkcnt >>= 1;
3066		else if ((blksz >> 1) >= HDA_BLK_MIN)
3067			blksz >>= 1;
3068		else
3069			break;
3070	}
3071
3072	if ((sndbuf_getblksz(ch->b) != blksz ||
3073	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
3074	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
3075		device_printf(sc->dev, "%s: failed blksz=%u blkcnt=%u\n",
3076		    __func__, blksz, blkcnt);
3077
3078	ch->blksz = sndbuf_getblksz(ch->b);
3079	ch->blkcnt = sndbuf_getblkcnt(ch->b);
3080
3081	return (1);
3082}
3083
3084static int
3085hdac_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
3086{
3087	struct hdac_chan *ch = data;
3088	struct hdac_softc *sc = ch->devinfo->codec->sc;
3089
3090	hdac_channel_setfragments(obj, data, blksz, sc->chan_blkcnt);
3091
3092	return (ch->blksz);
3093}
3094
3095static void
3096hdac_channel_stop(struct hdac_softc *sc, struct hdac_chan *ch)
3097{
3098	struct hdac_devinfo *devinfo = ch->devinfo;
3099	nid_t cad = devinfo->codec->cad;
3100	int i;
3101
3102	hdac_stream_stop(ch);
3103
3104	for (i = 0; ch->io[i] != -1; i++) {
3105		hdac_command(sc,
3106		    HDA_CMD_SET_CONV_STREAM_CHAN(cad, ch->io[i],
3107		    0), cad);
3108	}
3109}
3110
3111static void
3112hdac_channel_start(struct hdac_softc *sc, struct hdac_chan *ch)
3113{
3114	ch->ptr = 0;
3115	ch->prevptr = 0;
3116	hdac_stream_stop(ch);
3117	hdac_stream_reset(ch);
3118	hdac_bdl_setup(ch);
3119	hdac_stream_setid(ch);
3120	hdac_stream_setup(ch);
3121	hdac_stream_start(ch);
3122}
3123
3124static int
3125hdac_channel_trigger(kobj_t obj, void *data, int go)
3126{
3127	struct hdac_chan *ch = data;
3128	struct hdac_softc *sc = ch->devinfo->codec->sc;
3129
3130	hdac_lock(sc);
3131	switch (go) {
3132	case PCMTRIG_START:
3133		hdac_channel_start(sc, ch);
3134		break;
3135	case PCMTRIG_STOP:
3136	case PCMTRIG_ABORT:
3137		hdac_channel_stop(sc, ch);
3138		break;
3139	default:
3140		break;
3141	}
3142	hdac_unlock(sc);
3143
3144	return (0);
3145}
3146
3147static int
3148hdac_channel_getptr(kobj_t obj, void *data)
3149{
3150	struct hdac_chan *ch = data;
3151	struct hdac_softc *sc = ch->devinfo->codec->sc;
3152	uint32_t ptr;
3153
3154	hdac_lock(sc);
3155	if (sc->polling != 0)
3156		ptr = ch->ptr;
3157	else if (ch->dmapos != NULL)
3158		ptr = *(ch->dmapos);
3159	else
3160		ptr = HDAC_READ_4(&sc->mem, ch->off + HDAC_SDLPIB);
3161	hdac_unlock(sc);
3162
3163	/*
3164	 * Round to available space and force 128 bytes aligment.
3165	 */
3166	ptr %= ch->blksz * ch->blkcnt;
3167	ptr &= HDA_BLK_ALIGN;
3168
3169	return (ptr);
3170}
3171
3172static struct pcmchan_caps *
3173hdac_channel_getcaps(kobj_t obj, void *data)
3174{
3175	return (&((struct hdac_chan *)data)->caps);
3176}
3177
3178static kobj_method_t hdac_channel_methods[] = {
3179	KOBJMETHOD(channel_init,		hdac_channel_init),
3180	KOBJMETHOD(channel_setformat,		hdac_channel_setformat),
3181	KOBJMETHOD(channel_setspeed,		hdac_channel_setspeed),
3182	KOBJMETHOD(channel_setblocksize,	hdac_channel_setblocksize),
3183	KOBJMETHOD(channel_setfragments,	hdac_channel_setfragments),
3184	KOBJMETHOD(channel_trigger,		hdac_channel_trigger),
3185	KOBJMETHOD(channel_getptr,		hdac_channel_getptr),
3186	KOBJMETHOD(channel_getcaps,		hdac_channel_getcaps),
3187	{ 0, 0 }
3188};
3189CHANNEL_DECLARE(hdac_channel);
3190
3191static void
3192hdac_jack_poll_callback(void *arg)
3193{
3194	struct hdac_devinfo *devinfo = arg;
3195	struct hdac_softc *sc;
3196
3197	if (devinfo == NULL || devinfo->codec == NULL ||
3198	    devinfo->codec->sc == NULL)
3199		return;
3200	sc = devinfo->codec->sc;
3201	hdac_lock(sc);
3202	if (sc->poll_ival == 0) {
3203		hdac_unlock(sc);
3204		return;
3205	}
3206	hdac_hp_switch_handler(devinfo);
3207	callout_reset(&sc->poll_jack, sc->poll_ival,
3208	    hdac_jack_poll_callback, devinfo);
3209	hdac_unlock(sc);
3210}
3211
3212static int
3213hdac_audio_ctl_ossmixer_init(struct snd_mixer *m)
3214{
3215	struct hdac_devinfo *devinfo = mix_getdevinfo(m);
3216	struct hdac_softc *sc = devinfo->codec->sc;
3217	struct hdac_widget *w, *cw;
3218	struct hdac_audio_ctl *ctl;
3219	uint32_t mask, recmask, id;
3220	int i, j, softpcmvol;
3221	nid_t cad;
3222
3223	hdac_lock(sc);
3224
3225	mask = 0;
3226	recmask = 0;
3227
3228	id = hdac_codec_id(devinfo);
3229	cad = devinfo->codec->cad;
3230	for (i = 0; i < HDAC_HP_SWITCH_LEN; i++) {
3231		if (!(HDA_DEV_MATCH(hdac_hp_switch[i].model,
3232		    sc->pci_subvendor) && hdac_hp_switch[i].id == id))
3233			continue;
3234		w = hdac_widget_get(devinfo, hdac_hp_switch[i].hpnid);
3235		if (w == NULL || w->enable == 0 || w->type !=
3236		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3237			continue;
3238		if (hdac_hp_switch[i].polling != 0)
3239			callout_reset(&sc->poll_jack, 1,
3240			    hdac_jack_poll_callback, devinfo);
3241		else if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
3242			hdac_command(sc,
3243			    HDA_CMD_SET_UNSOLICITED_RESPONSE(cad, w->nid,
3244			    HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE |
3245			    HDAC_UNSOLTAG_EVENT_HP), cad);
3246		else
3247			continue;
3248		hdac_hp_switch_handler(devinfo);
3249		HDA_BOOTVERBOSE(
3250			device_printf(sc->dev,
3251			    "HDA_DEBUG: Enabling headphone/speaker "
3252			    "audio routing switching:\n");
3253			device_printf(sc->dev,
3254			    "HDA_DEBUG: \tindex=%d nid=%d "
3255			    "pci_subvendor=0x%08x "
3256			    "codec=0x%08x [%s]\n",
3257			    i, w->nid, sc->pci_subvendor, id,
3258			    (hdac_hp_switch[i].polling != 0) ? "POLL" :
3259			    "UNSOL");
3260		);
3261		break;
3262	}
3263	for (i = 0; i < HDAC_EAPD_SWITCH_LEN; i++) {
3264		if (!(HDA_DEV_MATCH(hdac_eapd_switch[i].model,
3265		    sc->pci_subvendor) &&
3266		    hdac_eapd_switch[i].id == id))
3267			continue;
3268		w = hdac_widget_get(devinfo, hdac_eapd_switch[i].eapdnid);
3269		if (w == NULL || w->enable == 0)
3270			break;
3271		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3272		    w->param.eapdbtl == HDAC_INVALID)
3273			break;
3274		mask |= SOUND_MASK_OGAIN;
3275		break;
3276	}
3277
3278	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3279		w = hdac_widget_get(devinfo, i);
3280		if (w == NULL || w->enable == 0)
3281			continue;
3282		mask |= w->ctlflags;
3283		if (!(w->pflags & HDA_ADC_RECSEL))
3284			continue;
3285		for (j = 0; j < w->nconns; j++) {
3286			cw = hdac_widget_get(devinfo, w->conns[j]);
3287			if (cw == NULL || cw->enable == 0)
3288				continue;
3289			recmask |= cw->ctlflags;
3290		}
3291	}
3292
3293	if (!(mask & SOUND_MASK_PCM)) {
3294		softpcmvol = 1;
3295		mask |= SOUND_MASK_PCM;
3296	} else
3297		softpcmvol = (devinfo->function.audio.quirks &
3298		    HDA_QUIRK_SOFTPCMVOL) ? 1 : 0;
3299
3300	i = 0;
3301	ctl = NULL;
3302	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
3303		if (ctl->widget == NULL || ctl->enable == 0)
3304			continue;
3305		if (!(ctl->ossmask & SOUND_MASK_PCM))
3306			continue;
3307		if (ctl->step > 0)
3308			break;
3309	}
3310
3311	if (softpcmvol == 1 || ctl == NULL) {
3312		pcm_setflags(sc->dev, pcm_getflags(sc->dev) | SD_F_SOFTPCMVOL);
3313		HDA_BOOTVERBOSE(
3314			device_printf(sc->dev,
3315			    "HDA_DEBUG: %s Soft PCM volume\n",
3316			    (softpcmvol == 1) ?
3317			    "Forcing" : "Enabling");
3318		);
3319		i = 0;
3320		/*
3321		 * XXX Temporary quirk for STAC9220, until the parser
3322		 *     become smarter.
3323		 */
3324		if (id == HDA_CODEC_STAC9220) {
3325			mask |= SOUND_MASK_VOLUME;
3326			while ((ctl = hdac_audio_ctl_each(devinfo, &i)) !=
3327			    NULL) {
3328				if (ctl->widget == NULL || ctl->enable == 0)
3329					continue;
3330				if (ctl->widget->nid == 11 && ctl->index == 0) {
3331					ctl->ossmask = SOUND_MASK_VOLUME;
3332					ctl->ossval = 100 | (100 << 8);
3333				} else
3334					ctl->ossmask &= ~SOUND_MASK_VOLUME;
3335			}
3336		} else if (id == HDA_CODEC_STAC9221) {
3337			mask |= SOUND_MASK_VOLUME;
3338			while ((ctl = hdac_audio_ctl_each(devinfo, &i)) !=
3339			    NULL) {
3340				if (ctl->widget == NULL)
3341					continue;
3342				if (ctl->widget->type ==
3343				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT &&
3344				    ctl->index == 0 && (ctl->widget->nid == 2 ||
3345				    ctl->widget->enable != 0)) {
3346					ctl->enable = 1;
3347					ctl->ossmask = SOUND_MASK_VOLUME;
3348					ctl->ossval = 100 | (100 << 8);
3349				} else if (ctl->enable == 0)
3350					continue;
3351				else
3352					ctl->ossmask &= ~SOUND_MASK_VOLUME;
3353			}
3354		} else {
3355			mix_setparentchild(m, SOUND_MIXER_VOLUME,
3356			    SOUND_MASK_PCM);
3357			if (!(mask & SOUND_MASK_VOLUME))
3358				mix_setrealdev(m, SOUND_MIXER_VOLUME,
3359				    SOUND_MIXER_NONE);
3360			while ((ctl = hdac_audio_ctl_each(devinfo, &i)) !=
3361			    NULL) {
3362				if (ctl->widget == NULL || ctl->enable == 0)
3363					continue;
3364				if (!HDA_FLAG_MATCH(ctl->ossmask,
3365				    SOUND_MASK_VOLUME | SOUND_MASK_PCM))
3366					continue;
3367				if (!(ctl->mute == 1 && ctl->step == 0))
3368					ctl->enable = 0;
3369			}
3370		}
3371	}
3372
3373	recmask &= ~(SOUND_MASK_PCM | SOUND_MASK_RECLEV | SOUND_MASK_SPEAKER |
3374	    SOUND_MASK_BASS | SOUND_MASK_TREBLE | SOUND_MASK_IGAIN |
3375	    SOUND_MASK_OGAIN);
3376	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
3377	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
3378
3379	mix_setrecdevs(m, recmask);
3380	mix_setdevs(m, mask);
3381
3382	hdac_unlock(sc);
3383
3384	return (0);
3385}
3386
3387static int
3388hdac_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
3389					unsigned left, unsigned right)
3390{
3391	struct hdac_devinfo *devinfo = mix_getdevinfo(m);
3392	struct hdac_softc *sc = devinfo->codec->sc;
3393	struct hdac_widget *w;
3394	struct hdac_audio_ctl *ctl;
3395	uint32_t id, mute;
3396	int lvol, rvol, mlvol, mrvol;
3397	int i = 0;
3398
3399	hdac_lock(sc);
3400	if (dev == SOUND_MIXER_OGAIN) {
3401		uint32_t orig;
3402		/*if (left != right || !(left == 0 || left == 1)) {
3403			hdac_unlock(sc);
3404			return (-1);
3405		}*/
3406		id = hdac_codec_id(devinfo);
3407		for (i = 0; i < HDAC_EAPD_SWITCH_LEN; i++) {
3408			if (HDA_DEV_MATCH(hdac_eapd_switch[i].model,
3409			    sc->pci_subvendor) &&
3410			    hdac_eapd_switch[i].id == id)
3411				break;
3412		}
3413		if (i >= HDAC_EAPD_SWITCH_LEN) {
3414			hdac_unlock(sc);
3415			return (-1);
3416		}
3417		w = hdac_widget_get(devinfo, hdac_eapd_switch[i].eapdnid);
3418		if (w == NULL ||
3419		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3420		    w->param.eapdbtl == HDAC_INVALID) {
3421			hdac_unlock(sc);
3422			return (-1);
3423		}
3424		orig = w->param.eapdbtl;
3425		if (left == 0)
3426			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
3427		else
3428			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
3429		if (orig != w->param.eapdbtl) {
3430			uint32_t val;
3431
3432			if (hdac_eapd_switch[i].hp_switch != 0)
3433				hdac_hp_switch_handler(devinfo);
3434			val = w->param.eapdbtl;
3435			if (devinfo->function.audio.quirks & HDA_QUIRK_EAPDINV)
3436				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
3437			hdac_command(sc,
3438			    HDA_CMD_SET_EAPD_BTL_ENABLE(devinfo->codec->cad,
3439			    w->nid, val), devinfo->codec->cad);
3440		}
3441		hdac_unlock(sc);
3442		return (left | (left << 8));
3443	}
3444	if (dev == SOUND_MIXER_VOLUME)
3445		devinfo->function.audio.mvol = left | (right << 8);
3446
3447	mlvol = devinfo->function.audio.mvol & 0x7f;
3448	mrvol = (devinfo->function.audio.mvol >> 8) & 0x7f;
3449	lvol = 0;
3450	rvol = 0;
3451
3452	i = 0;
3453	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
3454		if (ctl->widget == NULL || ctl->enable == 0 ||
3455		    !(ctl->ossmask & (1 << dev)))
3456			continue;
3457		switch (dev) {
3458		case SOUND_MIXER_VOLUME:
3459			lvol = ((ctl->ossval & 0x7f) * left) / 100;
3460			lvol = (lvol * ctl->step) / 100;
3461			rvol = (((ctl->ossval >> 8) & 0x7f) * right) / 100;
3462			rvol = (rvol * ctl->step) / 100;
3463			break;
3464		default:
3465			if (ctl->ossmask & SOUND_MASK_VOLUME) {
3466				lvol = (left * mlvol) / 100;
3467				lvol = (lvol * ctl->step) / 100;
3468				rvol = (right * mrvol) / 100;
3469				rvol = (rvol * ctl->step) / 100;
3470			} else {
3471				lvol = (left * ctl->step) / 100;
3472				rvol = (right * ctl->step) / 100;
3473			}
3474			ctl->ossval = left | (right << 8);
3475			break;
3476		}
3477		mute = 0;
3478		if (ctl->step < 1) {
3479			mute |= (left == 0) ? HDA_AMP_MUTE_LEFT :
3480			    (ctl->muted & HDA_AMP_MUTE_LEFT);
3481			mute |= (right == 0) ? HDA_AMP_MUTE_RIGHT :
3482			    (ctl->muted & HDA_AMP_MUTE_RIGHT);
3483		} else {
3484			mute |= (lvol == 0) ? HDA_AMP_MUTE_LEFT :
3485			    (ctl->muted & HDA_AMP_MUTE_LEFT);
3486			mute |= (rvol == 0) ? HDA_AMP_MUTE_RIGHT :
3487			    (ctl->muted & HDA_AMP_MUTE_RIGHT);
3488		}
3489		hdac_audio_ctl_amp_set(ctl, mute, lvol, rvol);
3490	}
3491	hdac_unlock(sc);
3492
3493	return (left | (right << 8));
3494}
3495
3496static int
3497hdac_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
3498{
3499	struct hdac_devinfo *devinfo = mix_getdevinfo(m);
3500	struct hdac_widget *w, *cw;
3501	struct hdac_softc *sc = devinfo->codec->sc;
3502	uint32_t ret = src, target;
3503	int i, j;
3504
3505	target = 0;
3506	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
3507		if (src & (1 << i)) {
3508			target = 1 << i;
3509			break;
3510		}
3511	}
3512
3513	hdac_lock(sc);
3514
3515	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3516		w = hdac_widget_get(devinfo, i);
3517		if (w == NULL || w->enable == 0)
3518			continue;
3519		if (!(w->pflags & HDA_ADC_RECSEL))
3520			continue;
3521		for (j = 0; j < w->nconns; j++) {
3522			cw = hdac_widget_get(devinfo, w->conns[j]);
3523			if (cw == NULL || cw->enable == 0)
3524				continue;
3525			if ((target == SOUND_MASK_VOLUME &&
3526			    cw->type !=
3527			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3528			    (target != SOUND_MASK_VOLUME &&
3529			    cw->type ==
3530			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER))
3531				continue;
3532			if (cw->ctlflags & target) {
3533				if (!(w->pflags & HDA_ADC_LOCKED))
3534					hdac_widget_connection_select(w, j);
3535				ret = target;
3536				j += w->nconns;
3537			}
3538		}
3539	}
3540
3541	hdac_unlock(sc);
3542
3543	return (ret);
3544}
3545
3546static kobj_method_t hdac_audio_ctl_ossmixer_methods[] = {
3547	KOBJMETHOD(mixer_init,		hdac_audio_ctl_ossmixer_init),
3548	KOBJMETHOD(mixer_set,		hdac_audio_ctl_ossmixer_set),
3549	KOBJMETHOD(mixer_setrecsrc,	hdac_audio_ctl_ossmixer_setrecsrc),
3550	{ 0, 0 }
3551};
3552MIXER_DECLARE(hdac_audio_ctl_ossmixer);
3553
3554/****************************************************************************
3555 * int hdac_attach(device_t)
3556 *
3557 * Attach the device into the kernel. Interrupts usually won't be enabled
3558 * when this function is called. Setup everything that doesn't require
3559 * interrupts and defer probing of codecs until interrupts are enabled.
3560 ****************************************************************************/
3561static int
3562hdac_attach(device_t dev)
3563{
3564	struct hdac_softc *sc;
3565	int result;
3566	int i;
3567	uint16_t vendor;
3568	uint8_t v;
3569
3570	sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO);
3571	if (sc == NULL) {
3572		device_printf(dev, "cannot allocate softc\n");
3573		return (ENOMEM);
3574	}
3575
3576	sc->lock = snd_mtxcreate(device_get_nameunit(dev), HDAC_MTX_NAME);
3577	sc->dev = dev;
3578	sc->pci_subvendor = (uint32_t)pci_get_subdevice(sc->dev) << 16;
3579	sc->pci_subvendor |= (uint32_t)pci_get_subvendor(sc->dev) & 0x0000ffff;
3580	vendor = pci_get_vendor(dev);
3581
3582	if (sc->pci_subvendor == HP_NX6325_SUBVENDORX) {
3583		/* Screw nx6325 - subdevice/subvendor swapped */
3584		sc->pci_subvendor = HP_NX6325_SUBVENDOR;
3585	}
3586
3587	callout_init(&sc->poll_hda, CALLOUT_MPSAFE);
3588	callout_init(&sc->poll_hdac, CALLOUT_MPSAFE);
3589	callout_init(&sc->poll_jack, CALLOUT_MPSAFE);
3590
3591	sc->poll_ticks = 1;
3592	sc->poll_ival = HDAC_POLL_INTERVAL;
3593	if (resource_int_value(device_get_name(dev),
3594	    device_get_unit(dev), "polling", &i) == 0 && i != 0)
3595		sc->polling = 1;
3596	else
3597		sc->polling = 0;
3598
3599	sc->chan_size = pcm_getbuffersize(dev,
3600	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
3601
3602	if (resource_int_value(device_get_name(dev),
3603	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
3604		i &= HDA_BLK_ALIGN;
3605		if (i < HDA_BLK_MIN)
3606			i = HDA_BLK_MIN;
3607		sc->chan_blkcnt = sc->chan_size / i;
3608		i = 0;
3609		while (sc->chan_blkcnt >> i)
3610			i++;
3611		sc->chan_blkcnt = 1 << (i - 1);
3612		if (sc->chan_blkcnt < HDA_BDL_MIN)
3613			sc->chan_blkcnt = HDA_BDL_MIN;
3614		else if (sc->chan_blkcnt > HDA_BDL_MAX)
3615			sc->chan_blkcnt = HDA_BDL_MAX;
3616	} else
3617		sc->chan_blkcnt = HDA_BDL_DEFAULT;
3618
3619	result = bus_dma_tag_create(NULL,	/* parent */
3620	    HDAC_DMA_ALIGNMENT,			/* alignment */
3621	    0,					/* boundary */
3622	    BUS_SPACE_MAXADDR_32BIT,		/* lowaddr */
3623	    BUS_SPACE_MAXADDR,			/* highaddr */
3624	    NULL,				/* filtfunc */
3625	    NULL,				/* fistfuncarg */
3626	    sc->chan_size, 			/* maxsize */
3627	    1,					/* nsegments */
3628	    sc->chan_size, 			/* maxsegsz */
3629	    0,					/* flags */
3630	    NULL,				/* lockfunc */
3631	    NULL,				/* lockfuncarg */
3632	    &sc->chan_dmat);			/* dmat */
3633	if (result != 0) {
3634		device_printf(dev, "%s: bus_dma_tag_create failed (%x)\n",
3635		     __func__, result);
3636		snd_mtxfree(sc->lock);
3637		free(sc, M_DEVBUF);
3638		return (ENXIO);
3639	}
3640
3641
3642	sc->hdabus = NULL;
3643	for (i = 0; i < HDAC_CODEC_MAX; i++)
3644		sc->codecs[i] = NULL;
3645
3646	pci_enable_busmaster(dev);
3647
3648	if (vendor == INTEL_VENDORID) {
3649		/* TCSEL -> TC0 */
3650		v = pci_read_config(dev, 0x44, 1);
3651		pci_write_config(dev, 0x44, v & 0xf8, 1);
3652		HDA_BOOTVERBOSE(
3653			device_printf(dev, "TCSEL: 0x%02d -> 0x%02d\n", v,
3654			    pci_read_config(dev, 0x44, 1));
3655		);
3656	}
3657
3658#if defined(__i386__) || defined(__amd64__)
3659	sc->nocache = 1;
3660
3661	if (resource_int_value(device_get_name(dev),
3662	    device_get_unit(dev), "snoop", &i) == 0 && i != 0) {
3663#else
3664	sc->nocache = 0;
3665#endif
3666		/*
3667		 * Try to enable PCIe snoop to avoid messing around with
3668		 * uncacheable DMA attribute. Since PCIe snoop register
3669		 * config is pretty much vendor specific, there are no
3670		 * general solutions on how to enable it, forcing us (even
3671		 * Microsoft) to enable uncacheable or write combined DMA
3672		 * by default.
3673		 *
3674		 * http://msdn2.microsoft.com/en-us/library/ms790324.aspx
3675		 */
3676		for (i = 0; i < HDAC_PCIESNOOP_LEN; i++) {
3677			if (hdac_pcie_snoop[i].vendor != vendor)
3678				continue;
3679			sc->nocache = 0;
3680			if (hdac_pcie_snoop[i].reg == 0x00)
3681				break;
3682			v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
3683			if ((v & hdac_pcie_snoop[i].enable) ==
3684			    hdac_pcie_snoop[i].enable)
3685				break;
3686			v &= hdac_pcie_snoop[i].mask;
3687			v |= hdac_pcie_snoop[i].enable;
3688			pci_write_config(dev, hdac_pcie_snoop[i].reg, v, 1);
3689			v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
3690			if ((v & hdac_pcie_snoop[i].enable) !=
3691			    hdac_pcie_snoop[i].enable) {
3692				HDA_BOOTVERBOSE(
3693					device_printf(dev,
3694					    "WARNING: Failed to enable PCIe "
3695					    "snoop!\n");
3696				);
3697#if defined(__i386__) || defined(__amd64__)
3698				sc->nocache = 1;
3699#endif
3700			}
3701			break;
3702		}
3703#if defined(__i386__) || defined(__amd64__)
3704	}
3705#endif
3706
3707	HDA_BOOTVERBOSE(
3708		device_printf(dev, "DMA Coherency: %s / vendor=0x%04x\n",
3709		    (sc->nocache == 0) ? "PCIe snoop" : "Uncacheable", vendor);
3710	);
3711
3712	/* Allocate resources */
3713	result = hdac_mem_alloc(sc);
3714	if (result != 0)
3715		goto hdac_attach_fail;
3716	result = hdac_irq_alloc(sc);
3717	if (result != 0)
3718		goto hdac_attach_fail;
3719
3720	/* Get Capabilities */
3721	result = hdac_get_capabilities(sc);
3722	if (result != 0)
3723		goto hdac_attach_fail;
3724
3725	/* Allocate CORB and RIRB dma memory */
3726	result = hdac_dma_alloc(sc, &sc->corb_dma,
3727	    sc->corb_size * sizeof(uint32_t));
3728	if (result != 0)
3729		goto hdac_attach_fail;
3730	result = hdac_dma_alloc(sc, &sc->rirb_dma,
3731	    sc->rirb_size * sizeof(struct hdac_rirb));
3732	if (result != 0)
3733		goto hdac_attach_fail;
3734
3735	/* Quiesce everything */
3736	hdac_reset(sc);
3737
3738	/* Initialize the CORB and RIRB */
3739	hdac_corb_init(sc);
3740	hdac_rirb_init(sc);
3741
3742	/* Defer remaining of initialization until interrupts are enabled */
3743	sc->intrhook.ich_func = hdac_attach2;
3744	sc->intrhook.ich_arg = (void *)sc;
3745	if (cold == 0 || config_intrhook_establish(&sc->intrhook) != 0) {
3746		sc->intrhook.ich_func = NULL;
3747		hdac_attach2((void *)sc);
3748	}
3749
3750	return (0);
3751
3752hdac_attach_fail:
3753	hdac_irq_free(sc);
3754	hdac_dma_free(sc, &sc->rirb_dma);
3755	hdac_dma_free(sc, &sc->corb_dma);
3756	hdac_mem_free(sc);
3757	snd_mtxfree(sc->lock);
3758	free(sc, M_DEVBUF);
3759
3760	return (ENXIO);
3761}
3762
3763static void
3764hdac_audio_parse(struct hdac_devinfo *devinfo)
3765{
3766	struct hdac_softc *sc = devinfo->codec->sc;
3767	struct hdac_widget *w;
3768	uint32_t res;
3769	int i;
3770	nid_t cad, nid;
3771
3772	cad = devinfo->codec->cad;
3773	nid = devinfo->nid;
3774
3775	hdac_command(sc,
3776	    HDA_CMD_SET_POWER_STATE(cad, nid, HDA_CMD_POWER_STATE_D0), cad);
3777
3778	DELAY(100);
3779
3780	res = hdac_command(sc,
3781	    HDA_CMD_GET_PARAMETER(cad , nid, HDA_PARAM_SUB_NODE_COUNT), cad);
3782
3783	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
3784	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
3785	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
3786
3787	res = hdac_command(sc,
3788	    HDA_CMD_GET_PARAMETER(cad , nid, HDA_PARAM_GPIO_COUNT), cad);
3789	devinfo->function.audio.gpio = res;
3790
3791	HDA_BOOTVERBOSE(
3792		device_printf(sc->dev, "       Vendor: 0x%08x\n",
3793		    devinfo->vendor_id);
3794		device_printf(sc->dev, "       Device: 0x%08x\n",
3795		    devinfo->device_id);
3796		device_printf(sc->dev, "     Revision: 0x%08x\n",
3797		    devinfo->revision_id);
3798		device_printf(sc->dev, "     Stepping: 0x%08x\n",
3799		    devinfo->stepping_id);
3800		device_printf(sc->dev, "PCI Subvendor: 0x%08x\n",
3801		    sc->pci_subvendor);
3802		device_printf(sc->dev, "        Nodes: start=%d "
3803		    "endnode=%d total=%d\n",
3804		    devinfo->startnode, devinfo->endnode, devinfo->nodecnt);
3805		device_printf(sc->dev, "    CORB size: %d\n", sc->corb_size);
3806		device_printf(sc->dev, "    RIRB size: %d\n", sc->rirb_size);
3807		device_printf(sc->dev, "      Streams: ISS=%d OSS=%d BSS=%d\n",
3808		    sc->num_iss, sc->num_oss, sc->num_bss);
3809		device_printf(sc->dev, "         GPIO: 0x%08x\n",
3810		    devinfo->function.audio.gpio);
3811		device_printf(sc->dev, "               NumGPIO=%d NumGPO=%d "
3812		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
3813		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->function.audio.gpio),
3814		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->function.audio.gpio),
3815		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->function.audio.gpio),
3816		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->function.audio.gpio),
3817		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->function.audio.gpio));
3818	);
3819
3820	res = hdac_command(sc,
3821	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_SUPP_STREAM_FORMATS),
3822	    cad);
3823	devinfo->function.audio.supp_stream_formats = res;
3824
3825	res = hdac_command(sc,
3826	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE),
3827	    cad);
3828	devinfo->function.audio.supp_pcm_size_rate = res;
3829
3830	res = hdac_command(sc,
3831	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_OUTPUT_AMP_CAP),
3832	    cad);
3833	devinfo->function.audio.outamp_cap = res;
3834
3835	res = hdac_command(sc,
3836	    HDA_CMD_GET_PARAMETER(cad, nid, HDA_PARAM_INPUT_AMP_CAP),
3837	    cad);
3838	devinfo->function.audio.inamp_cap = res;
3839
3840	if (devinfo->nodecnt > 0)
3841		devinfo->widget = (struct hdac_widget *)malloc(
3842		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAC,
3843		    M_NOWAIT | M_ZERO);
3844	else
3845		devinfo->widget = NULL;
3846
3847	if (devinfo->widget == NULL) {
3848		device_printf(sc->dev, "unable to allocate widgets!\n");
3849		devinfo->endnode = devinfo->startnode;
3850		devinfo->nodecnt = 0;
3851		return;
3852	}
3853
3854	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3855		w = hdac_widget_get(devinfo, i);
3856		if (w == NULL)
3857			device_printf(sc->dev, "Ghost widget! nid=%d!\n", i);
3858		else {
3859			w->devinfo = devinfo;
3860			w->nid = i;
3861			w->enable = 1;
3862			w->selconn = -1;
3863			w->pflags = 0;
3864			w->ctlflags = 0;
3865			w->param.eapdbtl = HDAC_INVALID;
3866			hdac_widget_parse(w);
3867		}
3868	}
3869}
3870
3871static void
3872hdac_audio_ctl_parse(struct hdac_devinfo *devinfo)
3873{
3874	struct hdac_softc *sc = devinfo->codec->sc;
3875	struct hdac_audio_ctl *ctls;
3876	struct hdac_widget *w, *cw;
3877	int i, j, cnt, max, ocap, icap;
3878	int mute, offset, step, size;
3879
3880	/* XXX This is redundant */
3881	max = 0;
3882	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3883		w = hdac_widget_get(devinfo, i);
3884		if (w == NULL || w->enable == 0)
3885			continue;
3886		if (w->param.outamp_cap != 0)
3887			max++;
3888		if (w->param.inamp_cap != 0) {
3889			switch (w->type) {
3890			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3891			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3892				for (j = 0; j < w->nconns; j++) {
3893					cw = hdac_widget_get(devinfo,
3894					    w->conns[j]);
3895					if (cw == NULL || cw->enable == 0)
3896						continue;
3897					max++;
3898				}
3899				break;
3900			default:
3901				max++;
3902				break;
3903			}
3904		}
3905	}
3906
3907	devinfo->function.audio.ctlcnt = max;
3908
3909	if (max < 1)
3910		return;
3911
3912	ctls = (struct hdac_audio_ctl *)malloc(
3913	    sizeof(*ctls) * max, M_HDAC, M_ZERO | M_NOWAIT);
3914
3915	if (ctls == NULL) {
3916		/* Blekh! */
3917		device_printf(sc->dev, "unable to allocate ctls!\n");
3918		devinfo->function.audio.ctlcnt = 0;
3919		return;
3920	}
3921
3922	cnt = 0;
3923	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
3924		if (cnt >= max) {
3925			device_printf(sc->dev, "%s: Ctl overflow!\n",
3926			    __func__);
3927			break;
3928		}
3929		w = hdac_widget_get(devinfo, i);
3930		if (w == NULL || w->enable == 0)
3931			continue;
3932		ocap = w->param.outamp_cap;
3933		icap = w->param.inamp_cap;
3934		if (ocap != 0) {
3935			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
3936			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
3937			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
3938			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
3939			/*if (offset > step) {
3940				HDA_BOOTVERBOSE(
3941					device_printf(sc->dev,
3942					    "HDA_DEBUG: BUGGY outamp: nid=%d "
3943					    "[offset=%d > step=%d]\n",
3944					    w->nid, offset, step);
3945				);
3946				offset = step;
3947			}*/
3948			ctls[cnt].enable = 1;
3949			ctls[cnt].widget = w;
3950			ctls[cnt].mute = mute;
3951			ctls[cnt].step = step;
3952			ctls[cnt].size = size;
3953			ctls[cnt].offset = offset;
3954			ctls[cnt].left = offset;
3955			ctls[cnt].right = offset;
3956			ctls[cnt++].dir = HDA_CTL_OUT;
3957		}
3958
3959		if (icap != 0) {
3960			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
3961			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
3962			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
3963			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
3964			/*if (offset > step) {
3965				HDA_BOOTVERBOSE(
3966					device_printf(sc->dev,
3967					    "HDA_DEBUG: BUGGY inamp: nid=%d "
3968					    "[offset=%d > step=%d]\n",
3969					    w->nid, offset, step);
3970				);
3971				offset = step;
3972			}*/
3973			switch (w->type) {
3974			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3975			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3976				for (j = 0; j < w->nconns; j++) {
3977					if (cnt >= max) {
3978						device_printf(sc->dev,
3979						    "%s: Ctl overflow!\n",
3980						    __func__);
3981						break;
3982					}
3983					cw = hdac_widget_get(devinfo,
3984					    w->conns[j]);
3985					if (cw == NULL || cw->enable == 0)
3986						continue;
3987					ctls[cnt].enable = 1;
3988					ctls[cnt].widget = w;
3989					ctls[cnt].childwidget = cw;
3990					ctls[cnt].index = j;
3991					ctls[cnt].mute = mute;
3992					ctls[cnt].step = step;
3993					ctls[cnt].size = size;
3994					ctls[cnt].offset = offset;
3995					ctls[cnt].left = offset;
3996					ctls[cnt].right = offset;
3997					ctls[cnt++].dir = HDA_CTL_IN;
3998				}
3999				break;
4000			default:
4001				if (cnt >= max) {
4002					device_printf(sc->dev,
4003					    "%s: Ctl overflow!\n",
4004					    __func__);
4005					break;
4006				}
4007				ctls[cnt].enable = 1;
4008				ctls[cnt].widget = w;
4009				ctls[cnt].mute = mute;
4010				ctls[cnt].step = step;
4011				ctls[cnt].size = size;
4012				ctls[cnt].offset = offset;
4013				ctls[cnt].left = offset;
4014				ctls[cnt].right = offset;
4015				ctls[cnt++].dir = HDA_CTL_IN;
4016				break;
4017			}
4018		}
4019	}
4020
4021	devinfo->function.audio.ctl = ctls;
4022}
4023
4024static const struct {
4025	uint32_t model;
4026	uint32_t id;
4027	uint32_t set, unset;
4028} hdac_quirks[] = {
4029	/*
4030	 * XXX Force stereo quirk. Monoural recording / playback
4031	 *     on few codecs (especially ALC880) seems broken or
4032	 *     perhaps unsupported.
4033	 */
4034	{ HDA_MATCH_ALL, HDA_MATCH_ALL,
4035	    HDA_QUIRK_FORCESTEREO | HDA_QUIRK_IVREF, 0 },
4036	{ ACER_ALL_SUBVENDOR, HDA_MATCH_ALL,
4037	    HDA_QUIRK_GPIO0, 0 },
4038	{ ASUS_M5200_SUBVENDOR, HDA_CODEC_ALC880,
4039	    HDA_QUIRK_GPIO0, 0 },
4040	{ ASUS_A7M_SUBVENDOR, HDA_CODEC_ALC880,
4041	    HDA_QUIRK_GPIO0, 0 },
4042	{ ASUS_A7T_SUBVENDOR, HDA_CODEC_ALC882,
4043	    HDA_QUIRK_GPIO0, 0 },
4044	{ ASUS_W2J_SUBVENDOR, HDA_CODEC_ALC882,
4045	    HDA_QUIRK_GPIO0, 0 },
4046	{ ASUS_U5F_SUBVENDOR, HDA_CODEC_AD1986A,
4047	    HDA_QUIRK_EAPDINV, 0 },
4048	{ ASUS_A8JC_SUBVENDOR, HDA_CODEC_AD1986A,
4049	    HDA_QUIRK_EAPDINV, 0 },
4050	{ ASUS_F3JC_SUBVENDOR, HDA_CODEC_ALC861,
4051	    HDA_QUIRK_OVREF, 0 },
4052	{ ASUS_W6F_SUBVENDOR, HDA_CODEC_ALC861,
4053	    HDA_QUIRK_OVREF, 0 },
4054	{ UNIWILL_9075_SUBVENDOR, HDA_CODEC_ALC861,
4055	    HDA_QUIRK_OVREF, 0 },
4056	/*{ ASUS_M2N_SUBVENDOR, HDA_CODEC_AD1988,
4057	    HDA_QUIRK_IVREF80, HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF100 },*/
4058	{ MEDION_MD95257_SUBVENDOR, HDA_CODEC_ALC880,
4059	    HDA_QUIRK_GPIO1, 0 },
4060	{ LENOVO_3KN100_SUBVENDOR, HDA_CODEC_AD1986A,
4061	    HDA_QUIRK_EAPDINV, 0 },
4062	{ SAMSUNG_Q1_SUBVENDOR, HDA_CODEC_AD1986A,
4063	    HDA_QUIRK_EAPDINV, 0 },
4064	{ APPLE_INTEL_MAC, HDA_CODEC_STAC9221,
4065	    HDA_QUIRK_GPIO0 | HDA_QUIRK_GPIO1, 0 },
4066	{ HDA_MATCH_ALL, HDA_CODEC_AD1988,
4067	    HDA_QUIRK_IVREF80, HDA_QUIRK_IVREF50 | HDA_QUIRK_IVREF100 },
4068	{ HDA_MATCH_ALL, HDA_CODEC_CXVENICE,
4069	    0, HDA_QUIRK_FORCESTEREO },
4070	{ HDA_MATCH_ALL, HDA_CODEC_STACXXXX,
4071	    HDA_QUIRK_SOFTPCMVOL, 0 }
4072};
4073#define HDAC_QUIRKS_LEN (sizeof(hdac_quirks) / sizeof(hdac_quirks[0]))
4074
4075static void
4076hdac_vendor_patch_parse(struct hdac_devinfo *devinfo)
4077{
4078	struct hdac_widget *w;
4079	struct hdac_audio_ctl *ctl;
4080	uint32_t id, subvendor;
4081	int i;
4082
4083	id = hdac_codec_id(devinfo);
4084	subvendor = devinfo->codec->sc->pci_subvendor;
4085
4086	/*
4087	 * Quirks
4088	 */
4089	for (i = 0; i < HDAC_QUIRKS_LEN; i++) {
4090		if (!(HDA_DEV_MATCH(hdac_quirks[i].model, subvendor) &&
4091		    HDA_DEV_MATCH(hdac_quirks[i].id, id)))
4092			continue;
4093		if (hdac_quirks[i].set != 0)
4094			devinfo->function.audio.quirks |=
4095			    hdac_quirks[i].set;
4096		if (hdac_quirks[i].unset != 0)
4097			devinfo->function.audio.quirks &=
4098			    ~(hdac_quirks[i].unset);
4099	}
4100
4101	switch (id) {
4102	case HDA_CODEC_ALC260:
4103		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4104			w = hdac_widget_get(devinfo, i);
4105			if (w == NULL || w->enable == 0)
4106				continue;
4107			if (w->type !=
4108			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
4109				continue;
4110			if (w->nid != 5)
4111				w->enable = 0;
4112		}
4113		if (subvendor == HP_XW4300_SUBVENDOR) {
4114			ctl = hdac_audio_ctl_amp_get(devinfo, 16, 0, 1);
4115			if (ctl != NULL && ctl->widget != NULL) {
4116				ctl->ossmask = SOUND_MASK_SPEAKER;
4117				ctl->widget->ctlflags |= SOUND_MASK_SPEAKER;
4118			}
4119			ctl = hdac_audio_ctl_amp_get(devinfo, 17, 0, 1);
4120			if (ctl != NULL && ctl->widget != NULL) {
4121				ctl->ossmask = SOUND_MASK_SPEAKER;
4122				ctl->widget->ctlflags |= SOUND_MASK_SPEAKER;
4123			}
4124		} else if (subvendor == HP_3010_SUBVENDOR) {
4125			ctl = hdac_audio_ctl_amp_get(devinfo, 17, 0, 1);
4126			if (ctl != NULL && ctl->widget != NULL) {
4127				ctl->ossmask = SOUND_MASK_SPEAKER;
4128				ctl->widget->ctlflags |= SOUND_MASK_SPEAKER;
4129			}
4130			ctl = hdac_audio_ctl_amp_get(devinfo, 21, 0, 1);
4131			if (ctl != NULL && ctl->widget != NULL) {
4132				ctl->ossmask = SOUND_MASK_SPEAKER;
4133				ctl->widget->ctlflags |= SOUND_MASK_SPEAKER;
4134			}
4135		}
4136		break;
4137	case HDA_CODEC_ALC861:
4138		ctl = hdac_audio_ctl_amp_get(devinfo, 21, 2, 1);
4139		if (ctl != NULL)
4140			ctl->muted = HDA_AMP_MUTE_ALL;
4141		break;
4142	case HDA_CODEC_ALC880:
4143		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4144			w = hdac_widget_get(devinfo, i);
4145			if (w == NULL || w->enable == 0)
4146				continue;
4147			if (w->type ==
4148			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT &&
4149			    w->nid != 9 && w->nid != 29) {
4150					w->enable = 0;
4151			} else if (w->type !=
4152			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET &&
4153			    w->nid == 29) {
4154				w->type =
4155				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET;
4156				w->param.widget_cap &=
4157				    ~HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_MASK;
4158				w->param.widget_cap |=
4159				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET <<
4160				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_SHIFT;
4161				strlcpy(w->name, "beep widget", sizeof(w->name));
4162			}
4163		}
4164		break;
4165	case HDA_CODEC_ALC883:
4166		/*
4167		 * nid: 24/25 = External (jack) or Internal (fixed) Mic.
4168		 *              Clear vref cap for jack connectivity.
4169		 */
4170		w = hdac_widget_get(devinfo, 24);
4171		if (w != NULL && w->enable != 0 && w->type ==
4172		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4173		    (w->wclass.pin.config &
4174		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
4175		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4176			w->wclass.pin.cap &= ~(
4177			    HDA_PARAM_PIN_CAP_VREF_CTRL_100_MASK |
4178			    HDA_PARAM_PIN_CAP_VREF_CTRL_80_MASK |
4179			    HDA_PARAM_PIN_CAP_VREF_CTRL_50_MASK);
4180		w = hdac_widget_get(devinfo, 25);
4181		if (w != NULL && w->enable != 0 && w->type ==
4182		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4183		    (w->wclass.pin.config &
4184		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
4185		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4186			w->wclass.pin.cap &= ~(
4187			    HDA_PARAM_PIN_CAP_VREF_CTRL_100_MASK |
4188			    HDA_PARAM_PIN_CAP_VREF_CTRL_80_MASK |
4189			    HDA_PARAM_PIN_CAP_VREF_CTRL_50_MASK);
4190		/*
4191		 * nid: 26 = Line-in, leave it alone.
4192		 */
4193		break;
4194	case HDA_CODEC_AD1981HD:
4195		w = hdac_widget_get(devinfo, 11);
4196		if (w != NULL && w->enable != 0 && w->nconns > 3)
4197			w->selconn = 3;
4198		if (subvendor == IBM_M52_SUBVENDOR) {
4199			ctl = hdac_audio_ctl_amp_get(devinfo, 7, 0, 1);
4200			if (ctl != NULL)
4201				ctl->ossmask = SOUND_MASK_SPEAKER;
4202		}
4203		break;
4204	case HDA_CODEC_AD1986A:
4205		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4206			w = hdac_widget_get(devinfo, i);
4207			if (w == NULL || w->enable == 0)
4208				continue;
4209			if (w->type !=
4210			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
4211				continue;
4212			if (w->nid != 3)
4213				w->enable = 0;
4214		}
4215		if (subvendor == ASUS_M2NPVMX_SUBVENDOR) {
4216			/* nid 28 is mic, nid 29 is line-in */
4217			w = hdac_widget_get(devinfo, 15);
4218			if (w != NULL)
4219				w->selconn = 2;
4220			w = hdac_widget_get(devinfo, 16);
4221			if (w != NULL)
4222				w->selconn = 1;
4223		}
4224		break;
4225	case HDA_CODEC_AD1988:
4226		/*w = hdac_widget_get(devinfo, 12);
4227		if (w != NULL) {
4228			w->selconn = 1;
4229			w->pflags |= HDA_ADC_LOCKED;
4230		}
4231		w = hdac_widget_get(devinfo, 13);
4232		if (w != NULL) {
4233			w->selconn = 4;
4234			w->pflags |= HDA_ADC_LOCKED;
4235		}
4236		w = hdac_widget_get(devinfo, 14);
4237		if (w != NULL) {
4238			w->selconn = 2;
4239			w->pflags |= HDA_ADC_LOCKED;
4240		}*/
4241		ctl = hdac_audio_ctl_amp_get(devinfo, 57, 0, 1);
4242		if (ctl != NULL) {
4243			ctl->ossmask = SOUND_MASK_IGAIN;
4244			ctl->widget->ctlflags |= SOUND_MASK_IGAIN;
4245		}
4246		ctl = hdac_audio_ctl_amp_get(devinfo, 58, 0, 1);
4247		if (ctl != NULL) {
4248			ctl->ossmask = SOUND_MASK_IGAIN;
4249			ctl->widget->ctlflags |= SOUND_MASK_IGAIN;
4250		}
4251		ctl = hdac_audio_ctl_amp_get(devinfo, 60, 0, 1);
4252		if (ctl != NULL) {
4253			ctl->ossmask = SOUND_MASK_IGAIN;
4254			ctl->widget->ctlflags |= SOUND_MASK_IGAIN;
4255		}
4256		ctl = hdac_audio_ctl_amp_get(devinfo, 32, 0, 1);
4257		if (ctl != NULL) {
4258			ctl->ossmask = SOUND_MASK_MIC | SOUND_MASK_VOLUME;
4259			ctl->widget->ctlflags |= SOUND_MASK_MIC;
4260		}
4261		ctl = hdac_audio_ctl_amp_get(devinfo, 32, 4, 1);
4262		if (ctl != NULL) {
4263			ctl->ossmask = SOUND_MASK_MIC | SOUND_MASK_VOLUME;
4264			ctl->widget->ctlflags |= SOUND_MASK_MIC;
4265		}
4266		ctl = hdac_audio_ctl_amp_get(devinfo, 32, 1, 1);
4267		if (ctl != NULL) {
4268			ctl->ossmask = SOUND_MASK_LINE | SOUND_MASK_VOLUME;
4269			ctl->widget->ctlflags |= SOUND_MASK_LINE;
4270		}
4271		ctl = hdac_audio_ctl_amp_get(devinfo, 32, 7, 1);
4272		if (ctl != NULL) {
4273			ctl->ossmask = SOUND_MASK_SPEAKER | SOUND_MASK_VOLUME;
4274			ctl->widget->ctlflags |= SOUND_MASK_SPEAKER;
4275		}
4276		break;
4277	case HDA_CODEC_STAC9221:
4278		/*
4279		 * Dell XPS M1210 need all DACs for each output jacks
4280		 */
4281		if (subvendor == DELL_XPSM1210_SUBVENDOR)
4282			break;
4283		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4284			w = hdac_widget_get(devinfo, i);
4285			if (w == NULL || w->enable == 0)
4286				continue;
4287			if (w->type !=
4288			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
4289				continue;
4290			if (w->nid != 2)
4291				w->enable = 0;
4292		}
4293		break;
4294	case HDA_CODEC_STAC9221D:
4295		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4296			w = hdac_widget_get(devinfo, i);
4297			if (w == NULL || w->enable == 0)
4298				continue;
4299			if (w->type ==
4300			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT &&
4301			    w->nid != 6)
4302				w->enable = 0;
4303
4304		}
4305		break;
4306	case HDA_CODEC_STAC9227:
4307		w = hdac_widget_get(devinfo, 8);
4308		if (w != NULL)
4309			w->enable = 0;
4310		w = hdac_widget_get(devinfo, 9);
4311		if (w != NULL)
4312			w->enable = 0;
4313		break;
4314	case HDA_CODEC_CXWAIKIKI:
4315		if (subvendor == HP_DV5000_SUBVENDOR) {
4316			w = hdac_widget_get(devinfo, 27);
4317			if (w != NULL)
4318				w->enable = 0;
4319		}
4320		ctl = hdac_audio_ctl_amp_get(devinfo, 16, 0, 1);
4321		if (ctl != NULL)
4322			ctl->ossmask = SOUND_MASK_SKIP;
4323		ctl = hdac_audio_ctl_amp_get(devinfo, 25, 0, 1);
4324		if (ctl != NULL && ctl->childwidget != NULL &&
4325		    ctl->childwidget->enable != 0) {
4326			ctl->ossmask = SOUND_MASK_PCM | SOUND_MASK_VOLUME;
4327			ctl->childwidget->ctlflags |= SOUND_MASK_PCM;
4328		}
4329		ctl = hdac_audio_ctl_amp_get(devinfo, 25, 1, 1);
4330		if (ctl != NULL && ctl->childwidget != NULL &&
4331		    ctl->childwidget->enable != 0) {
4332			ctl->ossmask = SOUND_MASK_LINE | SOUND_MASK_VOLUME;
4333			ctl->childwidget->ctlflags |= SOUND_MASK_LINE;
4334		}
4335		ctl = hdac_audio_ctl_amp_get(devinfo, 25, 2, 1);
4336		if (ctl != NULL && ctl->childwidget != NULL &&
4337		    ctl->childwidget->enable != 0) {
4338			ctl->ossmask = SOUND_MASK_MIC | SOUND_MASK_VOLUME;
4339			ctl->childwidget->ctlflags |= SOUND_MASK_MIC;
4340		}
4341		ctl = hdac_audio_ctl_amp_get(devinfo, 26, 0, 1);
4342		if (ctl != NULL) {
4343			ctl->ossmask = SOUND_MASK_SKIP;
4344			/* XXX mixer \=rec mic broken.. why?!? */
4345			/* ctl->widget->ctlflags |= SOUND_MASK_MIC; */
4346		}
4347		break;
4348	default:
4349		break;
4350	}
4351}
4352
4353static int
4354hdac_audio_ctl_ossmixer_getnextdev(struct hdac_devinfo *devinfo)
4355{
4356	int *dev = &devinfo->function.audio.ossidx;
4357
4358	while (*dev < SOUND_MIXER_NRDEVICES) {
4359		switch (*dev) {
4360		case SOUND_MIXER_VOLUME:
4361		case SOUND_MIXER_BASS:
4362		case SOUND_MIXER_TREBLE:
4363		case SOUND_MIXER_PCM:
4364		case SOUND_MIXER_SPEAKER:
4365		case SOUND_MIXER_LINE:
4366		case SOUND_MIXER_MIC:
4367		case SOUND_MIXER_CD:
4368		case SOUND_MIXER_RECLEV:
4369		case SOUND_MIXER_IGAIN:
4370		case SOUND_MIXER_OGAIN:	/* reserved for EAPD switch */
4371			(*dev)++;
4372			break;
4373		default:
4374			return (*dev)++;
4375			break;
4376		}
4377	}
4378
4379	return (-1);
4380}
4381
4382static int
4383hdac_widget_find_dac_path(struct hdac_devinfo *devinfo, nid_t nid, int depth)
4384{
4385	struct hdac_widget *w;
4386	int i, ret = 0;
4387
4388	if (depth > HDA_PARSE_MAXDEPTH)
4389		return (0);
4390	w = hdac_widget_get(devinfo, nid);
4391	if (w == NULL || w->enable == 0)
4392		return (0);
4393	switch (w->type) {
4394	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4395		w->pflags |= HDA_DAC_PATH;
4396		ret = 1;
4397		break;
4398	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
4399	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
4400		for (i = 0; i < w->nconns; i++) {
4401			if (hdac_widget_find_dac_path(devinfo,
4402			    w->conns[i], depth + 1) != 0) {
4403				if (w->selconn == -1)
4404					w->selconn = i;
4405				ret = 1;
4406				w->pflags |= HDA_DAC_PATH;
4407			}
4408		}
4409		break;
4410	default:
4411		break;
4412	}
4413	return (ret);
4414}
4415
4416static int
4417hdac_widget_find_adc_path(struct hdac_devinfo *devinfo, nid_t nid, int depth)
4418{
4419	struct hdac_widget *w;
4420	int i, conndev, ret = 0;
4421
4422	if (depth > HDA_PARSE_MAXDEPTH)
4423		return (0);
4424	w = hdac_widget_get(devinfo, nid);
4425	if (w == NULL || w->enable == 0)
4426		return (0);
4427	switch (w->type) {
4428	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
4429	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
4430	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
4431		for (i = 0; i < w->nconns; i++) {
4432			if (hdac_widget_find_adc_path(devinfo, w->conns[i],
4433			    depth + 1) != 0) {
4434				if (w->selconn == -1)
4435					w->selconn = i;
4436				w->pflags |= HDA_ADC_PATH;
4437				ret = 1;
4438			}
4439		}
4440		break;
4441	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4442		conndev = w->wclass.pin.config &
4443		    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
4444		if (HDA_PARAM_PIN_CAP_INPUT_CAP(w->wclass.pin.cap) &&
4445		    (conndev == HDA_CONFIG_DEFAULTCONF_DEVICE_CD ||
4446		    conndev == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN ||
4447		    conndev == HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN)) {
4448			w->pflags |= HDA_ADC_PATH;
4449			ret = 1;
4450		}
4451		break;
4452	/*case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
4453		if (w->pflags & HDA_DAC_PATH) {
4454			w->pflags |= HDA_ADC_PATH;
4455			ret = 1;
4456		}
4457		break;*/
4458	default:
4459		break;
4460	}
4461	return (ret);
4462}
4463
4464static uint32_t
4465hdac_audio_ctl_outamp_build(struct hdac_devinfo *devinfo,
4466				nid_t nid, nid_t pnid, int index, int depth)
4467{
4468	struct hdac_widget *w, *pw;
4469	struct hdac_audio_ctl *ctl;
4470	uint32_t fl = 0;
4471	int i, ossdev, conndev, strategy;
4472
4473	if (depth > HDA_PARSE_MAXDEPTH)
4474		return (0);
4475
4476	w = hdac_widget_get(devinfo, nid);
4477	if (w == NULL || w->enable == 0)
4478		return (0);
4479
4480	pw = hdac_widget_get(devinfo, pnid);
4481	strategy = devinfo->function.audio.parsing_strategy;
4482
4483	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER
4484	    || w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR) {
4485		for (i = 0; i < w->nconns; i++) {
4486			fl |= hdac_audio_ctl_outamp_build(devinfo, w->conns[i],
4487			    w->nid, i, depth + 1);
4488		}
4489		w->ctlflags |= fl;
4490		return (fl);
4491	} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT &&
4492	    (w->pflags & HDA_DAC_PATH)) {
4493		i = 0;
4494		while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
4495			if (ctl->enable == 0 || ctl->widget == NULL)
4496				continue;
4497			/* XXX This should be compressed! */
4498			if (((ctl->widget->nid == w->nid) ||
4499			    (ctl->widget->nid == pnid && ctl->index == index &&
4500			    (ctl->dir & HDA_CTL_IN)) ||
4501			    (ctl->widget->nid == pnid && pw != NULL &&
4502			    pw->type ==
4503			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4504			    (pw->nconns < 2 || pw->selconn == index ||
4505			    pw->selconn == -1) &&
4506			    (ctl->dir & HDA_CTL_OUT)) ||
4507			    (strategy == HDA_PARSE_DIRECT &&
4508			    ctl->widget->nid == w->nid)) &&
4509			    !(ctl->ossmask & ~SOUND_MASK_VOLUME)) {
4510				/*if (pw != NULL && pw->selconn == -1)
4511					pw->selconn = index;
4512				fl |= SOUND_MASK_VOLUME;
4513				fl |= SOUND_MASK_PCM;
4514				ctl->ossmask |= SOUND_MASK_VOLUME;
4515				ctl->ossmask |= SOUND_MASK_PCM;
4516				ctl->ossdev = SOUND_MIXER_PCM;*/
4517				if (!(w->ctlflags & SOUND_MASK_PCM) ||
4518				    (pw != NULL &&
4519				    !(pw->ctlflags & SOUND_MASK_PCM))) {
4520					fl |= SOUND_MASK_VOLUME;
4521					fl |= SOUND_MASK_PCM;
4522					ctl->ossmask |= SOUND_MASK_VOLUME;
4523					ctl->ossmask |= SOUND_MASK_PCM;
4524					ctl->ossdev = SOUND_MIXER_PCM;
4525					w->ctlflags |= SOUND_MASK_VOLUME;
4526					w->ctlflags |= SOUND_MASK_PCM;
4527					if (pw != NULL) {
4528						if (pw->selconn == -1)
4529							pw->selconn = index;
4530						pw->ctlflags |=
4531						    SOUND_MASK_VOLUME;
4532						pw->ctlflags |=
4533						    SOUND_MASK_PCM;
4534					}
4535				}
4536			}
4537		}
4538		w->ctlflags |= fl;
4539		return (fl);
4540	} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4541	    HDA_PARAM_PIN_CAP_INPUT_CAP(w->wclass.pin.cap) &&
4542	    (w->pflags & HDA_ADC_PATH)) {
4543		conndev = w->wclass.pin.config &
4544		    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
4545		i = 0;
4546		while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
4547			if (ctl->enable == 0 || ctl->widget == NULL)
4548				continue;
4549			/* XXX This should be compressed! */
4550			if (((ctl->widget->nid == pnid && ctl->index == index &&
4551			    (ctl->dir & HDA_CTL_IN)) ||
4552			    (ctl->widget->nid == pnid && pw != NULL &&
4553			    pw->type ==
4554			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4555			    (pw->nconns < 2 || pw->selconn == index ||
4556			    pw->selconn == -1) &&
4557			    (ctl->dir & HDA_CTL_OUT)) ||
4558			    (strategy == HDA_PARSE_DIRECT &&
4559			    ctl->widget->nid == w->nid)) &&
4560			    !(ctl->ossmask & ~SOUND_MASK_VOLUME)) {
4561				if (pw != NULL && pw->selconn == -1)
4562					pw->selconn = index;
4563				ossdev = 0;
4564				switch (conndev) {
4565				case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4566					ossdev = SOUND_MIXER_MIC;
4567					break;
4568				case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4569					ossdev = SOUND_MIXER_LINE;
4570					break;
4571				case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4572					ossdev = SOUND_MIXER_CD;
4573					break;
4574				default:
4575					ossdev =
4576					    hdac_audio_ctl_ossmixer_getnextdev(
4577					    devinfo);
4578					if (ossdev < 0)
4579						ossdev = 0;
4580					break;
4581				}
4582				if (strategy == HDA_PARSE_MIXER) {
4583					fl |= SOUND_MASK_VOLUME;
4584					ctl->ossmask |= SOUND_MASK_VOLUME;
4585				}
4586				fl |= 1 << ossdev;
4587				ctl->ossmask |= 1 << ossdev;
4588				ctl->ossdev = ossdev;
4589			}
4590		}
4591		w->ctlflags |= fl;
4592		return (fl);
4593	} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET) {
4594		i = 0;
4595		while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
4596			if (ctl->enable == 0 || ctl->widget == NULL)
4597				continue;
4598			/* XXX This should be compressed! */
4599			if (((ctl->widget->nid == pnid && ctl->index == index &&
4600			    (ctl->dir & HDA_CTL_IN)) ||
4601			    (ctl->widget->nid == pnid && pw != NULL &&
4602			    pw->type ==
4603			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4604			    (pw->nconns < 2 || pw->selconn == index ||
4605			    pw->selconn == -1) &&
4606			    (ctl->dir & HDA_CTL_OUT)) ||
4607			    (strategy == HDA_PARSE_DIRECT &&
4608			    ctl->widget->nid == w->nid)) &&
4609			    !(ctl->ossmask & ~SOUND_MASK_VOLUME)) {
4610				if (pw != NULL && pw->selconn == -1)
4611					pw->selconn = index;
4612				fl |= SOUND_MASK_VOLUME;
4613				fl |= SOUND_MASK_SPEAKER;
4614				ctl->ossmask |= SOUND_MASK_VOLUME;
4615				ctl->ossmask |= SOUND_MASK_SPEAKER;
4616				ctl->ossdev = SOUND_MIXER_SPEAKER;
4617			}
4618		}
4619		w->ctlflags |= fl;
4620		return (fl);
4621	}
4622	return (0);
4623}
4624
4625static uint32_t
4626hdac_audio_ctl_inamp_build(struct hdac_devinfo *devinfo, nid_t nid, int depth)
4627{
4628	struct hdac_widget *w, *cw;
4629	struct hdac_audio_ctl *ctl;
4630	uint32_t fl;
4631	int i;
4632
4633	if (depth > HDA_PARSE_MAXDEPTH)
4634		return (0);
4635
4636	w = hdac_widget_get(devinfo, nid);
4637	if (w == NULL || w->enable == 0)
4638		return (0);
4639	/*if (!(w->pflags & HDA_ADC_PATH))
4640		return (0);
4641	if (!(w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4642	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
4643		return (0);*/
4644	i = 0;
4645	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
4646		if (ctl->enable == 0 || ctl->widget == NULL)
4647			continue;
4648		if (ctl->widget->nid == nid) {
4649			ctl->ossmask |= SOUND_MASK_RECLEV;
4650			w->ctlflags |= SOUND_MASK_RECLEV;
4651			return (SOUND_MASK_RECLEV);
4652		}
4653	}
4654	for (i = 0; i < w->nconns; i++) {
4655		cw = hdac_widget_get(devinfo, w->conns[i]);
4656		if (cw == NULL || cw->enable == 0)
4657			continue;
4658		if (cw->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR)
4659			continue;
4660		fl = hdac_audio_ctl_inamp_build(devinfo, cw->nid, depth + 1);
4661		if (fl != 0) {
4662			cw->ctlflags |= fl;
4663			w->ctlflags |= fl;
4664			return (fl);
4665		}
4666	}
4667	return (0);
4668}
4669
4670static int
4671hdac_audio_ctl_recsel_build(struct hdac_devinfo *devinfo, nid_t nid, int depth)
4672{
4673	struct hdac_widget *w, *cw;
4674	int i, child = 0;
4675
4676	if (depth > HDA_PARSE_MAXDEPTH)
4677		return (0);
4678
4679	w = hdac_widget_get(devinfo, nid);
4680	if (w == NULL || w->enable == 0)
4681		return (0);
4682	/*if (!(w->pflags & HDA_ADC_PATH))
4683		return (0);
4684	if (!(w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4685	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
4686		return (0);*/
4687	/* XXX weak! */
4688	for (i = 0; i < w->nconns; i++) {
4689		cw = hdac_widget_get(devinfo, w->conns[i]);
4690		if (cw == NULL)
4691			continue;
4692		if (++child > 1) {
4693			w->pflags |= HDA_ADC_RECSEL;
4694			return (1);
4695		}
4696	}
4697	for (i = 0; i < w->nconns; i++) {
4698		if (hdac_audio_ctl_recsel_build(devinfo,
4699		    w->conns[i], depth + 1) != 0)
4700			return (1);
4701	}
4702	return (0);
4703}
4704
4705static int
4706hdac_audio_build_tree_strategy(struct hdac_devinfo *devinfo)
4707{
4708	struct hdac_widget *w, *cw;
4709	int i, j, conndev, found_dac = 0;
4710	int strategy;
4711
4712	strategy = devinfo->function.audio.parsing_strategy;
4713
4714	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4715		w = hdac_widget_get(devinfo, i);
4716		if (w == NULL || w->enable == 0)
4717			continue;
4718		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4719			continue;
4720		if (!HDA_PARAM_PIN_CAP_OUTPUT_CAP(w->wclass.pin.cap))
4721			continue;
4722		conndev = w->wclass.pin.config &
4723		    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
4724		if (!(conndev == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
4725		    conndev == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
4726		    conndev == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT))
4727			continue;
4728		for (j = 0; j < w->nconns; j++) {
4729			cw = hdac_widget_get(devinfo, w->conns[j]);
4730			if (cw == NULL || cw->enable == 0)
4731				continue;
4732			if (strategy == HDA_PARSE_MIXER && !(cw->type ==
4733			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
4734			    cw->type ==
4735			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
4736				continue;
4737			if (hdac_widget_find_dac_path(devinfo, cw->nid, 0)
4738			    != 0) {
4739				if (w->selconn == -1)
4740					w->selconn = j;
4741				w->pflags |= HDA_DAC_PATH;
4742				found_dac++;
4743			}
4744		}
4745	}
4746
4747	return (found_dac);
4748}
4749
4750static void
4751hdac_audio_build_tree(struct hdac_devinfo *devinfo)
4752{
4753	struct hdac_widget *w;
4754	struct hdac_audio_ctl *ctl;
4755	int i, j, dacs, strategy;
4756
4757	/* Construct DAC path */
4758	strategy = HDA_PARSE_MIXER;
4759	devinfo->function.audio.parsing_strategy = strategy;
4760	HDA_BOOTVERBOSE(
4761		device_printf(devinfo->codec->sc->dev,
4762		    "HDA_DEBUG: HWiP: HDA Widget Parser - Revision %d\n",
4763		    HDA_WIDGET_PARSER_REV);
4764	);
4765	dacs = hdac_audio_build_tree_strategy(devinfo);
4766	if (dacs == 0) {
4767		HDA_BOOTVERBOSE(
4768			device_printf(devinfo->codec->sc->dev,
4769			    "HDA_DEBUG: HWiP: 0 DAC path found! "
4770			    "Retrying parser "
4771			    "using HDA_PARSE_DIRECT strategy.\n");
4772		);
4773		strategy = HDA_PARSE_DIRECT;
4774		devinfo->function.audio.parsing_strategy = strategy;
4775		dacs = hdac_audio_build_tree_strategy(devinfo);
4776	}
4777
4778	HDA_BOOTVERBOSE(
4779		device_printf(devinfo->codec->sc->dev,
4780		    "HDA_DEBUG: HWiP: Found %d DAC path using HDA_PARSE_%s "
4781		    "strategy.\n",
4782		    dacs, (strategy == HDA_PARSE_MIXER) ? "MIXER" : "DIRECT");
4783	);
4784
4785	/* Construct ADC path */
4786	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4787		w = hdac_widget_get(devinfo, i);
4788		if (w == NULL || w->enable == 0)
4789			continue;
4790		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
4791			continue;
4792		(void)hdac_widget_find_adc_path(devinfo, w->nid, 0);
4793	}
4794
4795	/* Output mixers */
4796	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4797		w = hdac_widget_get(devinfo, i);
4798		if (w == NULL || w->enable == 0)
4799			continue;
4800		if ((strategy == HDA_PARSE_MIXER &&
4801		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
4802		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR)
4803		    && (w->pflags & HDA_DAC_PATH)) ||
4804		    (strategy == HDA_PARSE_DIRECT && (w->pflags &
4805		    (HDA_DAC_PATH | HDA_ADC_PATH)))) {
4806			w->ctlflags |= hdac_audio_ctl_outamp_build(devinfo,
4807			    w->nid, devinfo->startnode - 1, 0, 0);
4808		} else if (w->type ==
4809		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET) {
4810			j = 0;
4811			while ((ctl = hdac_audio_ctl_each(devinfo, &j)) !=
4812			    NULL) {
4813				if (ctl->enable == 0 || ctl->widget == NULL)
4814					continue;
4815				if (ctl->widget->nid != w->nid)
4816					continue;
4817				ctl->ossmask |= SOUND_MASK_VOLUME;
4818				ctl->ossmask |= SOUND_MASK_SPEAKER;
4819				ctl->ossdev = SOUND_MIXER_SPEAKER;
4820				w->ctlflags |= SOUND_MASK_VOLUME;
4821				w->ctlflags |= SOUND_MASK_SPEAKER;
4822			}
4823		}
4824	}
4825
4826	/* Input mixers (rec) */
4827	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4828		w = hdac_widget_get(devinfo, i);
4829		if (w == NULL || w->enable == 0)
4830			continue;
4831		if (!(w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT &&
4832		    w->pflags & HDA_ADC_PATH))
4833			continue;
4834		hdac_audio_ctl_inamp_build(devinfo, w->nid, 0);
4835		hdac_audio_ctl_recsel_build(devinfo, w->nid, 0);
4836	}
4837}
4838
4839#define HDA_COMMIT_CONN	(1 << 0)
4840#define HDA_COMMIT_CTRL	(1 << 1)
4841#define HDA_COMMIT_EAPD	(1 << 2)
4842#define HDA_COMMIT_GPIO	(1 << 3)
4843#define HDA_COMMIT_MISC	(1 << 4)
4844#define HDA_COMMIT_ALL	(HDA_COMMIT_CONN | HDA_COMMIT_CTRL | \
4845			HDA_COMMIT_EAPD | HDA_COMMIT_GPIO | HDA_COMMIT_MISC)
4846
4847static void
4848hdac_audio_commit(struct hdac_devinfo *devinfo, uint32_t cfl)
4849{
4850	struct hdac_softc *sc = devinfo->codec->sc;
4851	struct hdac_widget *w;
4852	nid_t cad;
4853	int i;
4854
4855	if (!(cfl & HDA_COMMIT_ALL))
4856		return;
4857
4858	cad = devinfo->codec->cad;
4859
4860	if ((cfl & HDA_COMMIT_MISC)) {
4861		if (sc->pci_subvendor == APPLE_INTEL_MAC)
4862			hdac_command(sc, HDA_CMD_12BIT(cad, devinfo->nid,
4863			    0x7e7, 0), cad);
4864	}
4865
4866	if (cfl & HDA_COMMIT_GPIO) {
4867		uint32_t gdata, gmask, gdir;
4868		int commitgpio, numgpio;
4869
4870		gdata = 0;
4871		gmask = 0;
4872		gdir = 0;
4873		commitgpio = 0;
4874
4875		numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(
4876		    devinfo->function.audio.gpio);
4877
4878		if (devinfo->function.audio.quirks & HDA_QUIRK_GPIOFLUSH)
4879			commitgpio = (numgpio > 0) ? 1 : 0;
4880		else {
4881			for (i = 0; i < numgpio && i < HDA_GPIO_MAX; i++) {
4882				if (!(devinfo->function.audio.quirks &
4883				    (1 << i)))
4884					continue;
4885				if (commitgpio == 0) {
4886					commitgpio = 1;
4887					HDA_BOOTVERBOSE(
4888						gdata = hdac_command(sc,
4889						    HDA_CMD_GET_GPIO_DATA(cad,
4890						    devinfo->nid), cad);
4891						gmask = hdac_command(sc,
4892						    HDA_CMD_GET_GPIO_ENABLE_MASK(cad,
4893						    devinfo->nid), cad);
4894						gdir = hdac_command(sc,
4895						    HDA_CMD_GET_GPIO_DIRECTION(cad,
4896						    devinfo->nid), cad);
4897						device_printf(sc->dev,
4898						    "GPIO init: data=0x%08x "
4899						    "mask=0x%08x dir=0x%08x\n",
4900						    gdata, gmask, gdir);
4901						gdata = 0;
4902						gmask = 0;
4903						gdir = 0;
4904					);
4905				}
4906				gdata |= 1 << i;
4907				gmask |= 1 << i;
4908				gdir |= 1 << i;
4909			}
4910		}
4911
4912		if (commitgpio != 0) {
4913			HDA_BOOTVERBOSE(
4914				device_printf(sc->dev,
4915				    "GPIO commit: data=0x%08x mask=0x%08x "
4916				    "dir=0x%08x\n",
4917				    gdata, gmask, gdir);
4918			);
4919			hdac_command(sc,
4920			    HDA_CMD_SET_GPIO_ENABLE_MASK(cad, devinfo->nid,
4921			    gmask), cad);
4922			hdac_command(sc,
4923			    HDA_CMD_SET_GPIO_DIRECTION(cad, devinfo->nid,
4924			    gdir), cad);
4925			hdac_command(sc,
4926			    HDA_CMD_SET_GPIO_DATA(cad, devinfo->nid,
4927			    gdata), cad);
4928		}
4929	}
4930
4931	for (i = 0; i < devinfo->nodecnt; i++) {
4932		w = &devinfo->widget[i];
4933		if (w == NULL || w->enable == 0)
4934			continue;
4935		if (cfl & HDA_COMMIT_CONN) {
4936			if (w->selconn == -1)
4937				w->selconn = 0;
4938			if (w->nconns > 0)
4939				hdac_widget_connection_select(w, w->selconn);
4940		}
4941		if ((cfl & HDA_COMMIT_CTRL) &&
4942		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
4943		    	uint32_t pincap;
4944
4945			pincap = w->wclass.pin.cap;
4946
4947			if ((w->pflags & (HDA_DAC_PATH | HDA_ADC_PATH)) ==
4948			    (HDA_DAC_PATH | HDA_ADC_PATH))
4949				device_printf(sc->dev, "WARNING: node %d "
4950				    "participate both for DAC/ADC!\n", w->nid);
4951			if (w->pflags & HDA_DAC_PATH) {
4952				w->wclass.pin.ctrl &=
4953				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
4954				if ((w->wclass.pin.config &
4955				    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) !=
4956				    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
4957					w->wclass.pin.ctrl &=
4958					    ~HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
4959				if ((devinfo->function.audio.quirks & HDA_QUIRK_OVREF100) &&
4960				    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4961					w->wclass.pin.ctrl |=
4962					    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4963					    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4964				else if ((devinfo->function.audio.quirks & HDA_QUIRK_OVREF80) &&
4965				    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4966					w->wclass.pin.ctrl |=
4967					    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4968					    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4969				else if ((devinfo->function.audio.quirks & HDA_QUIRK_OVREF50) &&
4970				    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4971					w->wclass.pin.ctrl |=
4972					    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4973					    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4974			} else if (w->pflags & HDA_ADC_PATH) {
4975				w->wclass.pin.ctrl &=
4976				    ~(HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
4977				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE);
4978				if ((devinfo->function.audio.quirks & HDA_QUIRK_IVREF100) &&
4979				    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4980					w->wclass.pin.ctrl |=
4981					    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4982					    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4983				else if ((devinfo->function.audio.quirks & HDA_QUIRK_IVREF80) &&
4984				    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4985					w->wclass.pin.ctrl |=
4986					    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4987					    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4988				else if ((devinfo->function.audio.quirks & HDA_QUIRK_IVREF50) &&
4989				    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4990					w->wclass.pin.ctrl |=
4991					    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4992					    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4993			} else
4994				w->wclass.pin.ctrl &= ~(
4995				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
4996				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
4997				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
4998				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
4999			hdac_command(sc,
5000			    HDA_CMD_SET_PIN_WIDGET_CTRL(cad, w->nid,
5001			    w->wclass.pin.ctrl), cad);
5002		}
5003		if ((cfl & HDA_COMMIT_EAPD) &&
5004		    w->param.eapdbtl != HDAC_INVALID) {
5005		    	uint32_t val;
5006
5007			val = w->param.eapdbtl;
5008			if (devinfo->function.audio.quirks &
5009			    HDA_QUIRK_EAPDINV)
5010				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
5011			hdac_command(sc,
5012			    HDA_CMD_SET_EAPD_BTL_ENABLE(cad, w->nid,
5013			    val), cad);
5014
5015		}
5016		DELAY(1000);
5017	}
5018}
5019
5020static void
5021hdac_audio_ctl_commit(struct hdac_devinfo *devinfo)
5022{
5023	struct hdac_softc *sc = devinfo->codec->sc;
5024	struct hdac_audio_ctl *ctl;
5025	int i;
5026
5027	devinfo->function.audio.mvol = 100 | (100 << 8);
5028	i = 0;
5029	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
5030		if (ctl->enable == 0 || ctl->widget == NULL) {
5031			HDA_BOOTVERBOSE(
5032				device_printf(sc->dev, "[%2d] Ctl nid=%d",
5033				    i, (ctl->widget != NULL) ?
5034				    ctl->widget->nid : -1);
5035				if (ctl->childwidget != NULL)
5036					printf(" childnid=%d",
5037					    ctl->childwidget->nid);
5038				if (ctl->widget == NULL)
5039					printf(" NULL WIDGET!");
5040				printf(" DISABLED\n");
5041			);
5042			continue;
5043		}
5044		HDA_BOOTVERBOSE(
5045			if (ctl->ossmask == 0) {
5046				device_printf(sc->dev, "[%2d] Ctl nid=%d",
5047				    i, ctl->widget->nid);
5048				if (ctl->childwidget != NULL)
5049					printf(" childnid=%d",
5050					ctl->childwidget->nid);
5051				printf(" Bind to NONE\n");
5052			}
5053		);
5054		if (ctl->step > 0) {
5055			ctl->ossval = (ctl->left * 100) / ctl->step;
5056			ctl->ossval |= ((ctl->right * 100) / ctl->step) << 8;
5057		} else
5058			ctl->ossval = 0;
5059		hdac_audio_ctl_amp_set(ctl, HDA_AMP_MUTE_DEFAULT,
5060		    ctl->left, ctl->right);
5061	}
5062}
5063
5064static int
5065hdac_pcmchannel_setup(struct hdac_devinfo *devinfo, int dir)
5066{
5067	struct hdac_chan *ch;
5068	struct hdac_widget *w;
5069	uint32_t cap, fmtcap, pcmcap, path;
5070	int i, type, ret, max;
5071
5072	if (dir == PCMDIR_PLAY) {
5073		type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT;
5074		ch = &devinfo->codec->sc->play;
5075		path = HDA_DAC_PATH;
5076	} else {
5077		type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT;
5078		ch = &devinfo->codec->sc->rec;
5079		path = HDA_ADC_PATH;
5080	}
5081
5082	ch->caps = hdac_caps;
5083	ch->caps.fmtlist = ch->fmtlist;
5084	ch->bit16 = 1;
5085	ch->bit32 = 0;
5086	ch->pcmrates[0] = 48000;
5087	ch->pcmrates[1] = 0;
5088
5089	ret = 0;
5090	fmtcap = devinfo->function.audio.supp_stream_formats;
5091	pcmcap = devinfo->function.audio.supp_pcm_size_rate;
5092	max = (sizeof(ch->io) / sizeof(ch->io[0])) - 1;
5093
5094	for (i = devinfo->startnode; i < devinfo->endnode && ret < max; i++) {
5095		w = hdac_widget_get(devinfo, i);
5096		if (w == NULL || w->enable == 0 || w->type != type ||
5097		    !(w->pflags & path))
5098			continue;
5099		cap = w->param.widget_cap;
5100		/*if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(cap))
5101			continue;*/
5102		if (!HDA_PARAM_AUDIO_WIDGET_CAP_STEREO(cap))
5103			continue;
5104		cap = w->param.supp_stream_formats;
5105		/*if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap)) {
5106		}
5107		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap)) {
5108		}*/
5109		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5110			continue;
5111		if (ret == 0) {
5112			fmtcap = w->param.supp_stream_formats;
5113			pcmcap = w->param.supp_pcm_size_rate;
5114		} else {
5115			fmtcap &= w->param.supp_stream_formats;
5116			pcmcap &= w->param.supp_pcm_size_rate;
5117		}
5118		ch->io[ret++] = i;
5119	}
5120	ch->io[ret] = -1;
5121
5122	ch->supp_stream_formats = fmtcap;
5123	ch->supp_pcm_size_rate = pcmcap;
5124
5125	/*
5126	 *  8bit = 0
5127	 * 16bit = 1
5128	 * 20bit = 2
5129	 * 24bit = 3
5130	 * 32bit = 4
5131	 */
5132	if (ret > 0) {
5133		cap = pcmcap;
5134		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5135			ch->bit16 = 1;
5136		else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5137			ch->bit16 = 0;
5138		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5139			ch->bit32 = 4;
5140		else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5141			ch->bit32 = 3;
5142		else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5143			ch->bit32 = 2;
5144		i = 0;
5145		if (!(devinfo->function.audio.quirks & HDA_QUIRK_FORCESTEREO))
5146			ch->fmtlist[i++] = AFMT_S16_LE;
5147		ch->fmtlist[i++] = AFMT_S16_LE | AFMT_STEREO;
5148		if (ch->bit32 > 0) {
5149			if (!(devinfo->function.audio.quirks &
5150			    HDA_QUIRK_FORCESTEREO))
5151				ch->fmtlist[i++] = AFMT_S32_LE;
5152			ch->fmtlist[i++] = AFMT_S32_LE | AFMT_STEREO;
5153		}
5154		ch->fmtlist[i] = 0;
5155		i = 0;
5156		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5157			ch->pcmrates[i++] = 8000;
5158		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5159			ch->pcmrates[i++] = 11025;
5160		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5161			ch->pcmrates[i++] = 16000;
5162		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5163			ch->pcmrates[i++] = 22050;
5164		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5165			ch->pcmrates[i++] = 32000;
5166		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5167			ch->pcmrates[i++] = 44100;
5168		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(cap)) */
5169		ch->pcmrates[i++] = 48000;
5170		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5171			ch->pcmrates[i++] = 88200;
5172		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5173			ch->pcmrates[i++] = 96000;
5174		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5175			ch->pcmrates[i++] = 176400;
5176		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5177			ch->pcmrates[i++] = 192000;
5178		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(cap)) */
5179		ch->pcmrates[i] = 0;
5180		if (i > 0) {
5181			ch->caps.minspeed = ch->pcmrates[0];
5182			ch->caps.maxspeed = ch->pcmrates[i - 1];
5183		}
5184	}
5185
5186	return (ret);
5187}
5188
5189static void
5190hdac_dump_ctls(struct hdac_devinfo *devinfo, const char *banner, uint32_t flag)
5191{
5192	struct hdac_audio_ctl *ctl;
5193	struct hdac_softc *sc = devinfo->codec->sc;
5194	int i;
5195	uint32_t fl = 0;
5196
5197
5198	if (flag == 0) {
5199		fl = SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5200		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5201		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_OGAIN;
5202	}
5203
5204	i = 0;
5205	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
5206		if (ctl->enable == 0 || ctl->widget == NULL ||
5207		    ctl->widget->enable == 0 || (ctl->ossmask &
5208		    (SOUND_MASK_SKIP | SOUND_MASK_DISABLE)))
5209			continue;
5210		if ((flag == 0 && (ctl->ossmask & ~fl)) ||
5211		    (flag != 0 && (ctl->ossmask & flag))) {
5212			if (banner != NULL) {
5213				device_printf(sc->dev, "\n");
5214				device_printf(sc->dev, "%s\n", banner);
5215			}
5216			goto hdac_ctl_dump_it_all;
5217		}
5218	}
5219
5220	return;
5221
5222hdac_ctl_dump_it_all:
5223	i = 0;
5224	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
5225		if (ctl->enable == 0 || ctl->widget == NULL ||
5226		    ctl->widget->enable == 0)
5227			continue;
5228		if (!((flag == 0 && (ctl->ossmask & ~fl)) ||
5229		    (flag != 0 && (ctl->ossmask & flag))))
5230			continue;
5231		if (flag == 0) {
5232			device_printf(sc->dev, "\n");
5233			device_printf(sc->dev, "Unknown Ctl (OSS: %s)\n",
5234			    hdac_audio_ctl_ossmixer_mask2name(ctl->ossmask));
5235		}
5236		device_printf(sc->dev, "   |\n");
5237		device_printf(sc->dev, "   +-  nid: %2d index: %2d ",
5238		    ctl->widget->nid, ctl->index);
5239		if (ctl->childwidget != NULL)
5240			printf("(nid: %2d) ", ctl->childwidget->nid);
5241		else
5242			printf("          ");
5243		printf("mute: %d step: %3d size: %3d off: %3d dir=0x%x ossmask=0x%08x\n",
5244		    ctl->mute, ctl->step, ctl->size, ctl->offset, ctl->dir,
5245		    ctl->ossmask);
5246	}
5247}
5248
5249static void
5250hdac_dump_audio_formats(struct hdac_softc *sc, uint32_t fcap, uint32_t pcmcap)
5251{
5252	uint32_t cap;
5253
5254	cap = fcap;
5255	if (cap != 0) {
5256		device_printf(sc->dev, "     Stream cap: 0x%08x\n", cap);
5257		device_printf(sc->dev, "         Format:");
5258		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5259			printf(" AC3");
5260		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5261			printf(" FLOAT32");
5262		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5263			printf(" PCM");
5264		printf("\n");
5265	}
5266	cap = pcmcap;
5267	if (cap != 0) {
5268		device_printf(sc->dev, "        PCM cap: 0x%08x\n", cap);
5269		device_printf(sc->dev, "       PCM size:");
5270		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5271			printf(" 8");
5272		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5273			printf(" 16");
5274		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5275			printf(" 20");
5276		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5277			printf(" 24");
5278		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5279			printf(" 32");
5280		printf("\n");
5281		device_printf(sc->dev, "       PCM rate:");
5282		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5283			printf(" 8");
5284		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5285			printf(" 11");
5286		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5287			printf(" 16");
5288		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5289			printf(" 22");
5290		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5291			printf(" 32");
5292		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5293			printf(" 44");
5294		printf(" 48");
5295		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5296			printf(" 88");
5297		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5298			printf(" 96");
5299		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5300			printf(" 176");
5301		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5302			printf(" 192");
5303		printf("\n");
5304	}
5305}
5306
5307static void
5308hdac_dump_pin(struct hdac_softc *sc, struct hdac_widget *w)
5309{
5310	uint32_t pincap, wcap;
5311
5312	pincap = w->wclass.pin.cap;
5313	wcap = w->param.widget_cap;
5314
5315	device_printf(sc->dev, "        Pin cap: 0x%08x\n", pincap);
5316	device_printf(sc->dev, "                ");
5317	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5318		printf(" ISC");
5319	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5320		printf(" TRQD");
5321	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5322		printf(" PDC");
5323	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5324		printf(" HP");
5325	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5326		printf(" OUT");
5327	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5328		printf(" IN");
5329	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5330		printf(" BAL");
5331	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5332		printf(" VREF[");
5333		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5334			printf(" 50");
5335		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5336			printf(" 80");
5337		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5338			printf(" 100");
5339		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5340			printf(" GROUND");
5341		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5342			printf(" HIZ");
5343		printf(" ]");
5344	}
5345	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5346		printf(" EAPD");
5347	if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(wcap))
5348		printf(" : UNSOL");
5349	printf("\n");
5350	device_printf(sc->dev, "     Pin config: 0x%08x\n",
5351	    w->wclass.pin.config);
5352	device_printf(sc->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
5353	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5354		printf(" HP");
5355	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5356		printf(" IN");
5357	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5358		printf(" OUT");
5359	printf("\n");
5360}
5361
5362static void
5363hdac_dump_amp(struct hdac_softc *sc, uint32_t cap, char *banner)
5364{
5365	device_printf(sc->dev, "     %s amp: 0x%08x\n", banner, cap);
5366	device_printf(sc->dev, "                 "
5367	    "mute=%d step=%d size=%d offset=%d\n",
5368	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5369	    HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap),
5370	    HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap),
5371	    HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap));
5372}
5373
5374static void
5375hdac_dump_nodes(struct hdac_devinfo *devinfo)
5376{
5377	struct hdac_softc *sc = devinfo->codec->sc;
5378	struct hdac_widget *w, *cw;
5379	int i, j;
5380
5381	device_printf(sc->dev, "\n");
5382	device_printf(sc->dev, "Default Parameter\n");
5383	device_printf(sc->dev, "-----------------\n");
5384	hdac_dump_audio_formats(sc,
5385	    devinfo->function.audio.supp_stream_formats,
5386	    devinfo->function.audio.supp_pcm_size_rate);
5387	device_printf(sc->dev, "         IN amp: 0x%08x\n",
5388	    devinfo->function.audio.inamp_cap);
5389	device_printf(sc->dev, "        OUT amp: 0x%08x\n",
5390	    devinfo->function.audio.outamp_cap);
5391	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5392		w = hdac_widget_get(devinfo, i);
5393		if (w == NULL) {
5394			device_printf(sc->dev, "Ghost widget nid=%d\n", i);
5395			continue;
5396		}
5397		device_printf(sc->dev, "\n");
5398		device_printf(sc->dev, "            nid: %d [%s]%s\n", w->nid,
5399		    HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) ?
5400		    "DIGITAL" : "ANALOG",
5401		    (w->enable == 0) ? " [DISABLED]" : "");
5402		device_printf(sc->dev, "           name: %s\n", w->name);
5403		device_printf(sc->dev, "     widget_cap: 0x%08x\n",
5404		    w->param.widget_cap);
5405		device_printf(sc->dev, "    Parse flags: 0x%08x\n",
5406		    w->pflags);
5407		device_printf(sc->dev, "      Ctl flags: 0x%08x\n",
5408		    w->ctlflags);
5409		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5410		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5411			hdac_dump_audio_formats(sc,
5412			    w->param.supp_stream_formats,
5413			    w->param.supp_pcm_size_rate);
5414		} else if (w->type ==
5415		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5416			hdac_dump_pin(sc, w);
5417		if (w->param.eapdbtl != HDAC_INVALID)
5418			device_printf(sc->dev, "           EAPD: 0x%08x\n",
5419			    w->param.eapdbtl);
5420		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5421		    w->param.outamp_cap != 0)
5422			hdac_dump_amp(sc, w->param.outamp_cap, "Output");
5423		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5424		    w->param.inamp_cap != 0)
5425			hdac_dump_amp(sc, w->param.inamp_cap, " Input");
5426		device_printf(sc->dev, "    connections: %d\n", w->nconns);
5427		for (j = 0; j < w->nconns; j++) {
5428			cw = hdac_widget_get(devinfo, w->conns[j]);
5429			device_printf(sc->dev, "          |\n");
5430			device_printf(sc->dev, "          + <- nid=%d [%s]",
5431			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5432			if (cw == NULL)
5433				printf(" [UNKNOWN]");
5434			else if (cw->enable == 0)
5435				printf(" [DISABLED]");
5436			if (w->nconns > 1 && w->selconn == j && w->type !=
5437			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5438				printf(" (selected)");
5439			printf("\n");
5440		}
5441	}
5442
5443}
5444
5445static int
5446hdac_dump_dac_internal(struct hdac_devinfo *devinfo, nid_t nid, int depth)
5447{
5448	struct hdac_widget *w, *cw;
5449	struct hdac_softc *sc = devinfo->codec->sc;
5450	int i;
5451
5452	if (depth > HDA_PARSE_MAXDEPTH)
5453		return (0);
5454
5455	w = hdac_widget_get(devinfo, nid);
5456	if (w == NULL || w->enable == 0 || !(w->pflags & HDA_DAC_PATH))
5457		return (0);
5458
5459	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
5460		device_printf(sc->dev, "\n");
5461		device_printf(sc->dev, "    nid=%d [%s]\n", w->nid, w->name);
5462		device_printf(sc->dev, "      ^\n");
5463		device_printf(sc->dev, "      |\n");
5464		device_printf(sc->dev, "      +-----<------+\n");
5465	} else {
5466		device_printf(sc->dev, "                   ^\n");
5467		device_printf(sc->dev, "                   |\n");
5468		device_printf(sc->dev, "               ");
5469		printf("  nid=%d [%s]\n", w->nid, w->name);
5470	}
5471
5472	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
5473		return (1);
5474	} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
5475		for (i = 0; i < w->nconns; i++) {
5476			cw = hdac_widget_get(devinfo, w->conns[i]);
5477			if (cw == NULL || cw->enable == 0 || cw->type ==
5478			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5479				continue;
5480			if (hdac_dump_dac_internal(devinfo, cw->nid,
5481			    depth + 1) != 0)
5482				return (1);
5483		}
5484	} else if ((w->type ==
5485	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR ||
5486	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
5487	    w->selconn > -1 && w->selconn < w->nconns) {
5488		if (hdac_dump_dac_internal(devinfo, w->conns[w->selconn],
5489		    depth + 1) != 0)
5490			return (1);
5491	}
5492
5493	return (0);
5494}
5495
5496static void
5497hdac_dump_dac(struct hdac_devinfo *devinfo)
5498{
5499	struct hdac_widget *w;
5500	struct hdac_softc *sc = devinfo->codec->sc;
5501	int i, printed = 0;
5502
5503	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5504		w = hdac_widget_get(devinfo, i);
5505		if (w == NULL || w->enable == 0)
5506			continue;
5507		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
5508		    !(w->pflags & HDA_DAC_PATH))
5509			continue;
5510		if (printed == 0) {
5511			printed = 1;
5512			device_printf(sc->dev, "\n");
5513			device_printf(sc->dev, "Playback path:\n");
5514		}
5515		hdac_dump_dac_internal(devinfo, w->nid, 0);
5516	}
5517}
5518
5519static void
5520hdac_dump_adc(struct hdac_devinfo *devinfo)
5521{
5522	struct hdac_widget *w, *cw;
5523	struct hdac_softc *sc = devinfo->codec->sc;
5524	int i, j;
5525	int printed = 0;
5526	char ossdevs[256];
5527
5528	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5529		w = hdac_widget_get(devinfo, i);
5530		if (w == NULL || w->enable == 0)
5531			continue;
5532		if (!(w->pflags & HDA_ADC_RECSEL))
5533			continue;
5534		if (printed == 0) {
5535			printed = 1;
5536			device_printf(sc->dev, "\n");
5537			device_printf(sc->dev, "Recording sources:\n");
5538		}
5539		device_printf(sc->dev, "\n");
5540		device_printf(sc->dev, "    nid=%d [%s]\n", w->nid, w->name);
5541		for (j = 0; j < w->nconns; j++) {
5542			cw = hdac_widget_get(devinfo, w->conns[j]);
5543			if (cw == NULL || cw->enable == 0)
5544				continue;
5545			hdac_audio_ctl_ossmixer_mask2allname(cw->ctlflags,
5546			    ossdevs, sizeof(ossdevs));
5547			device_printf(sc->dev, "      |\n");
5548			device_printf(sc->dev, "      + <- nid=%d [%s]",
5549			    cw->nid, cw->name);
5550			if (strlen(ossdevs) > 0) {
5551				printf(" [recsrc: %s]", ossdevs);
5552			}
5553			printf("\n");
5554		}
5555	}
5556}
5557
5558static void
5559hdac_dump_pcmchannels(struct hdac_softc *sc, int pcnt, int rcnt)
5560{
5561	nid_t *nids;
5562
5563	if (pcnt > 0) {
5564		device_printf(sc->dev, "\n");
5565		device_printf(sc->dev, "   PCM Playback: %d\n", pcnt);
5566		hdac_dump_audio_formats(sc, sc->play.supp_stream_formats,
5567		    sc->play.supp_pcm_size_rate);
5568		device_printf(sc->dev, "            DAC:");
5569		for (nids = sc->play.io; *nids != -1; nids++)
5570			printf(" %d", *nids);
5571		printf("\n");
5572	}
5573
5574	if (rcnt > 0) {
5575		device_printf(sc->dev, "\n");
5576		device_printf(sc->dev, "     PCM Record: %d\n", rcnt);
5577		hdac_dump_audio_formats(sc, sc->play.supp_stream_formats,
5578		    sc->rec.supp_pcm_size_rate);
5579		device_printf(sc->dev, "            ADC:");
5580		for (nids = sc->rec.io; *nids != -1; nids++)
5581			printf(" %d", *nids);
5582		printf("\n");
5583	}
5584}
5585
5586static void
5587hdac_release_resources(struct hdac_softc *sc)
5588{
5589	struct hdac_devinfo *devinfo = NULL;
5590	device_t *devlist = NULL;
5591	int i, devcount;
5592
5593	if (sc == NULL)
5594		return;
5595
5596	hdac_lock(sc);
5597	sc->polling = 0;
5598	sc->poll_ival = 0;
5599	callout_stop(&sc->poll_hdac);
5600	callout_stop(&sc->poll_jack);
5601	hdac_reset(sc);
5602	hdac_unlock(sc);
5603	callout_drain(&sc->poll_hdac);
5604	callout_drain(&sc->poll_jack);
5605
5606	hdac_irq_free(sc);
5607
5608	device_get_children(sc->dev, &devlist, &devcount);
5609	for (i = 0; devlist != NULL && i < devcount; i++) {
5610		devinfo = (struct hdac_devinfo *)device_get_ivars(devlist[i]);
5611		if (devinfo == NULL)
5612			continue;
5613		if (devinfo->widget != NULL)
5614			free(devinfo->widget, M_HDAC);
5615		if (devinfo->node_type ==
5616		    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO &&
5617		    devinfo->function.audio.ctl != NULL)
5618			free(devinfo->function.audio.ctl, M_HDAC);
5619		free(devinfo, M_HDAC);
5620		device_delete_child(sc->dev, devlist[i]);
5621	}
5622	if (devlist != NULL)
5623		free(devlist, M_TEMP);
5624
5625	for (i = 0; i < HDAC_CODEC_MAX; i++) {
5626		if (sc->codecs[i] != NULL)
5627			free(sc->codecs[i], M_HDAC);
5628		sc->codecs[i] = NULL;
5629	}
5630
5631	hdac_dma_free(sc, &sc->pos_dma);
5632	hdac_dma_free(sc, &sc->rirb_dma);
5633	hdac_dma_free(sc, &sc->corb_dma);
5634	if (sc->play.blkcnt > 0)
5635		hdac_dma_free(sc, &sc->play.bdl_dma);
5636	if (sc->rec.blkcnt > 0)
5637		hdac_dma_free(sc, &sc->rec.bdl_dma);
5638	if (sc->chan_dmat != NULL) {
5639		bus_dma_tag_destroy(sc->chan_dmat);
5640		sc->chan_dmat = NULL;
5641	}
5642	hdac_mem_free(sc);
5643	snd_mtxfree(sc->lock);
5644	free(sc, M_DEVBUF);
5645}
5646
5647/* This function surely going to make its way into upper level someday. */
5648static void
5649hdac_config_fetch(struct hdac_softc *sc, uint32_t *on, uint32_t *off)
5650{
5651	const char *res = NULL;
5652	int i = 0, j, k, len, inv;
5653
5654	if (on != NULL)
5655		*on = 0;
5656	if (off != NULL)
5657		*off = 0;
5658	if (sc == NULL)
5659		return;
5660	if (resource_string_value(device_get_name(sc->dev),
5661	    device_get_unit(sc->dev), "config", &res) != 0)
5662		return;
5663	if (!(res != NULL && strlen(res) > 0))
5664		return;
5665	HDA_BOOTVERBOSE(
5666		device_printf(sc->dev, "HDA_DEBUG: HDA Config:");
5667	);
5668	for (;;) {
5669		while (res[i] != '\0' &&
5670		    (res[i] == ',' || isspace(res[i]) != 0))
5671			i++;
5672		if (res[i] == '\0') {
5673			HDA_BOOTVERBOSE(
5674				printf("\n");
5675			);
5676			return;
5677		}
5678		j = i;
5679		while (res[j] != '\0' &&
5680		    !(res[j] == ',' || isspace(res[j]) != 0))
5681			j++;
5682		len = j - i;
5683		if (len > 2 && strncmp(res + i, "no", 2) == 0)
5684			inv = 2;
5685		else
5686			inv = 0;
5687		for (k = 0; len > inv && k < HDAC_QUIRKS_TAB_LEN; k++) {
5688			if (strncmp(res + i + inv,
5689			    hdac_quirks_tab[k].key, len - inv) != 0)
5690				continue;
5691			if (len - inv != strlen(hdac_quirks_tab[k].key))
5692				break;
5693			HDA_BOOTVERBOSE(
5694				printf(" %s%s", (inv != 0) ? "no" : "",
5695				    hdac_quirks_tab[k].key);
5696			);
5697			if (inv == 0 && on != NULL)
5698				*on |= hdac_quirks_tab[k].value;
5699			else if (inv != 0 && off != NULL)
5700				*off |= hdac_quirks_tab[k].value;
5701			break;
5702		}
5703		i = j;
5704	}
5705}
5706
5707#ifdef SND_DYNSYSCTL
5708static int
5709sysctl_hdac_polling(SYSCTL_HANDLER_ARGS)
5710{
5711	struct hdac_softc *sc;
5712	struct hdac_devinfo *devinfo;
5713	device_t dev;
5714	uint32_t ctl;
5715	int err, val;
5716
5717	dev = oidp->oid_arg1;
5718	devinfo = pcm_getdevinfo(dev);
5719	if (devinfo == NULL || devinfo->codec == NULL ||
5720	    devinfo->codec->sc == NULL)
5721		return (EINVAL);
5722	sc = devinfo->codec->sc;
5723	hdac_lock(sc);
5724	val = sc->polling;
5725	hdac_unlock(sc);
5726	err = sysctl_handle_int(oidp, &val, 0, req);
5727
5728	if (err != 0 || req->newptr == NULL)
5729		return (err);
5730	if (val < 0 || val > 1)
5731		return (EINVAL);
5732
5733	hdac_lock(sc);
5734	if (val != sc->polling) {
5735		if (hda_chan_active(sc) != 0)
5736			err = EBUSY;
5737		else if (val == 0) {
5738			callout_stop(&sc->poll_hdac);
5739			hdac_unlock(sc);
5740			callout_drain(&sc->poll_hdac);
5741			hdac_lock(sc);
5742			HDAC_WRITE_2(&sc->mem, HDAC_RINTCNT,
5743			    sc->rirb_size / 2);
5744			ctl = HDAC_READ_1(&sc->mem, HDAC_RIRBCTL);
5745			ctl |= HDAC_RIRBCTL_RINTCTL;
5746			HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, ctl);
5747			HDAC_WRITE_4(&sc->mem, HDAC_INTCTL,
5748			    HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
5749			sc->polling = 0;
5750			DELAY(1000);
5751		} else {
5752			HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, 0);
5753			HDAC_WRITE_2(&sc->mem, HDAC_RINTCNT, 0);
5754			ctl = HDAC_READ_1(&sc->mem, HDAC_RIRBCTL);
5755			ctl &= ~HDAC_RIRBCTL_RINTCTL;
5756			HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, ctl);
5757			callout_reset(&sc->poll_hdac, 1, hdac_poll_callback,
5758			    sc);
5759			sc->polling = 1;
5760			DELAY(1000);
5761		}
5762	}
5763	hdac_unlock(sc);
5764
5765	return (err);
5766}
5767
5768static int
5769sysctl_hdac_polling_interval(SYSCTL_HANDLER_ARGS)
5770{
5771	struct hdac_softc *sc;
5772	struct hdac_devinfo *devinfo;
5773	device_t dev;
5774	int err, val;
5775
5776	dev = oidp->oid_arg1;
5777	devinfo = pcm_getdevinfo(dev);
5778	if (devinfo == NULL || devinfo->codec == NULL ||
5779	    devinfo->codec->sc == NULL)
5780		return (EINVAL);
5781	sc = devinfo->codec->sc;
5782	hdac_lock(sc);
5783	val = ((uint64_t)sc->poll_ival * 1000) / hz;
5784	hdac_unlock(sc);
5785	err = sysctl_handle_int(oidp, &val, 0, req);
5786
5787	if (err != 0 || req->newptr == NULL)
5788		return (err);
5789
5790	if (val < 1)
5791		val = 1;
5792	if (val > 5000)
5793		val = 5000;
5794	val = ((uint64_t)val * hz) / 1000;
5795	if (val < 1)
5796		val = 1;
5797	if (val > (hz * 5))
5798		val = hz * 5;
5799
5800	hdac_lock(sc);
5801	sc->poll_ival = val;
5802	hdac_unlock(sc);
5803
5804	return (err);
5805}
5806
5807#ifdef SND_DEBUG
5808static int
5809sysctl_hdac_dump(SYSCTL_HANDLER_ARGS)
5810{
5811	struct hdac_softc *sc;
5812	struct hdac_devinfo *devinfo;
5813	struct hdac_widget *w;
5814	device_t dev;
5815	uint32_t res, execres;
5816	int i, err, val;
5817	nid_t cad;
5818
5819	dev = oidp->oid_arg1;
5820	devinfo = pcm_getdevinfo(dev);
5821	if (devinfo == NULL || devinfo->codec == NULL ||
5822	    devinfo->codec->sc == NULL)
5823		return (EINVAL);
5824	val = 0;
5825	err = sysctl_handle_int(oidp, &val, 0, req);
5826	if (err != 0 || req->newptr == NULL || val == 0)
5827		return (err);
5828	sc = devinfo->codec->sc;
5829	cad = devinfo->codec->cad;
5830	hdac_lock(sc);
5831	device_printf(dev, "HDAC Dump AFG [nid=%d]:\n", devinfo->nid);
5832	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5833		w = hdac_widget_get(devinfo, i);
5834		if (w == NULL || w->type !=
5835		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5836			continue;
5837		execres = hdac_command(sc, HDA_CMD_SET_PIN_SENSE(cad, w->nid, 0),
5838		    cad);
5839		res = hdac_command(sc, HDA_CMD_GET_PIN_SENSE(cad, w->nid), cad);
5840		device_printf(dev, "nid=%-3d exec=0x%08x sense=0x%08x [%s]\n",
5841		    w->nid, execres, res,
5842		    (w->enable == 0) ? "DISABLED" : "ENABLED");
5843	}
5844	device_printf(dev,
5845	    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
5846	    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->function.audio.gpio),
5847	    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->function.audio.gpio),
5848	    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->function.audio.gpio),
5849	    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->function.audio.gpio),
5850	    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->function.audio.gpio));
5851	if (1 || HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->function.audio.gpio) > 0) {
5852		device_printf(dev, " GPI:");
5853		res = hdac_command(sc,
5854		    HDA_CMD_GET_GPI_DATA(cad, devinfo->nid), cad);
5855		printf(" data=0x%08x", res);
5856		res = hdac_command(sc,
5857		    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(cad, devinfo->nid),
5858		    cad);
5859		printf(" wake=0x%08x", res);
5860		res = hdac_command(sc,
5861		    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(cad, devinfo->nid),
5862		    cad);
5863		printf(" unsol=0x%08x", res);
5864		res = hdac_command(sc,
5865		    HDA_CMD_GET_GPI_STICKY_MASK(cad, devinfo->nid), cad);
5866		printf(" sticky=0x%08x\n", res);
5867	}
5868	if (1 || HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->function.audio.gpio) > 0) {
5869		device_printf(dev, " GPO:");
5870		res = hdac_command(sc,
5871		    HDA_CMD_GET_GPO_DATA(cad, devinfo->nid), cad);
5872		printf(" data=0x%08x\n", res);
5873	}
5874	if (1 || HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->function.audio.gpio) > 0) {
5875		device_printf(dev, "GPI0:");
5876		res = hdac_command(sc,
5877		    HDA_CMD_GET_GPIO_DATA(cad, devinfo->nid), cad);
5878		printf(" data=0x%08x", res);
5879		res = hdac_command(sc,
5880		    HDA_CMD_GET_GPIO_ENABLE_MASK(cad, devinfo->nid), cad);
5881		printf(" enable=0x%08x", res);
5882		res = hdac_command(sc,
5883		    HDA_CMD_GET_GPIO_DIRECTION(cad, devinfo->nid), cad);
5884		printf(" direction=0x%08x\n", res);
5885		res = hdac_command(sc,
5886		    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(cad, devinfo->nid), cad);
5887		device_printf(dev, "      wake=0x%08x", res);
5888		res = hdac_command(sc,
5889		    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(cad, devinfo->nid),
5890		    cad);
5891		printf("  unsol=0x%08x", res);
5892		res = hdac_command(sc,
5893		    HDA_CMD_GET_GPIO_STICKY_MASK(cad, devinfo->nid), cad);
5894		printf("    sticky=0x%08x\n", res);
5895	}
5896	hdac_unlock(sc);
5897	return (0);
5898}
5899#endif
5900#endif
5901
5902static void
5903hdac_attach2(void *arg)
5904{
5905	struct hdac_softc *sc;
5906	struct hdac_widget *w;
5907	struct hdac_audio_ctl *ctl;
5908	uint32_t quirks_on, quirks_off;
5909	int pcnt, rcnt;
5910	int i;
5911	char status[SND_STATUSLEN];
5912	device_t *devlist = NULL;
5913	int devcount;
5914	struct hdac_devinfo *devinfo = NULL;
5915
5916	sc = (struct hdac_softc *)arg;
5917
5918	hdac_config_fetch(sc, &quirks_on, &quirks_off);
5919
5920	HDA_BOOTVERBOSE(
5921		device_printf(sc->dev, "HDA_DEBUG: HDA Config: on=0x%08x off=0x%08x\n",
5922		    quirks_on, quirks_off);
5923	);
5924
5925	hdac_lock(sc);
5926
5927	/* Remove ourselves from the config hooks */
5928	if (sc->intrhook.ich_func != NULL) {
5929		config_intrhook_disestablish(&sc->intrhook);
5930		sc->intrhook.ich_func = NULL;
5931	}
5932
5933	/* Start the corb and rirb engines */
5934	HDA_BOOTVERBOSE(
5935		device_printf(sc->dev, "HDA_DEBUG: Starting CORB Engine...\n");
5936	);
5937	hdac_corb_start(sc);
5938	HDA_BOOTVERBOSE(
5939		device_printf(sc->dev, "HDA_DEBUG: Starting RIRB Engine...\n");
5940	);
5941	hdac_rirb_start(sc);
5942
5943	HDA_BOOTVERBOSE(
5944		device_printf(sc->dev,
5945		    "HDA_DEBUG: Enabling controller interrupt...\n");
5946	);
5947	if (sc->polling == 0)
5948		HDAC_WRITE_4(&sc->mem, HDAC_INTCTL,
5949		    HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
5950	HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
5951	    HDAC_GCTL_UNSOL);
5952
5953	DELAY(1000);
5954
5955	HDA_BOOTVERBOSE(
5956		device_printf(sc->dev, "HDA_DEBUG: Scanning HDA codecs...\n");
5957	);
5958	hdac_scan_codecs(sc);
5959
5960	device_get_children(sc->dev, &devlist, &devcount);
5961	for (i = 0; devlist != NULL && i < devcount; i++) {
5962		devinfo = (struct hdac_devinfo *)device_get_ivars(devlist[i]);
5963		if (devinfo != NULL && devinfo->node_type ==
5964		    HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) {
5965			break;
5966		} else
5967			devinfo = NULL;
5968	}
5969	if (devlist != NULL)
5970		free(devlist, M_TEMP);
5971
5972	if (devinfo == NULL) {
5973		hdac_unlock(sc);
5974		device_printf(sc->dev, "Audio Function Group not found!\n");
5975		hdac_release_resources(sc);
5976		return;
5977	}
5978
5979	HDA_BOOTVERBOSE(
5980		device_printf(sc->dev,
5981		    "HDA_DEBUG: Parsing AFG nid=%d cad=%d\n",
5982		    devinfo->nid, devinfo->codec->cad);
5983	);
5984	hdac_audio_parse(devinfo);
5985	HDA_BOOTVERBOSE(
5986		device_printf(sc->dev, "HDA_DEBUG: Parsing Ctls...\n");
5987	);
5988	hdac_audio_ctl_parse(devinfo);
5989	HDA_BOOTVERBOSE(
5990		device_printf(sc->dev, "HDA_DEBUG: Parsing vendor patch...\n");
5991	);
5992	hdac_vendor_patch_parse(devinfo);
5993	if (quirks_on != 0)
5994		devinfo->function.audio.quirks |= quirks_on;
5995	if (quirks_off != 0)
5996		devinfo->function.audio.quirks &= ~quirks_off;
5997
5998	/* XXX Disable all DIGITAL path. */
5999	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6000		w = hdac_widget_get(devinfo, i);
6001		if (w == NULL)
6002			continue;
6003		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
6004			w->enable = 0;
6005			continue;
6006		}
6007		/* XXX Disable useless pin ? */
6008		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
6009		    (w->wclass.pin.config &
6010		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
6011		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE)
6012			w->enable = 0;
6013	}
6014	i = 0;
6015	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
6016		if (ctl->widget == NULL)
6017			continue;
6018		if (ctl->ossmask & SOUND_MASK_DISABLE)
6019			ctl->enable = 0;
6020		w = ctl->widget;
6021		if (w->enable == 0 ||
6022		    HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
6023			ctl->enable = 0;
6024		w = ctl->childwidget;
6025		if (w == NULL)
6026			continue;
6027		if (w->enable == 0 ||
6028		    HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
6029			ctl->enable = 0;
6030	}
6031
6032	HDA_BOOTVERBOSE(
6033		device_printf(sc->dev, "HDA_DEBUG: Building AFG tree...\n");
6034	);
6035	hdac_audio_build_tree(devinfo);
6036
6037	i = 0;
6038	while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
6039		if (ctl->ossmask & (SOUND_MASK_SKIP | SOUND_MASK_DISABLE))
6040			ctl->ossmask = 0;
6041	}
6042	HDA_BOOTVERBOSE(
6043		device_printf(sc->dev, "HDA_DEBUG: AFG commit...\n");
6044	);
6045	hdac_audio_commit(devinfo, HDA_COMMIT_ALL);
6046	HDA_BOOTVERBOSE(
6047		device_printf(sc->dev, "HDA_DEBUG: Ctls commit...\n");
6048	);
6049	hdac_audio_ctl_commit(devinfo);
6050
6051	HDA_BOOTVERBOSE(
6052		device_printf(sc->dev, "HDA_DEBUG: PCMDIR_PLAY setup...\n");
6053	);
6054	pcnt = hdac_pcmchannel_setup(devinfo, PCMDIR_PLAY);
6055	HDA_BOOTVERBOSE(
6056		device_printf(sc->dev, "HDA_DEBUG: PCMDIR_REC setup...\n");
6057	);
6058	rcnt = hdac_pcmchannel_setup(devinfo, PCMDIR_REC);
6059
6060	hdac_unlock(sc);
6061	HDA_BOOTVERBOSE(
6062		device_printf(sc->dev,
6063		    "HDA_DEBUG: OSS mixer initialization...\n");
6064	);
6065
6066	/*
6067	 * There is no point of return after this. If the driver failed,
6068	 * so be it. Let the detach procedure do all the cleanup.
6069	 */
6070	if (mixer_init(sc->dev, &hdac_audio_ctl_ossmixer_class, devinfo) != 0)
6071		device_printf(sc->dev, "Can't register mixer\n");
6072
6073	if (pcnt > 0)
6074		pcnt = 1;
6075	if (rcnt > 0)
6076		rcnt = 1;
6077
6078	HDA_BOOTVERBOSE(
6079		device_printf(sc->dev,
6080		    "HDA_DEBUG: Registering PCM channels...\n");
6081	);
6082	if (pcm_register(sc->dev, devinfo, pcnt, rcnt) != 0)
6083		device_printf(sc->dev, "Can't register PCM\n");
6084
6085	sc->registered++;
6086
6087	if ((devinfo->function.audio.quirks & HDA_QUIRK_DMAPOS) &&
6088	    hdac_dma_alloc(sc, &sc->pos_dma,
6089	    (sc->num_iss + sc->num_oss + sc->num_bss) * 8) != 0) {
6090		HDA_BOOTVERBOSE(
6091			device_printf(sc->dev,
6092			    "Failed to allocate DMA pos buffer (non-fatal)\n");
6093		);
6094	}
6095
6096	for (i = 0; i < pcnt; i++)
6097		pcm_addchan(sc->dev, PCMDIR_PLAY, &hdac_channel_class, devinfo);
6098	for (i = 0; i < rcnt; i++)
6099		pcm_addchan(sc->dev, PCMDIR_REC, &hdac_channel_class, devinfo);
6100
6101#ifdef SND_DYNSYSCTL
6102	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
6103	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
6104	    "polling", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
6105	    sysctl_hdac_polling, "I", "Enable polling mode");
6106	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
6107	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
6108	    "polling_interval", CTLTYPE_INT | CTLFLAG_RW, sc->dev,
6109	    sizeof(sc->dev), sysctl_hdac_polling_interval, "I",
6110	    "Controller/Jack Sense polling interval (1-1000 ms)");
6111#ifdef SND_DEBUG
6112	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
6113	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
6114	    "dump", CTLTYPE_INT | CTLFLAG_RW, sc->dev, sizeof(sc->dev),
6115	    sysctl_hdac_dump, "I", "Dump states");
6116#endif
6117#endif
6118
6119	snprintf(status, SND_STATUSLEN, "at memory 0x%lx irq %ld %s [%s]",
6120	    rman_get_start(sc->mem.mem_res), rman_get_start(sc->irq.irq_res),
6121	    PCM_KLDSTRING(snd_hda), HDA_DRV_TEST_REV);
6122	pcm_setstatus(sc->dev, status);
6123	device_printf(sc->dev, "<HDA Codec: %s>\n", hdac_codec_name(devinfo));
6124	HDA_BOOTVERBOSE(
6125		device_printf(sc->dev, "<HDA Codec ID: 0x%08x>\n",
6126		    hdac_codec_id(devinfo));
6127	);
6128	device_printf(sc->dev, "<HDA Driver Revision: %s>\n",
6129	    HDA_DRV_TEST_REV);
6130
6131	HDA_BOOTVERBOSE(
6132		if (devinfo->function.audio.quirks != 0) {
6133			device_printf(sc->dev, "\n");
6134			device_printf(sc->dev, "HDA config/quirks:");
6135			for (i = 0; i < HDAC_QUIRKS_TAB_LEN; i++) {
6136				if ((devinfo->function.audio.quirks &
6137				    hdac_quirks_tab[i].value) ==
6138				    hdac_quirks_tab[i].value)
6139					printf(" %s", hdac_quirks_tab[i].key);
6140			}
6141			printf("\n");
6142		}
6143		device_printf(sc->dev, "\n");
6144		device_printf(sc->dev, "+-------------------+\n");
6145		device_printf(sc->dev, "| DUMPING HDA NODES |\n");
6146		device_printf(sc->dev, "+-------------------+\n");
6147		hdac_dump_nodes(devinfo);
6148		device_printf(sc->dev, "\n");
6149		device_printf(sc->dev, "+------------------------+\n");
6150		device_printf(sc->dev, "| DUMPING HDA AMPLIFIERS |\n");
6151		device_printf(sc->dev, "+------------------------+\n");
6152		device_printf(sc->dev, "\n");
6153		i = 0;
6154		while ((ctl = hdac_audio_ctl_each(devinfo, &i)) != NULL) {
6155			device_printf(sc->dev, "%3d: nid=%d", i,
6156			    (ctl->widget != NULL) ? ctl->widget->nid : -1);
6157			if (ctl->childwidget != NULL)
6158				printf(" cnid=%d", ctl->childwidget->nid);
6159			printf(" dir=0x%x index=%d "
6160			    "ossmask=0x%08x ossdev=%d%s\n",
6161			    ctl->dir, ctl->index,
6162			    ctl->ossmask, ctl->ossdev,
6163			    (ctl->enable == 0) ? " [DISABLED]" : "");
6164		}
6165		device_printf(sc->dev, "\n");
6166		device_printf(sc->dev, "+-----------------------------------+\n");
6167		device_printf(sc->dev, "| DUMPING HDA AUDIO/VOLUME CONTROLS |\n");
6168		device_printf(sc->dev, "+-----------------------------------+\n");
6169		hdac_dump_ctls(devinfo, "Master Volume (OSS: vol)", SOUND_MASK_VOLUME);
6170		hdac_dump_ctls(devinfo, "PCM Volume (OSS: pcm)", SOUND_MASK_PCM);
6171		hdac_dump_ctls(devinfo, "CD Volume (OSS: cd)", SOUND_MASK_CD);
6172		hdac_dump_ctls(devinfo, "Microphone Volume (OSS: mic)", SOUND_MASK_MIC);
6173		hdac_dump_ctls(devinfo, "Line-in Volume (OSS: line)", SOUND_MASK_LINE);
6174		hdac_dump_ctls(devinfo, "Recording Level (OSS: rec)", SOUND_MASK_RECLEV);
6175		hdac_dump_ctls(devinfo, "Speaker/Beep (OSS: speaker)", SOUND_MASK_SPEAKER);
6176		hdac_dump_ctls(devinfo, NULL, 0);
6177		hdac_dump_dac(devinfo);
6178		hdac_dump_adc(devinfo);
6179		device_printf(sc->dev, "\n");
6180		device_printf(sc->dev, "+--------------------------------------+\n");
6181		device_printf(sc->dev, "| DUMPING PCM Playback/Record Channels |\n");
6182		device_printf(sc->dev, "+--------------------------------------+\n");
6183		hdac_dump_pcmchannels(sc, pcnt, rcnt);
6184	);
6185
6186	if (sc->polling != 0) {
6187		hdac_lock(sc);
6188		callout_reset(&sc->poll_hdac, 1, hdac_poll_callback, sc);
6189		hdac_unlock(sc);
6190	}
6191}
6192
6193/****************************************************************************
6194 * int hdac_detach(device_t)
6195 *
6196 * Detach and free up resources utilized by the hdac device.
6197 ****************************************************************************/
6198static int
6199hdac_detach(device_t dev)
6200{
6201	struct hdac_softc *sc = NULL;
6202	struct hdac_devinfo *devinfo = NULL;
6203	int err;
6204
6205	devinfo = (struct hdac_devinfo *)pcm_getdevinfo(dev);
6206	if (devinfo != NULL && devinfo->codec != NULL)
6207		sc = devinfo->codec->sc;
6208	if (sc == NULL)
6209		return (0);
6210
6211	if (sc->registered > 0) {
6212		err = pcm_unregister(dev);
6213		if (err != 0)
6214			return (err);
6215	}
6216
6217	hdac_release_resources(sc);
6218
6219	return (0);
6220}
6221
6222static device_method_t hdac_methods[] = {
6223	/* device interface */
6224	DEVMETHOD(device_probe,		hdac_probe),
6225	DEVMETHOD(device_attach,	hdac_attach),
6226	DEVMETHOD(device_detach,	hdac_detach),
6227	{ 0, 0 }
6228};
6229
6230static driver_t hdac_driver = {
6231	"pcm",
6232	hdac_methods,
6233	PCM_SOFTC_SIZE,
6234};
6235
6236DRIVER_MODULE(snd_hda, pci, hdac_driver, pcm_devclass, 0, 0);
6237MODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
6238MODULE_VERSION(snd_hda, 1);
6239