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