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