hdaa.c revision 230507
1230130Smav/*-
2230130Smav * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3230130Smav * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4230130Smav * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
5230130Smav * All rights reserved.
6230130Smav *
7230130Smav * Redistribution and use in source and binary forms, with or without
8230130Smav * modification, are permitted provided that the following conditions
9230130Smav * are met:
10230130Smav * 1. Redistributions of source code must retain the above copyright
11230130Smav *    notice, this list of conditions and the following disclaimer.
12230130Smav * 2. Redistributions in binary form must reproduce the above copyright
13230130Smav *    notice, this list of conditions and the following disclaimer in the
14230130Smav *    documentation and/or other materials provided with the distribution.
15230130Smav *
16230130Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17230130Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18230130Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19230130Smav * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20230130Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21230130Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22230130Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23230130Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24230130Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25230130Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26230130Smav * SUCH DAMAGE.
27230130Smav */
28230130Smav
29230130Smav/*
30230130Smav * Intel High Definition Audio (Audio function) driver for FreeBSD.
31230130Smav */
32230130Smav
33230130Smav#ifdef HAVE_KERNEL_OPTION_HEADERS
34230130Smav#include "opt_snd.h"
35230130Smav#endif
36230130Smav
37230130Smav#include <dev/sound/pcm/sound.h>
38230130Smav
39230130Smav#include <sys/ctype.h>
40230130Smav#include <sys/taskqueue.h>
41230130Smav
42230130Smav#include <dev/sound/pci/hda/hdac.h>
43230130Smav#include <dev/sound/pci/hda/hdaa.h>
44230130Smav#include <dev/sound/pci/hda/hda_reg.h>
45230130Smav
46230130Smav#include "mixer_if.h"
47230130Smav
48230130SmavSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/hda/hdaa.c 230507 2012-01-24 14:17:13Z mav $");
49230130Smav
50230130Smav#define hdaa_lock(devinfo)	snd_mtxlock((devinfo)->lock)
51230130Smav#define hdaa_unlock(devinfo)	snd_mtxunlock((devinfo)->lock)
52230130Smav#define hdaa_lockassert(devinfo) snd_mtxassert((devinfo)->lock)
53230130Smav#define hdaa_lockowned(devinfo)	mtx_owned((devinfo)->lock)
54230130Smav
55230130Smavstatic const struct {
56230130Smav	char *key;
57230130Smav	uint32_t value;
58230130Smav} hdaa_quirks_tab[] = {
59230130Smav	{ "softpcmvol", HDAA_QUIRK_SOFTPCMVOL },
60230130Smav	{ "fixedrate", HDAA_QUIRK_FIXEDRATE },
61230130Smav	{ "forcestereo", HDAA_QUIRK_FORCESTEREO },
62230130Smav	{ "eapdinv", HDAA_QUIRK_EAPDINV },
63230130Smav	{ "senseinv", HDAA_QUIRK_SENSEINV },
64230130Smav	{ "ivref50", HDAA_QUIRK_IVREF50 },
65230130Smav	{ "ivref80", HDAA_QUIRK_IVREF80 },
66230130Smav	{ "ivref100", HDAA_QUIRK_IVREF100 },
67230130Smav	{ "ovref50", HDAA_QUIRK_OVREF50 },
68230130Smav	{ "ovref80", HDAA_QUIRK_OVREF80 },
69230130Smav	{ "ovref100", HDAA_QUIRK_OVREF100 },
70230130Smav	{ "ivref", HDAA_QUIRK_IVREF },
71230130Smav	{ "ovref", HDAA_QUIRK_OVREF },
72230130Smav	{ "vref", HDAA_QUIRK_VREF },
73230130Smav};
74230130Smav#define HDAA_QUIRKS_TAB_LEN	\
75230130Smav		(sizeof(hdaa_quirks_tab) / sizeof(hdaa_quirks_tab[0]))
76230130Smav
77230130Smav#define HDA_PARSE_MAXDEPTH	10
78230130Smav
79230130SmavMALLOC_DEFINE(M_HDAA, "hdaa", "HDA Audio");
80230130Smav
81230130Smavconst char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue", "Green", "Red",
82230130Smav    "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B", "Res.C", "Res.D",
83230130Smav    "White", "Other"};
84230130Smav
85230130Smavconst char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD",
86230130Smav    "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in",
87230130Smav    "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"};
88230130Smav
89230130Smavconst char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
90230130Smav
91230130Smavconst char *HDA_CONNECTORS[16] = {
92230130Smav    "Unknown", "1/8", "1/4", "ATAPI", "RCA", "Optical", "Digital", "Analog",
93230130Smav    "DIN", "XLR", "RJ-11", "Combo", "0xc", "0xd", "0xe", "Other" };
94230130Smav
95230130Smavconst char *HDA_LOCS[64] = {
96230130Smav    "0x00", "Rear", "Front", "Left", "Right", "Top", "Bottom", "Rear-panel",
97230130Smav	"Drive-bay", "0x09", "0x0a", "0x0b", "0x0c", "0x0d", "0x0e", "0x0f",
98230130Smav    "Internal", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "Riser",
99230130Smav	"0x18", "Onboard", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f",
100230130Smav    "External", "Ext-Rear", "Ext-Front", "Ext-Left", "Ext-Right", "Ext-Top", "Ext-Bottom", "0x07",
101230130Smav	"0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f",
102230130Smav    "Other", "0x31", "0x32", "0x33", "0x34", "0x35", "Other-Bott", "Lid-In",
103230130Smav	"Lid-Out", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f" };
104230130Smav
105230130Smavconst char *HDA_GPIO_ACTIONS[8] = {
106230130Smav    "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"};
107230130Smav
108230312Smavconst char *HDA_HDMI_CODING_TYPES[18] = {
109230312Smav    "undefined", "LPCM", "AC-3", "MPEG1", "MP3", "MPEG2", "AAC-LC", "DTS",
110230312Smav    "ATRAC", "DSD", "E-AC-3", "DTS-HD", "MLP", "DST", "WMAPro", "HE-AAC",
111230312Smav    "HE-AACv2", "MPEG-Surround"
112230312Smav};
113230312Smav
114230130Smav/* Default */
115230130Smavstatic uint32_t hdaa_fmt[] = {
116230130Smav	SND_FORMAT(AFMT_S16_LE, 2, 0),
117230130Smav	0
118230130Smav};
119230130Smav
120230130Smavstatic struct pcmchan_caps hdaa_caps = {48000, 48000, hdaa_fmt, 0};
121230130Smav
122230130Smavstatic const struct {
123230130Smav	uint32_t	rate;
124230130Smav	int		valid;
125230130Smav	uint16_t	base;
126230130Smav	uint16_t	mul;
127230130Smav	uint16_t	div;
128230130Smav} hda_rate_tab[] = {
129230130Smav	{   8000, 1, 0x0000, 0x0000, 0x0500 },	/* (48000 * 1) / 6 */
130230130Smav	{   9600, 0, 0x0000, 0x0000, 0x0400 },	/* (48000 * 1) / 5 */
131230130Smav	{  12000, 0, 0x0000, 0x0000, 0x0300 },	/* (48000 * 1) / 4 */
132230130Smav	{  16000, 1, 0x0000, 0x0000, 0x0200 },	/* (48000 * 1) / 3 */
133230130Smav	{  18000, 0, 0x0000, 0x1000, 0x0700 },	/* (48000 * 3) / 8 */
134230130Smav	{  19200, 0, 0x0000, 0x0800, 0x0400 },	/* (48000 * 2) / 5 */
135230130Smav	{  24000, 0, 0x0000, 0x0000, 0x0100 },	/* (48000 * 1) / 2 */
136230130Smav	{  28800, 0, 0x0000, 0x1000, 0x0400 },	/* (48000 * 3) / 5 */
137230130Smav	{  32000, 1, 0x0000, 0x0800, 0x0200 },	/* (48000 * 2) / 3 */
138230130Smav	{  36000, 0, 0x0000, 0x1000, 0x0300 },	/* (48000 * 3) / 4 */
139230130Smav	{  38400, 0, 0x0000, 0x1800, 0x0400 },	/* (48000 * 4) / 5 */
140230130Smav	{  48000, 1, 0x0000, 0x0000, 0x0000 },	/* (48000 * 1) / 1 */
141230130Smav	{  64000, 0, 0x0000, 0x1800, 0x0200 },	/* (48000 * 4) / 3 */
142230130Smav	{  72000, 0, 0x0000, 0x1000, 0x0100 },	/* (48000 * 3) / 2 */
143230130Smav	{  96000, 1, 0x0000, 0x0800, 0x0000 },	/* (48000 * 2) / 1 */
144230130Smav	{ 144000, 0, 0x0000, 0x1000, 0x0000 },	/* (48000 * 3) / 1 */
145230130Smav	{ 192000, 1, 0x0000, 0x1800, 0x0000 },	/* (48000 * 4) / 1 */
146230130Smav	{   8820, 0, 0x4000, 0x0000, 0x0400 },	/* (44100 * 1) / 5 */
147230130Smav	{  11025, 1, 0x4000, 0x0000, 0x0300 },	/* (44100 * 1) / 4 */
148230130Smav	{  12600, 0, 0x4000, 0x0800, 0x0600 },	/* (44100 * 2) / 7 */
149230130Smav	{  14700, 0, 0x4000, 0x0000, 0x0200 },	/* (44100 * 1) / 3 */
150230130Smav	{  17640, 0, 0x4000, 0x0800, 0x0400 },	/* (44100 * 2) / 5 */
151230130Smav	{  18900, 0, 0x4000, 0x1000, 0x0600 },	/* (44100 * 3) / 7 */
152230130Smav	{  22050, 1, 0x4000, 0x0000, 0x0100 },	/* (44100 * 1) / 2 */
153230130Smav	{  25200, 0, 0x4000, 0x1800, 0x0600 },	/* (44100 * 4) / 7 */
154230130Smav	{  26460, 0, 0x4000, 0x1000, 0x0400 },	/* (44100 * 3) / 5 */
155230130Smav	{  29400, 0, 0x4000, 0x0800, 0x0200 },	/* (44100 * 2) / 3 */
156230130Smav	{  33075, 0, 0x4000, 0x1000, 0x0300 },	/* (44100 * 3) / 4 */
157230130Smav	{  35280, 0, 0x4000, 0x1800, 0x0400 },	/* (44100 * 4) / 5 */
158230130Smav	{  44100, 1, 0x4000, 0x0000, 0x0000 },	/* (44100 * 1) / 1 */
159230130Smav	{  58800, 0, 0x4000, 0x1800, 0x0200 },	/* (44100 * 4) / 3 */
160230130Smav	{  66150, 0, 0x4000, 0x1000, 0x0100 },	/* (44100 * 3) / 2 */
161230130Smav	{  88200, 1, 0x4000, 0x0800, 0x0000 },	/* (44100 * 2) / 1 */
162230130Smav	{ 132300, 0, 0x4000, 0x1000, 0x0000 },	/* (44100 * 3) / 1 */
163230130Smav	{ 176400, 1, 0x4000, 0x1800, 0x0000 },	/* (44100 * 4) / 1 */
164230130Smav};
165230130Smav#define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
166230130Smav
167230451Smavconst static char *ossnames[] = SOUND_DEVICE_NAMES;
168230451Smav
169230130Smav/****************************************************************************
170230130Smav * Function prototypes
171230130Smav ****************************************************************************/
172230130Smavstatic int	hdaa_pcmchannel_setup(struct hdaa_chan *);
173230130Smav
174230130Smavstatic void	hdaa_widget_connection_select(struct hdaa_widget *, uint8_t);
175230130Smavstatic void	hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *,
176230130Smav						uint32_t, int, int);
177230130Smavstatic struct	hdaa_audio_ctl *hdaa_audio_ctl_amp_get(struct hdaa_devinfo *,
178230130Smav							nid_t, int, int, int);
179230130Smavstatic void	hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *,
180230130Smav				nid_t, int, int, int, int, int, int);
181230130Smav
182230130Smavstatic void	hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf);
183230130Smav
184230130Smavstatic char *
185230130Smavhdaa_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
186230130Smav{
187230130Smav	int i, first = 1;
188230130Smav
189230130Smav	bzero(buf, len);
190230130Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
191230130Smav		if (mask & (1 << i)) {
192230130Smav			if (first == 0)
193230130Smav				strlcat(buf, ", ", len);
194230451Smav			strlcat(buf, ossnames[i], len);
195230130Smav			first = 0;
196230130Smav		}
197230130Smav	}
198230130Smav	return (buf);
199230130Smav}
200230130Smav
201230130Smavstatic struct hdaa_audio_ctl *
202230130Smavhdaa_audio_ctl_each(struct hdaa_devinfo *devinfo, int *index)
203230130Smav{
204230130Smav	if (devinfo == NULL ||
205230130Smav	    index == NULL || devinfo->ctl == NULL ||
206230130Smav	    devinfo->ctlcnt < 1 ||
207230130Smav	    *index < 0 || *index >= devinfo->ctlcnt)
208230130Smav		return (NULL);
209230130Smav	return (&devinfo->ctl[(*index)++]);
210230130Smav}
211230130Smav
212230130Smavstatic struct hdaa_audio_ctl *
213230130Smavhdaa_audio_ctl_amp_get(struct hdaa_devinfo *devinfo, nid_t nid, int dir,
214230130Smav						int index, int cnt)
215230130Smav{
216230130Smav	struct hdaa_audio_ctl *ctl;
217230130Smav	int i, found = 0;
218230130Smav
219230130Smav	if (devinfo == NULL || devinfo->ctl == NULL)
220230130Smav		return (NULL);
221230130Smav
222230130Smav	i = 0;
223230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
224230130Smav		if (ctl->enable == 0)
225230130Smav			continue;
226230130Smav		if (ctl->widget->nid != nid)
227230130Smav			continue;
228230130Smav		if (dir && ctl->ndir != dir)
229230130Smav			continue;
230230130Smav		if (index >= 0 && ctl->ndir == HDAA_CTL_IN &&
231230130Smav		    ctl->dir == ctl->ndir && ctl->index != index)
232230130Smav			continue;
233230130Smav		found++;
234230130Smav		if (found == cnt || cnt <= 0)
235230130Smav			return (ctl);
236230130Smav	}
237230130Smav
238230130Smav	return (NULL);
239230130Smav}
240230130Smav
241230130Smav/*
242230130Smav * Jack detection (Speaker/HP redirection) event handler.
243230130Smav */
244230130Smavstatic void
245230312Smavhdaa_hp_switch_handler(struct hdaa_widget *w)
246230130Smav{
247230312Smav	struct hdaa_devinfo *devinfo = w->devinfo;
248230130Smav	struct hdaa_audio_as *as;
249230312Smav	struct hdaa_widget *w1;
250230130Smav	struct hdaa_audio_ctl *ctl;
251230130Smav	uint32_t val, res;
252230130Smav	int j;
253230130Smav
254230312Smav	if (w->enable == 0 || w->type !=
255230130Smav	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
256230130Smav		return;
257230130Smav
258230312Smav	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
259230130Smav	HDA_BOOTVERBOSE(
260230130Smav		device_printf(devinfo->dev,
261230312Smav		    "Pin sense: nid=%d sence=0x%08x", w->nid, res);
262230130Smav	);
263230130Smav	res = (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) != 0;
264230130Smav	if (devinfo->quirks & HDAA_QUIRK_SENSEINV)
265230130Smav		res ^= 1;
266230130Smav	HDA_BOOTVERBOSE(
267230312Smav		printf(" (%sconnected)\n", res == 0 ? "dis" : "");
268230130Smav	);
269230130Smav
270230312Smav	as = &devinfo->as[w->bindas];
271230312Smav	if (as->hpredir < 0 || as->pins[15] != w->nid)
272230312Smav		return;
273230312Smav
274230130Smav	/* (Un)Mute headphone pin. */
275230130Smav	ctl = hdaa_audio_ctl_amp_get(devinfo,
276230312Smav	    w->nid, HDAA_CTL_IN, -1, 1);
277230130Smav	if (ctl != NULL && ctl->mute) {
278230130Smav		/* If pin has muter - use it. */
279230130Smav		val = (res != 0) ? 0 : 1;
280230130Smav		if (val != ctl->forcemute) {
281230130Smav			ctl->forcemute = val;
282230130Smav			hdaa_audio_ctl_amp_set(ctl,
283230130Smav			    HDAA_AMP_MUTE_DEFAULT,
284230130Smav			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
285230130Smav		}
286230130Smav	} else {
287230130Smav		/* If there is no muter - disable pin output. */
288230312Smav		if (res != 0)
289230312Smav			val = w->wclass.pin.ctrl |
290230312Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
291230312Smav		else
292230312Smav			val = w->wclass.pin.ctrl &
293230312Smav			    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
294230312Smav		if (val != w->wclass.pin.ctrl) {
295230312Smav			w->wclass.pin.ctrl = val;
296230312Smav			hda_command(devinfo->dev,
297230312Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
298230312Smav			    w->nid, w->wclass.pin.ctrl));
299230130Smav		}
300230130Smav	}
301230130Smav	/* (Un)Mute other pins. */
302230130Smav	for (j = 0; j < 15; j++) {
303230130Smav		if (as->pins[j] <= 0)
304230130Smav			continue;
305230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo,
306230130Smav		    as->pins[j], HDAA_CTL_IN, -1, 1);
307230130Smav		if (ctl != NULL && ctl->mute) {
308230130Smav			/* If pin has muter - use it. */
309230130Smav			val = (res != 0) ? 1 : 0;
310230130Smav			if (val == ctl->forcemute)
311230130Smav				continue;
312230130Smav			ctl->forcemute = val;
313230130Smav			hdaa_audio_ctl_amp_set(ctl,
314230130Smav			    HDAA_AMP_MUTE_DEFAULT,
315230130Smav			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
316230130Smav			continue;
317230130Smav		}
318230130Smav		/* If there is no muter - disable pin output. */
319230312Smav		w1 = hdaa_widget_get(devinfo, as->pins[j]);
320230312Smav		if (w1 != NULL && w1->type ==
321230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
322230130Smav			if (res != 0)
323230312Smav				val = w1->wclass.pin.ctrl &
324230130Smav				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
325230130Smav			else
326230312Smav				val = w1->wclass.pin.ctrl |
327230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
328230312Smav			if (val != w1->wclass.pin.ctrl) {
329230312Smav				w1->wclass.pin.ctrl = val;
330230130Smav				hda_command(devinfo->dev,
331230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
332230312Smav				    w1->nid, w1->wclass.pin.ctrl));
333230130Smav			}
334230130Smav		}
335230130Smav	}
336230130Smav}
337230130Smav
338230130Smav/*
339230130Smav * Callback for poll based jack detection.
340230130Smav */
341230130Smavstatic void
342230130Smavhdaa_jack_poll_callback(void *arg)
343230130Smav{
344230130Smav	struct hdaa_devinfo *devinfo = arg;
345230312Smav	struct hdaa_widget *w;
346230130Smav	int i;
347230130Smav
348230130Smav	hdaa_lock(devinfo);
349230130Smav	if (devinfo->poll_ival == 0) {
350230130Smav		hdaa_unlock(devinfo);
351230130Smav		return;
352230130Smav	}
353230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
354230130Smav		if (devinfo->as[i].hpredir < 0)
355230130Smav			continue;
356230312Smav		w = hdaa_widget_get(devinfo, devinfo->as[i].pins[15]);
357230312Smav		if (w == NULL || w->enable == 0 || w->type !=
358230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
359230312Smav			continue;
360230312Smav		hdaa_hp_switch_handler(w);
361230130Smav	}
362230130Smav	callout_reset(&devinfo->poll_jack, devinfo->poll_ival,
363230130Smav	    hdaa_jack_poll_callback, devinfo);
364230130Smav	hdaa_unlock(devinfo);
365230130Smav}
366230130Smav
367230130Smav/*
368230130Smav * Jack detection initializer.
369230130Smav */
370230130Smavstatic void
371230130Smavhdaa_hp_switch_init(struct hdaa_devinfo *devinfo)
372230130Smav{
373230130Smav	struct hdaa_audio_as *as = devinfo->as;
374230130Smav        struct hdaa_widget *w;
375230130Smav        int i, poll = 0;
376230130Smav
377230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
378230130Smav		if (as[i].hpredir < 0)
379230130Smav			continue;
380230130Smav
381230130Smav		w = hdaa_widget_get(devinfo, as[i].pins[15]);
382230130Smav		if (w == NULL || w->enable == 0 || w->type !=
383230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
384230130Smav			continue;
385230130Smav		if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
386230130Smav		    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0) {
387230130Smav			device_printf(devinfo->dev,
388230130Smav			    "No jack detection support at pin %d\n",
389230130Smav			    as[i].pins[15]);
390230130Smav			continue;
391230130Smav		}
392230312Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap) &&
393230312Smav		    w->unsol < 0) {
394230312Smav			w->unsol = HDAC_UNSOL_ALLOC(
395230130Smav			    device_get_parent(devinfo->dev), devinfo->dev,
396230130Smav			    w->nid);
397230130Smav			hda_command(devinfo->dev,
398230130Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid,
399230130Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE |
400230312Smav			    w->unsol));
401230312Smav		}
402230312Smav		if (w->unsol < 0)
403230130Smav			poll = 1;
404230130Smav		HDA_BOOTVERBOSE(
405230130Smav			device_printf(devinfo->dev,
406230130Smav			    "Headphones redirection "
407230130Smav			    "for as=%d nid=%d using %s.\n",
408230130Smav			    i, w->nid,
409230130Smav			    (poll != 0) ? "polling" : "unsolicited responses");
410230130Smav		);
411230312Smav		hdaa_hp_switch_handler(w);
412230130Smav	}
413230130Smav	if (poll) {
414230130Smav		callout_reset(&devinfo->poll_jack, 1,
415230130Smav		    hdaa_jack_poll_callback, devinfo);
416230130Smav	}
417230130Smav}
418230130Smav
419230130Smavstatic void
420230130Smavhdaa_hp_switch_deinit(struct hdaa_devinfo *devinfo)
421230130Smav{
422230130Smav	struct hdaa_audio_as *as = devinfo->as;
423230130Smav        struct hdaa_widget *w;
424230130Smav        int i;
425230130Smav
426230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
427230130Smav		w = hdaa_widget_get(devinfo, as[i].pins[15]);
428230130Smav		if (w == NULL || w->enable == 0 || w->type !=
429230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
430230130Smav			continue;
431230312Smav		if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
432230312Smav		    HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
433230312Smav			continue;
434230312Smav		if (w->unsol < 0)
435230312Smav			continue;
436230130Smav		hda_command(devinfo->dev,
437230130Smav		    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0));
438230130Smav		HDAC_UNSOL_FREE(
439230130Smav		    device_get_parent(devinfo->dev), devinfo->dev,
440230312Smav		    w->unsol);
441230312Smav		w->unsol = -1;
442230130Smav	}
443230130Smav}
444230130Smav
445230312Smavstatic void
446230312Smavhdaa_eld_dump(struct hdaa_widget *w)
447230312Smav{
448230312Smav	struct hdaa_devinfo *devinfo = w->devinfo;
449230312Smav	device_t dev = devinfo->dev;
450230312Smav	uint8_t *sad;
451230312Smav	int len, mnl, i, sadc, fmt;
452230312Smav
453230312Smav	if (w->eld == NULL || w->eld_len < 4)
454230312Smav		return;
455230312Smav	device_printf(dev,
456230312Smav	    "ELD nid=%d: ELD_Ver=%u Baseline_ELD_Len=%u\n",
457230312Smav	    w->nid, w->eld[0] >> 3, w->eld[2]);
458230312Smav	if ((w->eld[0] >> 3) != 0x02)
459230312Smav		return;
460230312Smav	len = min(w->eld_len, (u_int)w->eld[2] * 4);
461230312Smav	mnl = w->eld[4] & 0x1f;
462230312Smav	device_printf(dev,
463230312Smav	    "ELD nid=%d: CEA_EDID_Ver=%u MNL=%u\n",
464230312Smav	    w->nid, w->eld[4] >> 5, mnl);
465230312Smav	sadc = w->eld[5] >> 4;
466230312Smav	device_printf(dev,
467230312Smav	    "ELD nid=%d: SAD_Count=%u Conn_Type=%u S_AI=%u HDCP=%u\n",
468230312Smav	    w->nid, sadc, (w->eld[5] >> 2) & 0x3,
469230312Smav	    (w->eld[5] >> 1) & 0x1, w->eld[5] & 0x1);
470230312Smav	device_printf(dev,
471230312Smav	    "ELD nid=%d: Aud_Synch_Delay=%ums\n",
472230312Smav	    w->nid, w->eld[6] * 2);
473230312Smav	device_printf(dev,
474230312Smav	    "ELD nid=%d: Channels=0x%b\n",
475230312Smav	    w->nid, w->eld[7],
476230312Smav	    "\020\07RLRC\06FLRC\05RC\04RLR\03FC\02LFE\01FLR");
477230312Smav	device_printf(dev,
478230312Smav	    "ELD nid=%d: Port_ID=0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
479230312Smav	    w->nid, w->eld[8], w->eld[9], w->eld[10], w->eld[11],
480230312Smav	    w->eld[12], w->eld[13], w->eld[14], w->eld[15]);
481230312Smav	device_printf(dev,
482230312Smav	    "ELD nid=%d: Manufacturer_Name=0x%02x%02x\n",
483230312Smav	    w->nid, w->eld[16], w->eld[17]);
484230312Smav	device_printf(dev,
485230312Smav	    "ELD nid=%d: Product_Code=0x%02x%02x\n",
486230312Smav	    w->nid, w->eld[18], w->eld[19]);
487230312Smav	device_printf(dev,
488230312Smav	    "ELD nid=%d: Monitor_Name_String='%.*s'\n",
489230312Smav	    w->nid, mnl, &w->eld[20]);
490230312Smav	for (i = 0; i < sadc; i++) {
491230312Smav		sad = &w->eld[20 + mnl + i * 3];
492230312Smav		fmt = (sad[0] >> 3) & 0x0f;
493230312Smav		if (fmt == HDA_HDMI_CODING_TYPE_REF_CTX) {
494230312Smav			fmt = (sad[2] >> 3) & 0x1f;
495230312Smav			if (fmt < 1 || fmt > 3)
496230312Smav				fmt = 0;
497230312Smav			else
498230312Smav				fmt += 14;
499230312Smav		}
500230312Smav		device_printf(dev,
501230312Smav		    "ELD nid=%d: %s %dch freqs=0x%b",
502230312Smav		    w->nid, HDA_HDMI_CODING_TYPES[fmt], (sad[0] & 0x07) + 1,
503230312Smav		    sad[1], "\020\007192\006176\00596\00488\00348\00244\00132");
504230312Smav		switch (fmt) {
505230312Smav		case HDA_HDMI_CODING_TYPE_LPCM:
506230312Smav			printf(" sizes=0x%b",
507230312Smav			    sad[2] & 0x07, "\020\00324\00220\00116");
508230312Smav			break;
509230312Smav		case HDA_HDMI_CODING_TYPE_AC3:
510230312Smav		case HDA_HDMI_CODING_TYPE_MPEG1:
511230312Smav		case HDA_HDMI_CODING_TYPE_MP3:
512230312Smav		case HDA_HDMI_CODING_TYPE_MPEG2:
513230312Smav		case HDA_HDMI_CODING_TYPE_AACLC:
514230312Smav		case HDA_HDMI_CODING_TYPE_DTS:
515230312Smav		case HDA_HDMI_CODING_TYPE_ATRAC:
516230312Smav			printf(" max_bitrate=%d", sad[2] * 8000);
517230312Smav			break;
518230312Smav		case HDA_HDMI_CODING_TYPE_WMAPRO:
519230312Smav			printf(" profile=%d", sad[2] & 0x07);
520230312Smav			break;
521230312Smav		}
522230312Smav		printf("\n");
523230312Smav	}
524230312Smav}
525230312Smav
526230312Smavstatic void
527230312Smavhdaa_eld_handler(struct hdaa_widget *w)
528230312Smav{
529230312Smav	struct hdaa_devinfo *devinfo = w->devinfo;
530230312Smav	uint32_t res;
531230312Smav	int i;
532230312Smav
533230312Smav	if (w->enable == 0 || w->type !=
534230312Smav	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
535230312Smav		return;
536230312Smav
537230312Smav	if (w->eld != NULL) {
538230312Smav		w->eld_len = 0;
539230312Smav		free(w->eld, M_HDAA);
540230312Smav		w->eld = NULL;
541230312Smav	}
542230312Smav
543230312Smav	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
544230312Smav	HDA_BOOTVERBOSE(
545230312Smav		device_printf(devinfo->dev,
546230312Smav		    "Pin sense: nid=%d sence=0x%08x "
547230312Smav		    "(%sconnected, ELD %svalid)\n",
548230312Smav		    w->nid, res,
549230312Smav		    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ? "" : "dis",
550230312Smav		    (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) ? "" : "in");
551230312Smav	);
552230312Smav	if ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) == 0)
553230312Smav		return;
554230312Smav
555230312Smav	res = hda_command(devinfo->dev,
556230312Smav	    HDA_CMD_GET_HDMI_DIP_SIZE(0, w->nid, 0x08));
557230312Smav	if (res == HDA_INVALID)
558230312Smav		return;
559230312Smav	w->eld_len = res & 0xff;
560230312Smav	if (w->eld_len != 0)
561230312Smav		w->eld = malloc(w->eld_len, M_HDAA, M_ZERO | M_NOWAIT);
562230312Smav	if (w->eld == NULL) {
563230312Smav		w->eld_len = 0;
564230312Smav		return;
565230312Smav	}
566230312Smav
567230312Smav	for (i = 0; i < w->eld_len; i++) {
568230312Smav		res = hda_command(devinfo->dev,
569230312Smav		    HDA_CMD_GET_HDMI_ELDD(0, w->nid, i));
570230312Smav		if (res & 0x80000000)
571230312Smav			w->eld[i] = res & 0xff;
572230312Smav	}
573230312Smav	HDA_BOOTVERBOSE(
574230312Smav		hdaa_eld_dump(w);
575230312Smav	);
576230312Smav}
577230312Smav
578230312Smav
579230312Smavstatic void
580230312Smavhdaa_eld_init(struct hdaa_devinfo *devinfo)
581230312Smav{
582230312Smav        struct hdaa_widget *w;
583230312Smav        int i;
584230312Smav
585230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
586230312Smav		w = hdaa_widget_get(devinfo, i);
587230312Smav		if (w == NULL || w->type !=
588230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
589230312Smav			continue;
590230312Smav		if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) &&
591230312Smav		    !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
592230312Smav			continue;
593230312Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap) &&
594230312Smav		    w->unsol < 0) {
595230312Smav			w->unsol = HDAC_UNSOL_ALLOC(
596230312Smav			    device_get_parent(devinfo->dev), devinfo->dev,
597230312Smav			    w->nid);
598230312Smav			hda_command(devinfo->dev,
599230312Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid,
600230312Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE |
601230312Smav			    w->unsol));
602230312Smav		}
603230312Smav		hdaa_eld_handler(w);
604230312Smav	}
605230312Smav}
606230312Smav
607230312Smavstatic void
608230312Smavhdaa_eld_deinit(struct hdaa_devinfo *devinfo)
609230312Smav{
610230312Smav	struct hdaa_widget *w;
611230312Smav	int i;
612230312Smav
613230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
614230312Smav		w = hdaa_widget_get(devinfo, i);
615230312Smav		if (w == NULL || w->type !=
616230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
617230312Smav			continue;
618230312Smav		if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) &&
619230312Smav		    !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
620230312Smav			continue;
621230312Smav		if (w->unsol < 0)
622230312Smav			continue;
623230312Smav		hda_command(devinfo->dev,
624230312Smav		    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0));
625230312Smav		HDAC_UNSOL_FREE(
626230312Smav		    device_get_parent(devinfo->dev), devinfo->dev,
627230312Smav		    w->unsol);
628230312Smav		w->unsol = -1;
629230312Smav	}
630230312Smav}
631230312Smav
632230130Smavuint32_t
633230130Smavhdaa_widget_pin_patch(uint32_t config, const char *str)
634230130Smav{
635230130Smav	char buf[256];
636230130Smav	char *key, *value, *rest, *bad;
637230130Smav	int ival, i;
638230130Smav
639230130Smav	strlcpy(buf, str, sizeof(buf));
640230130Smav	rest = buf;
641230130Smav	while ((key = strsep(&rest, "=")) != NULL) {
642230130Smav		value = strsep(&rest, " \t");
643230130Smav		if (value == NULL)
644230130Smav			break;
645230130Smav		ival = strtol(value, &bad, 10);
646230130Smav		if (strcmp(key, "seq") == 0) {
647230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK;
648230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_SEQUENCE_SHIFT) &
649230130Smav			    HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK);
650230130Smav		} else if (strcmp(key, "as") == 0) {
651230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK;
652230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT) &
653230130Smav			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK);
654230130Smav		} else if (strcmp(key, "misc") == 0) {
655230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_MISC_MASK;
656230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_MISC_SHIFT) &
657230130Smav			    HDA_CONFIG_DEFAULTCONF_MISC_MASK);
658230130Smav		} else if (strcmp(key, "color") == 0) {
659230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_COLOR_MASK;
660230130Smav			if (bad[0] == 0) {
661230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT) &
662230130Smav				    HDA_CONFIG_DEFAULTCONF_COLOR_MASK);
663230130Smav			};
664230130Smav			for (i = 0; i < 16; i++) {
665230130Smav				if (strcasecmp(HDA_COLORS[i], value) == 0) {
666230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT);
667230130Smav					break;
668230130Smav				}
669230130Smav			}
670230130Smav		} else if (strcmp(key, "ctype") == 0) {
671230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK;
672230130Smav			if (bad[0] == 0) {
673230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT) &
674230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK);
675230130Smav			}
676230130Smav			for (i = 0; i < 16; i++) {
677230130Smav				if (strcasecmp(HDA_CONNECTORS[i], value) == 0) {
678230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT);
679230130Smav					break;
680230130Smav				}
681230130Smav			}
682230130Smav		} else if (strcmp(key, "device") == 0) {
683230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
684230130Smav			if (bad[0] == 0) {
685230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT) &
686230130Smav				    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK);
687230130Smav				continue;
688230130Smav			};
689230130Smav			for (i = 0; i < 16; i++) {
690230130Smav				if (strcasecmp(HDA_DEVS[i], value) == 0) {
691230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT);
692230130Smav					break;
693230130Smav				}
694230130Smav			}
695230130Smav		} else if (strcmp(key, "loc") == 0) {
696230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_LOCATION_MASK;
697230130Smav			if (bad[0] == 0) {
698230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT) &
699230130Smav				    HDA_CONFIG_DEFAULTCONF_LOCATION_MASK);
700230130Smav				continue;
701230130Smav			}
702230130Smav			for (i = 0; i < 64; i++) {
703230130Smav				if (strcasecmp(HDA_LOCS[i], value) == 0) {
704230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT);
705230130Smav					break;
706230130Smav				}
707230130Smav			}
708230130Smav		} else if (strcmp(key, "conn") == 0) {
709230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
710230130Smav			if (bad[0] == 0) {
711230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT) &
712230130Smav				    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
713230130Smav				continue;
714230130Smav			};
715230130Smav			for (i = 0; i < 4; i++) {
716230130Smav				if (strcasecmp(HDA_CONNS[i], value) == 0) {
717230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT);
718230130Smav					break;
719230130Smav				}
720230130Smav			}
721230130Smav		}
722230130Smav	}
723230130Smav	return (config);
724230130Smav}
725230130Smav
726230130Smavuint32_t
727230130Smavhdaa_gpio_patch(uint32_t gpio, const char *str)
728230130Smav{
729230130Smav	char buf[256];
730230130Smav	char *key, *value, *rest;
731230130Smav	int ikey, i;
732230130Smav
733230130Smav	strlcpy(buf, str, sizeof(buf));
734230130Smav	rest = buf;
735230130Smav	while ((key = strsep(&rest, "=")) != NULL) {
736230130Smav		value = strsep(&rest, " \t");
737230130Smav		if (value == NULL)
738230130Smav			break;
739230130Smav		ikey = strtol(key, NULL, 10);
740230130Smav		if (ikey < 0 || ikey > 7)
741230130Smav			continue;
742230130Smav		for (i = 0; i < 7; i++) {
743230130Smav			if (strcasecmp(HDA_GPIO_ACTIONS[i], value) == 0) {
744230130Smav				gpio &= ~HDAA_GPIO_MASK(ikey);
745230130Smav				gpio |= i << HDAA_GPIO_SHIFT(ikey);
746230130Smav				break;
747230130Smav			}
748230130Smav		}
749230130Smav	}
750230130Smav	return (gpio);
751230130Smav}
752230130Smav
753230130Smavstatic void
754230130Smavhdaa_local_patch_pin(struct hdaa_widget *w)
755230130Smav{
756230130Smav	device_t dev = w->devinfo->dev;
757230130Smav	const char *res = NULL;
758230130Smav	uint32_t config, orig;
759230130Smav	char buf[32];
760230130Smav
761230130Smav	config = orig = w->wclass.pin.config;
762230130Smav	snprintf(buf, sizeof(buf), "cad%u.nid%u.config",
763230130Smav	    hda_get_codec_id(dev), w->nid);
764230130Smav	if (resource_string_value(device_get_name(
765230130Smav	    device_get_parent(device_get_parent(dev))),
766230130Smav	    device_get_unit(device_get_parent(device_get_parent(dev))),
767230130Smav	    buf, &res) == 0) {
768230130Smav		if (strncmp(res, "0x", 2) == 0) {
769230130Smav			config = strtol(res + 2, NULL, 16);
770230130Smav		} else {
771230130Smav			config = hdaa_widget_pin_patch(config, res);
772230130Smav		}
773230130Smav	}
774230130Smav	snprintf(buf, sizeof(buf), "nid%u.config", w->nid);
775230130Smav	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
776230130Smav	    buf, &res) == 0) {
777230130Smav		if (strncmp(res, "0x", 2) == 0) {
778230130Smav			config = strtol(res + 2, NULL, 16);
779230130Smav		} else {
780230130Smav			config = hdaa_widget_pin_patch(config, res);
781230130Smav		}
782230130Smav	}
783230130Smav	HDA_BOOTVERBOSE(
784230130Smav		if (config != orig)
785230130Smav			device_printf(w->devinfo->dev,
786230130Smav			    "Patching pin config nid=%u 0x%08x -> 0x%08x\n",
787230130Smav			    w->nid, orig, config);
788230130Smav	);
789230130Smav	w->wclass.pin.newconf = w->wclass.pin.config = config;
790230130Smav}
791230130Smav
792230130Smavstatic int
793230130Smavhdaa_sysctl_config(SYSCTL_HANDLER_ARGS)
794230130Smav{
795230130Smav	char buf[256];
796230130Smav	int error;
797230130Smav	uint32_t conf;
798230130Smav
799230130Smav	conf = *(uint32_t *)oidp->oid_arg1;
800230130Smav	snprintf(buf, sizeof(buf), "0x%08x as=%d seq=%d "
801230130Smav	    "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d",
802230130Smav	    conf,
803230130Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
804230130Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
805230130Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
806230130Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
807230130Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
808230130Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
809230130Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
810230130Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf));
811230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
812230130Smav	if (error != 0 || req->newptr == NULL)
813230130Smav		return (error);
814230130Smav	if (strncmp(buf, "0x", 2) == 0)
815230130Smav		conf = strtol(buf + 2, NULL, 16);
816230130Smav	else
817230130Smav		conf = hdaa_widget_pin_patch(conf, buf);
818230130Smav	*(uint32_t *)oidp->oid_arg1 = conf;
819230130Smav	return (0);
820230130Smav}
821230130Smav
822230130Smavstatic void
823230130Smavhdaa_config_fetch(const char *str, uint32_t *on, uint32_t *off)
824230130Smav{
825230130Smav	int i = 0, j, k, len, inv;
826230130Smav
827230130Smav	for (;;) {
828230130Smav		while (str[i] != '\0' &&
829230130Smav		    (str[i] == ',' || isspace(str[i]) != 0))
830230130Smav			i++;
831230130Smav		if (str[i] == '\0')
832230130Smav			return;
833230130Smav		j = i;
834230130Smav		while (str[j] != '\0' &&
835230130Smav		    !(str[j] == ',' || isspace(str[j]) != 0))
836230130Smav			j++;
837230130Smav		len = j - i;
838230130Smav		if (len > 2 && strncmp(str + i, "no", 2) == 0)
839230130Smav			inv = 2;
840230130Smav		else
841230130Smav			inv = 0;
842230130Smav		for (k = 0; len > inv && k < HDAA_QUIRKS_TAB_LEN; k++) {
843230130Smav			if (strncmp(str + i + inv,
844230130Smav			    hdaa_quirks_tab[k].key, len - inv) != 0)
845230130Smav				continue;
846230130Smav			if (len - inv != strlen(hdaa_quirks_tab[k].key))
847230130Smav				continue;
848230130Smav			if (inv == 0) {
849230130Smav				*on |= hdaa_quirks_tab[k].value;
850230130Smav				*off &= ~hdaa_quirks_tab[k].value;
851230130Smav			} else {
852230130Smav				*off |= hdaa_quirks_tab[k].value;
853230130Smav				*on &= ~hdaa_quirks_tab[k].value;
854230130Smav			}
855230130Smav			break;
856230130Smav		}
857230130Smav		i = j;
858230130Smav	}
859230130Smav}
860230130Smav
861230130Smavstatic int
862230130Smavhdaa_sysctl_quirks(SYSCTL_HANDLER_ARGS)
863230130Smav{
864230130Smav	char buf[256];
865230130Smav	int error, n = 0, i;
866230130Smav	uint32_t quirks, quirks_off;
867230130Smav
868230130Smav	quirks = *(uint32_t *)oidp->oid_arg1;
869230130Smav	buf[0] = 0;
870230130Smav	for (i = 0; i < HDAA_QUIRKS_TAB_LEN; i++) {
871230130Smav		if ((quirks & hdaa_quirks_tab[i].value) != 0)
872230130Smav			n += snprintf(buf + n, sizeof(buf) - n, "%s%s",
873230130Smav			    n != 0 ? "," : "", hdaa_quirks_tab[i].key);
874230130Smav	}
875230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
876230130Smav	if (error != 0 || req->newptr == NULL)
877230130Smav		return (error);
878230130Smav	if (strncmp(buf, "0x", 2) == 0)
879230130Smav		quirks = strtol(buf + 2, NULL, 16);
880230130Smav	else {
881230130Smav		quirks = 0;
882230130Smav		hdaa_config_fetch(buf, &quirks, &quirks_off);
883230130Smav	}
884230130Smav	*(uint32_t *)oidp->oid_arg1 = quirks;
885230130Smav	return (0);
886230130Smav}
887230130Smav
888230130Smavstatic void
889230130Smavhdaa_local_patch(struct hdaa_devinfo *devinfo)
890230130Smav{
891230130Smav	struct hdaa_widget *w;
892230130Smav	const char *res = NULL;
893230130Smav	uint32_t quirks_on = 0, quirks_off = 0, x;
894230130Smav	int i;
895230130Smav
896230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
897230130Smav		w = hdaa_widget_get(devinfo, i);
898230130Smav		if (w == NULL)
899230130Smav			continue;
900230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
901230130Smav			hdaa_local_patch_pin(w);
902230130Smav	}
903230130Smav
904230130Smav	if (resource_string_value(device_get_name(devinfo->dev),
905230130Smav	    device_get_unit(devinfo->dev), "config", &res) == 0) {
906230130Smav		if (res != NULL && strlen(res) > 0)
907230130Smav			hdaa_config_fetch(res, &quirks_on, &quirks_off);
908230130Smav		devinfo->quirks |= quirks_on;
909230130Smav		devinfo->quirks &= ~quirks_off;
910230130Smav	}
911230130Smav	if (devinfo->newquirks == -1)
912230130Smav		devinfo->newquirks = devinfo->quirks;
913230130Smav	else
914230130Smav		devinfo->quirks = devinfo->newquirks;
915230130Smav	HDA_BOOTHVERBOSE(
916230130Smav		device_printf(devinfo->dev,
917230130Smav		    "Config options: 0x%08x\n", devinfo->quirks);
918230130Smav	);
919230130Smav
920230130Smav	if (resource_string_value(device_get_name(devinfo->dev),
921230130Smav	    device_get_unit(devinfo->dev), "gpio_config", &res) == 0) {
922230130Smav		if (strncmp(res, "0x", 2) == 0) {
923230130Smav			devinfo->gpio = strtol(res + 2, NULL, 16);
924230130Smav		} else {
925230130Smav			devinfo->gpio = hdaa_gpio_patch(devinfo->gpio, res);
926230130Smav		}
927230130Smav	}
928230130Smav	if (devinfo->newgpio == -1)
929230130Smav		devinfo->newgpio = devinfo->gpio;
930230130Smav	else
931230130Smav		devinfo->gpio = devinfo->newgpio;
932230130Smav	if (devinfo->newgpo == -1)
933230130Smav		devinfo->newgpo = devinfo->gpo;
934230130Smav	else
935230130Smav		devinfo->gpo = devinfo->newgpo;
936230130Smav	HDA_BOOTHVERBOSE(
937230130Smav		device_printf(devinfo->dev, "GPIO config options:");
938230130Smav		for (i = 0; i < 7; i++) {
939230130Smav			x = (devinfo->gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
940230130Smav			if (x != 0)
941230130Smav				printf(" %d=%s", i, HDA_GPIO_ACTIONS[x]);
942230130Smav		}
943230130Smav		printf("\n");
944230130Smav	);
945230130Smav}
946230130Smav
947230130Smavstatic void
948230130Smavhdaa_widget_connection_parse(struct hdaa_widget *w)
949230130Smav{
950230130Smav	uint32_t res;
951230130Smav	int i, j, max, ents, entnum;
952230130Smav	nid_t nid = w->nid;
953230130Smav	nid_t cnid, addcnid, prevcnid;
954230130Smav
955230130Smav	w->nconns = 0;
956230130Smav
957230130Smav	res = hda_command(w->devinfo->dev,
958230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_CONN_LIST_LENGTH));
959230130Smav
960230130Smav	ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res);
961230130Smav
962230130Smav	if (ents < 1)
963230130Smav		return;
964230130Smav
965230130Smav	entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4;
966230130Smav	max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1;
967230130Smav	prevcnid = 0;
968230130Smav
969230130Smav#define CONN_RMASK(e)		(1 << ((32 / (e)) - 1))
970230130Smav#define CONN_NMASK(e)		(CONN_RMASK(e) - 1)
971230130Smav#define CONN_RESVAL(r, e, n)	((r) >> ((32 / (e)) * (n)))
972230130Smav#define CONN_RANGE(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_RMASK(e))
973230130Smav#define CONN_CNID(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_NMASK(e))
974230130Smav
975230130Smav	for (i = 0; i < ents; i += entnum) {
976230130Smav		res = hda_command(w->devinfo->dev,
977230130Smav		    HDA_CMD_GET_CONN_LIST_ENTRY(0, nid, i));
978230130Smav		for (j = 0; j < entnum; j++) {
979230130Smav			cnid = CONN_CNID(res, entnum, j);
980230130Smav			if (cnid == 0) {
981230130Smav				if (w->nconns < ents)
982230130Smav					device_printf(w->devinfo->dev,
983230130Smav					    "WARNING: nid=%d has zero cnid "
984230130Smav					    "entnum=%d j=%d index=%d "
985230130Smav					    "entries=%d found=%d res=0x%08x\n",
986230130Smav					    nid, entnum, j, i,
987230130Smav					    ents, w->nconns, res);
988230130Smav				else
989230130Smav					goto getconns_out;
990230130Smav			}
991230130Smav			if (cnid < w->devinfo->startnode ||
992230130Smav			    cnid >= w->devinfo->endnode) {
993230130Smav				HDA_BOOTVERBOSE(
994230130Smav					device_printf(w->devinfo->dev,
995230130Smav					    "WARNING: nid=%d has cnid outside "
996230130Smav					    "of the AFG range j=%d "
997230130Smav					    "entnum=%d index=%d res=0x%08x\n",
998230130Smav					    nid, j, entnum, i, res);
999230130Smav				);
1000230130Smav			}
1001230130Smav			if (CONN_RANGE(res, entnum, j) == 0)
1002230130Smav				addcnid = cnid;
1003230130Smav			else if (prevcnid == 0 || prevcnid >= cnid) {
1004230130Smav				device_printf(w->devinfo->dev,
1005230130Smav				    "WARNING: Invalid child range "
1006230130Smav				    "nid=%d index=%d j=%d entnum=%d "
1007230130Smav				    "prevcnid=%d cnid=%d res=0x%08x\n",
1008230130Smav				    nid, i, j, entnum, prevcnid,
1009230130Smav				    cnid, res);
1010230130Smav				addcnid = cnid;
1011230130Smav			} else
1012230130Smav				addcnid = prevcnid + 1;
1013230130Smav			while (addcnid <= cnid) {
1014230130Smav				if (w->nconns > max) {
1015230130Smav					device_printf(w->devinfo->dev,
1016230130Smav					    "Adding %d (nid=%d): "
1017230130Smav					    "Max connection reached! max=%d\n",
1018230130Smav					    addcnid, nid, max + 1);
1019230130Smav					goto getconns_out;
1020230130Smav				}
1021230130Smav				w->connsenable[w->nconns] = 1;
1022230130Smav				w->conns[w->nconns++] = addcnid++;
1023230130Smav			}
1024230130Smav			prevcnid = cnid;
1025230130Smav		}
1026230130Smav	}
1027230130Smav
1028230130Smavgetconns_out:
1029230130Smav	return;
1030230130Smav}
1031230130Smav
1032230130Smavstatic void
1033230130Smavhdaa_widget_parse(struct hdaa_widget *w)
1034230130Smav{
1035230130Smav	device_t dev = w->devinfo->dev;
1036230130Smav	uint32_t wcap, cap;
1037230130Smav	nid_t nid = w->nid;
1038230130Smav	char buf[64];
1039230130Smav
1040230130Smav	w->param.widget_cap = wcap = hda_command(dev,
1041230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_AUDIO_WIDGET_CAP));
1042230130Smav	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap);
1043230130Smav
1044230130Smav	hdaa_widget_connection_parse(w);
1045230130Smav
1046230130Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) {
1047230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1048230130Smav			w->param.outamp_cap =
1049230130Smav			    hda_command(dev,
1050230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1051230130Smav			    HDA_PARAM_OUTPUT_AMP_CAP));
1052230130Smav		else
1053230130Smav			w->param.outamp_cap =
1054230130Smav			    w->devinfo->outamp_cap;
1055230130Smav	} else
1056230130Smav		w->param.outamp_cap = 0;
1057230130Smav
1058230130Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) {
1059230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1060230130Smav			w->param.inamp_cap =
1061230130Smav			    hda_command(dev,
1062230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1063230130Smav			    HDA_PARAM_INPUT_AMP_CAP));
1064230130Smav		else
1065230130Smav			w->param.inamp_cap =
1066230130Smav			    w->devinfo->inamp_cap;
1067230130Smav	} else
1068230130Smav		w->param.inamp_cap = 0;
1069230130Smav
1070230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1071230130Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1072230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) {
1073230130Smav			cap = hda_command(dev,
1074230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1075230130Smav			    HDA_PARAM_SUPP_STREAM_FORMATS));
1076230130Smav			w->param.supp_stream_formats = (cap != 0) ? cap :
1077230130Smav			    w->devinfo->supp_stream_formats;
1078230130Smav			cap = hda_command(dev,
1079230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1080230130Smav			    HDA_PARAM_SUPP_PCM_SIZE_RATE));
1081230130Smav			w->param.supp_pcm_size_rate = (cap != 0) ? cap :
1082230130Smav			    w->devinfo->supp_pcm_size_rate;
1083230130Smav		} else {
1084230130Smav			w->param.supp_stream_formats =
1085230130Smav			    w->devinfo->supp_stream_formats;
1086230130Smav			w->param.supp_pcm_size_rate =
1087230130Smav			    w->devinfo->supp_pcm_size_rate;
1088230130Smav		}
1089230326Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1090230326Smav			w->wclass.conv.stripecap = hda_command(dev,
1091230326Smav			    HDA_CMD_GET_STRIPE_CONTROL(0, w->nid)) >> 20;
1092230326Smav		} else
1093230326Smav			w->wclass.conv.stripecap = 1;
1094230130Smav	} else {
1095230130Smav		w->param.supp_stream_formats = 0;
1096230130Smav		w->param.supp_pcm_size_rate = 0;
1097230130Smav	}
1098230130Smav
1099230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1100230130Smav		w->wclass.pin.original = w->wclass.pin.newconf =
1101230130Smav		    w->wclass.pin.config = hda_command(dev,
1102230130Smav			HDA_CMD_GET_CONFIGURATION_DEFAULT(0, w->nid));
1103230130Smav		w->wclass.pin.cap = hda_command(dev,
1104230130Smav		    HDA_CMD_GET_PARAMETER(0, w->nid, HDA_PARAM_PIN_CAP));;
1105230130Smav		w->wclass.pin.ctrl = hda_command(dev,
1106230130Smav		    HDA_CMD_GET_PIN_WIDGET_CTRL(0, nid));
1107230130Smav		if (HDA_PARAM_PIN_CAP_EAPD_CAP(w->wclass.pin.cap)) {
1108230130Smav			w->param.eapdbtl = hda_command(dev,
1109230130Smav			    HDA_CMD_GET_EAPD_BTL_ENABLE(0, nid));
1110230130Smav			w->param.eapdbtl &= 0x7;
1111230130Smav			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
1112230130Smav		} else
1113230130Smav			w->param.eapdbtl = HDA_INVALID;
1114230130Smav
1115230130Smav		hdaa_unlock(w->devinfo);
1116230130Smav		snprintf(buf, sizeof(buf), "nid%d_config", w->nid);
1117230130Smav		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1118230130Smav		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1119230130Smav		    buf, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1120230130Smav		    &w->wclass.pin.newconf, sizeof(&w->wclass.pin.newconf),
1121230130Smav		    hdaa_sysctl_config, "A", "Current pin configuration");
1122230130Smav		snprintf(buf, sizeof(buf), "nid%d_original", w->nid);
1123230130Smav		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1124230130Smav		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1125230130Smav		    buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1126230130Smav		    &w->wclass.pin.original, sizeof(&w->wclass.pin.original),
1127230130Smav		    hdaa_sysctl_config, "A", "Original pin configuration");
1128230130Smav		hdaa_lock(w->devinfo);
1129230130Smav	}
1130230312Smav	w->unsol = -1;
1131230130Smav}
1132230130Smav
1133230130Smavstatic void
1134230130Smavhdaa_widget_postprocess(struct hdaa_widget *w)
1135230130Smav{
1136230130Smav	char *typestr;
1137230130Smav
1138230130Smav	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(w->param.widget_cap);
1139230130Smav	switch (w->type) {
1140230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
1141230130Smav		typestr = "audio output";
1142230130Smav		break;
1143230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
1144230130Smav		typestr = "audio input";
1145230130Smav		break;
1146230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
1147230130Smav		typestr = "audio mixer";
1148230130Smav		break;
1149230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
1150230130Smav		typestr = "audio selector";
1151230130Smav		break;
1152230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
1153230130Smav		typestr = "pin";
1154230130Smav		break;
1155230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET:
1156230130Smav		typestr = "power widget";
1157230130Smav		break;
1158230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET:
1159230130Smav		typestr = "volume widget";
1160230130Smav		break;
1161230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
1162230130Smav		typestr = "beep widget";
1163230130Smav		break;
1164230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET:
1165230130Smav		typestr = "vendor widget";
1166230130Smav		break;
1167230130Smav	default:
1168230130Smav		typestr = "unknown type";
1169230130Smav		break;
1170230130Smav	}
1171230130Smav	strlcpy(w->name, typestr, sizeof(w->name));
1172230130Smav
1173230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1174230130Smav		uint32_t config;
1175230130Smav		const char *devstr;
1176230130Smav		int conn, color;
1177230130Smav
1178230130Smav		config = w->wclass.pin.config;
1179230130Smav		devstr = HDA_DEVS[(config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) >>
1180230130Smav		    HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT];
1181230130Smav		conn = (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) >>
1182230130Smav		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT;
1183230130Smav		color = (config & HDA_CONFIG_DEFAULTCONF_COLOR_MASK) >>
1184230130Smav		    HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT;
1185230130Smav		strlcat(w->name, ": ", sizeof(w->name));
1186230130Smav		strlcat(w->name, devstr, sizeof(w->name));
1187230130Smav		strlcat(w->name, " (", sizeof(w->name));
1188230130Smav		if (conn == 0 && color != 0 && color != 15) {
1189230130Smav			strlcat(w->name, HDA_COLORS[color], sizeof(w->name));
1190230130Smav			strlcat(w->name, " ", sizeof(w->name));
1191230130Smav		}
1192230130Smav		strlcat(w->name, HDA_CONNS[conn], sizeof(w->name));
1193230130Smav		strlcat(w->name, ")", sizeof(w->name));
1194230130Smav	}
1195230130Smav}
1196230130Smav
1197230130Smavstruct hdaa_widget *
1198230130Smavhdaa_widget_get(struct hdaa_devinfo *devinfo, nid_t nid)
1199230130Smav{
1200230130Smav	if (devinfo == NULL || devinfo->widget == NULL ||
1201230130Smav		    nid < devinfo->startnode || nid >= devinfo->endnode)
1202230130Smav		return (NULL);
1203230130Smav	return (&devinfo->widget[nid - devinfo->startnode]);
1204230130Smav}
1205230130Smav
1206230130Smavstatic void
1207230130Smavhdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *devinfo, nid_t nid,
1208230130Smav					int index, int lmute, int rmute,
1209230130Smav					int left, int right, int dir)
1210230130Smav{
1211230130Smav	uint16_t v = 0;
1212230130Smav
1213230130Smav	HDA_BOOTHVERBOSE(
1214230130Smav		device_printf(devinfo->dev,
1215230130Smav		    "Setting amplifier nid=%d index=%d %s mute=%d/%d vol=%d/%d\n",
1216230130Smav		    nid,index,dir ? "in" : "out",lmute,rmute,left,right);
1217230130Smav	);
1218230130Smav	if (left != right || lmute != rmute) {
1219230130Smav		v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
1220230130Smav		    (lmute << 7) | left;
1221230130Smav		hda_command(devinfo->dev,
1222230130Smav		    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1223230130Smav		v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
1224230130Smav		    (rmute << 7) | right;
1225230130Smav	} else
1226230130Smav		v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
1227230130Smav		    (lmute << 7) | left;
1228230130Smav
1229230130Smav	hda_command(devinfo->dev,
1230230130Smav	    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1231230130Smav}
1232230130Smav
1233230130Smavstatic void
1234230130Smavhdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *ctl, uint32_t mute,
1235230130Smav						int left, int right)
1236230130Smav{
1237230130Smav	nid_t nid;
1238230130Smav	int lmute, rmute;
1239230130Smav
1240230130Smav	nid = ctl->widget->nid;
1241230130Smav
1242230130Smav	/* Save new values if valid. */
1243230130Smav	if (mute != HDAA_AMP_MUTE_DEFAULT)
1244230130Smav		ctl->muted = mute;
1245230130Smav	if (left != HDAA_AMP_VOL_DEFAULT)
1246230130Smav		ctl->left = left;
1247230130Smav	if (right != HDAA_AMP_VOL_DEFAULT)
1248230130Smav		ctl->right = right;
1249230130Smav	/* Prepare effective values */
1250230130Smav	if (ctl->forcemute) {
1251230130Smav		lmute = 1;
1252230130Smav		rmute = 1;
1253230130Smav		left = 0;
1254230130Smav		right = 0;
1255230130Smav	} else {
1256230130Smav		lmute = HDAA_AMP_LEFT_MUTED(ctl->muted);
1257230130Smav		rmute = HDAA_AMP_RIGHT_MUTED(ctl->muted);
1258230130Smav		left = ctl->left;
1259230130Smav		right = ctl->right;
1260230130Smav	}
1261230130Smav	/* Apply effective values */
1262230130Smav	if (ctl->dir & HDAA_CTL_OUT)
1263230130Smav		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1264230130Smav		    lmute, rmute, left, right, 0);
1265230130Smav	if (ctl->dir & HDAA_CTL_IN)
1266230130Smav		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1267230130Smav		    lmute, rmute, left, right, 1);
1268230130Smav}
1269230130Smav
1270230130Smavstatic void
1271230130Smavhdaa_widget_connection_select(struct hdaa_widget *w, uint8_t index)
1272230130Smav{
1273230130Smav	if (w == NULL || w->nconns < 1 || index > (w->nconns - 1))
1274230130Smav		return;
1275230130Smav	HDA_BOOTHVERBOSE(
1276230130Smav		device_printf(w->devinfo->dev,
1277230130Smav		    "Setting selector nid=%d index=%d\n", w->nid, index);
1278230130Smav	);
1279230130Smav	hda_command(w->devinfo->dev,
1280230130Smav	    HDA_CMD_SET_CONNECTION_SELECT_CONTROL(0, w->nid, index));
1281230130Smav	w->selconn = index;
1282230130Smav}
1283230130Smav
1284230130Smav/****************************************************************************
1285230130Smav * Device Methods
1286230130Smav ****************************************************************************/
1287230130Smav
1288230130Smavstatic void *
1289230130Smavhdaa_channel_init(kobj_t obj, void *data, struct snd_dbuf *b,
1290230130Smav					struct pcm_channel *c, int dir)
1291230130Smav{
1292230130Smav	struct hdaa_chan *ch = data;
1293230130Smav	struct hdaa_pcm_devinfo *pdevinfo = ch->pdevinfo;
1294230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1295230130Smav
1296230130Smav	hdaa_lock(devinfo);
1297230130Smav	if (devinfo->quirks & HDAA_QUIRK_FIXEDRATE) {
1298230130Smav		ch->caps.minspeed = ch->caps.maxspeed = 48000;
1299230130Smav		ch->pcmrates[0] = 48000;
1300230130Smav		ch->pcmrates[1] = 0;
1301230130Smav	}
1302230130Smav	ch->dir = dir;
1303230130Smav	ch->b = b;
1304230130Smav	ch->c = c;
1305230130Smav	ch->blksz = pdevinfo->chan_size / pdevinfo->chan_blkcnt;
1306230130Smav	ch->blkcnt = pdevinfo->chan_blkcnt;
1307230130Smav	hdaa_unlock(devinfo);
1308230130Smav
1309230130Smav	if (sndbuf_alloc(ch->b, bus_get_dma_tag(devinfo->dev),
1310230130Smav	    hda_get_dma_nocache(devinfo->dev) ? BUS_DMA_NOCACHE : 0,
1311230130Smav	    pdevinfo->chan_size) != 0)
1312230130Smav		return (NULL);
1313230130Smav
1314230130Smav	return (ch);
1315230130Smav}
1316230130Smav
1317230130Smavstatic int
1318230130Smavhdaa_channel_setformat(kobj_t obj, void *data, uint32_t format)
1319230130Smav{
1320230130Smav	struct hdaa_chan *ch = data;
1321230130Smav	int i;
1322230130Smav
1323230130Smav	for (i = 0; ch->caps.fmtlist[i] != 0; i++) {
1324230130Smav		if (format == ch->caps.fmtlist[i]) {
1325230130Smav			ch->fmt = format;
1326230130Smav			return (0);
1327230130Smav		}
1328230130Smav	}
1329230130Smav
1330230130Smav	return (EINVAL);
1331230130Smav}
1332230130Smav
1333230130Smavstatic uint32_t
1334230130Smavhdaa_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
1335230130Smav{
1336230130Smav	struct hdaa_chan *ch = data;
1337230130Smav	uint32_t spd = 0, threshold;
1338230130Smav	int i;
1339230130Smav
1340230130Smav	/* First look for equal or multiple frequency. */
1341230130Smav	for (i = 0; ch->pcmrates[i] != 0; i++) {
1342230130Smav		spd = ch->pcmrates[i];
1343230130Smav		if (speed != 0 && spd / speed * speed == spd) {
1344230130Smav			ch->spd = spd;
1345230130Smav			return (spd);
1346230130Smav		}
1347230130Smav	}
1348230130Smav	/* If no match, just find nearest. */
1349230130Smav	for (i = 0; ch->pcmrates[i] != 0; i++) {
1350230130Smav		spd = ch->pcmrates[i];
1351230130Smav		threshold = spd + ((ch->pcmrates[i + 1] != 0) ?
1352230130Smav		    ((ch->pcmrates[i + 1] - spd) >> 1) : 0);
1353230130Smav		if (speed < threshold)
1354230130Smav			break;
1355230130Smav	}
1356230130Smav	ch->spd = spd;
1357230130Smav	return (spd);
1358230130Smav}
1359230130Smav
1360230130Smavstatic uint16_t
1361230130Smavhdaa_stream_format(struct hdaa_chan *ch)
1362230130Smav{
1363230130Smav	int i;
1364230130Smav	uint16_t fmt;
1365230130Smav
1366230130Smav	fmt = 0;
1367230130Smav	if (ch->fmt & AFMT_S16_LE)
1368230130Smav		fmt |= ch->bit16 << 4;
1369230130Smav	else if (ch->fmt & AFMT_S32_LE)
1370230130Smav		fmt |= ch->bit32 << 4;
1371230130Smav	else
1372230130Smav		fmt |= 1 << 4;
1373230130Smav	for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
1374230130Smav		if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
1375230130Smav			fmt |= hda_rate_tab[i].base;
1376230130Smav			fmt |= hda_rate_tab[i].mul;
1377230130Smav			fmt |= hda_rate_tab[i].div;
1378230130Smav			break;
1379230130Smav		}
1380230130Smav	}
1381230130Smav	fmt |= (AFMT_CHANNEL(ch->fmt) - 1);
1382230130Smav
1383230130Smav	return (fmt);
1384230130Smav}
1385230130Smav
1386230326Smavstatic int
1387230326Smavhdaa_allowed_stripes(uint16_t fmt)
1388230326Smav{
1389230326Smav	static const int bits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1390230326Smav	int size;
1391230326Smav
1392230326Smav	size = bits[(fmt >> 4) & 0x03];
1393230326Smav	size *= (fmt & 0x0f) + 1;
1394230326Smav	size *= ((fmt >> 11) & 0x07) + 1;
1395230326Smav	return (0xffffffffU >> (32 - fls(size / 8)));
1396230326Smav}
1397230326Smav
1398230130Smavstatic void
1399230130Smavhdaa_audio_setup(struct hdaa_chan *ch)
1400230130Smav{
1401230130Smav	struct hdaa_audio_as *as = &ch->devinfo->as[ch->as];
1402230312Smav	struct hdaa_widget *w, *wp;
1403230312Smav	int i, j, k, chn, cchn, totalchn, totalextchn, c;
1404230130Smav	uint16_t fmt, dfmt;
1405230312Smav	/* Mapping channel pairs to codec pins/converters. */
1406230312Smav	const static uint16_t convmap[2][5] =
1407230312Smav	    {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x0231 }, /* 5.1 */
1408230312Smav	     { 0x0010, 0x0001, 0x2001, 0x2031, 0x2431 }};/* 7.1 */
1409230312Smav	/* Mapping formats to HDMI channel allocations. */
1410230312Smav	const static uint8_t hdmica[2][8] =
1411230312Smav	    {{ 0x02, 0x00, 0x04, 0x08, 0x0a, 0x0e, 0x12, 0x12 }, /* x.0 */
1412230312Smav	     { 0x01, 0x03, 0x01, 0x03, 0x09, 0x0b, 0x0f, 0x13 }}; /* x.1 */
1413230312Smav	/* Mapping formats to HDMI channels order. */
1414230312Smav	const static uint32_t hdmich[2][8] =
1415230312Smav	    {{ 0xFFFF0F00, 0xFFFFFF10, 0xFFF2FF10, 0xFF32FF10,
1416230312Smav	       0xFF324F10, 0xF5324F10, 0x54326F10, 0x54326F10 }, /* x.0 */
1417230312Smav	     { 0xFFFFF000, 0xFFFF0100, 0xFFFFF210, 0xFFFF2310,
1418230312Smav	       0xFF32F410, 0xFF324510, 0xF6324510, 0x76325410 }}; /* x.1 */
1419230312Smav	int convmapid = -1;
1420230312Smav	nid_t nid;
1421230312Smav	uint8_t csum;
1422230130Smav
1423230130Smav	totalchn = AFMT_CHANNEL(ch->fmt);
1424230312Smav	totalextchn = AFMT_EXTCHANNEL(ch->fmt);
1425230130Smav	HDA_BOOTHVERBOSE(
1426230130Smav		device_printf(ch->pdevinfo->dev,
1427230312Smav		    "PCMDIR_%s: Stream setup fmt=%08x (%d.%d) speed=%d\n",
1428230130Smav		    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1429230312Smav		    ch->fmt, totalchn - totalextchn, totalextchn, ch->spd);
1430230130Smav	);
1431230130Smav	fmt = hdaa_stream_format(ch);
1432230130Smav
1433230312Smav	/* Set channels to I/O converters mapping for known speaker setups. */
1434230130Smav	if ((as->pinset == 0x0007 || as->pinset == 0x0013)) /* Standard 5.1 */
1435230312Smav		convmapid = 0;
1436230130Smav	else if (as->pinset == 0x0017) /* Standard 7.1 */
1437230312Smav		convmapid = 1;
1438230130Smav
1439230130Smav	dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN;
1440230130Smav	if (ch->fmt & AFMT_AC3)
1441230130Smav		dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO;
1442230130Smav
1443230130Smav	chn = 0;
1444230130Smav	for (i = 0; ch->io[i] != -1; i++) {
1445230130Smav		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1446230130Smav		if (w == NULL)
1447230130Smav			continue;
1448230130Smav
1449230130Smav		/* If HP redirection is enabled, but failed to use same
1450230130Smav		   DAC, make last DAC to duplicate first one. */
1451230130Smav		if (as->fakeredir && i == (as->pincnt - 1)) {
1452230130Smav			c = (ch->sid << 4);
1453230130Smav		} else {
1454230312Smav			/* Map channels to I/O converters, if set. */
1455230312Smav			if (convmapid >= 0)
1456230312Smav				chn = (((convmap[convmapid][totalchn / 2]
1457230312Smav				    >> i * 4) & 0xf) - 1) * 2;
1458230130Smav			if (chn < 0 || chn >= totalchn) {
1459230130Smav				c = 0;
1460230130Smav			} else {
1461230130Smav				c = (ch->sid << 4) | chn;
1462230130Smav			}
1463230130Smav		}
1464230130Smav		hda_command(ch->devinfo->dev,
1465230130Smav		    HDA_CMD_SET_CONV_FMT(0, ch->io[i], fmt));
1466230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1467230130Smav			hda_command(ch->devinfo->dev,
1468230130Smav			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], dfmt));
1469230130Smav		}
1470230130Smav		hda_command(ch->devinfo->dev,
1471230130Smav		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i], c));
1472230326Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1473230326Smav			hda_command(ch->devinfo->dev,
1474230326Smav			    HDA_CMD_SET_STRIPE_CONTROL(0, w->nid, ch->stripectl));
1475230326Smav		}
1476230312Smav		cchn = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1477230312Smav		if (cchn > 1 && chn < totalchn) {
1478230312Smav			cchn = min(cchn, totalchn - chn - 1);
1479230312Smav			hda_command(ch->devinfo->dev,
1480230312Smav			    HDA_CMD_SET_CONV_CHAN_COUNT(0, ch->io[i], cchn));
1481230312Smav		}
1482230312Smav		HDA_BOOTHVERBOSE(
1483230312Smav			device_printf(ch->pdevinfo->dev,
1484230312Smav			    "PCMDIR_%s: Stream setup nid=%d: "
1485230312Smav			    "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x, "
1486230326Smav			    "chan_count=0x%02x, stripe=%d\n",
1487230312Smav			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1488230326Smav			    ch->io[i], fmt, dfmt, c, cchn, ch->stripectl);
1489230312Smav		);
1490230312Smav		for (j = 0; j < 16; j++) {
1491230312Smav			if (as->dacs[ch->asindex][j] != ch->io[i])
1492230312Smav				continue;
1493230312Smav			nid = as->pins[j];
1494230312Smav			wp = hdaa_widget_get(ch->devinfo, nid);
1495230312Smav			if (wp == NULL)
1496230312Smav				continue;
1497230312Smav			if (!HDA_PARAM_PIN_CAP_DP(wp->wclass.pin.cap) &&
1498230312Smav			    !HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap))
1499230312Smav				continue;
1500230312Smav
1501230312Smav			/* Set channel mapping. */
1502230312Smav			for (k = 0; k < 8; k++) {
1503230312Smav				hda_command(ch->devinfo->dev,
1504230312Smav				    HDA_CMD_SET_HDMI_CHAN_SLOT(0, nid,
1505230312Smav				    (((hdmich[totalextchn == 0 ? 0 : 1][totalchn - 1]
1506230312Smav				     >> (k * 4)) & 0xf) << 4) | k));
1507230312Smav			}
1508230312Smav
1509230312Smav			/* Stop audio infoframe transmission. */
1510230312Smav			hda_command(ch->devinfo->dev,
1511230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1512230312Smav			hda_command(ch->devinfo->dev,
1513230312Smav			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0x00));
1514230312Smav
1515230312Smav			/* Clear audio infoframe buffer. */
1516230312Smav			hda_command(ch->devinfo->dev,
1517230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1518230312Smav			for (k = 0; k < 32; k++)
1519230312Smav				hda_command(ch->devinfo->dev,
1520230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1521230312Smav
1522230312Smav			/* Write HDMI/DisplayPort audio infoframe. */
1523230312Smav			hda_command(ch->devinfo->dev,
1524230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1525230312Smav			if (w->eld != NULL && w->eld_len >= 6 &&
1526230312Smav			    ((w->eld[5] >> 2) & 0x3) == 1) { /* DisplayPort */
1527230312Smav				hda_command(ch->devinfo->dev,
1528230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1529230312Smav				hda_command(ch->devinfo->dev,
1530230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x1b));
1531230312Smav				hda_command(ch->devinfo->dev,
1532230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x44));
1533230312Smav			} else {	/* HDMI */
1534230312Smav				hda_command(ch->devinfo->dev,
1535230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1536230312Smav				hda_command(ch->devinfo->dev,
1537230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x01));
1538230312Smav				hda_command(ch->devinfo->dev,
1539230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x0a));
1540230312Smav				csum = 0;
1541230312Smav				csum -= 0x84 + 0x01 + 0x0a + (totalchn - 1) +
1542230312Smav				    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1];
1543230312Smav				hda_command(ch->devinfo->dev,
1544230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, csum));
1545230312Smav			}
1546230312Smav			hda_command(ch->devinfo->dev,
1547230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, totalchn - 1));
1548230312Smav			hda_command(ch->devinfo->dev,
1549230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1550230312Smav			hda_command(ch->devinfo->dev,
1551230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1552230312Smav			hda_command(ch->devinfo->dev,
1553230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid,
1554230312Smav			    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1]));
1555230312Smav
1556230312Smav			/* Start audio infoframe transmission. */
1557230312Smav			hda_command(ch->devinfo->dev,
1558230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1559230312Smav			hda_command(ch->devinfo->dev,
1560230312Smav			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0xc0));
1561230312Smav		}
1562230312Smav		chn += cchn + 1;
1563230130Smav	}
1564230130Smav}
1565230130Smav
1566230130Smav/*
1567230130Smav * Greatest Common Divisor.
1568230130Smav */
1569230130Smavstatic unsigned
1570230130Smavgcd(unsigned a, unsigned b)
1571230130Smav{
1572230130Smav	u_int c;
1573230130Smav
1574230130Smav	while (b != 0) {
1575230130Smav		c = a;
1576230130Smav		a = b;
1577230130Smav		b = (c % b);
1578230130Smav	}
1579230130Smav	return (a);
1580230130Smav}
1581230130Smav
1582230130Smav/*
1583230130Smav * Least Common Multiple.
1584230130Smav */
1585230130Smavstatic unsigned
1586230130Smavlcm(unsigned a, unsigned b)
1587230130Smav{
1588230130Smav
1589230130Smav	return ((a * b) / gcd(a, b));
1590230130Smav}
1591230130Smav
1592230130Smavstatic int
1593230130Smavhdaa_channel_setfragments(kobj_t obj, void *data,
1594230130Smav					uint32_t blksz, uint32_t blkcnt)
1595230130Smav{
1596230130Smav	struct hdaa_chan *ch = data;
1597230130Smav
1598230130Smav	blksz -= blksz % lcm(HDA_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
1599230130Smav
1600230130Smav	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
1601230130Smav		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
1602230130Smav	if (blksz < HDA_BLK_MIN)
1603230130Smav		blksz = HDA_BLK_MIN;
1604230130Smav	if (blkcnt > HDA_BDL_MAX)
1605230130Smav		blkcnt = HDA_BDL_MAX;
1606230130Smav	if (blkcnt < HDA_BDL_MIN)
1607230130Smav		blkcnt = HDA_BDL_MIN;
1608230130Smav
1609230130Smav	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
1610230130Smav		if ((blkcnt >> 1) >= HDA_BDL_MIN)
1611230130Smav			blkcnt >>= 1;
1612230130Smav		else if ((blksz >> 1) >= HDA_BLK_MIN)
1613230130Smav			blksz >>= 1;
1614230130Smav		else
1615230130Smav			break;
1616230130Smav	}
1617230130Smav
1618230130Smav	if ((sndbuf_getblksz(ch->b) != blksz ||
1619230130Smav	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
1620230130Smav	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
1621230130Smav		device_printf(ch->devinfo->dev, "%s: failed blksz=%u blkcnt=%u\n",
1622230130Smav		    __func__, blksz, blkcnt);
1623230130Smav
1624230130Smav	ch->blksz = sndbuf_getblksz(ch->b);
1625230130Smav	ch->blkcnt = sndbuf_getblkcnt(ch->b);
1626230130Smav
1627230130Smav	return (0);
1628230130Smav}
1629230130Smav
1630230130Smavstatic uint32_t
1631230130Smavhdaa_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
1632230130Smav{
1633230130Smav	struct hdaa_chan *ch = data;
1634230130Smav
1635230130Smav	hdaa_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
1636230130Smav
1637230130Smav	return (ch->blksz);
1638230130Smav}
1639230130Smav
1640230130Smavstatic void
1641230130Smavhdaa_channel_stop(struct hdaa_chan *ch)
1642230130Smav{
1643230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
1644230130Smav	struct hdaa_widget *w;
1645230130Smav	int i;
1646230130Smav
1647230181Smav	if ((ch->flags & HDAA_CHN_RUNNING) == 0)
1648230181Smav		return;
1649230130Smav	ch->flags &= ~HDAA_CHN_RUNNING;
1650230130Smav	HDAC_STREAM_STOP(device_get_parent(devinfo->dev), devinfo->dev,
1651230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1652230130Smav	for (i = 0; ch->io[i] != -1; i++) {
1653230130Smav		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1654230130Smav		if (w == NULL)
1655230130Smav			continue;
1656230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1657230130Smav			hda_command(devinfo->dev,
1658230130Smav			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], 0));
1659230130Smav		}
1660230130Smav		hda_command(devinfo->dev,
1661230130Smav		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i],
1662230130Smav		    0));
1663230130Smav	}
1664230130Smav	HDAC_STREAM_FREE(device_get_parent(devinfo->dev), devinfo->dev,
1665230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1666230130Smav}
1667230130Smav
1668230130Smavstatic int
1669230130Smavhdaa_channel_start(struct hdaa_chan *ch)
1670230130Smav{
1671230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
1672230326Smav	uint32_t fmt;
1673230130Smav
1674230326Smav	fmt = hdaa_stream_format(ch);
1675230326Smav	ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt)) - 1;
1676230130Smav	ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev,
1677230326Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos);
1678230130Smav	if (ch->sid <= 0)
1679230130Smav		return (EBUSY);
1680230130Smav	hdaa_audio_setup(ch);
1681230130Smav	HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev,
1682230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1683230130Smav	HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev,
1684230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid,
1685230130Smav	    sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt);
1686230130Smav	ch->flags |= HDAA_CHN_RUNNING;
1687230130Smav	return (0);
1688230130Smav}
1689230130Smav
1690230130Smavstatic int
1691230130Smavhdaa_channel_trigger(kobj_t obj, void *data, int go)
1692230130Smav{
1693230130Smav	struct hdaa_chan *ch = data;
1694230130Smav	int error = 0;
1695230130Smav
1696230130Smav	if (!PCMTRIG_COMMON(go))
1697230130Smav		return (0);
1698230130Smav
1699230130Smav	hdaa_lock(ch->devinfo);
1700230130Smav	switch (go) {
1701230130Smav	case PCMTRIG_START:
1702230130Smav		error = hdaa_channel_start(ch);
1703230130Smav		break;
1704230130Smav	case PCMTRIG_STOP:
1705230130Smav	case PCMTRIG_ABORT:
1706230130Smav		hdaa_channel_stop(ch);
1707230130Smav		break;
1708230130Smav	default:
1709230130Smav		break;
1710230130Smav	}
1711230130Smav	hdaa_unlock(ch->devinfo);
1712230130Smav
1713230130Smav	return (error);
1714230130Smav}
1715230130Smav
1716230130Smavstatic uint32_t
1717230130Smavhdaa_channel_getptr(kobj_t obj, void *data)
1718230130Smav{
1719230130Smav	struct hdaa_chan *ch = data;
1720230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
1721230130Smav	uint32_t ptr;
1722230130Smav
1723230130Smav	hdaa_lock(devinfo);
1724230130Smav	if (ch->dmapos != NULL) {
1725230130Smav		ptr = *(ch->dmapos);
1726230130Smav	} else {
1727230130Smav		ptr = HDAC_STREAM_GETPTR(
1728230130Smav		    device_get_parent(devinfo->dev), devinfo->dev,
1729230130Smav		    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1730230130Smav	}
1731230130Smav	hdaa_unlock(devinfo);
1732230130Smav
1733230130Smav	/*
1734230130Smav	 * Round to available space and force 128 bytes aligment.
1735230130Smav	 */
1736230130Smav	ptr %= ch->blksz * ch->blkcnt;
1737230130Smav	ptr &= HDA_BLK_ALIGN;
1738230130Smav
1739230130Smav	return (ptr);
1740230130Smav}
1741230130Smav
1742230130Smavstatic struct pcmchan_caps *
1743230130Smavhdaa_channel_getcaps(kobj_t obj, void *data)
1744230130Smav{
1745230130Smav	return (&((struct hdaa_chan *)data)->caps);
1746230130Smav}
1747230130Smav
1748230130Smavstatic kobj_method_t hdaa_channel_methods[] = {
1749230130Smav	KOBJMETHOD(channel_init,		hdaa_channel_init),
1750230130Smav	KOBJMETHOD(channel_setformat,		hdaa_channel_setformat),
1751230130Smav	KOBJMETHOD(channel_setspeed,		hdaa_channel_setspeed),
1752230130Smav	KOBJMETHOD(channel_setblocksize,	hdaa_channel_setblocksize),
1753230130Smav	KOBJMETHOD(channel_setfragments,	hdaa_channel_setfragments),
1754230130Smav	KOBJMETHOD(channel_trigger,		hdaa_channel_trigger),
1755230130Smav	KOBJMETHOD(channel_getptr,		hdaa_channel_getptr),
1756230130Smav	KOBJMETHOD(channel_getcaps,		hdaa_channel_getcaps),
1757230130Smav	KOBJMETHOD_END
1758230130Smav};
1759230130SmavCHANNEL_DECLARE(hdaa_channel);
1760230130Smav
1761230130Smavstatic int
1762230130Smavhdaa_audio_ctl_ossmixer_init(struct snd_mixer *m)
1763230130Smav{
1764230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
1765230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1766230130Smav	struct hdaa_widget *w, *cw;
1767230130Smav	uint32_t mask, recmask;
1768230451Smav	int i, j;
1769230130Smav
1770230130Smav	hdaa_lock(devinfo);
1771230451Smav	pdevinfo->mixer = m;
1772230130Smav
1773230130Smav	/* Make sure that in case of soft volume it won't stay muted. */
1774230130Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1775230130Smav		pdevinfo->left[i] = 100;
1776230130Smav		pdevinfo->right[i] = 100;
1777230130Smav	}
1778230130Smav
1779230451Smav	/* Declare volume controls assigned to this association. */
1780230451Smav	mask = pdevinfo->ossmask;
1781230130Smav	if (pdevinfo->playas >= 0) {
1782230451Smav		/* Declate EAPD as ogain control. */
1783230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1784230130Smav			w = hdaa_widget_get(devinfo, i);
1785230130Smav			if (w == NULL || w->enable == 0)
1786230130Smav				continue;
1787230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
1788230130Smav			    w->param.eapdbtl == HDA_INVALID ||
1789230130Smav			    w->bindas != pdevinfo->playas)
1790230130Smav				continue;
1791230130Smav			mask |= SOUND_MASK_OGAIN;
1792230130Smav			break;
1793230130Smav		}
1794230130Smav
1795230451Smav		/* Declare soft PCM volume if needed. */
1796230451Smav		if ((mask & SOUND_MASK_PCM) == 0 ||
1797230451Smav		    (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) ||
1798230451Smav		    pdevinfo->minamp[SOUND_MIXER_PCM] ==
1799230451Smav		     pdevinfo->maxamp[SOUND_MIXER_PCM]) {
1800230451Smav			mask |= SOUND_MASK_PCM;
1801230451Smav			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
1802230451Smav			HDA_BOOTHVERBOSE(
1803230451Smav				device_printf(pdevinfo->dev,
1804230451Smav				    "Forcing Soft PCM volume\n");
1805230451Smav			);
1806230451Smav		}
1807230451Smav
1808230451Smav		/* Declare master volume if needed. */
1809230451Smav		if ((mask & SOUND_MASK_VOLUME) == 0) {
1810230451Smav			mask |= SOUND_MASK_VOLUME;
1811230451Smav			mix_setparentchild(m, SOUND_MIXER_VOLUME,
1812230451Smav			    SOUND_MASK_PCM);
1813230451Smav			mix_setrealdev(m, SOUND_MIXER_VOLUME,
1814230451Smav			    SOUND_MIXER_NONE);
1815230451Smav			HDA_BOOTHVERBOSE(
1816230451Smav				device_printf(pdevinfo->dev,
1817230451Smav				    "Forcing master volume with PCM\n");
1818230451Smav			);
1819230451Smav		}
1820230130Smav	}
1821230130Smav
1822230130Smav	/* Declare record sources available to this association. */
1823230451Smav	recmask = 0;
1824230130Smav	if (pdevinfo->recas >= 0) {
1825230130Smav		for (i = 0; i < 16; i++) {
1826230130Smav			if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0)
1827230130Smav				continue;
1828230130Smav			w = hdaa_widget_get(devinfo,
1829230130Smav			    devinfo->as[pdevinfo->recas].dacs[0][i]);
1830230130Smav			if (w == NULL || w->enable == 0)
1831230130Smav				continue;
1832230130Smav			for (j = 0; j < w->nconns; j++) {
1833230130Smav				if (w->connsenable[j] == 0)
1834230130Smav					continue;
1835230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
1836230130Smav				if (cw == NULL || cw->enable == 0)
1837230130Smav					continue;
1838230130Smav				if (cw->bindas != pdevinfo->recas &&
1839230130Smav				    cw->bindas != -2)
1840230130Smav					continue;
1841230130Smav				recmask |= cw->ossmask;
1842230130Smav			}
1843230130Smav		}
1844230130Smav	}
1845230130Smav
1846230451Smav	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
1847230451Smav	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
1848230451Smav	pdevinfo->ossmask = mask;
1849230451Smav
1850230451Smav	mix_setrecdevs(m, recmask);
1851230451Smav	mix_setdevs(m, mask);
1852230451Smav
1853230451Smav	hdaa_unlock(devinfo);
1854230451Smav
1855230451Smav	return (0);
1856230451Smav}
1857230451Smav
1858230451Smav/*
1859230451Smav * Update amplification per pdevinfo per ossdev, calculate summary coefficient
1860230451Smav * and write it to codec, update *left and *right to reflect remaining error.
1861230451Smav */
1862230451Smavstatic void
1863230451Smavhdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev,
1864230451Smav    int mute, int *left, int *right)
1865230451Smav{
1866230451Smav	int i, zleft, zright, sleft, sright, smute, lval, rval;
1867230451Smav
1868230451Smav	ctl->devleft[ossdev] = *left;
1869230451Smav	ctl->devright[ossdev] = *right;
1870230451Smav	ctl->devmute[ossdev] = mute;
1871230451Smav	smute = sleft = sright = zleft = zright = 0;
1872230451Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1873230451Smav		sleft += ctl->devleft[i];
1874230451Smav		sright += ctl->devright[i];
1875230451Smav		smute |= ctl->devmute[i];
1876230451Smav		if (i == ossdev)
1877230451Smav			continue;
1878230451Smav		zleft += ctl->devleft[i];
1879230451Smav		zright += ctl->devright[i];
1880230451Smav	}
1881230451Smav	lval = QDB2VAL(ctl, sleft);
1882230451Smav	rval = QDB2VAL(ctl, sright);
1883230451Smav	hdaa_audio_ctl_amp_set(ctl, smute, lval, rval);
1884230451Smav	*left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft));
1885230451Smav	*right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright));
1886230451Smav}
1887230451Smav
1888230451Smav/*
1889230451Smav * Trace signal from source, setting volumes on the way.
1890230451Smav */
1891230451Smavstatic void
1892230451Smavhdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo,
1893230451Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
1894230451Smav{
1895230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1896230451Smav	struct hdaa_widget *w, *wc;
1897230451Smav	struct hdaa_audio_ctl *ctl;
1898230451Smav	int i, j, conns = 0;
1899230451Smav
1900230451Smav	if (depth > HDA_PARSE_MAXDEPTH)
1901230451Smav		return;
1902230451Smav
1903230451Smav	w = hdaa_widget_get(devinfo, nid);
1904230451Smav	if (w == NULL || w->enable == 0)
1905230451Smav		return;
1906230451Smav
1907230451Smav	/* Count number of active inputs. */
1908230451Smav	if (depth > 0) {
1909230451Smav		for (j = 0; j < w->nconns; j++) {
1910230451Smav			if (!w->connsenable[j])
1911230451Smav				continue;
1912230451Smav			conns++;
1913230130Smav		}
1914230451Smav	}
1915230130Smav
1916230451Smav	/* If this is not a first step - use input mixer.
1917230451Smav	   Pins have common input ctl so care must be taken. */
1918230451Smav	if (depth > 0 && (conns == 1 ||
1919230451Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
1920230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
1921230451Smav		    index, 1);
1922230451Smav		if (ctl)
1923230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
1924230451Smav	}
1925230451Smav
1926230451Smav	/* If widget has own ossdev - not traverse it.
1927230451Smav	   It will be traversed on it's own. */
1928230451Smav	if (w->ossdev >= 0 && depth > 0)
1929230451Smav		return;
1930230451Smav
1931230451Smav	/* We must not traverse pin */
1932230451Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
1933230451Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
1934230451Smav	    depth > 0)
1935230451Smav		return;
1936230451Smav
1937230451Smav	/*
1938230451Smav	 * If signals mixed, we can't assign controls farther.
1939230451Smav	 * Ignore this on depth zero. Caller must knows why.
1940230451Smav	 */
1941230451Smav	if (conns > 1 &&
1942230451Smav	    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
1943230451Smav	     w->selconn != index))
1944230451Smav		return;
1945230451Smav
1946230451Smav	ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
1947230451Smav	if (ctl)
1948230451Smav		hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
1949230451Smav
1950230451Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1951230451Smav		wc = hdaa_widget_get(devinfo, i);
1952230451Smav		if (wc == NULL || wc->enable == 0)
1953230451Smav			continue;
1954230451Smav		for (j = 0; j < wc->nconns; j++) {
1955230451Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
1956230451Smav				hdaa_audio_ctl_source_volume(pdevinfo, ossdev,
1957230451Smav				    wc->nid, j, mute, left, right, depth + 1);
1958230451Smav			}
1959230130Smav		}
1960230130Smav	}
1961230451Smav	return;
1962230451Smav}
1963230130Smav
1964230451Smav/*
1965230451Smav * Trace signal from destination, setting volumes on the way.
1966230451Smav */
1967230451Smavstatic void
1968230451Smavhdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo,
1969230451Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
1970230451Smav{
1971230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1972230451Smav	struct hdaa_audio_as *as = devinfo->as;
1973230451Smav	struct hdaa_widget *w, *wc;
1974230451Smav	struct hdaa_audio_ctl *ctl;
1975230451Smav	int i, j, consumers, cleft, cright;
1976230451Smav
1977230451Smav	if (depth > HDA_PARSE_MAXDEPTH)
1978230451Smav		return;
1979230451Smav
1980230451Smav	w = hdaa_widget_get(devinfo, nid);
1981230451Smav	if (w == NULL || w->enable == 0)
1982230451Smav		return;
1983230451Smav
1984230451Smav	if (depth > 0) {
1985230451Smav		/* If this node produce output for several consumers,
1986230451Smav		   we can't touch it. */
1987230451Smav		consumers = 0;
1988230451Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1989230451Smav			wc = hdaa_widget_get(devinfo, i);
1990230451Smav			if (wc == NULL || wc->enable == 0)
1991230451Smav				continue;
1992230451Smav			for (j = 0; j < wc->nconns; j++) {
1993230451Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
1994230451Smav					consumers++;
1995230451Smav			}
1996230130Smav		}
1997230451Smav		/* The only exception is if real HP redirection is configured
1998230451Smav		   and this is a duplication point.
1999230451Smav		   XXX: Actually exception is not completely correct.
2000230451Smav		   XXX: Duplication point check is not perfect. */
2001230451Smav		if ((consumers == 2 && (w->bindas < 0 ||
2002230451Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
2003230451Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
2004230451Smav		    consumers > 2)
2005230451Smav			return;
2006230451Smav
2007230451Smav		/* Else use it's output mixer. */
2008230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2009230451Smav		    HDAA_CTL_OUT, -1, 1);
2010230451Smav		if (ctl)
2011230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2012230130Smav	}
2013230130Smav
2014230451Smav	/* We must not traverse pin */
2015230451Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2016230451Smav	    depth > 0)
2017230451Smav		return;
2018230130Smav
2019230451Smav	for (i = 0; i < w->nconns; i++) {
2020230451Smav		if (w->connsenable[i] == 0)
2021230451Smav			continue;
2022230451Smav		if (index >= 0 && i != index)
2023230451Smav			continue;
2024230451Smav		cleft = left;
2025230451Smav		cright = right;
2026230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2027230451Smav		    HDAA_CTL_IN, i, 1);
2028230451Smav		if (ctl)
2029230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright);
2030230451Smav		hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1,
2031230451Smav		    mute, cleft, cright, depth + 1);
2032230451Smav	}
2033230451Smav}
2034230130Smav
2035230451Smav/*
2036230451Smav * Set volumes for the specified pdevinfo and ossdev.
2037230451Smav */
2038230451Smavstatic void
2039230451Smavhdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev)
2040230451Smav{
2041230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2042230451Smav	struct hdaa_widget *w, *cw;
2043230451Smav	uint32_t mute;
2044230451Smav	int lvol, rvol;
2045230451Smav	int i, j;
2046230130Smav
2047230451Smav	mute = 0;
2048230451Smav	if (pdevinfo->left[dev] == 0) {
2049230451Smav		mute |= HDAA_AMP_MUTE_LEFT;
2050230451Smav		lvol = -4000;
2051230451Smav	} else
2052230451Smav		lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2053230451Smav		    pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev];
2054230451Smav	if (pdevinfo->right[dev] == 0) {
2055230451Smav		mute |= HDAA_AMP_MUTE_RIGHT;
2056230451Smav		rvol = -4000;
2057230451Smav	} else
2058230451Smav		rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2059230451Smav		    pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev];
2060230451Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2061230451Smav		w = hdaa_widget_get(devinfo, i);
2062230451Smav		if (w == NULL || w->enable == 0)
2063230451Smav			continue;
2064230451Smav		if (w->bindas < 0 && pdevinfo->index != 0)
2065230451Smav			continue;
2066230451Smav		if (w->bindas != pdevinfo->playas &&
2067230451Smav		    w->bindas != pdevinfo->recas)
2068230451Smav			continue;
2069230451Smav		if (dev == SOUND_MIXER_RECLEV &&
2070230451Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2071230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2072230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2073230451Smav			continue;
2074230451Smav		}
2075230451Smav		if (dev == SOUND_MIXER_VOLUME &&
2076230451Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2077230451Smav		    devinfo->as[w->bindas].dir == HDAA_CTL_OUT) {
2078230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2079230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2080230451Smav			continue;
2081230451Smav		}
2082230451Smav		if (dev == SOUND_MIXER_IGAIN &&
2083230451Smav		    w->pflags & HDAA_ADC_MONITOR) {
2084230451Smav			for (j = 0; j < w->nconns; j++) {
2085230451Smav				if (!w->connsenable[j])
2086230451Smav				    continue;
2087230451Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
2088230451Smav				if (cw == NULL || cw->enable == 0)
2089230451Smav				    continue;
2090230451Smav				if (cw->bindas == -1)
2091230451Smav				    continue;
2092230451Smav				if (cw->bindas >= 0 &&
2093230451Smav				    devinfo->as[cw->bindas].dir != HDAA_CTL_IN)
2094230451Smav					continue;
2095230451Smav				hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2096230451Smav				    w->nid, j, mute, lvol, rvol, 0);
2097230451Smav			}
2098230451Smav			continue;
2099230451Smav		}
2100230451Smav		if (w->ossdev != dev)
2101230451Smav			continue;
2102230451Smav		hdaa_audio_ctl_source_volume(pdevinfo, dev,
2103230451Smav		    w->nid, -1, mute, lvol, rvol, 0);
2104230451Smav		if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST))
2105230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2106230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2107230451Smav	}
2108230130Smav}
2109230130Smav
2110230451Smav/*
2111230451Smav * OSS Mixer set method.
2112230451Smav */
2113230130Smavstatic int
2114230130Smavhdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
2115230130Smav					unsigned left, unsigned right)
2116230130Smav{
2117230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2118230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2119230130Smav	struct hdaa_widget *w;
2120230451Smav	int i;
2121230130Smav
2122230130Smav	hdaa_lock(devinfo);
2123230451Smav
2124230130Smav	/* Save new values. */
2125230130Smav	pdevinfo->left[dev] = left;
2126230130Smav	pdevinfo->right[dev] = right;
2127230130Smav
2128230130Smav	/* 'ogain' is the special case implemented with EAPD. */
2129230130Smav	if (dev == SOUND_MIXER_OGAIN) {
2130230130Smav		uint32_t orig;
2131230130Smav		w = NULL;
2132230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2133230130Smav			w = hdaa_widget_get(devinfo, i);
2134230130Smav			if (w == NULL || w->enable == 0)
2135230130Smav				continue;
2136230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2137230130Smav			    w->param.eapdbtl == HDA_INVALID)
2138230130Smav				continue;
2139230130Smav			break;
2140230130Smav		}
2141230130Smav		if (i >= devinfo->endnode) {
2142230130Smav			hdaa_unlock(devinfo);
2143230130Smav			return (-1);
2144230130Smav		}
2145230130Smav		orig = w->param.eapdbtl;
2146230130Smav		if (left == 0)
2147230130Smav			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2148230130Smav		else
2149230130Smav			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2150230130Smav		if (orig != w->param.eapdbtl) {
2151230130Smav			uint32_t val;
2152230130Smav
2153230130Smav			val = w->param.eapdbtl;
2154230130Smav			if (devinfo->quirks & HDAA_QUIRK_EAPDINV)
2155230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2156230130Smav			hda_command(devinfo->dev,
2157230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val));
2158230130Smav		}
2159230130Smav		hdaa_unlock(devinfo);
2160230130Smav		return (left | (left << 8));
2161230130Smav	}
2162230130Smav
2163230130Smav	/* Recalculate all controls related to this OSS device. */
2164230451Smav	hdaa_audio_ctl_dev_volume(pdevinfo, dev);
2165230451Smav
2166230451Smav	hdaa_unlock(devinfo);
2167230451Smav	return (left | (right << 8));
2168230451Smav}
2169230451Smav
2170230451Smav/*
2171230451Smav * Set mixer settings to our own default values:
2172230451Smav * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others.
2173230451Smav */
2174230451Smavstatic void
2175230451Smavhdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo)
2176230451Smav{
2177230451Smav	int amp, vol, dev;
2178230451Smav
2179230451Smav	for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) {
2180230451Smav		if ((pdevinfo->ossmask & (1 << dev)) == 0)
2181230130Smav			continue;
2182230451Smav
2183230451Smav		/* If the value was overriden, leave it as is. */
2184230451Smav		if (resource_int_value(device_get_name(pdevinfo->dev),
2185230451Smav		    device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0)
2186230130Smav			continue;
2187230130Smav
2188230451Smav		vol = -1;
2189230451Smav		if (dev == SOUND_MIXER_OGAIN)
2190230451Smav			vol = 100;
2191230451Smav		else if (dev == SOUND_MIXER_IGAIN)
2192230451Smav			vol = 0;
2193230451Smav		else if (dev == SOUND_MIXER_MIC ||
2194230451Smav		    dev == SOUND_MIXER_MONITOR)
2195230451Smav			amp = 20 * 4;	/* +20dB */
2196230451Smav		else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital)
2197230451Smav			amp = -10 * 4;	/* -10dB */
2198230451Smav		else
2199230451Smav			amp = 0;
2200230451Smav		if (vol < 0 &&
2201230451Smav		    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) {
2202230451Smav			vol = 100;
2203230451Smav		} else if (vol < 0) {
2204230451Smav			vol = ((amp - pdevinfo->minamp[dev]) * 100 +
2205230451Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) /
2206230451Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]);
2207230451Smav			vol = imin(imax(vol, 1), 100);
2208230130Smav		}
2209230451Smav		mix_set(pdevinfo->mixer, dev, vol, vol);
2210230130Smav	}
2211230130Smav}
2212230130Smav
2213230130Smav/*
2214230451Smav * Recursively commutate specified record source.
2215230130Smav */
2216230130Smavstatic uint32_t
2217230130Smavhdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
2218230130Smav{
2219230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2220230130Smav	struct hdaa_widget *w, *cw;
2221230130Smav	struct hdaa_audio_ctl *ctl;
2222230130Smav	char buf[64];
2223230130Smav	int i, muted;
2224230130Smav	uint32_t res = 0;
2225230130Smav
2226230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
2227230130Smav		return (0);
2228230130Smav
2229230130Smav	w = hdaa_widget_get(devinfo, nid);
2230230130Smav	if (w == NULL || w->enable == 0)
2231230130Smav		return (0);
2232230130Smav
2233230130Smav	for (i = 0; i < w->nconns; i++) {
2234230130Smav		if (w->connsenable[i] == 0)
2235230130Smav			continue;
2236230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
2237230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
2238230130Smav			continue;
2239230130Smav		/* Call recursively to trace signal to it's source if needed. */
2240230130Smav		if ((src & cw->ossmask) != 0) {
2241230130Smav			if (cw->ossdev < 0) {
2242230130Smav				res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2243230130Smav				    w->conns[i], depth + 1);
2244230130Smav			} else {
2245230130Smav				res |= cw->ossmask;
2246230130Smav			}
2247230130Smav		}
2248230130Smav		/* We have two special cases: mixers and others (selectors). */
2249230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
2250230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo,
2251230130Smav			    w->nid, HDAA_CTL_IN, i, 1);
2252230130Smav			if (ctl == NULL)
2253230130Smav				continue;
2254230130Smav			/* If we have input control on this node mute them
2255230130Smav			 * according to requested sources. */
2256230130Smav			muted = (src & cw->ossmask) ? 0 : 1;
2257230130Smav			if (muted != ctl->forcemute) {
2258230130Smav				ctl->forcemute = muted;
2259230130Smav				hdaa_audio_ctl_amp_set(ctl,
2260230130Smav				    HDAA_AMP_MUTE_DEFAULT,
2261230130Smav				    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
2262230130Smav			}
2263230130Smav			HDA_BOOTHVERBOSE(
2264230130Smav				device_printf(pdevinfo->dev,
2265230130Smav				    "Recsel (%s): nid %d source %d %s\n",
2266230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2267230130Smav				    src, buf, sizeof(buf)),
2268230130Smav				    nid, i, muted?"mute":"unmute");
2269230130Smav			);
2270230130Smav		} else {
2271230130Smav			if (w->nconns == 1)
2272230130Smav				break;
2273230130Smav			if ((src & cw->ossmask) == 0)
2274230130Smav				continue;
2275230130Smav			/* If we found requested source - select it and exit. */
2276230130Smav			hdaa_widget_connection_select(w, i);
2277230130Smav			HDA_BOOTHVERBOSE(
2278230130Smav				device_printf(pdevinfo->dev,
2279230130Smav				    "Recsel (%s): nid %d source %d select\n",
2280230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2281230130Smav				    src, buf, sizeof(buf)),
2282230130Smav				    nid, i);
2283230130Smav			);
2284230130Smav			break;
2285230130Smav		}
2286230130Smav	}
2287230130Smav	return (res);
2288230130Smav}
2289230130Smav
2290230130Smavstatic uint32_t
2291230130Smavhdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
2292230130Smav{
2293230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2294230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2295230130Smav	struct hdaa_widget *w;
2296230130Smav	struct hdaa_audio_as *as;
2297230451Smav	struct hdaa_audio_ctl *ctl;
2298230130Smav	struct hdaa_chan *ch;
2299230130Smav	int i, j;
2300230130Smav	uint32_t ret = 0xffffffff;
2301230130Smav
2302230130Smav	hdaa_lock(devinfo);
2303230130Smav	if (pdevinfo->recas < 0) {
2304230130Smav		hdaa_unlock(devinfo);
2305230130Smav		return (0);
2306230130Smav	}
2307230130Smav	as = &devinfo->as[pdevinfo->recas];
2308230130Smav
2309230130Smav	/* For non-mixed associations we always recording everything. */
2310230130Smav	if (!as->mixed) {
2311230130Smav		hdaa_unlock(devinfo);
2312230130Smav		return (mix_getrecdevs(m));
2313230130Smav	}
2314230130Smav
2315230130Smav	/* Commutate requested recsrc for each ADC. */
2316230130Smav	for (j = 0; j < as->num_chans; j++) {
2317230130Smav		ch = &devinfo->chans[as->chans[j]];
2318230130Smav		for (i = 0; ch->io[i] >= 0; i++) {
2319230130Smav			w = hdaa_widget_get(devinfo, ch->io[i]);
2320230130Smav			if (w == NULL || w->enable == 0)
2321230130Smav				continue;
2322230130Smav			ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2323230130Smav			    ch->io[i], 0);
2324230130Smav		}
2325230130Smav	}
2326230451Smav	if (ret == 0xffffffff)
2327230451Smav		ret = 0;
2328230130Smav
2329230451Smav	/*
2330230451Smav	 * Some controls could be shared. Reset volumes for controls
2331230451Smav	 * related to previously chosen devices, as they may no longer
2332230451Smav	 * affect the signal.
2333230451Smav	 */
2334230451Smav	i = 0;
2335230451Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
2336230451Smav		if (ctl->enable == 0 ||
2337230451Smav		    !(ctl->ossmask & pdevinfo->recsrc))
2338230451Smav			continue;
2339230451Smav		if (!((pdevinfo->playas >= 0 &&
2340230451Smav		    ctl->widget->bindas == pdevinfo->playas) ||
2341230451Smav		    (pdevinfo->recas >= 0 &&
2342230451Smav		    ctl->widget->bindas == pdevinfo->recas) ||
2343230451Smav		    (pdevinfo->index == 0 &&
2344230451Smav		    ctl->widget->bindas == -2)))
2345230451Smav			continue;
2346230451Smav		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2347230451Smav			if (pdevinfo->recsrc & (1 << j)) {
2348230451Smav				ctl->devleft[j] = 0;
2349230451Smav				ctl->devright[j] = 0;
2350230451Smav				ctl->devmute[j] = 0;
2351230451Smav			}
2352230451Smav		}
2353230451Smav	}
2354230451Smav
2355230451Smav	/*
2356230451Smav	 * Some controls could be shared. Set volumes for controls
2357230451Smav	 * related to devices selected both previously and now.
2358230451Smav	 */
2359230451Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2360230451Smav		if ((ret | pdevinfo->recsrc) & (1 << j))
2361230451Smav			hdaa_audio_ctl_dev_volume(pdevinfo, j);
2362230451Smav	}
2363230451Smav
2364230451Smav	pdevinfo->recsrc = ret;
2365230130Smav	hdaa_unlock(devinfo);
2366230451Smav	return (ret);
2367230130Smav}
2368230130Smav
2369230130Smavstatic kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = {
2370230130Smav	KOBJMETHOD(mixer_init,		hdaa_audio_ctl_ossmixer_init),
2371230130Smav	KOBJMETHOD(mixer_set,		hdaa_audio_ctl_ossmixer_set),
2372230130Smav	KOBJMETHOD(mixer_setrecsrc,	hdaa_audio_ctl_ossmixer_setrecsrc),
2373230130Smav	KOBJMETHOD_END
2374230130Smav};
2375230130SmavMIXER_DECLARE(hdaa_audio_ctl_ossmixer);
2376230130Smav
2377230130Smavstatic void
2378230130Smavhdaa_dump_gpi(struct hdaa_devinfo *devinfo)
2379230130Smav{
2380230130Smav	device_t dev = devinfo->dev;
2381230130Smav	int i;
2382230130Smav	uint32_t data, wake, unsol, sticky;
2383230130Smav
2384230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) {
2385230130Smav		data = hda_command(dev,
2386230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
2387230130Smav		wake = hda_command(dev,
2388230130Smav		    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid));
2389230130Smav		unsol = hda_command(dev,
2390230130Smav		    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2391230130Smav		sticky = hda_command(dev,
2392230130Smav		    HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid));
2393230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) {
2394230130Smav			device_printf(dev, " GPI%d:%s%s%s state=%d", i,
2395230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2396230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2397230130Smav				    (wake & (1 << i)) ? " wake" : "",
2398230130Smav				    (data >> i) & 1);
2399230130Smav		}
2400230130Smav	}
2401230130Smav}
2402230130Smav
2403230130Smavstatic void
2404230130Smavhdaa_dump_gpio(struct hdaa_devinfo *devinfo)
2405230130Smav{
2406230130Smav	device_t dev = devinfo->dev;
2407230130Smav	int i;
2408230130Smav	uint32_t data, dir, enable, wake, unsol, sticky;
2409230130Smav
2410230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) {
2411230130Smav		data = hda_command(dev,
2412230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
2413230130Smav		enable = hda_command(dev,
2414230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
2415230130Smav		dir = hda_command(dev,
2416230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
2417230130Smav		wake = hda_command(dev,
2418230130Smav		    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid));
2419230130Smav		unsol = hda_command(dev,
2420230130Smav		    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2421230130Smav		sticky = hda_command(dev,
2422230130Smav		    HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid));
2423230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) {
2424230130Smav			device_printf(dev, " GPIO%d: ", i);
2425230130Smav			if ((enable & (1 << i)) == 0) {
2426230130Smav				printf("disabled\n");
2427230130Smav				continue;
2428230130Smav			}
2429230130Smav			if ((dir & (1 << i)) == 0) {
2430230130Smav				printf("input%s%s%s",
2431230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2432230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2433230130Smav				    (wake & (1 << i)) ? " wake" : "");
2434230130Smav			} else
2435230130Smav				printf("output");
2436230130Smav			printf(" state=%d\n", (data >> i) & 1);
2437230130Smav		}
2438230130Smav	}
2439230130Smav}
2440230130Smav
2441230130Smavstatic void
2442230130Smavhdaa_dump_gpo(struct hdaa_devinfo *devinfo)
2443230130Smav{
2444230130Smav	device_t dev = devinfo->dev;
2445230130Smav	int i;
2446230130Smav	uint32_t data;
2447230130Smav
2448230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) {
2449230130Smav		data = hda_command(dev,
2450230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
2451230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) {
2452230130Smav			device_printf(dev, " GPO%d: state=%d", i,
2453230130Smav				    (data >> i) & 1);
2454230130Smav		}
2455230130Smav	}
2456230130Smav}
2457230130Smav
2458230130Smavstatic void
2459230130Smavhdaa_audio_parse(struct hdaa_devinfo *devinfo)
2460230130Smav{
2461230130Smav	struct hdaa_widget *w;
2462230130Smav	uint32_t res;
2463230130Smav	int i;
2464230130Smav	nid_t nid;
2465230130Smav
2466230130Smav	nid = devinfo->nid;
2467230130Smav
2468230130Smav	res = hda_command(devinfo->dev,
2469230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT));
2470230130Smav	devinfo->gpio_cap = res;
2471230130Smav
2472230130Smav	HDA_BOOTVERBOSE(
2473230130Smav		device_printf(devinfo->dev,
2474230130Smav		    "NumGPIO=%d NumGPO=%d "
2475230130Smav		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
2476230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
2477230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
2478230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
2479230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
2480230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
2481230130Smav		hdaa_dump_gpi(devinfo);
2482230130Smav		hdaa_dump_gpio(devinfo);
2483230130Smav		hdaa_dump_gpo(devinfo);
2484230130Smav	);
2485230130Smav
2486230130Smav	res = hda_command(devinfo->dev,
2487230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS));
2488230130Smav	devinfo->supp_stream_formats = res;
2489230130Smav
2490230130Smav	res = hda_command(devinfo->dev,
2491230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE));
2492230130Smav	devinfo->supp_pcm_size_rate = res;
2493230130Smav
2494230130Smav	res = hda_command(devinfo->dev,
2495230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP));
2496230130Smav	devinfo->outamp_cap = res;
2497230130Smav
2498230130Smav	res = hda_command(devinfo->dev,
2499230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP));
2500230130Smav	devinfo->inamp_cap = res;
2501230130Smav
2502230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2503230130Smav		w = hdaa_widget_get(devinfo, i);
2504230130Smav		if (w == NULL)
2505230130Smav			device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i);
2506230130Smav		else {
2507230130Smav			w->devinfo = devinfo;
2508230130Smav			w->nid = i;
2509230130Smav			w->enable = 1;
2510230130Smav			w->selconn = -1;
2511230130Smav			w->pflags = 0;
2512230130Smav			w->ossdev = -1;
2513230130Smav			w->bindas = -1;
2514230130Smav			w->param.eapdbtl = HDA_INVALID;
2515230130Smav			hdaa_widget_parse(w);
2516230130Smav		}
2517230130Smav	}
2518230130Smav}
2519230130Smav
2520230130Smavstatic void
2521230130Smavhdaa_audio_postprocess(struct hdaa_devinfo *devinfo)
2522230130Smav{
2523230130Smav	struct hdaa_widget *w;
2524230130Smav	int i;
2525230130Smav
2526230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2527230130Smav		w = hdaa_widget_get(devinfo, i);
2528230130Smav		if (w == NULL)
2529230130Smav			continue;
2530230130Smav		hdaa_widget_postprocess(w);
2531230130Smav	}
2532230130Smav}
2533230130Smav
2534230130Smavstatic void
2535230130Smavhdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo)
2536230130Smav{
2537230130Smav	struct hdaa_audio_ctl *ctls;
2538230130Smav	struct hdaa_widget *w, *cw;
2539230130Smav	int i, j, cnt, max, ocap, icap;
2540230130Smav	int mute, offset, step, size;
2541230130Smav
2542230130Smav	/* XXX This is redundant */
2543230130Smav	max = 0;
2544230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2545230130Smav		w = hdaa_widget_get(devinfo, i);
2546230130Smav		if (w == NULL || w->enable == 0)
2547230130Smav			continue;
2548230130Smav		if (w->param.outamp_cap != 0)
2549230130Smav			max++;
2550230130Smav		if (w->param.inamp_cap != 0) {
2551230130Smav			switch (w->type) {
2552230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2553230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2554230130Smav				for (j = 0; j < w->nconns; j++) {
2555230130Smav					cw = hdaa_widget_get(devinfo,
2556230130Smav					    w->conns[j]);
2557230130Smav					if (cw == NULL || cw->enable == 0)
2558230130Smav						continue;
2559230130Smav					max++;
2560230130Smav				}
2561230130Smav				break;
2562230130Smav			default:
2563230130Smav				max++;
2564230130Smav				break;
2565230130Smav			}
2566230130Smav		}
2567230130Smav	}
2568230130Smav	devinfo->ctlcnt = max;
2569230130Smav
2570230130Smav	if (max < 1)
2571230130Smav		return;
2572230130Smav
2573230130Smav	ctls = (struct hdaa_audio_ctl *)malloc(
2574230130Smav	    sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT);
2575230130Smav
2576230130Smav	if (ctls == NULL) {
2577230130Smav		/* Blekh! */
2578230130Smav		device_printf(devinfo->dev, "unable to allocate ctls!\n");
2579230130Smav		devinfo->ctlcnt = 0;
2580230130Smav		return;
2581230130Smav	}
2582230130Smav
2583230130Smav	cnt = 0;
2584230130Smav	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
2585230130Smav		if (cnt >= max) {
2586230130Smav			device_printf(devinfo->dev, "%s: Ctl overflow!\n",
2587230130Smav			    __func__);
2588230130Smav			break;
2589230130Smav		}
2590230130Smav		w = hdaa_widget_get(devinfo, i);
2591230130Smav		if (w == NULL || w->enable == 0)
2592230130Smav			continue;
2593230130Smav		ocap = w->param.outamp_cap;
2594230130Smav		icap = w->param.inamp_cap;
2595230130Smav		if (ocap != 0) {
2596230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
2597230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
2598230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
2599230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
2600230130Smav			/*if (offset > step) {
2601230130Smav				HDA_BOOTVERBOSE(
2602230130Smav					device_printf(devinfo->dev,
2603230130Smav					    "BUGGY outamp: nid=%d "
2604230130Smav					    "[offset=%d > step=%d]\n",
2605230130Smav					    w->nid, offset, step);
2606230130Smav				);
2607230130Smav				offset = step;
2608230130Smav			}*/
2609230130Smav			ctls[cnt].enable = 1;
2610230130Smav			ctls[cnt].widget = w;
2611230130Smav			ctls[cnt].mute = mute;
2612230130Smav			ctls[cnt].step = step;
2613230130Smav			ctls[cnt].size = size;
2614230130Smav			ctls[cnt].offset = offset;
2615230130Smav			ctls[cnt].left = offset;
2616230130Smav			ctls[cnt].right = offset;
2617230130Smav			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2618230130Smav			    w->waspin)
2619230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
2620230130Smav			else
2621230130Smav				ctls[cnt].ndir = HDAA_CTL_OUT;
2622230130Smav			ctls[cnt++].dir = HDAA_CTL_OUT;
2623230130Smav		}
2624230130Smav
2625230130Smav		if (icap != 0) {
2626230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
2627230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
2628230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
2629230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
2630230130Smav			/*if (offset > step) {
2631230130Smav				HDA_BOOTVERBOSE(
2632230130Smav					device_printf(devinfo->dev,
2633230130Smav					    "BUGGY inamp: nid=%d "
2634230130Smav					    "[offset=%d > step=%d]\n",
2635230130Smav					    w->nid, offset, step);
2636230130Smav				);
2637230130Smav				offset = step;
2638230130Smav			}*/
2639230130Smav			switch (w->type) {
2640230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2641230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2642230130Smav				for (j = 0; j < w->nconns; j++) {
2643230130Smav					if (cnt >= max) {
2644230130Smav						device_printf(devinfo->dev,
2645230130Smav						    "%s: Ctl overflow!\n",
2646230130Smav						    __func__);
2647230130Smav						break;
2648230130Smav					}
2649230130Smav					cw = hdaa_widget_get(devinfo,
2650230130Smav					    w->conns[j]);
2651230130Smav					if (cw == NULL || cw->enable == 0)
2652230130Smav						continue;
2653230130Smav					ctls[cnt].enable = 1;
2654230130Smav					ctls[cnt].widget = w;
2655230130Smav					ctls[cnt].childwidget = cw;
2656230130Smav					ctls[cnt].index = j;
2657230130Smav					ctls[cnt].mute = mute;
2658230130Smav					ctls[cnt].step = step;
2659230130Smav					ctls[cnt].size = size;
2660230130Smav					ctls[cnt].offset = offset;
2661230130Smav					ctls[cnt].left = offset;
2662230130Smav					ctls[cnt].right = offset;
2663230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
2664230130Smav					ctls[cnt++].dir = HDAA_CTL_IN;
2665230130Smav				}
2666230130Smav				break;
2667230130Smav			default:
2668230130Smav				if (cnt >= max) {
2669230130Smav					device_printf(devinfo->dev,
2670230130Smav					    "%s: Ctl overflow!\n",
2671230130Smav					    __func__);
2672230130Smav					break;
2673230130Smav				}
2674230130Smav				ctls[cnt].enable = 1;
2675230130Smav				ctls[cnt].widget = w;
2676230130Smav				ctls[cnt].mute = mute;
2677230130Smav				ctls[cnt].step = step;
2678230130Smav				ctls[cnt].size = size;
2679230130Smav				ctls[cnt].offset = offset;
2680230130Smav				ctls[cnt].left = offset;
2681230130Smav				ctls[cnt].right = offset;
2682230130Smav				if (w->type ==
2683230130Smav				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2684230130Smav					ctls[cnt].ndir = HDAA_CTL_OUT;
2685230130Smav				else
2686230130Smav					ctls[cnt].ndir = HDAA_CTL_IN;
2687230130Smav				ctls[cnt++].dir = HDAA_CTL_IN;
2688230130Smav				break;
2689230130Smav			}
2690230130Smav		}
2691230130Smav	}
2692230130Smav
2693230130Smav	devinfo->ctl = ctls;
2694230130Smav}
2695230130Smav
2696230130Smavstatic void
2697230130Smavhdaa_audio_as_parse(struct hdaa_devinfo *devinfo)
2698230130Smav{
2699230130Smav	struct hdaa_audio_as *as;
2700230130Smav	struct hdaa_widget *w;
2701230130Smav	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
2702230130Smav
2703230130Smav	/* Count present associations */
2704230130Smav	max = 0;
2705230130Smav	for (j = 1; j < 16; j++) {
2706230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2707230130Smav			w = hdaa_widget_get(devinfo, i);
2708230130Smav			if (w == NULL || w->enable == 0)
2709230130Smav				continue;
2710230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2711230130Smav				continue;
2712230130Smav			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
2713230130Smav			    != j)
2714230130Smav				continue;
2715230130Smav			max++;
2716230130Smav			if (j != 15)  /* There could be many 1-pin assocs #15 */
2717230130Smav				break;
2718230130Smav		}
2719230130Smav	}
2720230130Smav
2721230130Smav	devinfo->ascnt = max;
2722230130Smav
2723230130Smav	if (max < 1)
2724230130Smav		return;
2725230130Smav
2726230130Smav	as = (struct hdaa_audio_as *)malloc(
2727230130Smav	    sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT);
2728230130Smav
2729230130Smav	if (as == NULL) {
2730230130Smav		/* Blekh! */
2731230130Smav		device_printf(devinfo->dev, "unable to allocate assocs!\n");
2732230130Smav		devinfo->ascnt = 0;
2733230130Smav		return;
2734230130Smav	}
2735230130Smav
2736230130Smav	for (i = 0; i < max; i++) {
2737230130Smav		as[i].hpredir = -1;
2738230130Smav		as[i].digital = 0;
2739230130Smav		as[i].num_chans = 1;
2740230130Smav		as[i].location = -1;
2741230130Smav	}
2742230130Smav
2743230130Smav	/* Scan associations skipping as=0. */
2744230130Smav	cnt = 0;
2745230130Smav	for (j = 1; j < 16; j++) {
2746230130Smav		first = 16;
2747230130Smav		hpredir = 0;
2748230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2749230130Smav			w = hdaa_widget_get(devinfo, i);
2750230130Smav			if (w == NULL || w->enable == 0)
2751230130Smav				continue;
2752230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2753230130Smav				continue;
2754230130Smav			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
2755230130Smav			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
2756230130Smav			if (assoc != j) {
2757230130Smav				continue;
2758230130Smav			}
2759230130Smav			KASSERT(cnt < max,
2760230130Smav			    ("%s: Associations owerflow (%d of %d)",
2761230130Smav			    __func__, cnt, max));
2762230130Smav			type = w->wclass.pin.config &
2763230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2764230130Smav			/* Get pin direction. */
2765230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
2766230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
2767230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
2768230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
2769230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
2770230130Smav				dir = HDAA_CTL_OUT;
2771230130Smav			else
2772230130Smav				dir = HDAA_CTL_IN;
2773230130Smav			/* If this is a first pin - create new association. */
2774230130Smav			if (as[cnt].pincnt == 0) {
2775230130Smav				as[cnt].enable = 1;
2776230130Smav				as[cnt].index = j;
2777230130Smav				as[cnt].dir = dir;
2778230130Smav			}
2779230130Smav			if (seq < first)
2780230130Smav				first = seq;
2781230130Smav			/* Check association correctness. */
2782230130Smav			if (as[cnt].pins[seq] != 0) {
2783230130Smav				device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) "
2784230130Smav				    "in association %d! Disabling association.\n",
2785230130Smav				    __func__, seq, w->nid, j);
2786230130Smav				as[cnt].enable = 0;
2787230130Smav			}
2788230130Smav			if (dir != as[cnt].dir) {
2789230130Smav				device_printf(devinfo->dev, "%s: Pin %d has wrong "
2790230130Smav				    "direction for association %d! Disabling "
2791230130Smav				    "association.\n",
2792230130Smav				    __func__, w->nid, j);
2793230130Smav				as[cnt].enable = 0;
2794230130Smav			}
2795230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
2796230507Smav				as[cnt].digital |= 0x1;
2797230507Smav				if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
2798230507Smav					as[cnt].digital |= 0x2;
2799230130Smav				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
2800230507Smav					as[cnt].digital |= 0x4;
2801230130Smav			}
2802230130Smav			if (as[cnt].location == -1) {
2803230130Smav				as[cnt].location =
2804230130Smav				    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config);
2805230130Smav			} else if (as[cnt].location !=
2806230130Smav			    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) {
2807230130Smav				as[cnt].location = -2;
2808230130Smav			}
2809230130Smav			/* Headphones with seq=15 may mean redirection. */
2810230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
2811230130Smav			    seq == 15)
2812230130Smav				hpredir = 1;
2813230130Smav			as[cnt].pins[seq] = w->nid;
2814230130Smav			as[cnt].pincnt++;
2815230130Smav			/* Association 15 is a multiple unassociated pins. */
2816230130Smav			if (j == 15)
2817230130Smav				cnt++;
2818230130Smav		}
2819230130Smav		if (j != 15 && as[cnt].pincnt > 0) {
2820230130Smav			if (hpredir && as[cnt].pincnt > 1)
2821230130Smav				as[cnt].hpredir = first;
2822230130Smav			cnt++;
2823230130Smav		}
2824230130Smav	}
2825230130Smav	for (i = 0; i < max; i++) {
2826230130Smav		if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 ||
2827230130Smav		    as[i].pins[14] > 0 || as[i].pins[15] > 0))
2828230130Smav			as[i].mixed = 1;
2829230130Smav	}
2830230130Smav	HDA_BOOTVERBOSE(
2831230130Smav		device_printf(devinfo->dev,
2832230130Smav		    "%d associations found:\n", max);
2833230130Smav		for (i = 0; i < max; i++) {
2834230130Smav			device_printf(devinfo->dev,
2835230130Smav			    "Association %d (%d) %s%s:\n",
2836230130Smav			    i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out",
2837230130Smav			    as[i].enable?"":" (disabled)");
2838230130Smav			for (j = 0; j < 16; j++) {
2839230130Smav				if (as[i].pins[j] == 0)
2840230130Smav					continue;
2841230130Smav				device_printf(devinfo->dev,
2842230130Smav				    " Pin nid=%d seq=%d\n",
2843230130Smav				    as[i].pins[j], j);
2844230130Smav			}
2845230130Smav		}
2846230130Smav	);
2847230130Smav
2848230130Smav	devinfo->as = as;
2849230130Smav}
2850230130Smav
2851230130Smav/*
2852230130Smav * Trace path from DAC to pin.
2853230130Smav */
2854230130Smavstatic nid_t
2855230130Smavhdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
2856230130Smav    int dupseq, int min, int only, int depth)
2857230130Smav{
2858230130Smav	struct hdaa_widget *w;
2859230130Smav	int i, im = -1;
2860230130Smav	nid_t m = 0, ret;
2861230130Smav
2862230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
2863230130Smav		return (0);
2864230130Smav	w = hdaa_widget_get(devinfo, nid);
2865230130Smav	if (w == NULL || w->enable == 0)
2866230130Smav		return (0);
2867230130Smav	HDA_BOOTHVERBOSE(
2868230130Smav		if (!only) {
2869230130Smav			device_printf(devinfo->dev,
2870230130Smav			    " %*stracing via nid %d\n",
2871230130Smav				depth + 1, "", w->nid);
2872230130Smav		}
2873230130Smav	);
2874230130Smav	/* Use only unused widgets */
2875230130Smav	if (w->bindas >= 0 && w->bindas != as) {
2876230130Smav		HDA_BOOTHVERBOSE(
2877230130Smav			if (!only) {
2878230130Smav				device_printf(devinfo->dev,
2879230130Smav				    " %*snid %d busy by association %d\n",
2880230130Smav					depth + 1, "", w->nid, w->bindas);
2881230130Smav			}
2882230130Smav		);
2883230130Smav		return (0);
2884230130Smav	}
2885230130Smav	if (dupseq < 0) {
2886230130Smav		if (w->bindseqmask != 0) {
2887230130Smav			HDA_BOOTHVERBOSE(
2888230130Smav				if (!only) {
2889230130Smav					device_printf(devinfo->dev,
2890230130Smav					    " %*snid %d busy by seqmask %x\n",
2891230130Smav						depth + 1, "", w->nid, w->bindseqmask);
2892230130Smav				}
2893230130Smav			);
2894230130Smav			return (0);
2895230130Smav		}
2896230130Smav	} else {
2897230130Smav		/* If this is headphones - allow duplicate first pin. */
2898230130Smav		if (w->bindseqmask != 0 &&
2899230130Smav		    (w->bindseqmask & (1 << dupseq)) == 0) {
2900230130Smav			HDA_BOOTHVERBOSE(
2901230130Smav				device_printf(devinfo->dev,
2902230130Smav				    " %*snid %d busy by seqmask %x\n",
2903230130Smav					depth + 1, "", w->nid, w->bindseqmask);
2904230130Smav			);
2905230130Smav			return (0);
2906230130Smav		}
2907230130Smav	}
2908230130Smav
2909230130Smav	switch (w->type) {
2910230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
2911230130Smav		/* Do not traverse input. AD1988 has digital monitor
2912230130Smav		for which we are not ready. */
2913230130Smav		break;
2914230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
2915230130Smav		/* If we are tracing HP take only dac of first pin. */
2916230130Smav		if ((only == 0 || only == w->nid) &&
2917230130Smav		    (w->nid >= min) && (dupseq < 0 || w->nid ==
2918230130Smav		    devinfo->as[as].dacs[0][dupseq]))
2919230130Smav			m = w->nid;
2920230130Smav		break;
2921230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
2922230130Smav		if (depth > 0)
2923230130Smav			break;
2924230130Smav		/* Fall */
2925230130Smav	default:
2926230130Smav		/* Find reachable DACs with smallest nid respecting constraints. */
2927230130Smav		for (i = 0; i < w->nconns; i++) {
2928230130Smav			if (w->connsenable[i] == 0)
2929230130Smav				continue;
2930230130Smav			if (w->selconn != -1 && w->selconn != i)
2931230130Smav				continue;
2932230130Smav			if ((ret = hdaa_audio_trace_dac(devinfo, as, seq,
2933230130Smav			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
2934230130Smav				if (m == 0 || ret < m) {
2935230130Smav					m = ret;
2936230130Smav					im = i;
2937230130Smav				}
2938230130Smav				if (only || dupseq >= 0)
2939230130Smav					break;
2940230130Smav			}
2941230130Smav		}
2942230130Smav		if (im >= 0 && only && ((w->nconns > 1 &&
2943230130Smav		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
2944230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
2945230130Smav			w->selconn = im;
2946230130Smav		break;
2947230130Smav	}
2948230130Smav	if (m && only) {
2949230130Smav		w->bindas = as;
2950230130Smav		w->bindseqmask |= (1 << seq);
2951230130Smav	}
2952230130Smav	HDA_BOOTHVERBOSE(
2953230130Smav		if (!only) {
2954230130Smav			device_printf(devinfo->dev,
2955230130Smav			    " %*snid %d returned %d\n",
2956230130Smav				depth + 1, "", w->nid, m);
2957230130Smav		}
2958230130Smav	);
2959230130Smav	return (m);
2960230130Smav}
2961230130Smav
2962230130Smav/*
2963230130Smav * Trace path from widget to ADC.
2964230130Smav */
2965230130Smavstatic nid_t
2966230130Smavhdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
2967230130Smav    int mixed, int min, int only, int depth, int *length, int onlylength)
2968230130Smav{
2969230130Smav	struct hdaa_widget *w, *wc;
2970230130Smav	int i, j, im, lm = HDA_PARSE_MAXDEPTH;
2971230130Smav	nid_t m = 0, ret;
2972230130Smav
2973230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
2974230130Smav		return (0);
2975230130Smav	w = hdaa_widget_get(devinfo, nid);
2976230130Smav	if (w == NULL || w->enable == 0)
2977230130Smav		return (0);
2978230130Smav	HDA_BOOTHVERBOSE(
2979230130Smav		device_printf(devinfo->dev,
2980230130Smav		    " %*stracing via nid %d\n",
2981230130Smav			depth + 1, "", w->nid);
2982230130Smav	);
2983230130Smav	/* Use only unused widgets */
2984230130Smav	if (w->bindas >= 0 && w->bindas != as) {
2985230130Smav		HDA_BOOTHVERBOSE(
2986230130Smav			device_printf(devinfo->dev,
2987230130Smav			    " %*snid %d busy by association %d\n",
2988230130Smav				depth + 1, "", w->nid, w->bindas);
2989230130Smav		);
2990230130Smav		return (0);
2991230130Smav	}
2992230130Smav	if (!mixed && w->bindseqmask != 0) {
2993230130Smav		HDA_BOOTHVERBOSE(
2994230130Smav			device_printf(devinfo->dev,
2995230130Smav			    " %*snid %d busy by seqmask %x\n",
2996230130Smav				depth + 1, "", w->nid, w->bindseqmask);
2997230130Smav		);
2998230130Smav		return (0);
2999230130Smav	}
3000230130Smav	switch (w->type) {
3001230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3002230130Smav		if ((only == 0 || only == w->nid) && (w->nid >= min) &&
3003230130Smav		    (onlylength == 0 || onlylength == depth)) {
3004230130Smav			m = w->nid;
3005230130Smav			if (length != NULL)
3006230130Smav				*length = depth;
3007230130Smav		}
3008230130Smav		break;
3009230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3010230130Smav		if (depth > 0)
3011230130Smav			break;
3012230130Smav		/* Fall */
3013230130Smav	default:
3014230130Smav		/* Try to find reachable ADCs with specified nid. */
3015230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3016230130Smav			wc = hdaa_widget_get(devinfo, j);
3017230130Smav			if (wc == NULL || wc->enable == 0)
3018230130Smav				continue;
3019230130Smav			im = -1;
3020230130Smav			for (i = 0; i < wc->nconns; i++) {
3021230130Smav				if (wc->connsenable[i] == 0)
3022230130Smav					continue;
3023230130Smav				if (wc->conns[i] != nid)
3024230130Smav					continue;
3025230130Smav				if ((ret = hdaa_audio_trace_adc(devinfo, as, seq,
3026230130Smav				    j, mixed, min, only, depth + 1,
3027230130Smav				    length, onlylength)) != 0) {
3028230130Smav					if (m == 0 || ret < m ||
3029230130Smav					    (ret == m && length != NULL &&
3030230130Smav					     *length < lm)) {
3031230130Smav						m = ret;
3032230130Smav						im = i;
3033230130Smav						lm = *length;
3034230130Smav					}
3035230130Smav					if (only)
3036230130Smav						break;
3037230130Smav				}
3038230130Smav			}
3039230130Smav			if (im >= 0 && only && ((wc->nconns > 1 &&
3040230130Smav			    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3041230130Smav			    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3042230130Smav				wc->selconn = im;
3043230130Smav		}
3044230130Smav		break;
3045230130Smav	}
3046230130Smav	if (m && only) {
3047230130Smav		w->bindas = as;
3048230130Smav		w->bindseqmask |= (1 << seq);
3049230130Smav	}
3050230130Smav	HDA_BOOTHVERBOSE(
3051230130Smav		device_printf(devinfo->dev,
3052230130Smav		    " %*snid %d returned %d\n",
3053230130Smav			depth + 1, "", w->nid, m);
3054230130Smav	);
3055230130Smav	return (m);
3056230130Smav}
3057230130Smav
3058230130Smav/*
3059230130Smav * Erase trace path of the specified association.
3060230130Smav */
3061230130Smavstatic void
3062230130Smavhdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq)
3063230130Smav{
3064230130Smav	struct hdaa_widget *w;
3065230130Smav	int i;
3066230130Smav
3067230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3068230130Smav		w = hdaa_widget_get(devinfo, i);
3069230130Smav		if (w == NULL || w->enable == 0)
3070230130Smav			continue;
3071230130Smav		if (w->bindas == as) {
3072230130Smav			if (seq >= 0) {
3073230130Smav				w->bindseqmask &= ~(1 << seq);
3074230130Smav				if (w->bindseqmask == 0) {
3075230130Smav					w->bindas = -1;
3076230130Smav					w->selconn = -1;
3077230130Smav				}
3078230130Smav			} else {
3079230130Smav				w->bindas = -1;
3080230130Smav				w->bindseqmask = 0;
3081230130Smav				w->selconn = -1;
3082230130Smav			}
3083230130Smav		}
3084230130Smav	}
3085230130Smav}
3086230130Smav
3087230130Smav/*
3088230130Smav * Trace association path from DAC to output
3089230130Smav */
3090230130Smavstatic int
3091230130Smavhdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq)
3092230130Smav{
3093230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3094230130Smav	int i, hpredir;
3095230130Smav	nid_t min, res;
3096230130Smav
3097230130Smav	/* Find next pin */
3098230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3099230130Smav		;
3100230130Smav	/* Check if there is no any left. If so - we succeeded. */
3101230130Smav	if (i == 16)
3102230130Smav		return (1);
3103230130Smav
3104230130Smav	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
3105230130Smav	min = 0;
3106230130Smav	do {
3107230130Smav		HDA_BOOTHVERBOSE(
3108230130Smav			device_printf(devinfo->dev,
3109230130Smav			    " Tracing pin %d with min nid %d",
3110230130Smav			    ases[as].pins[i], min);
3111230130Smav			if (hpredir >= 0)
3112230130Smav				printf(" and hpredir %d", hpredir);
3113230130Smav			printf("\n");
3114230130Smav		);
3115230130Smav		/* Trace this pin taking min nid into account. */
3116230130Smav		res = hdaa_audio_trace_dac(devinfo, as, i,
3117230130Smav		    ases[as].pins[i], hpredir, min, 0, 0);
3118230130Smav		if (res == 0) {
3119230130Smav			/* If we failed - return to previous and redo it. */
3120230130Smav			HDA_BOOTVERBOSE(
3121230130Smav				device_printf(devinfo->dev,
3122230130Smav				    " Unable to trace pin %d seq %d with min "
3123230130Smav				    "nid %d",
3124230130Smav				    ases[as].pins[i], i, min);
3125230130Smav				if (hpredir >= 0)
3126230130Smav					printf(" and hpredir %d", hpredir);
3127230130Smav				printf("\n");
3128230130Smav			);
3129230130Smav			return (0);
3130230130Smav		}
3131230130Smav		HDA_BOOTVERBOSE(
3132230130Smav			device_printf(devinfo->dev,
3133230130Smav			    " Pin %d traced to DAC %d",
3134230130Smav			    ases[as].pins[i], res);
3135230130Smav			if (hpredir >= 0)
3136230130Smav				printf(" and hpredir %d", hpredir);
3137230130Smav			if (ases[as].fakeredir)
3138230130Smav				printf(" with fake redirection");
3139230130Smav			printf("\n");
3140230130Smav		);
3141230130Smav		/* Trace again to mark the path */
3142230130Smav		hdaa_audio_trace_dac(devinfo, as, i,
3143230130Smav		    ases[as].pins[i], hpredir, min, res, 0);
3144230130Smav		ases[as].dacs[0][i] = res;
3145230130Smav		/* We succeeded, so call next. */
3146230130Smav		if (hdaa_audio_trace_as_out(devinfo, as, i + 1))
3147230130Smav			return (1);
3148230130Smav		/* If next failed, we should retry with next min */
3149230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3150230130Smav		ases[as].dacs[0][i] = 0;
3151230130Smav		min = res + 1;
3152230130Smav	} while (1);
3153230130Smav}
3154230130Smav
3155230130Smav/*
3156230130Smav * Check equivalency of two DACs.
3157230130Smav */
3158230130Smavstatic int
3159230130Smavhdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3160230130Smav{
3161230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3162230130Smav	struct hdaa_widget *w3;
3163230130Smav	int i, j, c1, c2;
3164230130Smav
3165230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3166230130Smav		return (0);
3167230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3168230130Smav		w3 = hdaa_widget_get(devinfo, i);
3169230130Smav		if (w3 == NULL || w3->enable == 0)
3170230130Smav			continue;
3171230130Smav		if (w3->bindas != w1->bindas)
3172230130Smav			continue;
3173230130Smav		if (w3->nconns == 0)
3174230130Smav			continue;
3175230130Smav		c1 = c2 = -1;
3176230130Smav		for (j = 0; j < w3->nconns; j++) {
3177230130Smav			if (w3->connsenable[j] == 0)
3178230130Smav				continue;
3179230130Smav			if (w3->conns[j] == w1->nid)
3180230130Smav				c1 = j;
3181230130Smav			if (w3->conns[j] == w2->nid)
3182230130Smav				c2 = j;
3183230130Smav		}
3184230130Smav		if (c1 < 0)
3185230130Smav			continue;
3186230130Smav		if (c2 < 0)
3187230130Smav			return (0);
3188230130Smav		if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3189230130Smav			return (0);
3190230130Smav	}
3191230130Smav	return (1);
3192230130Smav}
3193230130Smav
3194230130Smav/*
3195230130Smav * Check equivalency of two ADCs.
3196230130Smav */
3197230130Smavstatic int
3198230130Smavhdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3199230130Smav{
3200230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3201230130Smav	struct hdaa_widget *w3, *w4;
3202230130Smav	int i;
3203230130Smav
3204230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3205230130Smav		return (0);
3206230130Smav	if (w1->nconns != 1 || w2->nconns != 1)
3207230130Smav		return (0);
3208230130Smav	if (w1->conns[0] == w2->conns[0])
3209230130Smav		return (1);
3210230130Smav	w3 = hdaa_widget_get(devinfo, w1->conns[0]);
3211230130Smav	if (w3 == NULL || w3->enable == 0)
3212230130Smav		return (0);
3213230130Smav	w4 = hdaa_widget_get(devinfo, w2->conns[0]);
3214230130Smav	if (w4 == NULL || w4->enable == 0)
3215230130Smav		return (0);
3216230130Smav	if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask)
3217230130Smav		return (1);
3218230130Smav	if (w4->bindas >= 0)
3219230130Smav		return (0);
3220230130Smav	if (w3->type != w4->type)
3221230130Smav		return (0);
3222230130Smav	if (memcmp(&w3->param, &w4->param, sizeof(w3->param)))
3223230130Smav		return (0);
3224230130Smav	if (w3->nconns != w4->nconns)
3225230130Smav		return (0);
3226230130Smav	for (i = 0; i < w3->nconns; i++) {
3227230130Smav		if (w3->conns[i] != w4->conns[i])
3228230130Smav			return (0);
3229230130Smav	}
3230230130Smav	return (1);
3231230130Smav}
3232230130Smav
3233230130Smav/*
3234230130Smav * Look for equivalent DAC/ADC to implement second channel.
3235230130Smav */
3236230130Smavstatic void
3237230130Smavhdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid)
3238230130Smav{
3239230130Smav	struct hdaa_audio_as *as = &devinfo->as[asid];
3240230130Smav	struct hdaa_widget *w1, *w2;
3241230130Smav	int i, pos;
3242230130Smav	nid_t nid1, nid2;
3243230130Smav
3244230130Smav	HDA_BOOTVERBOSE(
3245230130Smav		device_printf(devinfo->dev,
3246230130Smav		    "Looking for additional %sC "
3247230130Smav		    "for association %d (%d)\n",
3248230130Smav		    (as->dir == HDAA_CTL_OUT) ? "DA" : "AD",
3249230130Smav		    asid, as->index);
3250230130Smav	);
3251230130Smav
3252230130Smav	/* Find the exisitng DAC position and return if found more the one. */
3253230130Smav	pos = -1;
3254230130Smav	for (i = 0; i < 16; i++) {
3255230130Smav		if (as->dacs[0][i] <= 0)
3256230130Smav			continue;
3257230130Smav		if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos])
3258230130Smav			return;
3259230130Smav		pos = i;
3260230130Smav	}
3261230130Smav
3262230130Smav	nid1 = as->dacs[0][pos];
3263230130Smav	w1 = hdaa_widget_get(devinfo, nid1);
3264230130Smav	w2 = NULL;
3265230130Smav	for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) {
3266230130Smav		w2 = hdaa_widget_get(devinfo, nid2);
3267230130Smav		if (w2 == NULL || w2->enable == 0)
3268230130Smav			continue;
3269230130Smav		if (w2->bindas >= 0)
3270230130Smav			continue;
3271230130Smav		if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
3272230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
3273230130Smav				continue;
3274230130Smav			if (hdaa_audio_dacs_equal(w1, w2))
3275230130Smav				break;
3276230130Smav		} else {
3277230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3278230130Smav				continue;
3279230130Smav			if (hdaa_audio_adcs_equal(w1, w2))
3280230130Smav				break;
3281230130Smav		}
3282230130Smav	}
3283230130Smav	if (nid2 >= devinfo->endnode)
3284230130Smav		return;
3285230130Smav	w2->bindas = w1->bindas;
3286230130Smav	w2->bindseqmask = w1->bindseqmask;
3287230130Smav	if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
3288230130Smav		HDA_BOOTVERBOSE(
3289230130Smav			device_printf(devinfo->dev,
3290230130Smav			    " ADC %d considered equal to ADC %d\n", nid2, nid1);
3291230130Smav		);
3292230130Smav		w1 = hdaa_widget_get(devinfo, w1->conns[0]);
3293230130Smav		w2 = hdaa_widget_get(devinfo, w2->conns[0]);
3294230130Smav		w2->bindas = w1->bindas;
3295230130Smav		w2->bindseqmask = w1->bindseqmask;
3296230130Smav	} else {
3297230130Smav		HDA_BOOTVERBOSE(
3298230130Smav			device_printf(devinfo->dev,
3299230130Smav			    " DAC %d considered equal to DAC %d\n", nid2, nid1);
3300230130Smav		);
3301230130Smav	}
3302230130Smav	for (i = 0; i < 16; i++) {
3303230130Smav		if (as->dacs[0][i] <= 0)
3304230130Smav			continue;
3305230130Smav		as->dacs[as->num_chans][i] = nid2;
3306230130Smav	}
3307230130Smav	as->num_chans++;
3308230130Smav}
3309230130Smav
3310230130Smav/*
3311230130Smav * Trace association path from input to ADC
3312230130Smav */
3313230130Smavstatic int
3314230130Smavhdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as)
3315230130Smav{
3316230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3317230130Smav	struct hdaa_widget *w;
3318230130Smav	int i, j, k, length;
3319230130Smav
3320230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3321230130Smav		w = hdaa_widget_get(devinfo, j);
3322230130Smav		if (w == NULL || w->enable == 0)
3323230130Smav			continue;
3324230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3325230130Smav			continue;
3326230130Smav		if (w->bindas >= 0 && w->bindas != as)
3327230130Smav			continue;
3328230130Smav
3329230130Smav		/* Find next pin */
3330230130Smav		for (i = 0; i < 16; i++) {
3331230130Smav			if (ases[as].pins[i] == 0)
3332230130Smav				continue;
3333230130Smav
3334230130Smav			HDA_BOOTHVERBOSE(
3335230130Smav				device_printf(devinfo->dev,
3336230130Smav				    " Tracing pin %d to ADC %d\n",
3337230130Smav				    ases[as].pins[i], j);
3338230130Smav			);
3339230130Smav			/* Trace this pin taking goal into account. */
3340230130Smav			if (hdaa_audio_trace_adc(devinfo, as, i,
3341230130Smav			    ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) {
3342230130Smav				/* If we failed - return to previous and redo it. */
3343230130Smav				HDA_BOOTVERBOSE(
3344230130Smav					device_printf(devinfo->dev,
3345230130Smav					    " Unable to trace pin %d to ADC %d, undo traces\n",
3346230130Smav					    ases[as].pins[i], j);
3347230130Smav				);
3348230130Smav				hdaa_audio_undo_trace(devinfo, as, -1);
3349230130Smav				for (k = 0; k < 16; k++)
3350230130Smav					ases[as].dacs[0][k] = 0;
3351230130Smav				break;
3352230130Smav			}
3353230130Smav			HDA_BOOTVERBOSE(
3354230130Smav				device_printf(devinfo->dev,
3355230130Smav				    " Pin %d traced to ADC %d\n",
3356230130Smav				    ases[as].pins[i], j);
3357230130Smav			);
3358230130Smav			ases[as].dacs[0][i] = j;
3359230130Smav		}
3360230130Smav		if (i == 16)
3361230130Smav			return (1);
3362230130Smav	}
3363230130Smav	return (0);
3364230130Smav}
3365230130Smav
3366230130Smav/*
3367230130Smav * Trace association path from input to multiple ADCs
3368230130Smav */
3369230130Smavstatic int
3370230130Smavhdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq)
3371230130Smav{
3372230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3373230130Smav	int i, length;
3374230130Smav	nid_t min, res;
3375230130Smav
3376230130Smav	/* Find next pin */
3377230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3378230130Smav		;
3379230130Smav	/* Check if there is no any left. If so - we succeeded. */
3380230130Smav	if (i == 16)
3381230130Smav		return (1);
3382230130Smav
3383230130Smav	min = 0;
3384230130Smav	do {
3385230130Smav		HDA_BOOTHVERBOSE(
3386230130Smav			device_printf(devinfo->dev,
3387230130Smav			    " Tracing pin %d with min nid %d",
3388230130Smav			    ases[as].pins[i], min);
3389230130Smav			printf("\n");
3390230130Smav		);
3391230130Smav		/* Trace this pin taking min nid into account. */
3392230130Smav		res = hdaa_audio_trace_adc(devinfo, as, i,
3393230130Smav		    ases[as].pins[i], 0, min, 0, 0, &length, 0);
3394230130Smav		if (res == 0) {
3395230130Smav			/* If we failed - return to previous and redo it. */
3396230130Smav			HDA_BOOTVERBOSE(
3397230130Smav				device_printf(devinfo->dev,
3398230130Smav				    " Unable to trace pin %d seq %d with min "
3399230130Smav				    "nid %d",
3400230130Smav				    ases[as].pins[i], i, min);
3401230130Smav				printf("\n");
3402230130Smav			);
3403230130Smav			return (0);
3404230130Smav		}
3405230130Smav		HDA_BOOTVERBOSE(
3406230130Smav			device_printf(devinfo->dev,
3407230130Smav			    " Pin %d traced to ADC %d\n",
3408230130Smav			    ases[as].pins[i], res);
3409230130Smav		);
3410230130Smav		/* Trace again to mark the path */
3411230130Smav		hdaa_audio_trace_adc(devinfo, as, i,
3412230130Smav		    ases[as].pins[i], 0, min, res, 0, &length, length);
3413230130Smav		ases[as].dacs[0][i] = res;
3414230130Smav		/* We succeeded, so call next. */
3415230130Smav		if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1))
3416230130Smav			return (1);
3417230130Smav		/* If next failed, we should retry with next min */
3418230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3419230130Smav		ases[as].dacs[0][i] = 0;
3420230130Smav		min = res + 1;
3421230130Smav	} while (1);
3422230130Smav}
3423230130Smav
3424230130Smav/*
3425230130Smav * Trace input monitor path from mixer to output association.
3426230130Smav */
3427230130Smavstatic int
3428230130Smavhdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth)
3429230130Smav{
3430230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3431230130Smav	struct hdaa_widget *w, *wc;
3432230130Smav	int i, j;
3433230130Smav	nid_t res = 0;
3434230130Smav
3435230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3436230130Smav		return (0);
3437230130Smav	w = hdaa_widget_get(devinfo, nid);
3438230130Smav	if (w == NULL || w->enable == 0)
3439230130Smav		return (0);
3440230130Smav	HDA_BOOTHVERBOSE(
3441230130Smav		device_printf(devinfo->dev,
3442230130Smav		    " %*stracing via nid %d\n",
3443230130Smav			depth + 1, "", w->nid);
3444230130Smav	);
3445230130Smav	/* Use only unused widgets */
3446230130Smav	if (depth > 0 && w->bindas != -1) {
3447230130Smav		if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) {
3448230130Smav			HDA_BOOTHVERBOSE(
3449230130Smav				device_printf(devinfo->dev,
3450230130Smav				    " %*snid %d found output association %d\n",
3451230130Smav					depth + 1, "", w->nid, w->bindas);
3452230130Smav			);
3453230130Smav			if (w->bindas >= 0)
3454230130Smav				w->pflags |= HDAA_ADC_MONITOR;
3455230130Smav			return (1);
3456230130Smav		} else {
3457230130Smav			HDA_BOOTHVERBOSE(
3458230130Smav				device_printf(devinfo->dev,
3459230130Smav				    " %*snid %d busy by input association %d\n",
3460230130Smav					depth + 1, "", w->nid, w->bindas);
3461230130Smav			);
3462230130Smav			return (0);
3463230130Smav		}
3464230130Smav	}
3465230130Smav
3466230130Smav	switch (w->type) {
3467230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3468230130Smav		/* Do not traverse input. AD1988 has digital monitor
3469230130Smav		for which we are not ready. */
3470230130Smav		break;
3471230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3472230130Smav		if (depth > 0)
3473230130Smav			break;
3474230130Smav		/* Fall */
3475230130Smav	default:
3476230130Smav		/* Try to find reachable ADCs with specified nid. */
3477230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3478230130Smav			wc = hdaa_widget_get(devinfo, j);
3479230130Smav			if (wc == NULL || wc->enable == 0)
3480230130Smav				continue;
3481230130Smav			for (i = 0; i < wc->nconns; i++) {
3482230130Smav				if (wc->connsenable[i] == 0)
3483230130Smav					continue;
3484230130Smav				if (wc->conns[i] != nid)
3485230130Smav					continue;
3486230130Smav				if (hdaa_audio_trace_to_out(devinfo,
3487230130Smav				    j, depth + 1) != 0) {
3488230130Smav					res = 1;
3489230130Smav					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3490230130Smav					    wc->selconn == -1)
3491230130Smav						wc->selconn = i;
3492230130Smav				}
3493230130Smav			}
3494230130Smav		}
3495230130Smav		break;
3496230130Smav	}
3497230130Smav	if (res && w->bindas == -1)
3498230130Smav		w->bindas = -2;
3499230130Smav
3500230130Smav	HDA_BOOTHVERBOSE(
3501230130Smav		device_printf(devinfo->dev,
3502230130Smav		    " %*snid %d returned %d\n",
3503230130Smav			depth + 1, "", w->nid, res);
3504230130Smav	);
3505230130Smav	return (res);
3506230130Smav}
3507230130Smav
3508230130Smav/*
3509230130Smav * Trace extra associations (beeper, monitor)
3510230130Smav */
3511230130Smavstatic void
3512230130Smavhdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo)
3513230130Smav{
3514230130Smav	struct hdaa_audio_as *as = devinfo->as;
3515230130Smav	struct hdaa_widget *w;
3516230130Smav	int j;
3517230130Smav
3518230130Smav	/* Input monitor */
3519230130Smav	/* Find mixer associated with input, but supplying signal
3520230130Smav	   for output associations. Hope it will be input monitor. */
3521230130Smav	HDA_BOOTVERBOSE(
3522230130Smav		device_printf(devinfo->dev,
3523230130Smav		    "Tracing input monitor\n");
3524230130Smav	);
3525230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3526230130Smav		w = hdaa_widget_get(devinfo, j);
3527230130Smav		if (w == NULL || w->enable == 0)
3528230130Smav			continue;
3529230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3530230130Smav			continue;
3531230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3532230130Smav			continue;
3533230130Smav		HDA_BOOTVERBOSE(
3534230130Smav			device_printf(devinfo->dev,
3535230130Smav			    " Tracing nid %d to out\n",
3536230130Smav			    j);
3537230130Smav		);
3538230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3539230130Smav			HDA_BOOTVERBOSE(
3540230130Smav				device_printf(devinfo->dev,
3541230130Smav				    " nid %d is input monitor\n",
3542230130Smav					w->nid);
3543230130Smav			);
3544230130Smav			w->ossdev = SOUND_MIXER_IMIX;
3545230130Smav		}
3546230130Smav	}
3547230130Smav
3548230130Smav	/* Other inputs monitor */
3549230130Smav	/* Find input pins supplying signal for output associations.
3550230130Smav	   Hope it will be input monitoring. */
3551230130Smav	HDA_BOOTVERBOSE(
3552230130Smav		device_printf(devinfo->dev,
3553230130Smav		    "Tracing other input monitors\n");
3554230130Smav	);
3555230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3556230130Smav		w = hdaa_widget_get(devinfo, j);
3557230130Smav		if (w == NULL || w->enable == 0)
3558230130Smav			continue;
3559230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3560230130Smav			continue;
3561230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3562230130Smav			continue;
3563230130Smav		HDA_BOOTVERBOSE(
3564230130Smav			device_printf(devinfo->dev,
3565230130Smav			    " Tracing nid %d to out\n",
3566230130Smav			    j);
3567230130Smav		);
3568230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3569230130Smav			HDA_BOOTVERBOSE(
3570230130Smav				device_printf(devinfo->dev,
3571230130Smav				    " nid %d is input monitor\n",
3572230130Smav					w->nid);
3573230130Smav			);
3574230130Smav		}
3575230130Smav	}
3576230130Smav
3577230130Smav	/* Beeper */
3578230130Smav	HDA_BOOTVERBOSE(
3579230130Smav		device_printf(devinfo->dev,
3580230130Smav		    "Tracing beeper\n");
3581230130Smav	);
3582230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3583230130Smav		w = hdaa_widget_get(devinfo, j);
3584230130Smav		if (w == NULL || w->enable == 0)
3585230130Smav			continue;
3586230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
3587230130Smav			continue;
3588230130Smav		HDA_BOOTHVERBOSE(
3589230130Smav			device_printf(devinfo->dev,
3590230130Smav			    " Tracing nid %d to out\n",
3591230130Smav			    j);
3592230130Smav		);
3593230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3594230130Smav			HDA_BOOTVERBOSE(
3595230130Smav				device_printf(devinfo->dev,
3596230130Smav				    " nid %d traced to out\n",
3597230130Smav				    j);
3598230130Smav			);
3599230130Smav		}
3600230130Smav		w->bindas = -2;
3601230130Smav	}
3602230130Smav}
3603230130Smav
3604230130Smav/*
3605230130Smav * Bind assotiations to PCM channels
3606230130Smav */
3607230130Smavstatic void
3608230130Smavhdaa_audio_bind_as(struct hdaa_devinfo *devinfo)
3609230130Smav{
3610230130Smav	struct hdaa_audio_as *as = devinfo->as;
3611230130Smav	int i, j, cnt = 0, free;
3612230130Smav
3613230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
3614230130Smav		if (as[j].enable)
3615230130Smav			cnt += as[j].num_chans;
3616230130Smav	}
3617230130Smav	if (devinfo->num_chans == 0) {
3618230130Smav		devinfo->chans = (struct hdaa_chan *)malloc(
3619230130Smav		    sizeof(struct hdaa_chan) * cnt,
3620230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
3621230130Smav		if (devinfo->chans == NULL) {
3622230130Smav			device_printf(devinfo->dev,
3623230130Smav			    "Channels memory allocation failed!\n");
3624230130Smav			return;
3625230130Smav		}
3626230130Smav	} else {
3627230130Smav		devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans,
3628230130Smav		    sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt),
3629230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
3630230130Smav		if (devinfo->chans == NULL) {
3631230130Smav			devinfo->num_chans = 0;
3632230130Smav			device_printf(devinfo->dev,
3633230130Smav			    "Channels memory allocation failed!\n");
3634230130Smav			return;
3635230130Smav		}
3636230130Smav		/* Fixup relative pointers after realloc */
3637230130Smav		for (j = 0; j < devinfo->num_chans; j++)
3638230130Smav			devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist;
3639230130Smav	}
3640230130Smav	free = devinfo->num_chans;
3641230130Smav	devinfo->num_chans += cnt;
3642230130Smav
3643230130Smav	for (j = free; j < free + cnt; j++) {
3644230130Smav		devinfo->chans[j].devinfo = devinfo;
3645230130Smav		devinfo->chans[j].as = -1;
3646230130Smav	}
3647230130Smav
3648230130Smav	/* Assign associations in order of their numbers, */
3649230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
3650230130Smav		if (as[j].enable == 0)
3651230130Smav			continue;
3652230130Smav		for (i = 0; i < as[j].num_chans; i++) {
3653230130Smav			devinfo->chans[free].as = j;
3654230130Smav			devinfo->chans[free].asindex = i;
3655230130Smav			devinfo->chans[free].dir =
3656230130Smav			    (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
3657230130Smav			hdaa_pcmchannel_setup(&devinfo->chans[free]);
3658230130Smav			as[j].chans[i] = free;
3659230130Smav			free++;
3660230130Smav		}
3661230130Smav	}
3662230130Smav}
3663230130Smav
3664230130Smavstatic void
3665230130Smavhdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo)
3666230130Smav{
3667230130Smav	struct hdaa_widget *w;
3668230130Smav	int i;
3669230130Smav
3670230130Smav	/* Disable power and volume widgets. */
3671230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3672230130Smav		w = hdaa_widget_get(devinfo, i);
3673230130Smav		if (w == NULL || w->enable == 0)
3674230130Smav			continue;
3675230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
3676230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
3677230130Smav			w->enable = 0;
3678230130Smav			HDA_BOOTHVERBOSE(
3679230130Smav				device_printf(devinfo->dev,
3680230130Smav				    " Disabling nid %d due to it's"
3681230130Smav				    " non-audio type.\n",
3682230130Smav				    w->nid);
3683230130Smav			);
3684230130Smav		}
3685230130Smav	}
3686230130Smav}
3687230130Smav
3688230130Smavstatic void
3689230130Smavhdaa_audio_disable_useless(struct hdaa_devinfo *devinfo)
3690230130Smav{
3691230130Smav	struct hdaa_widget *w, *cw;
3692230130Smav	struct hdaa_audio_ctl *ctl;
3693230130Smav	int done, found, i, j, k;
3694230130Smav
3695230130Smav	/* Disable useless pins. */
3696230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3697230130Smav		w = hdaa_widget_get(devinfo, i);
3698230130Smav		if (w == NULL || w->enable == 0)
3699230130Smav			continue;
3700230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
3701230130Smav			if ((w->wclass.pin.config &
3702230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
3703230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
3704230130Smav				w->enable = 0;
3705230130Smav				HDA_BOOTHVERBOSE(
3706230130Smav					device_printf(devinfo->dev,
3707230130Smav					    " Disabling pin nid %d due"
3708230130Smav					    " to None connectivity.\n",
3709230130Smav					    w->nid);
3710230130Smav				);
3711230130Smav			} else if ((w->wclass.pin.config &
3712230130Smav			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
3713230130Smav				w->enable = 0;
3714230130Smav				HDA_BOOTHVERBOSE(
3715230130Smav					device_printf(devinfo->dev,
3716230130Smav					    " Disabling unassociated"
3717230130Smav					    " pin nid %d.\n",
3718230130Smav					    w->nid);
3719230130Smav				);
3720230130Smav			}
3721230130Smav		}
3722230130Smav	}
3723230130Smav	do {
3724230130Smav		done = 1;
3725230130Smav		/* Disable and mute controls for disabled widgets. */
3726230130Smav		i = 0;
3727230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
3728230130Smav			if (ctl->enable == 0)
3729230130Smav				continue;
3730230130Smav			if (ctl->widget->enable == 0 ||
3731230130Smav			    (ctl->childwidget != NULL &&
3732230130Smav			    ctl->childwidget->enable == 0)) {
3733230130Smav				ctl->forcemute = 1;
3734230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
3735230130Smav				ctl->left = 0;
3736230130Smav				ctl->right = 0;
3737230130Smav				ctl->enable = 0;
3738230130Smav				if (ctl->ndir == HDAA_CTL_IN)
3739230130Smav					ctl->widget->connsenable[ctl->index] = 0;
3740230130Smav				done = 0;
3741230130Smav				HDA_BOOTHVERBOSE(
3742230130Smav					device_printf(devinfo->dev,
3743230130Smav					    " Disabling ctl %d nid %d cnid %d due"
3744230130Smav					    " to disabled widget.\n", i,
3745230130Smav					    ctl->widget->nid,
3746230130Smav					    (ctl->childwidget != NULL)?
3747230130Smav					    ctl->childwidget->nid:-1);
3748230130Smav				);
3749230130Smav			}
3750230130Smav		}
3751230130Smav		/* Disable useless widgets. */
3752230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3753230130Smav			w = hdaa_widget_get(devinfo, i);
3754230130Smav			if (w == NULL || w->enable == 0)
3755230130Smav				continue;
3756230130Smav			/* Disable inputs with disabled child widgets. */
3757230130Smav			for (j = 0; j < w->nconns; j++) {
3758230130Smav				if (w->connsenable[j]) {
3759230130Smav					cw = hdaa_widget_get(devinfo, w->conns[j]);
3760230130Smav					if (cw == NULL || cw->enable == 0) {
3761230130Smav						w->connsenable[j] = 0;
3762230130Smav						HDA_BOOTHVERBOSE(
3763230130Smav							device_printf(devinfo->dev,
3764230130Smav							    " Disabling nid %d connection %d due"
3765230130Smav							    " to disabled child widget.\n",
3766230130Smav							    i, j);
3767230130Smav						);
3768230130Smav					}
3769230130Smav				}
3770230130Smav			}
3771230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3772230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3773230130Smav				continue;
3774230130Smav			/* Disable mixers and selectors without inputs. */
3775230130Smav			found = 0;
3776230130Smav			for (j = 0; j < w->nconns; j++) {
3777230130Smav				if (w->connsenable[j]) {
3778230130Smav					found = 1;
3779230130Smav					break;
3780230130Smav				}
3781230130Smav			}
3782230130Smav			if (found == 0) {
3783230130Smav				w->enable = 0;
3784230130Smav				done = 0;
3785230130Smav				HDA_BOOTHVERBOSE(
3786230130Smav					device_printf(devinfo->dev,
3787230130Smav					    " Disabling nid %d due to all it's"
3788230130Smav					    " inputs disabled.\n", w->nid);
3789230130Smav				);
3790230130Smav			}
3791230130Smav			/* Disable nodes without consumers. */
3792230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3793230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3794230130Smav				continue;
3795230130Smav			found = 0;
3796230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
3797230130Smav				cw = hdaa_widget_get(devinfo, k);
3798230130Smav				if (cw == NULL || cw->enable == 0)
3799230130Smav					continue;
3800230130Smav				for (j = 0; j < cw->nconns; j++) {
3801230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
3802230130Smav						found = 1;
3803230130Smav						break;
3804230130Smav					}
3805230130Smav				}
3806230130Smav			}
3807230130Smav			if (found == 0) {
3808230130Smav				w->enable = 0;
3809230130Smav				done = 0;
3810230130Smav				HDA_BOOTHVERBOSE(
3811230130Smav					device_printf(devinfo->dev,
3812230130Smav					    " Disabling nid %d due to all it's"
3813230130Smav					    " consumers disabled.\n", w->nid);
3814230130Smav				);
3815230130Smav			}
3816230130Smav		}
3817230130Smav	} while (done == 0);
3818230130Smav
3819230130Smav}
3820230130Smav
3821230130Smavstatic void
3822230130Smavhdaa_audio_disable_unas(struct hdaa_devinfo *devinfo)
3823230130Smav{
3824230130Smav	struct hdaa_audio_as *as = devinfo->as;
3825230130Smav	struct hdaa_widget *w, *cw;
3826230130Smav	struct hdaa_audio_ctl *ctl;
3827230130Smav	int i, j, k;
3828230130Smav
3829230130Smav	/* Disable unassosiated widgets. */
3830230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3831230130Smav		w = hdaa_widget_get(devinfo, i);
3832230130Smav		if (w == NULL || w->enable == 0)
3833230130Smav			continue;
3834230130Smav		if (w->bindas == -1) {
3835230130Smav			w->enable = 0;
3836230130Smav			HDA_BOOTHVERBOSE(
3837230130Smav				device_printf(devinfo->dev,
3838230130Smav				    " Disabling unassociated nid %d.\n",
3839230130Smav				    w->nid);
3840230130Smav			);
3841230130Smav		}
3842230130Smav	}
3843230130Smav	/* Disable input connections on input pin and
3844230130Smav	 * output on output. */
3845230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3846230130Smav		w = hdaa_widget_get(devinfo, i);
3847230130Smav		if (w == NULL || w->enable == 0)
3848230130Smav			continue;
3849230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3850230130Smav			continue;
3851230130Smav		if (w->bindas < 0)
3852230130Smav			continue;
3853230130Smav		if (as[w->bindas].dir == HDAA_CTL_IN) {
3854230130Smav			for (j = 0; j < w->nconns; j++) {
3855230130Smav				if (w->connsenable[j] == 0)
3856230130Smav					continue;
3857230130Smav				w->connsenable[j] = 0;
3858230130Smav				HDA_BOOTHVERBOSE(
3859230130Smav					device_printf(devinfo->dev,
3860230130Smav					    " Disabling connection to input pin "
3861230130Smav					    "nid %d conn %d.\n",
3862230130Smav					    i, j);
3863230130Smav				);
3864230130Smav			}
3865230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
3866230130Smav			    HDAA_CTL_IN, -1, 1);
3867230130Smav			if (ctl && ctl->enable) {
3868230130Smav				ctl->forcemute = 1;
3869230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
3870230130Smav				ctl->left = 0;
3871230130Smav				ctl->right = 0;
3872230130Smav				ctl->enable = 0;
3873230130Smav			}
3874230130Smav		} else {
3875230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
3876230130Smav			    HDAA_CTL_OUT, -1, 1);
3877230130Smav			if (ctl && ctl->enable) {
3878230130Smav				ctl->forcemute = 1;
3879230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
3880230130Smav				ctl->left = 0;
3881230130Smav				ctl->right = 0;
3882230130Smav				ctl->enable = 0;
3883230130Smav			}
3884230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
3885230130Smav				cw = hdaa_widget_get(devinfo, k);
3886230130Smav				if (cw == NULL || cw->enable == 0)
3887230130Smav					continue;
3888230130Smav				for (j = 0; j < cw->nconns; j++) {
3889230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
3890230130Smav						cw->connsenable[j] = 0;
3891230130Smav						HDA_BOOTHVERBOSE(
3892230130Smav							device_printf(devinfo->dev,
3893230130Smav							    " Disabling connection from output pin "
3894230130Smav							    "nid %d conn %d cnid %d.\n",
3895230130Smav							    k, j, i);
3896230130Smav						);
3897230130Smav						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
3898230130Smav						    cw->nconns > 1)
3899230130Smav							continue;
3900230130Smav						ctl = hdaa_audio_ctl_amp_get(devinfo, k,
3901230130Smav					    HDAA_CTL_IN, j, 1);
3902230130Smav						if (ctl && ctl->enable) {
3903230130Smav							ctl->forcemute = 1;
3904230130Smav							ctl->muted = HDAA_AMP_MUTE_ALL;
3905230130Smav							ctl->left = 0;
3906230130Smav							ctl->right = 0;
3907230130Smav							ctl->enable = 0;
3908230130Smav						}
3909230130Smav					}
3910230130Smav				}
3911230130Smav			}
3912230130Smav		}
3913230130Smav	}
3914230130Smav}
3915230130Smav
3916230130Smavstatic void
3917230130Smavhdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo)
3918230130Smav{
3919230130Smav	struct hdaa_audio_as *as = devinfo->as;
3920230130Smav	struct hdaa_widget *w;
3921230130Smav	int i, j;
3922230130Smav
3923230130Smav	/* On playback path we can safely disable all unseleted inputs. */
3924230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3925230130Smav		w = hdaa_widget_get(devinfo, i);
3926230130Smav		if (w == NULL || w->enable == 0)
3927230130Smav			continue;
3928230130Smav		if (w->nconns <= 1)
3929230130Smav			continue;
3930230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3931230130Smav			continue;
3932230130Smav		if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN)
3933230130Smav			continue;
3934230130Smav		for (j = 0; j < w->nconns; j++) {
3935230130Smav			if (w->connsenable[j] == 0)
3936230130Smav				continue;
3937230130Smav			if (w->selconn < 0 || w->selconn == j)
3938230130Smav				continue;
3939230130Smav			w->connsenable[j] = 0;
3940230130Smav			HDA_BOOTHVERBOSE(
3941230130Smav				device_printf(devinfo->dev,
3942230130Smav				    " Disabling unselected connection "
3943230130Smav				    "nid %d conn %d.\n",
3944230130Smav				    i, j);
3945230130Smav			);
3946230130Smav		}
3947230130Smav	}
3948230130Smav}
3949230130Smav
3950230130Smavstatic void
3951230130Smavhdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo)
3952230130Smav{
3953230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3954230130Smav	struct hdaa_widget *w, *cw;
3955230130Smav	struct hdaa_audio_ctl *ctl;
3956230130Smav	int i, j;
3957230130Smav
3958230130Smav	/* Disable crossassociatement and unwanted crosschannel connections. */
3959230130Smav	/* ... using selectors */
3960230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3961230130Smav		w = hdaa_widget_get(devinfo, i);
3962230130Smav		if (w == NULL || w->enable == 0)
3963230130Smav			continue;
3964230130Smav		if (w->nconns <= 1)
3965230130Smav			continue;
3966230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3967230130Smav			continue;
3968230130Smav		/* Allow any -> mix */
3969230130Smav		if (w->bindas == -2)
3970230130Smav			continue;
3971230130Smav		for (j = 0; j < w->nconns; j++) {
3972230130Smav			if (w->connsenable[j] == 0)
3973230130Smav				continue;
3974230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
3975230130Smav			if (cw == NULL || w->enable == 0)
3976230130Smav				continue;
3977230130Smav			/* Allow mix -> out. */
3978230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
3979230130Smav			    ases[w->bindas].dir == HDAA_CTL_OUT)
3980230130Smav				continue;
3981230130Smav			/* Allow mix -> mixed-in. */
3982230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
3983230130Smav			    ases[w->bindas].mixed)
3984230130Smav				continue;
3985230130Smav			/* Allow in -> mix. */
3986230130Smav			if ((w->pflags & HDAA_ADC_MONITOR) &&
3987230130Smav			     cw->bindas >= 0 &&
3988230130Smav			     ases[cw->bindas].dir == HDAA_CTL_IN)
3989230130Smav				continue;
3990230130Smav			/* Allow if have common as/seqs. */
3991230130Smav			if (w->bindas == cw->bindas &&
3992230130Smav			    (w->bindseqmask & cw->bindseqmask) != 0)
3993230130Smav				continue;
3994230130Smav			w->connsenable[j] = 0;
3995230130Smav			HDA_BOOTHVERBOSE(
3996230130Smav				device_printf(devinfo->dev,
3997230130Smav				    " Disabling crossassociatement connection "
3998230130Smav				    "nid %d conn %d cnid %d.\n",
3999230130Smav				    i, j, cw->nid);
4000230130Smav			);
4001230130Smav		}
4002230130Smav	}
4003230130Smav	/* ... using controls */
4004230130Smav	i = 0;
4005230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4006230130Smav		if (ctl->enable == 0 || ctl->childwidget == NULL)
4007230130Smav			continue;
4008230130Smav		/* Allow any -> mix */
4009230130Smav		if (ctl->widget->bindas == -2)
4010230130Smav			continue;
4011230130Smav		/* Allow mix -> out. */
4012230130Smav		if (ctl->childwidget->bindas == -2 &&
4013230130Smav		    ctl->widget->bindas >= 0 &&
4014230130Smav		    ases[ctl->widget->bindas].dir == HDAA_CTL_OUT)
4015230130Smav			continue;
4016230130Smav		/* Allow mix -> mixed-in. */
4017230130Smav		if (ctl->childwidget->bindas == -2 &&
4018230130Smav		    ctl->widget->bindas >= 0 &&
4019230130Smav		    ases[ctl->widget->bindas].mixed)
4020230130Smav			continue;
4021230130Smav		/* Allow in -> mix. */
4022230130Smav		if ((ctl->widget->pflags & HDAA_ADC_MONITOR) &&
4023230130Smav		    ctl->childwidget->bindas >= 0 &&
4024230130Smav		    ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN)
4025230130Smav			continue;
4026230130Smav		/* Allow if have common as/seqs. */
4027230130Smav		if (ctl->widget->bindas == ctl->childwidget->bindas &&
4028230130Smav		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0)
4029230130Smav			continue;
4030230130Smav		ctl->forcemute = 1;
4031230130Smav		ctl->muted = HDAA_AMP_MUTE_ALL;
4032230130Smav		ctl->left = 0;
4033230130Smav		ctl->right = 0;
4034230130Smav		ctl->enable = 0;
4035230130Smav		if (ctl->ndir == HDAA_CTL_IN)
4036230130Smav			ctl->widget->connsenable[ctl->index] = 0;
4037230130Smav		HDA_BOOTHVERBOSE(
4038230130Smav			device_printf(devinfo->dev,
4039230130Smav			    " Disabling crossassociatement connection "
4040230130Smav			    "ctl %d nid %d cnid %d.\n", i,
4041230130Smav			    ctl->widget->nid,
4042230130Smav			    ctl->childwidget->nid);
4043230130Smav		);
4044230130Smav	}
4045230130Smav
4046230130Smav}
4047230130Smav
4048230130Smav/*
4049230451Smav * Find controls to control amplification for source and calculate possible
4050230451Smav * amplification range.
4051230130Smav */
4052230130Smavstatic int
4053230130Smavhdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4054230451Smav    int ossdev, int ctlable, int depth, int *minamp, int *maxamp)
4055230130Smav{
4056230130Smav	struct hdaa_widget *w, *wc;
4057230130Smav	struct hdaa_audio_ctl *ctl;
4058230451Smav	int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4059230130Smav
4060230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4061230451Smav		return (found);
4062230130Smav
4063230130Smav	w = hdaa_widget_get(devinfo, nid);
4064230130Smav	if (w == NULL || w->enable == 0)
4065230451Smav		return (found);
4066230130Smav
4067230130Smav	/* Count number of active inputs. */
4068230130Smav	if (depth > 0) {
4069230130Smav		for (j = 0; j < w->nconns; j++) {
4070230451Smav			if (!w->connsenable[j])
4071230451Smav				continue;
4072230451Smav			conns++;
4073230130Smav		}
4074230130Smav	}
4075230130Smav
4076230130Smav	/* If this is not a first step - use input mixer.
4077230130Smav	   Pins have common input ctl so care must be taken. */
4078230130Smav	if (depth > 0 && ctlable && (conns == 1 ||
4079230130Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
4080230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
4081230130Smav		    index, 1);
4082230130Smav		if (ctl) {
4083230451Smav			ctl->ossmask |= (1 << ossdev);
4084230451Smav			found++;
4085230451Smav			if (*minamp == *maxamp) {
4086230451Smav				*minamp += MINQDB(ctl);
4087230451Smav				*maxamp += MAXQDB(ctl);
4088230451Smav			}
4089230130Smav		}
4090230130Smav	}
4091230130Smav
4092230130Smav	/* If widget has own ossdev - not traverse it.
4093230130Smav	   It will be traversed on it's own. */
4094230130Smav	if (w->ossdev >= 0 && depth > 0)
4095230451Smav		return (found);
4096230130Smav
4097230130Smav	/* We must not traverse pin */
4098230130Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4099230130Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
4100230130Smav	    depth > 0)
4101230451Smav		return (found);
4102230130Smav
4103230130Smav	/* record that this widget exports such signal, */
4104230130Smav	w->ossmask |= (1 << ossdev);
4105230130Smav
4106230451Smav	/*
4107230451Smav	 * If signals mixed, we can't assign controls farther.
4108230130Smav	 * Ignore this on depth zero. Caller must knows why.
4109230130Smav	 */
4110230451Smav	if (conns > 1 &&
4111230451Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4112230130Smav		ctlable = 0;
4113230130Smav
4114230130Smav	if (ctlable) {
4115230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
4116230130Smav		if (ctl) {
4117230451Smav			ctl->ossmask |= (1 << ossdev);
4118230451Smav			found++;
4119230451Smav			if (*minamp == *maxamp) {
4120230451Smav				*minamp += MINQDB(ctl);
4121230451Smav				*maxamp += MAXQDB(ctl);
4122230451Smav			}
4123230130Smav		}
4124230130Smav	}
4125230130Smav
4126230451Smav	cminamp = cmaxamp = 0;
4127230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4128230130Smav		wc = hdaa_widget_get(devinfo, i);
4129230130Smav		if (wc == NULL || wc->enable == 0)
4130230130Smav			continue;
4131230130Smav		for (j = 0; j < wc->nconns; j++) {
4132230130Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
4133230451Smav				tminamp = tmaxamp = 0;
4134230451Smav				found += hdaa_audio_ctl_source_amp(devinfo,
4135230451Smav				    wc->nid, j, ossdev, ctlable, depth + 1,
4136230451Smav				    &tminamp, &tmaxamp);
4137230451Smav				if (cminamp == 0 && cmaxamp == 0) {
4138230451Smav					cminamp = tminamp;
4139230451Smav					cmaxamp = tmaxamp;
4140230451Smav				} else if (tminamp != tmaxamp) {
4141230451Smav					cminamp = imax(cminamp, tminamp);
4142230451Smav					cmaxamp = imin(cmaxamp, tmaxamp);
4143230451Smav				}
4144230130Smav			}
4145230130Smav		}
4146230130Smav	}
4147230451Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4148230451Smav		*minamp += cminamp;
4149230451Smav		*maxamp += cmaxamp;
4150230451Smav	}
4151230451Smav	return (found);
4152230130Smav}
4153230130Smav
4154230130Smav/*
4155230451Smav * Find controls to control amplification for destination and calculate
4156230451Smav * possible amplification range.
4157230130Smav */
4158230451Smavstatic int
4159230130Smavhdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4160230451Smav    int ossdev, int depth, int *minamp, int *maxamp)
4161230130Smav{
4162230130Smav	struct hdaa_audio_as *as = devinfo->as;
4163230130Smav	struct hdaa_widget *w, *wc;
4164230130Smav	struct hdaa_audio_ctl *ctl;
4165230451Smav	int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4166230130Smav
4167230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4168230451Smav		return (found);
4169230130Smav
4170230130Smav	w = hdaa_widget_get(devinfo, nid);
4171230130Smav	if (w == NULL || w->enable == 0)
4172230451Smav		return (found);
4173230130Smav
4174230130Smav	if (depth > 0) {
4175230130Smav		/* If this node produce output for several consumers,
4176230130Smav		   we can't touch it. */
4177230130Smav		consumers = 0;
4178230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4179230130Smav			wc = hdaa_widget_get(devinfo, i);
4180230130Smav			if (wc == NULL || wc->enable == 0)
4181230130Smav				continue;
4182230130Smav			for (j = 0; j < wc->nconns; j++) {
4183230130Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
4184230130Smav					consumers++;
4185230130Smav			}
4186230130Smav		}
4187230130Smav		/* The only exception is if real HP redirection is configured
4188230130Smav		   and this is a duplication point.
4189230130Smav		   XXX: Actually exception is not completely correct.
4190230130Smav		   XXX: Duplication point check is not perfect. */
4191230130Smav		if ((consumers == 2 && (w->bindas < 0 ||
4192230130Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
4193230130Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
4194230130Smav		    consumers > 2)
4195230451Smav			return (found);
4196230130Smav
4197230130Smav		/* Else use it's output mixer. */
4198230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4199230130Smav		    HDAA_CTL_OUT, -1, 1);
4200230130Smav		if (ctl) {
4201230451Smav			ctl->ossmask |= (1 << ossdev);
4202230451Smav			found++;
4203230451Smav			if (*minamp == *maxamp) {
4204230451Smav				*minamp += MINQDB(ctl);
4205230451Smav				*maxamp += MAXQDB(ctl);
4206230451Smav			}
4207230130Smav		}
4208230130Smav	}
4209230130Smav
4210230130Smav	/* We must not traverse pin */
4211230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4212230130Smav	    depth > 0)
4213230451Smav		return (found);
4214230130Smav
4215230451Smav	cminamp = cmaxamp = 0;
4216230130Smav	for (i = 0; i < w->nconns; i++) {
4217230130Smav		if (w->connsenable[i] == 0)
4218230130Smav			continue;
4219230130Smav		if (index >= 0 && i != index)
4220230130Smav			continue;
4221230451Smav		tminamp = tmaxamp = 0;
4222230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4223230130Smav		    HDAA_CTL_IN, i, 1);
4224230130Smav		if (ctl) {
4225230451Smav			ctl->ossmask |= (1 << ossdev);
4226230451Smav			found++;
4227230451Smav			if (*minamp == *maxamp) {
4228230451Smav				tminamp += MINQDB(ctl);
4229230451Smav				tmaxamp += MAXQDB(ctl);
4230230451Smav			}
4231230130Smav		}
4232230451Smav		found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
4233230451Smav		    depth + 1, &tminamp, &tmaxamp);
4234230451Smav		if (cminamp == 0 && cmaxamp == 0) {
4235230451Smav			cminamp = tminamp;
4236230451Smav			cmaxamp = tmaxamp;
4237230451Smav		} else if (tminamp != tmaxamp) {
4238230451Smav			cminamp = imax(cminamp, tminamp);
4239230451Smav			cmaxamp = imin(cmaxamp, tmaxamp);
4240230451Smav		}
4241230130Smav	}
4242230451Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4243230451Smav		*minamp += cminamp;
4244230451Smav		*maxamp += cmaxamp;
4245230451Smav	}
4246230451Smav	return (found);
4247230130Smav}
4248230130Smav
4249230130Smav/*
4250230130Smav * Assign OSS names to sound sources
4251230130Smav */
4252230130Smavstatic void
4253230130Smavhdaa_audio_assign_names(struct hdaa_devinfo *devinfo)
4254230130Smav{
4255230130Smav	struct hdaa_audio_as *as = devinfo->as;
4256230130Smav	struct hdaa_widget *w;
4257230130Smav	int i, j;
4258230130Smav	int type = -1, use, used = 0;
4259230130Smav	static const int types[7][13] = {
4260230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4261230130Smav	      SOUND_MIXER_LINE3, -1 },	/* line */
4262230130Smav	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
4263230130Smav	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
4264230130Smav	    { SOUND_MIXER_CD, -1 },	/* cd */
4265230130Smav	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
4266230130Smav	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
4267230130Smav	      -1 },	/* digital */
4268230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4269230130Smav	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
4270230130Smav	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
4271230130Smav	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
4272230130Smav	      -1 }	/* others */
4273230130Smav	};
4274230130Smav
4275230130Smav	/* Surely known names */
4276230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4277230130Smav		w = hdaa_widget_get(devinfo, i);
4278230130Smav		if (w == NULL || w->enable == 0)
4279230130Smav			continue;
4280230130Smav		if (w->bindas == -1)
4281230130Smav			continue;
4282230130Smav		use = -1;
4283230130Smav		switch (w->type) {
4284230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4285230130Smav			if (as[w->bindas].dir == HDAA_CTL_OUT)
4286230130Smav				break;
4287230130Smav			type = -1;
4288230130Smav			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4289230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4290230130Smav				type = 0;
4291230130Smav				break;
4292230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4293230130Smav				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
4294230130Smav				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4295230130Smav					break;
4296230130Smav				type = 1;
4297230130Smav				break;
4298230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4299230130Smav				type = 3;
4300230130Smav				break;
4301230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4302230130Smav				type = 4;
4303230130Smav				break;
4304230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
4305230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
4306230130Smav				type = 5;
4307230130Smav				break;
4308230130Smav			}
4309230130Smav			if (type == -1)
4310230130Smav				break;
4311230130Smav			j = 0;
4312230130Smav			while (types[type][j] >= 0 &&
4313230130Smav			    (used & (1 << types[type][j])) != 0) {
4314230130Smav				j++;
4315230130Smav			}
4316230130Smav			if (types[type][j] >= 0)
4317230130Smav				use = types[type][j];
4318230130Smav			break;
4319230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4320230130Smav			use = SOUND_MIXER_PCM;
4321230130Smav			break;
4322230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
4323230130Smav			use = SOUND_MIXER_SPEAKER;
4324230130Smav			break;
4325230130Smav		default:
4326230130Smav			break;
4327230130Smav		}
4328230130Smav		if (use >= 0) {
4329230130Smav			w->ossdev = use;
4330230130Smav			used |= (1 << use);
4331230130Smav		}
4332230130Smav	}
4333230130Smav	/* Semi-known names */
4334230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4335230130Smav		w = hdaa_widget_get(devinfo, i);
4336230130Smav		if (w == NULL || w->enable == 0)
4337230130Smav			continue;
4338230130Smav		if (w->ossdev >= 0)
4339230130Smav			continue;
4340230130Smav		if (w->bindas == -1)
4341230130Smav			continue;
4342230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4343230130Smav			continue;
4344230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4345230130Smav			continue;
4346230130Smav		type = -1;
4347230130Smav		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4348230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
4349230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4350230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
4351230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
4352230130Smav			type = 0;
4353230130Smav			break;
4354230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4355230130Smav			type = 2;
4356230130Smav			break;
4357230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
4358230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
4359230130Smav			type = 5;
4360230130Smav			break;
4361230130Smav		}
4362230130Smav		if (type == -1)
4363230130Smav			break;
4364230130Smav		j = 0;
4365230130Smav		while (types[type][j] >= 0 &&
4366230130Smav		    (used & (1 << types[type][j])) != 0) {
4367230130Smav			j++;
4368230130Smav		}
4369230130Smav		if (types[type][j] >= 0) {
4370230130Smav			w->ossdev = types[type][j];
4371230130Smav			used |= (1 << types[type][j]);
4372230130Smav		}
4373230130Smav	}
4374230130Smav	/* Others */
4375230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4376230130Smav		w = hdaa_widget_get(devinfo, i);
4377230130Smav		if (w == NULL || w->enable == 0)
4378230130Smav			continue;
4379230130Smav		if (w->ossdev >= 0)
4380230130Smav			continue;
4381230130Smav		if (w->bindas == -1)
4382230130Smav			continue;
4383230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4384230130Smav			continue;
4385230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4386230130Smav			continue;
4387230130Smav		j = 0;
4388230130Smav		while (types[6][j] >= 0 &&
4389230130Smav		    (used & (1 << types[6][j])) != 0) {
4390230130Smav			j++;
4391230130Smav		}
4392230130Smav		if (types[6][j] >= 0) {
4393230130Smav			w->ossdev = types[6][j];
4394230130Smav			used |= (1 << types[6][j]);
4395230130Smav		}
4396230130Smav	}
4397230130Smav}
4398230130Smav
4399230130Smavstatic void
4400230130Smavhdaa_audio_build_tree(struct hdaa_devinfo *devinfo)
4401230130Smav{
4402230130Smav	struct hdaa_audio_as *as = devinfo->as;
4403230130Smav	int j, res;
4404230130Smav
4405230130Smav	/* Trace all associations in order of their numbers. */
4406230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4407230130Smav		if (as[j].enable == 0)
4408230130Smav			continue;
4409230130Smav		HDA_BOOTVERBOSE(
4410230130Smav			device_printf(devinfo->dev,
4411230130Smav			    "Tracing association %d (%d)\n", j, as[j].index);
4412230130Smav		);
4413230130Smav		if (as[j].dir == HDAA_CTL_OUT) {
4414230130Smavretry:
4415230130Smav			res = hdaa_audio_trace_as_out(devinfo, j, 0);
4416230130Smav			if (res == 0 && as[j].hpredir >= 0 &&
4417230130Smav			    as[j].fakeredir == 0) {
4418230130Smav				/* If CODEC can't do analog HP redirection
4419230130Smav				   try to make it using one more DAC. */
4420230130Smav				as[j].fakeredir = 1;
4421230130Smav				goto retry;
4422230130Smav			}
4423230130Smav		} else if (as[j].mixed)
4424230130Smav			res = hdaa_audio_trace_as_in(devinfo, j);
4425230130Smav		else
4426230130Smav			res = hdaa_audio_trace_as_in_mch(devinfo, j, 0);
4427230130Smav		if (res) {
4428230130Smav			HDA_BOOTVERBOSE(
4429230130Smav				device_printf(devinfo->dev,
4430230130Smav				    "Association %d (%d) trace succeeded\n",
4431230130Smav				    j, as[j].index);
4432230130Smav			);
4433230130Smav		} else {
4434230130Smav			HDA_BOOTVERBOSE(
4435230130Smav				device_printf(devinfo->dev,
4436230130Smav				    "Association %d (%d) trace failed\n",
4437230130Smav				    j, as[j].index);
4438230130Smav			);
4439230130Smav			as[j].enable = 0;
4440230130Smav		}
4441230130Smav	}
4442230130Smav
4443230130Smav	/* Look for additional DACs/ADCs. */
4444230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4445230130Smav		if (as[j].enable == 0)
4446230130Smav			continue;
4447230130Smav		hdaa_audio_adddac(devinfo, j);
4448230130Smav	}
4449230130Smav
4450230130Smav	/* Trace mixer and beeper pseudo associations. */
4451230130Smav	hdaa_audio_trace_as_extra(devinfo);
4452230130Smav}
4453230130Smav
4454230451Smav/*
4455230451Smav * Store in pdevinfo new data about whether and how we can control signal
4456230451Smav * for OSS device to/from specified widget.
4457230451Smav */
4458230130Smavstatic void
4459230451Smavhdaa_adjust_amp(struct hdaa_widget *w, int ossdev,
4460230451Smav    int found, int minamp, int maxamp)
4461230451Smav{
4462230451Smav	struct hdaa_devinfo *devinfo = w->devinfo;
4463230451Smav	struct hdaa_pcm_devinfo *pdevinfo;
4464230451Smav
4465230451Smav	if (w->bindas >= 0)
4466230451Smav		pdevinfo = devinfo->as[w->bindas].pdevinfo;
4467230451Smav	else
4468230451Smav		pdevinfo = &devinfo->devs[0];
4469230451Smav	if (found)
4470230451Smav		pdevinfo->ossmask |= (1 << ossdev);
4471230451Smav	if (minamp == 0 && maxamp == 0)
4472230451Smav		return;
4473230451Smav	if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) {
4474230451Smav		pdevinfo->minamp[ossdev] = minamp;
4475230451Smav		pdevinfo->maxamp[ossdev] = maxamp;
4476230451Smav	} else {
4477230451Smav		pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp);
4478230451Smav		pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp);
4479230451Smav	}
4480230451Smav}
4481230451Smav
4482230451Smav/*
4483230451Smav * Trace signals from/to all possible sources/destionstions to find possible
4484230451Smav * recording sources, OSS device control ranges and to assign controls.
4485230451Smav */
4486230451Smavstatic void
4487230130Smavhdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo)
4488230130Smav{
4489230130Smav	struct hdaa_audio_as *as = devinfo->as;
4490230130Smav	struct hdaa_widget *w, *cw;
4491230451Smav	int i, j, minamp, maxamp, found;
4492230130Smav
4493230130Smav	/* Assign mixers to the tree. */
4494230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4495230130Smav		w = hdaa_widget_get(devinfo, i);
4496230130Smav		if (w == NULL || w->enable == 0)
4497230130Smav			continue;
4498230451Smav		minamp = maxamp = 0;
4499230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
4500230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
4501230130Smav		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4502230130Smav		    as[w->bindas].dir == HDAA_CTL_IN)) {
4503230130Smav			if (w->ossdev < 0)
4504230130Smav				continue;
4505230451Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4506230451Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4507230451Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4508230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
4509230451Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4510230451Smav			    SOUND_MIXER_RECLEV, 0, &minamp, &maxamp);
4511230451Smav			hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp);
4512230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4513230130Smav		    as[w->bindas].dir == HDAA_CTL_OUT) {
4514230451Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4515230451Smav			    SOUND_MIXER_VOLUME, 0, &minamp, &maxamp);
4516230451Smav			hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp);
4517230130Smav		}
4518230130Smav		if (w->ossdev == SOUND_MIXER_IMIX) {
4519230451Smav			minamp = maxamp = 0;
4520230451Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4521230451Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4522230451Smav			if (minamp == maxamp) {
4523230130Smav				/* If we are unable to control input monitor
4524230130Smav				   as source - try to control it as destination. */
4525230451Smav				found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4526230451Smav				    w->ossdev, 0, &minamp, &maxamp);
4527230451Smav				w->pflags |= HDAA_IMIX_AS_DST;
4528230130Smav			}
4529230451Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4530230130Smav		}
4531230130Smav		if (w->pflags & HDAA_ADC_MONITOR) {
4532230130Smav			for (j = 0; j < w->nconns; j++) {
4533230130Smav				if (!w->connsenable[j])
4534230130Smav				    continue;
4535230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
4536230130Smav				if (cw == NULL || cw->enable == 0)
4537230130Smav				    continue;
4538230130Smav				if (cw->bindas == -1)
4539230130Smav				    continue;
4540230130Smav				if (cw->bindas >= 0 &&
4541230130Smav				    as[cw->bindas].dir != HDAA_CTL_IN)
4542230130Smav					continue;
4543230451Smav				minamp = maxamp = 0;
4544230451Smav				found = hdaa_audio_ctl_dest_amp(devinfo,
4545230451Smav				    w->nid, j, SOUND_MIXER_IGAIN, 0,
4546230451Smav				    &minamp, &maxamp);
4547230451Smav				hdaa_adjust_amp(w, SOUND_MIXER_IGAIN,
4548230451Smav				    found, minamp, maxamp);
4549230130Smav			}
4550230130Smav		}
4551230130Smav	}
4552230130Smav}
4553230130Smav
4554230130Smavstatic void
4555230130Smavhdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo)
4556230130Smav{
4557230130Smav	struct hdaa_audio_as *as = devinfo->as;
4558230130Smav	struct hdaa_widget *w;
4559230130Smav	uint32_t pincap;
4560230130Smav	int i;
4561230130Smav
4562230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
4563230130Smav		w = &devinfo->widget[i];
4564230130Smav		if (w == NULL)
4565230130Smav			continue;
4566230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4567230130Smav		    w->waspin == 0)
4568230130Smav			continue;
4569230130Smav
4570230130Smav		pincap = w->wclass.pin.cap;
4571230130Smav
4572230130Smav		/* Disable everything. */
4573230130Smav		w->wclass.pin.ctrl &= ~(
4574230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
4575230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
4576230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
4577230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
4578230130Smav
4579230130Smav		if (w->enable == 0) {
4580230130Smav			/* Pin is unused so left it disabled. */
4581230130Smav			continue;
4582230130Smav		} else if (w->waspin) {
4583230130Smav			/* Enable input for beeper input. */
4584230130Smav			w->wclass.pin.ctrl |=
4585230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
4586230130Smav		} else if (w->bindas < 0 || as[w->bindas].enable == 0) {
4587230130Smav			/* Pin is unused so left it disabled. */
4588230130Smav			continue;
4589230130Smav		} else if (as[w->bindas].dir == HDAA_CTL_IN) {
4590230130Smav			/* Input pin, configure for input. */
4591230130Smav			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
4592230130Smav				w->wclass.pin.ctrl |=
4593230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
4594230130Smav
4595230130Smav			if ((devinfo->quirks & HDAA_QUIRK_IVREF100) &&
4596230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4597230130Smav				w->wclass.pin.ctrl |=
4598230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4599230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4600230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) &&
4601230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4602230130Smav				w->wclass.pin.ctrl |=
4603230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4604230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4605230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) &&
4606230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4607230130Smav				w->wclass.pin.ctrl |=
4608230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4609230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4610230130Smav		} else {
4611230130Smav			/* Output pin, configure for output. */
4612230130Smav			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
4613230130Smav				w->wclass.pin.ctrl |=
4614230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
4615230130Smav
4616230130Smav			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
4617230130Smav			    (w->wclass.pin.config &
4618230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
4619230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
4620230130Smav				w->wclass.pin.ctrl |=
4621230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
4622230130Smav
4623230130Smav			if ((devinfo->quirks & HDAA_QUIRK_OVREF100) &&
4624230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4625230130Smav				w->wclass.pin.ctrl |=
4626230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4627230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4628230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) &&
4629230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4630230130Smav				w->wclass.pin.ctrl |=
4631230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4632230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4633230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) &&
4634230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4635230130Smav				w->wclass.pin.ctrl |=
4636230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4637230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4638230130Smav		}
4639230130Smav	}
4640230130Smav}
4641230130Smav
4642230130Smavstatic void
4643230130Smavhdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo)
4644230130Smav{
4645230130Smav	struct hdaa_audio_ctl *ctl;
4646230130Smav	int i, z;
4647230130Smav
4648230130Smav	i = 0;
4649230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4650230130Smav		if (ctl->enable == 0 || ctl->ossmask != 0) {
4651230130Smav			/* Mute disabled and mixer controllable controls.
4652230130Smav			 * Last will be initialized by mixer_init().
4653230130Smav			 * This expected to reduce click on startup. */
4654230130Smav			hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0);
4655230130Smav			continue;
4656230130Smav		}
4657230130Smav		/* Init fixed controls to 0dB amplification. */
4658230130Smav		z = ctl->offset;
4659230130Smav		if (z > ctl->step)
4660230130Smav			z = ctl->step;
4661230130Smav		hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z);
4662230130Smav	}
4663230130Smav}
4664230130Smav
4665230130Smavstatic void
4666230130Smavhdaa_gpio_commit(struct hdaa_devinfo *devinfo)
4667230130Smav{
4668230130Smav	uint32_t gdata, gmask, gdir;
4669230130Smav	int i, numgpio;
4670230130Smav
4671230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
4672230130Smav	if (devinfo->gpio != 0 && numgpio != 0) {
4673230130Smav		gdata = hda_command(devinfo->dev,
4674230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
4675230130Smav		gmask = hda_command(devinfo->dev,
4676230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
4677230130Smav		gdir = hda_command(devinfo->dev,
4678230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
4679230130Smav		for (i = 0; i < numgpio; i++) {
4680230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4681230130Smav			    HDAA_GPIO_SET(i)) {
4682230130Smav				gdata |= (1 << i);
4683230130Smav				gmask |= (1 << i);
4684230130Smav				gdir |= (1 << i);
4685230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4686230130Smav			    HDAA_GPIO_CLEAR(i)) {
4687230130Smav				gdata &= ~(1 << i);
4688230130Smav				gmask |= (1 << i);
4689230130Smav				gdir |= (1 << i);
4690230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4691230130Smav			    HDAA_GPIO_DISABLE(i)) {
4692230130Smav				gmask &= ~(1 << i);
4693230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4694230130Smav			    HDAA_GPIO_INPUT(i)) {
4695230130Smav				gmask |= (1 << i);
4696230130Smav				gdir &= ~(1 << i);
4697230130Smav			}
4698230130Smav		}
4699230130Smav		HDA_BOOTVERBOSE(
4700230130Smav			device_printf(devinfo->dev, "GPIO commit\n");
4701230130Smav		);
4702230130Smav		hda_command(devinfo->dev,
4703230130Smav		    HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask));
4704230130Smav		hda_command(devinfo->dev,
4705230130Smav		    HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir));
4706230130Smav		hda_command(devinfo->dev,
4707230130Smav		    HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata));
4708230130Smav		HDA_BOOTVERBOSE(
4709230130Smav			hdaa_dump_gpio(devinfo);
4710230130Smav		);
4711230130Smav	}
4712230130Smav}
4713230130Smav
4714230130Smavstatic void
4715230130Smavhdaa_gpo_commit(struct hdaa_devinfo *devinfo)
4716230130Smav{
4717230130Smav	uint32_t gdata;
4718230130Smav	int i, numgpo;
4719230130Smav
4720230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
4721230130Smav	if (devinfo->gpo != 0 && numgpo != 0) {
4722230130Smav		gdata = hda_command(devinfo->dev,
4723230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
4724230130Smav		for (i = 0; i < numgpo; i++) {
4725230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4726230130Smav			    HDAA_GPIO_SET(i)) {
4727230130Smav				gdata |= (1 << i);
4728230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4729230130Smav			    HDAA_GPIO_CLEAR(i)) {
4730230130Smav				gdata &= ~(1 << i);
4731230130Smav			}
4732230130Smav		}
4733230130Smav		HDA_BOOTVERBOSE(
4734230130Smav			device_printf(devinfo->dev, "GPO commit\n");
4735230130Smav		);
4736230130Smav		hda_command(devinfo->dev,
4737230130Smav		    HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata));
4738230130Smav		HDA_BOOTVERBOSE(
4739230130Smav			hdaa_dump_gpo(devinfo);
4740230130Smav		);
4741230130Smav	}
4742230130Smav}
4743230130Smav
4744230130Smavstatic void
4745230130Smavhdaa_audio_commit(struct hdaa_devinfo *devinfo)
4746230130Smav{
4747230130Smav	struct hdaa_widget *w;
4748230130Smav	int i;
4749230130Smav
4750230130Smav	/* Commit controls. */
4751230130Smav	hdaa_audio_ctl_commit(devinfo);
4752230130Smav
4753230130Smav	/* Commit selectors, pins and EAPD. */
4754230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
4755230130Smav		w = &devinfo->widget[i];
4756230130Smav		if (w == NULL)
4757230130Smav			continue;
4758230130Smav		if (w->selconn == -1)
4759230130Smav			w->selconn = 0;
4760230130Smav		if (w->nconns > 0)
4761230130Smav			hdaa_widget_connection_select(w, w->selconn);
4762230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
4763230130Smav		    w->waspin) {
4764230130Smav			hda_command(devinfo->dev,
4765230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid,
4766230130Smav			    w->wclass.pin.ctrl));
4767230130Smav		}
4768230130Smav		if (w->param.eapdbtl != HDA_INVALID) {
4769230130Smav			uint32_t val;
4770230130Smav
4771230130Smav			val = w->param.eapdbtl;
4772230130Smav			if (devinfo->quirks &
4773230130Smav			    HDAA_QUIRK_EAPDINV)
4774230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
4775230130Smav			hda_command(devinfo->dev,
4776230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid,
4777230130Smav			    val));
4778230130Smav		}
4779230130Smav	}
4780230130Smav
4781230130Smav	hdaa_gpio_commit(devinfo);
4782230130Smav	hdaa_gpo_commit(devinfo);
4783230130Smav}
4784230130Smav
4785230130Smavstatic void
4786230130Smavhdaa_powerup(struct hdaa_devinfo *devinfo)
4787230130Smav{
4788230130Smav	int i;
4789230130Smav
4790230130Smav	hda_command(devinfo->dev,
4791230130Smav	    HDA_CMD_SET_POWER_STATE(0,
4792230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D0));
4793230130Smav	DELAY(100);
4794230130Smav
4795230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4796230130Smav		hda_command(devinfo->dev,
4797230130Smav		    HDA_CMD_SET_POWER_STATE(0,
4798230130Smav		    i, HDA_CMD_POWER_STATE_D0));
4799230130Smav	}
4800230130Smav	DELAY(1000);
4801230130Smav}
4802230130Smav
4803230130Smavstatic int
4804230130Smavhdaa_pcmchannel_setup(struct hdaa_chan *ch)
4805230130Smav{
4806230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
4807230130Smav	struct hdaa_audio_as *as = devinfo->as;
4808230130Smav	struct hdaa_widget *w;
4809230130Smav	uint32_t cap, fmtcap, pcmcap;
4810230130Smav	int i, j, ret, channels, onlystereo;
4811230130Smav	uint16_t pinset;
4812230130Smav
4813230130Smav	ch->caps = hdaa_caps;
4814230130Smav	ch->caps.fmtlist = ch->fmtlist;
4815230130Smav	ch->bit16 = 1;
4816230130Smav	ch->bit32 = 0;
4817230130Smav	ch->pcmrates[0] = 48000;
4818230130Smav	ch->pcmrates[1] = 0;
4819230326Smav	ch->stripecap = 0xff;
4820230130Smav
4821230130Smav	ret = 0;
4822230130Smav	channels = 0;
4823230130Smav	onlystereo = 1;
4824230130Smav	pinset = 0;
4825230130Smav	fmtcap = devinfo->supp_stream_formats;
4826230130Smav	pcmcap = devinfo->supp_pcm_size_rate;
4827230130Smav
4828230130Smav	for (i = 0; i < 16; i++) {
4829230130Smav		/* Check as is correct */
4830230130Smav		if (ch->as < 0)
4831230130Smav			break;
4832230130Smav		/* Cound only present DACs */
4833230130Smav		if (as[ch->as].dacs[ch->asindex][i] <= 0)
4834230130Smav			continue;
4835230130Smav		/* Ignore duplicates */
4836230130Smav		for (j = 0; j < ret; j++) {
4837230130Smav			if (ch->io[j] == as[ch->as].dacs[ch->asindex][i])
4838230130Smav				break;
4839230130Smav		}
4840230130Smav		if (j < ret)
4841230130Smav			continue;
4842230130Smav
4843230130Smav		w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]);
4844230130Smav		if (w == NULL || w->enable == 0)
4845230130Smav			continue;
4846230130Smav		cap = w->param.supp_stream_formats;
4847230130Smav		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
4848230130Smav		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
4849230130Smav			continue;
4850230130Smav		/* Many CODECs does not declare AC3 support on SPDIF.
4851230130Smav		   I don't beleave that they doesn't support it! */
4852230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
4853230130Smav			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
4854230130Smav		if (ret == 0) {
4855230130Smav			fmtcap = cap;
4856230130Smav			pcmcap = w->param.supp_pcm_size_rate;
4857230130Smav		} else {
4858230130Smav			fmtcap &= cap;
4859230130Smav			pcmcap &= w->param.supp_pcm_size_rate;
4860230130Smav		}
4861230130Smav		ch->io[ret++] = as[ch->as].dacs[ch->asindex][i];
4862230326Smav		ch->stripecap &= w->wclass.conv.stripecap;
4863230130Smav		/* Do not count redirection pin/dac channels. */
4864230130Smav		if (i == 15 && as[ch->as].hpredir >= 0)
4865230130Smav			continue;
4866230130Smav		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
4867230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
4868230130Smav			onlystereo = 0;
4869230130Smav		pinset |= (1 << i);
4870230130Smav	}
4871230130Smav	ch->io[ret] = -1;
4872230130Smav	ch->channels = channels;
4873230130Smav
4874230130Smav	if (as[ch->as].fakeredir)
4875230130Smav		ret--;
4876230130Smav	/* Standard speaks only about stereo pins and playback, ... */
4877230130Smav	if ((!onlystereo) || as[ch->as].mixed)
4878230130Smav		pinset = 0;
4879230130Smav	/* ..., but there it gives us info about speakers layout. */
4880230130Smav	as[ch->as].pinset = pinset;
4881230130Smav
4882230130Smav	ch->supp_stream_formats = fmtcap;
4883230130Smav	ch->supp_pcm_size_rate = pcmcap;
4884230130Smav
4885230130Smav	/*
4886230130Smav	 *  8bit = 0
4887230130Smav	 * 16bit = 1
4888230130Smav	 * 20bit = 2
4889230130Smav	 * 24bit = 3
4890230130Smav	 * 32bit = 4
4891230130Smav	 */
4892230130Smav	if (ret > 0) {
4893230130Smav		i = 0;
4894230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
4895230130Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
4896230130Smav				ch->bit16 = 1;
4897230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
4898230130Smav				ch->bit16 = 0;
4899230488Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
4900230130Smav				ch->bit32 = 3;
4901230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
4902230130Smav				ch->bit32 = 2;
4903230488Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
4904230488Smav				ch->bit32 = 4;
4905230130Smav			if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) {
4906230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
4907230130Smav				if (ch->bit32)
4908230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
4909230130Smav			}
4910230130Smav			if (channels >= 2) {
4911230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
4912230130Smav				if (ch->bit32)
4913230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
4914230130Smav			}
4915230312Smav			if (channels >= 3 && !onlystereo) {
4916230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0);
4917230312Smav				if (ch->bit32)
4918230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0);
4919230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1);
4920230312Smav				if (ch->bit32)
4921230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1);
4922230312Smav			}
4923230312Smav			if (channels >= 4) {
4924230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
4925230130Smav				if (ch->bit32)
4926230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
4927230312Smav				if (!onlystereo) {
4928230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1);
4929230312Smav					if (ch->bit32)
4930230312Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1);
4931230312Smav				}
4932230130Smav			}
4933230312Smav			if (channels >= 5 && !onlystereo) {
4934230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0);
4935230312Smav				if (ch->bit32)
4936230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0);
4937230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1);
4938230312Smav				if (ch->bit32)
4939230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1);
4940230312Smav			}
4941230312Smav			if (channels >= 6) {
4942230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
4943230130Smav				if (ch->bit32)
4944230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
4945230312Smav				if (!onlystereo) {
4946230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0);
4947230312Smav					if (ch->bit32)
4948230312Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0);
4949230312Smav				}
4950230130Smav			}
4951230312Smav			if (channels >= 7 && !onlystereo) {
4952230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0);
4953230312Smav				if (ch->bit32)
4954230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0);
4955230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1);
4956230312Smav				if (ch->bit32)
4957230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1);
4958230312Smav			}
4959230312Smav			if (channels >= 8) {
4960230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
4961230130Smav				if (ch->bit32)
4962230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
4963230130Smav			}
4964230130Smav		}
4965230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
4966230130Smav			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
4967230130Smav		}
4968230130Smav		ch->fmtlist[i] = 0;
4969230130Smav		i = 0;
4970230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
4971230130Smav			ch->pcmrates[i++] = 8000;
4972230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
4973230130Smav			ch->pcmrates[i++] = 11025;
4974230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
4975230130Smav			ch->pcmrates[i++] = 16000;
4976230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
4977230130Smav			ch->pcmrates[i++] = 22050;
4978230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
4979230130Smav			ch->pcmrates[i++] = 32000;
4980230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
4981230130Smav			ch->pcmrates[i++] = 44100;
4982230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
4983230130Smav		ch->pcmrates[i++] = 48000;
4984230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
4985230130Smav			ch->pcmrates[i++] = 88200;
4986230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
4987230130Smav			ch->pcmrates[i++] = 96000;
4988230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
4989230130Smav			ch->pcmrates[i++] = 176400;
4990230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
4991230130Smav			ch->pcmrates[i++] = 192000;
4992230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
4993230130Smav		ch->pcmrates[i] = 0;
4994230130Smav		if (i > 0) {
4995230130Smav			ch->caps.minspeed = ch->pcmrates[0];
4996230130Smav			ch->caps.maxspeed = ch->pcmrates[i - 1];
4997230130Smav		}
4998230130Smav	}
4999230130Smav
5000230130Smav	return (ret);
5001230130Smav}
5002230130Smav
5003230130Smavstatic void
5004230451Smavhdaa_prepare_pcms(struct hdaa_devinfo *devinfo)
5005230130Smav{
5006230130Smav	struct hdaa_audio_as *as = devinfo->as;
5007230130Smav	int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
5008230130Smav
5009230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5010230130Smav		if (as[i].enable == 0)
5011230130Smav			continue;
5012230130Smav		if (as[i].dir == HDAA_CTL_IN) {
5013230130Smav			if (as[i].digital)
5014230130Smav				drdev++;
5015230130Smav			else
5016230130Smav				ardev++;
5017230130Smav		} else {
5018230130Smav			if (as[i].digital)
5019230130Smav				dpdev++;
5020230130Smav			else
5021230130Smav				apdev++;
5022230130Smav		}
5023230130Smav	}
5024230130Smav	devinfo->num_devs =
5025230130Smav	    max(ardev, apdev) + max(drdev, dpdev);
5026230130Smav	devinfo->devs =
5027230130Smav	    (struct hdaa_pcm_devinfo *)malloc(
5028230130Smav	    devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo),
5029230130Smav	    M_HDAA, M_ZERO | M_NOWAIT);
5030230130Smav	if (devinfo->devs == NULL) {
5031230130Smav		device_printf(devinfo->dev,
5032230130Smav		    "Unable to allocate memory for devices\n");
5033230130Smav		return;
5034230130Smav	}
5035230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5036230130Smav		devinfo->devs[i].index = i;
5037230130Smav		devinfo->devs[i].devinfo = devinfo;
5038230130Smav		devinfo->devs[i].playas = -1;
5039230130Smav		devinfo->devs[i].recas = -1;
5040230130Smav		devinfo->devs[i].digital = 255;
5041230130Smav	}
5042230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5043230130Smav		if (as[i].enable == 0)
5044230130Smav			continue;
5045230130Smav		for (j = 0; j < devinfo->num_devs; j++) {
5046230130Smav			if (devinfo->devs[j].digital != 255 &&
5047230130Smav			    (!devinfo->devs[j].digital) !=
5048230130Smav			    (!as[i].digital))
5049230130Smav				continue;
5050230130Smav			if (as[i].dir == HDAA_CTL_IN) {
5051230130Smav				if (devinfo->devs[j].recas >= 0)
5052230130Smav					continue;
5053230130Smav				devinfo->devs[j].recas = i;
5054230130Smav			} else {
5055230130Smav				if (devinfo->devs[j].playas >= 0)
5056230130Smav					continue;
5057230130Smav				devinfo->devs[j].playas = i;
5058230130Smav			}
5059230451Smav			as[i].pdevinfo = &devinfo->devs[j];
5060230130Smav			for (k = 0; k < as[i].num_chans; k++) {
5061230130Smav				devinfo->chans[as[i].chans[k]].pdevinfo =
5062230130Smav				    &devinfo->devs[j];
5063230130Smav			}
5064230130Smav			devinfo->devs[j].digital = as[i].digital;
5065230130Smav			break;
5066230130Smav		}
5067230130Smav	}
5068230451Smav}
5069230451Smav
5070230451Smavstatic void
5071230451Smavhdaa_create_pcms(struct hdaa_devinfo *devinfo)
5072230451Smav{
5073230451Smav	int i;
5074230451Smav
5075230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5076230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
5077230130Smav
5078230130Smav		pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1);
5079230130Smav		device_set_ivars(pdevinfo->dev, (void *)pdevinfo);
5080230130Smav	}
5081230130Smav}
5082230130Smav
5083230130Smavstatic void
5084230130Smavhdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
5085230130Smav{
5086230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5087230130Smav	struct hdaa_audio_ctl *ctl;
5088230130Smav	char buf[64];
5089230130Smav	int i, j, printed;
5090230130Smav
5091230130Smav	if (flag == 0) {
5092230130Smav		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5093230130Smav		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5094230130Smav		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
5095230130Smav		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
5096230130Smav	}
5097230130Smav
5098230130Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
5099230130Smav		if ((flag & (1 << j)) == 0)
5100230130Smav			continue;
5101230130Smav		i = 0;
5102230130Smav		printed = 0;
5103230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5104230130Smav			if (ctl->enable == 0 ||
5105230130Smav			    ctl->widget->enable == 0)
5106230130Smav				continue;
5107230130Smav			if (!((pdevinfo->playas >= 0 &&
5108230130Smav			    ctl->widget->bindas == pdevinfo->playas) ||
5109230130Smav			    (pdevinfo->recas >= 0 &&
5110230130Smav			    ctl->widget->bindas == pdevinfo->recas) ||
5111230130Smav			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
5112230130Smav				continue;
5113230130Smav			if ((ctl->ossmask & (1 << j)) == 0)
5114230130Smav				continue;
5115230130Smav
5116230130Smav			if (printed == 0) {
5117230130Smav				device_printf(pdevinfo->dev, "\n");
5118230130Smav				if (banner != NULL) {
5119230130Smav					device_printf(pdevinfo->dev, "%s", banner);
5120230130Smav				} else {
5121230130Smav					device_printf(pdevinfo->dev, "Unknown Ctl");
5122230130Smav				}
5123230451Smav				printf(" (OSS: %s)",
5124230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(1 << j,
5125230130Smav				    buf, sizeof(buf)));
5126230451Smav				if (pdevinfo->ossmask & (1 << j)) {
5127230451Smav					printf(": %+d/%+ddB\n",
5128230451Smav					    pdevinfo->minamp[j] / 4,
5129230451Smav					    pdevinfo->maxamp[j] / 4);
5130230451Smav				} else
5131230451Smav					printf("\n");
5132230130Smav				device_printf(pdevinfo->dev, "   |\n");
5133230130Smav				printed = 1;
5134230130Smav			}
5135230130Smav			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
5136230130Smav				ctl->widget->nid,
5137230130Smav				(ctl->ndir == HDAA_CTL_IN)?"in ":"out");
5138230130Smav			if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir)
5139230130Smav				printf(" %2d): ", ctl->index);
5140230130Smav			else
5141230130Smav				printf("):    ");
5142230130Smav			if (ctl->step > 0) {
5143230130Smav				printf("%+d/%+ddB (%d steps)%s\n",
5144230451Smav				    MINQDB(ctl) / 4,
5145230451Smav				    MAXQDB(ctl) / 4,
5146230130Smav				    ctl->step + 1,
5147230130Smav				    ctl->mute?" + mute":"");
5148230130Smav			} else
5149230130Smav				printf("%s\n", ctl->mute?"mute":"");
5150230130Smav		}
5151230130Smav	}
5152230130Smav}
5153230130Smav
5154230130Smavstatic void
5155230130Smavhdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
5156230130Smav{
5157230130Smav	uint32_t cap;
5158230130Smav
5159230130Smav	cap = fcap;
5160230130Smav	if (cap != 0) {
5161230130Smav		device_printf(dev, "     Stream cap: 0x%08x\n", cap);
5162230130Smav		device_printf(dev, "                ");
5163230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5164230130Smav			printf(" AC3");
5165230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5166230130Smav			printf(" FLOAT32");
5167230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5168230130Smav			printf(" PCM");
5169230130Smav		printf("\n");
5170230130Smav	}
5171230130Smav	cap = pcmcap;
5172230130Smav	if (cap != 0) {
5173230130Smav		device_printf(dev, "        PCM cap: 0x%08x\n", cap);
5174230130Smav		device_printf(dev, "                ");
5175230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5176230130Smav			printf(" 8");
5177230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5178230130Smav			printf(" 16");
5179230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5180230130Smav			printf(" 20");
5181230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5182230130Smav			printf(" 24");
5183230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5184230130Smav			printf(" 32");
5185230130Smav		printf(" bits,");
5186230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5187230130Smav			printf(" 8");
5188230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5189230130Smav			printf(" 11");
5190230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5191230130Smav			printf(" 16");
5192230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5193230130Smav			printf(" 22");
5194230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5195230130Smav			printf(" 32");
5196230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5197230130Smav			printf(" 44");
5198230130Smav		printf(" 48");
5199230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5200230130Smav			printf(" 88");
5201230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5202230130Smav			printf(" 96");
5203230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5204230130Smav			printf(" 176");
5205230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5206230130Smav			printf(" 192");
5207230130Smav		printf(" KHz\n");
5208230130Smav	}
5209230130Smav}
5210230130Smav
5211230130Smavstatic void
5212230130Smavhdaa_dump_pin(struct hdaa_widget *w)
5213230130Smav{
5214230130Smav	uint32_t pincap;
5215230130Smav
5216230130Smav	pincap = w->wclass.pin.cap;
5217230130Smav
5218230130Smav	device_printf(w->devinfo->dev, "        Pin cap: 0x%08x\n", pincap);
5219230130Smav	device_printf(w->devinfo->dev, "                ");
5220230130Smav	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5221230130Smav		printf(" ISC");
5222230130Smav	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5223230130Smav		printf(" TRQD");
5224230130Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5225230130Smav		printf(" PDC");
5226230130Smav	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5227230130Smav		printf(" HP");
5228230130Smav	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5229230130Smav		printf(" OUT");
5230230130Smav	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5231230130Smav		printf(" IN");
5232230130Smav	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5233230130Smav		printf(" BAL");
5234230130Smav	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
5235230130Smav		printf(" HDMI");
5236230130Smav	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5237230130Smav		printf(" VREF[");
5238230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5239230130Smav			printf(" 50");
5240230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5241230130Smav			printf(" 80");
5242230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5243230130Smav			printf(" 100");
5244230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5245230130Smav			printf(" GROUND");
5246230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5247230130Smav			printf(" HIZ");
5248230130Smav		printf(" ]");
5249230130Smav	}
5250230130Smav	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5251230130Smav		printf(" EAPD");
5252230130Smav	if (HDA_PARAM_PIN_CAP_DP(pincap))
5253230130Smav		printf(" DP");
5254230130Smav	if (HDA_PARAM_PIN_CAP_HBR(pincap))
5255230130Smav		printf(" HBR");
5256230130Smav	printf("\n");
5257230130Smav	device_printf(w->devinfo->dev, "     Pin config: 0x%08x\n",
5258230130Smav	    w->wclass.pin.config);
5259230130Smav	device_printf(w->devinfo->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
5260230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5261230130Smav		printf(" HP");
5262230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5263230130Smav		printf(" IN");
5264230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5265230130Smav		printf(" OUT");
5266230312Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
5267230312Smav		if ((w->wclass.pin.ctrl &
5268230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
5269230312Smav			printf(" HBR");
5270230312Smav		else if ((w->wclass.pin.ctrl &
5271230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5272230312Smav			printf(" EPTs");
5273230312Smav	} else {
5274230312Smav		if ((w->wclass.pin.ctrl &
5275230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5276230312Smav			printf(" VREFs");
5277230312Smav	}
5278230130Smav	printf("\n");
5279230130Smav}
5280230130Smav
5281230130Smavstatic void
5282230130Smavhdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf)
5283230130Smav{
5284230130Smav
5285230130Smav	device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d "
5286230130Smav	    "%-13s %-5s %-7s %-10s %-7s %d%s\n",
5287230130Smav	    w->nid, conf,
5288230130Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
5289230130Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
5290230130Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
5291230130Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
5292230130Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
5293230130Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
5294230130Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
5295230130Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
5296230130Smav	    (w->enable == 0)?" DISA":"");
5297230130Smav}
5298230130Smav
5299230130Smavstatic void
5300230130Smavhdaa_dump_pin_configs(struct hdaa_devinfo *devinfo)
5301230130Smav{
5302230130Smav	struct hdaa_widget *w;
5303230130Smav	int i;
5304230130Smav
5305230130Smav	device_printf(devinfo->dev, "nid   0x    as seq "
5306230130Smav	    "device       conn  jack    loc        color   misc\n");
5307230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5308230130Smav		w = hdaa_widget_get(devinfo, i);
5309230130Smav		if (w == NULL)
5310230130Smav			continue;
5311230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5312230130Smav			continue;
5313230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
5314230130Smav	}
5315230130Smav}
5316230130Smav
5317230130Smavstatic void
5318230130Smavhdaa_dump_amp(device_t dev, uint32_t cap, char *banner)
5319230130Smav{
5320230130Smav	device_printf(dev, "     %s amp: 0x%08x\n", banner, cap);
5321230130Smav	device_printf(dev, "                 "
5322230130Smav	    "mute=%d step=%d size=%d offset=%d\n",
5323230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5324230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap),
5325230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap),
5326230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap));
5327230130Smav}
5328230130Smav
5329230130Smavstatic void
5330230130Smavhdaa_dump_nodes(struct hdaa_devinfo *devinfo)
5331230130Smav{
5332230130Smav	struct hdaa_widget *w, *cw;
5333230130Smav	char buf[64];
5334230130Smav	int i, j;
5335230130Smav
5336230130Smav	device_printf(devinfo->dev, "\n");
5337230130Smav	device_printf(devinfo->dev, "Default Parameter\n");
5338230130Smav	device_printf(devinfo->dev, "-----------------\n");
5339230130Smav	hdaa_dump_audio_formats(devinfo->dev,
5340230130Smav	    devinfo->supp_stream_formats,
5341230130Smav	    devinfo->supp_pcm_size_rate);
5342230130Smav	device_printf(devinfo->dev, "         IN amp: 0x%08x\n",
5343230130Smav	    devinfo->inamp_cap);
5344230130Smav	device_printf(devinfo->dev, "        OUT amp: 0x%08x\n",
5345230130Smav	    devinfo->outamp_cap);
5346230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5347230130Smav		w = hdaa_widget_get(devinfo, i);
5348230130Smav		if (w == NULL) {
5349230130Smav			device_printf(devinfo->dev, "Ghost widget nid=%d\n", i);
5350230130Smav			continue;
5351230130Smav		}
5352230130Smav		device_printf(devinfo->dev, "\n");
5353230130Smav		device_printf(devinfo->dev, "            nid: %d%s\n", w->nid,
5354230130Smav		    (w->enable == 0) ? " [DISABLED]" : "");
5355230130Smav		device_printf(devinfo->dev, "           Name: %s\n", w->name);
5356230130Smav		device_printf(devinfo->dev, "     Widget cap: 0x%08x\n",
5357230130Smav		    w->param.widget_cap);
5358230130Smav		if (w->param.widget_cap & 0x0ee1) {
5359230130Smav			device_printf(devinfo->dev, "                ");
5360230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
5361230130Smav			    printf(" LRSWAP");
5362230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
5363230130Smav			    printf(" PWR");
5364230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5365230130Smav			    printf(" DIGITAL");
5366230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
5367230130Smav			    printf(" UNSOL");
5368230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
5369230130Smav			    printf(" PROC");
5370230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
5371230326Smav			    printf(" STRIPE(x%d)",
5372230326Smav				1 << (fls(w->wclass.conv.stripecap) - 1));
5373230130Smav			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
5374230130Smav			if (j == 1)
5375230130Smav			    printf(" STEREO");
5376230130Smav			else if (j > 1)
5377230130Smav			    printf(" %dCH", j + 1);
5378230130Smav			printf("\n");
5379230130Smav		}
5380230130Smav		if (w->bindas != -1) {
5381230130Smav			device_printf(devinfo->dev, "    Association: %d (0x%08x)\n",
5382230130Smav			    w->bindas, w->bindseqmask);
5383230130Smav		}
5384230130Smav		if (w->ossmask != 0 || w->ossdev >= 0) {
5385230130Smav			device_printf(devinfo->dev, "            OSS: %s",
5386230130Smav			    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
5387230130Smav			if (w->ossdev >= 0)
5388230451Smav			    printf(" (%s)", ossnames[w->ossdev]);
5389230130Smav			printf("\n");
5390230130Smav		}
5391230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5392230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5393230130Smav			hdaa_dump_audio_formats(devinfo->dev,
5394230130Smav			    w->param.supp_stream_formats,
5395230130Smav			    w->param.supp_pcm_size_rate);
5396230130Smav		} else if (w->type ==
5397230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
5398230130Smav			hdaa_dump_pin(w);
5399230130Smav		if (w->param.eapdbtl != HDA_INVALID)
5400230130Smav			device_printf(devinfo->dev, "           EAPD: 0x%08x\n",
5401230130Smav			    w->param.eapdbtl);
5402230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5403230130Smav		    w->param.outamp_cap != 0)
5404230130Smav			hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output");
5405230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5406230130Smav		    w->param.inamp_cap != 0)
5407230130Smav			hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input");
5408230130Smav		if (w->nconns > 0) {
5409230130Smav			device_printf(devinfo->dev, "    connections: %d\n", w->nconns);
5410230130Smav			device_printf(devinfo->dev, "          |\n");
5411230130Smav		}
5412230130Smav		for (j = 0; j < w->nconns; j++) {
5413230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
5414230130Smav			device_printf(devinfo->dev, "          + %s<- nid=%d [%s]",
5415230130Smav			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
5416230130Smav			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5417230130Smav			if (cw == NULL)
5418230130Smav				printf(" [UNKNOWN]");
5419230130Smav			else if (cw->enable == 0)
5420230130Smav				printf(" [DISABLED]");
5421230130Smav			if (w->nconns > 1 && w->selconn == j && w->type !=
5422230130Smav			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5423230130Smav				printf(" (selected)");
5424230130Smav			printf("\n");
5425230130Smav		}
5426230130Smav	}
5427230130Smav
5428230130Smav}
5429230130Smav
5430230130Smavstatic void
5431230130Smavhdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth)
5432230130Smav{
5433230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5434230130Smav	struct hdaa_widget *w, *cw;
5435230130Smav	char buf[64];
5436230130Smav	int i, printed = 0;
5437230130Smav
5438230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
5439230130Smav		return;
5440230130Smav
5441230130Smav	w = hdaa_widget_get(devinfo, nid);
5442230130Smav	if (w == NULL || w->enable == 0)
5443230130Smav		return;
5444230130Smav
5445230130Smav	if (depth == 0)
5446230130Smav		device_printf(pdevinfo->dev, "%*s", 4, "");
5447230130Smav	else
5448230130Smav		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
5449230130Smav	printf("nid=%d [%s]", w->nid, w->name);
5450230130Smav
5451230130Smav	if (depth > 0) {
5452230130Smav		if (w->ossmask == 0) {
5453230130Smav			printf("\n");
5454230130Smav			return;
5455230130Smav		}
5456230130Smav		printf(" [src: %s]",
5457230130Smav		    hdaa_audio_ctl_ossmixer_mask2allname(
5458230130Smav			w->ossmask, buf, sizeof(buf)));
5459230130Smav		if (w->ossdev >= 0) {
5460230130Smav			printf("\n");
5461230130Smav			return;
5462230130Smav		}
5463230130Smav	}
5464230130Smav	printf("\n");
5465230130Smav
5466230130Smav	for (i = 0; i < w->nconns; i++) {
5467230130Smav		if (w->connsenable[i] == 0)
5468230130Smav			continue;
5469230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
5470230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
5471230130Smav			continue;
5472230130Smav		if (printed == 0) {
5473230130Smav			device_printf(pdevinfo->dev, "%*s  |\n", 4 + (depth) * 7, "");
5474230130Smav			printed = 1;
5475230130Smav		}
5476230130Smav		hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
5477230130Smav	}
5478230130Smav
5479230130Smav}
5480230130Smav
5481230130Smavstatic void
5482230130Smavhdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo)
5483230130Smav{
5484230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5485230130Smav	struct hdaa_audio_as *as;
5486230130Smav	struct hdaa_widget *w;
5487230130Smav	int i, printed = 0;
5488230130Smav
5489230130Smav	if (pdevinfo->playas < 0)
5490230130Smav		return;
5491230130Smav
5492230130Smav	as = &devinfo->as[pdevinfo->playas];
5493230130Smav	for (i = 0; i < 16; i++) {
5494230130Smav		if (as->pins[i] <= 0)
5495230130Smav			continue;
5496230130Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
5497230130Smav		if (w == NULL || w->enable == 0)
5498230130Smav			continue;
5499230130Smav		if (printed == 0) {
5500230130Smav			printed = 1;
5501230130Smav			device_printf(pdevinfo->dev, "\n");
5502230130Smav			device_printf(pdevinfo->dev, "Playback:\n");
5503230130Smav		}
5504230130Smav		device_printf(pdevinfo->dev, "\n");
5505230130Smav		hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0);
5506230130Smav	}
5507230130Smav}
5508230130Smav
5509230130Smavstatic void
5510230130Smavhdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo)
5511230130Smav{
5512230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5513230130Smav	struct hdaa_widget *w;
5514230130Smav	int i;
5515230130Smav	int printed = 0;
5516230130Smav
5517230130Smav	if (pdevinfo->recas < 0)
5518230130Smav		return;
5519230130Smav
5520230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5521230130Smav		w = hdaa_widget_get(devinfo, i);
5522230130Smav		if (w == NULL || w->enable == 0)
5523230130Smav			continue;
5524230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
5525230130Smav			continue;
5526230130Smav		if (w->bindas != pdevinfo->recas)
5527230130Smav			continue;
5528230130Smav		if (printed == 0) {
5529230130Smav			printed = 1;
5530230130Smav			device_printf(pdevinfo->dev, "\n");
5531230130Smav			device_printf(pdevinfo->dev, "Record:\n");
5532230130Smav		}
5533230130Smav		device_printf(pdevinfo->dev, "\n");
5534230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
5535230130Smav	}
5536230130Smav}
5537230130Smav
5538230130Smavstatic void
5539230130Smavhdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo)
5540230130Smav{
5541230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5542230130Smav	struct hdaa_widget *w;
5543230130Smav	int i;
5544230130Smav	int printed = 0;
5545230130Smav
5546230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5547230130Smav		w = hdaa_widget_get(devinfo, i);
5548230130Smav		if (w == NULL || w->enable == 0)
5549230130Smav			continue;
5550230130Smav		if (w->ossdev != SOUND_MIXER_IMIX)
5551230130Smav			continue;
5552230451Smav		if (w->bindas != pdevinfo->recas)
5553230451Smav			continue;
5554230130Smav		if (printed == 0) {
5555230130Smav			printed = 1;
5556230130Smav			device_printf(pdevinfo->dev, "\n");
5557230130Smav			device_printf(pdevinfo->dev, "Input Mix:\n");
5558230130Smav		}
5559230130Smav		device_printf(pdevinfo->dev, "\n");
5560230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
5561230130Smav	}
5562230130Smav}
5563230130Smav
5564230130Smavstatic void
5565230130Smavhdaa_dump_pcmchannels(struct hdaa_pcm_devinfo *pdevinfo)
5566230130Smav{
5567230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5568230130Smav	nid_t *nids;
5569230130Smav	int chid, i;
5570230130Smav
5571230130Smav	if (pdevinfo->playas >= 0) {
5572230130Smav		device_printf(pdevinfo->dev, "\n");
5573230130Smav		device_printf(pdevinfo->dev, "Playback:\n");
5574230130Smav		device_printf(pdevinfo->dev, "\n");
5575230130Smav		chid = devinfo->as[pdevinfo->playas].chans[0];
5576230130Smav		hdaa_dump_audio_formats(pdevinfo->dev,
5577230130Smav		    devinfo->chans[chid].supp_stream_formats,
5578230130Smav		    devinfo->chans[chid].supp_pcm_size_rate);
5579230130Smav		for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) {
5580230130Smav			chid = devinfo->as[pdevinfo->playas].chans[i];
5581230130Smav			device_printf(pdevinfo->dev, "            DAC:");
5582230130Smav			for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5583230130Smav				printf(" %d", *nids);
5584230130Smav			printf("\n");
5585230130Smav		}
5586230130Smav	}
5587230130Smav	if (pdevinfo->recas >= 0) {
5588230130Smav		device_printf(pdevinfo->dev, "\n");
5589230130Smav		device_printf(pdevinfo->dev, "Record:\n");
5590230130Smav		device_printf(pdevinfo->dev, "\n");
5591230130Smav		chid = devinfo->as[pdevinfo->recas].chans[0];
5592230130Smav		hdaa_dump_audio_formats(pdevinfo->dev,
5593230130Smav		    devinfo->chans[chid].supp_stream_formats,
5594230130Smav		    devinfo->chans[chid].supp_pcm_size_rate);
5595230130Smav		for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) {
5596230130Smav			chid = devinfo->as[pdevinfo->recas].chans[i];
5597230130Smav			device_printf(pdevinfo->dev, "            DAC:");
5598230130Smav			for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5599230130Smav				printf(" %d", *nids);
5600230130Smav			printf("\n");
5601230130Smav		}
5602230130Smav	}
5603230130Smav}
5604230130Smav
5605230130Smavstatic void
5606230130Smavhdaa_pindump(device_t dev)
5607230130Smav{
5608230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5609230130Smav	struct hdaa_widget *w;
5610230130Smav	uint32_t res, pincap, delay;
5611230130Smav	int i;
5612230130Smav
5613230130Smav	device_printf(dev, "Dumping AFG pins:\n");
5614230130Smav	device_printf(dev, "nid   0x    as seq "
5615230130Smav	    "device       conn  jack    loc        color   misc\n");
5616230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5617230130Smav		w = hdaa_widget_get(devinfo, i);
5618230130Smav		if (w == NULL || w->type !=
5619230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5620230130Smav			continue;
5621230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
5622230130Smav		pincap = w->wclass.pin.cap;
5623230130Smav		device_printf(dev, "    Caps: %2s %3s %2s %4s %4s",
5624230130Smav		    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
5625230130Smav		    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
5626230130Smav		    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
5627230130Smav		    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
5628230130Smav		    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
5629230130Smav		if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
5630230130Smav		    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
5631230130Smav			if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
5632230130Smav				delay = 0;
5633230130Smav				hda_command(dev,
5634230130Smav				    HDA_CMD_SET_PIN_SENSE(0, w->nid, 0));
5635230130Smav				do {
5636230130Smav					res = hda_command(dev,
5637230130Smav					    HDA_CMD_GET_PIN_SENSE(0, w->nid));
5638230130Smav					if (res != 0x7fffffff && res != 0xffffffff)
5639230130Smav						break;
5640230130Smav					DELAY(10);
5641230130Smav				} while (++delay < 10000);
5642230130Smav			} else {
5643230130Smav				delay = 0;
5644230130Smav				res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0,
5645230130Smav				    w->nid));
5646230130Smav			}
5647230312Smav			printf(" Sense: 0x%08x (%sconnected%s)", res,
5648230130Smav			    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ?
5649230312Smav			     "" : "dis",
5650230312Smav			    (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) &&
5651230312Smav			     (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ?
5652230312Smav			      ", ELD valid" : "");
5653230130Smav			if (delay > 0)
5654230130Smav				printf(" delay %dus", delay * 10);
5655230130Smav		}
5656230130Smav		printf("\n");
5657230130Smav	}
5658230130Smav	device_printf(dev,
5659230130Smav	    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
5660230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
5661230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
5662230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
5663230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
5664230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
5665230130Smav	hdaa_dump_gpi(devinfo);
5666230130Smav	hdaa_dump_gpio(devinfo);
5667230130Smav	hdaa_dump_gpo(devinfo);
5668230130Smav}
5669230130Smav
5670230130Smavstatic void
5671230130Smavhdaa_configure(device_t dev)
5672230130Smav{
5673230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5674230130Smav	struct hdaa_audio_ctl *ctl;
5675230130Smav	int i;
5676230130Smav
5677230130Smav	HDA_BOOTHVERBOSE(
5678230130Smav		device_printf(dev, "Applying built-in patches...\n");
5679230130Smav	);
5680230130Smav	hdaa_patch(devinfo);
5681230130Smav	HDA_BOOTHVERBOSE(
5682230130Smav		device_printf(dev, "Applying local patches...\n");
5683230130Smav	);
5684230130Smav	hdaa_local_patch(devinfo);
5685230130Smav	hdaa_audio_postprocess(devinfo);
5686230130Smav	HDA_BOOTHVERBOSE(
5687230130Smav		device_printf(dev, "Parsing Ctls...\n");
5688230130Smav	);
5689230130Smav	hdaa_audio_ctl_parse(devinfo);
5690230130Smav	HDA_BOOTHVERBOSE(
5691230130Smav		device_printf(dev, "Disabling nonaudio...\n");
5692230130Smav	);
5693230130Smav	hdaa_audio_disable_nonaudio(devinfo);
5694230130Smav	HDA_BOOTHVERBOSE(
5695230130Smav		device_printf(dev, "Disabling useless...\n");
5696230130Smav	);
5697230130Smav	hdaa_audio_disable_useless(devinfo);
5698230130Smav	HDA_BOOTVERBOSE(
5699230130Smav		device_printf(dev, "Patched pins configuration:\n");
5700230130Smav		hdaa_dump_pin_configs(devinfo);
5701230130Smav	);
5702230130Smav	HDA_BOOTHVERBOSE(
5703230130Smav		device_printf(dev, "Parsing pin associations...\n");
5704230130Smav	);
5705230130Smav	hdaa_audio_as_parse(devinfo);
5706230130Smav	HDA_BOOTHVERBOSE(
5707230130Smav		device_printf(dev, "Building AFG tree...\n");
5708230130Smav	);
5709230130Smav	hdaa_audio_build_tree(devinfo);
5710230130Smav	HDA_BOOTHVERBOSE(
5711230130Smav		device_printf(dev, "Disabling unassociated "
5712230130Smav		    "widgets...\n");
5713230130Smav	);
5714230130Smav	hdaa_audio_disable_unas(devinfo);
5715230130Smav	HDA_BOOTHVERBOSE(
5716230130Smav		device_printf(dev, "Disabling nonselected "
5717230130Smav		    "inputs...\n");
5718230130Smav	);
5719230130Smav	hdaa_audio_disable_notselected(devinfo);
5720230130Smav	HDA_BOOTHVERBOSE(
5721230130Smav		device_printf(dev, "Disabling useless...\n");
5722230130Smav	);
5723230130Smav	hdaa_audio_disable_useless(devinfo);
5724230130Smav	HDA_BOOTHVERBOSE(
5725230130Smav		device_printf(dev, "Disabling "
5726230130Smav		    "crossassociatement connections...\n");
5727230130Smav	);
5728230130Smav	hdaa_audio_disable_crossas(devinfo);
5729230130Smav	HDA_BOOTHVERBOSE(
5730230130Smav		device_printf(dev, "Disabling useless...\n");
5731230130Smav	);
5732230130Smav	hdaa_audio_disable_useless(devinfo);
5733230130Smav	HDA_BOOTHVERBOSE(
5734230130Smav		device_printf(dev, "Binding associations to channels...\n");
5735230130Smav	);
5736230130Smav	hdaa_audio_bind_as(devinfo);
5737230130Smav	HDA_BOOTHVERBOSE(
5738230130Smav		device_printf(dev, "Assigning names to signal sources...\n");
5739230130Smav	);
5740230130Smav	hdaa_audio_assign_names(devinfo);
5741230130Smav	HDA_BOOTHVERBOSE(
5742230451Smav		device_printf(dev, "Preparing PCM devices...\n");
5743230451Smav	);
5744230451Smav	hdaa_prepare_pcms(devinfo);
5745230451Smav	HDA_BOOTHVERBOSE(
5746230130Smav		device_printf(dev, "Assigning mixers to the tree...\n");
5747230130Smav	);
5748230130Smav	hdaa_audio_assign_mixers(devinfo);
5749230130Smav	HDA_BOOTHVERBOSE(
5750230130Smav		device_printf(dev, "Preparing pin controls...\n");
5751230130Smav	);
5752230130Smav	hdaa_audio_prepare_pin_ctrl(devinfo);
5753230130Smav	HDA_BOOTHVERBOSE(
5754230130Smav		device_printf(dev, "AFG commit...\n");
5755230130Smav	);
5756230130Smav	hdaa_audio_commit(devinfo);
5757230130Smav	HDA_BOOTHVERBOSE(
5758230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
5759230130Smav	);
5760230130Smav	hdaa_patch_direct(devinfo);
5761230130Smav	HDA_BOOTHVERBOSE(
5762230312Smav		device_printf(dev, "ELD init...\n");
5763230312Smav	);
5764230312Smav	hdaa_eld_init(devinfo);
5765230312Smav	HDA_BOOTHVERBOSE(
5766230130Smav		device_printf(dev, "HP switch init...\n");
5767230130Smav	);
5768230130Smav	hdaa_hp_switch_init(devinfo);
5769230130Smav	HDA_BOOTHVERBOSE(
5770230130Smav		device_printf(dev, "Creating PCM devices...\n");
5771230130Smav	);
5772230130Smav	hdaa_create_pcms(devinfo);
5773230130Smav
5774230130Smav	HDA_BOOTVERBOSE(
5775230130Smav		if (devinfo->quirks != 0) {
5776230130Smav			device_printf(dev, "FG config/quirks:");
5777230130Smav			for (i = 0; i < HDAA_QUIRKS_TAB_LEN; i++) {
5778230130Smav				if ((devinfo->quirks &
5779230130Smav				    hdaa_quirks_tab[i].value) ==
5780230130Smav				    hdaa_quirks_tab[i].value)
5781230130Smav					printf(" %s", hdaa_quirks_tab[i].key);
5782230130Smav			}
5783230130Smav			printf("\n");
5784230130Smav		}
5785230130Smav
5786230130Smav		device_printf(dev, "\n");
5787230130Smav		device_printf(dev, "+-------------------+\n");
5788230130Smav		device_printf(dev, "| DUMPING HDA NODES |\n");
5789230130Smav		device_printf(dev, "+-------------------+\n");
5790230130Smav		hdaa_dump_nodes(devinfo);
5791230130Smav	);
5792230130Smav
5793230130Smav	HDA_BOOTHVERBOSE(
5794230130Smav		device_printf(dev, "\n");
5795230130Smav		device_printf(dev, "+------------------------+\n");
5796230130Smav		device_printf(dev, "| DUMPING HDA AMPLIFIERS |\n");
5797230130Smav		device_printf(dev, "+------------------------+\n");
5798230130Smav		device_printf(dev, "\n");
5799230130Smav		i = 0;
5800230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5801230130Smav			device_printf(dev, "%3d: nid %3d %s (%s) index %d", i,
5802230130Smav			    (ctl->widget != NULL) ? ctl->widget->nid : -1,
5803230130Smav			    (ctl->ndir == HDAA_CTL_IN)?"in ":"out",
5804230130Smav			    (ctl->dir == HDAA_CTL_IN)?"in ":"out",
5805230130Smav			    ctl->index);
5806230130Smav			if (ctl->childwidget != NULL)
5807230130Smav				printf(" cnid %3d", ctl->childwidget->nid);
5808230130Smav			else
5809230130Smav				printf("         ");
5810230130Smav			printf(" ossmask=0x%08x\n",
5811230130Smav			    ctl->ossmask);
5812230130Smav			device_printf(dev,
5813230130Smav			    "       mute: %d step: %3d size: %3d off: %3d%s\n",
5814230130Smav			    ctl->mute, ctl->step, ctl->size, ctl->offset,
5815230130Smav			    (ctl->enable == 0) ? " [DISABLED]" :
5816230130Smav			    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
5817230130Smav		}
5818230130Smav	);
5819230130Smav
5820230130Smav	HDA_BOOTVERBOSE(
5821230130Smav		device_printf(dev, "\n");
5822230130Smav	);
5823230130Smav}
5824230130Smav
5825230130Smavstatic void
5826230130Smavhdaa_unconfigure(device_t dev)
5827230130Smav{
5828230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5829230130Smav	struct hdaa_widget *w;
5830230130Smav	int i, j;
5831230130Smav
5832230130Smav	HDA_BOOTHVERBOSE(
5833230130Smav		device_printf(dev, "HP switch deinit...\n");
5834230130Smav	);
5835230130Smav	hdaa_hp_switch_deinit(devinfo);
5836230312Smav	HDA_BOOTHVERBOSE(
5837230312Smav		device_printf(dev, "ELD deinit...\n");
5838230312Smav	);
5839230312Smav	hdaa_eld_deinit(devinfo);
5840230130Smav	free(devinfo->ctl, M_HDAA);
5841230130Smav	devinfo->ctl = NULL;
5842230130Smav	devinfo->ctlcnt = 0;
5843230130Smav	free(devinfo->as, M_HDAA);
5844230130Smav	devinfo->as = NULL;
5845230130Smav	devinfo->ascnt = 0;
5846230130Smav	free(devinfo->devs, M_HDAA);
5847230130Smav	devinfo->devs = NULL;
5848230130Smav	devinfo->num_devs = 0;
5849230130Smav	free(devinfo->chans, M_HDAA);
5850230130Smav	devinfo->chans = NULL;
5851230130Smav	devinfo->num_chans = 0;
5852230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5853230130Smav		w = hdaa_widget_get(devinfo, i);
5854230130Smav		if (w == NULL)
5855230130Smav			continue;
5856230130Smav		w->enable = 1;
5857230130Smav		w->selconn = -1;
5858230130Smav		w->pflags = 0;
5859230130Smav		w->bindas = -1;
5860230130Smav		w->bindseqmask = 0;
5861230130Smav		w->ossdev = -1;
5862230130Smav		w->ossmask = 0;
5863230130Smav		for (j = 0; j < w->nconns; j++)
5864230130Smav			w->connsenable[j] = 1;
5865230451Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5866230451Smav			w->wclass.pin.config = w->wclass.pin.newconf;
5867230312Smav		if (w->eld != NULL) {
5868230312Smav			w->eld_len = 0;
5869230312Smav			free(w->eld, M_HDAA);
5870230312Smav			w->eld = NULL;
5871230312Smav		}
5872230130Smav	}
5873230130Smav}
5874230130Smav
5875230130Smavstatic int
5876230130Smavhdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS)
5877230130Smav{
5878230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5879230130Smav	device_t dev = devinfo->dev;
5880230130Smav	char buf[256];
5881230130Smav	int n = 0, i, numgpi;
5882230130Smav	uint32_t data = 0;
5883230130Smav
5884230130Smav	buf[0] = 0;
5885230130Smav	hdaa_lock(devinfo);
5886230130Smav	numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap);
5887230130Smav	if (numgpi > 0) {
5888230130Smav		data = hda_command(dev,
5889230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
5890230130Smav	}
5891230130Smav	hdaa_unlock(devinfo);
5892230130Smav	for (i = 0; i < numgpi; i++) {
5893230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
5894230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
5895230130Smav	}
5896230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5897230130Smav}
5898230130Smav
5899230130Smavstatic int
5900230130Smavhdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS)
5901230130Smav{
5902230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5903230130Smav	device_t dev = devinfo->dev;
5904230130Smav	char buf[256];
5905230130Smav	int n = 0, i, numgpio;
5906230130Smav	uint32_t data = 0, enable = 0, dir = 0;
5907230130Smav
5908230130Smav	buf[0] = 0;
5909230130Smav	hdaa_lock(devinfo);
5910230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5911230130Smav	if (numgpio > 0) {
5912230130Smav		data = hda_command(dev,
5913230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
5914230130Smav		enable = hda_command(dev,
5915230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
5916230130Smav		dir = hda_command(dev,
5917230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
5918230130Smav	}
5919230130Smav	hdaa_unlock(devinfo);
5920230130Smav	for (i = 0; i < numgpio; i++) {
5921230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=",
5922230130Smav		    n != 0 ? " " : "", i);
5923230130Smav		if ((enable & (1 << i)) == 0) {
5924230130Smav			n += snprintf(buf + n, sizeof(buf) - n, "disabled");
5925230130Smav			continue;
5926230130Smav		}
5927230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)",
5928230130Smav		    ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1));
5929230130Smav	}
5930230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5931230130Smav}
5932230130Smav
5933230130Smavstatic int
5934230130Smavhdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS)
5935230130Smav{
5936230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5937230130Smav	char buf[256];
5938230130Smav	int error, n = 0, i, numgpio;
5939230130Smav	uint32_t gpio, x;
5940230130Smav
5941230130Smav	gpio = devinfo->newgpio;
5942230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5943230130Smav	buf[0] = 0;
5944230130Smav	for (i = 0; i < numgpio; i++) {
5945230130Smav		x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
5946230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
5947230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
5948230130Smav	}
5949230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
5950230130Smav	if (error != 0 || req->newptr == NULL)
5951230130Smav		return (error);
5952230130Smav	if (strncmp(buf, "0x", 2) == 0)
5953230130Smav		gpio = strtol(buf + 2, NULL, 16);
5954230130Smav	else
5955230130Smav		gpio = hdaa_gpio_patch(gpio, buf);
5956230130Smav	hdaa_lock(devinfo);
5957230130Smav	devinfo->newgpio = devinfo->gpio = gpio;
5958230130Smav	hdaa_gpio_commit(devinfo);
5959230130Smav	hdaa_unlock(devinfo);
5960230130Smav	return (0);
5961230130Smav}
5962230130Smav
5963230130Smavstatic int
5964230130Smavhdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS)
5965230130Smav{
5966230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5967230130Smav	device_t dev = devinfo->dev;
5968230130Smav	char buf[256];
5969230130Smav	int n = 0, i, numgpo;
5970230130Smav	uint32_t data = 0;
5971230130Smav
5972230130Smav	buf[0] = 0;
5973230130Smav	hdaa_lock(devinfo);
5974230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
5975230130Smav	if (numgpo > 0) {
5976230130Smav		data = hda_command(dev,
5977230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
5978230130Smav	}
5979230130Smav	hdaa_unlock(devinfo);
5980230130Smav	for (i = 0; i < numgpo; i++) {
5981230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
5982230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
5983230130Smav	}
5984230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5985230130Smav}
5986230130Smav
5987230130Smavstatic int
5988230130Smavhdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS)
5989230130Smav{
5990230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5991230130Smav	char buf[256];
5992230130Smav	int error, n = 0, i, numgpo;
5993230130Smav	uint32_t gpo, x;
5994230130Smav
5995230130Smav	gpo = devinfo->newgpo;
5996230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
5997230130Smav	buf[0] = 0;
5998230130Smav	for (i = 0; i < numgpo; i++) {
5999230130Smav		x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6000230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6001230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6002230130Smav	}
6003230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6004230130Smav	if (error != 0 || req->newptr == NULL)
6005230130Smav		return (error);
6006230130Smav	if (strncmp(buf, "0x", 2) == 0)
6007230130Smav		gpo = strtol(buf + 2, NULL, 16);
6008230130Smav	else
6009230130Smav		gpo = hdaa_gpio_patch(gpo, buf);
6010230130Smav	hdaa_lock(devinfo);
6011230130Smav	devinfo->newgpo = devinfo->gpo = gpo;
6012230130Smav	hdaa_gpo_commit(devinfo);
6013230130Smav	hdaa_unlock(devinfo);
6014230130Smav	return (0);
6015230130Smav}
6016230130Smav
6017230130Smavstatic int
6018230130Smavhdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS)
6019230130Smav{
6020230130Smav	device_t dev;
6021230130Smav	struct hdaa_devinfo *devinfo;
6022230130Smav	int error, val;
6023230130Smav
6024230130Smav	dev = oidp->oid_arg1;
6025230130Smav	devinfo = device_get_softc(dev);
6026230130Smav	if (devinfo == NULL)
6027230130Smav		return (EINVAL);
6028230130Smav	val = 0;
6029230130Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6030230130Smav	if (error != 0 || req->newptr == NULL || val == 0)
6031230130Smav		return (error);
6032230130Smav
6033230130Smav	HDA_BOOTHVERBOSE(
6034230130Smav		device_printf(dev, "Reconfiguration...\n");
6035230130Smav	);
6036230130Smav	if ((error = device_delete_children(dev)) != 0)
6037230130Smav		return (error);
6038230130Smav	hdaa_lock(devinfo);
6039230130Smav	hdaa_unconfigure(dev);
6040230130Smav	hdaa_configure(dev);
6041230130Smav	hdaa_unlock(devinfo);
6042230130Smav	bus_generic_attach(dev);
6043230130Smav	HDA_BOOTHVERBOSE(
6044230130Smav		device_printf(dev, "Reconfiguration done\n");
6045230130Smav	);
6046230130Smav	return (0);
6047230130Smav}
6048230130Smav
6049230130Smavstatic int
6050230130Smavhdaa_suspend(device_t dev)
6051230130Smav{
6052230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6053230130Smav	int i;
6054230130Smav
6055230130Smav	HDA_BOOTHVERBOSE(
6056230130Smav		device_printf(dev, "Suspend...\n");
6057230130Smav	);
6058230130Smav	hdaa_lock(devinfo);
6059230130Smav	HDA_BOOTHVERBOSE(
6060230130Smav		device_printf(dev, "Stop streams...\n");
6061230130Smav	);
6062230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6063230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) {
6064230130Smav			devinfo->chans[i].flags |= HDAA_CHN_SUSPEND;
6065230130Smav			hdaa_channel_stop(&devinfo->chans[i]);
6066230130Smav		}
6067230130Smav	}
6068230130Smav	HDA_BOOTHVERBOSE(
6069230130Smav		device_printf(dev, "HP switch deinit...\n");
6070230130Smav	);
6071230130Smav	hdaa_hp_switch_deinit(devinfo);
6072230130Smav	HDA_BOOTHVERBOSE(
6073230130Smav		device_printf(dev, "Power down FG"
6074230130Smav		    " nid=%d to the D3 state...\n",
6075230130Smav		    devinfo->nid);
6076230130Smav	);
6077230130Smav	hda_command(devinfo->dev,
6078230130Smav	    HDA_CMD_SET_POWER_STATE(0,
6079230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D3));
6080230130Smav	callout_stop(&devinfo->poll_jack);
6081230130Smav	hdaa_unlock(devinfo);
6082230130Smav	callout_drain(&devinfo->poll_jack);
6083230130Smav	HDA_BOOTHVERBOSE(
6084230130Smav		device_printf(dev, "Suspend done\n");
6085230130Smav	);
6086230130Smav	return (0);
6087230130Smav}
6088230130Smav
6089230130Smavstatic int
6090230130Smavhdaa_resume(device_t dev)
6091230130Smav{
6092230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6093230130Smav	int i;
6094230130Smav
6095230130Smav	HDA_BOOTHVERBOSE(
6096230130Smav		device_printf(dev, "Resume...\n");
6097230130Smav	);
6098230130Smav	hdaa_lock(devinfo);
6099230130Smav	HDA_BOOTHVERBOSE(
6100230130Smav		device_printf(dev, "Power up audio FG nid=%d...\n",
6101230130Smav		    devinfo->nid);
6102230130Smav	);
6103230130Smav	hdaa_powerup(devinfo);
6104230130Smav	HDA_BOOTHVERBOSE(
6105230130Smav		device_printf(dev, "AFG commit...\n");
6106230130Smav	);
6107230130Smav	hdaa_audio_commit(devinfo);
6108230130Smav	HDA_BOOTHVERBOSE(
6109230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
6110230130Smav	);
6111230130Smav	hdaa_patch_direct(devinfo);
6112230130Smav	HDA_BOOTHVERBOSE(
6113230130Smav		device_printf(dev, "HP switch init...\n");
6114230130Smav	);
6115230130Smav	hdaa_hp_switch_init(devinfo);
6116230130Smav
6117230130Smav	hdaa_unlock(devinfo);
6118230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
6119230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
6120230130Smav		HDA_BOOTHVERBOSE(
6121230130Smav			device_printf(pdevinfo->dev,
6122230130Smav			    "OSS mixer reinitialization...\n");
6123230130Smav		);
6124230130Smav		if (mixer_reinit(pdevinfo->dev) == -1)
6125230130Smav			device_printf(pdevinfo->dev,
6126230130Smav			    "unable to reinitialize the mixer\n");
6127230130Smav	}
6128230130Smav	hdaa_lock(devinfo);
6129230130Smav	HDA_BOOTHVERBOSE(
6130230130Smav		device_printf(dev, "Start streams...\n");
6131230130Smav	);
6132230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6133230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) {
6134230130Smav			devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND;
6135230130Smav			hdaa_channel_start(&devinfo->chans[i]);
6136230130Smav		}
6137230130Smav	}
6138230130Smav	hdaa_unlock(devinfo);
6139230130Smav	HDA_BOOTHVERBOSE(
6140230130Smav		device_printf(dev, "Resume done\n");
6141230130Smav	);
6142230130Smav	return (0);
6143230130Smav}
6144230130Smav
6145230130Smavstatic int
6146230130Smavhdaa_probe(device_t dev)
6147230130Smav{
6148230130Smav	char buf[128];
6149230130Smav
6150230130Smav	if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
6151230130Smav		return (ENXIO);
6152230130Smav	snprintf(buf, sizeof(buf), "%s Audio Function Group",
6153230130Smav	    device_get_desc(device_get_parent(dev)));
6154230130Smav	device_set_desc_copy(dev, buf);
6155230130Smav	return (BUS_PROBE_DEFAULT);
6156230130Smav}
6157230130Smav
6158230130Smavstatic int
6159230130Smavhdaa_attach(device_t dev)
6160230130Smav{
6161230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6162230130Smav	uint32_t res;
6163230130Smav	nid_t nid = hda_get_node_id(dev);
6164230130Smav
6165230130Smav	devinfo->dev = dev;
6166230130Smav	devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
6167230130Smav	devinfo->nid = nid;
6168230130Smav	devinfo->newquirks = -1;
6169230130Smav	devinfo->newgpio = -1;
6170230130Smav	devinfo->newgpo = -1;
6171230130Smav	callout_init(&devinfo->poll_jack, CALLOUT_MPSAFE);
6172230130Smav	devinfo->poll_ival = hz;
6173230130Smav
6174230130Smav	hdaa_lock(devinfo);
6175230130Smav	res = hda_command(dev,
6176230130Smav	    HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT));
6177230130Smav	hdaa_unlock(devinfo);
6178230130Smav
6179230130Smav	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
6180230130Smav	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
6181230130Smav	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
6182230130Smav
6183230130Smav	HDA_BOOTVERBOSE(
6184230130Smav		device_printf(dev,
6185230130Smav		    "Audio Function Group at nid=%d: %d subnodes %d-%d\n",
6186230130Smav		    nid, devinfo->nodecnt,
6187230130Smav		    devinfo->startnode, devinfo->endnode - 1);
6188230130Smav	);
6189230130Smav
6190230130Smav	if (devinfo->nodecnt > 0)
6191230130Smav		devinfo->widget = (struct hdaa_widget *)malloc(
6192230130Smav		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA,
6193230130Smav		    M_WAITOK | M_ZERO);
6194230130Smav	else
6195230130Smav		devinfo->widget = NULL;
6196230130Smav
6197230130Smav	hdaa_lock(devinfo);
6198230130Smav	HDA_BOOTHVERBOSE(
6199230130Smav		device_printf(dev, "Powering up...\n");
6200230130Smav	);
6201230130Smav	hdaa_powerup(devinfo);
6202230130Smav	HDA_BOOTHVERBOSE(
6203230130Smav		device_printf(dev, "Parsing audio FG...\n");
6204230130Smav	);
6205230130Smav	hdaa_audio_parse(devinfo);
6206230130Smav	HDA_BOOTVERBOSE(
6207230130Smav		device_printf(dev, "Original pins configuration:\n");
6208230130Smav		hdaa_dump_pin_configs(devinfo);
6209230130Smav	);
6210230130Smav	hdaa_configure(dev);
6211230130Smav	hdaa_unlock(devinfo);
6212230130Smav
6213230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6214230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6215230130Smav	    "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6216230130Smav	    &devinfo->newquirks, sizeof(&devinfo->newquirks),
6217230130Smav	    hdaa_sysctl_quirks, "A", "Configuration options");
6218230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6219230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6220230130Smav	    "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6221230130Smav	    devinfo, sizeof(devinfo),
6222230130Smav	    hdaa_sysctl_gpi_state, "A", "GPI state");
6223230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6224230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6225230130Smav	    "gpio_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6226230130Smav	    devinfo, sizeof(devinfo),
6227230130Smav	    hdaa_sysctl_gpio_state, "A", "GPIO state");
6228230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6229230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6230230130Smav	    "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6231230130Smav	    devinfo, sizeof(devinfo),
6232230130Smav	    hdaa_sysctl_gpio_config, "A", "GPIO configuration");
6233230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6234230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6235230130Smav	    "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6236230130Smav	    devinfo, sizeof(devinfo),
6237230130Smav	    hdaa_sysctl_gpo_state, "A", "GPO state");
6238230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6239230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6240230130Smav	    "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6241230130Smav	    devinfo, sizeof(devinfo),
6242230130Smav	    hdaa_sysctl_gpo_config, "A", "GPO configuration");
6243230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6244230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6245230130Smav	    "reconfig", CTLTYPE_INT | CTLFLAG_RW,
6246230130Smav	    dev, sizeof(dev),
6247230130Smav	    hdaa_sysctl_reconfig, "I", "Reprocess configuration");
6248230130Smav	bus_generic_attach(dev);
6249230130Smav	return (0);
6250230130Smav}
6251230130Smav
6252230130Smavstatic int
6253230130Smavhdaa_detach(device_t dev)
6254230130Smav{
6255230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6256230130Smav	int error;
6257230130Smav
6258230130Smav	if ((error = device_delete_children(dev)) != 0)
6259230130Smav		return (error);
6260230130Smav
6261230130Smav	hdaa_lock(devinfo);
6262230130Smav	hdaa_unconfigure(dev);
6263230130Smav	devinfo->poll_ival = 0;
6264230130Smav	callout_stop(&devinfo->poll_jack);
6265230130Smav	hdaa_unlock(devinfo);
6266230130Smav	callout_drain(&devinfo->poll_jack);
6267230130Smav
6268230130Smav	free(devinfo->widget, M_HDAA);
6269230130Smav	return (0);
6270230130Smav}
6271230130Smav
6272230130Smavstatic int
6273230130Smavhdaa_print_child(device_t dev, device_t child)
6274230130Smav{
6275230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6276230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6277230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6278230130Smav	struct hdaa_audio_as *as;
6279230130Smav	int retval, first = 1, i;
6280230130Smav
6281230130Smav	retval = bus_print_child_header(dev, child);
6282230130Smav	retval += printf(" at nid ");
6283230130Smav	if (pdevinfo->playas >= 0) {
6284230130Smav		as = &devinfo->as[pdevinfo->playas];
6285230130Smav		for (i = 0; i < 16; i++) {
6286230130Smav			if (as->pins[i] <= 0)
6287230130Smav				continue;
6288230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6289230130Smav			first = 0;
6290230130Smav		}
6291230130Smav	}
6292230130Smav	if (pdevinfo->recas >= 0) {
6293230130Smav		if (pdevinfo->playas >= 0) {
6294230130Smav			retval += printf(" and ");
6295230130Smav			first = 1;
6296230130Smav		}
6297230130Smav		as = &devinfo->as[pdevinfo->recas];
6298230130Smav		for (i = 0; i < 16; i++) {
6299230130Smav			if (as->pins[i] <= 0)
6300230130Smav				continue;
6301230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6302230130Smav			first = 0;
6303230130Smav		}
6304230130Smav	}
6305230130Smav	retval += bus_print_child_footer(dev, child);
6306230130Smav
6307230130Smav	return (retval);
6308230130Smav}
6309230130Smav
6310230130Smavstatic int
6311230130Smavhdaa_child_location_str(device_t dev, device_t child, char *buf,
6312230130Smav    size_t buflen)
6313230130Smav{
6314230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6315230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6316230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6317230130Smav	struct hdaa_audio_as *as;
6318230130Smav	int first = 1, i, len = 0;
6319230130Smav
6320230130Smav	len += snprintf(buf + len, buflen - len, "nid=");
6321230130Smav	if (pdevinfo->playas >= 0) {
6322230130Smav		as = &devinfo->as[pdevinfo->playas];
6323230130Smav		for (i = 0; i < 16; i++) {
6324230130Smav			if (as->pins[i] <= 0)
6325230130Smav				continue;
6326230130Smav			len += snprintf(buf + len, buflen - len,
6327230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6328230130Smav			first = 0;
6329230130Smav		}
6330230130Smav	}
6331230130Smav	if (pdevinfo->recas >= 0) {
6332230130Smav		as = &devinfo->as[pdevinfo->recas];
6333230130Smav		for (i = 0; i < 16; i++) {
6334230130Smav			if (as->pins[i] <= 0)
6335230130Smav				continue;
6336230130Smav			len += snprintf(buf + len, buflen - len,
6337230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6338230130Smav			first = 0;
6339230130Smav		}
6340230130Smav	}
6341230130Smav	return (0);
6342230130Smav}
6343230130Smav
6344230130Smavstatic void
6345230130Smavhdaa_stream_intr(device_t dev, int dir, int stream)
6346230130Smav{
6347230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6348230130Smav	struct hdaa_chan *ch;
6349230130Smav	int i;
6350230130Smav
6351230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6352230130Smav		ch = &devinfo->chans[i];
6353230130Smav		if (!(ch->flags & HDAA_CHN_RUNNING))
6354230130Smav			continue;
6355230130Smav		if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) &&
6356230130Smav		    ch->sid == stream) {
6357230130Smav			hdaa_unlock(devinfo);
6358230130Smav			chn_intr(ch->c);
6359230130Smav			hdaa_lock(devinfo);
6360230130Smav		}
6361230130Smav	}
6362230130Smav}
6363230130Smav
6364230130Smavstatic void
6365230130Smavhdaa_unsol_intr(device_t dev, uint32_t resp)
6366230130Smav{
6367230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6368230312Smav	struct hdaa_widget *w;
6369230312Smav	int i, tag, flags;
6370230130Smav
6371230312Smav	HDA_BOOTHVERBOSE(
6372230312Smav		device_printf(dev, "Unsolicited response %08x\n", resp);
6373230312Smav	);
6374230130Smav	tag = resp >> 26;
6375230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6376230312Smav		w = hdaa_widget_get(devinfo, i);
6377230312Smav		if (w == NULL || w->enable == 0 || w->type !=
6378230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6379230312Smav			continue;
6380230312Smav		if (w->unsol != tag)
6381230312Smav			continue;
6382230312Smav		if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
6383230312Smav		    HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
6384230312Smav			flags = resp & 0x03;
6385230312Smav		else
6386230312Smav			flags = 0x01;
6387230312Smav		if (flags & 0x01)
6388230312Smav			hdaa_hp_switch_handler(w);
6389230312Smav		if (flags & 0x02)
6390230312Smav			hdaa_eld_handler(w);
6391230130Smav	}
6392230130Smav}
6393230130Smav
6394230130Smavstatic device_method_t hdaa_methods[] = {
6395230130Smav	/* device interface */
6396230130Smav	DEVMETHOD(device_probe,		hdaa_probe),
6397230130Smav	DEVMETHOD(device_attach,	hdaa_attach),
6398230130Smav	DEVMETHOD(device_detach,	hdaa_detach),
6399230130Smav	DEVMETHOD(device_suspend,	hdaa_suspend),
6400230130Smav	DEVMETHOD(device_resume,	hdaa_resume),
6401230130Smav	/* Bus interface */
6402230130Smav	DEVMETHOD(bus_print_child,	hdaa_print_child),
6403230130Smav	DEVMETHOD(bus_child_location_str, hdaa_child_location_str),
6404230130Smav	DEVMETHOD(hdac_stream_intr,	hdaa_stream_intr),
6405230130Smav	DEVMETHOD(hdac_unsol_intr,	hdaa_unsol_intr),
6406230130Smav	DEVMETHOD(hdac_pindump,		hdaa_pindump),
6407230130Smav	{ 0, 0 }
6408230130Smav};
6409230130Smav
6410230130Smavstatic driver_t hdaa_driver = {
6411230130Smav	"hdaa",
6412230130Smav	hdaa_methods,
6413230130Smav	sizeof(struct hdaa_devinfo),
6414230130Smav};
6415230130Smav
6416230130Smavstatic devclass_t hdaa_devclass;
6417230130Smav
6418230130SmavDRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, 0, 0);
6419230130Smav
6420230130Smavstatic void
6421230130Smavhdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid,
6422230130Smav    char *buf, int buflen)
6423230130Smav{
6424230130Smav	struct hdaa_audio_as *as;
6425230130Smav	int c;
6426230130Smav
6427230130Smav	as = &devinfo->as[asid];
6428230130Smav	c = devinfo->chans[as->chans[0]].channels;
6429230130Smav	if (c == 1)
6430230130Smav		snprintf(buf, buflen, "mono");
6431230130Smav	else if (c == 2)
6432230130Smav		buf[0] = 0;
6433230130Smav	else if (as->pinset == 0x0003)
6434230130Smav		snprintf(buf, buflen, "3.1");
6435230130Smav	else if (as->pinset == 0x0005 || as->pinset == 0x0011)
6436230130Smav		snprintf(buf, buflen, "4.0");
6437230130Smav	else if (as->pinset == 0x0007 || as->pinset == 0x0013)
6438230130Smav		snprintf(buf, buflen, "5.1");
6439230130Smav	else if (as->pinset == 0x0017)
6440230130Smav		snprintf(buf, buflen, "7.1");
6441230130Smav	else
6442230130Smav		snprintf(buf, buflen, "%dch", c);
6443230130Smav}
6444230130Smav
6445230130Smavstatic int
6446230488Smavhdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS)
6447230488Smav{
6448230488Smav	struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1;
6449230488Smav	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
6450230488Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6451230488Smav	struct hdaa_chan *ch;
6452230488Smav	int error, val, i;
6453230488Smav	uint32_t pcmcap;
6454230488Smav
6455230488Smav	ch = &devinfo->chans[as->chans[0]];
6456230488Smav	val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 :
6457230488Smav	    ((ch->bit32 == 2) ? 20 : 0));
6458230488Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6459230488Smav	if (error != 0 || req->newptr == NULL)
6460230488Smav		return (error);
6461230488Smav	pcmcap = ch->supp_pcm_size_rate;
6462230488Smav	if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6463230488Smav		ch->bit32 = 4;
6464230488Smav	else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6465230488Smav		ch->bit32 = 3;
6466230488Smav	else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6467230488Smav		ch->bit32 = 2;
6468230488Smav	else
6469230488Smav		return (EINVAL);
6470230488Smav	for (i = 1; i < as->num_chans; i++)
6471230488Smav		devinfo->chans[as->chans[i]].bit32 = ch->bit32;
6472230488Smav	return (0);
6473230488Smav}
6474230488Smav
6475230488Smavstatic int
6476230130Smavhdaa_pcm_probe(device_t dev)
6477230130Smav{
6478230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6479230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6480230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6481230130Smav	char chans1[8], chans2[8];
6482230130Smav	char buf[128];
6483230130Smav	int loc1, loc2;
6484230130Smav
6485230130Smav	if (pdevinfo->playas >= 0)
6486230130Smav		loc1 = devinfo->as[pdevinfo->playas].location;
6487230130Smav	else
6488230130Smav		loc1 = devinfo->as[pdevinfo->recas].location;
6489230130Smav	if (pdevinfo->recas >= 0)
6490230130Smav		loc2 = devinfo->as[pdevinfo->recas].location;
6491230130Smav	else
6492230130Smav		loc2 = loc1;
6493230130Smav	if (loc1 != loc2)
6494230130Smav		loc1 = -2;
6495230130Smav	if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0')
6496230130Smav		loc1 = -2;
6497230130Smav	chans1[0] = 0;
6498230130Smav	chans2[0] = 0;
6499230130Smav	if (pdevinfo->playas >= 0)
6500230130Smav		hdaa_chan_formula(devinfo, pdevinfo->playas,
6501230130Smav		    chans1, sizeof(chans1));
6502230130Smav	if (pdevinfo->recas >= 0)
6503230130Smav		hdaa_chan_formula(devinfo, pdevinfo->recas,
6504230130Smav		    chans2, sizeof(chans2));
6505230130Smav	if (chans1[0] != 0 || chans2[0] != 0) {
6506230130Smav		if (chans1[0] == 0 && pdevinfo->playas >= 0)
6507230130Smav			snprintf(chans1, sizeof(chans1), "2.0");
6508230130Smav		else if (chans2[0] == 0 && pdevinfo->recas >= 0)
6509230130Smav			snprintf(chans2, sizeof(chans2), "2.0");
6510230130Smav		if (strcmp(chans1, chans2) == 0)
6511230130Smav			chans2[0] = 0;
6512230130Smav	}
6513230130Smav	snprintf(buf, sizeof(buf), "%s PCM (%s%s%s%s%s%s%s)",
6514230130Smav	    device_get_desc(device_get_parent(device_get_parent(dev))),
6515230130Smav	    loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "",
6516230507Smav	    (pdevinfo->digital == 0x7)?"HDMI/DP":
6517230507Smav	    ((pdevinfo->digital == 0x5)?"DisplayPort":
6518230507Smav	    ((pdevinfo->digital == 0x3)?"HDMI":
6519230507Smav	    ((pdevinfo->digital)?"Digital":"Analog"))),
6520230130Smav	    chans1[0] ? " " : "", chans1,
6521230130Smav	    chans2[0] ? "/" : "", chans2);
6522230130Smav	device_set_desc_copy(dev, buf);
6523230130Smav	return (BUS_PROBE_SPECIFIC);
6524230130Smav}
6525230130Smav
6526230130Smavstatic int
6527230130Smavhdaa_pcm_attach(device_t dev)
6528230130Smav{
6529230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6530230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6531230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6532230130Smav	struct hdaa_audio_as *as;
6533230488Smav	struct snddev_info *d;
6534230130Smav	char status[SND_STATUSLEN];
6535230130Smav	int i;
6536230130Smav
6537230130Smav	pdevinfo->chan_size = pcm_getbuffersize(dev,
6538230130Smav	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
6539230130Smav
6540230130Smav	HDA_BOOTVERBOSE(
6541230130Smav		device_printf(dev, "+--------------------------------------+\n");
6542230130Smav		device_printf(dev, "| DUMPING PCM Playback/Record Channels |\n");
6543230130Smav		device_printf(dev, "+--------------------------------------+\n");
6544230130Smav		hdaa_dump_pcmchannels(pdevinfo);
6545230130Smav		device_printf(dev, "\n");
6546230130Smav		device_printf(dev, "+-------------------------------+\n");
6547230130Smav		device_printf(dev, "| DUMPING Playback/Record Paths |\n");
6548230130Smav		device_printf(dev, "+-------------------------------+\n");
6549230130Smav		hdaa_dump_dac(pdevinfo);
6550230130Smav		hdaa_dump_adc(pdevinfo);
6551230130Smav		hdaa_dump_mix(pdevinfo);
6552230130Smav		device_printf(dev, "\n");
6553230130Smav		device_printf(dev, "+-------------------------+\n");
6554230130Smav		device_printf(dev, "| DUMPING Volume Controls |\n");
6555230130Smav		device_printf(dev, "+-------------------------+\n");
6556230130Smav		hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
6557230130Smav		hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
6558230130Smav		hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
6559230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
6560230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
6561230130Smav		hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
6562230130Smav		hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
6563230130Smav		hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
6564230130Smav		hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
6565230130Smav		hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
6566230130Smav		hdaa_dump_ctls(pdevinfo, NULL, 0);
6567230130Smav		device_printf(dev, "\n");
6568230130Smav	);
6569230130Smav
6570230130Smav	if (resource_int_value(device_get_name(dev),
6571230130Smav	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
6572230130Smav		i &= HDA_BLK_ALIGN;
6573230130Smav		if (i < HDA_BLK_MIN)
6574230130Smav			i = HDA_BLK_MIN;
6575230130Smav		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
6576230130Smav		i = 0;
6577230130Smav		while (pdevinfo->chan_blkcnt >> i)
6578230130Smav			i++;
6579230130Smav		pdevinfo->chan_blkcnt = 1 << (i - 1);
6580230130Smav		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
6581230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
6582230130Smav		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
6583230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
6584230130Smav	} else
6585230130Smav		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
6586230130Smav
6587230130Smav	/*
6588230130Smav	 * We don't register interrupt handler with snd_setup_intr
6589230130Smav	 * in pcm device. Mark pcm device as MPSAFE manually.
6590230130Smav	 */
6591230130Smav	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
6592230130Smav
6593230130Smav	HDA_BOOTHVERBOSE(
6594230130Smav		device_printf(dev, "OSS mixer initialization...\n");
6595230130Smav	);
6596230130Smav	if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0)
6597230130Smav		device_printf(dev, "Can't register mixer\n");
6598230451Smav	else
6599230451Smav		hdaa_audio_ctl_set_defaults(pdevinfo);
6600230130Smav
6601230130Smav	HDA_BOOTHVERBOSE(
6602230130Smav		device_printf(dev, "Registering PCM channels...\n");
6603230130Smav	);
6604230130Smav	if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0,
6605230130Smav	    (pdevinfo->recas >= 0)?1:0) != 0)
6606230130Smav		device_printf(dev, "Can't register PCM\n");
6607230130Smav
6608230130Smav	pdevinfo->registered++;
6609230130Smav
6610230488Smav	d = device_get_softc(dev);
6611230130Smav	if (pdevinfo->playas >= 0) {
6612230130Smav		as = &devinfo->as[pdevinfo->playas];
6613230130Smav		for (i = 0; i < as->num_chans; i++)
6614230130Smav			pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class,
6615230130Smav			    &devinfo->chans[as->chans[i]]);
6616230488Smav		SYSCTL_ADD_PROC(&d->play_sysctl_ctx,
6617230488Smav		    SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO,
6618230488Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6619230488Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
6620230488Smav		    "Resolution of 32bit samples (20/24/32bit)");
6621230130Smav	}
6622230130Smav	if (pdevinfo->recas >= 0) {
6623230130Smav		as = &devinfo->as[pdevinfo->recas];
6624230130Smav		for (i = 0; i < as->num_chans; i++)
6625230130Smav			pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class,
6626230130Smav			    &devinfo->chans[as->chans[i]]);
6627230488Smav		SYSCTL_ADD_PROC(&d->rec_sysctl_ctx,
6628230488Smav		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
6629230488Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6630230488Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
6631230488Smav		    "Resolution of 32bit samples (20/24/32bit)");
6632230130Smav	}
6633230130Smav
6634230130Smav	snprintf(status, SND_STATUSLEN, "on %s %s",
6635230130Smav	    device_get_nameunit(device_get_parent(dev)),
6636230130Smav	    PCM_KLDSTRING(snd_hda));
6637230130Smav	pcm_setstatus(dev, status);
6638230130Smav
6639230130Smav	return (0);
6640230130Smav}
6641230130Smav
6642230130Smavstatic int
6643230130Smavhdaa_pcm_detach(device_t dev)
6644230130Smav{
6645230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6646230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6647230130Smav	int err;
6648230130Smav
6649230130Smav	if (pdevinfo->registered > 0) {
6650230130Smav		err = pcm_unregister(dev);
6651230130Smav		if (err != 0)
6652230130Smav			return (err);
6653230130Smav	}
6654230130Smav
6655230130Smav	return (0);
6656230130Smav}
6657230130Smav
6658230130Smavstatic device_method_t hdaa_pcm_methods[] = {
6659230130Smav	/* device interface */
6660230130Smav	DEVMETHOD(device_probe,		hdaa_pcm_probe),
6661230130Smav	DEVMETHOD(device_attach,	hdaa_pcm_attach),
6662230130Smav	DEVMETHOD(device_detach,	hdaa_pcm_detach),
6663230130Smav	{ 0, 0 }
6664230130Smav};
6665230130Smav
6666230130Smavstatic driver_t hdaa_pcm_driver = {
6667230130Smav	"pcm",
6668230130Smav	hdaa_pcm_methods,
6669230130Smav	PCM_SOFTC_SIZE,
6670230130Smav};
6671230130Smav
6672230130SmavDRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, 0, 0);
6673230130SmavMODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
6674230130SmavMODULE_VERSION(snd_hda, 1);
6675