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