hdaa.c revision 273736
1238104Sdes/*-
2238104Sdes * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3238104Sdes * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4238104Sdes * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
5238104Sdes * All rights reserved.
6238104Sdes *
7238104Sdes * Redistribution and use in source and binary forms, with or without
8238104Sdes * modification, are permitted provided that the following conditions
9238104Sdes * are met:
10238104Sdes * 1. Redistributions of source code must retain the above copyright
11238104Sdes *    notice, this list of conditions and the following disclaimer.
12238104Sdes * 2. Redistributions in binary form must reproduce the above copyright
13238104Sdes *    notice, this list of conditions and the following disclaimer in the
14238104Sdes *    documentation and/or other materials provided with the distribution.
15238104Sdes *
16238104Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17238104Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18238104Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19238104Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20238104Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21238104Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22238104Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23238104Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24238104Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25238104Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26238104Sdes * SUCH DAMAGE.
27238104Sdes */
28238104Sdes
29238104Sdes/*
30238104Sdes * Intel High Definition Audio (Audio function) driver for FreeBSD.
31238104Sdes */
32238104Sdes
33238104Sdes#ifdef HAVE_KERNEL_OPTION_HEADERS
34238104Sdes#include "opt_snd.h"
35238104Sdes#endif
36238104Sdes
37238104Sdes#include <dev/sound/pcm/sound.h>
38238104Sdes
39238104Sdes#include <sys/ctype.h>
40238104Sdes#include <sys/taskqueue.h>
41238104Sdes
42238104Sdes#include <dev/sound/pci/hda/hdac.h>
43238104Sdes#include <dev/sound/pci/hda/hdaa.h>
44238104Sdes#include <dev/sound/pci/hda/hda_reg.h>
45238104Sdes
46238104Sdes#include "mixer_if.h"
47238104Sdes
48238104SdesSND_DECLARE_FILE("$FreeBSD: stable/10/sys/dev/sound/pci/hda/hdaa.c 273736 2014-10-27 14:38:00Z hselasky $");
49238104Sdes
50238104Sdes#define hdaa_lock(devinfo)	snd_mtxlock((devinfo)->lock)
51238104Sdes#define hdaa_unlock(devinfo)	snd_mtxunlock((devinfo)->lock)
52238104Sdes#define hdaa_lockassert(devinfo) snd_mtxassert((devinfo)->lock)
53238104Sdes#define hdaa_lockowned(devinfo)	mtx_owned((devinfo)->lock)
54238104Sdes
55238104Sdesstatic const struct {
56238104Sdes	const char *key;
57238104Sdes	uint32_t value;
58238104Sdes} hdaa_quirks_tab[] = {
59238104Sdes	{ "softpcmvol", HDAA_QUIRK_SOFTPCMVOL },
60238104Sdes	{ "fixedrate", HDAA_QUIRK_FIXEDRATE },
61238104Sdes	{ "forcestereo", HDAA_QUIRK_FORCESTEREO },
62238104Sdes	{ "eapdinv", HDAA_QUIRK_EAPDINV },
63238104Sdes	{ "senseinv", HDAA_QUIRK_SENSEINV },
64238104Sdes	{ "ivref50", HDAA_QUIRK_IVREF50 },
65238104Sdes	{ "ivref80", HDAA_QUIRK_IVREF80 },
66238104Sdes	{ "ivref100", HDAA_QUIRK_IVREF100 },
67238104Sdes	{ "ovref50", HDAA_QUIRK_OVREF50 },
68238104Sdes	{ "ovref80", HDAA_QUIRK_OVREF80 },
69238104Sdes	{ "ovref100", HDAA_QUIRK_OVREF100 },
70238104Sdes	{ "ivref", HDAA_QUIRK_IVREF },
71238104Sdes	{ "ovref", HDAA_QUIRK_OVREF },
72238104Sdes	{ "vref", HDAA_QUIRK_VREF },
73238104Sdes};
74238104Sdes
75238104Sdes#define HDA_PARSE_MAXDEPTH	10
76238104Sdes
77238104SdesMALLOC_DEFINE(M_HDAA, "hdaa", "HDA Audio");
78238104Sdes
79238104Sdesstatic const char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue",
80238104Sdes    "Green", "Red", "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B",
81238104Sdes    "Res.C", "Res.D", "White", "Other"};
82238104Sdes
83238104Sdesstatic const char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD",
84238104Sdes    "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in",
85238104Sdes    "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"};
86238104Sdes
87238104Sdesstatic const char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
88238104Sdes
89238104Sdesstatic const char *HDA_CONNECTORS[16] = {
90269257Sdes    "Unknown", "1/8", "1/4", "ATAPI", "RCA", "Optical", "Digital", "Analog",
91269257Sdes    "DIN", "XLR", "RJ-11", "Combo", "0xc", "0xd", "0xe", "Other" };
92269257Sdes
93269257Sdesstatic const char *HDA_LOCS[64] = {
94269257Sdes    "0x00", "Rear", "Front", "Left", "Right", "Top", "Bottom", "Rear-panel",
95269257Sdes	"Drive-bay", "0x09", "0x0a", "0x0b", "0x0c", "0x0d", "0x0e", "0x0f",
96269257Sdes    "Internal", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "Riser",
97269257Sdes	"0x18", "Onboard", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f",
98238104Sdes    "External", "Ext-Rear", "Ext-Front", "Ext-Left", "Ext-Right", "Ext-Top", "Ext-Bottom", "0x07",
99238104Sdes	"0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f",
100238104Sdes    "Other", "0x31", "0x32", "0x33", "0x34", "0x35", "Other-Bott", "Lid-In",
101238104Sdes	"Lid-Out", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f" };
102238104Sdes
103238104Sdesstatic const char *HDA_GPIO_ACTIONS[8] = {
104238104Sdes    "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"};
105238104Sdes
106238104Sdesstatic const char *HDA_HDMI_CODING_TYPES[18] = {
107238104Sdes    "undefined", "LPCM", "AC-3", "MPEG1", "MP3", "MPEG2", "AAC-LC", "DTS",
108238104Sdes    "ATRAC", "DSD", "E-AC-3", "DTS-HD", "MLP", "DST", "WMAPro", "HE-AAC",
109238104Sdes    "HE-AACv2", "MPEG-Surround"
110238104Sdes};
111238104Sdes
112238104Sdes/* Default */
113238104Sdesstatic uint32_t hdaa_fmt[] = {
114238104Sdes	SND_FORMAT(AFMT_S16_LE, 2, 0),
115238104Sdes	0
116238104Sdes};
117238104Sdes
118238104Sdesstatic struct pcmchan_caps hdaa_caps = {48000, 48000, hdaa_fmt, 0};
119238104Sdes
120238104Sdesstatic const struct {
121238104Sdes	uint32_t	rate;
122238104Sdes	int		valid;
123238104Sdes	uint16_t	base;
124238104Sdes	uint16_t	mul;
125238104Sdes	uint16_t	div;
126238104Sdes} hda_rate_tab[] = {
127238104Sdes	{   8000, 1, 0x0000, 0x0000, 0x0500 },	/* (48000 * 1) / 6 */
128238104Sdes	{   9600, 0, 0x0000, 0x0000, 0x0400 },	/* (48000 * 1) / 5 */
129238104Sdes	{  12000, 0, 0x0000, 0x0000, 0x0300 },	/* (48000 * 1) / 4 */
130269257Sdes	{  16000, 1, 0x0000, 0x0000, 0x0200 },	/* (48000 * 1) / 3 */
131238104Sdes	{  18000, 0, 0x0000, 0x1000, 0x0700 },	/* (48000 * 3) / 8 */
132238104Sdes	{  19200, 0, 0x0000, 0x0800, 0x0400 },	/* (48000 * 2) / 5 */
133238104Sdes	{  24000, 0, 0x0000, 0x0000, 0x0100 },	/* (48000 * 1) / 2 */
134238104Sdes	{  28800, 0, 0x0000, 0x1000, 0x0400 },	/* (48000 * 3) / 5 */
135238104Sdes	{  32000, 1, 0x0000, 0x0800, 0x0200 },	/* (48000 * 2) / 3 */
136238104Sdes	{  36000, 0, 0x0000, 0x1000, 0x0300 },	/* (48000 * 3) / 4 */
137238104Sdes	{  38400, 0, 0x0000, 0x1800, 0x0400 },	/* (48000 * 4) / 5 */
138238104Sdes	{  48000, 1, 0x0000, 0x0000, 0x0000 },	/* (48000 * 1) / 1 */
139238104Sdes	{  64000, 0, 0x0000, 0x1800, 0x0200 },	/* (48000 * 4) / 3 */
140238104Sdes	{  72000, 0, 0x0000, 0x1000, 0x0100 },	/* (48000 * 3) / 2 */
141238104Sdes	{  96000, 1, 0x0000, 0x0800, 0x0000 },	/* (48000 * 2) / 1 */
142238104Sdes	{ 144000, 0, 0x0000, 0x1000, 0x0000 },	/* (48000 * 3) / 1 */
143238104Sdes	{ 192000, 1, 0x0000, 0x1800, 0x0000 },	/* (48000 * 4) / 1 */
144238104Sdes	{   8820, 0, 0x4000, 0x0000, 0x0400 },	/* (44100 * 1) / 5 */
145238104Sdes	{  11025, 1, 0x4000, 0x0000, 0x0300 },	/* (44100 * 1) / 4 */
146238104Sdes	{  12600, 0, 0x4000, 0x0800, 0x0600 },	/* (44100 * 2) / 7 */
147238104Sdes	{  14700, 0, 0x4000, 0x0000, 0x0200 },	/* (44100 * 1) / 3 */
148238104Sdes	{  17640, 0, 0x4000, 0x0800, 0x0400 },	/* (44100 * 2) / 5 */
149269257Sdes	{  18900, 0, 0x4000, 0x1000, 0x0600 },	/* (44100 * 3) / 7 */
150269257Sdes	{  22050, 1, 0x4000, 0x0000, 0x0100 },	/* (44100 * 1) / 2 */
151269257Sdes	{  25200, 0, 0x4000, 0x1800, 0x0600 },	/* (44100 * 4) / 7 */
152269257Sdes	{  26460, 0, 0x4000, 0x1000, 0x0400 },	/* (44100 * 3) / 5 */
153269257Sdes	{  29400, 0, 0x4000, 0x0800, 0x0200 },	/* (44100 * 2) / 3 */
154269257Sdes	{  33075, 0, 0x4000, 0x1000, 0x0300 },	/* (44100 * 3) / 4 */
155238104Sdes	{  35280, 0, 0x4000, 0x1800, 0x0400 },	/* (44100 * 4) / 5 */
156238104Sdes	{  44100, 1, 0x4000, 0x0000, 0x0000 },	/* (44100 * 1) / 1 */
157238104Sdes	{  58800, 0, 0x4000, 0x1800, 0x0200 },	/* (44100 * 4) / 3 */
158238104Sdes	{  66150, 0, 0x4000, 0x1000, 0x0100 },	/* (44100 * 3) / 2 */
159238104Sdes	{  88200, 1, 0x4000, 0x0800, 0x0000 },	/* (44100 * 2) / 1 */
160238104Sdes	{ 132300, 0, 0x4000, 0x1000, 0x0000 },	/* (44100 * 3) / 1 */
161238104Sdes	{ 176400, 1, 0x4000, 0x1800, 0x0000 },	/* (44100 * 4) / 1 */
162238104Sdes};
163238104Sdes#define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
164269257Sdes
165269257Sdesconst static char *ossnames[] = SOUND_DEVICE_NAMES;
166269257Sdes
167238104Sdes/****************************************************************************
168269257Sdes * Function prototypes
169269257Sdes ****************************************************************************/
170269257Sdesstatic int	hdaa_pcmchannel_setup(struct hdaa_chan *);
171269257Sdes
172269257Sdesstatic void	hdaa_widget_connection_select(struct hdaa_widget *, uint8_t);
173238104Sdesstatic void	hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *,
174269257Sdes						uint32_t, int, int);
175269257Sdesstatic struct	hdaa_audio_ctl *hdaa_audio_ctl_amp_get(struct hdaa_devinfo *,
176269257Sdes							nid_t, int, int, int);
177238104Sdesstatic void	hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *,
178238104Sdes				nid_t, int, int, int, int, int, int);
179238104Sdes
180269257Sdesstatic void	hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf);
181269257Sdes
182238104Sdesstatic char *
183269257Sdeshdaa_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
184238104Sdes{
185238104Sdes	int i, first = 1;
186238104Sdes
187269257Sdes	bzero(buf, len);
188238104Sdes	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
189269257Sdes		if (mask & (1 << i)) {
190238104Sdes			if (first == 0)
191238104Sdes				strlcat(buf, ", ", len);
192238104Sdes			strlcat(buf, ossnames[i], len);
193238104Sdes			first = 0;
194238104Sdes		}
195238104Sdes	}
196238104Sdes	return (buf);
197238104Sdes}
198238104Sdes
199238104Sdesstatic struct hdaa_audio_ctl *
200238104Sdeshdaa_audio_ctl_each(struct hdaa_devinfo *devinfo, int *index)
201238104Sdes{
202238104Sdes	if (devinfo == NULL ||
203238104Sdes	    index == NULL || devinfo->ctl == NULL ||
204238104Sdes	    devinfo->ctlcnt < 1 ||
205238104Sdes	    *index < 0 || *index >= devinfo->ctlcnt)
206238104Sdes		return (NULL);
207238104Sdes	return (&devinfo->ctl[(*index)++]);
208238104Sdes}
209238104Sdes
210238104Sdesstatic struct hdaa_audio_ctl *
211238104Sdeshdaa_audio_ctl_amp_get(struct hdaa_devinfo *devinfo, nid_t nid, int dir,
212269257Sdes						int index, int cnt)
213269257Sdes{
214238104Sdes	struct hdaa_audio_ctl *ctl;
215238104Sdes	int i, found = 0;
216238104Sdes
217238104Sdes	if (devinfo == NULL || devinfo->ctl == NULL)
218269257Sdes		return (NULL);
219269257Sdes
220269257Sdes	i = 0;
221238104Sdes	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
222269257Sdes		if (ctl->enable == 0)
223238104Sdes			continue;
224238104Sdes		if (ctl->widget->nid != nid)
225238104Sdes			continue;
226238104Sdes		if (dir && ctl->ndir != dir)
227238104Sdes			continue;
228238104Sdes		if (index >= 0 && ctl->ndir == HDAA_CTL_IN &&
229238104Sdes		    ctl->dir == ctl->ndir && ctl->index != index)
230238104Sdes			continue;
231269257Sdes		found++;
232269257Sdes		if (found == cnt || cnt <= 0)
233238104Sdes			return (ctl);
234238104Sdes	}
235238104Sdes
236238104Sdes	return (NULL);
237238104Sdes}
238238104Sdes
239238104Sdesstatic const struct matrix {
240238104Sdes	struct pcmchan_matrix	m;
241269257Sdes	int			analog;
242269257Sdes} matrixes[]  = {
243238104Sdes    { SND_CHN_MATRIX_MAP_1_0,	1 },
244269257Sdes    { SND_CHN_MATRIX_MAP_2_0,	1 },
245269257Sdes    { SND_CHN_MATRIX_MAP_2_1,	0 },
246269257Sdes    { SND_CHN_MATRIX_MAP_3_0,	0 },
247238104Sdes    { SND_CHN_MATRIX_MAP_3_1,	0 },
248269257Sdes    { SND_CHN_MATRIX_MAP_4_0,	1 },
249238104Sdes    { SND_CHN_MATRIX_MAP_4_1,	0 },
250238104Sdes    { SND_CHN_MATRIX_MAP_5_0,	0 },
251238104Sdes    { SND_CHN_MATRIX_MAP_5_1,	1 },
252238104Sdes    { SND_CHN_MATRIX_MAP_6_0,	0 },
253238104Sdes    { SND_CHN_MATRIX_MAP_6_1,	0 },
254238104Sdes    { SND_CHN_MATRIX_MAP_7_0,	0 },
255269257Sdes    { SND_CHN_MATRIX_MAP_7_1,	1 },
256238104Sdes};
257238104Sdes
258238104Sdesstatic const char *channel_names[] = SND_CHN_T_NAMES;
259238104Sdes
260238104Sdes/*
261238104Sdes * Connected channels change handler.
262238104Sdes */
263238104Sdesstatic void
264238104Sdeshdaa_channels_handler(struct hdaa_audio_as *as)
265238104Sdes{
266238104Sdes	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
267238104Sdes	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
268238104Sdes	struct hdaa_chan *ch = &devinfo->chans[as->chans[0]];
269238104Sdes	struct hdaa_widget *w;
270238104Sdes	uint8_t *eld;
271238104Sdes	int i, total, sub, assume, channels;
272238104Sdes	uint16_t cpins, upins, tpins;
273238104Sdes
274269257Sdes	cpins = upins = 0;
275238104Sdes	eld = NULL;
276238104Sdes	for (i = 0; i < 16; i++) {
277238104Sdes		if (as->pins[i] <= 0)
278238104Sdes			continue;
279238104Sdes		w = hdaa_widget_get(devinfo, as->pins[i]);
280238104Sdes		if (w == NULL)
281238104Sdes			continue;
282238104Sdes		if (w->wclass.pin.connected == 1)
283238104Sdes			cpins |= (1 << i);
284238104Sdes		else if (w->wclass.pin.connected != 0)
285238104Sdes			upins |= (1 << i);
286269257Sdes		if (w->eld != NULL && w->eld_len >= 8)
287269257Sdes			eld = w->eld;
288238104Sdes	}
289238104Sdes	tpins = cpins | upins;
290269257Sdes	if (as->hpredir >= 0)
291238104Sdes		tpins &= 0x7fff;
292238104Sdes	if (tpins == 0)
293238104Sdes		tpins = as->pinset;
294238104Sdes
295238104Sdes	total = sub = assume = channels = 0;
296269257Sdes	if (eld) {
297238104Sdes		/* Map CEA speakers to sound(4) channels. */
298238104Sdes		if (eld[7] & 0x01) /* Front Left/Right */
299238104Sdes			channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
300238104Sdes		if (eld[7] & 0x02) /* Low Frequency Effect */
301269257Sdes			channels |= SND_CHN_T_MASK_LF;
302269257Sdes		if (eld[7] & 0x04) /* Front Center */
303269257Sdes			channels |= SND_CHN_T_MASK_FC;
304238104Sdes		if (eld[7] & 0x08) { /* Rear Left/Right */
305269257Sdes			/* If we have both RLR and RLRC, report RLR as side. */
306238104Sdes			if (eld[7] & 0x40) /* Rear Left/Right Center */
307238104Sdes			    channels |= SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR;
308238104Sdes			else
309238104Sdes			    channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
310238104Sdes		}
311269257Sdes		if (eld[7] & 0x10) /* Rear center */
312269257Sdes			channels |= SND_CHN_T_MASK_BC;
313238104Sdes		if (eld[7] & 0x20) /* Front Left/Right Center */
314238104Sdes			channels |= SND_CHN_T_MASK_FLC | SND_CHN_T_MASK_FRC;
315238104Sdes		if (eld[7] & 0x40) /* Rear Left/Right Center */
316238104Sdes			channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
317238104Sdes	} else if (as->pinset != 0 && (tpins & 0xffe0) == 0) {
318238104Sdes		/* Map UAA speakers to sound(4) channels. */
319238104Sdes		if (tpins & 0x0001)
320238104Sdes			channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
321238104Sdes		if (tpins & 0x0002)
322238104Sdes			channels |= SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF;
323238104Sdes		if (tpins & 0x0004)
324238104Sdes			channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
325238104Sdes		if (tpins & 0x0008)
326238104Sdes			channels |= SND_CHN_T_MASK_FLC | SND_CHN_T_MASK_FRC;
327238104Sdes		if (tpins & 0x0010) {
328238104Sdes			/* If there is no back pin, report side as back. */
329238104Sdes			if ((as->pinset & 0x0004) == 0)
330238104Sdes			    channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
331238104Sdes			else
332238104Sdes			    channels |= SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR;
333238104Sdes		}
334238104Sdes	} else if (as->mixed) {
335238104Sdes		/* Mixed assoc can be only stereo or theoretically mono. */
336238104Sdes		if (ch->channels == 1)
337238104Sdes			channels |= SND_CHN_T_MASK_FC;
338238104Sdes		else
339238104Sdes			channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
340238104Sdes	}
341269257Sdes	if (channels) {	/* We have some usable channels info. */
342269257Sdes		HDA_BOOTVERBOSE(
343238104Sdes			device_printf(pdevinfo->dev, "%s channel set is: ",
344269257Sdes			    as->dir == HDAA_CTL_OUT ? "Playback" : "Recording");
345269257Sdes			for (i = 0; i < SND_CHN_T_MAX; i++)
346269257Sdes				if (channels & (1 << i))
347269257Sdes					printf("%s, ", channel_names[i]);
348269257Sdes			printf("\n");
349269257Sdes		);
350269257Sdes		/* Look for maximal fitting matrix. */
351269257Sdes		for (i = 0; i < sizeof(matrixes) / sizeof(struct matrix); i++) {
352269257Sdes			if (as->pinset != 0 && matrixes[i].analog == 0)
353269257Sdes				continue;
354269257Sdes			if ((matrixes[i].m.mask & ~channels) == 0) {
355269257Sdes				total = matrixes[i].m.channels;
356269257Sdes				sub = matrixes[i].m.ext;
357269257Sdes			}
358269257Sdes		}
359269257Sdes	}
360269257Sdes	if (total == 0) {
361269257Sdes		assume = 1;
362269257Sdes		total = ch->channels;
363269257Sdes		sub = (total == 6 || total == 8) ? 1 : 0;
364269257Sdes	}
365269257Sdes	HDA_BOOTVERBOSE(
366269257Sdes		device_printf(pdevinfo->dev,
367269257Sdes		    "%s channel matrix is: %s%d.%d (%s)\n",
368269257Sdes		    as->dir == HDAA_CTL_OUT ? "Playback" : "Recording",
369269257Sdes		    assume ? "unknown, assuming " : "", total - sub, sub,
370269257Sdes		    cpins != 0 ? "connected" :
371269257Sdes		    (upins != 0 ? "unknown" : "disconnected"));
372269257Sdes	);
373269257Sdes}
374269257Sdes
375269257Sdes/*
376269257Sdes * Headphones redirection change handler.
377269257Sdes */
378269257Sdesstatic void
379269257Sdeshdaa_hpredir_handler(struct hdaa_widget *w)
380269257Sdes{
381269257Sdes	struct hdaa_devinfo *devinfo = w->devinfo;
382269257Sdes	struct hdaa_audio_as *as = &devinfo->as[w->bindas];
383269257Sdes	struct hdaa_widget *w1;
384269257Sdes	struct hdaa_audio_ctl *ctl;
385269257Sdes	uint32_t val;
386269257Sdes	int j, connected = w->wclass.pin.connected;
387269257Sdes
388269257Sdes	HDA_BOOTVERBOSE(
389269257Sdes		device_printf((as->pdevinfo && as->pdevinfo->dev) ?
390269257Sdes		    as->pdevinfo->dev : devinfo->dev,
391269257Sdes		    "Redirect output to: %s\n",
392269257Sdes		    connected ? "headphones": "main");
393269257Sdes	);
394269257Sdes	/* (Un)Mute headphone pin. */
395269257Sdes	ctl = hdaa_audio_ctl_amp_get(devinfo,
396269257Sdes	    w->nid, HDAA_CTL_IN, -1, 1);
397269257Sdes	if (ctl != NULL && ctl->mute) {
398269257Sdes		/* If pin has muter - use it. */
399269257Sdes		val = connected ? 0 : 1;
400269257Sdes		if (val != ctl->forcemute) {
401269257Sdes			ctl->forcemute = val;
402269257Sdes			hdaa_audio_ctl_amp_set(ctl,
403269257Sdes			    HDAA_AMP_MUTE_DEFAULT,
404269257Sdes			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
405269257Sdes		}
406269257Sdes	} else {
407269257Sdes		/* If there is no muter - disable pin output. */
408269257Sdes		if (connected)
409269257Sdes			val = w->wclass.pin.ctrl |
410269257Sdes			    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
411269257Sdes		else
412269257Sdes			val = w->wclass.pin.ctrl &
413269257Sdes			    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
414269257Sdes		if (val != w->wclass.pin.ctrl) {
415269257Sdes			w->wclass.pin.ctrl = val;
416269257Sdes			hda_command(devinfo->dev,
417269257Sdes			    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
418269257Sdes			    w->nid, w->wclass.pin.ctrl));
419269257Sdes		}
420269257Sdes	}
421269257Sdes	/* (Un)Mute other pins. */
422269257Sdes	for (j = 0; j < 15; j++) {
423269257Sdes		if (as->pins[j] <= 0)
424269257Sdes			continue;
425269257Sdes		ctl = hdaa_audio_ctl_amp_get(devinfo,
426269257Sdes		    as->pins[j], HDAA_CTL_IN, -1, 1);
427269257Sdes		if (ctl != NULL && ctl->mute) {
428269257Sdes			/* If pin has muter - use it. */
429269257Sdes			val = connected ? 1 : 0;
430269257Sdes			if (val == ctl->forcemute)
431269257Sdes				continue;
432238104Sdes			ctl->forcemute = val;
433269257Sdes			hdaa_audio_ctl_amp_set(ctl,
434269257Sdes			    HDAA_AMP_MUTE_DEFAULT,
435269257Sdes			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
436238104Sdes			continue;
437269257Sdes		}
438269257Sdes		/* If there is no muter - disable pin output. */
439269257Sdes		w1 = hdaa_widget_get(devinfo, as->pins[j]);
440269257Sdes		if (w1 != NULL) {
441269257Sdes			if (connected)
442238104Sdes				val = w1->wclass.pin.ctrl &
443269257Sdes				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
444269257Sdes			else
445269257Sdes				val = w1->wclass.pin.ctrl |
446269257Sdes				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
447269257Sdes			if (val != w1->wclass.pin.ctrl) {
448269257Sdes				w1->wclass.pin.ctrl = val;
449269257Sdes				hda_command(devinfo->dev,
450269257Sdes				    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
451269257Sdes				    w1->nid, w1->wclass.pin.ctrl));
452269257Sdes			}
453269257Sdes		}
454269257Sdes	}
455269257Sdes}
456238104Sdes
457269257Sdes/*
458269257Sdes * Recording source change handler.
459269257Sdes */
460269257Sdesstatic void
461269257Sdeshdaa_autorecsrc_handler(struct hdaa_audio_as *as, struct hdaa_widget *w)
462269257Sdes{
463269257Sdes	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
464269257Sdes	struct hdaa_devinfo *devinfo;
465269257Sdes	struct hdaa_widget *w1;
466269257Sdes	int i, mask, fullmask, prio, bestprio;
467269257Sdes	char buf[128];
468269257Sdes
469269257Sdes	if (!as->mixed || pdevinfo == NULL || pdevinfo->mixer == NULL)
470269257Sdes		return;
471269257Sdes	/* Don't touch anything if we asked not to. */
472269257Sdes	if (pdevinfo->autorecsrc == 0 ||
473269257Sdes	    (pdevinfo->autorecsrc == 1 && w != NULL))
474269257Sdes		return;
475269257Sdes	/* Don't touch anything if "mix" or "speaker" selected. */
476269257Sdes	if (pdevinfo->recsrc & (SOUND_MASK_IMIX | SOUND_MASK_SPEAKER))
477269257Sdes		return;
478238104Sdes	/* Don't touch anything if several selected. */
479269257Sdes	if (ffs(pdevinfo->recsrc) != fls(pdevinfo->recsrc))
480269257Sdes		return;
481269257Sdes	devinfo = pdevinfo->devinfo;
482269257Sdes	mask = fullmask = 0;
483269257Sdes	bestprio = 0;
484269257Sdes	for (i = 0; i < 16; i++) {
485269257Sdes		if (as->pins[i] <= 0)
486269257Sdes			continue;
487269257Sdes		w1 = hdaa_widget_get(devinfo, as->pins[i]);
488269257Sdes		if (w1 == NULL || w1->enable == 0)
489269257Sdes			continue;
490269257Sdes		if (w1->wclass.pin.connected == 0)
491269257Sdes			continue;
492269257Sdes		prio = (w1->wclass.pin.connected == 1) ? 2 : 1;
493269257Sdes		if (prio < bestprio)
494269257Sdes			continue;
495269257Sdes		if (prio > bestprio) {
496269257Sdes			mask = 0;
497238104Sdes			bestprio = prio;
498269257Sdes		}
499269257Sdes		mask |= (1 << w1->ossdev);
500269257Sdes		fullmask |= (1 << w1->ossdev);
501269257Sdes	}
502238104Sdes	if (mask == 0)
503269257Sdes		return;
504269257Sdes	/* Prefer newly connected input. */
505269257Sdes	if (w != NULL && (mask & (1 << w->ossdev)))
506269257Sdes		mask = (1 << w->ossdev);
507269257Sdes	/* Prefer previously selected input */
508269257Sdes	if (mask & pdevinfo->recsrc)
509269257Sdes		mask &= pdevinfo->recsrc;
510269257Sdes	/* Prefer mic. */
511269257Sdes	if (mask & SOUND_MASK_MIC)
512269257Sdes		mask = SOUND_MASK_MIC;
513269257Sdes	/* Prefer monitor (2nd mic). */
514269257Sdes	if (mask & SOUND_MASK_MONITOR)
515269257Sdes		mask = SOUND_MASK_MONITOR;
516269257Sdes	/* Just take first one. */
517269257Sdes	mask = (1 << (ffs(mask) - 1));
518269257Sdes	HDA_BOOTVERBOSE(
519238104Sdes		hdaa_audio_ctl_ossmixer_mask2allname(mask, buf, sizeof(buf));
520269257Sdes		device_printf(pdevinfo->dev,
521269257Sdes		    "Automatically set rec source to: %s\n", buf);
522269257Sdes	);
523269257Sdes	hdaa_unlock(devinfo);
524269257Sdes	mix_setrecsrc(pdevinfo->mixer, mask);
525269257Sdes	hdaa_lock(devinfo);
526238104Sdes}
527269257Sdes
528269257Sdes/*
529269257Sdes * Jack presence detection event handler.
530269257Sdes */
531269257Sdesstatic void
532238104Sdeshdaa_presence_handler(struct hdaa_widget *w)
533269257Sdes{
534269257Sdes	struct hdaa_devinfo *devinfo = w->devinfo;
535269257Sdes	struct hdaa_audio_as *as;
536269257Sdes	uint32_t res;
537269257Sdes	int connected, old;
538269257Sdes
539269257Sdes	if (w->enable == 0 || w->type !=
540238104Sdes	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
541269257Sdes		return;
542269257Sdes
543269257Sdes	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
544269257Sdes	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
545269257Sdes		return;
546269257Sdes
547269257Sdes	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
548269257Sdes	connected = (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) != 0;
549269257Sdes	if (devinfo->quirks & HDAA_QUIRK_SENSEINV)
550269257Sdes		connected = !connected;
551269257Sdes	old = w->wclass.pin.connected;
552269257Sdes	if (connected == old)
553269257Sdes		return;
554269257Sdes	w->wclass.pin.connected = connected;
555269257Sdes	HDA_BOOTVERBOSE(
556269257Sdes		if (connected || old != 2) {
557269257Sdes			device_printf(devinfo->dev,
558269257Sdes			    "Pin sense: nid=%d sense=0x%08x (%sconnected)\n",
559269257Sdes			    w->nid, res, !connected ? "dis" : "");
560269257Sdes		}
561269257Sdes	);
562269257Sdes
563269257Sdes	as = &devinfo->as[w->bindas];
564269257Sdes	if (as->hpredir >= 0 && as->pins[15] == w->nid)
565269257Sdes		hdaa_hpredir_handler(w);
566269257Sdes	if (as->dir == HDAA_CTL_IN && old != 2)
567269257Sdes		hdaa_autorecsrc_handler(as, w);
568269257Sdes	if (old != 2)
569269257Sdes		hdaa_channels_handler(as);
570269257Sdes}
571269257Sdes
572269257Sdes/*
573269257Sdes * Callback for poll based presence detection.
574269257Sdes */
575269257Sdesstatic void
576269257Sdeshdaa_jack_poll_callback(void *arg)
577238104Sdes{
578238104Sdes	struct hdaa_devinfo *devinfo = arg;
579269257Sdes	struct hdaa_widget *w;
580269257Sdes	int i;
581269257Sdes
582269257Sdes	hdaa_lock(devinfo);
583269257Sdes	if (devinfo->poll_ival == 0) {
584269257Sdes		hdaa_unlock(devinfo);
585238104Sdes		return;
586269257Sdes	}
587269257Sdes	for (i = 0; i < devinfo->ascnt; i++) {
588269257Sdes		if (devinfo->as[i].hpredir < 0)
589269257Sdes			continue;
590269257Sdes		w = hdaa_widget_get(devinfo, devinfo->as[i].pins[15]);
591269257Sdes		if (w == NULL || w->enable == 0 || w->type !=
592269257Sdes		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
593269257Sdes			continue;
594269257Sdes		hdaa_presence_handler(w);
595269257Sdes	}
596269257Sdes	callout_reset(&devinfo->poll_jack, devinfo->poll_ival,
597269257Sdes	    hdaa_jack_poll_callback, devinfo);
598269257Sdes	hdaa_unlock(devinfo);
599269257Sdes}
600269257Sdes
601238104Sdesstatic void
602269257Sdeshdaa_eld_dump(struct hdaa_widget *w)
603238104Sdes{
604238104Sdes	struct hdaa_devinfo *devinfo = w->devinfo;
605238104Sdes	device_t dev = devinfo->dev;
606238104Sdes	uint8_t *sad;
607269257Sdes	int len, mnl, i, sadc, fmt;
608269257Sdes
609269257Sdes	if (w->eld == NULL || w->eld_len < 4)
610238104Sdes		return;
611238104Sdes	device_printf(dev,
612238104Sdes	    "ELD nid=%d: ELD_Ver=%u Baseline_ELD_Len=%u\n",
613238104Sdes	    w->nid, w->eld[0] >> 3, w->eld[2]);
614238104Sdes	if ((w->eld[0] >> 3) != 0x02)
615238104Sdes		return;
616246854Sdes	len = min(w->eld_len, (u_int)w->eld[2] * 4);
617246854Sdes	mnl = w->eld[4] & 0x1f;
618246854Sdes	device_printf(dev,
619238104Sdes	    "ELD nid=%d: CEA_EDID_Ver=%u MNL=%u\n",
620238104Sdes	    w->nid, w->eld[4] >> 5, mnl);
621238104Sdes	sadc = w->eld[5] >> 4;
622269257Sdes	device_printf(dev,
623269257Sdes	    "ELD nid=%d: SAD_Count=%u Conn_Type=%u S_AI=%u HDCP=%u\n",
624269257Sdes	    w->nid, sadc, (w->eld[5] >> 2) & 0x3,
625269257Sdes	    (w->eld[5] >> 1) & 0x1, w->eld[5] & 0x1);
626269257Sdes	device_printf(dev,
627269257Sdes	    "ELD nid=%d: Aud_Synch_Delay=%ums\n",
628269257Sdes	    w->nid, w->eld[6] * 2);
629269257Sdes	device_printf(dev,
630269257Sdes	    "ELD nid=%d: Channels=0x%b\n",
631269257Sdes	    w->nid, w->eld[7],
632269257Sdes	    "\020\07RLRC\06FLRC\05RC\04RLR\03FC\02LFE\01FLR");
633269257Sdes	device_printf(dev,
634269257Sdes	    "ELD nid=%d: Port_ID=0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
635238104Sdes	    w->nid, w->eld[8], w->eld[9], w->eld[10], w->eld[11],
636238104Sdes	    w->eld[12], w->eld[13], w->eld[14], w->eld[15]);
637238104Sdes	device_printf(dev,
638238104Sdes	    "ELD nid=%d: Manufacturer_Name=0x%02x%02x\n",
639269257Sdes	    w->nid, w->eld[16], w->eld[17]);
640269257Sdes	device_printf(dev,
641269257Sdes	    "ELD nid=%d: Product_Code=0x%02x%02x\n",
642269257Sdes	    w->nid, w->eld[18], w->eld[19]);
643238104Sdes	device_printf(dev,
644238104Sdes	    "ELD nid=%d: Monitor_Name_String='%.*s'\n",
645269257Sdes	    w->nid, mnl, &w->eld[20]);
646238104Sdes	for (i = 0; i < sadc; i++) {
647238104Sdes		sad = &w->eld[20 + mnl + i * 3];
648238104Sdes		fmt = (sad[0] >> 3) & 0x0f;
649238104Sdes		if (fmt == HDA_HDMI_CODING_TYPE_REF_CTX) {
650238104Sdes			fmt = (sad[2] >> 3) & 0x1f;
651238104Sdes			if (fmt < 1 || fmt > 3)
652238104Sdes				fmt = 0;
653238104Sdes			else
654238104Sdes				fmt += 14;
655238104Sdes		}
656238104Sdes		device_printf(dev,
657238104Sdes		    "ELD nid=%d: %s %dch freqs=0x%b",
658238104Sdes		    w->nid, HDA_HDMI_CODING_TYPES[fmt], (sad[0] & 0x07) + 1,
659238104Sdes		    sad[1], "\020\007192\006176\00596\00488\00348\00244\00132");
660238104Sdes		switch (fmt) {
661238104Sdes		case HDA_HDMI_CODING_TYPE_LPCM:
662238104Sdes			printf(" sizes=0x%b",
663238104Sdes			    sad[2] & 0x07, "\020\00324\00220\00116");
664238104Sdes			break;
665238104Sdes		case HDA_HDMI_CODING_TYPE_AC3:
666238104Sdes		case HDA_HDMI_CODING_TYPE_MPEG1:
667238104Sdes		case HDA_HDMI_CODING_TYPE_MP3:
668238104Sdes		case HDA_HDMI_CODING_TYPE_MPEG2:
669238104Sdes		case HDA_HDMI_CODING_TYPE_AACLC:
670238104Sdes		case HDA_HDMI_CODING_TYPE_DTS:
671238104Sdes		case HDA_HDMI_CODING_TYPE_ATRAC:
672238104Sdes			printf(" max_bitrate=%d", sad[2] * 8000);
673238104Sdes			break;
674238104Sdes		case HDA_HDMI_CODING_TYPE_WMAPRO:
675238104Sdes			printf(" profile=%d", sad[2] & 0x07);
676238104Sdes			break;
677238104Sdes		}
678238104Sdes		printf("\n");
679238104Sdes	}
680238104Sdes}
681238104Sdes
682238104Sdesstatic void
683238104Sdeshdaa_eld_handler(struct hdaa_widget *w)
684238104Sdes{
685238104Sdes	struct hdaa_devinfo *devinfo = w->devinfo;
686238104Sdes	uint32_t res;
687238104Sdes	int i;
688238104Sdes
689238104Sdes	if (w->enable == 0 || w->type !=
690238104Sdes	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
691238104Sdes		return;
692238104Sdes
693238104Sdes	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
694238104Sdes	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
695238104Sdes		return;
696238104Sdes
697238104Sdes	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
698238104Sdes	if ((w->eld != 0) == ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) != 0))
699238104Sdes		return;
700238104Sdes	if (w->eld != NULL) {
701238104Sdes		w->eld_len = 0;
702238104Sdes		free(w->eld, M_HDAA);
703238104Sdes		w->eld = NULL;
704238104Sdes	}
705238104Sdes	HDA_BOOTVERBOSE(
706238104Sdes		device_printf(devinfo->dev,
707238104Sdes		    "Pin sense: nid=%d sense=0x%08x "
708238104Sdes		    "(%sconnected, ELD %svalid)\n",
709238104Sdes		    w->nid, res,
710238104Sdes		    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ? "" : "dis",
711238104Sdes		    (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) ? "" : "in");
712238104Sdes	);
713238104Sdes	if ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) == 0)
714238104Sdes		return;
715238104Sdes
716238104Sdes	res = hda_command(devinfo->dev,
717238104Sdes	    HDA_CMD_GET_HDMI_DIP_SIZE(0, w->nid, 0x08));
718238104Sdes	if (res == HDA_INVALID)
719238104Sdes		return;
720238104Sdes	w->eld_len = res & 0xff;
721238104Sdes	if (w->eld_len != 0)
722238104Sdes		w->eld = malloc(w->eld_len, M_HDAA, M_ZERO | M_NOWAIT);
723238104Sdes	if (w->eld == NULL) {
724238104Sdes		w->eld_len = 0;
725238104Sdes		return;
726238104Sdes	}
727238104Sdes
728238104Sdes	for (i = 0; i < w->eld_len; i++) {
729238104Sdes		res = hda_command(devinfo->dev,
730238104Sdes		    HDA_CMD_GET_HDMI_ELDD(0, w->nid, i));
731238104Sdes		if (res & 0x80000000)
732238104Sdes			w->eld[i] = res & 0xff;
733238104Sdes	}
734238104Sdes	HDA_BOOTVERBOSE(
735238104Sdes		hdaa_eld_dump(w);
736238104Sdes	);
737238104Sdes	hdaa_channels_handler(&devinfo->as[w->bindas]);
738238104Sdes}
739238104Sdes
740238104Sdes/*
741238104Sdes * Pin sense initializer.
742238104Sdes */
743238104Sdesstatic void
744238104Sdeshdaa_sense_init(struct hdaa_devinfo *devinfo)
745238104Sdes{
746238104Sdes	struct hdaa_audio_as *as;
747238104Sdes	struct hdaa_widget *w;
748238104Sdes	int i, poll = 0;
749238104Sdes
750238104Sdes	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
751238104Sdes		w = hdaa_widget_get(devinfo, i);
752238104Sdes		if (w == NULL || w->enable == 0 || w->type !=
753238104Sdes		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
754238104Sdes			continue;
755238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap)) {
756246854Sdes			if (w->unsol < 0)
757246854Sdes				w->unsol = HDAC_UNSOL_ALLOC(
758246854Sdes				    device_get_parent(devinfo->dev),
759246854Sdes				    devinfo->dev, w->nid);
760246854Sdes			hda_command(devinfo->dev,
761246854Sdes			    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid,
762246854Sdes			    HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE | w->unsol));
763238104Sdes		}
764238104Sdes		as = &devinfo->as[w->bindas];
765238104Sdes		if (as->hpredir >= 0 && as->pins[15] == w->nid) {
766238104Sdes			if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
767238104Sdes			    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0) {
768238104Sdes				device_printf(devinfo->dev,
769238104Sdes				    "No presence detection support at nid %d\n",
770238104Sdes				    w->nid);
771238104Sdes			} else {
772238104Sdes				if (w->unsol < 0)
773238104Sdes					poll = 1;
774238104Sdes				HDA_BOOTVERBOSE(
775238104Sdes					device_printf(devinfo->dev,
776238104Sdes					    "Headphones redirection for "
777238104Sdes					    "association %d nid=%d using %s.\n",
778238104Sdes					    w->bindas, w->nid,
779238104Sdes					    (w->unsol < 0) ? "polling" :
780238104Sdes					    "unsolicited responses");
781238104Sdes				);
782238104Sdes			};
783238104Sdes		}
784238104Sdes		hdaa_presence_handler(w);
785238104Sdes		if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) &&
786238104Sdes		    !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
787238104Sdes			continue;
788238104Sdes		hdaa_eld_handler(w);
789238104Sdes	}
790238104Sdes	if (poll) {
791238104Sdes		callout_reset(&devinfo->poll_jack, 1,
792238104Sdes		    hdaa_jack_poll_callback, devinfo);
793238104Sdes	}
794238104Sdes}
795238104Sdes
796238104Sdesstatic void
797238104Sdeshdaa_sense_deinit(struct hdaa_devinfo *devinfo)
798238104Sdes{
799238104Sdes	struct hdaa_widget *w;
800238104Sdes	int i;
801238104Sdes
802238104Sdes	callout_stop(&devinfo->poll_jack);
803238104Sdes	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
804238104Sdes		w = hdaa_widget_get(devinfo, i);
805238104Sdes		if (w == NULL || w->enable == 0 || w->type !=
806238104Sdes		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
807238104Sdes			continue;
808238104Sdes		if (w->unsol < 0)
809238104Sdes			continue;
810238104Sdes		hda_command(devinfo->dev,
811238104Sdes		    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0));
812238104Sdes		HDAC_UNSOL_FREE(
813238104Sdes		    device_get_parent(devinfo->dev), devinfo->dev,
814238104Sdes		    w->unsol);
815238104Sdes		w->unsol = -1;
816238104Sdes	}
817238104Sdes}
818238104Sdes
819238104Sdesuint32_t
820238104Sdeshdaa_widget_pin_patch(uint32_t config, const char *str)
821238104Sdes{
822238104Sdes	char buf[256];
823238104Sdes	char *key, *value, *rest, *bad;
824238104Sdes	int ival, i;
825238104Sdes
826238104Sdes	strlcpy(buf, str, sizeof(buf));
827238104Sdes	rest = buf;
828238104Sdes	while ((key = strsep(&rest, "=")) != NULL) {
829238104Sdes		value = strsep(&rest, " \t");
830238104Sdes		if (value == NULL)
831238104Sdes			break;
832238104Sdes		ival = strtol(value, &bad, 10);
833238104Sdes		if (strcmp(key, "seq") == 0) {
834238104Sdes			config &= ~HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK;
835238104Sdes			config |= ((ival << HDA_CONFIG_DEFAULTCONF_SEQUENCE_SHIFT) &
836238104Sdes			    HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK);
837238104Sdes		} else if (strcmp(key, "as") == 0) {
838238104Sdes			config &= ~HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK;
839238104Sdes			config |= ((ival << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT) &
840238104Sdes			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK);
841238104Sdes		} else if (strcmp(key, "misc") == 0) {
842238104Sdes			config &= ~HDA_CONFIG_DEFAULTCONF_MISC_MASK;
843238104Sdes			config |= ((ival << HDA_CONFIG_DEFAULTCONF_MISC_SHIFT) &
844238104Sdes			    HDA_CONFIG_DEFAULTCONF_MISC_MASK);
845238104Sdes		} else if (strcmp(key, "color") == 0) {
846238104Sdes			config &= ~HDA_CONFIG_DEFAULTCONF_COLOR_MASK;
847238104Sdes			if (bad[0] == 0) {
848238104Sdes				config |= ((ival << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT) &
849238104Sdes				    HDA_CONFIG_DEFAULTCONF_COLOR_MASK);
850238104Sdes			};
851238104Sdes			for (i = 0; i < 16; i++) {
852238104Sdes				if (strcasecmp(HDA_COLORS[i], value) == 0) {
853238104Sdes					config |= (i << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT);
854238104Sdes					break;
855238104Sdes				}
856238104Sdes			}
857238104Sdes		} else if (strcmp(key, "ctype") == 0) {
858238104Sdes			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK;
859238104Sdes			if (bad[0] == 0) {
860238104Sdes			config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT) &
861238104Sdes			    HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK);
862238104Sdes			}
863238104Sdes			for (i = 0; i < 16; i++) {
864238104Sdes				if (strcasecmp(HDA_CONNECTORS[i], value) == 0) {
865238104Sdes					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT);
866238104Sdes					break;
867238104Sdes				}
868238104Sdes			}
869238104Sdes		} else if (strcmp(key, "device") == 0) {
870238104Sdes			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
871238104Sdes			if (bad[0] == 0) {
872238104Sdes				config |= ((ival << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT) &
873238104Sdes				    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK);
874238104Sdes				continue;
875238104Sdes			};
876238104Sdes			for (i = 0; i < 16; i++) {
877238104Sdes				if (strcasecmp(HDA_DEVS[i], value) == 0) {
878238104Sdes					config |= (i << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT);
879238104Sdes					break;
880238104Sdes				}
881238104Sdes			}
882238104Sdes		} else if (strcmp(key, "loc") == 0) {
883238104Sdes			config &= ~HDA_CONFIG_DEFAULTCONF_LOCATION_MASK;
884238104Sdes			if (bad[0] == 0) {
885238104Sdes				config |= ((ival << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT) &
886238104Sdes				    HDA_CONFIG_DEFAULTCONF_LOCATION_MASK);
887238104Sdes				continue;
888238104Sdes			}
889238104Sdes			for (i = 0; i < 64; i++) {
890238104Sdes				if (strcasecmp(HDA_LOCS[i], value) == 0) {
891238104Sdes					config |= (i << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT);
892238104Sdes					break;
893238104Sdes				}
894238104Sdes			}
895238104Sdes		} else if (strcmp(key, "conn") == 0) {
896238104Sdes			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
897238104Sdes			if (bad[0] == 0) {
898238104Sdes				config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT) &
899238104Sdes				    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
900238104Sdes				continue;
901238104Sdes			};
902238104Sdes			for (i = 0; i < 4; i++) {
903238104Sdes				if (strcasecmp(HDA_CONNS[i], value) == 0) {
904238104Sdes					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT);
905238104Sdes					break;
906238104Sdes				}
907238104Sdes			}
908238104Sdes		}
909238104Sdes	}
910238104Sdes	return (config);
911238104Sdes}
912238104Sdes
913238104Sdesuint32_t
914238104Sdeshdaa_gpio_patch(uint32_t gpio, const char *str)
915238104Sdes{
916238104Sdes	char buf[256];
917238104Sdes	char *key, *value, *rest;
918238104Sdes	int ikey, i;
919238104Sdes
920238104Sdes	strlcpy(buf, str, sizeof(buf));
921238104Sdes	rest = buf;
922238104Sdes	while ((key = strsep(&rest, "=")) != NULL) {
923238104Sdes		value = strsep(&rest, " \t");
924238104Sdes		if (value == NULL)
925238104Sdes			break;
926238104Sdes		ikey = strtol(key, NULL, 10);
927238104Sdes		if (ikey < 0 || ikey > 7)
928238104Sdes			continue;
929238104Sdes		for (i = 0; i < 7; i++) {
930238104Sdes			if (strcasecmp(HDA_GPIO_ACTIONS[i], value) == 0) {
931238104Sdes				gpio &= ~HDAA_GPIO_MASK(ikey);
932238104Sdes				gpio |= i << HDAA_GPIO_SHIFT(ikey);
933238104Sdes				break;
934238104Sdes			}
935238104Sdes		}
936238104Sdes	}
937238104Sdes	return (gpio);
938238104Sdes}
939238104Sdes
940238104Sdesstatic void
941238104Sdeshdaa_local_patch_pin(struct hdaa_widget *w)
942238104Sdes{
943238104Sdes	device_t dev = w->devinfo->dev;
944238104Sdes	const char *res = NULL;
945238104Sdes	uint32_t config, orig;
946238104Sdes	char buf[32];
947238104Sdes
948238104Sdes	config = orig = w->wclass.pin.config;
949238104Sdes	snprintf(buf, sizeof(buf), "cad%u.nid%u.config",
950238104Sdes	    hda_get_codec_id(dev), w->nid);
951238104Sdes	if (resource_string_value(device_get_name(
952238104Sdes	    device_get_parent(device_get_parent(dev))),
953238104Sdes	    device_get_unit(device_get_parent(device_get_parent(dev))),
954238104Sdes	    buf, &res) == 0) {
955238104Sdes		if (strncmp(res, "0x", 2) == 0) {
956238104Sdes			config = strtol(res + 2, NULL, 16);
957238104Sdes		} else {
958238104Sdes			config = hdaa_widget_pin_patch(config, res);
959238104Sdes		}
960238104Sdes	}
961238104Sdes	snprintf(buf, sizeof(buf), "nid%u.config", w->nid);
962238104Sdes	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
963238104Sdes	    buf, &res) == 0) {
964246827Sdes		if (strncmp(res, "0x", 2) == 0) {
965238104Sdes			config = strtol(res + 2, NULL, 16);
966238104Sdes		} else {
967238104Sdes			config = hdaa_widget_pin_patch(config, res);
968238104Sdes		}
969238104Sdes	}
970238104Sdes	HDA_BOOTVERBOSE(
971238104Sdes		if (config != orig)
972238104Sdes			device_printf(w->devinfo->dev,
973238104Sdes			    "Patching pin config nid=%u 0x%08x -> 0x%08x\n",
974238104Sdes			    w->nid, orig, config);
975238104Sdes	);
976238104Sdes	w->wclass.pin.newconf = w->wclass.pin.config = config;
977238104Sdes}
978238104Sdes
979238104Sdesstatic void
980238104Sdeshdaa_dump_audio_formats_sb(struct sbuf *sb, uint32_t fcap, uint32_t pcmcap)
981238104Sdes{
982238104Sdes	uint32_t cap;
983238104Sdes
984238104Sdes	cap = fcap;
985238104Sdes	if (cap != 0) {
986238104Sdes		sbuf_printf(sb, "     Stream cap: 0x%08x", cap);
987238104Sdes		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
988238104Sdes			sbuf_printf(sb, " AC3");
989238104Sdes		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
990238104Sdes			sbuf_printf(sb, " FLOAT32");
991238104Sdes		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
992238104Sdes			sbuf_printf(sb, " PCM");
993238104Sdes		sbuf_printf(sb, "\n");
994238104Sdes	}
995238104Sdes	cap = pcmcap;
996238104Sdes	if (cap != 0) {
997238104Sdes		sbuf_printf(sb, "        PCM cap: 0x%08x", cap);
998238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
999238104Sdes			sbuf_printf(sb, " 8");
1000238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
1001238104Sdes			sbuf_printf(sb, " 16");
1002238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
1003238104Sdes			sbuf_printf(sb, " 20");
1004238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
1005238104Sdes			sbuf_printf(sb, " 24");
1006238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
1007238104Sdes			sbuf_printf(sb, " 32");
1008238104Sdes		sbuf_printf(sb, " bits,");
1009238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
1010238104Sdes			sbuf_printf(sb, " 8");
1011238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
1012238104Sdes			sbuf_printf(sb, " 11");
1013238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
1014238104Sdes			sbuf_printf(sb, " 16");
1015238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
1016238104Sdes			sbuf_printf(sb, " 22");
1017238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
1018238104Sdes			sbuf_printf(sb, " 32");
1019238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
1020238104Sdes			sbuf_printf(sb, " 44");
1021238104Sdes		sbuf_printf(sb, " 48");
1022238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
1023238104Sdes			sbuf_printf(sb, " 88");
1024238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
1025238104Sdes			sbuf_printf(sb, " 96");
1026238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
1027238104Sdes			sbuf_printf(sb, " 176");
1028238104Sdes		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
1029238104Sdes			sbuf_printf(sb, " 192");
1030238104Sdes		sbuf_printf(sb, " KHz\n");
1031238104Sdes	}
1032238104Sdes}
1033238104Sdes
1034238104Sdesstatic void
1035238104Sdeshdaa_dump_pin_sb(struct sbuf *sb, struct hdaa_widget *w)
1036238104Sdes{
1037238104Sdes	uint32_t pincap, conf;
1038238104Sdes
1039238104Sdes	pincap = w->wclass.pin.cap;
1040238104Sdes
1041238104Sdes	sbuf_printf(sb, "        Pin cap: 0x%08x", pincap);
1042238104Sdes	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
1043238104Sdes		sbuf_printf(sb, " ISC");
1044238104Sdes	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
1045238104Sdes		sbuf_printf(sb, " TRQD");
1046238104Sdes	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
1047238104Sdes		sbuf_printf(sb, " PDC");
1048238104Sdes	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
1049238104Sdes		sbuf_printf(sb, " HP");
1050238104Sdes	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
1051238104Sdes		sbuf_printf(sb, " OUT");
1052238104Sdes	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
1053238104Sdes		sbuf_printf(sb, " IN");
1054238104Sdes	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
1055238104Sdes		sbuf_printf(sb, " BAL");
1056238104Sdes	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
1057238104Sdes		sbuf_printf(sb, " HDMI");
1058238104Sdes	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
1059238104Sdes		sbuf_printf(sb, " VREF[");
1060238104Sdes		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
1061238104Sdes			sbuf_printf(sb, " 50");
1062238104Sdes		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
1063238104Sdes			sbuf_printf(sb, " 80");
1064238104Sdes		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
1065238104Sdes			sbuf_printf(sb, " 100");
1066238104Sdes		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
1067238104Sdes			sbuf_printf(sb, " GROUND");
1068238104Sdes		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
1069238104Sdes			sbuf_printf(sb, " HIZ");
1070238104Sdes		sbuf_printf(sb, " ]");
1071238104Sdes	}
1072238104Sdes	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
1073238104Sdes		sbuf_printf(sb, " EAPD");
1074238104Sdes	if (HDA_PARAM_PIN_CAP_DP(pincap))
1075238104Sdes		sbuf_printf(sb, " DP");
1076238104Sdes	if (HDA_PARAM_PIN_CAP_HBR(pincap))
1077238104Sdes		sbuf_printf(sb, " HBR");
1078238104Sdes	sbuf_printf(sb, "\n");
1079238104Sdes	conf = w->wclass.pin.config;
1080238104Sdes	sbuf_printf(sb, "     Pin config: 0x%08x", conf);
1081238104Sdes	sbuf_printf(sb, " as=%d seq=%d "
1082238104Sdes	    "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d\n",
1083238104Sdes	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
1084238104Sdes	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
1085238104Sdes	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
1086238104Sdes	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
1087238104Sdes	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
1088238104Sdes	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
1089238104Sdes	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
1090238104Sdes	    HDA_CONFIG_DEFAULTCONF_MISC(conf));
1091238104Sdes	sbuf_printf(sb, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
1092238104Sdes	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
1093238104Sdes		sbuf_printf(sb, " HP");
1094238104Sdes	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
1095238104Sdes		sbuf_printf(sb, " IN");
1096238104Sdes	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
1097238104Sdes		sbuf_printf(sb, " OUT");
1098238104Sdes	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1099238104Sdes		if ((w->wclass.pin.ctrl &
1100238104Sdes		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
1101238104Sdes			sbuf_printf(sb, " HBR");
1102238104Sdes		else if ((w->wclass.pin.ctrl &
1103238104Sdes		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
1104238104Sdes			sbuf_printf(sb, " EPTs");
1105238104Sdes	} else {
1106238104Sdes		if ((w->wclass.pin.ctrl &
1107238104Sdes		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
1108238104Sdes			sbuf_printf(sb, " VREFs");
1109238104Sdes	}
1110238104Sdes	sbuf_printf(sb, "\n");
1111238104Sdes}
1112238104Sdes
1113238104Sdesstatic void
1114238104Sdeshdaa_dump_amp_sb(struct sbuf *sb, uint32_t cap, const char *banner)
1115238104Sdes{
1116238104Sdes	int offset, size, step;
1117238104Sdes
1118238104Sdes	offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap);
1119238104Sdes	size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap);
1120238104Sdes	step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap);
1121238104Sdes	sbuf_printf(sb, "     %s amp: 0x%08x "
1122238104Sdes	    "mute=%d step=%d size=%d offset=%d (%+d/%+ddB)\n",
1123238104Sdes	    banner, cap,
1124238104Sdes	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
1125238104Sdes	    step, size, offset,
1126238104Sdes	    ((0 - offset) * (size + 1)) / 4,
1127238104Sdes	    ((step - offset) * (size + 1)) / 4);
1128238104Sdes}
1129238104Sdes
1130238104Sdes
1131238104Sdesstatic int
1132238104Sdeshdaa_sysctl_caps(SYSCTL_HANDLER_ARGS)
1133238104Sdes{
1134238104Sdes	struct hdaa_devinfo *devinfo;
1135238104Sdes	struct hdaa_widget *w, *cw;
1136238104Sdes	struct sbuf sb;
1137238104Sdes	char buf[64];
1138238104Sdes	int error, j;
1139238104Sdes
1140238104Sdes	w = (struct hdaa_widget *)oidp->oid_arg1;
1141238104Sdes	devinfo = w->devinfo;
1142238104Sdes	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1143238104Sdes
1144238104Sdes	sbuf_printf(&sb, "%s%s\n", w->name,
1145238104Sdes	    (w->enable == 0) ? " [DISABLED]" : "");
1146238104Sdes	sbuf_printf(&sb, "     Widget cap: 0x%08x",
1147238104Sdes	    w->param.widget_cap);
1148238104Sdes	if (w->param.widget_cap & 0x0ee1) {
1149238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
1150238104Sdes		    sbuf_printf(&sb, " LRSWAP");
1151238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
1152238104Sdes		    sbuf_printf(&sb, " PWR");
1153238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
1154238104Sdes		    sbuf_printf(&sb, " DIGITAL");
1155238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
1156238104Sdes		    sbuf_printf(&sb, " UNSOL");
1157238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
1158238104Sdes		    sbuf_printf(&sb, " PROC");
1159238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
1160238104Sdes		    sbuf_printf(&sb, " STRIPE(x%d)",
1161238104Sdes			1 << (fls(w->wclass.conv.stripecap) - 1));
1162238104Sdes		j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1163238104Sdes		if (j == 1)
1164238104Sdes		    sbuf_printf(&sb, " STEREO");
1165238104Sdes		else if (j > 1)
1166238104Sdes		    sbuf_printf(&sb, " %dCH", j + 1);
1167238104Sdes	}
1168238104Sdes	sbuf_printf(&sb, "\n");
1169238104Sdes	if (w->bindas != -1) {
1170238104Sdes		sbuf_printf(&sb, "    Association: %d (0x%04x)\n",
1171238104Sdes		    w->bindas, w->bindseqmask);
1172238104Sdes	}
1173238104Sdes	if (w->ossmask != 0 || w->ossdev >= 0) {
1174238104Sdes		sbuf_printf(&sb, "            OSS: %s",
1175238104Sdes		    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
1176238104Sdes		if (w->ossdev >= 0)
1177238104Sdes		    sbuf_printf(&sb, " (%s)", ossnames[w->ossdev]);
1178238104Sdes		sbuf_printf(&sb, "\n");
1179238104Sdes	}
1180238104Sdes	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1181238104Sdes	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1182238104Sdes		hdaa_dump_audio_formats_sb(&sb,
1183238104Sdes		    w->param.supp_stream_formats,
1184238104Sdes		    w->param.supp_pcm_size_rate);
1185238104Sdes	} else if (w->type ==
1186238104Sdes	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
1187238104Sdes		hdaa_dump_pin_sb(&sb, w);
1188238104Sdes	if (w->param.eapdbtl != HDA_INVALID) {
1189238104Sdes		sbuf_printf(&sb, "           EAPD: 0x%08x%s%s%s\n",
1190238104Sdes		    w->param.eapdbtl,
1191238104Sdes		    (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_LR_SWAP) ?
1192238104Sdes		     " LRSWAP" : "",
1193246854Sdes		    (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD) ?
1194246854Sdes		     " EAPD" : "",
1195238104Sdes		    (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_BTL) ?
1196238104Sdes		     " BTL" : "");
1197238104Sdes	}
1198238104Sdes	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
1199238104Sdes	    w->param.outamp_cap != 0)
1200238104Sdes		hdaa_dump_amp_sb(&sb, w->param.outamp_cap, "Output");
1201238104Sdes	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
1202238104Sdes	    w->param.inamp_cap != 0)
1203238104Sdes		hdaa_dump_amp_sb(&sb, w->param.inamp_cap, " Input");
1204238104Sdes	if (w->nconns > 0)
1205238104Sdes		sbuf_printf(&sb, "    Connections: %d\n", w->nconns);
1206238104Sdes	for (j = 0; j < w->nconns; j++) {
1207238104Sdes		cw = hdaa_widget_get(devinfo, w->conns[j]);
1208238104Sdes		sbuf_printf(&sb, "          + %s<- nid=%d [%s]",
1209238104Sdes		    (w->connsenable[j] == 0)?"[DISABLED] ":"",
1210238104Sdes		    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
1211238104Sdes		if (cw == NULL)
1212238104Sdes			sbuf_printf(&sb, " [UNKNOWN]");
1213238104Sdes		else if (cw->enable == 0)
1214238104Sdes			sbuf_printf(&sb, " [DISABLED]");
1215238104Sdes		if (w->nconns > 1 && w->selconn == j && w->type !=
1216238104Sdes		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
1217238104Sdes			sbuf_printf(&sb, " (selected)");
1218238104Sdes		sbuf_printf(&sb, "\n");
1219238104Sdes	}
1220238104Sdes	error = sbuf_finish(&sb);
1221238104Sdes	sbuf_delete(&sb);
1222238104Sdes	return (error);
1223238104Sdes}
1224238104Sdes
1225238104Sdesstatic int
1226238104Sdeshdaa_sysctl_config(SYSCTL_HANDLER_ARGS)
1227238104Sdes{
1228238104Sdes	char buf[256];
1229238104Sdes	int error;
1230238104Sdes	uint32_t conf;
1231238104Sdes
1232238104Sdes	conf = *(uint32_t *)oidp->oid_arg1;
1233238104Sdes	snprintf(buf, sizeof(buf), "0x%08x as=%d seq=%d "
1234238104Sdes	    "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d",
1235238104Sdes	    conf,
1236238104Sdes	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
1237238104Sdes	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
1238238104Sdes	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
1239238104Sdes	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
1240238104Sdes	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
1241238104Sdes	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
1242238104Sdes	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
1243238104Sdes	    HDA_CONFIG_DEFAULTCONF_MISC(conf));
1244238104Sdes	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1245238104Sdes	if (error != 0 || req->newptr == NULL)
1246238104Sdes		return (error);
1247238104Sdes	if (strncmp(buf, "0x", 2) == 0)
1248238104Sdes		conf = strtol(buf + 2, NULL, 16);
1249238104Sdes	else
1250238104Sdes		conf = hdaa_widget_pin_patch(conf, buf);
1251238104Sdes	*(uint32_t *)oidp->oid_arg1 = conf;
1252238104Sdes	return (0);
1253238104Sdes}
1254238104Sdes
1255238104Sdesstatic void
1256238104Sdeshdaa_config_fetch(const char *str, uint32_t *on, uint32_t *off)
1257238104Sdes{
1258238104Sdes	int i = 0, j, k, len, inv;
1259238104Sdes
1260238104Sdes	for (;;) {
1261238104Sdes		while (str[i] != '\0' &&
1262238104Sdes		    (str[i] == ',' || isspace(str[i]) != 0))
1263238104Sdes			i++;
1264238104Sdes		if (str[i] == '\0')
1265238104Sdes			return;
1266238104Sdes		j = i;
1267238104Sdes		while (str[j] != '\0' &&
1268238104Sdes		    !(str[j] == ',' || isspace(str[j]) != 0))
1269238104Sdes			j++;
1270238104Sdes		len = j - i;
1271238104Sdes		if (len > 2 && strncmp(str + i, "no", 2) == 0)
1272238104Sdes			inv = 2;
1273238104Sdes		else
1274238104Sdes			inv = 0;
1275238104Sdes		for (k = 0; len > inv && k < nitems(hdaa_quirks_tab); k++) {
1276238104Sdes			if (strncmp(str + i + inv,
1277238104Sdes			    hdaa_quirks_tab[k].key, len - inv) != 0)
1278238104Sdes				continue;
1279238104Sdes			if (len - inv != strlen(hdaa_quirks_tab[k].key))
1280238104Sdes				continue;
1281238104Sdes			if (inv == 0) {
1282238104Sdes				*on |= hdaa_quirks_tab[k].value;
1283238104Sdes				*off &= ~hdaa_quirks_tab[k].value;
1284238104Sdes			} else {
1285238104Sdes				*off |= hdaa_quirks_tab[k].value;
1286238104Sdes				*on &= ~hdaa_quirks_tab[k].value;
1287238104Sdes			}
1288238104Sdes			break;
1289238104Sdes		}
1290238104Sdes		i = j;
1291238104Sdes	}
1292238104Sdes}
1293238104Sdes
1294238104Sdesstatic int
1295238104Sdeshdaa_sysctl_quirks(SYSCTL_HANDLER_ARGS)
1296238104Sdes{
1297238104Sdes	char buf[256];
1298238104Sdes	int error, n = 0, i;
1299238104Sdes	uint32_t quirks, quirks_off;
1300238104Sdes
1301238104Sdes	quirks = *(uint32_t *)oidp->oid_arg1;
1302238104Sdes	buf[0] = 0;
1303238104Sdes	for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
1304238104Sdes		if ((quirks & hdaa_quirks_tab[i].value) != 0)
1305238104Sdes			n += snprintf(buf + n, sizeof(buf) - n, "%s%s",
1306238104Sdes			    n != 0 ? "," : "", hdaa_quirks_tab[i].key);
1307238104Sdes	}
1308238104Sdes	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1309238104Sdes	if (error != 0 || req->newptr == NULL)
1310238104Sdes		return (error);
1311238104Sdes	if (strncmp(buf, "0x", 2) == 0)
1312238104Sdes		quirks = strtol(buf + 2, NULL, 16);
1313238104Sdes	else {
1314238104Sdes		quirks = 0;
1315238104Sdes		hdaa_config_fetch(buf, &quirks, &quirks_off);
1316238104Sdes	}
1317238104Sdes	*(uint32_t *)oidp->oid_arg1 = quirks;
1318238104Sdes	return (0);
1319238104Sdes}
1320238104Sdes
1321238104Sdesstatic void
1322238104Sdeshdaa_local_patch(struct hdaa_devinfo *devinfo)
1323238104Sdes{
1324238104Sdes	struct hdaa_widget *w;
1325238104Sdes	const char *res = NULL;
1326238104Sdes	uint32_t quirks_on = 0, quirks_off = 0, x;
1327238104Sdes	int i;
1328238104Sdes
1329238104Sdes	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1330238104Sdes		w = hdaa_widget_get(devinfo, i);
1331238104Sdes		if (w == NULL)
1332238104Sdes			continue;
1333238104Sdes		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
1334238104Sdes			hdaa_local_patch_pin(w);
1335238104Sdes	}
1336238104Sdes
1337238104Sdes	if (resource_string_value(device_get_name(devinfo->dev),
1338238104Sdes	    device_get_unit(devinfo->dev), "config", &res) == 0) {
1339238104Sdes		if (res != NULL && strlen(res) > 0)
1340238104Sdes			hdaa_config_fetch(res, &quirks_on, &quirks_off);
1341238104Sdes		devinfo->quirks |= quirks_on;
1342238104Sdes		devinfo->quirks &= ~quirks_off;
1343238104Sdes	}
1344238104Sdes	if (devinfo->newquirks == -1)
1345238104Sdes		devinfo->newquirks = devinfo->quirks;
1346238104Sdes	else
1347238104Sdes		devinfo->quirks = devinfo->newquirks;
1348238104Sdes	HDA_BOOTHVERBOSE(
1349238104Sdes		device_printf(devinfo->dev,
1350238104Sdes		    "Config options: 0x%08x\n", devinfo->quirks);
1351238104Sdes	);
1352238104Sdes
1353238104Sdes	if (resource_string_value(device_get_name(devinfo->dev),
1354238104Sdes	    device_get_unit(devinfo->dev), "gpio_config", &res) == 0) {
1355238104Sdes		if (strncmp(res, "0x", 2) == 0) {
1356238104Sdes			devinfo->gpio = strtol(res + 2, NULL, 16);
1357238104Sdes		} else {
1358238104Sdes			devinfo->gpio = hdaa_gpio_patch(devinfo->gpio, res);
1359238104Sdes		}
1360238104Sdes	}
1361238104Sdes	if (devinfo->newgpio == -1)
1362238104Sdes		devinfo->newgpio = devinfo->gpio;
1363238104Sdes	else
1364238104Sdes		devinfo->gpio = devinfo->newgpio;
1365238104Sdes	if (devinfo->newgpo == -1)
1366238104Sdes		devinfo->newgpo = devinfo->gpo;
1367238104Sdes	else
1368238104Sdes		devinfo->gpo = devinfo->newgpo;
1369238104Sdes	HDA_BOOTHVERBOSE(
1370238104Sdes		device_printf(devinfo->dev, "GPIO config options:");
1371238104Sdes		for (i = 0; i < 7; i++) {
1372238104Sdes			x = (devinfo->gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
1373238104Sdes			if (x != 0)
1374238104Sdes				printf(" %d=%s", i, HDA_GPIO_ACTIONS[x]);
1375238104Sdes		}
1376238104Sdes		printf("\n");
1377238104Sdes	);
1378238104Sdes}
1379238104Sdes
1380238104Sdesstatic void
1381238104Sdeshdaa_widget_connection_parse(struct hdaa_widget *w)
1382238104Sdes{
1383238104Sdes	uint32_t res;
1384238104Sdes	int i, j, max, ents, entnum;
1385238104Sdes	nid_t nid = w->nid;
1386238104Sdes	nid_t cnid, addcnid, prevcnid;
1387238104Sdes
1388238104Sdes	w->nconns = 0;
1389238104Sdes
1390238104Sdes	res = hda_command(w->devinfo->dev,
1391238104Sdes	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_CONN_LIST_LENGTH));
1392238104Sdes
1393238104Sdes	ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res);
1394238104Sdes
1395238104Sdes	if (ents < 1)
1396238104Sdes		return;
1397238104Sdes
1398238104Sdes	entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4;
1399238104Sdes	max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1;
1400238104Sdes	prevcnid = 0;
1401238104Sdes
1402238104Sdes#define CONN_RMASK(e)		(1 << ((32 / (e)) - 1))
1403238104Sdes#define CONN_NMASK(e)		(CONN_RMASK(e) - 1)
1404238104Sdes#define CONN_RESVAL(r, e, n)	((r) >> ((32 / (e)) * (n)))
1405238104Sdes#define CONN_RANGE(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_RMASK(e))
1406238104Sdes#define CONN_CNID(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_NMASK(e))
1407238104Sdes
1408238104Sdes	for (i = 0; i < ents; i += entnum) {
1409238104Sdes		res = hda_command(w->devinfo->dev,
1410238104Sdes		    HDA_CMD_GET_CONN_LIST_ENTRY(0, nid, i));
1411238104Sdes		for (j = 0; j < entnum; j++) {
1412238104Sdes			cnid = CONN_CNID(res, entnum, j);
1413238104Sdes			if (cnid == 0) {
1414238104Sdes				if (w->nconns < ents)
1415238104Sdes					device_printf(w->devinfo->dev,
1416238104Sdes					    "WARNING: nid=%d has zero cnid "
1417238104Sdes					    "entnum=%d j=%d index=%d "
1418238104Sdes					    "entries=%d found=%d res=0x%08x\n",
1419238104Sdes					    nid, entnum, j, i,
1420238104Sdes					    ents, w->nconns, res);
1421238104Sdes				else
1422238104Sdes					goto getconns_out;
1423246827Sdes			}
1424238104Sdes			if (cnid < w->devinfo->startnode ||
1425238104Sdes			    cnid >= w->devinfo->endnode) {
1426238104Sdes				HDA_BOOTVERBOSE(
1427238104Sdes					device_printf(w->devinfo->dev,
1428238104Sdes					    "WARNING: nid=%d has cnid outside "
1429238104Sdes					    "of the AFG range j=%d "
1430238104Sdes					    "entnum=%d index=%d res=0x%08x\n",
1431238104Sdes					    nid, j, entnum, i, res);
1432238104Sdes				);
1433238104Sdes			}
1434238104Sdes			if (CONN_RANGE(res, entnum, j) == 0)
1435238104Sdes				addcnid = cnid;
1436238104Sdes			else if (prevcnid == 0 || prevcnid >= cnid) {
1437238104Sdes				device_printf(w->devinfo->dev,
1438238104Sdes				    "WARNING: Invalid child range "
1439238104Sdes				    "nid=%d index=%d j=%d entnum=%d "
1440238104Sdes				    "prevcnid=%d cnid=%d res=0x%08x\n",
1441238104Sdes				    nid, i, j, entnum, prevcnid,
1442238104Sdes				    cnid, res);
1443238104Sdes				addcnid = cnid;
1444238104Sdes			} else
1445238104Sdes				addcnid = prevcnid + 1;
1446238104Sdes			while (addcnid <= cnid) {
1447238104Sdes				if (w->nconns > max) {
1448238104Sdes					device_printf(w->devinfo->dev,
1449238104Sdes					    "Adding %d (nid=%d): "
1450238104Sdes					    "Max connection reached! max=%d\n",
1451238104Sdes					    addcnid, nid, max + 1);
1452238104Sdes					goto getconns_out;
1453238104Sdes				}
1454238104Sdes				w->connsenable[w->nconns] = 1;
1455238104Sdes				w->conns[w->nconns++] = addcnid++;
1456238104Sdes			}
1457238104Sdes			prevcnid = cnid;
1458238104Sdes		}
1459238104Sdes	}
1460238104Sdes
1461238104Sdesgetconns_out:
1462238104Sdes	return;
1463238104Sdes}
1464238104Sdes
1465238104Sdesstatic void
1466238104Sdeshdaa_widget_parse(struct hdaa_widget *w)
1467238104Sdes{
1468238104Sdes	device_t dev = w->devinfo->dev;
1469238104Sdes	uint32_t wcap, cap;
1470238104Sdes	nid_t nid = w->nid;
1471238104Sdes	char buf[64];
1472238104Sdes
1473238104Sdes	w->param.widget_cap = wcap = hda_command(dev,
1474238104Sdes	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_AUDIO_WIDGET_CAP));
1475238104Sdes	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap);
1476238104Sdes
1477238104Sdes	hdaa_widget_connection_parse(w);
1478238104Sdes
1479238104Sdes	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) {
1480238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1481238104Sdes			w->param.outamp_cap =
1482238104Sdes			    hda_command(dev,
1483238104Sdes			    HDA_CMD_GET_PARAMETER(0, nid,
1484238104Sdes			    HDA_PARAM_OUTPUT_AMP_CAP));
1485238104Sdes		else
1486238104Sdes			w->param.outamp_cap =
1487238104Sdes			    w->devinfo->outamp_cap;
1488238104Sdes	} else
1489238104Sdes		w->param.outamp_cap = 0;
1490238104Sdes
1491238104Sdes	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) {
1492238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1493238104Sdes			w->param.inamp_cap =
1494238104Sdes			    hda_command(dev,
1495238104Sdes			    HDA_CMD_GET_PARAMETER(0, nid,
1496238104Sdes			    HDA_PARAM_INPUT_AMP_CAP));
1497238104Sdes		else
1498238104Sdes			w->param.inamp_cap =
1499238104Sdes			    w->devinfo->inamp_cap;
1500246854Sdes	} else
1501238104Sdes		w->param.inamp_cap = 0;
1502238104Sdes
1503238104Sdes	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1504238104Sdes	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1505238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) {
1506238104Sdes			cap = hda_command(dev,
1507238104Sdes			    HDA_CMD_GET_PARAMETER(0, nid,
1508238104Sdes			    HDA_PARAM_SUPP_STREAM_FORMATS));
1509238104Sdes			w->param.supp_stream_formats = (cap != 0) ? cap :
1510238104Sdes			    w->devinfo->supp_stream_formats;
1511238104Sdes			cap = hda_command(dev,
1512238104Sdes			    HDA_CMD_GET_PARAMETER(0, nid,
1513238104Sdes			    HDA_PARAM_SUPP_PCM_SIZE_RATE));
1514238104Sdes			w->param.supp_pcm_size_rate = (cap != 0) ? cap :
1515238104Sdes			    w->devinfo->supp_pcm_size_rate;
1516238104Sdes		} else {
1517238104Sdes			w->param.supp_stream_formats =
1518238104Sdes			    w->devinfo->supp_stream_formats;
1519238104Sdes			w->param.supp_pcm_size_rate =
1520238104Sdes			    w->devinfo->supp_pcm_size_rate;
1521238104Sdes		}
1522238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1523238104Sdes			w->wclass.conv.stripecap = hda_command(dev,
1524238104Sdes			    HDA_CMD_GET_STRIPE_CONTROL(0, w->nid)) >> 20;
1525238104Sdes		} else
1526238104Sdes			w->wclass.conv.stripecap = 1;
1527238104Sdes	} else {
1528238104Sdes		w->param.supp_stream_formats = 0;
1529238104Sdes		w->param.supp_pcm_size_rate = 0;
1530238104Sdes	}
1531238104Sdes
1532238104Sdes	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1533238104Sdes		w->wclass.pin.original = w->wclass.pin.newconf =
1534238104Sdes		    w->wclass.pin.config = hda_command(dev,
1535238104Sdes			HDA_CMD_GET_CONFIGURATION_DEFAULT(0, w->nid));
1536238104Sdes		w->wclass.pin.cap = hda_command(dev,
1537238104Sdes		    HDA_CMD_GET_PARAMETER(0, w->nid, HDA_PARAM_PIN_CAP));
1538238104Sdes		w->wclass.pin.ctrl = hda_command(dev,
1539238104Sdes		    HDA_CMD_GET_PIN_WIDGET_CTRL(0, nid));
1540238104Sdes		w->wclass.pin.connected = 2;
1541238104Sdes		if (HDA_PARAM_PIN_CAP_EAPD_CAP(w->wclass.pin.cap)) {
1542238104Sdes			w->param.eapdbtl = hda_command(dev,
1543238104Sdes			    HDA_CMD_GET_EAPD_BTL_ENABLE(0, nid));
1544238104Sdes			w->param.eapdbtl &= 0x7;
1545238104Sdes			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
1546238104Sdes		} else
1547238104Sdes			w->param.eapdbtl = HDA_INVALID;
1548238104Sdes	}
1549238104Sdes	w->unsol = -1;
1550238104Sdes
1551238104Sdes	hdaa_unlock(w->devinfo);
1552238104Sdes	snprintf(buf, sizeof(buf), "nid%d", w->nid);
1553238104Sdes	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1554238104Sdes	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1555238104Sdes	    buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1556238104Sdes	    w, sizeof(w), hdaa_sysctl_caps, "A", "Node capabilities");
1557238104Sdes	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1558238104Sdes		snprintf(buf, sizeof(buf), "nid%d_config", w->nid);
1559238104Sdes		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1560238104Sdes		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1561238104Sdes		    buf, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1562238104Sdes		    &w->wclass.pin.newconf, sizeof(&w->wclass.pin.newconf),
1563238104Sdes		    hdaa_sysctl_config, "A", "Current pin configuration");
1564238104Sdes		snprintf(buf, sizeof(buf), "nid%d_original", w->nid);
1565238104Sdes		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1566238104Sdes		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1567238104Sdes		    buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1568238104Sdes		    &w->wclass.pin.original, sizeof(&w->wclass.pin.original),
1569238104Sdes		    hdaa_sysctl_config, "A", "Original pin configuration");
1570238104Sdes	}
1571238104Sdes	hdaa_lock(w->devinfo);
1572238104Sdes}
1573238104Sdes
1574238104Sdesstatic void
1575238104Sdeshdaa_widget_postprocess(struct hdaa_widget *w)
1576238104Sdes{
1577238104Sdes	const char *typestr;
1578238104Sdes
1579238104Sdes	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(w->param.widget_cap);
1580238104Sdes	switch (w->type) {
1581238104Sdes	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
1582238104Sdes		typestr = "audio output";
1583238104Sdes		break;
1584238104Sdes	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
1585238104Sdes		typestr = "audio input";
1586238104Sdes		break;
1587238104Sdes	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
1588238104Sdes		typestr = "audio mixer";
1589238104Sdes		break;
1590238104Sdes	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
1591238104Sdes		typestr = "audio selector";
1592238104Sdes		break;
1593238104Sdes	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
1594238104Sdes		typestr = "pin";
1595238104Sdes		break;
1596238104Sdes	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET:
1597238104Sdes		typestr = "power widget";
1598238104Sdes		break;
1599238104Sdes	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET:
1600238104Sdes		typestr = "volume widget";
1601238104Sdes		break;
1602238104Sdes	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
1603238104Sdes		typestr = "beep widget";
1604238104Sdes		break;
1605238104Sdes	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET:
1606238104Sdes		typestr = "vendor widget";
1607238104Sdes		break;
1608238104Sdes	default:
1609238104Sdes		typestr = "unknown type";
1610238104Sdes		break;
1611238104Sdes	}
1612238104Sdes	strlcpy(w->name, typestr, sizeof(w->name));
1613238104Sdes
1614238104Sdes	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1615238104Sdes		uint32_t config;
1616238104Sdes		const char *devstr;
1617238104Sdes		int conn, color;
1618238104Sdes
1619238104Sdes		config = w->wclass.pin.config;
1620238104Sdes		devstr = HDA_DEVS[(config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) >>
1621238104Sdes		    HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT];
1622238104Sdes		conn = (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) >>
1623238104Sdes		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT;
1624238104Sdes		color = (config & HDA_CONFIG_DEFAULTCONF_COLOR_MASK) >>
1625238104Sdes		    HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT;
1626238104Sdes		strlcat(w->name, ": ", sizeof(w->name));
1627238104Sdes		strlcat(w->name, devstr, sizeof(w->name));
1628238104Sdes		strlcat(w->name, " (", sizeof(w->name));
1629238104Sdes		if (conn == 0 && color != 0 && color != 15) {
1630238104Sdes			strlcat(w->name, HDA_COLORS[color], sizeof(w->name));
1631238104Sdes			strlcat(w->name, " ", sizeof(w->name));
1632238104Sdes		}
1633238104Sdes		strlcat(w->name, HDA_CONNS[conn], sizeof(w->name));
1634238104Sdes		strlcat(w->name, ")", sizeof(w->name));
1635238104Sdes	}
1636238104Sdes}
1637238104Sdes
1638238104Sdesstruct hdaa_widget *
1639238104Sdeshdaa_widget_get(struct hdaa_devinfo *devinfo, nid_t nid)
1640238104Sdes{
1641238104Sdes	if (devinfo == NULL || devinfo->widget == NULL ||
1642238104Sdes		    nid < devinfo->startnode || nid >= devinfo->endnode)
1643238104Sdes		return (NULL);
1644238104Sdes	return (&devinfo->widget[nid - devinfo->startnode]);
1645238104Sdes}
1646238104Sdes
1647238104Sdesstatic void
1648238104Sdeshdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *devinfo, nid_t nid,
1649238104Sdes					int index, int lmute, int rmute,
1650238104Sdes					int left, int right, int dir)
1651238104Sdes{
1652238104Sdes	uint16_t v = 0;
1653238104Sdes
1654238104Sdes	HDA_BOOTHVERBOSE(
1655238104Sdes		device_printf(devinfo->dev,
1656238104Sdes		    "Setting amplifier nid=%d index=%d %s mute=%d/%d vol=%d/%d\n",
1657238104Sdes		    nid,index,dir ? "in" : "out",lmute,rmute,left,right);
1658238104Sdes	);
1659238104Sdes	if (left != right || lmute != rmute) {
1660238104Sdes		v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
1661238104Sdes		    (lmute << 7) | left;
1662238104Sdes		hda_command(devinfo->dev,
1663238104Sdes		    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1664238104Sdes		v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
1665238104Sdes		    (rmute << 7) | right;
1666238104Sdes	} else
1667238104Sdes		v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
1668238104Sdes		    (lmute << 7) | left;
1669238104Sdes
1670238104Sdes	hda_command(devinfo->dev,
1671238104Sdes	    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1672238104Sdes}
1673238104Sdes
1674238104Sdesstatic void
1675238104Sdeshdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *ctl, uint32_t mute,
1676238104Sdes						int left, int right)
1677238104Sdes{
1678238104Sdes	nid_t nid;
1679238104Sdes	int lmute, rmute;
1680238104Sdes
1681238104Sdes	nid = ctl->widget->nid;
1682238104Sdes
1683238104Sdes	/* Save new values if valid. */
1684238104Sdes	if (mute != HDAA_AMP_MUTE_DEFAULT)
1685238104Sdes		ctl->muted = mute;
1686238104Sdes	if (left != HDAA_AMP_VOL_DEFAULT)
1687238104Sdes		ctl->left = left;
1688238104Sdes	if (right != HDAA_AMP_VOL_DEFAULT)
1689238104Sdes		ctl->right = right;
1690238104Sdes	/* Prepare effective values */
1691238104Sdes	if (ctl->forcemute) {
1692238104Sdes		lmute = 1;
1693238104Sdes		rmute = 1;
1694238104Sdes		left = 0;
1695238104Sdes		right = 0;
1696238104Sdes	} else {
1697238104Sdes		lmute = HDAA_AMP_LEFT_MUTED(ctl->muted);
1698238104Sdes		rmute = HDAA_AMP_RIGHT_MUTED(ctl->muted);
1699238104Sdes		left = ctl->left;
1700238104Sdes		right = ctl->right;
1701238104Sdes	}
1702238104Sdes	/* Apply effective values */
1703238104Sdes	if (ctl->dir & HDAA_CTL_OUT)
1704238104Sdes		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1705238104Sdes		    lmute, rmute, left, right, 0);
1706238104Sdes	if (ctl->dir & HDAA_CTL_IN)
1707238104Sdes		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1708238104Sdes		    lmute, rmute, left, right, 1);
1709238104Sdes}
1710238104Sdes
1711238104Sdesstatic void
1712238104Sdeshdaa_widget_connection_select(struct hdaa_widget *w, uint8_t index)
1713238104Sdes{
1714238104Sdes	if (w == NULL || w->nconns < 1 || index > (w->nconns - 1))
1715238104Sdes		return;
1716238104Sdes	HDA_BOOTHVERBOSE(
1717238104Sdes		device_printf(w->devinfo->dev,
1718238104Sdes		    "Setting selector nid=%d index=%d\n", w->nid, index);
1719238104Sdes	);
1720238104Sdes	hda_command(w->devinfo->dev,
1721238104Sdes	    HDA_CMD_SET_CONNECTION_SELECT_CONTROL(0, w->nid, index));
1722238104Sdes	w->selconn = index;
1723238104Sdes}
1724238104Sdes
1725238104Sdes/****************************************************************************
1726238104Sdes * Device Methods
1727238104Sdes ****************************************************************************/
1728238104Sdes
1729238104Sdesstatic void *
1730238104Sdeshdaa_channel_init(kobj_t obj, void *data, struct snd_dbuf *b,
1731238104Sdes					struct pcm_channel *c, int dir)
1732238104Sdes{
1733238104Sdes	struct hdaa_chan *ch = data;
1734238104Sdes	struct hdaa_pcm_devinfo *pdevinfo = ch->pdevinfo;
1735238104Sdes	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1736238104Sdes
1737238104Sdes	hdaa_lock(devinfo);
1738238104Sdes	if (devinfo->quirks & HDAA_QUIRK_FIXEDRATE) {
1739238104Sdes		ch->caps.minspeed = ch->caps.maxspeed = 48000;
1740238104Sdes		ch->pcmrates[0] = 48000;
1741238104Sdes		ch->pcmrates[1] = 0;
1742238104Sdes	}
1743238104Sdes	ch->dir = dir;
1744238104Sdes	ch->b = b;
1745238104Sdes	ch->c = c;
1746238104Sdes	ch->blksz = pdevinfo->chan_size / pdevinfo->chan_blkcnt;
1747238104Sdes	ch->blkcnt = pdevinfo->chan_blkcnt;
1748238104Sdes	hdaa_unlock(devinfo);
1749238104Sdes
1750238104Sdes	if (sndbuf_alloc(ch->b, bus_get_dma_tag(devinfo->dev),
1751238104Sdes	    hda_get_dma_nocache(devinfo->dev) ? BUS_DMA_NOCACHE : 0,
1752238104Sdes	    pdevinfo->chan_size) != 0)
1753238104Sdes		return (NULL);
1754238104Sdes
1755238104Sdes	return (ch);
1756238104Sdes}
1757238104Sdes
1758238104Sdesstatic int
1759238104Sdeshdaa_channel_setformat(kobj_t obj, void *data, uint32_t format)
1760238104Sdes{
1761238104Sdes	struct hdaa_chan *ch = data;
1762238104Sdes	int i;
1763238104Sdes
1764238104Sdes	for (i = 0; ch->caps.fmtlist[i] != 0; i++) {
1765238104Sdes		if (format == ch->caps.fmtlist[i]) {
1766238104Sdes			ch->fmt = format;
1767238104Sdes			return (0);
1768238104Sdes		}
1769238104Sdes	}
1770238104Sdes
1771238104Sdes	return (EINVAL);
1772238104Sdes}
1773238104Sdes
1774238104Sdesstatic uint32_t
1775238104Sdeshdaa_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
1776238104Sdes{
1777238104Sdes	struct hdaa_chan *ch = data;
1778238104Sdes	uint32_t spd = 0, threshold;
1779238104Sdes	int i;
1780238104Sdes
1781238104Sdes	/* First look for equal or multiple frequency. */
1782238104Sdes	for (i = 0; ch->pcmrates[i] != 0; i++) {
1783238104Sdes		spd = ch->pcmrates[i];
1784238104Sdes		if (speed != 0 && spd / speed * speed == spd) {
1785238104Sdes			ch->spd = spd;
1786238104Sdes			return (spd);
1787238104Sdes		}
1788238104Sdes	}
1789238104Sdes	/* If no match, just find nearest. */
1790238104Sdes	for (i = 0; ch->pcmrates[i] != 0; i++) {
1791238104Sdes		spd = ch->pcmrates[i];
1792238104Sdes		threshold = spd + ((ch->pcmrates[i + 1] != 0) ?
1793238104Sdes		    ((ch->pcmrates[i + 1] - spd) >> 1) : 0);
1794238104Sdes		if (speed < threshold)
1795238104Sdes			break;
1796238104Sdes	}
1797238104Sdes	ch->spd = spd;
1798238104Sdes	return (spd);
1799238104Sdes}
1800238104Sdes
1801238104Sdesstatic uint16_t
1802238104Sdeshdaa_stream_format(struct hdaa_chan *ch)
1803238104Sdes{
1804238104Sdes	int i;
1805238104Sdes	uint16_t fmt;
1806238104Sdes
1807238104Sdes	fmt = 0;
1808238104Sdes	if (ch->fmt & AFMT_S16_LE)
1809238104Sdes		fmt |= ch->bit16 << 4;
1810238104Sdes	else if (ch->fmt & AFMT_S32_LE)
1811238104Sdes		fmt |= ch->bit32 << 4;
1812238104Sdes	else
1813238104Sdes		fmt |= 1 << 4;
1814238104Sdes	for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
1815238104Sdes		if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
1816238104Sdes			fmt |= hda_rate_tab[i].base;
1817238104Sdes			fmt |= hda_rate_tab[i].mul;
1818238104Sdes			fmt |= hda_rate_tab[i].div;
1819238104Sdes			break;
1820238104Sdes		}
1821238104Sdes	}
1822238104Sdes	fmt |= (AFMT_CHANNEL(ch->fmt) - 1);
1823238104Sdes
1824238104Sdes	return (fmt);
1825238104Sdes}
1826238104Sdes
1827238104Sdesstatic int
1828238104Sdeshdaa_allowed_stripes(uint16_t fmt)
1829238104Sdes{
1830238104Sdes	static const int bits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1831238104Sdes	int size;
1832238104Sdes
1833238104Sdes	size = bits[(fmt >> 4) & 0x03];
1834238104Sdes	size *= (fmt & 0x0f) + 1;
1835238104Sdes	size *= ((fmt >> 11) & 0x07) + 1;
1836238104Sdes	return (0xffffffffU >> (32 - fls(size / 8)));
1837238104Sdes}
1838238104Sdes
1839238104Sdesstatic void
1840238104Sdeshdaa_audio_setup(struct hdaa_chan *ch)
1841238104Sdes{
1842238104Sdes	struct hdaa_audio_as *as = &ch->devinfo->as[ch->as];
1843238104Sdes	struct hdaa_widget *w, *wp;
1844238104Sdes	int i, j, k, chn, cchn, totalchn, totalextchn, c;
1845238104Sdes	uint16_t fmt, dfmt;
1846238104Sdes	/* Mapping channel pairs to codec pins/converters. */
1847238104Sdes	const static uint16_t convmap[2][5] =
1848238104Sdes	    /*  1.0     2.0     4.0     5.1     7.1  */
1849238104Sdes	    {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x4231 },	/* no dup. */
1850238104Sdes	     { 0x0010, 0x0001, 0x2201, 0x2231, 0x4231 }};	/* side dup. */
1851238104Sdes	/* Mapping formats to HDMI channel allocations. */
1852238104Sdes	const static uint8_t hdmica[2][8] =
1853238104Sdes	    /*  1     2     3     4     5     6     7     8  */
1854238104Sdes	    {{ 0x02, 0x00, 0x04, 0x08, 0x0a, 0x0e, 0x12, 0x12 }, /* x.0 */
1855238104Sdes	     { 0x01, 0x03, 0x01, 0x03, 0x09, 0x0b, 0x0f, 0x13 }}; /* x.1 */
1856238104Sdes	/* Mapping formats to HDMI channels order. */
1857238104Sdes	const static uint32_t hdmich[2][8] =
1858238104Sdes	    /*  1  /  5     2  /  6     3  /  7     4  /  8  */
1859238104Sdes	    {{ 0xFFFF0F00, 0xFFFFFF10, 0xFFF2FF10, 0xFF32FF10,
1860238104Sdes	       0xFF324F10, 0xF5324F10, 0x54326F10, 0x54326F10 }, /* x.0 */
1861238104Sdes	     { 0xFFFFF000, 0xFFFF0100, 0xFFFFF210, 0xFFFF2310,
1862238104Sdes	       0xFF32F410, 0xFF324510, 0xF6324510, 0x76325410 }}; /* x.1 */
1863238104Sdes	int convmapid = -1;
1864238104Sdes	nid_t nid;
1865238104Sdes	uint8_t csum;
1866238104Sdes
1867238104Sdes	totalchn = AFMT_CHANNEL(ch->fmt);
1868238104Sdes	totalextchn = AFMT_EXTCHANNEL(ch->fmt);
1869238104Sdes	HDA_BOOTHVERBOSE(
1870238104Sdes		device_printf(ch->pdevinfo->dev,
1871238104Sdes		    "PCMDIR_%s: Stream setup fmt=%08x (%d.%d) speed=%d\n",
1872238104Sdes		    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1873269257Sdes		    ch->fmt, totalchn - totalextchn, totalextchn, ch->spd);
1874238104Sdes	);
1875238104Sdes	fmt = hdaa_stream_format(ch);
1876238104Sdes
1877238104Sdes	/* Set channels to I/O converters mapping for known speaker setups. */
1878238104Sdes	if ((as->pinset == 0x0007 || as->pinset == 0x0013) || /* Standard 5.1 */
1879238104Sdes	    (as->pinset == 0x0017)) /* Standard 7.1 */
1880238104Sdes		convmapid = (ch->dir == PCMDIR_PLAY);
1881238104Sdes
1882238104Sdes	dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN;
1883238104Sdes	if (ch->fmt & AFMT_AC3)
1884238104Sdes		dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO;
1885238104Sdes
1886238104Sdes	chn = 0;
1887238104Sdes	for (i = 0; ch->io[i] != -1; i++) {
1888238104Sdes		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1889238104Sdes		if (w == NULL)
1890238104Sdes			continue;
1891238104Sdes
1892238104Sdes		/* If HP redirection is enabled, but failed to use same
1893238104Sdes		   DAC, make last DAC to duplicate first one. */
1894238104Sdes		if (as->fakeredir && i == (as->pincnt - 1)) {
1895238104Sdes			c = (ch->sid << 4);
1896238104Sdes		} else {
1897238104Sdes			/* Map channels to I/O converters, if set. */
1898238104Sdes			if (convmapid >= 0)
1899238104Sdes				chn = (((convmap[convmapid][totalchn / 2]
1900238104Sdes				    >> i * 4) & 0xf) - 1) * 2;
1901238104Sdes			if (chn < 0 || chn >= totalchn) {
1902238104Sdes				c = 0;
1903238104Sdes			} else {
1904238104Sdes				c = (ch->sid << 4) | chn;
1905238104Sdes			}
1906238104Sdes		}
1907238104Sdes		hda_command(ch->devinfo->dev,
1908238104Sdes		    HDA_CMD_SET_CONV_FMT(0, ch->io[i], fmt));
1909238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1910238104Sdes			hda_command(ch->devinfo->dev,
1911238104Sdes			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], dfmt));
1912238104Sdes		}
1913238104Sdes		hda_command(ch->devinfo->dev,
1914238104Sdes		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i], c));
1915238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1916238104Sdes			hda_command(ch->devinfo->dev,
1917238104Sdes			    HDA_CMD_SET_STRIPE_CONTROL(0, w->nid, ch->stripectl));
1918238104Sdes		}
1919238104Sdes		cchn = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1920238104Sdes		if (cchn > 1 && chn < totalchn) {
1921238104Sdes			cchn = min(cchn, totalchn - chn - 1);
1922238104Sdes			hda_command(ch->devinfo->dev,
1923238104Sdes			    HDA_CMD_SET_CONV_CHAN_COUNT(0, ch->io[i], cchn));
1924238104Sdes		}
1925238104Sdes		HDA_BOOTHVERBOSE(
1926238104Sdes			device_printf(ch->pdevinfo->dev,
1927238104Sdes			    "PCMDIR_%s: Stream setup nid=%d: "
1928238104Sdes			    "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x, "
1929238104Sdes			    "chan_count=0x%02x, stripe=%d\n",
1930238104Sdes			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1931238104Sdes			    ch->io[i], fmt, dfmt, c, cchn, ch->stripectl);
1932238104Sdes		);
1933238104Sdes		for (j = 0; j < 16; j++) {
1934238104Sdes			if (as->dacs[ch->asindex][j] != ch->io[i])
1935238104Sdes				continue;
1936238104Sdes			nid = as->pins[j];
1937238104Sdes			wp = hdaa_widget_get(ch->devinfo, nid);
1938238104Sdes			if (wp == NULL)
1939238104Sdes				continue;
1940238104Sdes			if (!HDA_PARAM_PIN_CAP_DP(wp->wclass.pin.cap) &&
1941238104Sdes			    !HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap))
1942238104Sdes				continue;
1943238104Sdes
1944238104Sdes			/* Set channel mapping. */
1945238104Sdes			for (k = 0; k < 8; k++) {
1946238104Sdes				hda_command(ch->devinfo->dev,
1947238104Sdes				    HDA_CMD_SET_HDMI_CHAN_SLOT(0, nid,
1948238104Sdes				    (((hdmich[totalextchn == 0 ? 0 : 1][totalchn - 1]
1949238104Sdes				     >> (k * 4)) & 0xf) << 4) | k));
1950238104Sdes			}
1951269257Sdes
1952269257Sdes			/*
1953269257Sdes			 * Enable High Bit Rate (HBR) Encoded Packet Type
1954269257Sdes			 * (EPT), if supported and needed (8ch data).
1955269257Sdes			 */
1956269257Sdes			if (HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap) &&
1957269257Sdes			    HDA_PARAM_PIN_CAP_HBR(wp->wclass.pin.cap)) {
1958269257Sdes				wp->wclass.pin.ctrl &=
1959269257Sdes				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK;
1960238104Sdes				if ((ch->fmt & AFMT_AC3) && (cchn == 7))
1961238104Sdes					wp->wclass.pin.ctrl |= 0x03;
1962238104Sdes				hda_command(ch->devinfo->dev,
1963238104Sdes				    HDA_CMD_SET_PIN_WIDGET_CTRL(0, nid,
1964238104Sdes				    wp->wclass.pin.ctrl));
1965238104Sdes			}
1966238104Sdes
1967238104Sdes			/* Stop audio infoframe transmission. */
1968238104Sdes			hda_command(ch->devinfo->dev,
1969246854Sdes			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1970246854Sdes			hda_command(ch->devinfo->dev,
1971246854Sdes			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0x00));
1972246854Sdes
1973246854Sdes			/* Clear audio infoframe buffer. */
1974246854Sdes			hda_command(ch->devinfo->dev,
1975269257Sdes			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1976269257Sdes			for (k = 0; k < 32; k++)
1977269257Sdes				hda_command(ch->devinfo->dev,
1978269257Sdes				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1979269257Sdes
1980269257Sdes			/* Write HDMI/DisplayPort audio infoframe. */
1981269257Sdes			hda_command(ch->devinfo->dev,
1982269257Sdes			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1983269257Sdes			if (w->eld != NULL && w->eld_len >= 6 &&
1984269257Sdes			    ((w->eld[5] >> 2) & 0x3) == 1) { /* DisplayPort */
1985269257Sdes				hda_command(ch->devinfo->dev,
1986269257Sdes				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1987269257Sdes				hda_command(ch->devinfo->dev,
1988269257Sdes				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x1b));
1989269257Sdes				hda_command(ch->devinfo->dev,
1990269257Sdes				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x44));
1991269257Sdes			} else {	/* HDMI */
1992269257Sdes				hda_command(ch->devinfo->dev,
1993269257Sdes				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1994269257Sdes				hda_command(ch->devinfo->dev,
1995269257Sdes				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x01));
1996269257Sdes				hda_command(ch->devinfo->dev,
1997269257Sdes				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x0a));
1998269257Sdes				csum = 0;
1999269257Sdes				csum -= 0x84 + 0x01 + 0x0a + (totalchn - 1) +
2000269257Sdes				    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1];
2001269257Sdes				hda_command(ch->devinfo->dev,
2002269257Sdes				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, csum));
2003269257Sdes			}
2004269257Sdes			hda_command(ch->devinfo->dev,
2005269257Sdes			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, totalchn - 1));
2006269257Sdes			hda_command(ch->devinfo->dev,
2007269257Sdes			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
2008269257Sdes			hda_command(ch->devinfo->dev,
2009269257Sdes			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
2010269257Sdes			hda_command(ch->devinfo->dev,
2011269257Sdes			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid,
2012238104Sdes			    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1]));
2013238104Sdes
2014238104Sdes			/* Start audio infoframe transmission. */
2015238104Sdes			hda_command(ch->devinfo->dev,
2016238104Sdes			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
2017238104Sdes			hda_command(ch->devinfo->dev,
2018238104Sdes			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0xc0));
2019238104Sdes		}
2020238104Sdes		chn += cchn + 1;
2021238104Sdes	}
2022238104Sdes}
2023238104Sdes
2024238104Sdes/*
2025238104Sdes * Greatest Common Divisor.
2026238104Sdes */
2027238104Sdesstatic unsigned
2028238104Sdesgcd(unsigned a, unsigned b)
2029238104Sdes{
2030238104Sdes	u_int c;
2031238104Sdes
2032238104Sdes	while (b != 0) {
2033238104Sdes		c = a;
2034238104Sdes		a = b;
2035238104Sdes		b = (c % b);
2036238104Sdes	}
2037238104Sdes	return (a);
2038238104Sdes}
2039238104Sdes
2040238104Sdes/*
2041238104Sdes * Least Common Multiple.
2042238104Sdes */
2043238104Sdesstatic unsigned
2044238104Sdeslcm(unsigned a, unsigned b)
2045238104Sdes{
2046238104Sdes
2047238104Sdes	return ((a * b) / gcd(a, b));
2048238104Sdes}
2049238104Sdes
2050238104Sdesstatic int
2051238104Sdeshdaa_channel_setfragments(kobj_t obj, void *data,
2052238104Sdes					uint32_t blksz, uint32_t blkcnt)
2053238104Sdes{
2054238104Sdes	struct hdaa_chan *ch = data;
2055238104Sdes
2056238104Sdes	blksz -= blksz % lcm(HDA_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
2057238104Sdes
2058238104Sdes	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
2059238104Sdes		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
2060238104Sdes	if (blksz < HDA_BLK_MIN)
2061238104Sdes		blksz = HDA_BLK_MIN;
2062238104Sdes	if (blkcnt > HDA_BDL_MAX)
2063238104Sdes		blkcnt = HDA_BDL_MAX;
2064238104Sdes	if (blkcnt < HDA_BDL_MIN)
2065238104Sdes		blkcnt = HDA_BDL_MIN;
2066238104Sdes
2067238104Sdes	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
2068238104Sdes		if ((blkcnt >> 1) >= HDA_BDL_MIN)
2069238104Sdes			blkcnt >>= 1;
2070238104Sdes		else if ((blksz >> 1) >= HDA_BLK_MIN)
2071238104Sdes			blksz >>= 1;
2072238104Sdes		else
2073238104Sdes			break;
2074238104Sdes	}
2075269257Sdes
2076238104Sdes	if ((sndbuf_getblksz(ch->b) != blksz ||
2077238104Sdes	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
2078238104Sdes	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
2079238104Sdes		device_printf(ch->devinfo->dev, "%s: failed blksz=%u blkcnt=%u\n",
2080238104Sdes		    __func__, blksz, blkcnt);
2081238104Sdes
2082238104Sdes	ch->blksz = sndbuf_getblksz(ch->b);
2083238104Sdes	ch->blkcnt = sndbuf_getblkcnt(ch->b);
2084238104Sdes
2085238104Sdes	return (0);
2086238104Sdes}
2087238104Sdes
2088238104Sdesstatic uint32_t
2089238104Sdeshdaa_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
2090238104Sdes{
2091238104Sdes	struct hdaa_chan *ch = data;
2092238104Sdes
2093238104Sdes	hdaa_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
2094238104Sdes
2095238104Sdes	return (ch->blksz);
2096238104Sdes}
2097238104Sdes
2098238104Sdesstatic void
2099238104Sdeshdaa_channel_stop(struct hdaa_chan *ch)
2100238104Sdes{
2101238104Sdes	struct hdaa_devinfo *devinfo = ch->devinfo;
2102238104Sdes	struct hdaa_widget *w;
2103238104Sdes	int i;
2104238104Sdes
2105238104Sdes	if ((ch->flags & HDAA_CHN_RUNNING) == 0)
2106238104Sdes		return;
2107238104Sdes	ch->flags &= ~HDAA_CHN_RUNNING;
2108238104Sdes	HDAC_STREAM_STOP(device_get_parent(devinfo->dev), devinfo->dev,
2109238104Sdes	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2110238104Sdes	for (i = 0; ch->io[i] != -1; i++) {
2111238104Sdes		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
2112238104Sdes		if (w == NULL)
2113238104Sdes			continue;
2114238104Sdes		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
2115269257Sdes			hda_command(devinfo->dev,
2116238104Sdes			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], 0));
2117238104Sdes		}
2118238104Sdes		hda_command(devinfo->dev,
2119246854Sdes		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i],
2120238104Sdes		    0));
2121238104Sdes	}
2122238104Sdes	HDAC_STREAM_FREE(device_get_parent(devinfo->dev), devinfo->dev,
2123246854Sdes	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2124238104Sdes}
2125246854Sdes
2126246854Sdesstatic int
2127238104Sdeshdaa_channel_start(struct hdaa_chan *ch)
2128238104Sdes{
2129269257Sdes	struct hdaa_devinfo *devinfo = ch->devinfo;
2130269257Sdes	uint32_t fmt;
2131269257Sdes
2132269257Sdes	fmt = hdaa_stream_format(ch);
2133269257Sdes	ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt)) - 1;
2134269257Sdes	ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev,
2135269257Sdes	    ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos);
2136269257Sdes	if (ch->sid <= 0)
2137269257Sdes		return (EBUSY);
2138269257Sdes	hdaa_audio_setup(ch);
2139269257Sdes	HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev,
2140269257Sdes	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2141238104Sdes	HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev,
2142269257Sdes	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid,
2143269257Sdes	    sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt);
2144269257Sdes	ch->flags |= HDAA_CHN_RUNNING;
2145269257Sdes	return (0);
2146269257Sdes}
2147238104Sdes
2148269257Sdesstatic int
2149238104Sdeshdaa_channel_trigger(kobj_t obj, void *data, int go)
2150269257Sdes{
2151269257Sdes	struct hdaa_chan *ch = data;
2152269257Sdes	int error = 0;
2153269257Sdes
2154269257Sdes	if (!PCMTRIG_COMMON(go))
2155269257Sdes		return (0);
2156238104Sdes
2157269257Sdes	hdaa_lock(ch->devinfo);
2158269257Sdes	switch (go) {
2159238104Sdes	case PCMTRIG_START:
2160238104Sdes		error = hdaa_channel_start(ch);
2161238104Sdes		break;
2162238104Sdes	case PCMTRIG_STOP:
2163238104Sdes	case PCMTRIG_ABORT:
2164238104Sdes		hdaa_channel_stop(ch);
2165238104Sdes		break;
2166238104Sdes	default:
2167238104Sdes		break;
2168238104Sdes	}
2169238104Sdes	hdaa_unlock(ch->devinfo);
2170238104Sdes
2171238104Sdes	return (error);
2172238104Sdes}
2173238104Sdes
2174238104Sdesstatic uint32_t
2175238104Sdeshdaa_channel_getptr(kobj_t obj, void *data)
2176238104Sdes{
2177238104Sdes	struct hdaa_chan *ch = data;
2178238104Sdes	struct hdaa_devinfo *devinfo = ch->devinfo;
2179238104Sdes	uint32_t ptr;
2180238104Sdes
2181238104Sdes	hdaa_lock(devinfo);
2182238104Sdes	if (ch->dmapos != NULL) {
2183238104Sdes		ptr = *(ch->dmapos);
2184238104Sdes	} else {
2185238104Sdes		ptr = HDAC_STREAM_GETPTR(
2186238104Sdes		    device_get_parent(devinfo->dev), devinfo->dev,
2187238104Sdes		    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2188238104Sdes	}
2189238104Sdes	hdaa_unlock(devinfo);
2190238104Sdes
2191238104Sdes	/*
2192238104Sdes	 * Round to available space and force 128 bytes aligment.
2193238104Sdes	 */
2194238104Sdes	ptr %= ch->blksz * ch->blkcnt;
2195238104Sdes	ptr &= HDA_BLK_ALIGN;
2196238104Sdes
2197238104Sdes	return (ptr);
2198269257Sdes}
2199269257Sdes
2200269257Sdesstatic struct pcmchan_caps *
2201269257Sdeshdaa_channel_getcaps(kobj_t obj, void *data)
2202269257Sdes{
2203238104Sdes	return (&((struct hdaa_chan *)data)->caps);
2204269257Sdes}
2205238104Sdes
2206269257Sdesstatic kobj_method_t hdaa_channel_methods[] = {
2207238104Sdes	KOBJMETHOD(channel_init,		hdaa_channel_init),
2208269257Sdes	KOBJMETHOD(channel_setformat,		hdaa_channel_setformat),
2209238104Sdes	KOBJMETHOD(channel_setspeed,		hdaa_channel_setspeed),
2210269257Sdes	KOBJMETHOD(channel_setblocksize,	hdaa_channel_setblocksize),
2211269257Sdes	KOBJMETHOD(channel_setfragments,	hdaa_channel_setfragments),
2212269257Sdes	KOBJMETHOD(channel_trigger,		hdaa_channel_trigger),
2213269257Sdes	KOBJMETHOD(channel_getptr,		hdaa_channel_getptr),
2214269257Sdes	KOBJMETHOD(channel_getcaps,		hdaa_channel_getcaps),
2215269257Sdes	KOBJMETHOD_END
2216269257Sdes};
2217269257SdesCHANNEL_DECLARE(hdaa_channel);
2218269257Sdes
2219269257Sdesstatic int
2220269257Sdeshdaa_audio_ctl_ossmixer_init(struct snd_mixer *m)
2221269257Sdes{
2222269257Sdes	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2223269257Sdes	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2224238104Sdes	struct hdaa_widget *w, *cw;
2225238104Sdes	uint32_t mask, recmask;
2226238104Sdes	int i, j;
2227238104Sdes
2228238104Sdes	hdaa_lock(devinfo);
2229238104Sdes	pdevinfo->mixer = m;
2230238104Sdes
2231238104Sdes	/* Make sure that in case of soft volume it won't stay muted. */
2232238104Sdes	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2233238104Sdes		pdevinfo->left[i] = 100;
2234238104Sdes		pdevinfo->right[i] = 100;
2235238104Sdes	}
2236238104Sdes
2237238104Sdes	/* Declare volume controls assigned to this association. */
2238238104Sdes	mask = pdevinfo->ossmask;
2239238104Sdes	if (pdevinfo->playas >= 0) {
2240238104Sdes		/* Declate EAPD as ogain control. */
2241238104Sdes		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2242238104Sdes			w = hdaa_widget_get(devinfo, i);
2243238104Sdes			if (w == NULL || w->enable == 0)
2244238104Sdes				continue;
2245238104Sdes			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2246238104Sdes			    w->param.eapdbtl == HDA_INVALID ||
2247238104Sdes			    w->bindas != pdevinfo->playas)
2248238104Sdes				continue;
2249238104Sdes			mask |= SOUND_MASK_OGAIN;
2250238104Sdes			break;
2251238104Sdes		}
2252238104Sdes
2253238104Sdes		/* Declare soft PCM volume if needed. */
2254238104Sdes		if ((mask & SOUND_MASK_PCM) == 0 ||
2255238104Sdes		    (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) ||
2256238104Sdes		    pdevinfo->minamp[SOUND_MIXER_PCM] ==
2257238104Sdes		     pdevinfo->maxamp[SOUND_MIXER_PCM]) {
2258238104Sdes			mask |= SOUND_MASK_PCM;
2259238104Sdes			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
2260238104Sdes			HDA_BOOTHVERBOSE(
2261238104Sdes				device_printf(pdevinfo->dev,
2262238104Sdes				    "Forcing Soft PCM volume\n");
2263238104Sdes			);
2264238104Sdes		}
2265238104Sdes
2266238104Sdes		/* Declare master volume if needed. */
2267238104Sdes		if ((mask & SOUND_MASK_VOLUME) == 0) {
2268238104Sdes			mask |= SOUND_MASK_VOLUME;
2269238104Sdes			mix_setparentchild(m, SOUND_MIXER_VOLUME,
2270238104Sdes			    SOUND_MASK_PCM);
2271238104Sdes			mix_setrealdev(m, SOUND_MIXER_VOLUME,
2272238104Sdes			    SOUND_MIXER_NONE);
2273238104Sdes			HDA_BOOTHVERBOSE(
2274238104Sdes				device_printf(pdevinfo->dev,
2275238104Sdes				    "Forcing master volume with PCM\n");
2276238104Sdes			);
2277238104Sdes		}
2278238104Sdes	}
2279238104Sdes
2280238104Sdes	/* Declare record sources available to this association. */
2281238104Sdes	recmask = 0;
2282238104Sdes	if (pdevinfo->recas >= 0) {
2283238104Sdes		for (i = 0; i < 16; i++) {
2284238104Sdes			if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0)
2285238104Sdes				continue;
2286238104Sdes			w = hdaa_widget_get(devinfo,
2287238104Sdes			    devinfo->as[pdevinfo->recas].dacs[0][i]);
2288238104Sdes			if (w == NULL || w->enable == 0)
2289238104Sdes				continue;
2290238104Sdes			for (j = 0; j < w->nconns; j++) {
2291238104Sdes				if (w->connsenable[j] == 0)
2292238104Sdes					continue;
2293238104Sdes				cw = hdaa_widget_get(devinfo, w->conns[j]);
2294238104Sdes				if (cw == NULL || cw->enable == 0)
2295238104Sdes					continue;
2296238104Sdes				if (cw->bindas != pdevinfo->recas &&
2297238104Sdes				    cw->bindas != -2)
2298238104Sdes					continue;
2299238104Sdes				recmask |= cw->ossmask;
2300238104Sdes			}
2301238104Sdes		}
2302238104Sdes	}
2303238104Sdes
2304238104Sdes	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2305238104Sdes	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2306238104Sdes	pdevinfo->ossmask = mask;
2307238104Sdes
2308238104Sdes	mix_setrecdevs(m, recmask);
2309238104Sdes	mix_setdevs(m, mask);
2310238104Sdes
2311238104Sdes	hdaa_unlock(devinfo);
2312238104Sdes
2313238104Sdes	return (0);
2314238104Sdes}
2315238104Sdes
2316238104Sdes/*
2317238104Sdes * Update amplification per pdevinfo per ossdev, calculate summary coefficient
2318238104Sdes * and write it to codec, update *left and *right to reflect remaining error.
2319238104Sdes */
2320238104Sdesstatic void
2321238104Sdeshdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev,
2322238104Sdes    int mute, int *left, int *right)
2323238104Sdes{
2324238104Sdes	int i, zleft, zright, sleft, sright, smute, lval, rval;
2325238104Sdes
2326238104Sdes	ctl->devleft[ossdev] = *left;
2327238104Sdes	ctl->devright[ossdev] = *right;
2328238104Sdes	ctl->devmute[ossdev] = mute;
2329238104Sdes	smute = sleft = sright = zleft = zright = 0;
2330238104Sdes	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2331238104Sdes		sleft += ctl->devleft[i];
2332238104Sdes		sright += ctl->devright[i];
2333238104Sdes		smute |= ctl->devmute[i];
2334238104Sdes		if (i == ossdev)
2335238104Sdes			continue;
2336238104Sdes		zleft += ctl->devleft[i];
2337238104Sdes		zright += ctl->devright[i];
2338238104Sdes	}
2339238104Sdes	lval = QDB2VAL(ctl, sleft);
2340238104Sdes	rval = QDB2VAL(ctl, sright);
2341238104Sdes	hdaa_audio_ctl_amp_set(ctl, smute, lval, rval);
2342238104Sdes	*left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft));
2343238104Sdes	*right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright));
2344238104Sdes}
2345238104Sdes
2346238104Sdes/*
2347238104Sdes * Trace signal from source, setting volumes on the way.
2348238104Sdes */
2349238104Sdesstatic void
2350238104Sdeshdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo,
2351238104Sdes    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2352238104Sdes{
2353238104Sdes	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2354238104Sdes	struct hdaa_widget *w, *wc;
2355238104Sdes	struct hdaa_audio_ctl *ctl;
2356238104Sdes	int i, j, conns = 0;
2357238104Sdes
2358238104Sdes	if (depth > HDA_PARSE_MAXDEPTH)
2359238104Sdes		return;
2360238104Sdes
2361238104Sdes	w = hdaa_widget_get(devinfo, nid);
2362238104Sdes	if (w == NULL || w->enable == 0)
2363269257Sdes		return;
2364269257Sdes
2365269257Sdes	/* Count number of active inputs. */
2366269257Sdes	if (depth > 0) {
2367269257Sdes		for (j = 0; j < w->nconns; j++) {
2368269257Sdes			if (!w->connsenable[j])
2369269257Sdes				continue;
2370269257Sdes			conns++;
2371269257Sdes		}
2372269257Sdes	}
2373269257Sdes
2374269257Sdes	/* If this is not a first step - use input mixer.
2375269257Sdes	   Pins have common input ctl so care must be taken. */
2376269257Sdes	if (depth > 0 && (conns == 1 ||
2377269257Sdes	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
2378269257Sdes		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
2379269257Sdes		    index, 1);
2380269257Sdes		if (ctl)
2381269257Sdes			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2382269257Sdes	}
2383269257Sdes
2384269257Sdes	/* If widget has own ossdev - not traverse it.
2385269257Sdes	   It will be traversed on it's own. */
2386269257Sdes	if (w->ossdev >= 0 && depth > 0)
2387269257Sdes		return;
2388269257Sdes
2389269257Sdes	/* We must not traverse pin */
2390269257Sdes	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
2391269257Sdes	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
2392269257Sdes	    depth > 0)
2393269257Sdes		return;
2394269257Sdes
2395238104Sdes	/*
2396269257Sdes	 * If signals mixed, we can't assign controls farther.
2397269257Sdes	 * Ignore this on depth zero. Caller must knows why.
2398269257Sdes	 */
2399269257Sdes	if (conns > 1 &&
2400269257Sdes	    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
2401269257Sdes	     w->selconn != index))
2402269257Sdes		return;
2403269257Sdes
2404269257Sdes	ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
2405238104Sdes	if (ctl)
2406238104Sdes		hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2407238104Sdes
2408238104Sdes	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2409238104Sdes		wc = hdaa_widget_get(devinfo, i);
2410238104Sdes		if (wc == NULL || wc->enable == 0)
2411238104Sdes			continue;
2412238104Sdes		for (j = 0; j < wc->nconns; j++) {
2413238104Sdes			if (wc->connsenable[j] && wc->conns[j] == nid) {
2414238104Sdes				hdaa_audio_ctl_source_volume(pdevinfo, ossdev,
2415269257Sdes				    wc->nid, j, mute, left, right, depth + 1);
2416269257Sdes			}
2417269257Sdes		}
2418269257Sdes	}
2419269257Sdes	return;
2420269257Sdes}
2421269257Sdes
2422269257Sdes/*
2423269257Sdes * Trace signal from destination, setting volumes on the way.
2424269257Sdes */
2425269257Sdesstatic void
2426269257Sdeshdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo,
2427269257Sdes    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2428269257Sdes{
2429269257Sdes	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2430269257Sdes	struct hdaa_audio_as *as = devinfo->as;
2431269257Sdes	struct hdaa_widget *w, *wc;
2432269257Sdes	struct hdaa_audio_ctl *ctl;
2433269257Sdes	int i, j, consumers, cleft, cright;
2434269257Sdes
2435269257Sdes	if (depth > HDA_PARSE_MAXDEPTH)
2436269257Sdes		return;
2437269257Sdes
2438269257Sdes	w = hdaa_widget_get(devinfo, nid);
2439269257Sdes	if (w == NULL || w->enable == 0)
2440269257Sdes		return;
2441269257Sdes
2442269257Sdes	if (depth > 0) {
2443269257Sdes		/* If this node produce output for several consumers,
2444269257Sdes		   we can't touch it. */
2445269257Sdes		consumers = 0;
2446269257Sdes		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2447269257Sdes			wc = hdaa_widget_get(devinfo, i);
2448269257Sdes			if (wc == NULL || wc->enable == 0)
2449269257Sdes				continue;
2450269257Sdes			for (j = 0; j < wc->nconns; j++) {
2451269257Sdes				if (wc->connsenable[j] && wc->conns[j] == nid)
2452269257Sdes					consumers++;
2453269257Sdes			}
2454269257Sdes		}
2455269257Sdes		/* The only exception is if real HP redirection is configured
2456269257Sdes		   and this is a duplication point.
2457269257Sdes		   XXX: Actually exception is not completely correct.
2458269257Sdes		   XXX: Duplication point check is not perfect. */
2459269257Sdes		if ((consumers == 2 && (w->bindas < 0 ||
2460269257Sdes		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
2461269257Sdes		    (w->bindseqmask & (1 << 15)) == 0)) ||
2462269257Sdes		    consumers > 2)
2463269257Sdes			return;
2464269257Sdes
2465269257Sdes		/* Else use it's output mixer. */
2466269257Sdes		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2467269257Sdes		    HDAA_CTL_OUT, -1, 1);
2468269257Sdes		if (ctl)
2469269257Sdes			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2470269257Sdes	}
2471269257Sdes
2472269257Sdes	/* We must not traverse pin */
2473269257Sdes	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2474269257Sdes	    depth > 0)
2475269257Sdes		return;
2476269257Sdes
2477269257Sdes	for (i = 0; i < w->nconns; i++) {
2478269257Sdes		if (w->connsenable[i] == 0)
2479269257Sdes			continue;
2480269257Sdes		if (index >= 0 && i != index)
2481269257Sdes			continue;
2482269257Sdes		cleft = left;
2483269257Sdes		cright = right;
2484269257Sdes		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2485269257Sdes		    HDAA_CTL_IN, i, 1);
2486269257Sdes		if (ctl)
2487269257Sdes			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright);
2488269257Sdes		hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1,
2489269257Sdes		    mute, cleft, cright, depth + 1);
2490269257Sdes	}
2491269257Sdes}
2492269257Sdes
2493269257Sdes/*
2494269257Sdes * Set volumes for the specified pdevinfo and ossdev.
2495269257Sdes */
2496269257Sdesstatic void
2497269257Sdeshdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev)
2498269257Sdes{
2499269257Sdes	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2500269257Sdes	struct hdaa_widget *w, *cw;
2501269257Sdes	uint32_t mute;
2502269257Sdes	int lvol, rvol;
2503269257Sdes	int i, j;
2504269257Sdes
2505269257Sdes	mute = 0;
2506269257Sdes	if (pdevinfo->left[dev] == 0) {
2507269257Sdes		mute |= HDAA_AMP_MUTE_LEFT;
2508269257Sdes		lvol = -4000;
2509269257Sdes	} else
2510269257Sdes		lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2511269257Sdes		    pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev];
2512269257Sdes	if (pdevinfo->right[dev] == 0) {
2513269257Sdes		mute |= HDAA_AMP_MUTE_RIGHT;
2514269257Sdes		rvol = -4000;
2515269257Sdes	} else
2516269257Sdes		rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2517269257Sdes		    pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev];
2518269257Sdes	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2519269257Sdes		w = hdaa_widget_get(devinfo, i);
2520269257Sdes		if (w == NULL || w->enable == 0)
2521269257Sdes			continue;
2522269257Sdes		if (w->bindas < 0) {
2523269257Sdes			if (pdevinfo->index != 0)
2524269257Sdes				continue;
2525269257Sdes		} else {
2526269257Sdes			if (w->bindas != pdevinfo->playas &&
2527269257Sdes			    w->bindas != pdevinfo->recas)
2528269257Sdes				continue;
2529269257Sdes		}
2530269257Sdes		if (dev == SOUND_MIXER_RECLEV &&
2531269257Sdes		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2532269257Sdes			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2533269257Sdes			    w->nid, -1, mute, lvol, rvol, 0);
2534238104Sdes			continue;
2535238104Sdes		}
2536238104Sdes		if (dev == SOUND_MIXER_VOLUME &&
2537238104Sdes		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2538269257Sdes		    devinfo->as[w->bindas].dir == HDAA_CTL_OUT) {
2539238104Sdes			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2540238104Sdes			    w->nid, -1, mute, lvol, rvol, 0);
2541238104Sdes			continue;
2542238104Sdes		}
2543238104Sdes		if (dev == SOUND_MIXER_IGAIN &&
2544238104Sdes		    w->pflags & HDAA_ADC_MONITOR) {
2545238104Sdes			for (j = 0; j < w->nconns; j++) {
2546238104Sdes				if (!w->connsenable[j])
2547238104Sdes				    continue;
2548238104Sdes				cw = hdaa_widget_get(devinfo, w->conns[j]);
2549238104Sdes				if (cw == NULL || cw->enable == 0)
2550238104Sdes				    continue;
2551238104Sdes				if (cw->bindas == -1)
2552238104Sdes				    continue;
2553238104Sdes				if (cw->bindas >= 0 &&
2554238104Sdes				    devinfo->as[cw->bindas].dir != HDAA_CTL_IN)
2555238104Sdes					continue;
2556238104Sdes				hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2557238104Sdes				    w->nid, j, mute, lvol, rvol, 0);
2558238104Sdes			}
2559238104Sdes			continue;
2560238104Sdes		}
2561238104Sdes		if (w->ossdev != dev)
2562238104Sdes			continue;
2563238104Sdes		hdaa_audio_ctl_source_volume(pdevinfo, dev,
2564238104Sdes		    w->nid, -1, mute, lvol, rvol, 0);
2565238104Sdes		if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST))
2566238104Sdes			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2567238104Sdes			    w->nid, -1, mute, lvol, rvol, 0);
2568238104Sdes	}
2569238104Sdes}
2570238104Sdes
2571238104Sdes/*
2572238104Sdes * OSS Mixer set method.
2573238104Sdes */
2574238104Sdesstatic int
2575238104Sdeshdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
2576238104Sdes					unsigned left, unsigned right)
2577238104Sdes{
2578238104Sdes	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2579238104Sdes	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2580238104Sdes	struct hdaa_widget *w;
2581238104Sdes	int i;
2582238104Sdes
2583238104Sdes	hdaa_lock(devinfo);
2584238104Sdes
2585238104Sdes	/* Save new values. */
2586238104Sdes	pdevinfo->left[dev] = left;
2587238104Sdes	pdevinfo->right[dev] = right;
2588238104Sdes
2589238104Sdes	/* 'ogain' is the special case implemented with EAPD. */
2590238104Sdes	if (dev == SOUND_MIXER_OGAIN) {
2591238104Sdes		uint32_t orig;
2592238104Sdes		w = NULL;
2593238104Sdes		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2594238104Sdes			w = hdaa_widget_get(devinfo, i);
2595238104Sdes			if (w == NULL || w->enable == 0)
2596238104Sdes				continue;
2597238104Sdes			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2598238104Sdes			    w->param.eapdbtl == HDA_INVALID)
2599238104Sdes				continue;
2600238104Sdes			break;
2601238104Sdes		}
2602238104Sdes		if (i >= devinfo->endnode) {
2603238104Sdes			hdaa_unlock(devinfo);
2604238104Sdes			return (-1);
2605238104Sdes		}
2606238104Sdes		orig = w->param.eapdbtl;
2607238104Sdes		if (left == 0)
2608238104Sdes			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2609238104Sdes		else
2610238104Sdes			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2611238104Sdes		if (orig != w->param.eapdbtl) {
2612238104Sdes			uint32_t val;
2613238104Sdes
2614238104Sdes			val = w->param.eapdbtl;
2615238104Sdes			if (devinfo->quirks & HDAA_QUIRK_EAPDINV)
2616238104Sdes				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2617238104Sdes			hda_command(devinfo->dev,
2618238104Sdes			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val));
2619238104Sdes		}
2620238104Sdes		hdaa_unlock(devinfo);
2621238104Sdes		return (left | (left << 8));
2622238104Sdes	}
2623238104Sdes
2624238104Sdes	/* Recalculate all controls related to this OSS device. */
2625238104Sdes	hdaa_audio_ctl_dev_volume(pdevinfo, dev);
2626238104Sdes
2627238104Sdes	hdaa_unlock(devinfo);
2628238104Sdes	return (left | (right << 8));
2629238104Sdes}
2630238104Sdes
2631238104Sdes/*
2632238104Sdes * Set mixer settings to our own default values:
2633238104Sdes * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others.
2634238104Sdes */
2635238104Sdesstatic void
2636238104Sdeshdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo)
2637238104Sdes{
2638238104Sdes	int amp, vol, dev;
2639238104Sdes
2640238104Sdes	for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) {
2641238104Sdes		if ((pdevinfo->ossmask & (1 << dev)) == 0)
2642238104Sdes			continue;
2643238104Sdes
2644238104Sdes		/* If the value was overriden, leave it as is. */
2645238104Sdes		if (resource_int_value(device_get_name(pdevinfo->dev),
2646238104Sdes		    device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0)
2647238104Sdes			continue;
2648238104Sdes
2649238104Sdes		vol = -1;
2650238104Sdes		if (dev == SOUND_MIXER_OGAIN)
2651238104Sdes			vol = 100;
2652238104Sdes		else if (dev == SOUND_MIXER_IGAIN)
2653238104Sdes			vol = 0;
2654238104Sdes		else if (dev == SOUND_MIXER_MIC ||
2655238104Sdes		    dev == SOUND_MIXER_MONITOR)
2656238104Sdes			amp = 20 * 4;	/* +20dB */
2657238104Sdes		else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital)
2658238104Sdes			amp = -10 * 4;	/* -10dB */
2659238104Sdes		else
2660238104Sdes			amp = 0;
2661238104Sdes		if (vol < 0 &&
2662238104Sdes		    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) {
2663238104Sdes			vol = 100;
2664238104Sdes		} else if (vol < 0) {
2665238104Sdes			vol = ((amp - pdevinfo->minamp[dev]) * 100 +
2666238104Sdes			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) /
2667238104Sdes			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]);
2668238104Sdes			vol = imin(imax(vol, 1), 100);
2669238104Sdes		}
2670238104Sdes		mix_set(pdevinfo->mixer, dev, vol, vol);
2671238104Sdes	}
2672238104Sdes}
2673238104Sdes
2674238104Sdes/*
2675238104Sdes * Recursively commutate specified record source.
2676238104Sdes */
2677238104Sdesstatic uint32_t
2678238104Sdeshdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
2679238104Sdes{
2680238104Sdes	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2681238104Sdes	struct hdaa_widget *w, *cw;
2682238104Sdes	struct hdaa_audio_ctl *ctl;
2683238104Sdes	char buf[64];
2684238104Sdes	int i, muted;
2685238104Sdes	uint32_t res = 0;
2686238104Sdes
2687238104Sdes	if (depth > HDA_PARSE_MAXDEPTH)
2688		return (0);
2689
2690	w = hdaa_widget_get(devinfo, nid);
2691	if (w == NULL || w->enable == 0)
2692		return (0);
2693
2694	for (i = 0; i < w->nconns; i++) {
2695		if (w->connsenable[i] == 0)
2696			continue;
2697		cw = hdaa_widget_get(devinfo, w->conns[i]);
2698		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
2699			continue;
2700		/* Call recursively to trace signal to it's source if needed. */
2701		if ((src & cw->ossmask) != 0) {
2702			if (cw->ossdev < 0) {
2703				res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2704				    w->conns[i], depth + 1);
2705			} else {
2706				res |= cw->ossmask;
2707			}
2708		}
2709		/* We have two special cases: mixers and others (selectors). */
2710		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
2711			ctl = hdaa_audio_ctl_amp_get(devinfo,
2712			    w->nid, HDAA_CTL_IN, i, 1);
2713			if (ctl == NULL)
2714				continue;
2715			/* If we have input control on this node mute them
2716			 * according to requested sources. */
2717			muted = (src & cw->ossmask) ? 0 : 1;
2718			if (muted != ctl->forcemute) {
2719				ctl->forcemute = muted;
2720				hdaa_audio_ctl_amp_set(ctl,
2721				    HDAA_AMP_MUTE_DEFAULT,
2722				    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
2723			}
2724			HDA_BOOTHVERBOSE(
2725				device_printf(pdevinfo->dev,
2726				    "Recsel (%s): nid %d source %d %s\n",
2727				    hdaa_audio_ctl_ossmixer_mask2allname(
2728				    src, buf, sizeof(buf)),
2729				    nid, i, muted?"mute":"unmute");
2730			);
2731		} else {
2732			if (w->nconns == 1)
2733				break;
2734			if ((src & cw->ossmask) == 0)
2735				continue;
2736			/* If we found requested source - select it and exit. */
2737			hdaa_widget_connection_select(w, i);
2738			HDA_BOOTHVERBOSE(
2739				device_printf(pdevinfo->dev,
2740				    "Recsel (%s): nid %d source %d select\n",
2741				    hdaa_audio_ctl_ossmixer_mask2allname(
2742				    src, buf, sizeof(buf)),
2743				    nid, i);
2744			);
2745			break;
2746		}
2747	}
2748	return (res);
2749}
2750
2751static uint32_t
2752hdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
2753{
2754	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2755	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2756	struct hdaa_widget *w;
2757	struct hdaa_audio_as *as;
2758	struct hdaa_audio_ctl *ctl;
2759	struct hdaa_chan *ch;
2760	int i, j;
2761	uint32_t ret = 0xffffffff;
2762
2763	hdaa_lock(devinfo);
2764	if (pdevinfo->recas < 0) {
2765		hdaa_unlock(devinfo);
2766		return (0);
2767	}
2768	as = &devinfo->as[pdevinfo->recas];
2769
2770	/* For non-mixed associations we always recording everything. */
2771	if (!as->mixed) {
2772		hdaa_unlock(devinfo);
2773		return (mix_getrecdevs(m));
2774	}
2775
2776	/* Commutate requested recsrc for each ADC. */
2777	for (j = 0; j < as->num_chans; j++) {
2778		ch = &devinfo->chans[as->chans[j]];
2779		for (i = 0; ch->io[i] >= 0; i++) {
2780			w = hdaa_widget_get(devinfo, ch->io[i]);
2781			if (w == NULL || w->enable == 0)
2782				continue;
2783			ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2784			    ch->io[i], 0);
2785		}
2786	}
2787	if (ret == 0xffffffff)
2788		ret = 0;
2789
2790	/*
2791	 * Some controls could be shared. Reset volumes for controls
2792	 * related to previously chosen devices, as they may no longer
2793	 * affect the signal.
2794	 */
2795	i = 0;
2796	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
2797		if (ctl->enable == 0 ||
2798		    !(ctl->ossmask & pdevinfo->recsrc))
2799			continue;
2800		if (!((pdevinfo->playas >= 0 &&
2801		    ctl->widget->bindas == pdevinfo->playas) ||
2802		    (pdevinfo->recas >= 0 &&
2803		    ctl->widget->bindas == pdevinfo->recas) ||
2804		    (pdevinfo->index == 0 &&
2805		    ctl->widget->bindas == -2)))
2806			continue;
2807		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2808			if (pdevinfo->recsrc & (1 << j)) {
2809				ctl->devleft[j] = 0;
2810				ctl->devright[j] = 0;
2811				ctl->devmute[j] = 0;
2812			}
2813		}
2814	}
2815
2816	/*
2817	 * Some controls could be shared. Set volumes for controls
2818	 * related to devices selected both previously and now.
2819	 */
2820	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2821		if ((ret | pdevinfo->recsrc) & (1 << j))
2822			hdaa_audio_ctl_dev_volume(pdevinfo, j);
2823	}
2824
2825	pdevinfo->recsrc = ret;
2826	hdaa_unlock(devinfo);
2827	return (ret);
2828}
2829
2830static kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = {
2831	KOBJMETHOD(mixer_init,		hdaa_audio_ctl_ossmixer_init),
2832	KOBJMETHOD(mixer_set,		hdaa_audio_ctl_ossmixer_set),
2833	KOBJMETHOD(mixer_setrecsrc,	hdaa_audio_ctl_ossmixer_setrecsrc),
2834	KOBJMETHOD_END
2835};
2836MIXER_DECLARE(hdaa_audio_ctl_ossmixer);
2837
2838static void
2839hdaa_dump_gpi(struct hdaa_devinfo *devinfo)
2840{
2841	device_t dev = devinfo->dev;
2842	int i;
2843	uint32_t data, wake, unsol, sticky;
2844
2845	if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) {
2846		data = hda_command(dev,
2847		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
2848		wake = hda_command(dev,
2849		    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid));
2850		unsol = hda_command(dev,
2851		    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2852		sticky = hda_command(dev,
2853		    HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid));
2854		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) {
2855			device_printf(dev, " GPI%d:%s%s%s state=%d", i,
2856				    (sticky & (1 << i)) ? " sticky" : "",
2857				    (unsol & (1 << i)) ? " unsol" : "",
2858				    (wake & (1 << i)) ? " wake" : "",
2859				    (data >> i) & 1);
2860		}
2861	}
2862}
2863
2864static void
2865hdaa_dump_gpio(struct hdaa_devinfo *devinfo)
2866{
2867	device_t dev = devinfo->dev;
2868	int i;
2869	uint32_t data, dir, enable, wake, unsol, sticky;
2870
2871	if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) {
2872		data = hda_command(dev,
2873		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
2874		enable = hda_command(dev,
2875		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
2876		dir = hda_command(dev,
2877		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
2878		wake = hda_command(dev,
2879		    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid));
2880		unsol = hda_command(dev,
2881		    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2882		sticky = hda_command(dev,
2883		    HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid));
2884		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) {
2885			device_printf(dev, " GPIO%d: ", i);
2886			if ((enable & (1 << i)) == 0) {
2887				printf("disabled\n");
2888				continue;
2889			}
2890			if ((dir & (1 << i)) == 0) {
2891				printf("input%s%s%s",
2892				    (sticky & (1 << i)) ? " sticky" : "",
2893				    (unsol & (1 << i)) ? " unsol" : "",
2894				    (wake & (1 << i)) ? " wake" : "");
2895			} else
2896				printf("output");
2897			printf(" state=%d\n", (data >> i) & 1);
2898		}
2899	}
2900}
2901
2902static void
2903hdaa_dump_gpo(struct hdaa_devinfo *devinfo)
2904{
2905	device_t dev = devinfo->dev;
2906	int i;
2907	uint32_t data;
2908
2909	if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) {
2910		data = hda_command(dev,
2911		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
2912		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) {
2913			device_printf(dev, " GPO%d: state=%d", i,
2914				    (data >> i) & 1);
2915		}
2916	}
2917}
2918
2919static void
2920hdaa_audio_parse(struct hdaa_devinfo *devinfo)
2921{
2922	struct hdaa_widget *w;
2923	uint32_t res;
2924	int i;
2925	nid_t nid;
2926
2927	nid = devinfo->nid;
2928
2929	res = hda_command(devinfo->dev,
2930	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT));
2931	devinfo->gpio_cap = res;
2932
2933	HDA_BOOTVERBOSE(
2934		device_printf(devinfo->dev,
2935		    "NumGPIO=%d NumGPO=%d "
2936		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
2937		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
2938		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
2939		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
2940		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
2941		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
2942		hdaa_dump_gpi(devinfo);
2943		hdaa_dump_gpio(devinfo);
2944		hdaa_dump_gpo(devinfo);
2945	);
2946
2947	res = hda_command(devinfo->dev,
2948	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS));
2949	devinfo->supp_stream_formats = res;
2950
2951	res = hda_command(devinfo->dev,
2952	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE));
2953	devinfo->supp_pcm_size_rate = res;
2954
2955	res = hda_command(devinfo->dev,
2956	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP));
2957	devinfo->outamp_cap = res;
2958
2959	res = hda_command(devinfo->dev,
2960	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP));
2961	devinfo->inamp_cap = res;
2962
2963	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2964		w = hdaa_widget_get(devinfo, i);
2965		if (w == NULL)
2966			device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i);
2967		else {
2968			w->devinfo = devinfo;
2969			w->nid = i;
2970			w->enable = 1;
2971			w->selconn = -1;
2972			w->pflags = 0;
2973			w->ossdev = -1;
2974			w->bindas = -1;
2975			w->param.eapdbtl = HDA_INVALID;
2976			hdaa_widget_parse(w);
2977		}
2978	}
2979}
2980
2981static void
2982hdaa_audio_postprocess(struct hdaa_devinfo *devinfo)
2983{
2984	struct hdaa_widget *w;
2985	int i;
2986
2987	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2988		w = hdaa_widget_get(devinfo, i);
2989		if (w == NULL)
2990			continue;
2991		hdaa_widget_postprocess(w);
2992	}
2993}
2994
2995static void
2996hdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo)
2997{
2998	struct hdaa_audio_ctl *ctls;
2999	struct hdaa_widget *w, *cw;
3000	int i, j, cnt, max, ocap, icap;
3001	int mute, offset, step, size;
3002
3003	/* XXX This is redundant */
3004	max = 0;
3005	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3006		w = hdaa_widget_get(devinfo, i);
3007		if (w == NULL || w->enable == 0)
3008			continue;
3009		if (w->param.outamp_cap != 0)
3010			max++;
3011		if (w->param.inamp_cap != 0) {
3012			switch (w->type) {
3013			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3014			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3015				for (j = 0; j < w->nconns; j++) {
3016					cw = hdaa_widget_get(devinfo,
3017					    w->conns[j]);
3018					if (cw == NULL || cw->enable == 0)
3019						continue;
3020					max++;
3021				}
3022				break;
3023			default:
3024				max++;
3025				break;
3026			}
3027		}
3028	}
3029	devinfo->ctlcnt = max;
3030
3031	if (max < 1)
3032		return;
3033
3034	ctls = (struct hdaa_audio_ctl *)malloc(
3035	    sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT);
3036
3037	if (ctls == NULL) {
3038		/* Blekh! */
3039		device_printf(devinfo->dev, "unable to allocate ctls!\n");
3040		devinfo->ctlcnt = 0;
3041		return;
3042	}
3043
3044	cnt = 0;
3045	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
3046		if (cnt >= max) {
3047			device_printf(devinfo->dev, "%s: Ctl overflow!\n",
3048			    __func__);
3049			break;
3050		}
3051		w = hdaa_widget_get(devinfo, i);
3052		if (w == NULL || w->enable == 0)
3053			continue;
3054		ocap = w->param.outamp_cap;
3055		icap = w->param.inamp_cap;
3056		if (ocap != 0) {
3057			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
3058			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
3059			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
3060			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
3061			/*if (offset > step) {
3062				HDA_BOOTVERBOSE(
3063					device_printf(devinfo->dev,
3064					    "BUGGY outamp: nid=%d "
3065					    "[offset=%d > step=%d]\n",
3066					    w->nid, offset, step);
3067				);
3068				offset = step;
3069			}*/
3070			ctls[cnt].enable = 1;
3071			ctls[cnt].widget = w;
3072			ctls[cnt].mute = mute;
3073			ctls[cnt].step = step;
3074			ctls[cnt].size = size;
3075			ctls[cnt].offset = offset;
3076			ctls[cnt].left = offset;
3077			ctls[cnt].right = offset;
3078			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3079			    w->waspin)
3080				ctls[cnt].ndir = HDAA_CTL_IN;
3081			else
3082				ctls[cnt].ndir = HDAA_CTL_OUT;
3083			ctls[cnt++].dir = HDAA_CTL_OUT;
3084		}
3085
3086		if (icap != 0) {
3087			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
3088			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
3089			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
3090			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
3091			/*if (offset > step) {
3092				HDA_BOOTVERBOSE(
3093					device_printf(devinfo->dev,
3094					    "BUGGY inamp: nid=%d "
3095					    "[offset=%d > step=%d]\n",
3096					    w->nid, offset, step);
3097				);
3098				offset = step;
3099			}*/
3100			switch (w->type) {
3101			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3102			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3103				for (j = 0; j < w->nconns; j++) {
3104					if (cnt >= max) {
3105						device_printf(devinfo->dev,
3106						    "%s: Ctl overflow!\n",
3107						    __func__);
3108						break;
3109					}
3110					cw = hdaa_widget_get(devinfo,
3111					    w->conns[j]);
3112					if (cw == NULL || cw->enable == 0)
3113						continue;
3114					ctls[cnt].enable = 1;
3115					ctls[cnt].widget = w;
3116					ctls[cnt].childwidget = cw;
3117					ctls[cnt].index = j;
3118					ctls[cnt].mute = mute;
3119					ctls[cnt].step = step;
3120					ctls[cnt].size = size;
3121					ctls[cnt].offset = offset;
3122					ctls[cnt].left = offset;
3123					ctls[cnt].right = offset;
3124				ctls[cnt].ndir = HDAA_CTL_IN;
3125					ctls[cnt++].dir = HDAA_CTL_IN;
3126				}
3127				break;
3128			default:
3129				if (cnt >= max) {
3130					device_printf(devinfo->dev,
3131					    "%s: Ctl overflow!\n",
3132					    __func__);
3133					break;
3134				}
3135				ctls[cnt].enable = 1;
3136				ctls[cnt].widget = w;
3137				ctls[cnt].mute = mute;
3138				ctls[cnt].step = step;
3139				ctls[cnt].size = size;
3140				ctls[cnt].offset = offset;
3141				ctls[cnt].left = offset;
3142				ctls[cnt].right = offset;
3143				if (w->type ==
3144				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3145					ctls[cnt].ndir = HDAA_CTL_OUT;
3146				else
3147					ctls[cnt].ndir = HDAA_CTL_IN;
3148				ctls[cnt++].dir = HDAA_CTL_IN;
3149				break;
3150			}
3151		}
3152	}
3153
3154	devinfo->ctl = ctls;
3155}
3156
3157static void
3158hdaa_audio_as_parse(struct hdaa_devinfo *devinfo)
3159{
3160	struct hdaa_audio_as *as;
3161	struct hdaa_widget *w;
3162	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
3163
3164	/* Count present associations */
3165	max = 0;
3166	for (j = 1; j < 16; j++) {
3167		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3168			w = hdaa_widget_get(devinfo, i);
3169			if (w == NULL || w->enable == 0)
3170				continue;
3171			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3172				continue;
3173			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
3174			    != j)
3175				continue;
3176			max++;
3177			if (j != 15)  /* There could be many 1-pin assocs #15 */
3178				break;
3179		}
3180	}
3181
3182	devinfo->ascnt = max;
3183
3184	if (max < 1)
3185		return;
3186
3187	as = (struct hdaa_audio_as *)malloc(
3188	    sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT);
3189
3190	if (as == NULL) {
3191		/* Blekh! */
3192		device_printf(devinfo->dev, "unable to allocate assocs!\n");
3193		devinfo->ascnt = 0;
3194		return;
3195	}
3196
3197	for (i = 0; i < max; i++) {
3198		as[i].hpredir = -1;
3199		as[i].digital = 0;
3200		as[i].num_chans = 1;
3201		as[i].location = -1;
3202	}
3203
3204	/* Scan associations skipping as=0. */
3205	cnt = 0;
3206	for (j = 1; j < 16; j++) {
3207		first = 16;
3208		hpredir = 0;
3209		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3210			w = hdaa_widget_get(devinfo, i);
3211			if (w == NULL || w->enable == 0)
3212				continue;
3213			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3214				continue;
3215			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
3216			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
3217			if (assoc != j) {
3218				continue;
3219			}
3220			KASSERT(cnt < max,
3221			    ("%s: Associations owerflow (%d of %d)",
3222			    __func__, cnt, max));
3223			type = w->wclass.pin.config &
3224			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
3225			/* Get pin direction. */
3226			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
3227			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
3228			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
3229			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
3230			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
3231				dir = HDAA_CTL_OUT;
3232			else
3233				dir = HDAA_CTL_IN;
3234			/* If this is a first pin - create new association. */
3235			if (as[cnt].pincnt == 0) {
3236				as[cnt].enable = 1;
3237				as[cnt].index = j;
3238				as[cnt].dir = dir;
3239			}
3240			if (seq < first)
3241				first = seq;
3242			/* Check association correctness. */
3243			if (as[cnt].pins[seq] != 0) {
3244				device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) "
3245				    "in association %d! Disabling association.\n",
3246				    __func__, seq, w->nid, j);
3247				as[cnt].enable = 0;
3248			}
3249			if (dir != as[cnt].dir) {
3250				device_printf(devinfo->dev, "%s: Pin %d has wrong "
3251				    "direction for association %d! Disabling "
3252				    "association.\n",
3253				    __func__, w->nid, j);
3254				as[cnt].enable = 0;
3255			}
3256			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
3257				as[cnt].digital |= 0x1;
3258				if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
3259					as[cnt].digital |= 0x2;
3260				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
3261					as[cnt].digital |= 0x4;
3262			}
3263			if (as[cnt].location == -1) {
3264				as[cnt].location =
3265				    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config);
3266			} else if (as[cnt].location !=
3267			    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) {
3268				as[cnt].location = -2;
3269			}
3270			/* Headphones with seq=15 may mean redirection. */
3271			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
3272			    seq == 15)
3273				hpredir = 1;
3274			as[cnt].pins[seq] = w->nid;
3275			as[cnt].pincnt++;
3276			/* Association 15 is a multiple unassociated pins. */
3277			if (j == 15)
3278				cnt++;
3279		}
3280		if (j != 15 && as[cnt].pincnt > 0) {
3281			if (hpredir && as[cnt].pincnt > 1)
3282				as[cnt].hpredir = first;
3283			cnt++;
3284		}
3285	}
3286	for (i = 0; i < max; i++) {
3287		if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 ||
3288		    as[i].pins[14] > 0 || as[i].pins[15] > 0))
3289			as[i].mixed = 1;
3290	}
3291	HDA_BOOTVERBOSE(
3292		device_printf(devinfo->dev,
3293		    "%d associations found:\n", max);
3294		for (i = 0; i < max; i++) {
3295			device_printf(devinfo->dev,
3296			    "Association %d (%d) %s%s:\n",
3297			    i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out",
3298			    as[i].enable?"":" (disabled)");
3299			for (j = 0; j < 16; j++) {
3300				if (as[i].pins[j] == 0)
3301					continue;
3302				device_printf(devinfo->dev,
3303				    " Pin nid=%d seq=%d\n",
3304				    as[i].pins[j], j);
3305			}
3306		}
3307	);
3308
3309	devinfo->as = as;
3310}
3311
3312/*
3313 * Trace path from DAC to pin.
3314 */
3315static nid_t
3316hdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3317    int dupseq, int min, int only, int depth)
3318{
3319	struct hdaa_widget *w;
3320	int i, im = -1;
3321	nid_t m = 0, ret;
3322
3323	if (depth > HDA_PARSE_MAXDEPTH)
3324		return (0);
3325	w = hdaa_widget_get(devinfo, nid);
3326	if (w == NULL || w->enable == 0)
3327		return (0);
3328	HDA_BOOTHVERBOSE(
3329		if (!only) {
3330			device_printf(devinfo->dev,
3331			    " %*stracing via nid %d\n",
3332				depth + 1, "", w->nid);
3333		}
3334	);
3335	/* Use only unused widgets */
3336	if (w->bindas >= 0 && w->bindas != as) {
3337		HDA_BOOTHVERBOSE(
3338			if (!only) {
3339				device_printf(devinfo->dev,
3340				    " %*snid %d busy by association %d\n",
3341					depth + 1, "", w->nid, w->bindas);
3342			}
3343		);
3344		return (0);
3345	}
3346	if (dupseq < 0) {
3347		if (w->bindseqmask != 0) {
3348			HDA_BOOTHVERBOSE(
3349				if (!only) {
3350					device_printf(devinfo->dev,
3351					    " %*snid %d busy by seqmask %x\n",
3352						depth + 1, "", w->nid, w->bindseqmask);
3353				}
3354			);
3355			return (0);
3356		}
3357	} else {
3358		/* If this is headphones - allow duplicate first pin. */
3359		if (w->bindseqmask != 0 &&
3360		    (w->bindseqmask & (1 << dupseq)) == 0) {
3361			HDA_BOOTHVERBOSE(
3362				device_printf(devinfo->dev,
3363				    " %*snid %d busy by seqmask %x\n",
3364					depth + 1, "", w->nid, w->bindseqmask);
3365			);
3366			return (0);
3367		}
3368	}
3369
3370	switch (w->type) {
3371	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3372		/* Do not traverse input. AD1988 has digital monitor
3373		for which we are not ready. */
3374		break;
3375	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
3376		/* If we are tracing HP take only dac of first pin. */
3377		if ((only == 0 || only == w->nid) &&
3378		    (w->nid >= min) && (dupseq < 0 || w->nid ==
3379		    devinfo->as[as].dacs[0][dupseq]))
3380			m = w->nid;
3381		break;
3382	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3383		if (depth > 0)
3384			break;
3385		/* Fall */
3386	default:
3387		/* Find reachable DACs with smallest nid respecting constraints. */
3388		for (i = 0; i < w->nconns; i++) {
3389			if (w->connsenable[i] == 0)
3390				continue;
3391			if (w->selconn != -1 && w->selconn != i)
3392				continue;
3393			if ((ret = hdaa_audio_trace_dac(devinfo, as, seq,
3394			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
3395				if (m == 0 || ret < m) {
3396					m = ret;
3397					im = i;
3398				}
3399				if (only || dupseq >= 0)
3400					break;
3401			}
3402		}
3403		if (im >= 0 && only && ((w->nconns > 1 &&
3404		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3405		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3406			w->selconn = im;
3407		break;
3408	}
3409	if (m && only) {
3410		w->bindas = as;
3411		w->bindseqmask |= (1 << seq);
3412	}
3413	HDA_BOOTHVERBOSE(
3414		if (!only) {
3415			device_printf(devinfo->dev,
3416			    " %*snid %d returned %d\n",
3417				depth + 1, "", w->nid, m);
3418		}
3419	);
3420	return (m);
3421}
3422
3423/*
3424 * Trace path from widget to ADC.
3425 */
3426static nid_t
3427hdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3428    int mixed, int min, int only, int depth, int *length, int onlylength)
3429{
3430	struct hdaa_widget *w, *wc;
3431	int i, j, im, lm = HDA_PARSE_MAXDEPTH;
3432	nid_t m = 0, ret;
3433
3434	if (depth > HDA_PARSE_MAXDEPTH)
3435		return (0);
3436	w = hdaa_widget_get(devinfo, nid);
3437	if (w == NULL || w->enable == 0)
3438		return (0);
3439	HDA_BOOTHVERBOSE(
3440		device_printf(devinfo->dev,
3441		    " %*stracing via nid %d\n",
3442			depth + 1, "", w->nid);
3443	);
3444	/* Use only unused widgets */
3445	if (w->bindas >= 0 && w->bindas != as) {
3446		HDA_BOOTHVERBOSE(
3447			device_printf(devinfo->dev,
3448			    " %*snid %d busy by association %d\n",
3449				depth + 1, "", w->nid, w->bindas);
3450		);
3451		return (0);
3452	}
3453	if (!mixed && w->bindseqmask != 0) {
3454		HDA_BOOTHVERBOSE(
3455			device_printf(devinfo->dev,
3456			    " %*snid %d busy by seqmask %x\n",
3457				depth + 1, "", w->nid, w->bindseqmask);
3458		);
3459		return (0);
3460	}
3461	switch (w->type) {
3462	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3463		if ((only == 0 || only == w->nid) && (w->nid >= min) &&
3464		    (onlylength == 0 || onlylength == depth)) {
3465			m = w->nid;
3466			*length = depth;
3467		}
3468		break;
3469	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3470		if (depth > 0)
3471			break;
3472		/* Fall */
3473	default:
3474		/* Try to find reachable ADCs with specified nid. */
3475		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3476			wc = hdaa_widget_get(devinfo, j);
3477			if (wc == NULL || wc->enable == 0)
3478				continue;
3479			im = -1;
3480			for (i = 0; i < wc->nconns; i++) {
3481				if (wc->connsenable[i] == 0)
3482					continue;
3483				if (wc->conns[i] != nid)
3484					continue;
3485				if ((ret = hdaa_audio_trace_adc(devinfo, as, seq,
3486				    j, mixed, min, only, depth + 1,
3487				    length, onlylength)) != 0) {
3488					if (m == 0 || ret < m ||
3489					    (ret == m && *length < lm)) {
3490						m = ret;
3491						im = i;
3492						lm = *length;
3493					} else
3494						*length = lm;
3495					if (only)
3496						break;
3497				}
3498			}
3499			if (im >= 0 && only && ((wc->nconns > 1 &&
3500			    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3501			    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3502				wc->selconn = im;
3503		}
3504		break;
3505	}
3506	if (m && only) {
3507		w->bindas = as;
3508		w->bindseqmask |= (1 << seq);
3509	}
3510	HDA_BOOTHVERBOSE(
3511		device_printf(devinfo->dev,
3512		    " %*snid %d returned %d\n",
3513			depth + 1, "", w->nid, m);
3514	);
3515	return (m);
3516}
3517
3518/*
3519 * Erase trace path of the specified association.
3520 */
3521static void
3522hdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq)
3523{
3524	struct hdaa_widget *w;
3525	int i;
3526
3527	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3528		w = hdaa_widget_get(devinfo, i);
3529		if (w == NULL || w->enable == 0)
3530			continue;
3531		if (w->bindas == as) {
3532			if (seq >= 0) {
3533				w->bindseqmask &= ~(1 << seq);
3534				if (w->bindseqmask == 0) {
3535					w->bindas = -1;
3536					w->selconn = -1;
3537				}
3538			} else {
3539				w->bindas = -1;
3540				w->bindseqmask = 0;
3541				w->selconn = -1;
3542			}
3543		}
3544	}
3545}
3546
3547/*
3548 * Trace association path from DAC to output
3549 */
3550static int
3551hdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq)
3552{
3553	struct hdaa_audio_as *ases = devinfo->as;
3554	int i, hpredir;
3555	nid_t min, res;
3556
3557	/* Find next pin */
3558	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3559		;
3560	/* Check if there is no any left. If so - we succeeded. */
3561	if (i == 16)
3562		return (1);
3563
3564	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
3565	min = 0;
3566	do {
3567		HDA_BOOTHVERBOSE(
3568			device_printf(devinfo->dev,
3569			    " Tracing pin %d with min nid %d",
3570			    ases[as].pins[i], min);
3571			if (hpredir >= 0)
3572				printf(" and hpredir %d", hpredir);
3573			printf("\n");
3574		);
3575		/* Trace this pin taking min nid into account. */
3576		res = hdaa_audio_trace_dac(devinfo, as, i,
3577		    ases[as].pins[i], hpredir, min, 0, 0);
3578		if (res == 0) {
3579			/* If we failed - return to previous and redo it. */
3580			HDA_BOOTVERBOSE(
3581				device_printf(devinfo->dev,
3582				    " Unable to trace pin %d seq %d with min "
3583				    "nid %d",
3584				    ases[as].pins[i], i, min);
3585				if (hpredir >= 0)
3586					printf(" and hpredir %d", hpredir);
3587				printf("\n");
3588			);
3589			return (0);
3590		}
3591		HDA_BOOTVERBOSE(
3592			device_printf(devinfo->dev,
3593			    " Pin %d traced to DAC %d",
3594			    ases[as].pins[i], res);
3595			if (hpredir >= 0)
3596				printf(" and hpredir %d", hpredir);
3597			if (ases[as].fakeredir)
3598				printf(" with fake redirection");
3599			printf("\n");
3600		);
3601		/* Trace again to mark the path */
3602		hdaa_audio_trace_dac(devinfo, as, i,
3603		    ases[as].pins[i], hpredir, min, res, 0);
3604		ases[as].dacs[0][i] = res;
3605		/* We succeeded, so call next. */
3606		if (hdaa_audio_trace_as_out(devinfo, as, i + 1))
3607			return (1);
3608		/* If next failed, we should retry with next min */
3609		hdaa_audio_undo_trace(devinfo, as, i);
3610		ases[as].dacs[0][i] = 0;
3611		min = res + 1;
3612	} while (1);
3613}
3614
3615/*
3616 * Check equivalency of two DACs.
3617 */
3618static int
3619hdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3620{
3621	struct hdaa_devinfo *devinfo = w1->devinfo;
3622	struct hdaa_widget *w3;
3623	int i, j, c1, c2;
3624
3625	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3626		return (0);
3627	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3628		w3 = hdaa_widget_get(devinfo, i);
3629		if (w3 == NULL || w3->enable == 0)
3630			continue;
3631		if (w3->bindas != w1->bindas)
3632			continue;
3633		if (w3->nconns == 0)
3634			continue;
3635		c1 = c2 = -1;
3636		for (j = 0; j < w3->nconns; j++) {
3637			if (w3->connsenable[j] == 0)
3638				continue;
3639			if (w3->conns[j] == w1->nid)
3640				c1 = j;
3641			if (w3->conns[j] == w2->nid)
3642				c2 = j;
3643		}
3644		if (c1 < 0)
3645			continue;
3646		if (c2 < 0)
3647			return (0);
3648		if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3649			return (0);
3650	}
3651	return (1);
3652}
3653
3654/*
3655 * Check equivalency of two ADCs.
3656 */
3657static int
3658hdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3659{
3660	struct hdaa_devinfo *devinfo = w1->devinfo;
3661	struct hdaa_widget *w3, *w4;
3662	int i;
3663
3664	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3665		return (0);
3666	if (w1->nconns != 1 || w2->nconns != 1)
3667		return (0);
3668	if (w1->conns[0] == w2->conns[0])
3669		return (1);
3670	w3 = hdaa_widget_get(devinfo, w1->conns[0]);
3671	if (w3 == NULL || w3->enable == 0)
3672		return (0);
3673	w4 = hdaa_widget_get(devinfo, w2->conns[0]);
3674	if (w4 == NULL || w4->enable == 0)
3675		return (0);
3676	if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask)
3677		return (1);
3678	if (w4->bindas >= 0)
3679		return (0);
3680	if (w3->type != w4->type)
3681		return (0);
3682	if (memcmp(&w3->param, &w4->param, sizeof(w3->param)))
3683		return (0);
3684	if (w3->nconns != w4->nconns)
3685		return (0);
3686	for (i = 0; i < w3->nconns; i++) {
3687		if (w3->conns[i] != w4->conns[i])
3688			return (0);
3689	}
3690	return (1);
3691}
3692
3693/*
3694 * Look for equivalent DAC/ADC to implement second channel.
3695 */
3696static void
3697hdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid)
3698{
3699	struct hdaa_audio_as *as = &devinfo->as[asid];
3700	struct hdaa_widget *w1, *w2;
3701	int i, pos;
3702	nid_t nid1, nid2;
3703
3704	HDA_BOOTVERBOSE(
3705		device_printf(devinfo->dev,
3706		    "Looking for additional %sC "
3707		    "for association %d (%d)\n",
3708		    (as->dir == HDAA_CTL_OUT) ? "DA" : "AD",
3709		    asid, as->index);
3710	);
3711
3712	/* Find the exisitng DAC position and return if found more the one. */
3713	pos = -1;
3714	for (i = 0; i < 16; i++) {
3715		if (as->dacs[0][i] <= 0)
3716			continue;
3717		if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos])
3718			return;
3719		pos = i;
3720	}
3721
3722	nid1 = as->dacs[0][pos];
3723	w1 = hdaa_widget_get(devinfo, nid1);
3724	w2 = NULL;
3725	for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) {
3726		w2 = hdaa_widget_get(devinfo, nid2);
3727		if (w2 == NULL || w2->enable == 0)
3728			continue;
3729		if (w2->bindas >= 0)
3730			continue;
3731		if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
3732			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
3733				continue;
3734			if (hdaa_audio_dacs_equal(w1, w2))
3735				break;
3736		} else {
3737			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3738				continue;
3739			if (hdaa_audio_adcs_equal(w1, w2))
3740				break;
3741		}
3742	}
3743	if (nid2 >= devinfo->endnode)
3744		return;
3745	w2->bindas = w1->bindas;
3746	w2->bindseqmask = w1->bindseqmask;
3747	if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
3748		HDA_BOOTVERBOSE(
3749			device_printf(devinfo->dev,
3750			    " ADC %d considered equal to ADC %d\n", nid2, nid1);
3751		);
3752		w1 = hdaa_widget_get(devinfo, w1->conns[0]);
3753		w2 = hdaa_widget_get(devinfo, w2->conns[0]);
3754		w2->bindas = w1->bindas;
3755		w2->bindseqmask = w1->bindseqmask;
3756	} else {
3757		HDA_BOOTVERBOSE(
3758			device_printf(devinfo->dev,
3759			    " DAC %d considered equal to DAC %d\n", nid2, nid1);
3760		);
3761	}
3762	for (i = 0; i < 16; i++) {
3763		if (as->dacs[0][i] <= 0)
3764			continue;
3765		as->dacs[as->num_chans][i] = nid2;
3766	}
3767	as->num_chans++;
3768}
3769
3770/*
3771 * Trace association path from input to ADC
3772 */
3773static int
3774hdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as)
3775{
3776	struct hdaa_audio_as *ases = devinfo->as;
3777	struct hdaa_widget *w;
3778	int i, j, k, length;
3779
3780	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3781		w = hdaa_widget_get(devinfo, j);
3782		if (w == NULL || w->enable == 0)
3783			continue;
3784		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3785			continue;
3786		if (w->bindas >= 0 && w->bindas != as)
3787			continue;
3788
3789		/* Find next pin */
3790		for (i = 0; i < 16; i++) {
3791			if (ases[as].pins[i] == 0)
3792				continue;
3793
3794			HDA_BOOTHVERBOSE(
3795				device_printf(devinfo->dev,
3796				    " Tracing pin %d to ADC %d\n",
3797				    ases[as].pins[i], j);
3798			);
3799			/* Trace this pin taking goal into account. */
3800			if (hdaa_audio_trace_adc(devinfo, as, i,
3801			    ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) {
3802				/* If we failed - return to previous and redo it. */
3803				HDA_BOOTVERBOSE(
3804					device_printf(devinfo->dev,
3805					    " Unable to trace pin %d to ADC %d, undo traces\n",
3806					    ases[as].pins[i], j);
3807				);
3808				hdaa_audio_undo_trace(devinfo, as, -1);
3809				for (k = 0; k < 16; k++)
3810					ases[as].dacs[0][k] = 0;
3811				break;
3812			}
3813			HDA_BOOTVERBOSE(
3814				device_printf(devinfo->dev,
3815				    " Pin %d traced to ADC %d\n",
3816				    ases[as].pins[i], j);
3817			);
3818			ases[as].dacs[0][i] = j;
3819		}
3820		if (i == 16)
3821			return (1);
3822	}
3823	return (0);
3824}
3825
3826/*
3827 * Trace association path from input to multiple ADCs
3828 */
3829static int
3830hdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq)
3831{
3832	struct hdaa_audio_as *ases = devinfo->as;
3833	int i, length;
3834	nid_t min, res;
3835
3836	/* Find next pin */
3837	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3838		;
3839	/* Check if there is no any left. If so - we succeeded. */
3840	if (i == 16)
3841		return (1);
3842
3843	min = 0;
3844	do {
3845		HDA_BOOTHVERBOSE(
3846			device_printf(devinfo->dev,
3847			    " Tracing pin %d with min nid %d",
3848			    ases[as].pins[i], min);
3849			printf("\n");
3850		);
3851		/* Trace this pin taking min nid into account. */
3852		res = hdaa_audio_trace_adc(devinfo, as, i,
3853		    ases[as].pins[i], 0, min, 0, 0, &length, 0);
3854		if (res == 0) {
3855			/* If we failed - return to previous and redo it. */
3856			HDA_BOOTVERBOSE(
3857				device_printf(devinfo->dev,
3858				    " Unable to trace pin %d seq %d with min "
3859				    "nid %d",
3860				    ases[as].pins[i], i, min);
3861				printf("\n");
3862			);
3863			return (0);
3864		}
3865		HDA_BOOTVERBOSE(
3866			device_printf(devinfo->dev,
3867			    " Pin %d traced to ADC %d\n",
3868			    ases[as].pins[i], res);
3869		);
3870		/* Trace again to mark the path */
3871		hdaa_audio_trace_adc(devinfo, as, i,
3872		    ases[as].pins[i], 0, min, res, 0, &length, length);
3873		ases[as].dacs[0][i] = res;
3874		/* We succeeded, so call next. */
3875		if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1))
3876			return (1);
3877		/* If next failed, we should retry with next min */
3878		hdaa_audio_undo_trace(devinfo, as, i);
3879		ases[as].dacs[0][i] = 0;
3880		min = res + 1;
3881	} while (1);
3882}
3883
3884/*
3885 * Trace input monitor path from mixer to output association.
3886 */
3887static int
3888hdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth)
3889{
3890	struct hdaa_audio_as *ases = devinfo->as;
3891	struct hdaa_widget *w, *wc;
3892	int i, j;
3893	nid_t res = 0;
3894
3895	if (depth > HDA_PARSE_MAXDEPTH)
3896		return (0);
3897	w = hdaa_widget_get(devinfo, nid);
3898	if (w == NULL || w->enable == 0)
3899		return (0);
3900	HDA_BOOTHVERBOSE(
3901		device_printf(devinfo->dev,
3902		    " %*stracing via nid %d\n",
3903			depth + 1, "", w->nid);
3904	);
3905	/* Use only unused widgets */
3906	if (depth > 0 && w->bindas != -1) {
3907		if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) {
3908			HDA_BOOTHVERBOSE(
3909				device_printf(devinfo->dev,
3910				    " %*snid %d found output association %d\n",
3911					depth + 1, "", w->nid, w->bindas);
3912			);
3913			if (w->bindas >= 0)
3914				w->pflags |= HDAA_ADC_MONITOR;
3915			return (1);
3916		} else {
3917			HDA_BOOTHVERBOSE(
3918				device_printf(devinfo->dev,
3919				    " %*snid %d busy by input association %d\n",
3920					depth + 1, "", w->nid, w->bindas);
3921			);
3922			return (0);
3923		}
3924	}
3925
3926	switch (w->type) {
3927	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3928		/* Do not traverse input. AD1988 has digital monitor
3929		for which we are not ready. */
3930		break;
3931	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3932		if (depth > 0)
3933			break;
3934		/* Fall */
3935	default:
3936		/* Try to find reachable ADCs with specified nid. */
3937		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3938			wc = hdaa_widget_get(devinfo, j);
3939			if (wc == NULL || wc->enable == 0)
3940				continue;
3941			for (i = 0; i < wc->nconns; i++) {
3942				if (wc->connsenable[i] == 0)
3943					continue;
3944				if (wc->conns[i] != nid)
3945					continue;
3946				if (hdaa_audio_trace_to_out(devinfo,
3947				    j, depth + 1) != 0) {
3948					res = 1;
3949					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3950					    wc->selconn == -1)
3951						wc->selconn = i;
3952				}
3953			}
3954		}
3955		break;
3956	}
3957	if (res && w->bindas == -1)
3958		w->bindas = -2;
3959
3960	HDA_BOOTHVERBOSE(
3961		device_printf(devinfo->dev,
3962		    " %*snid %d returned %d\n",
3963			depth + 1, "", w->nid, res);
3964	);
3965	return (res);
3966}
3967
3968/*
3969 * Trace extra associations (beeper, monitor)
3970 */
3971static void
3972hdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo)
3973{
3974	struct hdaa_audio_as *as = devinfo->as;
3975	struct hdaa_widget *w;
3976	int j;
3977
3978	/* Input monitor */
3979	/* Find mixer associated with input, but supplying signal
3980	   for output associations. Hope it will be input monitor. */
3981	HDA_BOOTVERBOSE(
3982		device_printf(devinfo->dev,
3983		    "Tracing input monitor\n");
3984	);
3985	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3986		w = hdaa_widget_get(devinfo, j);
3987		if (w == NULL || w->enable == 0)
3988			continue;
3989		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3990			continue;
3991		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3992			continue;
3993		HDA_BOOTVERBOSE(
3994			device_printf(devinfo->dev,
3995			    " Tracing nid %d to out\n",
3996			    j);
3997		);
3998		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3999			HDA_BOOTVERBOSE(
4000				device_printf(devinfo->dev,
4001				    " nid %d is input monitor\n",
4002					w->nid);
4003			);
4004			w->ossdev = SOUND_MIXER_IMIX;
4005		}
4006	}
4007
4008	/* Other inputs monitor */
4009	/* Find input pins supplying signal for output associations.
4010	   Hope it will be input monitoring. */
4011	HDA_BOOTVERBOSE(
4012		device_printf(devinfo->dev,
4013		    "Tracing other input monitors\n");
4014	);
4015	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4016		w = hdaa_widget_get(devinfo, j);
4017		if (w == NULL || w->enable == 0)
4018			continue;
4019		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4020			continue;
4021		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
4022			continue;
4023		HDA_BOOTVERBOSE(
4024			device_printf(devinfo->dev,
4025			    " Tracing nid %d to out\n",
4026			    j);
4027		);
4028		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4029			HDA_BOOTVERBOSE(
4030				device_printf(devinfo->dev,
4031				    " nid %d is input monitor\n",
4032					w->nid);
4033			);
4034		}
4035	}
4036
4037	/* Beeper */
4038	HDA_BOOTVERBOSE(
4039		device_printf(devinfo->dev,
4040		    "Tracing beeper\n");
4041	);
4042	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4043		w = hdaa_widget_get(devinfo, j);
4044		if (w == NULL || w->enable == 0)
4045			continue;
4046		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
4047			continue;
4048		HDA_BOOTHVERBOSE(
4049			device_printf(devinfo->dev,
4050			    " Tracing nid %d to out\n",
4051			    j);
4052		);
4053		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4054			HDA_BOOTVERBOSE(
4055				device_printf(devinfo->dev,
4056				    " nid %d traced to out\n",
4057				    j);
4058			);
4059		}
4060		w->bindas = -2;
4061	}
4062}
4063
4064/*
4065 * Bind assotiations to PCM channels
4066 */
4067static void
4068hdaa_audio_bind_as(struct hdaa_devinfo *devinfo)
4069{
4070	struct hdaa_audio_as *as = devinfo->as;
4071	int i, j, cnt = 0, free;
4072
4073	for (j = 0; j < devinfo->ascnt; j++) {
4074		if (as[j].enable)
4075			cnt += as[j].num_chans;
4076	}
4077	if (devinfo->num_chans == 0) {
4078		devinfo->chans = (struct hdaa_chan *)malloc(
4079		    sizeof(struct hdaa_chan) * cnt,
4080		    M_HDAA, M_ZERO | M_NOWAIT);
4081		if (devinfo->chans == NULL) {
4082			device_printf(devinfo->dev,
4083			    "Channels memory allocation failed!\n");
4084			return;
4085		}
4086	} else {
4087		devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans,
4088		    sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt),
4089		    M_HDAA, M_ZERO | M_NOWAIT);
4090		if (devinfo->chans == NULL) {
4091			devinfo->num_chans = 0;
4092			device_printf(devinfo->dev,
4093			    "Channels memory allocation failed!\n");
4094			return;
4095		}
4096		/* Fixup relative pointers after realloc */
4097		for (j = 0; j < devinfo->num_chans; j++)
4098			devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist;
4099	}
4100	free = devinfo->num_chans;
4101	devinfo->num_chans += cnt;
4102
4103	for (j = free; j < free + cnt; j++) {
4104		devinfo->chans[j].devinfo = devinfo;
4105		devinfo->chans[j].as = -1;
4106	}
4107
4108	/* Assign associations in order of their numbers, */
4109	for (j = 0; j < devinfo->ascnt; j++) {
4110		if (as[j].enable == 0)
4111			continue;
4112		for (i = 0; i < as[j].num_chans; i++) {
4113			devinfo->chans[free].as = j;
4114			devinfo->chans[free].asindex = i;
4115			devinfo->chans[free].dir =
4116			    (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
4117			hdaa_pcmchannel_setup(&devinfo->chans[free]);
4118			as[j].chans[i] = free;
4119			free++;
4120		}
4121	}
4122}
4123
4124static void
4125hdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo)
4126{
4127	struct hdaa_widget *w;
4128	int i;
4129
4130	/* Disable power and volume widgets. */
4131	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4132		w = hdaa_widget_get(devinfo, i);
4133		if (w == NULL || w->enable == 0)
4134			continue;
4135		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
4136		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
4137			w->enable = 0;
4138			HDA_BOOTHVERBOSE(
4139				device_printf(devinfo->dev,
4140				    " Disabling nid %d due to it's"
4141				    " non-audio type.\n",
4142				    w->nid);
4143			);
4144		}
4145	}
4146}
4147
4148static void
4149hdaa_audio_disable_useless(struct hdaa_devinfo *devinfo)
4150{
4151	struct hdaa_widget *w, *cw;
4152	struct hdaa_audio_ctl *ctl;
4153	int done, found, i, j, k;
4154
4155	/* Disable useless pins. */
4156	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4157		w = hdaa_widget_get(devinfo, i);
4158		if (w == NULL || w->enable == 0)
4159			continue;
4160		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
4161			if ((w->wclass.pin.config &
4162			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
4163			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
4164				w->enable = 0;
4165				HDA_BOOTHVERBOSE(
4166					device_printf(devinfo->dev,
4167					    " Disabling pin nid %d due"
4168					    " to None connectivity.\n",
4169					    w->nid);
4170				);
4171			} else if ((w->wclass.pin.config &
4172			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
4173				w->enable = 0;
4174				HDA_BOOTHVERBOSE(
4175					device_printf(devinfo->dev,
4176					    " Disabling unassociated"
4177					    " pin nid %d.\n",
4178					    w->nid);
4179				);
4180			}
4181		}
4182	}
4183	do {
4184		done = 1;
4185		/* Disable and mute controls for disabled widgets. */
4186		i = 0;
4187		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4188			if (ctl->enable == 0)
4189				continue;
4190			if (ctl->widget->enable == 0 ||
4191			    (ctl->childwidget != NULL &&
4192			    ctl->childwidget->enable == 0)) {
4193				ctl->forcemute = 1;
4194				ctl->muted = HDAA_AMP_MUTE_ALL;
4195				ctl->left = 0;
4196				ctl->right = 0;
4197				ctl->enable = 0;
4198				if (ctl->ndir == HDAA_CTL_IN)
4199					ctl->widget->connsenable[ctl->index] = 0;
4200				done = 0;
4201				HDA_BOOTHVERBOSE(
4202					device_printf(devinfo->dev,
4203					    " Disabling ctl %d nid %d cnid %d due"
4204					    " to disabled widget.\n", i,
4205					    ctl->widget->nid,
4206					    (ctl->childwidget != NULL)?
4207					    ctl->childwidget->nid:-1);
4208				);
4209			}
4210		}
4211		/* Disable useless widgets. */
4212		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4213			w = hdaa_widget_get(devinfo, i);
4214			if (w == NULL || w->enable == 0)
4215				continue;
4216			/* Disable inputs with disabled child widgets. */
4217			for (j = 0; j < w->nconns; j++) {
4218				if (w->connsenable[j]) {
4219					cw = hdaa_widget_get(devinfo, w->conns[j]);
4220					if (cw == NULL || cw->enable == 0) {
4221						w->connsenable[j] = 0;
4222						HDA_BOOTHVERBOSE(
4223							device_printf(devinfo->dev,
4224							    " Disabling nid %d connection %d due"
4225							    " to disabled child widget.\n",
4226							    i, j);
4227						);
4228					}
4229				}
4230			}
4231			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4232			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4233				continue;
4234			/* Disable mixers and selectors without inputs. */
4235			found = 0;
4236			for (j = 0; j < w->nconns; j++) {
4237				if (w->connsenable[j]) {
4238					found = 1;
4239					break;
4240				}
4241			}
4242			if (found == 0) {
4243				w->enable = 0;
4244				done = 0;
4245				HDA_BOOTHVERBOSE(
4246					device_printf(devinfo->dev,
4247					    " Disabling nid %d due to all it's"
4248					    " inputs disabled.\n", w->nid);
4249				);
4250			}
4251			/* Disable nodes without consumers. */
4252			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4253			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4254				continue;
4255			found = 0;
4256			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4257				cw = hdaa_widget_get(devinfo, k);
4258				if (cw == NULL || cw->enable == 0)
4259					continue;
4260				for (j = 0; j < cw->nconns; j++) {
4261					if (cw->connsenable[j] && cw->conns[j] == i) {
4262						found = 1;
4263						break;
4264					}
4265				}
4266			}
4267			if (found == 0) {
4268				w->enable = 0;
4269				done = 0;
4270				HDA_BOOTHVERBOSE(
4271					device_printf(devinfo->dev,
4272					    " Disabling nid %d due to all it's"
4273					    " consumers disabled.\n", w->nid);
4274				);
4275			}
4276		}
4277	} while (done == 0);
4278
4279}
4280
4281static void
4282hdaa_audio_disable_unas(struct hdaa_devinfo *devinfo)
4283{
4284	struct hdaa_audio_as *as = devinfo->as;
4285	struct hdaa_widget *w, *cw;
4286	struct hdaa_audio_ctl *ctl;
4287	int i, j, k;
4288
4289	/* Disable unassosiated widgets. */
4290	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4291		w = hdaa_widget_get(devinfo, i);
4292		if (w == NULL || w->enable == 0)
4293			continue;
4294		if (w->bindas == -1) {
4295			w->enable = 0;
4296			HDA_BOOTHVERBOSE(
4297				device_printf(devinfo->dev,
4298				    " Disabling unassociated nid %d.\n",
4299				    w->nid);
4300			);
4301		}
4302	}
4303	/* Disable input connections on input pin and
4304	 * output on output. */
4305	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4306		w = hdaa_widget_get(devinfo, i);
4307		if (w == NULL || w->enable == 0)
4308			continue;
4309		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4310			continue;
4311		if (w->bindas < 0)
4312			continue;
4313		if (as[w->bindas].dir == HDAA_CTL_IN) {
4314			for (j = 0; j < w->nconns; j++) {
4315				if (w->connsenable[j] == 0)
4316					continue;
4317				w->connsenable[j] = 0;
4318				HDA_BOOTHVERBOSE(
4319					device_printf(devinfo->dev,
4320					    " Disabling connection to input pin "
4321					    "nid %d conn %d.\n",
4322					    i, j);
4323				);
4324			}
4325			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4326			    HDAA_CTL_IN, -1, 1);
4327			if (ctl && ctl->enable) {
4328				ctl->forcemute = 1;
4329				ctl->muted = HDAA_AMP_MUTE_ALL;
4330				ctl->left = 0;
4331				ctl->right = 0;
4332				ctl->enable = 0;
4333			}
4334		} else {
4335			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4336			    HDAA_CTL_OUT, -1, 1);
4337			if (ctl && ctl->enable) {
4338				ctl->forcemute = 1;
4339				ctl->muted = HDAA_AMP_MUTE_ALL;
4340				ctl->left = 0;
4341				ctl->right = 0;
4342				ctl->enable = 0;
4343			}
4344			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4345				cw = hdaa_widget_get(devinfo, k);
4346				if (cw == NULL || cw->enable == 0)
4347					continue;
4348				for (j = 0; j < cw->nconns; j++) {
4349					if (cw->connsenable[j] && cw->conns[j] == i) {
4350						cw->connsenable[j] = 0;
4351						HDA_BOOTHVERBOSE(
4352							device_printf(devinfo->dev,
4353							    " Disabling connection from output pin "
4354							    "nid %d conn %d cnid %d.\n",
4355							    k, j, i);
4356						);
4357						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4358						    cw->nconns > 1)
4359							continue;
4360						ctl = hdaa_audio_ctl_amp_get(devinfo, k,
4361					    HDAA_CTL_IN, j, 1);
4362						if (ctl && ctl->enable) {
4363							ctl->forcemute = 1;
4364							ctl->muted = HDAA_AMP_MUTE_ALL;
4365							ctl->left = 0;
4366							ctl->right = 0;
4367							ctl->enable = 0;
4368						}
4369					}
4370				}
4371			}
4372		}
4373	}
4374}
4375
4376static void
4377hdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo)
4378{
4379	struct hdaa_audio_as *as = devinfo->as;
4380	struct hdaa_widget *w;
4381	int i, j;
4382
4383	/* On playback path we can safely disable all unseleted inputs. */
4384	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4385		w = hdaa_widget_get(devinfo, i);
4386		if (w == NULL || w->enable == 0)
4387			continue;
4388		if (w->nconns <= 1)
4389			continue;
4390		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4391			continue;
4392		if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN)
4393			continue;
4394		for (j = 0; j < w->nconns; j++) {
4395			if (w->connsenable[j] == 0)
4396				continue;
4397			if (w->selconn < 0 || w->selconn == j)
4398				continue;
4399			w->connsenable[j] = 0;
4400			HDA_BOOTHVERBOSE(
4401				device_printf(devinfo->dev,
4402				    " Disabling unselected connection "
4403				    "nid %d conn %d.\n",
4404				    i, j);
4405			);
4406		}
4407	}
4408}
4409
4410static void
4411hdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo)
4412{
4413	struct hdaa_audio_as *ases = devinfo->as;
4414	struct hdaa_widget *w, *cw;
4415	struct hdaa_audio_ctl *ctl;
4416	int i, j;
4417
4418	/* Disable crossassociatement and unwanted crosschannel connections. */
4419	/* ... using selectors */
4420	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4421		w = hdaa_widget_get(devinfo, i);
4422		if (w == NULL || w->enable == 0)
4423			continue;
4424		if (w->nconns <= 1)
4425			continue;
4426		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4427			continue;
4428		/* Allow any -> mix */
4429		if (w->bindas == -2)
4430			continue;
4431		for (j = 0; j < w->nconns; j++) {
4432			if (w->connsenable[j] == 0)
4433				continue;
4434			cw = hdaa_widget_get(devinfo, w->conns[j]);
4435			if (cw == NULL || w->enable == 0)
4436				continue;
4437			/* Allow mix -> out. */
4438			if (cw->bindas == -2 && w->bindas >= 0 &&
4439			    ases[w->bindas].dir == HDAA_CTL_OUT)
4440				continue;
4441			/* Allow mix -> mixed-in. */
4442			if (cw->bindas == -2 && w->bindas >= 0 &&
4443			    ases[w->bindas].mixed)
4444				continue;
4445			/* Allow in -> mix. */
4446			if ((w->pflags & HDAA_ADC_MONITOR) &&
4447			     cw->bindas >= 0 &&
4448			     ases[cw->bindas].dir == HDAA_CTL_IN)
4449				continue;
4450			/* Allow if have common as/seqs. */
4451			if (w->bindas == cw->bindas &&
4452			    (w->bindseqmask & cw->bindseqmask) != 0)
4453				continue;
4454			w->connsenable[j] = 0;
4455			HDA_BOOTHVERBOSE(
4456				device_printf(devinfo->dev,
4457				    " Disabling crossassociatement connection "
4458				    "nid %d conn %d cnid %d.\n",
4459				    i, j, cw->nid);
4460			);
4461		}
4462	}
4463	/* ... using controls */
4464	i = 0;
4465	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4466		if (ctl->enable == 0 || ctl->childwidget == NULL)
4467			continue;
4468		/* Allow any -> mix */
4469		if (ctl->widget->bindas == -2)
4470			continue;
4471		/* Allow mix -> out. */
4472		if (ctl->childwidget->bindas == -2 &&
4473		    ctl->widget->bindas >= 0 &&
4474		    ases[ctl->widget->bindas].dir == HDAA_CTL_OUT)
4475			continue;
4476		/* Allow mix -> mixed-in. */
4477		if (ctl->childwidget->bindas == -2 &&
4478		    ctl->widget->bindas >= 0 &&
4479		    ases[ctl->widget->bindas].mixed)
4480			continue;
4481		/* Allow in -> mix. */
4482		if ((ctl->widget->pflags & HDAA_ADC_MONITOR) &&
4483		    ctl->childwidget->bindas >= 0 &&
4484		    ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN)
4485			continue;
4486		/* Allow if have common as/seqs. */
4487		if (ctl->widget->bindas == ctl->childwidget->bindas &&
4488		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0)
4489			continue;
4490		ctl->forcemute = 1;
4491		ctl->muted = HDAA_AMP_MUTE_ALL;
4492		ctl->left = 0;
4493		ctl->right = 0;
4494		ctl->enable = 0;
4495		if (ctl->ndir == HDAA_CTL_IN)
4496			ctl->widget->connsenable[ctl->index] = 0;
4497		HDA_BOOTHVERBOSE(
4498			device_printf(devinfo->dev,
4499			    " Disabling crossassociatement connection "
4500			    "ctl %d nid %d cnid %d.\n", i,
4501			    ctl->widget->nid,
4502			    ctl->childwidget->nid);
4503		);
4504	}
4505
4506}
4507
4508/*
4509 * Find controls to control amplification for source and calculate possible
4510 * amplification range.
4511 */
4512static int
4513hdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4514    int ossdev, int ctlable, int depth, int *minamp, int *maxamp)
4515{
4516	struct hdaa_widget *w, *wc;
4517	struct hdaa_audio_ctl *ctl;
4518	int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4519
4520	if (depth > HDA_PARSE_MAXDEPTH)
4521		return (found);
4522
4523	w = hdaa_widget_get(devinfo, nid);
4524	if (w == NULL || w->enable == 0)
4525		return (found);
4526
4527	/* Count number of active inputs. */
4528	if (depth > 0) {
4529		for (j = 0; j < w->nconns; j++) {
4530			if (!w->connsenable[j])
4531				continue;
4532			conns++;
4533		}
4534	}
4535
4536	/* If this is not a first step - use input mixer.
4537	   Pins have common input ctl so care must be taken. */
4538	if (depth > 0 && ctlable && (conns == 1 ||
4539	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
4540		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
4541		    index, 1);
4542		if (ctl) {
4543			ctl->ossmask |= (1 << ossdev);
4544			found++;
4545			if (*minamp == *maxamp) {
4546				*minamp += MINQDB(ctl);
4547				*maxamp += MAXQDB(ctl);
4548			}
4549		}
4550	}
4551
4552	/* If widget has own ossdev - not traverse it.
4553	   It will be traversed on it's own. */
4554	if (w->ossdev >= 0 && depth > 0)
4555		return (found);
4556
4557	/* We must not traverse pin */
4558	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4559	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
4560	    depth > 0)
4561		return (found);
4562
4563	/* record that this widget exports such signal, */
4564	w->ossmask |= (1 << ossdev);
4565
4566	/*
4567	 * If signals mixed, we can't assign controls farther.
4568	 * Ignore this on depth zero. Caller must knows why.
4569	 */
4570	if (conns > 1 &&
4571	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4572		ctlable = 0;
4573
4574	if (ctlable) {
4575		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
4576		if (ctl) {
4577			ctl->ossmask |= (1 << ossdev);
4578			found++;
4579			if (*minamp == *maxamp) {
4580				*minamp += MINQDB(ctl);
4581				*maxamp += MAXQDB(ctl);
4582			}
4583		}
4584	}
4585
4586	cminamp = cmaxamp = 0;
4587	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4588		wc = hdaa_widget_get(devinfo, i);
4589		if (wc == NULL || wc->enable == 0)
4590			continue;
4591		for (j = 0; j < wc->nconns; j++) {
4592			if (wc->connsenable[j] && wc->conns[j] == nid) {
4593				tminamp = tmaxamp = 0;
4594				found += hdaa_audio_ctl_source_amp(devinfo,
4595				    wc->nid, j, ossdev, ctlable, depth + 1,
4596				    &tminamp, &tmaxamp);
4597				if (cminamp == 0 && cmaxamp == 0) {
4598					cminamp = tminamp;
4599					cmaxamp = tmaxamp;
4600				} else if (tminamp != tmaxamp) {
4601					cminamp = imax(cminamp, tminamp);
4602					cmaxamp = imin(cmaxamp, tmaxamp);
4603				}
4604			}
4605		}
4606	}
4607	if (*minamp == *maxamp && cminamp < cmaxamp) {
4608		*minamp += cminamp;
4609		*maxamp += cmaxamp;
4610	}
4611	return (found);
4612}
4613
4614/*
4615 * Find controls to control amplification for destination and calculate
4616 * possible amplification range.
4617 */
4618static int
4619hdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4620    int ossdev, int depth, int *minamp, int *maxamp)
4621{
4622	struct hdaa_audio_as *as = devinfo->as;
4623	struct hdaa_widget *w, *wc;
4624	struct hdaa_audio_ctl *ctl;
4625	int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4626
4627	if (depth > HDA_PARSE_MAXDEPTH)
4628		return (found);
4629
4630	w = hdaa_widget_get(devinfo, nid);
4631	if (w == NULL || w->enable == 0)
4632		return (found);
4633
4634	if (depth > 0) {
4635		/* If this node produce output for several consumers,
4636		   we can't touch it. */
4637		consumers = 0;
4638		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4639			wc = hdaa_widget_get(devinfo, i);
4640			if (wc == NULL || wc->enable == 0)
4641				continue;
4642			for (j = 0; j < wc->nconns; j++) {
4643				if (wc->connsenable[j] && wc->conns[j] == nid)
4644					consumers++;
4645			}
4646		}
4647		/* The only exception is if real HP redirection is configured
4648		   and this is a duplication point.
4649		   XXX: Actually exception is not completely correct.
4650		   XXX: Duplication point check is not perfect. */
4651		if ((consumers == 2 && (w->bindas < 0 ||
4652		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
4653		    (w->bindseqmask & (1 << 15)) == 0)) ||
4654		    consumers > 2)
4655			return (found);
4656
4657		/* Else use it's output mixer. */
4658		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4659		    HDAA_CTL_OUT, -1, 1);
4660		if (ctl) {
4661			ctl->ossmask |= (1 << ossdev);
4662			found++;
4663			if (*minamp == *maxamp) {
4664				*minamp += MINQDB(ctl);
4665				*maxamp += MAXQDB(ctl);
4666			}
4667		}
4668	}
4669
4670	/* We must not traverse pin */
4671	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4672	    depth > 0)
4673		return (found);
4674
4675	cminamp = cmaxamp = 0;
4676	for (i = 0; i < w->nconns; i++) {
4677		if (w->connsenable[i] == 0)
4678			continue;
4679		if (index >= 0 && i != index)
4680			continue;
4681		tminamp = tmaxamp = 0;
4682		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4683		    HDAA_CTL_IN, i, 1);
4684		if (ctl) {
4685			ctl->ossmask |= (1 << ossdev);
4686			found++;
4687			if (*minamp == *maxamp) {
4688				tminamp += MINQDB(ctl);
4689				tmaxamp += MAXQDB(ctl);
4690			}
4691		}
4692		found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
4693		    depth + 1, &tminamp, &tmaxamp);
4694		if (cminamp == 0 && cmaxamp == 0) {
4695			cminamp = tminamp;
4696			cmaxamp = tmaxamp;
4697		} else if (tminamp != tmaxamp) {
4698			cminamp = imax(cminamp, tminamp);
4699			cmaxamp = imin(cmaxamp, tmaxamp);
4700		}
4701	}
4702	if (*minamp == *maxamp && cminamp < cmaxamp) {
4703		*minamp += cminamp;
4704		*maxamp += cmaxamp;
4705	}
4706	return (found);
4707}
4708
4709/*
4710 * Assign OSS names to sound sources
4711 */
4712static void
4713hdaa_audio_assign_names(struct hdaa_devinfo *devinfo)
4714{
4715	struct hdaa_audio_as *as = devinfo->as;
4716	struct hdaa_widget *w;
4717	int i, j;
4718	int type = -1, use, used = 0;
4719	static const int types[7][13] = {
4720	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4721	      SOUND_MIXER_LINE3, -1 },	/* line */
4722	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
4723	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
4724	    { SOUND_MIXER_CD, -1 },	/* cd */
4725	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
4726	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
4727	      -1 },	/* digital */
4728	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4729	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
4730	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
4731	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
4732	      -1 }	/* others */
4733	};
4734
4735	/* Surely known names */
4736	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4737		w = hdaa_widget_get(devinfo, i);
4738		if (w == NULL || w->enable == 0)
4739			continue;
4740		if (w->bindas == -1)
4741			continue;
4742		use = -1;
4743		switch (w->type) {
4744		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4745			if (as[w->bindas].dir == HDAA_CTL_OUT)
4746				break;
4747			type = -1;
4748			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4749			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4750				type = 0;
4751				break;
4752			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4753				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
4754				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4755					break;
4756				type = 1;
4757				break;
4758			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4759				type = 3;
4760				break;
4761			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4762				type = 4;
4763				break;
4764			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
4765			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
4766				type = 5;
4767				break;
4768			}
4769			if (type == -1)
4770				break;
4771			j = 0;
4772			while (types[type][j] >= 0 &&
4773			    (used & (1 << types[type][j])) != 0) {
4774				j++;
4775			}
4776			if (types[type][j] >= 0)
4777				use = types[type][j];
4778			break;
4779		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4780			use = SOUND_MIXER_PCM;
4781			break;
4782		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
4783			use = SOUND_MIXER_SPEAKER;
4784			break;
4785		default:
4786			break;
4787		}
4788		if (use >= 0) {
4789			w->ossdev = use;
4790			used |= (1 << use);
4791		}
4792	}
4793	/* Semi-known names */
4794	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4795		w = hdaa_widget_get(devinfo, i);
4796		if (w == NULL || w->enable == 0)
4797			continue;
4798		if (w->ossdev >= 0)
4799			continue;
4800		if (w->bindas == -1)
4801			continue;
4802		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4803			continue;
4804		if (as[w->bindas].dir == HDAA_CTL_OUT)
4805			continue;
4806		type = -1;
4807		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4808		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
4809		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4810		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
4811		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
4812			type = 0;
4813			break;
4814		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4815			type = 2;
4816			break;
4817		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
4818		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
4819			type = 5;
4820			break;
4821		}
4822		if (type == -1)
4823			break;
4824		j = 0;
4825		while (types[type][j] >= 0 &&
4826		    (used & (1 << types[type][j])) != 0) {
4827			j++;
4828		}
4829		if (types[type][j] >= 0) {
4830			w->ossdev = types[type][j];
4831			used |= (1 << types[type][j]);
4832		}
4833	}
4834	/* Others */
4835	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4836		w = hdaa_widget_get(devinfo, i);
4837		if (w == NULL || w->enable == 0)
4838			continue;
4839		if (w->ossdev >= 0)
4840			continue;
4841		if (w->bindas == -1)
4842			continue;
4843		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4844			continue;
4845		if (as[w->bindas].dir == HDAA_CTL_OUT)
4846			continue;
4847		j = 0;
4848		while (types[6][j] >= 0 &&
4849		    (used & (1 << types[6][j])) != 0) {
4850			j++;
4851		}
4852		if (types[6][j] >= 0) {
4853			w->ossdev = types[6][j];
4854			used |= (1 << types[6][j]);
4855		}
4856	}
4857}
4858
4859static void
4860hdaa_audio_build_tree(struct hdaa_devinfo *devinfo)
4861{
4862	struct hdaa_audio_as *as = devinfo->as;
4863	int j, res;
4864
4865	/* Trace all associations in order of their numbers. */
4866	for (j = 0; j < devinfo->ascnt; j++) {
4867		if (as[j].enable == 0)
4868			continue;
4869		HDA_BOOTVERBOSE(
4870			device_printf(devinfo->dev,
4871			    "Tracing association %d (%d)\n", j, as[j].index);
4872		);
4873		if (as[j].dir == HDAA_CTL_OUT) {
4874retry:
4875			res = hdaa_audio_trace_as_out(devinfo, j, 0);
4876			if (res == 0 && as[j].hpredir >= 0 &&
4877			    as[j].fakeredir == 0) {
4878				/* If CODEC can't do analog HP redirection
4879				   try to make it using one more DAC. */
4880				as[j].fakeredir = 1;
4881				goto retry;
4882			}
4883		} else if (as[j].mixed)
4884			res = hdaa_audio_trace_as_in(devinfo, j);
4885		else
4886			res = hdaa_audio_trace_as_in_mch(devinfo, j, 0);
4887		if (res) {
4888			HDA_BOOTVERBOSE(
4889				device_printf(devinfo->dev,
4890				    "Association %d (%d) trace succeeded\n",
4891				    j, as[j].index);
4892			);
4893		} else {
4894			HDA_BOOTVERBOSE(
4895				device_printf(devinfo->dev,
4896				    "Association %d (%d) trace failed\n",
4897				    j, as[j].index);
4898			);
4899			as[j].enable = 0;
4900		}
4901	}
4902
4903	/* Look for additional DACs/ADCs. */
4904	for (j = 0; j < devinfo->ascnt; j++) {
4905		if (as[j].enable == 0)
4906			continue;
4907		hdaa_audio_adddac(devinfo, j);
4908	}
4909
4910	/* Trace mixer and beeper pseudo associations. */
4911	hdaa_audio_trace_as_extra(devinfo);
4912}
4913
4914/*
4915 * Store in pdevinfo new data about whether and how we can control signal
4916 * for OSS device to/from specified widget.
4917 */
4918static void
4919hdaa_adjust_amp(struct hdaa_widget *w, int ossdev,
4920    int found, int minamp, int maxamp)
4921{
4922	struct hdaa_devinfo *devinfo = w->devinfo;
4923	struct hdaa_pcm_devinfo *pdevinfo;
4924
4925	if (w->bindas >= 0)
4926		pdevinfo = devinfo->as[w->bindas].pdevinfo;
4927	else
4928		pdevinfo = &devinfo->devs[0];
4929	if (found)
4930		pdevinfo->ossmask |= (1 << ossdev);
4931	if (minamp == 0 && maxamp == 0)
4932		return;
4933	if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) {
4934		pdevinfo->minamp[ossdev] = minamp;
4935		pdevinfo->maxamp[ossdev] = maxamp;
4936	} else {
4937		pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp);
4938		pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp);
4939	}
4940}
4941
4942/*
4943 * Trace signals from/to all possible sources/destionstions to find possible
4944 * recording sources, OSS device control ranges and to assign controls.
4945 */
4946static void
4947hdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo)
4948{
4949	struct hdaa_audio_as *as = devinfo->as;
4950	struct hdaa_widget *w, *cw;
4951	int i, j, minamp, maxamp, found;
4952
4953	/* Assign mixers to the tree. */
4954	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4955		w = hdaa_widget_get(devinfo, i);
4956		if (w == NULL || w->enable == 0)
4957			continue;
4958		minamp = maxamp = 0;
4959		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
4960		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
4961		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4962		    as[w->bindas].dir == HDAA_CTL_IN)) {
4963			if (w->ossdev < 0)
4964				continue;
4965			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4966			    w->ossdev, 1, 0, &minamp, &maxamp);
4967			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4968		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
4969			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4970			    SOUND_MIXER_RECLEV, 0, &minamp, &maxamp);
4971			hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp);
4972		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4973		    as[w->bindas].dir == HDAA_CTL_OUT) {
4974			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4975			    SOUND_MIXER_VOLUME, 0, &minamp, &maxamp);
4976			hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp);
4977		}
4978		if (w->ossdev == SOUND_MIXER_IMIX) {
4979			minamp = maxamp = 0;
4980			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4981			    w->ossdev, 1, 0, &minamp, &maxamp);
4982			if (minamp == maxamp) {
4983				/* If we are unable to control input monitor
4984				   as source - try to control it as destination. */
4985				found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4986				    w->ossdev, 0, &minamp, &maxamp);
4987				w->pflags |= HDAA_IMIX_AS_DST;
4988			}
4989			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4990		}
4991		if (w->pflags & HDAA_ADC_MONITOR) {
4992			for (j = 0; j < w->nconns; j++) {
4993				if (!w->connsenable[j])
4994				    continue;
4995				cw = hdaa_widget_get(devinfo, w->conns[j]);
4996				if (cw == NULL || cw->enable == 0)
4997				    continue;
4998				if (cw->bindas == -1)
4999				    continue;
5000				if (cw->bindas >= 0 &&
5001				    as[cw->bindas].dir != HDAA_CTL_IN)
5002					continue;
5003				minamp = maxamp = 0;
5004				found = hdaa_audio_ctl_dest_amp(devinfo,
5005				    w->nid, j, SOUND_MIXER_IGAIN, 0,
5006				    &minamp, &maxamp);
5007				hdaa_adjust_amp(w, SOUND_MIXER_IGAIN,
5008				    found, minamp, maxamp);
5009			}
5010		}
5011	}
5012}
5013
5014static void
5015hdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo)
5016{
5017	struct hdaa_audio_as *as = devinfo->as;
5018	struct hdaa_widget *w;
5019	uint32_t pincap;
5020	int i;
5021
5022	for (i = 0; i < devinfo->nodecnt; i++) {
5023		w = &devinfo->widget[i];
5024		if (w == NULL)
5025			continue;
5026		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
5027		    w->waspin == 0)
5028			continue;
5029
5030		pincap = w->wclass.pin.cap;
5031
5032		/* Disable everything. */
5033		w->wclass.pin.ctrl &= ~(
5034		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
5035		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
5036		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
5037		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
5038
5039		if (w->enable == 0) {
5040			/* Pin is unused so left it disabled. */
5041			continue;
5042		} else if (w->waspin) {
5043			/* Enable input for beeper input. */
5044			w->wclass.pin.ctrl |=
5045			    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5046		} else if (w->bindas < 0 || as[w->bindas].enable == 0) {
5047			/* Pin is unused so left it disabled. */
5048			continue;
5049		} else if (as[w->bindas].dir == HDAA_CTL_IN) {
5050			/* Input pin, configure for input. */
5051			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5052				w->wclass.pin.ctrl |=
5053				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5054
5055			if ((devinfo->quirks & HDAA_QUIRK_IVREF100) &&
5056			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5057				w->wclass.pin.ctrl |=
5058				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5059				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5060			else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) &&
5061			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5062				w->wclass.pin.ctrl |=
5063				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5064				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5065			else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) &&
5066			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5067				w->wclass.pin.ctrl |=
5068				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5069				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5070		} else {
5071			/* Output pin, configure for output. */
5072			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5073				w->wclass.pin.ctrl |=
5074				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
5075
5076			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
5077			    (w->wclass.pin.config &
5078			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
5079			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
5080				w->wclass.pin.ctrl |=
5081				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
5082
5083			if ((devinfo->quirks & HDAA_QUIRK_OVREF100) &&
5084			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5085				w->wclass.pin.ctrl |=
5086				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5087				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5088			else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) &&
5089			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5090				w->wclass.pin.ctrl |=
5091				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5092				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5093			else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) &&
5094			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5095				w->wclass.pin.ctrl |=
5096				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5097				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5098		}
5099	}
5100}
5101
5102static void
5103hdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo)
5104{
5105	struct hdaa_audio_ctl *ctl;
5106	int i, z;
5107
5108	i = 0;
5109	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5110		if (ctl->enable == 0 || ctl->ossmask != 0) {
5111			/* Mute disabled and mixer controllable controls.
5112			 * Last will be initialized by mixer_init().
5113			 * This expected to reduce click on startup. */
5114			hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0);
5115			continue;
5116		}
5117		/* Init fixed controls to 0dB amplification. */
5118		z = ctl->offset;
5119		if (z > ctl->step)
5120			z = ctl->step;
5121		hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z);
5122	}
5123}
5124
5125static void
5126hdaa_gpio_commit(struct hdaa_devinfo *devinfo)
5127{
5128	uint32_t gdata, gmask, gdir;
5129	int i, numgpio;
5130
5131	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5132	if (devinfo->gpio != 0 && numgpio != 0) {
5133		gdata = hda_command(devinfo->dev,
5134		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
5135		gmask = hda_command(devinfo->dev,
5136		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
5137		gdir = hda_command(devinfo->dev,
5138		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
5139		for (i = 0; i < numgpio; i++) {
5140			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5141			    HDAA_GPIO_SET(i)) {
5142				gdata |= (1 << i);
5143				gmask |= (1 << i);
5144				gdir |= (1 << i);
5145			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5146			    HDAA_GPIO_CLEAR(i)) {
5147				gdata &= ~(1 << i);
5148				gmask |= (1 << i);
5149				gdir |= (1 << i);
5150			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5151			    HDAA_GPIO_DISABLE(i)) {
5152				gmask &= ~(1 << i);
5153			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5154			    HDAA_GPIO_INPUT(i)) {
5155				gmask |= (1 << i);
5156				gdir &= ~(1 << i);
5157			}
5158		}
5159		HDA_BOOTVERBOSE(
5160			device_printf(devinfo->dev, "GPIO commit\n");
5161		);
5162		hda_command(devinfo->dev,
5163		    HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask));
5164		hda_command(devinfo->dev,
5165		    HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir));
5166		hda_command(devinfo->dev,
5167		    HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata));
5168		HDA_BOOTVERBOSE(
5169			hdaa_dump_gpio(devinfo);
5170		);
5171	}
5172}
5173
5174static void
5175hdaa_gpo_commit(struct hdaa_devinfo *devinfo)
5176{
5177	uint32_t gdata;
5178	int i, numgpo;
5179
5180	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
5181	if (devinfo->gpo != 0 && numgpo != 0) {
5182		gdata = hda_command(devinfo->dev,
5183		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
5184		for (i = 0; i < numgpo; i++) {
5185			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5186			    HDAA_GPIO_SET(i)) {
5187				gdata |= (1 << i);
5188			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5189			    HDAA_GPIO_CLEAR(i)) {
5190				gdata &= ~(1 << i);
5191			}
5192		}
5193		HDA_BOOTVERBOSE(
5194			device_printf(devinfo->dev, "GPO commit\n");
5195		);
5196		hda_command(devinfo->dev,
5197		    HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata));
5198		HDA_BOOTVERBOSE(
5199			hdaa_dump_gpo(devinfo);
5200		);
5201	}
5202}
5203
5204static void
5205hdaa_audio_commit(struct hdaa_devinfo *devinfo)
5206{
5207	struct hdaa_widget *w;
5208	int i;
5209
5210	/* Commit controls. */
5211	hdaa_audio_ctl_commit(devinfo);
5212
5213	/* Commit selectors, pins and EAPD. */
5214	for (i = 0; i < devinfo->nodecnt; i++) {
5215		w = &devinfo->widget[i];
5216		if (w == NULL)
5217			continue;
5218		if (w->selconn == -1)
5219			w->selconn = 0;
5220		if (w->nconns > 0)
5221			hdaa_widget_connection_select(w, w->selconn);
5222		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
5223		    w->waspin) {
5224			hda_command(devinfo->dev,
5225			    HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid,
5226			    w->wclass.pin.ctrl));
5227		}
5228		if (w->param.eapdbtl != HDA_INVALID) {
5229			uint32_t val;
5230
5231			val = w->param.eapdbtl;
5232			if (devinfo->quirks &
5233			    HDAA_QUIRK_EAPDINV)
5234				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
5235			hda_command(devinfo->dev,
5236			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid,
5237			    val));
5238		}
5239	}
5240
5241	hdaa_gpio_commit(devinfo);
5242	hdaa_gpo_commit(devinfo);
5243}
5244
5245static void
5246hdaa_powerup(struct hdaa_devinfo *devinfo)
5247{
5248	int i;
5249
5250	hda_command(devinfo->dev,
5251	    HDA_CMD_SET_POWER_STATE(0,
5252	    devinfo->nid, HDA_CMD_POWER_STATE_D0));
5253	DELAY(100);
5254
5255	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5256		hda_command(devinfo->dev,
5257		    HDA_CMD_SET_POWER_STATE(0,
5258		    i, HDA_CMD_POWER_STATE_D0));
5259	}
5260	DELAY(1000);
5261}
5262
5263static int
5264hdaa_pcmchannel_setup(struct hdaa_chan *ch)
5265{
5266	struct hdaa_devinfo *devinfo = ch->devinfo;
5267	struct hdaa_audio_as *as = devinfo->as;
5268	struct hdaa_widget *w;
5269	uint32_t cap, fmtcap, pcmcap;
5270	int i, j, ret, channels, onlystereo;
5271	uint16_t pinset;
5272
5273	ch->caps = hdaa_caps;
5274	ch->caps.fmtlist = ch->fmtlist;
5275	ch->bit16 = 1;
5276	ch->bit32 = 0;
5277	ch->pcmrates[0] = 48000;
5278	ch->pcmrates[1] = 0;
5279	ch->stripecap = 0xff;
5280
5281	ret = 0;
5282	channels = 0;
5283	onlystereo = 1;
5284	pinset = 0;
5285	fmtcap = devinfo->supp_stream_formats;
5286	pcmcap = devinfo->supp_pcm_size_rate;
5287
5288	for (i = 0; i < 16; i++) {
5289		/* Check as is correct */
5290		if (ch->as < 0)
5291			break;
5292		/* Cound only present DACs */
5293		if (as[ch->as].dacs[ch->asindex][i] <= 0)
5294			continue;
5295		/* Ignore duplicates */
5296		for (j = 0; j < ret; j++) {
5297			if (ch->io[j] == as[ch->as].dacs[ch->asindex][i])
5298				break;
5299		}
5300		if (j < ret)
5301			continue;
5302
5303		w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]);
5304		if (w == NULL || w->enable == 0)
5305			continue;
5306		cap = w->param.supp_stream_formats;
5307		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
5308		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5309			continue;
5310		/* Many CODECs does not declare AC3 support on SPDIF.
5311		   I don't beleave that they doesn't support it! */
5312		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5313			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
5314		if (ret == 0) {
5315			fmtcap = cap;
5316			pcmcap = w->param.supp_pcm_size_rate;
5317		} else {
5318			fmtcap &= cap;
5319			pcmcap &= w->param.supp_pcm_size_rate;
5320		}
5321		ch->io[ret++] = as[ch->as].dacs[ch->asindex][i];
5322		ch->stripecap &= w->wclass.conv.stripecap;
5323		/* Do not count redirection pin/dac channels. */
5324		if (i == 15 && as[ch->as].hpredir >= 0)
5325			continue;
5326		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
5327		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
5328			onlystereo = 0;
5329		pinset |= (1 << i);
5330	}
5331	ch->io[ret] = -1;
5332	ch->channels = channels;
5333
5334	if (as[ch->as].fakeredir)
5335		ret--;
5336	/* Standard speaks only about stereo pins and playback, ... */
5337	if ((!onlystereo) || as[ch->as].mixed)
5338		pinset = 0;
5339	/* ..., but there it gives us info about speakers layout. */
5340	as[ch->as].pinset = pinset;
5341
5342	ch->supp_stream_formats = fmtcap;
5343	ch->supp_pcm_size_rate = pcmcap;
5344
5345	/*
5346	 *  8bit = 0
5347	 * 16bit = 1
5348	 * 20bit = 2
5349	 * 24bit = 3
5350	 * 32bit = 4
5351	 */
5352	if (ret > 0) {
5353		i = 0;
5354		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
5355			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
5356				ch->bit16 = 1;
5357			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
5358				ch->bit16 = 0;
5359			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
5360				ch->bit32 = 3;
5361			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
5362				ch->bit32 = 2;
5363			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
5364				ch->bit32 = 4;
5365			if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) {
5366				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
5367				if (ch->bit32)
5368					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
5369			}
5370			if (channels >= 2) {
5371				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
5372				if (ch->bit32)
5373					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
5374			}
5375			if (channels >= 3 && !onlystereo) {
5376				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0);
5377				if (ch->bit32)
5378					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0);
5379				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1);
5380				if (ch->bit32)
5381					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1);
5382			}
5383			if (channels >= 4) {
5384				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
5385				if (ch->bit32)
5386					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
5387				if (!onlystereo) {
5388					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1);
5389					if (ch->bit32)
5390						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1);
5391				}
5392			}
5393			if (channels >= 5 && !onlystereo) {
5394				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0);
5395				if (ch->bit32)
5396					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0);
5397				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1);
5398				if (ch->bit32)
5399					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1);
5400			}
5401			if (channels >= 6) {
5402				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
5403				if (ch->bit32)
5404					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
5405				if (!onlystereo) {
5406					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0);
5407					if (ch->bit32)
5408						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0);
5409				}
5410			}
5411			if (channels >= 7 && !onlystereo) {
5412				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0);
5413				if (ch->bit32)
5414					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0);
5415				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1);
5416				if (ch->bit32)
5417					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1);
5418			}
5419			if (channels >= 8) {
5420				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
5421				if (ch->bit32)
5422					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
5423			}
5424		}
5425		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
5426			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
5427			if (channels >= 8) {
5428				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 0);
5429				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 1);
5430			}
5431		}
5432		ch->fmtlist[i] = 0;
5433		i = 0;
5434		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
5435			ch->pcmrates[i++] = 8000;
5436		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
5437			ch->pcmrates[i++] = 11025;
5438		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
5439			ch->pcmrates[i++] = 16000;
5440		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
5441			ch->pcmrates[i++] = 22050;
5442		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
5443			ch->pcmrates[i++] = 32000;
5444		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
5445			ch->pcmrates[i++] = 44100;
5446		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
5447		ch->pcmrates[i++] = 48000;
5448		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
5449			ch->pcmrates[i++] = 88200;
5450		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
5451			ch->pcmrates[i++] = 96000;
5452		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
5453			ch->pcmrates[i++] = 176400;
5454		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
5455			ch->pcmrates[i++] = 192000;
5456		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
5457		ch->pcmrates[i] = 0;
5458		if (i > 0) {
5459			ch->caps.minspeed = ch->pcmrates[0];
5460			ch->caps.maxspeed = ch->pcmrates[i - 1];
5461		}
5462	}
5463
5464	return (ret);
5465}
5466
5467static void
5468hdaa_prepare_pcms(struct hdaa_devinfo *devinfo)
5469{
5470	struct hdaa_audio_as *as = devinfo->as;
5471	int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
5472
5473	for (i = 0; i < devinfo->ascnt; i++) {
5474		if (as[i].enable == 0)
5475			continue;
5476		if (as[i].dir == HDAA_CTL_IN) {
5477			if (as[i].digital)
5478				drdev++;
5479			else
5480				ardev++;
5481		} else {
5482			if (as[i].digital)
5483				dpdev++;
5484			else
5485				apdev++;
5486		}
5487	}
5488	devinfo->num_devs =
5489	    max(ardev, apdev) + max(drdev, dpdev);
5490	devinfo->devs =
5491	    (struct hdaa_pcm_devinfo *)malloc(
5492	    devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo),
5493	    M_HDAA, M_ZERO | M_NOWAIT);
5494	if (devinfo->devs == NULL) {
5495		device_printf(devinfo->dev,
5496		    "Unable to allocate memory for devices\n");
5497		return;
5498	}
5499	for (i = 0; i < devinfo->num_devs; i++) {
5500		devinfo->devs[i].index = i;
5501		devinfo->devs[i].devinfo = devinfo;
5502		devinfo->devs[i].playas = -1;
5503		devinfo->devs[i].recas = -1;
5504		devinfo->devs[i].digital = 255;
5505	}
5506	for (i = 0; i < devinfo->ascnt; i++) {
5507		if (as[i].enable == 0)
5508			continue;
5509		for (j = 0; j < devinfo->num_devs; j++) {
5510			if (devinfo->devs[j].digital != 255 &&
5511			    (!devinfo->devs[j].digital) !=
5512			    (!as[i].digital))
5513				continue;
5514			if (as[i].dir == HDAA_CTL_IN) {
5515				if (devinfo->devs[j].recas >= 0)
5516					continue;
5517				devinfo->devs[j].recas = i;
5518			} else {
5519				if (devinfo->devs[j].playas >= 0)
5520					continue;
5521				devinfo->devs[j].playas = i;
5522			}
5523			as[i].pdevinfo = &devinfo->devs[j];
5524			for (k = 0; k < as[i].num_chans; k++) {
5525				devinfo->chans[as[i].chans[k]].pdevinfo =
5526				    &devinfo->devs[j];
5527			}
5528			devinfo->devs[j].digital = as[i].digital;
5529			break;
5530		}
5531	}
5532}
5533
5534static void
5535hdaa_create_pcms(struct hdaa_devinfo *devinfo)
5536{
5537	int i;
5538
5539	for (i = 0; i < devinfo->num_devs; i++) {
5540		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
5541
5542		pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1);
5543		device_set_ivars(pdevinfo->dev, (void *)pdevinfo);
5544	}
5545}
5546
5547static void
5548hdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
5549{
5550	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5551	struct hdaa_audio_ctl *ctl;
5552	char buf[64];
5553	int i, j, printed = 0;
5554
5555	if (flag == 0) {
5556		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5557		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5558		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
5559		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
5560	}
5561
5562	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
5563		if ((flag & (1 << j)) == 0)
5564			continue;
5565		i = 0;
5566		printed = 0;
5567		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5568			if (ctl->enable == 0 ||
5569			    ctl->widget->enable == 0)
5570				continue;
5571			if (!((pdevinfo->playas >= 0 &&
5572			    ctl->widget->bindas == pdevinfo->playas) ||
5573			    (pdevinfo->recas >= 0 &&
5574			    ctl->widget->bindas == pdevinfo->recas) ||
5575			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
5576				continue;
5577			if ((ctl->ossmask & (1 << j)) == 0)
5578				continue;
5579
5580			if (printed == 0) {
5581				if (banner != NULL) {
5582					device_printf(pdevinfo->dev, "%s", banner);
5583				} else {
5584					device_printf(pdevinfo->dev, "Unknown Ctl");
5585				}
5586				printf(" (OSS: %s)",
5587				    hdaa_audio_ctl_ossmixer_mask2allname(1 << j,
5588				    buf, sizeof(buf)));
5589				if (pdevinfo->ossmask & (1 << j)) {
5590					printf(": %+d/%+ddB\n",
5591					    pdevinfo->minamp[j] / 4,
5592					    pdevinfo->maxamp[j] / 4);
5593				} else
5594					printf("\n");
5595				printed = 1;
5596			}
5597			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
5598				ctl->widget->nid,
5599				(ctl->ndir == HDAA_CTL_IN)?"in ":"out");
5600			if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir)
5601				printf(" %2d): ", ctl->index);
5602			else
5603				printf("):    ");
5604			if (ctl->step > 0) {
5605				printf("%+d/%+ddB (%d steps)%s\n",
5606				    MINQDB(ctl) / 4,
5607				    MAXQDB(ctl) / 4,
5608				    ctl->step + 1,
5609				    ctl->mute?" + mute":"");
5610			} else
5611				printf("%s\n", ctl->mute?"mute":"");
5612		}
5613	}
5614	if (printed)
5615		device_printf(pdevinfo->dev, "\n");
5616}
5617
5618static void
5619hdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
5620{
5621	uint32_t cap;
5622
5623	cap = fcap;
5624	if (cap != 0) {
5625		device_printf(dev, "     Stream cap: 0x%08x", cap);
5626		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5627			printf(" AC3");
5628		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5629			printf(" FLOAT32");
5630		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5631			printf(" PCM");
5632		printf("\n");
5633	}
5634	cap = pcmcap;
5635	if (cap != 0) {
5636		device_printf(dev, "        PCM cap: 0x%08x", cap);
5637		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5638			printf(" 8");
5639		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5640			printf(" 16");
5641		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5642			printf(" 20");
5643		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5644			printf(" 24");
5645		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5646			printf(" 32");
5647		printf(" bits,");
5648		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5649			printf(" 8");
5650		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5651			printf(" 11");
5652		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5653			printf(" 16");
5654		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5655			printf(" 22");
5656		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5657			printf(" 32");
5658		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5659			printf(" 44");
5660		printf(" 48");
5661		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5662			printf(" 88");
5663		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5664			printf(" 96");
5665		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5666			printf(" 176");
5667		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5668			printf(" 192");
5669		printf(" KHz\n");
5670	}
5671}
5672
5673static void
5674hdaa_dump_pin(struct hdaa_widget *w)
5675{
5676	uint32_t pincap;
5677
5678	pincap = w->wclass.pin.cap;
5679
5680	device_printf(w->devinfo->dev, "        Pin cap: 0x%08x", pincap);
5681	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5682		printf(" ISC");
5683	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5684		printf(" TRQD");
5685	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5686		printf(" PDC");
5687	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5688		printf(" HP");
5689	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5690		printf(" OUT");
5691	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5692		printf(" IN");
5693	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5694		printf(" BAL");
5695	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
5696		printf(" HDMI");
5697	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5698		printf(" VREF[");
5699		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5700			printf(" 50");
5701		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5702			printf(" 80");
5703		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5704			printf(" 100");
5705		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5706			printf(" GROUND");
5707		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5708			printf(" HIZ");
5709		printf(" ]");
5710	}
5711	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5712		printf(" EAPD");
5713	if (HDA_PARAM_PIN_CAP_DP(pincap))
5714		printf(" DP");
5715	if (HDA_PARAM_PIN_CAP_HBR(pincap))
5716		printf(" HBR");
5717	printf("\n");
5718	device_printf(w->devinfo->dev, "     Pin config: 0x%08x\n",
5719	    w->wclass.pin.config);
5720	device_printf(w->devinfo->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
5721	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5722		printf(" HP");
5723	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5724		printf(" IN");
5725	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5726		printf(" OUT");
5727	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
5728		if ((w->wclass.pin.ctrl &
5729		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
5730			printf(" HBR");
5731		else if ((w->wclass.pin.ctrl &
5732		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5733			printf(" EPTs");
5734	} else {
5735		if ((w->wclass.pin.ctrl &
5736		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5737			printf(" VREFs");
5738	}
5739	printf("\n");
5740}
5741
5742static void
5743hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf)
5744{
5745
5746	device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d "
5747	    "%-13s %-5s %-7s %-10s %-7s %d%s\n",
5748	    w->nid, conf,
5749	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
5750	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
5751	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
5752	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
5753	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
5754	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
5755	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
5756	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
5757	    (w->enable == 0)?" DISA":"");
5758}
5759
5760static void
5761hdaa_dump_pin_configs(struct hdaa_devinfo *devinfo)
5762{
5763	struct hdaa_widget *w;
5764	int i;
5765
5766	device_printf(devinfo->dev, "nid   0x    as seq "
5767	    "device       conn  jack    loc        color   misc\n");
5768	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5769		w = hdaa_widget_get(devinfo, i);
5770		if (w == NULL)
5771			continue;
5772		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5773			continue;
5774		hdaa_dump_pin_config(w, w->wclass.pin.config);
5775	}
5776}
5777
5778static void
5779hdaa_dump_amp(device_t dev, uint32_t cap, const char *banner)
5780{
5781	int offset, size, step;
5782
5783	offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap);
5784	size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap);
5785	step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap);
5786	device_printf(dev, "     %s amp: 0x%08x "
5787	    "mute=%d step=%d size=%d offset=%d (%+d/%+ddB)\n",
5788	    banner, cap,
5789	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5790	    step, size, offset,
5791	    ((0 - offset) * (size + 1)) / 4,
5792	    ((step - offset) * (size + 1)) / 4);
5793}
5794
5795static void
5796hdaa_dump_nodes(struct hdaa_devinfo *devinfo)
5797{
5798	struct hdaa_widget *w, *cw;
5799	char buf[64];
5800	int i, j;
5801
5802	device_printf(devinfo->dev, "\n");
5803	device_printf(devinfo->dev, "Default parameters:\n");
5804	hdaa_dump_audio_formats(devinfo->dev,
5805	    devinfo->supp_stream_formats,
5806	    devinfo->supp_pcm_size_rate);
5807	hdaa_dump_amp(devinfo->dev, devinfo->inamp_cap, " Input");
5808	hdaa_dump_amp(devinfo->dev, devinfo->outamp_cap, "Output");
5809	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5810		w = hdaa_widget_get(devinfo, i);
5811		if (w == NULL) {
5812			device_printf(devinfo->dev, "Ghost widget nid=%d\n", i);
5813			continue;
5814		}
5815		device_printf(devinfo->dev, "\n");
5816		device_printf(devinfo->dev, "            nid: %d%s\n", w->nid,
5817		    (w->enable == 0) ? " [DISABLED]" : "");
5818		device_printf(devinfo->dev, "           Name: %s\n", w->name);
5819		device_printf(devinfo->dev, "     Widget cap: 0x%08x",
5820		    w->param.widget_cap);
5821		if (w->param.widget_cap & 0x0ee1) {
5822			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
5823			    printf(" LRSWAP");
5824			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
5825			    printf(" PWR");
5826			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5827			    printf(" DIGITAL");
5828			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
5829			    printf(" UNSOL");
5830			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
5831			    printf(" PROC");
5832			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
5833			    printf(" STRIPE(x%d)",
5834				1 << (fls(w->wclass.conv.stripecap) - 1));
5835			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
5836			if (j == 1)
5837			    printf(" STEREO");
5838			else if (j > 1)
5839			    printf(" %dCH", j + 1);
5840		}
5841		printf("\n");
5842		if (w->bindas != -1) {
5843			device_printf(devinfo->dev, "    Association: %d (0x%04x)\n",
5844			    w->bindas, w->bindseqmask);
5845		}
5846		if (w->ossmask != 0 || w->ossdev >= 0) {
5847			device_printf(devinfo->dev, "            OSS: %s",
5848			    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
5849			if (w->ossdev >= 0)
5850			    printf(" (%s)", ossnames[w->ossdev]);
5851			printf("\n");
5852		}
5853		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5854		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5855			hdaa_dump_audio_formats(devinfo->dev,
5856			    w->param.supp_stream_formats,
5857			    w->param.supp_pcm_size_rate);
5858		} else if (w->type ==
5859		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
5860			hdaa_dump_pin(w);
5861		if (w->param.eapdbtl != HDA_INVALID)
5862			device_printf(devinfo->dev, "           EAPD: 0x%08x\n",
5863			    w->param.eapdbtl);
5864		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5865		    w->param.outamp_cap != 0)
5866			hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output");
5867		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5868		    w->param.inamp_cap != 0)
5869			hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input");
5870		if (w->nconns > 0)
5871			device_printf(devinfo->dev, "    Connections: %d\n", w->nconns);
5872		for (j = 0; j < w->nconns; j++) {
5873			cw = hdaa_widget_get(devinfo, w->conns[j]);
5874			device_printf(devinfo->dev, "          + %s<- nid=%d [%s]",
5875			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
5876			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5877			if (cw == NULL)
5878				printf(" [UNKNOWN]");
5879			else if (cw->enable == 0)
5880				printf(" [DISABLED]");
5881			if (w->nconns > 1 && w->selconn == j && w->type !=
5882			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5883				printf(" (selected)");
5884			printf("\n");
5885		}
5886	}
5887
5888}
5889
5890static void
5891hdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth)
5892{
5893	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5894	struct hdaa_widget *w, *cw;
5895	char buf[64];
5896	int i;
5897
5898	if (depth > HDA_PARSE_MAXDEPTH)
5899		return;
5900
5901	w = hdaa_widget_get(devinfo, nid);
5902	if (w == NULL || w->enable == 0)
5903		return;
5904
5905	if (depth == 0)
5906		device_printf(pdevinfo->dev, "%*s", 4, "");
5907	else
5908		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
5909	printf("nid=%d [%s]", w->nid, w->name);
5910
5911	if (depth > 0) {
5912		if (w->ossmask == 0) {
5913			printf("\n");
5914			return;
5915		}
5916		printf(" [src: %s]",
5917		    hdaa_audio_ctl_ossmixer_mask2allname(
5918			w->ossmask, buf, sizeof(buf)));
5919		if (w->ossdev >= 0) {
5920			printf("\n");
5921			return;
5922		}
5923	}
5924	printf("\n");
5925
5926	for (i = 0; i < w->nconns; i++) {
5927		if (w->connsenable[i] == 0)
5928			continue;
5929		cw = hdaa_widget_get(devinfo, w->conns[i]);
5930		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
5931			continue;
5932		hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
5933	}
5934
5935}
5936
5937static void
5938hdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo)
5939{
5940	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5941	struct hdaa_audio_as *as;
5942	struct hdaa_widget *w;
5943	nid_t *nids;
5944	int chid, i;
5945
5946	if (pdevinfo->playas < 0)
5947		return;
5948
5949	device_printf(pdevinfo->dev, "Playback:\n");
5950
5951	chid = devinfo->as[pdevinfo->playas].chans[0];
5952	hdaa_dump_audio_formats(pdevinfo->dev,
5953	    devinfo->chans[chid].supp_stream_formats,
5954	    devinfo->chans[chid].supp_pcm_size_rate);
5955	for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) {
5956		chid = devinfo->as[pdevinfo->playas].chans[i];
5957		device_printf(pdevinfo->dev, "            DAC:");
5958		for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5959			printf(" %d", *nids);
5960		printf("\n");
5961	}
5962
5963	as = &devinfo->as[pdevinfo->playas];
5964	for (i = 0; i < 16; i++) {
5965		if (as->pins[i] <= 0)
5966			continue;
5967		w = hdaa_widget_get(devinfo, as->pins[i]);
5968		if (w == NULL || w->enable == 0)
5969			continue;
5970		device_printf(pdevinfo->dev, "\n");
5971		hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0);
5972	}
5973	device_printf(pdevinfo->dev, "\n");
5974}
5975
5976static void
5977hdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo)
5978{
5979	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5980	struct hdaa_widget *w;
5981	nid_t *nids;
5982	int chid, i;
5983
5984	if (pdevinfo->recas < 0)
5985		return;
5986
5987	device_printf(pdevinfo->dev, "Record:\n");
5988
5989	chid = devinfo->as[pdevinfo->recas].chans[0];
5990	hdaa_dump_audio_formats(pdevinfo->dev,
5991	    devinfo->chans[chid].supp_stream_formats,
5992	    devinfo->chans[chid].supp_pcm_size_rate);
5993	for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) {
5994		chid = devinfo->as[pdevinfo->recas].chans[i];
5995		device_printf(pdevinfo->dev, "            ADC:");
5996		for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5997			printf(" %d", *nids);
5998		printf("\n");
5999	}
6000
6001	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6002		w = hdaa_widget_get(devinfo, i);
6003		if (w == NULL || w->enable == 0)
6004			continue;
6005		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
6006			continue;
6007		if (w->bindas != pdevinfo->recas)
6008			continue;
6009		device_printf(pdevinfo->dev, "\n");
6010		hdaa_dump_dst_nid(pdevinfo, i, 0);
6011	}
6012	device_printf(pdevinfo->dev, "\n");
6013}
6014
6015static void
6016hdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo)
6017{
6018	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6019	struct hdaa_widget *w;
6020	int i;
6021	int printed = 0;
6022
6023	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6024		w = hdaa_widget_get(devinfo, i);
6025		if (w == NULL || w->enable == 0)
6026			continue;
6027		if (w->ossdev != SOUND_MIXER_IMIX)
6028			continue;
6029		if (w->bindas != pdevinfo->recas)
6030			continue;
6031		if (printed == 0) {
6032			printed = 1;
6033			device_printf(pdevinfo->dev, "Input Mix:\n");
6034		}
6035		device_printf(pdevinfo->dev, "\n");
6036		hdaa_dump_dst_nid(pdevinfo, i, 0);
6037	}
6038	if (printed)
6039		device_printf(pdevinfo->dev, "\n");
6040}
6041
6042static void
6043hdaa_pindump(device_t dev)
6044{
6045	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6046	struct hdaa_widget *w;
6047	uint32_t res, pincap, delay;
6048	int i;
6049
6050	device_printf(dev, "Dumping AFG pins:\n");
6051	device_printf(dev, "nid   0x    as seq "
6052	    "device       conn  jack    loc        color   misc\n");
6053	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6054		w = hdaa_widget_get(devinfo, i);
6055		if (w == NULL || w->type !=
6056		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6057			continue;
6058		hdaa_dump_pin_config(w, w->wclass.pin.config);
6059		pincap = w->wclass.pin.cap;
6060		device_printf(dev, "    Caps: %2s %3s %2s %4s %4s",
6061		    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
6062		    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
6063		    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
6064		    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
6065		    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
6066		if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
6067		    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
6068			if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
6069				delay = 0;
6070				hda_command(dev,
6071				    HDA_CMD_SET_PIN_SENSE(0, w->nid, 0));
6072				do {
6073					res = hda_command(dev,
6074					    HDA_CMD_GET_PIN_SENSE(0, w->nid));
6075					if (res != 0x7fffffff && res != 0xffffffff)
6076						break;
6077					DELAY(10);
6078				} while (++delay < 10000);
6079			} else {
6080				delay = 0;
6081				res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0,
6082				    w->nid));
6083			}
6084			printf(" Sense: 0x%08x (%sconnected%s)", res,
6085			    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ?
6086			     "" : "dis",
6087			    (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) &&
6088			     (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ?
6089			      ", ELD valid" : "");
6090			if (delay > 0)
6091				printf(" delay %dus", delay * 10);
6092		}
6093		printf("\n");
6094	}
6095	device_printf(dev,
6096	    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
6097	    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
6098	    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
6099	    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
6100	    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
6101	    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
6102	hdaa_dump_gpi(devinfo);
6103	hdaa_dump_gpio(devinfo);
6104	hdaa_dump_gpo(devinfo);
6105}
6106
6107static void
6108hdaa_configure(device_t dev)
6109{
6110	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6111	struct hdaa_audio_ctl *ctl;
6112	int i;
6113
6114	HDA_BOOTHVERBOSE(
6115		device_printf(dev, "Applying built-in patches...\n");
6116	);
6117	hdaa_patch(devinfo);
6118	HDA_BOOTHVERBOSE(
6119		device_printf(dev, "Applying local patches...\n");
6120	);
6121	hdaa_local_patch(devinfo);
6122	hdaa_audio_postprocess(devinfo);
6123	HDA_BOOTHVERBOSE(
6124		device_printf(dev, "Parsing Ctls...\n");
6125	);
6126	hdaa_audio_ctl_parse(devinfo);
6127	HDA_BOOTHVERBOSE(
6128		device_printf(dev, "Disabling nonaudio...\n");
6129	);
6130	hdaa_audio_disable_nonaudio(devinfo);
6131	HDA_BOOTHVERBOSE(
6132		device_printf(dev, "Disabling useless...\n");
6133	);
6134	hdaa_audio_disable_useless(devinfo);
6135	HDA_BOOTVERBOSE(
6136		device_printf(dev, "Patched pins configuration:\n");
6137		hdaa_dump_pin_configs(devinfo);
6138	);
6139	HDA_BOOTHVERBOSE(
6140		device_printf(dev, "Parsing pin associations...\n");
6141	);
6142	hdaa_audio_as_parse(devinfo);
6143	HDA_BOOTHVERBOSE(
6144		device_printf(dev, "Building AFG tree...\n");
6145	);
6146	hdaa_audio_build_tree(devinfo);
6147	HDA_BOOTHVERBOSE(
6148		device_printf(dev, "Disabling unassociated "
6149		    "widgets...\n");
6150	);
6151	hdaa_audio_disable_unas(devinfo);
6152	HDA_BOOTHVERBOSE(
6153		device_printf(dev, "Disabling nonselected "
6154		    "inputs...\n");
6155	);
6156	hdaa_audio_disable_notselected(devinfo);
6157	HDA_BOOTHVERBOSE(
6158		device_printf(dev, "Disabling useless...\n");
6159	);
6160	hdaa_audio_disable_useless(devinfo);
6161	HDA_BOOTHVERBOSE(
6162		device_printf(dev, "Disabling "
6163		    "crossassociatement connections...\n");
6164	);
6165	hdaa_audio_disable_crossas(devinfo);
6166	HDA_BOOTHVERBOSE(
6167		device_printf(dev, "Disabling useless...\n");
6168	);
6169	hdaa_audio_disable_useless(devinfo);
6170	HDA_BOOTHVERBOSE(
6171		device_printf(dev, "Binding associations to channels...\n");
6172	);
6173	hdaa_audio_bind_as(devinfo);
6174	HDA_BOOTHVERBOSE(
6175		device_printf(dev, "Assigning names to signal sources...\n");
6176	);
6177	hdaa_audio_assign_names(devinfo);
6178	HDA_BOOTHVERBOSE(
6179		device_printf(dev, "Preparing PCM devices...\n");
6180	);
6181	hdaa_prepare_pcms(devinfo);
6182	HDA_BOOTHVERBOSE(
6183		device_printf(dev, "Assigning mixers to the tree...\n");
6184	);
6185	hdaa_audio_assign_mixers(devinfo);
6186	HDA_BOOTHVERBOSE(
6187		device_printf(dev, "Preparing pin controls...\n");
6188	);
6189	hdaa_audio_prepare_pin_ctrl(devinfo);
6190	HDA_BOOTHVERBOSE(
6191		device_printf(dev, "AFG commit...\n");
6192	);
6193	hdaa_audio_commit(devinfo);
6194	HDA_BOOTHVERBOSE(
6195		device_printf(dev, "Applying direct built-in patches...\n");
6196	);
6197	hdaa_patch_direct(devinfo);
6198	HDA_BOOTHVERBOSE(
6199		device_printf(dev, "Pin sense init...\n");
6200	);
6201	hdaa_sense_init(devinfo);
6202	HDA_BOOTHVERBOSE(
6203		device_printf(dev, "Creating PCM devices...\n");
6204	);
6205	hdaa_create_pcms(devinfo);
6206
6207	HDA_BOOTVERBOSE(
6208		if (devinfo->quirks != 0) {
6209			device_printf(dev, "FG config/quirks:");
6210			for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
6211				if ((devinfo->quirks &
6212				    hdaa_quirks_tab[i].value) ==
6213				    hdaa_quirks_tab[i].value)
6214					printf(" %s", hdaa_quirks_tab[i].key);
6215			}
6216			printf("\n");
6217		}
6218	);
6219
6220	HDA_BOOTHVERBOSE(
6221		device_printf(dev, "\n");
6222		device_printf(dev, "+-----------+\n");
6223		device_printf(dev, "| HDA NODES |\n");
6224		device_printf(dev, "+-----------+\n");
6225		hdaa_dump_nodes(devinfo);
6226
6227		device_printf(dev, "\n");
6228		device_printf(dev, "+----------------+\n");
6229		device_printf(dev, "| HDA AMPLIFIERS |\n");
6230		device_printf(dev, "+----------------+\n");
6231		device_printf(dev, "\n");
6232		i = 0;
6233		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
6234			device_printf(dev, "%3d: nid %3d %s (%s) index %d", i,
6235			    (ctl->widget != NULL) ? ctl->widget->nid : -1,
6236			    (ctl->ndir == HDAA_CTL_IN)?"in ":"out",
6237			    (ctl->dir == HDAA_CTL_IN)?"in ":"out",
6238			    ctl->index);
6239			if (ctl->childwidget != NULL)
6240				printf(" cnid %3d", ctl->childwidget->nid);
6241			else
6242				printf("         ");
6243			printf(" ossmask=0x%08x\n",
6244			    ctl->ossmask);
6245			device_printf(dev,
6246			    "       mute: %d step: %3d size: %3d off: %3d%s\n",
6247			    ctl->mute, ctl->step, ctl->size, ctl->offset,
6248			    (ctl->enable == 0) ? " [DISABLED]" :
6249			    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
6250		}
6251		device_printf(dev, "\n");
6252	);
6253}
6254
6255static void
6256hdaa_unconfigure(device_t dev)
6257{
6258	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6259	struct hdaa_widget *w;
6260	int i, j;
6261
6262	HDA_BOOTHVERBOSE(
6263		device_printf(dev, "Pin sense deinit...\n");
6264	);
6265	hdaa_sense_deinit(devinfo);
6266	free(devinfo->ctl, M_HDAA);
6267	devinfo->ctl = NULL;
6268	devinfo->ctlcnt = 0;
6269	free(devinfo->as, M_HDAA);
6270	devinfo->as = NULL;
6271	devinfo->ascnt = 0;
6272	free(devinfo->devs, M_HDAA);
6273	devinfo->devs = NULL;
6274	devinfo->num_devs = 0;
6275	free(devinfo->chans, M_HDAA);
6276	devinfo->chans = NULL;
6277	devinfo->num_chans = 0;
6278	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6279		w = hdaa_widget_get(devinfo, i);
6280		if (w == NULL)
6281			continue;
6282		w->enable = 1;
6283		w->selconn = -1;
6284		w->pflags = 0;
6285		w->bindas = -1;
6286		w->bindseqmask = 0;
6287		w->ossdev = -1;
6288		w->ossmask = 0;
6289		for (j = 0; j < w->nconns; j++)
6290			w->connsenable[j] = 1;
6291		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6292			w->wclass.pin.config = w->wclass.pin.newconf;
6293		if (w->eld != NULL) {
6294			w->eld_len = 0;
6295			free(w->eld, M_HDAA);
6296			w->eld = NULL;
6297		}
6298	}
6299}
6300
6301static int
6302hdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS)
6303{
6304	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6305	device_t dev = devinfo->dev;
6306	char buf[256];
6307	int n = 0, i, numgpi;
6308	uint32_t data = 0;
6309
6310	buf[0] = 0;
6311	hdaa_lock(devinfo);
6312	numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap);
6313	if (numgpi > 0) {
6314		data = hda_command(dev,
6315		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
6316	}
6317	hdaa_unlock(devinfo);
6318	for (i = 0; i < numgpi; i++) {
6319		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6320		    n != 0 ? " " : "", i, ((data >> i) & 1));
6321	}
6322	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6323}
6324
6325static int
6326hdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS)
6327{
6328	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6329	device_t dev = devinfo->dev;
6330	char buf[256];
6331	int n = 0, i, numgpio;
6332	uint32_t data = 0, enable = 0, dir = 0;
6333
6334	buf[0] = 0;
6335	hdaa_lock(devinfo);
6336	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6337	if (numgpio > 0) {
6338		data = hda_command(dev,
6339		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
6340		enable = hda_command(dev,
6341		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
6342		dir = hda_command(dev,
6343		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
6344	}
6345	hdaa_unlock(devinfo);
6346	for (i = 0; i < numgpio; i++) {
6347		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=",
6348		    n != 0 ? " " : "", i);
6349		if ((enable & (1 << i)) == 0) {
6350			n += snprintf(buf + n, sizeof(buf) - n, "disabled");
6351			continue;
6352		}
6353		n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)",
6354		    ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1));
6355	}
6356	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6357}
6358
6359static int
6360hdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS)
6361{
6362	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6363	char buf[256];
6364	int error, n = 0, i, numgpio;
6365	uint32_t gpio, x;
6366
6367	gpio = devinfo->newgpio;
6368	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6369	buf[0] = 0;
6370	for (i = 0; i < numgpio; i++) {
6371		x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6372		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6373		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6374	}
6375	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6376	if (error != 0 || req->newptr == NULL)
6377		return (error);
6378	if (strncmp(buf, "0x", 2) == 0)
6379		gpio = strtol(buf + 2, NULL, 16);
6380	else
6381		gpio = hdaa_gpio_patch(gpio, buf);
6382	hdaa_lock(devinfo);
6383	devinfo->newgpio = devinfo->gpio = gpio;
6384	hdaa_gpio_commit(devinfo);
6385	hdaa_unlock(devinfo);
6386	return (0);
6387}
6388
6389static int
6390hdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS)
6391{
6392	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6393	device_t dev = devinfo->dev;
6394	char buf[256];
6395	int n = 0, i, numgpo;
6396	uint32_t data = 0;
6397
6398	buf[0] = 0;
6399	hdaa_lock(devinfo);
6400	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6401	if (numgpo > 0) {
6402		data = hda_command(dev,
6403		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
6404	}
6405	hdaa_unlock(devinfo);
6406	for (i = 0; i < numgpo; i++) {
6407		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6408		    n != 0 ? " " : "", i, ((data >> i) & 1));
6409	}
6410	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6411}
6412
6413static int
6414hdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS)
6415{
6416	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6417	char buf[256];
6418	int error, n = 0, i, numgpo;
6419	uint32_t gpo, x;
6420
6421	gpo = devinfo->newgpo;
6422	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6423	buf[0] = 0;
6424	for (i = 0; i < numgpo; i++) {
6425		x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6426		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6427		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6428	}
6429	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6430	if (error != 0 || req->newptr == NULL)
6431		return (error);
6432	if (strncmp(buf, "0x", 2) == 0)
6433		gpo = strtol(buf + 2, NULL, 16);
6434	else
6435		gpo = hdaa_gpio_patch(gpo, buf);
6436	hdaa_lock(devinfo);
6437	devinfo->newgpo = devinfo->gpo = gpo;
6438	hdaa_gpo_commit(devinfo);
6439	hdaa_unlock(devinfo);
6440	return (0);
6441}
6442
6443static int
6444hdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS)
6445{
6446	device_t dev;
6447	struct hdaa_devinfo *devinfo;
6448	int error, val;
6449
6450	dev = oidp->oid_arg1;
6451	devinfo = device_get_softc(dev);
6452	if (devinfo == NULL)
6453		return (EINVAL);
6454	val = 0;
6455	error = sysctl_handle_int(oidp, &val, 0, req);
6456	if (error != 0 || req->newptr == NULL || val == 0)
6457		return (error);
6458
6459	HDA_BOOTHVERBOSE(
6460		device_printf(dev, "Reconfiguration...\n");
6461	);
6462	if ((error = device_delete_children(dev)) != 0)
6463		return (error);
6464	hdaa_lock(devinfo);
6465	hdaa_unconfigure(dev);
6466	hdaa_configure(dev);
6467	hdaa_unlock(devinfo);
6468	bus_generic_attach(dev);
6469	HDA_BOOTHVERBOSE(
6470		device_printf(dev, "Reconfiguration done\n");
6471	);
6472	return (0);
6473}
6474
6475static int
6476hdaa_suspend(device_t dev)
6477{
6478	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6479	int i;
6480
6481	HDA_BOOTHVERBOSE(
6482		device_printf(dev, "Suspend...\n");
6483	);
6484	hdaa_lock(devinfo);
6485	HDA_BOOTHVERBOSE(
6486		device_printf(dev, "Stop streams...\n");
6487	);
6488	for (i = 0; i < devinfo->num_chans; i++) {
6489		if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) {
6490			devinfo->chans[i].flags |= HDAA_CHN_SUSPEND;
6491			hdaa_channel_stop(&devinfo->chans[i]);
6492		}
6493	}
6494	HDA_BOOTHVERBOSE(
6495		device_printf(dev, "Power down FG"
6496		    " nid=%d to the D3 state...\n",
6497		    devinfo->nid);
6498	);
6499	hda_command(devinfo->dev,
6500	    HDA_CMD_SET_POWER_STATE(0,
6501	    devinfo->nid, HDA_CMD_POWER_STATE_D3));
6502	callout_stop(&devinfo->poll_jack);
6503	hdaa_unlock(devinfo);
6504	callout_drain(&devinfo->poll_jack);
6505	HDA_BOOTHVERBOSE(
6506		device_printf(dev, "Suspend done\n");
6507	);
6508	return (0);
6509}
6510
6511static int
6512hdaa_resume(device_t dev)
6513{
6514	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6515	int i;
6516
6517	HDA_BOOTHVERBOSE(
6518		device_printf(dev, "Resume...\n");
6519	);
6520	hdaa_lock(devinfo);
6521	HDA_BOOTHVERBOSE(
6522		device_printf(dev, "Power up audio FG nid=%d...\n",
6523		    devinfo->nid);
6524	);
6525	hdaa_powerup(devinfo);
6526	HDA_BOOTHVERBOSE(
6527		device_printf(dev, "AFG commit...\n");
6528	);
6529	hdaa_audio_commit(devinfo);
6530	HDA_BOOTHVERBOSE(
6531		device_printf(dev, "Applying direct built-in patches...\n");
6532	);
6533	hdaa_patch_direct(devinfo);
6534	HDA_BOOTHVERBOSE(
6535		device_printf(dev, "Pin sense init...\n");
6536	);
6537	hdaa_sense_init(devinfo);
6538
6539	hdaa_unlock(devinfo);
6540	for (i = 0; i < devinfo->num_devs; i++) {
6541		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
6542		HDA_BOOTHVERBOSE(
6543			device_printf(pdevinfo->dev,
6544			    "OSS mixer reinitialization...\n");
6545		);
6546		if (mixer_reinit(pdevinfo->dev) == -1)
6547			device_printf(pdevinfo->dev,
6548			    "unable to reinitialize the mixer\n");
6549	}
6550	hdaa_lock(devinfo);
6551	HDA_BOOTHVERBOSE(
6552		device_printf(dev, "Start streams...\n");
6553	);
6554	for (i = 0; i < devinfo->num_chans; i++) {
6555		if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) {
6556			devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND;
6557			hdaa_channel_start(&devinfo->chans[i]);
6558		}
6559	}
6560	hdaa_unlock(devinfo);
6561	HDA_BOOTHVERBOSE(
6562		device_printf(dev, "Resume done\n");
6563	);
6564	return (0);
6565}
6566
6567static int
6568hdaa_probe(device_t dev)
6569{
6570	const char *pdesc;
6571	char buf[128];
6572
6573	if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
6574		return (ENXIO);
6575	pdesc = device_get_desc(device_get_parent(dev));
6576	snprintf(buf, sizeof(buf), "%.*s Audio Function Group",
6577	    (int)(strlen(pdesc) - 10), pdesc);
6578	device_set_desc_copy(dev, buf);
6579	return (BUS_PROBE_DEFAULT);
6580}
6581
6582static int
6583hdaa_attach(device_t dev)
6584{
6585	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6586	uint32_t res;
6587	nid_t nid = hda_get_node_id(dev);
6588
6589	devinfo->dev = dev;
6590	devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
6591	devinfo->nid = nid;
6592	devinfo->newquirks = -1;
6593	devinfo->newgpio = -1;
6594	devinfo->newgpo = -1;
6595	callout_init(&devinfo->poll_jack, CALLOUT_MPSAFE);
6596	devinfo->poll_ival = hz;
6597
6598	hdaa_lock(devinfo);
6599	res = hda_command(dev,
6600	    HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT));
6601	hdaa_unlock(devinfo);
6602
6603	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
6604	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
6605	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
6606
6607	HDA_BOOTVERBOSE(
6608		device_printf(dev, "Subsystem ID: 0x%08x\n",
6609		    hda_get_subsystem_id(dev));
6610	);
6611	HDA_BOOTHVERBOSE(
6612		device_printf(dev,
6613		    "Audio Function Group at nid=%d: %d subnodes %d-%d\n",
6614		    nid, devinfo->nodecnt,
6615		    devinfo->startnode, devinfo->endnode - 1);
6616	);
6617
6618	if (devinfo->nodecnt > 0)
6619		devinfo->widget = (struct hdaa_widget *)malloc(
6620		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA,
6621		    M_WAITOK | M_ZERO);
6622	else
6623		devinfo->widget = NULL;
6624
6625	hdaa_lock(devinfo);
6626	HDA_BOOTHVERBOSE(
6627		device_printf(dev, "Powering up...\n");
6628	);
6629	hdaa_powerup(devinfo);
6630	HDA_BOOTHVERBOSE(
6631		device_printf(dev, "Parsing audio FG...\n");
6632	);
6633	hdaa_audio_parse(devinfo);
6634	HDA_BOOTVERBOSE(
6635		device_printf(dev, "Original pins configuration:\n");
6636		hdaa_dump_pin_configs(devinfo);
6637	);
6638	hdaa_configure(dev);
6639	hdaa_unlock(devinfo);
6640
6641	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6642	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6643	    "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6644	    &devinfo->newquirks, sizeof(&devinfo->newquirks),
6645	    hdaa_sysctl_quirks, "A", "Configuration options");
6646	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6647	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6648	    "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6649	    devinfo, sizeof(devinfo),
6650	    hdaa_sysctl_gpi_state, "A", "GPI state");
6651	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6652	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6653	    "gpio_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6654	    devinfo, sizeof(devinfo),
6655	    hdaa_sysctl_gpio_state, "A", "GPIO state");
6656	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6657	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6658	    "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6659	    devinfo, sizeof(devinfo),
6660	    hdaa_sysctl_gpio_config, "A", "GPIO configuration");
6661	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6662	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6663	    "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6664	    devinfo, sizeof(devinfo),
6665	    hdaa_sysctl_gpo_state, "A", "GPO state");
6666	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6667	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6668	    "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6669	    devinfo, sizeof(devinfo),
6670	    hdaa_sysctl_gpo_config, "A", "GPO configuration");
6671	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6672	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6673	    "reconfig", CTLTYPE_INT | CTLFLAG_RW,
6674	    dev, sizeof(dev),
6675	    hdaa_sysctl_reconfig, "I", "Reprocess configuration");
6676	bus_generic_attach(dev);
6677	return (0);
6678}
6679
6680static int
6681hdaa_detach(device_t dev)
6682{
6683	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6684	int error;
6685
6686	if ((error = device_delete_children(dev)) != 0)
6687		return (error);
6688
6689	hdaa_lock(devinfo);
6690	hdaa_unconfigure(dev);
6691	devinfo->poll_ival = 0;
6692	callout_stop(&devinfo->poll_jack);
6693	hdaa_unlock(devinfo);
6694	callout_drain(&devinfo->poll_jack);
6695
6696	free(devinfo->widget, M_HDAA);
6697	return (0);
6698}
6699
6700static int
6701hdaa_print_child(device_t dev, device_t child)
6702{
6703	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6704	struct hdaa_pcm_devinfo *pdevinfo =
6705	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6706	struct hdaa_audio_as *as;
6707	int retval, first = 1, i;
6708
6709	retval = bus_print_child_header(dev, child);
6710	retval += printf(" at nid ");
6711	if (pdevinfo->playas >= 0) {
6712		as = &devinfo->as[pdevinfo->playas];
6713		for (i = 0; i < 16; i++) {
6714			if (as->pins[i] <= 0)
6715				continue;
6716			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6717			first = 0;
6718		}
6719	}
6720	if (pdevinfo->recas >= 0) {
6721		if (pdevinfo->playas >= 0) {
6722			retval += printf(" and ");
6723			first = 1;
6724		}
6725		as = &devinfo->as[pdevinfo->recas];
6726		for (i = 0; i < 16; i++) {
6727			if (as->pins[i] <= 0)
6728				continue;
6729			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6730			first = 0;
6731		}
6732	}
6733	retval += bus_print_child_footer(dev, child);
6734
6735	return (retval);
6736}
6737
6738static int
6739hdaa_child_location_str(device_t dev, device_t child, char *buf,
6740    size_t buflen)
6741{
6742	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6743	struct hdaa_pcm_devinfo *pdevinfo =
6744	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6745	struct hdaa_audio_as *as;
6746	int first = 1, i, len = 0;
6747
6748	len += snprintf(buf + len, buflen - len, "nid=");
6749	if (pdevinfo->playas >= 0) {
6750		as = &devinfo->as[pdevinfo->playas];
6751		for (i = 0; i < 16; i++) {
6752			if (as->pins[i] <= 0)
6753				continue;
6754			len += snprintf(buf + len, buflen - len,
6755			    "%s%d", first ? "" : ",", as->pins[i]);
6756			first = 0;
6757		}
6758	}
6759	if (pdevinfo->recas >= 0) {
6760		as = &devinfo->as[pdevinfo->recas];
6761		for (i = 0; i < 16; i++) {
6762			if (as->pins[i] <= 0)
6763				continue;
6764			len += snprintf(buf + len, buflen - len,
6765			    "%s%d", first ? "" : ",", as->pins[i]);
6766			first = 0;
6767		}
6768	}
6769	return (0);
6770}
6771
6772static void
6773hdaa_stream_intr(device_t dev, int dir, int stream)
6774{
6775	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6776	struct hdaa_chan *ch;
6777	int i;
6778
6779	for (i = 0; i < devinfo->num_chans; i++) {
6780		ch = &devinfo->chans[i];
6781		if (!(ch->flags & HDAA_CHN_RUNNING))
6782			continue;
6783		if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) &&
6784		    ch->sid == stream) {
6785			hdaa_unlock(devinfo);
6786			chn_intr(ch->c);
6787			hdaa_lock(devinfo);
6788		}
6789	}
6790}
6791
6792static void
6793hdaa_unsol_intr(device_t dev, uint32_t resp)
6794{
6795	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6796	struct hdaa_widget *w;
6797	int i, tag, flags;
6798
6799	HDA_BOOTHVERBOSE(
6800		device_printf(dev, "Unsolicited response %08x\n", resp);
6801	);
6802	tag = resp >> 26;
6803	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6804		w = hdaa_widget_get(devinfo, i);
6805		if (w == NULL || w->enable == 0 || w->type !=
6806		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6807			continue;
6808		if (w->unsol != tag)
6809			continue;
6810		if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
6811		    HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
6812			flags = resp & 0x03;
6813		else
6814			flags = 0x01;
6815		if (flags & 0x01)
6816			hdaa_presence_handler(w);
6817		if (flags & 0x02)
6818			hdaa_eld_handler(w);
6819	}
6820}
6821
6822static device_method_t hdaa_methods[] = {
6823	/* device interface */
6824	DEVMETHOD(device_probe,		hdaa_probe),
6825	DEVMETHOD(device_attach,	hdaa_attach),
6826	DEVMETHOD(device_detach,	hdaa_detach),
6827	DEVMETHOD(device_suspend,	hdaa_suspend),
6828	DEVMETHOD(device_resume,	hdaa_resume),
6829	/* Bus interface */
6830	DEVMETHOD(bus_print_child,	hdaa_print_child),
6831	DEVMETHOD(bus_child_location_str, hdaa_child_location_str),
6832	DEVMETHOD(hdac_stream_intr,	hdaa_stream_intr),
6833	DEVMETHOD(hdac_unsol_intr,	hdaa_unsol_intr),
6834	DEVMETHOD(hdac_pindump,		hdaa_pindump),
6835	DEVMETHOD_END
6836};
6837
6838static driver_t hdaa_driver = {
6839	"hdaa",
6840	hdaa_methods,
6841	sizeof(struct hdaa_devinfo),
6842};
6843
6844static devclass_t hdaa_devclass;
6845
6846DRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, NULL, NULL);
6847
6848static void
6849hdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid,
6850    char *buf, int buflen)
6851{
6852	struct hdaa_audio_as *as;
6853	int c;
6854
6855	as = &devinfo->as[asid];
6856	c = devinfo->chans[as->chans[0]].channels;
6857	if (c == 1)
6858		snprintf(buf, buflen, "mono");
6859	else if (c == 2) {
6860		if (as->hpredir < 0)
6861			buf[0] = 0;
6862		else
6863			snprintf(buf, buflen, "2.0");
6864	} else if (as->pinset == 0x0003)
6865		snprintf(buf, buflen, "3.1");
6866	else if (as->pinset == 0x0005 || as->pinset == 0x0011)
6867		snprintf(buf, buflen, "4.0");
6868	else if (as->pinset == 0x0007 || as->pinset == 0x0013)
6869		snprintf(buf, buflen, "5.1");
6870	else if (as->pinset == 0x0017)
6871		snprintf(buf, buflen, "7.1");
6872	else
6873		snprintf(buf, buflen, "%dch", c);
6874	if (as->hpredir >= 0)
6875		strlcat(buf, "+HP", buflen);
6876}
6877
6878static int
6879hdaa_chan_type(struct hdaa_devinfo *devinfo, int asid)
6880{
6881	struct hdaa_audio_as *as;
6882	struct hdaa_widget *w;
6883	int i, t = -1, t1;
6884
6885	as = &devinfo->as[asid];
6886	for (i = 0; i < 16; i++) {
6887		w = hdaa_widget_get(devinfo, as->pins[i]);
6888		if (w == NULL || w->enable == 0 || w->type !=
6889		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6890			continue;
6891		t1 = HDA_CONFIG_DEFAULTCONF_DEVICE(w->wclass.pin.config);
6892		if (t == -1)
6893			t = t1;
6894		else if (t != t1) {
6895			t = -2;
6896			break;
6897		}
6898	}
6899	return (t);
6900}
6901
6902static int
6903hdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS)
6904{
6905	struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1;
6906	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
6907	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6908	struct hdaa_chan *ch;
6909	int error, val, i;
6910	uint32_t pcmcap;
6911
6912	ch = &devinfo->chans[as->chans[0]];
6913	val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 :
6914	    ((ch->bit32 == 2) ? 20 : 0));
6915	error = sysctl_handle_int(oidp, &val, 0, req);
6916	if (error != 0 || req->newptr == NULL)
6917		return (error);
6918	pcmcap = ch->supp_pcm_size_rate;
6919	if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6920		ch->bit32 = 4;
6921	else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6922		ch->bit32 = 3;
6923	else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6924		ch->bit32 = 2;
6925	else
6926		return (EINVAL);
6927	for (i = 1; i < as->num_chans; i++)
6928		devinfo->chans[as->chans[i]].bit32 = ch->bit32;
6929	return (0);
6930}
6931
6932static int
6933hdaa_pcm_probe(device_t dev)
6934{
6935	struct hdaa_pcm_devinfo *pdevinfo =
6936	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6937	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6938	const char *pdesc;
6939	char chans1[8], chans2[8];
6940	char buf[128];
6941	int loc1, loc2, t1, t2;
6942
6943	if (pdevinfo->playas >= 0)
6944		loc1 = devinfo->as[pdevinfo->playas].location;
6945	else
6946		loc1 = devinfo->as[pdevinfo->recas].location;
6947	if (pdevinfo->recas >= 0)
6948		loc2 = devinfo->as[pdevinfo->recas].location;
6949	else
6950		loc2 = loc1;
6951	if (loc1 != loc2)
6952		loc1 = -2;
6953	if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0')
6954		loc1 = -2;
6955	chans1[0] = 0;
6956	chans2[0] = 0;
6957	t1 = t2 = -1;
6958	if (pdevinfo->playas >= 0) {
6959		hdaa_chan_formula(devinfo, pdevinfo->playas,
6960		    chans1, sizeof(chans1));
6961		t1 = hdaa_chan_type(devinfo, pdevinfo->playas);
6962	}
6963	if (pdevinfo->recas >= 0) {
6964		hdaa_chan_formula(devinfo, pdevinfo->recas,
6965		    chans2, sizeof(chans2));
6966		t2 = hdaa_chan_type(devinfo, pdevinfo->recas);
6967	}
6968	if (chans1[0] != 0 || chans2[0] != 0) {
6969		if (chans1[0] == 0 && pdevinfo->playas >= 0)
6970			snprintf(chans1, sizeof(chans1), "2.0");
6971		else if (chans2[0] == 0 && pdevinfo->recas >= 0)
6972			snprintf(chans2, sizeof(chans2), "2.0");
6973		if (strcmp(chans1, chans2) == 0)
6974			chans2[0] = 0;
6975	}
6976	if (t1 == -1)
6977		t1 = t2;
6978	else if (t2 == -1)
6979		t2 = t1;
6980	if (t1 != t2)
6981		t1 = -2;
6982	if (pdevinfo->digital)
6983		t1 = -2;
6984	pdesc = device_get_desc(device_get_parent(dev));
6985	snprintf(buf, sizeof(buf), "%.*s (%s%s%s%s%s%s%s%s%s)",
6986	    (int)(strlen(pdesc) - 21), pdesc,
6987	    loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "",
6988	    (pdevinfo->digital == 0x7)?"HDMI/DP":
6989	    ((pdevinfo->digital == 0x5)?"DisplayPort":
6990	    ((pdevinfo->digital == 0x3)?"HDMI":
6991	    ((pdevinfo->digital)?"Digital":"Analog"))),
6992	    chans1[0] ? " " : "", chans1,
6993	    chans2[0] ? "/" : "", chans2,
6994	    t1 >= 0 ? " " : "", t1 >= 0 ? HDA_DEVS[t1] : "");
6995	device_set_desc_copy(dev, buf);
6996	return (BUS_PROBE_SPECIFIC);
6997}
6998
6999static int
7000hdaa_pcm_attach(device_t dev)
7001{
7002	struct hdaa_pcm_devinfo *pdevinfo =
7003	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
7004	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
7005	struct hdaa_audio_as *as;
7006	struct snddev_info *d;
7007	char status[SND_STATUSLEN];
7008	int i;
7009
7010	pdevinfo->chan_size = pcm_getbuffersize(dev,
7011	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
7012
7013	HDA_BOOTVERBOSE(
7014		hdaa_dump_dac(pdevinfo);
7015		hdaa_dump_adc(pdevinfo);
7016		hdaa_dump_mix(pdevinfo);
7017		hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
7018		hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
7019		hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
7020		hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
7021		hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
7022		hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
7023		hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
7024		hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
7025		hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
7026		hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
7027		hdaa_dump_ctls(pdevinfo, NULL, 0);
7028	);
7029
7030	if (resource_int_value(device_get_name(dev),
7031	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
7032		i &= HDA_BLK_ALIGN;
7033		if (i < HDA_BLK_MIN)
7034			i = HDA_BLK_MIN;
7035		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
7036		i = 0;
7037		while (pdevinfo->chan_blkcnt >> i)
7038			i++;
7039		pdevinfo->chan_blkcnt = 1 << (i - 1);
7040		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
7041			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
7042		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
7043			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
7044	} else
7045		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
7046
7047	/*
7048	 * We don't register interrupt handler with snd_setup_intr
7049	 * in pcm device. Mark pcm device as MPSAFE manually.
7050	 */
7051	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
7052
7053	HDA_BOOTHVERBOSE(
7054		device_printf(dev, "OSS mixer initialization...\n");
7055	);
7056	if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0)
7057		device_printf(dev, "Can't register mixer\n");
7058
7059	HDA_BOOTHVERBOSE(
7060		device_printf(dev, "Registering PCM channels...\n");
7061	);
7062	if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0,
7063	    (pdevinfo->recas >= 0)?1:0) != 0)
7064		device_printf(dev, "Can't register PCM\n");
7065
7066	pdevinfo->registered++;
7067
7068	d = device_get_softc(dev);
7069	if (pdevinfo->playas >= 0) {
7070		as = &devinfo->as[pdevinfo->playas];
7071		for (i = 0; i < as->num_chans; i++)
7072			pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class,
7073			    &devinfo->chans[as->chans[i]]);
7074		SYSCTL_ADD_PROC(&d->play_sysctl_ctx,
7075		    SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO,
7076		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7077		    as, sizeof(as), hdaa_sysctl_32bit, "I",
7078		    "Resolution of 32bit samples (20/24/32bit)");
7079	}
7080	if (pdevinfo->recas >= 0) {
7081		as = &devinfo->as[pdevinfo->recas];
7082		for (i = 0; i < as->num_chans; i++)
7083			pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class,
7084			    &devinfo->chans[as->chans[i]]);
7085		SYSCTL_ADD_PROC(&d->rec_sysctl_ctx,
7086		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7087		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7088		    as, sizeof(as), hdaa_sysctl_32bit, "I",
7089		    "Resolution of 32bit samples (20/24/32bit)");
7090		pdevinfo->autorecsrc = 2;
7091		resource_int_value(device_get_name(dev), device_get_unit(dev),
7092		    "rec.autosrc", &pdevinfo->autorecsrc);
7093		SYSCTL_ADD_INT(&d->rec_sysctl_ctx,
7094		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7095		    "autosrc", CTLFLAG_RW,
7096		    &pdevinfo->autorecsrc, 0,
7097		    "Automatic recording source selection");
7098	}
7099
7100	if (pdevinfo->mixer != NULL) {
7101		hdaa_audio_ctl_set_defaults(pdevinfo);
7102		hdaa_lock(devinfo);
7103		if (pdevinfo->playas >= 0) {
7104			as = &devinfo->as[pdevinfo->playas];
7105			hdaa_channels_handler(as);
7106		}
7107		if (pdevinfo->recas >= 0) {
7108			as = &devinfo->as[pdevinfo->recas];
7109			hdaa_autorecsrc_handler(as, NULL);
7110			hdaa_channels_handler(as);
7111		}
7112		hdaa_unlock(devinfo);
7113	}
7114
7115	snprintf(status, SND_STATUSLEN, "on %s %s",
7116	    device_get_nameunit(device_get_parent(dev)),
7117	    PCM_KLDSTRING(snd_hda));
7118	pcm_setstatus(dev, status);
7119
7120	return (0);
7121}
7122
7123static int
7124hdaa_pcm_detach(device_t dev)
7125{
7126	struct hdaa_pcm_devinfo *pdevinfo =
7127	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
7128	int err;
7129
7130	if (pdevinfo->registered > 0) {
7131		err = pcm_unregister(dev);
7132		if (err != 0)
7133			return (err);
7134	}
7135
7136	return (0);
7137}
7138
7139static device_method_t hdaa_pcm_methods[] = {
7140	/* device interface */
7141	DEVMETHOD(device_probe,		hdaa_pcm_probe),
7142	DEVMETHOD(device_attach,	hdaa_pcm_attach),
7143	DEVMETHOD(device_detach,	hdaa_pcm_detach),
7144	DEVMETHOD_END
7145};
7146
7147static driver_t hdaa_pcm_driver = {
7148	"pcm",
7149	hdaa_pcm_methods,
7150	PCM_SOFTC_SIZE,
7151};
7152
7153DRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, NULL, NULL);
7154MODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
7155MODULE_VERSION(snd_hda, 1);
7156