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