1230130Smav/*-
2230130Smav * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3230130Smav * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4230130Smav * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
5230130Smav * All rights reserved.
6230130Smav *
7230130Smav * Redistribution and use in source and binary forms, with or without
8230130Smav * modification, are permitted provided that the following conditions
9230130Smav * are met:
10230130Smav * 1. Redistributions of source code must retain the above copyright
11230130Smav *    notice, this list of conditions and the following disclaimer.
12230130Smav * 2. Redistributions in binary form must reproduce the above copyright
13230130Smav *    notice, this list of conditions and the following disclaimer in the
14230130Smav *    documentation and/or other materials provided with the distribution.
15230130Smav *
16230130Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17230130Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18230130Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19230130Smav * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20230130Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21230130Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22230130Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23230130Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24230130Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25230130Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26230130Smav * SUCH DAMAGE.
27230130Smav */
28230130Smav
29230130Smav/*
30230130Smav * Intel High Definition Audio (Audio function) driver for FreeBSD.
31230130Smav */
32230130Smav
33230130Smav#ifdef HAVE_KERNEL_OPTION_HEADERS
34230130Smav#include "opt_snd.h"
35230130Smav#endif
36230130Smav
37230130Smav#include <dev/sound/pcm/sound.h>
38230130Smav
39230130Smav#include <sys/ctype.h>
40230130Smav#include <sys/taskqueue.h>
41230130Smav
42230130Smav#include <dev/sound/pci/hda/hdac.h>
43230130Smav#include <dev/sound/pci/hda/hdaa.h>
44230130Smav#include <dev/sound/pci/hda/hda_reg.h>
45230130Smav
46230130Smav#include "mixer_if.h"
47230130Smav
48230130SmavSND_DECLARE_FILE("$FreeBSD: stable/11/sys/dev/sound/pci/hda/hdaa.c 317010 2017-04-16 08:04:01Z mmel $");
49230130Smav
50230130Smav#define hdaa_lock(devinfo)	snd_mtxlock((devinfo)->lock)
51230130Smav#define hdaa_unlock(devinfo)	snd_mtxunlock((devinfo)->lock)
52230130Smav#define hdaa_lockassert(devinfo) snd_mtxassert((devinfo)->lock)
53230130Smav#define hdaa_lockowned(devinfo)	mtx_owned((devinfo)->lock)
54230130Smav
55230130Smavstatic const struct {
56264832Smarius	const char *key;
57230130Smav	uint32_t value;
58230130Smav} hdaa_quirks_tab[] = {
59230130Smav	{ "softpcmvol", HDAA_QUIRK_SOFTPCMVOL },
60230130Smav	{ "fixedrate", HDAA_QUIRK_FIXEDRATE },
61230130Smav	{ "forcestereo", HDAA_QUIRK_FORCESTEREO },
62230130Smav	{ "eapdinv", HDAA_QUIRK_EAPDINV },
63230130Smav	{ "senseinv", HDAA_QUIRK_SENSEINV },
64230130Smav	{ "ivref50", HDAA_QUIRK_IVREF50 },
65230130Smav	{ "ivref80", HDAA_QUIRK_IVREF80 },
66230130Smav	{ "ivref100", HDAA_QUIRK_IVREF100 },
67230130Smav	{ "ovref50", HDAA_QUIRK_OVREF50 },
68230130Smav	{ "ovref80", HDAA_QUIRK_OVREF80 },
69230130Smav	{ "ovref100", HDAA_QUIRK_OVREF100 },
70230130Smav	{ "ivref", HDAA_QUIRK_IVREF },
71230130Smav	{ "ovref", HDAA_QUIRK_OVREF },
72230130Smav	{ "vref", HDAA_QUIRK_VREF },
73230130Smav};
74230130Smav
75230130Smav#define HDA_PARSE_MAXDEPTH	10
76230130Smav
77230130SmavMALLOC_DEFINE(M_HDAA, "hdaa", "HDA Audio");
78230130Smav
79264832Smariusstatic const char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue",
80264832Smarius    "Green", "Red", "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B",
81264832Smarius    "Res.C", "Res.D", "White", "Other"};
82230130Smav
83264832Smariusstatic const char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD",
84230130Smav    "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in",
85230130Smav    "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"};
86230130Smav
87264832Smariusstatic const char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
88230130Smav
89264832Smariusstatic const char *HDA_CONNECTORS[16] = {
90230130Smav    "Unknown", "1/8", "1/4", "ATAPI", "RCA", "Optical", "Digital", "Analog",
91230130Smav    "DIN", "XLR", "RJ-11", "Combo", "0xc", "0xd", "0xe", "Other" };
92230130Smav
93264832Smariusstatic const char *HDA_LOCS[64] = {
94230130Smav    "0x00", "Rear", "Front", "Left", "Right", "Top", "Bottom", "Rear-panel",
95230130Smav	"Drive-bay", "0x09", "0x0a", "0x0b", "0x0c", "0x0d", "0x0e", "0x0f",
96230130Smav    "Internal", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "Riser",
97230130Smav	"0x18", "Onboard", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f",
98230130Smav    "External", "Ext-Rear", "Ext-Front", "Ext-Left", "Ext-Right", "Ext-Top", "Ext-Bottom", "0x07",
99230130Smav	"0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f",
100230130Smav    "Other", "0x31", "0x32", "0x33", "0x34", "0x35", "Other-Bott", "Lid-In",
101230130Smav	"Lid-Out", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f" };
102230130Smav
103264832Smariusstatic const char *HDA_GPIO_ACTIONS[8] = {
104230130Smav    "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"};
105230130Smav
106264832Smariusstatic const char *HDA_HDMI_CODING_TYPES[18] = {
107230312Smav    "undefined", "LPCM", "AC-3", "MPEG1", "MP3", "MPEG2", "AAC-LC", "DTS",
108230312Smav    "ATRAC", "DSD", "E-AC-3", "DTS-HD", "MLP", "DST", "WMAPro", "HE-AAC",
109230312Smav    "HE-AACv2", "MPEG-Surround"
110230312Smav};
111230312Smav
112230130Smav/* Default */
113230130Smavstatic uint32_t hdaa_fmt[] = {
114230130Smav	SND_FORMAT(AFMT_S16_LE, 2, 0),
115230130Smav	0
116230130Smav};
117230130Smav
118230130Smavstatic struct pcmchan_caps hdaa_caps = {48000, 48000, hdaa_fmt, 0};
119230130Smav
120230130Smavstatic const struct {
121230130Smav	uint32_t	rate;
122230130Smav	int		valid;
123230130Smav	uint16_t	base;
124230130Smav	uint16_t	mul;
125230130Smav	uint16_t	div;
126230130Smav} hda_rate_tab[] = {
127230130Smav	{   8000, 1, 0x0000, 0x0000, 0x0500 },	/* (48000 * 1) / 6 */
128230130Smav	{   9600, 0, 0x0000, 0x0000, 0x0400 },	/* (48000 * 1) / 5 */
129230130Smav	{  12000, 0, 0x0000, 0x0000, 0x0300 },	/* (48000 * 1) / 4 */
130230130Smav	{  16000, 1, 0x0000, 0x0000, 0x0200 },	/* (48000 * 1) / 3 */
131230130Smav	{  18000, 0, 0x0000, 0x1000, 0x0700 },	/* (48000 * 3) / 8 */
132230130Smav	{  19200, 0, 0x0000, 0x0800, 0x0400 },	/* (48000 * 2) / 5 */
133230130Smav	{  24000, 0, 0x0000, 0x0000, 0x0100 },	/* (48000 * 1) / 2 */
134230130Smav	{  28800, 0, 0x0000, 0x1000, 0x0400 },	/* (48000 * 3) / 5 */
135230130Smav	{  32000, 1, 0x0000, 0x0800, 0x0200 },	/* (48000 * 2) / 3 */
136230130Smav	{  36000, 0, 0x0000, 0x1000, 0x0300 },	/* (48000 * 3) / 4 */
137230130Smav	{  38400, 0, 0x0000, 0x1800, 0x0400 },	/* (48000 * 4) / 5 */
138230130Smav	{  48000, 1, 0x0000, 0x0000, 0x0000 },	/* (48000 * 1) / 1 */
139230130Smav	{  64000, 0, 0x0000, 0x1800, 0x0200 },	/* (48000 * 4) / 3 */
140230130Smav	{  72000, 0, 0x0000, 0x1000, 0x0100 },	/* (48000 * 3) / 2 */
141230130Smav	{  96000, 1, 0x0000, 0x0800, 0x0000 },	/* (48000 * 2) / 1 */
142230130Smav	{ 144000, 0, 0x0000, 0x1000, 0x0000 },	/* (48000 * 3) / 1 */
143230130Smav	{ 192000, 1, 0x0000, 0x1800, 0x0000 },	/* (48000 * 4) / 1 */
144230130Smav	{   8820, 0, 0x4000, 0x0000, 0x0400 },	/* (44100 * 1) / 5 */
145230130Smav	{  11025, 1, 0x4000, 0x0000, 0x0300 },	/* (44100 * 1) / 4 */
146230130Smav	{  12600, 0, 0x4000, 0x0800, 0x0600 },	/* (44100 * 2) / 7 */
147230130Smav	{  14700, 0, 0x4000, 0x0000, 0x0200 },	/* (44100 * 1) / 3 */
148230130Smav	{  17640, 0, 0x4000, 0x0800, 0x0400 },	/* (44100 * 2) / 5 */
149230130Smav	{  18900, 0, 0x4000, 0x1000, 0x0600 },	/* (44100 * 3) / 7 */
150230130Smav	{  22050, 1, 0x4000, 0x0000, 0x0100 },	/* (44100 * 1) / 2 */
151230130Smav	{  25200, 0, 0x4000, 0x1800, 0x0600 },	/* (44100 * 4) / 7 */
152230130Smav	{  26460, 0, 0x4000, 0x1000, 0x0400 },	/* (44100 * 3) / 5 */
153230130Smav	{  29400, 0, 0x4000, 0x0800, 0x0200 },	/* (44100 * 2) / 3 */
154230130Smav	{  33075, 0, 0x4000, 0x1000, 0x0300 },	/* (44100 * 3) / 4 */
155230130Smav	{  35280, 0, 0x4000, 0x1800, 0x0400 },	/* (44100 * 4) / 5 */
156230130Smav	{  44100, 1, 0x4000, 0x0000, 0x0000 },	/* (44100 * 1) / 1 */
157230130Smav	{  58800, 0, 0x4000, 0x1800, 0x0200 },	/* (44100 * 4) / 3 */
158230130Smav	{  66150, 0, 0x4000, 0x1000, 0x0100 },	/* (44100 * 3) / 2 */
159230130Smav	{  88200, 1, 0x4000, 0x0800, 0x0000 },	/* (44100 * 2) / 1 */
160230130Smav	{ 132300, 0, 0x4000, 0x1000, 0x0000 },	/* (44100 * 3) / 1 */
161230130Smav	{ 176400, 1, 0x4000, 0x1800, 0x0000 },	/* (44100 * 4) / 1 */
162230130Smav};
163230130Smav#define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
164230130Smav
165230451Smavconst static char *ossnames[] = SOUND_DEVICE_NAMES;
166230451Smav
167230130Smav/****************************************************************************
168230130Smav * Function prototypes
169230130Smav ****************************************************************************/
170230130Smavstatic int	hdaa_pcmchannel_setup(struct hdaa_chan *);
171230130Smav
172230130Smavstatic void	hdaa_widget_connection_select(struct hdaa_widget *, uint8_t);
173230130Smavstatic void	hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *,
174230130Smav						uint32_t, int, int);
175230130Smavstatic struct	hdaa_audio_ctl *hdaa_audio_ctl_amp_get(struct hdaa_devinfo *,
176230130Smav							nid_t, int, int, int);
177230130Smavstatic void	hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *,
178230130Smav				nid_t, int, int, int, int, int, int);
179230130Smav
180230130Smavstatic void	hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf);
181230130Smav
182230130Smavstatic char *
183230130Smavhdaa_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
184230130Smav{
185230130Smav	int i, first = 1;
186230130Smav
187230130Smav	bzero(buf, len);
188230130Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
189230130Smav		if (mask & (1 << i)) {
190230130Smav			if (first == 0)
191230130Smav				strlcat(buf, ", ", len);
192230451Smav			strlcat(buf, ossnames[i], len);
193230130Smav			first = 0;
194230130Smav		}
195230130Smav	}
196230130Smav	return (buf);
197230130Smav}
198230130Smav
199230130Smavstatic struct hdaa_audio_ctl *
200230130Smavhdaa_audio_ctl_each(struct hdaa_devinfo *devinfo, int *index)
201230130Smav{
202230130Smav	if (devinfo == NULL ||
203230130Smav	    index == NULL || devinfo->ctl == NULL ||
204230130Smav	    devinfo->ctlcnt < 1 ||
205230130Smav	    *index < 0 || *index >= devinfo->ctlcnt)
206230130Smav		return (NULL);
207230130Smav	return (&devinfo->ctl[(*index)++]);
208230130Smav}
209230130Smav
210230130Smavstatic struct hdaa_audio_ctl *
211230130Smavhdaa_audio_ctl_amp_get(struct hdaa_devinfo *devinfo, nid_t nid, int dir,
212230130Smav						int index, int cnt)
213230130Smav{
214230130Smav	struct hdaa_audio_ctl *ctl;
215230130Smav	int i, found = 0;
216230130Smav
217230130Smav	if (devinfo == NULL || devinfo->ctl == NULL)
218230130Smav		return (NULL);
219230130Smav
220230130Smav	i = 0;
221230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
222230130Smav		if (ctl->enable == 0)
223230130Smav			continue;
224230130Smav		if (ctl->widget->nid != nid)
225230130Smav			continue;
226230130Smav		if (dir && ctl->ndir != dir)
227230130Smav			continue;
228230130Smav		if (index >= 0 && ctl->ndir == HDAA_CTL_IN &&
229230130Smav		    ctl->dir == ctl->ndir && ctl->index != index)
230230130Smav			continue;
231230130Smav		found++;
232230130Smav		if (found == cnt || cnt <= 0)
233230130Smav			return (ctl);
234230130Smav	}
235230130Smav
236230130Smav	return (NULL);
237230130Smav}
238230130Smav
239243181Smavstatic const struct matrix {
240243181Smav	struct pcmchan_matrix	m;
241243181Smav	int			analog;
242243181Smav} matrixes[]  = {
243243181Smav    { SND_CHN_MATRIX_MAP_1_0,	1 },
244243181Smav    { SND_CHN_MATRIX_MAP_2_0,	1 },
245243181Smav    { SND_CHN_MATRIX_MAP_2_1,	0 },
246243181Smav    { SND_CHN_MATRIX_MAP_3_0,	0 },
247243181Smav    { SND_CHN_MATRIX_MAP_3_1,	0 },
248243181Smav    { SND_CHN_MATRIX_MAP_4_0,	1 },
249243181Smav    { SND_CHN_MATRIX_MAP_4_1,	0 },
250243181Smav    { SND_CHN_MATRIX_MAP_5_0,	0 },
251243181Smav    { SND_CHN_MATRIX_MAP_5_1,	1 },
252243181Smav    { SND_CHN_MATRIX_MAP_6_0,	0 },
253243181Smav    { SND_CHN_MATRIX_MAP_6_1,	0 },
254243181Smav    { SND_CHN_MATRIX_MAP_7_0,	0 },
255243181Smav    { SND_CHN_MATRIX_MAP_7_1,	1 },
256243181Smav};
257243181Smav
258243181Smavstatic const char *channel_names[] = SND_CHN_T_NAMES;
259243181Smav
260230130Smav/*
261243181Smav * Connected channels change handler.
262243181Smav */
263243181Smavstatic void
264243181Smavhdaa_channels_handler(struct hdaa_audio_as *as)
265243181Smav{
266243181Smav	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
267243181Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
268243181Smav	struct hdaa_chan *ch = &devinfo->chans[as->chans[0]];
269243181Smav	struct hdaa_widget *w;
270243181Smav	uint8_t *eld;
271243181Smav	int i, total, sub, assume, channels;
272243181Smav	uint16_t cpins, upins, tpins;
273243181Smav
274243181Smav	cpins = upins = 0;
275243181Smav	eld = NULL;
276243181Smav	for (i = 0; i < 16; i++) {
277243181Smav		if (as->pins[i] <= 0)
278243181Smav			continue;
279243181Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
280243181Smav		if (w == NULL)
281243181Smav			continue;
282243181Smav		if (w->wclass.pin.connected == 1)
283243181Smav			cpins |= (1 << i);
284243181Smav		else if (w->wclass.pin.connected != 0)
285243181Smav			upins |= (1 << i);
286243181Smav		if (w->eld != NULL && w->eld_len >= 8)
287243181Smav			eld = w->eld;
288243181Smav	}
289243181Smav	tpins = cpins | upins;
290243181Smav	if (as->hpredir >= 0)
291243181Smav		tpins &= 0x7fff;
292243181Smav	if (tpins == 0)
293243181Smav		tpins = as->pinset;
294243181Smav
295243181Smav	total = sub = assume = channels = 0;
296243181Smav	if (eld) {
297243181Smav		/* Map CEA speakers to sound(4) channels. */
298243181Smav		if (eld[7] & 0x01) /* Front Left/Right */
299243181Smav			channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
300243181Smav		if (eld[7] & 0x02) /* Low Frequency Effect */
301243181Smav			channels |= SND_CHN_T_MASK_LF;
302243181Smav		if (eld[7] & 0x04) /* Front Center */
303243181Smav			channels |= SND_CHN_T_MASK_FC;
304243181Smav		if (eld[7] & 0x08) { /* Rear Left/Right */
305243181Smav			/* If we have both RLR and RLRC, report RLR as side. */
306243181Smav			if (eld[7] & 0x40) /* Rear Left/Right Center */
307243181Smav			    channels |= SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR;
308243181Smav			else
309243181Smav			    channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
310243181Smav		}
311243181Smav		if (eld[7] & 0x10) /* Rear center */
312243181Smav			channels |= SND_CHN_T_MASK_BC;
313243181Smav		if (eld[7] & 0x20) /* Front Left/Right Center */
314243181Smav			channels |= SND_CHN_T_MASK_FLC | SND_CHN_T_MASK_FRC;
315243181Smav		if (eld[7] & 0x40) /* Rear Left/Right Center */
316243181Smav			channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
317243181Smav	} else if (as->pinset != 0 && (tpins & 0xffe0) == 0) {
318243181Smav		/* Map UAA speakers to sound(4) channels. */
319243181Smav		if (tpins & 0x0001)
320243181Smav			channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
321243181Smav		if (tpins & 0x0002)
322243181Smav			channels |= SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF;
323243181Smav		if (tpins & 0x0004)
324243181Smav			channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
325243181Smav		if (tpins & 0x0008)
326243181Smav			channels |= SND_CHN_T_MASK_FLC | SND_CHN_T_MASK_FRC;
327243181Smav		if (tpins & 0x0010) {
328243181Smav			/* If there is no back pin, report side as back. */
329243181Smav			if ((as->pinset & 0x0004) == 0)
330243181Smav			    channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
331243181Smav			else
332243181Smav			    channels |= SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR;
333243181Smav		}
334243181Smav	} else if (as->mixed) {
335243181Smav		/* Mixed assoc can be only stereo or theoretically mono. */
336243181Smav		if (ch->channels == 1)
337243181Smav			channels |= SND_CHN_T_MASK_FC;
338243181Smav		else
339243181Smav			channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
340243181Smav	}
341243181Smav	if (channels) {	/* We have some usable channels info. */
342243181Smav		HDA_BOOTVERBOSE(
343243181Smav			device_printf(pdevinfo->dev, "%s channel set is: ",
344243181Smav			    as->dir == HDAA_CTL_OUT ? "Playback" : "Recording");
345243181Smav			for (i = 0; i < SND_CHN_T_MAX; i++)
346243181Smav				if (channels & (1 << i))
347243181Smav					printf("%s, ", channel_names[i]);
348243181Smav			printf("\n");
349243181Smav		);
350243181Smav		/* Look for maximal fitting matrix. */
351243181Smav		for (i = 0; i < sizeof(matrixes) / sizeof(struct matrix); i++) {
352243181Smav			if (as->pinset != 0 && matrixes[i].analog == 0)
353243181Smav				continue;
354243181Smav			if ((matrixes[i].m.mask & ~channels) == 0) {
355243181Smav				total = matrixes[i].m.channels;
356243181Smav				sub = matrixes[i].m.ext;
357243181Smav			}
358243181Smav		}
359243181Smav	}
360243181Smav	if (total == 0) {
361243181Smav		assume = 1;
362243181Smav		total = ch->channels;
363243181Smav		sub = (total == 6 || total == 8) ? 1 : 0;
364243181Smav	}
365243181Smav	HDA_BOOTVERBOSE(
366243181Smav		device_printf(pdevinfo->dev,
367243181Smav		    "%s channel matrix is: %s%d.%d (%s)\n",
368243181Smav		    as->dir == HDAA_CTL_OUT ? "Playback" : "Recording",
369243181Smav		    assume ? "unknown, assuming " : "", total - sub, sub,
370243181Smav		    cpins != 0 ? "connected" :
371243181Smav		    (upins != 0 ? "unknown" : "disconnected"));
372243181Smav	);
373243181Smav}
374243181Smav
375243181Smav/*
376230551Smav * Headphones redirection change handler.
377230130Smav */
378230130Smavstatic void
379230551Smavhdaa_hpredir_handler(struct hdaa_widget *w)
380230130Smav{
381230312Smav	struct hdaa_devinfo *devinfo = w->devinfo;
382230551Smav	struct hdaa_audio_as *as = &devinfo->as[w->bindas];
383230312Smav	struct hdaa_widget *w1;
384230130Smav	struct hdaa_audio_ctl *ctl;
385230551Smav	uint32_t val;
386230551Smav	int j, connected = w->wclass.pin.connected;
387230130Smav
388230130Smav	HDA_BOOTVERBOSE(
389230551Smav		device_printf((as->pdevinfo && as->pdevinfo->dev) ?
390230551Smav		    as->pdevinfo->dev : devinfo->dev,
391230551Smav		    "Redirect output to: %s\n",
392230551Smav		    connected ? "headphones": "main");
393230130Smav	);
394230130Smav	/* (Un)Mute headphone pin. */
395230130Smav	ctl = hdaa_audio_ctl_amp_get(devinfo,
396230312Smav	    w->nid, HDAA_CTL_IN, -1, 1);
397230130Smav	if (ctl != NULL && ctl->mute) {
398230130Smav		/* If pin has muter - use it. */
399230551Smav		val = connected ? 0 : 1;
400230130Smav		if (val != ctl->forcemute) {
401230130Smav			ctl->forcemute = val;
402230130Smav			hdaa_audio_ctl_amp_set(ctl,
403230130Smav			    HDAA_AMP_MUTE_DEFAULT,
404230130Smav			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
405230130Smav		}
406230130Smav	} else {
407230130Smav		/* If there is no muter - disable pin output. */
408230551Smav		if (connected)
409230312Smav			val = w->wclass.pin.ctrl |
410230312Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
411230312Smav		else
412230312Smav			val = w->wclass.pin.ctrl &
413230312Smav			    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
414230312Smav		if (val != w->wclass.pin.ctrl) {
415230312Smav			w->wclass.pin.ctrl = val;
416230312Smav			hda_command(devinfo->dev,
417230312Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
418230312Smav			    w->nid, w->wclass.pin.ctrl));
419230130Smav		}
420230130Smav	}
421230130Smav	/* (Un)Mute other pins. */
422230130Smav	for (j = 0; j < 15; j++) {
423230130Smav		if (as->pins[j] <= 0)
424230130Smav			continue;
425230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo,
426230130Smav		    as->pins[j], HDAA_CTL_IN, -1, 1);
427230130Smav		if (ctl != NULL && ctl->mute) {
428230130Smav			/* If pin has muter - use it. */
429230551Smav			val = connected ? 1 : 0;
430230130Smav			if (val == ctl->forcemute)
431230130Smav				continue;
432230130Smav			ctl->forcemute = val;
433230130Smav			hdaa_audio_ctl_amp_set(ctl,
434230130Smav			    HDAA_AMP_MUTE_DEFAULT,
435230130Smav			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
436230130Smav			continue;
437230130Smav		}
438230130Smav		/* If there is no muter - disable pin output. */
439230312Smav		w1 = hdaa_widget_get(devinfo, as->pins[j]);
440230551Smav		if (w1 != NULL) {
441230551Smav			if (connected)
442230312Smav				val = w1->wclass.pin.ctrl &
443230130Smav				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
444230130Smav			else
445230312Smav				val = w1->wclass.pin.ctrl |
446230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
447230312Smav			if (val != w1->wclass.pin.ctrl) {
448230312Smav				w1->wclass.pin.ctrl = val;
449230130Smav				hda_command(devinfo->dev,
450230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
451230312Smav				    w1->nid, w1->wclass.pin.ctrl));
452230130Smav			}
453230130Smav		}
454230130Smav	}
455230130Smav}
456230130Smav
457230130Smav/*
458230551Smav * Recording source change handler.
459230130Smav */
460230130Smavstatic void
461230551Smavhdaa_autorecsrc_handler(struct hdaa_audio_as *as, struct hdaa_widget *w)
462230130Smav{
463230551Smav	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
464230551Smav	struct hdaa_devinfo *devinfo;
465230551Smav	struct hdaa_widget *w1;
466230551Smav	int i, mask, fullmask, prio, bestprio;
467230551Smav	char buf[128];
468230130Smav
469230551Smav	if (!as->mixed || pdevinfo == NULL || pdevinfo->mixer == NULL)
470230130Smav		return;
471230551Smav	/* Don't touch anything if we asked not to. */
472230551Smav	if (pdevinfo->autorecsrc == 0 ||
473230551Smav	    (pdevinfo->autorecsrc == 1 && w != NULL))
474230551Smav		return;
475230551Smav	/* Don't touch anything if "mix" or "speaker" selected. */
476230551Smav	if (pdevinfo->recsrc & (SOUND_MASK_IMIX | SOUND_MASK_SPEAKER))
477230551Smav		return;
478230551Smav	/* Don't touch anything if several selected. */
479230551Smav	if (ffs(pdevinfo->recsrc) != fls(pdevinfo->recsrc))
480230551Smav		return;
481230551Smav	devinfo = pdevinfo->devinfo;
482230551Smav	mask = fullmask = 0;
483230551Smav	bestprio = 0;
484230551Smav	for (i = 0; i < 16; i++) {
485230551Smav		if (as->pins[i] <= 0)
486230130Smav			continue;
487230551Smav		w1 = hdaa_widget_get(devinfo, as->pins[i]);
488230551Smav		if (w1 == NULL || w1->enable == 0)
489230312Smav			continue;
490230551Smav		if (w1->wclass.pin.connected == 0)
491230551Smav			continue;
492230551Smav		prio = (w1->wclass.pin.connected == 1) ? 2 : 1;
493230551Smav		if (prio < bestprio)
494230551Smav			continue;
495230551Smav		if (prio > bestprio) {
496230551Smav			mask = 0;
497230551Smav			bestprio = prio;
498230551Smav		}
499230551Smav		mask |= (1 << w1->ossdev);
500230551Smav		fullmask |= (1 << w1->ossdev);
501230130Smav	}
502230551Smav	if (mask == 0)
503230551Smav		return;
504230551Smav	/* Prefer newly connected input. */
505230551Smav	if (w != NULL && (mask & (1 << w->ossdev)))
506230551Smav		mask = (1 << w->ossdev);
507230551Smav	/* Prefer previously selected input */
508230551Smav	if (mask & pdevinfo->recsrc)
509230551Smav		mask &= pdevinfo->recsrc;
510230551Smav	/* Prefer mic. */
511230551Smav	if (mask & SOUND_MASK_MIC)
512230551Smav		mask = SOUND_MASK_MIC;
513230551Smav	/* Prefer monitor (2nd mic). */
514230551Smav	if (mask & SOUND_MASK_MONITOR)
515230551Smav		mask = SOUND_MASK_MONITOR;
516230551Smav	/* Just take first one. */
517230551Smav	mask = (1 << (ffs(mask) - 1));
518230551Smav	HDA_BOOTVERBOSE(
519230551Smav		hdaa_audio_ctl_ossmixer_mask2allname(mask, buf, sizeof(buf));
520230551Smav		device_printf(pdevinfo->dev,
521230551Smav		    "Automatically set rec source to: %s\n", buf);
522230551Smav	);
523230130Smav	hdaa_unlock(devinfo);
524230551Smav	mix_setrecsrc(pdevinfo->mixer, mask);
525230551Smav	hdaa_lock(devinfo);
526230130Smav}
527230130Smav
528230130Smav/*
529230551Smav * Jack presence detection event handler.
530230130Smav */
531230130Smavstatic void
532230551Smavhdaa_presence_handler(struct hdaa_widget *w)
533230130Smav{
534230551Smav	struct hdaa_devinfo *devinfo = w->devinfo;
535230551Smav	struct hdaa_audio_as *as;
536230551Smav	uint32_t res;
537242357Smav	int connected, old;
538230130Smav
539230551Smav	if (w->enable == 0 || w->type !=
540230551Smav	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
541230551Smav		return;
542230130Smav
543230551Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
544230551Smav	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
545230551Smav		return;
546230551Smav
547230551Smav	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
548230551Smav	connected = (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) != 0;
549230551Smav	if (devinfo->quirks & HDAA_QUIRK_SENSEINV)
550230551Smav		connected = !connected;
551242357Smav	old = w->wclass.pin.connected;
552242357Smav	if (connected == old)
553230551Smav		return;
554230551Smav	w->wclass.pin.connected = connected;
555230551Smav	HDA_BOOTVERBOSE(
556242357Smav		if (connected || old != 2) {
557242357Smav			device_printf(devinfo->dev,
558246983Suqs			    "Pin sense: nid=%d sense=0x%08x (%sconnected)\n",
559242357Smav			    w->nid, res, !connected ? "dis" : "");
560242357Smav		}
561230551Smav	);
562230551Smav
563230551Smav	as = &devinfo->as[w->bindas];
564230551Smav	if (as->hpredir >= 0 && as->pins[15] == w->nid)
565230551Smav		hdaa_hpredir_handler(w);
566242357Smav	if (as->dir == HDAA_CTL_IN && old != 2)
567230551Smav		hdaa_autorecsrc_handler(as, w);
568243181Smav	if (old != 2)
569243181Smav		hdaa_channels_handler(as);
570230130Smav}
571230130Smav
572230551Smav/*
573230551Smav * Callback for poll based presence detection.
574230551Smav */
575230130Smavstatic void
576230551Smavhdaa_jack_poll_callback(void *arg)
577230130Smav{
578230551Smav	struct hdaa_devinfo *devinfo = arg;
579230551Smav	struct hdaa_widget *w;
580230551Smav	int i;
581230130Smav
582230551Smav	hdaa_lock(devinfo);
583230551Smav	if (devinfo->poll_ival == 0) {
584230551Smav		hdaa_unlock(devinfo);
585230551Smav		return;
586230551Smav	}
587230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
588230551Smav		if (devinfo->as[i].hpredir < 0)
589230551Smav			continue;
590230551Smav		w = hdaa_widget_get(devinfo, devinfo->as[i].pins[15]);
591230130Smav		if (w == NULL || w->enable == 0 || w->type !=
592230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
593230130Smav			continue;
594230551Smav		hdaa_presence_handler(w);
595230130Smav	}
596230551Smav	callout_reset(&devinfo->poll_jack, devinfo->poll_ival,
597230551Smav	    hdaa_jack_poll_callback, devinfo);
598230551Smav	hdaa_unlock(devinfo);
599230130Smav}
600230130Smav
601230312Smavstatic void
602230312Smavhdaa_eld_dump(struct hdaa_widget *w)
603230312Smav{
604230312Smav	struct hdaa_devinfo *devinfo = w->devinfo;
605230312Smav	device_t dev = devinfo->dev;
606230312Smav	uint8_t *sad;
607230312Smav	int len, mnl, i, sadc, fmt;
608230312Smav
609230312Smav	if (w->eld == NULL || w->eld_len < 4)
610230312Smav		return;
611230312Smav	device_printf(dev,
612230312Smav	    "ELD nid=%d: ELD_Ver=%u Baseline_ELD_Len=%u\n",
613230312Smav	    w->nid, w->eld[0] >> 3, w->eld[2]);
614230312Smav	if ((w->eld[0] >> 3) != 0x02)
615230312Smav		return;
616230312Smav	len = min(w->eld_len, (u_int)w->eld[2] * 4);
617230312Smav	mnl = w->eld[4] & 0x1f;
618230312Smav	device_printf(dev,
619230312Smav	    "ELD nid=%d: CEA_EDID_Ver=%u MNL=%u\n",
620230312Smav	    w->nid, w->eld[4] >> 5, mnl);
621230312Smav	sadc = w->eld[5] >> 4;
622230312Smav	device_printf(dev,
623230312Smav	    "ELD nid=%d: SAD_Count=%u Conn_Type=%u S_AI=%u HDCP=%u\n",
624230312Smav	    w->nid, sadc, (w->eld[5] >> 2) & 0x3,
625230312Smav	    (w->eld[5] >> 1) & 0x1, w->eld[5] & 0x1);
626230312Smav	device_printf(dev,
627230312Smav	    "ELD nid=%d: Aud_Synch_Delay=%ums\n",
628230312Smav	    w->nid, w->eld[6] * 2);
629230312Smav	device_printf(dev,
630230312Smav	    "ELD nid=%d: Channels=0x%b\n",
631230312Smav	    w->nid, w->eld[7],
632230312Smav	    "\020\07RLRC\06FLRC\05RC\04RLR\03FC\02LFE\01FLR");
633230312Smav	device_printf(dev,
634230312Smav	    "ELD nid=%d: Port_ID=0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
635230312Smav	    w->nid, w->eld[8], w->eld[9], w->eld[10], w->eld[11],
636230312Smav	    w->eld[12], w->eld[13], w->eld[14], w->eld[15]);
637230312Smav	device_printf(dev,
638230312Smav	    "ELD nid=%d: Manufacturer_Name=0x%02x%02x\n",
639230312Smav	    w->nid, w->eld[16], w->eld[17]);
640230312Smav	device_printf(dev,
641230312Smav	    "ELD nid=%d: Product_Code=0x%02x%02x\n",
642230312Smav	    w->nid, w->eld[18], w->eld[19]);
643230312Smav	device_printf(dev,
644230312Smav	    "ELD nid=%d: Monitor_Name_String='%.*s'\n",
645230312Smav	    w->nid, mnl, &w->eld[20]);
646230312Smav	for (i = 0; i < sadc; i++) {
647230312Smav		sad = &w->eld[20 + mnl + i * 3];
648230312Smav		fmt = (sad[0] >> 3) & 0x0f;
649230312Smav		if (fmt == HDA_HDMI_CODING_TYPE_REF_CTX) {
650230312Smav			fmt = (sad[2] >> 3) & 0x1f;
651230312Smav			if (fmt < 1 || fmt > 3)
652230312Smav				fmt = 0;
653230312Smav			else
654230312Smav				fmt += 14;
655230312Smav		}
656230312Smav		device_printf(dev,
657230312Smav		    "ELD nid=%d: %s %dch freqs=0x%b",
658230312Smav		    w->nid, HDA_HDMI_CODING_TYPES[fmt], (sad[0] & 0x07) + 1,
659230312Smav		    sad[1], "\020\007192\006176\00596\00488\00348\00244\00132");
660230312Smav		switch (fmt) {
661230312Smav		case HDA_HDMI_CODING_TYPE_LPCM:
662230312Smav			printf(" sizes=0x%b",
663230312Smav			    sad[2] & 0x07, "\020\00324\00220\00116");
664230312Smav			break;
665230312Smav		case HDA_HDMI_CODING_TYPE_AC3:
666230312Smav		case HDA_HDMI_CODING_TYPE_MPEG1:
667230312Smav		case HDA_HDMI_CODING_TYPE_MP3:
668230312Smav		case HDA_HDMI_CODING_TYPE_MPEG2:
669230312Smav		case HDA_HDMI_CODING_TYPE_AACLC:
670230312Smav		case HDA_HDMI_CODING_TYPE_DTS:
671230312Smav		case HDA_HDMI_CODING_TYPE_ATRAC:
672230312Smav			printf(" max_bitrate=%d", sad[2] * 8000);
673230312Smav			break;
674230312Smav		case HDA_HDMI_CODING_TYPE_WMAPRO:
675230312Smav			printf(" profile=%d", sad[2] & 0x07);
676230312Smav			break;
677230312Smav		}
678230312Smav		printf("\n");
679230312Smav	}
680230312Smav}
681230312Smav
682230312Smavstatic void
683230312Smavhdaa_eld_handler(struct hdaa_widget *w)
684230312Smav{
685230312Smav	struct hdaa_devinfo *devinfo = w->devinfo;
686230312Smav	uint32_t res;
687230312Smav	int i;
688230312Smav
689230312Smav	if (w->enable == 0 || w->type !=
690230312Smav	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
691230312Smav		return;
692230312Smav
693230551Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
694230551Smav	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
695230551Smav		return;
696230551Smav
697230551Smav	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
698230551Smav	if ((w->eld != 0) == ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) != 0))
699230551Smav		return;
700230312Smav	if (w->eld != NULL) {
701230312Smav		w->eld_len = 0;
702230312Smav		free(w->eld, M_HDAA);
703230312Smav		w->eld = NULL;
704230312Smav	}
705230312Smav	HDA_BOOTVERBOSE(
706230312Smav		device_printf(devinfo->dev,
707246983Suqs		    "Pin sense: nid=%d sense=0x%08x "
708230312Smav		    "(%sconnected, ELD %svalid)\n",
709230312Smav		    w->nid, res,
710230312Smav		    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ? "" : "dis",
711230312Smav		    (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) ? "" : "in");
712230312Smav	);
713230312Smav	if ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) == 0)
714230312Smav		return;
715230312Smav
716230312Smav	res = hda_command(devinfo->dev,
717230312Smav	    HDA_CMD_GET_HDMI_DIP_SIZE(0, w->nid, 0x08));
718230312Smav	if (res == HDA_INVALID)
719230312Smav		return;
720230312Smav	w->eld_len = res & 0xff;
721230312Smav	if (w->eld_len != 0)
722230312Smav		w->eld = malloc(w->eld_len, M_HDAA, M_ZERO | M_NOWAIT);
723230312Smav	if (w->eld == NULL) {
724230312Smav		w->eld_len = 0;
725230312Smav		return;
726230312Smav	}
727230312Smav
728230312Smav	for (i = 0; i < w->eld_len; i++) {
729230312Smav		res = hda_command(devinfo->dev,
730230312Smav		    HDA_CMD_GET_HDMI_ELDD(0, w->nid, i));
731230312Smav		if (res & 0x80000000)
732230312Smav			w->eld[i] = res & 0xff;
733230312Smav	}
734230312Smav	HDA_BOOTVERBOSE(
735230312Smav		hdaa_eld_dump(w);
736230312Smav	);
737243181Smav	hdaa_channels_handler(&devinfo->as[w->bindas]);
738230312Smav}
739230312Smav
740230551Smav/*
741230551Smav * Pin sense initializer.
742230551Smav */
743230312Smavstatic void
744230551Smavhdaa_sense_init(struct hdaa_devinfo *devinfo)
745230312Smav{
746230551Smav	struct hdaa_audio_as *as;
747230551Smav	struct hdaa_widget *w;
748230551Smav	int i, poll = 0;
749230312Smav
750230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
751230312Smav		w = hdaa_widget_get(devinfo, i);
752230551Smav		if (w == NULL || w->enable == 0 || w->type !=
753230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
754230312Smav			continue;
755233692Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap)) {
756233692Smav			if (w->unsol < 0)
757233692Smav				w->unsol = HDAC_UNSOL_ALLOC(
758233692Smav				    device_get_parent(devinfo->dev),
759233692Smav				    devinfo->dev, w->nid);
760230312Smav			hda_command(devinfo->dev,
761230312Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid,
762230551Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE | w->unsol));
763230312Smav		}
764230551Smav		as = &devinfo->as[w->bindas];
765230551Smav		if (as->hpredir >= 0 && as->pins[15] == w->nid) {
766230551Smav			if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
767230551Smav			    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0) {
768230551Smav				device_printf(devinfo->dev,
769230551Smav				    "No presence detection support at nid %d\n",
770240884Smav				    w->nid);
771230551Smav			} else {
772230551Smav				if (w->unsol < 0)
773230551Smav					poll = 1;
774230551Smav				HDA_BOOTVERBOSE(
775230551Smav					device_printf(devinfo->dev,
776230551Smav					    "Headphones redirection for "
777230551Smav					    "association %d nid=%d using %s.\n",
778230551Smav					    w->bindas, w->nid,
779240884Smav					    (w->unsol < 0) ? "polling" :
780230551Smav					    "unsolicited responses");
781230551Smav				);
782297793Spfg			}
783230551Smav		}
784230551Smav		hdaa_presence_handler(w);
785230551Smav		if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) &&
786230551Smav		    !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
787230551Smav			continue;
788230312Smav		hdaa_eld_handler(w);
789230312Smav	}
790230551Smav	if (poll) {
791230551Smav		callout_reset(&devinfo->poll_jack, 1,
792230551Smav		    hdaa_jack_poll_callback, devinfo);
793230551Smav	}
794230312Smav}
795230312Smav
796230312Smavstatic void
797230551Smavhdaa_sense_deinit(struct hdaa_devinfo *devinfo)
798230312Smav{
799230312Smav	struct hdaa_widget *w;
800230312Smav	int i;
801230312Smav
802230551Smav	callout_stop(&devinfo->poll_jack);
803230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
804230312Smav		w = hdaa_widget_get(devinfo, i);
805230551Smav		if (w == NULL || w->enable == 0 || w->type !=
806230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
807230312Smav			continue;
808230312Smav		if (w->unsol < 0)
809230312Smav			continue;
810230312Smav		hda_command(devinfo->dev,
811230312Smav		    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0));
812230312Smav		HDAC_UNSOL_FREE(
813230312Smav		    device_get_parent(devinfo->dev), devinfo->dev,
814230312Smav		    w->unsol);
815230312Smav		w->unsol = -1;
816230312Smav	}
817230312Smav}
818230312Smav
819230130Smavuint32_t
820230130Smavhdaa_widget_pin_patch(uint32_t config, const char *str)
821230130Smav{
822230130Smav	char buf[256];
823230130Smav	char *key, *value, *rest, *bad;
824230130Smav	int ival, i;
825230130Smav
826230130Smav	strlcpy(buf, str, sizeof(buf));
827230130Smav	rest = buf;
828230130Smav	while ((key = strsep(&rest, "=")) != NULL) {
829230130Smav		value = strsep(&rest, " \t");
830230130Smav		if (value == NULL)
831230130Smav			break;
832230130Smav		ival = strtol(value, &bad, 10);
833230130Smav		if (strcmp(key, "seq") == 0) {
834230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK;
835230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_SEQUENCE_SHIFT) &
836230130Smav			    HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK);
837230130Smav		} else if (strcmp(key, "as") == 0) {
838230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK;
839230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT) &
840230130Smav			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK);
841230130Smav		} else if (strcmp(key, "misc") == 0) {
842230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_MISC_MASK;
843230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_MISC_SHIFT) &
844230130Smav			    HDA_CONFIG_DEFAULTCONF_MISC_MASK);
845230130Smav		} else if (strcmp(key, "color") == 0) {
846230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_COLOR_MASK;
847230130Smav			if (bad[0] == 0) {
848230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT) &
849230130Smav				    HDA_CONFIG_DEFAULTCONF_COLOR_MASK);
850297793Spfg			}
851230130Smav			for (i = 0; i < 16; i++) {
852230130Smav				if (strcasecmp(HDA_COLORS[i], value) == 0) {
853230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT);
854230130Smav					break;
855230130Smav				}
856230130Smav			}
857230130Smav		} else if (strcmp(key, "ctype") == 0) {
858230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK;
859230130Smav			if (bad[0] == 0) {
860230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT) &
861230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK);
862230130Smav			}
863230130Smav			for (i = 0; i < 16; i++) {
864230130Smav				if (strcasecmp(HDA_CONNECTORS[i], value) == 0) {
865230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT);
866230130Smav					break;
867230130Smav				}
868230130Smav			}
869230130Smav		} else if (strcmp(key, "device") == 0) {
870230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
871230130Smav			if (bad[0] == 0) {
872230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT) &
873230130Smav				    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK);
874230130Smav				continue;
875297793Spfg			}
876230130Smav			for (i = 0; i < 16; i++) {
877230130Smav				if (strcasecmp(HDA_DEVS[i], value) == 0) {
878230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT);
879230130Smav					break;
880230130Smav				}
881230130Smav			}
882230130Smav		} else if (strcmp(key, "loc") == 0) {
883230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_LOCATION_MASK;
884230130Smav			if (bad[0] == 0) {
885230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT) &
886230130Smav				    HDA_CONFIG_DEFAULTCONF_LOCATION_MASK);
887230130Smav				continue;
888230130Smav			}
889230130Smav			for (i = 0; i < 64; i++) {
890230130Smav				if (strcasecmp(HDA_LOCS[i], value) == 0) {
891230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT);
892230130Smav					break;
893230130Smav				}
894230130Smav			}
895230130Smav		} else if (strcmp(key, "conn") == 0) {
896230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
897230130Smav			if (bad[0] == 0) {
898230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT) &
899230130Smav				    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
900230130Smav				continue;
901297793Spfg			}
902230130Smav			for (i = 0; i < 4; i++) {
903230130Smav				if (strcasecmp(HDA_CONNS[i], value) == 0) {
904230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT);
905230130Smav					break;
906230130Smav				}
907230130Smav			}
908230130Smav		}
909230130Smav	}
910230130Smav	return (config);
911230130Smav}
912230130Smav
913230130Smavuint32_t
914230130Smavhdaa_gpio_patch(uint32_t gpio, const char *str)
915230130Smav{
916230130Smav	char buf[256];
917230130Smav	char *key, *value, *rest;
918230130Smav	int ikey, i;
919230130Smav
920230130Smav	strlcpy(buf, str, sizeof(buf));
921230130Smav	rest = buf;
922230130Smav	while ((key = strsep(&rest, "=")) != NULL) {
923230130Smav		value = strsep(&rest, " \t");
924230130Smav		if (value == NULL)
925230130Smav			break;
926230130Smav		ikey = strtol(key, NULL, 10);
927230130Smav		if (ikey < 0 || ikey > 7)
928230130Smav			continue;
929230130Smav		for (i = 0; i < 7; i++) {
930230130Smav			if (strcasecmp(HDA_GPIO_ACTIONS[i], value) == 0) {
931230130Smav				gpio &= ~HDAA_GPIO_MASK(ikey);
932230130Smav				gpio |= i << HDAA_GPIO_SHIFT(ikey);
933230130Smav				break;
934230130Smav			}
935230130Smav		}
936230130Smav	}
937230130Smav	return (gpio);
938230130Smav}
939230130Smav
940230130Smavstatic void
941230130Smavhdaa_local_patch_pin(struct hdaa_widget *w)
942230130Smav{
943230130Smav	device_t dev = w->devinfo->dev;
944230130Smav	const char *res = NULL;
945230130Smav	uint32_t config, orig;
946230130Smav	char buf[32];
947230130Smav
948230130Smav	config = orig = w->wclass.pin.config;
949230130Smav	snprintf(buf, sizeof(buf), "cad%u.nid%u.config",
950230130Smav	    hda_get_codec_id(dev), w->nid);
951230130Smav	if (resource_string_value(device_get_name(
952230130Smav	    device_get_parent(device_get_parent(dev))),
953230130Smav	    device_get_unit(device_get_parent(device_get_parent(dev))),
954230130Smav	    buf, &res) == 0) {
955230130Smav		if (strncmp(res, "0x", 2) == 0) {
956230130Smav			config = strtol(res + 2, NULL, 16);
957230130Smav		} else {
958230130Smav			config = hdaa_widget_pin_patch(config, res);
959230130Smav		}
960230130Smav	}
961230130Smav	snprintf(buf, sizeof(buf), "nid%u.config", w->nid);
962230130Smav	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
963230130Smav	    buf, &res) == 0) {
964230130Smav		if (strncmp(res, "0x", 2) == 0) {
965230130Smav			config = strtol(res + 2, NULL, 16);
966230130Smav		} else {
967230130Smav			config = hdaa_widget_pin_patch(config, res);
968230130Smav		}
969230130Smav	}
970230130Smav	HDA_BOOTVERBOSE(
971230130Smav		if (config != orig)
972230130Smav			device_printf(w->devinfo->dev,
973230130Smav			    "Patching pin config nid=%u 0x%08x -> 0x%08x\n",
974230130Smav			    w->nid, orig, config);
975230130Smav	);
976230130Smav	w->wclass.pin.newconf = w->wclass.pin.config = config;
977230130Smav}
978230130Smav
979243530Smavstatic void
980243530Smavhdaa_dump_audio_formats_sb(struct sbuf *sb, uint32_t fcap, uint32_t pcmcap)
981243530Smav{
982243530Smav	uint32_t cap;
983243530Smav
984243530Smav	cap = fcap;
985243530Smav	if (cap != 0) {
986243530Smav		sbuf_printf(sb, "     Stream cap: 0x%08x", cap);
987243530Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
988243530Smav			sbuf_printf(sb, " AC3");
989243530Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
990243530Smav			sbuf_printf(sb, " FLOAT32");
991243530Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
992243530Smav			sbuf_printf(sb, " PCM");
993243530Smav		sbuf_printf(sb, "\n");
994243530Smav	}
995243530Smav	cap = pcmcap;
996243530Smav	if (cap != 0) {
997243530Smav		sbuf_printf(sb, "        PCM cap: 0x%08x", cap);
998243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
999243530Smav			sbuf_printf(sb, " 8");
1000243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
1001243530Smav			sbuf_printf(sb, " 16");
1002243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
1003243530Smav			sbuf_printf(sb, " 20");
1004243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
1005243530Smav			sbuf_printf(sb, " 24");
1006243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
1007243530Smav			sbuf_printf(sb, " 32");
1008243530Smav		sbuf_printf(sb, " bits,");
1009243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
1010243530Smav			sbuf_printf(sb, " 8");
1011243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
1012243530Smav			sbuf_printf(sb, " 11");
1013243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
1014243530Smav			sbuf_printf(sb, " 16");
1015243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
1016243530Smav			sbuf_printf(sb, " 22");
1017243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
1018243530Smav			sbuf_printf(sb, " 32");
1019243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
1020243530Smav			sbuf_printf(sb, " 44");
1021243530Smav		sbuf_printf(sb, " 48");
1022243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
1023243530Smav			sbuf_printf(sb, " 88");
1024243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
1025243530Smav			sbuf_printf(sb, " 96");
1026243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
1027243530Smav			sbuf_printf(sb, " 176");
1028243530Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
1029243530Smav			sbuf_printf(sb, " 192");
1030243530Smav		sbuf_printf(sb, " KHz\n");
1031243530Smav	}
1032243530Smav}
1033243530Smav
1034243530Smavstatic void
1035243530Smavhdaa_dump_pin_sb(struct sbuf *sb, struct hdaa_widget *w)
1036243530Smav{
1037243530Smav	uint32_t pincap, conf;
1038243530Smav
1039243530Smav	pincap = w->wclass.pin.cap;
1040243530Smav
1041243530Smav	sbuf_printf(sb, "        Pin cap: 0x%08x", pincap);
1042243530Smav	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
1043243530Smav		sbuf_printf(sb, " ISC");
1044243530Smav	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
1045243530Smav		sbuf_printf(sb, " TRQD");
1046243530Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
1047243530Smav		sbuf_printf(sb, " PDC");
1048243530Smav	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
1049243530Smav		sbuf_printf(sb, " HP");
1050243530Smav	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
1051243530Smav		sbuf_printf(sb, " OUT");
1052243530Smav	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
1053243530Smav		sbuf_printf(sb, " IN");
1054243530Smav	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
1055243530Smav		sbuf_printf(sb, " BAL");
1056243530Smav	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
1057243530Smav		sbuf_printf(sb, " HDMI");
1058243530Smav	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
1059243530Smav		sbuf_printf(sb, " VREF[");
1060243530Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
1061243530Smav			sbuf_printf(sb, " 50");
1062243530Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
1063243530Smav			sbuf_printf(sb, " 80");
1064243530Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
1065243530Smav			sbuf_printf(sb, " 100");
1066243530Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
1067243530Smav			sbuf_printf(sb, " GROUND");
1068243530Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
1069243530Smav			sbuf_printf(sb, " HIZ");
1070243530Smav		sbuf_printf(sb, " ]");
1071243530Smav	}
1072243530Smav	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
1073243530Smav		sbuf_printf(sb, " EAPD");
1074243530Smav	if (HDA_PARAM_PIN_CAP_DP(pincap))
1075243530Smav		sbuf_printf(sb, " DP");
1076243530Smav	if (HDA_PARAM_PIN_CAP_HBR(pincap))
1077243530Smav		sbuf_printf(sb, " HBR");
1078243530Smav	sbuf_printf(sb, "\n");
1079243530Smav	conf = w->wclass.pin.config;
1080243530Smav	sbuf_printf(sb, "     Pin config: 0x%08x", conf);
1081243530Smav	sbuf_printf(sb, " as=%d seq=%d "
1082243530Smav	    "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d\n",
1083243530Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
1084243530Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
1085243530Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
1086243530Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
1087243530Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
1088243530Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
1089243530Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
1090243530Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf));
1091243530Smav	sbuf_printf(sb, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
1092243530Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
1093243530Smav		sbuf_printf(sb, " HP");
1094243530Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
1095243530Smav		sbuf_printf(sb, " IN");
1096243530Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
1097243530Smav		sbuf_printf(sb, " OUT");
1098243530Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1099243530Smav		if ((w->wclass.pin.ctrl &
1100243530Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
1101243530Smav			sbuf_printf(sb, " HBR");
1102243530Smav		else if ((w->wclass.pin.ctrl &
1103243530Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
1104243530Smav			sbuf_printf(sb, " EPTs");
1105243530Smav	} else {
1106243530Smav		if ((w->wclass.pin.ctrl &
1107243530Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
1108243530Smav			sbuf_printf(sb, " VREFs");
1109243530Smav	}
1110243530Smav	sbuf_printf(sb, "\n");
1111243530Smav}
1112243530Smav
1113243530Smavstatic void
1114264832Smariushdaa_dump_amp_sb(struct sbuf *sb, uint32_t cap, const char *banner)
1115243530Smav{
1116243530Smav	int offset, size, step;
1117243530Smav
1118243530Smav	offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap);
1119243530Smav	size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap);
1120243530Smav	step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap);
1121243530Smav	sbuf_printf(sb, "     %s amp: 0x%08x "
1122243530Smav	    "mute=%d step=%d size=%d offset=%d (%+d/%+ddB)\n",
1123243530Smav	    banner, cap,
1124243530Smav	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
1125243530Smav	    step, size, offset,
1126243530Smav	    ((0 - offset) * (size + 1)) / 4,
1127243530Smav	    ((step - offset) * (size + 1)) / 4);
1128243530Smav}
1129243530Smav
1130243530Smav
1131230130Smavstatic int
1132243530Smavhdaa_sysctl_caps(SYSCTL_HANDLER_ARGS)
1133243530Smav{
1134243530Smav	struct hdaa_devinfo *devinfo;
1135243530Smav	struct hdaa_widget *w, *cw;
1136243530Smav	struct sbuf sb;
1137243530Smav	char buf[64];
1138243530Smav	int error, j;
1139243530Smav
1140243530Smav	w = (struct hdaa_widget *)oidp->oid_arg1;
1141243530Smav	devinfo = w->devinfo;
1142243530Smav	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1143243530Smav
1144243530Smav	sbuf_printf(&sb, "%s%s\n", w->name,
1145243530Smav	    (w->enable == 0) ? " [DISABLED]" : "");
1146243530Smav	sbuf_printf(&sb, "     Widget cap: 0x%08x",
1147243530Smav	    w->param.widget_cap);
1148243530Smav	if (w->param.widget_cap & 0x0ee1) {
1149243530Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
1150243530Smav		    sbuf_printf(&sb, " LRSWAP");
1151243530Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
1152243530Smav		    sbuf_printf(&sb, " PWR");
1153243530Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
1154243530Smav		    sbuf_printf(&sb, " DIGITAL");
1155243530Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
1156243530Smav		    sbuf_printf(&sb, " UNSOL");
1157243530Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
1158243530Smav		    sbuf_printf(&sb, " PROC");
1159243530Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
1160243530Smav		    sbuf_printf(&sb, " STRIPE(x%d)",
1161243530Smav			1 << (fls(w->wclass.conv.stripecap) - 1));
1162243530Smav		j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1163243530Smav		if (j == 1)
1164243530Smav		    sbuf_printf(&sb, " STEREO");
1165243530Smav		else if (j > 1)
1166243530Smav		    sbuf_printf(&sb, " %dCH", j + 1);
1167243530Smav	}
1168243530Smav	sbuf_printf(&sb, "\n");
1169243530Smav	if (w->bindas != -1) {
1170243530Smav		sbuf_printf(&sb, "    Association: %d (0x%04x)\n",
1171243530Smav		    w->bindas, w->bindseqmask);
1172243530Smav	}
1173243530Smav	if (w->ossmask != 0 || w->ossdev >= 0) {
1174243530Smav		sbuf_printf(&sb, "            OSS: %s",
1175243530Smav		    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
1176243530Smav		if (w->ossdev >= 0)
1177243530Smav		    sbuf_printf(&sb, " (%s)", ossnames[w->ossdev]);
1178243530Smav		sbuf_printf(&sb, "\n");
1179243530Smav	}
1180243530Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1181243530Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1182243530Smav		hdaa_dump_audio_formats_sb(&sb,
1183243530Smav		    w->param.supp_stream_formats,
1184243530Smav		    w->param.supp_pcm_size_rate);
1185243530Smav	} else if (w->type ==
1186243530Smav	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
1187243530Smav		hdaa_dump_pin_sb(&sb, w);
1188243530Smav	if (w->param.eapdbtl != HDA_INVALID) {
1189243530Smav		sbuf_printf(&sb, "           EAPD: 0x%08x%s%s%s\n",
1190243530Smav		    w->param.eapdbtl,
1191243530Smav		    (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_LR_SWAP) ?
1192243530Smav		     " LRSWAP" : "",
1193243530Smav		    (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD) ?
1194243530Smav		     " EAPD" : "",
1195243530Smav		    (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_BTL) ?
1196243530Smav		     " BTL" : "");
1197243530Smav	}
1198243530Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
1199243530Smav	    w->param.outamp_cap != 0)
1200243530Smav		hdaa_dump_amp_sb(&sb, w->param.outamp_cap, "Output");
1201243530Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
1202243530Smav	    w->param.inamp_cap != 0)
1203243530Smav		hdaa_dump_amp_sb(&sb, w->param.inamp_cap, " Input");
1204243530Smav	if (w->nconns > 0)
1205243530Smav		sbuf_printf(&sb, "    Connections: %d\n", w->nconns);
1206243530Smav	for (j = 0; j < w->nconns; j++) {
1207243530Smav		cw = hdaa_widget_get(devinfo, w->conns[j]);
1208243530Smav		sbuf_printf(&sb, "          + %s<- nid=%d [%s]",
1209243530Smav		    (w->connsenable[j] == 0)?"[DISABLED] ":"",
1210243530Smav		    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
1211243530Smav		if (cw == NULL)
1212243530Smav			sbuf_printf(&sb, " [UNKNOWN]");
1213243530Smav		else if (cw->enable == 0)
1214243530Smav			sbuf_printf(&sb, " [DISABLED]");
1215243530Smav		if (w->nconns > 1 && w->selconn == j && w->type !=
1216243530Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
1217243530Smav			sbuf_printf(&sb, " (selected)");
1218243530Smav		sbuf_printf(&sb, "\n");
1219243530Smav	}
1220243530Smav	error = sbuf_finish(&sb);
1221243530Smav	sbuf_delete(&sb);
1222243530Smav	return (error);
1223243530Smav}
1224243530Smav
1225243530Smavstatic int
1226230130Smavhdaa_sysctl_config(SYSCTL_HANDLER_ARGS)
1227230130Smav{
1228230130Smav	char buf[256];
1229230130Smav	int error;
1230230130Smav	uint32_t conf;
1231230130Smav
1232230130Smav	conf = *(uint32_t *)oidp->oid_arg1;
1233230130Smav	snprintf(buf, sizeof(buf), "0x%08x as=%d seq=%d "
1234230130Smav	    "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d",
1235230130Smav	    conf,
1236230130Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
1237230130Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
1238230130Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
1239230130Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
1240230130Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
1241230130Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
1242230130Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
1243230130Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf));
1244230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1245230130Smav	if (error != 0 || req->newptr == NULL)
1246230130Smav		return (error);
1247230130Smav	if (strncmp(buf, "0x", 2) == 0)
1248230130Smav		conf = strtol(buf + 2, NULL, 16);
1249230130Smav	else
1250230130Smav		conf = hdaa_widget_pin_patch(conf, buf);
1251230130Smav	*(uint32_t *)oidp->oid_arg1 = conf;
1252230130Smav	return (0);
1253230130Smav}
1254230130Smav
1255230130Smavstatic void
1256230130Smavhdaa_config_fetch(const char *str, uint32_t *on, uint32_t *off)
1257230130Smav{
1258230130Smav	int i = 0, j, k, len, inv;
1259230130Smav
1260230130Smav	for (;;) {
1261230130Smav		while (str[i] != '\0' &&
1262230130Smav		    (str[i] == ',' || isspace(str[i]) != 0))
1263230130Smav			i++;
1264230130Smav		if (str[i] == '\0')
1265230130Smav			return;
1266230130Smav		j = i;
1267230130Smav		while (str[j] != '\0' &&
1268230130Smav		    !(str[j] == ',' || isspace(str[j]) != 0))
1269230130Smav			j++;
1270230130Smav		len = j - i;
1271230130Smav		if (len > 2 && strncmp(str + i, "no", 2) == 0)
1272230130Smav			inv = 2;
1273230130Smav		else
1274230130Smav			inv = 0;
1275264832Smarius		for (k = 0; len > inv && k < nitems(hdaa_quirks_tab); k++) {
1276230130Smav			if (strncmp(str + i + inv,
1277230130Smav			    hdaa_quirks_tab[k].key, len - inv) != 0)
1278230130Smav				continue;
1279230130Smav			if (len - inv != strlen(hdaa_quirks_tab[k].key))
1280230130Smav				continue;
1281230130Smav			if (inv == 0) {
1282230130Smav				*on |= hdaa_quirks_tab[k].value;
1283230130Smav				*off &= ~hdaa_quirks_tab[k].value;
1284230130Smav			} else {
1285230130Smav				*off |= hdaa_quirks_tab[k].value;
1286230130Smav				*on &= ~hdaa_quirks_tab[k].value;
1287230130Smav			}
1288230130Smav			break;
1289230130Smav		}
1290230130Smav		i = j;
1291230130Smav	}
1292230130Smav}
1293230130Smav
1294230130Smavstatic int
1295230130Smavhdaa_sysctl_quirks(SYSCTL_HANDLER_ARGS)
1296230130Smav{
1297230130Smav	char buf[256];
1298230130Smav	int error, n = 0, i;
1299230130Smav	uint32_t quirks, quirks_off;
1300230130Smav
1301230130Smav	quirks = *(uint32_t *)oidp->oid_arg1;
1302230130Smav	buf[0] = 0;
1303264832Smarius	for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
1304230130Smav		if ((quirks & hdaa_quirks_tab[i].value) != 0)
1305230130Smav			n += snprintf(buf + n, sizeof(buf) - n, "%s%s",
1306230130Smav			    n != 0 ? "," : "", hdaa_quirks_tab[i].key);
1307230130Smav	}
1308230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1309230130Smav	if (error != 0 || req->newptr == NULL)
1310230130Smav		return (error);
1311230130Smav	if (strncmp(buf, "0x", 2) == 0)
1312230130Smav		quirks = strtol(buf + 2, NULL, 16);
1313230130Smav	else {
1314230130Smav		quirks = 0;
1315230130Smav		hdaa_config_fetch(buf, &quirks, &quirks_off);
1316230130Smav	}
1317230130Smav	*(uint32_t *)oidp->oid_arg1 = quirks;
1318230130Smav	return (0);
1319230130Smav}
1320230130Smav
1321230130Smavstatic void
1322230130Smavhdaa_local_patch(struct hdaa_devinfo *devinfo)
1323230130Smav{
1324230130Smav	struct hdaa_widget *w;
1325230130Smav	const char *res = NULL;
1326230130Smav	uint32_t quirks_on = 0, quirks_off = 0, x;
1327230130Smav	int i;
1328230130Smav
1329230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1330230130Smav		w = hdaa_widget_get(devinfo, i);
1331230130Smav		if (w == NULL)
1332230130Smav			continue;
1333230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
1334230130Smav			hdaa_local_patch_pin(w);
1335230130Smav	}
1336230130Smav
1337230130Smav	if (resource_string_value(device_get_name(devinfo->dev),
1338230130Smav	    device_get_unit(devinfo->dev), "config", &res) == 0) {
1339230130Smav		if (res != NULL && strlen(res) > 0)
1340230130Smav			hdaa_config_fetch(res, &quirks_on, &quirks_off);
1341230130Smav		devinfo->quirks |= quirks_on;
1342230130Smav		devinfo->quirks &= ~quirks_off;
1343230130Smav	}
1344230130Smav	if (devinfo->newquirks == -1)
1345230130Smav		devinfo->newquirks = devinfo->quirks;
1346230130Smav	else
1347230130Smav		devinfo->quirks = devinfo->newquirks;
1348230130Smav	HDA_BOOTHVERBOSE(
1349230130Smav		device_printf(devinfo->dev,
1350230130Smav		    "Config options: 0x%08x\n", devinfo->quirks);
1351230130Smav	);
1352230130Smav
1353230130Smav	if (resource_string_value(device_get_name(devinfo->dev),
1354230130Smav	    device_get_unit(devinfo->dev), "gpio_config", &res) == 0) {
1355230130Smav		if (strncmp(res, "0x", 2) == 0) {
1356230130Smav			devinfo->gpio = strtol(res + 2, NULL, 16);
1357230130Smav		} else {
1358230130Smav			devinfo->gpio = hdaa_gpio_patch(devinfo->gpio, res);
1359230130Smav		}
1360230130Smav	}
1361230130Smav	if (devinfo->newgpio == -1)
1362230130Smav		devinfo->newgpio = devinfo->gpio;
1363230130Smav	else
1364230130Smav		devinfo->gpio = devinfo->newgpio;
1365230130Smav	if (devinfo->newgpo == -1)
1366230130Smav		devinfo->newgpo = devinfo->gpo;
1367230130Smav	else
1368230130Smav		devinfo->gpo = devinfo->newgpo;
1369230130Smav	HDA_BOOTHVERBOSE(
1370230130Smav		device_printf(devinfo->dev, "GPIO config options:");
1371230130Smav		for (i = 0; i < 7; i++) {
1372230130Smav			x = (devinfo->gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
1373230130Smav			if (x != 0)
1374230130Smav				printf(" %d=%s", i, HDA_GPIO_ACTIONS[x]);
1375230130Smav		}
1376230130Smav		printf("\n");
1377230130Smav	);
1378230130Smav}
1379230130Smav
1380230130Smavstatic void
1381230130Smavhdaa_widget_connection_parse(struct hdaa_widget *w)
1382230130Smav{
1383230130Smav	uint32_t res;
1384230130Smav	int i, j, max, ents, entnum;
1385230130Smav	nid_t nid = w->nid;
1386230130Smav	nid_t cnid, addcnid, prevcnid;
1387230130Smav
1388230130Smav	w->nconns = 0;
1389230130Smav
1390230130Smav	res = hda_command(w->devinfo->dev,
1391230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_CONN_LIST_LENGTH));
1392230130Smav
1393230130Smav	ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res);
1394230130Smav
1395230130Smav	if (ents < 1)
1396230130Smav		return;
1397230130Smav
1398230130Smav	entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4;
1399230130Smav	max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1;
1400230130Smav	prevcnid = 0;
1401230130Smav
1402230130Smav#define CONN_RMASK(e)		(1 << ((32 / (e)) - 1))
1403230130Smav#define CONN_NMASK(e)		(CONN_RMASK(e) - 1)
1404230130Smav#define CONN_RESVAL(r, e, n)	((r) >> ((32 / (e)) * (n)))
1405230130Smav#define CONN_RANGE(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_RMASK(e))
1406230130Smav#define CONN_CNID(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_NMASK(e))
1407230130Smav
1408230130Smav	for (i = 0; i < ents; i += entnum) {
1409230130Smav		res = hda_command(w->devinfo->dev,
1410230130Smav		    HDA_CMD_GET_CONN_LIST_ENTRY(0, nid, i));
1411230130Smav		for (j = 0; j < entnum; j++) {
1412230130Smav			cnid = CONN_CNID(res, entnum, j);
1413230130Smav			if (cnid == 0) {
1414230130Smav				if (w->nconns < ents)
1415230130Smav					device_printf(w->devinfo->dev,
1416230130Smav					    "WARNING: nid=%d has zero cnid "
1417230130Smav					    "entnum=%d j=%d index=%d "
1418230130Smav					    "entries=%d found=%d res=0x%08x\n",
1419230130Smav					    nid, entnum, j, i,
1420230130Smav					    ents, w->nconns, res);
1421230130Smav				else
1422230130Smav					goto getconns_out;
1423230130Smav			}
1424230130Smav			if (cnid < w->devinfo->startnode ||
1425230130Smav			    cnid >= w->devinfo->endnode) {
1426230130Smav				HDA_BOOTVERBOSE(
1427230130Smav					device_printf(w->devinfo->dev,
1428230130Smav					    "WARNING: nid=%d has cnid outside "
1429230130Smav					    "of the AFG range j=%d "
1430230130Smav					    "entnum=%d index=%d res=0x%08x\n",
1431230130Smav					    nid, j, entnum, i, res);
1432230130Smav				);
1433230130Smav			}
1434230130Smav			if (CONN_RANGE(res, entnum, j) == 0)
1435230130Smav				addcnid = cnid;
1436230130Smav			else if (prevcnid == 0 || prevcnid >= cnid) {
1437230130Smav				device_printf(w->devinfo->dev,
1438230130Smav				    "WARNING: Invalid child range "
1439230130Smav				    "nid=%d index=%d j=%d entnum=%d "
1440230130Smav				    "prevcnid=%d cnid=%d res=0x%08x\n",
1441230130Smav				    nid, i, j, entnum, prevcnid,
1442230130Smav				    cnid, res);
1443230130Smav				addcnid = cnid;
1444230130Smav			} else
1445230130Smav				addcnid = prevcnid + 1;
1446230130Smav			while (addcnid <= cnid) {
1447230130Smav				if (w->nconns > max) {
1448230130Smav					device_printf(w->devinfo->dev,
1449230130Smav					    "Adding %d (nid=%d): "
1450230130Smav					    "Max connection reached! max=%d\n",
1451230130Smav					    addcnid, nid, max + 1);
1452230130Smav					goto getconns_out;
1453230130Smav				}
1454230130Smav				w->connsenable[w->nconns] = 1;
1455230130Smav				w->conns[w->nconns++] = addcnid++;
1456230130Smav			}
1457230130Smav			prevcnid = cnid;
1458230130Smav		}
1459230130Smav	}
1460230130Smav
1461230130Smavgetconns_out:
1462230130Smav	return;
1463230130Smav}
1464230130Smav
1465230130Smavstatic void
1466230130Smavhdaa_widget_parse(struct hdaa_widget *w)
1467230130Smav{
1468230130Smav	device_t dev = w->devinfo->dev;
1469230130Smav	uint32_t wcap, cap;
1470230130Smav	nid_t nid = w->nid;
1471230130Smav	char buf[64];
1472230130Smav
1473230130Smav	w->param.widget_cap = wcap = hda_command(dev,
1474230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_AUDIO_WIDGET_CAP));
1475230130Smav	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap);
1476230130Smav
1477230130Smav	hdaa_widget_connection_parse(w);
1478230130Smav
1479230130Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) {
1480230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1481230130Smav			w->param.outamp_cap =
1482230130Smav			    hda_command(dev,
1483230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1484230130Smav			    HDA_PARAM_OUTPUT_AMP_CAP));
1485230130Smav		else
1486230130Smav			w->param.outamp_cap =
1487230130Smav			    w->devinfo->outamp_cap;
1488230130Smav	} else
1489230130Smav		w->param.outamp_cap = 0;
1490230130Smav
1491230130Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) {
1492230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1493230130Smav			w->param.inamp_cap =
1494230130Smav			    hda_command(dev,
1495230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1496230130Smav			    HDA_PARAM_INPUT_AMP_CAP));
1497230130Smav		else
1498230130Smav			w->param.inamp_cap =
1499230130Smav			    w->devinfo->inamp_cap;
1500230130Smav	} else
1501230130Smav		w->param.inamp_cap = 0;
1502230130Smav
1503230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1504230130Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1505230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) {
1506230130Smav			cap = hda_command(dev,
1507230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1508230130Smav			    HDA_PARAM_SUPP_STREAM_FORMATS));
1509230130Smav			w->param.supp_stream_formats = (cap != 0) ? cap :
1510230130Smav			    w->devinfo->supp_stream_formats;
1511230130Smav			cap = hda_command(dev,
1512230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1513230130Smav			    HDA_PARAM_SUPP_PCM_SIZE_RATE));
1514230130Smav			w->param.supp_pcm_size_rate = (cap != 0) ? cap :
1515230130Smav			    w->devinfo->supp_pcm_size_rate;
1516230130Smav		} else {
1517230130Smav			w->param.supp_stream_formats =
1518230130Smav			    w->devinfo->supp_stream_formats;
1519230130Smav			w->param.supp_pcm_size_rate =
1520230130Smav			    w->devinfo->supp_pcm_size_rate;
1521230130Smav		}
1522230326Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1523230326Smav			w->wclass.conv.stripecap = hda_command(dev,
1524230326Smav			    HDA_CMD_GET_STRIPE_CONTROL(0, w->nid)) >> 20;
1525230326Smav		} else
1526230326Smav			w->wclass.conv.stripecap = 1;
1527230130Smav	} else {
1528230130Smav		w->param.supp_stream_formats = 0;
1529230130Smav		w->param.supp_pcm_size_rate = 0;
1530230130Smav	}
1531230130Smav
1532230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1533230130Smav		w->wclass.pin.original = w->wclass.pin.newconf =
1534230130Smav		    w->wclass.pin.config = hda_command(dev,
1535230130Smav			HDA_CMD_GET_CONFIGURATION_DEFAULT(0, w->nid));
1536230130Smav		w->wclass.pin.cap = hda_command(dev,
1537241844Seadler		    HDA_CMD_GET_PARAMETER(0, w->nid, HDA_PARAM_PIN_CAP));
1538230130Smav		w->wclass.pin.ctrl = hda_command(dev,
1539230130Smav		    HDA_CMD_GET_PIN_WIDGET_CTRL(0, nid));
1540242357Smav		w->wclass.pin.connected = 2;
1541230130Smav		if (HDA_PARAM_PIN_CAP_EAPD_CAP(w->wclass.pin.cap)) {
1542230130Smav			w->param.eapdbtl = hda_command(dev,
1543230130Smav			    HDA_CMD_GET_EAPD_BTL_ENABLE(0, nid));
1544230130Smav			w->param.eapdbtl &= 0x7;
1545230130Smav			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
1546230130Smav		} else
1547230130Smav			w->param.eapdbtl = HDA_INVALID;
1548243530Smav	}
1549243530Smav	w->unsol = -1;
1550230130Smav
1551243530Smav	hdaa_unlock(w->devinfo);
1552243530Smav	snprintf(buf, sizeof(buf), "nid%d", w->nid);
1553243530Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1554243530Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1555243530Smav	    buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1556299504Scem	    w, 0, hdaa_sysctl_caps, "A", "Node capabilities");
1557243530Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1558230130Smav		snprintf(buf, sizeof(buf), "nid%d_config", w->nid);
1559230130Smav		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1560230130Smav		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1561230130Smav		    buf, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1562299504Scem		    &w->wclass.pin.newconf, 0, hdaa_sysctl_config, "A",
1563299504Scem		    "Current pin configuration");
1564230130Smav		snprintf(buf, sizeof(buf), "nid%d_original", w->nid);
1565230130Smav		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1566230130Smav		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1567230130Smav		    buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1568299504Scem		    &w->wclass.pin.original, 0, hdaa_sysctl_config, "A",
1569299504Scem		    "Original pin configuration");
1570230130Smav	}
1571243530Smav	hdaa_lock(w->devinfo);
1572230130Smav}
1573230130Smav
1574230130Smavstatic void
1575230130Smavhdaa_widget_postprocess(struct hdaa_widget *w)
1576230130Smav{
1577264832Smarius	const char *typestr;
1578230130Smav
1579230130Smav	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(w->param.widget_cap);
1580230130Smav	switch (w->type) {
1581230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
1582230130Smav		typestr = "audio output";
1583230130Smav		break;
1584230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
1585230130Smav		typestr = "audio input";
1586230130Smav		break;
1587230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
1588230130Smav		typestr = "audio mixer";
1589230130Smav		break;
1590230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
1591230130Smav		typestr = "audio selector";
1592230130Smav		break;
1593230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
1594230130Smav		typestr = "pin";
1595230130Smav		break;
1596230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET:
1597230130Smav		typestr = "power widget";
1598230130Smav		break;
1599230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET:
1600230130Smav		typestr = "volume widget";
1601230130Smav		break;
1602230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
1603230130Smav		typestr = "beep widget";
1604230130Smav		break;
1605230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET:
1606230130Smav		typestr = "vendor widget";
1607230130Smav		break;
1608230130Smav	default:
1609230130Smav		typestr = "unknown type";
1610230130Smav		break;
1611230130Smav	}
1612230130Smav	strlcpy(w->name, typestr, sizeof(w->name));
1613230130Smav
1614230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1615230130Smav		uint32_t config;
1616230130Smav		const char *devstr;
1617230130Smav		int conn, color;
1618230130Smav
1619230130Smav		config = w->wclass.pin.config;
1620230130Smav		devstr = HDA_DEVS[(config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) >>
1621230130Smav		    HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT];
1622230130Smav		conn = (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) >>
1623230130Smav		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT;
1624230130Smav		color = (config & HDA_CONFIG_DEFAULTCONF_COLOR_MASK) >>
1625230130Smav		    HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT;
1626230130Smav		strlcat(w->name, ": ", sizeof(w->name));
1627230130Smav		strlcat(w->name, devstr, sizeof(w->name));
1628230130Smav		strlcat(w->name, " (", sizeof(w->name));
1629230130Smav		if (conn == 0 && color != 0 && color != 15) {
1630230130Smav			strlcat(w->name, HDA_COLORS[color], sizeof(w->name));
1631230130Smav			strlcat(w->name, " ", sizeof(w->name));
1632230130Smav		}
1633230130Smav		strlcat(w->name, HDA_CONNS[conn], sizeof(w->name));
1634230130Smav		strlcat(w->name, ")", sizeof(w->name));
1635230130Smav	}
1636230130Smav}
1637230130Smav
1638230130Smavstruct hdaa_widget *
1639230130Smavhdaa_widget_get(struct hdaa_devinfo *devinfo, nid_t nid)
1640230130Smav{
1641230130Smav	if (devinfo == NULL || devinfo->widget == NULL ||
1642230130Smav		    nid < devinfo->startnode || nid >= devinfo->endnode)
1643230130Smav		return (NULL);
1644230130Smav	return (&devinfo->widget[nid - devinfo->startnode]);
1645230130Smav}
1646230130Smav
1647230130Smavstatic void
1648230130Smavhdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *devinfo, nid_t nid,
1649230130Smav					int index, int lmute, int rmute,
1650230130Smav					int left, int right, int dir)
1651230130Smav{
1652230130Smav	uint16_t v = 0;
1653230130Smav
1654230130Smav	HDA_BOOTHVERBOSE(
1655230130Smav		device_printf(devinfo->dev,
1656230130Smav		    "Setting amplifier nid=%d index=%d %s mute=%d/%d vol=%d/%d\n",
1657230130Smav		    nid,index,dir ? "in" : "out",lmute,rmute,left,right);
1658230130Smav	);
1659230130Smav	if (left != right || lmute != rmute) {
1660230130Smav		v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
1661230130Smav		    (lmute << 7) | left;
1662230130Smav		hda_command(devinfo->dev,
1663230130Smav		    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1664230130Smav		v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
1665230130Smav		    (rmute << 7) | right;
1666230130Smav	} else
1667230130Smav		v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
1668230130Smav		    (lmute << 7) | left;
1669230130Smav
1670230130Smav	hda_command(devinfo->dev,
1671230130Smav	    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1672230130Smav}
1673230130Smav
1674230130Smavstatic void
1675230130Smavhdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *ctl, uint32_t mute,
1676230130Smav						int left, int right)
1677230130Smav{
1678230130Smav	nid_t nid;
1679230130Smav	int lmute, rmute;
1680230130Smav
1681230130Smav	nid = ctl->widget->nid;
1682230130Smav
1683230130Smav	/* Save new values if valid. */
1684230130Smav	if (mute != HDAA_AMP_MUTE_DEFAULT)
1685230130Smav		ctl->muted = mute;
1686230130Smav	if (left != HDAA_AMP_VOL_DEFAULT)
1687230130Smav		ctl->left = left;
1688230130Smav	if (right != HDAA_AMP_VOL_DEFAULT)
1689230130Smav		ctl->right = right;
1690230130Smav	/* Prepare effective values */
1691230130Smav	if (ctl->forcemute) {
1692230130Smav		lmute = 1;
1693230130Smav		rmute = 1;
1694230130Smav		left = 0;
1695230130Smav		right = 0;
1696230130Smav	} else {
1697230130Smav		lmute = HDAA_AMP_LEFT_MUTED(ctl->muted);
1698230130Smav		rmute = HDAA_AMP_RIGHT_MUTED(ctl->muted);
1699230130Smav		left = ctl->left;
1700230130Smav		right = ctl->right;
1701230130Smav	}
1702230130Smav	/* Apply effective values */
1703230130Smav	if (ctl->dir & HDAA_CTL_OUT)
1704230130Smav		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1705230130Smav		    lmute, rmute, left, right, 0);
1706230130Smav	if (ctl->dir & HDAA_CTL_IN)
1707230130Smav		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1708230130Smav		    lmute, rmute, left, right, 1);
1709230130Smav}
1710230130Smav
1711230130Smavstatic void
1712230130Smavhdaa_widget_connection_select(struct hdaa_widget *w, uint8_t index)
1713230130Smav{
1714230130Smav	if (w == NULL || w->nconns < 1 || index > (w->nconns - 1))
1715230130Smav		return;
1716230130Smav	HDA_BOOTHVERBOSE(
1717230130Smav		device_printf(w->devinfo->dev,
1718230130Smav		    "Setting selector nid=%d index=%d\n", w->nid, index);
1719230130Smav	);
1720230130Smav	hda_command(w->devinfo->dev,
1721230130Smav	    HDA_CMD_SET_CONNECTION_SELECT_CONTROL(0, w->nid, index));
1722230130Smav	w->selconn = index;
1723230130Smav}
1724230130Smav
1725230130Smav/****************************************************************************
1726230130Smav * Device Methods
1727230130Smav ****************************************************************************/
1728230130Smav
1729230130Smavstatic void *
1730230130Smavhdaa_channel_init(kobj_t obj, void *data, struct snd_dbuf *b,
1731230130Smav					struct pcm_channel *c, int dir)
1732230130Smav{
1733230130Smav	struct hdaa_chan *ch = data;
1734230130Smav	struct hdaa_pcm_devinfo *pdevinfo = ch->pdevinfo;
1735230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1736230130Smav
1737230130Smav	hdaa_lock(devinfo);
1738230130Smav	if (devinfo->quirks & HDAA_QUIRK_FIXEDRATE) {
1739230130Smav		ch->caps.minspeed = ch->caps.maxspeed = 48000;
1740230130Smav		ch->pcmrates[0] = 48000;
1741230130Smav		ch->pcmrates[1] = 0;
1742230130Smav	}
1743230130Smav	ch->dir = dir;
1744230130Smav	ch->b = b;
1745230130Smav	ch->c = c;
1746230130Smav	ch->blksz = pdevinfo->chan_size / pdevinfo->chan_blkcnt;
1747230130Smav	ch->blkcnt = pdevinfo->chan_blkcnt;
1748230130Smav	hdaa_unlock(devinfo);
1749230130Smav
1750230130Smav	if (sndbuf_alloc(ch->b, bus_get_dma_tag(devinfo->dev),
1751230130Smav	    hda_get_dma_nocache(devinfo->dev) ? BUS_DMA_NOCACHE : 0,
1752230130Smav	    pdevinfo->chan_size) != 0)
1753230130Smav		return (NULL);
1754230130Smav
1755230130Smav	return (ch);
1756230130Smav}
1757230130Smav
1758230130Smavstatic int
1759230130Smavhdaa_channel_setformat(kobj_t obj, void *data, uint32_t format)
1760230130Smav{
1761230130Smav	struct hdaa_chan *ch = data;
1762230130Smav	int i;
1763230130Smav
1764230130Smav	for (i = 0; ch->caps.fmtlist[i] != 0; i++) {
1765230130Smav		if (format == ch->caps.fmtlist[i]) {
1766230130Smav			ch->fmt = format;
1767230130Smav			return (0);
1768230130Smav		}
1769230130Smav	}
1770230130Smav
1771230130Smav	return (EINVAL);
1772230130Smav}
1773230130Smav
1774230130Smavstatic uint32_t
1775230130Smavhdaa_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
1776230130Smav{
1777230130Smav	struct hdaa_chan *ch = data;
1778230130Smav	uint32_t spd = 0, threshold;
1779230130Smav	int i;
1780230130Smav
1781230130Smav	/* First look for equal or multiple frequency. */
1782230130Smav	for (i = 0; ch->pcmrates[i] != 0; i++) {
1783230130Smav		spd = ch->pcmrates[i];
1784230130Smav		if (speed != 0 && spd / speed * speed == spd) {
1785230130Smav			ch->spd = spd;
1786230130Smav			return (spd);
1787230130Smav		}
1788230130Smav	}
1789230130Smav	/* If no match, just find nearest. */
1790230130Smav	for (i = 0; ch->pcmrates[i] != 0; i++) {
1791230130Smav		spd = ch->pcmrates[i];
1792230130Smav		threshold = spd + ((ch->pcmrates[i + 1] != 0) ?
1793230130Smav		    ((ch->pcmrates[i + 1] - spd) >> 1) : 0);
1794230130Smav		if (speed < threshold)
1795230130Smav			break;
1796230130Smav	}
1797230130Smav	ch->spd = spd;
1798230130Smav	return (spd);
1799230130Smav}
1800230130Smav
1801230130Smavstatic uint16_t
1802230130Smavhdaa_stream_format(struct hdaa_chan *ch)
1803230130Smav{
1804230130Smav	int i;
1805230130Smav	uint16_t fmt;
1806230130Smav
1807230130Smav	fmt = 0;
1808230130Smav	if (ch->fmt & AFMT_S16_LE)
1809230130Smav		fmt |= ch->bit16 << 4;
1810230130Smav	else if (ch->fmt & AFMT_S32_LE)
1811230130Smav		fmt |= ch->bit32 << 4;
1812230130Smav	else
1813230130Smav		fmt |= 1 << 4;
1814230130Smav	for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
1815230130Smav		if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
1816230130Smav			fmt |= hda_rate_tab[i].base;
1817230130Smav			fmt |= hda_rate_tab[i].mul;
1818230130Smav			fmt |= hda_rate_tab[i].div;
1819230130Smav			break;
1820230130Smav		}
1821230130Smav	}
1822230130Smav	fmt |= (AFMT_CHANNEL(ch->fmt) - 1);
1823230130Smav
1824230130Smav	return (fmt);
1825230130Smav}
1826230130Smav
1827230326Smavstatic int
1828230326Smavhdaa_allowed_stripes(uint16_t fmt)
1829230326Smav{
1830230326Smav	static const int bits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1831230326Smav	int size;
1832230326Smav
1833230326Smav	size = bits[(fmt >> 4) & 0x03];
1834230326Smav	size *= (fmt & 0x0f) + 1;
1835230326Smav	size *= ((fmt >> 11) & 0x07) + 1;
1836230326Smav	return (0xffffffffU >> (32 - fls(size / 8)));
1837230326Smav}
1838230326Smav
1839230130Smavstatic void
1840230130Smavhdaa_audio_setup(struct hdaa_chan *ch)
1841230130Smav{
1842230130Smav	struct hdaa_audio_as *as = &ch->devinfo->as[ch->as];
1843230312Smav	struct hdaa_widget *w, *wp;
1844230312Smav	int i, j, k, chn, cchn, totalchn, totalextchn, c;
1845230130Smav	uint16_t fmt, dfmt;
1846230312Smav	/* Mapping channel pairs to codec pins/converters. */
1847230312Smav	const static uint16_t convmap[2][5] =
1848243181Smav	    /*  1.0     2.0     4.0     5.1     7.1  */
1849243181Smav	    {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x4231 },	/* no dup. */
1850243181Smav	     { 0x0010, 0x0001, 0x2201, 0x2231, 0x4231 }};	/* side dup. */
1851230312Smav	/* Mapping formats to HDMI channel allocations. */
1852230312Smav	const static uint8_t hdmica[2][8] =
1853243181Smav	    /*  1     2     3     4     5     6     7     8  */
1854230312Smav	    {{ 0x02, 0x00, 0x04, 0x08, 0x0a, 0x0e, 0x12, 0x12 }, /* x.0 */
1855230312Smav	     { 0x01, 0x03, 0x01, 0x03, 0x09, 0x0b, 0x0f, 0x13 }}; /* x.1 */
1856230312Smav	/* Mapping formats to HDMI channels order. */
1857230312Smav	const static uint32_t hdmich[2][8] =
1858243181Smav	    /*  1  /  5     2  /  6     3  /  7     4  /  8  */
1859230312Smav	    {{ 0xFFFF0F00, 0xFFFFFF10, 0xFFF2FF10, 0xFF32FF10,
1860230312Smav	       0xFF324F10, 0xF5324F10, 0x54326F10, 0x54326F10 }, /* x.0 */
1861230312Smav	     { 0xFFFFF000, 0xFFFF0100, 0xFFFFF210, 0xFFFF2310,
1862230312Smav	       0xFF32F410, 0xFF324510, 0xF6324510, 0x76325410 }}; /* x.1 */
1863230312Smav	int convmapid = -1;
1864230312Smav	nid_t nid;
1865230312Smav	uint8_t csum;
1866230130Smav
1867230130Smav	totalchn = AFMT_CHANNEL(ch->fmt);
1868230312Smav	totalextchn = AFMT_EXTCHANNEL(ch->fmt);
1869230130Smav	HDA_BOOTHVERBOSE(
1870230130Smav		device_printf(ch->pdevinfo->dev,
1871230312Smav		    "PCMDIR_%s: Stream setup fmt=%08x (%d.%d) speed=%d\n",
1872230130Smav		    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1873230312Smav		    ch->fmt, totalchn - totalextchn, totalextchn, ch->spd);
1874230130Smav	);
1875230130Smav	fmt = hdaa_stream_format(ch);
1876230130Smav
1877230312Smav	/* Set channels to I/O converters mapping for known speaker setups. */
1878243181Smav	if ((as->pinset == 0x0007 || as->pinset == 0x0013) || /* Standard 5.1 */
1879243181Smav	    (as->pinset == 0x0017)) /* Standard 7.1 */
1880243181Smav		convmapid = (ch->dir == PCMDIR_PLAY);
1881230130Smav
1882230130Smav	dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN;
1883230130Smav	if (ch->fmt & AFMT_AC3)
1884230130Smav		dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO;
1885230130Smav
1886230130Smav	chn = 0;
1887230130Smav	for (i = 0; ch->io[i] != -1; i++) {
1888230130Smav		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1889230130Smav		if (w == NULL)
1890230130Smav			continue;
1891230130Smav
1892230130Smav		/* If HP redirection is enabled, but failed to use same
1893230130Smav		   DAC, make last DAC to duplicate first one. */
1894230130Smav		if (as->fakeredir && i == (as->pincnt - 1)) {
1895230130Smav			c = (ch->sid << 4);
1896230130Smav		} else {
1897230312Smav			/* Map channels to I/O converters, if set. */
1898230312Smav			if (convmapid >= 0)
1899230312Smav				chn = (((convmap[convmapid][totalchn / 2]
1900230312Smav				    >> i * 4) & 0xf) - 1) * 2;
1901230130Smav			if (chn < 0 || chn >= totalchn) {
1902230130Smav				c = 0;
1903230130Smav			} else {
1904230130Smav				c = (ch->sid << 4) | chn;
1905230130Smav			}
1906230130Smav		}
1907230130Smav		hda_command(ch->devinfo->dev,
1908230130Smav		    HDA_CMD_SET_CONV_FMT(0, ch->io[i], fmt));
1909230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1910230130Smav			hda_command(ch->devinfo->dev,
1911230130Smav			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], dfmt));
1912230130Smav		}
1913230130Smav		hda_command(ch->devinfo->dev,
1914230130Smav		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i], c));
1915230326Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1916230326Smav			hda_command(ch->devinfo->dev,
1917230326Smav			    HDA_CMD_SET_STRIPE_CONTROL(0, w->nid, ch->stripectl));
1918230326Smav		}
1919230312Smav		cchn = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1920230312Smav		if (cchn > 1 && chn < totalchn) {
1921230312Smav			cchn = min(cchn, totalchn - chn - 1);
1922230312Smav			hda_command(ch->devinfo->dev,
1923230312Smav			    HDA_CMD_SET_CONV_CHAN_COUNT(0, ch->io[i], cchn));
1924230312Smav		}
1925230312Smav		HDA_BOOTHVERBOSE(
1926230312Smav			device_printf(ch->pdevinfo->dev,
1927230312Smav			    "PCMDIR_%s: Stream setup nid=%d: "
1928230312Smav			    "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x, "
1929230326Smav			    "chan_count=0x%02x, stripe=%d\n",
1930230312Smav			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1931230326Smav			    ch->io[i], fmt, dfmt, c, cchn, ch->stripectl);
1932230312Smav		);
1933230312Smav		for (j = 0; j < 16; j++) {
1934230312Smav			if (as->dacs[ch->asindex][j] != ch->io[i])
1935230312Smav				continue;
1936230312Smav			nid = as->pins[j];
1937230312Smav			wp = hdaa_widget_get(ch->devinfo, nid);
1938230312Smav			if (wp == NULL)
1939230312Smav				continue;
1940230312Smav			if (!HDA_PARAM_PIN_CAP_DP(wp->wclass.pin.cap) &&
1941230312Smav			    !HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap))
1942230312Smav				continue;
1943230312Smav
1944230312Smav			/* Set channel mapping. */
1945230312Smav			for (k = 0; k < 8; k++) {
1946230312Smav				hda_command(ch->devinfo->dev,
1947230312Smav				    HDA_CMD_SET_HDMI_CHAN_SLOT(0, nid,
1948230312Smav				    (((hdmich[totalextchn == 0 ? 0 : 1][totalchn - 1]
1949230312Smav				     >> (k * 4)) & 0xf) << 4) | k));
1950230312Smav			}
1951230312Smav
1952230511Smav			/*
1953230511Smav			 * Enable High Bit Rate (HBR) Encoded Packet Type
1954230511Smav			 * (EPT), if supported and needed (8ch data).
1955230511Smav			 */
1956230511Smav			if (HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap) &&
1957230511Smav			    HDA_PARAM_PIN_CAP_HBR(wp->wclass.pin.cap)) {
1958230511Smav				wp->wclass.pin.ctrl &=
1959230511Smav				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK;
1960230641Smav				if ((ch->fmt & AFMT_AC3) && (cchn == 7))
1961230511Smav					wp->wclass.pin.ctrl |= 0x03;
1962230511Smav				hda_command(ch->devinfo->dev,
1963230511Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL(0, nid,
1964230511Smav				    wp->wclass.pin.ctrl));
1965230511Smav			}
1966230511Smav
1967230312Smav			/* Stop audio infoframe transmission. */
1968230312Smav			hda_command(ch->devinfo->dev,
1969230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1970230312Smav			hda_command(ch->devinfo->dev,
1971230312Smav			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0x00));
1972230312Smav
1973230312Smav			/* Clear audio infoframe buffer. */
1974230312Smav			hda_command(ch->devinfo->dev,
1975230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1976230312Smav			for (k = 0; k < 32; k++)
1977230312Smav				hda_command(ch->devinfo->dev,
1978230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1979230312Smav
1980230312Smav			/* Write HDMI/DisplayPort audio infoframe. */
1981230312Smav			hda_command(ch->devinfo->dev,
1982230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1983230312Smav			if (w->eld != NULL && w->eld_len >= 6 &&
1984230312Smav			    ((w->eld[5] >> 2) & 0x3) == 1) { /* DisplayPort */
1985230312Smav				hda_command(ch->devinfo->dev,
1986230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1987230312Smav				hda_command(ch->devinfo->dev,
1988230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x1b));
1989230312Smav				hda_command(ch->devinfo->dev,
1990230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x44));
1991230312Smav			} else {	/* HDMI */
1992230312Smav				hda_command(ch->devinfo->dev,
1993230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1994230312Smav				hda_command(ch->devinfo->dev,
1995230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x01));
1996230312Smav				hda_command(ch->devinfo->dev,
1997230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x0a));
1998230312Smav				csum = 0;
1999230312Smav				csum -= 0x84 + 0x01 + 0x0a + (totalchn - 1) +
2000230312Smav				    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1];
2001230312Smav				hda_command(ch->devinfo->dev,
2002230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, csum));
2003230312Smav			}
2004230312Smav			hda_command(ch->devinfo->dev,
2005230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, totalchn - 1));
2006230312Smav			hda_command(ch->devinfo->dev,
2007230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
2008230312Smav			hda_command(ch->devinfo->dev,
2009230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
2010230312Smav			hda_command(ch->devinfo->dev,
2011230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid,
2012230312Smav			    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1]));
2013230312Smav
2014230312Smav			/* Start audio infoframe transmission. */
2015230312Smav			hda_command(ch->devinfo->dev,
2016230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
2017230312Smav			hda_command(ch->devinfo->dev,
2018230312Smav			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0xc0));
2019230312Smav		}
2020230312Smav		chn += cchn + 1;
2021230130Smav	}
2022230130Smav}
2023230130Smav
2024230130Smav/*
2025230130Smav * Greatest Common Divisor.
2026230130Smav */
2027230130Smavstatic unsigned
2028230130Smavgcd(unsigned a, unsigned b)
2029230130Smav{
2030230130Smav	u_int c;
2031230130Smav
2032230130Smav	while (b != 0) {
2033230130Smav		c = a;
2034230130Smav		a = b;
2035230130Smav		b = (c % b);
2036230130Smav	}
2037230130Smav	return (a);
2038230130Smav}
2039230130Smav
2040230130Smav/*
2041230130Smav * Least Common Multiple.
2042230130Smav */
2043230130Smavstatic unsigned
2044230130Smavlcm(unsigned a, unsigned b)
2045230130Smav{
2046230130Smav
2047230130Smav	return ((a * b) / gcd(a, b));
2048230130Smav}
2049230130Smav
2050230130Smavstatic int
2051230130Smavhdaa_channel_setfragments(kobj_t obj, void *data,
2052230130Smav					uint32_t blksz, uint32_t blkcnt)
2053230130Smav{
2054230130Smav	struct hdaa_chan *ch = data;
2055230130Smav
2056230130Smav	blksz -= blksz % lcm(HDA_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
2057230130Smav
2058230130Smav	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
2059230130Smav		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
2060230130Smav	if (blksz < HDA_BLK_MIN)
2061230130Smav		blksz = HDA_BLK_MIN;
2062230130Smav	if (blkcnt > HDA_BDL_MAX)
2063230130Smav		blkcnt = HDA_BDL_MAX;
2064230130Smav	if (blkcnt < HDA_BDL_MIN)
2065230130Smav		blkcnt = HDA_BDL_MIN;
2066230130Smav
2067230130Smav	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
2068230130Smav		if ((blkcnt >> 1) >= HDA_BDL_MIN)
2069230130Smav			blkcnt >>= 1;
2070230130Smav		else if ((blksz >> 1) >= HDA_BLK_MIN)
2071230130Smav			blksz >>= 1;
2072230130Smav		else
2073230130Smav			break;
2074230130Smav	}
2075230130Smav
2076230130Smav	if ((sndbuf_getblksz(ch->b) != blksz ||
2077230130Smav	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
2078230130Smav	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
2079230130Smav		device_printf(ch->devinfo->dev, "%s: failed blksz=%u blkcnt=%u\n",
2080230130Smav		    __func__, blksz, blkcnt);
2081230130Smav
2082230130Smav	ch->blksz = sndbuf_getblksz(ch->b);
2083230130Smav	ch->blkcnt = sndbuf_getblkcnt(ch->b);
2084230130Smav
2085230130Smav	return (0);
2086230130Smav}
2087230130Smav
2088230130Smavstatic uint32_t
2089230130Smavhdaa_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
2090230130Smav{
2091230130Smav	struct hdaa_chan *ch = data;
2092230130Smav
2093230130Smav	hdaa_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
2094230130Smav
2095230130Smav	return (ch->blksz);
2096230130Smav}
2097230130Smav
2098230130Smavstatic void
2099230130Smavhdaa_channel_stop(struct hdaa_chan *ch)
2100230130Smav{
2101230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
2102230130Smav	struct hdaa_widget *w;
2103230130Smav	int i;
2104230130Smav
2105230181Smav	if ((ch->flags & HDAA_CHN_RUNNING) == 0)
2106230181Smav		return;
2107230130Smav	ch->flags &= ~HDAA_CHN_RUNNING;
2108230130Smav	HDAC_STREAM_STOP(device_get_parent(devinfo->dev), devinfo->dev,
2109230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2110230130Smav	for (i = 0; ch->io[i] != -1; i++) {
2111230130Smav		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
2112230130Smav		if (w == NULL)
2113230130Smav			continue;
2114230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
2115230130Smav			hda_command(devinfo->dev,
2116230130Smav			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], 0));
2117230130Smav		}
2118230130Smav		hda_command(devinfo->dev,
2119230130Smav		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i],
2120230130Smav		    0));
2121230130Smav	}
2122230130Smav	HDAC_STREAM_FREE(device_get_parent(devinfo->dev), devinfo->dev,
2123230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2124230130Smav}
2125230130Smav
2126230130Smavstatic int
2127230130Smavhdaa_channel_start(struct hdaa_chan *ch)
2128230130Smav{
2129230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
2130230326Smav	uint32_t fmt;
2131230130Smav
2132230326Smav	fmt = hdaa_stream_format(ch);
2133317010Smmel	ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt) &
2134317010Smmel	    hda_get_stripes_mask(devinfo->dev)) - 1;
2135230130Smav	ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev,
2136230326Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos);
2137230130Smav	if (ch->sid <= 0)
2138230130Smav		return (EBUSY);
2139230130Smav	hdaa_audio_setup(ch);
2140230130Smav	HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev,
2141230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2142230130Smav	HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev,
2143230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid,
2144230130Smav	    sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt);
2145230130Smav	ch->flags |= HDAA_CHN_RUNNING;
2146230130Smav	return (0);
2147230130Smav}
2148230130Smav
2149230130Smavstatic int
2150230130Smavhdaa_channel_trigger(kobj_t obj, void *data, int go)
2151230130Smav{
2152230130Smav	struct hdaa_chan *ch = data;
2153230130Smav	int error = 0;
2154230130Smav
2155230130Smav	if (!PCMTRIG_COMMON(go))
2156230130Smav		return (0);
2157230130Smav
2158230130Smav	hdaa_lock(ch->devinfo);
2159230130Smav	switch (go) {
2160230130Smav	case PCMTRIG_START:
2161230130Smav		error = hdaa_channel_start(ch);
2162230130Smav		break;
2163230130Smav	case PCMTRIG_STOP:
2164230130Smav	case PCMTRIG_ABORT:
2165230130Smav		hdaa_channel_stop(ch);
2166230130Smav		break;
2167230130Smav	default:
2168230130Smav		break;
2169230130Smav	}
2170230130Smav	hdaa_unlock(ch->devinfo);
2171230130Smav
2172230130Smav	return (error);
2173230130Smav}
2174230130Smav
2175230130Smavstatic uint32_t
2176230130Smavhdaa_channel_getptr(kobj_t obj, void *data)
2177230130Smav{
2178230130Smav	struct hdaa_chan *ch = data;
2179230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
2180230130Smav	uint32_t ptr;
2181230130Smav
2182230130Smav	hdaa_lock(devinfo);
2183230130Smav	if (ch->dmapos != NULL) {
2184230130Smav		ptr = *(ch->dmapos);
2185230130Smav	} else {
2186230130Smav		ptr = HDAC_STREAM_GETPTR(
2187230130Smav		    device_get_parent(devinfo->dev), devinfo->dev,
2188230130Smav		    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2189230130Smav	}
2190230130Smav	hdaa_unlock(devinfo);
2191230130Smav
2192230130Smav	/*
2193230130Smav	 * Round to available space and force 128 bytes aligment.
2194230130Smav	 */
2195230130Smav	ptr %= ch->blksz * ch->blkcnt;
2196230130Smav	ptr &= HDA_BLK_ALIGN;
2197230130Smav
2198230130Smav	return (ptr);
2199230130Smav}
2200230130Smav
2201230130Smavstatic struct pcmchan_caps *
2202230130Smavhdaa_channel_getcaps(kobj_t obj, void *data)
2203230130Smav{
2204230130Smav	return (&((struct hdaa_chan *)data)->caps);
2205230130Smav}
2206230130Smav
2207230130Smavstatic kobj_method_t hdaa_channel_methods[] = {
2208230130Smav	KOBJMETHOD(channel_init,		hdaa_channel_init),
2209230130Smav	KOBJMETHOD(channel_setformat,		hdaa_channel_setformat),
2210230130Smav	KOBJMETHOD(channel_setspeed,		hdaa_channel_setspeed),
2211230130Smav	KOBJMETHOD(channel_setblocksize,	hdaa_channel_setblocksize),
2212230130Smav	KOBJMETHOD(channel_setfragments,	hdaa_channel_setfragments),
2213230130Smav	KOBJMETHOD(channel_trigger,		hdaa_channel_trigger),
2214230130Smav	KOBJMETHOD(channel_getptr,		hdaa_channel_getptr),
2215230130Smav	KOBJMETHOD(channel_getcaps,		hdaa_channel_getcaps),
2216230130Smav	KOBJMETHOD_END
2217230130Smav};
2218230130SmavCHANNEL_DECLARE(hdaa_channel);
2219230130Smav
2220230130Smavstatic int
2221230130Smavhdaa_audio_ctl_ossmixer_init(struct snd_mixer *m)
2222230130Smav{
2223230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2224230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2225230130Smav	struct hdaa_widget *w, *cw;
2226230130Smav	uint32_t mask, recmask;
2227230451Smav	int i, j;
2228230130Smav
2229230130Smav	hdaa_lock(devinfo);
2230230451Smav	pdevinfo->mixer = m;
2231230130Smav
2232230130Smav	/* Make sure that in case of soft volume it won't stay muted. */
2233230130Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2234230130Smav		pdevinfo->left[i] = 100;
2235230130Smav		pdevinfo->right[i] = 100;
2236230130Smav	}
2237230130Smav
2238230451Smav	/* Declare volume controls assigned to this association. */
2239230451Smav	mask = pdevinfo->ossmask;
2240230130Smav	if (pdevinfo->playas >= 0) {
2241230451Smav		/* Declate EAPD as ogain control. */
2242230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2243230130Smav			w = hdaa_widget_get(devinfo, i);
2244230130Smav			if (w == NULL || w->enable == 0)
2245230130Smav				continue;
2246230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2247230130Smav			    w->param.eapdbtl == HDA_INVALID ||
2248230130Smav			    w->bindas != pdevinfo->playas)
2249230130Smav				continue;
2250230130Smav			mask |= SOUND_MASK_OGAIN;
2251230130Smav			break;
2252230130Smav		}
2253230130Smav
2254230451Smav		/* Declare soft PCM volume if needed. */
2255230451Smav		if ((mask & SOUND_MASK_PCM) == 0 ||
2256230451Smav		    (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) ||
2257230451Smav		    pdevinfo->minamp[SOUND_MIXER_PCM] ==
2258230451Smav		     pdevinfo->maxamp[SOUND_MIXER_PCM]) {
2259230451Smav			mask |= SOUND_MASK_PCM;
2260230451Smav			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
2261230451Smav			HDA_BOOTHVERBOSE(
2262230451Smav				device_printf(pdevinfo->dev,
2263230451Smav				    "Forcing Soft PCM volume\n");
2264230451Smav			);
2265230451Smav		}
2266230451Smav
2267230451Smav		/* Declare master volume if needed. */
2268230451Smav		if ((mask & SOUND_MASK_VOLUME) == 0) {
2269230451Smav			mask |= SOUND_MASK_VOLUME;
2270230451Smav			mix_setparentchild(m, SOUND_MIXER_VOLUME,
2271230451Smav			    SOUND_MASK_PCM);
2272230451Smav			mix_setrealdev(m, SOUND_MIXER_VOLUME,
2273230451Smav			    SOUND_MIXER_NONE);
2274230451Smav			HDA_BOOTHVERBOSE(
2275230451Smav				device_printf(pdevinfo->dev,
2276230451Smav				    "Forcing master volume with PCM\n");
2277230451Smav			);
2278230451Smav		}
2279230130Smav	}
2280230130Smav
2281230130Smav	/* Declare record sources available to this association. */
2282230451Smav	recmask = 0;
2283230130Smav	if (pdevinfo->recas >= 0) {
2284230130Smav		for (i = 0; i < 16; i++) {
2285230130Smav			if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0)
2286230130Smav				continue;
2287230130Smav			w = hdaa_widget_get(devinfo,
2288230130Smav			    devinfo->as[pdevinfo->recas].dacs[0][i]);
2289230130Smav			if (w == NULL || w->enable == 0)
2290230130Smav				continue;
2291230130Smav			for (j = 0; j < w->nconns; j++) {
2292230130Smav				if (w->connsenable[j] == 0)
2293230130Smav					continue;
2294230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
2295230130Smav				if (cw == NULL || cw->enable == 0)
2296230130Smav					continue;
2297230130Smav				if (cw->bindas != pdevinfo->recas &&
2298230130Smav				    cw->bindas != -2)
2299230130Smav					continue;
2300230130Smav				recmask |= cw->ossmask;
2301230130Smav			}
2302230130Smav		}
2303230130Smav	}
2304230130Smav
2305230451Smav	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2306230451Smav	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2307230451Smav	pdevinfo->ossmask = mask;
2308230451Smav
2309230451Smav	mix_setrecdevs(m, recmask);
2310230451Smav	mix_setdevs(m, mask);
2311230451Smav
2312230451Smav	hdaa_unlock(devinfo);
2313230451Smav
2314230451Smav	return (0);
2315230451Smav}
2316230451Smav
2317230451Smav/*
2318230451Smav * Update amplification per pdevinfo per ossdev, calculate summary coefficient
2319230451Smav * and write it to codec, update *left and *right to reflect remaining error.
2320230451Smav */
2321230451Smavstatic void
2322230451Smavhdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev,
2323230451Smav    int mute, int *left, int *right)
2324230451Smav{
2325230451Smav	int i, zleft, zright, sleft, sright, smute, lval, rval;
2326230451Smav
2327230451Smav	ctl->devleft[ossdev] = *left;
2328230451Smav	ctl->devright[ossdev] = *right;
2329230451Smav	ctl->devmute[ossdev] = mute;
2330230451Smav	smute = sleft = sright = zleft = zright = 0;
2331230451Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2332230451Smav		sleft += ctl->devleft[i];
2333230451Smav		sright += ctl->devright[i];
2334230451Smav		smute |= ctl->devmute[i];
2335230451Smav		if (i == ossdev)
2336230451Smav			continue;
2337230451Smav		zleft += ctl->devleft[i];
2338230451Smav		zright += ctl->devright[i];
2339230451Smav	}
2340230451Smav	lval = QDB2VAL(ctl, sleft);
2341230451Smav	rval = QDB2VAL(ctl, sright);
2342230451Smav	hdaa_audio_ctl_amp_set(ctl, smute, lval, rval);
2343230451Smav	*left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft));
2344230451Smav	*right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright));
2345230451Smav}
2346230451Smav
2347230451Smav/*
2348230451Smav * Trace signal from source, setting volumes on the way.
2349230451Smav */
2350230451Smavstatic void
2351230451Smavhdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo,
2352230451Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2353230451Smav{
2354230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2355230451Smav	struct hdaa_widget *w, *wc;
2356230451Smav	struct hdaa_audio_ctl *ctl;
2357230451Smav	int i, j, conns = 0;
2358230451Smav
2359230451Smav	if (depth > HDA_PARSE_MAXDEPTH)
2360230451Smav		return;
2361230451Smav
2362230451Smav	w = hdaa_widget_get(devinfo, nid);
2363230451Smav	if (w == NULL || w->enable == 0)
2364230451Smav		return;
2365230451Smav
2366230451Smav	/* Count number of active inputs. */
2367230451Smav	if (depth > 0) {
2368230451Smav		for (j = 0; j < w->nconns; j++) {
2369230451Smav			if (!w->connsenable[j])
2370230451Smav				continue;
2371230451Smav			conns++;
2372230130Smav		}
2373230451Smav	}
2374230130Smav
2375230451Smav	/* If this is not a first step - use input mixer.
2376230451Smav	   Pins have common input ctl so care must be taken. */
2377230451Smav	if (depth > 0 && (conns == 1 ||
2378230451Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
2379230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
2380230451Smav		    index, 1);
2381230451Smav		if (ctl)
2382230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2383230451Smav	}
2384230451Smav
2385230451Smav	/* If widget has own ossdev - not traverse it.
2386230451Smav	   It will be traversed on it's own. */
2387230451Smav	if (w->ossdev >= 0 && depth > 0)
2388230451Smav		return;
2389230451Smav
2390230451Smav	/* We must not traverse pin */
2391230451Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
2392230451Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
2393230451Smav	    depth > 0)
2394230451Smav		return;
2395230451Smav
2396230451Smav	/*
2397230451Smav	 * If signals mixed, we can't assign controls farther.
2398230451Smav	 * Ignore this on depth zero. Caller must knows why.
2399230451Smav	 */
2400230451Smav	if (conns > 1 &&
2401230451Smav	    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
2402230451Smav	     w->selconn != index))
2403230451Smav		return;
2404230451Smav
2405230451Smav	ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
2406230451Smav	if (ctl)
2407230451Smav		hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2408230451Smav
2409230451Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2410230451Smav		wc = hdaa_widget_get(devinfo, i);
2411230451Smav		if (wc == NULL || wc->enable == 0)
2412230451Smav			continue;
2413230451Smav		for (j = 0; j < wc->nconns; j++) {
2414230451Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
2415230451Smav				hdaa_audio_ctl_source_volume(pdevinfo, ossdev,
2416230451Smav				    wc->nid, j, mute, left, right, depth + 1);
2417230451Smav			}
2418230130Smav		}
2419230130Smav	}
2420230451Smav	return;
2421230451Smav}
2422230130Smav
2423230451Smav/*
2424230451Smav * Trace signal from destination, setting volumes on the way.
2425230451Smav */
2426230451Smavstatic void
2427230451Smavhdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo,
2428230451Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2429230451Smav{
2430230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2431230451Smav	struct hdaa_audio_as *as = devinfo->as;
2432230451Smav	struct hdaa_widget *w, *wc;
2433230451Smav	struct hdaa_audio_ctl *ctl;
2434230451Smav	int i, j, consumers, cleft, cright;
2435230451Smav
2436230451Smav	if (depth > HDA_PARSE_MAXDEPTH)
2437230451Smav		return;
2438230451Smav
2439230451Smav	w = hdaa_widget_get(devinfo, nid);
2440230451Smav	if (w == NULL || w->enable == 0)
2441230451Smav		return;
2442230451Smav
2443230451Smav	if (depth > 0) {
2444230451Smav		/* If this node produce output for several consumers,
2445230451Smav		   we can't touch it. */
2446230451Smav		consumers = 0;
2447230451Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2448230451Smav			wc = hdaa_widget_get(devinfo, i);
2449230451Smav			if (wc == NULL || wc->enable == 0)
2450230451Smav				continue;
2451230451Smav			for (j = 0; j < wc->nconns; j++) {
2452230451Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
2453230451Smav					consumers++;
2454230451Smav			}
2455230130Smav		}
2456230451Smav		/* The only exception is if real HP redirection is configured
2457230451Smav		   and this is a duplication point.
2458230451Smav		   XXX: Actually exception is not completely correct.
2459230451Smav		   XXX: Duplication point check is not perfect. */
2460230451Smav		if ((consumers == 2 && (w->bindas < 0 ||
2461230451Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
2462230451Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
2463230451Smav		    consumers > 2)
2464230451Smav			return;
2465230451Smav
2466230451Smav		/* Else use it's output mixer. */
2467230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2468230451Smav		    HDAA_CTL_OUT, -1, 1);
2469230451Smav		if (ctl)
2470230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2471230130Smav	}
2472230130Smav
2473230451Smav	/* We must not traverse pin */
2474230451Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2475230451Smav	    depth > 0)
2476230451Smav		return;
2477230130Smav
2478230451Smav	for (i = 0; i < w->nconns; i++) {
2479230451Smav		if (w->connsenable[i] == 0)
2480230451Smav			continue;
2481230451Smav		if (index >= 0 && i != index)
2482230451Smav			continue;
2483230451Smav		cleft = left;
2484230451Smav		cright = right;
2485230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2486230451Smav		    HDAA_CTL_IN, i, 1);
2487230451Smav		if (ctl)
2488230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright);
2489230451Smav		hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1,
2490230451Smav		    mute, cleft, cright, depth + 1);
2491230451Smav	}
2492230451Smav}
2493230130Smav
2494230451Smav/*
2495230451Smav * Set volumes for the specified pdevinfo and ossdev.
2496230451Smav */
2497230451Smavstatic void
2498230451Smavhdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev)
2499230451Smav{
2500230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2501230451Smav	struct hdaa_widget *w, *cw;
2502230451Smav	uint32_t mute;
2503230451Smav	int lvol, rvol;
2504230451Smav	int i, j;
2505230130Smav
2506230451Smav	mute = 0;
2507230451Smav	if (pdevinfo->left[dev] == 0) {
2508230451Smav		mute |= HDAA_AMP_MUTE_LEFT;
2509230451Smav		lvol = -4000;
2510230451Smav	} else
2511230451Smav		lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2512230451Smav		    pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev];
2513230451Smav	if (pdevinfo->right[dev] == 0) {
2514230451Smav		mute |= HDAA_AMP_MUTE_RIGHT;
2515230451Smav		rvol = -4000;
2516230451Smav	} else
2517230451Smav		rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2518230451Smav		    pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev];
2519230451Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2520230451Smav		w = hdaa_widget_get(devinfo, i);
2521230451Smav		if (w == NULL || w->enable == 0)
2522230451Smav			continue;
2523239326Smav		if (w->bindas < 0) {
2524239326Smav			if (pdevinfo->index != 0)
2525239326Smav				continue;
2526239326Smav		} else {
2527239326Smav			if (w->bindas != pdevinfo->playas &&
2528239326Smav			    w->bindas != pdevinfo->recas)
2529239326Smav				continue;
2530239326Smav		}
2531230451Smav		if (dev == SOUND_MIXER_RECLEV &&
2532230451Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2533230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2534230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2535230451Smav			continue;
2536230451Smav		}
2537230451Smav		if (dev == SOUND_MIXER_VOLUME &&
2538230451Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2539230451Smav		    devinfo->as[w->bindas].dir == HDAA_CTL_OUT) {
2540230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2541230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2542230451Smav			continue;
2543230451Smav		}
2544230451Smav		if (dev == SOUND_MIXER_IGAIN &&
2545230451Smav		    w->pflags & HDAA_ADC_MONITOR) {
2546230451Smav			for (j = 0; j < w->nconns; j++) {
2547230451Smav				if (!w->connsenable[j])
2548230451Smav				    continue;
2549230451Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
2550230451Smav				if (cw == NULL || cw->enable == 0)
2551230451Smav				    continue;
2552230451Smav				if (cw->bindas == -1)
2553230451Smav				    continue;
2554230451Smav				if (cw->bindas >= 0 &&
2555230451Smav				    devinfo->as[cw->bindas].dir != HDAA_CTL_IN)
2556230451Smav					continue;
2557230451Smav				hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2558230451Smav				    w->nid, j, mute, lvol, rvol, 0);
2559230451Smav			}
2560230451Smav			continue;
2561230451Smav		}
2562230451Smav		if (w->ossdev != dev)
2563230451Smav			continue;
2564230451Smav		hdaa_audio_ctl_source_volume(pdevinfo, dev,
2565230451Smav		    w->nid, -1, mute, lvol, rvol, 0);
2566230451Smav		if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST))
2567230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2568230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2569230451Smav	}
2570230130Smav}
2571230130Smav
2572230451Smav/*
2573230451Smav * OSS Mixer set method.
2574230451Smav */
2575230130Smavstatic int
2576230130Smavhdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
2577230130Smav					unsigned left, unsigned right)
2578230130Smav{
2579230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2580230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2581230130Smav	struct hdaa_widget *w;
2582230451Smav	int i;
2583230130Smav
2584230130Smav	hdaa_lock(devinfo);
2585230451Smav
2586230130Smav	/* Save new values. */
2587230130Smav	pdevinfo->left[dev] = left;
2588230130Smav	pdevinfo->right[dev] = right;
2589230130Smav
2590230130Smav	/* 'ogain' is the special case implemented with EAPD. */
2591230130Smav	if (dev == SOUND_MIXER_OGAIN) {
2592230130Smav		uint32_t orig;
2593230130Smav		w = NULL;
2594230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2595230130Smav			w = hdaa_widget_get(devinfo, i);
2596230130Smav			if (w == NULL || w->enable == 0)
2597230130Smav				continue;
2598230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2599230130Smav			    w->param.eapdbtl == HDA_INVALID)
2600230130Smav				continue;
2601230130Smav			break;
2602230130Smav		}
2603230130Smav		if (i >= devinfo->endnode) {
2604230130Smav			hdaa_unlock(devinfo);
2605230130Smav			return (-1);
2606230130Smav		}
2607230130Smav		orig = w->param.eapdbtl;
2608230130Smav		if (left == 0)
2609230130Smav			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2610230130Smav		else
2611230130Smav			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2612230130Smav		if (orig != w->param.eapdbtl) {
2613230130Smav			uint32_t val;
2614230130Smav
2615230130Smav			val = w->param.eapdbtl;
2616230130Smav			if (devinfo->quirks & HDAA_QUIRK_EAPDINV)
2617230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2618230130Smav			hda_command(devinfo->dev,
2619230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val));
2620230130Smav		}
2621230130Smav		hdaa_unlock(devinfo);
2622230130Smav		return (left | (left << 8));
2623230130Smav	}
2624230130Smav
2625230130Smav	/* Recalculate all controls related to this OSS device. */
2626230451Smav	hdaa_audio_ctl_dev_volume(pdevinfo, dev);
2627230451Smav
2628230451Smav	hdaa_unlock(devinfo);
2629230451Smav	return (left | (right << 8));
2630230451Smav}
2631230451Smav
2632230451Smav/*
2633230451Smav * Set mixer settings to our own default values:
2634230451Smav * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others.
2635230451Smav */
2636230451Smavstatic void
2637230451Smavhdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo)
2638230451Smav{
2639230451Smav	int amp, vol, dev;
2640230451Smav
2641230451Smav	for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) {
2642230451Smav		if ((pdevinfo->ossmask & (1 << dev)) == 0)
2643230130Smav			continue;
2644230451Smav
2645230451Smav		/* If the value was overriden, leave it as is. */
2646230451Smav		if (resource_int_value(device_get_name(pdevinfo->dev),
2647230451Smav		    device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0)
2648230130Smav			continue;
2649230130Smav
2650230451Smav		vol = -1;
2651230451Smav		if (dev == SOUND_MIXER_OGAIN)
2652230451Smav			vol = 100;
2653230451Smav		else if (dev == SOUND_MIXER_IGAIN)
2654230451Smav			vol = 0;
2655230451Smav		else if (dev == SOUND_MIXER_MIC ||
2656230451Smav		    dev == SOUND_MIXER_MONITOR)
2657230451Smav			amp = 20 * 4;	/* +20dB */
2658230451Smav		else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital)
2659230451Smav			amp = -10 * 4;	/* -10dB */
2660230451Smav		else
2661230451Smav			amp = 0;
2662230451Smav		if (vol < 0 &&
2663230451Smav		    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) {
2664230451Smav			vol = 100;
2665230451Smav		} else if (vol < 0) {
2666230451Smav			vol = ((amp - pdevinfo->minamp[dev]) * 100 +
2667230451Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) /
2668230451Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]);
2669230451Smav			vol = imin(imax(vol, 1), 100);
2670230130Smav		}
2671230451Smav		mix_set(pdevinfo->mixer, dev, vol, vol);
2672230130Smav	}
2673230130Smav}
2674230130Smav
2675230130Smav/*
2676230451Smav * Recursively commutate specified record source.
2677230130Smav */
2678230130Smavstatic uint32_t
2679230130Smavhdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
2680230130Smav{
2681230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2682230130Smav	struct hdaa_widget *w, *cw;
2683230130Smav	struct hdaa_audio_ctl *ctl;
2684230130Smav	char buf[64];
2685230130Smav	int i, muted;
2686230130Smav	uint32_t res = 0;
2687230130Smav
2688230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
2689230130Smav		return (0);
2690230130Smav
2691230130Smav	w = hdaa_widget_get(devinfo, nid);
2692230130Smav	if (w == NULL || w->enable == 0)
2693230130Smav		return (0);
2694230130Smav
2695230130Smav	for (i = 0; i < w->nconns; i++) {
2696230130Smav		if (w->connsenable[i] == 0)
2697230130Smav			continue;
2698230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
2699230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
2700230130Smav			continue;
2701230130Smav		/* Call recursively to trace signal to it's source if needed. */
2702230130Smav		if ((src & cw->ossmask) != 0) {
2703230130Smav			if (cw->ossdev < 0) {
2704230130Smav				res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2705230130Smav				    w->conns[i], depth + 1);
2706230130Smav			} else {
2707230130Smav				res |= cw->ossmask;
2708230130Smav			}
2709230130Smav		}
2710230130Smav		/* We have two special cases: mixers and others (selectors). */
2711230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
2712230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo,
2713230130Smav			    w->nid, HDAA_CTL_IN, i, 1);
2714230130Smav			if (ctl == NULL)
2715230130Smav				continue;
2716230130Smav			/* If we have input control on this node mute them
2717230130Smav			 * according to requested sources. */
2718230130Smav			muted = (src & cw->ossmask) ? 0 : 1;
2719230130Smav			if (muted != ctl->forcemute) {
2720230130Smav				ctl->forcemute = muted;
2721230130Smav				hdaa_audio_ctl_amp_set(ctl,
2722230130Smav				    HDAA_AMP_MUTE_DEFAULT,
2723230130Smav				    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
2724230130Smav			}
2725230130Smav			HDA_BOOTHVERBOSE(
2726230130Smav				device_printf(pdevinfo->dev,
2727230130Smav				    "Recsel (%s): nid %d source %d %s\n",
2728230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2729230130Smav				    src, buf, sizeof(buf)),
2730230130Smav				    nid, i, muted?"mute":"unmute");
2731230130Smav			);
2732230130Smav		} else {
2733230130Smav			if (w->nconns == 1)
2734230130Smav				break;
2735230130Smav			if ((src & cw->ossmask) == 0)
2736230130Smav				continue;
2737230130Smav			/* If we found requested source - select it and exit. */
2738230130Smav			hdaa_widget_connection_select(w, i);
2739230130Smav			HDA_BOOTHVERBOSE(
2740230130Smav				device_printf(pdevinfo->dev,
2741230130Smav				    "Recsel (%s): nid %d source %d select\n",
2742230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2743230130Smav				    src, buf, sizeof(buf)),
2744230130Smav				    nid, i);
2745230130Smav			);
2746230130Smav			break;
2747230130Smav		}
2748230130Smav	}
2749230130Smav	return (res);
2750230130Smav}
2751230130Smav
2752230130Smavstatic uint32_t
2753230130Smavhdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
2754230130Smav{
2755230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2756230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2757230130Smav	struct hdaa_widget *w;
2758230130Smav	struct hdaa_audio_as *as;
2759230451Smav	struct hdaa_audio_ctl *ctl;
2760230130Smav	struct hdaa_chan *ch;
2761230130Smav	int i, j;
2762230130Smav	uint32_t ret = 0xffffffff;
2763230130Smav
2764230130Smav	hdaa_lock(devinfo);
2765230130Smav	if (pdevinfo->recas < 0) {
2766230130Smav		hdaa_unlock(devinfo);
2767230130Smav		return (0);
2768230130Smav	}
2769230130Smav	as = &devinfo->as[pdevinfo->recas];
2770230130Smav
2771230130Smav	/* For non-mixed associations we always recording everything. */
2772230130Smav	if (!as->mixed) {
2773230130Smav		hdaa_unlock(devinfo);
2774230130Smav		return (mix_getrecdevs(m));
2775230130Smav	}
2776230130Smav
2777230130Smav	/* Commutate requested recsrc for each ADC. */
2778230130Smav	for (j = 0; j < as->num_chans; j++) {
2779230130Smav		ch = &devinfo->chans[as->chans[j]];
2780230130Smav		for (i = 0; ch->io[i] >= 0; i++) {
2781230130Smav			w = hdaa_widget_get(devinfo, ch->io[i]);
2782230130Smav			if (w == NULL || w->enable == 0)
2783230130Smav				continue;
2784230130Smav			ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2785230130Smav			    ch->io[i], 0);
2786230130Smav		}
2787230130Smav	}
2788230451Smav	if (ret == 0xffffffff)
2789230451Smav		ret = 0;
2790230130Smav
2791230451Smav	/*
2792230451Smav	 * Some controls could be shared. Reset volumes for controls
2793230451Smav	 * related to previously chosen devices, as they may no longer
2794230451Smav	 * affect the signal.
2795230451Smav	 */
2796230451Smav	i = 0;
2797230451Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
2798230451Smav		if (ctl->enable == 0 ||
2799230451Smav		    !(ctl->ossmask & pdevinfo->recsrc))
2800230451Smav			continue;
2801230451Smav		if (!((pdevinfo->playas >= 0 &&
2802230451Smav		    ctl->widget->bindas == pdevinfo->playas) ||
2803230451Smav		    (pdevinfo->recas >= 0 &&
2804230451Smav		    ctl->widget->bindas == pdevinfo->recas) ||
2805230451Smav		    (pdevinfo->index == 0 &&
2806230451Smav		    ctl->widget->bindas == -2)))
2807230451Smav			continue;
2808230451Smav		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2809230451Smav			if (pdevinfo->recsrc & (1 << j)) {
2810230451Smav				ctl->devleft[j] = 0;
2811230451Smav				ctl->devright[j] = 0;
2812230451Smav				ctl->devmute[j] = 0;
2813230451Smav			}
2814230451Smav		}
2815230451Smav	}
2816230451Smav
2817230451Smav	/*
2818230451Smav	 * Some controls could be shared. Set volumes for controls
2819230451Smav	 * related to devices selected both previously and now.
2820230451Smav	 */
2821230451Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2822230451Smav		if ((ret | pdevinfo->recsrc) & (1 << j))
2823230451Smav			hdaa_audio_ctl_dev_volume(pdevinfo, j);
2824230451Smav	}
2825230451Smav
2826230451Smav	pdevinfo->recsrc = ret;
2827230130Smav	hdaa_unlock(devinfo);
2828230451Smav	return (ret);
2829230130Smav}
2830230130Smav
2831230130Smavstatic kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = {
2832230130Smav	KOBJMETHOD(mixer_init,		hdaa_audio_ctl_ossmixer_init),
2833230130Smav	KOBJMETHOD(mixer_set,		hdaa_audio_ctl_ossmixer_set),
2834230130Smav	KOBJMETHOD(mixer_setrecsrc,	hdaa_audio_ctl_ossmixer_setrecsrc),
2835230130Smav	KOBJMETHOD_END
2836230130Smav};
2837230130SmavMIXER_DECLARE(hdaa_audio_ctl_ossmixer);
2838230130Smav
2839230130Smavstatic void
2840230130Smavhdaa_dump_gpi(struct hdaa_devinfo *devinfo)
2841230130Smav{
2842230130Smav	device_t dev = devinfo->dev;
2843230130Smav	int i;
2844230130Smav	uint32_t data, wake, unsol, sticky;
2845230130Smav
2846230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) {
2847230130Smav		data = hda_command(dev,
2848230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
2849230130Smav		wake = hda_command(dev,
2850230130Smav		    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid));
2851230130Smav		unsol = hda_command(dev,
2852230130Smav		    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2853230130Smav		sticky = hda_command(dev,
2854230130Smav		    HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid));
2855230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) {
2856230130Smav			device_printf(dev, " GPI%d:%s%s%s state=%d", i,
2857230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2858230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2859230130Smav				    (wake & (1 << i)) ? " wake" : "",
2860230130Smav				    (data >> i) & 1);
2861230130Smav		}
2862230130Smav	}
2863230130Smav}
2864230130Smav
2865230130Smavstatic void
2866230130Smavhdaa_dump_gpio(struct hdaa_devinfo *devinfo)
2867230130Smav{
2868230130Smav	device_t dev = devinfo->dev;
2869230130Smav	int i;
2870230130Smav	uint32_t data, dir, enable, wake, unsol, sticky;
2871230130Smav
2872230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) {
2873230130Smav		data = hda_command(dev,
2874230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
2875230130Smav		enable = hda_command(dev,
2876230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
2877230130Smav		dir = hda_command(dev,
2878230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
2879230130Smav		wake = hda_command(dev,
2880230130Smav		    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid));
2881230130Smav		unsol = hda_command(dev,
2882230130Smav		    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2883230130Smav		sticky = hda_command(dev,
2884230130Smav		    HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid));
2885230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) {
2886230130Smav			device_printf(dev, " GPIO%d: ", i);
2887230130Smav			if ((enable & (1 << i)) == 0) {
2888230130Smav				printf("disabled\n");
2889230130Smav				continue;
2890230130Smav			}
2891230130Smav			if ((dir & (1 << i)) == 0) {
2892230130Smav				printf("input%s%s%s",
2893230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2894230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2895230130Smav				    (wake & (1 << i)) ? " wake" : "");
2896230130Smav			} else
2897230130Smav				printf("output");
2898230130Smav			printf(" state=%d\n", (data >> i) & 1);
2899230130Smav		}
2900230130Smav	}
2901230130Smav}
2902230130Smav
2903230130Smavstatic void
2904230130Smavhdaa_dump_gpo(struct hdaa_devinfo *devinfo)
2905230130Smav{
2906230130Smav	device_t dev = devinfo->dev;
2907230130Smav	int i;
2908230130Smav	uint32_t data;
2909230130Smav
2910230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) {
2911230130Smav		data = hda_command(dev,
2912230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
2913230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) {
2914230130Smav			device_printf(dev, " GPO%d: state=%d", i,
2915230130Smav				    (data >> i) & 1);
2916230130Smav		}
2917230130Smav	}
2918230130Smav}
2919230130Smav
2920230130Smavstatic void
2921230130Smavhdaa_audio_parse(struct hdaa_devinfo *devinfo)
2922230130Smav{
2923230130Smav	struct hdaa_widget *w;
2924230130Smav	uint32_t res;
2925230130Smav	int i;
2926230130Smav	nid_t nid;
2927230130Smav
2928230130Smav	nid = devinfo->nid;
2929230130Smav
2930230130Smav	res = hda_command(devinfo->dev,
2931230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT));
2932230130Smav	devinfo->gpio_cap = res;
2933230130Smav
2934230130Smav	HDA_BOOTVERBOSE(
2935230130Smav		device_printf(devinfo->dev,
2936230130Smav		    "NumGPIO=%d NumGPO=%d "
2937230130Smav		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
2938230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
2939230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
2940230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
2941230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
2942230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
2943230130Smav		hdaa_dump_gpi(devinfo);
2944230130Smav		hdaa_dump_gpio(devinfo);
2945230130Smav		hdaa_dump_gpo(devinfo);
2946230130Smav	);
2947230130Smav
2948230130Smav	res = hda_command(devinfo->dev,
2949230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS));
2950230130Smav	devinfo->supp_stream_formats = res;
2951230130Smav
2952230130Smav	res = hda_command(devinfo->dev,
2953230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE));
2954230130Smav	devinfo->supp_pcm_size_rate = res;
2955230130Smav
2956230130Smav	res = hda_command(devinfo->dev,
2957230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP));
2958230130Smav	devinfo->outamp_cap = res;
2959230130Smav
2960230130Smav	res = hda_command(devinfo->dev,
2961230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP));
2962230130Smav	devinfo->inamp_cap = res;
2963230130Smav
2964230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2965230130Smav		w = hdaa_widget_get(devinfo, i);
2966230130Smav		if (w == NULL)
2967230130Smav			device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i);
2968230130Smav		else {
2969230130Smav			w->devinfo = devinfo;
2970230130Smav			w->nid = i;
2971230130Smav			w->enable = 1;
2972230130Smav			w->selconn = -1;
2973230130Smav			w->pflags = 0;
2974230130Smav			w->ossdev = -1;
2975230130Smav			w->bindas = -1;
2976230130Smav			w->param.eapdbtl = HDA_INVALID;
2977230130Smav			hdaa_widget_parse(w);
2978230130Smav		}
2979230130Smav	}
2980230130Smav}
2981230130Smav
2982230130Smavstatic void
2983230130Smavhdaa_audio_postprocess(struct hdaa_devinfo *devinfo)
2984230130Smav{
2985230130Smav	struct hdaa_widget *w;
2986230130Smav	int i;
2987230130Smav
2988230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2989230130Smav		w = hdaa_widget_get(devinfo, i);
2990230130Smav		if (w == NULL)
2991230130Smav			continue;
2992230130Smav		hdaa_widget_postprocess(w);
2993230130Smav	}
2994230130Smav}
2995230130Smav
2996230130Smavstatic void
2997230130Smavhdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo)
2998230130Smav{
2999230130Smav	struct hdaa_audio_ctl *ctls;
3000230130Smav	struct hdaa_widget *w, *cw;
3001230130Smav	int i, j, cnt, max, ocap, icap;
3002230130Smav	int mute, offset, step, size;
3003230130Smav
3004230130Smav	/* XXX This is redundant */
3005230130Smav	max = 0;
3006230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3007230130Smav		w = hdaa_widget_get(devinfo, i);
3008230130Smav		if (w == NULL || w->enable == 0)
3009230130Smav			continue;
3010230130Smav		if (w->param.outamp_cap != 0)
3011230130Smav			max++;
3012230130Smav		if (w->param.inamp_cap != 0) {
3013230130Smav			switch (w->type) {
3014230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3015230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3016230130Smav				for (j = 0; j < w->nconns; j++) {
3017230130Smav					cw = hdaa_widget_get(devinfo,
3018230130Smav					    w->conns[j]);
3019230130Smav					if (cw == NULL || cw->enable == 0)
3020230130Smav						continue;
3021230130Smav					max++;
3022230130Smav				}
3023230130Smav				break;
3024230130Smav			default:
3025230130Smav				max++;
3026230130Smav				break;
3027230130Smav			}
3028230130Smav		}
3029230130Smav	}
3030230130Smav	devinfo->ctlcnt = max;
3031230130Smav
3032230130Smav	if (max < 1)
3033230130Smav		return;
3034230130Smav
3035230130Smav	ctls = (struct hdaa_audio_ctl *)malloc(
3036230130Smav	    sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT);
3037230130Smav
3038230130Smav	if (ctls == NULL) {
3039230130Smav		/* Blekh! */
3040230130Smav		device_printf(devinfo->dev, "unable to allocate ctls!\n");
3041230130Smav		devinfo->ctlcnt = 0;
3042230130Smav		return;
3043230130Smav	}
3044230130Smav
3045230130Smav	cnt = 0;
3046230130Smav	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
3047230130Smav		if (cnt >= max) {
3048230130Smav			device_printf(devinfo->dev, "%s: Ctl overflow!\n",
3049230130Smav			    __func__);
3050230130Smav			break;
3051230130Smav		}
3052230130Smav		w = hdaa_widget_get(devinfo, i);
3053230130Smav		if (w == NULL || w->enable == 0)
3054230130Smav			continue;
3055230130Smav		ocap = w->param.outamp_cap;
3056230130Smav		icap = w->param.inamp_cap;
3057230130Smav		if (ocap != 0) {
3058230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
3059230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
3060230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
3061230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
3062230130Smav			/*if (offset > step) {
3063230130Smav				HDA_BOOTVERBOSE(
3064230130Smav					device_printf(devinfo->dev,
3065230130Smav					    "BUGGY outamp: nid=%d "
3066230130Smav					    "[offset=%d > step=%d]\n",
3067230130Smav					    w->nid, offset, step);
3068230130Smav				);
3069230130Smav				offset = step;
3070230130Smav			}*/
3071230130Smav			ctls[cnt].enable = 1;
3072230130Smav			ctls[cnt].widget = w;
3073230130Smav			ctls[cnt].mute = mute;
3074230130Smav			ctls[cnt].step = step;
3075230130Smav			ctls[cnt].size = size;
3076230130Smav			ctls[cnt].offset = offset;
3077230130Smav			ctls[cnt].left = offset;
3078230130Smav			ctls[cnt].right = offset;
3079230130Smav			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3080230130Smav			    w->waspin)
3081230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
3082230130Smav			else
3083230130Smav				ctls[cnt].ndir = HDAA_CTL_OUT;
3084230130Smav			ctls[cnt++].dir = HDAA_CTL_OUT;
3085230130Smav		}
3086230130Smav
3087230130Smav		if (icap != 0) {
3088230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
3089230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
3090230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
3091230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
3092230130Smav			/*if (offset > step) {
3093230130Smav				HDA_BOOTVERBOSE(
3094230130Smav					device_printf(devinfo->dev,
3095230130Smav					    "BUGGY inamp: nid=%d "
3096230130Smav					    "[offset=%d > step=%d]\n",
3097230130Smav					    w->nid, offset, step);
3098230130Smav				);
3099230130Smav				offset = step;
3100230130Smav			}*/
3101230130Smav			switch (w->type) {
3102230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3103230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3104230130Smav				for (j = 0; j < w->nconns; j++) {
3105230130Smav					if (cnt >= max) {
3106230130Smav						device_printf(devinfo->dev,
3107230130Smav						    "%s: Ctl overflow!\n",
3108230130Smav						    __func__);
3109230130Smav						break;
3110230130Smav					}
3111230130Smav					cw = hdaa_widget_get(devinfo,
3112230130Smav					    w->conns[j]);
3113230130Smav					if (cw == NULL || cw->enable == 0)
3114230130Smav						continue;
3115230130Smav					ctls[cnt].enable = 1;
3116230130Smav					ctls[cnt].widget = w;
3117230130Smav					ctls[cnt].childwidget = cw;
3118230130Smav					ctls[cnt].index = j;
3119230130Smav					ctls[cnt].mute = mute;
3120230130Smav					ctls[cnt].step = step;
3121230130Smav					ctls[cnt].size = size;
3122230130Smav					ctls[cnt].offset = offset;
3123230130Smav					ctls[cnt].left = offset;
3124230130Smav					ctls[cnt].right = offset;
3125230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
3126230130Smav					ctls[cnt++].dir = HDAA_CTL_IN;
3127230130Smav				}
3128230130Smav				break;
3129230130Smav			default:
3130230130Smav				if (cnt >= max) {
3131230130Smav					device_printf(devinfo->dev,
3132230130Smav					    "%s: Ctl overflow!\n",
3133230130Smav					    __func__);
3134230130Smav					break;
3135230130Smav				}
3136230130Smav				ctls[cnt].enable = 1;
3137230130Smav				ctls[cnt].widget = w;
3138230130Smav				ctls[cnt].mute = mute;
3139230130Smav				ctls[cnt].step = step;
3140230130Smav				ctls[cnt].size = size;
3141230130Smav				ctls[cnt].offset = offset;
3142230130Smav				ctls[cnt].left = offset;
3143230130Smav				ctls[cnt].right = offset;
3144230130Smav				if (w->type ==
3145230130Smav				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3146230130Smav					ctls[cnt].ndir = HDAA_CTL_OUT;
3147230130Smav				else
3148230130Smav					ctls[cnt].ndir = HDAA_CTL_IN;
3149230130Smav				ctls[cnt++].dir = HDAA_CTL_IN;
3150230130Smav				break;
3151230130Smav			}
3152230130Smav		}
3153230130Smav	}
3154230130Smav
3155230130Smav	devinfo->ctl = ctls;
3156230130Smav}
3157230130Smav
3158230130Smavstatic void
3159230130Smavhdaa_audio_as_parse(struct hdaa_devinfo *devinfo)
3160230130Smav{
3161230130Smav	struct hdaa_audio_as *as;
3162230130Smav	struct hdaa_widget *w;
3163230130Smav	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
3164230130Smav
3165230130Smav	/* Count present associations */
3166230130Smav	max = 0;
3167230130Smav	for (j = 1; j < 16; j++) {
3168230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3169230130Smav			w = hdaa_widget_get(devinfo, i);
3170230130Smav			if (w == NULL || w->enable == 0)
3171230130Smav				continue;
3172230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3173230130Smav				continue;
3174230130Smav			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
3175230130Smav			    != j)
3176230130Smav				continue;
3177230130Smav			max++;
3178230130Smav			if (j != 15)  /* There could be many 1-pin assocs #15 */
3179230130Smav				break;
3180230130Smav		}
3181230130Smav	}
3182230130Smav
3183230130Smav	devinfo->ascnt = max;
3184230130Smav
3185230130Smav	if (max < 1)
3186230130Smav		return;
3187230130Smav
3188230130Smav	as = (struct hdaa_audio_as *)malloc(
3189230130Smav	    sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT);
3190230130Smav
3191230130Smav	if (as == NULL) {
3192230130Smav		/* Blekh! */
3193230130Smav		device_printf(devinfo->dev, "unable to allocate assocs!\n");
3194230130Smav		devinfo->ascnt = 0;
3195230130Smav		return;
3196230130Smav	}
3197230130Smav
3198230130Smav	for (i = 0; i < max; i++) {
3199230130Smav		as[i].hpredir = -1;
3200230130Smav		as[i].digital = 0;
3201230130Smav		as[i].num_chans = 1;
3202230130Smav		as[i].location = -1;
3203230130Smav	}
3204230130Smav
3205230130Smav	/* Scan associations skipping as=0. */
3206230130Smav	cnt = 0;
3207283064Shselasky	for (j = 1; j < 16 && cnt < max; j++) {
3208230130Smav		first = 16;
3209230130Smav		hpredir = 0;
3210230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3211230130Smav			w = hdaa_widget_get(devinfo, i);
3212230130Smav			if (w == NULL || w->enable == 0)
3213230130Smav				continue;
3214230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3215230130Smav				continue;
3216230130Smav			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
3217230130Smav			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
3218230130Smav			if (assoc != j) {
3219230130Smav				continue;
3220230130Smav			}
3221230130Smav			KASSERT(cnt < max,
3222230130Smav			    ("%s: Associations owerflow (%d of %d)",
3223230130Smav			    __func__, cnt, max));
3224230130Smav			type = w->wclass.pin.config &
3225230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
3226230130Smav			/* Get pin direction. */
3227230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
3228230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
3229230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
3230230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
3231230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
3232230130Smav				dir = HDAA_CTL_OUT;
3233230130Smav			else
3234230130Smav				dir = HDAA_CTL_IN;
3235230130Smav			/* If this is a first pin - create new association. */
3236230130Smav			if (as[cnt].pincnt == 0) {
3237230130Smav				as[cnt].enable = 1;
3238230130Smav				as[cnt].index = j;
3239230130Smav				as[cnt].dir = dir;
3240230130Smav			}
3241230130Smav			if (seq < first)
3242230130Smav				first = seq;
3243230130Smav			/* Check association correctness. */
3244230130Smav			if (as[cnt].pins[seq] != 0) {
3245230130Smav				device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) "
3246230130Smav				    "in association %d! Disabling association.\n",
3247230130Smav				    __func__, seq, w->nid, j);
3248230130Smav				as[cnt].enable = 0;
3249230130Smav			}
3250230130Smav			if (dir != as[cnt].dir) {
3251230130Smav				device_printf(devinfo->dev, "%s: Pin %d has wrong "
3252230130Smav				    "direction for association %d! Disabling "
3253230130Smav				    "association.\n",
3254230130Smav				    __func__, w->nid, j);
3255230130Smav				as[cnt].enable = 0;
3256230130Smav			}
3257230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
3258230507Smav				as[cnt].digital |= 0x1;
3259230507Smav				if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
3260230507Smav					as[cnt].digital |= 0x2;
3261230130Smav				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
3262230507Smav					as[cnt].digital |= 0x4;
3263230130Smav			}
3264230130Smav			if (as[cnt].location == -1) {
3265230130Smav				as[cnt].location =
3266230130Smav				    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config);
3267230130Smav			} else if (as[cnt].location !=
3268230130Smav			    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) {
3269230130Smav				as[cnt].location = -2;
3270230130Smav			}
3271230130Smav			/* Headphones with seq=15 may mean redirection. */
3272230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
3273230130Smav			    seq == 15)
3274230130Smav				hpredir = 1;
3275230130Smav			as[cnt].pins[seq] = w->nid;
3276230130Smav			as[cnt].pincnt++;
3277230130Smav			/* Association 15 is a multiple unassociated pins. */
3278230130Smav			if (j == 15)
3279230130Smav				cnt++;
3280230130Smav		}
3281230130Smav		if (j != 15 && as[cnt].pincnt > 0) {
3282230130Smav			if (hpredir && as[cnt].pincnt > 1)
3283230130Smav				as[cnt].hpredir = first;
3284230130Smav			cnt++;
3285230130Smav		}
3286230130Smav	}
3287230130Smav	for (i = 0; i < max; i++) {
3288230130Smav		if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 ||
3289230130Smav		    as[i].pins[14] > 0 || as[i].pins[15] > 0))
3290230130Smav			as[i].mixed = 1;
3291230130Smav	}
3292230130Smav	HDA_BOOTVERBOSE(
3293230130Smav		device_printf(devinfo->dev,
3294230130Smav		    "%d associations found:\n", max);
3295230130Smav		for (i = 0; i < max; i++) {
3296230130Smav			device_printf(devinfo->dev,
3297230130Smav			    "Association %d (%d) %s%s:\n",
3298230130Smav			    i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out",
3299230130Smav			    as[i].enable?"":" (disabled)");
3300230130Smav			for (j = 0; j < 16; j++) {
3301230130Smav				if (as[i].pins[j] == 0)
3302230130Smav					continue;
3303230130Smav				device_printf(devinfo->dev,
3304230130Smav				    " Pin nid=%d seq=%d\n",
3305230130Smav				    as[i].pins[j], j);
3306230130Smav			}
3307230130Smav		}
3308230130Smav	);
3309230130Smav
3310230130Smav	devinfo->as = as;
3311230130Smav}
3312230130Smav
3313230130Smav/*
3314230130Smav * Trace path from DAC to pin.
3315230130Smav */
3316230130Smavstatic nid_t
3317230130Smavhdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3318230130Smav    int dupseq, int min, int only, int depth)
3319230130Smav{
3320230130Smav	struct hdaa_widget *w;
3321230130Smav	int i, im = -1;
3322230130Smav	nid_t m = 0, ret;
3323230130Smav
3324230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3325230130Smav		return (0);
3326230130Smav	w = hdaa_widget_get(devinfo, nid);
3327230130Smav	if (w == NULL || w->enable == 0)
3328230130Smav		return (0);
3329230130Smav	HDA_BOOTHVERBOSE(
3330230130Smav		if (!only) {
3331230130Smav			device_printf(devinfo->dev,
3332230130Smav			    " %*stracing via nid %d\n",
3333230130Smav				depth + 1, "", w->nid);
3334230130Smav		}
3335230130Smav	);
3336230130Smav	/* Use only unused widgets */
3337230130Smav	if (w->bindas >= 0 && w->bindas != as) {
3338230130Smav		HDA_BOOTHVERBOSE(
3339230130Smav			if (!only) {
3340230130Smav				device_printf(devinfo->dev,
3341230130Smav				    " %*snid %d busy by association %d\n",
3342230130Smav					depth + 1, "", w->nid, w->bindas);
3343230130Smav			}
3344230130Smav		);
3345230130Smav		return (0);
3346230130Smav	}
3347230130Smav	if (dupseq < 0) {
3348230130Smav		if (w->bindseqmask != 0) {
3349230130Smav			HDA_BOOTHVERBOSE(
3350230130Smav				if (!only) {
3351230130Smav					device_printf(devinfo->dev,
3352230130Smav					    " %*snid %d busy by seqmask %x\n",
3353230130Smav						depth + 1, "", w->nid, w->bindseqmask);
3354230130Smav				}
3355230130Smav			);
3356230130Smav			return (0);
3357230130Smav		}
3358230130Smav	} else {
3359230130Smav		/* If this is headphones - allow duplicate first pin. */
3360230130Smav		if (w->bindseqmask != 0 &&
3361230130Smav		    (w->bindseqmask & (1 << dupseq)) == 0) {
3362230130Smav			HDA_BOOTHVERBOSE(
3363230130Smav				device_printf(devinfo->dev,
3364230130Smav				    " %*snid %d busy by seqmask %x\n",
3365230130Smav					depth + 1, "", w->nid, w->bindseqmask);
3366230130Smav			);
3367230130Smav			return (0);
3368230130Smav		}
3369230130Smav	}
3370230130Smav
3371230130Smav	switch (w->type) {
3372230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3373230130Smav		/* Do not traverse input. AD1988 has digital monitor
3374230130Smav		for which we are not ready. */
3375230130Smav		break;
3376230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
3377230130Smav		/* If we are tracing HP take only dac of first pin. */
3378230130Smav		if ((only == 0 || only == w->nid) &&
3379230130Smav		    (w->nid >= min) && (dupseq < 0 || w->nid ==
3380230130Smav		    devinfo->as[as].dacs[0][dupseq]))
3381230130Smav			m = w->nid;
3382230130Smav		break;
3383230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3384230130Smav		if (depth > 0)
3385230130Smav			break;
3386230130Smav		/* Fall */
3387230130Smav	default:
3388230130Smav		/* Find reachable DACs with smallest nid respecting constraints. */
3389230130Smav		for (i = 0; i < w->nconns; i++) {
3390230130Smav			if (w->connsenable[i] == 0)
3391230130Smav				continue;
3392230130Smav			if (w->selconn != -1 && w->selconn != i)
3393230130Smav				continue;
3394230130Smav			if ((ret = hdaa_audio_trace_dac(devinfo, as, seq,
3395230130Smav			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
3396230130Smav				if (m == 0 || ret < m) {
3397230130Smav					m = ret;
3398230130Smav					im = i;
3399230130Smav				}
3400230130Smav				if (only || dupseq >= 0)
3401230130Smav					break;
3402230130Smav			}
3403230130Smav		}
3404230130Smav		if (im >= 0 && only && ((w->nconns > 1 &&
3405230130Smav		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3406230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3407230130Smav			w->selconn = im;
3408230130Smav		break;
3409230130Smav	}
3410230130Smav	if (m && only) {
3411230130Smav		w->bindas = as;
3412230130Smav		w->bindseqmask |= (1 << seq);
3413230130Smav	}
3414230130Smav	HDA_BOOTHVERBOSE(
3415230130Smav		if (!only) {
3416230130Smav			device_printf(devinfo->dev,
3417230130Smav			    " %*snid %d returned %d\n",
3418230130Smav				depth + 1, "", w->nid, m);
3419230130Smav		}
3420230130Smav	);
3421230130Smav	return (m);
3422230130Smav}
3423230130Smav
3424230130Smav/*
3425230130Smav * Trace path from widget to ADC.
3426230130Smav */
3427230130Smavstatic nid_t
3428230130Smavhdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3429230130Smav    int mixed, int min, int only, int depth, int *length, int onlylength)
3430230130Smav{
3431230130Smav	struct hdaa_widget *w, *wc;
3432230130Smav	int i, j, im, lm = HDA_PARSE_MAXDEPTH;
3433230130Smav	nid_t m = 0, ret;
3434230130Smav
3435230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3436230130Smav		return (0);
3437230130Smav	w = hdaa_widget_get(devinfo, nid);
3438230130Smav	if (w == NULL || w->enable == 0)
3439230130Smav		return (0);
3440230130Smav	HDA_BOOTHVERBOSE(
3441230130Smav		device_printf(devinfo->dev,
3442230130Smav		    " %*stracing via nid %d\n",
3443230130Smav			depth + 1, "", w->nid);
3444230130Smav	);
3445230130Smav	/* Use only unused widgets */
3446230130Smav	if (w->bindas >= 0 && w->bindas != as) {
3447230130Smav		HDA_BOOTHVERBOSE(
3448230130Smav			device_printf(devinfo->dev,
3449230130Smav			    " %*snid %d busy by association %d\n",
3450230130Smav				depth + 1, "", w->nid, w->bindas);
3451230130Smav		);
3452230130Smav		return (0);
3453230130Smav	}
3454230130Smav	if (!mixed && w->bindseqmask != 0) {
3455230130Smav		HDA_BOOTHVERBOSE(
3456230130Smav			device_printf(devinfo->dev,
3457230130Smav			    " %*snid %d busy by seqmask %x\n",
3458230130Smav				depth + 1, "", w->nid, w->bindseqmask);
3459230130Smav		);
3460230130Smav		return (0);
3461230130Smav	}
3462230130Smav	switch (w->type) {
3463230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3464230130Smav		if ((only == 0 || only == w->nid) && (w->nid >= min) &&
3465230130Smav		    (onlylength == 0 || onlylength == depth)) {
3466230130Smav			m = w->nid;
3467239254Smav			*length = depth;
3468230130Smav		}
3469230130Smav		break;
3470230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3471230130Smav		if (depth > 0)
3472230130Smav			break;
3473230130Smav		/* Fall */
3474230130Smav	default:
3475230130Smav		/* Try to find reachable ADCs with specified nid. */
3476230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3477230130Smav			wc = hdaa_widget_get(devinfo, j);
3478230130Smav			if (wc == NULL || wc->enable == 0)
3479230130Smav				continue;
3480230130Smav			im = -1;
3481230130Smav			for (i = 0; i < wc->nconns; i++) {
3482230130Smav				if (wc->connsenable[i] == 0)
3483230130Smav					continue;
3484230130Smav				if (wc->conns[i] != nid)
3485230130Smav					continue;
3486230130Smav				if ((ret = hdaa_audio_trace_adc(devinfo, as, seq,
3487230130Smav				    j, mixed, min, only, depth + 1,
3488230130Smav				    length, onlylength)) != 0) {
3489230130Smav					if (m == 0 || ret < m ||
3490239254Smav					    (ret == m && *length < lm)) {
3491230130Smav						m = ret;
3492230130Smav						im = i;
3493230130Smav						lm = *length;
3494239254Smav					} else
3495239254Smav						*length = lm;
3496230130Smav					if (only)
3497230130Smav						break;
3498230130Smav				}
3499230130Smav			}
3500230130Smav			if (im >= 0 && only && ((wc->nconns > 1 &&
3501230130Smav			    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3502230130Smav			    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3503230130Smav				wc->selconn = im;
3504230130Smav		}
3505230130Smav		break;
3506230130Smav	}
3507230130Smav	if (m && only) {
3508230130Smav		w->bindas = as;
3509230130Smav		w->bindseqmask |= (1 << seq);
3510230130Smav	}
3511230130Smav	HDA_BOOTHVERBOSE(
3512230130Smav		device_printf(devinfo->dev,
3513230130Smav		    " %*snid %d returned %d\n",
3514230130Smav			depth + 1, "", w->nid, m);
3515230130Smav	);
3516230130Smav	return (m);
3517230130Smav}
3518230130Smav
3519230130Smav/*
3520230130Smav * Erase trace path of the specified association.
3521230130Smav */
3522230130Smavstatic void
3523230130Smavhdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq)
3524230130Smav{
3525230130Smav	struct hdaa_widget *w;
3526230130Smav	int i;
3527230130Smav
3528230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3529230130Smav		w = hdaa_widget_get(devinfo, i);
3530230130Smav		if (w == NULL || w->enable == 0)
3531230130Smav			continue;
3532230130Smav		if (w->bindas == as) {
3533230130Smav			if (seq >= 0) {
3534230130Smav				w->bindseqmask &= ~(1 << seq);
3535230130Smav				if (w->bindseqmask == 0) {
3536230130Smav					w->bindas = -1;
3537230130Smav					w->selconn = -1;
3538230130Smav				}
3539230130Smav			} else {
3540230130Smav				w->bindas = -1;
3541230130Smav				w->bindseqmask = 0;
3542230130Smav				w->selconn = -1;
3543230130Smav			}
3544230130Smav		}
3545230130Smav	}
3546230130Smav}
3547230130Smav
3548230130Smav/*
3549230130Smav * Trace association path from DAC to output
3550230130Smav */
3551230130Smavstatic int
3552230130Smavhdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq)
3553230130Smav{
3554230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3555230130Smav	int i, hpredir;
3556230130Smav	nid_t min, res;
3557230130Smav
3558230130Smav	/* Find next pin */
3559230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3560230130Smav		;
3561230130Smav	/* Check if there is no any left. If so - we succeeded. */
3562230130Smav	if (i == 16)
3563230130Smav		return (1);
3564230130Smav
3565230130Smav	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
3566230130Smav	min = 0;
3567230130Smav	do {
3568230130Smav		HDA_BOOTHVERBOSE(
3569230130Smav			device_printf(devinfo->dev,
3570230130Smav			    " Tracing pin %d with min nid %d",
3571230130Smav			    ases[as].pins[i], min);
3572230130Smav			if (hpredir >= 0)
3573230130Smav				printf(" and hpredir %d", hpredir);
3574230130Smav			printf("\n");
3575230130Smav		);
3576230130Smav		/* Trace this pin taking min nid into account. */
3577230130Smav		res = hdaa_audio_trace_dac(devinfo, as, i,
3578230130Smav		    ases[as].pins[i], hpredir, min, 0, 0);
3579230130Smav		if (res == 0) {
3580230130Smav			/* If we failed - return to previous and redo it. */
3581230130Smav			HDA_BOOTVERBOSE(
3582230130Smav				device_printf(devinfo->dev,
3583230130Smav				    " Unable to trace pin %d seq %d with min "
3584230130Smav				    "nid %d",
3585230130Smav				    ases[as].pins[i], i, min);
3586230130Smav				if (hpredir >= 0)
3587230130Smav					printf(" and hpredir %d", hpredir);
3588230130Smav				printf("\n");
3589230130Smav			);
3590230130Smav			return (0);
3591230130Smav		}
3592230130Smav		HDA_BOOTVERBOSE(
3593230130Smav			device_printf(devinfo->dev,
3594230130Smav			    " Pin %d traced to DAC %d",
3595230130Smav			    ases[as].pins[i], res);
3596230130Smav			if (hpredir >= 0)
3597230130Smav				printf(" and hpredir %d", hpredir);
3598230130Smav			if (ases[as].fakeredir)
3599230130Smav				printf(" with fake redirection");
3600230130Smav			printf("\n");
3601230130Smav		);
3602230130Smav		/* Trace again to mark the path */
3603230130Smav		hdaa_audio_trace_dac(devinfo, as, i,
3604230130Smav		    ases[as].pins[i], hpredir, min, res, 0);
3605230130Smav		ases[as].dacs[0][i] = res;
3606230130Smav		/* We succeeded, so call next. */
3607230130Smav		if (hdaa_audio_trace_as_out(devinfo, as, i + 1))
3608230130Smav			return (1);
3609230130Smav		/* If next failed, we should retry with next min */
3610230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3611230130Smav		ases[as].dacs[0][i] = 0;
3612230130Smav		min = res + 1;
3613230130Smav	} while (1);
3614230130Smav}
3615230130Smav
3616230130Smav/*
3617230130Smav * Check equivalency of two DACs.
3618230130Smav */
3619230130Smavstatic int
3620230130Smavhdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3621230130Smav{
3622230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3623230130Smav	struct hdaa_widget *w3;
3624230130Smav	int i, j, c1, c2;
3625230130Smav
3626230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3627230130Smav		return (0);
3628230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3629230130Smav		w3 = hdaa_widget_get(devinfo, i);
3630230130Smav		if (w3 == NULL || w3->enable == 0)
3631230130Smav			continue;
3632230130Smav		if (w3->bindas != w1->bindas)
3633230130Smav			continue;
3634230130Smav		if (w3->nconns == 0)
3635230130Smav			continue;
3636230130Smav		c1 = c2 = -1;
3637230130Smav		for (j = 0; j < w3->nconns; j++) {
3638230130Smav			if (w3->connsenable[j] == 0)
3639230130Smav				continue;
3640230130Smav			if (w3->conns[j] == w1->nid)
3641230130Smav				c1 = j;
3642230130Smav			if (w3->conns[j] == w2->nid)
3643230130Smav				c2 = j;
3644230130Smav		}
3645230130Smav		if (c1 < 0)
3646230130Smav			continue;
3647230130Smav		if (c2 < 0)
3648230130Smav			return (0);
3649230130Smav		if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3650230130Smav			return (0);
3651230130Smav	}
3652230130Smav	return (1);
3653230130Smav}
3654230130Smav
3655230130Smav/*
3656230130Smav * Check equivalency of two ADCs.
3657230130Smav */
3658230130Smavstatic int
3659230130Smavhdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3660230130Smav{
3661230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3662230130Smav	struct hdaa_widget *w3, *w4;
3663230130Smav	int i;
3664230130Smav
3665230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3666230130Smav		return (0);
3667230130Smav	if (w1->nconns != 1 || w2->nconns != 1)
3668230130Smav		return (0);
3669230130Smav	if (w1->conns[0] == w2->conns[0])
3670230130Smav		return (1);
3671230130Smav	w3 = hdaa_widget_get(devinfo, w1->conns[0]);
3672230130Smav	if (w3 == NULL || w3->enable == 0)
3673230130Smav		return (0);
3674230130Smav	w4 = hdaa_widget_get(devinfo, w2->conns[0]);
3675230130Smav	if (w4 == NULL || w4->enable == 0)
3676230130Smav		return (0);
3677230130Smav	if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask)
3678230130Smav		return (1);
3679230130Smav	if (w4->bindas >= 0)
3680230130Smav		return (0);
3681230130Smav	if (w3->type != w4->type)
3682230130Smav		return (0);
3683230130Smav	if (memcmp(&w3->param, &w4->param, sizeof(w3->param)))
3684230130Smav		return (0);
3685230130Smav	if (w3->nconns != w4->nconns)
3686230130Smav		return (0);
3687230130Smav	for (i = 0; i < w3->nconns; i++) {
3688230130Smav		if (w3->conns[i] != w4->conns[i])
3689230130Smav			return (0);
3690230130Smav	}
3691230130Smav	return (1);
3692230130Smav}
3693230130Smav
3694230130Smav/*
3695230130Smav * Look for equivalent DAC/ADC to implement second channel.
3696230130Smav */
3697230130Smavstatic void
3698230130Smavhdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid)
3699230130Smav{
3700230130Smav	struct hdaa_audio_as *as = &devinfo->as[asid];
3701230130Smav	struct hdaa_widget *w1, *w2;
3702230130Smav	int i, pos;
3703230130Smav	nid_t nid1, nid2;
3704230130Smav
3705230130Smav	HDA_BOOTVERBOSE(
3706230130Smav		device_printf(devinfo->dev,
3707230130Smav		    "Looking for additional %sC "
3708230130Smav		    "for association %d (%d)\n",
3709230130Smav		    (as->dir == HDAA_CTL_OUT) ? "DA" : "AD",
3710230130Smav		    asid, as->index);
3711230130Smav	);
3712230130Smav
3713230130Smav	/* Find the exisitng DAC position and return if found more the one. */
3714230130Smav	pos = -1;
3715230130Smav	for (i = 0; i < 16; i++) {
3716230130Smav		if (as->dacs[0][i] <= 0)
3717230130Smav			continue;
3718230130Smav		if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos])
3719230130Smav			return;
3720230130Smav		pos = i;
3721230130Smav	}
3722230130Smav
3723230130Smav	nid1 = as->dacs[0][pos];
3724230130Smav	w1 = hdaa_widget_get(devinfo, nid1);
3725230130Smav	w2 = NULL;
3726230130Smav	for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) {
3727230130Smav		w2 = hdaa_widget_get(devinfo, nid2);
3728230130Smav		if (w2 == NULL || w2->enable == 0)
3729230130Smav			continue;
3730230130Smav		if (w2->bindas >= 0)
3731230130Smav			continue;
3732230130Smav		if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
3733230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
3734230130Smav				continue;
3735230130Smav			if (hdaa_audio_dacs_equal(w1, w2))
3736230130Smav				break;
3737230130Smav		} else {
3738230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3739230130Smav				continue;
3740230130Smav			if (hdaa_audio_adcs_equal(w1, w2))
3741230130Smav				break;
3742230130Smav		}
3743230130Smav	}
3744230130Smav	if (nid2 >= devinfo->endnode)
3745230130Smav		return;
3746230130Smav	w2->bindas = w1->bindas;
3747230130Smav	w2->bindseqmask = w1->bindseqmask;
3748230130Smav	if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
3749230130Smav		HDA_BOOTVERBOSE(
3750230130Smav			device_printf(devinfo->dev,
3751230130Smav			    " ADC %d considered equal to ADC %d\n", nid2, nid1);
3752230130Smav		);
3753230130Smav		w1 = hdaa_widget_get(devinfo, w1->conns[0]);
3754230130Smav		w2 = hdaa_widget_get(devinfo, w2->conns[0]);
3755230130Smav		w2->bindas = w1->bindas;
3756230130Smav		w2->bindseqmask = w1->bindseqmask;
3757230130Smav	} else {
3758230130Smav		HDA_BOOTVERBOSE(
3759230130Smav			device_printf(devinfo->dev,
3760230130Smav			    " DAC %d considered equal to DAC %d\n", nid2, nid1);
3761230130Smav		);
3762230130Smav	}
3763230130Smav	for (i = 0; i < 16; i++) {
3764230130Smav		if (as->dacs[0][i] <= 0)
3765230130Smav			continue;
3766230130Smav		as->dacs[as->num_chans][i] = nid2;
3767230130Smav	}
3768230130Smav	as->num_chans++;
3769230130Smav}
3770230130Smav
3771230130Smav/*
3772230130Smav * Trace association path from input to ADC
3773230130Smav */
3774230130Smavstatic int
3775230130Smavhdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as)
3776230130Smav{
3777230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3778230130Smav	struct hdaa_widget *w;
3779230130Smav	int i, j, k, length;
3780230130Smav
3781230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3782230130Smav		w = hdaa_widget_get(devinfo, j);
3783230130Smav		if (w == NULL || w->enable == 0)
3784230130Smav			continue;
3785230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3786230130Smav			continue;
3787230130Smav		if (w->bindas >= 0 && w->bindas != as)
3788230130Smav			continue;
3789230130Smav
3790230130Smav		/* Find next pin */
3791230130Smav		for (i = 0; i < 16; i++) {
3792230130Smav			if (ases[as].pins[i] == 0)
3793230130Smav				continue;
3794230130Smav
3795230130Smav			HDA_BOOTHVERBOSE(
3796230130Smav				device_printf(devinfo->dev,
3797230130Smav				    " Tracing pin %d to ADC %d\n",
3798230130Smav				    ases[as].pins[i], j);
3799230130Smav			);
3800230130Smav			/* Trace this pin taking goal into account. */
3801230130Smav			if (hdaa_audio_trace_adc(devinfo, as, i,
3802230130Smav			    ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) {
3803230130Smav				/* If we failed - return to previous and redo it. */
3804230130Smav				HDA_BOOTVERBOSE(
3805230130Smav					device_printf(devinfo->dev,
3806230130Smav					    " Unable to trace pin %d to ADC %d, undo traces\n",
3807230130Smav					    ases[as].pins[i], j);
3808230130Smav				);
3809230130Smav				hdaa_audio_undo_trace(devinfo, as, -1);
3810230130Smav				for (k = 0; k < 16; k++)
3811230130Smav					ases[as].dacs[0][k] = 0;
3812230130Smav				break;
3813230130Smav			}
3814230130Smav			HDA_BOOTVERBOSE(
3815230130Smav				device_printf(devinfo->dev,
3816230130Smav				    " Pin %d traced to ADC %d\n",
3817230130Smav				    ases[as].pins[i], j);
3818230130Smav			);
3819230130Smav			ases[as].dacs[0][i] = j;
3820230130Smav		}
3821230130Smav		if (i == 16)
3822230130Smav			return (1);
3823230130Smav	}
3824230130Smav	return (0);
3825230130Smav}
3826230130Smav
3827230130Smav/*
3828230130Smav * Trace association path from input to multiple ADCs
3829230130Smav */
3830230130Smavstatic int
3831230130Smavhdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq)
3832230130Smav{
3833230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3834230130Smav	int i, length;
3835230130Smav	nid_t min, res;
3836230130Smav
3837230130Smav	/* Find next pin */
3838230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3839230130Smav		;
3840230130Smav	/* Check if there is no any left. If so - we succeeded. */
3841230130Smav	if (i == 16)
3842230130Smav		return (1);
3843230130Smav
3844230130Smav	min = 0;
3845230130Smav	do {
3846230130Smav		HDA_BOOTHVERBOSE(
3847230130Smav			device_printf(devinfo->dev,
3848230130Smav			    " Tracing pin %d with min nid %d",
3849230130Smav			    ases[as].pins[i], min);
3850230130Smav			printf("\n");
3851230130Smav		);
3852230130Smav		/* Trace this pin taking min nid into account. */
3853230130Smav		res = hdaa_audio_trace_adc(devinfo, as, i,
3854230130Smav		    ases[as].pins[i], 0, min, 0, 0, &length, 0);
3855230130Smav		if (res == 0) {
3856230130Smav			/* If we failed - return to previous and redo it. */
3857230130Smav			HDA_BOOTVERBOSE(
3858230130Smav				device_printf(devinfo->dev,
3859230130Smav				    " Unable to trace pin %d seq %d with min "
3860230130Smav				    "nid %d",
3861230130Smav				    ases[as].pins[i], i, min);
3862230130Smav				printf("\n");
3863230130Smav			);
3864230130Smav			return (0);
3865230130Smav		}
3866230130Smav		HDA_BOOTVERBOSE(
3867230130Smav			device_printf(devinfo->dev,
3868230130Smav			    " Pin %d traced to ADC %d\n",
3869230130Smav			    ases[as].pins[i], res);
3870230130Smav		);
3871230130Smav		/* Trace again to mark the path */
3872230130Smav		hdaa_audio_trace_adc(devinfo, as, i,
3873230130Smav		    ases[as].pins[i], 0, min, res, 0, &length, length);
3874230130Smav		ases[as].dacs[0][i] = res;
3875230130Smav		/* We succeeded, so call next. */
3876230130Smav		if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1))
3877230130Smav			return (1);
3878230130Smav		/* If next failed, we should retry with next min */
3879230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3880230130Smav		ases[as].dacs[0][i] = 0;
3881230130Smav		min = res + 1;
3882230130Smav	} while (1);
3883230130Smav}
3884230130Smav
3885230130Smav/*
3886230130Smav * Trace input monitor path from mixer to output association.
3887230130Smav */
3888230130Smavstatic int
3889230130Smavhdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth)
3890230130Smav{
3891230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3892230130Smav	struct hdaa_widget *w, *wc;
3893230130Smav	int i, j;
3894230130Smav	nid_t res = 0;
3895230130Smav
3896230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3897230130Smav		return (0);
3898230130Smav	w = hdaa_widget_get(devinfo, nid);
3899230130Smav	if (w == NULL || w->enable == 0)
3900230130Smav		return (0);
3901230130Smav	HDA_BOOTHVERBOSE(
3902230130Smav		device_printf(devinfo->dev,
3903230130Smav		    " %*stracing via nid %d\n",
3904230130Smav			depth + 1, "", w->nid);
3905230130Smav	);
3906230130Smav	/* Use only unused widgets */
3907230130Smav	if (depth > 0 && w->bindas != -1) {
3908230130Smav		if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) {
3909230130Smav			HDA_BOOTHVERBOSE(
3910230130Smav				device_printf(devinfo->dev,
3911230130Smav				    " %*snid %d found output association %d\n",
3912230130Smav					depth + 1, "", w->nid, w->bindas);
3913230130Smav			);
3914230130Smav			if (w->bindas >= 0)
3915230130Smav				w->pflags |= HDAA_ADC_MONITOR;
3916230130Smav			return (1);
3917230130Smav		} else {
3918230130Smav			HDA_BOOTHVERBOSE(
3919230130Smav				device_printf(devinfo->dev,
3920230130Smav				    " %*snid %d busy by input association %d\n",
3921230130Smav					depth + 1, "", w->nid, w->bindas);
3922230130Smav			);
3923230130Smav			return (0);
3924230130Smav		}
3925230130Smav	}
3926230130Smav
3927230130Smav	switch (w->type) {
3928230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3929230130Smav		/* Do not traverse input. AD1988 has digital monitor
3930230130Smav		for which we are not ready. */
3931230130Smav		break;
3932230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3933230130Smav		if (depth > 0)
3934230130Smav			break;
3935230130Smav		/* Fall */
3936230130Smav	default:
3937230130Smav		/* Try to find reachable ADCs with specified nid. */
3938230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3939230130Smav			wc = hdaa_widget_get(devinfo, j);
3940230130Smav			if (wc == NULL || wc->enable == 0)
3941230130Smav				continue;
3942230130Smav			for (i = 0; i < wc->nconns; i++) {
3943230130Smav				if (wc->connsenable[i] == 0)
3944230130Smav					continue;
3945230130Smav				if (wc->conns[i] != nid)
3946230130Smav					continue;
3947230130Smav				if (hdaa_audio_trace_to_out(devinfo,
3948230130Smav				    j, depth + 1) != 0) {
3949230130Smav					res = 1;
3950230130Smav					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3951230130Smav					    wc->selconn == -1)
3952230130Smav						wc->selconn = i;
3953230130Smav				}
3954230130Smav			}
3955230130Smav		}
3956230130Smav		break;
3957230130Smav	}
3958230130Smav	if (res && w->bindas == -1)
3959230130Smav		w->bindas = -2;
3960230130Smav
3961230130Smav	HDA_BOOTHVERBOSE(
3962230130Smav		device_printf(devinfo->dev,
3963230130Smav		    " %*snid %d returned %d\n",
3964230130Smav			depth + 1, "", w->nid, res);
3965230130Smav	);
3966230130Smav	return (res);
3967230130Smav}
3968230130Smav
3969230130Smav/*
3970230130Smav * Trace extra associations (beeper, monitor)
3971230130Smav */
3972230130Smavstatic void
3973230130Smavhdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo)
3974230130Smav{
3975230130Smav	struct hdaa_audio_as *as = devinfo->as;
3976230130Smav	struct hdaa_widget *w;
3977230130Smav	int j;
3978230130Smav
3979230130Smav	/* Input monitor */
3980230130Smav	/* Find mixer associated with input, but supplying signal
3981230130Smav	   for output associations. Hope it will be input monitor. */
3982230130Smav	HDA_BOOTVERBOSE(
3983230130Smav		device_printf(devinfo->dev,
3984230130Smav		    "Tracing input monitor\n");
3985230130Smav	);
3986230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3987230130Smav		w = hdaa_widget_get(devinfo, j);
3988230130Smav		if (w == NULL || w->enable == 0)
3989230130Smav			continue;
3990230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3991230130Smav			continue;
3992230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3993230130Smav			continue;
3994230130Smav		HDA_BOOTVERBOSE(
3995230130Smav			device_printf(devinfo->dev,
3996230130Smav			    " Tracing nid %d to out\n",
3997230130Smav			    j);
3998230130Smav		);
3999230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4000230130Smav			HDA_BOOTVERBOSE(
4001230130Smav				device_printf(devinfo->dev,
4002230130Smav				    " nid %d is input monitor\n",
4003230130Smav					w->nid);
4004230130Smav			);
4005230130Smav			w->ossdev = SOUND_MIXER_IMIX;
4006230130Smav		}
4007230130Smav	}
4008230130Smav
4009230130Smav	/* Other inputs monitor */
4010230130Smav	/* Find input pins supplying signal for output associations.
4011230130Smav	   Hope it will be input monitoring. */
4012230130Smav	HDA_BOOTVERBOSE(
4013230130Smav		device_printf(devinfo->dev,
4014230130Smav		    "Tracing other input monitors\n");
4015230130Smav	);
4016230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4017230130Smav		w = hdaa_widget_get(devinfo, j);
4018230130Smav		if (w == NULL || w->enable == 0)
4019230130Smav			continue;
4020230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4021230130Smav			continue;
4022230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
4023230130Smav			continue;
4024230130Smav		HDA_BOOTVERBOSE(
4025230130Smav			device_printf(devinfo->dev,
4026230130Smav			    " Tracing nid %d to out\n",
4027230130Smav			    j);
4028230130Smav		);
4029230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4030230130Smav			HDA_BOOTVERBOSE(
4031230130Smav				device_printf(devinfo->dev,
4032230130Smav				    " nid %d is input monitor\n",
4033230130Smav					w->nid);
4034230130Smav			);
4035230130Smav		}
4036230130Smav	}
4037230130Smav
4038230130Smav	/* Beeper */
4039230130Smav	HDA_BOOTVERBOSE(
4040230130Smav		device_printf(devinfo->dev,
4041230130Smav		    "Tracing beeper\n");
4042230130Smav	);
4043230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4044230130Smav		w = hdaa_widget_get(devinfo, j);
4045230130Smav		if (w == NULL || w->enable == 0)
4046230130Smav			continue;
4047230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
4048230130Smav			continue;
4049230130Smav		HDA_BOOTHVERBOSE(
4050230130Smav			device_printf(devinfo->dev,
4051230130Smav			    " Tracing nid %d to out\n",
4052230130Smav			    j);
4053230130Smav		);
4054230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4055230130Smav			HDA_BOOTVERBOSE(
4056230130Smav				device_printf(devinfo->dev,
4057230130Smav				    " nid %d traced to out\n",
4058230130Smav				    j);
4059230130Smav			);
4060230130Smav		}
4061230130Smav		w->bindas = -2;
4062230130Smav	}
4063230130Smav}
4064230130Smav
4065230130Smav/*
4066230130Smav * Bind assotiations to PCM channels
4067230130Smav */
4068230130Smavstatic void
4069230130Smavhdaa_audio_bind_as(struct hdaa_devinfo *devinfo)
4070230130Smav{
4071230130Smav	struct hdaa_audio_as *as = devinfo->as;
4072230130Smav	int i, j, cnt = 0, free;
4073230130Smav
4074230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4075230130Smav		if (as[j].enable)
4076230130Smav			cnt += as[j].num_chans;
4077230130Smav	}
4078230130Smav	if (devinfo->num_chans == 0) {
4079230130Smav		devinfo->chans = (struct hdaa_chan *)malloc(
4080230130Smav		    sizeof(struct hdaa_chan) * cnt,
4081230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
4082230130Smav		if (devinfo->chans == NULL) {
4083230130Smav			device_printf(devinfo->dev,
4084230130Smav			    "Channels memory allocation failed!\n");
4085230130Smav			return;
4086230130Smav		}
4087230130Smav	} else {
4088230130Smav		devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans,
4089230130Smav		    sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt),
4090230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
4091230130Smav		if (devinfo->chans == NULL) {
4092230130Smav			devinfo->num_chans = 0;
4093230130Smav			device_printf(devinfo->dev,
4094230130Smav			    "Channels memory allocation failed!\n");
4095230130Smav			return;
4096230130Smav		}
4097230130Smav		/* Fixup relative pointers after realloc */
4098230130Smav		for (j = 0; j < devinfo->num_chans; j++)
4099230130Smav			devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist;
4100230130Smav	}
4101230130Smav	free = devinfo->num_chans;
4102230130Smav	devinfo->num_chans += cnt;
4103230130Smav
4104230130Smav	for (j = free; j < free + cnt; j++) {
4105230130Smav		devinfo->chans[j].devinfo = devinfo;
4106230130Smav		devinfo->chans[j].as = -1;
4107230130Smav	}
4108230130Smav
4109230130Smav	/* Assign associations in order of their numbers, */
4110230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4111230130Smav		if (as[j].enable == 0)
4112230130Smav			continue;
4113230130Smav		for (i = 0; i < as[j].num_chans; i++) {
4114230130Smav			devinfo->chans[free].as = j;
4115230130Smav			devinfo->chans[free].asindex = i;
4116230130Smav			devinfo->chans[free].dir =
4117230130Smav			    (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
4118230130Smav			hdaa_pcmchannel_setup(&devinfo->chans[free]);
4119230130Smav			as[j].chans[i] = free;
4120230130Smav			free++;
4121230130Smav		}
4122230130Smav	}
4123230130Smav}
4124230130Smav
4125230130Smavstatic void
4126230130Smavhdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo)
4127230130Smav{
4128230130Smav	struct hdaa_widget *w;
4129230130Smav	int i;
4130230130Smav
4131230130Smav	/* Disable power and volume widgets. */
4132230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4133230130Smav		w = hdaa_widget_get(devinfo, i);
4134230130Smav		if (w == NULL || w->enable == 0)
4135230130Smav			continue;
4136230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
4137230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
4138230130Smav			w->enable = 0;
4139230130Smav			HDA_BOOTHVERBOSE(
4140230130Smav				device_printf(devinfo->dev,
4141230130Smav				    " Disabling nid %d due to it's"
4142230130Smav				    " non-audio type.\n",
4143230130Smav				    w->nid);
4144230130Smav			);
4145230130Smav		}
4146230130Smav	}
4147230130Smav}
4148230130Smav
4149230130Smavstatic void
4150230130Smavhdaa_audio_disable_useless(struct hdaa_devinfo *devinfo)
4151230130Smav{
4152230130Smav	struct hdaa_widget *w, *cw;
4153230130Smav	struct hdaa_audio_ctl *ctl;
4154230130Smav	int done, found, i, j, k;
4155230130Smav
4156230130Smav	/* Disable useless pins. */
4157230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4158230130Smav		w = hdaa_widget_get(devinfo, i);
4159230130Smav		if (w == NULL || w->enable == 0)
4160230130Smav			continue;
4161230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
4162230130Smav			if ((w->wclass.pin.config &
4163230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
4164230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
4165230130Smav				w->enable = 0;
4166230130Smav				HDA_BOOTHVERBOSE(
4167230130Smav					device_printf(devinfo->dev,
4168230130Smav					    " Disabling pin nid %d due"
4169230130Smav					    " to None connectivity.\n",
4170230130Smav					    w->nid);
4171230130Smav				);
4172230130Smav			} else if ((w->wclass.pin.config &
4173230130Smav			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
4174230130Smav				w->enable = 0;
4175230130Smav				HDA_BOOTHVERBOSE(
4176230130Smav					device_printf(devinfo->dev,
4177230130Smav					    " Disabling unassociated"
4178230130Smav					    " pin nid %d.\n",
4179230130Smav					    w->nid);
4180230130Smav				);
4181230130Smav			}
4182230130Smav		}
4183230130Smav	}
4184230130Smav	do {
4185230130Smav		done = 1;
4186230130Smav		/* Disable and mute controls for disabled widgets. */
4187230130Smav		i = 0;
4188230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4189230130Smav			if (ctl->enable == 0)
4190230130Smav				continue;
4191230130Smav			if (ctl->widget->enable == 0 ||
4192230130Smav			    (ctl->childwidget != NULL &&
4193230130Smav			    ctl->childwidget->enable == 0)) {
4194230130Smav				ctl->forcemute = 1;
4195230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
4196230130Smav				ctl->left = 0;
4197230130Smav				ctl->right = 0;
4198230130Smav				ctl->enable = 0;
4199230130Smav				if (ctl->ndir == HDAA_CTL_IN)
4200230130Smav					ctl->widget->connsenable[ctl->index] = 0;
4201230130Smav				done = 0;
4202230130Smav				HDA_BOOTHVERBOSE(
4203230130Smav					device_printf(devinfo->dev,
4204230130Smav					    " Disabling ctl %d nid %d cnid %d due"
4205230130Smav					    " to disabled widget.\n", i,
4206230130Smav					    ctl->widget->nid,
4207230130Smav					    (ctl->childwidget != NULL)?
4208230130Smav					    ctl->childwidget->nid:-1);
4209230130Smav				);
4210230130Smav			}
4211230130Smav		}
4212230130Smav		/* Disable useless widgets. */
4213230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4214230130Smav			w = hdaa_widget_get(devinfo, i);
4215230130Smav			if (w == NULL || w->enable == 0)
4216230130Smav				continue;
4217230130Smav			/* Disable inputs with disabled child widgets. */
4218230130Smav			for (j = 0; j < w->nconns; j++) {
4219230130Smav				if (w->connsenable[j]) {
4220230130Smav					cw = hdaa_widget_get(devinfo, w->conns[j]);
4221230130Smav					if (cw == NULL || cw->enable == 0) {
4222230130Smav						w->connsenable[j] = 0;
4223230130Smav						HDA_BOOTHVERBOSE(
4224230130Smav							device_printf(devinfo->dev,
4225230130Smav							    " Disabling nid %d connection %d due"
4226230130Smav							    " to disabled child widget.\n",
4227230130Smav							    i, j);
4228230130Smav						);
4229230130Smav					}
4230230130Smav				}
4231230130Smav			}
4232230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4233230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4234230130Smav				continue;
4235230130Smav			/* Disable mixers and selectors without inputs. */
4236230130Smav			found = 0;
4237230130Smav			for (j = 0; j < w->nconns; j++) {
4238230130Smav				if (w->connsenable[j]) {
4239230130Smav					found = 1;
4240230130Smav					break;
4241230130Smav				}
4242230130Smav			}
4243230130Smav			if (found == 0) {
4244230130Smav				w->enable = 0;
4245230130Smav				done = 0;
4246230130Smav				HDA_BOOTHVERBOSE(
4247230130Smav					device_printf(devinfo->dev,
4248230130Smav					    " Disabling nid %d due to all it's"
4249230130Smav					    " inputs disabled.\n", w->nid);
4250230130Smav				);
4251230130Smav			}
4252230130Smav			/* Disable nodes without consumers. */
4253230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4254230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4255230130Smav				continue;
4256230130Smav			found = 0;
4257230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4258230130Smav				cw = hdaa_widget_get(devinfo, k);
4259230130Smav				if (cw == NULL || cw->enable == 0)
4260230130Smav					continue;
4261230130Smav				for (j = 0; j < cw->nconns; j++) {
4262230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
4263230130Smav						found = 1;
4264230130Smav						break;
4265230130Smav					}
4266230130Smav				}
4267230130Smav			}
4268230130Smav			if (found == 0) {
4269230130Smav				w->enable = 0;
4270230130Smav				done = 0;
4271230130Smav				HDA_BOOTHVERBOSE(
4272230130Smav					device_printf(devinfo->dev,
4273230130Smav					    " Disabling nid %d due to all it's"
4274230130Smav					    " consumers disabled.\n", w->nid);
4275230130Smav				);
4276230130Smav			}
4277230130Smav		}
4278230130Smav	} while (done == 0);
4279230130Smav
4280230130Smav}
4281230130Smav
4282230130Smavstatic void
4283230130Smavhdaa_audio_disable_unas(struct hdaa_devinfo *devinfo)
4284230130Smav{
4285230130Smav	struct hdaa_audio_as *as = devinfo->as;
4286230130Smav	struct hdaa_widget *w, *cw;
4287230130Smav	struct hdaa_audio_ctl *ctl;
4288230130Smav	int i, j, k;
4289230130Smav
4290230130Smav	/* Disable unassosiated widgets. */
4291230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4292230130Smav		w = hdaa_widget_get(devinfo, i);
4293230130Smav		if (w == NULL || w->enable == 0)
4294230130Smav			continue;
4295230130Smav		if (w->bindas == -1) {
4296230130Smav			w->enable = 0;
4297230130Smav			HDA_BOOTHVERBOSE(
4298230130Smav				device_printf(devinfo->dev,
4299230130Smav				    " Disabling unassociated nid %d.\n",
4300230130Smav				    w->nid);
4301230130Smav			);
4302230130Smav		}
4303230130Smav	}
4304230130Smav	/* Disable input connections on input pin and
4305230130Smav	 * output on output. */
4306230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4307230130Smav		w = hdaa_widget_get(devinfo, i);
4308230130Smav		if (w == NULL || w->enable == 0)
4309230130Smav			continue;
4310230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4311230130Smav			continue;
4312230130Smav		if (w->bindas < 0)
4313230130Smav			continue;
4314230130Smav		if (as[w->bindas].dir == HDAA_CTL_IN) {
4315230130Smav			for (j = 0; j < w->nconns; j++) {
4316230130Smav				if (w->connsenable[j] == 0)
4317230130Smav					continue;
4318230130Smav				w->connsenable[j] = 0;
4319230130Smav				HDA_BOOTHVERBOSE(
4320230130Smav					device_printf(devinfo->dev,
4321230130Smav					    " Disabling connection to input pin "
4322230130Smav					    "nid %d conn %d.\n",
4323230130Smav					    i, j);
4324230130Smav				);
4325230130Smav			}
4326230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4327230130Smav			    HDAA_CTL_IN, -1, 1);
4328230130Smav			if (ctl && ctl->enable) {
4329230130Smav				ctl->forcemute = 1;
4330230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
4331230130Smav				ctl->left = 0;
4332230130Smav				ctl->right = 0;
4333230130Smav				ctl->enable = 0;
4334230130Smav			}
4335230130Smav		} else {
4336230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4337230130Smav			    HDAA_CTL_OUT, -1, 1);
4338230130Smav			if (ctl && ctl->enable) {
4339230130Smav				ctl->forcemute = 1;
4340230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
4341230130Smav				ctl->left = 0;
4342230130Smav				ctl->right = 0;
4343230130Smav				ctl->enable = 0;
4344230130Smav			}
4345230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4346230130Smav				cw = hdaa_widget_get(devinfo, k);
4347230130Smav				if (cw == NULL || cw->enable == 0)
4348230130Smav					continue;
4349230130Smav				for (j = 0; j < cw->nconns; j++) {
4350230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
4351230130Smav						cw->connsenable[j] = 0;
4352230130Smav						HDA_BOOTHVERBOSE(
4353230130Smav							device_printf(devinfo->dev,
4354230130Smav							    " Disabling connection from output pin "
4355230130Smav							    "nid %d conn %d cnid %d.\n",
4356230130Smav							    k, j, i);
4357230130Smav						);
4358230130Smav						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4359230130Smav						    cw->nconns > 1)
4360230130Smav							continue;
4361230130Smav						ctl = hdaa_audio_ctl_amp_get(devinfo, k,
4362230130Smav					    HDAA_CTL_IN, j, 1);
4363230130Smav						if (ctl && ctl->enable) {
4364230130Smav							ctl->forcemute = 1;
4365230130Smav							ctl->muted = HDAA_AMP_MUTE_ALL;
4366230130Smav							ctl->left = 0;
4367230130Smav							ctl->right = 0;
4368230130Smav							ctl->enable = 0;
4369230130Smav						}
4370230130Smav					}
4371230130Smav				}
4372230130Smav			}
4373230130Smav		}
4374230130Smav	}
4375230130Smav}
4376230130Smav
4377230130Smavstatic void
4378230130Smavhdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo)
4379230130Smav{
4380230130Smav	struct hdaa_audio_as *as = devinfo->as;
4381230130Smav	struct hdaa_widget *w;
4382230130Smav	int i, j;
4383230130Smav
4384230130Smav	/* On playback path we can safely disable all unseleted inputs. */
4385230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4386230130Smav		w = hdaa_widget_get(devinfo, i);
4387230130Smav		if (w == NULL || w->enable == 0)
4388230130Smav			continue;
4389230130Smav		if (w->nconns <= 1)
4390230130Smav			continue;
4391230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4392230130Smav			continue;
4393230130Smav		if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN)
4394230130Smav			continue;
4395230130Smav		for (j = 0; j < w->nconns; j++) {
4396230130Smav			if (w->connsenable[j] == 0)
4397230130Smav				continue;
4398230130Smav			if (w->selconn < 0 || w->selconn == j)
4399230130Smav				continue;
4400230130Smav			w->connsenable[j] = 0;
4401230130Smav			HDA_BOOTHVERBOSE(
4402230130Smav				device_printf(devinfo->dev,
4403230130Smav				    " Disabling unselected connection "
4404230130Smav				    "nid %d conn %d.\n",
4405230130Smav				    i, j);
4406230130Smav			);
4407230130Smav		}
4408230130Smav	}
4409230130Smav}
4410230130Smav
4411230130Smavstatic void
4412230130Smavhdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo)
4413230130Smav{
4414230130Smav	struct hdaa_audio_as *ases = devinfo->as;
4415230130Smav	struct hdaa_widget *w, *cw;
4416230130Smav	struct hdaa_audio_ctl *ctl;
4417230130Smav	int i, j;
4418230130Smav
4419230130Smav	/* Disable crossassociatement and unwanted crosschannel connections. */
4420230130Smav	/* ... using selectors */
4421230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4422230130Smav		w = hdaa_widget_get(devinfo, i);
4423230130Smav		if (w == NULL || w->enable == 0)
4424230130Smav			continue;
4425230130Smav		if (w->nconns <= 1)
4426230130Smav			continue;
4427230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4428230130Smav			continue;
4429230130Smav		/* Allow any -> mix */
4430230130Smav		if (w->bindas == -2)
4431230130Smav			continue;
4432230130Smav		for (j = 0; j < w->nconns; j++) {
4433230130Smav			if (w->connsenable[j] == 0)
4434230130Smav				continue;
4435230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
4436230130Smav			if (cw == NULL || w->enable == 0)
4437230130Smav				continue;
4438230130Smav			/* Allow mix -> out. */
4439230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
4440230130Smav			    ases[w->bindas].dir == HDAA_CTL_OUT)
4441230130Smav				continue;
4442230130Smav			/* Allow mix -> mixed-in. */
4443230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
4444230130Smav			    ases[w->bindas].mixed)
4445230130Smav				continue;
4446230130Smav			/* Allow in -> mix. */
4447230130Smav			if ((w->pflags & HDAA_ADC_MONITOR) &&
4448230130Smav			     cw->bindas >= 0 &&
4449230130Smav			     ases[cw->bindas].dir == HDAA_CTL_IN)
4450230130Smav				continue;
4451230130Smav			/* Allow if have common as/seqs. */
4452230130Smav			if (w->bindas == cw->bindas &&
4453230130Smav			    (w->bindseqmask & cw->bindseqmask) != 0)
4454230130Smav				continue;
4455230130Smav			w->connsenable[j] = 0;
4456230130Smav			HDA_BOOTHVERBOSE(
4457230130Smav				device_printf(devinfo->dev,
4458230130Smav				    " Disabling crossassociatement connection "
4459230130Smav				    "nid %d conn %d cnid %d.\n",
4460230130Smav				    i, j, cw->nid);
4461230130Smav			);
4462230130Smav		}
4463230130Smav	}
4464230130Smav	/* ... using controls */
4465230130Smav	i = 0;
4466230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4467230130Smav		if (ctl->enable == 0 || ctl->childwidget == NULL)
4468230130Smav			continue;
4469230130Smav		/* Allow any -> mix */
4470230130Smav		if (ctl->widget->bindas == -2)
4471230130Smav			continue;
4472230130Smav		/* Allow mix -> out. */
4473230130Smav		if (ctl->childwidget->bindas == -2 &&
4474230130Smav		    ctl->widget->bindas >= 0 &&
4475230130Smav		    ases[ctl->widget->bindas].dir == HDAA_CTL_OUT)
4476230130Smav			continue;
4477230130Smav		/* Allow mix -> mixed-in. */
4478230130Smav		if (ctl->childwidget->bindas == -2 &&
4479230130Smav		    ctl->widget->bindas >= 0 &&
4480230130Smav		    ases[ctl->widget->bindas].mixed)
4481230130Smav			continue;
4482230130Smav		/* Allow in -> mix. */
4483230130Smav		if ((ctl->widget->pflags & HDAA_ADC_MONITOR) &&
4484230130Smav		    ctl->childwidget->bindas >= 0 &&
4485230130Smav		    ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN)
4486230130Smav			continue;
4487230130Smav		/* Allow if have common as/seqs. */
4488230130Smav		if (ctl->widget->bindas == ctl->childwidget->bindas &&
4489230130Smav		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0)
4490230130Smav			continue;
4491230130Smav		ctl->forcemute = 1;
4492230130Smav		ctl->muted = HDAA_AMP_MUTE_ALL;
4493230130Smav		ctl->left = 0;
4494230130Smav		ctl->right = 0;
4495230130Smav		ctl->enable = 0;
4496230130Smav		if (ctl->ndir == HDAA_CTL_IN)
4497230130Smav			ctl->widget->connsenable[ctl->index] = 0;
4498230130Smav		HDA_BOOTHVERBOSE(
4499230130Smav			device_printf(devinfo->dev,
4500230130Smav			    " Disabling crossassociatement connection "
4501230130Smav			    "ctl %d nid %d cnid %d.\n", i,
4502230130Smav			    ctl->widget->nid,
4503230130Smav			    ctl->childwidget->nid);
4504230130Smav		);
4505230130Smav	}
4506230130Smav
4507230130Smav}
4508230130Smav
4509230130Smav/*
4510230451Smav * Find controls to control amplification for source and calculate possible
4511230451Smav * amplification range.
4512230130Smav */
4513230130Smavstatic int
4514230130Smavhdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4515230451Smav    int ossdev, int ctlable, int depth, int *minamp, int *maxamp)
4516230130Smav{
4517230130Smav	struct hdaa_widget *w, *wc;
4518230130Smav	struct hdaa_audio_ctl *ctl;
4519230451Smav	int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4520230130Smav
4521230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4522230451Smav		return (found);
4523230130Smav
4524230130Smav	w = hdaa_widget_get(devinfo, nid);
4525230130Smav	if (w == NULL || w->enable == 0)
4526230451Smav		return (found);
4527230130Smav
4528230130Smav	/* Count number of active inputs. */
4529230130Smav	if (depth > 0) {
4530230130Smav		for (j = 0; j < w->nconns; j++) {
4531230451Smav			if (!w->connsenable[j])
4532230451Smav				continue;
4533230451Smav			conns++;
4534230130Smav		}
4535230130Smav	}
4536230130Smav
4537230130Smav	/* If this is not a first step - use input mixer.
4538230130Smav	   Pins have common input ctl so care must be taken. */
4539230130Smav	if (depth > 0 && ctlable && (conns == 1 ||
4540230130Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
4541230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
4542230130Smav		    index, 1);
4543230130Smav		if (ctl) {
4544230451Smav			ctl->ossmask |= (1 << ossdev);
4545230451Smav			found++;
4546230451Smav			if (*minamp == *maxamp) {
4547230451Smav				*minamp += MINQDB(ctl);
4548230451Smav				*maxamp += MAXQDB(ctl);
4549230451Smav			}
4550230130Smav		}
4551230130Smav	}
4552230130Smav
4553230130Smav	/* If widget has own ossdev - not traverse it.
4554230130Smav	   It will be traversed on it's own. */
4555230130Smav	if (w->ossdev >= 0 && depth > 0)
4556230451Smav		return (found);
4557230130Smav
4558230130Smav	/* We must not traverse pin */
4559230130Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4560230130Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
4561230130Smav	    depth > 0)
4562230451Smav		return (found);
4563230130Smav
4564230130Smav	/* record that this widget exports such signal, */
4565230130Smav	w->ossmask |= (1 << ossdev);
4566230130Smav
4567230451Smav	/*
4568230451Smav	 * If signals mixed, we can't assign controls farther.
4569230130Smav	 * Ignore this on depth zero. Caller must knows why.
4570230130Smav	 */
4571230451Smav	if (conns > 1 &&
4572230451Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4573230130Smav		ctlable = 0;
4574230130Smav
4575230130Smav	if (ctlable) {
4576230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
4577230130Smav		if (ctl) {
4578230451Smav			ctl->ossmask |= (1 << ossdev);
4579230451Smav			found++;
4580230451Smav			if (*minamp == *maxamp) {
4581230451Smav				*minamp += MINQDB(ctl);
4582230451Smav				*maxamp += MAXQDB(ctl);
4583230451Smav			}
4584230130Smav		}
4585230130Smav	}
4586230130Smav
4587230451Smav	cminamp = cmaxamp = 0;
4588230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4589230130Smav		wc = hdaa_widget_get(devinfo, i);
4590230130Smav		if (wc == NULL || wc->enable == 0)
4591230130Smav			continue;
4592230130Smav		for (j = 0; j < wc->nconns; j++) {
4593230130Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
4594230451Smav				tminamp = tmaxamp = 0;
4595230451Smav				found += hdaa_audio_ctl_source_amp(devinfo,
4596230451Smav				    wc->nid, j, ossdev, ctlable, depth + 1,
4597230451Smav				    &tminamp, &tmaxamp);
4598230451Smav				if (cminamp == 0 && cmaxamp == 0) {
4599230451Smav					cminamp = tminamp;
4600230451Smav					cmaxamp = tmaxamp;
4601230451Smav				} else if (tminamp != tmaxamp) {
4602230451Smav					cminamp = imax(cminamp, tminamp);
4603230451Smav					cmaxamp = imin(cmaxamp, tmaxamp);
4604230451Smav				}
4605230130Smav			}
4606230130Smav		}
4607230130Smav	}
4608230451Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4609230451Smav		*minamp += cminamp;
4610230451Smav		*maxamp += cmaxamp;
4611230451Smav	}
4612230451Smav	return (found);
4613230130Smav}
4614230130Smav
4615230130Smav/*
4616230451Smav * Find controls to control amplification for destination and calculate
4617230451Smav * possible amplification range.
4618230130Smav */
4619230451Smavstatic int
4620230130Smavhdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4621230451Smav    int ossdev, int depth, int *minamp, int *maxamp)
4622230130Smav{
4623230130Smav	struct hdaa_audio_as *as = devinfo->as;
4624230130Smav	struct hdaa_widget *w, *wc;
4625230130Smav	struct hdaa_audio_ctl *ctl;
4626230451Smav	int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4627230130Smav
4628230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4629230451Smav		return (found);
4630230130Smav
4631230130Smav	w = hdaa_widget_get(devinfo, nid);
4632230130Smav	if (w == NULL || w->enable == 0)
4633230451Smav		return (found);
4634230130Smav
4635230130Smav	if (depth > 0) {
4636230130Smav		/* If this node produce output for several consumers,
4637230130Smav		   we can't touch it. */
4638230130Smav		consumers = 0;
4639230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4640230130Smav			wc = hdaa_widget_get(devinfo, i);
4641230130Smav			if (wc == NULL || wc->enable == 0)
4642230130Smav				continue;
4643230130Smav			for (j = 0; j < wc->nconns; j++) {
4644230130Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
4645230130Smav					consumers++;
4646230130Smav			}
4647230130Smav		}
4648230130Smav		/* The only exception is if real HP redirection is configured
4649230130Smav		   and this is a duplication point.
4650230130Smav		   XXX: Actually exception is not completely correct.
4651230130Smav		   XXX: Duplication point check is not perfect. */
4652230130Smav		if ((consumers == 2 && (w->bindas < 0 ||
4653230130Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
4654230130Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
4655230130Smav		    consumers > 2)
4656230451Smav			return (found);
4657230130Smav
4658230130Smav		/* Else use it's output mixer. */
4659230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4660230130Smav		    HDAA_CTL_OUT, -1, 1);
4661230130Smav		if (ctl) {
4662230451Smav			ctl->ossmask |= (1 << ossdev);
4663230451Smav			found++;
4664230451Smav			if (*minamp == *maxamp) {
4665230451Smav				*minamp += MINQDB(ctl);
4666230451Smav				*maxamp += MAXQDB(ctl);
4667230451Smav			}
4668230130Smav		}
4669230130Smav	}
4670230130Smav
4671230130Smav	/* We must not traverse pin */
4672230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4673230130Smav	    depth > 0)
4674230451Smav		return (found);
4675230130Smav
4676230451Smav	cminamp = cmaxamp = 0;
4677230130Smav	for (i = 0; i < w->nconns; i++) {
4678230130Smav		if (w->connsenable[i] == 0)
4679230130Smav			continue;
4680230130Smav		if (index >= 0 && i != index)
4681230130Smav			continue;
4682230451Smav		tminamp = tmaxamp = 0;
4683230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4684230130Smav		    HDAA_CTL_IN, i, 1);
4685230130Smav		if (ctl) {
4686230451Smav			ctl->ossmask |= (1 << ossdev);
4687230451Smav			found++;
4688230451Smav			if (*minamp == *maxamp) {
4689230451Smav				tminamp += MINQDB(ctl);
4690230451Smav				tmaxamp += MAXQDB(ctl);
4691230451Smav			}
4692230130Smav		}
4693230451Smav		found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
4694230451Smav		    depth + 1, &tminamp, &tmaxamp);
4695230451Smav		if (cminamp == 0 && cmaxamp == 0) {
4696230451Smav			cminamp = tminamp;
4697230451Smav			cmaxamp = tmaxamp;
4698230451Smav		} else if (tminamp != tmaxamp) {
4699230451Smav			cminamp = imax(cminamp, tminamp);
4700230451Smav			cmaxamp = imin(cmaxamp, tmaxamp);
4701230451Smav		}
4702230130Smav	}
4703230451Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4704230451Smav		*minamp += cminamp;
4705230451Smav		*maxamp += cmaxamp;
4706230451Smav	}
4707230451Smav	return (found);
4708230130Smav}
4709230130Smav
4710230130Smav/*
4711230130Smav * Assign OSS names to sound sources
4712230130Smav */
4713230130Smavstatic void
4714230130Smavhdaa_audio_assign_names(struct hdaa_devinfo *devinfo)
4715230130Smav{
4716230130Smav	struct hdaa_audio_as *as = devinfo->as;
4717230130Smav	struct hdaa_widget *w;
4718230130Smav	int i, j;
4719230130Smav	int type = -1, use, used = 0;
4720230130Smav	static const int types[7][13] = {
4721230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4722230130Smav	      SOUND_MIXER_LINE3, -1 },	/* line */
4723230130Smav	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
4724230130Smav	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
4725230130Smav	    { SOUND_MIXER_CD, -1 },	/* cd */
4726230130Smav	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
4727230130Smav	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
4728230130Smav	      -1 },	/* digital */
4729230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4730230130Smav	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
4731230130Smav	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
4732230130Smav	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
4733230130Smav	      -1 }	/* others */
4734230130Smav	};
4735230130Smav
4736230130Smav	/* Surely known names */
4737230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4738230130Smav		w = hdaa_widget_get(devinfo, i);
4739230130Smav		if (w == NULL || w->enable == 0)
4740230130Smav			continue;
4741230130Smav		if (w->bindas == -1)
4742230130Smav			continue;
4743230130Smav		use = -1;
4744230130Smav		switch (w->type) {
4745230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4746230130Smav			if (as[w->bindas].dir == HDAA_CTL_OUT)
4747230130Smav				break;
4748230130Smav			type = -1;
4749230130Smav			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4750230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4751230130Smav				type = 0;
4752230130Smav				break;
4753230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4754230130Smav				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
4755230130Smav				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4756230130Smav					break;
4757230130Smav				type = 1;
4758230130Smav				break;
4759230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4760230130Smav				type = 3;
4761230130Smav				break;
4762230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4763230130Smav				type = 4;
4764230130Smav				break;
4765230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
4766230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
4767230130Smav				type = 5;
4768230130Smav				break;
4769230130Smav			}
4770230130Smav			if (type == -1)
4771230130Smav				break;
4772230130Smav			j = 0;
4773230130Smav			while (types[type][j] >= 0 &&
4774230130Smav			    (used & (1 << types[type][j])) != 0) {
4775230130Smav				j++;
4776230130Smav			}
4777230130Smav			if (types[type][j] >= 0)
4778230130Smav				use = types[type][j];
4779230130Smav			break;
4780230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4781230130Smav			use = SOUND_MIXER_PCM;
4782230130Smav			break;
4783230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
4784230130Smav			use = SOUND_MIXER_SPEAKER;
4785230130Smav			break;
4786230130Smav		default:
4787230130Smav			break;
4788230130Smav		}
4789230130Smav		if (use >= 0) {
4790230130Smav			w->ossdev = use;
4791230130Smav			used |= (1 << use);
4792230130Smav		}
4793230130Smav	}
4794230130Smav	/* Semi-known names */
4795230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4796230130Smav		w = hdaa_widget_get(devinfo, i);
4797230130Smav		if (w == NULL || w->enable == 0)
4798230130Smav			continue;
4799230130Smav		if (w->ossdev >= 0)
4800230130Smav			continue;
4801230130Smav		if (w->bindas == -1)
4802230130Smav			continue;
4803230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4804230130Smav			continue;
4805230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4806230130Smav			continue;
4807230130Smav		type = -1;
4808230130Smav		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4809230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
4810230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4811230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
4812230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
4813230130Smav			type = 0;
4814230130Smav			break;
4815230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4816230130Smav			type = 2;
4817230130Smav			break;
4818230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
4819230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
4820230130Smav			type = 5;
4821230130Smav			break;
4822230130Smav		}
4823230130Smav		if (type == -1)
4824230130Smav			break;
4825230130Smav		j = 0;
4826230130Smav		while (types[type][j] >= 0 &&
4827230130Smav		    (used & (1 << types[type][j])) != 0) {
4828230130Smav			j++;
4829230130Smav		}
4830230130Smav		if (types[type][j] >= 0) {
4831230130Smav			w->ossdev = types[type][j];
4832230130Smav			used |= (1 << types[type][j]);
4833230130Smav		}
4834230130Smav	}
4835230130Smav	/* Others */
4836230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4837230130Smav		w = hdaa_widget_get(devinfo, i);
4838230130Smav		if (w == NULL || w->enable == 0)
4839230130Smav			continue;
4840230130Smav		if (w->ossdev >= 0)
4841230130Smav			continue;
4842230130Smav		if (w->bindas == -1)
4843230130Smav			continue;
4844230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4845230130Smav			continue;
4846230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4847230130Smav			continue;
4848230130Smav		j = 0;
4849230130Smav		while (types[6][j] >= 0 &&
4850230130Smav		    (used & (1 << types[6][j])) != 0) {
4851230130Smav			j++;
4852230130Smav		}
4853230130Smav		if (types[6][j] >= 0) {
4854230130Smav			w->ossdev = types[6][j];
4855230130Smav			used |= (1 << types[6][j]);
4856230130Smav		}
4857230130Smav	}
4858230130Smav}
4859230130Smav
4860230130Smavstatic void
4861230130Smavhdaa_audio_build_tree(struct hdaa_devinfo *devinfo)
4862230130Smav{
4863230130Smav	struct hdaa_audio_as *as = devinfo->as;
4864230130Smav	int j, res;
4865230130Smav
4866230130Smav	/* Trace all associations in order of their numbers. */
4867230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4868230130Smav		if (as[j].enable == 0)
4869230130Smav			continue;
4870230130Smav		HDA_BOOTVERBOSE(
4871230130Smav			device_printf(devinfo->dev,
4872230130Smav			    "Tracing association %d (%d)\n", j, as[j].index);
4873230130Smav		);
4874230130Smav		if (as[j].dir == HDAA_CTL_OUT) {
4875230130Smavretry:
4876230130Smav			res = hdaa_audio_trace_as_out(devinfo, j, 0);
4877230130Smav			if (res == 0 && as[j].hpredir >= 0 &&
4878230130Smav			    as[j].fakeredir == 0) {
4879230130Smav				/* If CODEC can't do analog HP redirection
4880230130Smav				   try to make it using one more DAC. */
4881230130Smav				as[j].fakeredir = 1;
4882230130Smav				goto retry;
4883230130Smav			}
4884230130Smav		} else if (as[j].mixed)
4885230130Smav			res = hdaa_audio_trace_as_in(devinfo, j);
4886230130Smav		else
4887230130Smav			res = hdaa_audio_trace_as_in_mch(devinfo, j, 0);
4888230130Smav		if (res) {
4889230130Smav			HDA_BOOTVERBOSE(
4890230130Smav				device_printf(devinfo->dev,
4891230130Smav				    "Association %d (%d) trace succeeded\n",
4892230130Smav				    j, as[j].index);
4893230130Smav			);
4894230130Smav		} else {
4895230130Smav			HDA_BOOTVERBOSE(
4896230130Smav				device_printf(devinfo->dev,
4897230130Smav				    "Association %d (%d) trace failed\n",
4898230130Smav				    j, as[j].index);
4899230130Smav			);
4900230130Smav			as[j].enable = 0;
4901230130Smav		}
4902230130Smav	}
4903230130Smav
4904230130Smav	/* Look for additional DACs/ADCs. */
4905230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4906230130Smav		if (as[j].enable == 0)
4907230130Smav			continue;
4908230130Smav		hdaa_audio_adddac(devinfo, j);
4909230130Smav	}
4910230130Smav
4911230130Smav	/* Trace mixer and beeper pseudo associations. */
4912230130Smav	hdaa_audio_trace_as_extra(devinfo);
4913230130Smav}
4914230130Smav
4915230451Smav/*
4916230451Smav * Store in pdevinfo new data about whether and how we can control signal
4917230451Smav * for OSS device to/from specified widget.
4918230451Smav */
4919230130Smavstatic void
4920230451Smavhdaa_adjust_amp(struct hdaa_widget *w, int ossdev,
4921230451Smav    int found, int minamp, int maxamp)
4922230451Smav{
4923230451Smav	struct hdaa_devinfo *devinfo = w->devinfo;
4924230451Smav	struct hdaa_pcm_devinfo *pdevinfo;
4925230451Smav
4926230451Smav	if (w->bindas >= 0)
4927230451Smav		pdevinfo = devinfo->as[w->bindas].pdevinfo;
4928230451Smav	else
4929230451Smav		pdevinfo = &devinfo->devs[0];
4930230451Smav	if (found)
4931230451Smav		pdevinfo->ossmask |= (1 << ossdev);
4932230451Smav	if (minamp == 0 && maxamp == 0)
4933230451Smav		return;
4934230451Smav	if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) {
4935230451Smav		pdevinfo->minamp[ossdev] = minamp;
4936230451Smav		pdevinfo->maxamp[ossdev] = maxamp;
4937230451Smav	} else {
4938230451Smav		pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp);
4939230451Smav		pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp);
4940230451Smav	}
4941230451Smav}
4942230451Smav
4943230451Smav/*
4944230451Smav * Trace signals from/to all possible sources/destionstions to find possible
4945230451Smav * recording sources, OSS device control ranges and to assign controls.
4946230451Smav */
4947230451Smavstatic void
4948230130Smavhdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo)
4949230130Smav{
4950230130Smav	struct hdaa_audio_as *as = devinfo->as;
4951230130Smav	struct hdaa_widget *w, *cw;
4952230451Smav	int i, j, minamp, maxamp, found;
4953230130Smav
4954230130Smav	/* Assign mixers to the tree. */
4955230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4956230130Smav		w = hdaa_widget_get(devinfo, i);
4957230130Smav		if (w == NULL || w->enable == 0)
4958230130Smav			continue;
4959230451Smav		minamp = maxamp = 0;
4960230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
4961230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
4962230130Smav		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4963230130Smav		    as[w->bindas].dir == HDAA_CTL_IN)) {
4964230130Smav			if (w->ossdev < 0)
4965230130Smav				continue;
4966230451Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4967230451Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4968230451Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4969230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
4970230451Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4971230451Smav			    SOUND_MIXER_RECLEV, 0, &minamp, &maxamp);
4972230451Smav			hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp);
4973230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4974230130Smav		    as[w->bindas].dir == HDAA_CTL_OUT) {
4975230451Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4976230451Smav			    SOUND_MIXER_VOLUME, 0, &minamp, &maxamp);
4977230451Smav			hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp);
4978230130Smav		}
4979230130Smav		if (w->ossdev == SOUND_MIXER_IMIX) {
4980230451Smav			minamp = maxamp = 0;
4981230451Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4982230451Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4983230451Smav			if (minamp == maxamp) {
4984230130Smav				/* If we are unable to control input monitor
4985230130Smav				   as source - try to control it as destination. */
4986230451Smav				found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4987230451Smav				    w->ossdev, 0, &minamp, &maxamp);
4988230451Smav				w->pflags |= HDAA_IMIX_AS_DST;
4989230130Smav			}
4990230451Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4991230130Smav		}
4992230130Smav		if (w->pflags & HDAA_ADC_MONITOR) {
4993230130Smav			for (j = 0; j < w->nconns; j++) {
4994230130Smav				if (!w->connsenable[j])
4995230130Smav				    continue;
4996230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
4997230130Smav				if (cw == NULL || cw->enable == 0)
4998230130Smav				    continue;
4999230130Smav				if (cw->bindas == -1)
5000230130Smav				    continue;
5001230130Smav				if (cw->bindas >= 0 &&
5002230130Smav				    as[cw->bindas].dir != HDAA_CTL_IN)
5003230130Smav					continue;
5004230451Smav				minamp = maxamp = 0;
5005230451Smav				found = hdaa_audio_ctl_dest_amp(devinfo,
5006230451Smav				    w->nid, j, SOUND_MIXER_IGAIN, 0,
5007230451Smav				    &minamp, &maxamp);
5008230451Smav				hdaa_adjust_amp(w, SOUND_MIXER_IGAIN,
5009230451Smav				    found, minamp, maxamp);
5010230130Smav			}
5011230130Smav		}
5012230130Smav	}
5013230130Smav}
5014230130Smav
5015230130Smavstatic void
5016230130Smavhdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo)
5017230130Smav{
5018230130Smav	struct hdaa_audio_as *as = devinfo->as;
5019230130Smav	struct hdaa_widget *w;
5020230130Smav	uint32_t pincap;
5021230130Smav	int i;
5022230130Smav
5023230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
5024230130Smav		w = &devinfo->widget[i];
5025230130Smav		if (w == NULL)
5026230130Smav			continue;
5027230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
5028230130Smav		    w->waspin == 0)
5029230130Smav			continue;
5030230130Smav
5031230130Smav		pincap = w->wclass.pin.cap;
5032230130Smav
5033230130Smav		/* Disable everything. */
5034230130Smav		w->wclass.pin.ctrl &= ~(
5035230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
5036230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
5037230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
5038230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
5039230130Smav
5040230130Smav		if (w->enable == 0) {
5041230130Smav			/* Pin is unused so left it disabled. */
5042230130Smav			continue;
5043230130Smav		} else if (w->waspin) {
5044230130Smav			/* Enable input for beeper input. */
5045230130Smav			w->wclass.pin.ctrl |=
5046230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5047230130Smav		} else if (w->bindas < 0 || as[w->bindas].enable == 0) {
5048230130Smav			/* Pin is unused so left it disabled. */
5049230130Smav			continue;
5050230130Smav		} else if (as[w->bindas].dir == HDAA_CTL_IN) {
5051230130Smav			/* Input pin, configure for input. */
5052230130Smav			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5053230130Smav				w->wclass.pin.ctrl |=
5054230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5055230130Smav
5056230130Smav			if ((devinfo->quirks & HDAA_QUIRK_IVREF100) &&
5057230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5058230130Smav				w->wclass.pin.ctrl |=
5059230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5060230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5061230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) &&
5062230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5063230130Smav				w->wclass.pin.ctrl |=
5064230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5065230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5066230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) &&
5067230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5068230130Smav				w->wclass.pin.ctrl |=
5069230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5070230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5071230130Smav		} else {
5072230130Smav			/* Output pin, configure for output. */
5073230130Smav			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5074230130Smav				w->wclass.pin.ctrl |=
5075230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
5076230130Smav
5077230130Smav			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
5078230130Smav			    (w->wclass.pin.config &
5079230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
5080230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
5081230130Smav				w->wclass.pin.ctrl |=
5082230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
5083230130Smav
5084230130Smav			if ((devinfo->quirks & HDAA_QUIRK_OVREF100) &&
5085230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5086230130Smav				w->wclass.pin.ctrl |=
5087230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5088230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5089230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) &&
5090230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5091230130Smav				w->wclass.pin.ctrl |=
5092230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5093230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5094230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) &&
5095230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5096230130Smav				w->wclass.pin.ctrl |=
5097230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5098230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5099230130Smav		}
5100230130Smav	}
5101230130Smav}
5102230130Smav
5103230130Smavstatic void
5104230130Smavhdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo)
5105230130Smav{
5106230130Smav	struct hdaa_audio_ctl *ctl;
5107230130Smav	int i, z;
5108230130Smav
5109230130Smav	i = 0;
5110230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5111230130Smav		if (ctl->enable == 0 || ctl->ossmask != 0) {
5112230130Smav			/* Mute disabled and mixer controllable controls.
5113230130Smav			 * Last will be initialized by mixer_init().
5114230130Smav			 * This expected to reduce click on startup. */
5115230130Smav			hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0);
5116230130Smav			continue;
5117230130Smav		}
5118230130Smav		/* Init fixed controls to 0dB amplification. */
5119230130Smav		z = ctl->offset;
5120230130Smav		if (z > ctl->step)
5121230130Smav			z = ctl->step;
5122230130Smav		hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z);
5123230130Smav	}
5124230130Smav}
5125230130Smav
5126230130Smavstatic void
5127230130Smavhdaa_gpio_commit(struct hdaa_devinfo *devinfo)
5128230130Smav{
5129230130Smav	uint32_t gdata, gmask, gdir;
5130230130Smav	int i, numgpio;
5131230130Smav
5132230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5133230130Smav	if (devinfo->gpio != 0 && numgpio != 0) {
5134230130Smav		gdata = hda_command(devinfo->dev,
5135230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
5136230130Smav		gmask = hda_command(devinfo->dev,
5137230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
5138230130Smav		gdir = hda_command(devinfo->dev,
5139230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
5140230130Smav		for (i = 0; i < numgpio; i++) {
5141230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5142230130Smav			    HDAA_GPIO_SET(i)) {
5143230130Smav				gdata |= (1 << i);
5144230130Smav				gmask |= (1 << i);
5145230130Smav				gdir |= (1 << i);
5146230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5147230130Smav			    HDAA_GPIO_CLEAR(i)) {
5148230130Smav				gdata &= ~(1 << i);
5149230130Smav				gmask |= (1 << i);
5150230130Smav				gdir |= (1 << i);
5151230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5152230130Smav			    HDAA_GPIO_DISABLE(i)) {
5153230130Smav				gmask &= ~(1 << i);
5154230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5155230130Smav			    HDAA_GPIO_INPUT(i)) {
5156230130Smav				gmask |= (1 << i);
5157230130Smav				gdir &= ~(1 << i);
5158230130Smav			}
5159230130Smav		}
5160230130Smav		HDA_BOOTVERBOSE(
5161230130Smav			device_printf(devinfo->dev, "GPIO commit\n");
5162230130Smav		);
5163230130Smav		hda_command(devinfo->dev,
5164230130Smav		    HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask));
5165230130Smav		hda_command(devinfo->dev,
5166230130Smav		    HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir));
5167230130Smav		hda_command(devinfo->dev,
5168230130Smav		    HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata));
5169230130Smav		HDA_BOOTVERBOSE(
5170230130Smav			hdaa_dump_gpio(devinfo);
5171230130Smav		);
5172230130Smav	}
5173230130Smav}
5174230130Smav
5175230130Smavstatic void
5176230130Smavhdaa_gpo_commit(struct hdaa_devinfo *devinfo)
5177230130Smav{
5178230130Smav	uint32_t gdata;
5179230130Smav	int i, numgpo;
5180230130Smav
5181230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
5182230130Smav	if (devinfo->gpo != 0 && numgpo != 0) {
5183230130Smav		gdata = hda_command(devinfo->dev,
5184230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
5185230130Smav		for (i = 0; i < numgpo; i++) {
5186230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5187230130Smav			    HDAA_GPIO_SET(i)) {
5188230130Smav				gdata |= (1 << i);
5189230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5190230130Smav			    HDAA_GPIO_CLEAR(i)) {
5191230130Smav				gdata &= ~(1 << i);
5192230130Smav			}
5193230130Smav		}
5194230130Smav		HDA_BOOTVERBOSE(
5195230130Smav			device_printf(devinfo->dev, "GPO commit\n");
5196230130Smav		);
5197230130Smav		hda_command(devinfo->dev,
5198230130Smav		    HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata));
5199230130Smav		HDA_BOOTVERBOSE(
5200230130Smav			hdaa_dump_gpo(devinfo);
5201230130Smav		);
5202230130Smav	}
5203230130Smav}
5204230130Smav
5205230130Smavstatic void
5206230130Smavhdaa_audio_commit(struct hdaa_devinfo *devinfo)
5207230130Smav{
5208230130Smav	struct hdaa_widget *w;
5209230130Smav	int i;
5210230130Smav
5211230130Smav	/* Commit controls. */
5212230130Smav	hdaa_audio_ctl_commit(devinfo);
5213230130Smav
5214230130Smav	/* Commit selectors, pins and EAPD. */
5215230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
5216230130Smav		w = &devinfo->widget[i];
5217230130Smav		if (w == NULL)
5218230130Smav			continue;
5219230130Smav		if (w->selconn == -1)
5220230130Smav			w->selconn = 0;
5221230130Smav		if (w->nconns > 0)
5222230130Smav			hdaa_widget_connection_select(w, w->selconn);
5223230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
5224230130Smav		    w->waspin) {
5225230130Smav			hda_command(devinfo->dev,
5226230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid,
5227230130Smav			    w->wclass.pin.ctrl));
5228230130Smav		}
5229230130Smav		if (w->param.eapdbtl != HDA_INVALID) {
5230230130Smav			uint32_t val;
5231230130Smav
5232230130Smav			val = w->param.eapdbtl;
5233230130Smav			if (devinfo->quirks &
5234230130Smav			    HDAA_QUIRK_EAPDINV)
5235230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
5236230130Smav			hda_command(devinfo->dev,
5237230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid,
5238230130Smav			    val));
5239230130Smav		}
5240230130Smav	}
5241230130Smav
5242230130Smav	hdaa_gpio_commit(devinfo);
5243230130Smav	hdaa_gpo_commit(devinfo);
5244230130Smav}
5245230130Smav
5246230130Smavstatic void
5247230130Smavhdaa_powerup(struct hdaa_devinfo *devinfo)
5248230130Smav{
5249230130Smav	int i;
5250230130Smav
5251230130Smav	hda_command(devinfo->dev,
5252230130Smav	    HDA_CMD_SET_POWER_STATE(0,
5253230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D0));
5254230130Smav	DELAY(100);
5255230130Smav
5256230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5257230130Smav		hda_command(devinfo->dev,
5258230130Smav		    HDA_CMD_SET_POWER_STATE(0,
5259230130Smav		    i, HDA_CMD_POWER_STATE_D0));
5260230130Smav	}
5261230130Smav	DELAY(1000);
5262230130Smav}
5263230130Smav
5264230130Smavstatic int
5265230130Smavhdaa_pcmchannel_setup(struct hdaa_chan *ch)
5266230130Smav{
5267230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
5268230130Smav	struct hdaa_audio_as *as = devinfo->as;
5269230130Smav	struct hdaa_widget *w;
5270230130Smav	uint32_t cap, fmtcap, pcmcap;
5271230130Smav	int i, j, ret, channels, onlystereo;
5272230130Smav	uint16_t pinset;
5273230130Smav
5274230130Smav	ch->caps = hdaa_caps;
5275230130Smav	ch->caps.fmtlist = ch->fmtlist;
5276230130Smav	ch->bit16 = 1;
5277230130Smav	ch->bit32 = 0;
5278230130Smav	ch->pcmrates[0] = 48000;
5279230130Smav	ch->pcmrates[1] = 0;
5280230326Smav	ch->stripecap = 0xff;
5281230130Smav
5282230130Smav	ret = 0;
5283230130Smav	channels = 0;
5284230130Smav	onlystereo = 1;
5285230130Smav	pinset = 0;
5286230130Smav	fmtcap = devinfo->supp_stream_formats;
5287230130Smav	pcmcap = devinfo->supp_pcm_size_rate;
5288230130Smav
5289230130Smav	for (i = 0; i < 16; i++) {
5290230130Smav		/* Check as is correct */
5291230130Smav		if (ch->as < 0)
5292230130Smav			break;
5293230130Smav		/* Cound only present DACs */
5294230130Smav		if (as[ch->as].dacs[ch->asindex][i] <= 0)
5295230130Smav			continue;
5296230130Smav		/* Ignore duplicates */
5297230130Smav		for (j = 0; j < ret; j++) {
5298230130Smav			if (ch->io[j] == as[ch->as].dacs[ch->asindex][i])
5299230130Smav				break;
5300230130Smav		}
5301230130Smav		if (j < ret)
5302230130Smav			continue;
5303230130Smav
5304230130Smav		w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]);
5305230130Smav		if (w == NULL || w->enable == 0)
5306230130Smav			continue;
5307230130Smav		cap = w->param.supp_stream_formats;
5308230130Smav		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
5309230130Smav		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5310230130Smav			continue;
5311230130Smav		/* Many CODECs does not declare AC3 support on SPDIF.
5312230130Smav		   I don't beleave that they doesn't support it! */
5313230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5314230130Smav			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
5315230130Smav		if (ret == 0) {
5316230130Smav			fmtcap = cap;
5317230130Smav			pcmcap = w->param.supp_pcm_size_rate;
5318230130Smav		} else {
5319230130Smav			fmtcap &= cap;
5320230130Smav			pcmcap &= w->param.supp_pcm_size_rate;
5321230130Smav		}
5322230130Smav		ch->io[ret++] = as[ch->as].dacs[ch->asindex][i];
5323230326Smav		ch->stripecap &= w->wclass.conv.stripecap;
5324230130Smav		/* Do not count redirection pin/dac channels. */
5325230130Smav		if (i == 15 && as[ch->as].hpredir >= 0)
5326230130Smav			continue;
5327230130Smav		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
5328230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
5329230130Smav			onlystereo = 0;
5330230130Smav		pinset |= (1 << i);
5331230130Smav	}
5332230130Smav	ch->io[ret] = -1;
5333230130Smav	ch->channels = channels;
5334230130Smav
5335230130Smav	if (as[ch->as].fakeredir)
5336230130Smav		ret--;
5337230130Smav	/* Standard speaks only about stereo pins and playback, ... */
5338230130Smav	if ((!onlystereo) || as[ch->as].mixed)
5339230130Smav		pinset = 0;
5340230130Smav	/* ..., but there it gives us info about speakers layout. */
5341230130Smav	as[ch->as].pinset = pinset;
5342230130Smav
5343230130Smav	ch->supp_stream_formats = fmtcap;
5344230130Smav	ch->supp_pcm_size_rate = pcmcap;
5345230130Smav
5346230130Smav	/*
5347230130Smav	 *  8bit = 0
5348230130Smav	 * 16bit = 1
5349230130Smav	 * 20bit = 2
5350230130Smav	 * 24bit = 3
5351230130Smav	 * 32bit = 4
5352230130Smav	 */
5353230130Smav	if (ret > 0) {
5354230130Smav		i = 0;
5355230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
5356230130Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
5357230130Smav				ch->bit16 = 1;
5358230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
5359230130Smav				ch->bit16 = 0;
5360230488Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
5361230130Smav				ch->bit32 = 3;
5362230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
5363230130Smav				ch->bit32 = 2;
5364230488Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
5365230488Smav				ch->bit32 = 4;
5366230130Smav			if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) {
5367230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
5368230130Smav				if (ch->bit32)
5369230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
5370230130Smav			}
5371230130Smav			if (channels >= 2) {
5372230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
5373230130Smav				if (ch->bit32)
5374230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
5375230130Smav			}
5376230312Smav			if (channels >= 3 && !onlystereo) {
5377230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0);
5378230312Smav				if (ch->bit32)
5379230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0);
5380230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1);
5381230312Smav				if (ch->bit32)
5382230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1);
5383230312Smav			}
5384230312Smav			if (channels >= 4) {
5385230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
5386230130Smav				if (ch->bit32)
5387230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
5388230312Smav				if (!onlystereo) {
5389230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1);
5390230312Smav					if (ch->bit32)
5391230312Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1);
5392230312Smav				}
5393230130Smav			}
5394230312Smav			if (channels >= 5 && !onlystereo) {
5395230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0);
5396230312Smav				if (ch->bit32)
5397230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0);
5398230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1);
5399230312Smav				if (ch->bit32)
5400230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1);
5401230312Smav			}
5402230312Smav			if (channels >= 6) {
5403230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
5404230130Smav				if (ch->bit32)
5405230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
5406230312Smav				if (!onlystereo) {
5407230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0);
5408230312Smav					if (ch->bit32)
5409230312Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0);
5410230312Smav				}
5411230130Smav			}
5412230312Smav			if (channels >= 7 && !onlystereo) {
5413230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0);
5414230312Smav				if (ch->bit32)
5415230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0);
5416230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1);
5417230312Smav				if (ch->bit32)
5418230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1);
5419230312Smav			}
5420230312Smav			if (channels >= 8) {
5421230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
5422230130Smav				if (ch->bit32)
5423230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
5424230130Smav			}
5425230130Smav		}
5426230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
5427230130Smav			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
5428230513Smav			if (channels >= 8) {
5429230513Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 0);
5430230513Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 1);
5431230513Smav			}
5432230130Smav		}
5433230130Smav		ch->fmtlist[i] = 0;
5434230130Smav		i = 0;
5435230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
5436230130Smav			ch->pcmrates[i++] = 8000;
5437230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
5438230130Smav			ch->pcmrates[i++] = 11025;
5439230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
5440230130Smav			ch->pcmrates[i++] = 16000;
5441230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
5442230130Smav			ch->pcmrates[i++] = 22050;
5443230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
5444230130Smav			ch->pcmrates[i++] = 32000;
5445230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
5446230130Smav			ch->pcmrates[i++] = 44100;
5447230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
5448230130Smav		ch->pcmrates[i++] = 48000;
5449230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
5450230130Smav			ch->pcmrates[i++] = 88200;
5451230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
5452230130Smav			ch->pcmrates[i++] = 96000;
5453230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
5454230130Smav			ch->pcmrates[i++] = 176400;
5455230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
5456230130Smav			ch->pcmrates[i++] = 192000;
5457230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
5458230130Smav		ch->pcmrates[i] = 0;
5459230130Smav		if (i > 0) {
5460230130Smav			ch->caps.minspeed = ch->pcmrates[0];
5461230130Smav			ch->caps.maxspeed = ch->pcmrates[i - 1];
5462230130Smav		}
5463230130Smav	}
5464230130Smav
5465230130Smav	return (ret);
5466230130Smav}
5467230130Smav
5468230130Smavstatic void
5469230451Smavhdaa_prepare_pcms(struct hdaa_devinfo *devinfo)
5470230130Smav{
5471230130Smav	struct hdaa_audio_as *as = devinfo->as;
5472230130Smav	int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
5473230130Smav
5474230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5475230130Smav		if (as[i].enable == 0)
5476230130Smav			continue;
5477230130Smav		if (as[i].dir == HDAA_CTL_IN) {
5478230130Smav			if (as[i].digital)
5479230130Smav				drdev++;
5480230130Smav			else
5481230130Smav				ardev++;
5482230130Smav		} else {
5483230130Smav			if (as[i].digital)
5484230130Smav				dpdev++;
5485230130Smav			else
5486230130Smav				apdev++;
5487230130Smav		}
5488230130Smav	}
5489230130Smav	devinfo->num_devs =
5490230130Smav	    max(ardev, apdev) + max(drdev, dpdev);
5491230130Smav	devinfo->devs =
5492230130Smav	    (struct hdaa_pcm_devinfo *)malloc(
5493230130Smav	    devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo),
5494230130Smav	    M_HDAA, M_ZERO | M_NOWAIT);
5495230130Smav	if (devinfo->devs == NULL) {
5496230130Smav		device_printf(devinfo->dev,
5497230130Smav		    "Unable to allocate memory for devices\n");
5498230130Smav		return;
5499230130Smav	}
5500230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5501230130Smav		devinfo->devs[i].index = i;
5502230130Smav		devinfo->devs[i].devinfo = devinfo;
5503230130Smav		devinfo->devs[i].playas = -1;
5504230130Smav		devinfo->devs[i].recas = -1;
5505230130Smav		devinfo->devs[i].digital = 255;
5506230130Smav	}
5507230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5508230130Smav		if (as[i].enable == 0)
5509230130Smav			continue;
5510230130Smav		for (j = 0; j < devinfo->num_devs; j++) {
5511230130Smav			if (devinfo->devs[j].digital != 255 &&
5512230130Smav			    (!devinfo->devs[j].digital) !=
5513230130Smav			    (!as[i].digital))
5514230130Smav				continue;
5515230130Smav			if (as[i].dir == HDAA_CTL_IN) {
5516230130Smav				if (devinfo->devs[j].recas >= 0)
5517230130Smav					continue;
5518230130Smav				devinfo->devs[j].recas = i;
5519230130Smav			} else {
5520230130Smav				if (devinfo->devs[j].playas >= 0)
5521230130Smav					continue;
5522230130Smav				devinfo->devs[j].playas = i;
5523230130Smav			}
5524230451Smav			as[i].pdevinfo = &devinfo->devs[j];
5525230130Smav			for (k = 0; k < as[i].num_chans; k++) {
5526230130Smav				devinfo->chans[as[i].chans[k]].pdevinfo =
5527230130Smav				    &devinfo->devs[j];
5528230130Smav			}
5529230130Smav			devinfo->devs[j].digital = as[i].digital;
5530230130Smav			break;
5531230130Smav		}
5532230130Smav	}
5533230451Smav}
5534230451Smav
5535230451Smavstatic void
5536230451Smavhdaa_create_pcms(struct hdaa_devinfo *devinfo)
5537230451Smav{
5538230451Smav	int i;
5539230451Smav
5540230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5541230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
5542230130Smav
5543230130Smav		pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1);
5544230130Smav		device_set_ivars(pdevinfo->dev, (void *)pdevinfo);
5545230130Smav	}
5546230130Smav}
5547230130Smav
5548230130Smavstatic void
5549230130Smavhdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
5550230130Smav{
5551230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5552230130Smav	struct hdaa_audio_ctl *ctl;
5553230130Smav	char buf[64];
5554243535Smav	int i, j, printed = 0;
5555230130Smav
5556230130Smav	if (flag == 0) {
5557230130Smav		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5558230130Smav		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5559230130Smav		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
5560230130Smav		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
5561230130Smav	}
5562230130Smav
5563230130Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
5564230130Smav		if ((flag & (1 << j)) == 0)
5565230130Smav			continue;
5566230130Smav		i = 0;
5567230130Smav		printed = 0;
5568230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5569230130Smav			if (ctl->enable == 0 ||
5570230130Smav			    ctl->widget->enable == 0)
5571230130Smav				continue;
5572230130Smav			if (!((pdevinfo->playas >= 0 &&
5573230130Smav			    ctl->widget->bindas == pdevinfo->playas) ||
5574230130Smav			    (pdevinfo->recas >= 0 &&
5575230130Smav			    ctl->widget->bindas == pdevinfo->recas) ||
5576230130Smav			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
5577230130Smav				continue;
5578230130Smav			if ((ctl->ossmask & (1 << j)) == 0)
5579230130Smav				continue;
5580230130Smav
5581230130Smav			if (printed == 0) {
5582230130Smav				if (banner != NULL) {
5583230130Smav					device_printf(pdevinfo->dev, "%s", banner);
5584230130Smav				} else {
5585230130Smav					device_printf(pdevinfo->dev, "Unknown Ctl");
5586230130Smav				}
5587230451Smav				printf(" (OSS: %s)",
5588230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(1 << j,
5589230130Smav				    buf, sizeof(buf)));
5590230451Smav				if (pdevinfo->ossmask & (1 << j)) {
5591230451Smav					printf(": %+d/%+ddB\n",
5592230451Smav					    pdevinfo->minamp[j] / 4,
5593230451Smav					    pdevinfo->maxamp[j] / 4);
5594230451Smav				} else
5595230451Smav					printf("\n");
5596230130Smav				printed = 1;
5597230130Smav			}
5598230130Smav			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
5599230130Smav				ctl->widget->nid,
5600230130Smav				(ctl->ndir == HDAA_CTL_IN)?"in ":"out");
5601230130Smav			if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir)
5602230130Smav				printf(" %2d): ", ctl->index);
5603230130Smav			else
5604230130Smav				printf("):    ");
5605230130Smav			if (ctl->step > 0) {
5606230130Smav				printf("%+d/%+ddB (%d steps)%s\n",
5607230451Smav				    MINQDB(ctl) / 4,
5608230451Smav				    MAXQDB(ctl) / 4,
5609230130Smav				    ctl->step + 1,
5610230130Smav				    ctl->mute?" + mute":"");
5611230130Smav			} else
5612230130Smav				printf("%s\n", ctl->mute?"mute":"");
5613230130Smav		}
5614230130Smav	}
5615243530Smav	if (printed)
5616243530Smav		device_printf(pdevinfo->dev, "\n");
5617230130Smav}
5618230130Smav
5619230130Smavstatic void
5620230130Smavhdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
5621230130Smav{
5622230130Smav	uint32_t cap;
5623230130Smav
5624230130Smav	cap = fcap;
5625230130Smav	if (cap != 0) {
5626243530Smav		device_printf(dev, "     Stream cap: 0x%08x", cap);
5627230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5628230130Smav			printf(" AC3");
5629230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5630230130Smav			printf(" FLOAT32");
5631230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5632230130Smav			printf(" PCM");
5633230130Smav		printf("\n");
5634230130Smav	}
5635230130Smav	cap = pcmcap;
5636230130Smav	if (cap != 0) {
5637243530Smav		device_printf(dev, "        PCM cap: 0x%08x", cap);
5638230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5639230130Smav			printf(" 8");
5640230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5641230130Smav			printf(" 16");
5642230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5643230130Smav			printf(" 20");
5644230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5645230130Smav			printf(" 24");
5646230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5647230130Smav			printf(" 32");
5648230130Smav		printf(" bits,");
5649230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5650230130Smav			printf(" 8");
5651230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5652230130Smav			printf(" 11");
5653230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5654230130Smav			printf(" 16");
5655230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5656230130Smav			printf(" 22");
5657230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5658230130Smav			printf(" 32");
5659230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5660230130Smav			printf(" 44");
5661230130Smav		printf(" 48");
5662230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5663230130Smav			printf(" 88");
5664230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5665230130Smav			printf(" 96");
5666230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5667230130Smav			printf(" 176");
5668230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5669230130Smav			printf(" 192");
5670230130Smav		printf(" KHz\n");
5671230130Smav	}
5672230130Smav}
5673230130Smav
5674230130Smavstatic void
5675230130Smavhdaa_dump_pin(struct hdaa_widget *w)
5676230130Smav{
5677230130Smav	uint32_t pincap;
5678230130Smav
5679230130Smav	pincap = w->wclass.pin.cap;
5680230130Smav
5681243530Smav	device_printf(w->devinfo->dev, "        Pin cap: 0x%08x", pincap);
5682230130Smav	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5683230130Smav		printf(" ISC");
5684230130Smav	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5685230130Smav		printf(" TRQD");
5686230130Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5687230130Smav		printf(" PDC");
5688230130Smav	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5689230130Smav		printf(" HP");
5690230130Smav	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5691230130Smav		printf(" OUT");
5692230130Smav	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5693230130Smav		printf(" IN");
5694230130Smav	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5695230130Smav		printf(" BAL");
5696230130Smav	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
5697230130Smav		printf(" HDMI");
5698230130Smav	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5699230130Smav		printf(" VREF[");
5700230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5701230130Smav			printf(" 50");
5702230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5703230130Smav			printf(" 80");
5704230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5705230130Smav			printf(" 100");
5706230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5707230130Smav			printf(" GROUND");
5708230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5709230130Smav			printf(" HIZ");
5710230130Smav		printf(" ]");
5711230130Smav	}
5712230130Smav	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5713230130Smav		printf(" EAPD");
5714230130Smav	if (HDA_PARAM_PIN_CAP_DP(pincap))
5715230130Smav		printf(" DP");
5716230130Smav	if (HDA_PARAM_PIN_CAP_HBR(pincap))
5717230130Smav		printf(" HBR");
5718230130Smav	printf("\n");
5719230130Smav	device_printf(w->devinfo->dev, "     Pin config: 0x%08x\n",
5720230130Smav	    w->wclass.pin.config);
5721230130Smav	device_printf(w->devinfo->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
5722230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5723230130Smav		printf(" HP");
5724230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5725230130Smav		printf(" IN");
5726230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5727230130Smav		printf(" OUT");
5728230312Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
5729230312Smav		if ((w->wclass.pin.ctrl &
5730230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
5731230312Smav			printf(" HBR");
5732230312Smav		else if ((w->wclass.pin.ctrl &
5733230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5734230312Smav			printf(" EPTs");
5735230312Smav	} else {
5736230312Smav		if ((w->wclass.pin.ctrl &
5737230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5738230312Smav			printf(" VREFs");
5739230312Smav	}
5740230130Smav	printf("\n");
5741230130Smav}
5742230130Smav
5743230130Smavstatic void
5744230130Smavhdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf)
5745230130Smav{
5746230130Smav
5747230130Smav	device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d "
5748230130Smav	    "%-13s %-5s %-7s %-10s %-7s %d%s\n",
5749230130Smav	    w->nid, conf,
5750230130Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
5751230130Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
5752230130Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
5753230130Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
5754230130Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
5755230130Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
5756230130Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
5757230130Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
5758230130Smav	    (w->enable == 0)?" DISA":"");
5759230130Smav}
5760230130Smav
5761230130Smavstatic void
5762230130Smavhdaa_dump_pin_configs(struct hdaa_devinfo *devinfo)
5763230130Smav{
5764230130Smav	struct hdaa_widget *w;
5765230130Smav	int i;
5766230130Smav
5767230130Smav	device_printf(devinfo->dev, "nid   0x    as seq "
5768230130Smav	    "device       conn  jack    loc        color   misc\n");
5769230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5770230130Smav		w = hdaa_widget_get(devinfo, i);
5771230130Smav		if (w == NULL)
5772230130Smav			continue;
5773230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5774230130Smav			continue;
5775230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
5776230130Smav	}
5777230130Smav}
5778230130Smav
5779230130Smavstatic void
5780264832Smariushdaa_dump_amp(device_t dev, uint32_t cap, const char *banner)
5781230130Smav{
5782243530Smav	int offset, size, step;
5783243530Smav
5784243530Smav	offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap);
5785243530Smav	size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap);
5786243530Smav	step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap);
5787243530Smav	device_printf(dev, "     %s amp: 0x%08x "
5788243530Smav	    "mute=%d step=%d size=%d offset=%d (%+d/%+ddB)\n",
5789243530Smav	    banner, cap,
5790230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5791243530Smav	    step, size, offset,
5792243530Smav	    ((0 - offset) * (size + 1)) / 4,
5793243530Smav	    ((step - offset) * (size + 1)) / 4);
5794230130Smav}
5795230130Smav
5796230130Smavstatic void
5797230130Smavhdaa_dump_nodes(struct hdaa_devinfo *devinfo)
5798230130Smav{
5799230130Smav	struct hdaa_widget *w, *cw;
5800230130Smav	char buf[64];
5801230130Smav	int i, j;
5802230130Smav
5803230130Smav	device_printf(devinfo->dev, "\n");
5804243530Smav	device_printf(devinfo->dev, "Default parameters:\n");
5805230130Smav	hdaa_dump_audio_formats(devinfo->dev,
5806230130Smav	    devinfo->supp_stream_formats,
5807230130Smav	    devinfo->supp_pcm_size_rate);
5808243530Smav	hdaa_dump_amp(devinfo->dev, devinfo->inamp_cap, " Input");
5809243530Smav	hdaa_dump_amp(devinfo->dev, devinfo->outamp_cap, "Output");
5810230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5811230130Smav		w = hdaa_widget_get(devinfo, i);
5812230130Smav		if (w == NULL) {
5813230130Smav			device_printf(devinfo->dev, "Ghost widget nid=%d\n", i);
5814230130Smav			continue;
5815230130Smav		}
5816230130Smav		device_printf(devinfo->dev, "\n");
5817230130Smav		device_printf(devinfo->dev, "            nid: %d%s\n", w->nid,
5818230130Smav		    (w->enable == 0) ? " [DISABLED]" : "");
5819230130Smav		device_printf(devinfo->dev, "           Name: %s\n", w->name);
5820243530Smav		device_printf(devinfo->dev, "     Widget cap: 0x%08x",
5821230130Smav		    w->param.widget_cap);
5822230130Smav		if (w->param.widget_cap & 0x0ee1) {
5823230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
5824230130Smav			    printf(" LRSWAP");
5825230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
5826230130Smav			    printf(" PWR");
5827230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5828230130Smav			    printf(" DIGITAL");
5829230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
5830230130Smav			    printf(" UNSOL");
5831230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
5832230130Smav			    printf(" PROC");
5833230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
5834230326Smav			    printf(" STRIPE(x%d)",
5835230326Smav				1 << (fls(w->wclass.conv.stripecap) - 1));
5836230130Smav			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
5837230130Smav			if (j == 1)
5838230130Smav			    printf(" STEREO");
5839230130Smav			else if (j > 1)
5840230130Smav			    printf(" %dCH", j + 1);
5841230130Smav		}
5842243530Smav		printf("\n");
5843230130Smav		if (w->bindas != -1) {
5844243530Smav			device_printf(devinfo->dev, "    Association: %d (0x%04x)\n",
5845230130Smav			    w->bindas, w->bindseqmask);
5846230130Smav		}
5847230130Smav		if (w->ossmask != 0 || w->ossdev >= 0) {
5848230130Smav			device_printf(devinfo->dev, "            OSS: %s",
5849230130Smav			    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
5850230130Smav			if (w->ossdev >= 0)
5851230451Smav			    printf(" (%s)", ossnames[w->ossdev]);
5852230130Smav			printf("\n");
5853230130Smav		}
5854230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5855230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5856230130Smav			hdaa_dump_audio_formats(devinfo->dev,
5857230130Smav			    w->param.supp_stream_formats,
5858230130Smav			    w->param.supp_pcm_size_rate);
5859230130Smav		} else if (w->type ==
5860230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
5861230130Smav			hdaa_dump_pin(w);
5862230130Smav		if (w->param.eapdbtl != HDA_INVALID)
5863230130Smav			device_printf(devinfo->dev, "           EAPD: 0x%08x\n",
5864230130Smav			    w->param.eapdbtl);
5865230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5866230130Smav		    w->param.outamp_cap != 0)
5867230130Smav			hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output");
5868230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5869230130Smav		    w->param.inamp_cap != 0)
5870230130Smav			hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input");
5871243530Smav		if (w->nconns > 0)
5872243530Smav			device_printf(devinfo->dev, "    Connections: %d\n", w->nconns);
5873230130Smav		for (j = 0; j < w->nconns; j++) {
5874230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
5875230130Smav			device_printf(devinfo->dev, "          + %s<- nid=%d [%s]",
5876230130Smav			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
5877230130Smav			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5878230130Smav			if (cw == NULL)
5879230130Smav				printf(" [UNKNOWN]");
5880230130Smav			else if (cw->enable == 0)
5881230130Smav				printf(" [DISABLED]");
5882230130Smav			if (w->nconns > 1 && w->selconn == j && w->type !=
5883230130Smav			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5884230130Smav				printf(" (selected)");
5885230130Smav			printf("\n");
5886230130Smav		}
5887230130Smav	}
5888230130Smav
5889230130Smav}
5890230130Smav
5891230130Smavstatic void
5892230130Smavhdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth)
5893230130Smav{
5894230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5895230130Smav	struct hdaa_widget *w, *cw;
5896230130Smav	char buf[64];
5897243530Smav	int i;
5898230130Smav
5899230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
5900230130Smav		return;
5901230130Smav
5902230130Smav	w = hdaa_widget_get(devinfo, nid);
5903230130Smav	if (w == NULL || w->enable == 0)
5904230130Smav		return;
5905230130Smav
5906230130Smav	if (depth == 0)
5907230130Smav		device_printf(pdevinfo->dev, "%*s", 4, "");
5908230130Smav	else
5909230130Smav		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
5910230130Smav	printf("nid=%d [%s]", w->nid, w->name);
5911230130Smav
5912230130Smav	if (depth > 0) {
5913230130Smav		if (w->ossmask == 0) {
5914230130Smav			printf("\n");
5915230130Smav			return;
5916230130Smav		}
5917230130Smav		printf(" [src: %s]",
5918230130Smav		    hdaa_audio_ctl_ossmixer_mask2allname(
5919230130Smav			w->ossmask, buf, sizeof(buf)));
5920230130Smav		if (w->ossdev >= 0) {
5921230130Smav			printf("\n");
5922230130Smav			return;
5923230130Smav		}
5924230130Smav	}
5925230130Smav	printf("\n");
5926230130Smav
5927230130Smav	for (i = 0; i < w->nconns; i++) {
5928230130Smav		if (w->connsenable[i] == 0)
5929230130Smav			continue;
5930230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
5931230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
5932230130Smav			continue;
5933230130Smav		hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
5934230130Smav	}
5935230130Smav
5936230130Smav}
5937230130Smav
5938230130Smavstatic void
5939230130Smavhdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo)
5940230130Smav{
5941230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5942230130Smav	struct hdaa_audio_as *as;
5943230130Smav	struct hdaa_widget *w;
5944243530Smav	nid_t *nids;
5945243530Smav	int chid, i;
5946230130Smav
5947230130Smav	if (pdevinfo->playas < 0)
5948230130Smav		return;
5949230130Smav
5950243530Smav	device_printf(pdevinfo->dev, "Playback:\n");
5951243530Smav
5952243530Smav	chid = devinfo->as[pdevinfo->playas].chans[0];
5953243530Smav	hdaa_dump_audio_formats(pdevinfo->dev,
5954243530Smav	    devinfo->chans[chid].supp_stream_formats,
5955243530Smav	    devinfo->chans[chid].supp_pcm_size_rate);
5956243530Smav	for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) {
5957243530Smav		chid = devinfo->as[pdevinfo->playas].chans[i];
5958243530Smav		device_printf(pdevinfo->dev, "            DAC:");
5959243530Smav		for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5960243530Smav			printf(" %d", *nids);
5961243530Smav		printf("\n");
5962243530Smav	}
5963243530Smav
5964230130Smav	as = &devinfo->as[pdevinfo->playas];
5965230130Smav	for (i = 0; i < 16; i++) {
5966230130Smav		if (as->pins[i] <= 0)
5967230130Smav			continue;
5968230130Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
5969230130Smav		if (w == NULL || w->enable == 0)
5970230130Smav			continue;
5971230130Smav		device_printf(pdevinfo->dev, "\n");
5972230130Smav		hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0);
5973230130Smav	}
5974243530Smav	device_printf(pdevinfo->dev, "\n");
5975230130Smav}
5976230130Smav
5977230130Smavstatic void
5978230130Smavhdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo)
5979230130Smav{
5980230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5981230130Smav	struct hdaa_widget *w;
5982243530Smav	nid_t *nids;
5983243530Smav	int chid, i;
5984230130Smav
5985230130Smav	if (pdevinfo->recas < 0)
5986230130Smav		return;
5987230130Smav
5988243530Smav	device_printf(pdevinfo->dev, "Record:\n");
5989243530Smav
5990243530Smav	chid = devinfo->as[pdevinfo->recas].chans[0];
5991243530Smav	hdaa_dump_audio_formats(pdevinfo->dev,
5992243530Smav	    devinfo->chans[chid].supp_stream_formats,
5993243530Smav	    devinfo->chans[chid].supp_pcm_size_rate);
5994243530Smav	for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) {
5995243530Smav		chid = devinfo->as[pdevinfo->recas].chans[i];
5996243530Smav		device_printf(pdevinfo->dev, "            ADC:");
5997243530Smav		for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5998243530Smav			printf(" %d", *nids);
5999243530Smav		printf("\n");
6000243530Smav	}
6001243530Smav
6002230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6003230130Smav		w = hdaa_widget_get(devinfo, i);
6004230130Smav		if (w == NULL || w->enable == 0)
6005230130Smav			continue;
6006230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
6007230130Smav			continue;
6008230130Smav		if (w->bindas != pdevinfo->recas)
6009230130Smav			continue;
6010230130Smav		device_printf(pdevinfo->dev, "\n");
6011230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
6012230130Smav	}
6013243530Smav	device_printf(pdevinfo->dev, "\n");
6014230130Smav}
6015230130Smav
6016230130Smavstatic void
6017230130Smavhdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo)
6018230130Smav{
6019230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6020230130Smav	struct hdaa_widget *w;
6021230130Smav	int i;
6022230130Smav	int printed = 0;
6023230130Smav
6024230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6025230130Smav		w = hdaa_widget_get(devinfo, i);
6026230130Smav		if (w == NULL || w->enable == 0)
6027230130Smav			continue;
6028230130Smav		if (w->ossdev != SOUND_MIXER_IMIX)
6029230130Smav			continue;
6030230451Smav		if (w->bindas != pdevinfo->recas)
6031230451Smav			continue;
6032230130Smav		if (printed == 0) {
6033230130Smav			printed = 1;
6034230130Smav			device_printf(pdevinfo->dev, "Input Mix:\n");
6035230130Smav		}
6036230130Smav		device_printf(pdevinfo->dev, "\n");
6037230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
6038230130Smav	}
6039243530Smav	if (printed)
6040230130Smav		device_printf(pdevinfo->dev, "\n");
6041230130Smav}
6042230130Smav
6043230130Smavstatic void
6044230130Smavhdaa_pindump(device_t dev)
6045230130Smav{
6046230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6047230130Smav	struct hdaa_widget *w;
6048230130Smav	uint32_t res, pincap, delay;
6049230130Smav	int i;
6050230130Smav
6051230130Smav	device_printf(dev, "Dumping AFG pins:\n");
6052230130Smav	device_printf(dev, "nid   0x    as seq "
6053230130Smav	    "device       conn  jack    loc        color   misc\n");
6054230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6055230130Smav		w = hdaa_widget_get(devinfo, i);
6056230130Smav		if (w == NULL || w->type !=
6057230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6058230130Smav			continue;
6059230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
6060230130Smav		pincap = w->wclass.pin.cap;
6061230130Smav		device_printf(dev, "    Caps: %2s %3s %2s %4s %4s",
6062230130Smav		    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
6063230130Smav		    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
6064230130Smav		    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
6065230130Smav		    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
6066230130Smav		    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
6067230130Smav		if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
6068230130Smav		    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
6069230130Smav			if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
6070230130Smav				delay = 0;
6071230130Smav				hda_command(dev,
6072230130Smav				    HDA_CMD_SET_PIN_SENSE(0, w->nid, 0));
6073230130Smav				do {
6074230130Smav					res = hda_command(dev,
6075230130Smav					    HDA_CMD_GET_PIN_SENSE(0, w->nid));
6076230130Smav					if (res != 0x7fffffff && res != 0xffffffff)
6077230130Smav						break;
6078230130Smav					DELAY(10);
6079230130Smav				} while (++delay < 10000);
6080230130Smav			} else {
6081230130Smav				delay = 0;
6082230130Smav				res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0,
6083230130Smav				    w->nid));
6084230130Smav			}
6085230312Smav			printf(" Sense: 0x%08x (%sconnected%s)", res,
6086230130Smav			    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ?
6087230312Smav			     "" : "dis",
6088230312Smav			    (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) &&
6089230312Smav			     (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ?
6090230312Smav			      ", ELD valid" : "");
6091230130Smav			if (delay > 0)
6092230130Smav				printf(" delay %dus", delay * 10);
6093230130Smav		}
6094230130Smav		printf("\n");
6095230130Smav	}
6096230130Smav	device_printf(dev,
6097230130Smav	    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
6098230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
6099230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
6100230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
6101230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
6102230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
6103230130Smav	hdaa_dump_gpi(devinfo);
6104230130Smav	hdaa_dump_gpio(devinfo);
6105230130Smav	hdaa_dump_gpo(devinfo);
6106230130Smav}
6107230130Smav
6108230130Smavstatic void
6109230130Smavhdaa_configure(device_t dev)
6110230130Smav{
6111230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6112230130Smav	struct hdaa_audio_ctl *ctl;
6113230130Smav	int i;
6114230130Smav
6115230130Smav	HDA_BOOTHVERBOSE(
6116230130Smav		device_printf(dev, "Applying built-in patches...\n");
6117230130Smav	);
6118230130Smav	hdaa_patch(devinfo);
6119230130Smav	HDA_BOOTHVERBOSE(
6120230130Smav		device_printf(dev, "Applying local patches...\n");
6121230130Smav	);
6122230130Smav	hdaa_local_patch(devinfo);
6123230130Smav	hdaa_audio_postprocess(devinfo);
6124230130Smav	HDA_BOOTHVERBOSE(
6125230130Smav		device_printf(dev, "Parsing Ctls...\n");
6126230130Smav	);
6127230130Smav	hdaa_audio_ctl_parse(devinfo);
6128230130Smav	HDA_BOOTHVERBOSE(
6129230130Smav		device_printf(dev, "Disabling nonaudio...\n");
6130230130Smav	);
6131230130Smav	hdaa_audio_disable_nonaudio(devinfo);
6132230130Smav	HDA_BOOTHVERBOSE(
6133230130Smav		device_printf(dev, "Disabling useless...\n");
6134230130Smav	);
6135230130Smav	hdaa_audio_disable_useless(devinfo);
6136230130Smav	HDA_BOOTVERBOSE(
6137230130Smav		device_printf(dev, "Patched pins configuration:\n");
6138230130Smav		hdaa_dump_pin_configs(devinfo);
6139230130Smav	);
6140230130Smav	HDA_BOOTHVERBOSE(
6141230130Smav		device_printf(dev, "Parsing pin associations...\n");
6142230130Smav	);
6143230130Smav	hdaa_audio_as_parse(devinfo);
6144230130Smav	HDA_BOOTHVERBOSE(
6145230130Smav		device_printf(dev, "Building AFG tree...\n");
6146230130Smav	);
6147230130Smav	hdaa_audio_build_tree(devinfo);
6148230130Smav	HDA_BOOTHVERBOSE(
6149230130Smav		device_printf(dev, "Disabling unassociated "
6150230130Smav		    "widgets...\n");
6151230130Smav	);
6152230130Smav	hdaa_audio_disable_unas(devinfo);
6153230130Smav	HDA_BOOTHVERBOSE(
6154230130Smav		device_printf(dev, "Disabling nonselected "
6155230130Smav		    "inputs...\n");
6156230130Smav	);
6157230130Smav	hdaa_audio_disable_notselected(devinfo);
6158230130Smav	HDA_BOOTHVERBOSE(
6159230130Smav		device_printf(dev, "Disabling useless...\n");
6160230130Smav	);
6161230130Smav	hdaa_audio_disable_useless(devinfo);
6162230130Smav	HDA_BOOTHVERBOSE(
6163230130Smav		device_printf(dev, "Disabling "
6164230130Smav		    "crossassociatement connections...\n");
6165230130Smav	);
6166230130Smav	hdaa_audio_disable_crossas(devinfo);
6167230130Smav	HDA_BOOTHVERBOSE(
6168230130Smav		device_printf(dev, "Disabling useless...\n");
6169230130Smav	);
6170230130Smav	hdaa_audio_disable_useless(devinfo);
6171230130Smav	HDA_BOOTHVERBOSE(
6172230130Smav		device_printf(dev, "Binding associations to channels...\n");
6173230130Smav	);
6174230130Smav	hdaa_audio_bind_as(devinfo);
6175230130Smav	HDA_BOOTHVERBOSE(
6176230130Smav		device_printf(dev, "Assigning names to signal sources...\n");
6177230130Smav	);
6178230130Smav	hdaa_audio_assign_names(devinfo);
6179230130Smav	HDA_BOOTHVERBOSE(
6180230451Smav		device_printf(dev, "Preparing PCM devices...\n");
6181230451Smav	);
6182230451Smav	hdaa_prepare_pcms(devinfo);
6183230451Smav	HDA_BOOTHVERBOSE(
6184230130Smav		device_printf(dev, "Assigning mixers to the tree...\n");
6185230130Smav	);
6186230130Smav	hdaa_audio_assign_mixers(devinfo);
6187230130Smav	HDA_BOOTHVERBOSE(
6188230130Smav		device_printf(dev, "Preparing pin controls...\n");
6189230130Smav	);
6190230130Smav	hdaa_audio_prepare_pin_ctrl(devinfo);
6191230130Smav	HDA_BOOTHVERBOSE(
6192230130Smav		device_printf(dev, "AFG commit...\n");
6193230130Smav	);
6194230130Smav	hdaa_audio_commit(devinfo);
6195230130Smav	HDA_BOOTHVERBOSE(
6196230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
6197230130Smav	);
6198230130Smav	hdaa_patch_direct(devinfo);
6199230130Smav	HDA_BOOTHVERBOSE(
6200230551Smav		device_printf(dev, "Pin sense init...\n");
6201230312Smav	);
6202230551Smav	hdaa_sense_init(devinfo);
6203230312Smav	HDA_BOOTHVERBOSE(
6204230130Smav		device_printf(dev, "Creating PCM devices...\n");
6205230130Smav	);
6206230130Smav	hdaa_create_pcms(devinfo);
6207230130Smav
6208230130Smav	HDA_BOOTVERBOSE(
6209230130Smav		if (devinfo->quirks != 0) {
6210230130Smav			device_printf(dev, "FG config/quirks:");
6211264832Smarius			for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
6212230130Smav				if ((devinfo->quirks &
6213230130Smav				    hdaa_quirks_tab[i].value) ==
6214230130Smav				    hdaa_quirks_tab[i].value)
6215230130Smav					printf(" %s", hdaa_quirks_tab[i].key);
6216230130Smav			}
6217230130Smav			printf("\n");
6218230130Smav		}
6219243530Smav	);
6220230130Smav
6221243530Smav	HDA_BOOTHVERBOSE(
6222230130Smav		device_printf(dev, "\n");
6223243530Smav		device_printf(dev, "+-----------+\n");
6224243530Smav		device_printf(dev, "| HDA NODES |\n");
6225243530Smav		device_printf(dev, "+-----------+\n");
6226230130Smav		hdaa_dump_nodes(devinfo);
6227230130Smav
6228230130Smav		device_printf(dev, "\n");
6229243530Smav		device_printf(dev, "+----------------+\n");
6230243530Smav		device_printf(dev, "| HDA AMPLIFIERS |\n");
6231243530Smav		device_printf(dev, "+----------------+\n");
6232230130Smav		device_printf(dev, "\n");
6233230130Smav		i = 0;
6234230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
6235230130Smav			device_printf(dev, "%3d: nid %3d %s (%s) index %d", i,
6236230130Smav			    (ctl->widget != NULL) ? ctl->widget->nid : -1,
6237230130Smav			    (ctl->ndir == HDAA_CTL_IN)?"in ":"out",
6238230130Smav			    (ctl->dir == HDAA_CTL_IN)?"in ":"out",
6239230130Smav			    ctl->index);
6240230130Smav			if (ctl->childwidget != NULL)
6241230130Smav				printf(" cnid %3d", ctl->childwidget->nid);
6242230130Smav			else
6243230130Smav				printf("         ");
6244230130Smav			printf(" ossmask=0x%08x\n",
6245230130Smav			    ctl->ossmask);
6246230130Smav			device_printf(dev,
6247230130Smav			    "       mute: %d step: %3d size: %3d off: %3d%s\n",
6248230130Smav			    ctl->mute, ctl->step, ctl->size, ctl->offset,
6249230130Smav			    (ctl->enable == 0) ? " [DISABLED]" :
6250230130Smav			    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
6251230130Smav		}
6252230130Smav		device_printf(dev, "\n");
6253230130Smav	);
6254230130Smav}
6255230130Smav
6256230130Smavstatic void
6257230130Smavhdaa_unconfigure(device_t dev)
6258230130Smav{
6259230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6260230130Smav	struct hdaa_widget *w;
6261230130Smav	int i, j;
6262230130Smav
6263230130Smav	HDA_BOOTHVERBOSE(
6264230551Smav		device_printf(dev, "Pin sense deinit...\n");
6265230130Smav	);
6266230551Smav	hdaa_sense_deinit(devinfo);
6267230130Smav	free(devinfo->ctl, M_HDAA);
6268230130Smav	devinfo->ctl = NULL;
6269230130Smav	devinfo->ctlcnt = 0;
6270230130Smav	free(devinfo->as, M_HDAA);
6271230130Smav	devinfo->as = NULL;
6272230130Smav	devinfo->ascnt = 0;
6273230130Smav	free(devinfo->devs, M_HDAA);
6274230130Smav	devinfo->devs = NULL;
6275230130Smav	devinfo->num_devs = 0;
6276230130Smav	free(devinfo->chans, M_HDAA);
6277230130Smav	devinfo->chans = NULL;
6278230130Smav	devinfo->num_chans = 0;
6279230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6280230130Smav		w = hdaa_widget_get(devinfo, i);
6281230130Smav		if (w == NULL)
6282230130Smav			continue;
6283230130Smav		w->enable = 1;
6284230130Smav		w->selconn = -1;
6285230130Smav		w->pflags = 0;
6286230130Smav		w->bindas = -1;
6287230130Smav		w->bindseqmask = 0;
6288230130Smav		w->ossdev = -1;
6289230130Smav		w->ossmask = 0;
6290230130Smav		for (j = 0; j < w->nconns; j++)
6291230130Smav			w->connsenable[j] = 1;
6292230451Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6293230451Smav			w->wclass.pin.config = w->wclass.pin.newconf;
6294230312Smav		if (w->eld != NULL) {
6295230312Smav			w->eld_len = 0;
6296230312Smav			free(w->eld, M_HDAA);
6297230312Smav			w->eld = NULL;
6298230312Smav		}
6299230130Smav	}
6300230130Smav}
6301230130Smav
6302230130Smavstatic int
6303230130Smavhdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS)
6304230130Smav{
6305230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6306230130Smav	device_t dev = devinfo->dev;
6307230130Smav	char buf[256];
6308230130Smav	int n = 0, i, numgpi;
6309230130Smav	uint32_t data = 0;
6310230130Smav
6311230130Smav	buf[0] = 0;
6312230130Smav	hdaa_lock(devinfo);
6313230130Smav	numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap);
6314230130Smav	if (numgpi > 0) {
6315230130Smav		data = hda_command(dev,
6316230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
6317230130Smav	}
6318230130Smav	hdaa_unlock(devinfo);
6319230130Smav	for (i = 0; i < numgpi; i++) {
6320230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6321230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
6322230130Smav	}
6323230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6324230130Smav}
6325230130Smav
6326230130Smavstatic int
6327230130Smavhdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS)
6328230130Smav{
6329230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6330230130Smav	device_t dev = devinfo->dev;
6331230130Smav	char buf[256];
6332230130Smav	int n = 0, i, numgpio;
6333230130Smav	uint32_t data = 0, enable = 0, dir = 0;
6334230130Smav
6335230130Smav	buf[0] = 0;
6336230130Smav	hdaa_lock(devinfo);
6337230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6338230130Smav	if (numgpio > 0) {
6339230130Smav		data = hda_command(dev,
6340230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
6341230130Smav		enable = hda_command(dev,
6342230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
6343230130Smav		dir = hda_command(dev,
6344230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
6345230130Smav	}
6346230130Smav	hdaa_unlock(devinfo);
6347230130Smav	for (i = 0; i < numgpio; i++) {
6348230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=",
6349230130Smav		    n != 0 ? " " : "", i);
6350230130Smav		if ((enable & (1 << i)) == 0) {
6351230130Smav			n += snprintf(buf + n, sizeof(buf) - n, "disabled");
6352230130Smav			continue;
6353230130Smav		}
6354230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)",
6355230130Smav		    ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1));
6356230130Smav	}
6357230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6358230130Smav}
6359230130Smav
6360230130Smavstatic int
6361230130Smavhdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS)
6362230130Smav{
6363230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6364230130Smav	char buf[256];
6365230130Smav	int error, n = 0, i, numgpio;
6366230130Smav	uint32_t gpio, x;
6367230130Smav
6368230130Smav	gpio = devinfo->newgpio;
6369230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6370230130Smav	buf[0] = 0;
6371230130Smav	for (i = 0; i < numgpio; i++) {
6372230130Smav		x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6373230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6374230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6375230130Smav	}
6376230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6377230130Smav	if (error != 0 || req->newptr == NULL)
6378230130Smav		return (error);
6379230130Smav	if (strncmp(buf, "0x", 2) == 0)
6380230130Smav		gpio = strtol(buf + 2, NULL, 16);
6381230130Smav	else
6382230130Smav		gpio = hdaa_gpio_patch(gpio, buf);
6383230130Smav	hdaa_lock(devinfo);
6384230130Smav	devinfo->newgpio = devinfo->gpio = gpio;
6385230130Smav	hdaa_gpio_commit(devinfo);
6386230130Smav	hdaa_unlock(devinfo);
6387230130Smav	return (0);
6388230130Smav}
6389230130Smav
6390230130Smavstatic int
6391230130Smavhdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS)
6392230130Smav{
6393230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6394230130Smav	device_t dev = devinfo->dev;
6395230130Smav	char buf[256];
6396230130Smav	int n = 0, i, numgpo;
6397230130Smav	uint32_t data = 0;
6398230130Smav
6399230130Smav	buf[0] = 0;
6400230130Smav	hdaa_lock(devinfo);
6401230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6402230130Smav	if (numgpo > 0) {
6403230130Smav		data = hda_command(dev,
6404230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
6405230130Smav	}
6406230130Smav	hdaa_unlock(devinfo);
6407230130Smav	for (i = 0; i < numgpo; i++) {
6408230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6409230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
6410230130Smav	}
6411230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6412230130Smav}
6413230130Smav
6414230130Smavstatic int
6415230130Smavhdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS)
6416230130Smav{
6417230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6418230130Smav	char buf[256];
6419230130Smav	int error, n = 0, i, numgpo;
6420230130Smav	uint32_t gpo, x;
6421230130Smav
6422230130Smav	gpo = devinfo->newgpo;
6423230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6424230130Smav	buf[0] = 0;
6425230130Smav	for (i = 0; i < numgpo; i++) {
6426230130Smav		x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6427230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6428230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6429230130Smav	}
6430230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6431230130Smav	if (error != 0 || req->newptr == NULL)
6432230130Smav		return (error);
6433230130Smav	if (strncmp(buf, "0x", 2) == 0)
6434230130Smav		gpo = strtol(buf + 2, NULL, 16);
6435230130Smav	else
6436230130Smav		gpo = hdaa_gpio_patch(gpo, buf);
6437230130Smav	hdaa_lock(devinfo);
6438230130Smav	devinfo->newgpo = devinfo->gpo = gpo;
6439230130Smav	hdaa_gpo_commit(devinfo);
6440230130Smav	hdaa_unlock(devinfo);
6441230130Smav	return (0);
6442230130Smav}
6443230130Smav
6444230130Smavstatic int
6445230130Smavhdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS)
6446230130Smav{
6447230130Smav	device_t dev;
6448230130Smav	struct hdaa_devinfo *devinfo;
6449230130Smav	int error, val;
6450230130Smav
6451230130Smav	dev = oidp->oid_arg1;
6452230130Smav	devinfo = device_get_softc(dev);
6453230130Smav	if (devinfo == NULL)
6454230130Smav		return (EINVAL);
6455230130Smav	val = 0;
6456230130Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6457230130Smav	if (error != 0 || req->newptr == NULL || val == 0)
6458230130Smav		return (error);
6459230130Smav
6460230130Smav	HDA_BOOTHVERBOSE(
6461230130Smav		device_printf(dev, "Reconfiguration...\n");
6462230130Smav	);
6463230130Smav	if ((error = device_delete_children(dev)) != 0)
6464230130Smav		return (error);
6465230130Smav	hdaa_lock(devinfo);
6466230130Smav	hdaa_unconfigure(dev);
6467230130Smav	hdaa_configure(dev);
6468230130Smav	hdaa_unlock(devinfo);
6469230130Smav	bus_generic_attach(dev);
6470230130Smav	HDA_BOOTHVERBOSE(
6471230130Smav		device_printf(dev, "Reconfiguration done\n");
6472230130Smav	);
6473230130Smav	return (0);
6474230130Smav}
6475230130Smav
6476230130Smavstatic int
6477230130Smavhdaa_suspend(device_t dev)
6478230130Smav{
6479230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6480230130Smav	int i;
6481230130Smav
6482230130Smav	HDA_BOOTHVERBOSE(
6483230130Smav		device_printf(dev, "Suspend...\n");
6484230130Smav	);
6485230130Smav	hdaa_lock(devinfo);
6486230130Smav	HDA_BOOTHVERBOSE(
6487230130Smav		device_printf(dev, "Stop streams...\n");
6488230130Smav	);
6489230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6490230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) {
6491230130Smav			devinfo->chans[i].flags |= HDAA_CHN_SUSPEND;
6492230130Smav			hdaa_channel_stop(&devinfo->chans[i]);
6493230130Smav		}
6494230130Smav	}
6495230130Smav	HDA_BOOTHVERBOSE(
6496230130Smav		device_printf(dev, "Power down FG"
6497230130Smav		    " nid=%d to the D3 state...\n",
6498230130Smav		    devinfo->nid);
6499230130Smav	);
6500230130Smav	hda_command(devinfo->dev,
6501230130Smav	    HDA_CMD_SET_POWER_STATE(0,
6502230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D3));
6503230130Smav	callout_stop(&devinfo->poll_jack);
6504230130Smav	hdaa_unlock(devinfo);
6505230130Smav	callout_drain(&devinfo->poll_jack);
6506230130Smav	HDA_BOOTHVERBOSE(
6507230130Smav		device_printf(dev, "Suspend done\n");
6508230130Smav	);
6509230130Smav	return (0);
6510230130Smav}
6511230130Smav
6512230130Smavstatic int
6513230130Smavhdaa_resume(device_t dev)
6514230130Smav{
6515230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6516230130Smav	int i;
6517230130Smav
6518230130Smav	HDA_BOOTHVERBOSE(
6519230130Smav		device_printf(dev, "Resume...\n");
6520230130Smav	);
6521230130Smav	hdaa_lock(devinfo);
6522230130Smav	HDA_BOOTHVERBOSE(
6523230130Smav		device_printf(dev, "Power up audio FG nid=%d...\n",
6524230130Smav		    devinfo->nid);
6525230130Smav	);
6526230130Smav	hdaa_powerup(devinfo);
6527230130Smav	HDA_BOOTHVERBOSE(
6528230130Smav		device_printf(dev, "AFG commit...\n");
6529230130Smav	);
6530230130Smav	hdaa_audio_commit(devinfo);
6531230130Smav	HDA_BOOTHVERBOSE(
6532230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
6533230130Smav	);
6534230130Smav	hdaa_patch_direct(devinfo);
6535230130Smav	HDA_BOOTHVERBOSE(
6536230551Smav		device_printf(dev, "Pin sense init...\n");
6537230130Smav	);
6538230551Smav	hdaa_sense_init(devinfo);
6539230130Smav
6540230130Smav	hdaa_unlock(devinfo);
6541230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
6542230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
6543230130Smav		HDA_BOOTHVERBOSE(
6544230130Smav			device_printf(pdevinfo->dev,
6545230130Smav			    "OSS mixer reinitialization...\n");
6546230130Smav		);
6547230130Smav		if (mixer_reinit(pdevinfo->dev) == -1)
6548230130Smav			device_printf(pdevinfo->dev,
6549230130Smav			    "unable to reinitialize the mixer\n");
6550230130Smav	}
6551230130Smav	hdaa_lock(devinfo);
6552230130Smav	HDA_BOOTHVERBOSE(
6553230130Smav		device_printf(dev, "Start streams...\n");
6554230130Smav	);
6555230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6556230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) {
6557230130Smav			devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND;
6558230130Smav			hdaa_channel_start(&devinfo->chans[i]);
6559230130Smav		}
6560230130Smav	}
6561230130Smav	hdaa_unlock(devinfo);
6562230130Smav	HDA_BOOTHVERBOSE(
6563230130Smav		device_printf(dev, "Resume done\n");
6564230130Smav	);
6565230130Smav	return (0);
6566230130Smav}
6567230130Smav
6568230130Smavstatic int
6569230130Smavhdaa_probe(device_t dev)
6570230130Smav{
6571238022Smav	const char *pdesc;
6572230130Smav	char buf[128];
6573230130Smav
6574230130Smav	if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
6575230130Smav		return (ENXIO);
6576238022Smav	pdesc = device_get_desc(device_get_parent(dev));
6577238022Smav	snprintf(buf, sizeof(buf), "%.*s Audio Function Group",
6578238022Smav	    (int)(strlen(pdesc) - 10), pdesc);
6579230130Smav	device_set_desc_copy(dev, buf);
6580230130Smav	return (BUS_PROBE_DEFAULT);
6581230130Smav}
6582230130Smav
6583230130Smavstatic int
6584230130Smavhdaa_attach(device_t dev)
6585230130Smav{
6586230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6587230130Smav	uint32_t res;
6588230130Smav	nid_t nid = hda_get_node_id(dev);
6589230130Smav
6590230130Smav	devinfo->dev = dev;
6591230130Smav	devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
6592230130Smav	devinfo->nid = nid;
6593230130Smav	devinfo->newquirks = -1;
6594230130Smav	devinfo->newgpio = -1;
6595230130Smav	devinfo->newgpo = -1;
6596283291Sjkim	callout_init(&devinfo->poll_jack, 1);
6597230130Smav	devinfo->poll_ival = hz;
6598230130Smav
6599230130Smav	hdaa_lock(devinfo);
6600230130Smav	res = hda_command(dev,
6601230130Smav	    HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT));
6602230130Smav	hdaa_unlock(devinfo);
6603230130Smav
6604230130Smav	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
6605230130Smav	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
6606230130Smav	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
6607230130Smav
6608230130Smav	HDA_BOOTVERBOSE(
6609242352Smav		device_printf(dev, "Subsystem ID: 0x%08x\n",
6610242352Smav		    hda_get_subsystem_id(dev));
6611242352Smav	);
6612242352Smav	HDA_BOOTHVERBOSE(
6613230130Smav		device_printf(dev,
6614230130Smav		    "Audio Function Group at nid=%d: %d subnodes %d-%d\n",
6615230130Smav		    nid, devinfo->nodecnt,
6616230130Smav		    devinfo->startnode, devinfo->endnode - 1);
6617230130Smav	);
6618230130Smav
6619230130Smav	if (devinfo->nodecnt > 0)
6620230130Smav		devinfo->widget = (struct hdaa_widget *)malloc(
6621230130Smav		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA,
6622230130Smav		    M_WAITOK | M_ZERO);
6623230130Smav	else
6624230130Smav		devinfo->widget = NULL;
6625230130Smav
6626230130Smav	hdaa_lock(devinfo);
6627230130Smav	HDA_BOOTHVERBOSE(
6628230130Smav		device_printf(dev, "Powering up...\n");
6629230130Smav	);
6630230130Smav	hdaa_powerup(devinfo);
6631230130Smav	HDA_BOOTHVERBOSE(
6632230130Smav		device_printf(dev, "Parsing audio FG...\n");
6633230130Smav	);
6634230130Smav	hdaa_audio_parse(devinfo);
6635230130Smav	HDA_BOOTVERBOSE(
6636230130Smav		device_printf(dev, "Original pins configuration:\n");
6637230130Smav		hdaa_dump_pin_configs(devinfo);
6638230130Smav	);
6639230130Smav	hdaa_configure(dev);
6640230130Smav	hdaa_unlock(devinfo);
6641230130Smav
6642230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6643230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6644230130Smav	    "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6645299503Scem	    &devinfo->newquirks, 0, hdaa_sysctl_quirks, "A",
6646299503Scem	    "Configuration options");
6647230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6648230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6649230130Smav	    "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6650299503Scem	    devinfo, 0, hdaa_sysctl_gpi_state, "A", "GPI state");
6651230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6652230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6653230130Smav	    "gpio_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6654299503Scem	    devinfo, 0, hdaa_sysctl_gpio_state, "A", "GPIO state");
6655230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6656230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6657230130Smav	    "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6658299503Scem	    devinfo, 0, hdaa_sysctl_gpio_config, "A", "GPIO configuration");
6659230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6660230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6661230130Smav	    "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6662299503Scem	    devinfo, 0, hdaa_sysctl_gpo_state, "A", "GPO state");
6663230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6664230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6665230130Smav	    "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6666299503Scem	    devinfo, 0, hdaa_sysctl_gpo_config, "A", "GPO configuration");
6667230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6668230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6669230130Smav	    "reconfig", CTLTYPE_INT | CTLFLAG_RW,
6670299503Scem	    dev, 0, hdaa_sysctl_reconfig, "I", "Reprocess configuration");
6671230130Smav	bus_generic_attach(dev);
6672230130Smav	return (0);
6673230130Smav}
6674230130Smav
6675230130Smavstatic int
6676230130Smavhdaa_detach(device_t dev)
6677230130Smav{
6678230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6679230130Smav	int error;
6680230130Smav
6681230130Smav	if ((error = device_delete_children(dev)) != 0)
6682230130Smav		return (error);
6683230130Smav
6684230130Smav	hdaa_lock(devinfo);
6685230130Smav	hdaa_unconfigure(dev);
6686230130Smav	devinfo->poll_ival = 0;
6687230130Smav	callout_stop(&devinfo->poll_jack);
6688230130Smav	hdaa_unlock(devinfo);
6689230130Smav	callout_drain(&devinfo->poll_jack);
6690230130Smav
6691230130Smav	free(devinfo->widget, M_HDAA);
6692230130Smav	return (0);
6693230130Smav}
6694230130Smav
6695230130Smavstatic int
6696230130Smavhdaa_print_child(device_t dev, device_t child)
6697230130Smav{
6698230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6699230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6700230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6701230130Smav	struct hdaa_audio_as *as;
6702230130Smav	int retval, first = 1, i;
6703230130Smav
6704230130Smav	retval = bus_print_child_header(dev, child);
6705230130Smav	retval += printf(" at nid ");
6706230130Smav	if (pdevinfo->playas >= 0) {
6707230130Smav		as = &devinfo->as[pdevinfo->playas];
6708230130Smav		for (i = 0; i < 16; i++) {
6709230130Smav			if (as->pins[i] <= 0)
6710230130Smav				continue;
6711230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6712230130Smav			first = 0;
6713230130Smav		}
6714230130Smav	}
6715230130Smav	if (pdevinfo->recas >= 0) {
6716230130Smav		if (pdevinfo->playas >= 0) {
6717230130Smav			retval += printf(" and ");
6718230130Smav			first = 1;
6719230130Smav		}
6720230130Smav		as = &devinfo->as[pdevinfo->recas];
6721230130Smav		for (i = 0; i < 16; i++) {
6722230130Smav			if (as->pins[i] <= 0)
6723230130Smav				continue;
6724230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6725230130Smav			first = 0;
6726230130Smav		}
6727230130Smav	}
6728230130Smav	retval += bus_print_child_footer(dev, child);
6729230130Smav
6730230130Smav	return (retval);
6731230130Smav}
6732230130Smav
6733230130Smavstatic int
6734230130Smavhdaa_child_location_str(device_t dev, device_t child, char *buf,
6735230130Smav    size_t buflen)
6736230130Smav{
6737230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6738230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6739230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6740230130Smav	struct hdaa_audio_as *as;
6741230130Smav	int first = 1, i, len = 0;
6742230130Smav
6743230130Smav	len += snprintf(buf + len, buflen - len, "nid=");
6744230130Smav	if (pdevinfo->playas >= 0) {
6745230130Smav		as = &devinfo->as[pdevinfo->playas];
6746230130Smav		for (i = 0; i < 16; i++) {
6747230130Smav			if (as->pins[i] <= 0)
6748230130Smav				continue;
6749230130Smav			len += snprintf(buf + len, buflen - len,
6750230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6751230130Smav			first = 0;
6752230130Smav		}
6753230130Smav	}
6754230130Smav	if (pdevinfo->recas >= 0) {
6755230130Smav		as = &devinfo->as[pdevinfo->recas];
6756230130Smav		for (i = 0; i < 16; i++) {
6757230130Smav			if (as->pins[i] <= 0)
6758230130Smav				continue;
6759230130Smav			len += snprintf(buf + len, buflen - len,
6760230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6761230130Smav			first = 0;
6762230130Smav		}
6763230130Smav	}
6764230130Smav	return (0);
6765230130Smav}
6766230130Smav
6767230130Smavstatic void
6768230130Smavhdaa_stream_intr(device_t dev, int dir, int stream)
6769230130Smav{
6770230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6771230130Smav	struct hdaa_chan *ch;
6772230130Smav	int i;
6773230130Smav
6774230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6775230130Smav		ch = &devinfo->chans[i];
6776230130Smav		if (!(ch->flags & HDAA_CHN_RUNNING))
6777230130Smav			continue;
6778230130Smav		if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) &&
6779230130Smav		    ch->sid == stream) {
6780230130Smav			hdaa_unlock(devinfo);
6781230130Smav			chn_intr(ch->c);
6782230130Smav			hdaa_lock(devinfo);
6783230130Smav		}
6784230130Smav	}
6785230130Smav}
6786230130Smav
6787230130Smavstatic void
6788230130Smavhdaa_unsol_intr(device_t dev, uint32_t resp)
6789230130Smav{
6790230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6791230312Smav	struct hdaa_widget *w;
6792230312Smav	int i, tag, flags;
6793230130Smav
6794230312Smav	HDA_BOOTHVERBOSE(
6795230312Smav		device_printf(dev, "Unsolicited response %08x\n", resp);
6796230312Smav	);
6797230130Smav	tag = resp >> 26;
6798230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6799230312Smav		w = hdaa_widget_get(devinfo, i);
6800230312Smav		if (w == NULL || w->enable == 0 || w->type !=
6801230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6802230312Smav			continue;
6803230312Smav		if (w->unsol != tag)
6804230312Smav			continue;
6805230312Smav		if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
6806230312Smav		    HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
6807230312Smav			flags = resp & 0x03;
6808230312Smav		else
6809230312Smav			flags = 0x01;
6810230312Smav		if (flags & 0x01)
6811230551Smav			hdaa_presence_handler(w);
6812230312Smav		if (flags & 0x02)
6813230312Smav			hdaa_eld_handler(w);
6814230130Smav	}
6815230130Smav}
6816230130Smav
6817230130Smavstatic device_method_t hdaa_methods[] = {
6818230130Smav	/* device interface */
6819230130Smav	DEVMETHOD(device_probe,		hdaa_probe),
6820230130Smav	DEVMETHOD(device_attach,	hdaa_attach),
6821230130Smav	DEVMETHOD(device_detach,	hdaa_detach),
6822230130Smav	DEVMETHOD(device_suspend,	hdaa_suspend),
6823230130Smav	DEVMETHOD(device_resume,	hdaa_resume),
6824230130Smav	/* Bus interface */
6825230130Smav	DEVMETHOD(bus_print_child,	hdaa_print_child),
6826230130Smav	DEVMETHOD(bus_child_location_str, hdaa_child_location_str),
6827230130Smav	DEVMETHOD(hdac_stream_intr,	hdaa_stream_intr),
6828230130Smav	DEVMETHOD(hdac_unsol_intr,	hdaa_unsol_intr),
6829230130Smav	DEVMETHOD(hdac_pindump,		hdaa_pindump),
6830264832Smarius	DEVMETHOD_END
6831230130Smav};
6832230130Smav
6833230130Smavstatic driver_t hdaa_driver = {
6834230130Smav	"hdaa",
6835230130Smav	hdaa_methods,
6836230130Smav	sizeof(struct hdaa_devinfo),
6837230130Smav};
6838230130Smav
6839230130Smavstatic devclass_t hdaa_devclass;
6840230130Smav
6841264832SmariusDRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, NULL, NULL);
6842230130Smav
6843230130Smavstatic void
6844230130Smavhdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid,
6845230130Smav    char *buf, int buflen)
6846230130Smav{
6847230130Smav	struct hdaa_audio_as *as;
6848230130Smav	int c;
6849230130Smav
6850230130Smav	as = &devinfo->as[asid];
6851230130Smav	c = devinfo->chans[as->chans[0]].channels;
6852230130Smav	if (c == 1)
6853230130Smav		snprintf(buf, buflen, "mono");
6854230585Smav	else if (c == 2) {
6855230585Smav		if (as->hpredir < 0)
6856230585Smav			buf[0] = 0;
6857230585Smav		else
6858230585Smav			snprintf(buf, buflen, "2.0");
6859230585Smav	} else if (as->pinset == 0x0003)
6860230130Smav		snprintf(buf, buflen, "3.1");
6861230130Smav	else if (as->pinset == 0x0005 || as->pinset == 0x0011)
6862230130Smav		snprintf(buf, buflen, "4.0");
6863230130Smav	else if (as->pinset == 0x0007 || as->pinset == 0x0013)
6864230130Smav		snprintf(buf, buflen, "5.1");
6865230130Smav	else if (as->pinset == 0x0017)
6866230130Smav		snprintf(buf, buflen, "7.1");
6867230130Smav	else
6868230130Smav		snprintf(buf, buflen, "%dch", c);
6869230585Smav	if (as->hpredir >= 0)
6870230585Smav		strlcat(buf, "+HP", buflen);
6871230130Smav}
6872230130Smav
6873230130Smavstatic int
6874230585Smavhdaa_chan_type(struct hdaa_devinfo *devinfo, int asid)
6875230585Smav{
6876230585Smav	struct hdaa_audio_as *as;
6877230585Smav	struct hdaa_widget *w;
6878230585Smav	int i, t = -1, t1;
6879230585Smav
6880230585Smav	as = &devinfo->as[asid];
6881230585Smav	for (i = 0; i < 16; i++) {
6882230585Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
6883230585Smav		if (w == NULL || w->enable == 0 || w->type !=
6884230585Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6885230585Smav			continue;
6886230585Smav		t1 = HDA_CONFIG_DEFAULTCONF_DEVICE(w->wclass.pin.config);
6887230585Smav		if (t == -1)
6888230585Smav			t = t1;
6889230585Smav		else if (t != t1) {
6890230585Smav			t = -2;
6891230585Smav			break;
6892230585Smav		}
6893230585Smav	}
6894230585Smav	return (t);
6895230585Smav}
6896230585Smav
6897230585Smavstatic int
6898230488Smavhdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS)
6899230488Smav{
6900230488Smav	struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1;
6901230488Smav	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
6902230488Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6903230488Smav	struct hdaa_chan *ch;
6904230488Smav	int error, val, i;
6905230488Smav	uint32_t pcmcap;
6906230488Smav
6907230488Smav	ch = &devinfo->chans[as->chans[0]];
6908230488Smav	val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 :
6909230488Smav	    ((ch->bit32 == 2) ? 20 : 0));
6910230488Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6911230488Smav	if (error != 0 || req->newptr == NULL)
6912230488Smav		return (error);
6913230488Smav	pcmcap = ch->supp_pcm_size_rate;
6914230488Smav	if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6915230488Smav		ch->bit32 = 4;
6916230488Smav	else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6917230488Smav		ch->bit32 = 3;
6918230488Smav	else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6919230488Smav		ch->bit32 = 2;
6920230488Smav	else
6921230488Smav		return (EINVAL);
6922230488Smav	for (i = 1; i < as->num_chans; i++)
6923230488Smav		devinfo->chans[as->chans[i]].bit32 = ch->bit32;
6924230488Smav	return (0);
6925230488Smav}
6926230488Smav
6927230488Smavstatic int
6928230130Smavhdaa_pcm_probe(device_t dev)
6929230130Smav{
6930230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6931230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6932230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6933238022Smav	const char *pdesc;
6934230130Smav	char chans1[8], chans2[8];
6935230130Smav	char buf[128];
6936230585Smav	int loc1, loc2, t1, t2;
6937230130Smav
6938230130Smav	if (pdevinfo->playas >= 0)
6939230130Smav		loc1 = devinfo->as[pdevinfo->playas].location;
6940230130Smav	else
6941230130Smav		loc1 = devinfo->as[pdevinfo->recas].location;
6942230130Smav	if (pdevinfo->recas >= 0)
6943230130Smav		loc2 = devinfo->as[pdevinfo->recas].location;
6944230130Smav	else
6945230130Smav		loc2 = loc1;
6946230130Smav	if (loc1 != loc2)
6947230130Smav		loc1 = -2;
6948230130Smav	if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0')
6949230130Smav		loc1 = -2;
6950230130Smav	chans1[0] = 0;
6951230130Smav	chans2[0] = 0;
6952230585Smav	t1 = t2 = -1;
6953230585Smav	if (pdevinfo->playas >= 0) {
6954230130Smav		hdaa_chan_formula(devinfo, pdevinfo->playas,
6955230130Smav		    chans1, sizeof(chans1));
6956230585Smav		t1 = hdaa_chan_type(devinfo, pdevinfo->playas);
6957230585Smav	}
6958230585Smav	if (pdevinfo->recas >= 0) {
6959230130Smav		hdaa_chan_formula(devinfo, pdevinfo->recas,
6960230130Smav		    chans2, sizeof(chans2));
6961230585Smav		t2 = hdaa_chan_type(devinfo, pdevinfo->recas);
6962230585Smav	}
6963230130Smav	if (chans1[0] != 0 || chans2[0] != 0) {
6964230130Smav		if (chans1[0] == 0 && pdevinfo->playas >= 0)
6965230130Smav			snprintf(chans1, sizeof(chans1), "2.0");
6966230130Smav		else if (chans2[0] == 0 && pdevinfo->recas >= 0)
6967230130Smav			snprintf(chans2, sizeof(chans2), "2.0");
6968230130Smav		if (strcmp(chans1, chans2) == 0)
6969230130Smav			chans2[0] = 0;
6970230130Smav	}
6971230585Smav	if (t1 == -1)
6972230585Smav		t1 = t2;
6973230585Smav	else if (t2 == -1)
6974230585Smav		t2 = t1;
6975230585Smav	if (t1 != t2)
6976230585Smav		t1 = -2;
6977230585Smav	if (pdevinfo->digital)
6978230585Smav		t1 = -2;
6979238022Smav	pdesc = device_get_desc(device_get_parent(dev));
6980238022Smav	snprintf(buf, sizeof(buf), "%.*s (%s%s%s%s%s%s%s%s%s)",
6981238022Smav	    (int)(strlen(pdesc) - 21), pdesc,
6982230130Smav	    loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "",
6983230507Smav	    (pdevinfo->digital == 0x7)?"HDMI/DP":
6984230507Smav	    ((pdevinfo->digital == 0x5)?"DisplayPort":
6985230507Smav	    ((pdevinfo->digital == 0x3)?"HDMI":
6986230507Smav	    ((pdevinfo->digital)?"Digital":"Analog"))),
6987230130Smav	    chans1[0] ? " " : "", chans1,
6988230585Smav	    chans2[0] ? "/" : "", chans2,
6989230585Smav	    t1 >= 0 ? " " : "", t1 >= 0 ? HDA_DEVS[t1] : "");
6990230130Smav	device_set_desc_copy(dev, buf);
6991230130Smav	return (BUS_PROBE_SPECIFIC);
6992230130Smav}
6993230130Smav
6994230130Smavstatic int
6995230130Smavhdaa_pcm_attach(device_t dev)
6996230130Smav{
6997230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6998230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6999230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
7000230130Smav	struct hdaa_audio_as *as;
7001230488Smav	struct snddev_info *d;
7002230130Smav	char status[SND_STATUSLEN];
7003230130Smav	int i;
7004230130Smav
7005230130Smav	pdevinfo->chan_size = pcm_getbuffersize(dev,
7006230130Smav	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
7007230130Smav
7008230130Smav	HDA_BOOTVERBOSE(
7009230130Smav		hdaa_dump_dac(pdevinfo);
7010230130Smav		hdaa_dump_adc(pdevinfo);
7011230130Smav		hdaa_dump_mix(pdevinfo);
7012230130Smav		hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
7013230130Smav		hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
7014230130Smav		hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
7015230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
7016230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
7017230130Smav		hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
7018230130Smav		hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
7019230130Smav		hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
7020230130Smav		hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
7021230130Smav		hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
7022230130Smav		hdaa_dump_ctls(pdevinfo, NULL, 0);
7023230130Smav	);
7024230130Smav
7025230130Smav	if (resource_int_value(device_get_name(dev),
7026230130Smav	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
7027230130Smav		i &= HDA_BLK_ALIGN;
7028230130Smav		if (i < HDA_BLK_MIN)
7029230130Smav			i = HDA_BLK_MIN;
7030230130Smav		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
7031230130Smav		i = 0;
7032230130Smav		while (pdevinfo->chan_blkcnt >> i)
7033230130Smav			i++;
7034230130Smav		pdevinfo->chan_blkcnt = 1 << (i - 1);
7035230130Smav		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
7036230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
7037230130Smav		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
7038230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
7039230130Smav	} else
7040230130Smav		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
7041230130Smav
7042230130Smav	/*
7043230130Smav	 * We don't register interrupt handler with snd_setup_intr
7044230130Smav	 * in pcm device. Mark pcm device as MPSAFE manually.
7045230130Smav	 */
7046230130Smav	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
7047230130Smav
7048230130Smav	HDA_BOOTHVERBOSE(
7049230130Smav		device_printf(dev, "OSS mixer initialization...\n");
7050230130Smav	);
7051230130Smav	if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0)
7052230130Smav		device_printf(dev, "Can't register mixer\n");
7053230130Smav
7054230130Smav	HDA_BOOTHVERBOSE(
7055230130Smav		device_printf(dev, "Registering PCM channels...\n");
7056230130Smav	);
7057230130Smav	if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0,
7058230130Smav	    (pdevinfo->recas >= 0)?1:0) != 0)
7059230130Smav		device_printf(dev, "Can't register PCM\n");
7060230130Smav
7061230130Smav	pdevinfo->registered++;
7062230130Smav
7063230488Smav	d = device_get_softc(dev);
7064230130Smav	if (pdevinfo->playas >= 0) {
7065230130Smav		as = &devinfo->as[pdevinfo->playas];
7066230130Smav		for (i = 0; i < as->num_chans; i++)
7067230130Smav			pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class,
7068230130Smav			    &devinfo->chans[as->chans[i]]);
7069230488Smav		SYSCTL_ADD_PROC(&d->play_sysctl_ctx,
7070230488Smav		    SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO,
7071230488Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7072230488Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
7073230488Smav		    "Resolution of 32bit samples (20/24/32bit)");
7074230130Smav	}
7075230130Smav	if (pdevinfo->recas >= 0) {
7076230130Smav		as = &devinfo->as[pdevinfo->recas];
7077230130Smav		for (i = 0; i < as->num_chans; i++)
7078230130Smav			pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class,
7079230130Smav			    &devinfo->chans[as->chans[i]]);
7080230488Smav		SYSCTL_ADD_PROC(&d->rec_sysctl_ctx,
7081230488Smav		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7082230488Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7083230488Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
7084230488Smav		    "Resolution of 32bit samples (20/24/32bit)");
7085230551Smav		pdevinfo->autorecsrc = 2;
7086230554Smav		resource_int_value(device_get_name(dev), device_get_unit(dev),
7087230554Smav		    "rec.autosrc", &pdevinfo->autorecsrc);
7088230551Smav		SYSCTL_ADD_INT(&d->rec_sysctl_ctx,
7089230551Smav		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7090273377Shselasky		    "autosrc", CTLFLAG_RW,
7091230551Smav		    &pdevinfo->autorecsrc, 0,
7092230551Smav		    "Automatic recording source selection");
7093230130Smav	}
7094230130Smav
7095230551Smav	if (pdevinfo->mixer != NULL) {
7096230551Smav		hdaa_audio_ctl_set_defaults(pdevinfo);
7097243181Smav		hdaa_lock(devinfo);
7098243181Smav		if (pdevinfo->playas >= 0) {
7099243181Smav			as = &devinfo->as[pdevinfo->playas];
7100243181Smav			hdaa_channels_handler(as);
7101243181Smav		}
7102230551Smav		if (pdevinfo->recas >= 0) {
7103230551Smav			as = &devinfo->as[pdevinfo->recas];
7104230551Smav			hdaa_autorecsrc_handler(as, NULL);
7105243181Smav			hdaa_channels_handler(as);
7106230551Smav		}
7107243181Smav		hdaa_unlock(devinfo);
7108230551Smav	}
7109230551Smav
7110230130Smav	snprintf(status, SND_STATUSLEN, "on %s %s",
7111230130Smav	    device_get_nameunit(device_get_parent(dev)),
7112230130Smav	    PCM_KLDSTRING(snd_hda));
7113230130Smav	pcm_setstatus(dev, status);
7114230130Smav
7115230130Smav	return (0);
7116230130Smav}
7117230130Smav
7118230130Smavstatic int
7119230130Smavhdaa_pcm_detach(device_t dev)
7120230130Smav{
7121230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
7122230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
7123230130Smav	int err;
7124230130Smav
7125230130Smav	if (pdevinfo->registered > 0) {
7126230130Smav		err = pcm_unregister(dev);
7127230130Smav		if (err != 0)
7128230130Smav			return (err);
7129230130Smav	}
7130230130Smav
7131230130Smav	return (0);
7132230130Smav}
7133230130Smav
7134230130Smavstatic device_method_t hdaa_pcm_methods[] = {
7135230130Smav	/* device interface */
7136230130Smav	DEVMETHOD(device_probe,		hdaa_pcm_probe),
7137230130Smav	DEVMETHOD(device_attach,	hdaa_pcm_attach),
7138230130Smav	DEVMETHOD(device_detach,	hdaa_pcm_detach),
7139264832Smarius	DEVMETHOD_END
7140230130Smav};
7141230130Smav
7142230130Smavstatic driver_t hdaa_pcm_driver = {
7143230130Smav	"pcm",
7144230130Smav	hdaa_pcm_methods,
7145230130Smav	PCM_SOFTC_SIZE,
7146230130Smav};
7147230130Smav
7148264832SmariusDRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, NULL, NULL);
7149230130SmavMODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
7150230130SmavMODULE_VERSION(snd_hda, 1);
7151