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