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