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