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/10/sys/dev/sound/pci/hda/hdaa.c 314667 2017-03-04 13:03:31Z avg $");
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 {
56264961Smarius	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
79264961Smariusstatic const char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue",
80264961Smarius    "Green", "Red", "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B",
81264961Smarius    "Res.C", "Res.D", "White", "Other"};
82230130Smav
83264961Smariusstatic 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
87264961Smariusstatic const char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
88230130Smav
89264961Smariusstatic 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
93264961Smariusstatic 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
103264961Smariusstatic const char *HDA_GPIO_ACTIONS[8] = {
104230130Smav    "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"};
105230130Smav
106264961Smariusstatic 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				);
782230551Smav			};
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);
850230130Smav			};
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;
875230130Smav			};
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;
901230130Smav			};
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
1114264961Smariushdaa_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;
1275264961Smarius		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;
1303264961Smarius	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,
1556301799Sngie	    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,
1562301799Sngie		    &w->wclass.pin.newconf, 0, hdaa_sysctl_config, "A",
1563301799Sngie		    "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,
1568301799Sngie		    &w->wclass.pin.original, 0, hdaa_sysctl_config, "A",
1569301799Sngie		    "Original pin configuration");
1570230130Smav	}
1571243530Smav	hdaa_lock(w->devinfo);
1572230130Smav}
1573230130Smav
1574230130Smavstatic void
1575230130Smavhdaa_widget_postprocess(struct hdaa_widget *w)
1576230130Smav{
1577264961Smarius	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);
2133230326Smav	ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt)) - 1;
2134230130Smav	ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev,
2135230326Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos);
2136230130Smav	if (ch->sid <= 0)
2137230130Smav		return (EBUSY);
2138230130Smav	hdaa_audio_setup(ch);
2139230130Smav	HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev,
2140230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2141230130Smav	HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev,
2142230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid,
2143230130Smav	    sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt);
2144230130Smav	ch->flags |= HDAA_CHN_RUNNING;
2145230130Smav	return (0);
2146230130Smav}
2147230130Smav
2148230130Smavstatic int
2149230130Smavhdaa_channel_trigger(kobj_t obj, void *data, int go)
2150230130Smav{
2151230130Smav	struct hdaa_chan *ch = data;
2152230130Smav	int error = 0;
2153230130Smav
2154230130Smav	if (!PCMTRIG_COMMON(go))
2155230130Smav		return (0);
2156230130Smav
2157230130Smav	hdaa_lock(ch->devinfo);
2158230130Smav	switch (go) {
2159230130Smav	case PCMTRIG_START:
2160230130Smav		error = hdaa_channel_start(ch);
2161230130Smav		break;
2162230130Smav	case PCMTRIG_STOP:
2163230130Smav	case PCMTRIG_ABORT:
2164230130Smav		hdaa_channel_stop(ch);
2165230130Smav		break;
2166230130Smav	default:
2167230130Smav		break;
2168230130Smav	}
2169230130Smav	hdaa_unlock(ch->devinfo);
2170230130Smav
2171230130Smav	return (error);
2172230130Smav}
2173230130Smav
2174230130Smavstatic uint32_t
2175230130Smavhdaa_channel_getptr(kobj_t obj, void *data)
2176230130Smav{
2177230130Smav	struct hdaa_chan *ch = data;
2178230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
2179230130Smav	uint32_t ptr;
2180230130Smav
2181230130Smav	hdaa_lock(devinfo);
2182230130Smav	if (ch->dmapos != NULL) {
2183230130Smav		ptr = *(ch->dmapos);
2184230130Smav	} else {
2185230130Smav		ptr = HDAC_STREAM_GETPTR(
2186230130Smav		    device_get_parent(devinfo->dev), devinfo->dev,
2187230130Smav		    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2188230130Smav	}
2189230130Smav	hdaa_unlock(devinfo);
2190230130Smav
2191230130Smav	/*
2192230130Smav	 * Round to available space and force 128 bytes aligment.
2193230130Smav	 */
2194230130Smav	ptr %= ch->blksz * ch->blkcnt;
2195230130Smav	ptr &= HDA_BLK_ALIGN;
2196230130Smav
2197230130Smav	return (ptr);
2198230130Smav}
2199230130Smav
2200230130Smavstatic struct pcmchan_caps *
2201230130Smavhdaa_channel_getcaps(kobj_t obj, void *data)
2202230130Smav{
2203230130Smav	return (&((struct hdaa_chan *)data)->caps);
2204230130Smav}
2205230130Smav
2206230130Smavstatic kobj_method_t hdaa_channel_methods[] = {
2207230130Smav	KOBJMETHOD(channel_init,		hdaa_channel_init),
2208230130Smav	KOBJMETHOD(channel_setformat,		hdaa_channel_setformat),
2209230130Smav	KOBJMETHOD(channel_setspeed,		hdaa_channel_setspeed),
2210230130Smav	KOBJMETHOD(channel_setblocksize,	hdaa_channel_setblocksize),
2211230130Smav	KOBJMETHOD(channel_setfragments,	hdaa_channel_setfragments),
2212230130Smav	KOBJMETHOD(channel_trigger,		hdaa_channel_trigger),
2213230130Smav	KOBJMETHOD(channel_getptr,		hdaa_channel_getptr),
2214230130Smav	KOBJMETHOD(channel_getcaps,		hdaa_channel_getcaps),
2215230130Smav	KOBJMETHOD_END
2216230130Smav};
2217230130SmavCHANNEL_DECLARE(hdaa_channel);
2218230130Smav
2219230130Smavstatic int
2220230130Smavhdaa_audio_ctl_ossmixer_init(struct snd_mixer *m)
2221230130Smav{
2222230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2223230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2224230130Smav	struct hdaa_widget *w, *cw;
2225230130Smav	uint32_t mask, recmask;
2226230451Smav	int i, j;
2227230130Smav
2228230130Smav	hdaa_lock(devinfo);
2229230451Smav	pdevinfo->mixer = m;
2230230130Smav
2231230130Smav	/* Make sure that in case of soft volume it won't stay muted. */
2232230130Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2233230130Smav		pdevinfo->left[i] = 100;
2234230130Smav		pdevinfo->right[i] = 100;
2235230130Smav	}
2236230130Smav
2237230451Smav	/* Declare volume controls assigned to this association. */
2238230451Smav	mask = pdevinfo->ossmask;
2239230130Smav	if (pdevinfo->playas >= 0) {
2240230451Smav		/* Declate EAPD as ogain control. */
2241230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2242230130Smav			w = hdaa_widget_get(devinfo, i);
2243230130Smav			if (w == NULL || w->enable == 0)
2244230130Smav				continue;
2245230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2246230130Smav			    w->param.eapdbtl == HDA_INVALID ||
2247230130Smav			    w->bindas != pdevinfo->playas)
2248230130Smav				continue;
2249230130Smav			mask |= SOUND_MASK_OGAIN;
2250230130Smav			break;
2251230130Smav		}
2252230130Smav
2253230451Smav		/* Declare soft PCM volume if needed. */
2254230451Smav		if ((mask & SOUND_MASK_PCM) == 0 ||
2255230451Smav		    (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) ||
2256230451Smav		    pdevinfo->minamp[SOUND_MIXER_PCM] ==
2257230451Smav		     pdevinfo->maxamp[SOUND_MIXER_PCM]) {
2258230451Smav			mask |= SOUND_MASK_PCM;
2259230451Smav			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
2260230451Smav			HDA_BOOTHVERBOSE(
2261230451Smav				device_printf(pdevinfo->dev,
2262230451Smav				    "Forcing Soft PCM volume\n");
2263230451Smav			);
2264230451Smav		}
2265230451Smav
2266230451Smav		/* Declare master volume if needed. */
2267230451Smav		if ((mask & SOUND_MASK_VOLUME) == 0) {
2268230451Smav			mask |= SOUND_MASK_VOLUME;
2269230451Smav			mix_setparentchild(m, SOUND_MIXER_VOLUME,
2270230451Smav			    SOUND_MASK_PCM);
2271230451Smav			mix_setrealdev(m, SOUND_MIXER_VOLUME,
2272230451Smav			    SOUND_MIXER_NONE);
2273230451Smav			HDA_BOOTHVERBOSE(
2274230451Smav				device_printf(pdevinfo->dev,
2275230451Smav				    "Forcing master volume with PCM\n");
2276230451Smav			);
2277230451Smav		}
2278230130Smav	}
2279230130Smav
2280230130Smav	/* Declare record sources available to this association. */
2281230451Smav	recmask = 0;
2282230130Smav	if (pdevinfo->recas >= 0) {
2283230130Smav		for (i = 0; i < 16; i++) {
2284230130Smav			if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0)
2285230130Smav				continue;
2286230130Smav			w = hdaa_widget_get(devinfo,
2287230130Smav			    devinfo->as[pdevinfo->recas].dacs[0][i]);
2288230130Smav			if (w == NULL || w->enable == 0)
2289230130Smav				continue;
2290230130Smav			for (j = 0; j < w->nconns; j++) {
2291230130Smav				if (w->connsenable[j] == 0)
2292230130Smav					continue;
2293230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
2294230130Smav				if (cw == NULL || cw->enable == 0)
2295230130Smav					continue;
2296230130Smav				if (cw->bindas != pdevinfo->recas &&
2297230130Smav				    cw->bindas != -2)
2298230130Smav					continue;
2299230130Smav				recmask |= cw->ossmask;
2300230130Smav			}
2301230130Smav		}
2302230130Smav	}
2303230130Smav
2304230451Smav	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2305230451Smav	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2306230451Smav	pdevinfo->ossmask = mask;
2307230451Smav
2308230451Smav	mix_setrecdevs(m, recmask);
2309230451Smav	mix_setdevs(m, mask);
2310230451Smav
2311230451Smav	hdaa_unlock(devinfo);
2312230451Smav
2313230451Smav	return (0);
2314230451Smav}
2315230451Smav
2316230451Smav/*
2317230451Smav * Update amplification per pdevinfo per ossdev, calculate summary coefficient
2318230451Smav * and write it to codec, update *left and *right to reflect remaining error.
2319230451Smav */
2320230451Smavstatic void
2321230451Smavhdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev,
2322230451Smav    int mute, int *left, int *right)
2323230451Smav{
2324230451Smav	int i, zleft, zright, sleft, sright, smute, lval, rval;
2325230451Smav
2326230451Smav	ctl->devleft[ossdev] = *left;
2327230451Smav	ctl->devright[ossdev] = *right;
2328230451Smav	ctl->devmute[ossdev] = mute;
2329230451Smav	smute = sleft = sright = zleft = zright = 0;
2330230451Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2331230451Smav		sleft += ctl->devleft[i];
2332230451Smav		sright += ctl->devright[i];
2333230451Smav		smute |= ctl->devmute[i];
2334230451Smav		if (i == ossdev)
2335230451Smav			continue;
2336230451Smav		zleft += ctl->devleft[i];
2337230451Smav		zright += ctl->devright[i];
2338230451Smav	}
2339230451Smav	lval = QDB2VAL(ctl, sleft);
2340230451Smav	rval = QDB2VAL(ctl, sright);
2341230451Smav	hdaa_audio_ctl_amp_set(ctl, smute, lval, rval);
2342230451Smav	*left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft));
2343230451Smav	*right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright));
2344230451Smav}
2345230451Smav
2346230451Smav/*
2347230451Smav * Trace signal from source, setting volumes on the way.
2348230451Smav */
2349230451Smavstatic void
2350230451Smavhdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo,
2351230451Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2352230451Smav{
2353230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2354230451Smav	struct hdaa_widget *w, *wc;
2355230451Smav	struct hdaa_audio_ctl *ctl;
2356230451Smav	int i, j, conns = 0;
2357230451Smav
2358230451Smav	if (depth > HDA_PARSE_MAXDEPTH)
2359230451Smav		return;
2360230451Smav
2361230451Smav	w = hdaa_widget_get(devinfo, nid);
2362230451Smav	if (w == NULL || w->enable == 0)
2363230451Smav		return;
2364230451Smav
2365230451Smav	/* Count number of active inputs. */
2366230451Smav	if (depth > 0) {
2367230451Smav		for (j = 0; j < w->nconns; j++) {
2368230451Smav			if (!w->connsenable[j])
2369230451Smav				continue;
2370230451Smav			conns++;
2371230130Smav		}
2372230451Smav	}
2373230130Smav
2374230451Smav	/* If this is not a first step - use input mixer.
2375230451Smav	   Pins have common input ctl so care must be taken. */
2376230451Smav	if (depth > 0 && (conns == 1 ||
2377230451Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
2378230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
2379230451Smav		    index, 1);
2380230451Smav		if (ctl)
2381230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2382230451Smav	}
2383230451Smav
2384230451Smav	/* If widget has own ossdev - not traverse it.
2385230451Smav	   It will be traversed on it's own. */
2386230451Smav	if (w->ossdev >= 0 && depth > 0)
2387230451Smav		return;
2388230451Smav
2389230451Smav	/* We must not traverse pin */
2390230451Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
2391230451Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
2392230451Smav	    depth > 0)
2393230451Smav		return;
2394230451Smav
2395230451Smav	/*
2396230451Smav	 * If signals mixed, we can't assign controls farther.
2397230451Smav	 * Ignore this on depth zero. Caller must knows why.
2398230451Smav	 */
2399230451Smav	if (conns > 1 &&
2400230451Smav	    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
2401230451Smav	     w->selconn != index))
2402230451Smav		return;
2403230451Smav
2404230451Smav	ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
2405230451Smav	if (ctl)
2406230451Smav		hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2407230451Smav
2408230451Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2409230451Smav		wc = hdaa_widget_get(devinfo, i);
2410230451Smav		if (wc == NULL || wc->enable == 0)
2411230451Smav			continue;
2412230451Smav		for (j = 0; j < wc->nconns; j++) {
2413230451Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
2414230451Smav				hdaa_audio_ctl_source_volume(pdevinfo, ossdev,
2415230451Smav				    wc->nid, j, mute, left, right, depth + 1);
2416230451Smav			}
2417230130Smav		}
2418230130Smav	}
2419230451Smav	return;
2420230451Smav}
2421230130Smav
2422230451Smav/*
2423230451Smav * Trace signal from destination, setting volumes on the way.
2424230451Smav */
2425230451Smavstatic void
2426230451Smavhdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo,
2427230451Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2428230451Smav{
2429230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2430230451Smav	struct hdaa_audio_as *as = devinfo->as;
2431230451Smav	struct hdaa_widget *w, *wc;
2432230451Smav	struct hdaa_audio_ctl *ctl;
2433230451Smav	int i, j, consumers, cleft, cright;
2434230451Smav
2435230451Smav	if (depth > HDA_PARSE_MAXDEPTH)
2436230451Smav		return;
2437230451Smav
2438230451Smav	w = hdaa_widget_get(devinfo, nid);
2439230451Smav	if (w == NULL || w->enable == 0)
2440230451Smav		return;
2441230451Smav
2442230451Smav	if (depth > 0) {
2443230451Smav		/* If this node produce output for several consumers,
2444230451Smav		   we can't touch it. */
2445230451Smav		consumers = 0;
2446230451Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2447230451Smav			wc = hdaa_widget_get(devinfo, i);
2448230451Smav			if (wc == NULL || wc->enable == 0)
2449230451Smav				continue;
2450230451Smav			for (j = 0; j < wc->nconns; j++) {
2451230451Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
2452230451Smav					consumers++;
2453230451Smav			}
2454230130Smav		}
2455230451Smav		/* The only exception is if real HP redirection is configured
2456230451Smav		   and this is a duplication point.
2457230451Smav		   XXX: Actually exception is not completely correct.
2458230451Smav		   XXX: Duplication point check is not perfect. */
2459230451Smav		if ((consumers == 2 && (w->bindas < 0 ||
2460230451Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
2461230451Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
2462230451Smav		    consumers > 2)
2463230451Smav			return;
2464230451Smav
2465230451Smav		/* Else use it's output mixer. */
2466230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2467230451Smav		    HDAA_CTL_OUT, -1, 1);
2468230451Smav		if (ctl)
2469230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2470230130Smav	}
2471230130Smav
2472230451Smav	/* We must not traverse pin */
2473230451Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2474230451Smav	    depth > 0)
2475230451Smav		return;
2476230130Smav
2477230451Smav	for (i = 0; i < w->nconns; i++) {
2478230451Smav		if (w->connsenable[i] == 0)
2479230451Smav			continue;
2480230451Smav		if (index >= 0 && i != index)
2481230451Smav			continue;
2482230451Smav		cleft = left;
2483230451Smav		cright = right;
2484230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2485230451Smav		    HDAA_CTL_IN, i, 1);
2486230451Smav		if (ctl)
2487230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright);
2488230451Smav		hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1,
2489230451Smav		    mute, cleft, cright, depth + 1);
2490230451Smav	}
2491230451Smav}
2492230130Smav
2493230451Smav/*
2494230451Smav * Set volumes for the specified pdevinfo and ossdev.
2495230451Smav */
2496230451Smavstatic void
2497230451Smavhdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev)
2498230451Smav{
2499230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2500230451Smav	struct hdaa_widget *w, *cw;
2501230451Smav	uint32_t mute;
2502230451Smav	int lvol, rvol;
2503230451Smav	int i, j;
2504230130Smav
2505230451Smav	mute = 0;
2506230451Smav	if (pdevinfo->left[dev] == 0) {
2507230451Smav		mute |= HDAA_AMP_MUTE_LEFT;
2508230451Smav		lvol = -4000;
2509230451Smav	} else
2510230451Smav		lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2511230451Smav		    pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev];
2512230451Smav	if (pdevinfo->right[dev] == 0) {
2513230451Smav		mute |= HDAA_AMP_MUTE_RIGHT;
2514230451Smav		rvol = -4000;
2515230451Smav	} else
2516230451Smav		rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2517230451Smav		    pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev];
2518230451Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2519230451Smav		w = hdaa_widget_get(devinfo, i);
2520230451Smav		if (w == NULL || w->enable == 0)
2521230451Smav			continue;
2522239326Smav		if (w->bindas < 0) {
2523239326Smav			if (pdevinfo->index != 0)
2524239326Smav				continue;
2525239326Smav		} else {
2526239326Smav			if (w->bindas != pdevinfo->playas &&
2527239326Smav			    w->bindas != pdevinfo->recas)
2528239326Smav				continue;
2529239326Smav		}
2530230451Smav		if (dev == SOUND_MIXER_RECLEV &&
2531230451Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2532230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2533230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2534230451Smav			continue;
2535230451Smav		}
2536230451Smav		if (dev == SOUND_MIXER_VOLUME &&
2537230451Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2538230451Smav		    devinfo->as[w->bindas].dir == HDAA_CTL_OUT) {
2539230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2540230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2541230451Smav			continue;
2542230451Smav		}
2543230451Smav		if (dev == SOUND_MIXER_IGAIN &&
2544230451Smav		    w->pflags & HDAA_ADC_MONITOR) {
2545230451Smav			for (j = 0; j < w->nconns; j++) {
2546230451Smav				if (!w->connsenable[j])
2547230451Smav				    continue;
2548230451Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
2549230451Smav				if (cw == NULL || cw->enable == 0)
2550230451Smav				    continue;
2551230451Smav				if (cw->bindas == -1)
2552230451Smav				    continue;
2553230451Smav				if (cw->bindas >= 0 &&
2554230451Smav				    devinfo->as[cw->bindas].dir != HDAA_CTL_IN)
2555230451Smav					continue;
2556230451Smav				hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2557230451Smav				    w->nid, j, mute, lvol, rvol, 0);
2558230451Smav			}
2559230451Smav			continue;
2560230451Smav		}
2561230451Smav		if (w->ossdev != dev)
2562230451Smav			continue;
2563230451Smav		hdaa_audio_ctl_source_volume(pdevinfo, dev,
2564230451Smav		    w->nid, -1, mute, lvol, rvol, 0);
2565230451Smav		if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST))
2566230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2567230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2568230451Smav	}
2569230130Smav}
2570230130Smav
2571230451Smav/*
2572230451Smav * OSS Mixer set method.
2573230451Smav */
2574230130Smavstatic int
2575230130Smavhdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
2576230130Smav					unsigned left, unsigned right)
2577230130Smav{
2578230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2579230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2580230130Smav	struct hdaa_widget *w;
2581230451Smav	int i;
2582230130Smav
2583230130Smav	hdaa_lock(devinfo);
2584230451Smav
2585230130Smav	/* Save new values. */
2586230130Smav	pdevinfo->left[dev] = left;
2587230130Smav	pdevinfo->right[dev] = right;
2588230130Smav
2589230130Smav	/* 'ogain' is the special case implemented with EAPD. */
2590230130Smav	if (dev == SOUND_MIXER_OGAIN) {
2591230130Smav		uint32_t orig;
2592230130Smav		w = NULL;
2593230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2594230130Smav			w = hdaa_widget_get(devinfo, i);
2595230130Smav			if (w == NULL || w->enable == 0)
2596230130Smav				continue;
2597230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2598230130Smav			    w->param.eapdbtl == HDA_INVALID)
2599230130Smav				continue;
2600230130Smav			break;
2601230130Smav		}
2602230130Smav		if (i >= devinfo->endnode) {
2603230130Smav			hdaa_unlock(devinfo);
2604230130Smav			return (-1);
2605230130Smav		}
2606230130Smav		orig = w->param.eapdbtl;
2607230130Smav		if (left == 0)
2608230130Smav			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2609230130Smav		else
2610230130Smav			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2611230130Smav		if (orig != w->param.eapdbtl) {
2612230130Smav			uint32_t val;
2613230130Smav
2614230130Smav			val = w->param.eapdbtl;
2615230130Smav			if (devinfo->quirks & HDAA_QUIRK_EAPDINV)
2616230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2617230130Smav			hda_command(devinfo->dev,
2618230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val));
2619230130Smav		}
2620230130Smav		hdaa_unlock(devinfo);
2621230130Smav		return (left | (left << 8));
2622230130Smav	}
2623230130Smav
2624230130Smav	/* Recalculate all controls related to this OSS device. */
2625230451Smav	hdaa_audio_ctl_dev_volume(pdevinfo, dev);
2626230451Smav
2627230451Smav	hdaa_unlock(devinfo);
2628230451Smav	return (left | (right << 8));
2629230451Smav}
2630230451Smav
2631230451Smav/*
2632230451Smav * Set mixer settings to our own default values:
2633230451Smav * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others.
2634230451Smav */
2635230451Smavstatic void
2636230451Smavhdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo)
2637230451Smav{
2638230451Smav	int amp, vol, dev;
2639230451Smav
2640230451Smav	for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) {
2641230451Smav		if ((pdevinfo->ossmask & (1 << dev)) == 0)
2642230130Smav			continue;
2643230451Smav
2644230451Smav		/* If the value was overriden, leave it as is. */
2645230451Smav		if (resource_int_value(device_get_name(pdevinfo->dev),
2646230451Smav		    device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0)
2647230130Smav			continue;
2648230130Smav
2649230451Smav		vol = -1;
2650230451Smav		if (dev == SOUND_MIXER_OGAIN)
2651230451Smav			vol = 100;
2652230451Smav		else if (dev == SOUND_MIXER_IGAIN)
2653230451Smav			vol = 0;
2654230451Smav		else if (dev == SOUND_MIXER_MIC ||
2655230451Smav		    dev == SOUND_MIXER_MONITOR)
2656230451Smav			amp = 20 * 4;	/* +20dB */
2657230451Smav		else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital)
2658230451Smav			amp = -10 * 4;	/* -10dB */
2659230451Smav		else
2660230451Smav			amp = 0;
2661230451Smav		if (vol < 0 &&
2662230451Smav		    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) {
2663230451Smav			vol = 100;
2664230451Smav		} else if (vol < 0) {
2665230451Smav			vol = ((amp - pdevinfo->minamp[dev]) * 100 +
2666230451Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) /
2667230451Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]);
2668230451Smav			vol = imin(imax(vol, 1), 100);
2669230130Smav		}
2670230451Smav		mix_set(pdevinfo->mixer, dev, vol, vol);
2671230130Smav	}
2672230130Smav}
2673230130Smav
2674230130Smav/*
2675230451Smav * Recursively commutate specified record source.
2676230130Smav */
2677230130Smavstatic uint32_t
2678230130Smavhdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
2679230130Smav{
2680230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2681230130Smav	struct hdaa_widget *w, *cw;
2682230130Smav	struct hdaa_audio_ctl *ctl;
2683230130Smav	char buf[64];
2684230130Smav	int i, muted;
2685230130Smav	uint32_t res = 0;
2686230130Smav
2687230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
2688230130Smav		return (0);
2689230130Smav
2690230130Smav	w = hdaa_widget_get(devinfo, nid);
2691230130Smav	if (w == NULL || w->enable == 0)
2692230130Smav		return (0);
2693230130Smav
2694230130Smav	for (i = 0; i < w->nconns; i++) {
2695230130Smav		if (w->connsenable[i] == 0)
2696230130Smav			continue;
2697230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
2698230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
2699230130Smav			continue;
2700230130Smav		/* Call recursively to trace signal to it's source if needed. */
2701230130Smav		if ((src & cw->ossmask) != 0) {
2702230130Smav			if (cw->ossdev < 0) {
2703230130Smav				res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2704230130Smav				    w->conns[i], depth + 1);
2705230130Smav			} else {
2706230130Smav				res |= cw->ossmask;
2707230130Smav			}
2708230130Smav		}
2709230130Smav		/* We have two special cases: mixers and others (selectors). */
2710230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
2711230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo,
2712230130Smav			    w->nid, HDAA_CTL_IN, i, 1);
2713230130Smav			if (ctl == NULL)
2714230130Smav				continue;
2715230130Smav			/* If we have input control on this node mute them
2716230130Smav			 * according to requested sources. */
2717230130Smav			muted = (src & cw->ossmask) ? 0 : 1;
2718230130Smav			if (muted != ctl->forcemute) {
2719230130Smav				ctl->forcemute = muted;
2720230130Smav				hdaa_audio_ctl_amp_set(ctl,
2721230130Smav				    HDAA_AMP_MUTE_DEFAULT,
2722230130Smav				    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
2723230130Smav			}
2724230130Smav			HDA_BOOTHVERBOSE(
2725230130Smav				device_printf(pdevinfo->dev,
2726230130Smav				    "Recsel (%s): nid %d source %d %s\n",
2727230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2728230130Smav				    src, buf, sizeof(buf)),
2729230130Smav				    nid, i, muted?"mute":"unmute");
2730230130Smav			);
2731230130Smav		} else {
2732230130Smav			if (w->nconns == 1)
2733230130Smav				break;
2734230130Smav			if ((src & cw->ossmask) == 0)
2735230130Smav				continue;
2736230130Smav			/* If we found requested source - select it and exit. */
2737230130Smav			hdaa_widget_connection_select(w, i);
2738230130Smav			HDA_BOOTHVERBOSE(
2739230130Smav				device_printf(pdevinfo->dev,
2740230130Smav				    "Recsel (%s): nid %d source %d select\n",
2741230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2742230130Smav				    src, buf, sizeof(buf)),
2743230130Smav				    nid, i);
2744230130Smav			);
2745230130Smav			break;
2746230130Smav		}
2747230130Smav	}
2748230130Smav	return (res);
2749230130Smav}
2750230130Smav
2751230130Smavstatic uint32_t
2752230130Smavhdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
2753230130Smav{
2754230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2755230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2756230130Smav	struct hdaa_widget *w;
2757230130Smav	struct hdaa_audio_as *as;
2758230451Smav	struct hdaa_audio_ctl *ctl;
2759230130Smav	struct hdaa_chan *ch;
2760230130Smav	int i, j;
2761230130Smav	uint32_t ret = 0xffffffff;
2762230130Smav
2763230130Smav	hdaa_lock(devinfo);
2764230130Smav	if (pdevinfo->recas < 0) {
2765230130Smav		hdaa_unlock(devinfo);
2766230130Smav		return (0);
2767230130Smav	}
2768230130Smav	as = &devinfo->as[pdevinfo->recas];
2769230130Smav
2770230130Smav	/* For non-mixed associations we always recording everything. */
2771230130Smav	if (!as->mixed) {
2772230130Smav		hdaa_unlock(devinfo);
2773230130Smav		return (mix_getrecdevs(m));
2774230130Smav	}
2775230130Smav
2776230130Smav	/* Commutate requested recsrc for each ADC. */
2777230130Smav	for (j = 0; j < as->num_chans; j++) {
2778230130Smav		ch = &devinfo->chans[as->chans[j]];
2779230130Smav		for (i = 0; ch->io[i] >= 0; i++) {
2780230130Smav			w = hdaa_widget_get(devinfo, ch->io[i]);
2781230130Smav			if (w == NULL || w->enable == 0)
2782230130Smav				continue;
2783230130Smav			ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2784230130Smav			    ch->io[i], 0);
2785230130Smav		}
2786230130Smav	}
2787230451Smav	if (ret == 0xffffffff)
2788230451Smav		ret = 0;
2789230130Smav
2790230451Smav	/*
2791230451Smav	 * Some controls could be shared. Reset volumes for controls
2792230451Smav	 * related to previously chosen devices, as they may no longer
2793230451Smav	 * affect the signal.
2794230451Smav	 */
2795230451Smav	i = 0;
2796230451Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
2797230451Smav		if (ctl->enable == 0 ||
2798230451Smav		    !(ctl->ossmask & pdevinfo->recsrc))
2799230451Smav			continue;
2800230451Smav		if (!((pdevinfo->playas >= 0 &&
2801230451Smav		    ctl->widget->bindas == pdevinfo->playas) ||
2802230451Smav		    (pdevinfo->recas >= 0 &&
2803230451Smav		    ctl->widget->bindas == pdevinfo->recas) ||
2804230451Smav		    (pdevinfo->index == 0 &&
2805230451Smav		    ctl->widget->bindas == -2)))
2806230451Smav			continue;
2807230451Smav		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2808230451Smav			if (pdevinfo->recsrc & (1 << j)) {
2809230451Smav				ctl->devleft[j] = 0;
2810230451Smav				ctl->devright[j] = 0;
2811230451Smav				ctl->devmute[j] = 0;
2812230451Smav			}
2813230451Smav		}
2814230451Smav	}
2815230451Smav
2816230451Smav	/*
2817230451Smav	 * Some controls could be shared. Set volumes for controls
2818230451Smav	 * related to devices selected both previously and now.
2819230451Smav	 */
2820230451Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2821230451Smav		if ((ret | pdevinfo->recsrc) & (1 << j))
2822230451Smav			hdaa_audio_ctl_dev_volume(pdevinfo, j);
2823230451Smav	}
2824230451Smav
2825230451Smav	pdevinfo->recsrc = ret;
2826230130Smav	hdaa_unlock(devinfo);
2827230451Smav	return (ret);
2828230130Smav}
2829230130Smav
2830230130Smavstatic kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = {
2831230130Smav	KOBJMETHOD(mixer_init,		hdaa_audio_ctl_ossmixer_init),
2832230130Smav	KOBJMETHOD(mixer_set,		hdaa_audio_ctl_ossmixer_set),
2833230130Smav	KOBJMETHOD(mixer_setrecsrc,	hdaa_audio_ctl_ossmixer_setrecsrc),
2834230130Smav	KOBJMETHOD_END
2835230130Smav};
2836230130SmavMIXER_DECLARE(hdaa_audio_ctl_ossmixer);
2837230130Smav
2838230130Smavstatic void
2839230130Smavhdaa_dump_gpi(struct hdaa_devinfo *devinfo)
2840230130Smav{
2841230130Smav	device_t dev = devinfo->dev;
2842230130Smav	int i;
2843230130Smav	uint32_t data, wake, unsol, sticky;
2844230130Smav
2845230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) {
2846230130Smav		data = hda_command(dev,
2847230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
2848230130Smav		wake = hda_command(dev,
2849230130Smav		    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid));
2850230130Smav		unsol = hda_command(dev,
2851230130Smav		    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2852230130Smav		sticky = hda_command(dev,
2853230130Smav		    HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid));
2854230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) {
2855230130Smav			device_printf(dev, " GPI%d:%s%s%s state=%d", i,
2856230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2857230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2858230130Smav				    (wake & (1 << i)) ? " wake" : "",
2859230130Smav				    (data >> i) & 1);
2860230130Smav		}
2861230130Smav	}
2862230130Smav}
2863230130Smav
2864230130Smavstatic void
2865230130Smavhdaa_dump_gpio(struct hdaa_devinfo *devinfo)
2866230130Smav{
2867230130Smav	device_t dev = devinfo->dev;
2868230130Smav	int i;
2869230130Smav	uint32_t data, dir, enable, wake, unsol, sticky;
2870230130Smav
2871230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) {
2872230130Smav		data = hda_command(dev,
2873230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
2874230130Smav		enable = hda_command(dev,
2875230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
2876230130Smav		dir = hda_command(dev,
2877230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
2878230130Smav		wake = hda_command(dev,
2879230130Smav		    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid));
2880230130Smav		unsol = hda_command(dev,
2881230130Smav		    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2882230130Smav		sticky = hda_command(dev,
2883230130Smav		    HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid));
2884230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) {
2885230130Smav			device_printf(dev, " GPIO%d: ", i);
2886230130Smav			if ((enable & (1 << i)) == 0) {
2887230130Smav				printf("disabled\n");
2888230130Smav				continue;
2889230130Smav			}
2890230130Smav			if ((dir & (1 << i)) == 0) {
2891230130Smav				printf("input%s%s%s",
2892230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2893230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2894230130Smav				    (wake & (1 << i)) ? " wake" : "");
2895230130Smav			} else
2896230130Smav				printf("output");
2897230130Smav			printf(" state=%d\n", (data >> i) & 1);
2898230130Smav		}
2899230130Smav	}
2900230130Smav}
2901230130Smav
2902230130Smavstatic void
2903230130Smavhdaa_dump_gpo(struct hdaa_devinfo *devinfo)
2904230130Smav{
2905230130Smav	device_t dev = devinfo->dev;
2906230130Smav	int i;
2907230130Smav	uint32_t data;
2908230130Smav
2909230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) {
2910230130Smav		data = hda_command(dev,
2911230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
2912230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) {
2913230130Smav			device_printf(dev, " GPO%d: state=%d", i,
2914230130Smav				    (data >> i) & 1);
2915230130Smav		}
2916230130Smav	}
2917230130Smav}
2918230130Smav
2919230130Smavstatic void
2920230130Smavhdaa_audio_parse(struct hdaa_devinfo *devinfo)
2921230130Smav{
2922230130Smav	struct hdaa_widget *w;
2923230130Smav	uint32_t res;
2924230130Smav	int i;
2925230130Smav	nid_t nid;
2926230130Smav
2927230130Smav	nid = devinfo->nid;
2928230130Smav
2929230130Smav	res = hda_command(devinfo->dev,
2930230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT));
2931230130Smav	devinfo->gpio_cap = res;
2932230130Smav
2933230130Smav	HDA_BOOTVERBOSE(
2934230130Smav		device_printf(devinfo->dev,
2935230130Smav		    "NumGPIO=%d NumGPO=%d "
2936230130Smav		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
2937230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
2938230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
2939230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
2940230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
2941230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
2942230130Smav		hdaa_dump_gpi(devinfo);
2943230130Smav		hdaa_dump_gpio(devinfo);
2944230130Smav		hdaa_dump_gpo(devinfo);
2945230130Smav	);
2946230130Smav
2947230130Smav	res = hda_command(devinfo->dev,
2948230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS));
2949230130Smav	devinfo->supp_stream_formats = res;
2950230130Smav
2951230130Smav	res = hda_command(devinfo->dev,
2952230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE));
2953230130Smav	devinfo->supp_pcm_size_rate = res;
2954230130Smav
2955230130Smav	res = hda_command(devinfo->dev,
2956230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP));
2957230130Smav	devinfo->outamp_cap = res;
2958230130Smav
2959230130Smav	res = hda_command(devinfo->dev,
2960230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP));
2961230130Smav	devinfo->inamp_cap = res;
2962230130Smav
2963230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2964230130Smav		w = hdaa_widget_get(devinfo, i);
2965230130Smav		if (w == NULL)
2966230130Smav			device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i);
2967230130Smav		else {
2968230130Smav			w->devinfo = devinfo;
2969230130Smav			w->nid = i;
2970230130Smav			w->enable = 1;
2971230130Smav			w->selconn = -1;
2972230130Smav			w->pflags = 0;
2973230130Smav			w->ossdev = -1;
2974230130Smav			w->bindas = -1;
2975230130Smav			w->param.eapdbtl = HDA_INVALID;
2976230130Smav			hdaa_widget_parse(w);
2977230130Smav		}
2978230130Smav	}
2979230130Smav}
2980230130Smav
2981230130Smavstatic void
2982230130Smavhdaa_audio_postprocess(struct hdaa_devinfo *devinfo)
2983230130Smav{
2984230130Smav	struct hdaa_widget *w;
2985230130Smav	int i;
2986230130Smav
2987230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2988230130Smav		w = hdaa_widget_get(devinfo, i);
2989230130Smav		if (w == NULL)
2990230130Smav			continue;
2991230130Smav		hdaa_widget_postprocess(w);
2992230130Smav	}
2993230130Smav}
2994230130Smav
2995230130Smavstatic void
2996230130Smavhdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo)
2997230130Smav{
2998230130Smav	struct hdaa_audio_ctl *ctls;
2999230130Smav	struct hdaa_widget *w, *cw;
3000230130Smav	int i, j, cnt, max, ocap, icap;
3001230130Smav	int mute, offset, step, size;
3002230130Smav
3003230130Smav	/* XXX This is redundant */
3004230130Smav	max = 0;
3005230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3006230130Smav		w = hdaa_widget_get(devinfo, i);
3007230130Smav		if (w == NULL || w->enable == 0)
3008230130Smav			continue;
3009230130Smav		if (w->param.outamp_cap != 0)
3010230130Smav			max++;
3011230130Smav		if (w->param.inamp_cap != 0) {
3012230130Smav			switch (w->type) {
3013230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3014230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3015230130Smav				for (j = 0; j < w->nconns; j++) {
3016230130Smav					cw = hdaa_widget_get(devinfo,
3017230130Smav					    w->conns[j]);
3018230130Smav					if (cw == NULL || cw->enable == 0)
3019230130Smav						continue;
3020230130Smav					max++;
3021230130Smav				}
3022230130Smav				break;
3023230130Smav			default:
3024230130Smav				max++;
3025230130Smav				break;
3026230130Smav			}
3027230130Smav		}
3028230130Smav	}
3029230130Smav	devinfo->ctlcnt = max;
3030230130Smav
3031230130Smav	if (max < 1)
3032230130Smav		return;
3033230130Smav
3034230130Smav	ctls = (struct hdaa_audio_ctl *)malloc(
3035230130Smav	    sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT);
3036230130Smav
3037230130Smav	if (ctls == NULL) {
3038230130Smav		/* Blekh! */
3039230130Smav		device_printf(devinfo->dev, "unable to allocate ctls!\n");
3040230130Smav		devinfo->ctlcnt = 0;
3041230130Smav		return;
3042230130Smav	}
3043230130Smav
3044230130Smav	cnt = 0;
3045230130Smav	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
3046230130Smav		if (cnt >= max) {
3047230130Smav			device_printf(devinfo->dev, "%s: Ctl overflow!\n",
3048230130Smav			    __func__);
3049230130Smav			break;
3050230130Smav		}
3051230130Smav		w = hdaa_widget_get(devinfo, i);
3052230130Smav		if (w == NULL || w->enable == 0)
3053230130Smav			continue;
3054230130Smav		ocap = w->param.outamp_cap;
3055230130Smav		icap = w->param.inamp_cap;
3056230130Smav		if (ocap != 0) {
3057230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
3058230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
3059230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
3060230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
3061230130Smav			/*if (offset > step) {
3062230130Smav				HDA_BOOTVERBOSE(
3063230130Smav					device_printf(devinfo->dev,
3064230130Smav					    "BUGGY outamp: nid=%d "
3065230130Smav					    "[offset=%d > step=%d]\n",
3066230130Smav					    w->nid, offset, step);
3067230130Smav				);
3068230130Smav				offset = step;
3069230130Smav			}*/
3070230130Smav			ctls[cnt].enable = 1;
3071230130Smav			ctls[cnt].widget = w;
3072230130Smav			ctls[cnt].mute = mute;
3073230130Smav			ctls[cnt].step = step;
3074230130Smav			ctls[cnt].size = size;
3075230130Smav			ctls[cnt].offset = offset;
3076230130Smav			ctls[cnt].left = offset;
3077230130Smav			ctls[cnt].right = offset;
3078230130Smav			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3079230130Smav			    w->waspin)
3080230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
3081230130Smav			else
3082230130Smav				ctls[cnt].ndir = HDAA_CTL_OUT;
3083230130Smav			ctls[cnt++].dir = HDAA_CTL_OUT;
3084230130Smav		}
3085230130Smav
3086230130Smav		if (icap != 0) {
3087230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
3088230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
3089230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
3090230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
3091230130Smav			/*if (offset > step) {
3092230130Smav				HDA_BOOTVERBOSE(
3093230130Smav					device_printf(devinfo->dev,
3094230130Smav					    "BUGGY inamp: nid=%d "
3095230130Smav					    "[offset=%d > step=%d]\n",
3096230130Smav					    w->nid, offset, step);
3097230130Smav				);
3098230130Smav				offset = step;
3099230130Smav			}*/
3100230130Smav			switch (w->type) {
3101230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3102230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3103230130Smav				for (j = 0; j < w->nconns; j++) {
3104230130Smav					if (cnt >= max) {
3105230130Smav						device_printf(devinfo->dev,
3106230130Smav						    "%s: Ctl overflow!\n",
3107230130Smav						    __func__);
3108230130Smav						break;
3109230130Smav					}
3110230130Smav					cw = hdaa_widget_get(devinfo,
3111230130Smav					    w->conns[j]);
3112230130Smav					if (cw == NULL || cw->enable == 0)
3113230130Smav						continue;
3114230130Smav					ctls[cnt].enable = 1;
3115230130Smav					ctls[cnt].widget = w;
3116230130Smav					ctls[cnt].childwidget = cw;
3117230130Smav					ctls[cnt].index = j;
3118230130Smav					ctls[cnt].mute = mute;
3119230130Smav					ctls[cnt].step = step;
3120230130Smav					ctls[cnt].size = size;
3121230130Smav					ctls[cnt].offset = offset;
3122230130Smav					ctls[cnt].left = offset;
3123230130Smav					ctls[cnt].right = offset;
3124230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
3125230130Smav					ctls[cnt++].dir = HDAA_CTL_IN;
3126230130Smav				}
3127230130Smav				break;
3128230130Smav			default:
3129230130Smav				if (cnt >= max) {
3130230130Smav					device_printf(devinfo->dev,
3131230130Smav					    "%s: Ctl overflow!\n",
3132230130Smav					    __func__);
3133230130Smav					break;
3134230130Smav				}
3135230130Smav				ctls[cnt].enable = 1;
3136230130Smav				ctls[cnt].widget = w;
3137230130Smav				ctls[cnt].mute = mute;
3138230130Smav				ctls[cnt].step = step;
3139230130Smav				ctls[cnt].size = size;
3140230130Smav				ctls[cnt].offset = offset;
3141230130Smav				ctls[cnt].left = offset;
3142230130Smav				ctls[cnt].right = offset;
3143230130Smav				if (w->type ==
3144230130Smav				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3145230130Smav					ctls[cnt].ndir = HDAA_CTL_OUT;
3146230130Smav				else
3147230130Smav					ctls[cnt].ndir = HDAA_CTL_IN;
3148230130Smav				ctls[cnt++].dir = HDAA_CTL_IN;
3149230130Smav				break;
3150230130Smav			}
3151230130Smav		}
3152230130Smav	}
3153230130Smav
3154230130Smav	devinfo->ctl = ctls;
3155230130Smav}
3156230130Smav
3157230130Smavstatic void
3158230130Smavhdaa_audio_as_parse(struct hdaa_devinfo *devinfo)
3159230130Smav{
3160230130Smav	struct hdaa_audio_as *as;
3161230130Smav	struct hdaa_widget *w;
3162230130Smav	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
3163230130Smav
3164230130Smav	/* Count present associations */
3165230130Smav	max = 0;
3166230130Smav	for (j = 1; j < 16; j++) {
3167230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3168230130Smav			w = hdaa_widget_get(devinfo, i);
3169230130Smav			if (w == NULL || w->enable == 0)
3170230130Smav				continue;
3171230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3172230130Smav				continue;
3173230130Smav			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
3174230130Smav			    != j)
3175230130Smav				continue;
3176230130Smav			max++;
3177230130Smav			if (j != 15)  /* There could be many 1-pin assocs #15 */
3178230130Smav				break;
3179230130Smav		}
3180230130Smav	}
3181230130Smav
3182230130Smav	devinfo->ascnt = max;
3183230130Smav
3184230130Smav	if (max < 1)
3185230130Smav		return;
3186230130Smav
3187230130Smav	as = (struct hdaa_audio_as *)malloc(
3188230130Smav	    sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT);
3189230130Smav
3190230130Smav	if (as == NULL) {
3191230130Smav		/* Blekh! */
3192230130Smav		device_printf(devinfo->dev, "unable to allocate assocs!\n");
3193230130Smav		devinfo->ascnt = 0;
3194230130Smav		return;
3195230130Smav	}
3196230130Smav
3197230130Smav	for (i = 0; i < max; i++) {
3198230130Smav		as[i].hpredir = -1;
3199230130Smav		as[i].digital = 0;
3200230130Smav		as[i].num_chans = 1;
3201230130Smav		as[i].location = -1;
3202230130Smav	}
3203230130Smav
3204230130Smav	/* Scan associations skipping as=0. */
3205230130Smav	cnt = 0;
3206283609Shselasky	for (j = 1; j < 16 && cnt < max; j++) {
3207230130Smav		first = 16;
3208230130Smav		hpredir = 0;
3209230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3210230130Smav			w = hdaa_widget_get(devinfo, i);
3211230130Smav			if (w == NULL || w->enable == 0)
3212230130Smav				continue;
3213230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3214230130Smav				continue;
3215230130Smav			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
3216230130Smav			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
3217230130Smav			if (assoc != j) {
3218230130Smav				continue;
3219230130Smav			}
3220230130Smav			KASSERT(cnt < max,
3221230130Smav			    ("%s: Associations owerflow (%d of %d)",
3222230130Smav			    __func__, cnt, max));
3223230130Smav			type = w->wclass.pin.config &
3224230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
3225230130Smav			/* Get pin direction. */
3226230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
3227230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
3228230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
3229230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
3230230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
3231230130Smav				dir = HDAA_CTL_OUT;
3232230130Smav			else
3233230130Smav				dir = HDAA_CTL_IN;
3234230130Smav			/* If this is a first pin - create new association. */
3235230130Smav			if (as[cnt].pincnt == 0) {
3236230130Smav				as[cnt].enable = 1;
3237230130Smav				as[cnt].index = j;
3238230130Smav				as[cnt].dir = dir;
3239230130Smav			}
3240230130Smav			if (seq < first)
3241230130Smav				first = seq;
3242230130Smav			/* Check association correctness. */
3243230130Smav			if (as[cnt].pins[seq] != 0) {
3244230130Smav				device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) "
3245230130Smav				    "in association %d! Disabling association.\n",
3246230130Smav				    __func__, seq, w->nid, j);
3247230130Smav				as[cnt].enable = 0;
3248230130Smav			}
3249230130Smav			if (dir != as[cnt].dir) {
3250230130Smav				device_printf(devinfo->dev, "%s: Pin %d has wrong "
3251230130Smav				    "direction for association %d! Disabling "
3252230130Smav				    "association.\n",
3253230130Smav				    __func__, w->nid, j);
3254230130Smav				as[cnt].enable = 0;
3255230130Smav			}
3256230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
3257230507Smav				as[cnt].digital |= 0x1;
3258230507Smav				if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
3259230507Smav					as[cnt].digital |= 0x2;
3260230130Smav				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
3261230507Smav					as[cnt].digital |= 0x4;
3262230130Smav			}
3263230130Smav			if (as[cnt].location == -1) {
3264230130Smav				as[cnt].location =
3265230130Smav				    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config);
3266230130Smav			} else if (as[cnt].location !=
3267230130Smav			    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) {
3268230130Smav				as[cnt].location = -2;
3269230130Smav			}
3270230130Smav			/* Headphones with seq=15 may mean redirection. */
3271230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
3272230130Smav			    seq == 15)
3273230130Smav				hpredir = 1;
3274230130Smav			as[cnt].pins[seq] = w->nid;
3275230130Smav			as[cnt].pincnt++;
3276230130Smav			/* Association 15 is a multiple unassociated pins. */
3277230130Smav			if (j == 15)
3278230130Smav				cnt++;
3279230130Smav		}
3280230130Smav		if (j != 15 && as[cnt].pincnt > 0) {
3281230130Smav			if (hpredir && as[cnt].pincnt > 1)
3282230130Smav				as[cnt].hpredir = first;
3283230130Smav			cnt++;
3284230130Smav		}
3285230130Smav	}
3286230130Smav	for (i = 0; i < max; i++) {
3287230130Smav		if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 ||
3288230130Smav		    as[i].pins[14] > 0 || as[i].pins[15] > 0))
3289230130Smav			as[i].mixed = 1;
3290230130Smav	}
3291230130Smav	HDA_BOOTVERBOSE(
3292230130Smav		device_printf(devinfo->dev,
3293230130Smav		    "%d associations found:\n", max);
3294230130Smav		for (i = 0; i < max; i++) {
3295230130Smav			device_printf(devinfo->dev,
3296230130Smav			    "Association %d (%d) %s%s:\n",
3297230130Smav			    i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out",
3298230130Smav			    as[i].enable?"":" (disabled)");
3299230130Smav			for (j = 0; j < 16; j++) {
3300230130Smav				if (as[i].pins[j] == 0)
3301230130Smav					continue;
3302230130Smav				device_printf(devinfo->dev,
3303230130Smav				    " Pin nid=%d seq=%d\n",
3304230130Smav				    as[i].pins[j], j);
3305230130Smav			}
3306230130Smav		}
3307230130Smav	);
3308230130Smav
3309230130Smav	devinfo->as = as;
3310230130Smav}
3311230130Smav
3312230130Smav/*
3313230130Smav * Trace path from DAC to pin.
3314230130Smav */
3315230130Smavstatic nid_t
3316230130Smavhdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3317230130Smav    int dupseq, int min, int only, int depth)
3318230130Smav{
3319230130Smav	struct hdaa_widget *w;
3320230130Smav	int i, im = -1;
3321230130Smav	nid_t m = 0, ret;
3322230130Smav
3323230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3324230130Smav		return (0);
3325230130Smav	w = hdaa_widget_get(devinfo, nid);
3326230130Smav	if (w == NULL || w->enable == 0)
3327230130Smav		return (0);
3328230130Smav	HDA_BOOTHVERBOSE(
3329230130Smav		if (!only) {
3330230130Smav			device_printf(devinfo->dev,
3331230130Smav			    " %*stracing via nid %d\n",
3332230130Smav				depth + 1, "", w->nid);
3333230130Smav		}
3334230130Smav	);
3335230130Smav	/* Use only unused widgets */
3336230130Smav	if (w->bindas >= 0 && w->bindas != as) {
3337230130Smav		HDA_BOOTHVERBOSE(
3338230130Smav			if (!only) {
3339230130Smav				device_printf(devinfo->dev,
3340230130Smav				    " %*snid %d busy by association %d\n",
3341230130Smav					depth + 1, "", w->nid, w->bindas);
3342230130Smav			}
3343230130Smav		);
3344230130Smav		return (0);
3345230130Smav	}
3346230130Smav	if (dupseq < 0) {
3347230130Smav		if (w->bindseqmask != 0) {
3348230130Smav			HDA_BOOTHVERBOSE(
3349230130Smav				if (!only) {
3350230130Smav					device_printf(devinfo->dev,
3351230130Smav					    " %*snid %d busy by seqmask %x\n",
3352230130Smav						depth + 1, "", w->nid, w->bindseqmask);
3353230130Smav				}
3354230130Smav			);
3355230130Smav			return (0);
3356230130Smav		}
3357230130Smav	} else {
3358230130Smav		/* If this is headphones - allow duplicate first pin. */
3359230130Smav		if (w->bindseqmask != 0 &&
3360230130Smav		    (w->bindseqmask & (1 << dupseq)) == 0) {
3361230130Smav			HDA_BOOTHVERBOSE(
3362230130Smav				device_printf(devinfo->dev,
3363230130Smav				    " %*snid %d busy by seqmask %x\n",
3364230130Smav					depth + 1, "", w->nid, w->bindseqmask);
3365230130Smav			);
3366230130Smav			return (0);
3367230130Smav		}
3368230130Smav	}
3369230130Smav
3370230130Smav	switch (w->type) {
3371230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3372230130Smav		/* Do not traverse input. AD1988 has digital monitor
3373230130Smav		for which we are not ready. */
3374230130Smav		break;
3375230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
3376230130Smav		/* If we are tracing HP take only dac of first pin. */
3377230130Smav		if ((only == 0 || only == w->nid) &&
3378230130Smav		    (w->nid >= min) && (dupseq < 0 || w->nid ==
3379230130Smav		    devinfo->as[as].dacs[0][dupseq]))
3380230130Smav			m = w->nid;
3381230130Smav		break;
3382230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3383230130Smav		if (depth > 0)
3384230130Smav			break;
3385230130Smav		/* Fall */
3386230130Smav	default:
3387230130Smav		/* Find reachable DACs with smallest nid respecting constraints. */
3388230130Smav		for (i = 0; i < w->nconns; i++) {
3389230130Smav			if (w->connsenable[i] == 0)
3390230130Smav				continue;
3391230130Smav			if (w->selconn != -1 && w->selconn != i)
3392230130Smav				continue;
3393230130Smav			if ((ret = hdaa_audio_trace_dac(devinfo, as, seq,
3394230130Smav			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
3395230130Smav				if (m == 0 || ret < m) {
3396230130Smav					m = ret;
3397230130Smav					im = i;
3398230130Smav				}
3399230130Smav				if (only || dupseq >= 0)
3400230130Smav					break;
3401230130Smav			}
3402230130Smav		}
3403230130Smav		if (im >= 0 && only && ((w->nconns > 1 &&
3404230130Smav		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3405230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3406230130Smav			w->selconn = im;
3407230130Smav		break;
3408230130Smav	}
3409230130Smav	if (m && only) {
3410230130Smav		w->bindas = as;
3411230130Smav		w->bindseqmask |= (1 << seq);
3412230130Smav	}
3413230130Smav	HDA_BOOTHVERBOSE(
3414230130Smav		if (!only) {
3415230130Smav			device_printf(devinfo->dev,
3416230130Smav			    " %*snid %d returned %d\n",
3417230130Smav				depth + 1, "", w->nid, m);
3418230130Smav		}
3419230130Smav	);
3420230130Smav	return (m);
3421230130Smav}
3422230130Smav
3423230130Smav/*
3424230130Smav * Trace path from widget to ADC.
3425230130Smav */
3426230130Smavstatic nid_t
3427230130Smavhdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3428230130Smav    int mixed, int min, int only, int depth, int *length, int onlylength)
3429230130Smav{
3430230130Smav	struct hdaa_widget *w, *wc;
3431230130Smav	int i, j, im, lm = HDA_PARSE_MAXDEPTH;
3432230130Smav	nid_t m = 0, ret;
3433230130Smav
3434230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3435230130Smav		return (0);
3436230130Smav	w = hdaa_widget_get(devinfo, nid);
3437230130Smav	if (w == NULL || w->enable == 0)
3438230130Smav		return (0);
3439230130Smav	HDA_BOOTHVERBOSE(
3440230130Smav		device_printf(devinfo->dev,
3441230130Smav		    " %*stracing via nid %d\n",
3442230130Smav			depth + 1, "", w->nid);
3443230130Smav	);
3444230130Smav	/* Use only unused widgets */
3445230130Smav	if (w->bindas >= 0 && w->bindas != as) {
3446230130Smav		HDA_BOOTHVERBOSE(
3447230130Smav			device_printf(devinfo->dev,
3448230130Smav			    " %*snid %d busy by association %d\n",
3449230130Smav				depth + 1, "", w->nid, w->bindas);
3450230130Smav		);
3451230130Smav		return (0);
3452230130Smav	}
3453230130Smav	if (!mixed && w->bindseqmask != 0) {
3454230130Smav		HDA_BOOTHVERBOSE(
3455230130Smav			device_printf(devinfo->dev,
3456230130Smav			    " %*snid %d busy by seqmask %x\n",
3457230130Smav				depth + 1, "", w->nid, w->bindseqmask);
3458230130Smav		);
3459230130Smav		return (0);
3460230130Smav	}
3461230130Smav	switch (w->type) {
3462230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3463230130Smav		if ((only == 0 || only == w->nid) && (w->nid >= min) &&
3464230130Smav		    (onlylength == 0 || onlylength == depth)) {
3465230130Smav			m = w->nid;
3466239254Smav			*length = depth;
3467230130Smav		}
3468230130Smav		break;
3469230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3470230130Smav		if (depth > 0)
3471230130Smav			break;
3472230130Smav		/* Fall */
3473230130Smav	default:
3474230130Smav		/* Try to find reachable ADCs with specified nid. */
3475230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3476230130Smav			wc = hdaa_widget_get(devinfo, j);
3477230130Smav			if (wc == NULL || wc->enable == 0)
3478230130Smav				continue;
3479230130Smav			im = -1;
3480230130Smav			for (i = 0; i < wc->nconns; i++) {
3481230130Smav				if (wc->connsenable[i] == 0)
3482230130Smav					continue;
3483230130Smav				if (wc->conns[i] != nid)
3484230130Smav					continue;
3485230130Smav				if ((ret = hdaa_audio_trace_adc(devinfo, as, seq,
3486230130Smav				    j, mixed, min, only, depth + 1,
3487230130Smav				    length, onlylength)) != 0) {
3488230130Smav					if (m == 0 || ret < m ||
3489239254Smav					    (ret == m && *length < lm)) {
3490230130Smav						m = ret;
3491230130Smav						im = i;
3492230130Smav						lm = *length;
3493239254Smav					} else
3494239254Smav						*length = lm;
3495230130Smav					if (only)
3496230130Smav						break;
3497230130Smav				}
3498230130Smav			}
3499230130Smav			if (im >= 0 && only && ((wc->nconns > 1 &&
3500230130Smav			    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3501230130Smav			    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3502230130Smav				wc->selconn = im;
3503230130Smav		}
3504230130Smav		break;
3505230130Smav	}
3506230130Smav	if (m && only) {
3507230130Smav		w->bindas = as;
3508230130Smav		w->bindseqmask |= (1 << seq);
3509230130Smav	}
3510230130Smav	HDA_BOOTHVERBOSE(
3511230130Smav		device_printf(devinfo->dev,
3512230130Smav		    " %*snid %d returned %d\n",
3513230130Smav			depth + 1, "", w->nid, m);
3514230130Smav	);
3515230130Smav	return (m);
3516230130Smav}
3517230130Smav
3518230130Smav/*
3519230130Smav * Erase trace path of the specified association.
3520230130Smav */
3521230130Smavstatic void
3522230130Smavhdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq)
3523230130Smav{
3524230130Smav	struct hdaa_widget *w;
3525230130Smav	int i;
3526230130Smav
3527230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3528230130Smav		w = hdaa_widget_get(devinfo, i);
3529230130Smav		if (w == NULL || w->enable == 0)
3530230130Smav			continue;
3531230130Smav		if (w->bindas == as) {
3532230130Smav			if (seq >= 0) {
3533230130Smav				w->bindseqmask &= ~(1 << seq);
3534230130Smav				if (w->bindseqmask == 0) {
3535230130Smav					w->bindas = -1;
3536230130Smav					w->selconn = -1;
3537230130Smav				}
3538230130Smav			} else {
3539230130Smav				w->bindas = -1;
3540230130Smav				w->bindseqmask = 0;
3541230130Smav				w->selconn = -1;
3542230130Smav			}
3543230130Smav		}
3544230130Smav	}
3545230130Smav}
3546230130Smav
3547230130Smav/*
3548230130Smav * Trace association path from DAC to output
3549230130Smav */
3550230130Smavstatic int
3551230130Smavhdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq)
3552230130Smav{
3553230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3554230130Smav	int i, hpredir;
3555230130Smav	nid_t min, res;
3556230130Smav
3557230130Smav	/* Find next pin */
3558230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3559230130Smav		;
3560230130Smav	/* Check if there is no any left. If so - we succeeded. */
3561230130Smav	if (i == 16)
3562230130Smav		return (1);
3563230130Smav
3564230130Smav	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
3565230130Smav	min = 0;
3566230130Smav	do {
3567230130Smav		HDA_BOOTHVERBOSE(
3568230130Smav			device_printf(devinfo->dev,
3569230130Smav			    " Tracing pin %d with min nid %d",
3570230130Smav			    ases[as].pins[i], min);
3571230130Smav			if (hpredir >= 0)
3572230130Smav				printf(" and hpredir %d", hpredir);
3573230130Smav			printf("\n");
3574230130Smav		);
3575230130Smav		/* Trace this pin taking min nid into account. */
3576230130Smav		res = hdaa_audio_trace_dac(devinfo, as, i,
3577230130Smav		    ases[as].pins[i], hpredir, min, 0, 0);
3578230130Smav		if (res == 0) {
3579230130Smav			/* If we failed - return to previous and redo it. */
3580230130Smav			HDA_BOOTVERBOSE(
3581230130Smav				device_printf(devinfo->dev,
3582230130Smav				    " Unable to trace pin %d seq %d with min "
3583230130Smav				    "nid %d",
3584230130Smav				    ases[as].pins[i], i, min);
3585230130Smav				if (hpredir >= 0)
3586230130Smav					printf(" and hpredir %d", hpredir);
3587230130Smav				printf("\n");
3588230130Smav			);
3589230130Smav			return (0);
3590230130Smav		}
3591230130Smav		HDA_BOOTVERBOSE(
3592230130Smav			device_printf(devinfo->dev,
3593230130Smav			    " Pin %d traced to DAC %d",
3594230130Smav			    ases[as].pins[i], res);
3595230130Smav			if (hpredir >= 0)
3596230130Smav				printf(" and hpredir %d", hpredir);
3597230130Smav			if (ases[as].fakeredir)
3598230130Smav				printf(" with fake redirection");
3599230130Smav			printf("\n");
3600230130Smav		);
3601230130Smav		/* Trace again to mark the path */
3602230130Smav		hdaa_audio_trace_dac(devinfo, as, i,
3603230130Smav		    ases[as].pins[i], hpredir, min, res, 0);
3604230130Smav		ases[as].dacs[0][i] = res;
3605230130Smav		/* We succeeded, so call next. */
3606230130Smav		if (hdaa_audio_trace_as_out(devinfo, as, i + 1))
3607230130Smav			return (1);
3608230130Smav		/* If next failed, we should retry with next min */
3609230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3610230130Smav		ases[as].dacs[0][i] = 0;
3611230130Smav		min = res + 1;
3612230130Smav	} while (1);
3613230130Smav}
3614230130Smav
3615230130Smav/*
3616230130Smav * Check equivalency of two DACs.
3617230130Smav */
3618230130Smavstatic int
3619230130Smavhdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3620230130Smav{
3621230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3622230130Smav	struct hdaa_widget *w3;
3623230130Smav	int i, j, c1, c2;
3624230130Smav
3625230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3626230130Smav		return (0);
3627230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3628230130Smav		w3 = hdaa_widget_get(devinfo, i);
3629230130Smav		if (w3 == NULL || w3->enable == 0)
3630230130Smav			continue;
3631230130Smav		if (w3->bindas != w1->bindas)
3632230130Smav			continue;
3633230130Smav		if (w3->nconns == 0)
3634230130Smav			continue;
3635230130Smav		c1 = c2 = -1;
3636230130Smav		for (j = 0; j < w3->nconns; j++) {
3637230130Smav			if (w3->connsenable[j] == 0)
3638230130Smav				continue;
3639230130Smav			if (w3->conns[j] == w1->nid)
3640230130Smav				c1 = j;
3641230130Smav			if (w3->conns[j] == w2->nid)
3642230130Smav				c2 = j;
3643230130Smav		}
3644230130Smav		if (c1 < 0)
3645230130Smav			continue;
3646230130Smav		if (c2 < 0)
3647230130Smav			return (0);
3648230130Smav		if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3649230130Smav			return (0);
3650230130Smav	}
3651230130Smav	return (1);
3652230130Smav}
3653230130Smav
3654230130Smav/*
3655230130Smav * Check equivalency of two ADCs.
3656230130Smav */
3657230130Smavstatic int
3658230130Smavhdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3659230130Smav{
3660230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3661230130Smav	struct hdaa_widget *w3, *w4;
3662230130Smav	int i;
3663230130Smav
3664230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3665230130Smav		return (0);
3666230130Smav	if (w1->nconns != 1 || w2->nconns != 1)
3667230130Smav		return (0);
3668230130Smav	if (w1->conns[0] == w2->conns[0])
3669230130Smav		return (1);
3670230130Smav	w3 = hdaa_widget_get(devinfo, w1->conns[0]);
3671230130Smav	if (w3 == NULL || w3->enable == 0)
3672230130Smav		return (0);
3673230130Smav	w4 = hdaa_widget_get(devinfo, w2->conns[0]);
3674230130Smav	if (w4 == NULL || w4->enable == 0)
3675230130Smav		return (0);
3676230130Smav	if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask)
3677230130Smav		return (1);
3678230130Smav	if (w4->bindas >= 0)
3679230130Smav		return (0);
3680230130Smav	if (w3->type != w4->type)
3681230130Smav		return (0);
3682230130Smav	if (memcmp(&w3->param, &w4->param, sizeof(w3->param)))
3683230130Smav		return (0);
3684230130Smav	if (w3->nconns != w4->nconns)
3685230130Smav		return (0);
3686230130Smav	for (i = 0; i < w3->nconns; i++) {
3687230130Smav		if (w3->conns[i] != w4->conns[i])
3688230130Smav			return (0);
3689230130Smav	}
3690230130Smav	return (1);
3691230130Smav}
3692230130Smav
3693230130Smav/*
3694230130Smav * Look for equivalent DAC/ADC to implement second channel.
3695230130Smav */
3696230130Smavstatic void
3697230130Smavhdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid)
3698230130Smav{
3699230130Smav	struct hdaa_audio_as *as = &devinfo->as[asid];
3700230130Smav	struct hdaa_widget *w1, *w2;
3701230130Smav	int i, pos;
3702230130Smav	nid_t nid1, nid2;
3703230130Smav
3704230130Smav	HDA_BOOTVERBOSE(
3705230130Smav		device_printf(devinfo->dev,
3706230130Smav		    "Looking for additional %sC "
3707230130Smav		    "for association %d (%d)\n",
3708230130Smav		    (as->dir == HDAA_CTL_OUT) ? "DA" : "AD",
3709230130Smav		    asid, as->index);
3710230130Smav	);
3711230130Smav
3712230130Smav	/* Find the exisitng DAC position and return if found more the one. */
3713230130Smav	pos = -1;
3714230130Smav	for (i = 0; i < 16; i++) {
3715230130Smav		if (as->dacs[0][i] <= 0)
3716230130Smav			continue;
3717230130Smav		if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos])
3718230130Smav			return;
3719230130Smav		pos = i;
3720230130Smav	}
3721230130Smav
3722230130Smav	nid1 = as->dacs[0][pos];
3723230130Smav	w1 = hdaa_widget_get(devinfo, nid1);
3724230130Smav	w2 = NULL;
3725230130Smav	for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) {
3726230130Smav		w2 = hdaa_widget_get(devinfo, nid2);
3727230130Smav		if (w2 == NULL || w2->enable == 0)
3728230130Smav			continue;
3729230130Smav		if (w2->bindas >= 0)
3730230130Smav			continue;
3731230130Smav		if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
3732230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
3733230130Smav				continue;
3734230130Smav			if (hdaa_audio_dacs_equal(w1, w2))
3735230130Smav				break;
3736230130Smav		} else {
3737230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3738230130Smav				continue;
3739230130Smav			if (hdaa_audio_adcs_equal(w1, w2))
3740230130Smav				break;
3741230130Smav		}
3742230130Smav	}
3743230130Smav	if (nid2 >= devinfo->endnode)
3744230130Smav		return;
3745230130Smav	w2->bindas = w1->bindas;
3746230130Smav	w2->bindseqmask = w1->bindseqmask;
3747230130Smav	if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
3748230130Smav		HDA_BOOTVERBOSE(
3749230130Smav			device_printf(devinfo->dev,
3750230130Smav			    " ADC %d considered equal to ADC %d\n", nid2, nid1);
3751230130Smav		);
3752230130Smav		w1 = hdaa_widget_get(devinfo, w1->conns[0]);
3753230130Smav		w2 = hdaa_widget_get(devinfo, w2->conns[0]);
3754230130Smav		w2->bindas = w1->bindas;
3755230130Smav		w2->bindseqmask = w1->bindseqmask;
3756230130Smav	} else {
3757230130Smav		HDA_BOOTVERBOSE(
3758230130Smav			device_printf(devinfo->dev,
3759230130Smav			    " DAC %d considered equal to DAC %d\n", nid2, nid1);
3760230130Smav		);
3761230130Smav	}
3762230130Smav	for (i = 0; i < 16; i++) {
3763230130Smav		if (as->dacs[0][i] <= 0)
3764230130Smav			continue;
3765230130Smav		as->dacs[as->num_chans][i] = nid2;
3766230130Smav	}
3767230130Smav	as->num_chans++;
3768230130Smav}
3769230130Smav
3770230130Smav/*
3771230130Smav * Trace association path from input to ADC
3772230130Smav */
3773230130Smavstatic int
3774230130Smavhdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as)
3775230130Smav{
3776230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3777230130Smav	struct hdaa_widget *w;
3778230130Smav	int i, j, k, length;
3779230130Smav
3780230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3781230130Smav		w = hdaa_widget_get(devinfo, j);
3782230130Smav		if (w == NULL || w->enable == 0)
3783230130Smav			continue;
3784230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3785230130Smav			continue;
3786230130Smav		if (w->bindas >= 0 && w->bindas != as)
3787230130Smav			continue;
3788230130Smav
3789230130Smav		/* Find next pin */
3790230130Smav		for (i = 0; i < 16; i++) {
3791230130Smav			if (ases[as].pins[i] == 0)
3792230130Smav				continue;
3793230130Smav
3794230130Smav			HDA_BOOTHVERBOSE(
3795230130Smav				device_printf(devinfo->dev,
3796230130Smav				    " Tracing pin %d to ADC %d\n",
3797230130Smav				    ases[as].pins[i], j);
3798230130Smav			);
3799230130Smav			/* Trace this pin taking goal into account. */
3800230130Smav			if (hdaa_audio_trace_adc(devinfo, as, i,
3801230130Smav			    ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) {
3802230130Smav				/* If we failed - return to previous and redo it. */
3803230130Smav				HDA_BOOTVERBOSE(
3804230130Smav					device_printf(devinfo->dev,
3805230130Smav					    " Unable to trace pin %d to ADC %d, undo traces\n",
3806230130Smav					    ases[as].pins[i], j);
3807230130Smav				);
3808230130Smav				hdaa_audio_undo_trace(devinfo, as, -1);
3809230130Smav				for (k = 0; k < 16; k++)
3810230130Smav					ases[as].dacs[0][k] = 0;
3811230130Smav				break;
3812230130Smav			}
3813230130Smav			HDA_BOOTVERBOSE(
3814230130Smav				device_printf(devinfo->dev,
3815230130Smav				    " Pin %d traced to ADC %d\n",
3816230130Smav				    ases[as].pins[i], j);
3817230130Smav			);
3818230130Smav			ases[as].dacs[0][i] = j;
3819230130Smav		}
3820230130Smav		if (i == 16)
3821230130Smav			return (1);
3822230130Smav	}
3823230130Smav	return (0);
3824230130Smav}
3825230130Smav
3826230130Smav/*
3827230130Smav * Trace association path from input to multiple ADCs
3828230130Smav */
3829230130Smavstatic int
3830230130Smavhdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq)
3831230130Smav{
3832230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3833230130Smav	int i, length;
3834230130Smav	nid_t min, res;
3835230130Smav
3836230130Smav	/* Find next pin */
3837230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3838230130Smav		;
3839230130Smav	/* Check if there is no any left. If so - we succeeded. */
3840230130Smav	if (i == 16)
3841230130Smav		return (1);
3842230130Smav
3843230130Smav	min = 0;
3844230130Smav	do {
3845230130Smav		HDA_BOOTHVERBOSE(
3846230130Smav			device_printf(devinfo->dev,
3847230130Smav			    " Tracing pin %d with min nid %d",
3848230130Smav			    ases[as].pins[i], min);
3849230130Smav			printf("\n");
3850230130Smav		);
3851230130Smav		/* Trace this pin taking min nid into account. */
3852230130Smav		res = hdaa_audio_trace_adc(devinfo, as, i,
3853230130Smav		    ases[as].pins[i], 0, min, 0, 0, &length, 0);
3854230130Smav		if (res == 0) {
3855230130Smav			/* If we failed - return to previous and redo it. */
3856230130Smav			HDA_BOOTVERBOSE(
3857230130Smav				device_printf(devinfo->dev,
3858230130Smav				    " Unable to trace pin %d seq %d with min "
3859230130Smav				    "nid %d",
3860230130Smav				    ases[as].pins[i], i, min);
3861230130Smav				printf("\n");
3862230130Smav			);
3863230130Smav			return (0);
3864230130Smav		}
3865230130Smav		HDA_BOOTVERBOSE(
3866230130Smav			device_printf(devinfo->dev,
3867230130Smav			    " Pin %d traced to ADC %d\n",
3868230130Smav			    ases[as].pins[i], res);
3869230130Smav		);
3870230130Smav		/* Trace again to mark the path */
3871230130Smav		hdaa_audio_trace_adc(devinfo, as, i,
3872230130Smav		    ases[as].pins[i], 0, min, res, 0, &length, length);
3873230130Smav		ases[as].dacs[0][i] = res;
3874230130Smav		/* We succeeded, so call next. */
3875230130Smav		if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1))
3876230130Smav			return (1);
3877230130Smav		/* If next failed, we should retry with next min */
3878230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3879230130Smav		ases[as].dacs[0][i] = 0;
3880230130Smav		min = res + 1;
3881230130Smav	} while (1);
3882230130Smav}
3883230130Smav
3884230130Smav/*
3885230130Smav * Trace input monitor path from mixer to output association.
3886230130Smav */
3887230130Smavstatic int
3888230130Smavhdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth)
3889230130Smav{
3890230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3891230130Smav	struct hdaa_widget *w, *wc;
3892230130Smav	int i, j;
3893230130Smav	nid_t res = 0;
3894230130Smav
3895230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3896230130Smav		return (0);
3897230130Smav	w = hdaa_widget_get(devinfo, nid);
3898230130Smav	if (w == NULL || w->enable == 0)
3899230130Smav		return (0);
3900230130Smav	HDA_BOOTHVERBOSE(
3901230130Smav		device_printf(devinfo->dev,
3902230130Smav		    " %*stracing via nid %d\n",
3903230130Smav			depth + 1, "", w->nid);
3904230130Smav	);
3905230130Smav	/* Use only unused widgets */
3906230130Smav	if (depth > 0 && w->bindas != -1) {
3907230130Smav		if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) {
3908230130Smav			HDA_BOOTHVERBOSE(
3909230130Smav				device_printf(devinfo->dev,
3910230130Smav				    " %*snid %d found output association %d\n",
3911230130Smav					depth + 1, "", w->nid, w->bindas);
3912230130Smav			);
3913230130Smav			if (w->bindas >= 0)
3914230130Smav				w->pflags |= HDAA_ADC_MONITOR;
3915230130Smav			return (1);
3916230130Smav		} else {
3917230130Smav			HDA_BOOTHVERBOSE(
3918230130Smav				device_printf(devinfo->dev,
3919230130Smav				    " %*snid %d busy by input association %d\n",
3920230130Smav					depth + 1, "", w->nid, w->bindas);
3921230130Smav			);
3922230130Smav			return (0);
3923230130Smav		}
3924230130Smav	}
3925230130Smav
3926230130Smav	switch (w->type) {
3927230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3928230130Smav		/* Do not traverse input. AD1988 has digital monitor
3929230130Smav		for which we are not ready. */
3930230130Smav		break;
3931230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3932230130Smav		if (depth > 0)
3933230130Smav			break;
3934230130Smav		/* Fall */
3935230130Smav	default:
3936230130Smav		/* Try to find reachable ADCs with specified nid. */
3937230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3938230130Smav			wc = hdaa_widget_get(devinfo, j);
3939230130Smav			if (wc == NULL || wc->enable == 0)
3940230130Smav				continue;
3941230130Smav			for (i = 0; i < wc->nconns; i++) {
3942230130Smav				if (wc->connsenable[i] == 0)
3943230130Smav					continue;
3944230130Smav				if (wc->conns[i] != nid)
3945230130Smav					continue;
3946230130Smav				if (hdaa_audio_trace_to_out(devinfo,
3947230130Smav				    j, depth + 1) != 0) {
3948230130Smav					res = 1;
3949230130Smav					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3950230130Smav					    wc->selconn == -1)
3951230130Smav						wc->selconn = i;
3952230130Smav				}
3953230130Smav			}
3954230130Smav		}
3955230130Smav		break;
3956230130Smav	}
3957230130Smav	if (res && w->bindas == -1)
3958230130Smav		w->bindas = -2;
3959230130Smav
3960230130Smav	HDA_BOOTHVERBOSE(
3961230130Smav		device_printf(devinfo->dev,
3962230130Smav		    " %*snid %d returned %d\n",
3963230130Smav			depth + 1, "", w->nid, res);
3964230130Smav	);
3965230130Smav	return (res);
3966230130Smav}
3967230130Smav
3968230130Smav/*
3969230130Smav * Trace extra associations (beeper, monitor)
3970230130Smav */
3971230130Smavstatic void
3972230130Smavhdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo)
3973230130Smav{
3974230130Smav	struct hdaa_audio_as *as = devinfo->as;
3975230130Smav	struct hdaa_widget *w;
3976230130Smav	int j;
3977230130Smav
3978230130Smav	/* Input monitor */
3979230130Smav	/* Find mixer associated with input, but supplying signal
3980230130Smav	   for output associations. Hope it will be input monitor. */
3981230130Smav	HDA_BOOTVERBOSE(
3982230130Smav		device_printf(devinfo->dev,
3983230130Smav		    "Tracing input monitor\n");
3984230130Smav	);
3985230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3986230130Smav		w = hdaa_widget_get(devinfo, j);
3987230130Smav		if (w == NULL || w->enable == 0)
3988230130Smav			continue;
3989230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3990230130Smav			continue;
3991230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3992230130Smav			continue;
3993230130Smav		HDA_BOOTVERBOSE(
3994230130Smav			device_printf(devinfo->dev,
3995230130Smav			    " Tracing nid %d to out\n",
3996230130Smav			    j);
3997230130Smav		);
3998230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3999230130Smav			HDA_BOOTVERBOSE(
4000230130Smav				device_printf(devinfo->dev,
4001230130Smav				    " nid %d is input monitor\n",
4002230130Smav					w->nid);
4003230130Smav			);
4004230130Smav			w->ossdev = SOUND_MIXER_IMIX;
4005230130Smav		}
4006230130Smav	}
4007230130Smav
4008230130Smav	/* Other inputs monitor */
4009230130Smav	/* Find input pins supplying signal for output associations.
4010230130Smav	   Hope it will be input monitoring. */
4011230130Smav	HDA_BOOTVERBOSE(
4012230130Smav		device_printf(devinfo->dev,
4013230130Smav		    "Tracing other input monitors\n");
4014230130Smav	);
4015230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4016230130Smav		w = hdaa_widget_get(devinfo, j);
4017230130Smav		if (w == NULL || w->enable == 0)
4018230130Smav			continue;
4019230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4020230130Smav			continue;
4021230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
4022230130Smav			continue;
4023230130Smav		HDA_BOOTVERBOSE(
4024230130Smav			device_printf(devinfo->dev,
4025230130Smav			    " Tracing nid %d to out\n",
4026230130Smav			    j);
4027230130Smav		);
4028230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4029230130Smav			HDA_BOOTVERBOSE(
4030230130Smav				device_printf(devinfo->dev,
4031230130Smav				    " nid %d is input monitor\n",
4032230130Smav					w->nid);
4033230130Smav			);
4034230130Smav		}
4035230130Smav	}
4036230130Smav
4037230130Smav	/* Beeper */
4038230130Smav	HDA_BOOTVERBOSE(
4039230130Smav		device_printf(devinfo->dev,
4040230130Smav		    "Tracing beeper\n");
4041230130Smav	);
4042230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4043230130Smav		w = hdaa_widget_get(devinfo, j);
4044230130Smav		if (w == NULL || w->enable == 0)
4045230130Smav			continue;
4046230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
4047230130Smav			continue;
4048230130Smav		HDA_BOOTHVERBOSE(
4049230130Smav			device_printf(devinfo->dev,
4050230130Smav			    " Tracing nid %d to out\n",
4051230130Smav			    j);
4052230130Smav		);
4053230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4054230130Smav			HDA_BOOTVERBOSE(
4055230130Smav				device_printf(devinfo->dev,
4056230130Smav				    " nid %d traced to out\n",
4057230130Smav				    j);
4058230130Smav			);
4059230130Smav		}
4060230130Smav		w->bindas = -2;
4061230130Smav	}
4062230130Smav}
4063230130Smav
4064230130Smav/*
4065230130Smav * Bind assotiations to PCM channels
4066230130Smav */
4067230130Smavstatic void
4068230130Smavhdaa_audio_bind_as(struct hdaa_devinfo *devinfo)
4069230130Smav{
4070230130Smav	struct hdaa_audio_as *as = devinfo->as;
4071230130Smav	int i, j, cnt = 0, free;
4072230130Smav
4073230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4074230130Smav		if (as[j].enable)
4075230130Smav			cnt += as[j].num_chans;
4076230130Smav	}
4077230130Smav	if (devinfo->num_chans == 0) {
4078230130Smav		devinfo->chans = (struct hdaa_chan *)malloc(
4079230130Smav		    sizeof(struct hdaa_chan) * cnt,
4080230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
4081230130Smav		if (devinfo->chans == NULL) {
4082230130Smav			device_printf(devinfo->dev,
4083230130Smav			    "Channels memory allocation failed!\n");
4084230130Smav			return;
4085230130Smav		}
4086230130Smav	} else {
4087230130Smav		devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans,
4088230130Smav		    sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt),
4089230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
4090230130Smav		if (devinfo->chans == NULL) {
4091230130Smav			devinfo->num_chans = 0;
4092230130Smav			device_printf(devinfo->dev,
4093230130Smav			    "Channels memory allocation failed!\n");
4094230130Smav			return;
4095230130Smav		}
4096230130Smav		/* Fixup relative pointers after realloc */
4097230130Smav		for (j = 0; j < devinfo->num_chans; j++)
4098230130Smav			devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist;
4099230130Smav	}
4100230130Smav	free = devinfo->num_chans;
4101230130Smav	devinfo->num_chans += cnt;
4102230130Smav
4103230130Smav	for (j = free; j < free + cnt; j++) {
4104230130Smav		devinfo->chans[j].devinfo = devinfo;
4105230130Smav		devinfo->chans[j].as = -1;
4106230130Smav	}
4107230130Smav
4108230130Smav	/* Assign associations in order of their numbers, */
4109230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4110230130Smav		if (as[j].enable == 0)
4111230130Smav			continue;
4112230130Smav		for (i = 0; i < as[j].num_chans; i++) {
4113230130Smav			devinfo->chans[free].as = j;
4114230130Smav			devinfo->chans[free].asindex = i;
4115230130Smav			devinfo->chans[free].dir =
4116230130Smav			    (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
4117230130Smav			hdaa_pcmchannel_setup(&devinfo->chans[free]);
4118230130Smav			as[j].chans[i] = free;
4119230130Smav			free++;
4120230130Smav		}
4121230130Smav	}
4122230130Smav}
4123230130Smav
4124230130Smavstatic void
4125230130Smavhdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo)
4126230130Smav{
4127230130Smav	struct hdaa_widget *w;
4128230130Smav	int i;
4129230130Smav
4130230130Smav	/* Disable power and volume widgets. */
4131230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4132230130Smav		w = hdaa_widget_get(devinfo, i);
4133230130Smav		if (w == NULL || w->enable == 0)
4134230130Smav			continue;
4135230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
4136230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
4137230130Smav			w->enable = 0;
4138230130Smav			HDA_BOOTHVERBOSE(
4139230130Smav				device_printf(devinfo->dev,
4140230130Smav				    " Disabling nid %d due to it's"
4141230130Smav				    " non-audio type.\n",
4142230130Smav				    w->nid);
4143230130Smav			);
4144230130Smav		}
4145230130Smav	}
4146230130Smav}
4147230130Smav
4148230130Smavstatic void
4149230130Smavhdaa_audio_disable_useless(struct hdaa_devinfo *devinfo)
4150230130Smav{
4151230130Smav	struct hdaa_widget *w, *cw;
4152230130Smav	struct hdaa_audio_ctl *ctl;
4153230130Smav	int done, found, i, j, k;
4154230130Smav
4155230130Smav	/* Disable useless pins. */
4156230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4157230130Smav		w = hdaa_widget_get(devinfo, i);
4158230130Smav		if (w == NULL || w->enable == 0)
4159230130Smav			continue;
4160230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
4161230130Smav			if ((w->wclass.pin.config &
4162230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
4163230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
4164230130Smav				w->enable = 0;
4165230130Smav				HDA_BOOTHVERBOSE(
4166230130Smav					device_printf(devinfo->dev,
4167230130Smav					    " Disabling pin nid %d due"
4168230130Smav					    " to None connectivity.\n",
4169230130Smav					    w->nid);
4170230130Smav				);
4171230130Smav			} else if ((w->wclass.pin.config &
4172230130Smav			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
4173230130Smav				w->enable = 0;
4174230130Smav				HDA_BOOTHVERBOSE(
4175230130Smav					device_printf(devinfo->dev,
4176230130Smav					    " Disabling unassociated"
4177230130Smav					    " pin nid %d.\n",
4178230130Smav					    w->nid);
4179230130Smav				);
4180230130Smav			}
4181230130Smav		}
4182230130Smav	}
4183230130Smav	do {
4184230130Smav		done = 1;
4185230130Smav		/* Disable and mute controls for disabled widgets. */
4186230130Smav		i = 0;
4187230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4188230130Smav			if (ctl->enable == 0)
4189230130Smav				continue;
4190230130Smav			if (ctl->widget->enable == 0 ||
4191230130Smav			    (ctl->childwidget != NULL &&
4192230130Smav			    ctl->childwidget->enable == 0)) {
4193230130Smav				ctl->forcemute = 1;
4194230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
4195230130Smav				ctl->left = 0;
4196230130Smav				ctl->right = 0;
4197230130Smav				ctl->enable = 0;
4198230130Smav				if (ctl->ndir == HDAA_CTL_IN)
4199230130Smav					ctl->widget->connsenable[ctl->index] = 0;
4200230130Smav				done = 0;
4201230130Smav				HDA_BOOTHVERBOSE(
4202230130Smav					device_printf(devinfo->dev,
4203230130Smav					    " Disabling ctl %d nid %d cnid %d due"
4204230130Smav					    " to disabled widget.\n", i,
4205230130Smav					    ctl->widget->nid,
4206230130Smav					    (ctl->childwidget != NULL)?
4207230130Smav					    ctl->childwidget->nid:-1);
4208230130Smav				);
4209230130Smav			}
4210230130Smav		}
4211230130Smav		/* Disable useless widgets. */
4212230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4213230130Smav			w = hdaa_widget_get(devinfo, i);
4214230130Smav			if (w == NULL || w->enable == 0)
4215230130Smav				continue;
4216230130Smav			/* Disable inputs with disabled child widgets. */
4217230130Smav			for (j = 0; j < w->nconns; j++) {
4218230130Smav				if (w->connsenable[j]) {
4219230130Smav					cw = hdaa_widget_get(devinfo, w->conns[j]);
4220230130Smav					if (cw == NULL || cw->enable == 0) {
4221230130Smav						w->connsenable[j] = 0;
4222230130Smav						HDA_BOOTHVERBOSE(
4223230130Smav							device_printf(devinfo->dev,
4224230130Smav							    " Disabling nid %d connection %d due"
4225230130Smav							    " to disabled child widget.\n",
4226230130Smav							    i, j);
4227230130Smav						);
4228230130Smav					}
4229230130Smav				}
4230230130Smav			}
4231230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4232230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4233230130Smav				continue;
4234230130Smav			/* Disable mixers and selectors without inputs. */
4235230130Smav			found = 0;
4236230130Smav			for (j = 0; j < w->nconns; j++) {
4237230130Smav				if (w->connsenable[j]) {
4238230130Smav					found = 1;
4239230130Smav					break;
4240230130Smav				}
4241230130Smav			}
4242230130Smav			if (found == 0) {
4243230130Smav				w->enable = 0;
4244230130Smav				done = 0;
4245230130Smav				HDA_BOOTHVERBOSE(
4246230130Smav					device_printf(devinfo->dev,
4247230130Smav					    " Disabling nid %d due to all it's"
4248230130Smav					    " inputs disabled.\n", w->nid);
4249230130Smav				);
4250230130Smav			}
4251230130Smav			/* Disable nodes without consumers. */
4252230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4253230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4254230130Smav				continue;
4255230130Smav			found = 0;
4256230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4257230130Smav				cw = hdaa_widget_get(devinfo, k);
4258230130Smav				if (cw == NULL || cw->enable == 0)
4259230130Smav					continue;
4260230130Smav				for (j = 0; j < cw->nconns; j++) {
4261230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
4262230130Smav						found = 1;
4263230130Smav						break;
4264230130Smav					}
4265230130Smav				}
4266230130Smav			}
4267230130Smav			if (found == 0) {
4268230130Smav				w->enable = 0;
4269230130Smav				done = 0;
4270230130Smav				HDA_BOOTHVERBOSE(
4271230130Smav					device_printf(devinfo->dev,
4272230130Smav					    " Disabling nid %d due to all it's"
4273230130Smav					    " consumers disabled.\n", w->nid);
4274230130Smav				);
4275230130Smav			}
4276230130Smav		}
4277230130Smav	} while (done == 0);
4278230130Smav
4279230130Smav}
4280230130Smav
4281230130Smavstatic void
4282230130Smavhdaa_audio_disable_unas(struct hdaa_devinfo *devinfo)
4283230130Smav{
4284230130Smav	struct hdaa_audio_as *as = devinfo->as;
4285230130Smav	struct hdaa_widget *w, *cw;
4286230130Smav	struct hdaa_audio_ctl *ctl;
4287230130Smav	int i, j, k;
4288230130Smav
4289230130Smav	/* Disable unassosiated widgets. */
4290230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4291230130Smav		w = hdaa_widget_get(devinfo, i);
4292230130Smav		if (w == NULL || w->enable == 0)
4293230130Smav			continue;
4294230130Smav		if (w->bindas == -1) {
4295230130Smav			w->enable = 0;
4296230130Smav			HDA_BOOTHVERBOSE(
4297230130Smav				device_printf(devinfo->dev,
4298230130Smav				    " Disabling unassociated nid %d.\n",
4299230130Smav				    w->nid);
4300230130Smav			);
4301230130Smav		}
4302230130Smav	}
4303230130Smav	/* Disable input connections on input pin and
4304230130Smav	 * output on output. */
4305230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4306230130Smav		w = hdaa_widget_get(devinfo, i);
4307230130Smav		if (w == NULL || w->enable == 0)
4308230130Smav			continue;
4309230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4310230130Smav			continue;
4311230130Smav		if (w->bindas < 0)
4312230130Smav			continue;
4313230130Smav		if (as[w->bindas].dir == HDAA_CTL_IN) {
4314230130Smav			for (j = 0; j < w->nconns; j++) {
4315230130Smav				if (w->connsenable[j] == 0)
4316230130Smav					continue;
4317230130Smav				w->connsenable[j] = 0;
4318230130Smav				HDA_BOOTHVERBOSE(
4319230130Smav					device_printf(devinfo->dev,
4320230130Smav					    " Disabling connection to input pin "
4321230130Smav					    "nid %d conn %d.\n",
4322230130Smav					    i, j);
4323230130Smav				);
4324230130Smav			}
4325230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4326230130Smav			    HDAA_CTL_IN, -1, 1);
4327230130Smav			if (ctl && ctl->enable) {
4328230130Smav				ctl->forcemute = 1;
4329230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
4330230130Smav				ctl->left = 0;
4331230130Smav				ctl->right = 0;
4332230130Smav				ctl->enable = 0;
4333230130Smav			}
4334230130Smav		} else {
4335230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4336230130Smav			    HDAA_CTL_OUT, -1, 1);
4337230130Smav			if (ctl && ctl->enable) {
4338230130Smav				ctl->forcemute = 1;
4339230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
4340230130Smav				ctl->left = 0;
4341230130Smav				ctl->right = 0;
4342230130Smav				ctl->enable = 0;
4343230130Smav			}
4344230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4345230130Smav				cw = hdaa_widget_get(devinfo, k);
4346230130Smav				if (cw == NULL || cw->enable == 0)
4347230130Smav					continue;
4348230130Smav				for (j = 0; j < cw->nconns; j++) {
4349230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
4350230130Smav						cw->connsenable[j] = 0;
4351230130Smav						HDA_BOOTHVERBOSE(
4352230130Smav							device_printf(devinfo->dev,
4353230130Smav							    " Disabling connection from output pin "
4354230130Smav							    "nid %d conn %d cnid %d.\n",
4355230130Smav							    k, j, i);
4356230130Smav						);
4357230130Smav						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4358230130Smav						    cw->nconns > 1)
4359230130Smav							continue;
4360230130Smav						ctl = hdaa_audio_ctl_amp_get(devinfo, k,
4361230130Smav					    HDAA_CTL_IN, j, 1);
4362230130Smav						if (ctl && ctl->enable) {
4363230130Smav							ctl->forcemute = 1;
4364230130Smav							ctl->muted = HDAA_AMP_MUTE_ALL;
4365230130Smav							ctl->left = 0;
4366230130Smav							ctl->right = 0;
4367230130Smav							ctl->enable = 0;
4368230130Smav						}
4369230130Smav					}
4370230130Smav				}
4371230130Smav			}
4372230130Smav		}
4373230130Smav	}
4374230130Smav}
4375230130Smav
4376230130Smavstatic void
4377230130Smavhdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo)
4378230130Smav{
4379230130Smav	struct hdaa_audio_as *as = devinfo->as;
4380230130Smav	struct hdaa_widget *w;
4381230130Smav	int i, j;
4382230130Smav
4383230130Smav	/* On playback path we can safely disable all unseleted inputs. */
4384230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4385230130Smav		w = hdaa_widget_get(devinfo, i);
4386230130Smav		if (w == NULL || w->enable == 0)
4387230130Smav			continue;
4388230130Smav		if (w->nconns <= 1)
4389230130Smav			continue;
4390230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4391230130Smav			continue;
4392230130Smav		if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN)
4393230130Smav			continue;
4394230130Smav		for (j = 0; j < w->nconns; j++) {
4395230130Smav			if (w->connsenable[j] == 0)
4396230130Smav				continue;
4397230130Smav			if (w->selconn < 0 || w->selconn == j)
4398230130Smav				continue;
4399230130Smav			w->connsenable[j] = 0;
4400230130Smav			HDA_BOOTHVERBOSE(
4401230130Smav				device_printf(devinfo->dev,
4402230130Smav				    " Disabling unselected connection "
4403230130Smav				    "nid %d conn %d.\n",
4404230130Smav				    i, j);
4405230130Smav			);
4406230130Smav		}
4407230130Smav	}
4408230130Smav}
4409230130Smav
4410230130Smavstatic void
4411230130Smavhdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo)
4412230130Smav{
4413230130Smav	struct hdaa_audio_as *ases = devinfo->as;
4414230130Smav	struct hdaa_widget *w, *cw;
4415230130Smav	struct hdaa_audio_ctl *ctl;
4416230130Smav	int i, j;
4417230130Smav
4418230130Smav	/* Disable crossassociatement and unwanted crosschannel connections. */
4419230130Smav	/* ... using selectors */
4420230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4421230130Smav		w = hdaa_widget_get(devinfo, i);
4422230130Smav		if (w == NULL || w->enable == 0)
4423230130Smav			continue;
4424230130Smav		if (w->nconns <= 1)
4425230130Smav			continue;
4426230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4427230130Smav			continue;
4428230130Smav		/* Allow any -> mix */
4429230130Smav		if (w->bindas == -2)
4430230130Smav			continue;
4431230130Smav		for (j = 0; j < w->nconns; j++) {
4432230130Smav			if (w->connsenable[j] == 0)
4433230130Smav				continue;
4434230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
4435230130Smav			if (cw == NULL || w->enable == 0)
4436230130Smav				continue;
4437230130Smav			/* Allow mix -> out. */
4438230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
4439230130Smav			    ases[w->bindas].dir == HDAA_CTL_OUT)
4440230130Smav				continue;
4441230130Smav			/* Allow mix -> mixed-in. */
4442230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
4443230130Smav			    ases[w->bindas].mixed)
4444230130Smav				continue;
4445230130Smav			/* Allow in -> mix. */
4446230130Smav			if ((w->pflags & HDAA_ADC_MONITOR) &&
4447230130Smav			     cw->bindas >= 0 &&
4448230130Smav			     ases[cw->bindas].dir == HDAA_CTL_IN)
4449230130Smav				continue;
4450230130Smav			/* Allow if have common as/seqs. */
4451230130Smav			if (w->bindas == cw->bindas &&
4452230130Smav			    (w->bindseqmask & cw->bindseqmask) != 0)
4453230130Smav				continue;
4454230130Smav			w->connsenable[j] = 0;
4455230130Smav			HDA_BOOTHVERBOSE(
4456230130Smav				device_printf(devinfo->dev,
4457230130Smav				    " Disabling crossassociatement connection "
4458230130Smav				    "nid %d conn %d cnid %d.\n",
4459230130Smav				    i, j, cw->nid);
4460230130Smav			);
4461230130Smav		}
4462230130Smav	}
4463230130Smav	/* ... using controls */
4464230130Smav	i = 0;
4465230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4466230130Smav		if (ctl->enable == 0 || ctl->childwidget == NULL)
4467230130Smav			continue;
4468230130Smav		/* Allow any -> mix */
4469230130Smav		if (ctl->widget->bindas == -2)
4470230130Smav			continue;
4471230130Smav		/* Allow mix -> out. */
4472230130Smav		if (ctl->childwidget->bindas == -2 &&
4473230130Smav		    ctl->widget->bindas >= 0 &&
4474230130Smav		    ases[ctl->widget->bindas].dir == HDAA_CTL_OUT)
4475230130Smav			continue;
4476230130Smav		/* Allow mix -> mixed-in. */
4477230130Smav		if (ctl->childwidget->bindas == -2 &&
4478230130Smav		    ctl->widget->bindas >= 0 &&
4479230130Smav		    ases[ctl->widget->bindas].mixed)
4480230130Smav			continue;
4481230130Smav		/* Allow in -> mix. */
4482230130Smav		if ((ctl->widget->pflags & HDAA_ADC_MONITOR) &&
4483230130Smav		    ctl->childwidget->bindas >= 0 &&
4484230130Smav		    ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN)
4485230130Smav			continue;
4486230130Smav		/* Allow if have common as/seqs. */
4487230130Smav		if (ctl->widget->bindas == ctl->childwidget->bindas &&
4488230130Smav		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0)
4489230130Smav			continue;
4490230130Smav		ctl->forcemute = 1;
4491230130Smav		ctl->muted = HDAA_AMP_MUTE_ALL;
4492230130Smav		ctl->left = 0;
4493230130Smav		ctl->right = 0;
4494230130Smav		ctl->enable = 0;
4495230130Smav		if (ctl->ndir == HDAA_CTL_IN)
4496230130Smav			ctl->widget->connsenable[ctl->index] = 0;
4497230130Smav		HDA_BOOTHVERBOSE(
4498230130Smav			device_printf(devinfo->dev,
4499230130Smav			    " Disabling crossassociatement connection "
4500230130Smav			    "ctl %d nid %d cnid %d.\n", i,
4501230130Smav			    ctl->widget->nid,
4502230130Smav			    ctl->childwidget->nid);
4503230130Smav		);
4504230130Smav	}
4505230130Smav
4506230130Smav}
4507230130Smav
4508230130Smav/*
4509230451Smav * Find controls to control amplification for source and calculate possible
4510230451Smav * amplification range.
4511230130Smav */
4512230130Smavstatic int
4513230130Smavhdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4514230451Smav    int ossdev, int ctlable, int depth, int *minamp, int *maxamp)
4515230130Smav{
4516230130Smav	struct hdaa_widget *w, *wc;
4517230130Smav	struct hdaa_audio_ctl *ctl;
4518230451Smav	int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4519230130Smav
4520230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4521230451Smav		return (found);
4522230130Smav
4523230130Smav	w = hdaa_widget_get(devinfo, nid);
4524230130Smav	if (w == NULL || w->enable == 0)
4525230451Smav		return (found);
4526230130Smav
4527230130Smav	/* Count number of active inputs. */
4528230130Smav	if (depth > 0) {
4529230130Smav		for (j = 0; j < w->nconns; j++) {
4530230451Smav			if (!w->connsenable[j])
4531230451Smav				continue;
4532230451Smav			conns++;
4533230130Smav		}
4534230130Smav	}
4535230130Smav
4536230130Smav	/* If this is not a first step - use input mixer.
4537230130Smav	   Pins have common input ctl so care must be taken. */
4538230130Smav	if (depth > 0 && ctlable && (conns == 1 ||
4539230130Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
4540230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
4541230130Smav		    index, 1);
4542230130Smav		if (ctl) {
4543230451Smav			ctl->ossmask |= (1 << ossdev);
4544230451Smav			found++;
4545230451Smav			if (*minamp == *maxamp) {
4546230451Smav				*minamp += MINQDB(ctl);
4547230451Smav				*maxamp += MAXQDB(ctl);
4548230451Smav			}
4549230130Smav		}
4550230130Smav	}
4551230130Smav
4552230130Smav	/* If widget has own ossdev - not traverse it.
4553230130Smav	   It will be traversed on it's own. */
4554230130Smav	if (w->ossdev >= 0 && depth > 0)
4555230451Smav		return (found);
4556230130Smav
4557230130Smav	/* We must not traverse pin */
4558230130Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4559230130Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
4560230130Smav	    depth > 0)
4561230451Smav		return (found);
4562230130Smav
4563230130Smav	/* record that this widget exports such signal, */
4564230130Smav	w->ossmask |= (1 << ossdev);
4565230130Smav
4566230451Smav	/*
4567230451Smav	 * If signals mixed, we can't assign controls farther.
4568230130Smav	 * Ignore this on depth zero. Caller must knows why.
4569230130Smav	 */
4570230451Smav	if (conns > 1 &&
4571230451Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4572230130Smav		ctlable = 0;
4573230130Smav
4574230130Smav	if (ctlable) {
4575230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
4576230130Smav		if (ctl) {
4577230451Smav			ctl->ossmask |= (1 << ossdev);
4578230451Smav			found++;
4579230451Smav			if (*minamp == *maxamp) {
4580230451Smav				*minamp += MINQDB(ctl);
4581230451Smav				*maxamp += MAXQDB(ctl);
4582230451Smav			}
4583230130Smav		}
4584230130Smav	}
4585230130Smav
4586230451Smav	cminamp = cmaxamp = 0;
4587230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4588230130Smav		wc = hdaa_widget_get(devinfo, i);
4589230130Smav		if (wc == NULL || wc->enable == 0)
4590230130Smav			continue;
4591230130Smav		for (j = 0; j < wc->nconns; j++) {
4592230130Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
4593230451Smav				tminamp = tmaxamp = 0;
4594230451Smav				found += hdaa_audio_ctl_source_amp(devinfo,
4595230451Smav				    wc->nid, j, ossdev, ctlable, depth + 1,
4596230451Smav				    &tminamp, &tmaxamp);
4597230451Smav				if (cminamp == 0 && cmaxamp == 0) {
4598230451Smav					cminamp = tminamp;
4599230451Smav					cmaxamp = tmaxamp;
4600230451Smav				} else if (tminamp != tmaxamp) {
4601230451Smav					cminamp = imax(cminamp, tminamp);
4602230451Smav					cmaxamp = imin(cmaxamp, tmaxamp);
4603230451Smav				}
4604230130Smav			}
4605230130Smav		}
4606230130Smav	}
4607230451Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4608230451Smav		*minamp += cminamp;
4609230451Smav		*maxamp += cmaxamp;
4610230451Smav	}
4611230451Smav	return (found);
4612230130Smav}
4613230130Smav
4614230130Smav/*
4615230451Smav * Find controls to control amplification for destination and calculate
4616230451Smav * possible amplification range.
4617230130Smav */
4618230451Smavstatic int
4619230130Smavhdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4620230451Smav    int ossdev, int depth, int *minamp, int *maxamp)
4621230130Smav{
4622230130Smav	struct hdaa_audio_as *as = devinfo->as;
4623230130Smav	struct hdaa_widget *w, *wc;
4624230130Smav	struct hdaa_audio_ctl *ctl;
4625230451Smav	int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4626230130Smav
4627230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4628230451Smav		return (found);
4629230130Smav
4630230130Smav	w = hdaa_widget_get(devinfo, nid);
4631230130Smav	if (w == NULL || w->enable == 0)
4632230451Smav		return (found);
4633230130Smav
4634230130Smav	if (depth > 0) {
4635230130Smav		/* If this node produce output for several consumers,
4636230130Smav		   we can't touch it. */
4637230130Smav		consumers = 0;
4638230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4639230130Smav			wc = hdaa_widget_get(devinfo, i);
4640230130Smav			if (wc == NULL || wc->enable == 0)
4641230130Smav				continue;
4642230130Smav			for (j = 0; j < wc->nconns; j++) {
4643230130Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
4644230130Smav					consumers++;
4645230130Smav			}
4646230130Smav		}
4647230130Smav		/* The only exception is if real HP redirection is configured
4648230130Smav		   and this is a duplication point.
4649230130Smav		   XXX: Actually exception is not completely correct.
4650230130Smav		   XXX: Duplication point check is not perfect. */
4651230130Smav		if ((consumers == 2 && (w->bindas < 0 ||
4652230130Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
4653230130Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
4654230130Smav		    consumers > 2)
4655230451Smav			return (found);
4656230130Smav
4657230130Smav		/* Else use it's output mixer. */
4658230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4659230130Smav		    HDAA_CTL_OUT, -1, 1);
4660230130Smav		if (ctl) {
4661230451Smav			ctl->ossmask |= (1 << ossdev);
4662230451Smav			found++;
4663230451Smav			if (*minamp == *maxamp) {
4664230451Smav				*minamp += MINQDB(ctl);
4665230451Smav				*maxamp += MAXQDB(ctl);
4666230451Smav			}
4667230130Smav		}
4668230130Smav	}
4669230130Smav
4670230130Smav	/* We must not traverse pin */
4671230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4672230130Smav	    depth > 0)
4673230451Smav		return (found);
4674230130Smav
4675230451Smav	cminamp = cmaxamp = 0;
4676230130Smav	for (i = 0; i < w->nconns; i++) {
4677230130Smav		if (w->connsenable[i] == 0)
4678230130Smav			continue;
4679230130Smav		if (index >= 0 && i != index)
4680230130Smav			continue;
4681230451Smav		tminamp = tmaxamp = 0;
4682230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4683230130Smav		    HDAA_CTL_IN, i, 1);
4684230130Smav		if (ctl) {
4685230451Smav			ctl->ossmask |= (1 << ossdev);
4686230451Smav			found++;
4687230451Smav			if (*minamp == *maxamp) {
4688230451Smav				tminamp += MINQDB(ctl);
4689230451Smav				tmaxamp += MAXQDB(ctl);
4690230451Smav			}
4691230130Smav		}
4692230451Smav		found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
4693230451Smav		    depth + 1, &tminamp, &tmaxamp);
4694230451Smav		if (cminamp == 0 && cmaxamp == 0) {
4695230451Smav			cminamp = tminamp;
4696230451Smav			cmaxamp = tmaxamp;
4697230451Smav		} else if (tminamp != tmaxamp) {
4698230451Smav			cminamp = imax(cminamp, tminamp);
4699230451Smav			cmaxamp = imin(cmaxamp, tmaxamp);
4700230451Smav		}
4701230130Smav	}
4702230451Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4703230451Smav		*minamp += cminamp;
4704230451Smav		*maxamp += cmaxamp;
4705230451Smav	}
4706230451Smav	return (found);
4707230130Smav}
4708230130Smav
4709230130Smav/*
4710230130Smav * Assign OSS names to sound sources
4711230130Smav */
4712230130Smavstatic void
4713230130Smavhdaa_audio_assign_names(struct hdaa_devinfo *devinfo)
4714230130Smav{
4715230130Smav	struct hdaa_audio_as *as = devinfo->as;
4716230130Smav	struct hdaa_widget *w;
4717230130Smav	int i, j;
4718230130Smav	int type = -1, use, used = 0;
4719230130Smav	static const int types[7][13] = {
4720230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4721230130Smav	      SOUND_MIXER_LINE3, -1 },	/* line */
4722230130Smav	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
4723230130Smav	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
4724230130Smav	    { SOUND_MIXER_CD, -1 },	/* cd */
4725230130Smav	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
4726230130Smav	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
4727230130Smav	      -1 },	/* digital */
4728230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4729230130Smav	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
4730230130Smav	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
4731230130Smav	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
4732230130Smav	      -1 }	/* others */
4733230130Smav	};
4734230130Smav
4735230130Smav	/* Surely known names */
4736230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4737230130Smav		w = hdaa_widget_get(devinfo, i);
4738230130Smav		if (w == NULL || w->enable == 0)
4739230130Smav			continue;
4740230130Smav		if (w->bindas == -1)
4741230130Smav			continue;
4742230130Smav		use = -1;
4743230130Smav		switch (w->type) {
4744230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4745230130Smav			if (as[w->bindas].dir == HDAA_CTL_OUT)
4746230130Smav				break;
4747230130Smav			type = -1;
4748230130Smav			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4749230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4750230130Smav				type = 0;
4751230130Smav				break;
4752230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4753230130Smav				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
4754230130Smav				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4755230130Smav					break;
4756230130Smav				type = 1;
4757230130Smav				break;
4758230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4759230130Smav				type = 3;
4760230130Smav				break;
4761230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4762230130Smav				type = 4;
4763230130Smav				break;
4764230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
4765230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
4766230130Smav				type = 5;
4767230130Smav				break;
4768230130Smav			}
4769230130Smav			if (type == -1)
4770230130Smav				break;
4771230130Smav			j = 0;
4772230130Smav			while (types[type][j] >= 0 &&
4773230130Smav			    (used & (1 << types[type][j])) != 0) {
4774230130Smav				j++;
4775230130Smav			}
4776230130Smav			if (types[type][j] >= 0)
4777230130Smav				use = types[type][j];
4778230130Smav			break;
4779230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4780230130Smav			use = SOUND_MIXER_PCM;
4781230130Smav			break;
4782230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
4783230130Smav			use = SOUND_MIXER_SPEAKER;
4784230130Smav			break;
4785230130Smav		default:
4786230130Smav			break;
4787230130Smav		}
4788230130Smav		if (use >= 0) {
4789230130Smav			w->ossdev = use;
4790230130Smav			used |= (1 << use);
4791230130Smav		}
4792230130Smav	}
4793230130Smav	/* Semi-known names */
4794230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4795230130Smav		w = hdaa_widget_get(devinfo, i);
4796230130Smav		if (w == NULL || w->enable == 0)
4797230130Smav			continue;
4798230130Smav		if (w->ossdev >= 0)
4799230130Smav			continue;
4800230130Smav		if (w->bindas == -1)
4801230130Smav			continue;
4802230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4803230130Smav			continue;
4804230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4805230130Smav			continue;
4806230130Smav		type = -1;
4807230130Smav		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4808230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
4809230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4810230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
4811230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
4812230130Smav			type = 0;
4813230130Smav			break;
4814230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4815230130Smav			type = 2;
4816230130Smav			break;
4817230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
4818230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
4819230130Smav			type = 5;
4820230130Smav			break;
4821230130Smav		}
4822230130Smav		if (type == -1)
4823230130Smav			break;
4824230130Smav		j = 0;
4825230130Smav		while (types[type][j] >= 0 &&
4826230130Smav		    (used & (1 << types[type][j])) != 0) {
4827230130Smav			j++;
4828230130Smav		}
4829230130Smav		if (types[type][j] >= 0) {
4830230130Smav			w->ossdev = types[type][j];
4831230130Smav			used |= (1 << types[type][j]);
4832230130Smav		}
4833230130Smav	}
4834230130Smav	/* Others */
4835230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4836230130Smav		w = hdaa_widget_get(devinfo, i);
4837230130Smav		if (w == NULL || w->enable == 0)
4838230130Smav			continue;
4839230130Smav		if (w->ossdev >= 0)
4840230130Smav			continue;
4841230130Smav		if (w->bindas == -1)
4842230130Smav			continue;
4843230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4844230130Smav			continue;
4845230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4846230130Smav			continue;
4847230130Smav		j = 0;
4848230130Smav		while (types[6][j] >= 0 &&
4849230130Smav		    (used & (1 << types[6][j])) != 0) {
4850230130Smav			j++;
4851230130Smav		}
4852230130Smav		if (types[6][j] >= 0) {
4853230130Smav			w->ossdev = types[6][j];
4854230130Smav			used |= (1 << types[6][j]);
4855230130Smav		}
4856230130Smav	}
4857230130Smav}
4858230130Smav
4859230130Smavstatic void
4860230130Smavhdaa_audio_build_tree(struct hdaa_devinfo *devinfo)
4861230130Smav{
4862230130Smav	struct hdaa_audio_as *as = devinfo->as;
4863230130Smav	int j, res;
4864230130Smav
4865230130Smav	/* Trace all associations in order of their numbers. */
4866230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4867230130Smav		if (as[j].enable == 0)
4868230130Smav			continue;
4869230130Smav		HDA_BOOTVERBOSE(
4870230130Smav			device_printf(devinfo->dev,
4871230130Smav			    "Tracing association %d (%d)\n", j, as[j].index);
4872230130Smav		);
4873230130Smav		if (as[j].dir == HDAA_CTL_OUT) {
4874230130Smavretry:
4875230130Smav			res = hdaa_audio_trace_as_out(devinfo, j, 0);
4876230130Smav			if (res == 0 && as[j].hpredir >= 0 &&
4877230130Smav			    as[j].fakeredir == 0) {
4878230130Smav				/* If CODEC can't do analog HP redirection
4879230130Smav				   try to make it using one more DAC. */
4880230130Smav				as[j].fakeredir = 1;
4881230130Smav				goto retry;
4882230130Smav			}
4883230130Smav		} else if (as[j].mixed)
4884230130Smav			res = hdaa_audio_trace_as_in(devinfo, j);
4885230130Smav		else
4886230130Smav			res = hdaa_audio_trace_as_in_mch(devinfo, j, 0);
4887230130Smav		if (res) {
4888230130Smav			HDA_BOOTVERBOSE(
4889230130Smav				device_printf(devinfo->dev,
4890230130Smav				    "Association %d (%d) trace succeeded\n",
4891230130Smav				    j, as[j].index);
4892230130Smav			);
4893230130Smav		} else {
4894230130Smav			HDA_BOOTVERBOSE(
4895230130Smav				device_printf(devinfo->dev,
4896230130Smav				    "Association %d (%d) trace failed\n",
4897230130Smav				    j, as[j].index);
4898230130Smav			);
4899230130Smav			as[j].enable = 0;
4900230130Smav		}
4901230130Smav	}
4902230130Smav
4903230130Smav	/* Look for additional DACs/ADCs. */
4904230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4905230130Smav		if (as[j].enable == 0)
4906230130Smav			continue;
4907230130Smav		hdaa_audio_adddac(devinfo, j);
4908230130Smav	}
4909230130Smav
4910230130Smav	/* Trace mixer and beeper pseudo associations. */
4911230130Smav	hdaa_audio_trace_as_extra(devinfo);
4912230130Smav}
4913230130Smav
4914230451Smav/*
4915230451Smav * Store in pdevinfo new data about whether and how we can control signal
4916230451Smav * for OSS device to/from specified widget.
4917230451Smav */
4918230130Smavstatic void
4919230451Smavhdaa_adjust_amp(struct hdaa_widget *w, int ossdev,
4920230451Smav    int found, int minamp, int maxamp)
4921230451Smav{
4922230451Smav	struct hdaa_devinfo *devinfo = w->devinfo;
4923230451Smav	struct hdaa_pcm_devinfo *pdevinfo;
4924230451Smav
4925230451Smav	if (w->bindas >= 0)
4926230451Smav		pdevinfo = devinfo->as[w->bindas].pdevinfo;
4927230451Smav	else
4928230451Smav		pdevinfo = &devinfo->devs[0];
4929230451Smav	if (found)
4930230451Smav		pdevinfo->ossmask |= (1 << ossdev);
4931230451Smav	if (minamp == 0 && maxamp == 0)
4932230451Smav		return;
4933230451Smav	if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) {
4934230451Smav		pdevinfo->minamp[ossdev] = minamp;
4935230451Smav		pdevinfo->maxamp[ossdev] = maxamp;
4936230451Smav	} else {
4937230451Smav		pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp);
4938230451Smav		pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp);
4939230451Smav	}
4940230451Smav}
4941230451Smav
4942230451Smav/*
4943230451Smav * Trace signals from/to all possible sources/destionstions to find possible
4944230451Smav * recording sources, OSS device control ranges and to assign controls.
4945230451Smav */
4946230451Smavstatic void
4947230130Smavhdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo)
4948230130Smav{
4949230130Smav	struct hdaa_audio_as *as = devinfo->as;
4950230130Smav	struct hdaa_widget *w, *cw;
4951230451Smav	int i, j, minamp, maxamp, found;
4952230130Smav
4953230130Smav	/* Assign mixers to the tree. */
4954230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4955230130Smav		w = hdaa_widget_get(devinfo, i);
4956230130Smav		if (w == NULL || w->enable == 0)
4957230130Smav			continue;
4958230451Smav		minamp = maxamp = 0;
4959230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
4960230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
4961230130Smav		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4962230130Smav		    as[w->bindas].dir == HDAA_CTL_IN)) {
4963230130Smav			if (w->ossdev < 0)
4964230130Smav				continue;
4965230451Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4966230451Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4967230451Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4968230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
4969230451Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4970230451Smav			    SOUND_MIXER_RECLEV, 0, &minamp, &maxamp);
4971230451Smav			hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp);
4972230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4973230130Smav		    as[w->bindas].dir == HDAA_CTL_OUT) {
4974230451Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4975230451Smav			    SOUND_MIXER_VOLUME, 0, &minamp, &maxamp);
4976230451Smav			hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp);
4977230130Smav		}
4978230130Smav		if (w->ossdev == SOUND_MIXER_IMIX) {
4979230451Smav			minamp = maxamp = 0;
4980230451Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4981230451Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4982230451Smav			if (minamp == maxamp) {
4983230130Smav				/* If we are unable to control input monitor
4984230130Smav				   as source - try to control it as destination. */
4985230451Smav				found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4986230451Smav				    w->ossdev, 0, &minamp, &maxamp);
4987230451Smav				w->pflags |= HDAA_IMIX_AS_DST;
4988230130Smav			}
4989230451Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4990230130Smav		}
4991230130Smav		if (w->pflags & HDAA_ADC_MONITOR) {
4992230130Smav			for (j = 0; j < w->nconns; j++) {
4993230130Smav				if (!w->connsenable[j])
4994230130Smav				    continue;
4995230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
4996230130Smav				if (cw == NULL || cw->enable == 0)
4997230130Smav				    continue;
4998230130Smav				if (cw->bindas == -1)
4999230130Smav				    continue;
5000230130Smav				if (cw->bindas >= 0 &&
5001230130Smav				    as[cw->bindas].dir != HDAA_CTL_IN)
5002230130Smav					continue;
5003230451Smav				minamp = maxamp = 0;
5004230451Smav				found = hdaa_audio_ctl_dest_amp(devinfo,
5005230451Smav				    w->nid, j, SOUND_MIXER_IGAIN, 0,
5006230451Smav				    &minamp, &maxamp);
5007230451Smav				hdaa_adjust_amp(w, SOUND_MIXER_IGAIN,
5008230451Smav				    found, minamp, maxamp);
5009230130Smav			}
5010230130Smav		}
5011230130Smav	}
5012230130Smav}
5013230130Smav
5014230130Smavstatic void
5015230130Smavhdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo)
5016230130Smav{
5017230130Smav	struct hdaa_audio_as *as = devinfo->as;
5018230130Smav	struct hdaa_widget *w;
5019230130Smav	uint32_t pincap;
5020230130Smav	int i;
5021230130Smav
5022230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
5023230130Smav		w = &devinfo->widget[i];
5024230130Smav		if (w == NULL)
5025230130Smav			continue;
5026230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
5027230130Smav		    w->waspin == 0)
5028230130Smav			continue;
5029230130Smav
5030230130Smav		pincap = w->wclass.pin.cap;
5031230130Smav
5032230130Smav		/* Disable everything. */
5033230130Smav		w->wclass.pin.ctrl &= ~(
5034230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
5035230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
5036230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
5037230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
5038230130Smav
5039230130Smav		if (w->enable == 0) {
5040230130Smav			/* Pin is unused so left it disabled. */
5041230130Smav			continue;
5042230130Smav		} else if (w->waspin) {
5043230130Smav			/* Enable input for beeper input. */
5044230130Smav			w->wclass.pin.ctrl |=
5045230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5046230130Smav		} else if (w->bindas < 0 || as[w->bindas].enable == 0) {
5047230130Smav			/* Pin is unused so left it disabled. */
5048230130Smav			continue;
5049230130Smav		} else if (as[w->bindas].dir == HDAA_CTL_IN) {
5050230130Smav			/* Input pin, configure for input. */
5051230130Smav			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5052230130Smav				w->wclass.pin.ctrl |=
5053230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5054230130Smav
5055230130Smav			if ((devinfo->quirks & HDAA_QUIRK_IVREF100) &&
5056230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5057230130Smav				w->wclass.pin.ctrl |=
5058230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5059230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5060230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) &&
5061230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5062230130Smav				w->wclass.pin.ctrl |=
5063230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5064230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5065230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) &&
5066230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5067230130Smav				w->wclass.pin.ctrl |=
5068230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5069230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5070230130Smav		} else {
5071230130Smav			/* Output pin, configure for output. */
5072230130Smav			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5073230130Smav				w->wclass.pin.ctrl |=
5074230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
5075230130Smav
5076230130Smav			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
5077230130Smav			    (w->wclass.pin.config &
5078230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
5079230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
5080230130Smav				w->wclass.pin.ctrl |=
5081230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
5082230130Smav
5083230130Smav			if ((devinfo->quirks & HDAA_QUIRK_OVREF100) &&
5084230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5085230130Smav				w->wclass.pin.ctrl |=
5086230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5087230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5088230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) &&
5089230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5090230130Smav				w->wclass.pin.ctrl |=
5091230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5092230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5093230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) &&
5094230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5095230130Smav				w->wclass.pin.ctrl |=
5096230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5097230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5098230130Smav		}
5099230130Smav	}
5100230130Smav}
5101230130Smav
5102230130Smavstatic void
5103230130Smavhdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo)
5104230130Smav{
5105230130Smav	struct hdaa_audio_ctl *ctl;
5106230130Smav	int i, z;
5107230130Smav
5108230130Smav	i = 0;
5109230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5110230130Smav		if (ctl->enable == 0 || ctl->ossmask != 0) {
5111230130Smav			/* Mute disabled and mixer controllable controls.
5112230130Smav			 * Last will be initialized by mixer_init().
5113230130Smav			 * This expected to reduce click on startup. */
5114230130Smav			hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0);
5115230130Smav			continue;
5116230130Smav		}
5117230130Smav		/* Init fixed controls to 0dB amplification. */
5118230130Smav		z = ctl->offset;
5119230130Smav		if (z > ctl->step)
5120230130Smav			z = ctl->step;
5121230130Smav		hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z);
5122230130Smav	}
5123230130Smav}
5124230130Smav
5125230130Smavstatic void
5126230130Smavhdaa_gpio_commit(struct hdaa_devinfo *devinfo)
5127230130Smav{
5128230130Smav	uint32_t gdata, gmask, gdir;
5129230130Smav	int i, numgpio;
5130230130Smav
5131230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5132230130Smav	if (devinfo->gpio != 0 && numgpio != 0) {
5133230130Smav		gdata = hda_command(devinfo->dev,
5134230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
5135230130Smav		gmask = hda_command(devinfo->dev,
5136230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
5137230130Smav		gdir = hda_command(devinfo->dev,
5138230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
5139230130Smav		for (i = 0; i < numgpio; i++) {
5140230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5141230130Smav			    HDAA_GPIO_SET(i)) {
5142230130Smav				gdata |= (1 << i);
5143230130Smav				gmask |= (1 << i);
5144230130Smav				gdir |= (1 << i);
5145230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5146230130Smav			    HDAA_GPIO_CLEAR(i)) {
5147230130Smav				gdata &= ~(1 << i);
5148230130Smav				gmask |= (1 << i);
5149230130Smav				gdir |= (1 << i);
5150230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5151230130Smav			    HDAA_GPIO_DISABLE(i)) {
5152230130Smav				gmask &= ~(1 << i);
5153230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5154230130Smav			    HDAA_GPIO_INPUT(i)) {
5155230130Smav				gmask |= (1 << i);
5156230130Smav				gdir &= ~(1 << i);
5157230130Smav			}
5158230130Smav		}
5159230130Smav		HDA_BOOTVERBOSE(
5160230130Smav			device_printf(devinfo->dev, "GPIO commit\n");
5161230130Smav		);
5162230130Smav		hda_command(devinfo->dev,
5163230130Smav		    HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask));
5164230130Smav		hda_command(devinfo->dev,
5165230130Smav		    HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir));
5166230130Smav		hda_command(devinfo->dev,
5167230130Smav		    HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata));
5168230130Smav		HDA_BOOTVERBOSE(
5169230130Smav			hdaa_dump_gpio(devinfo);
5170230130Smav		);
5171230130Smav	}
5172230130Smav}
5173230130Smav
5174230130Smavstatic void
5175230130Smavhdaa_gpo_commit(struct hdaa_devinfo *devinfo)
5176230130Smav{
5177230130Smav	uint32_t gdata;
5178230130Smav	int i, numgpo;
5179230130Smav
5180230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
5181230130Smav	if (devinfo->gpo != 0 && numgpo != 0) {
5182230130Smav		gdata = hda_command(devinfo->dev,
5183230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
5184230130Smav		for (i = 0; i < numgpo; i++) {
5185230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5186230130Smav			    HDAA_GPIO_SET(i)) {
5187230130Smav				gdata |= (1 << i);
5188230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5189230130Smav			    HDAA_GPIO_CLEAR(i)) {
5190230130Smav				gdata &= ~(1 << i);
5191230130Smav			}
5192230130Smav		}
5193230130Smav		HDA_BOOTVERBOSE(
5194230130Smav			device_printf(devinfo->dev, "GPO commit\n");
5195230130Smav		);
5196230130Smav		hda_command(devinfo->dev,
5197230130Smav		    HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata));
5198230130Smav		HDA_BOOTVERBOSE(
5199230130Smav			hdaa_dump_gpo(devinfo);
5200230130Smav		);
5201230130Smav	}
5202230130Smav}
5203230130Smav
5204230130Smavstatic void
5205230130Smavhdaa_audio_commit(struct hdaa_devinfo *devinfo)
5206230130Smav{
5207230130Smav	struct hdaa_widget *w;
5208230130Smav	int i;
5209230130Smav
5210230130Smav	/* Commit controls. */
5211230130Smav	hdaa_audio_ctl_commit(devinfo);
5212230130Smav
5213230130Smav	/* Commit selectors, pins and EAPD. */
5214230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
5215230130Smav		w = &devinfo->widget[i];
5216230130Smav		if (w == NULL)
5217230130Smav			continue;
5218230130Smav		if (w->selconn == -1)
5219230130Smav			w->selconn = 0;
5220230130Smav		if (w->nconns > 0)
5221230130Smav			hdaa_widget_connection_select(w, w->selconn);
5222230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
5223230130Smav		    w->waspin) {
5224230130Smav			hda_command(devinfo->dev,
5225230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid,
5226230130Smav			    w->wclass.pin.ctrl));
5227230130Smav		}
5228230130Smav		if (w->param.eapdbtl != HDA_INVALID) {
5229230130Smav			uint32_t val;
5230230130Smav
5231230130Smav			val = w->param.eapdbtl;
5232230130Smav			if (devinfo->quirks &
5233230130Smav			    HDAA_QUIRK_EAPDINV)
5234230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
5235230130Smav			hda_command(devinfo->dev,
5236230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid,
5237230130Smav			    val));
5238230130Smav		}
5239230130Smav	}
5240230130Smav
5241230130Smav	hdaa_gpio_commit(devinfo);
5242230130Smav	hdaa_gpo_commit(devinfo);
5243230130Smav}
5244230130Smav
5245230130Smavstatic void
5246230130Smavhdaa_powerup(struct hdaa_devinfo *devinfo)
5247230130Smav{
5248230130Smav	int i;
5249230130Smav
5250230130Smav	hda_command(devinfo->dev,
5251230130Smav	    HDA_CMD_SET_POWER_STATE(0,
5252230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D0));
5253230130Smav	DELAY(100);
5254230130Smav
5255230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5256230130Smav		hda_command(devinfo->dev,
5257230130Smav		    HDA_CMD_SET_POWER_STATE(0,
5258230130Smav		    i, HDA_CMD_POWER_STATE_D0));
5259230130Smav	}
5260230130Smav	DELAY(1000);
5261230130Smav}
5262230130Smav
5263230130Smavstatic int
5264230130Smavhdaa_pcmchannel_setup(struct hdaa_chan *ch)
5265230130Smav{
5266230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
5267230130Smav	struct hdaa_audio_as *as = devinfo->as;
5268230130Smav	struct hdaa_widget *w;
5269230130Smav	uint32_t cap, fmtcap, pcmcap;
5270230130Smav	int i, j, ret, channels, onlystereo;
5271230130Smav	uint16_t pinset;
5272230130Smav
5273230130Smav	ch->caps = hdaa_caps;
5274230130Smav	ch->caps.fmtlist = ch->fmtlist;
5275230130Smav	ch->bit16 = 1;
5276230130Smav	ch->bit32 = 0;
5277230130Smav	ch->pcmrates[0] = 48000;
5278230130Smav	ch->pcmrates[1] = 0;
5279230326Smav	ch->stripecap = 0xff;
5280230130Smav
5281230130Smav	ret = 0;
5282230130Smav	channels = 0;
5283230130Smav	onlystereo = 1;
5284230130Smav	pinset = 0;
5285230130Smav	fmtcap = devinfo->supp_stream_formats;
5286230130Smav	pcmcap = devinfo->supp_pcm_size_rate;
5287230130Smav
5288230130Smav	for (i = 0; i < 16; i++) {
5289230130Smav		/* Check as is correct */
5290230130Smav		if (ch->as < 0)
5291230130Smav			break;
5292230130Smav		/* Cound only present DACs */
5293230130Smav		if (as[ch->as].dacs[ch->asindex][i] <= 0)
5294230130Smav			continue;
5295230130Smav		/* Ignore duplicates */
5296230130Smav		for (j = 0; j < ret; j++) {
5297230130Smav			if (ch->io[j] == as[ch->as].dacs[ch->asindex][i])
5298230130Smav				break;
5299230130Smav		}
5300230130Smav		if (j < ret)
5301230130Smav			continue;
5302230130Smav
5303230130Smav		w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]);
5304230130Smav		if (w == NULL || w->enable == 0)
5305230130Smav			continue;
5306230130Smav		cap = w->param.supp_stream_formats;
5307230130Smav		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
5308230130Smav		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5309230130Smav			continue;
5310230130Smav		/* Many CODECs does not declare AC3 support on SPDIF.
5311230130Smav		   I don't beleave that they doesn't support it! */
5312230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5313230130Smav			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
5314230130Smav		if (ret == 0) {
5315230130Smav			fmtcap = cap;
5316230130Smav			pcmcap = w->param.supp_pcm_size_rate;
5317230130Smav		} else {
5318230130Smav			fmtcap &= cap;
5319230130Smav			pcmcap &= w->param.supp_pcm_size_rate;
5320230130Smav		}
5321230130Smav		ch->io[ret++] = as[ch->as].dacs[ch->asindex][i];
5322230326Smav		ch->stripecap &= w->wclass.conv.stripecap;
5323230130Smav		/* Do not count redirection pin/dac channels. */
5324230130Smav		if (i == 15 && as[ch->as].hpredir >= 0)
5325230130Smav			continue;
5326230130Smav		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
5327230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
5328230130Smav			onlystereo = 0;
5329230130Smav		pinset |= (1 << i);
5330230130Smav	}
5331230130Smav	ch->io[ret] = -1;
5332230130Smav	ch->channels = channels;
5333230130Smav
5334230130Smav	if (as[ch->as].fakeredir)
5335230130Smav		ret--;
5336230130Smav	/* Standard speaks only about stereo pins and playback, ... */
5337230130Smav	if ((!onlystereo) || as[ch->as].mixed)
5338230130Smav		pinset = 0;
5339230130Smav	/* ..., but there it gives us info about speakers layout. */
5340230130Smav	as[ch->as].pinset = pinset;
5341230130Smav
5342230130Smav	ch->supp_stream_formats = fmtcap;
5343230130Smav	ch->supp_pcm_size_rate = pcmcap;
5344230130Smav
5345230130Smav	/*
5346230130Smav	 *  8bit = 0
5347230130Smav	 * 16bit = 1
5348230130Smav	 * 20bit = 2
5349230130Smav	 * 24bit = 3
5350230130Smav	 * 32bit = 4
5351230130Smav	 */
5352230130Smav	if (ret > 0) {
5353230130Smav		i = 0;
5354230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
5355230130Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
5356230130Smav				ch->bit16 = 1;
5357230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
5358230130Smav				ch->bit16 = 0;
5359230488Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
5360230130Smav				ch->bit32 = 3;
5361230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
5362230130Smav				ch->bit32 = 2;
5363230488Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
5364230488Smav				ch->bit32 = 4;
5365230130Smav			if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) {
5366230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
5367230130Smav				if (ch->bit32)
5368230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
5369230130Smav			}
5370230130Smav			if (channels >= 2) {
5371230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
5372230130Smav				if (ch->bit32)
5373230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
5374230130Smav			}
5375230312Smav			if (channels >= 3 && !onlystereo) {
5376230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0);
5377230312Smav				if (ch->bit32)
5378230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0);
5379230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1);
5380230312Smav				if (ch->bit32)
5381230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1);
5382230312Smav			}
5383230312Smav			if (channels >= 4) {
5384230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
5385230130Smav				if (ch->bit32)
5386230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
5387230312Smav				if (!onlystereo) {
5388230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1);
5389230312Smav					if (ch->bit32)
5390230312Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1);
5391230312Smav				}
5392230130Smav			}
5393230312Smav			if (channels >= 5 && !onlystereo) {
5394230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0);
5395230312Smav				if (ch->bit32)
5396230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0);
5397230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1);
5398230312Smav				if (ch->bit32)
5399230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1);
5400230312Smav			}
5401230312Smav			if (channels >= 6) {
5402230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
5403230130Smav				if (ch->bit32)
5404230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
5405230312Smav				if (!onlystereo) {
5406230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0);
5407230312Smav					if (ch->bit32)
5408230312Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0);
5409230312Smav				}
5410230130Smav			}
5411230312Smav			if (channels >= 7 && !onlystereo) {
5412230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0);
5413230312Smav				if (ch->bit32)
5414230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0);
5415230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1);
5416230312Smav				if (ch->bit32)
5417230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1);
5418230312Smav			}
5419230312Smav			if (channels >= 8) {
5420230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
5421230130Smav				if (ch->bit32)
5422230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
5423230130Smav			}
5424230130Smav		}
5425230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
5426230130Smav			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
5427230513Smav			if (channels >= 8) {
5428230513Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 0);
5429230513Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 1);
5430230513Smav			}
5431230130Smav		}
5432230130Smav		ch->fmtlist[i] = 0;
5433230130Smav		i = 0;
5434230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
5435230130Smav			ch->pcmrates[i++] = 8000;
5436230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
5437230130Smav			ch->pcmrates[i++] = 11025;
5438230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
5439230130Smav			ch->pcmrates[i++] = 16000;
5440230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
5441230130Smav			ch->pcmrates[i++] = 22050;
5442230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
5443230130Smav			ch->pcmrates[i++] = 32000;
5444230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
5445230130Smav			ch->pcmrates[i++] = 44100;
5446230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
5447230130Smav		ch->pcmrates[i++] = 48000;
5448230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
5449230130Smav			ch->pcmrates[i++] = 88200;
5450230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
5451230130Smav			ch->pcmrates[i++] = 96000;
5452230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
5453230130Smav			ch->pcmrates[i++] = 176400;
5454230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
5455230130Smav			ch->pcmrates[i++] = 192000;
5456230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
5457230130Smav		ch->pcmrates[i] = 0;
5458230130Smav		if (i > 0) {
5459230130Smav			ch->caps.minspeed = ch->pcmrates[0];
5460230130Smav			ch->caps.maxspeed = ch->pcmrates[i - 1];
5461230130Smav		}
5462230130Smav	}
5463230130Smav
5464230130Smav	return (ret);
5465230130Smav}
5466230130Smav
5467230130Smavstatic void
5468230451Smavhdaa_prepare_pcms(struct hdaa_devinfo *devinfo)
5469230130Smav{
5470230130Smav	struct hdaa_audio_as *as = devinfo->as;
5471230130Smav	int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
5472230130Smav
5473230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5474230130Smav		if (as[i].enable == 0)
5475230130Smav			continue;
5476230130Smav		if (as[i].dir == HDAA_CTL_IN) {
5477230130Smav			if (as[i].digital)
5478230130Smav				drdev++;
5479230130Smav			else
5480230130Smav				ardev++;
5481230130Smav		} else {
5482230130Smav			if (as[i].digital)
5483230130Smav				dpdev++;
5484230130Smav			else
5485230130Smav				apdev++;
5486230130Smav		}
5487230130Smav	}
5488230130Smav	devinfo->num_devs =
5489230130Smav	    max(ardev, apdev) + max(drdev, dpdev);
5490230130Smav	devinfo->devs =
5491230130Smav	    (struct hdaa_pcm_devinfo *)malloc(
5492230130Smav	    devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo),
5493230130Smav	    M_HDAA, M_ZERO | M_NOWAIT);
5494230130Smav	if (devinfo->devs == NULL) {
5495230130Smav		device_printf(devinfo->dev,
5496230130Smav		    "Unable to allocate memory for devices\n");
5497230130Smav		return;
5498230130Smav	}
5499230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5500230130Smav		devinfo->devs[i].index = i;
5501230130Smav		devinfo->devs[i].devinfo = devinfo;
5502230130Smav		devinfo->devs[i].playas = -1;
5503230130Smav		devinfo->devs[i].recas = -1;
5504230130Smav		devinfo->devs[i].digital = 255;
5505230130Smav	}
5506230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5507230130Smav		if (as[i].enable == 0)
5508230130Smav			continue;
5509230130Smav		for (j = 0; j < devinfo->num_devs; j++) {
5510230130Smav			if (devinfo->devs[j].digital != 255 &&
5511230130Smav			    (!devinfo->devs[j].digital) !=
5512230130Smav			    (!as[i].digital))
5513230130Smav				continue;
5514230130Smav			if (as[i].dir == HDAA_CTL_IN) {
5515230130Smav				if (devinfo->devs[j].recas >= 0)
5516230130Smav					continue;
5517230130Smav				devinfo->devs[j].recas = i;
5518230130Smav			} else {
5519230130Smav				if (devinfo->devs[j].playas >= 0)
5520230130Smav					continue;
5521230130Smav				devinfo->devs[j].playas = i;
5522230130Smav			}
5523230451Smav			as[i].pdevinfo = &devinfo->devs[j];
5524230130Smav			for (k = 0; k < as[i].num_chans; k++) {
5525230130Smav				devinfo->chans[as[i].chans[k]].pdevinfo =
5526230130Smav				    &devinfo->devs[j];
5527230130Smav			}
5528230130Smav			devinfo->devs[j].digital = as[i].digital;
5529230130Smav			break;
5530230130Smav		}
5531230130Smav	}
5532230451Smav}
5533230451Smav
5534230451Smavstatic void
5535230451Smavhdaa_create_pcms(struct hdaa_devinfo *devinfo)
5536230451Smav{
5537230451Smav	int i;
5538230451Smav
5539230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5540230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
5541230130Smav
5542230130Smav		pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1);
5543230130Smav		device_set_ivars(pdevinfo->dev, (void *)pdevinfo);
5544230130Smav	}
5545230130Smav}
5546230130Smav
5547230130Smavstatic void
5548230130Smavhdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
5549230130Smav{
5550230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5551230130Smav	struct hdaa_audio_ctl *ctl;
5552230130Smav	char buf[64];
5553243535Smav	int i, j, printed = 0;
5554230130Smav
5555230130Smav	if (flag == 0) {
5556230130Smav		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5557230130Smav		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5558230130Smav		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
5559230130Smav		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
5560230130Smav	}
5561230130Smav
5562230130Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
5563230130Smav		if ((flag & (1 << j)) == 0)
5564230130Smav			continue;
5565230130Smav		i = 0;
5566230130Smav		printed = 0;
5567230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5568230130Smav			if (ctl->enable == 0 ||
5569230130Smav			    ctl->widget->enable == 0)
5570230130Smav				continue;
5571230130Smav			if (!((pdevinfo->playas >= 0 &&
5572230130Smav			    ctl->widget->bindas == pdevinfo->playas) ||
5573230130Smav			    (pdevinfo->recas >= 0 &&
5574230130Smav			    ctl->widget->bindas == pdevinfo->recas) ||
5575230130Smav			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
5576230130Smav				continue;
5577230130Smav			if ((ctl->ossmask & (1 << j)) == 0)
5578230130Smav				continue;
5579230130Smav
5580230130Smav			if (printed == 0) {
5581230130Smav				if (banner != NULL) {
5582230130Smav					device_printf(pdevinfo->dev, "%s", banner);
5583230130Smav				} else {
5584230130Smav					device_printf(pdevinfo->dev, "Unknown Ctl");
5585230130Smav				}
5586230451Smav				printf(" (OSS: %s)",
5587230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(1 << j,
5588230130Smav				    buf, sizeof(buf)));
5589230451Smav				if (pdevinfo->ossmask & (1 << j)) {
5590230451Smav					printf(": %+d/%+ddB\n",
5591230451Smav					    pdevinfo->minamp[j] / 4,
5592230451Smav					    pdevinfo->maxamp[j] / 4);
5593230451Smav				} else
5594230451Smav					printf("\n");
5595230130Smav				printed = 1;
5596230130Smav			}
5597230130Smav			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
5598230130Smav				ctl->widget->nid,
5599230130Smav				(ctl->ndir == HDAA_CTL_IN)?"in ":"out");
5600230130Smav			if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir)
5601230130Smav				printf(" %2d): ", ctl->index);
5602230130Smav			else
5603230130Smav				printf("):    ");
5604230130Smav			if (ctl->step > 0) {
5605230130Smav				printf("%+d/%+ddB (%d steps)%s\n",
5606230451Smav				    MINQDB(ctl) / 4,
5607230451Smav				    MAXQDB(ctl) / 4,
5608230130Smav				    ctl->step + 1,
5609230130Smav				    ctl->mute?" + mute":"");
5610230130Smav			} else
5611230130Smav				printf("%s\n", ctl->mute?"mute":"");
5612230130Smav		}
5613230130Smav	}
5614243530Smav	if (printed)
5615243530Smav		device_printf(pdevinfo->dev, "\n");
5616230130Smav}
5617230130Smav
5618230130Smavstatic void
5619230130Smavhdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
5620230130Smav{
5621230130Smav	uint32_t cap;
5622230130Smav
5623230130Smav	cap = fcap;
5624230130Smav	if (cap != 0) {
5625243530Smav		device_printf(dev, "     Stream cap: 0x%08x", cap);
5626230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5627230130Smav			printf(" AC3");
5628230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5629230130Smav			printf(" FLOAT32");
5630230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5631230130Smav			printf(" PCM");
5632230130Smav		printf("\n");
5633230130Smav	}
5634230130Smav	cap = pcmcap;
5635230130Smav	if (cap != 0) {
5636243530Smav		device_printf(dev, "        PCM cap: 0x%08x", cap);
5637230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5638230130Smav			printf(" 8");
5639230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5640230130Smav			printf(" 16");
5641230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5642230130Smav			printf(" 20");
5643230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5644230130Smav			printf(" 24");
5645230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5646230130Smav			printf(" 32");
5647230130Smav		printf(" bits,");
5648230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5649230130Smav			printf(" 8");
5650230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5651230130Smav			printf(" 11");
5652230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5653230130Smav			printf(" 16");
5654230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5655230130Smav			printf(" 22");
5656230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5657230130Smav			printf(" 32");
5658230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5659230130Smav			printf(" 44");
5660230130Smav		printf(" 48");
5661230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5662230130Smav			printf(" 88");
5663230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5664230130Smav			printf(" 96");
5665230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5666230130Smav			printf(" 176");
5667230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5668230130Smav			printf(" 192");
5669230130Smav		printf(" KHz\n");
5670230130Smav	}
5671230130Smav}
5672230130Smav
5673230130Smavstatic void
5674230130Smavhdaa_dump_pin(struct hdaa_widget *w)
5675230130Smav{
5676230130Smav	uint32_t pincap;
5677230130Smav
5678230130Smav	pincap = w->wclass.pin.cap;
5679230130Smav
5680243530Smav	device_printf(w->devinfo->dev, "        Pin cap: 0x%08x", pincap);
5681230130Smav	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5682230130Smav		printf(" ISC");
5683230130Smav	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5684230130Smav		printf(" TRQD");
5685230130Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5686230130Smav		printf(" PDC");
5687230130Smav	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5688230130Smav		printf(" HP");
5689230130Smav	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5690230130Smav		printf(" OUT");
5691230130Smav	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5692230130Smav		printf(" IN");
5693230130Smav	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5694230130Smav		printf(" BAL");
5695230130Smav	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
5696230130Smav		printf(" HDMI");
5697230130Smav	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5698230130Smav		printf(" VREF[");
5699230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5700230130Smav			printf(" 50");
5701230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5702230130Smav			printf(" 80");
5703230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5704230130Smav			printf(" 100");
5705230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5706230130Smav			printf(" GROUND");
5707230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5708230130Smav			printf(" HIZ");
5709230130Smav		printf(" ]");
5710230130Smav	}
5711230130Smav	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5712230130Smav		printf(" EAPD");
5713230130Smav	if (HDA_PARAM_PIN_CAP_DP(pincap))
5714230130Smav		printf(" DP");
5715230130Smav	if (HDA_PARAM_PIN_CAP_HBR(pincap))
5716230130Smav		printf(" HBR");
5717230130Smav	printf("\n");
5718230130Smav	device_printf(w->devinfo->dev, "     Pin config: 0x%08x\n",
5719230130Smav	    w->wclass.pin.config);
5720230130Smav	device_printf(w->devinfo->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
5721230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5722230130Smav		printf(" HP");
5723230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5724230130Smav		printf(" IN");
5725230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5726230130Smav		printf(" OUT");
5727230312Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
5728230312Smav		if ((w->wclass.pin.ctrl &
5729230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
5730230312Smav			printf(" HBR");
5731230312Smav		else if ((w->wclass.pin.ctrl &
5732230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5733230312Smav			printf(" EPTs");
5734230312Smav	} else {
5735230312Smav		if ((w->wclass.pin.ctrl &
5736230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5737230312Smav			printf(" VREFs");
5738230312Smav	}
5739230130Smav	printf("\n");
5740230130Smav}
5741230130Smav
5742230130Smavstatic void
5743230130Smavhdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf)
5744230130Smav{
5745230130Smav
5746230130Smav	device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d "
5747230130Smav	    "%-13s %-5s %-7s %-10s %-7s %d%s\n",
5748230130Smav	    w->nid, conf,
5749230130Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
5750230130Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
5751230130Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
5752230130Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
5753230130Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
5754230130Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
5755230130Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
5756230130Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
5757230130Smav	    (w->enable == 0)?" DISA":"");
5758230130Smav}
5759230130Smav
5760230130Smavstatic void
5761230130Smavhdaa_dump_pin_configs(struct hdaa_devinfo *devinfo)
5762230130Smav{
5763230130Smav	struct hdaa_widget *w;
5764230130Smav	int i;
5765230130Smav
5766230130Smav	device_printf(devinfo->dev, "nid   0x    as seq "
5767230130Smav	    "device       conn  jack    loc        color   misc\n");
5768230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5769230130Smav		w = hdaa_widget_get(devinfo, i);
5770230130Smav		if (w == NULL)
5771230130Smav			continue;
5772230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5773230130Smav			continue;
5774230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
5775230130Smav	}
5776230130Smav}
5777230130Smav
5778230130Smavstatic void
5779264961Smariushdaa_dump_amp(device_t dev, uint32_t cap, const char *banner)
5780230130Smav{
5781243530Smav	int offset, size, step;
5782243530Smav
5783243530Smav	offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap);
5784243530Smav	size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap);
5785243530Smav	step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap);
5786243530Smav	device_printf(dev, "     %s amp: 0x%08x "
5787243530Smav	    "mute=%d step=%d size=%d offset=%d (%+d/%+ddB)\n",
5788243530Smav	    banner, cap,
5789230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5790243530Smav	    step, size, offset,
5791243530Smav	    ((0 - offset) * (size + 1)) / 4,
5792243530Smav	    ((step - offset) * (size + 1)) / 4);
5793230130Smav}
5794230130Smav
5795230130Smavstatic void
5796230130Smavhdaa_dump_nodes(struct hdaa_devinfo *devinfo)
5797230130Smav{
5798230130Smav	struct hdaa_widget *w, *cw;
5799230130Smav	char buf[64];
5800230130Smav	int i, j;
5801230130Smav
5802230130Smav	device_printf(devinfo->dev, "\n");
5803243530Smav	device_printf(devinfo->dev, "Default parameters:\n");
5804230130Smav	hdaa_dump_audio_formats(devinfo->dev,
5805230130Smav	    devinfo->supp_stream_formats,
5806230130Smav	    devinfo->supp_pcm_size_rate);
5807243530Smav	hdaa_dump_amp(devinfo->dev, devinfo->inamp_cap, " Input");
5808243530Smav	hdaa_dump_amp(devinfo->dev, devinfo->outamp_cap, "Output");
5809230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5810230130Smav		w = hdaa_widget_get(devinfo, i);
5811230130Smav		if (w == NULL) {
5812230130Smav			device_printf(devinfo->dev, "Ghost widget nid=%d\n", i);
5813230130Smav			continue;
5814230130Smav		}
5815230130Smav		device_printf(devinfo->dev, "\n");
5816230130Smav		device_printf(devinfo->dev, "            nid: %d%s\n", w->nid,
5817230130Smav		    (w->enable == 0) ? " [DISABLED]" : "");
5818230130Smav		device_printf(devinfo->dev, "           Name: %s\n", w->name);
5819243530Smav		device_printf(devinfo->dev, "     Widget cap: 0x%08x",
5820230130Smav		    w->param.widget_cap);
5821230130Smav		if (w->param.widget_cap & 0x0ee1) {
5822230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
5823230130Smav			    printf(" LRSWAP");
5824230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
5825230130Smav			    printf(" PWR");
5826230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5827230130Smav			    printf(" DIGITAL");
5828230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
5829230130Smav			    printf(" UNSOL");
5830230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
5831230130Smav			    printf(" PROC");
5832230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
5833230326Smav			    printf(" STRIPE(x%d)",
5834230326Smav				1 << (fls(w->wclass.conv.stripecap) - 1));
5835230130Smav			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
5836230130Smav			if (j == 1)
5837230130Smav			    printf(" STEREO");
5838230130Smav			else if (j > 1)
5839230130Smav			    printf(" %dCH", j + 1);
5840230130Smav		}
5841243530Smav		printf("\n");
5842230130Smav		if (w->bindas != -1) {
5843243530Smav			device_printf(devinfo->dev, "    Association: %d (0x%04x)\n",
5844230130Smav			    w->bindas, w->bindseqmask);
5845230130Smav		}
5846230130Smav		if (w->ossmask != 0 || w->ossdev >= 0) {
5847230130Smav			device_printf(devinfo->dev, "            OSS: %s",
5848230130Smav			    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
5849230130Smav			if (w->ossdev >= 0)
5850230451Smav			    printf(" (%s)", ossnames[w->ossdev]);
5851230130Smav			printf("\n");
5852230130Smav		}
5853230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5854230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5855230130Smav			hdaa_dump_audio_formats(devinfo->dev,
5856230130Smav			    w->param.supp_stream_formats,
5857230130Smav			    w->param.supp_pcm_size_rate);
5858230130Smav		} else if (w->type ==
5859230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
5860230130Smav			hdaa_dump_pin(w);
5861230130Smav		if (w->param.eapdbtl != HDA_INVALID)
5862230130Smav			device_printf(devinfo->dev, "           EAPD: 0x%08x\n",
5863230130Smav			    w->param.eapdbtl);
5864230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5865230130Smav		    w->param.outamp_cap != 0)
5866230130Smav			hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output");
5867230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5868230130Smav		    w->param.inamp_cap != 0)
5869230130Smav			hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input");
5870243530Smav		if (w->nconns > 0)
5871243530Smav			device_printf(devinfo->dev, "    Connections: %d\n", w->nconns);
5872230130Smav		for (j = 0; j < w->nconns; j++) {
5873230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
5874230130Smav			device_printf(devinfo->dev, "          + %s<- nid=%d [%s]",
5875230130Smav			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
5876230130Smav			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5877230130Smav			if (cw == NULL)
5878230130Smav				printf(" [UNKNOWN]");
5879230130Smav			else if (cw->enable == 0)
5880230130Smav				printf(" [DISABLED]");
5881230130Smav			if (w->nconns > 1 && w->selconn == j && w->type !=
5882230130Smav			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5883230130Smav				printf(" (selected)");
5884230130Smav			printf("\n");
5885230130Smav		}
5886230130Smav	}
5887230130Smav
5888230130Smav}
5889230130Smav
5890230130Smavstatic void
5891230130Smavhdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth)
5892230130Smav{
5893230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5894230130Smav	struct hdaa_widget *w, *cw;
5895230130Smav	char buf[64];
5896243530Smav	int i;
5897230130Smav
5898230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
5899230130Smav		return;
5900230130Smav
5901230130Smav	w = hdaa_widget_get(devinfo, nid);
5902230130Smav	if (w == NULL || w->enable == 0)
5903230130Smav		return;
5904230130Smav
5905230130Smav	if (depth == 0)
5906230130Smav		device_printf(pdevinfo->dev, "%*s", 4, "");
5907230130Smav	else
5908230130Smav		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
5909230130Smav	printf("nid=%d [%s]", w->nid, w->name);
5910230130Smav
5911230130Smav	if (depth > 0) {
5912230130Smav		if (w->ossmask == 0) {
5913230130Smav			printf("\n");
5914230130Smav			return;
5915230130Smav		}
5916230130Smav		printf(" [src: %s]",
5917230130Smav		    hdaa_audio_ctl_ossmixer_mask2allname(
5918230130Smav			w->ossmask, buf, sizeof(buf)));
5919230130Smav		if (w->ossdev >= 0) {
5920230130Smav			printf("\n");
5921230130Smav			return;
5922230130Smav		}
5923230130Smav	}
5924230130Smav	printf("\n");
5925230130Smav
5926230130Smav	for (i = 0; i < w->nconns; i++) {
5927230130Smav		if (w->connsenable[i] == 0)
5928230130Smav			continue;
5929230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
5930230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
5931230130Smav			continue;
5932230130Smav		hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
5933230130Smav	}
5934230130Smav
5935230130Smav}
5936230130Smav
5937230130Smavstatic void
5938230130Smavhdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo)
5939230130Smav{
5940230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5941230130Smav	struct hdaa_audio_as *as;
5942230130Smav	struct hdaa_widget *w;
5943243530Smav	nid_t *nids;
5944243530Smav	int chid, i;
5945230130Smav
5946230130Smav	if (pdevinfo->playas < 0)
5947230130Smav		return;
5948230130Smav
5949243530Smav	device_printf(pdevinfo->dev, "Playback:\n");
5950243530Smav
5951243530Smav	chid = devinfo->as[pdevinfo->playas].chans[0];
5952243530Smav	hdaa_dump_audio_formats(pdevinfo->dev,
5953243530Smav	    devinfo->chans[chid].supp_stream_formats,
5954243530Smav	    devinfo->chans[chid].supp_pcm_size_rate);
5955243530Smav	for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) {
5956243530Smav		chid = devinfo->as[pdevinfo->playas].chans[i];
5957243530Smav		device_printf(pdevinfo->dev, "            DAC:");
5958243530Smav		for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5959243530Smav			printf(" %d", *nids);
5960243530Smav		printf("\n");
5961243530Smav	}
5962243530Smav
5963230130Smav	as = &devinfo->as[pdevinfo->playas];
5964230130Smav	for (i = 0; i < 16; i++) {
5965230130Smav		if (as->pins[i] <= 0)
5966230130Smav			continue;
5967230130Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
5968230130Smav		if (w == NULL || w->enable == 0)
5969230130Smav			continue;
5970230130Smav		device_printf(pdevinfo->dev, "\n");
5971230130Smav		hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0);
5972230130Smav	}
5973243530Smav	device_printf(pdevinfo->dev, "\n");
5974230130Smav}
5975230130Smav
5976230130Smavstatic void
5977230130Smavhdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo)
5978230130Smav{
5979230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5980230130Smav	struct hdaa_widget *w;
5981243530Smav	nid_t *nids;
5982243530Smav	int chid, i;
5983230130Smav
5984230130Smav	if (pdevinfo->recas < 0)
5985230130Smav		return;
5986230130Smav
5987243530Smav	device_printf(pdevinfo->dev, "Record:\n");
5988243530Smav
5989243530Smav	chid = devinfo->as[pdevinfo->recas].chans[0];
5990243530Smav	hdaa_dump_audio_formats(pdevinfo->dev,
5991243530Smav	    devinfo->chans[chid].supp_stream_formats,
5992243530Smav	    devinfo->chans[chid].supp_pcm_size_rate);
5993243530Smav	for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) {
5994243530Smav		chid = devinfo->as[pdevinfo->recas].chans[i];
5995243530Smav		device_printf(pdevinfo->dev, "            ADC:");
5996243530Smav		for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5997243530Smav			printf(" %d", *nids);
5998243530Smav		printf("\n");
5999243530Smav	}
6000243530Smav
6001230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6002230130Smav		w = hdaa_widget_get(devinfo, i);
6003230130Smav		if (w == NULL || w->enable == 0)
6004230130Smav			continue;
6005230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
6006230130Smav			continue;
6007230130Smav		if (w->bindas != pdevinfo->recas)
6008230130Smav			continue;
6009230130Smav		device_printf(pdevinfo->dev, "\n");
6010230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
6011230130Smav	}
6012243530Smav	device_printf(pdevinfo->dev, "\n");
6013230130Smav}
6014230130Smav
6015230130Smavstatic void
6016230130Smavhdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo)
6017230130Smav{
6018230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6019230130Smav	struct hdaa_widget *w;
6020230130Smav	int i;
6021230130Smav	int printed = 0;
6022230130Smav
6023230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6024230130Smav		w = hdaa_widget_get(devinfo, i);
6025230130Smav		if (w == NULL || w->enable == 0)
6026230130Smav			continue;
6027230130Smav		if (w->ossdev != SOUND_MIXER_IMIX)
6028230130Smav			continue;
6029230451Smav		if (w->bindas != pdevinfo->recas)
6030230451Smav			continue;
6031230130Smav		if (printed == 0) {
6032230130Smav			printed = 1;
6033230130Smav			device_printf(pdevinfo->dev, "Input Mix:\n");
6034230130Smav		}
6035230130Smav		device_printf(pdevinfo->dev, "\n");
6036230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
6037230130Smav	}
6038243530Smav	if (printed)
6039230130Smav		device_printf(pdevinfo->dev, "\n");
6040230130Smav}
6041230130Smav
6042230130Smavstatic void
6043230130Smavhdaa_pindump(device_t dev)
6044230130Smav{
6045230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6046230130Smav	struct hdaa_widget *w;
6047230130Smav	uint32_t res, pincap, delay;
6048230130Smav	int i;
6049230130Smav
6050230130Smav	device_printf(dev, "Dumping AFG pins:\n");
6051230130Smav	device_printf(dev, "nid   0x    as seq "
6052230130Smav	    "device       conn  jack    loc        color   misc\n");
6053230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6054230130Smav		w = hdaa_widget_get(devinfo, i);
6055230130Smav		if (w == NULL || w->type !=
6056230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6057230130Smav			continue;
6058230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
6059230130Smav		pincap = w->wclass.pin.cap;
6060230130Smav		device_printf(dev, "    Caps: %2s %3s %2s %4s %4s",
6061230130Smav		    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
6062230130Smav		    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
6063230130Smav		    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
6064230130Smav		    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
6065230130Smav		    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
6066230130Smav		if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
6067230130Smav		    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
6068230130Smav			if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
6069230130Smav				delay = 0;
6070230130Smav				hda_command(dev,
6071230130Smav				    HDA_CMD_SET_PIN_SENSE(0, w->nid, 0));
6072230130Smav				do {
6073230130Smav					res = hda_command(dev,
6074230130Smav					    HDA_CMD_GET_PIN_SENSE(0, w->nid));
6075230130Smav					if (res != 0x7fffffff && res != 0xffffffff)
6076230130Smav						break;
6077230130Smav					DELAY(10);
6078230130Smav				} while (++delay < 10000);
6079230130Smav			} else {
6080230130Smav				delay = 0;
6081230130Smav				res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0,
6082230130Smav				    w->nid));
6083230130Smav			}
6084230312Smav			printf(" Sense: 0x%08x (%sconnected%s)", res,
6085230130Smav			    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ?
6086230312Smav			     "" : "dis",
6087230312Smav			    (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) &&
6088230312Smav			     (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ?
6089230312Smav			      ", ELD valid" : "");
6090230130Smav			if (delay > 0)
6091230130Smav				printf(" delay %dus", delay * 10);
6092230130Smav		}
6093230130Smav		printf("\n");
6094230130Smav	}
6095230130Smav	device_printf(dev,
6096230130Smav	    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
6097230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
6098230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
6099230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
6100230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
6101230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
6102230130Smav	hdaa_dump_gpi(devinfo);
6103230130Smav	hdaa_dump_gpio(devinfo);
6104230130Smav	hdaa_dump_gpo(devinfo);
6105230130Smav}
6106230130Smav
6107230130Smavstatic void
6108230130Smavhdaa_configure(device_t dev)
6109230130Smav{
6110230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6111230130Smav	struct hdaa_audio_ctl *ctl;
6112230130Smav	int i;
6113230130Smav
6114230130Smav	HDA_BOOTHVERBOSE(
6115230130Smav		device_printf(dev, "Applying built-in patches...\n");
6116230130Smav	);
6117230130Smav	hdaa_patch(devinfo);
6118230130Smav	HDA_BOOTHVERBOSE(
6119230130Smav		device_printf(dev, "Applying local patches...\n");
6120230130Smav	);
6121230130Smav	hdaa_local_patch(devinfo);
6122230130Smav	hdaa_audio_postprocess(devinfo);
6123230130Smav	HDA_BOOTHVERBOSE(
6124230130Smav		device_printf(dev, "Parsing Ctls...\n");
6125230130Smav	);
6126230130Smav	hdaa_audio_ctl_parse(devinfo);
6127230130Smav	HDA_BOOTHVERBOSE(
6128230130Smav		device_printf(dev, "Disabling nonaudio...\n");
6129230130Smav	);
6130230130Smav	hdaa_audio_disable_nonaudio(devinfo);
6131230130Smav	HDA_BOOTHVERBOSE(
6132230130Smav		device_printf(dev, "Disabling useless...\n");
6133230130Smav	);
6134230130Smav	hdaa_audio_disable_useless(devinfo);
6135230130Smav	HDA_BOOTVERBOSE(
6136230130Smav		device_printf(dev, "Patched pins configuration:\n");
6137230130Smav		hdaa_dump_pin_configs(devinfo);
6138230130Smav	);
6139230130Smav	HDA_BOOTHVERBOSE(
6140230130Smav		device_printf(dev, "Parsing pin associations...\n");
6141230130Smav	);
6142230130Smav	hdaa_audio_as_parse(devinfo);
6143230130Smav	HDA_BOOTHVERBOSE(
6144230130Smav		device_printf(dev, "Building AFG tree...\n");
6145230130Smav	);
6146230130Smav	hdaa_audio_build_tree(devinfo);
6147230130Smav	HDA_BOOTHVERBOSE(
6148230130Smav		device_printf(dev, "Disabling unassociated "
6149230130Smav		    "widgets...\n");
6150230130Smav	);
6151230130Smav	hdaa_audio_disable_unas(devinfo);
6152230130Smav	HDA_BOOTHVERBOSE(
6153230130Smav		device_printf(dev, "Disabling nonselected "
6154230130Smav		    "inputs...\n");
6155230130Smav	);
6156230130Smav	hdaa_audio_disable_notselected(devinfo);
6157230130Smav	HDA_BOOTHVERBOSE(
6158230130Smav		device_printf(dev, "Disabling useless...\n");
6159230130Smav	);
6160230130Smav	hdaa_audio_disable_useless(devinfo);
6161230130Smav	HDA_BOOTHVERBOSE(
6162230130Smav		device_printf(dev, "Disabling "
6163230130Smav		    "crossassociatement connections...\n");
6164230130Smav	);
6165230130Smav	hdaa_audio_disable_crossas(devinfo);
6166230130Smav	HDA_BOOTHVERBOSE(
6167230130Smav		device_printf(dev, "Disabling useless...\n");
6168230130Smav	);
6169230130Smav	hdaa_audio_disable_useless(devinfo);
6170230130Smav	HDA_BOOTHVERBOSE(
6171230130Smav		device_printf(dev, "Binding associations to channels...\n");
6172230130Smav	);
6173230130Smav	hdaa_audio_bind_as(devinfo);
6174230130Smav	HDA_BOOTHVERBOSE(
6175230130Smav		device_printf(dev, "Assigning names to signal sources...\n");
6176230130Smav	);
6177230130Smav	hdaa_audio_assign_names(devinfo);
6178230130Smav	HDA_BOOTHVERBOSE(
6179230451Smav		device_printf(dev, "Preparing PCM devices...\n");
6180230451Smav	);
6181230451Smav	hdaa_prepare_pcms(devinfo);
6182230451Smav	HDA_BOOTHVERBOSE(
6183230130Smav		device_printf(dev, "Assigning mixers to the tree...\n");
6184230130Smav	);
6185230130Smav	hdaa_audio_assign_mixers(devinfo);
6186230130Smav	HDA_BOOTHVERBOSE(
6187230130Smav		device_printf(dev, "Preparing pin controls...\n");
6188230130Smav	);
6189230130Smav	hdaa_audio_prepare_pin_ctrl(devinfo);
6190230130Smav	HDA_BOOTHVERBOSE(
6191230130Smav		device_printf(dev, "AFG commit...\n");
6192230130Smav	);
6193230130Smav	hdaa_audio_commit(devinfo);
6194230130Smav	HDA_BOOTHVERBOSE(
6195230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
6196230130Smav	);
6197230130Smav	hdaa_patch_direct(devinfo);
6198230130Smav	HDA_BOOTHVERBOSE(
6199230551Smav		device_printf(dev, "Pin sense init...\n");
6200230312Smav	);
6201230551Smav	hdaa_sense_init(devinfo);
6202230312Smav	HDA_BOOTHVERBOSE(
6203230130Smav		device_printf(dev, "Creating PCM devices...\n");
6204230130Smav	);
6205230130Smav	hdaa_create_pcms(devinfo);
6206230130Smav
6207230130Smav	HDA_BOOTVERBOSE(
6208230130Smav		if (devinfo->quirks != 0) {
6209230130Smav			device_printf(dev, "FG config/quirks:");
6210264961Smarius			for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
6211230130Smav				if ((devinfo->quirks &
6212230130Smav				    hdaa_quirks_tab[i].value) ==
6213230130Smav				    hdaa_quirks_tab[i].value)
6214230130Smav					printf(" %s", hdaa_quirks_tab[i].key);
6215230130Smav			}
6216230130Smav			printf("\n");
6217230130Smav		}
6218243530Smav	);
6219230130Smav
6220243530Smav	HDA_BOOTHVERBOSE(
6221230130Smav		device_printf(dev, "\n");
6222243530Smav		device_printf(dev, "+-----------+\n");
6223243530Smav		device_printf(dev, "| HDA NODES |\n");
6224243530Smav		device_printf(dev, "+-----------+\n");
6225230130Smav		hdaa_dump_nodes(devinfo);
6226230130Smav
6227230130Smav		device_printf(dev, "\n");
6228243530Smav		device_printf(dev, "+----------------+\n");
6229243530Smav		device_printf(dev, "| HDA AMPLIFIERS |\n");
6230243530Smav		device_printf(dev, "+----------------+\n");
6231230130Smav		device_printf(dev, "\n");
6232230130Smav		i = 0;
6233230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
6234230130Smav			device_printf(dev, "%3d: nid %3d %s (%s) index %d", i,
6235230130Smav			    (ctl->widget != NULL) ? ctl->widget->nid : -1,
6236230130Smav			    (ctl->ndir == HDAA_CTL_IN)?"in ":"out",
6237230130Smav			    (ctl->dir == HDAA_CTL_IN)?"in ":"out",
6238230130Smav			    ctl->index);
6239230130Smav			if (ctl->childwidget != NULL)
6240230130Smav				printf(" cnid %3d", ctl->childwidget->nid);
6241230130Smav			else
6242230130Smav				printf("         ");
6243230130Smav			printf(" ossmask=0x%08x\n",
6244230130Smav			    ctl->ossmask);
6245230130Smav			device_printf(dev,
6246230130Smav			    "       mute: %d step: %3d size: %3d off: %3d%s\n",
6247230130Smav			    ctl->mute, ctl->step, ctl->size, ctl->offset,
6248230130Smav			    (ctl->enable == 0) ? " [DISABLED]" :
6249230130Smav			    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
6250230130Smav		}
6251230130Smav		device_printf(dev, "\n");
6252230130Smav	);
6253230130Smav}
6254230130Smav
6255230130Smavstatic void
6256230130Smavhdaa_unconfigure(device_t dev)
6257230130Smav{
6258230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6259230130Smav	struct hdaa_widget *w;
6260230130Smav	int i, j;
6261230130Smav
6262230130Smav	HDA_BOOTHVERBOSE(
6263230551Smav		device_printf(dev, "Pin sense deinit...\n");
6264230130Smav	);
6265230551Smav	hdaa_sense_deinit(devinfo);
6266230130Smav	free(devinfo->ctl, M_HDAA);
6267230130Smav	devinfo->ctl = NULL;
6268230130Smav	devinfo->ctlcnt = 0;
6269230130Smav	free(devinfo->as, M_HDAA);
6270230130Smav	devinfo->as = NULL;
6271230130Smav	devinfo->ascnt = 0;
6272230130Smav	free(devinfo->devs, M_HDAA);
6273230130Smav	devinfo->devs = NULL;
6274230130Smav	devinfo->num_devs = 0;
6275230130Smav	free(devinfo->chans, M_HDAA);
6276230130Smav	devinfo->chans = NULL;
6277230130Smav	devinfo->num_chans = 0;
6278230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6279230130Smav		w = hdaa_widget_get(devinfo, i);
6280230130Smav		if (w == NULL)
6281230130Smav			continue;
6282230130Smav		w->enable = 1;
6283230130Smav		w->selconn = -1;
6284230130Smav		w->pflags = 0;
6285230130Smav		w->bindas = -1;
6286230130Smav		w->bindseqmask = 0;
6287230130Smav		w->ossdev = -1;
6288230130Smav		w->ossmask = 0;
6289230130Smav		for (j = 0; j < w->nconns; j++)
6290230130Smav			w->connsenable[j] = 1;
6291230451Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6292230451Smav			w->wclass.pin.config = w->wclass.pin.newconf;
6293230312Smav		if (w->eld != NULL) {
6294230312Smav			w->eld_len = 0;
6295230312Smav			free(w->eld, M_HDAA);
6296230312Smav			w->eld = NULL;
6297230312Smav		}
6298230130Smav	}
6299230130Smav}
6300230130Smav
6301230130Smavstatic int
6302230130Smavhdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS)
6303230130Smav{
6304230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6305230130Smav	device_t dev = devinfo->dev;
6306230130Smav	char buf[256];
6307230130Smav	int n = 0, i, numgpi;
6308230130Smav	uint32_t data = 0;
6309230130Smav
6310230130Smav	buf[0] = 0;
6311230130Smav	hdaa_lock(devinfo);
6312230130Smav	numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap);
6313230130Smav	if (numgpi > 0) {
6314230130Smav		data = hda_command(dev,
6315230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
6316230130Smav	}
6317230130Smav	hdaa_unlock(devinfo);
6318230130Smav	for (i = 0; i < numgpi; i++) {
6319230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6320230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
6321230130Smav	}
6322230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6323230130Smav}
6324230130Smav
6325230130Smavstatic int
6326230130Smavhdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS)
6327230130Smav{
6328230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6329230130Smav	device_t dev = devinfo->dev;
6330230130Smav	char buf[256];
6331230130Smav	int n = 0, i, numgpio;
6332230130Smav	uint32_t data = 0, enable = 0, dir = 0;
6333230130Smav
6334230130Smav	buf[0] = 0;
6335230130Smav	hdaa_lock(devinfo);
6336230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6337230130Smav	if (numgpio > 0) {
6338230130Smav		data = hda_command(dev,
6339230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
6340230130Smav		enable = hda_command(dev,
6341230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
6342230130Smav		dir = hda_command(dev,
6343230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
6344230130Smav	}
6345230130Smav	hdaa_unlock(devinfo);
6346230130Smav	for (i = 0; i < numgpio; i++) {
6347230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=",
6348230130Smav		    n != 0 ? " " : "", i);
6349230130Smav		if ((enable & (1 << i)) == 0) {
6350230130Smav			n += snprintf(buf + n, sizeof(buf) - n, "disabled");
6351230130Smav			continue;
6352230130Smav		}
6353230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)",
6354230130Smav		    ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1));
6355230130Smav	}
6356230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6357230130Smav}
6358230130Smav
6359230130Smavstatic int
6360230130Smavhdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS)
6361230130Smav{
6362230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6363230130Smav	char buf[256];
6364230130Smav	int error, n = 0, i, numgpio;
6365230130Smav	uint32_t gpio, x;
6366230130Smav
6367230130Smav	gpio = devinfo->newgpio;
6368230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6369230130Smav	buf[0] = 0;
6370230130Smav	for (i = 0; i < numgpio; i++) {
6371230130Smav		x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6372230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6373230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6374230130Smav	}
6375230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6376230130Smav	if (error != 0 || req->newptr == NULL)
6377230130Smav		return (error);
6378230130Smav	if (strncmp(buf, "0x", 2) == 0)
6379230130Smav		gpio = strtol(buf + 2, NULL, 16);
6380230130Smav	else
6381230130Smav		gpio = hdaa_gpio_patch(gpio, buf);
6382230130Smav	hdaa_lock(devinfo);
6383230130Smav	devinfo->newgpio = devinfo->gpio = gpio;
6384230130Smav	hdaa_gpio_commit(devinfo);
6385230130Smav	hdaa_unlock(devinfo);
6386230130Smav	return (0);
6387230130Smav}
6388230130Smav
6389230130Smavstatic int
6390230130Smavhdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS)
6391230130Smav{
6392230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6393230130Smav	device_t dev = devinfo->dev;
6394230130Smav	char buf[256];
6395230130Smav	int n = 0, i, numgpo;
6396230130Smav	uint32_t data = 0;
6397230130Smav
6398230130Smav	buf[0] = 0;
6399230130Smav	hdaa_lock(devinfo);
6400230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6401230130Smav	if (numgpo > 0) {
6402230130Smav		data = hda_command(dev,
6403230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
6404230130Smav	}
6405230130Smav	hdaa_unlock(devinfo);
6406230130Smav	for (i = 0; i < numgpo; i++) {
6407230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6408230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
6409230130Smav	}
6410230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6411230130Smav}
6412230130Smav
6413230130Smavstatic int
6414230130Smavhdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS)
6415230130Smav{
6416230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6417230130Smav	char buf[256];
6418230130Smav	int error, n = 0, i, numgpo;
6419230130Smav	uint32_t gpo, x;
6420230130Smav
6421230130Smav	gpo = devinfo->newgpo;
6422230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6423230130Smav	buf[0] = 0;
6424230130Smav	for (i = 0; i < numgpo; i++) {
6425230130Smav		x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6426230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6427230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6428230130Smav	}
6429230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6430230130Smav	if (error != 0 || req->newptr == NULL)
6431230130Smav		return (error);
6432230130Smav	if (strncmp(buf, "0x", 2) == 0)
6433230130Smav		gpo = strtol(buf + 2, NULL, 16);
6434230130Smav	else
6435230130Smav		gpo = hdaa_gpio_patch(gpo, buf);
6436230130Smav	hdaa_lock(devinfo);
6437230130Smav	devinfo->newgpo = devinfo->gpo = gpo;
6438230130Smav	hdaa_gpo_commit(devinfo);
6439230130Smav	hdaa_unlock(devinfo);
6440230130Smav	return (0);
6441230130Smav}
6442230130Smav
6443230130Smavstatic int
6444230130Smavhdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS)
6445230130Smav{
6446230130Smav	device_t dev;
6447230130Smav	struct hdaa_devinfo *devinfo;
6448230130Smav	int error, val;
6449230130Smav
6450230130Smav	dev = oidp->oid_arg1;
6451230130Smav	devinfo = device_get_softc(dev);
6452230130Smav	if (devinfo == NULL)
6453230130Smav		return (EINVAL);
6454230130Smav	val = 0;
6455230130Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6456230130Smav	if (error != 0 || req->newptr == NULL || val == 0)
6457230130Smav		return (error);
6458230130Smav
6459230130Smav	HDA_BOOTHVERBOSE(
6460230130Smav		device_printf(dev, "Reconfiguration...\n");
6461230130Smav	);
6462230130Smav	if ((error = device_delete_children(dev)) != 0)
6463230130Smav		return (error);
6464230130Smav	hdaa_lock(devinfo);
6465230130Smav	hdaa_unconfigure(dev);
6466230130Smav	hdaa_configure(dev);
6467230130Smav	hdaa_unlock(devinfo);
6468230130Smav	bus_generic_attach(dev);
6469230130Smav	HDA_BOOTHVERBOSE(
6470230130Smav		device_printf(dev, "Reconfiguration done\n");
6471230130Smav	);
6472230130Smav	return (0);
6473230130Smav}
6474230130Smav
6475230130Smavstatic int
6476230130Smavhdaa_suspend(device_t dev)
6477230130Smav{
6478230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6479230130Smav	int i;
6480230130Smav
6481230130Smav	HDA_BOOTHVERBOSE(
6482230130Smav		device_printf(dev, "Suspend...\n");
6483230130Smav	);
6484230130Smav	hdaa_lock(devinfo);
6485230130Smav	HDA_BOOTHVERBOSE(
6486230130Smav		device_printf(dev, "Stop streams...\n");
6487230130Smav	);
6488230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6489230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) {
6490230130Smav			devinfo->chans[i].flags |= HDAA_CHN_SUSPEND;
6491230130Smav			hdaa_channel_stop(&devinfo->chans[i]);
6492230130Smav		}
6493230130Smav	}
6494230130Smav	HDA_BOOTHVERBOSE(
6495230130Smav		device_printf(dev, "Power down FG"
6496230130Smav		    " nid=%d to the D3 state...\n",
6497230130Smav		    devinfo->nid);
6498230130Smav	);
6499230130Smav	hda_command(devinfo->dev,
6500230130Smav	    HDA_CMD_SET_POWER_STATE(0,
6501230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D3));
6502230130Smav	callout_stop(&devinfo->poll_jack);
6503230130Smav	hdaa_unlock(devinfo);
6504230130Smav	callout_drain(&devinfo->poll_jack);
6505230130Smav	HDA_BOOTHVERBOSE(
6506230130Smav		device_printf(dev, "Suspend done\n");
6507230130Smav	);
6508230130Smav	return (0);
6509230130Smav}
6510230130Smav
6511230130Smavstatic int
6512230130Smavhdaa_resume(device_t dev)
6513230130Smav{
6514230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6515230130Smav	int i;
6516230130Smav
6517230130Smav	HDA_BOOTHVERBOSE(
6518230130Smav		device_printf(dev, "Resume...\n");
6519230130Smav	);
6520230130Smav	hdaa_lock(devinfo);
6521230130Smav	HDA_BOOTHVERBOSE(
6522230130Smav		device_printf(dev, "Power up audio FG nid=%d...\n",
6523230130Smav		    devinfo->nid);
6524230130Smav	);
6525230130Smav	hdaa_powerup(devinfo);
6526230130Smav	HDA_BOOTHVERBOSE(
6527230130Smav		device_printf(dev, "AFG commit...\n");
6528230130Smav	);
6529230130Smav	hdaa_audio_commit(devinfo);
6530230130Smav	HDA_BOOTHVERBOSE(
6531230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
6532230130Smav	);
6533230130Smav	hdaa_patch_direct(devinfo);
6534230130Smav	HDA_BOOTHVERBOSE(
6535230551Smav		device_printf(dev, "Pin sense init...\n");
6536230130Smav	);
6537230551Smav	hdaa_sense_init(devinfo);
6538230130Smav
6539230130Smav	hdaa_unlock(devinfo);
6540230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
6541230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
6542230130Smav		HDA_BOOTHVERBOSE(
6543230130Smav			device_printf(pdevinfo->dev,
6544230130Smav			    "OSS mixer reinitialization...\n");
6545230130Smav		);
6546230130Smav		if (mixer_reinit(pdevinfo->dev) == -1)
6547230130Smav			device_printf(pdevinfo->dev,
6548230130Smav			    "unable to reinitialize the mixer\n");
6549230130Smav	}
6550230130Smav	hdaa_lock(devinfo);
6551230130Smav	HDA_BOOTHVERBOSE(
6552230130Smav		device_printf(dev, "Start streams...\n");
6553230130Smav	);
6554230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6555230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) {
6556230130Smav			devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND;
6557230130Smav			hdaa_channel_start(&devinfo->chans[i]);
6558230130Smav		}
6559230130Smav	}
6560230130Smav	hdaa_unlock(devinfo);
6561230130Smav	HDA_BOOTHVERBOSE(
6562230130Smav		device_printf(dev, "Resume done\n");
6563230130Smav	);
6564230130Smav	return (0);
6565230130Smav}
6566230130Smav
6567230130Smavstatic int
6568230130Smavhdaa_probe(device_t dev)
6569230130Smav{
6570238022Smav	const char *pdesc;
6571230130Smav	char buf[128];
6572230130Smav
6573230130Smav	if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
6574230130Smav		return (ENXIO);
6575238022Smav	pdesc = device_get_desc(device_get_parent(dev));
6576238022Smav	snprintf(buf, sizeof(buf), "%.*s Audio Function Group",
6577238022Smav	    (int)(strlen(pdesc) - 10), pdesc);
6578230130Smav	device_set_desc_copy(dev, buf);
6579230130Smav	return (BUS_PROBE_DEFAULT);
6580230130Smav}
6581230130Smav
6582230130Smavstatic int
6583230130Smavhdaa_attach(device_t dev)
6584230130Smav{
6585230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6586230130Smav	uint32_t res;
6587230130Smav	nid_t nid = hda_get_node_id(dev);
6588230130Smav
6589230130Smav	devinfo->dev = dev;
6590230130Smav	devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
6591230130Smav	devinfo->nid = nid;
6592230130Smav	devinfo->newquirks = -1;
6593230130Smav	devinfo->newgpio = -1;
6594230130Smav	devinfo->newgpo = -1;
6595314667Savg	callout_init(&devinfo->poll_jack, 1);
6596230130Smav	devinfo->poll_ival = hz;
6597230130Smav
6598230130Smav	hdaa_lock(devinfo);
6599230130Smav	res = hda_command(dev,
6600230130Smav	    HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT));
6601230130Smav	hdaa_unlock(devinfo);
6602230130Smav
6603230130Smav	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
6604230130Smav	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
6605230130Smav	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
6606230130Smav
6607230130Smav	HDA_BOOTVERBOSE(
6608242352Smav		device_printf(dev, "Subsystem ID: 0x%08x\n",
6609242352Smav		    hda_get_subsystem_id(dev));
6610242352Smav	);
6611242352Smav	HDA_BOOTHVERBOSE(
6612230130Smav		device_printf(dev,
6613230130Smav		    "Audio Function Group at nid=%d: %d subnodes %d-%d\n",
6614230130Smav		    nid, devinfo->nodecnt,
6615230130Smav		    devinfo->startnode, devinfo->endnode - 1);
6616230130Smav	);
6617230130Smav
6618230130Smav	if (devinfo->nodecnt > 0)
6619230130Smav		devinfo->widget = (struct hdaa_widget *)malloc(
6620230130Smav		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA,
6621230130Smav		    M_WAITOK | M_ZERO);
6622230130Smav	else
6623230130Smav		devinfo->widget = NULL;
6624230130Smav
6625230130Smav	hdaa_lock(devinfo);
6626230130Smav	HDA_BOOTHVERBOSE(
6627230130Smav		device_printf(dev, "Powering up...\n");
6628230130Smav	);
6629230130Smav	hdaa_powerup(devinfo);
6630230130Smav	HDA_BOOTHVERBOSE(
6631230130Smav		device_printf(dev, "Parsing audio FG...\n");
6632230130Smav	);
6633230130Smav	hdaa_audio_parse(devinfo);
6634230130Smav	HDA_BOOTVERBOSE(
6635230130Smav		device_printf(dev, "Original pins configuration:\n");
6636230130Smav		hdaa_dump_pin_configs(devinfo);
6637230130Smav	);
6638230130Smav	hdaa_configure(dev);
6639230130Smav	hdaa_unlock(devinfo);
6640230130Smav
6641230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6642230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6643230130Smav	    "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6644301799Sngie	    &devinfo->newquirks, 0, hdaa_sysctl_quirks, "A",
6645301799Sngie	    "Configuration options");
6646230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6647230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6648230130Smav	    "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6649301799Sngie	    devinfo, 0, hdaa_sysctl_gpi_state, "A", "GPI state");
6650230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6651230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6652230130Smav	    "gpio_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6653301799Sngie	    devinfo, 0, hdaa_sysctl_gpio_state, "A", "GPIO state");
6654230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6655230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6656230130Smav	    "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6657301799Sngie	    devinfo, 0, hdaa_sysctl_gpio_config, "A", "GPIO configuration");
6658230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6659230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6660230130Smav	    "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6661301799Sngie	    devinfo, 0, hdaa_sysctl_gpo_state, "A", "GPO state");
6662230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6663230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6664230130Smav	    "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6665301799Sngie	    devinfo, 0, hdaa_sysctl_gpo_config, "A", "GPO configuration");
6666230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6667230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6668230130Smav	    "reconfig", CTLTYPE_INT | CTLFLAG_RW,
6669301799Sngie	    dev, 0, hdaa_sysctl_reconfig, "I", "Reprocess configuration");
6670230130Smav	bus_generic_attach(dev);
6671230130Smav	return (0);
6672230130Smav}
6673230130Smav
6674230130Smavstatic int
6675230130Smavhdaa_detach(device_t dev)
6676230130Smav{
6677230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6678230130Smav	int error;
6679230130Smav
6680230130Smav	if ((error = device_delete_children(dev)) != 0)
6681230130Smav		return (error);
6682230130Smav
6683230130Smav	hdaa_lock(devinfo);
6684230130Smav	hdaa_unconfigure(dev);
6685230130Smav	devinfo->poll_ival = 0;
6686230130Smav	callout_stop(&devinfo->poll_jack);
6687230130Smav	hdaa_unlock(devinfo);
6688230130Smav	callout_drain(&devinfo->poll_jack);
6689230130Smav
6690230130Smav	free(devinfo->widget, M_HDAA);
6691230130Smav	return (0);
6692230130Smav}
6693230130Smav
6694230130Smavstatic int
6695230130Smavhdaa_print_child(device_t dev, device_t child)
6696230130Smav{
6697230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6698230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6699230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6700230130Smav	struct hdaa_audio_as *as;
6701230130Smav	int retval, first = 1, i;
6702230130Smav
6703230130Smav	retval = bus_print_child_header(dev, child);
6704230130Smav	retval += printf(" at nid ");
6705230130Smav	if (pdevinfo->playas >= 0) {
6706230130Smav		as = &devinfo->as[pdevinfo->playas];
6707230130Smav		for (i = 0; i < 16; i++) {
6708230130Smav			if (as->pins[i] <= 0)
6709230130Smav				continue;
6710230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6711230130Smav			first = 0;
6712230130Smav		}
6713230130Smav	}
6714230130Smav	if (pdevinfo->recas >= 0) {
6715230130Smav		if (pdevinfo->playas >= 0) {
6716230130Smav			retval += printf(" and ");
6717230130Smav			first = 1;
6718230130Smav		}
6719230130Smav		as = &devinfo->as[pdevinfo->recas];
6720230130Smav		for (i = 0; i < 16; i++) {
6721230130Smav			if (as->pins[i] <= 0)
6722230130Smav				continue;
6723230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6724230130Smav			first = 0;
6725230130Smav		}
6726230130Smav	}
6727230130Smav	retval += bus_print_child_footer(dev, child);
6728230130Smav
6729230130Smav	return (retval);
6730230130Smav}
6731230130Smav
6732230130Smavstatic int
6733230130Smavhdaa_child_location_str(device_t dev, device_t child, char *buf,
6734230130Smav    size_t buflen)
6735230130Smav{
6736230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6737230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6738230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6739230130Smav	struct hdaa_audio_as *as;
6740230130Smav	int first = 1, i, len = 0;
6741230130Smav
6742230130Smav	len += snprintf(buf + len, buflen - len, "nid=");
6743230130Smav	if (pdevinfo->playas >= 0) {
6744230130Smav		as = &devinfo->as[pdevinfo->playas];
6745230130Smav		for (i = 0; i < 16; i++) {
6746230130Smav			if (as->pins[i] <= 0)
6747230130Smav				continue;
6748230130Smav			len += snprintf(buf + len, buflen - len,
6749230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6750230130Smav			first = 0;
6751230130Smav		}
6752230130Smav	}
6753230130Smav	if (pdevinfo->recas >= 0) {
6754230130Smav		as = &devinfo->as[pdevinfo->recas];
6755230130Smav		for (i = 0; i < 16; i++) {
6756230130Smav			if (as->pins[i] <= 0)
6757230130Smav				continue;
6758230130Smav			len += snprintf(buf + len, buflen - len,
6759230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6760230130Smav			first = 0;
6761230130Smav		}
6762230130Smav	}
6763230130Smav	return (0);
6764230130Smav}
6765230130Smav
6766230130Smavstatic void
6767230130Smavhdaa_stream_intr(device_t dev, int dir, int stream)
6768230130Smav{
6769230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6770230130Smav	struct hdaa_chan *ch;
6771230130Smav	int i;
6772230130Smav
6773230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6774230130Smav		ch = &devinfo->chans[i];
6775230130Smav		if (!(ch->flags & HDAA_CHN_RUNNING))
6776230130Smav			continue;
6777230130Smav		if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) &&
6778230130Smav		    ch->sid == stream) {
6779230130Smav			hdaa_unlock(devinfo);
6780230130Smav			chn_intr(ch->c);
6781230130Smav			hdaa_lock(devinfo);
6782230130Smav		}
6783230130Smav	}
6784230130Smav}
6785230130Smav
6786230130Smavstatic void
6787230130Smavhdaa_unsol_intr(device_t dev, uint32_t resp)
6788230130Smav{
6789230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6790230312Smav	struct hdaa_widget *w;
6791230312Smav	int i, tag, flags;
6792230130Smav
6793230312Smav	HDA_BOOTHVERBOSE(
6794230312Smav		device_printf(dev, "Unsolicited response %08x\n", resp);
6795230312Smav	);
6796230130Smav	tag = resp >> 26;
6797230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6798230312Smav		w = hdaa_widget_get(devinfo, i);
6799230312Smav		if (w == NULL || w->enable == 0 || w->type !=
6800230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6801230312Smav			continue;
6802230312Smav		if (w->unsol != tag)
6803230312Smav			continue;
6804230312Smav		if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
6805230312Smav		    HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
6806230312Smav			flags = resp & 0x03;
6807230312Smav		else
6808230312Smav			flags = 0x01;
6809230312Smav		if (flags & 0x01)
6810230551Smav			hdaa_presence_handler(w);
6811230312Smav		if (flags & 0x02)
6812230312Smav			hdaa_eld_handler(w);
6813230130Smav	}
6814230130Smav}
6815230130Smav
6816230130Smavstatic device_method_t hdaa_methods[] = {
6817230130Smav	/* device interface */
6818230130Smav	DEVMETHOD(device_probe,		hdaa_probe),
6819230130Smav	DEVMETHOD(device_attach,	hdaa_attach),
6820230130Smav	DEVMETHOD(device_detach,	hdaa_detach),
6821230130Smav	DEVMETHOD(device_suspend,	hdaa_suspend),
6822230130Smav	DEVMETHOD(device_resume,	hdaa_resume),
6823230130Smav	/* Bus interface */
6824230130Smav	DEVMETHOD(bus_print_child,	hdaa_print_child),
6825230130Smav	DEVMETHOD(bus_child_location_str, hdaa_child_location_str),
6826230130Smav	DEVMETHOD(hdac_stream_intr,	hdaa_stream_intr),
6827230130Smav	DEVMETHOD(hdac_unsol_intr,	hdaa_unsol_intr),
6828230130Smav	DEVMETHOD(hdac_pindump,		hdaa_pindump),
6829264961Smarius	DEVMETHOD_END
6830230130Smav};
6831230130Smav
6832230130Smavstatic driver_t hdaa_driver = {
6833230130Smav	"hdaa",
6834230130Smav	hdaa_methods,
6835230130Smav	sizeof(struct hdaa_devinfo),
6836230130Smav};
6837230130Smav
6838230130Smavstatic devclass_t hdaa_devclass;
6839230130Smav
6840264961SmariusDRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, NULL, NULL);
6841230130Smav
6842230130Smavstatic void
6843230130Smavhdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid,
6844230130Smav    char *buf, int buflen)
6845230130Smav{
6846230130Smav	struct hdaa_audio_as *as;
6847230130Smav	int c;
6848230130Smav
6849230130Smav	as = &devinfo->as[asid];
6850230130Smav	c = devinfo->chans[as->chans[0]].channels;
6851230130Smav	if (c == 1)
6852230130Smav		snprintf(buf, buflen, "mono");
6853230585Smav	else if (c == 2) {
6854230585Smav		if (as->hpredir < 0)
6855230585Smav			buf[0] = 0;
6856230585Smav		else
6857230585Smav			snprintf(buf, buflen, "2.0");
6858230585Smav	} else if (as->pinset == 0x0003)
6859230130Smav		snprintf(buf, buflen, "3.1");
6860230130Smav	else if (as->pinset == 0x0005 || as->pinset == 0x0011)
6861230130Smav		snprintf(buf, buflen, "4.0");
6862230130Smav	else if (as->pinset == 0x0007 || as->pinset == 0x0013)
6863230130Smav		snprintf(buf, buflen, "5.1");
6864230130Smav	else if (as->pinset == 0x0017)
6865230130Smav		snprintf(buf, buflen, "7.1");
6866230130Smav	else
6867230130Smav		snprintf(buf, buflen, "%dch", c);
6868230585Smav	if (as->hpredir >= 0)
6869230585Smav		strlcat(buf, "+HP", buflen);
6870230130Smav}
6871230130Smav
6872230130Smavstatic int
6873230585Smavhdaa_chan_type(struct hdaa_devinfo *devinfo, int asid)
6874230585Smav{
6875230585Smav	struct hdaa_audio_as *as;
6876230585Smav	struct hdaa_widget *w;
6877230585Smav	int i, t = -1, t1;
6878230585Smav
6879230585Smav	as = &devinfo->as[asid];
6880230585Smav	for (i = 0; i < 16; i++) {
6881230585Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
6882230585Smav		if (w == NULL || w->enable == 0 || w->type !=
6883230585Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6884230585Smav			continue;
6885230585Smav		t1 = HDA_CONFIG_DEFAULTCONF_DEVICE(w->wclass.pin.config);
6886230585Smav		if (t == -1)
6887230585Smav			t = t1;
6888230585Smav		else if (t != t1) {
6889230585Smav			t = -2;
6890230585Smav			break;
6891230585Smav		}
6892230585Smav	}
6893230585Smav	return (t);
6894230585Smav}
6895230585Smav
6896230585Smavstatic int
6897230488Smavhdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS)
6898230488Smav{
6899230488Smav	struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1;
6900230488Smav	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
6901230488Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6902230488Smav	struct hdaa_chan *ch;
6903230488Smav	int error, val, i;
6904230488Smav	uint32_t pcmcap;
6905230488Smav
6906230488Smav	ch = &devinfo->chans[as->chans[0]];
6907230488Smav	val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 :
6908230488Smav	    ((ch->bit32 == 2) ? 20 : 0));
6909230488Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6910230488Smav	if (error != 0 || req->newptr == NULL)
6911230488Smav		return (error);
6912230488Smav	pcmcap = ch->supp_pcm_size_rate;
6913230488Smav	if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6914230488Smav		ch->bit32 = 4;
6915230488Smav	else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6916230488Smav		ch->bit32 = 3;
6917230488Smav	else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6918230488Smav		ch->bit32 = 2;
6919230488Smav	else
6920230488Smav		return (EINVAL);
6921230488Smav	for (i = 1; i < as->num_chans; i++)
6922230488Smav		devinfo->chans[as->chans[i]].bit32 = ch->bit32;
6923230488Smav	return (0);
6924230488Smav}
6925230488Smav
6926230488Smavstatic int
6927230130Smavhdaa_pcm_probe(device_t dev)
6928230130Smav{
6929230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6930230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6931230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6932238022Smav	const char *pdesc;
6933230130Smav	char chans1[8], chans2[8];
6934230130Smav	char buf[128];
6935230585Smav	int loc1, loc2, t1, t2;
6936230130Smav
6937230130Smav	if (pdevinfo->playas >= 0)
6938230130Smav		loc1 = devinfo->as[pdevinfo->playas].location;
6939230130Smav	else
6940230130Smav		loc1 = devinfo->as[pdevinfo->recas].location;
6941230130Smav	if (pdevinfo->recas >= 0)
6942230130Smav		loc2 = devinfo->as[pdevinfo->recas].location;
6943230130Smav	else
6944230130Smav		loc2 = loc1;
6945230130Smav	if (loc1 != loc2)
6946230130Smav		loc1 = -2;
6947230130Smav	if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0')
6948230130Smav		loc1 = -2;
6949230130Smav	chans1[0] = 0;
6950230130Smav	chans2[0] = 0;
6951230585Smav	t1 = t2 = -1;
6952230585Smav	if (pdevinfo->playas >= 0) {
6953230130Smav		hdaa_chan_formula(devinfo, pdevinfo->playas,
6954230130Smav		    chans1, sizeof(chans1));
6955230585Smav		t1 = hdaa_chan_type(devinfo, pdevinfo->playas);
6956230585Smav	}
6957230585Smav	if (pdevinfo->recas >= 0) {
6958230130Smav		hdaa_chan_formula(devinfo, pdevinfo->recas,
6959230130Smav		    chans2, sizeof(chans2));
6960230585Smav		t2 = hdaa_chan_type(devinfo, pdevinfo->recas);
6961230585Smav	}
6962230130Smav	if (chans1[0] != 0 || chans2[0] != 0) {
6963230130Smav		if (chans1[0] == 0 && pdevinfo->playas >= 0)
6964230130Smav			snprintf(chans1, sizeof(chans1), "2.0");
6965230130Smav		else if (chans2[0] == 0 && pdevinfo->recas >= 0)
6966230130Smav			snprintf(chans2, sizeof(chans2), "2.0");
6967230130Smav		if (strcmp(chans1, chans2) == 0)
6968230130Smav			chans2[0] = 0;
6969230130Smav	}
6970230585Smav	if (t1 == -1)
6971230585Smav		t1 = t2;
6972230585Smav	else if (t2 == -1)
6973230585Smav		t2 = t1;
6974230585Smav	if (t1 != t2)
6975230585Smav		t1 = -2;
6976230585Smav	if (pdevinfo->digital)
6977230585Smav		t1 = -2;
6978238022Smav	pdesc = device_get_desc(device_get_parent(dev));
6979238022Smav	snprintf(buf, sizeof(buf), "%.*s (%s%s%s%s%s%s%s%s%s)",
6980238022Smav	    (int)(strlen(pdesc) - 21), pdesc,
6981230130Smav	    loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "",
6982230507Smav	    (pdevinfo->digital == 0x7)?"HDMI/DP":
6983230507Smav	    ((pdevinfo->digital == 0x5)?"DisplayPort":
6984230507Smav	    ((pdevinfo->digital == 0x3)?"HDMI":
6985230507Smav	    ((pdevinfo->digital)?"Digital":"Analog"))),
6986230130Smav	    chans1[0] ? " " : "", chans1,
6987230585Smav	    chans2[0] ? "/" : "", chans2,
6988230585Smav	    t1 >= 0 ? " " : "", t1 >= 0 ? HDA_DEVS[t1] : "");
6989230130Smav	device_set_desc_copy(dev, buf);
6990230130Smav	return (BUS_PROBE_SPECIFIC);
6991230130Smav}
6992230130Smav
6993230130Smavstatic int
6994230130Smavhdaa_pcm_attach(device_t dev)
6995230130Smav{
6996230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6997230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6998230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6999230130Smav	struct hdaa_audio_as *as;
7000230488Smav	struct snddev_info *d;
7001230130Smav	char status[SND_STATUSLEN];
7002230130Smav	int i;
7003230130Smav
7004230130Smav	pdevinfo->chan_size = pcm_getbuffersize(dev,
7005230130Smav	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
7006230130Smav
7007230130Smav	HDA_BOOTVERBOSE(
7008230130Smav		hdaa_dump_dac(pdevinfo);
7009230130Smav		hdaa_dump_adc(pdevinfo);
7010230130Smav		hdaa_dump_mix(pdevinfo);
7011230130Smav		hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
7012230130Smav		hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
7013230130Smav		hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
7014230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
7015230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
7016230130Smav		hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
7017230130Smav		hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
7018230130Smav		hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
7019230130Smav		hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
7020230130Smav		hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
7021230130Smav		hdaa_dump_ctls(pdevinfo, NULL, 0);
7022230130Smav	);
7023230130Smav
7024230130Smav	if (resource_int_value(device_get_name(dev),
7025230130Smav	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
7026230130Smav		i &= HDA_BLK_ALIGN;
7027230130Smav		if (i < HDA_BLK_MIN)
7028230130Smav			i = HDA_BLK_MIN;
7029230130Smav		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
7030230130Smav		i = 0;
7031230130Smav		while (pdevinfo->chan_blkcnt >> i)
7032230130Smav			i++;
7033230130Smav		pdevinfo->chan_blkcnt = 1 << (i - 1);
7034230130Smav		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
7035230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
7036230130Smav		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
7037230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
7038230130Smav	} else
7039230130Smav		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
7040230130Smav
7041230130Smav	/*
7042230130Smav	 * We don't register interrupt handler with snd_setup_intr
7043230130Smav	 * in pcm device. Mark pcm device as MPSAFE manually.
7044230130Smav	 */
7045230130Smav	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
7046230130Smav
7047230130Smav	HDA_BOOTHVERBOSE(
7048230130Smav		device_printf(dev, "OSS mixer initialization...\n");
7049230130Smav	);
7050230130Smav	if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0)
7051230130Smav		device_printf(dev, "Can't register mixer\n");
7052230130Smav
7053230130Smav	HDA_BOOTHVERBOSE(
7054230130Smav		device_printf(dev, "Registering PCM channels...\n");
7055230130Smav	);
7056230130Smav	if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0,
7057230130Smav	    (pdevinfo->recas >= 0)?1:0) != 0)
7058230130Smav		device_printf(dev, "Can't register PCM\n");
7059230130Smav
7060230130Smav	pdevinfo->registered++;
7061230130Smav
7062230488Smav	d = device_get_softc(dev);
7063230130Smav	if (pdevinfo->playas >= 0) {
7064230130Smav		as = &devinfo->as[pdevinfo->playas];
7065230130Smav		for (i = 0; i < as->num_chans; i++)
7066230130Smav			pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class,
7067230130Smav			    &devinfo->chans[as->chans[i]]);
7068230488Smav		SYSCTL_ADD_PROC(&d->play_sysctl_ctx,
7069230488Smav		    SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO,
7070230488Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7071230488Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
7072230488Smav		    "Resolution of 32bit samples (20/24/32bit)");
7073230130Smav	}
7074230130Smav	if (pdevinfo->recas >= 0) {
7075230130Smav		as = &devinfo->as[pdevinfo->recas];
7076230130Smav		for (i = 0; i < as->num_chans; i++)
7077230130Smav			pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class,
7078230130Smav			    &devinfo->chans[as->chans[i]]);
7079230488Smav		SYSCTL_ADD_PROC(&d->rec_sysctl_ctx,
7080230488Smav		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7081230488Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7082230488Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
7083230488Smav		    "Resolution of 32bit samples (20/24/32bit)");
7084230551Smav		pdevinfo->autorecsrc = 2;
7085230554Smav		resource_int_value(device_get_name(dev), device_get_unit(dev),
7086230554Smav		    "rec.autosrc", &pdevinfo->autorecsrc);
7087230551Smav		SYSCTL_ADD_INT(&d->rec_sysctl_ctx,
7088230551Smav		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7089273736Shselasky		    "autosrc", CTLFLAG_RW,
7090230551Smav		    &pdevinfo->autorecsrc, 0,
7091230551Smav		    "Automatic recording source selection");
7092230130Smav	}
7093230130Smav
7094230551Smav	if (pdevinfo->mixer != NULL) {
7095230551Smav		hdaa_audio_ctl_set_defaults(pdevinfo);
7096243181Smav		hdaa_lock(devinfo);
7097243181Smav		if (pdevinfo->playas >= 0) {
7098243181Smav			as = &devinfo->as[pdevinfo->playas];
7099243181Smav			hdaa_channels_handler(as);
7100243181Smav		}
7101230551Smav		if (pdevinfo->recas >= 0) {
7102230551Smav			as = &devinfo->as[pdevinfo->recas];
7103230551Smav			hdaa_autorecsrc_handler(as, NULL);
7104243181Smav			hdaa_channels_handler(as);
7105230551Smav		}
7106243181Smav		hdaa_unlock(devinfo);
7107230551Smav	}
7108230551Smav
7109230130Smav	snprintf(status, SND_STATUSLEN, "on %s %s",
7110230130Smav	    device_get_nameunit(device_get_parent(dev)),
7111230130Smav	    PCM_KLDSTRING(snd_hda));
7112230130Smav	pcm_setstatus(dev, status);
7113230130Smav
7114230130Smav	return (0);
7115230130Smav}
7116230130Smav
7117230130Smavstatic int
7118230130Smavhdaa_pcm_detach(device_t dev)
7119230130Smav{
7120230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
7121230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
7122230130Smav	int err;
7123230130Smav
7124230130Smav	if (pdevinfo->registered > 0) {
7125230130Smav		err = pcm_unregister(dev);
7126230130Smav		if (err != 0)
7127230130Smav			return (err);
7128230130Smav	}
7129230130Smav
7130230130Smav	return (0);
7131230130Smav}
7132230130Smav
7133230130Smavstatic device_method_t hdaa_pcm_methods[] = {
7134230130Smav	/* device interface */
7135230130Smav	DEVMETHOD(device_probe,		hdaa_pcm_probe),
7136230130Smav	DEVMETHOD(device_attach,	hdaa_pcm_attach),
7137230130Smav	DEVMETHOD(device_detach,	hdaa_pcm_detach),
7138264961Smarius	DEVMETHOD_END
7139230130Smav};
7140230130Smav
7141230130Smavstatic driver_t hdaa_pcm_driver = {
7142230130Smav	"pcm",
7143230130Smav	hdaa_pcm_methods,
7144230130Smav	PCM_SOFTC_SIZE,
7145230130Smav};
7146230130Smav
7147264961SmariusDRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, NULL, NULL);
7148230130SmavMODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
7149230130SmavMODULE_VERSION(snd_hda, 1);
7150