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