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