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