xref: /freebsd/sys/dev/sound/pci/hda/hdaa.c (revision d6b92ffa)
1 /*-
2  * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3  * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4  * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Intel High Definition Audio (Audio function) driver for FreeBSD.
31  */
32 
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
34 #include "opt_snd.h"
35 #endif
36 
37 #include <dev/sound/pcm/sound.h>
38 
39 #include <sys/ctype.h>
40 #include <sys/taskqueue.h>
41 
42 #include <dev/sound/pci/hda/hdac.h>
43 #include <dev/sound/pci/hda/hdaa.h>
44 #include <dev/sound/pci/hda/hda_reg.h>
45 
46 #include "mixer_if.h"
47 
48 SND_DECLARE_FILE("$FreeBSD$");
49 
50 #define hdaa_lock(devinfo)	snd_mtxlock((devinfo)->lock)
51 #define hdaa_unlock(devinfo)	snd_mtxunlock((devinfo)->lock)
52 #define hdaa_lockassert(devinfo) snd_mtxassert((devinfo)->lock)
53 #define hdaa_lockowned(devinfo)	mtx_owned((devinfo)->lock)
54 
55 static const struct {
56 	const char *key;
57 	uint32_t value;
58 } hdaa_quirks_tab[] = {
59 	{ "softpcmvol", HDAA_QUIRK_SOFTPCMVOL },
60 	{ "fixedrate", HDAA_QUIRK_FIXEDRATE },
61 	{ "forcestereo", HDAA_QUIRK_FORCESTEREO },
62 	{ "eapdinv", HDAA_QUIRK_EAPDINV },
63 	{ "senseinv", HDAA_QUIRK_SENSEINV },
64 	{ "ivref50", HDAA_QUIRK_IVREF50 },
65 	{ "ivref80", HDAA_QUIRK_IVREF80 },
66 	{ "ivref100", HDAA_QUIRK_IVREF100 },
67 	{ "ovref50", HDAA_QUIRK_OVREF50 },
68 	{ "ovref80", HDAA_QUIRK_OVREF80 },
69 	{ "ovref100", HDAA_QUIRK_OVREF100 },
70 	{ "ivref", HDAA_QUIRK_IVREF },
71 	{ "ovref", HDAA_QUIRK_OVREF },
72 	{ "vref", HDAA_QUIRK_VREF },
73 };
74 
75 #define HDA_PARSE_MAXDEPTH	10
76 
77 MALLOC_DEFINE(M_HDAA, "hdaa", "HDA Audio");
78 
79 static const char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue",
80     "Green", "Red", "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B",
81     "Res.C", "Res.D", "White", "Other"};
82 
83 static const char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD",
84     "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in",
85     "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"};
86 
87 static const char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
88 
89 static const char *HDA_CONNECTORS[16] = {
90     "Unknown", "1/8", "1/4", "ATAPI", "RCA", "Optical", "Digital", "Analog",
91     "DIN", "XLR", "RJ-11", "Combo", "0xc", "0xd", "0xe", "Other" };
92 
93 static const char *HDA_LOCS[64] = {
94     "0x00", "Rear", "Front", "Left", "Right", "Top", "Bottom", "Rear-panel",
95 	"Drive-bay", "0x09", "0x0a", "0x0b", "0x0c", "0x0d", "0x0e", "0x0f",
96     "Internal", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "Riser",
97 	"0x18", "Onboard", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f",
98     "External", "Ext-Rear", "Ext-Front", "Ext-Left", "Ext-Right", "Ext-Top", "Ext-Bottom", "0x07",
99 	"0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f",
100     "Other", "0x31", "0x32", "0x33", "0x34", "0x35", "Other-Bott", "Lid-In",
101 	"Lid-Out", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f" };
102 
103 static const char *HDA_GPIO_ACTIONS[8] = {
104     "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"};
105 
106 static const char *HDA_HDMI_CODING_TYPES[18] = {
107     "undefined", "LPCM", "AC-3", "MPEG1", "MP3", "MPEG2", "AAC-LC", "DTS",
108     "ATRAC", "DSD", "E-AC-3", "DTS-HD", "MLP", "DST", "WMAPro", "HE-AAC",
109     "HE-AACv2", "MPEG-Surround"
110 };
111 
112 /* Default */
113 static uint32_t hdaa_fmt[] = {
114 	SND_FORMAT(AFMT_S16_LE, 2, 0),
115 	0
116 };
117 
118 static struct pcmchan_caps hdaa_caps = {48000, 48000, hdaa_fmt, 0};
119 
120 static const struct {
121 	uint32_t	rate;
122 	int		valid;
123 	uint16_t	base;
124 	uint16_t	mul;
125 	uint16_t	div;
126 } hda_rate_tab[] = {
127 	{   8000, 1, 0x0000, 0x0000, 0x0500 },	/* (48000 * 1) / 6 */
128 	{   9600, 0, 0x0000, 0x0000, 0x0400 },	/* (48000 * 1) / 5 */
129 	{  12000, 0, 0x0000, 0x0000, 0x0300 },	/* (48000 * 1) / 4 */
130 	{  16000, 1, 0x0000, 0x0000, 0x0200 },	/* (48000 * 1) / 3 */
131 	{  18000, 0, 0x0000, 0x1000, 0x0700 },	/* (48000 * 3) / 8 */
132 	{  19200, 0, 0x0000, 0x0800, 0x0400 },	/* (48000 * 2) / 5 */
133 	{  24000, 0, 0x0000, 0x0000, 0x0100 },	/* (48000 * 1) / 2 */
134 	{  28800, 0, 0x0000, 0x1000, 0x0400 },	/* (48000 * 3) / 5 */
135 	{  32000, 1, 0x0000, 0x0800, 0x0200 },	/* (48000 * 2) / 3 */
136 	{  36000, 0, 0x0000, 0x1000, 0x0300 },	/* (48000 * 3) / 4 */
137 	{  38400, 0, 0x0000, 0x1800, 0x0400 },	/* (48000 * 4) / 5 */
138 	{  48000, 1, 0x0000, 0x0000, 0x0000 },	/* (48000 * 1) / 1 */
139 	{  64000, 0, 0x0000, 0x1800, 0x0200 },	/* (48000 * 4) / 3 */
140 	{  72000, 0, 0x0000, 0x1000, 0x0100 },	/* (48000 * 3) / 2 */
141 	{  96000, 1, 0x0000, 0x0800, 0x0000 },	/* (48000 * 2) / 1 */
142 	{ 144000, 0, 0x0000, 0x1000, 0x0000 },	/* (48000 * 3) / 1 */
143 	{ 192000, 1, 0x0000, 0x1800, 0x0000 },	/* (48000 * 4) / 1 */
144 	{   8820, 0, 0x4000, 0x0000, 0x0400 },	/* (44100 * 1) / 5 */
145 	{  11025, 1, 0x4000, 0x0000, 0x0300 },	/* (44100 * 1) / 4 */
146 	{  12600, 0, 0x4000, 0x0800, 0x0600 },	/* (44100 * 2) / 7 */
147 	{  14700, 0, 0x4000, 0x0000, 0x0200 },	/* (44100 * 1) / 3 */
148 	{  17640, 0, 0x4000, 0x0800, 0x0400 },	/* (44100 * 2) / 5 */
149 	{  18900, 0, 0x4000, 0x1000, 0x0600 },	/* (44100 * 3) / 7 */
150 	{  22050, 1, 0x4000, 0x0000, 0x0100 },	/* (44100 * 1) / 2 */
151 	{  25200, 0, 0x4000, 0x1800, 0x0600 },	/* (44100 * 4) / 7 */
152 	{  26460, 0, 0x4000, 0x1000, 0x0400 },	/* (44100 * 3) / 5 */
153 	{  29400, 0, 0x4000, 0x0800, 0x0200 },	/* (44100 * 2) / 3 */
154 	{  33075, 0, 0x4000, 0x1000, 0x0300 },	/* (44100 * 3) / 4 */
155 	{  35280, 0, 0x4000, 0x1800, 0x0400 },	/* (44100 * 4) / 5 */
156 	{  44100, 1, 0x4000, 0x0000, 0x0000 },	/* (44100 * 1) / 1 */
157 	{  58800, 0, 0x4000, 0x1800, 0x0200 },	/* (44100 * 4) / 3 */
158 	{  66150, 0, 0x4000, 0x1000, 0x0100 },	/* (44100 * 3) / 2 */
159 	{  88200, 1, 0x4000, 0x0800, 0x0000 },	/* (44100 * 2) / 1 */
160 	{ 132300, 0, 0x4000, 0x1000, 0x0000 },	/* (44100 * 3) / 1 */
161 	{ 176400, 1, 0x4000, 0x1800, 0x0000 },	/* (44100 * 4) / 1 */
162 };
163 #define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
164 
165 const static char *ossnames[] = SOUND_DEVICE_NAMES;
166 
167 /****************************************************************************
168  * Function prototypes
169  ****************************************************************************/
170 static int	hdaa_pcmchannel_setup(struct hdaa_chan *);
171 
172 static void	hdaa_widget_connection_select(struct hdaa_widget *, uint8_t);
173 static void	hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *,
174 						uint32_t, int, int);
175 static struct	hdaa_audio_ctl *hdaa_audio_ctl_amp_get(struct hdaa_devinfo *,
176 							nid_t, int, int, int);
177 static void	hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *,
178 				nid_t, int, int, int, int, int, int);
179 
180 static void	hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf);
181 
182 static char *
183 hdaa_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
184 {
185 	int i, first = 1;
186 
187 	bzero(buf, len);
188 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
189 		if (mask & (1 << i)) {
190 			if (first == 0)
191 				strlcat(buf, ", ", len);
192 			strlcat(buf, ossnames[i], len);
193 			first = 0;
194 		}
195 	}
196 	return (buf);
197 }
198 
199 static struct hdaa_audio_ctl *
200 hdaa_audio_ctl_each(struct hdaa_devinfo *devinfo, int *index)
201 {
202 	if (devinfo == NULL ||
203 	    index == NULL || devinfo->ctl == NULL ||
204 	    devinfo->ctlcnt < 1 ||
205 	    *index < 0 || *index >= devinfo->ctlcnt)
206 		return (NULL);
207 	return (&devinfo->ctl[(*index)++]);
208 }
209 
210 static struct hdaa_audio_ctl *
211 hdaa_audio_ctl_amp_get(struct hdaa_devinfo *devinfo, nid_t nid, int dir,
212 						int index, int cnt)
213 {
214 	struct hdaa_audio_ctl *ctl;
215 	int i, found = 0;
216 
217 	if (devinfo == NULL || devinfo->ctl == NULL)
218 		return (NULL);
219 
220 	i = 0;
221 	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
222 		if (ctl->enable == 0)
223 			continue;
224 		if (ctl->widget->nid != nid)
225 			continue;
226 		if (dir && ctl->ndir != dir)
227 			continue;
228 		if (index >= 0 && ctl->ndir == HDAA_CTL_IN &&
229 		    ctl->dir == ctl->ndir && ctl->index != index)
230 			continue;
231 		found++;
232 		if (found == cnt || cnt <= 0)
233 			return (ctl);
234 	}
235 
236 	return (NULL);
237 }
238 
239 static const struct matrix {
240 	struct pcmchan_matrix	m;
241 	int			analog;
242 } matrixes[]  = {
243     { SND_CHN_MATRIX_MAP_1_0,	1 },
244     { SND_CHN_MATRIX_MAP_2_0,	1 },
245     { SND_CHN_MATRIX_MAP_2_1,	0 },
246     { SND_CHN_MATRIX_MAP_3_0,	0 },
247     { SND_CHN_MATRIX_MAP_3_1,	0 },
248     { SND_CHN_MATRIX_MAP_4_0,	1 },
249     { SND_CHN_MATRIX_MAP_4_1,	0 },
250     { SND_CHN_MATRIX_MAP_5_0,	0 },
251     { SND_CHN_MATRIX_MAP_5_1,	1 },
252     { SND_CHN_MATRIX_MAP_6_0,	0 },
253     { SND_CHN_MATRIX_MAP_6_1,	0 },
254     { SND_CHN_MATRIX_MAP_7_0,	0 },
255     { SND_CHN_MATRIX_MAP_7_1,	1 },
256 };
257 
258 static const char *channel_names[] = SND_CHN_T_NAMES;
259 
260 /*
261  * Connected channels change handler.
262  */
263 static void
264 hdaa_channels_handler(struct hdaa_audio_as *as)
265 {
266 	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
267 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
268 	struct hdaa_chan *ch = &devinfo->chans[as->chans[0]];
269 	struct hdaa_widget *w;
270 	uint8_t *eld;
271 	int i, total, sub, assume, channels;
272 	uint16_t cpins, upins, tpins;
273 
274 	cpins = upins = 0;
275 	eld = NULL;
276 	for (i = 0; i < 16; i++) {
277 		if (as->pins[i] <= 0)
278 			continue;
279 		w = hdaa_widget_get(devinfo, as->pins[i]);
280 		if (w == NULL)
281 			continue;
282 		if (w->wclass.pin.connected == 1)
283 			cpins |= (1 << i);
284 		else if (w->wclass.pin.connected != 0)
285 			upins |= (1 << i);
286 		if (w->eld != NULL && w->eld_len >= 8)
287 			eld = w->eld;
288 	}
289 	tpins = cpins | upins;
290 	if (as->hpredir >= 0)
291 		tpins &= 0x7fff;
292 	if (tpins == 0)
293 		tpins = as->pinset;
294 
295 	total = sub = assume = channels = 0;
296 	if (eld) {
297 		/* Map CEA speakers to sound(4) channels. */
298 		if (eld[7] & 0x01) /* Front Left/Right */
299 			channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
300 		if (eld[7] & 0x02) /* Low Frequency Effect */
301 			channels |= SND_CHN_T_MASK_LF;
302 		if (eld[7] & 0x04) /* Front Center */
303 			channels |= SND_CHN_T_MASK_FC;
304 		if (eld[7] & 0x08) { /* Rear Left/Right */
305 			/* If we have both RLR and RLRC, report RLR as side. */
306 			if (eld[7] & 0x40) /* Rear Left/Right Center */
307 			    channels |= SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR;
308 			else
309 			    channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
310 		}
311 		if (eld[7] & 0x10) /* Rear center */
312 			channels |= SND_CHN_T_MASK_BC;
313 		if (eld[7] & 0x20) /* Front Left/Right Center */
314 			channels |= SND_CHN_T_MASK_FLC | SND_CHN_T_MASK_FRC;
315 		if (eld[7] & 0x40) /* Rear Left/Right Center */
316 			channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
317 	} else if (as->pinset != 0 && (tpins & 0xffe0) == 0) {
318 		/* Map UAA speakers to sound(4) channels. */
319 		if (tpins & 0x0001)
320 			channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
321 		if (tpins & 0x0002)
322 			channels |= SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF;
323 		if (tpins & 0x0004)
324 			channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
325 		if (tpins & 0x0008)
326 			channels |= SND_CHN_T_MASK_FLC | SND_CHN_T_MASK_FRC;
327 		if (tpins & 0x0010) {
328 			/* If there is no back pin, report side as back. */
329 			if ((as->pinset & 0x0004) == 0)
330 			    channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
331 			else
332 			    channels |= SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR;
333 		}
334 	} else if (as->mixed) {
335 		/* Mixed assoc can be only stereo or theoretically mono. */
336 		if (ch->channels == 1)
337 			channels |= SND_CHN_T_MASK_FC;
338 		else
339 			channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
340 	}
341 	if (channels) {	/* We have some usable channels info. */
342 		HDA_BOOTVERBOSE(
343 			device_printf(pdevinfo->dev, "%s channel set is: ",
344 			    as->dir == HDAA_CTL_OUT ? "Playback" : "Recording");
345 			for (i = 0; i < SND_CHN_T_MAX; i++)
346 				if (channels & (1 << i))
347 					printf("%s, ", channel_names[i]);
348 			printf("\n");
349 		);
350 		/* Look for maximal fitting matrix. */
351 		for (i = 0; i < sizeof(matrixes) / sizeof(struct matrix); i++) {
352 			if (as->pinset != 0 && matrixes[i].analog == 0)
353 				continue;
354 			if ((matrixes[i].m.mask & ~channels) == 0) {
355 				total = matrixes[i].m.channels;
356 				sub = matrixes[i].m.ext;
357 			}
358 		}
359 	}
360 	if (total == 0) {
361 		assume = 1;
362 		total = ch->channels;
363 		sub = (total == 6 || total == 8) ? 1 : 0;
364 	}
365 	HDA_BOOTVERBOSE(
366 		device_printf(pdevinfo->dev,
367 		    "%s channel matrix is: %s%d.%d (%s)\n",
368 		    as->dir == HDAA_CTL_OUT ? "Playback" : "Recording",
369 		    assume ? "unknown, assuming " : "", total - sub, sub,
370 		    cpins != 0 ? "connected" :
371 		    (upins != 0 ? "unknown" : "disconnected"));
372 	);
373 }
374 
375 /*
376  * Headphones redirection change handler.
377  */
378 static void
379 hdaa_hpredir_handler(struct hdaa_widget *w)
380 {
381 	struct hdaa_devinfo *devinfo = w->devinfo;
382 	struct hdaa_audio_as *as = &devinfo->as[w->bindas];
383 	struct hdaa_widget *w1;
384 	struct hdaa_audio_ctl *ctl;
385 	uint32_t val;
386 	int j, connected = w->wclass.pin.connected;
387 
388 	HDA_BOOTVERBOSE(
389 		device_printf((as->pdevinfo && as->pdevinfo->dev) ?
390 		    as->pdevinfo->dev : devinfo->dev,
391 		    "Redirect output to: %s\n",
392 		    connected ? "headphones": "main");
393 	);
394 	/* (Un)Mute headphone pin. */
395 	ctl = hdaa_audio_ctl_amp_get(devinfo,
396 	    w->nid, HDAA_CTL_IN, -1, 1);
397 	if (ctl != NULL && ctl->mute) {
398 		/* If pin has muter - use it. */
399 		val = connected ? 0 : 1;
400 		if (val != ctl->forcemute) {
401 			ctl->forcemute = val;
402 			hdaa_audio_ctl_amp_set(ctl,
403 			    HDAA_AMP_MUTE_DEFAULT,
404 			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
405 		}
406 	} else {
407 		/* If there is no muter - disable pin output. */
408 		if (connected)
409 			val = w->wclass.pin.ctrl |
410 			    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
411 		else
412 			val = w->wclass.pin.ctrl &
413 			    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
414 		if (val != w->wclass.pin.ctrl) {
415 			w->wclass.pin.ctrl = val;
416 			hda_command(devinfo->dev,
417 			    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
418 			    w->nid, w->wclass.pin.ctrl));
419 		}
420 	}
421 	/* (Un)Mute other pins. */
422 	for (j = 0; j < 15; j++) {
423 		if (as->pins[j] <= 0)
424 			continue;
425 		ctl = hdaa_audio_ctl_amp_get(devinfo,
426 		    as->pins[j], HDAA_CTL_IN, -1, 1);
427 		if (ctl != NULL && ctl->mute) {
428 			/* If pin has muter - use it. */
429 			val = connected ? 1 : 0;
430 			if (val == ctl->forcemute)
431 				continue;
432 			ctl->forcemute = val;
433 			hdaa_audio_ctl_amp_set(ctl,
434 			    HDAA_AMP_MUTE_DEFAULT,
435 			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
436 			continue;
437 		}
438 		/* If there is no muter - disable pin output. */
439 		w1 = hdaa_widget_get(devinfo, as->pins[j]);
440 		if (w1 != NULL) {
441 			if (connected)
442 				val = w1->wclass.pin.ctrl &
443 				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
444 			else
445 				val = w1->wclass.pin.ctrl |
446 				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
447 			if (val != w1->wclass.pin.ctrl) {
448 				w1->wclass.pin.ctrl = val;
449 				hda_command(devinfo->dev,
450 				    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
451 				    w1->nid, w1->wclass.pin.ctrl));
452 			}
453 		}
454 	}
455 }
456 
457 /*
458  * Recording source change handler.
459  */
460 static void
461 hdaa_autorecsrc_handler(struct hdaa_audio_as *as, struct hdaa_widget *w)
462 {
463 	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
464 	struct hdaa_devinfo *devinfo;
465 	struct hdaa_widget *w1;
466 	int i, mask, fullmask, prio, bestprio;
467 	char buf[128];
468 
469 	if (!as->mixed || pdevinfo == NULL || pdevinfo->mixer == NULL)
470 		return;
471 	/* Don't touch anything if we asked not to. */
472 	if (pdevinfo->autorecsrc == 0 ||
473 	    (pdevinfo->autorecsrc == 1 && w != NULL))
474 		return;
475 	/* Don't touch anything if "mix" or "speaker" selected. */
476 	if (pdevinfo->recsrc & (SOUND_MASK_IMIX | SOUND_MASK_SPEAKER))
477 		return;
478 	/* Don't touch anything if several selected. */
479 	if (ffs(pdevinfo->recsrc) != fls(pdevinfo->recsrc))
480 		return;
481 	devinfo = pdevinfo->devinfo;
482 	mask = fullmask = 0;
483 	bestprio = 0;
484 	for (i = 0; i < 16; i++) {
485 		if (as->pins[i] <= 0)
486 			continue;
487 		w1 = hdaa_widget_get(devinfo, as->pins[i]);
488 		if (w1 == NULL || w1->enable == 0)
489 			continue;
490 		if (w1->wclass.pin.connected == 0)
491 			continue;
492 		prio = (w1->wclass.pin.connected == 1) ? 2 : 1;
493 		if (prio < bestprio)
494 			continue;
495 		if (prio > bestprio) {
496 			mask = 0;
497 			bestprio = prio;
498 		}
499 		mask |= (1 << w1->ossdev);
500 		fullmask |= (1 << w1->ossdev);
501 	}
502 	if (mask == 0)
503 		return;
504 	/* Prefer newly connected input. */
505 	if (w != NULL && (mask & (1 << w->ossdev)))
506 		mask = (1 << w->ossdev);
507 	/* Prefer previously selected input */
508 	if (mask & pdevinfo->recsrc)
509 		mask &= pdevinfo->recsrc;
510 	/* Prefer mic. */
511 	if (mask & SOUND_MASK_MIC)
512 		mask = SOUND_MASK_MIC;
513 	/* Prefer monitor (2nd mic). */
514 	if (mask & SOUND_MASK_MONITOR)
515 		mask = SOUND_MASK_MONITOR;
516 	/* Just take first one. */
517 	mask = (1 << (ffs(mask) - 1));
518 	HDA_BOOTVERBOSE(
519 		hdaa_audio_ctl_ossmixer_mask2allname(mask, buf, sizeof(buf));
520 		device_printf(pdevinfo->dev,
521 		    "Automatically set rec source to: %s\n", buf);
522 	);
523 	hdaa_unlock(devinfo);
524 	mix_setrecsrc(pdevinfo->mixer, mask);
525 	hdaa_lock(devinfo);
526 }
527 
528 /*
529  * Jack presence detection event handler.
530  */
531 static void
532 hdaa_presence_handler(struct hdaa_widget *w)
533 {
534 	struct hdaa_devinfo *devinfo = w->devinfo;
535 	struct hdaa_audio_as *as;
536 	uint32_t res;
537 	int connected, old;
538 
539 	if (w->enable == 0 || w->type !=
540 	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
541 		return;
542 
543 	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
544 	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
545 		return;
546 
547 	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
548 	connected = (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) != 0;
549 	if (devinfo->quirks & HDAA_QUIRK_SENSEINV)
550 		connected = !connected;
551 	old = w->wclass.pin.connected;
552 	if (connected == old)
553 		return;
554 	w->wclass.pin.connected = connected;
555 	HDA_BOOTVERBOSE(
556 		if (connected || old != 2) {
557 			device_printf(devinfo->dev,
558 			    "Pin sense: nid=%d sense=0x%08x (%sconnected)\n",
559 			    w->nid, res, !connected ? "dis" : "");
560 		}
561 	);
562 
563 	as = &devinfo->as[w->bindas];
564 	if (as->hpredir >= 0 && as->pins[15] == w->nid)
565 		hdaa_hpredir_handler(w);
566 	if (as->dir == HDAA_CTL_IN && old != 2)
567 		hdaa_autorecsrc_handler(as, w);
568 	if (old != 2)
569 		hdaa_channels_handler(as);
570 }
571 
572 /*
573  * Callback for poll based presence detection.
574  */
575 static void
576 hdaa_jack_poll_callback(void *arg)
577 {
578 	struct hdaa_devinfo *devinfo = arg;
579 	struct hdaa_widget *w;
580 	int i;
581 
582 	hdaa_lock(devinfo);
583 	if (devinfo->poll_ival == 0) {
584 		hdaa_unlock(devinfo);
585 		return;
586 	}
587 	for (i = 0; i < devinfo->ascnt; i++) {
588 		if (devinfo->as[i].hpredir < 0)
589 			continue;
590 		w = hdaa_widget_get(devinfo, devinfo->as[i].pins[15]);
591 		if (w == NULL || w->enable == 0 || w->type !=
592 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
593 			continue;
594 		hdaa_presence_handler(w);
595 	}
596 	callout_reset(&devinfo->poll_jack, devinfo->poll_ival,
597 	    hdaa_jack_poll_callback, devinfo);
598 	hdaa_unlock(devinfo);
599 }
600 
601 static void
602 hdaa_eld_dump(struct hdaa_widget *w)
603 {
604 	struct hdaa_devinfo *devinfo = w->devinfo;
605 	device_t dev = devinfo->dev;
606 	uint8_t *sad;
607 	int len, mnl, i, sadc, fmt;
608 
609 	if (w->eld == NULL || w->eld_len < 4)
610 		return;
611 	device_printf(dev,
612 	    "ELD nid=%d: ELD_Ver=%u Baseline_ELD_Len=%u\n",
613 	    w->nid, w->eld[0] >> 3, w->eld[2]);
614 	if ((w->eld[0] >> 3) != 0x02)
615 		return;
616 	len = min(w->eld_len, (u_int)w->eld[2] * 4);
617 	mnl = w->eld[4] & 0x1f;
618 	device_printf(dev,
619 	    "ELD nid=%d: CEA_EDID_Ver=%u MNL=%u\n",
620 	    w->nid, w->eld[4] >> 5, mnl);
621 	sadc = w->eld[5] >> 4;
622 	device_printf(dev,
623 	    "ELD nid=%d: SAD_Count=%u Conn_Type=%u S_AI=%u HDCP=%u\n",
624 	    w->nid, sadc, (w->eld[5] >> 2) & 0x3,
625 	    (w->eld[5] >> 1) & 0x1, w->eld[5] & 0x1);
626 	device_printf(dev,
627 	    "ELD nid=%d: Aud_Synch_Delay=%ums\n",
628 	    w->nid, w->eld[6] * 2);
629 	device_printf(dev,
630 	    "ELD nid=%d: Channels=0x%b\n",
631 	    w->nid, w->eld[7],
632 	    "\020\07RLRC\06FLRC\05RC\04RLR\03FC\02LFE\01FLR");
633 	device_printf(dev,
634 	    "ELD nid=%d: Port_ID=0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
635 	    w->nid, w->eld[8], w->eld[9], w->eld[10], w->eld[11],
636 	    w->eld[12], w->eld[13], w->eld[14], w->eld[15]);
637 	device_printf(dev,
638 	    "ELD nid=%d: Manufacturer_Name=0x%02x%02x\n",
639 	    w->nid, w->eld[16], w->eld[17]);
640 	device_printf(dev,
641 	    "ELD nid=%d: Product_Code=0x%02x%02x\n",
642 	    w->nid, w->eld[18], w->eld[19]);
643 	device_printf(dev,
644 	    "ELD nid=%d: Monitor_Name_String='%.*s'\n",
645 	    w->nid, mnl, &w->eld[20]);
646 	for (i = 0; i < sadc; i++) {
647 		sad = &w->eld[20 + mnl + i * 3];
648 		fmt = (sad[0] >> 3) & 0x0f;
649 		if (fmt == HDA_HDMI_CODING_TYPE_REF_CTX) {
650 			fmt = (sad[2] >> 3) & 0x1f;
651 			if (fmt < 1 || fmt > 3)
652 				fmt = 0;
653 			else
654 				fmt += 14;
655 		}
656 		device_printf(dev,
657 		    "ELD nid=%d: %s %dch freqs=0x%b",
658 		    w->nid, HDA_HDMI_CODING_TYPES[fmt], (sad[0] & 0x07) + 1,
659 		    sad[1], "\020\007192\006176\00596\00488\00348\00244\00132");
660 		switch (fmt) {
661 		case HDA_HDMI_CODING_TYPE_LPCM:
662 			printf(" sizes=0x%b",
663 			    sad[2] & 0x07, "\020\00324\00220\00116");
664 			break;
665 		case HDA_HDMI_CODING_TYPE_AC3:
666 		case HDA_HDMI_CODING_TYPE_MPEG1:
667 		case HDA_HDMI_CODING_TYPE_MP3:
668 		case HDA_HDMI_CODING_TYPE_MPEG2:
669 		case HDA_HDMI_CODING_TYPE_AACLC:
670 		case HDA_HDMI_CODING_TYPE_DTS:
671 		case HDA_HDMI_CODING_TYPE_ATRAC:
672 			printf(" max_bitrate=%d", sad[2] * 8000);
673 			break;
674 		case HDA_HDMI_CODING_TYPE_WMAPRO:
675 			printf(" profile=%d", sad[2] & 0x07);
676 			break;
677 		}
678 		printf("\n");
679 	}
680 }
681 
682 static void
683 hdaa_eld_handler(struct hdaa_widget *w)
684 {
685 	struct hdaa_devinfo *devinfo = w->devinfo;
686 	uint32_t res;
687 	int i;
688 
689 	if (w->enable == 0 || w->type !=
690 	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
691 		return;
692 
693 	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
694 	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
695 		return;
696 
697 	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
698 	if ((w->eld != 0) == ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) != 0))
699 		return;
700 	if (w->eld != NULL) {
701 		w->eld_len = 0;
702 		free(w->eld, M_HDAA);
703 		w->eld = NULL;
704 	}
705 	HDA_BOOTVERBOSE(
706 		device_printf(devinfo->dev,
707 		    "Pin sense: nid=%d sense=0x%08x "
708 		    "(%sconnected, ELD %svalid)\n",
709 		    w->nid, res,
710 		    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ? "" : "dis",
711 		    (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) ? "" : "in");
712 	);
713 	if ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) == 0)
714 		return;
715 
716 	res = hda_command(devinfo->dev,
717 	    HDA_CMD_GET_HDMI_DIP_SIZE(0, w->nid, 0x08));
718 	if (res == HDA_INVALID)
719 		return;
720 	w->eld_len = res & 0xff;
721 	if (w->eld_len != 0)
722 		w->eld = malloc(w->eld_len, M_HDAA, M_ZERO | M_NOWAIT);
723 	if (w->eld == NULL) {
724 		w->eld_len = 0;
725 		return;
726 	}
727 
728 	for (i = 0; i < w->eld_len; i++) {
729 		res = hda_command(devinfo->dev,
730 		    HDA_CMD_GET_HDMI_ELDD(0, w->nid, i));
731 		if (res & 0x80000000)
732 			w->eld[i] = res & 0xff;
733 	}
734 	HDA_BOOTVERBOSE(
735 		hdaa_eld_dump(w);
736 	);
737 	hdaa_channels_handler(&devinfo->as[w->bindas]);
738 }
739 
740 /*
741  * Pin sense initializer.
742  */
743 static void
744 hdaa_sense_init(struct hdaa_devinfo *devinfo)
745 {
746 	struct hdaa_audio_as *as;
747 	struct hdaa_widget *w;
748 	int i, poll = 0;
749 
750 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
751 		w = hdaa_widget_get(devinfo, i);
752 		if (w == NULL || w->enable == 0 || w->type !=
753 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
754 			continue;
755 		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap)) {
756 			if (w->unsol < 0)
757 				w->unsol = HDAC_UNSOL_ALLOC(
758 				    device_get_parent(devinfo->dev),
759 				    devinfo->dev, w->nid);
760 			hda_command(devinfo->dev,
761 			    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid,
762 			    HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE | w->unsol));
763 		}
764 		as = &devinfo->as[w->bindas];
765 		if (as->hpredir >= 0 && as->pins[15] == w->nid) {
766 			if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
767 			    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0) {
768 				device_printf(devinfo->dev,
769 				    "No presence detection support at nid %d\n",
770 				    w->nid);
771 			} else {
772 				if (w->unsol < 0)
773 					poll = 1;
774 				HDA_BOOTVERBOSE(
775 					device_printf(devinfo->dev,
776 					    "Headphones redirection for "
777 					    "association %d nid=%d using %s.\n",
778 					    w->bindas, w->nid,
779 					    (w->unsol < 0) ? "polling" :
780 					    "unsolicited responses");
781 				);
782 			}
783 		}
784 		hdaa_presence_handler(w);
785 		if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) &&
786 		    !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
787 			continue;
788 		hdaa_eld_handler(w);
789 	}
790 	if (poll) {
791 		callout_reset(&devinfo->poll_jack, 1,
792 		    hdaa_jack_poll_callback, devinfo);
793 	}
794 }
795 
796 static void
797 hdaa_sense_deinit(struct hdaa_devinfo *devinfo)
798 {
799 	struct hdaa_widget *w;
800 	int i;
801 
802 	callout_stop(&devinfo->poll_jack);
803 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
804 		w = hdaa_widget_get(devinfo, i);
805 		if (w == NULL || w->enable == 0 || w->type !=
806 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
807 			continue;
808 		if (w->unsol < 0)
809 			continue;
810 		hda_command(devinfo->dev,
811 		    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0));
812 		HDAC_UNSOL_FREE(
813 		    device_get_parent(devinfo->dev), devinfo->dev,
814 		    w->unsol);
815 		w->unsol = -1;
816 	}
817 }
818 
819 uint32_t
820 hdaa_widget_pin_patch(uint32_t config, const char *str)
821 {
822 	char buf[256];
823 	char *key, *value, *rest, *bad;
824 	int ival, i;
825 
826 	strlcpy(buf, str, sizeof(buf));
827 	rest = buf;
828 	while ((key = strsep(&rest, "=")) != NULL) {
829 		value = strsep(&rest, " \t");
830 		if (value == NULL)
831 			break;
832 		ival = strtol(value, &bad, 10);
833 		if (strcmp(key, "seq") == 0) {
834 			config &= ~HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK;
835 			config |= ((ival << HDA_CONFIG_DEFAULTCONF_SEQUENCE_SHIFT) &
836 			    HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK);
837 		} else if (strcmp(key, "as") == 0) {
838 			config &= ~HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK;
839 			config |= ((ival << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT) &
840 			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK);
841 		} else if (strcmp(key, "misc") == 0) {
842 			config &= ~HDA_CONFIG_DEFAULTCONF_MISC_MASK;
843 			config |= ((ival << HDA_CONFIG_DEFAULTCONF_MISC_SHIFT) &
844 			    HDA_CONFIG_DEFAULTCONF_MISC_MASK);
845 		} else if (strcmp(key, "color") == 0) {
846 			config &= ~HDA_CONFIG_DEFAULTCONF_COLOR_MASK;
847 			if (bad[0] == 0) {
848 				config |= ((ival << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT) &
849 				    HDA_CONFIG_DEFAULTCONF_COLOR_MASK);
850 			}
851 			for (i = 0; i < 16; i++) {
852 				if (strcasecmp(HDA_COLORS[i], value) == 0) {
853 					config |= (i << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT);
854 					break;
855 				}
856 			}
857 		} else if (strcmp(key, "ctype") == 0) {
858 			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK;
859 			if (bad[0] == 0) {
860 			config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT) &
861 			    HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK);
862 			}
863 			for (i = 0; i < 16; i++) {
864 				if (strcasecmp(HDA_CONNECTORS[i], value) == 0) {
865 					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT);
866 					break;
867 				}
868 			}
869 		} else if (strcmp(key, "device") == 0) {
870 			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
871 			if (bad[0] == 0) {
872 				config |= ((ival << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT) &
873 				    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK);
874 				continue;
875 			}
876 			for (i = 0; i < 16; i++) {
877 				if (strcasecmp(HDA_DEVS[i], value) == 0) {
878 					config |= (i << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT);
879 					break;
880 				}
881 			}
882 		} else if (strcmp(key, "loc") == 0) {
883 			config &= ~HDA_CONFIG_DEFAULTCONF_LOCATION_MASK;
884 			if (bad[0] == 0) {
885 				config |= ((ival << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT) &
886 				    HDA_CONFIG_DEFAULTCONF_LOCATION_MASK);
887 				continue;
888 			}
889 			for (i = 0; i < 64; i++) {
890 				if (strcasecmp(HDA_LOCS[i], value) == 0) {
891 					config |= (i << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT);
892 					break;
893 				}
894 			}
895 		} else if (strcmp(key, "conn") == 0) {
896 			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
897 			if (bad[0] == 0) {
898 				config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT) &
899 				    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
900 				continue;
901 			}
902 			for (i = 0; i < 4; i++) {
903 				if (strcasecmp(HDA_CONNS[i], value) == 0) {
904 					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT);
905 					break;
906 				}
907 			}
908 		}
909 	}
910 	return (config);
911 }
912 
913 uint32_t
914 hdaa_gpio_patch(uint32_t gpio, const char *str)
915 {
916 	char buf[256];
917 	char *key, *value, *rest;
918 	int ikey, i;
919 
920 	strlcpy(buf, str, sizeof(buf));
921 	rest = buf;
922 	while ((key = strsep(&rest, "=")) != NULL) {
923 		value = strsep(&rest, " \t");
924 		if (value == NULL)
925 			break;
926 		ikey = strtol(key, NULL, 10);
927 		if (ikey < 0 || ikey > 7)
928 			continue;
929 		for (i = 0; i < 7; i++) {
930 			if (strcasecmp(HDA_GPIO_ACTIONS[i], value) == 0) {
931 				gpio &= ~HDAA_GPIO_MASK(ikey);
932 				gpio |= i << HDAA_GPIO_SHIFT(ikey);
933 				break;
934 			}
935 		}
936 	}
937 	return (gpio);
938 }
939 
940 static void
941 hdaa_local_patch_pin(struct hdaa_widget *w)
942 {
943 	device_t dev = w->devinfo->dev;
944 	const char *res = NULL;
945 	uint32_t config, orig;
946 	char buf[32];
947 
948 	config = orig = w->wclass.pin.config;
949 	snprintf(buf, sizeof(buf), "cad%u.nid%u.config",
950 	    hda_get_codec_id(dev), w->nid);
951 	if (resource_string_value(device_get_name(
952 	    device_get_parent(device_get_parent(dev))),
953 	    device_get_unit(device_get_parent(device_get_parent(dev))),
954 	    buf, &res) == 0) {
955 		if (strncmp(res, "0x", 2) == 0) {
956 			config = strtol(res + 2, NULL, 16);
957 		} else {
958 			config = hdaa_widget_pin_patch(config, res);
959 		}
960 	}
961 	snprintf(buf, sizeof(buf), "nid%u.config", w->nid);
962 	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
963 	    buf, &res) == 0) {
964 		if (strncmp(res, "0x", 2) == 0) {
965 			config = strtol(res + 2, NULL, 16);
966 		} else {
967 			config = hdaa_widget_pin_patch(config, res);
968 		}
969 	}
970 	HDA_BOOTVERBOSE(
971 		if (config != orig)
972 			device_printf(w->devinfo->dev,
973 			    "Patching pin config nid=%u 0x%08x -> 0x%08x\n",
974 			    w->nid, orig, config);
975 	);
976 	w->wclass.pin.newconf = w->wclass.pin.config = config;
977 }
978 
979 static void
980 hdaa_dump_audio_formats_sb(struct sbuf *sb, uint32_t fcap, uint32_t pcmcap)
981 {
982 	uint32_t cap;
983 
984 	cap = fcap;
985 	if (cap != 0) {
986 		sbuf_printf(sb, "     Stream cap: 0x%08x", cap);
987 		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
988 			sbuf_printf(sb, " AC3");
989 		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
990 			sbuf_printf(sb, " FLOAT32");
991 		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
992 			sbuf_printf(sb, " PCM");
993 		sbuf_printf(sb, "\n");
994 	}
995 	cap = pcmcap;
996 	if (cap != 0) {
997 		sbuf_printf(sb, "        PCM cap: 0x%08x", cap);
998 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
999 			sbuf_printf(sb, " 8");
1000 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
1001 			sbuf_printf(sb, " 16");
1002 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
1003 			sbuf_printf(sb, " 20");
1004 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
1005 			sbuf_printf(sb, " 24");
1006 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
1007 			sbuf_printf(sb, " 32");
1008 		sbuf_printf(sb, " bits,");
1009 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
1010 			sbuf_printf(sb, " 8");
1011 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
1012 			sbuf_printf(sb, " 11");
1013 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
1014 			sbuf_printf(sb, " 16");
1015 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
1016 			sbuf_printf(sb, " 22");
1017 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
1018 			sbuf_printf(sb, " 32");
1019 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
1020 			sbuf_printf(sb, " 44");
1021 		sbuf_printf(sb, " 48");
1022 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
1023 			sbuf_printf(sb, " 88");
1024 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
1025 			sbuf_printf(sb, " 96");
1026 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
1027 			sbuf_printf(sb, " 176");
1028 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
1029 			sbuf_printf(sb, " 192");
1030 		sbuf_printf(sb, " KHz\n");
1031 	}
1032 }
1033 
1034 static void
1035 hdaa_dump_pin_sb(struct sbuf *sb, struct hdaa_widget *w)
1036 {
1037 	uint32_t pincap, conf;
1038 
1039 	pincap = w->wclass.pin.cap;
1040 
1041 	sbuf_printf(sb, "        Pin cap: 0x%08x", pincap);
1042 	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
1043 		sbuf_printf(sb, " ISC");
1044 	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
1045 		sbuf_printf(sb, " TRQD");
1046 	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
1047 		sbuf_printf(sb, " PDC");
1048 	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
1049 		sbuf_printf(sb, " HP");
1050 	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
1051 		sbuf_printf(sb, " OUT");
1052 	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
1053 		sbuf_printf(sb, " IN");
1054 	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
1055 		sbuf_printf(sb, " BAL");
1056 	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
1057 		sbuf_printf(sb, " HDMI");
1058 	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
1059 		sbuf_printf(sb, " VREF[");
1060 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
1061 			sbuf_printf(sb, " 50");
1062 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
1063 			sbuf_printf(sb, " 80");
1064 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
1065 			sbuf_printf(sb, " 100");
1066 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
1067 			sbuf_printf(sb, " GROUND");
1068 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
1069 			sbuf_printf(sb, " HIZ");
1070 		sbuf_printf(sb, " ]");
1071 	}
1072 	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
1073 		sbuf_printf(sb, " EAPD");
1074 	if (HDA_PARAM_PIN_CAP_DP(pincap))
1075 		sbuf_printf(sb, " DP");
1076 	if (HDA_PARAM_PIN_CAP_HBR(pincap))
1077 		sbuf_printf(sb, " HBR");
1078 	sbuf_printf(sb, "\n");
1079 	conf = w->wclass.pin.config;
1080 	sbuf_printf(sb, "     Pin config: 0x%08x", conf);
1081 	sbuf_printf(sb, " as=%d seq=%d "
1082 	    "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d\n",
1083 	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
1084 	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
1085 	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
1086 	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
1087 	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
1088 	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
1089 	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
1090 	    HDA_CONFIG_DEFAULTCONF_MISC(conf));
1091 	sbuf_printf(sb, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
1092 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
1093 		sbuf_printf(sb, " HP");
1094 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
1095 		sbuf_printf(sb, " IN");
1096 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
1097 		sbuf_printf(sb, " OUT");
1098 	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1099 		if ((w->wclass.pin.ctrl &
1100 		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
1101 			sbuf_printf(sb, " HBR");
1102 		else if ((w->wclass.pin.ctrl &
1103 		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
1104 			sbuf_printf(sb, " EPTs");
1105 	} else {
1106 		if ((w->wclass.pin.ctrl &
1107 		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
1108 			sbuf_printf(sb, " VREFs");
1109 	}
1110 	sbuf_printf(sb, "\n");
1111 }
1112 
1113 static void
1114 hdaa_dump_amp_sb(struct sbuf *sb, uint32_t cap, const char *banner)
1115 {
1116 	int offset, size, step;
1117 
1118 	offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap);
1119 	size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap);
1120 	step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap);
1121 	sbuf_printf(sb, "     %s amp: 0x%08x "
1122 	    "mute=%d step=%d size=%d offset=%d (%+d/%+ddB)\n",
1123 	    banner, cap,
1124 	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
1125 	    step, size, offset,
1126 	    ((0 - offset) * (size + 1)) / 4,
1127 	    ((step - offset) * (size + 1)) / 4);
1128 }
1129 
1130 
1131 static int
1132 hdaa_sysctl_caps(SYSCTL_HANDLER_ARGS)
1133 {
1134 	struct hdaa_devinfo *devinfo;
1135 	struct hdaa_widget *w, *cw;
1136 	struct sbuf sb;
1137 	char buf[64];
1138 	int error, j;
1139 
1140 	w = (struct hdaa_widget *)oidp->oid_arg1;
1141 	devinfo = w->devinfo;
1142 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1143 
1144 	sbuf_printf(&sb, "%s%s\n", w->name,
1145 	    (w->enable == 0) ? " [DISABLED]" : "");
1146 	sbuf_printf(&sb, "     Widget cap: 0x%08x",
1147 	    w->param.widget_cap);
1148 	if (w->param.widget_cap & 0x0ee1) {
1149 		if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
1150 		    sbuf_printf(&sb, " LRSWAP");
1151 		if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
1152 		    sbuf_printf(&sb, " PWR");
1153 		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
1154 		    sbuf_printf(&sb, " DIGITAL");
1155 		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
1156 		    sbuf_printf(&sb, " UNSOL");
1157 		if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
1158 		    sbuf_printf(&sb, " PROC");
1159 		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
1160 		    sbuf_printf(&sb, " STRIPE(x%d)",
1161 			1 << (fls(w->wclass.conv.stripecap) - 1));
1162 		j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1163 		if (j == 1)
1164 		    sbuf_printf(&sb, " STEREO");
1165 		else if (j > 1)
1166 		    sbuf_printf(&sb, " %dCH", j + 1);
1167 	}
1168 	sbuf_printf(&sb, "\n");
1169 	if (w->bindas != -1) {
1170 		sbuf_printf(&sb, "    Association: %d (0x%04x)\n",
1171 		    w->bindas, w->bindseqmask);
1172 	}
1173 	if (w->ossmask != 0 || w->ossdev >= 0) {
1174 		sbuf_printf(&sb, "            OSS: %s",
1175 		    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
1176 		if (w->ossdev >= 0)
1177 		    sbuf_printf(&sb, " (%s)", ossnames[w->ossdev]);
1178 		sbuf_printf(&sb, "\n");
1179 	}
1180 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1181 	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1182 		hdaa_dump_audio_formats_sb(&sb,
1183 		    w->param.supp_stream_formats,
1184 		    w->param.supp_pcm_size_rate);
1185 	} else if (w->type ==
1186 	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
1187 		hdaa_dump_pin_sb(&sb, w);
1188 	if (w->param.eapdbtl != HDA_INVALID) {
1189 		sbuf_printf(&sb, "           EAPD: 0x%08x%s%s%s\n",
1190 		    w->param.eapdbtl,
1191 		    (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_LR_SWAP) ?
1192 		     " LRSWAP" : "",
1193 		    (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD) ?
1194 		     " EAPD" : "",
1195 		    (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_BTL) ?
1196 		     " BTL" : "");
1197 	}
1198 	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
1199 	    w->param.outamp_cap != 0)
1200 		hdaa_dump_amp_sb(&sb, w->param.outamp_cap, "Output");
1201 	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
1202 	    w->param.inamp_cap != 0)
1203 		hdaa_dump_amp_sb(&sb, w->param.inamp_cap, " Input");
1204 	if (w->nconns > 0)
1205 		sbuf_printf(&sb, "    Connections: %d\n", w->nconns);
1206 	for (j = 0; j < w->nconns; j++) {
1207 		cw = hdaa_widget_get(devinfo, w->conns[j]);
1208 		sbuf_printf(&sb, "          + %s<- nid=%d [%s]",
1209 		    (w->connsenable[j] == 0)?"[DISABLED] ":"",
1210 		    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
1211 		if (cw == NULL)
1212 			sbuf_printf(&sb, " [UNKNOWN]");
1213 		else if (cw->enable == 0)
1214 			sbuf_printf(&sb, " [DISABLED]");
1215 		if (w->nconns > 1 && w->selconn == j && w->type !=
1216 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
1217 			sbuf_printf(&sb, " (selected)");
1218 		sbuf_printf(&sb, "\n");
1219 	}
1220 	error = sbuf_finish(&sb);
1221 	sbuf_delete(&sb);
1222 	return (error);
1223 }
1224 
1225 static int
1226 hdaa_sysctl_config(SYSCTL_HANDLER_ARGS)
1227 {
1228 	char buf[256];
1229 	int error;
1230 	uint32_t conf;
1231 
1232 	conf = *(uint32_t *)oidp->oid_arg1;
1233 	snprintf(buf, sizeof(buf), "0x%08x as=%d seq=%d "
1234 	    "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d",
1235 	    conf,
1236 	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
1237 	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
1238 	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
1239 	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
1240 	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
1241 	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
1242 	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
1243 	    HDA_CONFIG_DEFAULTCONF_MISC(conf));
1244 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1245 	if (error != 0 || req->newptr == NULL)
1246 		return (error);
1247 	if (strncmp(buf, "0x", 2) == 0)
1248 		conf = strtol(buf + 2, NULL, 16);
1249 	else
1250 		conf = hdaa_widget_pin_patch(conf, buf);
1251 	*(uint32_t *)oidp->oid_arg1 = conf;
1252 	return (0);
1253 }
1254 
1255 static void
1256 hdaa_config_fetch(const char *str, uint32_t *on, uint32_t *off)
1257 {
1258 	int i = 0, j, k, len, inv;
1259 
1260 	for (;;) {
1261 		while (str[i] != '\0' &&
1262 		    (str[i] == ',' || isspace(str[i]) != 0))
1263 			i++;
1264 		if (str[i] == '\0')
1265 			return;
1266 		j = i;
1267 		while (str[j] != '\0' &&
1268 		    !(str[j] == ',' || isspace(str[j]) != 0))
1269 			j++;
1270 		len = j - i;
1271 		if (len > 2 && strncmp(str + i, "no", 2) == 0)
1272 			inv = 2;
1273 		else
1274 			inv = 0;
1275 		for (k = 0; len > inv && k < nitems(hdaa_quirks_tab); k++) {
1276 			if (strncmp(str + i + inv,
1277 			    hdaa_quirks_tab[k].key, len - inv) != 0)
1278 				continue;
1279 			if (len - inv != strlen(hdaa_quirks_tab[k].key))
1280 				continue;
1281 			if (inv == 0) {
1282 				*on |= hdaa_quirks_tab[k].value;
1283 				*off &= ~hdaa_quirks_tab[k].value;
1284 			} else {
1285 				*off |= hdaa_quirks_tab[k].value;
1286 				*on &= ~hdaa_quirks_tab[k].value;
1287 			}
1288 			break;
1289 		}
1290 		i = j;
1291 	}
1292 }
1293 
1294 static int
1295 hdaa_sysctl_quirks(SYSCTL_HANDLER_ARGS)
1296 {
1297 	char buf[256];
1298 	int error, n = 0, i;
1299 	uint32_t quirks, quirks_off;
1300 
1301 	quirks = *(uint32_t *)oidp->oid_arg1;
1302 	buf[0] = 0;
1303 	for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
1304 		if ((quirks & hdaa_quirks_tab[i].value) != 0)
1305 			n += snprintf(buf + n, sizeof(buf) - n, "%s%s",
1306 			    n != 0 ? "," : "", hdaa_quirks_tab[i].key);
1307 	}
1308 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1309 	if (error != 0 || req->newptr == NULL)
1310 		return (error);
1311 	if (strncmp(buf, "0x", 2) == 0)
1312 		quirks = strtol(buf + 2, NULL, 16);
1313 	else {
1314 		quirks = 0;
1315 		hdaa_config_fetch(buf, &quirks, &quirks_off);
1316 	}
1317 	*(uint32_t *)oidp->oid_arg1 = quirks;
1318 	return (0);
1319 }
1320 
1321 static void
1322 hdaa_local_patch(struct hdaa_devinfo *devinfo)
1323 {
1324 	struct hdaa_widget *w;
1325 	const char *res = NULL;
1326 	uint32_t quirks_on = 0, quirks_off = 0, x;
1327 	int i;
1328 
1329 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1330 		w = hdaa_widget_get(devinfo, i);
1331 		if (w == NULL)
1332 			continue;
1333 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
1334 			hdaa_local_patch_pin(w);
1335 	}
1336 
1337 	if (resource_string_value(device_get_name(devinfo->dev),
1338 	    device_get_unit(devinfo->dev), "config", &res) == 0) {
1339 		if (res != NULL && strlen(res) > 0)
1340 			hdaa_config_fetch(res, &quirks_on, &quirks_off);
1341 		devinfo->quirks |= quirks_on;
1342 		devinfo->quirks &= ~quirks_off;
1343 	}
1344 	if (devinfo->newquirks == -1)
1345 		devinfo->newquirks = devinfo->quirks;
1346 	else
1347 		devinfo->quirks = devinfo->newquirks;
1348 	HDA_BOOTHVERBOSE(
1349 		device_printf(devinfo->dev,
1350 		    "Config options: 0x%08x\n", devinfo->quirks);
1351 	);
1352 
1353 	if (resource_string_value(device_get_name(devinfo->dev),
1354 	    device_get_unit(devinfo->dev), "gpio_config", &res) == 0) {
1355 		if (strncmp(res, "0x", 2) == 0) {
1356 			devinfo->gpio = strtol(res + 2, NULL, 16);
1357 		} else {
1358 			devinfo->gpio = hdaa_gpio_patch(devinfo->gpio, res);
1359 		}
1360 	}
1361 	if (devinfo->newgpio == -1)
1362 		devinfo->newgpio = devinfo->gpio;
1363 	else
1364 		devinfo->gpio = devinfo->newgpio;
1365 	if (devinfo->newgpo == -1)
1366 		devinfo->newgpo = devinfo->gpo;
1367 	else
1368 		devinfo->gpo = devinfo->newgpo;
1369 	HDA_BOOTHVERBOSE(
1370 		device_printf(devinfo->dev, "GPIO config options:");
1371 		for (i = 0; i < 7; i++) {
1372 			x = (devinfo->gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
1373 			if (x != 0)
1374 				printf(" %d=%s", i, HDA_GPIO_ACTIONS[x]);
1375 		}
1376 		printf("\n");
1377 	);
1378 }
1379 
1380 static void
1381 hdaa_widget_connection_parse(struct hdaa_widget *w)
1382 {
1383 	uint32_t res;
1384 	int i, j, max, ents, entnum;
1385 	nid_t nid = w->nid;
1386 	nid_t cnid, addcnid, prevcnid;
1387 
1388 	w->nconns = 0;
1389 
1390 	res = hda_command(w->devinfo->dev,
1391 	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_CONN_LIST_LENGTH));
1392 
1393 	ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res);
1394 
1395 	if (ents < 1)
1396 		return;
1397 
1398 	entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4;
1399 	max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1;
1400 	prevcnid = 0;
1401 
1402 #define CONN_RMASK(e)		(1 << ((32 / (e)) - 1))
1403 #define CONN_NMASK(e)		(CONN_RMASK(e) - 1)
1404 #define CONN_RESVAL(r, e, n)	((r) >> ((32 / (e)) * (n)))
1405 #define CONN_RANGE(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_RMASK(e))
1406 #define CONN_CNID(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_NMASK(e))
1407 
1408 	for (i = 0; i < ents; i += entnum) {
1409 		res = hda_command(w->devinfo->dev,
1410 		    HDA_CMD_GET_CONN_LIST_ENTRY(0, nid, i));
1411 		for (j = 0; j < entnum; j++) {
1412 			cnid = CONN_CNID(res, entnum, j);
1413 			if (cnid == 0) {
1414 				if (w->nconns < ents)
1415 					device_printf(w->devinfo->dev,
1416 					    "WARNING: nid=%d has zero cnid "
1417 					    "entnum=%d j=%d index=%d "
1418 					    "entries=%d found=%d res=0x%08x\n",
1419 					    nid, entnum, j, i,
1420 					    ents, w->nconns, res);
1421 				else
1422 					goto getconns_out;
1423 			}
1424 			if (cnid < w->devinfo->startnode ||
1425 			    cnid >= w->devinfo->endnode) {
1426 				HDA_BOOTVERBOSE(
1427 					device_printf(w->devinfo->dev,
1428 					    "WARNING: nid=%d has cnid outside "
1429 					    "of the AFG range j=%d "
1430 					    "entnum=%d index=%d res=0x%08x\n",
1431 					    nid, j, entnum, i, res);
1432 				);
1433 			}
1434 			if (CONN_RANGE(res, entnum, j) == 0)
1435 				addcnid = cnid;
1436 			else if (prevcnid == 0 || prevcnid >= cnid) {
1437 				device_printf(w->devinfo->dev,
1438 				    "WARNING: Invalid child range "
1439 				    "nid=%d index=%d j=%d entnum=%d "
1440 				    "prevcnid=%d cnid=%d res=0x%08x\n",
1441 				    nid, i, j, entnum, prevcnid,
1442 				    cnid, res);
1443 				addcnid = cnid;
1444 			} else
1445 				addcnid = prevcnid + 1;
1446 			while (addcnid <= cnid) {
1447 				if (w->nconns > max) {
1448 					device_printf(w->devinfo->dev,
1449 					    "Adding %d (nid=%d): "
1450 					    "Max connection reached! max=%d\n",
1451 					    addcnid, nid, max + 1);
1452 					goto getconns_out;
1453 				}
1454 				w->connsenable[w->nconns] = 1;
1455 				w->conns[w->nconns++] = addcnid++;
1456 			}
1457 			prevcnid = cnid;
1458 		}
1459 	}
1460 
1461 getconns_out:
1462 	return;
1463 }
1464 
1465 static void
1466 hdaa_widget_parse(struct hdaa_widget *w)
1467 {
1468 	device_t dev = w->devinfo->dev;
1469 	uint32_t wcap, cap;
1470 	nid_t nid = w->nid;
1471 	char buf[64];
1472 
1473 	w->param.widget_cap = wcap = hda_command(dev,
1474 	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_AUDIO_WIDGET_CAP));
1475 	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap);
1476 
1477 	hdaa_widget_connection_parse(w);
1478 
1479 	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) {
1480 		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1481 			w->param.outamp_cap =
1482 			    hda_command(dev,
1483 			    HDA_CMD_GET_PARAMETER(0, nid,
1484 			    HDA_PARAM_OUTPUT_AMP_CAP));
1485 		else
1486 			w->param.outamp_cap =
1487 			    w->devinfo->outamp_cap;
1488 	} else
1489 		w->param.outamp_cap = 0;
1490 
1491 	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) {
1492 		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1493 			w->param.inamp_cap =
1494 			    hda_command(dev,
1495 			    HDA_CMD_GET_PARAMETER(0, nid,
1496 			    HDA_PARAM_INPUT_AMP_CAP));
1497 		else
1498 			w->param.inamp_cap =
1499 			    w->devinfo->inamp_cap;
1500 	} else
1501 		w->param.inamp_cap = 0;
1502 
1503 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1504 	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1505 		if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) {
1506 			cap = hda_command(dev,
1507 			    HDA_CMD_GET_PARAMETER(0, nid,
1508 			    HDA_PARAM_SUPP_STREAM_FORMATS));
1509 			w->param.supp_stream_formats = (cap != 0) ? cap :
1510 			    w->devinfo->supp_stream_formats;
1511 			cap = hda_command(dev,
1512 			    HDA_CMD_GET_PARAMETER(0, nid,
1513 			    HDA_PARAM_SUPP_PCM_SIZE_RATE));
1514 			w->param.supp_pcm_size_rate = (cap != 0) ? cap :
1515 			    w->devinfo->supp_pcm_size_rate;
1516 		} else {
1517 			w->param.supp_stream_formats =
1518 			    w->devinfo->supp_stream_formats;
1519 			w->param.supp_pcm_size_rate =
1520 			    w->devinfo->supp_pcm_size_rate;
1521 		}
1522 		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1523 			w->wclass.conv.stripecap = hda_command(dev,
1524 			    HDA_CMD_GET_STRIPE_CONTROL(0, w->nid)) >> 20;
1525 		} else
1526 			w->wclass.conv.stripecap = 1;
1527 	} else {
1528 		w->param.supp_stream_formats = 0;
1529 		w->param.supp_pcm_size_rate = 0;
1530 	}
1531 
1532 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1533 		w->wclass.pin.original = w->wclass.pin.newconf =
1534 		    w->wclass.pin.config = hda_command(dev,
1535 			HDA_CMD_GET_CONFIGURATION_DEFAULT(0, w->nid));
1536 		w->wclass.pin.cap = hda_command(dev,
1537 		    HDA_CMD_GET_PARAMETER(0, w->nid, HDA_PARAM_PIN_CAP));
1538 		w->wclass.pin.ctrl = hda_command(dev,
1539 		    HDA_CMD_GET_PIN_WIDGET_CTRL(0, nid));
1540 		w->wclass.pin.connected = 2;
1541 		if (HDA_PARAM_PIN_CAP_EAPD_CAP(w->wclass.pin.cap)) {
1542 			w->param.eapdbtl = hda_command(dev,
1543 			    HDA_CMD_GET_EAPD_BTL_ENABLE(0, nid));
1544 			w->param.eapdbtl &= 0x7;
1545 			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
1546 		} else
1547 			w->param.eapdbtl = HDA_INVALID;
1548 	}
1549 	w->unsol = -1;
1550 
1551 	hdaa_unlock(w->devinfo);
1552 	snprintf(buf, sizeof(buf), "nid%d", w->nid);
1553 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1554 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1555 	    buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1556 	    w, 0, hdaa_sysctl_caps, "A", "Node capabilities");
1557 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1558 		snprintf(buf, sizeof(buf), "nid%d_config", w->nid);
1559 		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1560 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1561 		    buf, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1562 		    &w->wclass.pin.newconf, 0, hdaa_sysctl_config, "A",
1563 		    "Current pin configuration");
1564 		snprintf(buf, sizeof(buf), "nid%d_original", w->nid);
1565 		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1566 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1567 		    buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1568 		    &w->wclass.pin.original, 0, hdaa_sysctl_config, "A",
1569 		    "Original pin configuration");
1570 	}
1571 	hdaa_lock(w->devinfo);
1572 }
1573 
1574 static void
1575 hdaa_widget_postprocess(struct hdaa_widget *w)
1576 {
1577 	const char *typestr;
1578 
1579 	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(w->param.widget_cap);
1580 	switch (w->type) {
1581 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
1582 		typestr = "audio output";
1583 		break;
1584 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
1585 		typestr = "audio input";
1586 		break;
1587 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
1588 		typestr = "audio mixer";
1589 		break;
1590 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
1591 		typestr = "audio selector";
1592 		break;
1593 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
1594 		typestr = "pin";
1595 		break;
1596 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET:
1597 		typestr = "power widget";
1598 		break;
1599 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET:
1600 		typestr = "volume widget";
1601 		break;
1602 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
1603 		typestr = "beep widget";
1604 		break;
1605 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET:
1606 		typestr = "vendor widget";
1607 		break;
1608 	default:
1609 		typestr = "unknown type";
1610 		break;
1611 	}
1612 	strlcpy(w->name, typestr, sizeof(w->name));
1613 
1614 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1615 		uint32_t config;
1616 		const char *devstr;
1617 		int conn, color;
1618 
1619 		config = w->wclass.pin.config;
1620 		devstr = HDA_DEVS[(config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) >>
1621 		    HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT];
1622 		conn = (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) >>
1623 		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT;
1624 		color = (config & HDA_CONFIG_DEFAULTCONF_COLOR_MASK) >>
1625 		    HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT;
1626 		strlcat(w->name, ": ", sizeof(w->name));
1627 		strlcat(w->name, devstr, sizeof(w->name));
1628 		strlcat(w->name, " (", sizeof(w->name));
1629 		if (conn == 0 && color != 0 && color != 15) {
1630 			strlcat(w->name, HDA_COLORS[color], sizeof(w->name));
1631 			strlcat(w->name, " ", sizeof(w->name));
1632 		}
1633 		strlcat(w->name, HDA_CONNS[conn], sizeof(w->name));
1634 		strlcat(w->name, ")", sizeof(w->name));
1635 	}
1636 }
1637 
1638 struct hdaa_widget *
1639 hdaa_widget_get(struct hdaa_devinfo *devinfo, nid_t nid)
1640 {
1641 	if (devinfo == NULL || devinfo->widget == NULL ||
1642 		    nid < devinfo->startnode || nid >= devinfo->endnode)
1643 		return (NULL);
1644 	return (&devinfo->widget[nid - devinfo->startnode]);
1645 }
1646 
1647 static void
1648 hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *devinfo, nid_t nid,
1649 					int index, int lmute, int rmute,
1650 					int left, int right, int dir)
1651 {
1652 	uint16_t v = 0;
1653 
1654 	HDA_BOOTHVERBOSE(
1655 		device_printf(devinfo->dev,
1656 		    "Setting amplifier nid=%d index=%d %s mute=%d/%d vol=%d/%d\n",
1657 		    nid,index,dir ? "in" : "out",lmute,rmute,left,right);
1658 	);
1659 	if (left != right || lmute != rmute) {
1660 		v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
1661 		    (lmute << 7) | left;
1662 		hda_command(devinfo->dev,
1663 		    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1664 		v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
1665 		    (rmute << 7) | right;
1666 	} else
1667 		v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
1668 		    (lmute << 7) | left;
1669 
1670 	hda_command(devinfo->dev,
1671 	    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1672 }
1673 
1674 static void
1675 hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *ctl, uint32_t mute,
1676 						int left, int right)
1677 {
1678 	nid_t nid;
1679 	int lmute, rmute;
1680 
1681 	nid = ctl->widget->nid;
1682 
1683 	/* Save new values if valid. */
1684 	if (mute != HDAA_AMP_MUTE_DEFAULT)
1685 		ctl->muted = mute;
1686 	if (left != HDAA_AMP_VOL_DEFAULT)
1687 		ctl->left = left;
1688 	if (right != HDAA_AMP_VOL_DEFAULT)
1689 		ctl->right = right;
1690 	/* Prepare effective values */
1691 	if (ctl->forcemute) {
1692 		lmute = 1;
1693 		rmute = 1;
1694 		left = 0;
1695 		right = 0;
1696 	} else {
1697 		lmute = HDAA_AMP_LEFT_MUTED(ctl->muted);
1698 		rmute = HDAA_AMP_RIGHT_MUTED(ctl->muted);
1699 		left = ctl->left;
1700 		right = ctl->right;
1701 	}
1702 	/* Apply effective values */
1703 	if (ctl->dir & HDAA_CTL_OUT)
1704 		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1705 		    lmute, rmute, left, right, 0);
1706 	if (ctl->dir & HDAA_CTL_IN)
1707 		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1708 		    lmute, rmute, left, right, 1);
1709 }
1710 
1711 static void
1712 hdaa_widget_connection_select(struct hdaa_widget *w, uint8_t index)
1713 {
1714 	if (w == NULL || w->nconns < 1 || index > (w->nconns - 1))
1715 		return;
1716 	HDA_BOOTHVERBOSE(
1717 		device_printf(w->devinfo->dev,
1718 		    "Setting selector nid=%d index=%d\n", w->nid, index);
1719 	);
1720 	hda_command(w->devinfo->dev,
1721 	    HDA_CMD_SET_CONNECTION_SELECT_CONTROL(0, w->nid, index));
1722 	w->selconn = index;
1723 }
1724 
1725 /****************************************************************************
1726  * Device Methods
1727  ****************************************************************************/
1728 
1729 static void *
1730 hdaa_channel_init(kobj_t obj, void *data, struct snd_dbuf *b,
1731 					struct pcm_channel *c, int dir)
1732 {
1733 	struct hdaa_chan *ch = data;
1734 	struct hdaa_pcm_devinfo *pdevinfo = ch->pdevinfo;
1735 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1736 
1737 	hdaa_lock(devinfo);
1738 	if (devinfo->quirks & HDAA_QUIRK_FIXEDRATE) {
1739 		ch->caps.minspeed = ch->caps.maxspeed = 48000;
1740 		ch->pcmrates[0] = 48000;
1741 		ch->pcmrates[1] = 0;
1742 	}
1743 	ch->dir = dir;
1744 	ch->b = b;
1745 	ch->c = c;
1746 	ch->blksz = pdevinfo->chan_size / pdevinfo->chan_blkcnt;
1747 	ch->blkcnt = pdevinfo->chan_blkcnt;
1748 	hdaa_unlock(devinfo);
1749 
1750 	if (sndbuf_alloc(ch->b, bus_get_dma_tag(devinfo->dev),
1751 	    hda_get_dma_nocache(devinfo->dev) ? BUS_DMA_NOCACHE : 0,
1752 	    pdevinfo->chan_size) != 0)
1753 		return (NULL);
1754 
1755 	return (ch);
1756 }
1757 
1758 static int
1759 hdaa_channel_setformat(kobj_t obj, void *data, uint32_t format)
1760 {
1761 	struct hdaa_chan *ch = data;
1762 	int i;
1763 
1764 	for (i = 0; ch->caps.fmtlist[i] != 0; i++) {
1765 		if (format == ch->caps.fmtlist[i]) {
1766 			ch->fmt = format;
1767 			return (0);
1768 		}
1769 	}
1770 
1771 	return (EINVAL);
1772 }
1773 
1774 static uint32_t
1775 hdaa_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
1776 {
1777 	struct hdaa_chan *ch = data;
1778 	uint32_t spd = 0, threshold;
1779 	int i;
1780 
1781 	/* First look for equal or multiple frequency. */
1782 	for (i = 0; ch->pcmrates[i] != 0; i++) {
1783 		spd = ch->pcmrates[i];
1784 		if (speed != 0 && spd / speed * speed == spd) {
1785 			ch->spd = spd;
1786 			return (spd);
1787 		}
1788 	}
1789 	/* If no match, just find nearest. */
1790 	for (i = 0; ch->pcmrates[i] != 0; i++) {
1791 		spd = ch->pcmrates[i];
1792 		threshold = spd + ((ch->pcmrates[i + 1] != 0) ?
1793 		    ((ch->pcmrates[i + 1] - spd) >> 1) : 0);
1794 		if (speed < threshold)
1795 			break;
1796 	}
1797 	ch->spd = spd;
1798 	return (spd);
1799 }
1800 
1801 static uint16_t
1802 hdaa_stream_format(struct hdaa_chan *ch)
1803 {
1804 	int i;
1805 	uint16_t fmt;
1806 
1807 	fmt = 0;
1808 	if (ch->fmt & AFMT_S16_LE)
1809 		fmt |= ch->bit16 << 4;
1810 	else if (ch->fmt & AFMT_S32_LE)
1811 		fmt |= ch->bit32 << 4;
1812 	else
1813 		fmt |= 1 << 4;
1814 	for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
1815 		if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
1816 			fmt |= hda_rate_tab[i].base;
1817 			fmt |= hda_rate_tab[i].mul;
1818 			fmt |= hda_rate_tab[i].div;
1819 			break;
1820 		}
1821 	}
1822 	fmt |= (AFMT_CHANNEL(ch->fmt) - 1);
1823 
1824 	return (fmt);
1825 }
1826 
1827 static int
1828 hdaa_allowed_stripes(uint16_t fmt)
1829 {
1830 	static const int bits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1831 	int size;
1832 
1833 	size = bits[(fmt >> 4) & 0x03];
1834 	size *= (fmt & 0x0f) + 1;
1835 	size *= ((fmt >> 11) & 0x07) + 1;
1836 	return (0xffffffffU >> (32 - fls(size / 8)));
1837 }
1838 
1839 static void
1840 hdaa_audio_setup(struct hdaa_chan *ch)
1841 {
1842 	struct hdaa_audio_as *as = &ch->devinfo->as[ch->as];
1843 	struct hdaa_widget *w, *wp;
1844 	int i, j, k, chn, cchn, totalchn, totalextchn, c;
1845 	uint16_t fmt, dfmt;
1846 	/* Mapping channel pairs to codec pins/converters. */
1847 	const static uint16_t convmap[2][5] =
1848 	    /*  1.0     2.0     4.0     5.1     7.1  */
1849 	    {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x4231 },	/* no dup. */
1850 	     { 0x0010, 0x0001, 0x2201, 0x2231, 0x4231 }};	/* side dup. */
1851 	/* Mapping formats to HDMI channel allocations. */
1852 	const static uint8_t hdmica[2][8] =
1853 	    /*  1     2     3     4     5     6     7     8  */
1854 	    {{ 0x02, 0x00, 0x04, 0x08, 0x0a, 0x0e, 0x12, 0x12 }, /* x.0 */
1855 	     { 0x01, 0x03, 0x01, 0x03, 0x09, 0x0b, 0x0f, 0x13 }}; /* x.1 */
1856 	/* Mapping formats to HDMI channels order. */
1857 	const static uint32_t hdmich[2][8] =
1858 	    /*  1  /  5     2  /  6     3  /  7     4  /  8  */
1859 	    {{ 0xFFFF0F00, 0xFFFFFF10, 0xFFF2FF10, 0xFF32FF10,
1860 	       0xFF324F10, 0xF5324F10, 0x54326F10, 0x54326F10 }, /* x.0 */
1861 	     { 0xFFFFF000, 0xFFFF0100, 0xFFFFF210, 0xFFFF2310,
1862 	       0xFF32F410, 0xFF324510, 0xF6324510, 0x76325410 }}; /* x.1 */
1863 	int convmapid = -1;
1864 	nid_t nid;
1865 	uint8_t csum;
1866 
1867 	totalchn = AFMT_CHANNEL(ch->fmt);
1868 	totalextchn = AFMT_EXTCHANNEL(ch->fmt);
1869 	HDA_BOOTHVERBOSE(
1870 		device_printf(ch->pdevinfo->dev,
1871 		    "PCMDIR_%s: Stream setup fmt=%08x (%d.%d) speed=%d\n",
1872 		    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1873 		    ch->fmt, totalchn - totalextchn, totalextchn, ch->spd);
1874 	);
1875 	fmt = hdaa_stream_format(ch);
1876 
1877 	/* Set channels to I/O converters mapping for known speaker setups. */
1878 	if ((as->pinset == 0x0007 || as->pinset == 0x0013) || /* Standard 5.1 */
1879 	    (as->pinset == 0x0017)) /* Standard 7.1 */
1880 		convmapid = (ch->dir == PCMDIR_PLAY);
1881 
1882 	dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN;
1883 	if (ch->fmt & AFMT_AC3)
1884 		dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO;
1885 
1886 	chn = 0;
1887 	for (i = 0; ch->io[i] != -1; i++) {
1888 		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1889 		if (w == NULL)
1890 			continue;
1891 
1892 		/* If HP redirection is enabled, but failed to use same
1893 		   DAC, make last DAC to duplicate first one. */
1894 		if (as->fakeredir && i == (as->pincnt - 1)) {
1895 			c = (ch->sid << 4);
1896 		} else {
1897 			/* Map channels to I/O converters, if set. */
1898 			if (convmapid >= 0)
1899 				chn = (((convmap[convmapid][totalchn / 2]
1900 				    >> i * 4) & 0xf) - 1) * 2;
1901 			if (chn < 0 || chn >= totalchn) {
1902 				c = 0;
1903 			} else {
1904 				c = (ch->sid << 4) | chn;
1905 			}
1906 		}
1907 		hda_command(ch->devinfo->dev,
1908 		    HDA_CMD_SET_CONV_FMT(0, ch->io[i], fmt));
1909 		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1910 			hda_command(ch->devinfo->dev,
1911 			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], dfmt));
1912 		}
1913 		hda_command(ch->devinfo->dev,
1914 		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i], c));
1915 		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1916 			hda_command(ch->devinfo->dev,
1917 			    HDA_CMD_SET_STRIPE_CONTROL(0, w->nid, ch->stripectl));
1918 		}
1919 		cchn = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1920 		if (cchn > 1 && chn < totalchn) {
1921 			cchn = min(cchn, totalchn - chn - 1);
1922 			hda_command(ch->devinfo->dev,
1923 			    HDA_CMD_SET_CONV_CHAN_COUNT(0, ch->io[i], cchn));
1924 		}
1925 		HDA_BOOTHVERBOSE(
1926 			device_printf(ch->pdevinfo->dev,
1927 			    "PCMDIR_%s: Stream setup nid=%d: "
1928 			    "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x, "
1929 			    "chan_count=0x%02x, stripe=%d\n",
1930 			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1931 			    ch->io[i], fmt, dfmt, c, cchn, ch->stripectl);
1932 		);
1933 		for (j = 0; j < 16; j++) {
1934 			if (as->dacs[ch->asindex][j] != ch->io[i])
1935 				continue;
1936 			nid = as->pins[j];
1937 			wp = hdaa_widget_get(ch->devinfo, nid);
1938 			if (wp == NULL)
1939 				continue;
1940 			if (!HDA_PARAM_PIN_CAP_DP(wp->wclass.pin.cap) &&
1941 			    !HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap))
1942 				continue;
1943 
1944 			/* Set channel mapping. */
1945 			for (k = 0; k < 8; k++) {
1946 				hda_command(ch->devinfo->dev,
1947 				    HDA_CMD_SET_HDMI_CHAN_SLOT(0, nid,
1948 				    (((hdmich[totalextchn == 0 ? 0 : 1][totalchn - 1]
1949 				     >> (k * 4)) & 0xf) << 4) | k));
1950 			}
1951 
1952 			/*
1953 			 * Enable High Bit Rate (HBR) Encoded Packet Type
1954 			 * (EPT), if supported and needed (8ch data).
1955 			 */
1956 			if (HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap) &&
1957 			    HDA_PARAM_PIN_CAP_HBR(wp->wclass.pin.cap)) {
1958 				wp->wclass.pin.ctrl &=
1959 				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK;
1960 				if ((ch->fmt & AFMT_AC3) && (cchn == 7))
1961 					wp->wclass.pin.ctrl |= 0x03;
1962 				hda_command(ch->devinfo->dev,
1963 				    HDA_CMD_SET_PIN_WIDGET_CTRL(0, nid,
1964 				    wp->wclass.pin.ctrl));
1965 			}
1966 
1967 			/* Stop audio infoframe transmission. */
1968 			hda_command(ch->devinfo->dev,
1969 			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1970 			hda_command(ch->devinfo->dev,
1971 			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0x00));
1972 
1973 			/* Clear audio infoframe buffer. */
1974 			hda_command(ch->devinfo->dev,
1975 			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1976 			for (k = 0; k < 32; k++)
1977 				hda_command(ch->devinfo->dev,
1978 				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1979 
1980 			/* Write HDMI/DisplayPort audio infoframe. */
1981 			hda_command(ch->devinfo->dev,
1982 			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1983 			if (w->eld != NULL && w->eld_len >= 6 &&
1984 			    ((w->eld[5] >> 2) & 0x3) == 1) { /* DisplayPort */
1985 				hda_command(ch->devinfo->dev,
1986 				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1987 				hda_command(ch->devinfo->dev,
1988 				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x1b));
1989 				hda_command(ch->devinfo->dev,
1990 				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x44));
1991 			} else {	/* HDMI */
1992 				hda_command(ch->devinfo->dev,
1993 				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1994 				hda_command(ch->devinfo->dev,
1995 				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x01));
1996 				hda_command(ch->devinfo->dev,
1997 				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x0a));
1998 				csum = 0;
1999 				csum -= 0x84 + 0x01 + 0x0a + (totalchn - 1) +
2000 				    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1];
2001 				hda_command(ch->devinfo->dev,
2002 				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, csum));
2003 			}
2004 			hda_command(ch->devinfo->dev,
2005 			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, totalchn - 1));
2006 			hda_command(ch->devinfo->dev,
2007 			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
2008 			hda_command(ch->devinfo->dev,
2009 			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
2010 			hda_command(ch->devinfo->dev,
2011 			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid,
2012 			    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1]));
2013 
2014 			/* Start audio infoframe transmission. */
2015 			hda_command(ch->devinfo->dev,
2016 			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
2017 			hda_command(ch->devinfo->dev,
2018 			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0xc0));
2019 		}
2020 		chn += cchn + 1;
2021 	}
2022 }
2023 
2024 /*
2025  * Greatest Common Divisor.
2026  */
2027 static unsigned
2028 gcd(unsigned a, unsigned b)
2029 {
2030 	u_int c;
2031 
2032 	while (b != 0) {
2033 		c = a;
2034 		a = b;
2035 		b = (c % b);
2036 	}
2037 	return (a);
2038 }
2039 
2040 /*
2041  * Least Common Multiple.
2042  */
2043 static unsigned
2044 lcm(unsigned a, unsigned b)
2045 {
2046 
2047 	return ((a * b) / gcd(a, b));
2048 }
2049 
2050 static int
2051 hdaa_channel_setfragments(kobj_t obj, void *data,
2052 					uint32_t blksz, uint32_t blkcnt)
2053 {
2054 	struct hdaa_chan *ch = data;
2055 
2056 	blksz -= blksz % lcm(HDA_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
2057 
2058 	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
2059 		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
2060 	if (blksz < HDA_BLK_MIN)
2061 		blksz = HDA_BLK_MIN;
2062 	if (blkcnt > HDA_BDL_MAX)
2063 		blkcnt = HDA_BDL_MAX;
2064 	if (blkcnt < HDA_BDL_MIN)
2065 		blkcnt = HDA_BDL_MIN;
2066 
2067 	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
2068 		if ((blkcnt >> 1) >= HDA_BDL_MIN)
2069 			blkcnt >>= 1;
2070 		else if ((blksz >> 1) >= HDA_BLK_MIN)
2071 			blksz >>= 1;
2072 		else
2073 			break;
2074 	}
2075 
2076 	if ((sndbuf_getblksz(ch->b) != blksz ||
2077 	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
2078 	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
2079 		device_printf(ch->devinfo->dev, "%s: failed blksz=%u blkcnt=%u\n",
2080 		    __func__, blksz, blkcnt);
2081 
2082 	ch->blksz = sndbuf_getblksz(ch->b);
2083 	ch->blkcnt = sndbuf_getblkcnt(ch->b);
2084 
2085 	return (0);
2086 }
2087 
2088 static uint32_t
2089 hdaa_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
2090 {
2091 	struct hdaa_chan *ch = data;
2092 
2093 	hdaa_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
2094 
2095 	return (ch->blksz);
2096 }
2097 
2098 static void
2099 hdaa_channel_stop(struct hdaa_chan *ch)
2100 {
2101 	struct hdaa_devinfo *devinfo = ch->devinfo;
2102 	struct hdaa_widget *w;
2103 	int i;
2104 
2105 	if ((ch->flags & HDAA_CHN_RUNNING) == 0)
2106 		return;
2107 	ch->flags &= ~HDAA_CHN_RUNNING;
2108 	HDAC_STREAM_STOP(device_get_parent(devinfo->dev), devinfo->dev,
2109 	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2110 	for (i = 0; ch->io[i] != -1; i++) {
2111 		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
2112 		if (w == NULL)
2113 			continue;
2114 		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
2115 			hda_command(devinfo->dev,
2116 			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], 0));
2117 		}
2118 		hda_command(devinfo->dev,
2119 		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i],
2120 		    0));
2121 	}
2122 	HDAC_STREAM_FREE(device_get_parent(devinfo->dev), devinfo->dev,
2123 	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2124 }
2125 
2126 static int
2127 hdaa_channel_start(struct hdaa_chan *ch)
2128 {
2129 	struct hdaa_devinfo *devinfo = ch->devinfo;
2130 	uint32_t fmt;
2131 
2132 	fmt = hdaa_stream_format(ch);
2133 	ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt) &
2134 	    hda_get_stripes_mask(devinfo->dev)) - 1;
2135 	ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev,
2136 	    ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos);
2137 	if (ch->sid <= 0)
2138 		return (EBUSY);
2139 	hdaa_audio_setup(ch);
2140 	HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev,
2141 	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2142 	HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev,
2143 	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid,
2144 	    sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt);
2145 	ch->flags |= HDAA_CHN_RUNNING;
2146 	return (0);
2147 }
2148 
2149 static int
2150 hdaa_channel_trigger(kobj_t obj, void *data, int go)
2151 {
2152 	struct hdaa_chan *ch = data;
2153 	int error = 0;
2154 
2155 	if (!PCMTRIG_COMMON(go))
2156 		return (0);
2157 
2158 	hdaa_lock(ch->devinfo);
2159 	switch (go) {
2160 	case PCMTRIG_START:
2161 		error = hdaa_channel_start(ch);
2162 		break;
2163 	case PCMTRIG_STOP:
2164 	case PCMTRIG_ABORT:
2165 		hdaa_channel_stop(ch);
2166 		break;
2167 	default:
2168 		break;
2169 	}
2170 	hdaa_unlock(ch->devinfo);
2171 
2172 	return (error);
2173 }
2174 
2175 static uint32_t
2176 hdaa_channel_getptr(kobj_t obj, void *data)
2177 {
2178 	struct hdaa_chan *ch = data;
2179 	struct hdaa_devinfo *devinfo = ch->devinfo;
2180 	uint32_t ptr;
2181 
2182 	hdaa_lock(devinfo);
2183 	if (ch->dmapos != NULL) {
2184 		ptr = *(ch->dmapos);
2185 	} else {
2186 		ptr = HDAC_STREAM_GETPTR(
2187 		    device_get_parent(devinfo->dev), devinfo->dev,
2188 		    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2189 	}
2190 	hdaa_unlock(devinfo);
2191 
2192 	/*
2193 	 * Round to available space and force 128 bytes aligment.
2194 	 */
2195 	ptr %= ch->blksz * ch->blkcnt;
2196 	ptr &= HDA_BLK_ALIGN;
2197 
2198 	return (ptr);
2199 }
2200 
2201 static struct pcmchan_caps *
2202 hdaa_channel_getcaps(kobj_t obj, void *data)
2203 {
2204 	return (&((struct hdaa_chan *)data)->caps);
2205 }
2206 
2207 static kobj_method_t hdaa_channel_methods[] = {
2208 	KOBJMETHOD(channel_init,		hdaa_channel_init),
2209 	KOBJMETHOD(channel_setformat,		hdaa_channel_setformat),
2210 	KOBJMETHOD(channel_setspeed,		hdaa_channel_setspeed),
2211 	KOBJMETHOD(channel_setblocksize,	hdaa_channel_setblocksize),
2212 	KOBJMETHOD(channel_setfragments,	hdaa_channel_setfragments),
2213 	KOBJMETHOD(channel_trigger,		hdaa_channel_trigger),
2214 	KOBJMETHOD(channel_getptr,		hdaa_channel_getptr),
2215 	KOBJMETHOD(channel_getcaps,		hdaa_channel_getcaps),
2216 	KOBJMETHOD_END
2217 };
2218 CHANNEL_DECLARE(hdaa_channel);
2219 
2220 static int
2221 hdaa_audio_ctl_ossmixer_init(struct snd_mixer *m)
2222 {
2223 	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2224 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2225 	struct hdaa_widget *w, *cw;
2226 	uint32_t mask, recmask;
2227 	int i, j;
2228 
2229 	hdaa_lock(devinfo);
2230 	pdevinfo->mixer = m;
2231 
2232 	/* Make sure that in case of soft volume it won't stay muted. */
2233 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2234 		pdevinfo->left[i] = 100;
2235 		pdevinfo->right[i] = 100;
2236 	}
2237 
2238 	/* Declare volume controls assigned to this association. */
2239 	mask = pdevinfo->ossmask;
2240 	if (pdevinfo->playas >= 0) {
2241 		/* Declate EAPD as ogain control. */
2242 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2243 			w = hdaa_widget_get(devinfo, i);
2244 			if (w == NULL || w->enable == 0)
2245 				continue;
2246 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2247 			    w->param.eapdbtl == HDA_INVALID ||
2248 			    w->bindas != pdevinfo->playas)
2249 				continue;
2250 			mask |= SOUND_MASK_OGAIN;
2251 			break;
2252 		}
2253 
2254 		/* Declare soft PCM volume if needed. */
2255 		if ((mask & SOUND_MASK_PCM) == 0 ||
2256 		    (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) ||
2257 		    pdevinfo->minamp[SOUND_MIXER_PCM] ==
2258 		     pdevinfo->maxamp[SOUND_MIXER_PCM]) {
2259 			mask |= SOUND_MASK_PCM;
2260 			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
2261 			HDA_BOOTHVERBOSE(
2262 				device_printf(pdevinfo->dev,
2263 				    "Forcing Soft PCM volume\n");
2264 			);
2265 		}
2266 
2267 		/* Declare master volume if needed. */
2268 		if ((mask & SOUND_MASK_VOLUME) == 0) {
2269 			mask |= SOUND_MASK_VOLUME;
2270 			mix_setparentchild(m, SOUND_MIXER_VOLUME,
2271 			    SOUND_MASK_PCM);
2272 			mix_setrealdev(m, SOUND_MIXER_VOLUME,
2273 			    SOUND_MIXER_NONE);
2274 			HDA_BOOTHVERBOSE(
2275 				device_printf(pdevinfo->dev,
2276 				    "Forcing master volume with PCM\n");
2277 			);
2278 		}
2279 	}
2280 
2281 	/* Declare record sources available to this association. */
2282 	recmask = 0;
2283 	if (pdevinfo->recas >= 0) {
2284 		for (i = 0; i < 16; i++) {
2285 			if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0)
2286 				continue;
2287 			w = hdaa_widget_get(devinfo,
2288 			    devinfo->as[pdevinfo->recas].dacs[0][i]);
2289 			if (w == NULL || w->enable == 0)
2290 				continue;
2291 			for (j = 0; j < w->nconns; j++) {
2292 				if (w->connsenable[j] == 0)
2293 					continue;
2294 				cw = hdaa_widget_get(devinfo, w->conns[j]);
2295 				if (cw == NULL || cw->enable == 0)
2296 					continue;
2297 				if (cw->bindas != pdevinfo->recas &&
2298 				    cw->bindas != -2)
2299 					continue;
2300 				recmask |= cw->ossmask;
2301 			}
2302 		}
2303 	}
2304 
2305 	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2306 	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2307 	pdevinfo->ossmask = mask;
2308 
2309 	mix_setrecdevs(m, recmask);
2310 	mix_setdevs(m, mask);
2311 
2312 	hdaa_unlock(devinfo);
2313 
2314 	return (0);
2315 }
2316 
2317 /*
2318  * Update amplification per pdevinfo per ossdev, calculate summary coefficient
2319  * and write it to codec, update *left and *right to reflect remaining error.
2320  */
2321 static void
2322 hdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev,
2323     int mute, int *left, int *right)
2324 {
2325 	int i, zleft, zright, sleft, sright, smute, lval, rval;
2326 
2327 	ctl->devleft[ossdev] = *left;
2328 	ctl->devright[ossdev] = *right;
2329 	ctl->devmute[ossdev] = mute;
2330 	smute = sleft = sright = zleft = zright = 0;
2331 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2332 		sleft += ctl->devleft[i];
2333 		sright += ctl->devright[i];
2334 		smute |= ctl->devmute[i];
2335 		if (i == ossdev)
2336 			continue;
2337 		zleft += ctl->devleft[i];
2338 		zright += ctl->devright[i];
2339 	}
2340 	lval = QDB2VAL(ctl, sleft);
2341 	rval = QDB2VAL(ctl, sright);
2342 	hdaa_audio_ctl_amp_set(ctl, smute, lval, rval);
2343 	*left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft));
2344 	*right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright));
2345 }
2346 
2347 /*
2348  * Trace signal from source, setting volumes on the way.
2349  */
2350 static void
2351 hdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo,
2352     int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2353 {
2354 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2355 	struct hdaa_widget *w, *wc;
2356 	struct hdaa_audio_ctl *ctl;
2357 	int i, j, conns = 0;
2358 
2359 	if (depth > HDA_PARSE_MAXDEPTH)
2360 		return;
2361 
2362 	w = hdaa_widget_get(devinfo, nid);
2363 	if (w == NULL || w->enable == 0)
2364 		return;
2365 
2366 	/* Count number of active inputs. */
2367 	if (depth > 0) {
2368 		for (j = 0; j < w->nconns; j++) {
2369 			if (!w->connsenable[j])
2370 				continue;
2371 			conns++;
2372 		}
2373 	}
2374 
2375 	/* If this is not a first step - use input mixer.
2376 	   Pins have common input ctl so care must be taken. */
2377 	if (depth > 0 && (conns == 1 ||
2378 	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
2379 		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
2380 		    index, 1);
2381 		if (ctl)
2382 			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2383 	}
2384 
2385 	/* If widget has own ossdev - not traverse it.
2386 	   It will be traversed on its own. */
2387 	if (w->ossdev >= 0 && depth > 0)
2388 		return;
2389 
2390 	/* We must not traverse pin */
2391 	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
2392 	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
2393 	    depth > 0)
2394 		return;
2395 
2396 	/*
2397 	 * If signals mixed, we can't assign controls farther.
2398 	 * Ignore this on depth zero. Caller must knows why.
2399 	 */
2400 	if (conns > 1 &&
2401 	    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
2402 	     w->selconn != index))
2403 		return;
2404 
2405 	ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
2406 	if (ctl)
2407 		hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2408 
2409 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2410 		wc = hdaa_widget_get(devinfo, i);
2411 		if (wc == NULL || wc->enable == 0)
2412 			continue;
2413 		for (j = 0; j < wc->nconns; j++) {
2414 			if (wc->connsenable[j] && wc->conns[j] == nid) {
2415 				hdaa_audio_ctl_source_volume(pdevinfo, ossdev,
2416 				    wc->nid, j, mute, left, right, depth + 1);
2417 			}
2418 		}
2419 	}
2420 	return;
2421 }
2422 
2423 /*
2424  * Trace signal from destination, setting volumes on the way.
2425  */
2426 static void
2427 hdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo,
2428     int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2429 {
2430 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2431 	struct hdaa_audio_as *as = devinfo->as;
2432 	struct hdaa_widget *w, *wc;
2433 	struct hdaa_audio_ctl *ctl;
2434 	int i, j, consumers, cleft, cright;
2435 
2436 	if (depth > HDA_PARSE_MAXDEPTH)
2437 		return;
2438 
2439 	w = hdaa_widget_get(devinfo, nid);
2440 	if (w == NULL || w->enable == 0)
2441 		return;
2442 
2443 	if (depth > 0) {
2444 		/* If this node produce output for several consumers,
2445 		   we can't touch it. */
2446 		consumers = 0;
2447 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2448 			wc = hdaa_widget_get(devinfo, i);
2449 			if (wc == NULL || wc->enable == 0)
2450 				continue;
2451 			for (j = 0; j < wc->nconns; j++) {
2452 				if (wc->connsenable[j] && wc->conns[j] == nid)
2453 					consumers++;
2454 			}
2455 		}
2456 		/* The only exception is if real HP redirection is configured
2457 		   and this is a duplication point.
2458 		   XXX: Actually exception is not completely correct.
2459 		   XXX: Duplication point check is not perfect. */
2460 		if ((consumers == 2 && (w->bindas < 0 ||
2461 		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
2462 		    (w->bindseqmask & (1 << 15)) == 0)) ||
2463 		    consumers > 2)
2464 			return;
2465 
2466 		/* Else use it's output mixer. */
2467 		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2468 		    HDAA_CTL_OUT, -1, 1);
2469 		if (ctl)
2470 			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2471 	}
2472 
2473 	/* We must not traverse pin */
2474 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2475 	    depth > 0)
2476 		return;
2477 
2478 	for (i = 0; i < w->nconns; i++) {
2479 		if (w->connsenable[i] == 0)
2480 			continue;
2481 		if (index >= 0 && i != index)
2482 			continue;
2483 		cleft = left;
2484 		cright = right;
2485 		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2486 		    HDAA_CTL_IN, i, 1);
2487 		if (ctl)
2488 			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright);
2489 		hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1,
2490 		    mute, cleft, cright, depth + 1);
2491 	}
2492 }
2493 
2494 /*
2495  * Set volumes for the specified pdevinfo and ossdev.
2496  */
2497 static void
2498 hdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev)
2499 {
2500 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2501 	struct hdaa_widget *w, *cw;
2502 	uint32_t mute;
2503 	int lvol, rvol;
2504 	int i, j;
2505 
2506 	mute = 0;
2507 	if (pdevinfo->left[dev] == 0) {
2508 		mute |= HDAA_AMP_MUTE_LEFT;
2509 		lvol = -4000;
2510 	} else
2511 		lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2512 		    pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev];
2513 	if (pdevinfo->right[dev] == 0) {
2514 		mute |= HDAA_AMP_MUTE_RIGHT;
2515 		rvol = -4000;
2516 	} else
2517 		rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2518 		    pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev];
2519 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2520 		w = hdaa_widget_get(devinfo, i);
2521 		if (w == NULL || w->enable == 0)
2522 			continue;
2523 		if (w->bindas < 0) {
2524 			if (pdevinfo->index != 0)
2525 				continue;
2526 		} else {
2527 			if (w->bindas != pdevinfo->playas &&
2528 			    w->bindas != pdevinfo->recas)
2529 				continue;
2530 		}
2531 		if (dev == SOUND_MIXER_RECLEV &&
2532 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2533 			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2534 			    w->nid, -1, mute, lvol, rvol, 0);
2535 			continue;
2536 		}
2537 		if (dev == SOUND_MIXER_VOLUME &&
2538 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2539 		    devinfo->as[w->bindas].dir == HDAA_CTL_OUT) {
2540 			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2541 			    w->nid, -1, mute, lvol, rvol, 0);
2542 			continue;
2543 		}
2544 		if (dev == SOUND_MIXER_IGAIN &&
2545 		    w->pflags & HDAA_ADC_MONITOR) {
2546 			for (j = 0; j < w->nconns; j++) {
2547 				if (!w->connsenable[j])
2548 				    continue;
2549 				cw = hdaa_widget_get(devinfo, w->conns[j]);
2550 				if (cw == NULL || cw->enable == 0)
2551 				    continue;
2552 				if (cw->bindas == -1)
2553 				    continue;
2554 				if (cw->bindas >= 0 &&
2555 				    devinfo->as[cw->bindas].dir != HDAA_CTL_IN)
2556 					continue;
2557 				hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2558 				    w->nid, j, mute, lvol, rvol, 0);
2559 			}
2560 			continue;
2561 		}
2562 		if (w->ossdev != dev)
2563 			continue;
2564 		hdaa_audio_ctl_source_volume(pdevinfo, dev,
2565 		    w->nid, -1, mute, lvol, rvol, 0);
2566 		if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST))
2567 			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2568 			    w->nid, -1, mute, lvol, rvol, 0);
2569 	}
2570 }
2571 
2572 /*
2573  * OSS Mixer set method.
2574  */
2575 static int
2576 hdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
2577 					unsigned left, unsigned right)
2578 {
2579 	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2580 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2581 	struct hdaa_widget *w;
2582 	int i;
2583 
2584 	hdaa_lock(devinfo);
2585 
2586 	/* Save new values. */
2587 	pdevinfo->left[dev] = left;
2588 	pdevinfo->right[dev] = right;
2589 
2590 	/* 'ogain' is the special case implemented with EAPD. */
2591 	if (dev == SOUND_MIXER_OGAIN) {
2592 		uint32_t orig;
2593 		w = NULL;
2594 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2595 			w = hdaa_widget_get(devinfo, i);
2596 			if (w == NULL || w->enable == 0)
2597 				continue;
2598 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2599 			    w->param.eapdbtl == HDA_INVALID)
2600 				continue;
2601 			break;
2602 		}
2603 		if (i >= devinfo->endnode) {
2604 			hdaa_unlock(devinfo);
2605 			return (-1);
2606 		}
2607 		orig = w->param.eapdbtl;
2608 		if (left == 0)
2609 			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2610 		else
2611 			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2612 		if (orig != w->param.eapdbtl) {
2613 			uint32_t val;
2614 
2615 			val = w->param.eapdbtl;
2616 			if (devinfo->quirks & HDAA_QUIRK_EAPDINV)
2617 				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2618 			hda_command(devinfo->dev,
2619 			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val));
2620 		}
2621 		hdaa_unlock(devinfo);
2622 		return (left | (left << 8));
2623 	}
2624 
2625 	/* Recalculate all controls related to this OSS device. */
2626 	hdaa_audio_ctl_dev_volume(pdevinfo, dev);
2627 
2628 	hdaa_unlock(devinfo);
2629 	return (left | (right << 8));
2630 }
2631 
2632 /*
2633  * Set mixer settings to our own default values:
2634  * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others.
2635  */
2636 static void
2637 hdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo)
2638 {
2639 	int amp, vol, dev;
2640 
2641 	for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) {
2642 		if ((pdevinfo->ossmask & (1 << dev)) == 0)
2643 			continue;
2644 
2645 		/* If the value was overriden, leave it as is. */
2646 		if (resource_int_value(device_get_name(pdevinfo->dev),
2647 		    device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0)
2648 			continue;
2649 
2650 		vol = -1;
2651 		if (dev == SOUND_MIXER_OGAIN)
2652 			vol = 100;
2653 		else if (dev == SOUND_MIXER_IGAIN)
2654 			vol = 0;
2655 		else if (dev == SOUND_MIXER_MIC ||
2656 		    dev == SOUND_MIXER_MONITOR)
2657 			amp = 20 * 4;	/* +20dB */
2658 		else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital)
2659 			amp = -10 * 4;	/* -10dB */
2660 		else
2661 			amp = 0;
2662 		if (vol < 0 &&
2663 		    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) {
2664 			vol = 100;
2665 		} else if (vol < 0) {
2666 			vol = ((amp - pdevinfo->minamp[dev]) * 100 +
2667 			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) /
2668 			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]);
2669 			vol = imin(imax(vol, 1), 100);
2670 		}
2671 		mix_set(pdevinfo->mixer, dev, vol, vol);
2672 	}
2673 }
2674 
2675 /*
2676  * Recursively commutate specified record source.
2677  */
2678 static uint32_t
2679 hdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
2680 {
2681 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2682 	struct hdaa_widget *w, *cw;
2683 	struct hdaa_audio_ctl *ctl;
2684 	char buf[64];
2685 	int i, muted;
2686 	uint32_t res = 0;
2687 
2688 	if (depth > HDA_PARSE_MAXDEPTH)
2689 		return (0);
2690 
2691 	w = hdaa_widget_get(devinfo, nid);
2692 	if (w == NULL || w->enable == 0)
2693 		return (0);
2694 
2695 	for (i = 0; i < w->nconns; i++) {
2696 		if (w->connsenable[i] == 0)
2697 			continue;
2698 		cw = hdaa_widget_get(devinfo, w->conns[i]);
2699 		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
2700 			continue;
2701 		/* Call recursively to trace signal to it's source if needed. */
2702 		if ((src & cw->ossmask) != 0) {
2703 			if (cw->ossdev < 0) {
2704 				res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2705 				    w->conns[i], depth + 1);
2706 			} else {
2707 				res |= cw->ossmask;
2708 			}
2709 		}
2710 		/* We have two special cases: mixers and others (selectors). */
2711 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
2712 			ctl = hdaa_audio_ctl_amp_get(devinfo,
2713 			    w->nid, HDAA_CTL_IN, i, 1);
2714 			if (ctl == NULL)
2715 				continue;
2716 			/* If we have input control on this node mute them
2717 			 * according to requested sources. */
2718 			muted = (src & cw->ossmask) ? 0 : 1;
2719 			if (muted != ctl->forcemute) {
2720 				ctl->forcemute = muted;
2721 				hdaa_audio_ctl_amp_set(ctl,
2722 				    HDAA_AMP_MUTE_DEFAULT,
2723 				    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
2724 			}
2725 			HDA_BOOTHVERBOSE(
2726 				device_printf(pdevinfo->dev,
2727 				    "Recsel (%s): nid %d source %d %s\n",
2728 				    hdaa_audio_ctl_ossmixer_mask2allname(
2729 				    src, buf, sizeof(buf)),
2730 				    nid, i, muted?"mute":"unmute");
2731 			);
2732 		} else {
2733 			if (w->nconns == 1)
2734 				break;
2735 			if ((src & cw->ossmask) == 0)
2736 				continue;
2737 			/* If we found requested source - select it and exit. */
2738 			hdaa_widget_connection_select(w, i);
2739 			HDA_BOOTHVERBOSE(
2740 				device_printf(pdevinfo->dev,
2741 				    "Recsel (%s): nid %d source %d select\n",
2742 				    hdaa_audio_ctl_ossmixer_mask2allname(
2743 				    src, buf, sizeof(buf)),
2744 				    nid, i);
2745 			);
2746 			break;
2747 		}
2748 	}
2749 	return (res);
2750 }
2751 
2752 static uint32_t
2753 hdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
2754 {
2755 	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2756 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2757 	struct hdaa_widget *w;
2758 	struct hdaa_audio_as *as;
2759 	struct hdaa_audio_ctl *ctl;
2760 	struct hdaa_chan *ch;
2761 	int i, j;
2762 	uint32_t ret = 0xffffffff;
2763 
2764 	hdaa_lock(devinfo);
2765 	if (pdevinfo->recas < 0) {
2766 		hdaa_unlock(devinfo);
2767 		return (0);
2768 	}
2769 	as = &devinfo->as[pdevinfo->recas];
2770 
2771 	/* For non-mixed associations we always recording everything. */
2772 	if (!as->mixed) {
2773 		hdaa_unlock(devinfo);
2774 		return (mix_getrecdevs(m));
2775 	}
2776 
2777 	/* Commutate requested recsrc for each ADC. */
2778 	for (j = 0; j < as->num_chans; j++) {
2779 		ch = &devinfo->chans[as->chans[j]];
2780 		for (i = 0; ch->io[i] >= 0; i++) {
2781 			w = hdaa_widget_get(devinfo, ch->io[i]);
2782 			if (w == NULL || w->enable == 0)
2783 				continue;
2784 			ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2785 			    ch->io[i], 0);
2786 		}
2787 	}
2788 	if (ret == 0xffffffff)
2789 		ret = 0;
2790 
2791 	/*
2792 	 * Some controls could be shared. Reset volumes for controls
2793 	 * related to previously chosen devices, as they may no longer
2794 	 * affect the signal.
2795 	 */
2796 	i = 0;
2797 	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
2798 		if (ctl->enable == 0 ||
2799 		    !(ctl->ossmask & pdevinfo->recsrc))
2800 			continue;
2801 		if (!((pdevinfo->playas >= 0 &&
2802 		    ctl->widget->bindas == pdevinfo->playas) ||
2803 		    (pdevinfo->recas >= 0 &&
2804 		    ctl->widget->bindas == pdevinfo->recas) ||
2805 		    (pdevinfo->index == 0 &&
2806 		    ctl->widget->bindas == -2)))
2807 			continue;
2808 		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2809 			if (pdevinfo->recsrc & (1 << j)) {
2810 				ctl->devleft[j] = 0;
2811 				ctl->devright[j] = 0;
2812 				ctl->devmute[j] = 0;
2813 			}
2814 		}
2815 	}
2816 
2817 	/*
2818 	 * Some controls could be shared. Set volumes for controls
2819 	 * related to devices selected both previously and now.
2820 	 */
2821 	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2822 		if ((ret | pdevinfo->recsrc) & (1 << j))
2823 			hdaa_audio_ctl_dev_volume(pdevinfo, j);
2824 	}
2825 
2826 	pdevinfo->recsrc = ret;
2827 	hdaa_unlock(devinfo);
2828 	return (ret);
2829 }
2830 
2831 static kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = {
2832 	KOBJMETHOD(mixer_init,		hdaa_audio_ctl_ossmixer_init),
2833 	KOBJMETHOD(mixer_set,		hdaa_audio_ctl_ossmixer_set),
2834 	KOBJMETHOD(mixer_setrecsrc,	hdaa_audio_ctl_ossmixer_setrecsrc),
2835 	KOBJMETHOD_END
2836 };
2837 MIXER_DECLARE(hdaa_audio_ctl_ossmixer);
2838 
2839 static void
2840 hdaa_dump_gpi(struct hdaa_devinfo *devinfo)
2841 {
2842 	device_t dev = devinfo->dev;
2843 	int i;
2844 	uint32_t data, wake, unsol, sticky;
2845 
2846 	if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) {
2847 		data = hda_command(dev,
2848 		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
2849 		wake = hda_command(dev,
2850 		    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid));
2851 		unsol = hda_command(dev,
2852 		    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2853 		sticky = hda_command(dev,
2854 		    HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid));
2855 		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) {
2856 			device_printf(dev, " GPI%d:%s%s%s state=%d", i,
2857 				    (sticky & (1 << i)) ? " sticky" : "",
2858 				    (unsol & (1 << i)) ? " unsol" : "",
2859 				    (wake & (1 << i)) ? " wake" : "",
2860 				    (data >> i) & 1);
2861 		}
2862 	}
2863 }
2864 
2865 static void
2866 hdaa_dump_gpio(struct hdaa_devinfo *devinfo)
2867 {
2868 	device_t dev = devinfo->dev;
2869 	int i;
2870 	uint32_t data, dir, enable, wake, unsol, sticky;
2871 
2872 	if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) {
2873 		data = hda_command(dev,
2874 		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
2875 		enable = hda_command(dev,
2876 		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
2877 		dir = hda_command(dev,
2878 		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
2879 		wake = hda_command(dev,
2880 		    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid));
2881 		unsol = hda_command(dev,
2882 		    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2883 		sticky = hda_command(dev,
2884 		    HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid));
2885 		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) {
2886 			device_printf(dev, " GPIO%d: ", i);
2887 			if ((enable & (1 << i)) == 0) {
2888 				printf("disabled\n");
2889 				continue;
2890 			}
2891 			if ((dir & (1 << i)) == 0) {
2892 				printf("input%s%s%s",
2893 				    (sticky & (1 << i)) ? " sticky" : "",
2894 				    (unsol & (1 << i)) ? " unsol" : "",
2895 				    (wake & (1 << i)) ? " wake" : "");
2896 			} else
2897 				printf("output");
2898 			printf(" state=%d\n", (data >> i) & 1);
2899 		}
2900 	}
2901 }
2902 
2903 static void
2904 hdaa_dump_gpo(struct hdaa_devinfo *devinfo)
2905 {
2906 	device_t dev = devinfo->dev;
2907 	int i;
2908 	uint32_t data;
2909 
2910 	if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) {
2911 		data = hda_command(dev,
2912 		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
2913 		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) {
2914 			device_printf(dev, " GPO%d: state=%d", i,
2915 				    (data >> i) & 1);
2916 		}
2917 	}
2918 }
2919 
2920 static void
2921 hdaa_audio_parse(struct hdaa_devinfo *devinfo)
2922 {
2923 	struct hdaa_widget *w;
2924 	uint32_t res;
2925 	int i;
2926 	nid_t nid;
2927 
2928 	nid = devinfo->nid;
2929 
2930 	res = hda_command(devinfo->dev,
2931 	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT));
2932 	devinfo->gpio_cap = res;
2933 
2934 	HDA_BOOTVERBOSE(
2935 		device_printf(devinfo->dev,
2936 		    "NumGPIO=%d NumGPO=%d "
2937 		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
2938 		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
2939 		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
2940 		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
2941 		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
2942 		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
2943 		hdaa_dump_gpi(devinfo);
2944 		hdaa_dump_gpio(devinfo);
2945 		hdaa_dump_gpo(devinfo);
2946 	);
2947 
2948 	res = hda_command(devinfo->dev,
2949 	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS));
2950 	devinfo->supp_stream_formats = res;
2951 
2952 	res = hda_command(devinfo->dev,
2953 	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE));
2954 	devinfo->supp_pcm_size_rate = res;
2955 
2956 	res = hda_command(devinfo->dev,
2957 	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP));
2958 	devinfo->outamp_cap = res;
2959 
2960 	res = hda_command(devinfo->dev,
2961 	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP));
2962 	devinfo->inamp_cap = res;
2963 
2964 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2965 		w = hdaa_widget_get(devinfo, i);
2966 		if (w == NULL)
2967 			device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i);
2968 		else {
2969 			w->devinfo = devinfo;
2970 			w->nid = i;
2971 			w->enable = 1;
2972 			w->selconn = -1;
2973 			w->pflags = 0;
2974 			w->ossdev = -1;
2975 			w->bindas = -1;
2976 			w->param.eapdbtl = HDA_INVALID;
2977 			hdaa_widget_parse(w);
2978 		}
2979 	}
2980 }
2981 
2982 static void
2983 hdaa_audio_postprocess(struct hdaa_devinfo *devinfo)
2984 {
2985 	struct hdaa_widget *w;
2986 	int i;
2987 
2988 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2989 		w = hdaa_widget_get(devinfo, i);
2990 		if (w == NULL)
2991 			continue;
2992 		hdaa_widget_postprocess(w);
2993 	}
2994 }
2995 
2996 static void
2997 hdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo)
2998 {
2999 	struct hdaa_audio_ctl *ctls;
3000 	struct hdaa_widget *w, *cw;
3001 	int i, j, cnt, max, ocap, icap;
3002 	int mute, offset, step, size;
3003 
3004 	/* XXX This is redundant */
3005 	max = 0;
3006 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3007 		w = hdaa_widget_get(devinfo, i);
3008 		if (w == NULL || w->enable == 0)
3009 			continue;
3010 		if (w->param.outamp_cap != 0)
3011 			max++;
3012 		if (w->param.inamp_cap != 0) {
3013 			switch (w->type) {
3014 			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3015 			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3016 				for (j = 0; j < w->nconns; j++) {
3017 					cw = hdaa_widget_get(devinfo,
3018 					    w->conns[j]);
3019 					if (cw == NULL || cw->enable == 0)
3020 						continue;
3021 					max++;
3022 				}
3023 				break;
3024 			default:
3025 				max++;
3026 				break;
3027 			}
3028 		}
3029 	}
3030 	devinfo->ctlcnt = max;
3031 
3032 	if (max < 1)
3033 		return;
3034 
3035 	ctls = (struct hdaa_audio_ctl *)malloc(
3036 	    sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT);
3037 
3038 	if (ctls == NULL) {
3039 		/* Blekh! */
3040 		device_printf(devinfo->dev, "unable to allocate ctls!\n");
3041 		devinfo->ctlcnt = 0;
3042 		return;
3043 	}
3044 
3045 	cnt = 0;
3046 	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
3047 		if (cnt >= max) {
3048 			device_printf(devinfo->dev, "%s: Ctl overflow!\n",
3049 			    __func__);
3050 			break;
3051 		}
3052 		w = hdaa_widget_get(devinfo, i);
3053 		if (w == NULL || w->enable == 0)
3054 			continue;
3055 		ocap = w->param.outamp_cap;
3056 		icap = w->param.inamp_cap;
3057 		if (ocap != 0) {
3058 			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
3059 			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
3060 			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
3061 			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
3062 			/*if (offset > step) {
3063 				HDA_BOOTVERBOSE(
3064 					device_printf(devinfo->dev,
3065 					    "BUGGY outamp: nid=%d "
3066 					    "[offset=%d > step=%d]\n",
3067 					    w->nid, offset, step);
3068 				);
3069 				offset = step;
3070 			}*/
3071 			ctls[cnt].enable = 1;
3072 			ctls[cnt].widget = w;
3073 			ctls[cnt].mute = mute;
3074 			ctls[cnt].step = step;
3075 			ctls[cnt].size = size;
3076 			ctls[cnt].offset = offset;
3077 			ctls[cnt].left = offset;
3078 			ctls[cnt].right = offset;
3079 			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3080 			    w->waspin)
3081 				ctls[cnt].ndir = HDAA_CTL_IN;
3082 			else
3083 				ctls[cnt].ndir = HDAA_CTL_OUT;
3084 			ctls[cnt++].dir = HDAA_CTL_OUT;
3085 		}
3086 
3087 		if (icap != 0) {
3088 			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
3089 			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
3090 			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
3091 			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
3092 			/*if (offset > step) {
3093 				HDA_BOOTVERBOSE(
3094 					device_printf(devinfo->dev,
3095 					    "BUGGY inamp: nid=%d "
3096 					    "[offset=%d > step=%d]\n",
3097 					    w->nid, offset, step);
3098 				);
3099 				offset = step;
3100 			}*/
3101 			switch (w->type) {
3102 			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3103 			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3104 				for (j = 0; j < w->nconns; j++) {
3105 					if (cnt >= max) {
3106 						device_printf(devinfo->dev,
3107 						    "%s: Ctl overflow!\n",
3108 						    __func__);
3109 						break;
3110 					}
3111 					cw = hdaa_widget_get(devinfo,
3112 					    w->conns[j]);
3113 					if (cw == NULL || cw->enable == 0)
3114 						continue;
3115 					ctls[cnt].enable = 1;
3116 					ctls[cnt].widget = w;
3117 					ctls[cnt].childwidget = cw;
3118 					ctls[cnt].index = j;
3119 					ctls[cnt].mute = mute;
3120 					ctls[cnt].step = step;
3121 					ctls[cnt].size = size;
3122 					ctls[cnt].offset = offset;
3123 					ctls[cnt].left = offset;
3124 					ctls[cnt].right = offset;
3125 				ctls[cnt].ndir = HDAA_CTL_IN;
3126 					ctls[cnt++].dir = HDAA_CTL_IN;
3127 				}
3128 				break;
3129 			default:
3130 				if (cnt >= max) {
3131 					device_printf(devinfo->dev,
3132 					    "%s: Ctl overflow!\n",
3133 					    __func__);
3134 					break;
3135 				}
3136 				ctls[cnt].enable = 1;
3137 				ctls[cnt].widget = w;
3138 				ctls[cnt].mute = mute;
3139 				ctls[cnt].step = step;
3140 				ctls[cnt].size = size;
3141 				ctls[cnt].offset = offset;
3142 				ctls[cnt].left = offset;
3143 				ctls[cnt].right = offset;
3144 				if (w->type ==
3145 				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3146 					ctls[cnt].ndir = HDAA_CTL_OUT;
3147 				else
3148 					ctls[cnt].ndir = HDAA_CTL_IN;
3149 				ctls[cnt++].dir = HDAA_CTL_IN;
3150 				break;
3151 			}
3152 		}
3153 	}
3154 
3155 	devinfo->ctl = ctls;
3156 }
3157 
3158 static void
3159 hdaa_audio_as_parse(struct hdaa_devinfo *devinfo)
3160 {
3161 	struct hdaa_audio_as *as;
3162 	struct hdaa_widget *w;
3163 	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
3164 
3165 	/* Count present associations */
3166 	max = 0;
3167 	for (j = 1; j < 16; j++) {
3168 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3169 			w = hdaa_widget_get(devinfo, i);
3170 			if (w == NULL || w->enable == 0)
3171 				continue;
3172 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3173 				continue;
3174 			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
3175 			    != j)
3176 				continue;
3177 			max++;
3178 			if (j != 15)  /* There could be many 1-pin assocs #15 */
3179 				break;
3180 		}
3181 	}
3182 
3183 	devinfo->ascnt = max;
3184 
3185 	if (max < 1)
3186 		return;
3187 
3188 	as = (struct hdaa_audio_as *)malloc(
3189 	    sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT);
3190 
3191 	if (as == NULL) {
3192 		/* Blekh! */
3193 		device_printf(devinfo->dev, "unable to allocate assocs!\n");
3194 		devinfo->ascnt = 0;
3195 		return;
3196 	}
3197 
3198 	for (i = 0; i < max; i++) {
3199 		as[i].hpredir = -1;
3200 		as[i].digital = 0;
3201 		as[i].num_chans = 1;
3202 		as[i].location = -1;
3203 	}
3204 
3205 	/* Scan associations skipping as=0. */
3206 	cnt = 0;
3207 	for (j = 1; j < 16 && cnt < max; j++) {
3208 		first = 16;
3209 		hpredir = 0;
3210 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3211 			w = hdaa_widget_get(devinfo, i);
3212 			if (w == NULL || w->enable == 0)
3213 				continue;
3214 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3215 				continue;
3216 			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
3217 			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
3218 			if (assoc != j) {
3219 				continue;
3220 			}
3221 			KASSERT(cnt < max,
3222 			    ("%s: Associations owerflow (%d of %d)",
3223 			    __func__, cnt, max));
3224 			type = w->wclass.pin.config &
3225 			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
3226 			/* Get pin direction. */
3227 			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
3228 			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
3229 			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
3230 			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
3231 			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
3232 				dir = HDAA_CTL_OUT;
3233 			else
3234 				dir = HDAA_CTL_IN;
3235 			/* If this is a first pin - create new association. */
3236 			if (as[cnt].pincnt == 0) {
3237 				as[cnt].enable = 1;
3238 				as[cnt].index = j;
3239 				as[cnt].dir = dir;
3240 			}
3241 			if (seq < first)
3242 				first = seq;
3243 			/* Check association correctness. */
3244 			if (as[cnt].pins[seq] != 0) {
3245 				device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) "
3246 				    "in association %d! Disabling association.\n",
3247 				    __func__, seq, w->nid, j);
3248 				as[cnt].enable = 0;
3249 			}
3250 			if (dir != as[cnt].dir) {
3251 				device_printf(devinfo->dev, "%s: Pin %d has wrong "
3252 				    "direction for association %d! Disabling "
3253 				    "association.\n",
3254 				    __func__, w->nid, j);
3255 				as[cnt].enable = 0;
3256 			}
3257 			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
3258 				as[cnt].digital |= 0x1;
3259 				if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
3260 					as[cnt].digital |= 0x2;
3261 				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
3262 					as[cnt].digital |= 0x4;
3263 			}
3264 			if (as[cnt].location == -1) {
3265 				as[cnt].location =
3266 				    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config);
3267 			} else if (as[cnt].location !=
3268 			    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) {
3269 				as[cnt].location = -2;
3270 			}
3271 			/* Headphones with seq=15 may mean redirection. */
3272 			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
3273 			    seq == 15)
3274 				hpredir = 1;
3275 			as[cnt].pins[seq] = w->nid;
3276 			as[cnt].pincnt++;
3277 			/* Association 15 is a multiple unassociated pins. */
3278 			if (j == 15)
3279 				cnt++;
3280 		}
3281 		if (j != 15 && as[cnt].pincnt > 0) {
3282 			if (hpredir && as[cnt].pincnt > 1)
3283 				as[cnt].hpredir = first;
3284 			cnt++;
3285 		}
3286 	}
3287 	for (i = 0; i < max; i++) {
3288 		if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 ||
3289 		    as[i].pins[14] > 0 || as[i].pins[15] > 0))
3290 			as[i].mixed = 1;
3291 	}
3292 	HDA_BOOTVERBOSE(
3293 		device_printf(devinfo->dev,
3294 		    "%d associations found:\n", max);
3295 		for (i = 0; i < max; i++) {
3296 			device_printf(devinfo->dev,
3297 			    "Association %d (%d) %s%s:\n",
3298 			    i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out",
3299 			    as[i].enable?"":" (disabled)");
3300 			for (j = 0; j < 16; j++) {
3301 				if (as[i].pins[j] == 0)
3302 					continue;
3303 				device_printf(devinfo->dev,
3304 				    " Pin nid=%d seq=%d\n",
3305 				    as[i].pins[j], j);
3306 			}
3307 		}
3308 	);
3309 
3310 	devinfo->as = as;
3311 }
3312 
3313 /*
3314  * Trace path from DAC to pin.
3315  */
3316 static nid_t
3317 hdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3318     int dupseq, int min, int only, int depth)
3319 {
3320 	struct hdaa_widget *w;
3321 	int i, im = -1;
3322 	nid_t m = 0, ret;
3323 
3324 	if (depth > HDA_PARSE_MAXDEPTH)
3325 		return (0);
3326 	w = hdaa_widget_get(devinfo, nid);
3327 	if (w == NULL || w->enable == 0)
3328 		return (0);
3329 	HDA_BOOTHVERBOSE(
3330 		if (!only) {
3331 			device_printf(devinfo->dev,
3332 			    " %*stracing via nid %d\n",
3333 				depth + 1, "", w->nid);
3334 		}
3335 	);
3336 	/* Use only unused widgets */
3337 	if (w->bindas >= 0 && w->bindas != as) {
3338 		HDA_BOOTHVERBOSE(
3339 			if (!only) {
3340 				device_printf(devinfo->dev,
3341 				    " %*snid %d busy by association %d\n",
3342 					depth + 1, "", w->nid, w->bindas);
3343 			}
3344 		);
3345 		return (0);
3346 	}
3347 	if (dupseq < 0) {
3348 		if (w->bindseqmask != 0) {
3349 			HDA_BOOTHVERBOSE(
3350 				if (!only) {
3351 					device_printf(devinfo->dev,
3352 					    " %*snid %d busy by seqmask %x\n",
3353 						depth + 1, "", w->nid, w->bindseqmask);
3354 				}
3355 			);
3356 			return (0);
3357 		}
3358 	} else {
3359 		/* If this is headphones - allow duplicate first pin. */
3360 		if (w->bindseqmask != 0 &&
3361 		    (w->bindseqmask & (1 << dupseq)) == 0) {
3362 			HDA_BOOTHVERBOSE(
3363 				device_printf(devinfo->dev,
3364 				    " %*snid %d busy by seqmask %x\n",
3365 					depth + 1, "", w->nid, w->bindseqmask);
3366 			);
3367 			return (0);
3368 		}
3369 	}
3370 
3371 	switch (w->type) {
3372 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3373 		/* Do not traverse input. AD1988 has digital monitor
3374 		for which we are not ready. */
3375 		break;
3376 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
3377 		/* If we are tracing HP take only dac of first pin. */
3378 		if ((only == 0 || only == w->nid) &&
3379 		    (w->nid >= min) && (dupseq < 0 || w->nid ==
3380 		    devinfo->as[as].dacs[0][dupseq]))
3381 			m = w->nid;
3382 		break;
3383 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3384 		if (depth > 0)
3385 			break;
3386 		/* Fall */
3387 	default:
3388 		/* Find reachable DACs with smallest nid respecting constraints. */
3389 		for (i = 0; i < w->nconns; i++) {
3390 			if (w->connsenable[i] == 0)
3391 				continue;
3392 			if (w->selconn != -1 && w->selconn != i)
3393 				continue;
3394 			if ((ret = hdaa_audio_trace_dac(devinfo, as, seq,
3395 			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
3396 				if (m == 0 || ret < m) {
3397 					m = ret;
3398 					im = i;
3399 				}
3400 				if (only || dupseq >= 0)
3401 					break;
3402 			}
3403 		}
3404 		if (im >= 0 && only && ((w->nconns > 1 &&
3405 		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3406 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3407 			w->selconn = im;
3408 		break;
3409 	}
3410 	if (m && only) {
3411 		w->bindas = as;
3412 		w->bindseqmask |= (1 << seq);
3413 	}
3414 	HDA_BOOTHVERBOSE(
3415 		if (!only) {
3416 			device_printf(devinfo->dev,
3417 			    " %*snid %d returned %d\n",
3418 				depth + 1, "", w->nid, m);
3419 		}
3420 	);
3421 	return (m);
3422 }
3423 
3424 /*
3425  * Trace path from widget to ADC.
3426  */
3427 static nid_t
3428 hdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3429     int mixed, int min, int only, int depth, int *length, int onlylength)
3430 {
3431 	struct hdaa_widget *w, *wc;
3432 	int i, j, im, lm = HDA_PARSE_MAXDEPTH;
3433 	nid_t m = 0, ret;
3434 
3435 	if (depth > HDA_PARSE_MAXDEPTH)
3436 		return (0);
3437 	w = hdaa_widget_get(devinfo, nid);
3438 	if (w == NULL || w->enable == 0)
3439 		return (0);
3440 	HDA_BOOTHVERBOSE(
3441 		device_printf(devinfo->dev,
3442 		    " %*stracing via nid %d\n",
3443 			depth + 1, "", w->nid);
3444 	);
3445 	/* Use only unused widgets */
3446 	if (w->bindas >= 0 && w->bindas != as) {
3447 		HDA_BOOTHVERBOSE(
3448 			device_printf(devinfo->dev,
3449 			    " %*snid %d busy by association %d\n",
3450 				depth + 1, "", w->nid, w->bindas);
3451 		);
3452 		return (0);
3453 	}
3454 	if (!mixed && w->bindseqmask != 0) {
3455 		HDA_BOOTHVERBOSE(
3456 			device_printf(devinfo->dev,
3457 			    " %*snid %d busy by seqmask %x\n",
3458 				depth + 1, "", w->nid, w->bindseqmask);
3459 		);
3460 		return (0);
3461 	}
3462 	switch (w->type) {
3463 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3464 		if ((only == 0 || only == w->nid) && (w->nid >= min) &&
3465 		    (onlylength == 0 || onlylength == depth)) {
3466 			m = w->nid;
3467 			*length = depth;
3468 		}
3469 		break;
3470 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3471 		if (depth > 0)
3472 			break;
3473 		/* Fall */
3474 	default:
3475 		/* Try to find reachable ADCs with specified nid. */
3476 		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3477 			wc = hdaa_widget_get(devinfo, j);
3478 			if (wc == NULL || wc->enable == 0)
3479 				continue;
3480 			im = -1;
3481 			for (i = 0; i < wc->nconns; i++) {
3482 				if (wc->connsenable[i] == 0)
3483 					continue;
3484 				if (wc->conns[i] != nid)
3485 					continue;
3486 				if ((ret = hdaa_audio_trace_adc(devinfo, as, seq,
3487 				    j, mixed, min, only, depth + 1,
3488 				    length, onlylength)) != 0) {
3489 					if (m == 0 || ret < m ||
3490 					    (ret == m && *length < lm)) {
3491 						m = ret;
3492 						im = i;
3493 						lm = *length;
3494 					} else
3495 						*length = lm;
3496 					if (only)
3497 						break;
3498 				}
3499 			}
3500 			if (im >= 0 && only && ((wc->nconns > 1 &&
3501 			    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3502 			    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3503 				wc->selconn = im;
3504 		}
3505 		break;
3506 	}
3507 	if (m && only) {
3508 		w->bindas = as;
3509 		w->bindseqmask |= (1 << seq);
3510 	}
3511 	HDA_BOOTHVERBOSE(
3512 		device_printf(devinfo->dev,
3513 		    " %*snid %d returned %d\n",
3514 			depth + 1, "", w->nid, m);
3515 	);
3516 	return (m);
3517 }
3518 
3519 /*
3520  * Erase trace path of the specified association.
3521  */
3522 static void
3523 hdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq)
3524 {
3525 	struct hdaa_widget *w;
3526 	int i;
3527 
3528 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3529 		w = hdaa_widget_get(devinfo, i);
3530 		if (w == NULL || w->enable == 0)
3531 			continue;
3532 		if (w->bindas == as) {
3533 			if (seq >= 0) {
3534 				w->bindseqmask &= ~(1 << seq);
3535 				if (w->bindseqmask == 0) {
3536 					w->bindas = -1;
3537 					w->selconn = -1;
3538 				}
3539 			} else {
3540 				w->bindas = -1;
3541 				w->bindseqmask = 0;
3542 				w->selconn = -1;
3543 			}
3544 		}
3545 	}
3546 }
3547 
3548 /*
3549  * Trace association path from DAC to output
3550  */
3551 static int
3552 hdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq)
3553 {
3554 	struct hdaa_audio_as *ases = devinfo->as;
3555 	int i, hpredir;
3556 	nid_t min, res;
3557 
3558 	/* Find next pin */
3559 	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3560 		;
3561 	/* Check if there is no any left. If so - we succeeded. */
3562 	if (i == 16)
3563 		return (1);
3564 
3565 	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
3566 	min = 0;
3567 	do {
3568 		HDA_BOOTHVERBOSE(
3569 			device_printf(devinfo->dev,
3570 			    " Tracing pin %d with min nid %d",
3571 			    ases[as].pins[i], min);
3572 			if (hpredir >= 0)
3573 				printf(" and hpredir %d", hpredir);
3574 			printf("\n");
3575 		);
3576 		/* Trace this pin taking min nid into account. */
3577 		res = hdaa_audio_trace_dac(devinfo, as, i,
3578 		    ases[as].pins[i], hpredir, min, 0, 0);
3579 		if (res == 0) {
3580 			/* If we failed - return to previous and redo it. */
3581 			HDA_BOOTVERBOSE(
3582 				device_printf(devinfo->dev,
3583 				    " Unable to trace pin %d seq %d with min "
3584 				    "nid %d",
3585 				    ases[as].pins[i], i, min);
3586 				if (hpredir >= 0)
3587 					printf(" and hpredir %d", hpredir);
3588 				printf("\n");
3589 			);
3590 			return (0);
3591 		}
3592 		HDA_BOOTVERBOSE(
3593 			device_printf(devinfo->dev,
3594 			    " Pin %d traced to DAC %d",
3595 			    ases[as].pins[i], res);
3596 			if (hpredir >= 0)
3597 				printf(" and hpredir %d", hpredir);
3598 			if (ases[as].fakeredir)
3599 				printf(" with fake redirection");
3600 			printf("\n");
3601 		);
3602 		/* Trace again to mark the path */
3603 		hdaa_audio_trace_dac(devinfo, as, i,
3604 		    ases[as].pins[i], hpredir, min, res, 0);
3605 		ases[as].dacs[0][i] = res;
3606 		/* We succeeded, so call next. */
3607 		if (hdaa_audio_trace_as_out(devinfo, as, i + 1))
3608 			return (1);
3609 		/* If next failed, we should retry with next min */
3610 		hdaa_audio_undo_trace(devinfo, as, i);
3611 		ases[as].dacs[0][i] = 0;
3612 		min = res + 1;
3613 	} while (1);
3614 }
3615 
3616 /*
3617  * Check equivalency of two DACs.
3618  */
3619 static int
3620 hdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3621 {
3622 	struct hdaa_devinfo *devinfo = w1->devinfo;
3623 	struct hdaa_widget *w3;
3624 	int i, j, c1, c2;
3625 
3626 	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3627 		return (0);
3628 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3629 		w3 = hdaa_widget_get(devinfo, i);
3630 		if (w3 == NULL || w3->enable == 0)
3631 			continue;
3632 		if (w3->bindas != w1->bindas)
3633 			continue;
3634 		if (w3->nconns == 0)
3635 			continue;
3636 		c1 = c2 = -1;
3637 		for (j = 0; j < w3->nconns; j++) {
3638 			if (w3->connsenable[j] == 0)
3639 				continue;
3640 			if (w3->conns[j] == w1->nid)
3641 				c1 = j;
3642 			if (w3->conns[j] == w2->nid)
3643 				c2 = j;
3644 		}
3645 		if (c1 < 0)
3646 			continue;
3647 		if (c2 < 0)
3648 			return (0);
3649 		if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3650 			return (0);
3651 	}
3652 	return (1);
3653 }
3654 
3655 /*
3656  * Check equivalency of two ADCs.
3657  */
3658 static int
3659 hdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3660 {
3661 	struct hdaa_devinfo *devinfo = w1->devinfo;
3662 	struct hdaa_widget *w3, *w4;
3663 	int i;
3664 
3665 	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3666 		return (0);
3667 	if (w1->nconns != 1 || w2->nconns != 1)
3668 		return (0);
3669 	if (w1->conns[0] == w2->conns[0])
3670 		return (1);
3671 	w3 = hdaa_widget_get(devinfo, w1->conns[0]);
3672 	if (w3 == NULL || w3->enable == 0)
3673 		return (0);
3674 	w4 = hdaa_widget_get(devinfo, w2->conns[0]);
3675 	if (w4 == NULL || w4->enable == 0)
3676 		return (0);
3677 	if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask)
3678 		return (1);
3679 	if (w4->bindas >= 0)
3680 		return (0);
3681 	if (w3->type != w4->type)
3682 		return (0);
3683 	if (memcmp(&w3->param, &w4->param, sizeof(w3->param)))
3684 		return (0);
3685 	if (w3->nconns != w4->nconns)
3686 		return (0);
3687 	for (i = 0; i < w3->nconns; i++) {
3688 		if (w3->conns[i] != w4->conns[i])
3689 			return (0);
3690 	}
3691 	return (1);
3692 }
3693 
3694 /*
3695  * Look for equivalent DAC/ADC to implement second channel.
3696  */
3697 static void
3698 hdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid)
3699 {
3700 	struct hdaa_audio_as *as = &devinfo->as[asid];
3701 	struct hdaa_widget *w1, *w2;
3702 	int i, pos;
3703 	nid_t nid1, nid2;
3704 
3705 	HDA_BOOTVERBOSE(
3706 		device_printf(devinfo->dev,
3707 		    "Looking for additional %sC "
3708 		    "for association %d (%d)\n",
3709 		    (as->dir == HDAA_CTL_OUT) ? "DA" : "AD",
3710 		    asid, as->index);
3711 	);
3712 
3713 	/* Find the exisitng DAC position and return if found more the one. */
3714 	pos = -1;
3715 	for (i = 0; i < 16; i++) {
3716 		if (as->dacs[0][i] <= 0)
3717 			continue;
3718 		if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos])
3719 			return;
3720 		pos = i;
3721 	}
3722 
3723 	nid1 = as->dacs[0][pos];
3724 	w1 = hdaa_widget_get(devinfo, nid1);
3725 	w2 = NULL;
3726 	for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) {
3727 		w2 = hdaa_widget_get(devinfo, nid2);
3728 		if (w2 == NULL || w2->enable == 0)
3729 			continue;
3730 		if (w2->bindas >= 0)
3731 			continue;
3732 		if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
3733 			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
3734 				continue;
3735 			if (hdaa_audio_dacs_equal(w1, w2))
3736 				break;
3737 		} else {
3738 			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3739 				continue;
3740 			if (hdaa_audio_adcs_equal(w1, w2))
3741 				break;
3742 		}
3743 	}
3744 	if (nid2 >= devinfo->endnode)
3745 		return;
3746 	w2->bindas = w1->bindas;
3747 	w2->bindseqmask = w1->bindseqmask;
3748 	if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
3749 		HDA_BOOTVERBOSE(
3750 			device_printf(devinfo->dev,
3751 			    " ADC %d considered equal to ADC %d\n", nid2, nid1);
3752 		);
3753 		w1 = hdaa_widget_get(devinfo, w1->conns[0]);
3754 		w2 = hdaa_widget_get(devinfo, w2->conns[0]);
3755 		w2->bindas = w1->bindas;
3756 		w2->bindseqmask = w1->bindseqmask;
3757 	} else {
3758 		HDA_BOOTVERBOSE(
3759 			device_printf(devinfo->dev,
3760 			    " DAC %d considered equal to DAC %d\n", nid2, nid1);
3761 		);
3762 	}
3763 	for (i = 0; i < 16; i++) {
3764 		if (as->dacs[0][i] <= 0)
3765 			continue;
3766 		as->dacs[as->num_chans][i] = nid2;
3767 	}
3768 	as->num_chans++;
3769 }
3770 
3771 /*
3772  * Trace association path from input to ADC
3773  */
3774 static int
3775 hdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as)
3776 {
3777 	struct hdaa_audio_as *ases = devinfo->as;
3778 	struct hdaa_widget *w;
3779 	int i, j, k, length;
3780 
3781 	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3782 		w = hdaa_widget_get(devinfo, j);
3783 		if (w == NULL || w->enable == 0)
3784 			continue;
3785 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3786 			continue;
3787 		if (w->bindas >= 0 && w->bindas != as)
3788 			continue;
3789 
3790 		/* Find next pin */
3791 		for (i = 0; i < 16; i++) {
3792 			if (ases[as].pins[i] == 0)
3793 				continue;
3794 
3795 			HDA_BOOTHVERBOSE(
3796 				device_printf(devinfo->dev,
3797 				    " Tracing pin %d to ADC %d\n",
3798 				    ases[as].pins[i], j);
3799 			);
3800 			/* Trace this pin taking goal into account. */
3801 			if (hdaa_audio_trace_adc(devinfo, as, i,
3802 			    ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) {
3803 				/* If we failed - return to previous and redo it. */
3804 				HDA_BOOTVERBOSE(
3805 					device_printf(devinfo->dev,
3806 					    " Unable to trace pin %d to ADC %d, undo traces\n",
3807 					    ases[as].pins[i], j);
3808 				);
3809 				hdaa_audio_undo_trace(devinfo, as, -1);
3810 				for (k = 0; k < 16; k++)
3811 					ases[as].dacs[0][k] = 0;
3812 				break;
3813 			}
3814 			HDA_BOOTVERBOSE(
3815 				device_printf(devinfo->dev,
3816 				    " Pin %d traced to ADC %d\n",
3817 				    ases[as].pins[i], j);
3818 			);
3819 			ases[as].dacs[0][i] = j;
3820 		}
3821 		if (i == 16)
3822 			return (1);
3823 	}
3824 	return (0);
3825 }
3826 
3827 /*
3828  * Trace association path from input to multiple ADCs
3829  */
3830 static int
3831 hdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq)
3832 {
3833 	struct hdaa_audio_as *ases = devinfo->as;
3834 	int i, length;
3835 	nid_t min, res;
3836 
3837 	/* Find next pin */
3838 	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3839 		;
3840 	/* Check if there is no any left. If so - we succeeded. */
3841 	if (i == 16)
3842 		return (1);
3843 
3844 	min = 0;
3845 	do {
3846 		HDA_BOOTHVERBOSE(
3847 			device_printf(devinfo->dev,
3848 			    " Tracing pin %d with min nid %d",
3849 			    ases[as].pins[i], min);
3850 			printf("\n");
3851 		);
3852 		/* Trace this pin taking min nid into account. */
3853 		res = hdaa_audio_trace_adc(devinfo, as, i,
3854 		    ases[as].pins[i], 0, min, 0, 0, &length, 0);
3855 		if (res == 0) {
3856 			/* If we failed - return to previous and redo it. */
3857 			HDA_BOOTVERBOSE(
3858 				device_printf(devinfo->dev,
3859 				    " Unable to trace pin %d seq %d with min "
3860 				    "nid %d",
3861 				    ases[as].pins[i], i, min);
3862 				printf("\n");
3863 			);
3864 			return (0);
3865 		}
3866 		HDA_BOOTVERBOSE(
3867 			device_printf(devinfo->dev,
3868 			    " Pin %d traced to ADC %d\n",
3869 			    ases[as].pins[i], res);
3870 		);
3871 		/* Trace again to mark the path */
3872 		hdaa_audio_trace_adc(devinfo, as, i,
3873 		    ases[as].pins[i], 0, min, res, 0, &length, length);
3874 		ases[as].dacs[0][i] = res;
3875 		/* We succeeded, so call next. */
3876 		if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1))
3877 			return (1);
3878 		/* If next failed, we should retry with next min */
3879 		hdaa_audio_undo_trace(devinfo, as, i);
3880 		ases[as].dacs[0][i] = 0;
3881 		min = res + 1;
3882 	} while (1);
3883 }
3884 
3885 /*
3886  * Trace input monitor path from mixer to output association.
3887  */
3888 static int
3889 hdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth)
3890 {
3891 	struct hdaa_audio_as *ases = devinfo->as;
3892 	struct hdaa_widget *w, *wc;
3893 	int i, j;
3894 	nid_t res = 0;
3895 
3896 	if (depth > HDA_PARSE_MAXDEPTH)
3897 		return (0);
3898 	w = hdaa_widget_get(devinfo, nid);
3899 	if (w == NULL || w->enable == 0)
3900 		return (0);
3901 	HDA_BOOTHVERBOSE(
3902 		device_printf(devinfo->dev,
3903 		    " %*stracing via nid %d\n",
3904 			depth + 1, "", w->nid);
3905 	);
3906 	/* Use only unused widgets */
3907 	if (depth > 0 && w->bindas != -1) {
3908 		if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) {
3909 			HDA_BOOTHVERBOSE(
3910 				device_printf(devinfo->dev,
3911 				    " %*snid %d found output association %d\n",
3912 					depth + 1, "", w->nid, w->bindas);
3913 			);
3914 			if (w->bindas >= 0)
3915 				w->pflags |= HDAA_ADC_MONITOR;
3916 			return (1);
3917 		} else {
3918 			HDA_BOOTHVERBOSE(
3919 				device_printf(devinfo->dev,
3920 				    " %*snid %d busy by input association %d\n",
3921 					depth + 1, "", w->nid, w->bindas);
3922 			);
3923 			return (0);
3924 		}
3925 	}
3926 
3927 	switch (w->type) {
3928 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3929 		/* Do not traverse input. AD1988 has digital monitor
3930 		for which we are not ready. */
3931 		break;
3932 	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3933 		if (depth > 0)
3934 			break;
3935 		/* Fall */
3936 	default:
3937 		/* Try to find reachable ADCs with specified nid. */
3938 		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3939 			wc = hdaa_widget_get(devinfo, j);
3940 			if (wc == NULL || wc->enable == 0)
3941 				continue;
3942 			for (i = 0; i < wc->nconns; i++) {
3943 				if (wc->connsenable[i] == 0)
3944 					continue;
3945 				if (wc->conns[i] != nid)
3946 					continue;
3947 				if (hdaa_audio_trace_to_out(devinfo,
3948 				    j, depth + 1) != 0) {
3949 					res = 1;
3950 					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3951 					    wc->selconn == -1)
3952 						wc->selconn = i;
3953 				}
3954 			}
3955 		}
3956 		break;
3957 	}
3958 	if (res && w->bindas == -1)
3959 		w->bindas = -2;
3960 
3961 	HDA_BOOTHVERBOSE(
3962 		device_printf(devinfo->dev,
3963 		    " %*snid %d returned %d\n",
3964 			depth + 1, "", w->nid, res);
3965 	);
3966 	return (res);
3967 }
3968 
3969 /*
3970  * Trace extra associations (beeper, monitor)
3971  */
3972 static void
3973 hdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo)
3974 {
3975 	struct hdaa_audio_as *as = devinfo->as;
3976 	struct hdaa_widget *w;
3977 	int j;
3978 
3979 	/* Input monitor */
3980 	/* Find mixer associated with input, but supplying signal
3981 	   for output associations. Hope it will be input monitor. */
3982 	HDA_BOOTVERBOSE(
3983 		device_printf(devinfo->dev,
3984 		    "Tracing input monitor\n");
3985 	);
3986 	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3987 		w = hdaa_widget_get(devinfo, j);
3988 		if (w == NULL || w->enable == 0)
3989 			continue;
3990 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3991 			continue;
3992 		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3993 			continue;
3994 		HDA_BOOTVERBOSE(
3995 			device_printf(devinfo->dev,
3996 			    " Tracing nid %d to out\n",
3997 			    j);
3998 		);
3999 		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4000 			HDA_BOOTVERBOSE(
4001 				device_printf(devinfo->dev,
4002 				    " nid %d is input monitor\n",
4003 					w->nid);
4004 			);
4005 			w->ossdev = SOUND_MIXER_IMIX;
4006 		}
4007 	}
4008 
4009 	/* Other inputs monitor */
4010 	/* Find input pins supplying signal for output associations.
4011 	   Hope it will be input monitoring. */
4012 	HDA_BOOTVERBOSE(
4013 		device_printf(devinfo->dev,
4014 		    "Tracing other input monitors\n");
4015 	);
4016 	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4017 		w = hdaa_widget_get(devinfo, j);
4018 		if (w == NULL || w->enable == 0)
4019 			continue;
4020 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4021 			continue;
4022 		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
4023 			continue;
4024 		HDA_BOOTVERBOSE(
4025 			device_printf(devinfo->dev,
4026 			    " Tracing nid %d to out\n",
4027 			    j);
4028 		);
4029 		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4030 			HDA_BOOTVERBOSE(
4031 				device_printf(devinfo->dev,
4032 				    " nid %d is input monitor\n",
4033 					w->nid);
4034 			);
4035 		}
4036 	}
4037 
4038 	/* Beeper */
4039 	HDA_BOOTVERBOSE(
4040 		device_printf(devinfo->dev,
4041 		    "Tracing beeper\n");
4042 	);
4043 	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4044 		w = hdaa_widget_get(devinfo, j);
4045 		if (w == NULL || w->enable == 0)
4046 			continue;
4047 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
4048 			continue;
4049 		HDA_BOOTHVERBOSE(
4050 			device_printf(devinfo->dev,
4051 			    " Tracing nid %d to out\n",
4052 			    j);
4053 		);
4054 		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4055 			HDA_BOOTVERBOSE(
4056 				device_printf(devinfo->dev,
4057 				    " nid %d traced to out\n",
4058 				    j);
4059 			);
4060 		}
4061 		w->bindas = -2;
4062 	}
4063 }
4064 
4065 /*
4066  * Bind assotiations to PCM channels
4067  */
4068 static void
4069 hdaa_audio_bind_as(struct hdaa_devinfo *devinfo)
4070 {
4071 	struct hdaa_audio_as *as = devinfo->as;
4072 	int i, j, cnt = 0, free;
4073 
4074 	for (j = 0; j < devinfo->ascnt; j++) {
4075 		if (as[j].enable)
4076 			cnt += as[j].num_chans;
4077 	}
4078 	if (devinfo->num_chans == 0) {
4079 		devinfo->chans = (struct hdaa_chan *)malloc(
4080 		    sizeof(struct hdaa_chan) * cnt,
4081 		    M_HDAA, M_ZERO | M_NOWAIT);
4082 		if (devinfo->chans == NULL) {
4083 			device_printf(devinfo->dev,
4084 			    "Channels memory allocation failed!\n");
4085 			return;
4086 		}
4087 	} else {
4088 		devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans,
4089 		    sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt),
4090 		    M_HDAA, M_ZERO | M_NOWAIT);
4091 		if (devinfo->chans == NULL) {
4092 			devinfo->num_chans = 0;
4093 			device_printf(devinfo->dev,
4094 			    "Channels memory allocation failed!\n");
4095 			return;
4096 		}
4097 		/* Fixup relative pointers after realloc */
4098 		for (j = 0; j < devinfo->num_chans; j++)
4099 			devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist;
4100 	}
4101 	free = devinfo->num_chans;
4102 	devinfo->num_chans += cnt;
4103 
4104 	for (j = free; j < free + cnt; j++) {
4105 		devinfo->chans[j].devinfo = devinfo;
4106 		devinfo->chans[j].as = -1;
4107 	}
4108 
4109 	/* Assign associations in order of their numbers, */
4110 	for (j = 0; j < devinfo->ascnt; j++) {
4111 		if (as[j].enable == 0)
4112 			continue;
4113 		for (i = 0; i < as[j].num_chans; i++) {
4114 			devinfo->chans[free].as = j;
4115 			devinfo->chans[free].asindex = i;
4116 			devinfo->chans[free].dir =
4117 			    (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
4118 			hdaa_pcmchannel_setup(&devinfo->chans[free]);
4119 			as[j].chans[i] = free;
4120 			free++;
4121 		}
4122 	}
4123 }
4124 
4125 static void
4126 hdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo)
4127 {
4128 	struct hdaa_widget *w;
4129 	int i;
4130 
4131 	/* Disable power and volume widgets. */
4132 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4133 		w = hdaa_widget_get(devinfo, i);
4134 		if (w == NULL || w->enable == 0)
4135 			continue;
4136 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
4137 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
4138 			w->enable = 0;
4139 			HDA_BOOTHVERBOSE(
4140 				device_printf(devinfo->dev,
4141 				    " Disabling nid %d due to it's"
4142 				    " non-audio type.\n",
4143 				    w->nid);
4144 			);
4145 		}
4146 	}
4147 }
4148 
4149 static void
4150 hdaa_audio_disable_useless(struct hdaa_devinfo *devinfo)
4151 {
4152 	struct hdaa_widget *w, *cw;
4153 	struct hdaa_audio_ctl *ctl;
4154 	int done, found, i, j, k;
4155 
4156 	/* Disable useless pins. */
4157 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4158 		w = hdaa_widget_get(devinfo, i);
4159 		if (w == NULL || w->enable == 0)
4160 			continue;
4161 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
4162 			if ((w->wclass.pin.config &
4163 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
4164 			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
4165 				w->enable = 0;
4166 				HDA_BOOTHVERBOSE(
4167 					device_printf(devinfo->dev,
4168 					    " Disabling pin nid %d due"
4169 					    " to None connectivity.\n",
4170 					    w->nid);
4171 				);
4172 			} else if ((w->wclass.pin.config &
4173 			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
4174 				w->enable = 0;
4175 				HDA_BOOTHVERBOSE(
4176 					device_printf(devinfo->dev,
4177 					    " Disabling unassociated"
4178 					    " pin nid %d.\n",
4179 					    w->nid);
4180 				);
4181 			}
4182 		}
4183 	}
4184 	do {
4185 		done = 1;
4186 		/* Disable and mute controls for disabled widgets. */
4187 		i = 0;
4188 		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4189 			if (ctl->enable == 0)
4190 				continue;
4191 			if (ctl->widget->enable == 0 ||
4192 			    (ctl->childwidget != NULL &&
4193 			    ctl->childwidget->enable == 0)) {
4194 				ctl->forcemute = 1;
4195 				ctl->muted = HDAA_AMP_MUTE_ALL;
4196 				ctl->left = 0;
4197 				ctl->right = 0;
4198 				ctl->enable = 0;
4199 				if (ctl->ndir == HDAA_CTL_IN)
4200 					ctl->widget->connsenable[ctl->index] = 0;
4201 				done = 0;
4202 				HDA_BOOTHVERBOSE(
4203 					device_printf(devinfo->dev,
4204 					    " Disabling ctl %d nid %d cnid %d due"
4205 					    " to disabled widget.\n", i,
4206 					    ctl->widget->nid,
4207 					    (ctl->childwidget != NULL)?
4208 					    ctl->childwidget->nid:-1);
4209 				);
4210 			}
4211 		}
4212 		/* Disable useless widgets. */
4213 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4214 			w = hdaa_widget_get(devinfo, i);
4215 			if (w == NULL || w->enable == 0)
4216 				continue;
4217 			/* Disable inputs with disabled child widgets. */
4218 			for (j = 0; j < w->nconns; j++) {
4219 				if (w->connsenable[j]) {
4220 					cw = hdaa_widget_get(devinfo, w->conns[j]);
4221 					if (cw == NULL || cw->enable == 0) {
4222 						w->connsenable[j] = 0;
4223 						HDA_BOOTHVERBOSE(
4224 							device_printf(devinfo->dev,
4225 							    " Disabling nid %d connection %d due"
4226 							    " to disabled child widget.\n",
4227 							    i, j);
4228 						);
4229 					}
4230 				}
4231 			}
4232 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4233 			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4234 				continue;
4235 			/* Disable mixers and selectors without inputs. */
4236 			found = 0;
4237 			for (j = 0; j < w->nconns; j++) {
4238 				if (w->connsenable[j]) {
4239 					found = 1;
4240 					break;
4241 				}
4242 			}
4243 			if (found == 0) {
4244 				w->enable = 0;
4245 				done = 0;
4246 				HDA_BOOTHVERBOSE(
4247 					device_printf(devinfo->dev,
4248 					    " Disabling nid %d due to all it's"
4249 					    " inputs disabled.\n", w->nid);
4250 				);
4251 			}
4252 			/* Disable nodes without consumers. */
4253 			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4254 			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4255 				continue;
4256 			found = 0;
4257 			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4258 				cw = hdaa_widget_get(devinfo, k);
4259 				if (cw == NULL || cw->enable == 0)
4260 					continue;
4261 				for (j = 0; j < cw->nconns; j++) {
4262 					if (cw->connsenable[j] && cw->conns[j] == i) {
4263 						found = 1;
4264 						break;
4265 					}
4266 				}
4267 			}
4268 			if (found == 0) {
4269 				w->enable = 0;
4270 				done = 0;
4271 				HDA_BOOTHVERBOSE(
4272 					device_printf(devinfo->dev,
4273 					    " Disabling nid %d due to all it's"
4274 					    " consumers disabled.\n", w->nid);
4275 				);
4276 			}
4277 		}
4278 	} while (done == 0);
4279 
4280 }
4281 
4282 static void
4283 hdaa_audio_disable_unas(struct hdaa_devinfo *devinfo)
4284 {
4285 	struct hdaa_audio_as *as = devinfo->as;
4286 	struct hdaa_widget *w, *cw;
4287 	struct hdaa_audio_ctl *ctl;
4288 	int i, j, k;
4289 
4290 	/* Disable unassosiated widgets. */
4291 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4292 		w = hdaa_widget_get(devinfo, i);
4293 		if (w == NULL || w->enable == 0)
4294 			continue;
4295 		if (w->bindas == -1) {
4296 			w->enable = 0;
4297 			HDA_BOOTHVERBOSE(
4298 				device_printf(devinfo->dev,
4299 				    " Disabling unassociated nid %d.\n",
4300 				    w->nid);
4301 			);
4302 		}
4303 	}
4304 	/* Disable input connections on input pin and
4305 	 * output on output. */
4306 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4307 		w = hdaa_widget_get(devinfo, i);
4308 		if (w == NULL || w->enable == 0)
4309 			continue;
4310 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4311 			continue;
4312 		if (w->bindas < 0)
4313 			continue;
4314 		if (as[w->bindas].dir == HDAA_CTL_IN) {
4315 			for (j = 0; j < w->nconns; j++) {
4316 				if (w->connsenable[j] == 0)
4317 					continue;
4318 				w->connsenable[j] = 0;
4319 				HDA_BOOTHVERBOSE(
4320 					device_printf(devinfo->dev,
4321 					    " Disabling connection to input pin "
4322 					    "nid %d conn %d.\n",
4323 					    i, j);
4324 				);
4325 			}
4326 			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4327 			    HDAA_CTL_IN, -1, 1);
4328 			if (ctl && ctl->enable) {
4329 				ctl->forcemute = 1;
4330 				ctl->muted = HDAA_AMP_MUTE_ALL;
4331 				ctl->left = 0;
4332 				ctl->right = 0;
4333 				ctl->enable = 0;
4334 			}
4335 		} else {
4336 			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4337 			    HDAA_CTL_OUT, -1, 1);
4338 			if (ctl && ctl->enable) {
4339 				ctl->forcemute = 1;
4340 				ctl->muted = HDAA_AMP_MUTE_ALL;
4341 				ctl->left = 0;
4342 				ctl->right = 0;
4343 				ctl->enable = 0;
4344 			}
4345 			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4346 				cw = hdaa_widget_get(devinfo, k);
4347 				if (cw == NULL || cw->enable == 0)
4348 					continue;
4349 				for (j = 0; j < cw->nconns; j++) {
4350 					if (cw->connsenable[j] && cw->conns[j] == i) {
4351 						cw->connsenable[j] = 0;
4352 						HDA_BOOTHVERBOSE(
4353 							device_printf(devinfo->dev,
4354 							    " Disabling connection from output pin "
4355 							    "nid %d conn %d cnid %d.\n",
4356 							    k, j, i);
4357 						);
4358 						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4359 						    cw->nconns > 1)
4360 							continue;
4361 						ctl = hdaa_audio_ctl_amp_get(devinfo, k,
4362 					    HDAA_CTL_IN, j, 1);
4363 						if (ctl && ctl->enable) {
4364 							ctl->forcemute = 1;
4365 							ctl->muted = HDAA_AMP_MUTE_ALL;
4366 							ctl->left = 0;
4367 							ctl->right = 0;
4368 							ctl->enable = 0;
4369 						}
4370 					}
4371 				}
4372 			}
4373 		}
4374 	}
4375 }
4376 
4377 static void
4378 hdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo)
4379 {
4380 	struct hdaa_audio_as *as = devinfo->as;
4381 	struct hdaa_widget *w;
4382 	int i, j;
4383 
4384 	/* On playback path we can safely disable all unseleted inputs. */
4385 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4386 		w = hdaa_widget_get(devinfo, i);
4387 		if (w == NULL || w->enable == 0)
4388 			continue;
4389 		if (w->nconns <= 1)
4390 			continue;
4391 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4392 			continue;
4393 		if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN)
4394 			continue;
4395 		for (j = 0; j < w->nconns; j++) {
4396 			if (w->connsenable[j] == 0)
4397 				continue;
4398 			if (w->selconn < 0 || w->selconn == j)
4399 				continue;
4400 			w->connsenable[j] = 0;
4401 			HDA_BOOTHVERBOSE(
4402 				device_printf(devinfo->dev,
4403 				    " Disabling unselected connection "
4404 				    "nid %d conn %d.\n",
4405 				    i, j);
4406 			);
4407 		}
4408 	}
4409 }
4410 
4411 static void
4412 hdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo)
4413 {
4414 	struct hdaa_audio_as *ases = devinfo->as;
4415 	struct hdaa_widget *w, *cw;
4416 	struct hdaa_audio_ctl *ctl;
4417 	int i, j;
4418 
4419 	/* Disable crossassociatement and unwanted crosschannel connections. */
4420 	/* ... using selectors */
4421 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4422 		w = hdaa_widget_get(devinfo, i);
4423 		if (w == NULL || w->enable == 0)
4424 			continue;
4425 		if (w->nconns <= 1)
4426 			continue;
4427 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4428 			continue;
4429 		/* Allow any -> mix */
4430 		if (w->bindas == -2)
4431 			continue;
4432 		for (j = 0; j < w->nconns; j++) {
4433 			if (w->connsenable[j] == 0)
4434 				continue;
4435 			cw = hdaa_widget_get(devinfo, w->conns[j]);
4436 			if (cw == NULL || w->enable == 0)
4437 				continue;
4438 			/* Allow mix -> out. */
4439 			if (cw->bindas == -2 && w->bindas >= 0 &&
4440 			    ases[w->bindas].dir == HDAA_CTL_OUT)
4441 				continue;
4442 			/* Allow mix -> mixed-in. */
4443 			if (cw->bindas == -2 && w->bindas >= 0 &&
4444 			    ases[w->bindas].mixed)
4445 				continue;
4446 			/* Allow in -> mix. */
4447 			if ((w->pflags & HDAA_ADC_MONITOR) &&
4448 			     cw->bindas >= 0 &&
4449 			     ases[cw->bindas].dir == HDAA_CTL_IN)
4450 				continue;
4451 			/* Allow if have common as/seqs. */
4452 			if (w->bindas == cw->bindas &&
4453 			    (w->bindseqmask & cw->bindseqmask) != 0)
4454 				continue;
4455 			w->connsenable[j] = 0;
4456 			HDA_BOOTHVERBOSE(
4457 				device_printf(devinfo->dev,
4458 				    " Disabling crossassociatement connection "
4459 				    "nid %d conn %d cnid %d.\n",
4460 				    i, j, cw->nid);
4461 			);
4462 		}
4463 	}
4464 	/* ... using controls */
4465 	i = 0;
4466 	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4467 		if (ctl->enable == 0 || ctl->childwidget == NULL)
4468 			continue;
4469 		/* Allow any -> mix */
4470 		if (ctl->widget->bindas == -2)
4471 			continue;
4472 		/* Allow mix -> out. */
4473 		if (ctl->childwidget->bindas == -2 &&
4474 		    ctl->widget->bindas >= 0 &&
4475 		    ases[ctl->widget->bindas].dir == HDAA_CTL_OUT)
4476 			continue;
4477 		/* Allow mix -> mixed-in. */
4478 		if (ctl->childwidget->bindas == -2 &&
4479 		    ctl->widget->bindas >= 0 &&
4480 		    ases[ctl->widget->bindas].mixed)
4481 			continue;
4482 		/* Allow in -> mix. */
4483 		if ((ctl->widget->pflags & HDAA_ADC_MONITOR) &&
4484 		    ctl->childwidget->bindas >= 0 &&
4485 		    ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN)
4486 			continue;
4487 		/* Allow if have common as/seqs. */
4488 		if (ctl->widget->bindas == ctl->childwidget->bindas &&
4489 		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0)
4490 			continue;
4491 		ctl->forcemute = 1;
4492 		ctl->muted = HDAA_AMP_MUTE_ALL;
4493 		ctl->left = 0;
4494 		ctl->right = 0;
4495 		ctl->enable = 0;
4496 		if (ctl->ndir == HDAA_CTL_IN)
4497 			ctl->widget->connsenable[ctl->index] = 0;
4498 		HDA_BOOTHVERBOSE(
4499 			device_printf(devinfo->dev,
4500 			    " Disabling crossassociatement connection "
4501 			    "ctl %d nid %d cnid %d.\n", i,
4502 			    ctl->widget->nid,
4503 			    ctl->childwidget->nid);
4504 		);
4505 	}
4506 
4507 }
4508 
4509 /*
4510  * Find controls to control amplification for source and calculate possible
4511  * amplification range.
4512  */
4513 static int
4514 hdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4515     int ossdev, int ctlable, int depth, int *minamp, int *maxamp)
4516 {
4517 	struct hdaa_widget *w, *wc;
4518 	struct hdaa_audio_ctl *ctl;
4519 	int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4520 
4521 	if (depth > HDA_PARSE_MAXDEPTH)
4522 		return (found);
4523 
4524 	w = hdaa_widget_get(devinfo, nid);
4525 	if (w == NULL || w->enable == 0)
4526 		return (found);
4527 
4528 	/* Count number of active inputs. */
4529 	if (depth > 0) {
4530 		for (j = 0; j < w->nconns; j++) {
4531 			if (!w->connsenable[j])
4532 				continue;
4533 			conns++;
4534 		}
4535 	}
4536 
4537 	/* If this is not a first step - use input mixer.
4538 	   Pins have common input ctl so care must be taken. */
4539 	if (depth > 0 && ctlable && (conns == 1 ||
4540 	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
4541 		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
4542 		    index, 1);
4543 		if (ctl) {
4544 			ctl->ossmask |= (1 << ossdev);
4545 			found++;
4546 			if (*minamp == *maxamp) {
4547 				*minamp += MINQDB(ctl);
4548 				*maxamp += MAXQDB(ctl);
4549 			}
4550 		}
4551 	}
4552 
4553 	/* If widget has own ossdev - not traverse it.
4554 	   It will be traversed on its own. */
4555 	if (w->ossdev >= 0 && depth > 0)
4556 		return (found);
4557 
4558 	/* We must not traverse pin */
4559 	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4560 	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
4561 	    depth > 0)
4562 		return (found);
4563 
4564 	/* record that this widget exports such signal, */
4565 	w->ossmask |= (1 << ossdev);
4566 
4567 	/*
4568 	 * If signals mixed, we can't assign controls farther.
4569 	 * Ignore this on depth zero. Caller must knows why.
4570 	 */
4571 	if (conns > 1 &&
4572 	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4573 		ctlable = 0;
4574 
4575 	if (ctlable) {
4576 		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
4577 		if (ctl) {
4578 			ctl->ossmask |= (1 << ossdev);
4579 			found++;
4580 			if (*minamp == *maxamp) {
4581 				*minamp += MINQDB(ctl);
4582 				*maxamp += MAXQDB(ctl);
4583 			}
4584 		}
4585 	}
4586 
4587 	cminamp = cmaxamp = 0;
4588 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4589 		wc = hdaa_widget_get(devinfo, i);
4590 		if (wc == NULL || wc->enable == 0)
4591 			continue;
4592 		for (j = 0; j < wc->nconns; j++) {
4593 			if (wc->connsenable[j] && wc->conns[j] == nid) {
4594 				tminamp = tmaxamp = 0;
4595 				found += hdaa_audio_ctl_source_amp(devinfo,
4596 				    wc->nid, j, ossdev, ctlable, depth + 1,
4597 				    &tminamp, &tmaxamp);
4598 				if (cminamp == 0 && cmaxamp == 0) {
4599 					cminamp = tminamp;
4600 					cmaxamp = tmaxamp;
4601 				} else if (tminamp != tmaxamp) {
4602 					cminamp = imax(cminamp, tminamp);
4603 					cmaxamp = imin(cmaxamp, tmaxamp);
4604 				}
4605 			}
4606 		}
4607 	}
4608 	if (*minamp == *maxamp && cminamp < cmaxamp) {
4609 		*minamp += cminamp;
4610 		*maxamp += cmaxamp;
4611 	}
4612 	return (found);
4613 }
4614 
4615 /*
4616  * Find controls to control amplification for destination and calculate
4617  * possible amplification range.
4618  */
4619 static int
4620 hdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4621     int ossdev, int depth, int *minamp, int *maxamp)
4622 {
4623 	struct hdaa_audio_as *as = devinfo->as;
4624 	struct hdaa_widget *w, *wc;
4625 	struct hdaa_audio_ctl *ctl;
4626 	int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4627 
4628 	if (depth > HDA_PARSE_MAXDEPTH)
4629 		return (found);
4630 
4631 	w = hdaa_widget_get(devinfo, nid);
4632 	if (w == NULL || w->enable == 0)
4633 		return (found);
4634 
4635 	if (depth > 0) {
4636 		/* If this node produce output for several consumers,
4637 		   we can't touch it. */
4638 		consumers = 0;
4639 		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4640 			wc = hdaa_widget_get(devinfo, i);
4641 			if (wc == NULL || wc->enable == 0)
4642 				continue;
4643 			for (j = 0; j < wc->nconns; j++) {
4644 				if (wc->connsenable[j] && wc->conns[j] == nid)
4645 					consumers++;
4646 			}
4647 		}
4648 		/* The only exception is if real HP redirection is configured
4649 		   and this is a duplication point.
4650 		   XXX: Actually exception is not completely correct.
4651 		   XXX: Duplication point check is not perfect. */
4652 		if ((consumers == 2 && (w->bindas < 0 ||
4653 		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
4654 		    (w->bindseqmask & (1 << 15)) == 0)) ||
4655 		    consumers > 2)
4656 			return (found);
4657 
4658 		/* Else use it's output mixer. */
4659 		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4660 		    HDAA_CTL_OUT, -1, 1);
4661 		if (ctl) {
4662 			ctl->ossmask |= (1 << ossdev);
4663 			found++;
4664 			if (*minamp == *maxamp) {
4665 				*minamp += MINQDB(ctl);
4666 				*maxamp += MAXQDB(ctl);
4667 			}
4668 		}
4669 	}
4670 
4671 	/* We must not traverse pin */
4672 	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4673 	    depth > 0)
4674 		return (found);
4675 
4676 	cminamp = cmaxamp = 0;
4677 	for (i = 0; i < w->nconns; i++) {
4678 		if (w->connsenable[i] == 0)
4679 			continue;
4680 		if (index >= 0 && i != index)
4681 			continue;
4682 		tminamp = tmaxamp = 0;
4683 		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4684 		    HDAA_CTL_IN, i, 1);
4685 		if (ctl) {
4686 			ctl->ossmask |= (1 << ossdev);
4687 			found++;
4688 			if (*minamp == *maxamp) {
4689 				tminamp += MINQDB(ctl);
4690 				tmaxamp += MAXQDB(ctl);
4691 			}
4692 		}
4693 		found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
4694 		    depth + 1, &tminamp, &tmaxamp);
4695 		if (cminamp == 0 && cmaxamp == 0) {
4696 			cminamp = tminamp;
4697 			cmaxamp = tmaxamp;
4698 		} else if (tminamp != tmaxamp) {
4699 			cminamp = imax(cminamp, tminamp);
4700 			cmaxamp = imin(cmaxamp, tmaxamp);
4701 		}
4702 	}
4703 	if (*minamp == *maxamp && cminamp < cmaxamp) {
4704 		*minamp += cminamp;
4705 		*maxamp += cmaxamp;
4706 	}
4707 	return (found);
4708 }
4709 
4710 /*
4711  * Assign OSS names to sound sources
4712  */
4713 static void
4714 hdaa_audio_assign_names(struct hdaa_devinfo *devinfo)
4715 {
4716 	struct hdaa_audio_as *as = devinfo->as;
4717 	struct hdaa_widget *w;
4718 	int i, j;
4719 	int type = -1, use, used = 0;
4720 	static const int types[7][13] = {
4721 	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4722 	      SOUND_MIXER_LINE3, -1 },	/* line */
4723 	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
4724 	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
4725 	    { SOUND_MIXER_CD, -1 },	/* cd */
4726 	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
4727 	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
4728 	      -1 },	/* digital */
4729 	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4730 	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
4731 	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
4732 	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
4733 	      -1 }	/* others */
4734 	};
4735 
4736 	/* Surely known names */
4737 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4738 		w = hdaa_widget_get(devinfo, i);
4739 		if (w == NULL || w->enable == 0)
4740 			continue;
4741 		if (w->bindas == -1)
4742 			continue;
4743 		use = -1;
4744 		switch (w->type) {
4745 		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4746 			if (as[w->bindas].dir == HDAA_CTL_OUT)
4747 				break;
4748 			type = -1;
4749 			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4750 			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4751 				type = 0;
4752 				break;
4753 			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4754 				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
4755 				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4756 					break;
4757 				type = 1;
4758 				break;
4759 			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4760 				type = 3;
4761 				break;
4762 			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4763 				type = 4;
4764 				break;
4765 			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
4766 			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
4767 				type = 5;
4768 				break;
4769 			}
4770 			if (type == -1)
4771 				break;
4772 			j = 0;
4773 			while (types[type][j] >= 0 &&
4774 			    (used & (1 << types[type][j])) != 0) {
4775 				j++;
4776 			}
4777 			if (types[type][j] >= 0)
4778 				use = types[type][j];
4779 			break;
4780 		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4781 			use = SOUND_MIXER_PCM;
4782 			break;
4783 		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
4784 			use = SOUND_MIXER_SPEAKER;
4785 			break;
4786 		default:
4787 			break;
4788 		}
4789 		if (use >= 0) {
4790 			w->ossdev = use;
4791 			used |= (1 << use);
4792 		}
4793 	}
4794 	/* Semi-known names */
4795 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4796 		w = hdaa_widget_get(devinfo, i);
4797 		if (w == NULL || w->enable == 0)
4798 			continue;
4799 		if (w->ossdev >= 0)
4800 			continue;
4801 		if (w->bindas == -1)
4802 			continue;
4803 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4804 			continue;
4805 		if (as[w->bindas].dir == HDAA_CTL_OUT)
4806 			continue;
4807 		type = -1;
4808 		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4809 		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
4810 		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4811 		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
4812 		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
4813 			type = 0;
4814 			break;
4815 		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4816 			type = 2;
4817 			break;
4818 		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
4819 		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
4820 			type = 5;
4821 			break;
4822 		}
4823 		if (type == -1)
4824 			break;
4825 		j = 0;
4826 		while (types[type][j] >= 0 &&
4827 		    (used & (1 << types[type][j])) != 0) {
4828 			j++;
4829 		}
4830 		if (types[type][j] >= 0) {
4831 			w->ossdev = types[type][j];
4832 			used |= (1 << types[type][j]);
4833 		}
4834 	}
4835 	/* Others */
4836 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4837 		w = hdaa_widget_get(devinfo, i);
4838 		if (w == NULL || w->enable == 0)
4839 			continue;
4840 		if (w->ossdev >= 0)
4841 			continue;
4842 		if (w->bindas == -1)
4843 			continue;
4844 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4845 			continue;
4846 		if (as[w->bindas].dir == HDAA_CTL_OUT)
4847 			continue;
4848 		j = 0;
4849 		while (types[6][j] >= 0 &&
4850 		    (used & (1 << types[6][j])) != 0) {
4851 			j++;
4852 		}
4853 		if (types[6][j] >= 0) {
4854 			w->ossdev = types[6][j];
4855 			used |= (1 << types[6][j]);
4856 		}
4857 	}
4858 }
4859 
4860 static void
4861 hdaa_audio_build_tree(struct hdaa_devinfo *devinfo)
4862 {
4863 	struct hdaa_audio_as *as = devinfo->as;
4864 	int j, res;
4865 
4866 	/* Trace all associations in order of their numbers. */
4867 	for (j = 0; j < devinfo->ascnt; j++) {
4868 		if (as[j].enable == 0)
4869 			continue;
4870 		HDA_BOOTVERBOSE(
4871 			device_printf(devinfo->dev,
4872 			    "Tracing association %d (%d)\n", j, as[j].index);
4873 		);
4874 		if (as[j].dir == HDAA_CTL_OUT) {
4875 retry:
4876 			res = hdaa_audio_trace_as_out(devinfo, j, 0);
4877 			if (res == 0 && as[j].hpredir >= 0 &&
4878 			    as[j].fakeredir == 0) {
4879 				/* If CODEC can't do analog HP redirection
4880 				   try to make it using one more DAC. */
4881 				as[j].fakeredir = 1;
4882 				goto retry;
4883 			}
4884 		} else if (as[j].mixed)
4885 			res = hdaa_audio_trace_as_in(devinfo, j);
4886 		else
4887 			res = hdaa_audio_trace_as_in_mch(devinfo, j, 0);
4888 		if (res) {
4889 			HDA_BOOTVERBOSE(
4890 				device_printf(devinfo->dev,
4891 				    "Association %d (%d) trace succeeded\n",
4892 				    j, as[j].index);
4893 			);
4894 		} else {
4895 			HDA_BOOTVERBOSE(
4896 				device_printf(devinfo->dev,
4897 				    "Association %d (%d) trace failed\n",
4898 				    j, as[j].index);
4899 			);
4900 			as[j].enable = 0;
4901 		}
4902 	}
4903 
4904 	/* Look for additional DACs/ADCs. */
4905 	for (j = 0; j < devinfo->ascnt; j++) {
4906 		if (as[j].enable == 0)
4907 			continue;
4908 		hdaa_audio_adddac(devinfo, j);
4909 	}
4910 
4911 	/* Trace mixer and beeper pseudo associations. */
4912 	hdaa_audio_trace_as_extra(devinfo);
4913 }
4914 
4915 /*
4916  * Store in pdevinfo new data about whether and how we can control signal
4917  * for OSS device to/from specified widget.
4918  */
4919 static void
4920 hdaa_adjust_amp(struct hdaa_widget *w, int ossdev,
4921     int found, int minamp, int maxamp)
4922 {
4923 	struct hdaa_devinfo *devinfo = w->devinfo;
4924 	struct hdaa_pcm_devinfo *pdevinfo;
4925 
4926 	if (w->bindas >= 0)
4927 		pdevinfo = devinfo->as[w->bindas].pdevinfo;
4928 	else
4929 		pdevinfo = &devinfo->devs[0];
4930 	if (found)
4931 		pdevinfo->ossmask |= (1 << ossdev);
4932 	if (minamp == 0 && maxamp == 0)
4933 		return;
4934 	if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) {
4935 		pdevinfo->minamp[ossdev] = minamp;
4936 		pdevinfo->maxamp[ossdev] = maxamp;
4937 	} else {
4938 		pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp);
4939 		pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp);
4940 	}
4941 }
4942 
4943 /*
4944  * Trace signals from/to all possible sources/destionstions to find possible
4945  * recording sources, OSS device control ranges and to assign controls.
4946  */
4947 static void
4948 hdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo)
4949 {
4950 	struct hdaa_audio_as *as = devinfo->as;
4951 	struct hdaa_widget *w, *cw;
4952 	int i, j, minamp, maxamp, found;
4953 
4954 	/* Assign mixers to the tree. */
4955 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4956 		w = hdaa_widget_get(devinfo, i);
4957 		if (w == NULL || w->enable == 0)
4958 			continue;
4959 		minamp = maxamp = 0;
4960 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
4961 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
4962 		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4963 		    as[w->bindas].dir == HDAA_CTL_IN)) {
4964 			if (w->ossdev < 0)
4965 				continue;
4966 			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4967 			    w->ossdev, 1, 0, &minamp, &maxamp);
4968 			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4969 		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
4970 			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4971 			    SOUND_MIXER_RECLEV, 0, &minamp, &maxamp);
4972 			hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp);
4973 		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4974 		    as[w->bindas].dir == HDAA_CTL_OUT) {
4975 			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4976 			    SOUND_MIXER_VOLUME, 0, &minamp, &maxamp);
4977 			hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp);
4978 		}
4979 		if (w->ossdev == SOUND_MIXER_IMIX) {
4980 			minamp = maxamp = 0;
4981 			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4982 			    w->ossdev, 1, 0, &minamp, &maxamp);
4983 			if (minamp == maxamp) {
4984 				/* If we are unable to control input monitor
4985 				   as source - try to control it as destination. */
4986 				found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4987 				    w->ossdev, 0, &minamp, &maxamp);
4988 				w->pflags |= HDAA_IMIX_AS_DST;
4989 			}
4990 			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4991 		}
4992 		if (w->pflags & HDAA_ADC_MONITOR) {
4993 			for (j = 0; j < w->nconns; j++) {
4994 				if (!w->connsenable[j])
4995 				    continue;
4996 				cw = hdaa_widget_get(devinfo, w->conns[j]);
4997 				if (cw == NULL || cw->enable == 0)
4998 				    continue;
4999 				if (cw->bindas == -1)
5000 				    continue;
5001 				if (cw->bindas >= 0 &&
5002 				    as[cw->bindas].dir != HDAA_CTL_IN)
5003 					continue;
5004 				minamp = maxamp = 0;
5005 				found = hdaa_audio_ctl_dest_amp(devinfo,
5006 				    w->nid, j, SOUND_MIXER_IGAIN, 0,
5007 				    &minamp, &maxamp);
5008 				hdaa_adjust_amp(w, SOUND_MIXER_IGAIN,
5009 				    found, minamp, maxamp);
5010 			}
5011 		}
5012 	}
5013 }
5014 
5015 static void
5016 hdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo)
5017 {
5018 	struct hdaa_audio_as *as = devinfo->as;
5019 	struct hdaa_widget *w;
5020 	uint32_t pincap;
5021 	int i;
5022 
5023 	for (i = 0; i < devinfo->nodecnt; i++) {
5024 		w = &devinfo->widget[i];
5025 		if (w == NULL)
5026 			continue;
5027 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
5028 		    w->waspin == 0)
5029 			continue;
5030 
5031 		pincap = w->wclass.pin.cap;
5032 
5033 		/* Disable everything. */
5034 		w->wclass.pin.ctrl &= ~(
5035 		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
5036 		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
5037 		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
5038 		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
5039 
5040 		if (w->enable == 0) {
5041 			/* Pin is unused so left it disabled. */
5042 			continue;
5043 		} else if (w->waspin) {
5044 			/* Enable input for beeper input. */
5045 			w->wclass.pin.ctrl |=
5046 			    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5047 		} else if (w->bindas < 0 || as[w->bindas].enable == 0) {
5048 			/* Pin is unused so left it disabled. */
5049 			continue;
5050 		} else if (as[w->bindas].dir == HDAA_CTL_IN) {
5051 			/* Input pin, configure for input. */
5052 			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5053 				w->wclass.pin.ctrl |=
5054 				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5055 
5056 			if ((devinfo->quirks & HDAA_QUIRK_IVREF100) &&
5057 			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5058 				w->wclass.pin.ctrl |=
5059 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5060 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5061 			else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) &&
5062 			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5063 				w->wclass.pin.ctrl |=
5064 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5065 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5066 			else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) &&
5067 			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5068 				w->wclass.pin.ctrl |=
5069 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5070 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5071 		} else {
5072 			/* Output pin, configure for output. */
5073 			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5074 				w->wclass.pin.ctrl |=
5075 				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
5076 
5077 			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
5078 			    (w->wclass.pin.config &
5079 			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
5080 			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
5081 				w->wclass.pin.ctrl |=
5082 				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
5083 
5084 			if ((devinfo->quirks & HDAA_QUIRK_OVREF100) &&
5085 			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5086 				w->wclass.pin.ctrl |=
5087 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5088 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5089 			else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) &&
5090 			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5091 				w->wclass.pin.ctrl |=
5092 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5093 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5094 			else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) &&
5095 			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5096 				w->wclass.pin.ctrl |=
5097 				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5098 				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5099 		}
5100 	}
5101 }
5102 
5103 static void
5104 hdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo)
5105 {
5106 	struct hdaa_audio_ctl *ctl;
5107 	int i, z;
5108 
5109 	i = 0;
5110 	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5111 		if (ctl->enable == 0 || ctl->ossmask != 0) {
5112 			/* Mute disabled and mixer controllable controls.
5113 			 * Last will be initialized by mixer_init().
5114 			 * This expected to reduce click on startup. */
5115 			hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0);
5116 			continue;
5117 		}
5118 		/* Init fixed controls to 0dB amplification. */
5119 		z = ctl->offset;
5120 		if (z > ctl->step)
5121 			z = ctl->step;
5122 		hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z);
5123 	}
5124 }
5125 
5126 static void
5127 hdaa_gpio_commit(struct hdaa_devinfo *devinfo)
5128 {
5129 	uint32_t gdata, gmask, gdir;
5130 	int i, numgpio;
5131 
5132 	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5133 	if (devinfo->gpio != 0 && numgpio != 0) {
5134 		gdata = hda_command(devinfo->dev,
5135 		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
5136 		gmask = hda_command(devinfo->dev,
5137 		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
5138 		gdir = hda_command(devinfo->dev,
5139 		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
5140 		for (i = 0; i < numgpio; i++) {
5141 			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5142 			    HDAA_GPIO_SET(i)) {
5143 				gdata |= (1 << i);
5144 				gmask |= (1 << i);
5145 				gdir |= (1 << i);
5146 			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5147 			    HDAA_GPIO_CLEAR(i)) {
5148 				gdata &= ~(1 << i);
5149 				gmask |= (1 << i);
5150 				gdir |= (1 << i);
5151 			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5152 			    HDAA_GPIO_DISABLE(i)) {
5153 				gmask &= ~(1 << i);
5154 			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5155 			    HDAA_GPIO_INPUT(i)) {
5156 				gmask |= (1 << i);
5157 				gdir &= ~(1 << i);
5158 			}
5159 		}
5160 		HDA_BOOTVERBOSE(
5161 			device_printf(devinfo->dev, "GPIO commit\n");
5162 		);
5163 		hda_command(devinfo->dev,
5164 		    HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask));
5165 		hda_command(devinfo->dev,
5166 		    HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir));
5167 		hda_command(devinfo->dev,
5168 		    HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata));
5169 		HDA_BOOTVERBOSE(
5170 			hdaa_dump_gpio(devinfo);
5171 		);
5172 	}
5173 }
5174 
5175 static void
5176 hdaa_gpo_commit(struct hdaa_devinfo *devinfo)
5177 {
5178 	uint32_t gdata;
5179 	int i, numgpo;
5180 
5181 	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
5182 	if (devinfo->gpo != 0 && numgpo != 0) {
5183 		gdata = hda_command(devinfo->dev,
5184 		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
5185 		for (i = 0; i < numgpo; i++) {
5186 			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5187 			    HDAA_GPIO_SET(i)) {
5188 				gdata |= (1 << i);
5189 			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5190 			    HDAA_GPIO_CLEAR(i)) {
5191 				gdata &= ~(1 << i);
5192 			}
5193 		}
5194 		HDA_BOOTVERBOSE(
5195 			device_printf(devinfo->dev, "GPO commit\n");
5196 		);
5197 		hda_command(devinfo->dev,
5198 		    HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata));
5199 		HDA_BOOTVERBOSE(
5200 			hdaa_dump_gpo(devinfo);
5201 		);
5202 	}
5203 }
5204 
5205 static void
5206 hdaa_audio_commit(struct hdaa_devinfo *devinfo)
5207 {
5208 	struct hdaa_widget *w;
5209 	int i;
5210 
5211 	/* Commit controls. */
5212 	hdaa_audio_ctl_commit(devinfo);
5213 
5214 	/* Commit selectors, pins and EAPD. */
5215 	for (i = 0; i < devinfo->nodecnt; i++) {
5216 		w = &devinfo->widget[i];
5217 		if (w == NULL)
5218 			continue;
5219 		if (w->selconn == -1)
5220 			w->selconn = 0;
5221 		if (w->nconns > 0)
5222 			hdaa_widget_connection_select(w, w->selconn);
5223 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
5224 		    w->waspin) {
5225 			hda_command(devinfo->dev,
5226 			    HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid,
5227 			    w->wclass.pin.ctrl));
5228 		}
5229 		if (w->param.eapdbtl != HDA_INVALID) {
5230 			uint32_t val;
5231 
5232 			val = w->param.eapdbtl;
5233 			if (devinfo->quirks &
5234 			    HDAA_QUIRK_EAPDINV)
5235 				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
5236 			hda_command(devinfo->dev,
5237 			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid,
5238 			    val));
5239 		}
5240 	}
5241 
5242 	hdaa_gpio_commit(devinfo);
5243 	hdaa_gpo_commit(devinfo);
5244 }
5245 
5246 static void
5247 hdaa_powerup(struct hdaa_devinfo *devinfo)
5248 {
5249 	int i;
5250 
5251 	hda_command(devinfo->dev,
5252 	    HDA_CMD_SET_POWER_STATE(0,
5253 	    devinfo->nid, HDA_CMD_POWER_STATE_D0));
5254 	DELAY(100);
5255 
5256 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5257 		hda_command(devinfo->dev,
5258 		    HDA_CMD_SET_POWER_STATE(0,
5259 		    i, HDA_CMD_POWER_STATE_D0));
5260 	}
5261 	DELAY(1000);
5262 }
5263 
5264 static int
5265 hdaa_pcmchannel_setup(struct hdaa_chan *ch)
5266 {
5267 	struct hdaa_devinfo *devinfo = ch->devinfo;
5268 	struct hdaa_audio_as *as = devinfo->as;
5269 	struct hdaa_widget *w;
5270 	uint32_t cap, fmtcap, pcmcap;
5271 	int i, j, ret, channels, onlystereo;
5272 	uint16_t pinset;
5273 
5274 	ch->caps = hdaa_caps;
5275 	ch->caps.fmtlist = ch->fmtlist;
5276 	ch->bit16 = 1;
5277 	ch->bit32 = 0;
5278 	ch->pcmrates[0] = 48000;
5279 	ch->pcmrates[1] = 0;
5280 	ch->stripecap = 0xff;
5281 
5282 	ret = 0;
5283 	channels = 0;
5284 	onlystereo = 1;
5285 	pinset = 0;
5286 	fmtcap = devinfo->supp_stream_formats;
5287 	pcmcap = devinfo->supp_pcm_size_rate;
5288 
5289 	for (i = 0; i < 16; i++) {
5290 		/* Check as is correct */
5291 		if (ch->as < 0)
5292 			break;
5293 		/* Cound only present DACs */
5294 		if (as[ch->as].dacs[ch->asindex][i] <= 0)
5295 			continue;
5296 		/* Ignore duplicates */
5297 		for (j = 0; j < ret; j++) {
5298 			if (ch->io[j] == as[ch->as].dacs[ch->asindex][i])
5299 				break;
5300 		}
5301 		if (j < ret)
5302 			continue;
5303 
5304 		w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]);
5305 		if (w == NULL || w->enable == 0)
5306 			continue;
5307 		cap = w->param.supp_stream_formats;
5308 		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
5309 		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5310 			continue;
5311 		/* Many CODECs does not declare AC3 support on SPDIF.
5312 		   I don't beleave that they doesn't support it! */
5313 		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5314 			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
5315 		if (ret == 0) {
5316 			fmtcap = cap;
5317 			pcmcap = w->param.supp_pcm_size_rate;
5318 		} else {
5319 			fmtcap &= cap;
5320 			pcmcap &= w->param.supp_pcm_size_rate;
5321 		}
5322 		ch->io[ret++] = as[ch->as].dacs[ch->asindex][i];
5323 		ch->stripecap &= w->wclass.conv.stripecap;
5324 		/* Do not count redirection pin/dac channels. */
5325 		if (i == 15 && as[ch->as].hpredir >= 0)
5326 			continue;
5327 		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
5328 		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
5329 			onlystereo = 0;
5330 		pinset |= (1 << i);
5331 	}
5332 	ch->io[ret] = -1;
5333 	ch->channels = channels;
5334 
5335 	if (as[ch->as].fakeredir)
5336 		ret--;
5337 	/* Standard speaks only about stereo pins and playback, ... */
5338 	if ((!onlystereo) || as[ch->as].mixed)
5339 		pinset = 0;
5340 	/* ..., but there it gives us info about speakers layout. */
5341 	as[ch->as].pinset = pinset;
5342 
5343 	ch->supp_stream_formats = fmtcap;
5344 	ch->supp_pcm_size_rate = pcmcap;
5345 
5346 	/*
5347 	 *  8bit = 0
5348 	 * 16bit = 1
5349 	 * 20bit = 2
5350 	 * 24bit = 3
5351 	 * 32bit = 4
5352 	 */
5353 	if (ret > 0) {
5354 		i = 0;
5355 		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
5356 			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
5357 				ch->bit16 = 1;
5358 			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
5359 				ch->bit16 = 0;
5360 			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
5361 				ch->bit32 = 3;
5362 			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
5363 				ch->bit32 = 2;
5364 			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
5365 				ch->bit32 = 4;
5366 			if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) {
5367 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
5368 				if (ch->bit32)
5369 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
5370 			}
5371 			if (channels >= 2) {
5372 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
5373 				if (ch->bit32)
5374 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
5375 			}
5376 			if (channels >= 3 && !onlystereo) {
5377 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0);
5378 				if (ch->bit32)
5379 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0);
5380 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1);
5381 				if (ch->bit32)
5382 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1);
5383 			}
5384 			if (channels >= 4) {
5385 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
5386 				if (ch->bit32)
5387 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
5388 				if (!onlystereo) {
5389 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1);
5390 					if (ch->bit32)
5391 						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1);
5392 				}
5393 			}
5394 			if (channels >= 5 && !onlystereo) {
5395 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0);
5396 				if (ch->bit32)
5397 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0);
5398 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1);
5399 				if (ch->bit32)
5400 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1);
5401 			}
5402 			if (channels >= 6) {
5403 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
5404 				if (ch->bit32)
5405 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
5406 				if (!onlystereo) {
5407 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0);
5408 					if (ch->bit32)
5409 						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0);
5410 				}
5411 			}
5412 			if (channels >= 7 && !onlystereo) {
5413 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0);
5414 				if (ch->bit32)
5415 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0);
5416 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1);
5417 				if (ch->bit32)
5418 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1);
5419 			}
5420 			if (channels >= 8) {
5421 				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
5422 				if (ch->bit32)
5423 					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
5424 			}
5425 		}
5426 		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
5427 			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
5428 			if (channels >= 8) {
5429 				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 0);
5430 				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 1);
5431 			}
5432 		}
5433 		ch->fmtlist[i] = 0;
5434 		i = 0;
5435 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
5436 			ch->pcmrates[i++] = 8000;
5437 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
5438 			ch->pcmrates[i++] = 11025;
5439 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
5440 			ch->pcmrates[i++] = 16000;
5441 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
5442 			ch->pcmrates[i++] = 22050;
5443 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
5444 			ch->pcmrates[i++] = 32000;
5445 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
5446 			ch->pcmrates[i++] = 44100;
5447 		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
5448 		ch->pcmrates[i++] = 48000;
5449 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
5450 			ch->pcmrates[i++] = 88200;
5451 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
5452 			ch->pcmrates[i++] = 96000;
5453 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
5454 			ch->pcmrates[i++] = 176400;
5455 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
5456 			ch->pcmrates[i++] = 192000;
5457 		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
5458 		ch->pcmrates[i] = 0;
5459 		if (i > 0) {
5460 			ch->caps.minspeed = ch->pcmrates[0];
5461 			ch->caps.maxspeed = ch->pcmrates[i - 1];
5462 		}
5463 	}
5464 
5465 	return (ret);
5466 }
5467 
5468 static void
5469 hdaa_prepare_pcms(struct hdaa_devinfo *devinfo)
5470 {
5471 	struct hdaa_audio_as *as = devinfo->as;
5472 	int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
5473 
5474 	for (i = 0; i < devinfo->ascnt; i++) {
5475 		if (as[i].enable == 0)
5476 			continue;
5477 		if (as[i].dir == HDAA_CTL_IN) {
5478 			if (as[i].digital)
5479 				drdev++;
5480 			else
5481 				ardev++;
5482 		} else {
5483 			if (as[i].digital)
5484 				dpdev++;
5485 			else
5486 				apdev++;
5487 		}
5488 	}
5489 	devinfo->num_devs =
5490 	    max(ardev, apdev) + max(drdev, dpdev);
5491 	devinfo->devs =
5492 	    (struct hdaa_pcm_devinfo *)malloc(
5493 	    devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo),
5494 	    M_HDAA, M_ZERO | M_NOWAIT);
5495 	if (devinfo->devs == NULL) {
5496 		device_printf(devinfo->dev,
5497 		    "Unable to allocate memory for devices\n");
5498 		return;
5499 	}
5500 	for (i = 0; i < devinfo->num_devs; i++) {
5501 		devinfo->devs[i].index = i;
5502 		devinfo->devs[i].devinfo = devinfo;
5503 		devinfo->devs[i].playas = -1;
5504 		devinfo->devs[i].recas = -1;
5505 		devinfo->devs[i].digital = 255;
5506 	}
5507 	for (i = 0; i < devinfo->ascnt; i++) {
5508 		if (as[i].enable == 0)
5509 			continue;
5510 		for (j = 0; j < devinfo->num_devs; j++) {
5511 			if (devinfo->devs[j].digital != 255 &&
5512 			    (!devinfo->devs[j].digital) !=
5513 			    (!as[i].digital))
5514 				continue;
5515 			if (as[i].dir == HDAA_CTL_IN) {
5516 				if (devinfo->devs[j].recas >= 0)
5517 					continue;
5518 				devinfo->devs[j].recas = i;
5519 			} else {
5520 				if (devinfo->devs[j].playas >= 0)
5521 					continue;
5522 				devinfo->devs[j].playas = i;
5523 			}
5524 			as[i].pdevinfo = &devinfo->devs[j];
5525 			for (k = 0; k < as[i].num_chans; k++) {
5526 				devinfo->chans[as[i].chans[k]].pdevinfo =
5527 				    &devinfo->devs[j];
5528 			}
5529 			devinfo->devs[j].digital = as[i].digital;
5530 			break;
5531 		}
5532 	}
5533 }
5534 
5535 static void
5536 hdaa_create_pcms(struct hdaa_devinfo *devinfo)
5537 {
5538 	int i;
5539 
5540 	for (i = 0; i < devinfo->num_devs; i++) {
5541 		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
5542 
5543 		pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1);
5544 		device_set_ivars(pdevinfo->dev, (void *)pdevinfo);
5545 	}
5546 }
5547 
5548 static void
5549 hdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
5550 {
5551 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5552 	struct hdaa_audio_ctl *ctl;
5553 	char buf[64];
5554 	int i, j, printed = 0;
5555 
5556 	if (flag == 0) {
5557 		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5558 		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5559 		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
5560 		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
5561 	}
5562 
5563 	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
5564 		if ((flag & (1 << j)) == 0)
5565 			continue;
5566 		i = 0;
5567 		printed = 0;
5568 		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5569 			if (ctl->enable == 0 ||
5570 			    ctl->widget->enable == 0)
5571 				continue;
5572 			if (!((pdevinfo->playas >= 0 &&
5573 			    ctl->widget->bindas == pdevinfo->playas) ||
5574 			    (pdevinfo->recas >= 0 &&
5575 			    ctl->widget->bindas == pdevinfo->recas) ||
5576 			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
5577 				continue;
5578 			if ((ctl->ossmask & (1 << j)) == 0)
5579 				continue;
5580 
5581 			if (printed == 0) {
5582 				if (banner != NULL) {
5583 					device_printf(pdevinfo->dev, "%s", banner);
5584 				} else {
5585 					device_printf(pdevinfo->dev, "Unknown Ctl");
5586 				}
5587 				printf(" (OSS: %s)",
5588 				    hdaa_audio_ctl_ossmixer_mask2allname(1 << j,
5589 				    buf, sizeof(buf)));
5590 				if (pdevinfo->ossmask & (1 << j)) {
5591 					printf(": %+d/%+ddB\n",
5592 					    pdevinfo->minamp[j] / 4,
5593 					    pdevinfo->maxamp[j] / 4);
5594 				} else
5595 					printf("\n");
5596 				printed = 1;
5597 			}
5598 			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
5599 				ctl->widget->nid,
5600 				(ctl->ndir == HDAA_CTL_IN)?"in ":"out");
5601 			if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir)
5602 				printf(" %2d): ", ctl->index);
5603 			else
5604 				printf("):    ");
5605 			if (ctl->step > 0) {
5606 				printf("%+d/%+ddB (%d steps)%s\n",
5607 				    MINQDB(ctl) / 4,
5608 				    MAXQDB(ctl) / 4,
5609 				    ctl->step + 1,
5610 				    ctl->mute?" + mute":"");
5611 			} else
5612 				printf("%s\n", ctl->mute?"mute":"");
5613 		}
5614 	}
5615 	if (printed)
5616 		device_printf(pdevinfo->dev, "\n");
5617 }
5618 
5619 static void
5620 hdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
5621 {
5622 	uint32_t cap;
5623 
5624 	cap = fcap;
5625 	if (cap != 0) {
5626 		device_printf(dev, "     Stream cap: 0x%08x", cap);
5627 		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5628 			printf(" AC3");
5629 		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5630 			printf(" FLOAT32");
5631 		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5632 			printf(" PCM");
5633 		printf("\n");
5634 	}
5635 	cap = pcmcap;
5636 	if (cap != 0) {
5637 		device_printf(dev, "        PCM cap: 0x%08x", cap);
5638 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5639 			printf(" 8");
5640 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5641 			printf(" 16");
5642 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5643 			printf(" 20");
5644 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5645 			printf(" 24");
5646 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5647 			printf(" 32");
5648 		printf(" bits,");
5649 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5650 			printf(" 8");
5651 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5652 			printf(" 11");
5653 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5654 			printf(" 16");
5655 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5656 			printf(" 22");
5657 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5658 			printf(" 32");
5659 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5660 			printf(" 44");
5661 		printf(" 48");
5662 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5663 			printf(" 88");
5664 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5665 			printf(" 96");
5666 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5667 			printf(" 176");
5668 		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5669 			printf(" 192");
5670 		printf(" KHz\n");
5671 	}
5672 }
5673 
5674 static void
5675 hdaa_dump_pin(struct hdaa_widget *w)
5676 {
5677 	uint32_t pincap;
5678 
5679 	pincap = w->wclass.pin.cap;
5680 
5681 	device_printf(w->devinfo->dev, "        Pin cap: 0x%08x", pincap);
5682 	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5683 		printf(" ISC");
5684 	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5685 		printf(" TRQD");
5686 	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5687 		printf(" PDC");
5688 	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5689 		printf(" HP");
5690 	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5691 		printf(" OUT");
5692 	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5693 		printf(" IN");
5694 	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5695 		printf(" BAL");
5696 	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
5697 		printf(" HDMI");
5698 	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5699 		printf(" VREF[");
5700 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5701 			printf(" 50");
5702 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5703 			printf(" 80");
5704 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5705 			printf(" 100");
5706 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5707 			printf(" GROUND");
5708 		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5709 			printf(" HIZ");
5710 		printf(" ]");
5711 	}
5712 	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5713 		printf(" EAPD");
5714 	if (HDA_PARAM_PIN_CAP_DP(pincap))
5715 		printf(" DP");
5716 	if (HDA_PARAM_PIN_CAP_HBR(pincap))
5717 		printf(" HBR");
5718 	printf("\n");
5719 	device_printf(w->devinfo->dev, "     Pin config: 0x%08x\n",
5720 	    w->wclass.pin.config);
5721 	device_printf(w->devinfo->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
5722 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5723 		printf(" HP");
5724 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5725 		printf(" IN");
5726 	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5727 		printf(" OUT");
5728 	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
5729 		if ((w->wclass.pin.ctrl &
5730 		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
5731 			printf(" HBR");
5732 		else if ((w->wclass.pin.ctrl &
5733 		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5734 			printf(" EPTs");
5735 	} else {
5736 		if ((w->wclass.pin.ctrl &
5737 		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5738 			printf(" VREFs");
5739 	}
5740 	printf("\n");
5741 }
5742 
5743 static void
5744 hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf)
5745 {
5746 
5747 	device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d "
5748 	    "%-13s %-5s %-7s %-10s %-7s %d%s\n",
5749 	    w->nid, conf,
5750 	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
5751 	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
5752 	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
5753 	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
5754 	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
5755 	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
5756 	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
5757 	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
5758 	    (w->enable == 0)?" DISA":"");
5759 }
5760 
5761 static void
5762 hdaa_dump_pin_configs(struct hdaa_devinfo *devinfo)
5763 {
5764 	struct hdaa_widget *w;
5765 	int i;
5766 
5767 	device_printf(devinfo->dev, "nid   0x    as seq "
5768 	    "device       conn  jack    loc        color   misc\n");
5769 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5770 		w = hdaa_widget_get(devinfo, i);
5771 		if (w == NULL)
5772 			continue;
5773 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5774 			continue;
5775 		hdaa_dump_pin_config(w, w->wclass.pin.config);
5776 	}
5777 }
5778 
5779 static void
5780 hdaa_dump_amp(device_t dev, uint32_t cap, const char *banner)
5781 {
5782 	int offset, size, step;
5783 
5784 	offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap);
5785 	size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap);
5786 	step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap);
5787 	device_printf(dev, "     %s amp: 0x%08x "
5788 	    "mute=%d step=%d size=%d offset=%d (%+d/%+ddB)\n",
5789 	    banner, cap,
5790 	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5791 	    step, size, offset,
5792 	    ((0 - offset) * (size + 1)) / 4,
5793 	    ((step - offset) * (size + 1)) / 4);
5794 }
5795 
5796 static void
5797 hdaa_dump_nodes(struct hdaa_devinfo *devinfo)
5798 {
5799 	struct hdaa_widget *w, *cw;
5800 	char buf[64];
5801 	int i, j;
5802 
5803 	device_printf(devinfo->dev, "\n");
5804 	device_printf(devinfo->dev, "Default parameters:\n");
5805 	hdaa_dump_audio_formats(devinfo->dev,
5806 	    devinfo->supp_stream_formats,
5807 	    devinfo->supp_pcm_size_rate);
5808 	hdaa_dump_amp(devinfo->dev, devinfo->inamp_cap, " Input");
5809 	hdaa_dump_amp(devinfo->dev, devinfo->outamp_cap, "Output");
5810 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5811 		w = hdaa_widget_get(devinfo, i);
5812 		if (w == NULL) {
5813 			device_printf(devinfo->dev, "Ghost widget nid=%d\n", i);
5814 			continue;
5815 		}
5816 		device_printf(devinfo->dev, "\n");
5817 		device_printf(devinfo->dev, "            nid: %d%s\n", w->nid,
5818 		    (w->enable == 0) ? " [DISABLED]" : "");
5819 		device_printf(devinfo->dev, "           Name: %s\n", w->name);
5820 		device_printf(devinfo->dev, "     Widget cap: 0x%08x",
5821 		    w->param.widget_cap);
5822 		if (w->param.widget_cap & 0x0ee1) {
5823 			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
5824 			    printf(" LRSWAP");
5825 			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
5826 			    printf(" PWR");
5827 			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5828 			    printf(" DIGITAL");
5829 			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
5830 			    printf(" UNSOL");
5831 			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
5832 			    printf(" PROC");
5833 			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
5834 			    printf(" STRIPE(x%d)",
5835 				1 << (fls(w->wclass.conv.stripecap) - 1));
5836 			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
5837 			if (j == 1)
5838 			    printf(" STEREO");
5839 			else if (j > 1)
5840 			    printf(" %dCH", j + 1);
5841 		}
5842 		printf("\n");
5843 		if (w->bindas != -1) {
5844 			device_printf(devinfo->dev, "    Association: %d (0x%04x)\n",
5845 			    w->bindas, w->bindseqmask);
5846 		}
5847 		if (w->ossmask != 0 || w->ossdev >= 0) {
5848 			device_printf(devinfo->dev, "            OSS: %s",
5849 			    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
5850 			if (w->ossdev >= 0)
5851 			    printf(" (%s)", ossnames[w->ossdev]);
5852 			printf("\n");
5853 		}
5854 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5855 		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5856 			hdaa_dump_audio_formats(devinfo->dev,
5857 			    w->param.supp_stream_formats,
5858 			    w->param.supp_pcm_size_rate);
5859 		} else if (w->type ==
5860 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
5861 			hdaa_dump_pin(w);
5862 		if (w->param.eapdbtl != HDA_INVALID)
5863 			device_printf(devinfo->dev, "           EAPD: 0x%08x\n",
5864 			    w->param.eapdbtl);
5865 		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5866 		    w->param.outamp_cap != 0)
5867 			hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output");
5868 		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5869 		    w->param.inamp_cap != 0)
5870 			hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input");
5871 		if (w->nconns > 0)
5872 			device_printf(devinfo->dev, "    Connections: %d\n", w->nconns);
5873 		for (j = 0; j < w->nconns; j++) {
5874 			cw = hdaa_widget_get(devinfo, w->conns[j]);
5875 			device_printf(devinfo->dev, "          + %s<- nid=%d [%s]",
5876 			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
5877 			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5878 			if (cw == NULL)
5879 				printf(" [UNKNOWN]");
5880 			else if (cw->enable == 0)
5881 				printf(" [DISABLED]");
5882 			if (w->nconns > 1 && w->selconn == j && w->type !=
5883 			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5884 				printf(" (selected)");
5885 			printf("\n");
5886 		}
5887 	}
5888 
5889 }
5890 
5891 static void
5892 hdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth)
5893 {
5894 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5895 	struct hdaa_widget *w, *cw;
5896 	char buf[64];
5897 	int i;
5898 
5899 	if (depth > HDA_PARSE_MAXDEPTH)
5900 		return;
5901 
5902 	w = hdaa_widget_get(devinfo, nid);
5903 	if (w == NULL || w->enable == 0)
5904 		return;
5905 
5906 	if (depth == 0)
5907 		device_printf(pdevinfo->dev, "%*s", 4, "");
5908 	else
5909 		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
5910 	printf("nid=%d [%s]", w->nid, w->name);
5911 
5912 	if (depth > 0) {
5913 		if (w->ossmask == 0) {
5914 			printf("\n");
5915 			return;
5916 		}
5917 		printf(" [src: %s]",
5918 		    hdaa_audio_ctl_ossmixer_mask2allname(
5919 			w->ossmask, buf, sizeof(buf)));
5920 		if (w->ossdev >= 0) {
5921 			printf("\n");
5922 			return;
5923 		}
5924 	}
5925 	printf("\n");
5926 
5927 	for (i = 0; i < w->nconns; i++) {
5928 		if (w->connsenable[i] == 0)
5929 			continue;
5930 		cw = hdaa_widget_get(devinfo, w->conns[i]);
5931 		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
5932 			continue;
5933 		hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
5934 	}
5935 
5936 }
5937 
5938 static void
5939 hdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo)
5940 {
5941 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5942 	struct hdaa_audio_as *as;
5943 	struct hdaa_widget *w;
5944 	nid_t *nids;
5945 	int chid, i;
5946 
5947 	if (pdevinfo->playas < 0)
5948 		return;
5949 
5950 	device_printf(pdevinfo->dev, "Playback:\n");
5951 
5952 	chid = devinfo->as[pdevinfo->playas].chans[0];
5953 	hdaa_dump_audio_formats(pdevinfo->dev,
5954 	    devinfo->chans[chid].supp_stream_formats,
5955 	    devinfo->chans[chid].supp_pcm_size_rate);
5956 	for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) {
5957 		chid = devinfo->as[pdevinfo->playas].chans[i];
5958 		device_printf(pdevinfo->dev, "            DAC:");
5959 		for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5960 			printf(" %d", *nids);
5961 		printf("\n");
5962 	}
5963 
5964 	as = &devinfo->as[pdevinfo->playas];
5965 	for (i = 0; i < 16; i++) {
5966 		if (as->pins[i] <= 0)
5967 			continue;
5968 		w = hdaa_widget_get(devinfo, as->pins[i]);
5969 		if (w == NULL || w->enable == 0)
5970 			continue;
5971 		device_printf(pdevinfo->dev, "\n");
5972 		hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0);
5973 	}
5974 	device_printf(pdevinfo->dev, "\n");
5975 }
5976 
5977 static void
5978 hdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo)
5979 {
5980 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5981 	struct hdaa_widget *w;
5982 	nid_t *nids;
5983 	int chid, i;
5984 
5985 	if (pdevinfo->recas < 0)
5986 		return;
5987 
5988 	device_printf(pdevinfo->dev, "Record:\n");
5989 
5990 	chid = devinfo->as[pdevinfo->recas].chans[0];
5991 	hdaa_dump_audio_formats(pdevinfo->dev,
5992 	    devinfo->chans[chid].supp_stream_formats,
5993 	    devinfo->chans[chid].supp_pcm_size_rate);
5994 	for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) {
5995 		chid = devinfo->as[pdevinfo->recas].chans[i];
5996 		device_printf(pdevinfo->dev, "            ADC:");
5997 		for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5998 			printf(" %d", *nids);
5999 		printf("\n");
6000 	}
6001 
6002 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6003 		w = hdaa_widget_get(devinfo, i);
6004 		if (w == NULL || w->enable == 0)
6005 			continue;
6006 		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
6007 			continue;
6008 		if (w->bindas != pdevinfo->recas)
6009 			continue;
6010 		device_printf(pdevinfo->dev, "\n");
6011 		hdaa_dump_dst_nid(pdevinfo, i, 0);
6012 	}
6013 	device_printf(pdevinfo->dev, "\n");
6014 }
6015 
6016 static void
6017 hdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo)
6018 {
6019 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6020 	struct hdaa_widget *w;
6021 	int i;
6022 	int printed = 0;
6023 
6024 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6025 		w = hdaa_widget_get(devinfo, i);
6026 		if (w == NULL || w->enable == 0)
6027 			continue;
6028 		if (w->ossdev != SOUND_MIXER_IMIX)
6029 			continue;
6030 		if (w->bindas != pdevinfo->recas)
6031 			continue;
6032 		if (printed == 0) {
6033 			printed = 1;
6034 			device_printf(pdevinfo->dev, "Input Mix:\n");
6035 		}
6036 		device_printf(pdevinfo->dev, "\n");
6037 		hdaa_dump_dst_nid(pdevinfo, i, 0);
6038 	}
6039 	if (printed)
6040 		device_printf(pdevinfo->dev, "\n");
6041 }
6042 
6043 static void
6044 hdaa_pindump(device_t dev)
6045 {
6046 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6047 	struct hdaa_widget *w;
6048 	uint32_t res, pincap, delay;
6049 	int i;
6050 
6051 	device_printf(dev, "Dumping AFG pins:\n");
6052 	device_printf(dev, "nid   0x    as seq "
6053 	    "device       conn  jack    loc        color   misc\n");
6054 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6055 		w = hdaa_widget_get(devinfo, i);
6056 		if (w == NULL || w->type !=
6057 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6058 			continue;
6059 		hdaa_dump_pin_config(w, w->wclass.pin.config);
6060 		pincap = w->wclass.pin.cap;
6061 		device_printf(dev, "    Caps: %2s %3s %2s %4s %4s",
6062 		    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
6063 		    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
6064 		    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
6065 		    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
6066 		    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
6067 		if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
6068 		    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
6069 			if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
6070 				delay = 0;
6071 				hda_command(dev,
6072 				    HDA_CMD_SET_PIN_SENSE(0, w->nid, 0));
6073 				do {
6074 					res = hda_command(dev,
6075 					    HDA_CMD_GET_PIN_SENSE(0, w->nid));
6076 					if (res != 0x7fffffff && res != 0xffffffff)
6077 						break;
6078 					DELAY(10);
6079 				} while (++delay < 10000);
6080 			} else {
6081 				delay = 0;
6082 				res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0,
6083 				    w->nid));
6084 			}
6085 			printf(" Sense: 0x%08x (%sconnected%s)", res,
6086 			    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ?
6087 			     "" : "dis",
6088 			    (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) &&
6089 			     (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ?
6090 			      ", ELD valid" : "");
6091 			if (delay > 0)
6092 				printf(" delay %dus", delay * 10);
6093 		}
6094 		printf("\n");
6095 	}
6096 	device_printf(dev,
6097 	    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
6098 	    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
6099 	    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
6100 	    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
6101 	    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
6102 	    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
6103 	hdaa_dump_gpi(devinfo);
6104 	hdaa_dump_gpio(devinfo);
6105 	hdaa_dump_gpo(devinfo);
6106 }
6107 
6108 static void
6109 hdaa_configure(device_t dev)
6110 {
6111 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6112 	struct hdaa_audio_ctl *ctl;
6113 	int i;
6114 
6115 	HDA_BOOTHVERBOSE(
6116 		device_printf(dev, "Applying built-in patches...\n");
6117 	);
6118 	hdaa_patch(devinfo);
6119 	HDA_BOOTHVERBOSE(
6120 		device_printf(dev, "Applying local patches...\n");
6121 	);
6122 	hdaa_local_patch(devinfo);
6123 	hdaa_audio_postprocess(devinfo);
6124 	HDA_BOOTHVERBOSE(
6125 		device_printf(dev, "Parsing Ctls...\n");
6126 	);
6127 	hdaa_audio_ctl_parse(devinfo);
6128 	HDA_BOOTHVERBOSE(
6129 		device_printf(dev, "Disabling nonaudio...\n");
6130 	);
6131 	hdaa_audio_disable_nonaudio(devinfo);
6132 	HDA_BOOTHVERBOSE(
6133 		device_printf(dev, "Disabling useless...\n");
6134 	);
6135 	hdaa_audio_disable_useless(devinfo);
6136 	HDA_BOOTVERBOSE(
6137 		device_printf(dev, "Patched pins configuration:\n");
6138 		hdaa_dump_pin_configs(devinfo);
6139 	);
6140 	HDA_BOOTHVERBOSE(
6141 		device_printf(dev, "Parsing pin associations...\n");
6142 	);
6143 	hdaa_audio_as_parse(devinfo);
6144 	HDA_BOOTHVERBOSE(
6145 		device_printf(dev, "Building AFG tree...\n");
6146 	);
6147 	hdaa_audio_build_tree(devinfo);
6148 	HDA_BOOTHVERBOSE(
6149 		device_printf(dev, "Disabling unassociated "
6150 		    "widgets...\n");
6151 	);
6152 	hdaa_audio_disable_unas(devinfo);
6153 	HDA_BOOTHVERBOSE(
6154 		device_printf(dev, "Disabling nonselected "
6155 		    "inputs...\n");
6156 	);
6157 	hdaa_audio_disable_notselected(devinfo);
6158 	HDA_BOOTHVERBOSE(
6159 		device_printf(dev, "Disabling useless...\n");
6160 	);
6161 	hdaa_audio_disable_useless(devinfo);
6162 	HDA_BOOTHVERBOSE(
6163 		device_printf(dev, "Disabling "
6164 		    "crossassociatement connections...\n");
6165 	);
6166 	hdaa_audio_disable_crossas(devinfo);
6167 	HDA_BOOTHVERBOSE(
6168 		device_printf(dev, "Disabling useless...\n");
6169 	);
6170 	hdaa_audio_disable_useless(devinfo);
6171 	HDA_BOOTHVERBOSE(
6172 		device_printf(dev, "Binding associations to channels...\n");
6173 	);
6174 	hdaa_audio_bind_as(devinfo);
6175 	HDA_BOOTHVERBOSE(
6176 		device_printf(dev, "Assigning names to signal sources...\n");
6177 	);
6178 	hdaa_audio_assign_names(devinfo);
6179 	HDA_BOOTHVERBOSE(
6180 		device_printf(dev, "Preparing PCM devices...\n");
6181 	);
6182 	hdaa_prepare_pcms(devinfo);
6183 	HDA_BOOTHVERBOSE(
6184 		device_printf(dev, "Assigning mixers to the tree...\n");
6185 	);
6186 	hdaa_audio_assign_mixers(devinfo);
6187 	HDA_BOOTHVERBOSE(
6188 		device_printf(dev, "Preparing pin controls...\n");
6189 	);
6190 	hdaa_audio_prepare_pin_ctrl(devinfo);
6191 	HDA_BOOTHVERBOSE(
6192 		device_printf(dev, "AFG commit...\n");
6193 	);
6194 	hdaa_audio_commit(devinfo);
6195 	HDA_BOOTHVERBOSE(
6196 		device_printf(dev, "Applying direct built-in patches...\n");
6197 	);
6198 	hdaa_patch_direct(devinfo);
6199 	HDA_BOOTHVERBOSE(
6200 		device_printf(dev, "Pin sense init...\n");
6201 	);
6202 	hdaa_sense_init(devinfo);
6203 	HDA_BOOTHVERBOSE(
6204 		device_printf(dev, "Creating PCM devices...\n");
6205 	);
6206 	hdaa_create_pcms(devinfo);
6207 
6208 	HDA_BOOTVERBOSE(
6209 		if (devinfo->quirks != 0) {
6210 			device_printf(dev, "FG config/quirks:");
6211 			for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
6212 				if ((devinfo->quirks &
6213 				    hdaa_quirks_tab[i].value) ==
6214 				    hdaa_quirks_tab[i].value)
6215 					printf(" %s", hdaa_quirks_tab[i].key);
6216 			}
6217 			printf("\n");
6218 		}
6219 	);
6220 
6221 	HDA_BOOTHVERBOSE(
6222 		device_printf(dev, "\n");
6223 		device_printf(dev, "+-----------+\n");
6224 		device_printf(dev, "| HDA NODES |\n");
6225 		device_printf(dev, "+-----------+\n");
6226 		hdaa_dump_nodes(devinfo);
6227 
6228 		device_printf(dev, "\n");
6229 		device_printf(dev, "+----------------+\n");
6230 		device_printf(dev, "| HDA AMPLIFIERS |\n");
6231 		device_printf(dev, "+----------------+\n");
6232 		device_printf(dev, "\n");
6233 		i = 0;
6234 		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
6235 			device_printf(dev, "%3d: nid %3d %s (%s) index %d", i,
6236 			    (ctl->widget != NULL) ? ctl->widget->nid : -1,
6237 			    (ctl->ndir == HDAA_CTL_IN)?"in ":"out",
6238 			    (ctl->dir == HDAA_CTL_IN)?"in ":"out",
6239 			    ctl->index);
6240 			if (ctl->childwidget != NULL)
6241 				printf(" cnid %3d", ctl->childwidget->nid);
6242 			else
6243 				printf("         ");
6244 			printf(" ossmask=0x%08x\n",
6245 			    ctl->ossmask);
6246 			device_printf(dev,
6247 			    "       mute: %d step: %3d size: %3d off: %3d%s\n",
6248 			    ctl->mute, ctl->step, ctl->size, ctl->offset,
6249 			    (ctl->enable == 0) ? " [DISABLED]" :
6250 			    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
6251 		}
6252 		device_printf(dev, "\n");
6253 	);
6254 }
6255 
6256 static void
6257 hdaa_unconfigure(device_t dev)
6258 {
6259 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6260 	struct hdaa_widget *w;
6261 	int i, j;
6262 
6263 	HDA_BOOTHVERBOSE(
6264 		device_printf(dev, "Pin sense deinit...\n");
6265 	);
6266 	hdaa_sense_deinit(devinfo);
6267 	free(devinfo->ctl, M_HDAA);
6268 	devinfo->ctl = NULL;
6269 	devinfo->ctlcnt = 0;
6270 	free(devinfo->as, M_HDAA);
6271 	devinfo->as = NULL;
6272 	devinfo->ascnt = 0;
6273 	free(devinfo->devs, M_HDAA);
6274 	devinfo->devs = NULL;
6275 	devinfo->num_devs = 0;
6276 	free(devinfo->chans, M_HDAA);
6277 	devinfo->chans = NULL;
6278 	devinfo->num_chans = 0;
6279 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6280 		w = hdaa_widget_get(devinfo, i);
6281 		if (w == NULL)
6282 			continue;
6283 		w->enable = 1;
6284 		w->selconn = -1;
6285 		w->pflags = 0;
6286 		w->bindas = -1;
6287 		w->bindseqmask = 0;
6288 		w->ossdev = -1;
6289 		w->ossmask = 0;
6290 		for (j = 0; j < w->nconns; j++)
6291 			w->connsenable[j] = 1;
6292 		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6293 			w->wclass.pin.config = w->wclass.pin.newconf;
6294 		if (w->eld != NULL) {
6295 			w->eld_len = 0;
6296 			free(w->eld, M_HDAA);
6297 			w->eld = NULL;
6298 		}
6299 	}
6300 }
6301 
6302 static int
6303 hdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS)
6304 {
6305 	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6306 	device_t dev = devinfo->dev;
6307 	char buf[256];
6308 	int n = 0, i, numgpi;
6309 	uint32_t data = 0;
6310 
6311 	buf[0] = 0;
6312 	hdaa_lock(devinfo);
6313 	numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap);
6314 	if (numgpi > 0) {
6315 		data = hda_command(dev,
6316 		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
6317 	}
6318 	hdaa_unlock(devinfo);
6319 	for (i = 0; i < numgpi; i++) {
6320 		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6321 		    n != 0 ? " " : "", i, ((data >> i) & 1));
6322 	}
6323 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6324 }
6325 
6326 static int
6327 hdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS)
6328 {
6329 	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6330 	device_t dev = devinfo->dev;
6331 	char buf[256];
6332 	int n = 0, i, numgpio;
6333 	uint32_t data = 0, enable = 0, dir = 0;
6334 
6335 	buf[0] = 0;
6336 	hdaa_lock(devinfo);
6337 	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6338 	if (numgpio > 0) {
6339 		data = hda_command(dev,
6340 		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
6341 		enable = hda_command(dev,
6342 		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
6343 		dir = hda_command(dev,
6344 		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
6345 	}
6346 	hdaa_unlock(devinfo);
6347 	for (i = 0; i < numgpio; i++) {
6348 		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=",
6349 		    n != 0 ? " " : "", i);
6350 		if ((enable & (1 << i)) == 0) {
6351 			n += snprintf(buf + n, sizeof(buf) - n, "disabled");
6352 			continue;
6353 		}
6354 		n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)",
6355 		    ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1));
6356 	}
6357 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6358 }
6359 
6360 static int
6361 hdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS)
6362 {
6363 	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6364 	char buf[256];
6365 	int error, n = 0, i, numgpio;
6366 	uint32_t gpio, x;
6367 
6368 	gpio = devinfo->newgpio;
6369 	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6370 	buf[0] = 0;
6371 	for (i = 0; i < numgpio; i++) {
6372 		x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6373 		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6374 		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6375 	}
6376 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6377 	if (error != 0 || req->newptr == NULL)
6378 		return (error);
6379 	if (strncmp(buf, "0x", 2) == 0)
6380 		gpio = strtol(buf + 2, NULL, 16);
6381 	else
6382 		gpio = hdaa_gpio_patch(gpio, buf);
6383 	hdaa_lock(devinfo);
6384 	devinfo->newgpio = devinfo->gpio = gpio;
6385 	hdaa_gpio_commit(devinfo);
6386 	hdaa_unlock(devinfo);
6387 	return (0);
6388 }
6389 
6390 static int
6391 hdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS)
6392 {
6393 	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6394 	device_t dev = devinfo->dev;
6395 	char buf[256];
6396 	int n = 0, i, numgpo;
6397 	uint32_t data = 0;
6398 
6399 	buf[0] = 0;
6400 	hdaa_lock(devinfo);
6401 	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6402 	if (numgpo > 0) {
6403 		data = hda_command(dev,
6404 		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
6405 	}
6406 	hdaa_unlock(devinfo);
6407 	for (i = 0; i < numgpo; i++) {
6408 		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6409 		    n != 0 ? " " : "", i, ((data >> i) & 1));
6410 	}
6411 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6412 }
6413 
6414 static int
6415 hdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS)
6416 {
6417 	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6418 	char buf[256];
6419 	int error, n = 0, i, numgpo;
6420 	uint32_t gpo, x;
6421 
6422 	gpo = devinfo->newgpo;
6423 	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6424 	buf[0] = 0;
6425 	for (i = 0; i < numgpo; i++) {
6426 		x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6427 		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6428 		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6429 	}
6430 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6431 	if (error != 0 || req->newptr == NULL)
6432 		return (error);
6433 	if (strncmp(buf, "0x", 2) == 0)
6434 		gpo = strtol(buf + 2, NULL, 16);
6435 	else
6436 		gpo = hdaa_gpio_patch(gpo, buf);
6437 	hdaa_lock(devinfo);
6438 	devinfo->newgpo = devinfo->gpo = gpo;
6439 	hdaa_gpo_commit(devinfo);
6440 	hdaa_unlock(devinfo);
6441 	return (0);
6442 }
6443 
6444 static int
6445 hdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS)
6446 {
6447 	device_t dev;
6448 	struct hdaa_devinfo *devinfo;
6449 	int error, val;
6450 
6451 	dev = oidp->oid_arg1;
6452 	devinfo = device_get_softc(dev);
6453 	if (devinfo == NULL)
6454 		return (EINVAL);
6455 	val = 0;
6456 	error = sysctl_handle_int(oidp, &val, 0, req);
6457 	if (error != 0 || req->newptr == NULL || val == 0)
6458 		return (error);
6459 
6460 	HDA_BOOTHVERBOSE(
6461 		device_printf(dev, "Reconfiguration...\n");
6462 	);
6463 	if ((error = device_delete_children(dev)) != 0)
6464 		return (error);
6465 	hdaa_lock(devinfo);
6466 	hdaa_unconfigure(dev);
6467 	hdaa_configure(dev);
6468 	hdaa_unlock(devinfo);
6469 	bus_generic_attach(dev);
6470 	HDA_BOOTHVERBOSE(
6471 		device_printf(dev, "Reconfiguration done\n");
6472 	);
6473 	return (0);
6474 }
6475 
6476 static int
6477 hdaa_suspend(device_t dev)
6478 {
6479 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6480 	int i;
6481 
6482 	HDA_BOOTHVERBOSE(
6483 		device_printf(dev, "Suspend...\n");
6484 	);
6485 	hdaa_lock(devinfo);
6486 	HDA_BOOTHVERBOSE(
6487 		device_printf(dev, "Stop streams...\n");
6488 	);
6489 	for (i = 0; i < devinfo->num_chans; i++) {
6490 		if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) {
6491 			devinfo->chans[i].flags |= HDAA_CHN_SUSPEND;
6492 			hdaa_channel_stop(&devinfo->chans[i]);
6493 		}
6494 	}
6495 	HDA_BOOTHVERBOSE(
6496 		device_printf(dev, "Power down FG"
6497 		    " nid=%d to the D3 state...\n",
6498 		    devinfo->nid);
6499 	);
6500 	hda_command(devinfo->dev,
6501 	    HDA_CMD_SET_POWER_STATE(0,
6502 	    devinfo->nid, HDA_CMD_POWER_STATE_D3));
6503 	callout_stop(&devinfo->poll_jack);
6504 	hdaa_unlock(devinfo);
6505 	callout_drain(&devinfo->poll_jack);
6506 	HDA_BOOTHVERBOSE(
6507 		device_printf(dev, "Suspend done\n");
6508 	);
6509 	return (0);
6510 }
6511 
6512 static int
6513 hdaa_resume(device_t dev)
6514 {
6515 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6516 	int i;
6517 
6518 	HDA_BOOTHVERBOSE(
6519 		device_printf(dev, "Resume...\n");
6520 	);
6521 	hdaa_lock(devinfo);
6522 	HDA_BOOTHVERBOSE(
6523 		device_printf(dev, "Power up audio FG nid=%d...\n",
6524 		    devinfo->nid);
6525 	);
6526 	hdaa_powerup(devinfo);
6527 	HDA_BOOTHVERBOSE(
6528 		device_printf(dev, "AFG commit...\n");
6529 	);
6530 	hdaa_audio_commit(devinfo);
6531 	HDA_BOOTHVERBOSE(
6532 		device_printf(dev, "Applying direct built-in patches...\n");
6533 	);
6534 	hdaa_patch_direct(devinfo);
6535 	HDA_BOOTHVERBOSE(
6536 		device_printf(dev, "Pin sense init...\n");
6537 	);
6538 	hdaa_sense_init(devinfo);
6539 
6540 	hdaa_unlock(devinfo);
6541 	for (i = 0; i < devinfo->num_devs; i++) {
6542 		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
6543 		HDA_BOOTHVERBOSE(
6544 			device_printf(pdevinfo->dev,
6545 			    "OSS mixer reinitialization...\n");
6546 		);
6547 		if (mixer_reinit(pdevinfo->dev) == -1)
6548 			device_printf(pdevinfo->dev,
6549 			    "unable to reinitialize the mixer\n");
6550 	}
6551 	hdaa_lock(devinfo);
6552 	HDA_BOOTHVERBOSE(
6553 		device_printf(dev, "Start streams...\n");
6554 	);
6555 	for (i = 0; i < devinfo->num_chans; i++) {
6556 		if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) {
6557 			devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND;
6558 			hdaa_channel_start(&devinfo->chans[i]);
6559 		}
6560 	}
6561 	hdaa_unlock(devinfo);
6562 	HDA_BOOTHVERBOSE(
6563 		device_printf(dev, "Resume done\n");
6564 	);
6565 	return (0);
6566 }
6567 
6568 static int
6569 hdaa_probe(device_t dev)
6570 {
6571 	const char *pdesc;
6572 	char buf[128];
6573 
6574 	if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
6575 		return (ENXIO);
6576 	pdesc = device_get_desc(device_get_parent(dev));
6577 	snprintf(buf, sizeof(buf), "%.*s Audio Function Group",
6578 	    (int)(strlen(pdesc) - 10), pdesc);
6579 	device_set_desc_copy(dev, buf);
6580 	return (BUS_PROBE_DEFAULT);
6581 }
6582 
6583 static int
6584 hdaa_attach(device_t dev)
6585 {
6586 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6587 	uint32_t res;
6588 	nid_t nid = hda_get_node_id(dev);
6589 
6590 	devinfo->dev = dev;
6591 	devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
6592 	devinfo->nid = nid;
6593 	devinfo->newquirks = -1;
6594 	devinfo->newgpio = -1;
6595 	devinfo->newgpo = -1;
6596 	callout_init(&devinfo->poll_jack, 1);
6597 	devinfo->poll_ival = hz;
6598 
6599 	hdaa_lock(devinfo);
6600 	res = hda_command(dev,
6601 	    HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT));
6602 	hdaa_unlock(devinfo);
6603 
6604 	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
6605 	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
6606 	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
6607 
6608 	HDA_BOOTVERBOSE(
6609 		device_printf(dev, "Subsystem ID: 0x%08x\n",
6610 		    hda_get_subsystem_id(dev));
6611 	);
6612 	HDA_BOOTHVERBOSE(
6613 		device_printf(dev,
6614 		    "Audio Function Group at nid=%d: %d subnodes %d-%d\n",
6615 		    nid, devinfo->nodecnt,
6616 		    devinfo->startnode, devinfo->endnode - 1);
6617 	);
6618 
6619 	if (devinfo->nodecnt > 0)
6620 		devinfo->widget = (struct hdaa_widget *)malloc(
6621 		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA,
6622 		    M_WAITOK | M_ZERO);
6623 	else
6624 		devinfo->widget = NULL;
6625 
6626 	hdaa_lock(devinfo);
6627 	HDA_BOOTHVERBOSE(
6628 		device_printf(dev, "Powering up...\n");
6629 	);
6630 	hdaa_powerup(devinfo);
6631 	HDA_BOOTHVERBOSE(
6632 		device_printf(dev, "Parsing audio FG...\n");
6633 	);
6634 	hdaa_audio_parse(devinfo);
6635 	HDA_BOOTVERBOSE(
6636 		device_printf(dev, "Original pins configuration:\n");
6637 		hdaa_dump_pin_configs(devinfo);
6638 	);
6639 	hdaa_configure(dev);
6640 	hdaa_unlock(devinfo);
6641 
6642 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6643 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6644 	    "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6645 	    &devinfo->newquirks, 0, hdaa_sysctl_quirks, "A",
6646 	    "Configuration options");
6647 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6648 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6649 	    "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6650 	    devinfo, 0, 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, 0, hdaa_sysctl_gpio_state, "A", "GPIO state");
6655 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6656 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6657 	    "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6658 	    devinfo, 0, hdaa_sysctl_gpio_config, "A", "GPIO configuration");
6659 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6660 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6661 	    "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6662 	    devinfo, 0, hdaa_sysctl_gpo_state, "A", "GPO state");
6663 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6664 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6665 	    "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6666 	    devinfo, 0, hdaa_sysctl_gpo_config, "A", "GPO configuration");
6667 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6668 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6669 	    "reconfig", CTLTYPE_INT | CTLFLAG_RW,
6670 	    dev, 0, hdaa_sysctl_reconfig, "I", "Reprocess configuration");
6671 	bus_generic_attach(dev);
6672 	return (0);
6673 }
6674 
6675 static int
6676 hdaa_detach(device_t dev)
6677 {
6678 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6679 	int error;
6680 
6681 	if ((error = device_delete_children(dev)) != 0)
6682 		return (error);
6683 
6684 	hdaa_lock(devinfo);
6685 	hdaa_unconfigure(dev);
6686 	devinfo->poll_ival = 0;
6687 	callout_stop(&devinfo->poll_jack);
6688 	hdaa_unlock(devinfo);
6689 	callout_drain(&devinfo->poll_jack);
6690 
6691 	free(devinfo->widget, M_HDAA);
6692 	return (0);
6693 }
6694 
6695 static int
6696 hdaa_print_child(device_t dev, device_t child)
6697 {
6698 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6699 	struct hdaa_pcm_devinfo *pdevinfo =
6700 	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6701 	struct hdaa_audio_as *as;
6702 	int retval, first = 1, i;
6703 
6704 	retval = bus_print_child_header(dev, child);
6705 	retval += printf(" at nid ");
6706 	if (pdevinfo->playas >= 0) {
6707 		as = &devinfo->as[pdevinfo->playas];
6708 		for (i = 0; i < 16; i++) {
6709 			if (as->pins[i] <= 0)
6710 				continue;
6711 			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6712 			first = 0;
6713 		}
6714 	}
6715 	if (pdevinfo->recas >= 0) {
6716 		if (pdevinfo->playas >= 0) {
6717 			retval += printf(" and ");
6718 			first = 1;
6719 		}
6720 		as = &devinfo->as[pdevinfo->recas];
6721 		for (i = 0; i < 16; i++) {
6722 			if (as->pins[i] <= 0)
6723 				continue;
6724 			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6725 			first = 0;
6726 		}
6727 	}
6728 	retval += bus_print_child_footer(dev, child);
6729 
6730 	return (retval);
6731 }
6732 
6733 static int
6734 hdaa_child_location_str(device_t dev, device_t child, char *buf,
6735     size_t buflen)
6736 {
6737 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6738 	struct hdaa_pcm_devinfo *pdevinfo =
6739 	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6740 	struct hdaa_audio_as *as;
6741 	int first = 1, i, len = 0;
6742 
6743 	len += snprintf(buf + len, buflen - len, "nid=");
6744 	if (pdevinfo->playas >= 0) {
6745 		as = &devinfo->as[pdevinfo->playas];
6746 		for (i = 0; i < 16; i++) {
6747 			if (as->pins[i] <= 0)
6748 				continue;
6749 			len += snprintf(buf + len, buflen - len,
6750 			    "%s%d", first ? "" : ",", as->pins[i]);
6751 			first = 0;
6752 		}
6753 	}
6754 	if (pdevinfo->recas >= 0) {
6755 		as = &devinfo->as[pdevinfo->recas];
6756 		for (i = 0; i < 16; i++) {
6757 			if (as->pins[i] <= 0)
6758 				continue;
6759 			len += snprintf(buf + len, buflen - len,
6760 			    "%s%d", first ? "" : ",", as->pins[i]);
6761 			first = 0;
6762 		}
6763 	}
6764 	return (0);
6765 }
6766 
6767 static void
6768 hdaa_stream_intr(device_t dev, int dir, int stream)
6769 {
6770 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6771 	struct hdaa_chan *ch;
6772 	int i;
6773 
6774 	for (i = 0; i < devinfo->num_chans; i++) {
6775 		ch = &devinfo->chans[i];
6776 		if (!(ch->flags & HDAA_CHN_RUNNING))
6777 			continue;
6778 		if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) &&
6779 		    ch->sid == stream) {
6780 			hdaa_unlock(devinfo);
6781 			chn_intr(ch->c);
6782 			hdaa_lock(devinfo);
6783 		}
6784 	}
6785 }
6786 
6787 static void
6788 hdaa_unsol_intr(device_t dev, uint32_t resp)
6789 {
6790 	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6791 	struct hdaa_widget *w;
6792 	int i, tag, flags;
6793 
6794 	HDA_BOOTHVERBOSE(
6795 		device_printf(dev, "Unsolicited response %08x\n", resp);
6796 	);
6797 	tag = resp >> 26;
6798 	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6799 		w = hdaa_widget_get(devinfo, i);
6800 		if (w == NULL || w->enable == 0 || w->type !=
6801 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6802 			continue;
6803 		if (w->unsol != tag)
6804 			continue;
6805 		if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
6806 		    HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
6807 			flags = resp & 0x03;
6808 		else
6809 			flags = 0x01;
6810 		if (flags & 0x01)
6811 			hdaa_presence_handler(w);
6812 		if (flags & 0x02)
6813 			hdaa_eld_handler(w);
6814 	}
6815 }
6816 
6817 static device_method_t hdaa_methods[] = {
6818 	/* device interface */
6819 	DEVMETHOD(device_probe,		hdaa_probe),
6820 	DEVMETHOD(device_attach,	hdaa_attach),
6821 	DEVMETHOD(device_detach,	hdaa_detach),
6822 	DEVMETHOD(device_suspend,	hdaa_suspend),
6823 	DEVMETHOD(device_resume,	hdaa_resume),
6824 	/* Bus interface */
6825 	DEVMETHOD(bus_print_child,	hdaa_print_child),
6826 	DEVMETHOD(bus_child_location_str, hdaa_child_location_str),
6827 	DEVMETHOD(hdac_stream_intr,	hdaa_stream_intr),
6828 	DEVMETHOD(hdac_unsol_intr,	hdaa_unsol_intr),
6829 	DEVMETHOD(hdac_pindump,		hdaa_pindump),
6830 	DEVMETHOD_END
6831 };
6832 
6833 static driver_t hdaa_driver = {
6834 	"hdaa",
6835 	hdaa_methods,
6836 	sizeof(struct hdaa_devinfo),
6837 };
6838 
6839 static devclass_t hdaa_devclass;
6840 
6841 DRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, NULL, NULL);
6842 
6843 static void
6844 hdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid,
6845     char *buf, int buflen)
6846 {
6847 	struct hdaa_audio_as *as;
6848 	int c;
6849 
6850 	as = &devinfo->as[asid];
6851 	c = devinfo->chans[as->chans[0]].channels;
6852 	if (c == 1)
6853 		snprintf(buf, buflen, "mono");
6854 	else if (c == 2) {
6855 		if (as->hpredir < 0)
6856 			buf[0] = 0;
6857 		else
6858 			snprintf(buf, buflen, "2.0");
6859 	} else if (as->pinset == 0x0003)
6860 		snprintf(buf, buflen, "3.1");
6861 	else if (as->pinset == 0x0005 || as->pinset == 0x0011)
6862 		snprintf(buf, buflen, "4.0");
6863 	else if (as->pinset == 0x0007 || as->pinset == 0x0013)
6864 		snprintf(buf, buflen, "5.1");
6865 	else if (as->pinset == 0x0017)
6866 		snprintf(buf, buflen, "7.1");
6867 	else
6868 		snprintf(buf, buflen, "%dch", c);
6869 	if (as->hpredir >= 0)
6870 		strlcat(buf, "+HP", buflen);
6871 }
6872 
6873 static int
6874 hdaa_chan_type(struct hdaa_devinfo *devinfo, int asid)
6875 {
6876 	struct hdaa_audio_as *as;
6877 	struct hdaa_widget *w;
6878 	int i, t = -1, t1;
6879 
6880 	as = &devinfo->as[asid];
6881 	for (i = 0; i < 16; i++) {
6882 		w = hdaa_widget_get(devinfo, as->pins[i]);
6883 		if (w == NULL || w->enable == 0 || w->type !=
6884 		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6885 			continue;
6886 		t1 = HDA_CONFIG_DEFAULTCONF_DEVICE(w->wclass.pin.config);
6887 		if (t == -1)
6888 			t = t1;
6889 		else if (t != t1) {
6890 			t = -2;
6891 			break;
6892 		}
6893 	}
6894 	return (t);
6895 }
6896 
6897 static int
6898 hdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS)
6899 {
6900 	struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1;
6901 	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
6902 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6903 	struct hdaa_chan *ch;
6904 	int error, val, i;
6905 	uint32_t pcmcap;
6906 
6907 	ch = &devinfo->chans[as->chans[0]];
6908 	val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 :
6909 	    ((ch->bit32 == 2) ? 20 : 0));
6910 	error = sysctl_handle_int(oidp, &val, 0, req);
6911 	if (error != 0 || req->newptr == NULL)
6912 		return (error);
6913 	pcmcap = ch->supp_pcm_size_rate;
6914 	if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6915 		ch->bit32 = 4;
6916 	else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6917 		ch->bit32 = 3;
6918 	else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6919 		ch->bit32 = 2;
6920 	else
6921 		return (EINVAL);
6922 	for (i = 1; i < as->num_chans; i++)
6923 		devinfo->chans[as->chans[i]].bit32 = ch->bit32;
6924 	return (0);
6925 }
6926 
6927 static int
6928 hdaa_pcm_probe(device_t dev)
6929 {
6930 	struct hdaa_pcm_devinfo *pdevinfo =
6931 	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6932 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6933 	const char *pdesc;
6934 	char chans1[8], chans2[8];
6935 	char buf[128];
6936 	int loc1, loc2, t1, t2;
6937 
6938 	if (pdevinfo->playas >= 0)
6939 		loc1 = devinfo->as[pdevinfo->playas].location;
6940 	else
6941 		loc1 = devinfo->as[pdevinfo->recas].location;
6942 	if (pdevinfo->recas >= 0)
6943 		loc2 = devinfo->as[pdevinfo->recas].location;
6944 	else
6945 		loc2 = loc1;
6946 	if (loc1 != loc2)
6947 		loc1 = -2;
6948 	if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0')
6949 		loc1 = -2;
6950 	chans1[0] = 0;
6951 	chans2[0] = 0;
6952 	t1 = t2 = -1;
6953 	if (pdevinfo->playas >= 0) {
6954 		hdaa_chan_formula(devinfo, pdevinfo->playas,
6955 		    chans1, sizeof(chans1));
6956 		t1 = hdaa_chan_type(devinfo, pdevinfo->playas);
6957 	}
6958 	if (pdevinfo->recas >= 0) {
6959 		hdaa_chan_formula(devinfo, pdevinfo->recas,
6960 		    chans2, sizeof(chans2));
6961 		t2 = hdaa_chan_type(devinfo, pdevinfo->recas);
6962 	}
6963 	if (chans1[0] != 0 || chans2[0] != 0) {
6964 		if (chans1[0] == 0 && pdevinfo->playas >= 0)
6965 			snprintf(chans1, sizeof(chans1), "2.0");
6966 		else if (chans2[0] == 0 && pdevinfo->recas >= 0)
6967 			snprintf(chans2, sizeof(chans2), "2.0");
6968 		if (strcmp(chans1, chans2) == 0)
6969 			chans2[0] = 0;
6970 	}
6971 	if (t1 == -1)
6972 		t1 = t2;
6973 	else if (t2 == -1)
6974 		t2 = t1;
6975 	if (t1 != t2)
6976 		t1 = -2;
6977 	if (pdevinfo->digital)
6978 		t1 = -2;
6979 	pdesc = device_get_desc(device_get_parent(dev));
6980 	snprintf(buf, sizeof(buf), "%.*s (%s%s%s%s%s%s%s%s%s)",
6981 	    (int)(strlen(pdesc) - 21), pdesc,
6982 	    loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "",
6983 	    (pdevinfo->digital == 0x7)?"HDMI/DP":
6984 	    ((pdevinfo->digital == 0x5)?"DisplayPort":
6985 	    ((pdevinfo->digital == 0x3)?"HDMI":
6986 	    ((pdevinfo->digital)?"Digital":"Analog"))),
6987 	    chans1[0] ? " " : "", chans1,
6988 	    chans2[0] ? "/" : "", chans2,
6989 	    t1 >= 0 ? " " : "", t1 >= 0 ? HDA_DEVS[t1] : "");
6990 	device_set_desc_copy(dev, buf);
6991 	return (BUS_PROBE_SPECIFIC);
6992 }
6993 
6994 static int
6995 hdaa_pcm_attach(device_t dev)
6996 {
6997 	struct hdaa_pcm_devinfo *pdevinfo =
6998 	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6999 	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
7000 	struct hdaa_audio_as *as;
7001 	struct snddev_info *d;
7002 	char status[SND_STATUSLEN];
7003 	int i;
7004 
7005 	pdevinfo->chan_size = pcm_getbuffersize(dev,
7006 	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
7007 
7008 	HDA_BOOTVERBOSE(
7009 		hdaa_dump_dac(pdevinfo);
7010 		hdaa_dump_adc(pdevinfo);
7011 		hdaa_dump_mix(pdevinfo);
7012 		hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
7013 		hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
7014 		hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
7015 		hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
7016 		hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
7017 		hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
7018 		hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
7019 		hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
7020 		hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
7021 		hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
7022 		hdaa_dump_ctls(pdevinfo, NULL, 0);
7023 	);
7024 
7025 	if (resource_int_value(device_get_name(dev),
7026 	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
7027 		i &= HDA_BLK_ALIGN;
7028 		if (i < HDA_BLK_MIN)
7029 			i = HDA_BLK_MIN;
7030 		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
7031 		i = 0;
7032 		while (pdevinfo->chan_blkcnt >> i)
7033 			i++;
7034 		pdevinfo->chan_blkcnt = 1 << (i - 1);
7035 		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
7036 			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
7037 		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
7038 			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
7039 	} else
7040 		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
7041 
7042 	/*
7043 	 * We don't register interrupt handler with snd_setup_intr
7044 	 * in pcm device. Mark pcm device as MPSAFE manually.
7045 	 */
7046 	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
7047 
7048 	HDA_BOOTHVERBOSE(
7049 		device_printf(dev, "OSS mixer initialization...\n");
7050 	);
7051 	if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0)
7052 		device_printf(dev, "Can't register mixer\n");
7053 
7054 	HDA_BOOTHVERBOSE(
7055 		device_printf(dev, "Registering PCM channels...\n");
7056 	);
7057 	if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0,
7058 	    (pdevinfo->recas >= 0)?1:0) != 0)
7059 		device_printf(dev, "Can't register PCM\n");
7060 
7061 	pdevinfo->registered++;
7062 
7063 	d = device_get_softc(dev);
7064 	if (pdevinfo->playas >= 0) {
7065 		as = &devinfo->as[pdevinfo->playas];
7066 		for (i = 0; i < as->num_chans; i++)
7067 			pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class,
7068 			    &devinfo->chans[as->chans[i]]);
7069 		SYSCTL_ADD_PROC(&d->play_sysctl_ctx,
7070 		    SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO,
7071 		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7072 		    as, sizeof(as), hdaa_sysctl_32bit, "I",
7073 		    "Resolution of 32bit samples (20/24/32bit)");
7074 	}
7075 	if (pdevinfo->recas >= 0) {
7076 		as = &devinfo->as[pdevinfo->recas];
7077 		for (i = 0; i < as->num_chans; i++)
7078 			pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class,
7079 			    &devinfo->chans[as->chans[i]]);
7080 		SYSCTL_ADD_PROC(&d->rec_sysctl_ctx,
7081 		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7082 		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7083 		    as, sizeof(as), hdaa_sysctl_32bit, "I",
7084 		    "Resolution of 32bit samples (20/24/32bit)");
7085 		pdevinfo->autorecsrc = 2;
7086 		resource_int_value(device_get_name(dev), device_get_unit(dev),
7087 		    "rec.autosrc", &pdevinfo->autorecsrc);
7088 		SYSCTL_ADD_INT(&d->rec_sysctl_ctx,
7089 		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7090 		    "autosrc", CTLFLAG_RW,
7091 		    &pdevinfo->autorecsrc, 0,
7092 		    "Automatic recording source selection");
7093 	}
7094 
7095 	if (pdevinfo->mixer != NULL) {
7096 		hdaa_audio_ctl_set_defaults(pdevinfo);
7097 		hdaa_lock(devinfo);
7098 		if (pdevinfo->playas >= 0) {
7099 			as = &devinfo->as[pdevinfo->playas];
7100 			hdaa_channels_handler(as);
7101 		}
7102 		if (pdevinfo->recas >= 0) {
7103 			as = &devinfo->as[pdevinfo->recas];
7104 			hdaa_autorecsrc_handler(as, NULL);
7105 			hdaa_channels_handler(as);
7106 		}
7107 		hdaa_unlock(devinfo);
7108 	}
7109 
7110 	snprintf(status, SND_STATUSLEN, "on %s %s",
7111 	    device_get_nameunit(device_get_parent(dev)),
7112 	    PCM_KLDSTRING(snd_hda));
7113 	pcm_setstatus(dev, status);
7114 
7115 	return (0);
7116 }
7117 
7118 static int
7119 hdaa_pcm_detach(device_t dev)
7120 {
7121 	struct hdaa_pcm_devinfo *pdevinfo =
7122 	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
7123 	int err;
7124 
7125 	if (pdevinfo->registered > 0) {
7126 		err = pcm_unregister(dev);
7127 		if (err != 0)
7128 			return (err);
7129 	}
7130 
7131 	return (0);
7132 }
7133 
7134 static device_method_t hdaa_pcm_methods[] = {
7135 	/* device interface */
7136 	DEVMETHOD(device_probe,		hdaa_pcm_probe),
7137 	DEVMETHOD(device_attach,	hdaa_pcm_attach),
7138 	DEVMETHOD(device_detach,	hdaa_pcm_detach),
7139 	DEVMETHOD_END
7140 };
7141 
7142 static driver_t hdaa_pcm_driver = {
7143 	"pcm",
7144 	hdaa_pcm_methods,
7145 	PCM_SOFTC_SIZE,
7146 };
7147 
7148 DRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, NULL, NULL);
7149 MODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
7150 MODULE_VERSION(snd_hda, 1);
7151