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