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