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$");
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 {
56264962Smarius	const 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
75230130Smav#define HDA_PARSE_MAXDEPTH	10
76230130Smav
77230130SmavMALLOC_DEFINE(M_HDAA, "hdaa", "HDA Audio");
78230130Smav
79264962Smariusstatic const char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue",
80264962Smarius    "Green", "Red", "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B",
81264962Smarius    "Res.C", "Res.D", "White", "Other"};
82230130Smav
83264962Smariusstatic const char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD",
84230130Smav    "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in",
85230130Smav    "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"};
86230130Smav
87264962Smariusstatic const char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
88230130Smav
89264962Smariusstatic const char *HDA_CONNECTORS[16] = {
90230130Smav    "Unknown", "1/8", "1/4", "ATAPI", "RCA", "Optical", "Digital", "Analog",
91230130Smav    "DIN", "XLR", "RJ-11", "Combo", "0xc", "0xd", "0xe", "Other" };
92230130Smav
93264962Smariusstatic const char *HDA_LOCS[64] = {
94230130Smav    "0x00", "Rear", "Front", "Left", "Right", "Top", "Bottom", "Rear-panel",
95230130Smav	"Drive-bay", "0x09", "0x0a", "0x0b", "0x0c", "0x0d", "0x0e", "0x0f",
96230130Smav    "Internal", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "Riser",
97230130Smav	"0x18", "Onboard", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f",
98230130Smav    "External", "Ext-Rear", "Ext-Front", "Ext-Left", "Ext-Right", "Ext-Top", "Ext-Bottom", "0x07",
99230130Smav	"0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f",
100230130Smav    "Other", "0x31", "0x32", "0x33", "0x34", "0x35", "Other-Bott", "Lid-In",
101230130Smav	"Lid-Out", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f" };
102230130Smav
103264962Smariusstatic const char *HDA_GPIO_ACTIONS[8] = {
104230130Smav    "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"};
105230130Smav
106264962Smariusstatic const char *HDA_HDMI_CODING_TYPES[18] = {
107232798Smav    "undefined", "LPCM", "AC-3", "MPEG1", "MP3", "MPEG2", "AAC-LC", "DTS",
108232798Smav    "ATRAC", "DSD", "E-AC-3", "DTS-HD", "MLP", "DST", "WMAPro", "HE-AAC",
109232798Smav    "HE-AACv2", "MPEG-Surround"
110232798Smav};
111232798Smav
112230130Smav/* Default */
113230130Smavstatic uint32_t hdaa_fmt[] = {
114230130Smav	SND_FORMAT(AFMT_S16_LE, 2, 0),
115230130Smav	0
116230130Smav};
117230130Smav
118230130Smavstatic struct pcmchan_caps hdaa_caps = {48000, 48000, hdaa_fmt, 0};
119230130Smav
120230130Smavstatic const struct {
121230130Smav	uint32_t	rate;
122230130Smav	int		valid;
123230130Smav	uint16_t	base;
124230130Smav	uint16_t	mul;
125230130Smav	uint16_t	div;
126230130Smav} hda_rate_tab[] = {
127230130Smav	{   8000, 1, 0x0000, 0x0000, 0x0500 },	/* (48000 * 1) / 6 */
128230130Smav	{   9600, 0, 0x0000, 0x0000, 0x0400 },	/* (48000 * 1) / 5 */
129230130Smav	{  12000, 0, 0x0000, 0x0000, 0x0300 },	/* (48000 * 1) / 4 */
130230130Smav	{  16000, 1, 0x0000, 0x0000, 0x0200 },	/* (48000 * 1) / 3 */
131230130Smav	{  18000, 0, 0x0000, 0x1000, 0x0700 },	/* (48000 * 3) / 8 */
132230130Smav	{  19200, 0, 0x0000, 0x0800, 0x0400 },	/* (48000 * 2) / 5 */
133230130Smav	{  24000, 0, 0x0000, 0x0000, 0x0100 },	/* (48000 * 1) / 2 */
134230130Smav	{  28800, 0, 0x0000, 0x1000, 0x0400 },	/* (48000 * 3) / 5 */
135230130Smav	{  32000, 1, 0x0000, 0x0800, 0x0200 },	/* (48000 * 2) / 3 */
136230130Smav	{  36000, 0, 0x0000, 0x1000, 0x0300 },	/* (48000 * 3) / 4 */
137230130Smav	{  38400, 0, 0x0000, 0x1800, 0x0400 },	/* (48000 * 4) / 5 */
138230130Smav	{  48000, 1, 0x0000, 0x0000, 0x0000 },	/* (48000 * 1) / 1 */
139230130Smav	{  64000, 0, 0x0000, 0x1800, 0x0200 },	/* (48000 * 4) / 3 */
140230130Smav	{  72000, 0, 0x0000, 0x1000, 0x0100 },	/* (48000 * 3) / 2 */
141230130Smav	{  96000, 1, 0x0000, 0x0800, 0x0000 },	/* (48000 * 2) / 1 */
142230130Smav	{ 144000, 0, 0x0000, 0x1000, 0x0000 },	/* (48000 * 3) / 1 */
143230130Smav	{ 192000, 1, 0x0000, 0x1800, 0x0000 },	/* (48000 * 4) / 1 */
144230130Smav	{   8820, 0, 0x4000, 0x0000, 0x0400 },	/* (44100 * 1) / 5 */
145230130Smav	{  11025, 1, 0x4000, 0x0000, 0x0300 },	/* (44100 * 1) / 4 */
146230130Smav	{  12600, 0, 0x4000, 0x0800, 0x0600 },	/* (44100 * 2) / 7 */
147230130Smav	{  14700, 0, 0x4000, 0x0000, 0x0200 },	/* (44100 * 1) / 3 */
148230130Smav	{  17640, 0, 0x4000, 0x0800, 0x0400 },	/* (44100 * 2) / 5 */
149230130Smav	{  18900, 0, 0x4000, 0x1000, 0x0600 },	/* (44100 * 3) / 7 */
150230130Smav	{  22050, 1, 0x4000, 0x0000, 0x0100 },	/* (44100 * 1) / 2 */
151230130Smav	{  25200, 0, 0x4000, 0x1800, 0x0600 },	/* (44100 * 4) / 7 */
152230130Smav	{  26460, 0, 0x4000, 0x1000, 0x0400 },	/* (44100 * 3) / 5 */
153230130Smav	{  29400, 0, 0x4000, 0x0800, 0x0200 },	/* (44100 * 2) / 3 */
154230130Smav	{  33075, 0, 0x4000, 0x1000, 0x0300 },	/* (44100 * 3) / 4 */
155230130Smav	{  35280, 0, 0x4000, 0x1800, 0x0400 },	/* (44100 * 4) / 5 */
156230130Smav	{  44100, 1, 0x4000, 0x0000, 0x0000 },	/* (44100 * 1) / 1 */
157230130Smav	{  58800, 0, 0x4000, 0x1800, 0x0200 },	/* (44100 * 4) / 3 */
158230130Smav	{  66150, 0, 0x4000, 0x1000, 0x0100 },	/* (44100 * 3) / 2 */
159230130Smav	{  88200, 1, 0x4000, 0x0800, 0x0000 },	/* (44100 * 2) / 1 */
160230130Smav	{ 132300, 0, 0x4000, 0x1000, 0x0000 },	/* (44100 * 3) / 1 */
161230130Smav	{ 176400, 1, 0x4000, 0x1800, 0x0000 },	/* (44100 * 4) / 1 */
162230130Smav};
163230130Smav#define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
164230130Smav
165232798Smavconst static char *ossnames[] = SOUND_DEVICE_NAMES;
166232798Smav
167230130Smav/****************************************************************************
168230130Smav * Function prototypes
169230130Smav ****************************************************************************/
170230130Smavstatic int	hdaa_pcmchannel_setup(struct hdaa_chan *);
171230130Smav
172230130Smavstatic void	hdaa_widget_connection_select(struct hdaa_widget *, uint8_t);
173230130Smavstatic void	hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *,
174230130Smav						uint32_t, int, int);
175230130Smavstatic struct	hdaa_audio_ctl *hdaa_audio_ctl_amp_get(struct hdaa_devinfo *,
176230130Smav							nid_t, int, int, int);
177230130Smavstatic void	hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *,
178230130Smav				nid_t, int, int, int, int, int, int);
179230130Smav
180230130Smavstatic void	hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf);
181230130Smav
182230130Smavstatic char *
183230130Smavhdaa_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
184230130Smav{
185230130Smav	int i, first = 1;
186230130Smav
187230130Smav	bzero(buf, len);
188230130Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
189230130Smav		if (mask & (1 << i)) {
190230130Smav			if (first == 0)
191230130Smav				strlcat(buf, ", ", len);
192232798Smav			strlcat(buf, ossnames[i], len);
193230130Smav			first = 0;
194230130Smav		}
195230130Smav	}
196230130Smav	return (buf);
197230130Smav}
198230130Smav
199230130Smavstatic struct hdaa_audio_ctl *
200230130Smavhdaa_audio_ctl_each(struct hdaa_devinfo *devinfo, int *index)
201230130Smav{
202230130Smav	if (devinfo == NULL ||
203230130Smav	    index == NULL || devinfo->ctl == NULL ||
204230130Smav	    devinfo->ctlcnt < 1 ||
205230130Smav	    *index < 0 || *index >= devinfo->ctlcnt)
206230130Smav		return (NULL);
207230130Smav	return (&devinfo->ctl[(*index)++]);
208230130Smav}
209230130Smav
210230130Smavstatic struct hdaa_audio_ctl *
211230130Smavhdaa_audio_ctl_amp_get(struct hdaa_devinfo *devinfo, nid_t nid, int dir,
212230130Smav						int index, int cnt)
213230130Smav{
214230130Smav	struct hdaa_audio_ctl *ctl;
215230130Smav	int i, found = 0;
216230130Smav
217230130Smav	if (devinfo == NULL || devinfo->ctl == NULL)
218230130Smav		return (NULL);
219230130Smav
220230130Smav	i = 0;
221230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
222230130Smav		if (ctl->enable == 0)
223230130Smav			continue;
224230130Smav		if (ctl->widget->nid != nid)
225230130Smav			continue;
226230130Smav		if (dir && ctl->ndir != dir)
227230130Smav			continue;
228230130Smav		if (index >= 0 && ctl->ndir == HDAA_CTL_IN &&
229230130Smav		    ctl->dir == ctl->ndir && ctl->index != index)
230230130Smav			continue;
231230130Smav		found++;
232230130Smav		if (found == cnt || cnt <= 0)
233230130Smav			return (ctl);
234230130Smav	}
235230130Smav
236230130Smav	return (NULL);
237230130Smav}
238230130Smav
239230130Smav/*
240232798Smav * Headphones redirection change handler.
241230130Smav */
242230130Smavstatic void
243232798Smavhdaa_hpredir_handler(struct hdaa_widget *w)
244230130Smav{
245232798Smav	struct hdaa_devinfo *devinfo = w->devinfo;
246232798Smav	struct hdaa_audio_as *as = &devinfo->as[w->bindas];
247232798Smav	struct hdaa_widget *w1;
248230130Smav	struct hdaa_audio_ctl *ctl;
249232798Smav	uint32_t val;
250232798Smav	int j, connected = w->wclass.pin.connected;
251230130Smav
252230130Smav	HDA_BOOTVERBOSE(
253232798Smav		device_printf((as->pdevinfo && as->pdevinfo->dev) ?
254232798Smav		    as->pdevinfo->dev : devinfo->dev,
255232798Smav		    "Redirect output to: %s\n",
256232798Smav		    connected ? "headphones": "main");
257230130Smav	);
258230130Smav	/* (Un)Mute headphone pin. */
259230130Smav	ctl = hdaa_audio_ctl_amp_get(devinfo,
260232798Smav	    w->nid, HDAA_CTL_IN, -1, 1);
261230130Smav	if (ctl != NULL && ctl->mute) {
262230130Smav		/* If pin has muter - use it. */
263232798Smav		val = connected ? 0 : 1;
264230130Smav		if (val != ctl->forcemute) {
265230130Smav			ctl->forcemute = val;
266230130Smav			hdaa_audio_ctl_amp_set(ctl,
267230130Smav			    HDAA_AMP_MUTE_DEFAULT,
268230130Smav			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
269230130Smav		}
270230130Smav	} else {
271230130Smav		/* If there is no muter - disable pin output. */
272232798Smav		if (connected)
273232798Smav			val = w->wclass.pin.ctrl |
274232798Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
275232798Smav		else
276232798Smav			val = w->wclass.pin.ctrl &
277232798Smav			    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
278232798Smav		if (val != w->wclass.pin.ctrl) {
279232798Smav			w->wclass.pin.ctrl = val;
280232798Smav			hda_command(devinfo->dev,
281232798Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
282232798Smav			    w->nid, w->wclass.pin.ctrl));
283230130Smav		}
284230130Smav	}
285230130Smav	/* (Un)Mute other pins. */
286230130Smav	for (j = 0; j < 15; j++) {
287230130Smav		if (as->pins[j] <= 0)
288230130Smav			continue;
289230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo,
290230130Smav		    as->pins[j], HDAA_CTL_IN, -1, 1);
291230130Smav		if (ctl != NULL && ctl->mute) {
292230130Smav			/* If pin has muter - use it. */
293232798Smav			val = connected ? 1 : 0;
294230130Smav			if (val == ctl->forcemute)
295230130Smav				continue;
296230130Smav			ctl->forcemute = val;
297230130Smav			hdaa_audio_ctl_amp_set(ctl,
298230130Smav			    HDAA_AMP_MUTE_DEFAULT,
299230130Smav			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
300230130Smav			continue;
301230130Smav		}
302230130Smav		/* If there is no muter - disable pin output. */
303232798Smav		w1 = hdaa_widget_get(devinfo, as->pins[j]);
304232798Smav		if (w1 != NULL) {
305232798Smav			if (connected)
306232798Smav				val = w1->wclass.pin.ctrl &
307230130Smav				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
308230130Smav			else
309232798Smav				val = w1->wclass.pin.ctrl |
310230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
311232798Smav			if (val != w1->wclass.pin.ctrl) {
312232798Smav				w1->wclass.pin.ctrl = val;
313230130Smav				hda_command(devinfo->dev,
314230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
315232798Smav				    w1->nid, w1->wclass.pin.ctrl));
316230130Smav			}
317230130Smav		}
318230130Smav	}
319230130Smav}
320230130Smav
321230130Smav/*
322232798Smav * Recording source change handler.
323230130Smav */
324230130Smavstatic void
325232798Smavhdaa_autorecsrc_handler(struct hdaa_audio_as *as, struct hdaa_widget *w)
326232798Smav{
327232798Smav	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
328232798Smav	struct hdaa_devinfo *devinfo;
329232798Smav	struct hdaa_widget *w1;
330232798Smav	int i, mask, fullmask, prio, bestprio;
331232798Smav	char buf[128];
332232798Smav
333232798Smav	if (!as->mixed || pdevinfo == NULL || pdevinfo->mixer == NULL)
334232798Smav		return;
335232798Smav	/* Don't touch anything if we asked not to. */
336232798Smav	if (pdevinfo->autorecsrc == 0 ||
337232798Smav	    (pdevinfo->autorecsrc == 1 && w != NULL))
338232798Smav		return;
339232798Smav	/* Don't touch anything if "mix" or "speaker" selected. */
340232798Smav	if (pdevinfo->recsrc & (SOUND_MASK_IMIX | SOUND_MASK_SPEAKER))
341232798Smav		return;
342232798Smav	/* Don't touch anything if several selected. */
343232798Smav	if (ffs(pdevinfo->recsrc) != fls(pdevinfo->recsrc))
344232798Smav		return;
345232798Smav	devinfo = pdevinfo->devinfo;
346232798Smav	mask = fullmask = 0;
347232798Smav	bestprio = 0;
348232798Smav	for (i = 0; i < 16; i++) {
349232798Smav		if (as->pins[i] <= 0)
350232798Smav			continue;
351232798Smav		w1 = hdaa_widget_get(devinfo, as->pins[i]);
352232798Smav		if (w1 == NULL || w1->enable == 0)
353232798Smav			continue;
354232798Smav		if (w1->wclass.pin.connected == 0)
355232798Smav			continue;
356232798Smav		prio = (w1->wclass.pin.connected == 1) ? 2 : 1;
357232798Smav		if (prio < bestprio)
358232798Smav			continue;
359232798Smav		if (prio > bestprio) {
360232798Smav			mask = 0;
361232798Smav			bestprio = prio;
362232798Smav		}
363232798Smav		mask |= (1 << w1->ossdev);
364232798Smav		fullmask |= (1 << w1->ossdev);
365232798Smav	}
366232798Smav	if (mask == 0)
367232798Smav		return;
368232798Smav	/* Prefer newly connected input. */
369232798Smav	if (w != NULL && (mask & (1 << w->ossdev)))
370232798Smav		mask = (1 << w->ossdev);
371232798Smav	/* Prefer previously selected input */
372232798Smav	if (mask & pdevinfo->recsrc)
373232798Smav		mask &= pdevinfo->recsrc;
374232798Smav	/* Prefer mic. */
375232798Smav	if (mask & SOUND_MASK_MIC)
376232798Smav		mask = SOUND_MASK_MIC;
377232798Smav	/* Prefer monitor (2nd mic). */
378232798Smav	if (mask & SOUND_MASK_MONITOR)
379232798Smav		mask = SOUND_MASK_MONITOR;
380232798Smav	/* Just take first one. */
381232798Smav	mask = (1 << (ffs(mask) - 1));
382232798Smav	HDA_BOOTVERBOSE(
383232798Smav		hdaa_audio_ctl_ossmixer_mask2allname(mask, buf, sizeof(buf));
384232798Smav		device_printf(pdevinfo->dev,
385232798Smav		    "Automatically set rec source to: %s\n", buf);
386232798Smav	);
387232798Smav	hdaa_unlock(devinfo);
388232798Smav	mix_setrecsrc(pdevinfo->mixer, mask);
389232798Smav	hdaa_lock(devinfo);
390232798Smav}
391232798Smav
392232798Smav/*
393232798Smav * Jack presence detection event handler.
394232798Smav */
395232798Smavstatic void
396232798Smavhdaa_presence_handler(struct hdaa_widget *w)
397232798Smav{
398232798Smav	struct hdaa_devinfo *devinfo = w->devinfo;
399232798Smav	struct hdaa_audio_as *as;
400232798Smav	uint32_t res;
401243062Smav	int connected, old;
402232798Smav
403232798Smav	if (w->enable == 0 || w->type !=
404232798Smav	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
405232798Smav		return;
406232798Smav
407232798Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
408232798Smav	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
409232798Smav		return;
410232798Smav
411232798Smav	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
412232798Smav	connected = (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) != 0;
413232798Smav	if (devinfo->quirks & HDAA_QUIRK_SENSEINV)
414232798Smav		connected = !connected;
415243062Smav	old = w->wclass.pin.connected;
416243062Smav	if (connected == old)
417232798Smav		return;
418232798Smav	w->wclass.pin.connected = connected;
419232798Smav	HDA_BOOTVERBOSE(
420243062Smav		if (connected || old != 2) {
421243062Smav			device_printf(devinfo->dev,
422243062Smav			    "Pin sense: nid=%d sence=0x%08x (%sconnected)\n",
423243062Smav			    w->nid, res, !connected ? "dis" : "");
424243062Smav		}
425232798Smav	);
426232798Smav
427232798Smav	as = &devinfo->as[w->bindas];
428232798Smav	if (as->hpredir >= 0 && as->pins[15] == w->nid)
429232798Smav		hdaa_hpredir_handler(w);
430243062Smav	if (as->dir == HDAA_CTL_IN && old != 2)
431232798Smav		hdaa_autorecsrc_handler(as, w);
432232798Smav}
433232798Smav
434232798Smav/*
435232798Smav * Callback for poll based presence detection.
436232798Smav */
437232798Smavstatic void
438230130Smavhdaa_jack_poll_callback(void *arg)
439230130Smav{
440230130Smav	struct hdaa_devinfo *devinfo = arg;
441232798Smav	struct hdaa_widget *w;
442230130Smav	int i;
443230130Smav
444230130Smav	hdaa_lock(devinfo);
445230130Smav	if (devinfo->poll_ival == 0) {
446230130Smav		hdaa_unlock(devinfo);
447230130Smav		return;
448230130Smav	}
449230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
450230130Smav		if (devinfo->as[i].hpredir < 0)
451230130Smav			continue;
452232798Smav		w = hdaa_widget_get(devinfo, devinfo->as[i].pins[15]);
453232798Smav		if (w == NULL || w->enable == 0 || w->type !=
454232798Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
455232798Smav			continue;
456232798Smav		hdaa_presence_handler(w);
457230130Smav	}
458230130Smav	callout_reset(&devinfo->poll_jack, devinfo->poll_ival,
459230130Smav	    hdaa_jack_poll_callback, devinfo);
460230130Smav	hdaa_unlock(devinfo);
461230130Smav}
462230130Smav
463232798Smavstatic void
464232798Smavhdaa_eld_dump(struct hdaa_widget *w)
465232798Smav{
466232798Smav	struct hdaa_devinfo *devinfo = w->devinfo;
467232798Smav	device_t dev = devinfo->dev;
468232798Smav	uint8_t *sad;
469232798Smav	int len, mnl, i, sadc, fmt;
470232798Smav
471232798Smav	if (w->eld == NULL || w->eld_len < 4)
472232798Smav		return;
473232798Smav	device_printf(dev,
474232798Smav	    "ELD nid=%d: ELD_Ver=%u Baseline_ELD_Len=%u\n",
475232798Smav	    w->nid, w->eld[0] >> 3, w->eld[2]);
476232798Smav	if ((w->eld[0] >> 3) != 0x02)
477232798Smav		return;
478232798Smav	len = min(w->eld_len, (u_int)w->eld[2] * 4);
479232798Smav	mnl = w->eld[4] & 0x1f;
480232798Smav	device_printf(dev,
481232798Smav	    "ELD nid=%d: CEA_EDID_Ver=%u MNL=%u\n",
482232798Smav	    w->nid, w->eld[4] >> 5, mnl);
483232798Smav	sadc = w->eld[5] >> 4;
484232798Smav	device_printf(dev,
485232798Smav	    "ELD nid=%d: SAD_Count=%u Conn_Type=%u S_AI=%u HDCP=%u\n",
486232798Smav	    w->nid, sadc, (w->eld[5] >> 2) & 0x3,
487232798Smav	    (w->eld[5] >> 1) & 0x1, w->eld[5] & 0x1);
488232798Smav	device_printf(dev,
489232798Smav	    "ELD nid=%d: Aud_Synch_Delay=%ums\n",
490232798Smav	    w->nid, w->eld[6] * 2);
491232798Smav	device_printf(dev,
492232798Smav	    "ELD nid=%d: Channels=0x%b\n",
493232798Smav	    w->nid, w->eld[7],
494232798Smav	    "\020\07RLRC\06FLRC\05RC\04RLR\03FC\02LFE\01FLR");
495232798Smav	device_printf(dev,
496232798Smav	    "ELD nid=%d: Port_ID=0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
497232798Smav	    w->nid, w->eld[8], w->eld[9], w->eld[10], w->eld[11],
498232798Smav	    w->eld[12], w->eld[13], w->eld[14], w->eld[15]);
499232798Smav	device_printf(dev,
500232798Smav	    "ELD nid=%d: Manufacturer_Name=0x%02x%02x\n",
501232798Smav	    w->nid, w->eld[16], w->eld[17]);
502232798Smav	device_printf(dev,
503232798Smav	    "ELD nid=%d: Product_Code=0x%02x%02x\n",
504232798Smav	    w->nid, w->eld[18], w->eld[19]);
505232798Smav	device_printf(dev,
506232798Smav	    "ELD nid=%d: Monitor_Name_String='%.*s'\n",
507232798Smav	    w->nid, mnl, &w->eld[20]);
508232798Smav	for (i = 0; i < sadc; i++) {
509232798Smav		sad = &w->eld[20 + mnl + i * 3];
510232798Smav		fmt = (sad[0] >> 3) & 0x0f;
511232798Smav		if (fmt == HDA_HDMI_CODING_TYPE_REF_CTX) {
512232798Smav			fmt = (sad[2] >> 3) & 0x1f;
513232798Smav			if (fmt < 1 || fmt > 3)
514232798Smav				fmt = 0;
515232798Smav			else
516232798Smav				fmt += 14;
517232798Smav		}
518232798Smav		device_printf(dev,
519232798Smav		    "ELD nid=%d: %s %dch freqs=0x%b",
520232798Smav		    w->nid, HDA_HDMI_CODING_TYPES[fmt], (sad[0] & 0x07) + 1,
521232798Smav		    sad[1], "\020\007192\006176\00596\00488\00348\00244\00132");
522232798Smav		switch (fmt) {
523232798Smav		case HDA_HDMI_CODING_TYPE_LPCM:
524232798Smav			printf(" sizes=0x%b",
525232798Smav			    sad[2] & 0x07, "\020\00324\00220\00116");
526232798Smav			break;
527232798Smav		case HDA_HDMI_CODING_TYPE_AC3:
528232798Smav		case HDA_HDMI_CODING_TYPE_MPEG1:
529232798Smav		case HDA_HDMI_CODING_TYPE_MP3:
530232798Smav		case HDA_HDMI_CODING_TYPE_MPEG2:
531232798Smav		case HDA_HDMI_CODING_TYPE_AACLC:
532232798Smav		case HDA_HDMI_CODING_TYPE_DTS:
533232798Smav		case HDA_HDMI_CODING_TYPE_ATRAC:
534232798Smav			printf(" max_bitrate=%d", sad[2] * 8000);
535232798Smav			break;
536232798Smav		case HDA_HDMI_CODING_TYPE_WMAPRO:
537232798Smav			printf(" profile=%d", sad[2] & 0x07);
538232798Smav			break;
539232798Smav		}
540232798Smav		printf("\n");
541232798Smav	}
542232798Smav}
543232798Smav
544232798Smavstatic void
545232798Smavhdaa_eld_handler(struct hdaa_widget *w)
546232798Smav{
547232798Smav	struct hdaa_devinfo *devinfo = w->devinfo;
548232798Smav	uint32_t res;
549232798Smav	int i;
550232798Smav
551232798Smav	if (w->enable == 0 || w->type !=
552232798Smav	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
553232798Smav		return;
554232798Smav
555232798Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
556232798Smav	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
557232798Smav		return;
558232798Smav
559232798Smav	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
560232798Smav	if ((w->eld != 0) == ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) != 0))
561232798Smav		return;
562232798Smav	if (w->eld != NULL) {
563232798Smav		w->eld_len = 0;
564232798Smav		free(w->eld, M_HDAA);
565232798Smav		w->eld = NULL;
566232798Smav	}
567232798Smav	HDA_BOOTVERBOSE(
568232798Smav		device_printf(devinfo->dev,
569232798Smav		    "Pin sense: nid=%d sence=0x%08x "
570232798Smav		    "(%sconnected, ELD %svalid)\n",
571232798Smav		    w->nid, res,
572232798Smav		    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ? "" : "dis",
573232798Smav		    (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) ? "" : "in");
574232798Smav	);
575232798Smav	if ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) == 0)
576232798Smav		return;
577232798Smav
578232798Smav	res = hda_command(devinfo->dev,
579232798Smav	    HDA_CMD_GET_HDMI_DIP_SIZE(0, w->nid, 0x08));
580232798Smav	if (res == HDA_INVALID)
581232798Smav		return;
582232798Smav	w->eld_len = res & 0xff;
583232798Smav	if (w->eld_len != 0)
584232798Smav		w->eld = malloc(w->eld_len, M_HDAA, M_ZERO | M_NOWAIT);
585232798Smav	if (w->eld == NULL) {
586232798Smav		w->eld_len = 0;
587232798Smav		return;
588232798Smav	}
589232798Smav
590232798Smav	for (i = 0; i < w->eld_len; i++) {
591232798Smav		res = hda_command(devinfo->dev,
592232798Smav		    HDA_CMD_GET_HDMI_ELDD(0, w->nid, i));
593232798Smav		if (res & 0x80000000)
594232798Smav			w->eld[i] = res & 0xff;
595232798Smav	}
596232798Smav	HDA_BOOTVERBOSE(
597232798Smav		hdaa_eld_dump(w);
598232798Smav	);
599232798Smav}
600232798Smav
601230130Smav/*
602232798Smav * Pin sense initializer.
603230130Smav */
604230130Smavstatic void
605232798Smavhdaa_sense_init(struct hdaa_devinfo *devinfo)
606230130Smav{
607232798Smav	struct hdaa_audio_as *as;
608232798Smav	struct hdaa_widget *w;
609232798Smav	int i, poll = 0;
610230130Smav
611232798Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
612232798Smav		w = hdaa_widget_get(devinfo, i);
613230130Smav		if (w == NULL || w->enable == 0 || w->type !=
614230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
615230130Smav			continue;
616233879Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap)) {
617233879Smav			if (w->unsol < 0)
618233879Smav				w->unsol = HDAC_UNSOL_ALLOC(
619233879Smav				    device_get_parent(devinfo->dev),
620233879Smav				    devinfo->dev, w->nid);
621230130Smav			hda_command(devinfo->dev,
622230130Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid,
623232798Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE | w->unsol));
624232798Smav		}
625232798Smav		as = &devinfo->as[w->bindas];
626232798Smav		if (as->hpredir >= 0 && as->pins[15] == w->nid) {
627232798Smav			if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
628232798Smav			    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0) {
629232798Smav				device_printf(devinfo->dev,
630232798Smav				    "No presence detection support at nid %d\n",
631240986Smav				    w->nid);
632232798Smav			} else {
633232798Smav				if (w->unsol < 0)
634232798Smav					poll = 1;
635232798Smav				HDA_BOOTVERBOSE(
636232798Smav					device_printf(devinfo->dev,
637232798Smav					    "Headphones redirection for "
638232798Smav					    "association %d nid=%d using %s.\n",
639232798Smav					    w->bindas, w->nid,
640240986Smav					    (w->unsol < 0) ? "polling" :
641232798Smav					    "unsolicited responses");
642232798Smav				);
643232798Smav			};
644232798Smav		}
645232798Smav		hdaa_presence_handler(w);
646232798Smav		if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) &&
647232798Smav		    !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
648232798Smav			continue;
649232798Smav		hdaa_eld_handler(w);
650230130Smav	}
651230130Smav	if (poll) {
652230130Smav		callout_reset(&devinfo->poll_jack, 1,
653230130Smav		    hdaa_jack_poll_callback, devinfo);
654230130Smav	}
655230130Smav}
656230130Smav
657230130Smavstatic void
658232798Smavhdaa_sense_deinit(struct hdaa_devinfo *devinfo)
659230130Smav{
660232798Smav	struct hdaa_widget *w;
661232798Smav	int i;
662230130Smav
663232798Smav	callout_stop(&devinfo->poll_jack);
664232798Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
665232798Smav		w = hdaa_widget_get(devinfo, i);
666230130Smav		if (w == NULL || w->enable == 0 || w->type !=
667230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
668230130Smav			continue;
669232798Smav		if (w->unsol < 0)
670232798Smav			continue;
671230130Smav		hda_command(devinfo->dev,
672230130Smav		    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0));
673230130Smav		HDAC_UNSOL_FREE(
674230130Smav		    device_get_parent(devinfo->dev), devinfo->dev,
675232798Smav		    w->unsol);
676232798Smav		w->unsol = -1;
677230130Smav	}
678230130Smav}
679230130Smav
680230130Smavuint32_t
681230130Smavhdaa_widget_pin_patch(uint32_t config, const char *str)
682230130Smav{
683230130Smav	char buf[256];
684230130Smav	char *key, *value, *rest, *bad;
685230130Smav	int ival, i;
686230130Smav
687230130Smav	strlcpy(buf, str, sizeof(buf));
688230130Smav	rest = buf;
689230130Smav	while ((key = strsep(&rest, "=")) != NULL) {
690230130Smav		value = strsep(&rest, " \t");
691230130Smav		if (value == NULL)
692230130Smav			break;
693230130Smav		ival = strtol(value, &bad, 10);
694230130Smav		if (strcmp(key, "seq") == 0) {
695230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK;
696230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_SEQUENCE_SHIFT) &
697230130Smav			    HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK);
698230130Smav		} else if (strcmp(key, "as") == 0) {
699230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK;
700230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT) &
701230130Smav			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK);
702230130Smav		} else if (strcmp(key, "misc") == 0) {
703230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_MISC_MASK;
704230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_MISC_SHIFT) &
705230130Smav			    HDA_CONFIG_DEFAULTCONF_MISC_MASK);
706230130Smav		} else if (strcmp(key, "color") == 0) {
707230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_COLOR_MASK;
708230130Smav			if (bad[0] == 0) {
709230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT) &
710230130Smav				    HDA_CONFIG_DEFAULTCONF_COLOR_MASK);
711230130Smav			};
712230130Smav			for (i = 0; i < 16; i++) {
713230130Smav				if (strcasecmp(HDA_COLORS[i], value) == 0) {
714230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT);
715230130Smav					break;
716230130Smav				}
717230130Smav			}
718230130Smav		} else if (strcmp(key, "ctype") == 0) {
719230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK;
720230130Smav			if (bad[0] == 0) {
721230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT) &
722230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK);
723230130Smav			}
724230130Smav			for (i = 0; i < 16; i++) {
725230130Smav				if (strcasecmp(HDA_CONNECTORS[i], value) == 0) {
726230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT);
727230130Smav					break;
728230130Smav				}
729230130Smav			}
730230130Smav		} else if (strcmp(key, "device") == 0) {
731230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
732230130Smav			if (bad[0] == 0) {
733230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT) &
734230130Smav				    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK);
735230130Smav				continue;
736230130Smav			};
737230130Smav			for (i = 0; i < 16; i++) {
738230130Smav				if (strcasecmp(HDA_DEVS[i], value) == 0) {
739230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT);
740230130Smav					break;
741230130Smav				}
742230130Smav			}
743230130Smav		} else if (strcmp(key, "loc") == 0) {
744230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_LOCATION_MASK;
745230130Smav			if (bad[0] == 0) {
746230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT) &
747230130Smav				    HDA_CONFIG_DEFAULTCONF_LOCATION_MASK);
748230130Smav				continue;
749230130Smav			}
750230130Smav			for (i = 0; i < 64; i++) {
751230130Smav				if (strcasecmp(HDA_LOCS[i], value) == 0) {
752230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT);
753230130Smav					break;
754230130Smav				}
755230130Smav			}
756230130Smav		} else if (strcmp(key, "conn") == 0) {
757230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
758230130Smav			if (bad[0] == 0) {
759230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT) &
760230130Smav				    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
761230130Smav				continue;
762230130Smav			};
763230130Smav			for (i = 0; i < 4; i++) {
764230130Smav				if (strcasecmp(HDA_CONNS[i], value) == 0) {
765230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT);
766230130Smav					break;
767230130Smav				}
768230130Smav			}
769230130Smav		}
770230130Smav	}
771230130Smav	return (config);
772230130Smav}
773230130Smav
774230130Smavuint32_t
775230130Smavhdaa_gpio_patch(uint32_t gpio, const char *str)
776230130Smav{
777230130Smav	char buf[256];
778230130Smav	char *key, *value, *rest;
779230130Smav	int ikey, i;
780230130Smav
781230130Smav	strlcpy(buf, str, sizeof(buf));
782230130Smav	rest = buf;
783230130Smav	while ((key = strsep(&rest, "=")) != NULL) {
784230130Smav		value = strsep(&rest, " \t");
785230130Smav		if (value == NULL)
786230130Smav			break;
787230130Smav		ikey = strtol(key, NULL, 10);
788230130Smav		if (ikey < 0 || ikey > 7)
789230130Smav			continue;
790230130Smav		for (i = 0; i < 7; i++) {
791230130Smav			if (strcasecmp(HDA_GPIO_ACTIONS[i], value) == 0) {
792230130Smav				gpio &= ~HDAA_GPIO_MASK(ikey);
793230130Smav				gpio |= i << HDAA_GPIO_SHIFT(ikey);
794230130Smav				break;
795230130Smav			}
796230130Smav		}
797230130Smav	}
798230130Smav	return (gpio);
799230130Smav}
800230130Smav
801230130Smavstatic void
802230130Smavhdaa_local_patch_pin(struct hdaa_widget *w)
803230130Smav{
804230130Smav	device_t dev = w->devinfo->dev;
805230130Smav	const char *res = NULL;
806230130Smav	uint32_t config, orig;
807230130Smav	char buf[32];
808230130Smav
809230130Smav	config = orig = w->wclass.pin.config;
810230130Smav	snprintf(buf, sizeof(buf), "cad%u.nid%u.config",
811230130Smav	    hda_get_codec_id(dev), w->nid);
812230130Smav	if (resource_string_value(device_get_name(
813230130Smav	    device_get_parent(device_get_parent(dev))),
814230130Smav	    device_get_unit(device_get_parent(device_get_parent(dev))),
815230130Smav	    buf, &res) == 0) {
816230130Smav		if (strncmp(res, "0x", 2) == 0) {
817230130Smav			config = strtol(res + 2, NULL, 16);
818230130Smav		} else {
819230130Smav			config = hdaa_widget_pin_patch(config, res);
820230130Smav		}
821230130Smav	}
822230130Smav	snprintf(buf, sizeof(buf), "nid%u.config", w->nid);
823230130Smav	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
824230130Smav	    buf, &res) == 0) {
825230130Smav		if (strncmp(res, "0x", 2) == 0) {
826230130Smav			config = strtol(res + 2, NULL, 16);
827230130Smav		} else {
828230130Smav			config = hdaa_widget_pin_patch(config, res);
829230130Smav		}
830230130Smav	}
831230130Smav	HDA_BOOTVERBOSE(
832230130Smav		if (config != orig)
833230130Smav			device_printf(w->devinfo->dev,
834230130Smav			    "Patching pin config nid=%u 0x%08x -> 0x%08x\n",
835230130Smav			    w->nid, orig, config);
836230130Smav	);
837230130Smav	w->wclass.pin.newconf = w->wclass.pin.config = config;
838230130Smav}
839230130Smav
840230130Smavstatic int
841230130Smavhdaa_sysctl_config(SYSCTL_HANDLER_ARGS)
842230130Smav{
843230130Smav	char buf[256];
844230130Smav	int error;
845230130Smav	uint32_t conf;
846230130Smav
847230130Smav	conf = *(uint32_t *)oidp->oid_arg1;
848230130Smav	snprintf(buf, sizeof(buf), "0x%08x as=%d seq=%d "
849230130Smav	    "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d",
850230130Smav	    conf,
851230130Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
852230130Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
853230130Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
854230130Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
855230130Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
856230130Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
857230130Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
858230130Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf));
859230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
860230130Smav	if (error != 0 || req->newptr == NULL)
861230130Smav		return (error);
862230130Smav	if (strncmp(buf, "0x", 2) == 0)
863230130Smav		conf = strtol(buf + 2, NULL, 16);
864230130Smav	else
865230130Smav		conf = hdaa_widget_pin_patch(conf, buf);
866230130Smav	*(uint32_t *)oidp->oid_arg1 = conf;
867230130Smav	return (0);
868230130Smav}
869230130Smav
870230130Smavstatic void
871230130Smavhdaa_config_fetch(const char *str, uint32_t *on, uint32_t *off)
872230130Smav{
873230130Smav	int i = 0, j, k, len, inv;
874230130Smav
875230130Smav	for (;;) {
876230130Smav		while (str[i] != '\0' &&
877230130Smav		    (str[i] == ',' || isspace(str[i]) != 0))
878230130Smav			i++;
879230130Smav		if (str[i] == '\0')
880230130Smav			return;
881230130Smav		j = i;
882230130Smav		while (str[j] != '\0' &&
883230130Smav		    !(str[j] == ',' || isspace(str[j]) != 0))
884230130Smav			j++;
885230130Smav		len = j - i;
886230130Smav		if (len > 2 && strncmp(str + i, "no", 2) == 0)
887230130Smav			inv = 2;
888230130Smav		else
889230130Smav			inv = 0;
890264962Smarius		for (k = 0; len > inv && k < nitems(hdaa_quirks_tab); k++) {
891230130Smav			if (strncmp(str + i + inv,
892230130Smav			    hdaa_quirks_tab[k].key, len - inv) != 0)
893230130Smav				continue;
894230130Smav			if (len - inv != strlen(hdaa_quirks_tab[k].key))
895230130Smav				continue;
896230130Smav			if (inv == 0) {
897230130Smav				*on |= hdaa_quirks_tab[k].value;
898230130Smav				*off &= ~hdaa_quirks_tab[k].value;
899230130Smav			} else {
900230130Smav				*off |= hdaa_quirks_tab[k].value;
901230130Smav				*on &= ~hdaa_quirks_tab[k].value;
902230130Smav			}
903230130Smav			break;
904230130Smav		}
905230130Smav		i = j;
906230130Smav	}
907230130Smav}
908230130Smav
909230130Smavstatic int
910230130Smavhdaa_sysctl_quirks(SYSCTL_HANDLER_ARGS)
911230130Smav{
912230130Smav	char buf[256];
913230130Smav	int error, n = 0, i;
914230130Smav	uint32_t quirks, quirks_off;
915230130Smav
916230130Smav	quirks = *(uint32_t *)oidp->oid_arg1;
917230130Smav	buf[0] = 0;
918264962Smarius	for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
919230130Smav		if ((quirks & hdaa_quirks_tab[i].value) != 0)
920230130Smav			n += snprintf(buf + n, sizeof(buf) - n, "%s%s",
921230130Smav			    n != 0 ? "," : "", hdaa_quirks_tab[i].key);
922230130Smav	}
923230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
924230130Smav	if (error != 0 || req->newptr == NULL)
925230130Smav		return (error);
926230130Smav	if (strncmp(buf, "0x", 2) == 0)
927230130Smav		quirks = strtol(buf + 2, NULL, 16);
928230130Smav	else {
929230130Smav		quirks = 0;
930230130Smav		hdaa_config_fetch(buf, &quirks, &quirks_off);
931230130Smav	}
932230130Smav	*(uint32_t *)oidp->oid_arg1 = quirks;
933230130Smav	return (0);
934230130Smav}
935230130Smav
936230130Smavstatic void
937230130Smavhdaa_local_patch(struct hdaa_devinfo *devinfo)
938230130Smav{
939230130Smav	struct hdaa_widget *w;
940230130Smav	const char *res = NULL;
941230130Smav	uint32_t quirks_on = 0, quirks_off = 0, x;
942230130Smav	int i;
943230130Smav
944230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
945230130Smav		w = hdaa_widget_get(devinfo, i);
946230130Smav		if (w == NULL)
947230130Smav			continue;
948230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
949230130Smav			hdaa_local_patch_pin(w);
950230130Smav	}
951230130Smav
952230130Smav	if (resource_string_value(device_get_name(devinfo->dev),
953230130Smav	    device_get_unit(devinfo->dev), "config", &res) == 0) {
954230130Smav		if (res != NULL && strlen(res) > 0)
955230130Smav			hdaa_config_fetch(res, &quirks_on, &quirks_off);
956230130Smav		devinfo->quirks |= quirks_on;
957230130Smav		devinfo->quirks &= ~quirks_off;
958230130Smav	}
959230130Smav	if (devinfo->newquirks == -1)
960230130Smav		devinfo->newquirks = devinfo->quirks;
961230130Smav	else
962230130Smav		devinfo->quirks = devinfo->newquirks;
963230130Smav	HDA_BOOTHVERBOSE(
964230130Smav		device_printf(devinfo->dev,
965230130Smav		    "Config options: 0x%08x\n", devinfo->quirks);
966230130Smav	);
967230130Smav
968230130Smav	if (resource_string_value(device_get_name(devinfo->dev),
969230130Smav	    device_get_unit(devinfo->dev), "gpio_config", &res) == 0) {
970230130Smav		if (strncmp(res, "0x", 2) == 0) {
971230130Smav			devinfo->gpio = strtol(res + 2, NULL, 16);
972230130Smav		} else {
973230130Smav			devinfo->gpio = hdaa_gpio_patch(devinfo->gpio, res);
974230130Smav		}
975230130Smav	}
976230130Smav	if (devinfo->newgpio == -1)
977230130Smav		devinfo->newgpio = devinfo->gpio;
978230130Smav	else
979230130Smav		devinfo->gpio = devinfo->newgpio;
980230130Smav	if (devinfo->newgpo == -1)
981230130Smav		devinfo->newgpo = devinfo->gpo;
982230130Smav	else
983230130Smav		devinfo->gpo = devinfo->newgpo;
984230130Smav	HDA_BOOTHVERBOSE(
985230130Smav		device_printf(devinfo->dev, "GPIO config options:");
986230130Smav		for (i = 0; i < 7; i++) {
987230130Smav			x = (devinfo->gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
988230130Smav			if (x != 0)
989230130Smav				printf(" %d=%s", i, HDA_GPIO_ACTIONS[x]);
990230130Smav		}
991230130Smav		printf("\n");
992230130Smav	);
993230130Smav}
994230130Smav
995230130Smavstatic void
996230130Smavhdaa_widget_connection_parse(struct hdaa_widget *w)
997230130Smav{
998230130Smav	uint32_t res;
999230130Smav	int i, j, max, ents, entnum;
1000230130Smav	nid_t nid = w->nid;
1001230130Smav	nid_t cnid, addcnid, prevcnid;
1002230130Smav
1003230130Smav	w->nconns = 0;
1004230130Smav
1005230130Smav	res = hda_command(w->devinfo->dev,
1006230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_CONN_LIST_LENGTH));
1007230130Smav
1008230130Smav	ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res);
1009230130Smav
1010230130Smav	if (ents < 1)
1011230130Smav		return;
1012230130Smav
1013230130Smav	entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4;
1014230130Smav	max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1;
1015230130Smav	prevcnid = 0;
1016230130Smav
1017230130Smav#define CONN_RMASK(e)		(1 << ((32 / (e)) - 1))
1018230130Smav#define CONN_NMASK(e)		(CONN_RMASK(e) - 1)
1019230130Smav#define CONN_RESVAL(r, e, n)	((r) >> ((32 / (e)) * (n)))
1020230130Smav#define CONN_RANGE(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_RMASK(e))
1021230130Smav#define CONN_CNID(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_NMASK(e))
1022230130Smav
1023230130Smav	for (i = 0; i < ents; i += entnum) {
1024230130Smav		res = hda_command(w->devinfo->dev,
1025230130Smav		    HDA_CMD_GET_CONN_LIST_ENTRY(0, nid, i));
1026230130Smav		for (j = 0; j < entnum; j++) {
1027230130Smav			cnid = CONN_CNID(res, entnum, j);
1028230130Smav			if (cnid == 0) {
1029230130Smav				if (w->nconns < ents)
1030230130Smav					device_printf(w->devinfo->dev,
1031230130Smav					    "WARNING: nid=%d has zero cnid "
1032230130Smav					    "entnum=%d j=%d index=%d "
1033230130Smav					    "entries=%d found=%d res=0x%08x\n",
1034230130Smav					    nid, entnum, j, i,
1035230130Smav					    ents, w->nconns, res);
1036230130Smav				else
1037230130Smav					goto getconns_out;
1038230130Smav			}
1039230130Smav			if (cnid < w->devinfo->startnode ||
1040230130Smav			    cnid >= w->devinfo->endnode) {
1041230130Smav				HDA_BOOTVERBOSE(
1042230130Smav					device_printf(w->devinfo->dev,
1043230130Smav					    "WARNING: nid=%d has cnid outside "
1044230130Smav					    "of the AFG range j=%d "
1045230130Smav					    "entnum=%d index=%d res=0x%08x\n",
1046230130Smav					    nid, j, entnum, i, res);
1047230130Smav				);
1048230130Smav			}
1049230130Smav			if (CONN_RANGE(res, entnum, j) == 0)
1050230130Smav				addcnid = cnid;
1051230130Smav			else if (prevcnid == 0 || prevcnid >= cnid) {
1052230130Smav				device_printf(w->devinfo->dev,
1053230130Smav				    "WARNING: Invalid child range "
1054230130Smav				    "nid=%d index=%d j=%d entnum=%d "
1055230130Smav				    "prevcnid=%d cnid=%d res=0x%08x\n",
1056230130Smav				    nid, i, j, entnum, prevcnid,
1057230130Smav				    cnid, res);
1058230130Smav				addcnid = cnid;
1059230130Smav			} else
1060230130Smav				addcnid = prevcnid + 1;
1061230130Smav			while (addcnid <= cnid) {
1062230130Smav				if (w->nconns > max) {
1063230130Smav					device_printf(w->devinfo->dev,
1064230130Smav					    "Adding %d (nid=%d): "
1065230130Smav					    "Max connection reached! max=%d\n",
1066230130Smav					    addcnid, nid, max + 1);
1067230130Smav					goto getconns_out;
1068230130Smav				}
1069230130Smav				w->connsenable[w->nconns] = 1;
1070230130Smav				w->conns[w->nconns++] = addcnid++;
1071230130Smav			}
1072230130Smav			prevcnid = cnid;
1073230130Smav		}
1074230130Smav	}
1075230130Smav
1076230130Smavgetconns_out:
1077230130Smav	return;
1078230130Smav}
1079230130Smav
1080230130Smavstatic void
1081230130Smavhdaa_widget_parse(struct hdaa_widget *w)
1082230130Smav{
1083230130Smav	device_t dev = w->devinfo->dev;
1084230130Smav	uint32_t wcap, cap;
1085230130Smav	nid_t nid = w->nid;
1086230130Smav	char buf[64];
1087230130Smav
1088230130Smav	w->param.widget_cap = wcap = hda_command(dev,
1089230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_AUDIO_WIDGET_CAP));
1090230130Smav	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap);
1091230130Smav
1092230130Smav	hdaa_widget_connection_parse(w);
1093230130Smav
1094230130Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) {
1095230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1096230130Smav			w->param.outamp_cap =
1097230130Smav			    hda_command(dev,
1098230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1099230130Smav			    HDA_PARAM_OUTPUT_AMP_CAP));
1100230130Smav		else
1101230130Smav			w->param.outamp_cap =
1102230130Smav			    w->devinfo->outamp_cap;
1103230130Smav	} else
1104230130Smav		w->param.outamp_cap = 0;
1105230130Smav
1106230130Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) {
1107230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1108230130Smav			w->param.inamp_cap =
1109230130Smav			    hda_command(dev,
1110230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1111230130Smav			    HDA_PARAM_INPUT_AMP_CAP));
1112230130Smav		else
1113230130Smav			w->param.inamp_cap =
1114230130Smav			    w->devinfo->inamp_cap;
1115230130Smav	} else
1116230130Smav		w->param.inamp_cap = 0;
1117230130Smav
1118230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1119230130Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1120230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) {
1121230130Smav			cap = hda_command(dev,
1122230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1123230130Smav			    HDA_PARAM_SUPP_STREAM_FORMATS));
1124230130Smav			w->param.supp_stream_formats = (cap != 0) ? cap :
1125230130Smav			    w->devinfo->supp_stream_formats;
1126230130Smav			cap = hda_command(dev,
1127230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1128230130Smav			    HDA_PARAM_SUPP_PCM_SIZE_RATE));
1129230130Smav			w->param.supp_pcm_size_rate = (cap != 0) ? cap :
1130230130Smav			    w->devinfo->supp_pcm_size_rate;
1131230130Smav		} else {
1132230130Smav			w->param.supp_stream_formats =
1133230130Smav			    w->devinfo->supp_stream_formats;
1134230130Smav			w->param.supp_pcm_size_rate =
1135230130Smav			    w->devinfo->supp_pcm_size_rate;
1136230130Smav		}
1137232798Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1138232798Smav			w->wclass.conv.stripecap = hda_command(dev,
1139232798Smav			    HDA_CMD_GET_STRIPE_CONTROL(0, w->nid)) >> 20;
1140232798Smav		} else
1141232798Smav			w->wclass.conv.stripecap = 1;
1142230130Smav	} else {
1143230130Smav		w->param.supp_stream_formats = 0;
1144230130Smav		w->param.supp_pcm_size_rate = 0;
1145230130Smav	}
1146230130Smav
1147230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1148230130Smav		w->wclass.pin.original = w->wclass.pin.newconf =
1149230130Smav		    w->wclass.pin.config = hda_command(dev,
1150230130Smav			HDA_CMD_GET_CONFIGURATION_DEFAULT(0, w->nid));
1151230130Smav		w->wclass.pin.cap = hda_command(dev,
1152242544Seadler		    HDA_CMD_GET_PARAMETER(0, w->nid, HDA_PARAM_PIN_CAP));
1153230130Smav		w->wclass.pin.ctrl = hda_command(dev,
1154230130Smav		    HDA_CMD_GET_PIN_WIDGET_CTRL(0, nid));
1155243062Smav		w->wclass.pin.connected = 2;
1156230130Smav		if (HDA_PARAM_PIN_CAP_EAPD_CAP(w->wclass.pin.cap)) {
1157230130Smav			w->param.eapdbtl = hda_command(dev,
1158230130Smav			    HDA_CMD_GET_EAPD_BTL_ENABLE(0, nid));
1159230130Smav			w->param.eapdbtl &= 0x7;
1160230130Smav			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
1161230130Smav		} else
1162230130Smav			w->param.eapdbtl = HDA_INVALID;
1163230130Smav
1164230130Smav		hdaa_unlock(w->devinfo);
1165230130Smav		snprintf(buf, sizeof(buf), "nid%d_config", w->nid);
1166230130Smav		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1167230130Smav		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1168230130Smav		    buf, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1169230130Smav		    &w->wclass.pin.newconf, sizeof(&w->wclass.pin.newconf),
1170230130Smav		    hdaa_sysctl_config, "A", "Current pin configuration");
1171230130Smav		snprintf(buf, sizeof(buf), "nid%d_original", w->nid);
1172230130Smav		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1173230130Smav		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1174230130Smav		    buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1175230130Smav		    &w->wclass.pin.original, sizeof(&w->wclass.pin.original),
1176230130Smav		    hdaa_sysctl_config, "A", "Original pin configuration");
1177230130Smav		hdaa_lock(w->devinfo);
1178230130Smav	}
1179232798Smav	w->unsol = -1;
1180230130Smav}
1181230130Smav
1182230130Smavstatic void
1183230130Smavhdaa_widget_postprocess(struct hdaa_widget *w)
1184230130Smav{
1185264962Smarius	const char *typestr;
1186230130Smav
1187230130Smav	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(w->param.widget_cap);
1188230130Smav	switch (w->type) {
1189230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
1190230130Smav		typestr = "audio output";
1191230130Smav		break;
1192230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
1193230130Smav		typestr = "audio input";
1194230130Smav		break;
1195230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
1196230130Smav		typestr = "audio mixer";
1197230130Smav		break;
1198230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
1199230130Smav		typestr = "audio selector";
1200230130Smav		break;
1201230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
1202230130Smav		typestr = "pin";
1203230130Smav		break;
1204230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET:
1205230130Smav		typestr = "power widget";
1206230130Smav		break;
1207230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET:
1208230130Smav		typestr = "volume widget";
1209230130Smav		break;
1210230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
1211230130Smav		typestr = "beep widget";
1212230130Smav		break;
1213230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET:
1214230130Smav		typestr = "vendor widget";
1215230130Smav		break;
1216230130Smav	default:
1217230130Smav		typestr = "unknown type";
1218230130Smav		break;
1219230130Smav	}
1220230130Smav	strlcpy(w->name, typestr, sizeof(w->name));
1221230130Smav
1222230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1223230130Smav		uint32_t config;
1224230130Smav		const char *devstr;
1225230130Smav		int conn, color;
1226230130Smav
1227230130Smav		config = w->wclass.pin.config;
1228230130Smav		devstr = HDA_DEVS[(config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) >>
1229230130Smav		    HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT];
1230230130Smav		conn = (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) >>
1231230130Smav		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT;
1232230130Smav		color = (config & HDA_CONFIG_DEFAULTCONF_COLOR_MASK) >>
1233230130Smav		    HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT;
1234230130Smav		strlcat(w->name, ": ", sizeof(w->name));
1235230130Smav		strlcat(w->name, devstr, sizeof(w->name));
1236230130Smav		strlcat(w->name, " (", sizeof(w->name));
1237230130Smav		if (conn == 0 && color != 0 && color != 15) {
1238230130Smav			strlcat(w->name, HDA_COLORS[color], sizeof(w->name));
1239230130Smav			strlcat(w->name, " ", sizeof(w->name));
1240230130Smav		}
1241230130Smav		strlcat(w->name, HDA_CONNS[conn], sizeof(w->name));
1242230130Smav		strlcat(w->name, ")", sizeof(w->name));
1243230130Smav	}
1244230130Smav}
1245230130Smav
1246230130Smavstruct hdaa_widget *
1247230130Smavhdaa_widget_get(struct hdaa_devinfo *devinfo, nid_t nid)
1248230130Smav{
1249230130Smav	if (devinfo == NULL || devinfo->widget == NULL ||
1250230130Smav		    nid < devinfo->startnode || nid >= devinfo->endnode)
1251230130Smav		return (NULL);
1252230130Smav	return (&devinfo->widget[nid - devinfo->startnode]);
1253230130Smav}
1254230130Smav
1255230130Smavstatic void
1256230130Smavhdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *devinfo, nid_t nid,
1257230130Smav					int index, int lmute, int rmute,
1258230130Smav					int left, int right, int dir)
1259230130Smav{
1260230130Smav	uint16_t v = 0;
1261230130Smav
1262230130Smav	HDA_BOOTHVERBOSE(
1263230130Smav		device_printf(devinfo->dev,
1264230130Smav		    "Setting amplifier nid=%d index=%d %s mute=%d/%d vol=%d/%d\n",
1265230130Smav		    nid,index,dir ? "in" : "out",lmute,rmute,left,right);
1266230130Smav	);
1267230130Smav	if (left != right || lmute != rmute) {
1268230130Smav		v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
1269230130Smav		    (lmute << 7) | left;
1270230130Smav		hda_command(devinfo->dev,
1271230130Smav		    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1272230130Smav		v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
1273230130Smav		    (rmute << 7) | right;
1274230130Smav	} else
1275230130Smav		v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
1276230130Smav		    (lmute << 7) | left;
1277230130Smav
1278230130Smav	hda_command(devinfo->dev,
1279230130Smav	    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1280230130Smav}
1281230130Smav
1282230130Smavstatic void
1283230130Smavhdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *ctl, uint32_t mute,
1284230130Smav						int left, int right)
1285230130Smav{
1286230130Smav	nid_t nid;
1287230130Smav	int lmute, rmute;
1288230130Smav
1289230130Smav	nid = ctl->widget->nid;
1290230130Smav
1291230130Smav	/* Save new values if valid. */
1292230130Smav	if (mute != HDAA_AMP_MUTE_DEFAULT)
1293230130Smav		ctl->muted = mute;
1294230130Smav	if (left != HDAA_AMP_VOL_DEFAULT)
1295230130Smav		ctl->left = left;
1296230130Smav	if (right != HDAA_AMP_VOL_DEFAULT)
1297230130Smav		ctl->right = right;
1298230130Smav	/* Prepare effective values */
1299230130Smav	if (ctl->forcemute) {
1300230130Smav		lmute = 1;
1301230130Smav		rmute = 1;
1302230130Smav		left = 0;
1303230130Smav		right = 0;
1304230130Smav	} else {
1305230130Smav		lmute = HDAA_AMP_LEFT_MUTED(ctl->muted);
1306230130Smav		rmute = HDAA_AMP_RIGHT_MUTED(ctl->muted);
1307230130Smav		left = ctl->left;
1308230130Smav		right = ctl->right;
1309230130Smav	}
1310230130Smav	/* Apply effective values */
1311230130Smav	if (ctl->dir & HDAA_CTL_OUT)
1312230130Smav		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1313230130Smav		    lmute, rmute, left, right, 0);
1314230130Smav	if (ctl->dir & HDAA_CTL_IN)
1315230130Smav		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1316230130Smav		    lmute, rmute, left, right, 1);
1317230130Smav}
1318230130Smav
1319230130Smavstatic void
1320230130Smavhdaa_widget_connection_select(struct hdaa_widget *w, uint8_t index)
1321230130Smav{
1322230130Smav	if (w == NULL || w->nconns < 1 || index > (w->nconns - 1))
1323230130Smav		return;
1324230130Smav	HDA_BOOTHVERBOSE(
1325230130Smav		device_printf(w->devinfo->dev,
1326230130Smav		    "Setting selector nid=%d index=%d\n", w->nid, index);
1327230130Smav	);
1328230130Smav	hda_command(w->devinfo->dev,
1329230130Smav	    HDA_CMD_SET_CONNECTION_SELECT_CONTROL(0, w->nid, index));
1330230130Smav	w->selconn = index;
1331230130Smav}
1332230130Smav
1333230130Smav/****************************************************************************
1334230130Smav * Device Methods
1335230130Smav ****************************************************************************/
1336230130Smav
1337230130Smavstatic void *
1338230130Smavhdaa_channel_init(kobj_t obj, void *data, struct snd_dbuf *b,
1339230130Smav					struct pcm_channel *c, int dir)
1340230130Smav{
1341230130Smav	struct hdaa_chan *ch = data;
1342230130Smav	struct hdaa_pcm_devinfo *pdevinfo = ch->pdevinfo;
1343230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1344230130Smav
1345230130Smav	hdaa_lock(devinfo);
1346230130Smav	if (devinfo->quirks & HDAA_QUIRK_FIXEDRATE) {
1347230130Smav		ch->caps.minspeed = ch->caps.maxspeed = 48000;
1348230130Smav		ch->pcmrates[0] = 48000;
1349230130Smav		ch->pcmrates[1] = 0;
1350230130Smav	}
1351230130Smav	ch->dir = dir;
1352230130Smav	ch->b = b;
1353230130Smav	ch->c = c;
1354230130Smav	ch->blksz = pdevinfo->chan_size / pdevinfo->chan_blkcnt;
1355230130Smav	ch->blkcnt = pdevinfo->chan_blkcnt;
1356230130Smav	hdaa_unlock(devinfo);
1357230130Smav
1358230130Smav	if (sndbuf_alloc(ch->b, bus_get_dma_tag(devinfo->dev),
1359230130Smav	    hda_get_dma_nocache(devinfo->dev) ? BUS_DMA_NOCACHE : 0,
1360230130Smav	    pdevinfo->chan_size) != 0)
1361230130Smav		return (NULL);
1362230130Smav
1363230130Smav	return (ch);
1364230130Smav}
1365230130Smav
1366230130Smavstatic int
1367230130Smavhdaa_channel_setformat(kobj_t obj, void *data, uint32_t format)
1368230130Smav{
1369230130Smav	struct hdaa_chan *ch = data;
1370230130Smav	int i;
1371230130Smav
1372230130Smav	for (i = 0; ch->caps.fmtlist[i] != 0; i++) {
1373230130Smav		if (format == ch->caps.fmtlist[i]) {
1374230130Smav			ch->fmt = format;
1375230130Smav			return (0);
1376230130Smav		}
1377230130Smav	}
1378230130Smav
1379230130Smav	return (EINVAL);
1380230130Smav}
1381230130Smav
1382230130Smavstatic uint32_t
1383230130Smavhdaa_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
1384230130Smav{
1385230130Smav	struct hdaa_chan *ch = data;
1386230130Smav	uint32_t spd = 0, threshold;
1387230130Smav	int i;
1388230130Smav
1389230130Smav	/* First look for equal or multiple frequency. */
1390230130Smav	for (i = 0; ch->pcmrates[i] != 0; i++) {
1391230130Smav		spd = ch->pcmrates[i];
1392230130Smav		if (speed != 0 && spd / speed * speed == spd) {
1393230130Smav			ch->spd = spd;
1394230130Smav			return (spd);
1395230130Smav		}
1396230130Smav	}
1397230130Smav	/* If no match, just find nearest. */
1398230130Smav	for (i = 0; ch->pcmrates[i] != 0; i++) {
1399230130Smav		spd = ch->pcmrates[i];
1400230130Smav		threshold = spd + ((ch->pcmrates[i + 1] != 0) ?
1401230130Smav		    ((ch->pcmrates[i + 1] - spd) >> 1) : 0);
1402230130Smav		if (speed < threshold)
1403230130Smav			break;
1404230130Smav	}
1405230130Smav	ch->spd = spd;
1406230130Smav	return (spd);
1407230130Smav}
1408230130Smav
1409230130Smavstatic uint16_t
1410230130Smavhdaa_stream_format(struct hdaa_chan *ch)
1411230130Smav{
1412230130Smav	int i;
1413230130Smav	uint16_t fmt;
1414230130Smav
1415230130Smav	fmt = 0;
1416230130Smav	if (ch->fmt & AFMT_S16_LE)
1417230130Smav		fmt |= ch->bit16 << 4;
1418230130Smav	else if (ch->fmt & AFMT_S32_LE)
1419230130Smav		fmt |= ch->bit32 << 4;
1420230130Smav	else
1421230130Smav		fmt |= 1 << 4;
1422230130Smav	for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
1423230130Smav		if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
1424230130Smav			fmt |= hda_rate_tab[i].base;
1425230130Smav			fmt |= hda_rate_tab[i].mul;
1426230130Smav			fmt |= hda_rate_tab[i].div;
1427230130Smav			break;
1428230130Smav		}
1429230130Smav	}
1430230130Smav	fmt |= (AFMT_CHANNEL(ch->fmt) - 1);
1431230130Smav
1432230130Smav	return (fmt);
1433230130Smav}
1434230130Smav
1435232798Smavstatic int
1436232798Smavhdaa_allowed_stripes(uint16_t fmt)
1437232798Smav{
1438232798Smav	static const int bits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1439232798Smav	int size;
1440232798Smav
1441232798Smav	size = bits[(fmt >> 4) & 0x03];
1442232798Smav	size *= (fmt & 0x0f) + 1;
1443232798Smav	size *= ((fmt >> 11) & 0x07) + 1;
1444232798Smav	return (0xffffffffU >> (32 - fls(size / 8)));
1445232798Smav}
1446232798Smav
1447230130Smavstatic void
1448230130Smavhdaa_audio_setup(struct hdaa_chan *ch)
1449230130Smav{
1450230130Smav	struct hdaa_audio_as *as = &ch->devinfo->as[ch->as];
1451232798Smav	struct hdaa_widget *w, *wp;
1452232798Smav	int i, j, k, chn, cchn, totalchn, totalextchn, c;
1453230130Smav	uint16_t fmt, dfmt;
1454232798Smav	/* Mapping channel pairs to codec pins/converters. */
1455232798Smav	const static uint16_t convmap[2][5] =
1456232798Smav	    {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x0231 }, /* 5.1 */
1457232798Smav	     { 0x0010, 0x0001, 0x2001, 0x2031, 0x2431 }};/* 7.1 */
1458232798Smav	/* Mapping formats to HDMI channel allocations. */
1459232798Smav	const static uint8_t hdmica[2][8] =
1460232798Smav	    {{ 0x02, 0x00, 0x04, 0x08, 0x0a, 0x0e, 0x12, 0x12 }, /* x.0 */
1461232798Smav	     { 0x01, 0x03, 0x01, 0x03, 0x09, 0x0b, 0x0f, 0x13 }}; /* x.1 */
1462232798Smav	/* Mapping formats to HDMI channels order. */
1463232798Smav	const static uint32_t hdmich[2][8] =
1464232798Smav	    {{ 0xFFFF0F00, 0xFFFFFF10, 0xFFF2FF10, 0xFF32FF10,
1465232798Smav	       0xFF324F10, 0xF5324F10, 0x54326F10, 0x54326F10 }, /* x.0 */
1466232798Smav	     { 0xFFFFF000, 0xFFFF0100, 0xFFFFF210, 0xFFFF2310,
1467232798Smav	       0xFF32F410, 0xFF324510, 0xF6324510, 0x76325410 }}; /* x.1 */
1468232798Smav	int convmapid = -1;
1469232798Smav	nid_t nid;
1470232798Smav	uint8_t csum;
1471230130Smav
1472230130Smav	totalchn = AFMT_CHANNEL(ch->fmt);
1473232798Smav	totalextchn = AFMT_EXTCHANNEL(ch->fmt);
1474230130Smav	HDA_BOOTHVERBOSE(
1475230130Smav		device_printf(ch->pdevinfo->dev,
1476232798Smav		    "PCMDIR_%s: Stream setup fmt=%08x (%d.%d) speed=%d\n",
1477230130Smav		    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1478232798Smav		    ch->fmt, totalchn - totalextchn, totalextchn, ch->spd);
1479230130Smav	);
1480230130Smav	fmt = hdaa_stream_format(ch);
1481230130Smav
1482232798Smav	/* Set channels to I/O converters mapping for known speaker setups. */
1483230130Smav	if ((as->pinset == 0x0007 || as->pinset == 0x0013)) /* Standard 5.1 */
1484232798Smav		convmapid = 0;
1485230130Smav	else if (as->pinset == 0x0017) /* Standard 7.1 */
1486232798Smav		convmapid = 1;
1487230130Smav
1488230130Smav	dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN;
1489230130Smav	if (ch->fmt & AFMT_AC3)
1490230130Smav		dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO;
1491230130Smav
1492230130Smav	chn = 0;
1493230130Smav	for (i = 0; ch->io[i] != -1; i++) {
1494230130Smav		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1495230130Smav		if (w == NULL)
1496230130Smav			continue;
1497230130Smav
1498230130Smav		/* If HP redirection is enabled, but failed to use same
1499230130Smav		   DAC, make last DAC to duplicate first one. */
1500230130Smav		if (as->fakeredir && i == (as->pincnt - 1)) {
1501230130Smav			c = (ch->sid << 4);
1502230130Smav		} else {
1503232798Smav			/* Map channels to I/O converters, if set. */
1504232798Smav			if (convmapid >= 0)
1505232798Smav				chn = (((convmap[convmapid][totalchn / 2]
1506232798Smav				    >> i * 4) & 0xf) - 1) * 2;
1507230130Smav			if (chn < 0 || chn >= totalchn) {
1508230130Smav				c = 0;
1509230130Smav			} else {
1510230130Smav				c = (ch->sid << 4) | chn;
1511230130Smav			}
1512230130Smav		}
1513230130Smav		hda_command(ch->devinfo->dev,
1514230130Smav		    HDA_CMD_SET_CONV_FMT(0, ch->io[i], fmt));
1515230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1516230130Smav			hda_command(ch->devinfo->dev,
1517230130Smav			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], dfmt));
1518230130Smav		}
1519230130Smav		hda_command(ch->devinfo->dev,
1520230130Smav		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i], c));
1521232798Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1522232798Smav			hda_command(ch->devinfo->dev,
1523232798Smav			    HDA_CMD_SET_STRIPE_CONTROL(0, w->nid, ch->stripectl));
1524232798Smav		}
1525232798Smav		cchn = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1526232798Smav		if (cchn > 1 && chn < totalchn) {
1527232798Smav			cchn = min(cchn, totalchn - chn - 1);
1528232798Smav			hda_command(ch->devinfo->dev,
1529232798Smav			    HDA_CMD_SET_CONV_CHAN_COUNT(0, ch->io[i], cchn));
1530232798Smav		}
1531232798Smav		HDA_BOOTHVERBOSE(
1532232798Smav			device_printf(ch->pdevinfo->dev,
1533232798Smav			    "PCMDIR_%s: Stream setup nid=%d: "
1534232798Smav			    "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x, "
1535232798Smav			    "chan_count=0x%02x, stripe=%d\n",
1536232798Smav			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1537232798Smav			    ch->io[i], fmt, dfmt, c, cchn, ch->stripectl);
1538232798Smav		);
1539232798Smav		for (j = 0; j < 16; j++) {
1540232798Smav			if (as->dacs[ch->asindex][j] != ch->io[i])
1541232798Smav				continue;
1542232798Smav			nid = as->pins[j];
1543232798Smav			wp = hdaa_widget_get(ch->devinfo, nid);
1544232798Smav			if (wp == NULL)
1545232798Smav				continue;
1546232798Smav			if (!HDA_PARAM_PIN_CAP_DP(wp->wclass.pin.cap) &&
1547232798Smav			    !HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap))
1548232798Smav				continue;
1549232798Smav
1550232798Smav			/* Set channel mapping. */
1551232798Smav			for (k = 0; k < 8; k++) {
1552232798Smav				hda_command(ch->devinfo->dev,
1553232798Smav				    HDA_CMD_SET_HDMI_CHAN_SLOT(0, nid,
1554232798Smav				    (((hdmich[totalextchn == 0 ? 0 : 1][totalchn - 1]
1555232798Smav				     >> (k * 4)) & 0xf) << 4) | k));
1556232798Smav			}
1557232798Smav
1558232798Smav			/*
1559232798Smav			 * Enable High Bit Rate (HBR) Encoded Packet Type
1560232798Smav			 * (EPT), if supported and needed (8ch data).
1561232798Smav			 */
1562232798Smav			if (HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap) &&
1563232798Smav			    HDA_PARAM_PIN_CAP_HBR(wp->wclass.pin.cap)) {
1564232798Smav				wp->wclass.pin.ctrl &=
1565232798Smav				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK;
1566232798Smav				if ((ch->fmt & AFMT_AC3) && (cchn == 7))
1567232798Smav					wp->wclass.pin.ctrl |= 0x03;
1568232798Smav				hda_command(ch->devinfo->dev,
1569232798Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL(0, nid,
1570232798Smav				    wp->wclass.pin.ctrl));
1571232798Smav			}
1572232798Smav
1573232798Smav			/* Stop audio infoframe transmission. */
1574232798Smav			hda_command(ch->devinfo->dev,
1575232798Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1576232798Smav			hda_command(ch->devinfo->dev,
1577232798Smav			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0x00));
1578232798Smav
1579232798Smav			/* Clear audio infoframe buffer. */
1580232798Smav			hda_command(ch->devinfo->dev,
1581232798Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1582232798Smav			for (k = 0; k < 32; k++)
1583232798Smav				hda_command(ch->devinfo->dev,
1584232798Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1585232798Smav
1586232798Smav			/* Write HDMI/DisplayPort audio infoframe. */
1587232798Smav			hda_command(ch->devinfo->dev,
1588232798Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1589232798Smav			if (w->eld != NULL && w->eld_len >= 6 &&
1590232798Smav			    ((w->eld[5] >> 2) & 0x3) == 1) { /* DisplayPort */
1591232798Smav				hda_command(ch->devinfo->dev,
1592232798Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1593232798Smav				hda_command(ch->devinfo->dev,
1594232798Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x1b));
1595232798Smav				hda_command(ch->devinfo->dev,
1596232798Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x44));
1597232798Smav			} else {	/* HDMI */
1598232798Smav				hda_command(ch->devinfo->dev,
1599232798Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1600232798Smav				hda_command(ch->devinfo->dev,
1601232798Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x01));
1602232798Smav				hda_command(ch->devinfo->dev,
1603232798Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x0a));
1604232798Smav				csum = 0;
1605232798Smav				csum -= 0x84 + 0x01 + 0x0a + (totalchn - 1) +
1606232798Smav				    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1];
1607232798Smav				hda_command(ch->devinfo->dev,
1608232798Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, csum));
1609232798Smav			}
1610232798Smav			hda_command(ch->devinfo->dev,
1611232798Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, totalchn - 1));
1612232798Smav			hda_command(ch->devinfo->dev,
1613232798Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1614232798Smav			hda_command(ch->devinfo->dev,
1615232798Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1616232798Smav			hda_command(ch->devinfo->dev,
1617232798Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid,
1618232798Smav			    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1]));
1619232798Smav
1620232798Smav			/* Start audio infoframe transmission. */
1621232798Smav			hda_command(ch->devinfo->dev,
1622232798Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1623232798Smav			hda_command(ch->devinfo->dev,
1624232798Smav			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0xc0));
1625232798Smav		}
1626232798Smav		chn += cchn + 1;
1627230130Smav	}
1628230130Smav}
1629230130Smav
1630230130Smav/*
1631230130Smav * Greatest Common Divisor.
1632230130Smav */
1633230130Smavstatic unsigned
1634230130Smavgcd(unsigned a, unsigned b)
1635230130Smav{
1636230130Smav	u_int c;
1637230130Smav
1638230130Smav	while (b != 0) {
1639230130Smav		c = a;
1640230130Smav		a = b;
1641230130Smav		b = (c % b);
1642230130Smav	}
1643230130Smav	return (a);
1644230130Smav}
1645230130Smav
1646230130Smav/*
1647230130Smav * Least Common Multiple.
1648230130Smav */
1649230130Smavstatic unsigned
1650230130Smavlcm(unsigned a, unsigned b)
1651230130Smav{
1652230130Smav
1653230130Smav	return ((a * b) / gcd(a, b));
1654230130Smav}
1655230130Smav
1656230130Smavstatic int
1657230130Smavhdaa_channel_setfragments(kobj_t obj, void *data,
1658230130Smav					uint32_t blksz, uint32_t blkcnt)
1659230130Smav{
1660230130Smav	struct hdaa_chan *ch = data;
1661230130Smav
1662230130Smav	blksz -= blksz % lcm(HDA_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
1663230130Smav
1664230130Smav	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
1665230130Smav		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
1666230130Smav	if (blksz < HDA_BLK_MIN)
1667230130Smav		blksz = HDA_BLK_MIN;
1668230130Smav	if (blkcnt > HDA_BDL_MAX)
1669230130Smav		blkcnt = HDA_BDL_MAX;
1670230130Smav	if (blkcnt < HDA_BDL_MIN)
1671230130Smav		blkcnt = HDA_BDL_MIN;
1672230130Smav
1673230130Smav	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
1674230130Smav		if ((blkcnt >> 1) >= HDA_BDL_MIN)
1675230130Smav			blkcnt >>= 1;
1676230130Smav		else if ((blksz >> 1) >= HDA_BLK_MIN)
1677230130Smav			blksz >>= 1;
1678230130Smav		else
1679230130Smav			break;
1680230130Smav	}
1681230130Smav
1682230130Smav	if ((sndbuf_getblksz(ch->b) != blksz ||
1683230130Smav	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
1684230130Smav	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
1685230130Smav		device_printf(ch->devinfo->dev, "%s: failed blksz=%u blkcnt=%u\n",
1686230130Smav		    __func__, blksz, blkcnt);
1687230130Smav
1688230130Smav	ch->blksz = sndbuf_getblksz(ch->b);
1689230130Smav	ch->blkcnt = sndbuf_getblkcnt(ch->b);
1690230130Smav
1691230130Smav	return (0);
1692230130Smav}
1693230130Smav
1694230130Smavstatic uint32_t
1695230130Smavhdaa_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
1696230130Smav{
1697230130Smav	struct hdaa_chan *ch = data;
1698230130Smav
1699230130Smav	hdaa_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
1700230130Smav
1701230130Smav	return (ch->blksz);
1702230130Smav}
1703230130Smav
1704230130Smavstatic void
1705230130Smavhdaa_channel_stop(struct hdaa_chan *ch)
1706230130Smav{
1707230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
1708230130Smav	struct hdaa_widget *w;
1709230130Smav	int i;
1710230130Smav
1711232798Smav	if ((ch->flags & HDAA_CHN_RUNNING) == 0)
1712232798Smav		return;
1713230130Smav	ch->flags &= ~HDAA_CHN_RUNNING;
1714230130Smav	HDAC_STREAM_STOP(device_get_parent(devinfo->dev), devinfo->dev,
1715230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1716230130Smav	for (i = 0; ch->io[i] != -1; i++) {
1717230130Smav		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1718230130Smav		if (w == NULL)
1719230130Smav			continue;
1720230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1721230130Smav			hda_command(devinfo->dev,
1722230130Smav			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], 0));
1723230130Smav		}
1724230130Smav		hda_command(devinfo->dev,
1725230130Smav		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i],
1726230130Smav		    0));
1727230130Smav	}
1728230130Smav	HDAC_STREAM_FREE(device_get_parent(devinfo->dev), devinfo->dev,
1729230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1730230130Smav}
1731230130Smav
1732230130Smavstatic int
1733230130Smavhdaa_channel_start(struct hdaa_chan *ch)
1734230130Smav{
1735230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
1736232798Smav	uint32_t fmt;
1737230130Smav
1738232798Smav	fmt = hdaa_stream_format(ch);
1739232798Smav	ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt)) - 1;
1740230130Smav	ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev,
1741232798Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos);
1742230130Smav	if (ch->sid <= 0)
1743230130Smav		return (EBUSY);
1744230130Smav	hdaa_audio_setup(ch);
1745230130Smav	HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev,
1746230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1747230130Smav	HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev,
1748230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid,
1749230130Smav	    sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt);
1750230130Smav	ch->flags |= HDAA_CHN_RUNNING;
1751230130Smav	return (0);
1752230130Smav}
1753230130Smav
1754230130Smavstatic int
1755230130Smavhdaa_channel_trigger(kobj_t obj, void *data, int go)
1756230130Smav{
1757230130Smav	struct hdaa_chan *ch = data;
1758230130Smav	int error = 0;
1759230130Smav
1760230130Smav	if (!PCMTRIG_COMMON(go))
1761230130Smav		return (0);
1762230130Smav
1763230130Smav	hdaa_lock(ch->devinfo);
1764230130Smav	switch (go) {
1765230130Smav	case PCMTRIG_START:
1766230130Smav		error = hdaa_channel_start(ch);
1767230130Smav		break;
1768230130Smav	case PCMTRIG_STOP:
1769230130Smav	case PCMTRIG_ABORT:
1770230130Smav		hdaa_channel_stop(ch);
1771230130Smav		break;
1772230130Smav	default:
1773230130Smav		break;
1774230130Smav	}
1775230130Smav	hdaa_unlock(ch->devinfo);
1776230130Smav
1777230130Smav	return (error);
1778230130Smav}
1779230130Smav
1780230130Smavstatic uint32_t
1781230130Smavhdaa_channel_getptr(kobj_t obj, void *data)
1782230130Smav{
1783230130Smav	struct hdaa_chan *ch = data;
1784230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
1785230130Smav	uint32_t ptr;
1786230130Smav
1787230130Smav	hdaa_lock(devinfo);
1788230130Smav	if (ch->dmapos != NULL) {
1789230130Smav		ptr = *(ch->dmapos);
1790230130Smav	} else {
1791230130Smav		ptr = HDAC_STREAM_GETPTR(
1792230130Smav		    device_get_parent(devinfo->dev), devinfo->dev,
1793230130Smav		    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1794230130Smav	}
1795230130Smav	hdaa_unlock(devinfo);
1796230130Smav
1797230130Smav	/*
1798230130Smav	 * Round to available space and force 128 bytes aligment.
1799230130Smav	 */
1800230130Smav	ptr %= ch->blksz * ch->blkcnt;
1801230130Smav	ptr &= HDA_BLK_ALIGN;
1802230130Smav
1803230130Smav	return (ptr);
1804230130Smav}
1805230130Smav
1806230130Smavstatic struct pcmchan_caps *
1807230130Smavhdaa_channel_getcaps(kobj_t obj, void *data)
1808230130Smav{
1809230130Smav	return (&((struct hdaa_chan *)data)->caps);
1810230130Smav}
1811230130Smav
1812230130Smavstatic kobj_method_t hdaa_channel_methods[] = {
1813230130Smav	KOBJMETHOD(channel_init,		hdaa_channel_init),
1814230130Smav	KOBJMETHOD(channel_setformat,		hdaa_channel_setformat),
1815230130Smav	KOBJMETHOD(channel_setspeed,		hdaa_channel_setspeed),
1816230130Smav	KOBJMETHOD(channel_setblocksize,	hdaa_channel_setblocksize),
1817230130Smav	KOBJMETHOD(channel_setfragments,	hdaa_channel_setfragments),
1818230130Smav	KOBJMETHOD(channel_trigger,		hdaa_channel_trigger),
1819230130Smav	KOBJMETHOD(channel_getptr,		hdaa_channel_getptr),
1820230130Smav	KOBJMETHOD(channel_getcaps,		hdaa_channel_getcaps),
1821230130Smav	KOBJMETHOD_END
1822230130Smav};
1823230130SmavCHANNEL_DECLARE(hdaa_channel);
1824230130Smav
1825230130Smavstatic int
1826230130Smavhdaa_audio_ctl_ossmixer_init(struct snd_mixer *m)
1827230130Smav{
1828230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
1829230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1830230130Smav	struct hdaa_widget *w, *cw;
1831230130Smav	uint32_t mask, recmask;
1832232798Smav	int i, j;
1833230130Smav
1834230130Smav	hdaa_lock(devinfo);
1835232798Smav	pdevinfo->mixer = m;
1836230130Smav
1837230130Smav	/* Make sure that in case of soft volume it won't stay muted. */
1838230130Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1839230130Smav		pdevinfo->left[i] = 100;
1840230130Smav		pdevinfo->right[i] = 100;
1841230130Smav	}
1842230130Smav
1843232798Smav	/* Declare volume controls assigned to this association. */
1844232798Smav	mask = pdevinfo->ossmask;
1845230130Smav	if (pdevinfo->playas >= 0) {
1846232798Smav		/* Declate EAPD as ogain control. */
1847230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1848230130Smav			w = hdaa_widget_get(devinfo, i);
1849230130Smav			if (w == NULL || w->enable == 0)
1850230130Smav				continue;
1851230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
1852230130Smav			    w->param.eapdbtl == HDA_INVALID ||
1853230130Smav			    w->bindas != pdevinfo->playas)
1854230130Smav				continue;
1855230130Smav			mask |= SOUND_MASK_OGAIN;
1856230130Smav			break;
1857230130Smav		}
1858230130Smav
1859232798Smav		/* Declare soft PCM volume if needed. */
1860232798Smav		if ((mask & SOUND_MASK_PCM) == 0 ||
1861232798Smav		    (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) ||
1862232798Smav		    pdevinfo->minamp[SOUND_MIXER_PCM] ==
1863232798Smav		     pdevinfo->maxamp[SOUND_MIXER_PCM]) {
1864232798Smav			mask |= SOUND_MASK_PCM;
1865232798Smav			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
1866232798Smav			HDA_BOOTHVERBOSE(
1867232798Smav				device_printf(pdevinfo->dev,
1868232798Smav				    "Forcing Soft PCM volume\n");
1869232798Smav			);
1870232798Smav		}
1871232798Smav
1872232798Smav		/* Declare master volume if needed. */
1873232798Smav		if ((mask & SOUND_MASK_VOLUME) == 0) {
1874232798Smav			mask |= SOUND_MASK_VOLUME;
1875232798Smav			mix_setparentchild(m, SOUND_MIXER_VOLUME,
1876232798Smav			    SOUND_MASK_PCM);
1877232798Smav			mix_setrealdev(m, SOUND_MIXER_VOLUME,
1878232798Smav			    SOUND_MIXER_NONE);
1879232798Smav			HDA_BOOTHVERBOSE(
1880232798Smav				device_printf(pdevinfo->dev,
1881232798Smav				    "Forcing master volume with PCM\n");
1882232798Smav			);
1883232798Smav		}
1884230130Smav	}
1885230130Smav
1886230130Smav	/* Declare record sources available to this association. */
1887232798Smav	recmask = 0;
1888230130Smav	if (pdevinfo->recas >= 0) {
1889230130Smav		for (i = 0; i < 16; i++) {
1890230130Smav			if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0)
1891230130Smav				continue;
1892230130Smav			w = hdaa_widget_get(devinfo,
1893230130Smav			    devinfo->as[pdevinfo->recas].dacs[0][i]);
1894230130Smav			if (w == NULL || w->enable == 0)
1895230130Smav				continue;
1896230130Smav			for (j = 0; j < w->nconns; j++) {
1897230130Smav				if (w->connsenable[j] == 0)
1898230130Smav					continue;
1899230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
1900230130Smav				if (cw == NULL || cw->enable == 0)
1901230130Smav					continue;
1902230130Smav				if (cw->bindas != pdevinfo->recas &&
1903230130Smav				    cw->bindas != -2)
1904230130Smav					continue;
1905230130Smav				recmask |= cw->ossmask;
1906230130Smav			}
1907230130Smav		}
1908230130Smav	}
1909230130Smav
1910232798Smav	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
1911232798Smav	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
1912232798Smav	pdevinfo->ossmask = mask;
1913232798Smav
1914232798Smav	mix_setrecdevs(m, recmask);
1915232798Smav	mix_setdevs(m, mask);
1916232798Smav
1917232798Smav	hdaa_unlock(devinfo);
1918232798Smav
1919232798Smav	return (0);
1920232798Smav}
1921232798Smav
1922232798Smav/*
1923232798Smav * Update amplification per pdevinfo per ossdev, calculate summary coefficient
1924232798Smav * and write it to codec, update *left and *right to reflect remaining error.
1925232798Smav */
1926232798Smavstatic void
1927232798Smavhdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev,
1928232798Smav    int mute, int *left, int *right)
1929232798Smav{
1930232798Smav	int i, zleft, zright, sleft, sright, smute, lval, rval;
1931232798Smav
1932232798Smav	ctl->devleft[ossdev] = *left;
1933232798Smav	ctl->devright[ossdev] = *right;
1934232798Smav	ctl->devmute[ossdev] = mute;
1935232798Smav	smute = sleft = sright = zleft = zright = 0;
1936232798Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1937232798Smav		sleft += ctl->devleft[i];
1938232798Smav		sright += ctl->devright[i];
1939232798Smav		smute |= ctl->devmute[i];
1940232798Smav		if (i == ossdev)
1941232798Smav			continue;
1942232798Smav		zleft += ctl->devleft[i];
1943232798Smav		zright += ctl->devright[i];
1944232798Smav	}
1945232798Smav	lval = QDB2VAL(ctl, sleft);
1946232798Smav	rval = QDB2VAL(ctl, sright);
1947232798Smav	hdaa_audio_ctl_amp_set(ctl, smute, lval, rval);
1948232798Smav	*left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft));
1949232798Smav	*right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright));
1950232798Smav}
1951232798Smav
1952232798Smav/*
1953232798Smav * Trace signal from source, setting volumes on the way.
1954232798Smav */
1955232798Smavstatic void
1956232798Smavhdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo,
1957232798Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
1958232798Smav{
1959232798Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1960232798Smav	struct hdaa_widget *w, *wc;
1961232798Smav	struct hdaa_audio_ctl *ctl;
1962232798Smav	int i, j, conns = 0;
1963232798Smav
1964232798Smav	if (depth > HDA_PARSE_MAXDEPTH)
1965232798Smav		return;
1966232798Smav
1967232798Smav	w = hdaa_widget_get(devinfo, nid);
1968232798Smav	if (w == NULL || w->enable == 0)
1969232798Smav		return;
1970232798Smav
1971232798Smav	/* Count number of active inputs. */
1972232798Smav	if (depth > 0) {
1973232798Smav		for (j = 0; j < w->nconns; j++) {
1974232798Smav			if (!w->connsenable[j])
1975232798Smav				continue;
1976232798Smav			conns++;
1977230130Smav		}
1978232798Smav	}
1979230130Smav
1980232798Smav	/* If this is not a first step - use input mixer.
1981232798Smav	   Pins have common input ctl so care must be taken. */
1982232798Smav	if (depth > 0 && (conns == 1 ||
1983232798Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
1984232798Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
1985232798Smav		    index, 1);
1986232798Smav		if (ctl)
1987232798Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
1988232798Smav	}
1989232798Smav
1990232798Smav	/* If widget has own ossdev - not traverse it.
1991232798Smav	   It will be traversed on it's own. */
1992232798Smav	if (w->ossdev >= 0 && depth > 0)
1993232798Smav		return;
1994232798Smav
1995232798Smav	/* We must not traverse pin */
1996232798Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
1997232798Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
1998232798Smav	    depth > 0)
1999232798Smav		return;
2000232798Smav
2001232798Smav	/*
2002232798Smav	 * If signals mixed, we can't assign controls farther.
2003232798Smav	 * Ignore this on depth zero. Caller must knows why.
2004232798Smav	 */
2005232798Smav	if (conns > 1 &&
2006232798Smav	    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
2007232798Smav	     w->selconn != index))
2008232798Smav		return;
2009232798Smav
2010232798Smav	ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
2011232798Smav	if (ctl)
2012232798Smav		hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2013232798Smav
2014232798Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2015232798Smav		wc = hdaa_widget_get(devinfo, i);
2016232798Smav		if (wc == NULL || wc->enable == 0)
2017232798Smav			continue;
2018232798Smav		for (j = 0; j < wc->nconns; j++) {
2019232798Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
2020232798Smav				hdaa_audio_ctl_source_volume(pdevinfo, ossdev,
2021232798Smav				    wc->nid, j, mute, left, right, depth + 1);
2022232798Smav			}
2023230130Smav		}
2024230130Smav	}
2025232798Smav	return;
2026232798Smav}
2027230130Smav
2028232798Smav/*
2029232798Smav * Trace signal from destination, setting volumes on the way.
2030232798Smav */
2031232798Smavstatic void
2032232798Smavhdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo,
2033232798Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2034232798Smav{
2035232798Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2036232798Smav	struct hdaa_audio_as *as = devinfo->as;
2037232798Smav	struct hdaa_widget *w, *wc;
2038232798Smav	struct hdaa_audio_ctl *ctl;
2039232798Smav	int i, j, consumers, cleft, cright;
2040232798Smav
2041232798Smav	if (depth > HDA_PARSE_MAXDEPTH)
2042232798Smav		return;
2043232798Smav
2044232798Smav	w = hdaa_widget_get(devinfo, nid);
2045232798Smav	if (w == NULL || w->enable == 0)
2046232798Smav		return;
2047232798Smav
2048232798Smav	if (depth > 0) {
2049232798Smav		/* If this node produce output for several consumers,
2050232798Smav		   we can't touch it. */
2051232798Smav		consumers = 0;
2052232798Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2053232798Smav			wc = hdaa_widget_get(devinfo, i);
2054232798Smav			if (wc == NULL || wc->enable == 0)
2055232798Smav				continue;
2056232798Smav			for (j = 0; j < wc->nconns; j++) {
2057232798Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
2058232798Smav					consumers++;
2059232798Smav			}
2060230130Smav		}
2061232798Smav		/* The only exception is if real HP redirection is configured
2062232798Smav		   and this is a duplication point.
2063232798Smav		   XXX: Actually exception is not completely correct.
2064232798Smav		   XXX: Duplication point check is not perfect. */
2065232798Smav		if ((consumers == 2 && (w->bindas < 0 ||
2066232798Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
2067232798Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
2068232798Smav		    consumers > 2)
2069232798Smav			return;
2070232798Smav
2071232798Smav		/* Else use it's output mixer. */
2072232798Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2073232798Smav		    HDAA_CTL_OUT, -1, 1);
2074232798Smav		if (ctl)
2075232798Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2076230130Smav	}
2077230130Smav
2078232798Smav	/* We must not traverse pin */
2079232798Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2080232798Smav	    depth > 0)
2081232798Smav		return;
2082230130Smav
2083232798Smav	for (i = 0; i < w->nconns; i++) {
2084232798Smav		if (w->connsenable[i] == 0)
2085232798Smav			continue;
2086232798Smav		if (index >= 0 && i != index)
2087232798Smav			continue;
2088232798Smav		cleft = left;
2089232798Smav		cright = right;
2090232798Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2091232798Smav		    HDAA_CTL_IN, i, 1);
2092232798Smav		if (ctl)
2093232798Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright);
2094232798Smav		hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1,
2095232798Smav		    mute, cleft, cright, depth + 1);
2096232798Smav	}
2097232798Smav}
2098230130Smav
2099232798Smav/*
2100232798Smav * Set volumes for the specified pdevinfo and ossdev.
2101232798Smav */
2102232798Smavstatic void
2103232798Smavhdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev)
2104232798Smav{
2105232798Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2106232798Smav	struct hdaa_widget *w, *cw;
2107232798Smav	uint32_t mute;
2108232798Smav	int lvol, rvol;
2109232798Smav	int i, j;
2110230130Smav
2111232798Smav	mute = 0;
2112232798Smav	if (pdevinfo->left[dev] == 0) {
2113232798Smav		mute |= HDAA_AMP_MUTE_LEFT;
2114232798Smav		lvol = -4000;
2115232798Smav	} else
2116232798Smav		lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2117232798Smav		    pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev];
2118232798Smav	if (pdevinfo->right[dev] == 0) {
2119232798Smav		mute |= HDAA_AMP_MUTE_RIGHT;
2120232798Smav		rvol = -4000;
2121232798Smav	} else
2122232798Smav		rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2123232798Smav		    pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev];
2124232798Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2125232798Smav		w = hdaa_widget_get(devinfo, i);
2126232798Smav		if (w == NULL || w->enable == 0)
2127232798Smav			continue;
2128240559Smav		if (w->bindas < 0) {
2129240559Smav			if (pdevinfo->index != 0)
2130240559Smav				continue;
2131240559Smav		} else {
2132240559Smav			if (w->bindas != pdevinfo->playas &&
2133240559Smav			    w->bindas != pdevinfo->recas)
2134240559Smav				continue;
2135240559Smav		}
2136232798Smav		if (dev == SOUND_MIXER_RECLEV &&
2137232798Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2138232798Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2139232798Smav			    w->nid, -1, mute, lvol, rvol, 0);
2140232798Smav			continue;
2141232798Smav		}
2142232798Smav		if (dev == SOUND_MIXER_VOLUME &&
2143232798Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2144232798Smav		    devinfo->as[w->bindas].dir == HDAA_CTL_OUT) {
2145232798Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2146232798Smav			    w->nid, -1, mute, lvol, rvol, 0);
2147232798Smav			continue;
2148232798Smav		}
2149232798Smav		if (dev == SOUND_MIXER_IGAIN &&
2150232798Smav		    w->pflags & HDAA_ADC_MONITOR) {
2151232798Smav			for (j = 0; j < w->nconns; j++) {
2152232798Smav				if (!w->connsenable[j])
2153232798Smav				    continue;
2154232798Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
2155232798Smav				if (cw == NULL || cw->enable == 0)
2156232798Smav				    continue;
2157232798Smav				if (cw->bindas == -1)
2158232798Smav				    continue;
2159232798Smav				if (cw->bindas >= 0 &&
2160232798Smav				    devinfo->as[cw->bindas].dir != HDAA_CTL_IN)
2161232798Smav					continue;
2162232798Smav				hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2163232798Smav				    w->nid, j, mute, lvol, rvol, 0);
2164232798Smav			}
2165232798Smav			continue;
2166232798Smav		}
2167232798Smav		if (w->ossdev != dev)
2168232798Smav			continue;
2169232798Smav		hdaa_audio_ctl_source_volume(pdevinfo, dev,
2170232798Smav		    w->nid, -1, mute, lvol, rvol, 0);
2171232798Smav		if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST))
2172232798Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2173232798Smav			    w->nid, -1, mute, lvol, rvol, 0);
2174232798Smav	}
2175230130Smav}
2176230130Smav
2177232798Smav/*
2178232798Smav * OSS Mixer set method.
2179232798Smav */
2180230130Smavstatic int
2181230130Smavhdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
2182230130Smav					unsigned left, unsigned right)
2183230130Smav{
2184230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2185230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2186230130Smav	struct hdaa_widget *w;
2187232798Smav	int i;
2188230130Smav
2189230130Smav	hdaa_lock(devinfo);
2190232798Smav
2191230130Smav	/* Save new values. */
2192230130Smav	pdevinfo->left[dev] = left;
2193230130Smav	pdevinfo->right[dev] = right;
2194230130Smav
2195230130Smav	/* 'ogain' is the special case implemented with EAPD. */
2196230130Smav	if (dev == SOUND_MIXER_OGAIN) {
2197230130Smav		uint32_t orig;
2198230130Smav		w = NULL;
2199230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2200230130Smav			w = hdaa_widget_get(devinfo, i);
2201230130Smav			if (w == NULL || w->enable == 0)
2202230130Smav				continue;
2203230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2204230130Smav			    w->param.eapdbtl == HDA_INVALID)
2205230130Smav				continue;
2206230130Smav			break;
2207230130Smav		}
2208230130Smav		if (i >= devinfo->endnode) {
2209230130Smav			hdaa_unlock(devinfo);
2210230130Smav			return (-1);
2211230130Smav		}
2212230130Smav		orig = w->param.eapdbtl;
2213230130Smav		if (left == 0)
2214230130Smav			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2215230130Smav		else
2216230130Smav			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2217230130Smav		if (orig != w->param.eapdbtl) {
2218230130Smav			uint32_t val;
2219230130Smav
2220230130Smav			val = w->param.eapdbtl;
2221230130Smav			if (devinfo->quirks & HDAA_QUIRK_EAPDINV)
2222230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2223230130Smav			hda_command(devinfo->dev,
2224230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val));
2225230130Smav		}
2226230130Smav		hdaa_unlock(devinfo);
2227230130Smav		return (left | (left << 8));
2228230130Smav	}
2229230130Smav
2230230130Smav	/* Recalculate all controls related to this OSS device. */
2231232798Smav	hdaa_audio_ctl_dev_volume(pdevinfo, dev);
2232232798Smav
2233232798Smav	hdaa_unlock(devinfo);
2234232798Smav	return (left | (right << 8));
2235232798Smav}
2236232798Smav
2237232798Smav/*
2238232798Smav * Set mixer settings to our own default values:
2239232798Smav * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others.
2240232798Smav */
2241232798Smavstatic void
2242232798Smavhdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo)
2243232798Smav{
2244232798Smav	int amp, vol, dev;
2245232798Smav
2246232798Smav	for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) {
2247232798Smav		if ((pdevinfo->ossmask & (1 << dev)) == 0)
2248230130Smav			continue;
2249232798Smav
2250232798Smav		/* If the value was overriden, leave it as is. */
2251232798Smav		if (resource_int_value(device_get_name(pdevinfo->dev),
2252232798Smav		    device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0)
2253230130Smav			continue;
2254230130Smav
2255232798Smav		vol = -1;
2256232798Smav		if (dev == SOUND_MIXER_OGAIN)
2257232798Smav			vol = 100;
2258232798Smav		else if (dev == SOUND_MIXER_IGAIN)
2259232798Smav			vol = 0;
2260232798Smav		else if (dev == SOUND_MIXER_MIC ||
2261232798Smav		    dev == SOUND_MIXER_MONITOR)
2262232798Smav			amp = 20 * 4;	/* +20dB */
2263232798Smav		else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital)
2264232798Smav			amp = -10 * 4;	/* -10dB */
2265232798Smav		else
2266232798Smav			amp = 0;
2267232798Smav		if (vol < 0 &&
2268232798Smav		    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) {
2269232798Smav			vol = 100;
2270232798Smav		} else if (vol < 0) {
2271232798Smav			vol = ((amp - pdevinfo->minamp[dev]) * 100 +
2272232798Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) /
2273232798Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]);
2274232798Smav			vol = imin(imax(vol, 1), 100);
2275230130Smav		}
2276232798Smav		mix_set(pdevinfo->mixer, dev, vol, vol);
2277230130Smav	}
2278230130Smav}
2279230130Smav
2280230130Smav/*
2281232798Smav * Recursively commutate specified record source.
2282230130Smav */
2283230130Smavstatic uint32_t
2284230130Smavhdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
2285230130Smav{
2286230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2287230130Smav	struct hdaa_widget *w, *cw;
2288230130Smav	struct hdaa_audio_ctl *ctl;
2289230130Smav	char buf[64];
2290230130Smav	int i, muted;
2291230130Smav	uint32_t res = 0;
2292230130Smav
2293230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
2294230130Smav		return (0);
2295230130Smav
2296230130Smav	w = hdaa_widget_get(devinfo, nid);
2297230130Smav	if (w == NULL || w->enable == 0)
2298230130Smav		return (0);
2299230130Smav
2300230130Smav	for (i = 0; i < w->nconns; i++) {
2301230130Smav		if (w->connsenable[i] == 0)
2302230130Smav			continue;
2303230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
2304230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
2305230130Smav			continue;
2306230130Smav		/* Call recursively to trace signal to it's source if needed. */
2307230130Smav		if ((src & cw->ossmask) != 0) {
2308230130Smav			if (cw->ossdev < 0) {
2309230130Smav				res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2310230130Smav				    w->conns[i], depth + 1);
2311230130Smav			} else {
2312230130Smav				res |= cw->ossmask;
2313230130Smav			}
2314230130Smav		}
2315230130Smav		/* We have two special cases: mixers and others (selectors). */
2316230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
2317230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo,
2318230130Smav			    w->nid, HDAA_CTL_IN, i, 1);
2319230130Smav			if (ctl == NULL)
2320230130Smav				continue;
2321230130Smav			/* If we have input control on this node mute them
2322230130Smav			 * according to requested sources. */
2323230130Smav			muted = (src & cw->ossmask) ? 0 : 1;
2324230130Smav			if (muted != ctl->forcemute) {
2325230130Smav				ctl->forcemute = muted;
2326230130Smav				hdaa_audio_ctl_amp_set(ctl,
2327230130Smav				    HDAA_AMP_MUTE_DEFAULT,
2328230130Smav				    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
2329230130Smav			}
2330230130Smav			HDA_BOOTHVERBOSE(
2331230130Smav				device_printf(pdevinfo->dev,
2332230130Smav				    "Recsel (%s): nid %d source %d %s\n",
2333230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2334230130Smav				    src, buf, sizeof(buf)),
2335230130Smav				    nid, i, muted?"mute":"unmute");
2336230130Smav			);
2337230130Smav		} else {
2338230130Smav			if (w->nconns == 1)
2339230130Smav				break;
2340230130Smav			if ((src & cw->ossmask) == 0)
2341230130Smav				continue;
2342230130Smav			/* If we found requested source - select it and exit. */
2343230130Smav			hdaa_widget_connection_select(w, i);
2344230130Smav			HDA_BOOTHVERBOSE(
2345230130Smav				device_printf(pdevinfo->dev,
2346230130Smav				    "Recsel (%s): nid %d source %d select\n",
2347230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2348230130Smav				    src, buf, sizeof(buf)),
2349230130Smav				    nid, i);
2350230130Smav			);
2351230130Smav			break;
2352230130Smav		}
2353230130Smav	}
2354230130Smav	return (res);
2355230130Smav}
2356230130Smav
2357230130Smavstatic uint32_t
2358230130Smavhdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
2359230130Smav{
2360230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2361230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2362230130Smav	struct hdaa_widget *w;
2363230130Smav	struct hdaa_audio_as *as;
2364232798Smav	struct hdaa_audio_ctl *ctl;
2365230130Smav	struct hdaa_chan *ch;
2366230130Smav	int i, j;
2367230130Smav	uint32_t ret = 0xffffffff;
2368230130Smav
2369230130Smav	hdaa_lock(devinfo);
2370230130Smav	if (pdevinfo->recas < 0) {
2371230130Smav		hdaa_unlock(devinfo);
2372230130Smav		return (0);
2373230130Smav	}
2374230130Smav	as = &devinfo->as[pdevinfo->recas];
2375230130Smav
2376230130Smav	/* For non-mixed associations we always recording everything. */
2377230130Smav	if (!as->mixed) {
2378230130Smav		hdaa_unlock(devinfo);
2379230130Smav		return (mix_getrecdevs(m));
2380230130Smav	}
2381230130Smav
2382230130Smav	/* Commutate requested recsrc for each ADC. */
2383230130Smav	for (j = 0; j < as->num_chans; j++) {
2384230130Smav		ch = &devinfo->chans[as->chans[j]];
2385230130Smav		for (i = 0; ch->io[i] >= 0; i++) {
2386230130Smav			w = hdaa_widget_get(devinfo, ch->io[i]);
2387230130Smav			if (w == NULL || w->enable == 0)
2388230130Smav				continue;
2389230130Smav			ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2390230130Smav			    ch->io[i], 0);
2391230130Smav		}
2392230130Smav	}
2393232798Smav	if (ret == 0xffffffff)
2394232798Smav		ret = 0;
2395230130Smav
2396232798Smav	/*
2397232798Smav	 * Some controls could be shared. Reset volumes for controls
2398232798Smav	 * related to previously chosen devices, as they may no longer
2399232798Smav	 * affect the signal.
2400232798Smav	 */
2401232798Smav	i = 0;
2402232798Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
2403232798Smav		if (ctl->enable == 0 ||
2404232798Smav		    !(ctl->ossmask & pdevinfo->recsrc))
2405232798Smav			continue;
2406232798Smav		if (!((pdevinfo->playas >= 0 &&
2407232798Smav		    ctl->widget->bindas == pdevinfo->playas) ||
2408232798Smav		    (pdevinfo->recas >= 0 &&
2409232798Smav		    ctl->widget->bindas == pdevinfo->recas) ||
2410232798Smav		    (pdevinfo->index == 0 &&
2411232798Smav		    ctl->widget->bindas == -2)))
2412232798Smav			continue;
2413232798Smav		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2414232798Smav			if (pdevinfo->recsrc & (1 << j)) {
2415232798Smav				ctl->devleft[j] = 0;
2416232798Smav				ctl->devright[j] = 0;
2417232798Smav				ctl->devmute[j] = 0;
2418232798Smav			}
2419232798Smav		}
2420232798Smav	}
2421232798Smav
2422232798Smav	/*
2423232798Smav	 * Some controls could be shared. Set volumes for controls
2424232798Smav	 * related to devices selected both previously and now.
2425232798Smav	 */
2426232798Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2427232798Smav		if ((ret | pdevinfo->recsrc) & (1 << j))
2428232798Smav			hdaa_audio_ctl_dev_volume(pdevinfo, j);
2429232798Smav	}
2430232798Smav
2431232798Smav	pdevinfo->recsrc = ret;
2432230130Smav	hdaa_unlock(devinfo);
2433232798Smav	return (ret);
2434230130Smav}
2435230130Smav
2436230130Smavstatic kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = {
2437230130Smav	KOBJMETHOD(mixer_init,		hdaa_audio_ctl_ossmixer_init),
2438230130Smav	KOBJMETHOD(mixer_set,		hdaa_audio_ctl_ossmixer_set),
2439230130Smav	KOBJMETHOD(mixer_setrecsrc,	hdaa_audio_ctl_ossmixer_setrecsrc),
2440230130Smav	KOBJMETHOD_END
2441230130Smav};
2442230130SmavMIXER_DECLARE(hdaa_audio_ctl_ossmixer);
2443230130Smav
2444230130Smavstatic void
2445230130Smavhdaa_dump_gpi(struct hdaa_devinfo *devinfo)
2446230130Smav{
2447230130Smav	device_t dev = devinfo->dev;
2448230130Smav	int i;
2449230130Smav	uint32_t data, wake, unsol, sticky;
2450230130Smav
2451230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) {
2452230130Smav		data = hda_command(dev,
2453230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
2454230130Smav		wake = hda_command(dev,
2455230130Smav		    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid));
2456230130Smav		unsol = hda_command(dev,
2457230130Smav		    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2458230130Smav		sticky = hda_command(dev,
2459230130Smav		    HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid));
2460230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) {
2461230130Smav			device_printf(dev, " GPI%d:%s%s%s state=%d", i,
2462230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2463230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2464230130Smav				    (wake & (1 << i)) ? " wake" : "",
2465230130Smav				    (data >> i) & 1);
2466230130Smav		}
2467230130Smav	}
2468230130Smav}
2469230130Smav
2470230130Smavstatic void
2471230130Smavhdaa_dump_gpio(struct hdaa_devinfo *devinfo)
2472230130Smav{
2473230130Smav	device_t dev = devinfo->dev;
2474230130Smav	int i;
2475230130Smav	uint32_t data, dir, enable, wake, unsol, sticky;
2476230130Smav
2477230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) {
2478230130Smav		data = hda_command(dev,
2479230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
2480230130Smav		enable = hda_command(dev,
2481230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
2482230130Smav		dir = hda_command(dev,
2483230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
2484230130Smav		wake = hda_command(dev,
2485230130Smav		    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid));
2486230130Smav		unsol = hda_command(dev,
2487230130Smav		    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2488230130Smav		sticky = hda_command(dev,
2489230130Smav		    HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid));
2490230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) {
2491230130Smav			device_printf(dev, " GPIO%d: ", i);
2492230130Smav			if ((enable & (1 << i)) == 0) {
2493230130Smav				printf("disabled\n");
2494230130Smav				continue;
2495230130Smav			}
2496230130Smav			if ((dir & (1 << i)) == 0) {
2497230130Smav				printf("input%s%s%s",
2498230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2499230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2500230130Smav				    (wake & (1 << i)) ? " wake" : "");
2501230130Smav			} else
2502230130Smav				printf("output");
2503230130Smav			printf(" state=%d\n", (data >> i) & 1);
2504230130Smav		}
2505230130Smav	}
2506230130Smav}
2507230130Smav
2508230130Smavstatic void
2509230130Smavhdaa_dump_gpo(struct hdaa_devinfo *devinfo)
2510230130Smav{
2511230130Smav	device_t dev = devinfo->dev;
2512230130Smav	int i;
2513230130Smav	uint32_t data;
2514230130Smav
2515230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) {
2516230130Smav		data = hda_command(dev,
2517230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
2518230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) {
2519230130Smav			device_printf(dev, " GPO%d: state=%d", i,
2520230130Smav				    (data >> i) & 1);
2521230130Smav		}
2522230130Smav	}
2523230130Smav}
2524230130Smav
2525230130Smavstatic void
2526230130Smavhdaa_audio_parse(struct hdaa_devinfo *devinfo)
2527230130Smav{
2528230130Smav	struct hdaa_widget *w;
2529230130Smav	uint32_t res;
2530230130Smav	int i;
2531230130Smav	nid_t nid;
2532230130Smav
2533230130Smav	nid = devinfo->nid;
2534230130Smav
2535230130Smav	res = hda_command(devinfo->dev,
2536230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT));
2537230130Smav	devinfo->gpio_cap = res;
2538230130Smav
2539230130Smav	HDA_BOOTVERBOSE(
2540230130Smav		device_printf(devinfo->dev,
2541230130Smav		    "NumGPIO=%d NumGPO=%d "
2542230130Smav		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
2543230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
2544230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
2545230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
2546230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
2547230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
2548230130Smav		hdaa_dump_gpi(devinfo);
2549230130Smav		hdaa_dump_gpio(devinfo);
2550230130Smav		hdaa_dump_gpo(devinfo);
2551230130Smav	);
2552230130Smav
2553230130Smav	res = hda_command(devinfo->dev,
2554230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS));
2555230130Smav	devinfo->supp_stream_formats = res;
2556230130Smav
2557230130Smav	res = hda_command(devinfo->dev,
2558230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE));
2559230130Smav	devinfo->supp_pcm_size_rate = res;
2560230130Smav
2561230130Smav	res = hda_command(devinfo->dev,
2562230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP));
2563230130Smav	devinfo->outamp_cap = res;
2564230130Smav
2565230130Smav	res = hda_command(devinfo->dev,
2566230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP));
2567230130Smav	devinfo->inamp_cap = res;
2568230130Smav
2569230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2570230130Smav		w = hdaa_widget_get(devinfo, i);
2571230130Smav		if (w == NULL)
2572230130Smav			device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i);
2573230130Smav		else {
2574230130Smav			w->devinfo = devinfo;
2575230130Smav			w->nid = i;
2576230130Smav			w->enable = 1;
2577230130Smav			w->selconn = -1;
2578230130Smav			w->pflags = 0;
2579230130Smav			w->ossdev = -1;
2580230130Smav			w->bindas = -1;
2581230130Smav			w->param.eapdbtl = HDA_INVALID;
2582230130Smav			hdaa_widget_parse(w);
2583230130Smav		}
2584230130Smav	}
2585230130Smav}
2586230130Smav
2587230130Smavstatic void
2588230130Smavhdaa_audio_postprocess(struct hdaa_devinfo *devinfo)
2589230130Smav{
2590230130Smav	struct hdaa_widget *w;
2591230130Smav	int i;
2592230130Smav
2593230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2594230130Smav		w = hdaa_widget_get(devinfo, i);
2595230130Smav		if (w == NULL)
2596230130Smav			continue;
2597230130Smav		hdaa_widget_postprocess(w);
2598230130Smav	}
2599230130Smav}
2600230130Smav
2601230130Smavstatic void
2602230130Smavhdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo)
2603230130Smav{
2604230130Smav	struct hdaa_audio_ctl *ctls;
2605230130Smav	struct hdaa_widget *w, *cw;
2606230130Smav	int i, j, cnt, max, ocap, icap;
2607230130Smav	int mute, offset, step, size;
2608230130Smav
2609230130Smav	/* XXX This is redundant */
2610230130Smav	max = 0;
2611230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2612230130Smav		w = hdaa_widget_get(devinfo, i);
2613230130Smav		if (w == NULL || w->enable == 0)
2614230130Smav			continue;
2615230130Smav		if (w->param.outamp_cap != 0)
2616230130Smav			max++;
2617230130Smav		if (w->param.inamp_cap != 0) {
2618230130Smav			switch (w->type) {
2619230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2620230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2621230130Smav				for (j = 0; j < w->nconns; j++) {
2622230130Smav					cw = hdaa_widget_get(devinfo,
2623230130Smav					    w->conns[j]);
2624230130Smav					if (cw == NULL || cw->enable == 0)
2625230130Smav						continue;
2626230130Smav					max++;
2627230130Smav				}
2628230130Smav				break;
2629230130Smav			default:
2630230130Smav				max++;
2631230130Smav				break;
2632230130Smav			}
2633230130Smav		}
2634230130Smav	}
2635230130Smav	devinfo->ctlcnt = max;
2636230130Smav
2637230130Smav	if (max < 1)
2638230130Smav		return;
2639230130Smav
2640230130Smav	ctls = (struct hdaa_audio_ctl *)malloc(
2641230130Smav	    sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT);
2642230130Smav
2643230130Smav	if (ctls == NULL) {
2644230130Smav		/* Blekh! */
2645230130Smav		device_printf(devinfo->dev, "unable to allocate ctls!\n");
2646230130Smav		devinfo->ctlcnt = 0;
2647230130Smav		return;
2648230130Smav	}
2649230130Smav
2650230130Smav	cnt = 0;
2651230130Smav	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
2652230130Smav		if (cnt >= max) {
2653230130Smav			device_printf(devinfo->dev, "%s: Ctl overflow!\n",
2654230130Smav			    __func__);
2655230130Smav			break;
2656230130Smav		}
2657230130Smav		w = hdaa_widget_get(devinfo, i);
2658230130Smav		if (w == NULL || w->enable == 0)
2659230130Smav			continue;
2660230130Smav		ocap = w->param.outamp_cap;
2661230130Smav		icap = w->param.inamp_cap;
2662230130Smav		if (ocap != 0) {
2663230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
2664230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
2665230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
2666230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
2667230130Smav			/*if (offset > step) {
2668230130Smav				HDA_BOOTVERBOSE(
2669230130Smav					device_printf(devinfo->dev,
2670230130Smav					    "BUGGY outamp: nid=%d "
2671230130Smav					    "[offset=%d > step=%d]\n",
2672230130Smav					    w->nid, offset, step);
2673230130Smav				);
2674230130Smav				offset = step;
2675230130Smav			}*/
2676230130Smav			ctls[cnt].enable = 1;
2677230130Smav			ctls[cnt].widget = w;
2678230130Smav			ctls[cnt].mute = mute;
2679230130Smav			ctls[cnt].step = step;
2680230130Smav			ctls[cnt].size = size;
2681230130Smav			ctls[cnt].offset = offset;
2682230130Smav			ctls[cnt].left = offset;
2683230130Smav			ctls[cnt].right = offset;
2684230130Smav			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2685230130Smav			    w->waspin)
2686230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
2687230130Smav			else
2688230130Smav				ctls[cnt].ndir = HDAA_CTL_OUT;
2689230130Smav			ctls[cnt++].dir = HDAA_CTL_OUT;
2690230130Smav		}
2691230130Smav
2692230130Smav		if (icap != 0) {
2693230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
2694230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
2695230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
2696230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
2697230130Smav			/*if (offset > step) {
2698230130Smav				HDA_BOOTVERBOSE(
2699230130Smav					device_printf(devinfo->dev,
2700230130Smav					    "BUGGY inamp: nid=%d "
2701230130Smav					    "[offset=%d > step=%d]\n",
2702230130Smav					    w->nid, offset, step);
2703230130Smav				);
2704230130Smav				offset = step;
2705230130Smav			}*/
2706230130Smav			switch (w->type) {
2707230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2708230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2709230130Smav				for (j = 0; j < w->nconns; j++) {
2710230130Smav					if (cnt >= max) {
2711230130Smav						device_printf(devinfo->dev,
2712230130Smav						    "%s: Ctl overflow!\n",
2713230130Smav						    __func__);
2714230130Smav						break;
2715230130Smav					}
2716230130Smav					cw = hdaa_widget_get(devinfo,
2717230130Smav					    w->conns[j]);
2718230130Smav					if (cw == NULL || cw->enable == 0)
2719230130Smav						continue;
2720230130Smav					ctls[cnt].enable = 1;
2721230130Smav					ctls[cnt].widget = w;
2722230130Smav					ctls[cnt].childwidget = cw;
2723230130Smav					ctls[cnt].index = j;
2724230130Smav					ctls[cnt].mute = mute;
2725230130Smav					ctls[cnt].step = step;
2726230130Smav					ctls[cnt].size = size;
2727230130Smav					ctls[cnt].offset = offset;
2728230130Smav					ctls[cnt].left = offset;
2729230130Smav					ctls[cnt].right = offset;
2730230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
2731230130Smav					ctls[cnt++].dir = HDAA_CTL_IN;
2732230130Smav				}
2733230130Smav				break;
2734230130Smav			default:
2735230130Smav				if (cnt >= max) {
2736230130Smav					device_printf(devinfo->dev,
2737230130Smav					    "%s: Ctl overflow!\n",
2738230130Smav					    __func__);
2739230130Smav					break;
2740230130Smav				}
2741230130Smav				ctls[cnt].enable = 1;
2742230130Smav				ctls[cnt].widget = w;
2743230130Smav				ctls[cnt].mute = mute;
2744230130Smav				ctls[cnt].step = step;
2745230130Smav				ctls[cnt].size = size;
2746230130Smav				ctls[cnt].offset = offset;
2747230130Smav				ctls[cnt].left = offset;
2748230130Smav				ctls[cnt].right = offset;
2749230130Smav				if (w->type ==
2750230130Smav				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2751230130Smav					ctls[cnt].ndir = HDAA_CTL_OUT;
2752230130Smav				else
2753230130Smav					ctls[cnt].ndir = HDAA_CTL_IN;
2754230130Smav				ctls[cnt++].dir = HDAA_CTL_IN;
2755230130Smav				break;
2756230130Smav			}
2757230130Smav		}
2758230130Smav	}
2759230130Smav
2760230130Smav	devinfo->ctl = ctls;
2761230130Smav}
2762230130Smav
2763230130Smavstatic void
2764230130Smavhdaa_audio_as_parse(struct hdaa_devinfo *devinfo)
2765230130Smav{
2766230130Smav	struct hdaa_audio_as *as;
2767230130Smav	struct hdaa_widget *w;
2768230130Smav	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
2769230130Smav
2770230130Smav	/* Count present associations */
2771230130Smav	max = 0;
2772230130Smav	for (j = 1; j < 16; j++) {
2773230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2774230130Smav			w = hdaa_widget_get(devinfo, i);
2775230130Smav			if (w == NULL || w->enable == 0)
2776230130Smav				continue;
2777230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2778230130Smav				continue;
2779230130Smav			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
2780230130Smav			    != j)
2781230130Smav				continue;
2782230130Smav			max++;
2783230130Smav			if (j != 15)  /* There could be many 1-pin assocs #15 */
2784230130Smav				break;
2785230130Smav		}
2786230130Smav	}
2787230130Smav
2788230130Smav	devinfo->ascnt = max;
2789230130Smav
2790230130Smav	if (max < 1)
2791230130Smav		return;
2792230130Smav
2793230130Smav	as = (struct hdaa_audio_as *)malloc(
2794230130Smav	    sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT);
2795230130Smav
2796230130Smav	if (as == NULL) {
2797230130Smav		/* Blekh! */
2798230130Smav		device_printf(devinfo->dev, "unable to allocate assocs!\n");
2799230130Smav		devinfo->ascnt = 0;
2800230130Smav		return;
2801230130Smav	}
2802230130Smav
2803230130Smav	for (i = 0; i < max; i++) {
2804230130Smav		as[i].hpredir = -1;
2805230130Smav		as[i].digital = 0;
2806230130Smav		as[i].num_chans = 1;
2807230130Smav		as[i].location = -1;
2808230130Smav	}
2809230130Smav
2810230130Smav	/* Scan associations skipping as=0. */
2811230130Smav	cnt = 0;
2812230130Smav	for (j = 1; j < 16; j++) {
2813230130Smav		first = 16;
2814230130Smav		hpredir = 0;
2815230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2816230130Smav			w = hdaa_widget_get(devinfo, i);
2817230130Smav			if (w == NULL || w->enable == 0)
2818230130Smav				continue;
2819230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2820230130Smav				continue;
2821230130Smav			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
2822230130Smav			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
2823230130Smav			if (assoc != j) {
2824230130Smav				continue;
2825230130Smav			}
2826230130Smav			KASSERT(cnt < max,
2827230130Smav			    ("%s: Associations owerflow (%d of %d)",
2828230130Smav			    __func__, cnt, max));
2829230130Smav			type = w->wclass.pin.config &
2830230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2831230130Smav			/* Get pin direction. */
2832230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
2833230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
2834230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
2835230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
2836230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
2837230130Smav				dir = HDAA_CTL_OUT;
2838230130Smav			else
2839230130Smav				dir = HDAA_CTL_IN;
2840230130Smav			/* If this is a first pin - create new association. */
2841230130Smav			if (as[cnt].pincnt == 0) {
2842230130Smav				as[cnt].enable = 1;
2843230130Smav				as[cnt].index = j;
2844230130Smav				as[cnt].dir = dir;
2845230130Smav			}
2846230130Smav			if (seq < first)
2847230130Smav				first = seq;
2848230130Smav			/* Check association correctness. */
2849230130Smav			if (as[cnt].pins[seq] != 0) {
2850230130Smav				device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) "
2851230130Smav				    "in association %d! Disabling association.\n",
2852230130Smav				    __func__, seq, w->nid, j);
2853230130Smav				as[cnt].enable = 0;
2854230130Smav			}
2855230130Smav			if (dir != as[cnt].dir) {
2856230130Smav				device_printf(devinfo->dev, "%s: Pin %d has wrong "
2857230130Smav				    "direction for association %d! Disabling "
2858230130Smav				    "association.\n",
2859230130Smav				    __func__, w->nid, j);
2860230130Smav				as[cnt].enable = 0;
2861230130Smav			}
2862230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
2863232798Smav				as[cnt].digital |= 0x1;
2864232798Smav				if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
2865232798Smav					as[cnt].digital |= 0x2;
2866230130Smav				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
2867232798Smav					as[cnt].digital |= 0x4;
2868230130Smav			}
2869230130Smav			if (as[cnt].location == -1) {
2870230130Smav				as[cnt].location =
2871230130Smav				    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config);
2872230130Smav			} else if (as[cnt].location !=
2873230130Smav			    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) {
2874230130Smav				as[cnt].location = -2;
2875230130Smav			}
2876230130Smav			/* Headphones with seq=15 may mean redirection. */
2877230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
2878230130Smav			    seq == 15)
2879230130Smav				hpredir = 1;
2880230130Smav			as[cnt].pins[seq] = w->nid;
2881230130Smav			as[cnt].pincnt++;
2882230130Smav			/* Association 15 is a multiple unassociated pins. */
2883230130Smav			if (j == 15)
2884230130Smav				cnt++;
2885230130Smav		}
2886230130Smav		if (j != 15 && as[cnt].pincnt > 0) {
2887230130Smav			if (hpredir && as[cnt].pincnt > 1)
2888230130Smav				as[cnt].hpredir = first;
2889230130Smav			cnt++;
2890230130Smav		}
2891230130Smav	}
2892230130Smav	for (i = 0; i < max; i++) {
2893230130Smav		if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 ||
2894230130Smav		    as[i].pins[14] > 0 || as[i].pins[15] > 0))
2895230130Smav			as[i].mixed = 1;
2896230130Smav	}
2897230130Smav	HDA_BOOTVERBOSE(
2898230130Smav		device_printf(devinfo->dev,
2899230130Smav		    "%d associations found:\n", max);
2900230130Smav		for (i = 0; i < max; i++) {
2901230130Smav			device_printf(devinfo->dev,
2902230130Smav			    "Association %d (%d) %s%s:\n",
2903230130Smav			    i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out",
2904230130Smav			    as[i].enable?"":" (disabled)");
2905230130Smav			for (j = 0; j < 16; j++) {
2906230130Smav				if (as[i].pins[j] == 0)
2907230130Smav					continue;
2908230130Smav				device_printf(devinfo->dev,
2909230130Smav				    " Pin nid=%d seq=%d\n",
2910230130Smav				    as[i].pins[j], j);
2911230130Smav			}
2912230130Smav		}
2913230130Smav	);
2914230130Smav
2915230130Smav	devinfo->as = as;
2916230130Smav}
2917230130Smav
2918230130Smav/*
2919230130Smav * Trace path from DAC to pin.
2920230130Smav */
2921230130Smavstatic nid_t
2922230130Smavhdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
2923230130Smav    int dupseq, int min, int only, int depth)
2924230130Smav{
2925230130Smav	struct hdaa_widget *w;
2926230130Smav	int i, im = -1;
2927230130Smav	nid_t m = 0, ret;
2928230130Smav
2929230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
2930230130Smav		return (0);
2931230130Smav	w = hdaa_widget_get(devinfo, nid);
2932230130Smav	if (w == NULL || w->enable == 0)
2933230130Smav		return (0);
2934230130Smav	HDA_BOOTHVERBOSE(
2935230130Smav		if (!only) {
2936230130Smav			device_printf(devinfo->dev,
2937230130Smav			    " %*stracing via nid %d\n",
2938230130Smav				depth + 1, "", w->nid);
2939230130Smav		}
2940230130Smav	);
2941230130Smav	/* Use only unused widgets */
2942230130Smav	if (w->bindas >= 0 && w->bindas != as) {
2943230130Smav		HDA_BOOTHVERBOSE(
2944230130Smav			if (!only) {
2945230130Smav				device_printf(devinfo->dev,
2946230130Smav				    " %*snid %d busy by association %d\n",
2947230130Smav					depth + 1, "", w->nid, w->bindas);
2948230130Smav			}
2949230130Smav		);
2950230130Smav		return (0);
2951230130Smav	}
2952230130Smav	if (dupseq < 0) {
2953230130Smav		if (w->bindseqmask != 0) {
2954230130Smav			HDA_BOOTHVERBOSE(
2955230130Smav				if (!only) {
2956230130Smav					device_printf(devinfo->dev,
2957230130Smav					    " %*snid %d busy by seqmask %x\n",
2958230130Smav						depth + 1, "", w->nid, w->bindseqmask);
2959230130Smav				}
2960230130Smav			);
2961230130Smav			return (0);
2962230130Smav		}
2963230130Smav	} else {
2964230130Smav		/* If this is headphones - allow duplicate first pin. */
2965230130Smav		if (w->bindseqmask != 0 &&
2966230130Smav		    (w->bindseqmask & (1 << dupseq)) == 0) {
2967230130Smav			HDA_BOOTHVERBOSE(
2968230130Smav				device_printf(devinfo->dev,
2969230130Smav				    " %*snid %d busy by seqmask %x\n",
2970230130Smav					depth + 1, "", w->nid, w->bindseqmask);
2971230130Smav			);
2972230130Smav			return (0);
2973230130Smav		}
2974230130Smav	}
2975230130Smav
2976230130Smav	switch (w->type) {
2977230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
2978230130Smav		/* Do not traverse input. AD1988 has digital monitor
2979230130Smav		for which we are not ready. */
2980230130Smav		break;
2981230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
2982230130Smav		/* If we are tracing HP take only dac of first pin. */
2983230130Smav		if ((only == 0 || only == w->nid) &&
2984230130Smav		    (w->nid >= min) && (dupseq < 0 || w->nid ==
2985230130Smav		    devinfo->as[as].dacs[0][dupseq]))
2986230130Smav			m = w->nid;
2987230130Smav		break;
2988230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
2989230130Smav		if (depth > 0)
2990230130Smav			break;
2991230130Smav		/* Fall */
2992230130Smav	default:
2993230130Smav		/* Find reachable DACs with smallest nid respecting constraints. */
2994230130Smav		for (i = 0; i < w->nconns; i++) {
2995230130Smav			if (w->connsenable[i] == 0)
2996230130Smav				continue;
2997230130Smav			if (w->selconn != -1 && w->selconn != i)
2998230130Smav				continue;
2999230130Smav			if ((ret = hdaa_audio_trace_dac(devinfo, as, seq,
3000230130Smav			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
3001230130Smav				if (m == 0 || ret < m) {
3002230130Smav					m = ret;
3003230130Smav					im = i;
3004230130Smav				}
3005230130Smav				if (only || dupseq >= 0)
3006230130Smav					break;
3007230130Smav			}
3008230130Smav		}
3009230130Smav		if (im >= 0 && only && ((w->nconns > 1 &&
3010230130Smav		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3011230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3012230130Smav			w->selconn = im;
3013230130Smav		break;
3014230130Smav	}
3015230130Smav	if (m && only) {
3016230130Smav		w->bindas = as;
3017230130Smav		w->bindseqmask |= (1 << seq);
3018230130Smav	}
3019230130Smav	HDA_BOOTHVERBOSE(
3020230130Smav		if (!only) {
3021230130Smav			device_printf(devinfo->dev,
3022230130Smav			    " %*snid %d returned %d\n",
3023230130Smav				depth + 1, "", w->nid, m);
3024230130Smav		}
3025230130Smav	);
3026230130Smav	return (m);
3027230130Smav}
3028230130Smav
3029230130Smav/*
3030230130Smav * Trace path from widget to ADC.
3031230130Smav */
3032230130Smavstatic nid_t
3033230130Smavhdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3034230130Smav    int mixed, int min, int only, int depth, int *length, int onlylength)
3035230130Smav{
3036230130Smav	struct hdaa_widget *w, *wc;
3037230130Smav	int i, j, im, lm = HDA_PARSE_MAXDEPTH;
3038230130Smav	nid_t m = 0, ret;
3039230130Smav
3040230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3041230130Smav		return (0);
3042230130Smav	w = hdaa_widget_get(devinfo, nid);
3043230130Smav	if (w == NULL || w->enable == 0)
3044230130Smav		return (0);
3045230130Smav	HDA_BOOTHVERBOSE(
3046230130Smav		device_printf(devinfo->dev,
3047230130Smav		    " %*stracing via nid %d\n",
3048230130Smav			depth + 1, "", w->nid);
3049230130Smav	);
3050230130Smav	/* Use only unused widgets */
3051230130Smav	if (w->bindas >= 0 && w->bindas != as) {
3052230130Smav		HDA_BOOTHVERBOSE(
3053230130Smav			device_printf(devinfo->dev,
3054230130Smav			    " %*snid %d busy by association %d\n",
3055230130Smav				depth + 1, "", w->nid, w->bindas);
3056230130Smav		);
3057230130Smav		return (0);
3058230130Smav	}
3059230130Smav	if (!mixed && w->bindseqmask != 0) {
3060230130Smav		HDA_BOOTHVERBOSE(
3061230130Smav			device_printf(devinfo->dev,
3062230130Smav			    " %*snid %d busy by seqmask %x\n",
3063230130Smav				depth + 1, "", w->nid, w->bindseqmask);
3064230130Smav		);
3065230130Smav		return (0);
3066230130Smav	}
3067230130Smav	switch (w->type) {
3068230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3069230130Smav		if ((only == 0 || only == w->nid) && (w->nid >= min) &&
3070230130Smav		    (onlylength == 0 || onlylength == depth)) {
3071230130Smav			m = w->nid;
3072240428Smav			*length = depth;
3073230130Smav		}
3074230130Smav		break;
3075230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3076230130Smav		if (depth > 0)
3077230130Smav			break;
3078230130Smav		/* Fall */
3079230130Smav	default:
3080230130Smav		/* Try to find reachable ADCs with specified nid. */
3081230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3082230130Smav			wc = hdaa_widget_get(devinfo, j);
3083230130Smav			if (wc == NULL || wc->enable == 0)
3084230130Smav				continue;
3085230130Smav			im = -1;
3086230130Smav			for (i = 0; i < wc->nconns; i++) {
3087230130Smav				if (wc->connsenable[i] == 0)
3088230130Smav					continue;
3089230130Smav				if (wc->conns[i] != nid)
3090230130Smav					continue;
3091230130Smav				if ((ret = hdaa_audio_trace_adc(devinfo, as, seq,
3092230130Smav				    j, mixed, min, only, depth + 1,
3093230130Smav				    length, onlylength)) != 0) {
3094230130Smav					if (m == 0 || ret < m ||
3095240428Smav					    (ret == m && *length < lm)) {
3096230130Smav						m = ret;
3097230130Smav						im = i;
3098230130Smav						lm = *length;
3099240428Smav					} else
3100240428Smav						*length = lm;
3101230130Smav					if (only)
3102230130Smav						break;
3103230130Smav				}
3104230130Smav			}
3105230130Smav			if (im >= 0 && only && ((wc->nconns > 1 &&
3106230130Smav			    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3107230130Smav			    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3108230130Smav				wc->selconn = im;
3109230130Smav		}
3110230130Smav		break;
3111230130Smav	}
3112230130Smav	if (m && only) {
3113230130Smav		w->bindas = as;
3114230130Smav		w->bindseqmask |= (1 << seq);
3115230130Smav	}
3116230130Smav	HDA_BOOTHVERBOSE(
3117230130Smav		device_printf(devinfo->dev,
3118230130Smav		    " %*snid %d returned %d\n",
3119230130Smav			depth + 1, "", w->nid, m);
3120230130Smav	);
3121230130Smav	return (m);
3122230130Smav}
3123230130Smav
3124230130Smav/*
3125230130Smav * Erase trace path of the specified association.
3126230130Smav */
3127230130Smavstatic void
3128230130Smavhdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq)
3129230130Smav{
3130230130Smav	struct hdaa_widget *w;
3131230130Smav	int i;
3132230130Smav
3133230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3134230130Smav		w = hdaa_widget_get(devinfo, i);
3135230130Smav		if (w == NULL || w->enable == 0)
3136230130Smav			continue;
3137230130Smav		if (w->bindas == as) {
3138230130Smav			if (seq >= 0) {
3139230130Smav				w->bindseqmask &= ~(1 << seq);
3140230130Smav				if (w->bindseqmask == 0) {
3141230130Smav					w->bindas = -1;
3142230130Smav					w->selconn = -1;
3143230130Smav				}
3144230130Smav			} else {
3145230130Smav				w->bindas = -1;
3146230130Smav				w->bindseqmask = 0;
3147230130Smav				w->selconn = -1;
3148230130Smav			}
3149230130Smav		}
3150230130Smav	}
3151230130Smav}
3152230130Smav
3153230130Smav/*
3154230130Smav * Trace association path from DAC to output
3155230130Smav */
3156230130Smavstatic int
3157230130Smavhdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq)
3158230130Smav{
3159230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3160230130Smav	int i, hpredir;
3161230130Smav	nid_t min, res;
3162230130Smav
3163230130Smav	/* Find next pin */
3164230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3165230130Smav		;
3166230130Smav	/* Check if there is no any left. If so - we succeeded. */
3167230130Smav	if (i == 16)
3168230130Smav		return (1);
3169230130Smav
3170230130Smav	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
3171230130Smav	min = 0;
3172230130Smav	do {
3173230130Smav		HDA_BOOTHVERBOSE(
3174230130Smav			device_printf(devinfo->dev,
3175230130Smav			    " Tracing pin %d with min nid %d",
3176230130Smav			    ases[as].pins[i], min);
3177230130Smav			if (hpredir >= 0)
3178230130Smav				printf(" and hpredir %d", hpredir);
3179230130Smav			printf("\n");
3180230130Smav		);
3181230130Smav		/* Trace this pin taking min nid into account. */
3182230130Smav		res = hdaa_audio_trace_dac(devinfo, as, i,
3183230130Smav		    ases[as].pins[i], hpredir, min, 0, 0);
3184230130Smav		if (res == 0) {
3185230130Smav			/* If we failed - return to previous and redo it. */
3186230130Smav			HDA_BOOTVERBOSE(
3187230130Smav				device_printf(devinfo->dev,
3188230130Smav				    " Unable to trace pin %d seq %d with min "
3189230130Smav				    "nid %d",
3190230130Smav				    ases[as].pins[i], i, min);
3191230130Smav				if (hpredir >= 0)
3192230130Smav					printf(" and hpredir %d", hpredir);
3193230130Smav				printf("\n");
3194230130Smav			);
3195230130Smav			return (0);
3196230130Smav		}
3197230130Smav		HDA_BOOTVERBOSE(
3198230130Smav			device_printf(devinfo->dev,
3199230130Smav			    " Pin %d traced to DAC %d",
3200230130Smav			    ases[as].pins[i], res);
3201230130Smav			if (hpredir >= 0)
3202230130Smav				printf(" and hpredir %d", hpredir);
3203230130Smav			if (ases[as].fakeredir)
3204230130Smav				printf(" with fake redirection");
3205230130Smav			printf("\n");
3206230130Smav		);
3207230130Smav		/* Trace again to mark the path */
3208230130Smav		hdaa_audio_trace_dac(devinfo, as, i,
3209230130Smav		    ases[as].pins[i], hpredir, min, res, 0);
3210230130Smav		ases[as].dacs[0][i] = res;
3211230130Smav		/* We succeeded, so call next. */
3212230130Smav		if (hdaa_audio_trace_as_out(devinfo, as, i + 1))
3213230130Smav			return (1);
3214230130Smav		/* If next failed, we should retry with next min */
3215230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3216230130Smav		ases[as].dacs[0][i] = 0;
3217230130Smav		min = res + 1;
3218230130Smav	} while (1);
3219230130Smav}
3220230130Smav
3221230130Smav/*
3222230130Smav * Check equivalency of two DACs.
3223230130Smav */
3224230130Smavstatic int
3225230130Smavhdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3226230130Smav{
3227230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3228230130Smav	struct hdaa_widget *w3;
3229230130Smav	int i, j, c1, c2;
3230230130Smav
3231230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3232230130Smav		return (0);
3233230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3234230130Smav		w3 = hdaa_widget_get(devinfo, i);
3235230130Smav		if (w3 == NULL || w3->enable == 0)
3236230130Smav			continue;
3237230130Smav		if (w3->bindas != w1->bindas)
3238230130Smav			continue;
3239230130Smav		if (w3->nconns == 0)
3240230130Smav			continue;
3241230130Smav		c1 = c2 = -1;
3242230130Smav		for (j = 0; j < w3->nconns; j++) {
3243230130Smav			if (w3->connsenable[j] == 0)
3244230130Smav				continue;
3245230130Smav			if (w3->conns[j] == w1->nid)
3246230130Smav				c1 = j;
3247230130Smav			if (w3->conns[j] == w2->nid)
3248230130Smav				c2 = j;
3249230130Smav		}
3250230130Smav		if (c1 < 0)
3251230130Smav			continue;
3252230130Smav		if (c2 < 0)
3253230130Smav			return (0);
3254230130Smav		if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3255230130Smav			return (0);
3256230130Smav	}
3257230130Smav	return (1);
3258230130Smav}
3259230130Smav
3260230130Smav/*
3261230130Smav * Check equivalency of two ADCs.
3262230130Smav */
3263230130Smavstatic int
3264230130Smavhdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3265230130Smav{
3266230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3267230130Smav	struct hdaa_widget *w3, *w4;
3268230130Smav	int i;
3269230130Smav
3270230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3271230130Smav		return (0);
3272230130Smav	if (w1->nconns != 1 || w2->nconns != 1)
3273230130Smav		return (0);
3274230130Smav	if (w1->conns[0] == w2->conns[0])
3275230130Smav		return (1);
3276230130Smav	w3 = hdaa_widget_get(devinfo, w1->conns[0]);
3277230130Smav	if (w3 == NULL || w3->enable == 0)
3278230130Smav		return (0);
3279230130Smav	w4 = hdaa_widget_get(devinfo, w2->conns[0]);
3280230130Smav	if (w4 == NULL || w4->enable == 0)
3281230130Smav		return (0);
3282230130Smav	if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask)
3283230130Smav		return (1);
3284230130Smav	if (w4->bindas >= 0)
3285230130Smav		return (0);
3286230130Smav	if (w3->type != w4->type)
3287230130Smav		return (0);
3288230130Smav	if (memcmp(&w3->param, &w4->param, sizeof(w3->param)))
3289230130Smav		return (0);
3290230130Smav	if (w3->nconns != w4->nconns)
3291230130Smav		return (0);
3292230130Smav	for (i = 0; i < w3->nconns; i++) {
3293230130Smav		if (w3->conns[i] != w4->conns[i])
3294230130Smav			return (0);
3295230130Smav	}
3296230130Smav	return (1);
3297230130Smav}
3298230130Smav
3299230130Smav/*
3300230130Smav * Look for equivalent DAC/ADC to implement second channel.
3301230130Smav */
3302230130Smavstatic void
3303230130Smavhdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid)
3304230130Smav{
3305230130Smav	struct hdaa_audio_as *as = &devinfo->as[asid];
3306230130Smav	struct hdaa_widget *w1, *w2;
3307230130Smav	int i, pos;
3308230130Smav	nid_t nid1, nid2;
3309230130Smav
3310230130Smav	HDA_BOOTVERBOSE(
3311230130Smav		device_printf(devinfo->dev,
3312230130Smav		    "Looking for additional %sC "
3313230130Smav		    "for association %d (%d)\n",
3314230130Smav		    (as->dir == HDAA_CTL_OUT) ? "DA" : "AD",
3315230130Smav		    asid, as->index);
3316230130Smav	);
3317230130Smav
3318230130Smav	/* Find the exisitng DAC position and return if found more the one. */
3319230130Smav	pos = -1;
3320230130Smav	for (i = 0; i < 16; i++) {
3321230130Smav		if (as->dacs[0][i] <= 0)
3322230130Smav			continue;
3323230130Smav		if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos])
3324230130Smav			return;
3325230130Smav		pos = i;
3326230130Smav	}
3327230130Smav
3328230130Smav	nid1 = as->dacs[0][pos];
3329230130Smav	w1 = hdaa_widget_get(devinfo, nid1);
3330230130Smav	w2 = NULL;
3331230130Smav	for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) {
3332230130Smav		w2 = hdaa_widget_get(devinfo, nid2);
3333230130Smav		if (w2 == NULL || w2->enable == 0)
3334230130Smav			continue;
3335230130Smav		if (w2->bindas >= 0)
3336230130Smav			continue;
3337230130Smav		if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
3338230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
3339230130Smav				continue;
3340230130Smav			if (hdaa_audio_dacs_equal(w1, w2))
3341230130Smav				break;
3342230130Smav		} else {
3343230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3344230130Smav				continue;
3345230130Smav			if (hdaa_audio_adcs_equal(w1, w2))
3346230130Smav				break;
3347230130Smav		}
3348230130Smav	}
3349230130Smav	if (nid2 >= devinfo->endnode)
3350230130Smav		return;
3351230130Smav	w2->bindas = w1->bindas;
3352230130Smav	w2->bindseqmask = w1->bindseqmask;
3353230130Smav	if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
3354230130Smav		HDA_BOOTVERBOSE(
3355230130Smav			device_printf(devinfo->dev,
3356230130Smav			    " ADC %d considered equal to ADC %d\n", nid2, nid1);
3357230130Smav		);
3358230130Smav		w1 = hdaa_widget_get(devinfo, w1->conns[0]);
3359230130Smav		w2 = hdaa_widget_get(devinfo, w2->conns[0]);
3360230130Smav		w2->bindas = w1->bindas;
3361230130Smav		w2->bindseqmask = w1->bindseqmask;
3362230130Smav	} else {
3363230130Smav		HDA_BOOTVERBOSE(
3364230130Smav			device_printf(devinfo->dev,
3365230130Smav			    " DAC %d considered equal to DAC %d\n", nid2, nid1);
3366230130Smav		);
3367230130Smav	}
3368230130Smav	for (i = 0; i < 16; i++) {
3369230130Smav		if (as->dacs[0][i] <= 0)
3370230130Smav			continue;
3371230130Smav		as->dacs[as->num_chans][i] = nid2;
3372230130Smav	}
3373230130Smav	as->num_chans++;
3374230130Smav}
3375230130Smav
3376230130Smav/*
3377230130Smav * Trace association path from input to ADC
3378230130Smav */
3379230130Smavstatic int
3380230130Smavhdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as)
3381230130Smav{
3382230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3383230130Smav	struct hdaa_widget *w;
3384230130Smav	int i, j, k, length;
3385230130Smav
3386230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3387230130Smav		w = hdaa_widget_get(devinfo, j);
3388230130Smav		if (w == NULL || w->enable == 0)
3389230130Smav			continue;
3390230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3391230130Smav			continue;
3392230130Smav		if (w->bindas >= 0 && w->bindas != as)
3393230130Smav			continue;
3394230130Smav
3395230130Smav		/* Find next pin */
3396230130Smav		for (i = 0; i < 16; i++) {
3397230130Smav			if (ases[as].pins[i] == 0)
3398230130Smav				continue;
3399230130Smav
3400230130Smav			HDA_BOOTHVERBOSE(
3401230130Smav				device_printf(devinfo->dev,
3402230130Smav				    " Tracing pin %d to ADC %d\n",
3403230130Smav				    ases[as].pins[i], j);
3404230130Smav			);
3405230130Smav			/* Trace this pin taking goal into account. */
3406230130Smav			if (hdaa_audio_trace_adc(devinfo, as, i,
3407230130Smav			    ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) {
3408230130Smav				/* If we failed - return to previous and redo it. */
3409230130Smav				HDA_BOOTVERBOSE(
3410230130Smav					device_printf(devinfo->dev,
3411230130Smav					    " Unable to trace pin %d to ADC %d, undo traces\n",
3412230130Smav					    ases[as].pins[i], j);
3413230130Smav				);
3414230130Smav				hdaa_audio_undo_trace(devinfo, as, -1);
3415230130Smav				for (k = 0; k < 16; k++)
3416230130Smav					ases[as].dacs[0][k] = 0;
3417230130Smav				break;
3418230130Smav			}
3419230130Smav			HDA_BOOTVERBOSE(
3420230130Smav				device_printf(devinfo->dev,
3421230130Smav				    " Pin %d traced to ADC %d\n",
3422230130Smav				    ases[as].pins[i], j);
3423230130Smav			);
3424230130Smav			ases[as].dacs[0][i] = j;
3425230130Smav		}
3426230130Smav		if (i == 16)
3427230130Smav			return (1);
3428230130Smav	}
3429230130Smav	return (0);
3430230130Smav}
3431230130Smav
3432230130Smav/*
3433230130Smav * Trace association path from input to multiple ADCs
3434230130Smav */
3435230130Smavstatic int
3436230130Smavhdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq)
3437230130Smav{
3438230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3439230130Smav	int i, length;
3440230130Smav	nid_t min, res;
3441230130Smav
3442230130Smav	/* Find next pin */
3443230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3444230130Smav		;
3445230130Smav	/* Check if there is no any left. If so - we succeeded. */
3446230130Smav	if (i == 16)
3447230130Smav		return (1);
3448230130Smav
3449230130Smav	min = 0;
3450230130Smav	do {
3451230130Smav		HDA_BOOTHVERBOSE(
3452230130Smav			device_printf(devinfo->dev,
3453230130Smav			    " Tracing pin %d with min nid %d",
3454230130Smav			    ases[as].pins[i], min);
3455230130Smav			printf("\n");
3456230130Smav		);
3457230130Smav		/* Trace this pin taking min nid into account. */
3458230130Smav		res = hdaa_audio_trace_adc(devinfo, as, i,
3459230130Smav		    ases[as].pins[i], 0, min, 0, 0, &length, 0);
3460230130Smav		if (res == 0) {
3461230130Smav			/* If we failed - return to previous and redo it. */
3462230130Smav			HDA_BOOTVERBOSE(
3463230130Smav				device_printf(devinfo->dev,
3464230130Smav				    " Unable to trace pin %d seq %d with min "
3465230130Smav				    "nid %d",
3466230130Smav				    ases[as].pins[i], i, min);
3467230130Smav				printf("\n");
3468230130Smav			);
3469230130Smav			return (0);
3470230130Smav		}
3471230130Smav		HDA_BOOTVERBOSE(
3472230130Smav			device_printf(devinfo->dev,
3473230130Smav			    " Pin %d traced to ADC %d\n",
3474230130Smav			    ases[as].pins[i], res);
3475230130Smav		);
3476230130Smav		/* Trace again to mark the path */
3477230130Smav		hdaa_audio_trace_adc(devinfo, as, i,
3478230130Smav		    ases[as].pins[i], 0, min, res, 0, &length, length);
3479230130Smav		ases[as].dacs[0][i] = res;
3480230130Smav		/* We succeeded, so call next. */
3481230130Smav		if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1))
3482230130Smav			return (1);
3483230130Smav		/* If next failed, we should retry with next min */
3484230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3485230130Smav		ases[as].dacs[0][i] = 0;
3486230130Smav		min = res + 1;
3487230130Smav	} while (1);
3488230130Smav}
3489230130Smav
3490230130Smav/*
3491230130Smav * Trace input monitor path from mixer to output association.
3492230130Smav */
3493230130Smavstatic int
3494230130Smavhdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth)
3495230130Smav{
3496230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3497230130Smav	struct hdaa_widget *w, *wc;
3498230130Smav	int i, j;
3499230130Smav	nid_t res = 0;
3500230130Smav
3501230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3502230130Smav		return (0);
3503230130Smav	w = hdaa_widget_get(devinfo, nid);
3504230130Smav	if (w == NULL || w->enable == 0)
3505230130Smav		return (0);
3506230130Smav	HDA_BOOTHVERBOSE(
3507230130Smav		device_printf(devinfo->dev,
3508230130Smav		    " %*stracing via nid %d\n",
3509230130Smav			depth + 1, "", w->nid);
3510230130Smav	);
3511230130Smav	/* Use only unused widgets */
3512230130Smav	if (depth > 0 && w->bindas != -1) {
3513230130Smav		if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) {
3514230130Smav			HDA_BOOTHVERBOSE(
3515230130Smav				device_printf(devinfo->dev,
3516230130Smav				    " %*snid %d found output association %d\n",
3517230130Smav					depth + 1, "", w->nid, w->bindas);
3518230130Smav			);
3519230130Smav			if (w->bindas >= 0)
3520230130Smav				w->pflags |= HDAA_ADC_MONITOR;
3521230130Smav			return (1);
3522230130Smav		} else {
3523230130Smav			HDA_BOOTHVERBOSE(
3524230130Smav				device_printf(devinfo->dev,
3525230130Smav				    " %*snid %d busy by input association %d\n",
3526230130Smav					depth + 1, "", w->nid, w->bindas);
3527230130Smav			);
3528230130Smav			return (0);
3529230130Smav		}
3530230130Smav	}
3531230130Smav
3532230130Smav	switch (w->type) {
3533230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3534230130Smav		/* Do not traverse input. AD1988 has digital monitor
3535230130Smav		for which we are not ready. */
3536230130Smav		break;
3537230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3538230130Smav		if (depth > 0)
3539230130Smav			break;
3540230130Smav		/* Fall */
3541230130Smav	default:
3542230130Smav		/* Try to find reachable ADCs with specified nid. */
3543230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3544230130Smav			wc = hdaa_widget_get(devinfo, j);
3545230130Smav			if (wc == NULL || wc->enable == 0)
3546230130Smav				continue;
3547230130Smav			for (i = 0; i < wc->nconns; i++) {
3548230130Smav				if (wc->connsenable[i] == 0)
3549230130Smav					continue;
3550230130Smav				if (wc->conns[i] != nid)
3551230130Smav					continue;
3552230130Smav				if (hdaa_audio_trace_to_out(devinfo,
3553230130Smav				    j, depth + 1) != 0) {
3554230130Smav					res = 1;
3555230130Smav					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3556230130Smav					    wc->selconn == -1)
3557230130Smav						wc->selconn = i;
3558230130Smav				}
3559230130Smav			}
3560230130Smav		}
3561230130Smav		break;
3562230130Smav	}
3563230130Smav	if (res && w->bindas == -1)
3564230130Smav		w->bindas = -2;
3565230130Smav
3566230130Smav	HDA_BOOTHVERBOSE(
3567230130Smav		device_printf(devinfo->dev,
3568230130Smav		    " %*snid %d returned %d\n",
3569230130Smav			depth + 1, "", w->nid, res);
3570230130Smav	);
3571230130Smav	return (res);
3572230130Smav}
3573230130Smav
3574230130Smav/*
3575230130Smav * Trace extra associations (beeper, monitor)
3576230130Smav */
3577230130Smavstatic void
3578230130Smavhdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo)
3579230130Smav{
3580230130Smav	struct hdaa_audio_as *as = devinfo->as;
3581230130Smav	struct hdaa_widget *w;
3582230130Smav	int j;
3583230130Smav
3584230130Smav	/* Input monitor */
3585230130Smav	/* Find mixer associated with input, but supplying signal
3586230130Smav	   for output associations. Hope it will be input monitor. */
3587230130Smav	HDA_BOOTVERBOSE(
3588230130Smav		device_printf(devinfo->dev,
3589230130Smav		    "Tracing input monitor\n");
3590230130Smav	);
3591230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3592230130Smav		w = hdaa_widget_get(devinfo, j);
3593230130Smav		if (w == NULL || w->enable == 0)
3594230130Smav			continue;
3595230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3596230130Smav			continue;
3597230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3598230130Smav			continue;
3599230130Smav		HDA_BOOTVERBOSE(
3600230130Smav			device_printf(devinfo->dev,
3601230130Smav			    " Tracing nid %d to out\n",
3602230130Smav			    j);
3603230130Smav		);
3604230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3605230130Smav			HDA_BOOTVERBOSE(
3606230130Smav				device_printf(devinfo->dev,
3607230130Smav				    " nid %d is input monitor\n",
3608230130Smav					w->nid);
3609230130Smav			);
3610230130Smav			w->ossdev = SOUND_MIXER_IMIX;
3611230130Smav		}
3612230130Smav	}
3613230130Smav
3614230130Smav	/* Other inputs monitor */
3615230130Smav	/* Find input pins supplying signal for output associations.
3616230130Smav	   Hope it will be input monitoring. */
3617230130Smav	HDA_BOOTVERBOSE(
3618230130Smav		device_printf(devinfo->dev,
3619230130Smav		    "Tracing other input monitors\n");
3620230130Smav	);
3621230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3622230130Smav		w = hdaa_widget_get(devinfo, j);
3623230130Smav		if (w == NULL || w->enable == 0)
3624230130Smav			continue;
3625230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3626230130Smav			continue;
3627230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3628230130Smav			continue;
3629230130Smav		HDA_BOOTVERBOSE(
3630230130Smav			device_printf(devinfo->dev,
3631230130Smav			    " Tracing nid %d to out\n",
3632230130Smav			    j);
3633230130Smav		);
3634230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3635230130Smav			HDA_BOOTVERBOSE(
3636230130Smav				device_printf(devinfo->dev,
3637230130Smav				    " nid %d is input monitor\n",
3638230130Smav					w->nid);
3639230130Smav			);
3640230130Smav		}
3641230130Smav	}
3642230130Smav
3643230130Smav	/* Beeper */
3644230130Smav	HDA_BOOTVERBOSE(
3645230130Smav		device_printf(devinfo->dev,
3646230130Smav		    "Tracing beeper\n");
3647230130Smav	);
3648230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3649230130Smav		w = hdaa_widget_get(devinfo, j);
3650230130Smav		if (w == NULL || w->enable == 0)
3651230130Smav			continue;
3652230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
3653230130Smav			continue;
3654230130Smav		HDA_BOOTHVERBOSE(
3655230130Smav			device_printf(devinfo->dev,
3656230130Smav			    " Tracing nid %d to out\n",
3657230130Smav			    j);
3658230130Smav		);
3659230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3660230130Smav			HDA_BOOTVERBOSE(
3661230130Smav				device_printf(devinfo->dev,
3662230130Smav				    " nid %d traced to out\n",
3663230130Smav				    j);
3664230130Smav			);
3665230130Smav		}
3666230130Smav		w->bindas = -2;
3667230130Smav	}
3668230130Smav}
3669230130Smav
3670230130Smav/*
3671230130Smav * Bind assotiations to PCM channels
3672230130Smav */
3673230130Smavstatic void
3674230130Smavhdaa_audio_bind_as(struct hdaa_devinfo *devinfo)
3675230130Smav{
3676230130Smav	struct hdaa_audio_as *as = devinfo->as;
3677230130Smav	int i, j, cnt = 0, free;
3678230130Smav
3679230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
3680230130Smav		if (as[j].enable)
3681230130Smav			cnt += as[j].num_chans;
3682230130Smav	}
3683230130Smav	if (devinfo->num_chans == 0) {
3684230130Smav		devinfo->chans = (struct hdaa_chan *)malloc(
3685230130Smav		    sizeof(struct hdaa_chan) * cnt,
3686230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
3687230130Smav		if (devinfo->chans == NULL) {
3688230130Smav			device_printf(devinfo->dev,
3689230130Smav			    "Channels memory allocation failed!\n");
3690230130Smav			return;
3691230130Smav		}
3692230130Smav	} else {
3693230130Smav		devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans,
3694230130Smav		    sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt),
3695230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
3696230130Smav		if (devinfo->chans == NULL) {
3697230130Smav			devinfo->num_chans = 0;
3698230130Smav			device_printf(devinfo->dev,
3699230130Smav			    "Channels memory allocation failed!\n");
3700230130Smav			return;
3701230130Smav		}
3702230130Smav		/* Fixup relative pointers after realloc */
3703230130Smav		for (j = 0; j < devinfo->num_chans; j++)
3704230130Smav			devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist;
3705230130Smav	}
3706230130Smav	free = devinfo->num_chans;
3707230130Smav	devinfo->num_chans += cnt;
3708230130Smav
3709230130Smav	for (j = free; j < free + cnt; j++) {
3710230130Smav		devinfo->chans[j].devinfo = devinfo;
3711230130Smav		devinfo->chans[j].as = -1;
3712230130Smav	}
3713230130Smav
3714230130Smav	/* Assign associations in order of their numbers, */
3715230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
3716230130Smav		if (as[j].enable == 0)
3717230130Smav			continue;
3718230130Smav		for (i = 0; i < as[j].num_chans; i++) {
3719230130Smav			devinfo->chans[free].as = j;
3720230130Smav			devinfo->chans[free].asindex = i;
3721230130Smav			devinfo->chans[free].dir =
3722230130Smav			    (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
3723230130Smav			hdaa_pcmchannel_setup(&devinfo->chans[free]);
3724230130Smav			as[j].chans[i] = free;
3725230130Smav			free++;
3726230130Smav		}
3727230130Smav	}
3728230130Smav}
3729230130Smav
3730230130Smavstatic void
3731230130Smavhdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo)
3732230130Smav{
3733230130Smav	struct hdaa_widget *w;
3734230130Smav	int i;
3735230130Smav
3736230130Smav	/* Disable power and volume widgets. */
3737230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3738230130Smav		w = hdaa_widget_get(devinfo, i);
3739230130Smav		if (w == NULL || w->enable == 0)
3740230130Smav			continue;
3741230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
3742230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
3743230130Smav			w->enable = 0;
3744230130Smav			HDA_BOOTHVERBOSE(
3745230130Smav				device_printf(devinfo->dev,
3746230130Smav				    " Disabling nid %d due to it's"
3747230130Smav				    " non-audio type.\n",
3748230130Smav				    w->nid);
3749230130Smav			);
3750230130Smav		}
3751230130Smav	}
3752230130Smav}
3753230130Smav
3754230130Smavstatic void
3755230130Smavhdaa_audio_disable_useless(struct hdaa_devinfo *devinfo)
3756230130Smav{
3757230130Smav	struct hdaa_widget *w, *cw;
3758230130Smav	struct hdaa_audio_ctl *ctl;
3759230130Smav	int done, found, i, j, k;
3760230130Smav
3761230130Smav	/* Disable useless pins. */
3762230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3763230130Smav		w = hdaa_widget_get(devinfo, i);
3764230130Smav		if (w == NULL || w->enable == 0)
3765230130Smav			continue;
3766230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
3767230130Smav			if ((w->wclass.pin.config &
3768230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
3769230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
3770230130Smav				w->enable = 0;
3771230130Smav				HDA_BOOTHVERBOSE(
3772230130Smav					device_printf(devinfo->dev,
3773230130Smav					    " Disabling pin nid %d due"
3774230130Smav					    " to None connectivity.\n",
3775230130Smav					    w->nid);
3776230130Smav				);
3777230130Smav			} else if ((w->wclass.pin.config &
3778230130Smav			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
3779230130Smav				w->enable = 0;
3780230130Smav				HDA_BOOTHVERBOSE(
3781230130Smav					device_printf(devinfo->dev,
3782230130Smav					    " Disabling unassociated"
3783230130Smav					    " pin nid %d.\n",
3784230130Smav					    w->nid);
3785230130Smav				);
3786230130Smav			}
3787230130Smav		}
3788230130Smav	}
3789230130Smav	do {
3790230130Smav		done = 1;
3791230130Smav		/* Disable and mute controls for disabled widgets. */
3792230130Smav		i = 0;
3793230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
3794230130Smav			if (ctl->enable == 0)
3795230130Smav				continue;
3796230130Smav			if (ctl->widget->enable == 0 ||
3797230130Smav			    (ctl->childwidget != NULL &&
3798230130Smav			    ctl->childwidget->enable == 0)) {
3799230130Smav				ctl->forcemute = 1;
3800230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
3801230130Smav				ctl->left = 0;
3802230130Smav				ctl->right = 0;
3803230130Smav				ctl->enable = 0;
3804230130Smav				if (ctl->ndir == HDAA_CTL_IN)
3805230130Smav					ctl->widget->connsenable[ctl->index] = 0;
3806230130Smav				done = 0;
3807230130Smav				HDA_BOOTHVERBOSE(
3808230130Smav					device_printf(devinfo->dev,
3809230130Smav					    " Disabling ctl %d nid %d cnid %d due"
3810230130Smav					    " to disabled widget.\n", i,
3811230130Smav					    ctl->widget->nid,
3812230130Smav					    (ctl->childwidget != NULL)?
3813230130Smav					    ctl->childwidget->nid:-1);
3814230130Smav				);
3815230130Smav			}
3816230130Smav		}
3817230130Smav		/* Disable useless widgets. */
3818230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3819230130Smav			w = hdaa_widget_get(devinfo, i);
3820230130Smav			if (w == NULL || w->enable == 0)
3821230130Smav				continue;
3822230130Smav			/* Disable inputs with disabled child widgets. */
3823230130Smav			for (j = 0; j < w->nconns; j++) {
3824230130Smav				if (w->connsenable[j]) {
3825230130Smav					cw = hdaa_widget_get(devinfo, w->conns[j]);
3826230130Smav					if (cw == NULL || cw->enable == 0) {
3827230130Smav						w->connsenable[j] = 0;
3828230130Smav						HDA_BOOTHVERBOSE(
3829230130Smav							device_printf(devinfo->dev,
3830230130Smav							    " Disabling nid %d connection %d due"
3831230130Smav							    " to disabled child widget.\n",
3832230130Smav							    i, j);
3833230130Smav						);
3834230130Smav					}
3835230130Smav				}
3836230130Smav			}
3837230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3838230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3839230130Smav				continue;
3840230130Smav			/* Disable mixers and selectors without inputs. */
3841230130Smav			found = 0;
3842230130Smav			for (j = 0; j < w->nconns; j++) {
3843230130Smav				if (w->connsenable[j]) {
3844230130Smav					found = 1;
3845230130Smav					break;
3846230130Smav				}
3847230130Smav			}
3848230130Smav			if (found == 0) {
3849230130Smav				w->enable = 0;
3850230130Smav				done = 0;
3851230130Smav				HDA_BOOTHVERBOSE(
3852230130Smav					device_printf(devinfo->dev,
3853230130Smav					    " Disabling nid %d due to all it's"
3854230130Smav					    " inputs disabled.\n", w->nid);
3855230130Smav				);
3856230130Smav			}
3857230130Smav			/* Disable nodes without consumers. */
3858230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3859230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3860230130Smav				continue;
3861230130Smav			found = 0;
3862230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
3863230130Smav				cw = hdaa_widget_get(devinfo, k);
3864230130Smav				if (cw == NULL || cw->enable == 0)
3865230130Smav					continue;
3866230130Smav				for (j = 0; j < cw->nconns; j++) {
3867230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
3868230130Smav						found = 1;
3869230130Smav						break;
3870230130Smav					}
3871230130Smav				}
3872230130Smav			}
3873230130Smav			if (found == 0) {
3874230130Smav				w->enable = 0;
3875230130Smav				done = 0;
3876230130Smav				HDA_BOOTHVERBOSE(
3877230130Smav					device_printf(devinfo->dev,
3878230130Smav					    " Disabling nid %d due to all it's"
3879230130Smav					    " consumers disabled.\n", w->nid);
3880230130Smav				);
3881230130Smav			}
3882230130Smav		}
3883230130Smav	} while (done == 0);
3884230130Smav
3885230130Smav}
3886230130Smav
3887230130Smavstatic void
3888230130Smavhdaa_audio_disable_unas(struct hdaa_devinfo *devinfo)
3889230130Smav{
3890230130Smav	struct hdaa_audio_as *as = devinfo->as;
3891230130Smav	struct hdaa_widget *w, *cw;
3892230130Smav	struct hdaa_audio_ctl *ctl;
3893230130Smav	int i, j, k;
3894230130Smav
3895230130Smav	/* Disable unassosiated widgets. */
3896230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3897230130Smav		w = hdaa_widget_get(devinfo, i);
3898230130Smav		if (w == NULL || w->enable == 0)
3899230130Smav			continue;
3900230130Smav		if (w->bindas == -1) {
3901230130Smav			w->enable = 0;
3902230130Smav			HDA_BOOTHVERBOSE(
3903230130Smav				device_printf(devinfo->dev,
3904230130Smav				    " Disabling unassociated nid %d.\n",
3905230130Smav				    w->nid);
3906230130Smav			);
3907230130Smav		}
3908230130Smav	}
3909230130Smav	/* Disable input connections on input pin and
3910230130Smav	 * output on output. */
3911230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3912230130Smav		w = hdaa_widget_get(devinfo, i);
3913230130Smav		if (w == NULL || w->enable == 0)
3914230130Smav			continue;
3915230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3916230130Smav			continue;
3917230130Smav		if (w->bindas < 0)
3918230130Smav			continue;
3919230130Smav		if (as[w->bindas].dir == HDAA_CTL_IN) {
3920230130Smav			for (j = 0; j < w->nconns; j++) {
3921230130Smav				if (w->connsenable[j] == 0)
3922230130Smav					continue;
3923230130Smav				w->connsenable[j] = 0;
3924230130Smav				HDA_BOOTHVERBOSE(
3925230130Smav					device_printf(devinfo->dev,
3926230130Smav					    " Disabling connection to input pin "
3927230130Smav					    "nid %d conn %d.\n",
3928230130Smav					    i, j);
3929230130Smav				);
3930230130Smav			}
3931230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
3932230130Smav			    HDAA_CTL_IN, -1, 1);
3933230130Smav			if (ctl && ctl->enable) {
3934230130Smav				ctl->forcemute = 1;
3935230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
3936230130Smav				ctl->left = 0;
3937230130Smav				ctl->right = 0;
3938230130Smav				ctl->enable = 0;
3939230130Smav			}
3940230130Smav		} else {
3941230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
3942230130Smav			    HDAA_CTL_OUT, -1, 1);
3943230130Smav			if (ctl && ctl->enable) {
3944230130Smav				ctl->forcemute = 1;
3945230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
3946230130Smav				ctl->left = 0;
3947230130Smav				ctl->right = 0;
3948230130Smav				ctl->enable = 0;
3949230130Smav			}
3950230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
3951230130Smav				cw = hdaa_widget_get(devinfo, k);
3952230130Smav				if (cw == NULL || cw->enable == 0)
3953230130Smav					continue;
3954230130Smav				for (j = 0; j < cw->nconns; j++) {
3955230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
3956230130Smav						cw->connsenable[j] = 0;
3957230130Smav						HDA_BOOTHVERBOSE(
3958230130Smav							device_printf(devinfo->dev,
3959230130Smav							    " Disabling connection from output pin "
3960230130Smav							    "nid %d conn %d cnid %d.\n",
3961230130Smav							    k, j, i);
3962230130Smav						);
3963230130Smav						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
3964230130Smav						    cw->nconns > 1)
3965230130Smav							continue;
3966230130Smav						ctl = hdaa_audio_ctl_amp_get(devinfo, k,
3967230130Smav					    HDAA_CTL_IN, j, 1);
3968230130Smav						if (ctl && ctl->enable) {
3969230130Smav							ctl->forcemute = 1;
3970230130Smav							ctl->muted = HDAA_AMP_MUTE_ALL;
3971230130Smav							ctl->left = 0;
3972230130Smav							ctl->right = 0;
3973230130Smav							ctl->enable = 0;
3974230130Smav						}
3975230130Smav					}
3976230130Smav				}
3977230130Smav			}
3978230130Smav		}
3979230130Smav	}
3980230130Smav}
3981230130Smav
3982230130Smavstatic void
3983230130Smavhdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo)
3984230130Smav{
3985230130Smav	struct hdaa_audio_as *as = devinfo->as;
3986230130Smav	struct hdaa_widget *w;
3987230130Smav	int i, j;
3988230130Smav
3989230130Smav	/* On playback path we can safely disable all unseleted inputs. */
3990230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3991230130Smav		w = hdaa_widget_get(devinfo, i);
3992230130Smav		if (w == NULL || w->enable == 0)
3993230130Smav			continue;
3994230130Smav		if (w->nconns <= 1)
3995230130Smav			continue;
3996230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3997230130Smav			continue;
3998230130Smav		if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN)
3999230130Smav			continue;
4000230130Smav		for (j = 0; j < w->nconns; j++) {
4001230130Smav			if (w->connsenable[j] == 0)
4002230130Smav				continue;
4003230130Smav			if (w->selconn < 0 || w->selconn == j)
4004230130Smav				continue;
4005230130Smav			w->connsenable[j] = 0;
4006230130Smav			HDA_BOOTHVERBOSE(
4007230130Smav				device_printf(devinfo->dev,
4008230130Smav				    " Disabling unselected connection "
4009230130Smav				    "nid %d conn %d.\n",
4010230130Smav				    i, j);
4011230130Smav			);
4012230130Smav		}
4013230130Smav	}
4014230130Smav}
4015230130Smav
4016230130Smavstatic void
4017230130Smavhdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo)
4018230130Smav{
4019230130Smav	struct hdaa_audio_as *ases = devinfo->as;
4020230130Smav	struct hdaa_widget *w, *cw;
4021230130Smav	struct hdaa_audio_ctl *ctl;
4022230130Smav	int i, j;
4023230130Smav
4024230130Smav	/* Disable crossassociatement and unwanted crosschannel connections. */
4025230130Smav	/* ... using selectors */
4026230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4027230130Smav		w = hdaa_widget_get(devinfo, i);
4028230130Smav		if (w == NULL || w->enable == 0)
4029230130Smav			continue;
4030230130Smav		if (w->nconns <= 1)
4031230130Smav			continue;
4032230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4033230130Smav			continue;
4034230130Smav		/* Allow any -> mix */
4035230130Smav		if (w->bindas == -2)
4036230130Smav			continue;
4037230130Smav		for (j = 0; j < w->nconns; j++) {
4038230130Smav			if (w->connsenable[j] == 0)
4039230130Smav				continue;
4040230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
4041230130Smav			if (cw == NULL || w->enable == 0)
4042230130Smav				continue;
4043230130Smav			/* Allow mix -> out. */
4044230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
4045230130Smav			    ases[w->bindas].dir == HDAA_CTL_OUT)
4046230130Smav				continue;
4047230130Smav			/* Allow mix -> mixed-in. */
4048230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
4049230130Smav			    ases[w->bindas].mixed)
4050230130Smav				continue;
4051230130Smav			/* Allow in -> mix. */
4052230130Smav			if ((w->pflags & HDAA_ADC_MONITOR) &&
4053230130Smav			     cw->bindas >= 0 &&
4054230130Smav			     ases[cw->bindas].dir == HDAA_CTL_IN)
4055230130Smav				continue;
4056230130Smav			/* Allow if have common as/seqs. */
4057230130Smav			if (w->bindas == cw->bindas &&
4058230130Smav			    (w->bindseqmask & cw->bindseqmask) != 0)
4059230130Smav				continue;
4060230130Smav			w->connsenable[j] = 0;
4061230130Smav			HDA_BOOTHVERBOSE(
4062230130Smav				device_printf(devinfo->dev,
4063230130Smav				    " Disabling crossassociatement connection "
4064230130Smav				    "nid %d conn %d cnid %d.\n",
4065230130Smav				    i, j, cw->nid);
4066230130Smav			);
4067230130Smav		}
4068230130Smav	}
4069230130Smav	/* ... using controls */
4070230130Smav	i = 0;
4071230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4072230130Smav		if (ctl->enable == 0 || ctl->childwidget == NULL)
4073230130Smav			continue;
4074230130Smav		/* Allow any -> mix */
4075230130Smav		if (ctl->widget->bindas == -2)
4076230130Smav			continue;
4077230130Smav		/* Allow mix -> out. */
4078230130Smav		if (ctl->childwidget->bindas == -2 &&
4079230130Smav		    ctl->widget->bindas >= 0 &&
4080230130Smav		    ases[ctl->widget->bindas].dir == HDAA_CTL_OUT)
4081230130Smav			continue;
4082230130Smav		/* Allow mix -> mixed-in. */
4083230130Smav		if (ctl->childwidget->bindas == -2 &&
4084230130Smav		    ctl->widget->bindas >= 0 &&
4085230130Smav		    ases[ctl->widget->bindas].mixed)
4086230130Smav			continue;
4087230130Smav		/* Allow in -> mix. */
4088230130Smav		if ((ctl->widget->pflags & HDAA_ADC_MONITOR) &&
4089230130Smav		    ctl->childwidget->bindas >= 0 &&
4090230130Smav		    ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN)
4091230130Smav			continue;
4092230130Smav		/* Allow if have common as/seqs. */
4093230130Smav		if (ctl->widget->bindas == ctl->childwidget->bindas &&
4094230130Smav		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0)
4095230130Smav			continue;
4096230130Smav		ctl->forcemute = 1;
4097230130Smav		ctl->muted = HDAA_AMP_MUTE_ALL;
4098230130Smav		ctl->left = 0;
4099230130Smav		ctl->right = 0;
4100230130Smav		ctl->enable = 0;
4101230130Smav		if (ctl->ndir == HDAA_CTL_IN)
4102230130Smav			ctl->widget->connsenable[ctl->index] = 0;
4103230130Smav		HDA_BOOTHVERBOSE(
4104230130Smav			device_printf(devinfo->dev,
4105230130Smav			    " Disabling crossassociatement connection "
4106230130Smav			    "ctl %d nid %d cnid %d.\n", i,
4107230130Smav			    ctl->widget->nid,
4108230130Smav			    ctl->childwidget->nid);
4109230130Smav		);
4110230130Smav	}
4111230130Smav
4112230130Smav}
4113230130Smav
4114230130Smav/*
4115232798Smav * Find controls to control amplification for source and calculate possible
4116232798Smav * amplification range.
4117230130Smav */
4118230130Smavstatic int
4119230130Smavhdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4120232798Smav    int ossdev, int ctlable, int depth, int *minamp, int *maxamp)
4121230130Smav{
4122230130Smav	struct hdaa_widget *w, *wc;
4123230130Smav	struct hdaa_audio_ctl *ctl;
4124232798Smav	int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4125230130Smav
4126230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4127232798Smav		return (found);
4128230130Smav
4129230130Smav	w = hdaa_widget_get(devinfo, nid);
4130230130Smav	if (w == NULL || w->enable == 0)
4131232798Smav		return (found);
4132230130Smav
4133230130Smav	/* Count number of active inputs. */
4134230130Smav	if (depth > 0) {
4135230130Smav		for (j = 0; j < w->nconns; j++) {
4136232798Smav			if (!w->connsenable[j])
4137232798Smav				continue;
4138232798Smav			conns++;
4139230130Smav		}
4140230130Smav	}
4141230130Smav
4142230130Smav	/* If this is not a first step - use input mixer.
4143230130Smav	   Pins have common input ctl so care must be taken. */
4144230130Smav	if (depth > 0 && ctlable && (conns == 1 ||
4145230130Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
4146230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
4147230130Smav		    index, 1);
4148230130Smav		if (ctl) {
4149232798Smav			ctl->ossmask |= (1 << ossdev);
4150232798Smav			found++;
4151232798Smav			if (*minamp == *maxamp) {
4152232798Smav				*minamp += MINQDB(ctl);
4153232798Smav				*maxamp += MAXQDB(ctl);
4154232798Smav			}
4155230130Smav		}
4156230130Smav	}
4157230130Smav
4158230130Smav	/* If widget has own ossdev - not traverse it.
4159230130Smav	   It will be traversed on it's own. */
4160230130Smav	if (w->ossdev >= 0 && depth > 0)
4161232798Smav		return (found);
4162230130Smav
4163230130Smav	/* We must not traverse pin */
4164230130Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4165230130Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
4166230130Smav	    depth > 0)
4167232798Smav		return (found);
4168230130Smav
4169230130Smav	/* record that this widget exports such signal, */
4170230130Smav	w->ossmask |= (1 << ossdev);
4171230130Smav
4172232798Smav	/*
4173232798Smav	 * If signals mixed, we can't assign controls farther.
4174230130Smav	 * Ignore this on depth zero. Caller must knows why.
4175230130Smav	 */
4176232798Smav	if (conns > 1 &&
4177232798Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4178230130Smav		ctlable = 0;
4179230130Smav
4180230130Smav	if (ctlable) {
4181230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
4182230130Smav		if (ctl) {
4183232798Smav			ctl->ossmask |= (1 << ossdev);
4184232798Smav			found++;
4185232798Smav			if (*minamp == *maxamp) {
4186232798Smav				*minamp += MINQDB(ctl);
4187232798Smav				*maxamp += MAXQDB(ctl);
4188232798Smav			}
4189230130Smav		}
4190230130Smav	}
4191230130Smav
4192232798Smav	cminamp = cmaxamp = 0;
4193230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4194230130Smav		wc = hdaa_widget_get(devinfo, i);
4195230130Smav		if (wc == NULL || wc->enable == 0)
4196230130Smav			continue;
4197230130Smav		for (j = 0; j < wc->nconns; j++) {
4198230130Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
4199232798Smav				tminamp = tmaxamp = 0;
4200232798Smav				found += hdaa_audio_ctl_source_amp(devinfo,
4201232798Smav				    wc->nid, j, ossdev, ctlable, depth + 1,
4202232798Smav				    &tminamp, &tmaxamp);
4203232798Smav				if (cminamp == 0 && cmaxamp == 0) {
4204232798Smav					cminamp = tminamp;
4205232798Smav					cmaxamp = tmaxamp;
4206232798Smav				} else if (tminamp != tmaxamp) {
4207232798Smav					cminamp = imax(cminamp, tminamp);
4208232798Smav					cmaxamp = imin(cmaxamp, tmaxamp);
4209232798Smav				}
4210230130Smav			}
4211230130Smav		}
4212230130Smav	}
4213232798Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4214232798Smav		*minamp += cminamp;
4215232798Smav		*maxamp += cmaxamp;
4216232798Smav	}
4217232798Smav	return (found);
4218230130Smav}
4219230130Smav
4220230130Smav/*
4221232798Smav * Find controls to control amplification for destination and calculate
4222232798Smav * possible amplification range.
4223230130Smav */
4224232798Smavstatic int
4225230130Smavhdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4226232798Smav    int ossdev, int depth, int *minamp, int *maxamp)
4227230130Smav{
4228230130Smav	struct hdaa_audio_as *as = devinfo->as;
4229230130Smav	struct hdaa_widget *w, *wc;
4230230130Smav	struct hdaa_audio_ctl *ctl;
4231232798Smav	int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4232230130Smav
4233230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4234232798Smav		return (found);
4235230130Smav
4236230130Smav	w = hdaa_widget_get(devinfo, nid);
4237230130Smav	if (w == NULL || w->enable == 0)
4238232798Smav		return (found);
4239230130Smav
4240230130Smav	if (depth > 0) {
4241230130Smav		/* If this node produce output for several consumers,
4242230130Smav		   we can't touch it. */
4243230130Smav		consumers = 0;
4244230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4245230130Smav			wc = hdaa_widget_get(devinfo, i);
4246230130Smav			if (wc == NULL || wc->enable == 0)
4247230130Smav				continue;
4248230130Smav			for (j = 0; j < wc->nconns; j++) {
4249230130Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
4250230130Smav					consumers++;
4251230130Smav			}
4252230130Smav		}
4253230130Smav		/* The only exception is if real HP redirection is configured
4254230130Smav		   and this is a duplication point.
4255230130Smav		   XXX: Actually exception is not completely correct.
4256230130Smav		   XXX: Duplication point check is not perfect. */
4257230130Smav		if ((consumers == 2 && (w->bindas < 0 ||
4258230130Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
4259230130Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
4260230130Smav		    consumers > 2)
4261232798Smav			return (found);
4262230130Smav
4263230130Smav		/* Else use it's output mixer. */
4264230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4265230130Smav		    HDAA_CTL_OUT, -1, 1);
4266230130Smav		if (ctl) {
4267232798Smav			ctl->ossmask |= (1 << ossdev);
4268232798Smav			found++;
4269232798Smav			if (*minamp == *maxamp) {
4270232798Smav				*minamp += MINQDB(ctl);
4271232798Smav				*maxamp += MAXQDB(ctl);
4272232798Smav			}
4273230130Smav		}
4274230130Smav	}
4275230130Smav
4276230130Smav	/* We must not traverse pin */
4277230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4278230130Smav	    depth > 0)
4279232798Smav		return (found);
4280230130Smav
4281232798Smav	cminamp = cmaxamp = 0;
4282230130Smav	for (i = 0; i < w->nconns; i++) {
4283230130Smav		if (w->connsenable[i] == 0)
4284230130Smav			continue;
4285230130Smav		if (index >= 0 && i != index)
4286230130Smav			continue;
4287232798Smav		tminamp = tmaxamp = 0;
4288230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4289230130Smav		    HDAA_CTL_IN, i, 1);
4290230130Smav		if (ctl) {
4291232798Smav			ctl->ossmask |= (1 << ossdev);
4292232798Smav			found++;
4293232798Smav			if (*minamp == *maxamp) {
4294232798Smav				tminamp += MINQDB(ctl);
4295232798Smav				tmaxamp += MAXQDB(ctl);
4296232798Smav			}
4297230130Smav		}
4298232798Smav		found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
4299232798Smav		    depth + 1, &tminamp, &tmaxamp);
4300232798Smav		if (cminamp == 0 && cmaxamp == 0) {
4301232798Smav			cminamp = tminamp;
4302232798Smav			cmaxamp = tmaxamp;
4303232798Smav		} else if (tminamp != tmaxamp) {
4304232798Smav			cminamp = imax(cminamp, tminamp);
4305232798Smav			cmaxamp = imin(cmaxamp, tmaxamp);
4306232798Smav		}
4307230130Smav	}
4308232798Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4309232798Smav		*minamp += cminamp;
4310232798Smav		*maxamp += cmaxamp;
4311232798Smav	}
4312232798Smav	return (found);
4313230130Smav}
4314230130Smav
4315230130Smav/*
4316230130Smav * Assign OSS names to sound sources
4317230130Smav */
4318230130Smavstatic void
4319230130Smavhdaa_audio_assign_names(struct hdaa_devinfo *devinfo)
4320230130Smav{
4321230130Smav	struct hdaa_audio_as *as = devinfo->as;
4322230130Smav	struct hdaa_widget *w;
4323230130Smav	int i, j;
4324230130Smav	int type = -1, use, used = 0;
4325230130Smav	static const int types[7][13] = {
4326230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4327230130Smav	      SOUND_MIXER_LINE3, -1 },	/* line */
4328230130Smav	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
4329230130Smav	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
4330230130Smav	    { SOUND_MIXER_CD, -1 },	/* cd */
4331230130Smav	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
4332230130Smav	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
4333230130Smav	      -1 },	/* digital */
4334230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4335230130Smav	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
4336230130Smav	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
4337230130Smav	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
4338230130Smav	      -1 }	/* others */
4339230130Smav	};
4340230130Smav
4341230130Smav	/* Surely known names */
4342230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4343230130Smav		w = hdaa_widget_get(devinfo, i);
4344230130Smav		if (w == NULL || w->enable == 0)
4345230130Smav			continue;
4346230130Smav		if (w->bindas == -1)
4347230130Smav			continue;
4348230130Smav		use = -1;
4349230130Smav		switch (w->type) {
4350230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4351230130Smav			if (as[w->bindas].dir == HDAA_CTL_OUT)
4352230130Smav				break;
4353230130Smav			type = -1;
4354230130Smav			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4355230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4356230130Smav				type = 0;
4357230130Smav				break;
4358230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4359230130Smav				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
4360230130Smav				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4361230130Smav					break;
4362230130Smav				type = 1;
4363230130Smav				break;
4364230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4365230130Smav				type = 3;
4366230130Smav				break;
4367230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4368230130Smav				type = 4;
4369230130Smav				break;
4370230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
4371230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
4372230130Smav				type = 5;
4373230130Smav				break;
4374230130Smav			}
4375230130Smav			if (type == -1)
4376230130Smav				break;
4377230130Smav			j = 0;
4378230130Smav			while (types[type][j] >= 0 &&
4379230130Smav			    (used & (1 << types[type][j])) != 0) {
4380230130Smav				j++;
4381230130Smav			}
4382230130Smav			if (types[type][j] >= 0)
4383230130Smav				use = types[type][j];
4384230130Smav			break;
4385230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4386230130Smav			use = SOUND_MIXER_PCM;
4387230130Smav			break;
4388230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
4389230130Smav			use = SOUND_MIXER_SPEAKER;
4390230130Smav			break;
4391230130Smav		default:
4392230130Smav			break;
4393230130Smav		}
4394230130Smav		if (use >= 0) {
4395230130Smav			w->ossdev = use;
4396230130Smav			used |= (1 << use);
4397230130Smav		}
4398230130Smav	}
4399230130Smav	/* Semi-known names */
4400230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4401230130Smav		w = hdaa_widget_get(devinfo, i);
4402230130Smav		if (w == NULL || w->enable == 0)
4403230130Smav			continue;
4404230130Smav		if (w->ossdev >= 0)
4405230130Smav			continue;
4406230130Smav		if (w->bindas == -1)
4407230130Smav			continue;
4408230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4409230130Smav			continue;
4410230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4411230130Smav			continue;
4412230130Smav		type = -1;
4413230130Smav		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4414230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
4415230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4416230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
4417230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
4418230130Smav			type = 0;
4419230130Smav			break;
4420230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4421230130Smav			type = 2;
4422230130Smav			break;
4423230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
4424230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
4425230130Smav			type = 5;
4426230130Smav			break;
4427230130Smav		}
4428230130Smav		if (type == -1)
4429230130Smav			break;
4430230130Smav		j = 0;
4431230130Smav		while (types[type][j] >= 0 &&
4432230130Smav		    (used & (1 << types[type][j])) != 0) {
4433230130Smav			j++;
4434230130Smav		}
4435230130Smav		if (types[type][j] >= 0) {
4436230130Smav			w->ossdev = types[type][j];
4437230130Smav			used |= (1 << types[type][j]);
4438230130Smav		}
4439230130Smav	}
4440230130Smav	/* Others */
4441230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4442230130Smav		w = hdaa_widget_get(devinfo, i);
4443230130Smav		if (w == NULL || w->enable == 0)
4444230130Smav			continue;
4445230130Smav		if (w->ossdev >= 0)
4446230130Smav			continue;
4447230130Smav		if (w->bindas == -1)
4448230130Smav			continue;
4449230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4450230130Smav			continue;
4451230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4452230130Smav			continue;
4453230130Smav		j = 0;
4454230130Smav		while (types[6][j] >= 0 &&
4455230130Smav		    (used & (1 << types[6][j])) != 0) {
4456230130Smav			j++;
4457230130Smav		}
4458230130Smav		if (types[6][j] >= 0) {
4459230130Smav			w->ossdev = types[6][j];
4460230130Smav			used |= (1 << types[6][j]);
4461230130Smav		}
4462230130Smav	}
4463230130Smav}
4464230130Smav
4465230130Smavstatic void
4466230130Smavhdaa_audio_build_tree(struct hdaa_devinfo *devinfo)
4467230130Smav{
4468230130Smav	struct hdaa_audio_as *as = devinfo->as;
4469230130Smav	int j, res;
4470230130Smav
4471230130Smav	/* Trace all associations in order of their numbers. */
4472230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4473230130Smav		if (as[j].enable == 0)
4474230130Smav			continue;
4475230130Smav		HDA_BOOTVERBOSE(
4476230130Smav			device_printf(devinfo->dev,
4477230130Smav			    "Tracing association %d (%d)\n", j, as[j].index);
4478230130Smav		);
4479230130Smav		if (as[j].dir == HDAA_CTL_OUT) {
4480230130Smavretry:
4481230130Smav			res = hdaa_audio_trace_as_out(devinfo, j, 0);
4482230130Smav			if (res == 0 && as[j].hpredir >= 0 &&
4483230130Smav			    as[j].fakeredir == 0) {
4484230130Smav				/* If CODEC can't do analog HP redirection
4485230130Smav				   try to make it using one more DAC. */
4486230130Smav				as[j].fakeredir = 1;
4487230130Smav				goto retry;
4488230130Smav			}
4489230130Smav		} else if (as[j].mixed)
4490230130Smav			res = hdaa_audio_trace_as_in(devinfo, j);
4491230130Smav		else
4492230130Smav			res = hdaa_audio_trace_as_in_mch(devinfo, j, 0);
4493230130Smav		if (res) {
4494230130Smav			HDA_BOOTVERBOSE(
4495230130Smav				device_printf(devinfo->dev,
4496230130Smav				    "Association %d (%d) trace succeeded\n",
4497230130Smav				    j, as[j].index);
4498230130Smav			);
4499230130Smav		} else {
4500230130Smav			HDA_BOOTVERBOSE(
4501230130Smav				device_printf(devinfo->dev,
4502230130Smav				    "Association %d (%d) trace failed\n",
4503230130Smav				    j, as[j].index);
4504230130Smav			);
4505230130Smav			as[j].enable = 0;
4506230130Smav		}
4507230130Smav	}
4508230130Smav
4509230130Smav	/* Look for additional DACs/ADCs. */
4510230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4511230130Smav		if (as[j].enable == 0)
4512230130Smav			continue;
4513230130Smav		hdaa_audio_adddac(devinfo, j);
4514230130Smav	}
4515230130Smav
4516230130Smav	/* Trace mixer and beeper pseudo associations. */
4517230130Smav	hdaa_audio_trace_as_extra(devinfo);
4518230130Smav}
4519230130Smav
4520232798Smav/*
4521232798Smav * Store in pdevinfo new data about whether and how we can control signal
4522232798Smav * for OSS device to/from specified widget.
4523232798Smav */
4524230130Smavstatic void
4525232798Smavhdaa_adjust_amp(struct hdaa_widget *w, int ossdev,
4526232798Smav    int found, int minamp, int maxamp)
4527232798Smav{
4528232798Smav	struct hdaa_devinfo *devinfo = w->devinfo;
4529232798Smav	struct hdaa_pcm_devinfo *pdevinfo;
4530232798Smav
4531232798Smav	if (w->bindas >= 0)
4532232798Smav		pdevinfo = devinfo->as[w->bindas].pdevinfo;
4533232798Smav	else
4534232798Smav		pdevinfo = &devinfo->devs[0];
4535232798Smav	if (found)
4536232798Smav		pdevinfo->ossmask |= (1 << ossdev);
4537232798Smav	if (minamp == 0 && maxamp == 0)
4538232798Smav		return;
4539232798Smav	if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) {
4540232798Smav		pdevinfo->minamp[ossdev] = minamp;
4541232798Smav		pdevinfo->maxamp[ossdev] = maxamp;
4542232798Smav	} else {
4543232798Smav		pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp);
4544232798Smav		pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp);
4545232798Smav	}
4546232798Smav}
4547232798Smav
4548232798Smav/*
4549232798Smav * Trace signals from/to all possible sources/destionstions to find possible
4550232798Smav * recording sources, OSS device control ranges and to assign controls.
4551232798Smav */
4552232798Smavstatic void
4553230130Smavhdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo)
4554230130Smav{
4555230130Smav	struct hdaa_audio_as *as = devinfo->as;
4556230130Smav	struct hdaa_widget *w, *cw;
4557232798Smav	int i, j, minamp, maxamp, found;
4558230130Smav
4559230130Smav	/* Assign mixers to the tree. */
4560230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4561230130Smav		w = hdaa_widget_get(devinfo, i);
4562230130Smav		if (w == NULL || w->enable == 0)
4563230130Smav			continue;
4564232798Smav		minamp = maxamp = 0;
4565230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
4566230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
4567230130Smav		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4568230130Smav		    as[w->bindas].dir == HDAA_CTL_IN)) {
4569230130Smav			if (w->ossdev < 0)
4570230130Smav				continue;
4571232798Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4572232798Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4573232798Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4574230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
4575232798Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4576232798Smav			    SOUND_MIXER_RECLEV, 0, &minamp, &maxamp);
4577232798Smav			hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp);
4578230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4579230130Smav		    as[w->bindas].dir == HDAA_CTL_OUT) {
4580232798Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4581232798Smav			    SOUND_MIXER_VOLUME, 0, &minamp, &maxamp);
4582232798Smav			hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp);
4583230130Smav		}
4584230130Smav		if (w->ossdev == SOUND_MIXER_IMIX) {
4585232798Smav			minamp = maxamp = 0;
4586232798Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4587232798Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4588232798Smav			if (minamp == maxamp) {
4589230130Smav				/* If we are unable to control input monitor
4590230130Smav				   as source - try to control it as destination. */
4591232798Smav				found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4592232798Smav				    w->ossdev, 0, &minamp, &maxamp);
4593232798Smav				w->pflags |= HDAA_IMIX_AS_DST;
4594230130Smav			}
4595232798Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4596230130Smav		}
4597230130Smav		if (w->pflags & HDAA_ADC_MONITOR) {
4598230130Smav			for (j = 0; j < w->nconns; j++) {
4599230130Smav				if (!w->connsenable[j])
4600230130Smav				    continue;
4601230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
4602230130Smav				if (cw == NULL || cw->enable == 0)
4603230130Smav				    continue;
4604230130Smav				if (cw->bindas == -1)
4605230130Smav				    continue;
4606230130Smav				if (cw->bindas >= 0 &&
4607230130Smav				    as[cw->bindas].dir != HDAA_CTL_IN)
4608230130Smav					continue;
4609232798Smav				minamp = maxamp = 0;
4610232798Smav				found = hdaa_audio_ctl_dest_amp(devinfo,
4611232798Smav				    w->nid, j, SOUND_MIXER_IGAIN, 0,
4612232798Smav				    &minamp, &maxamp);
4613232798Smav				hdaa_adjust_amp(w, SOUND_MIXER_IGAIN,
4614232798Smav				    found, minamp, maxamp);
4615230130Smav			}
4616230130Smav		}
4617230130Smav	}
4618230130Smav}
4619230130Smav
4620230130Smavstatic void
4621230130Smavhdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo)
4622230130Smav{
4623230130Smav	struct hdaa_audio_as *as = devinfo->as;
4624230130Smav	struct hdaa_widget *w;
4625230130Smav	uint32_t pincap;
4626230130Smav	int i;
4627230130Smav
4628230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
4629230130Smav		w = &devinfo->widget[i];
4630230130Smav		if (w == NULL)
4631230130Smav			continue;
4632230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4633230130Smav		    w->waspin == 0)
4634230130Smav			continue;
4635230130Smav
4636230130Smav		pincap = w->wclass.pin.cap;
4637230130Smav
4638230130Smav		/* Disable everything. */
4639230130Smav		w->wclass.pin.ctrl &= ~(
4640230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
4641230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
4642230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
4643230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
4644230130Smav
4645230130Smav		if (w->enable == 0) {
4646230130Smav			/* Pin is unused so left it disabled. */
4647230130Smav			continue;
4648230130Smav		} else if (w->waspin) {
4649230130Smav			/* Enable input for beeper input. */
4650230130Smav			w->wclass.pin.ctrl |=
4651230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
4652230130Smav		} else if (w->bindas < 0 || as[w->bindas].enable == 0) {
4653230130Smav			/* Pin is unused so left it disabled. */
4654230130Smav			continue;
4655230130Smav		} else if (as[w->bindas].dir == HDAA_CTL_IN) {
4656230130Smav			/* Input pin, configure for input. */
4657230130Smav			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
4658230130Smav				w->wclass.pin.ctrl |=
4659230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
4660230130Smav
4661230130Smav			if ((devinfo->quirks & HDAA_QUIRK_IVREF100) &&
4662230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4663230130Smav				w->wclass.pin.ctrl |=
4664230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4665230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4666230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) &&
4667230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4668230130Smav				w->wclass.pin.ctrl |=
4669230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4670230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4671230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) &&
4672230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4673230130Smav				w->wclass.pin.ctrl |=
4674230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4675230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4676230130Smav		} else {
4677230130Smav			/* Output pin, configure for output. */
4678230130Smav			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
4679230130Smav				w->wclass.pin.ctrl |=
4680230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
4681230130Smav
4682230130Smav			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
4683230130Smav			    (w->wclass.pin.config &
4684230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
4685230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
4686230130Smav				w->wclass.pin.ctrl |=
4687230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
4688230130Smav
4689230130Smav			if ((devinfo->quirks & HDAA_QUIRK_OVREF100) &&
4690230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4691230130Smav				w->wclass.pin.ctrl |=
4692230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4693230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4694230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) &&
4695230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4696230130Smav				w->wclass.pin.ctrl |=
4697230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4698230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4699230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) &&
4700230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4701230130Smav				w->wclass.pin.ctrl |=
4702230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4703230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4704230130Smav		}
4705230130Smav	}
4706230130Smav}
4707230130Smav
4708230130Smavstatic void
4709230130Smavhdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo)
4710230130Smav{
4711230130Smav	struct hdaa_audio_ctl *ctl;
4712230130Smav	int i, z;
4713230130Smav
4714230130Smav	i = 0;
4715230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4716230130Smav		if (ctl->enable == 0 || ctl->ossmask != 0) {
4717230130Smav			/* Mute disabled and mixer controllable controls.
4718230130Smav			 * Last will be initialized by mixer_init().
4719230130Smav			 * This expected to reduce click on startup. */
4720230130Smav			hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0);
4721230130Smav			continue;
4722230130Smav		}
4723230130Smav		/* Init fixed controls to 0dB amplification. */
4724230130Smav		z = ctl->offset;
4725230130Smav		if (z > ctl->step)
4726230130Smav			z = ctl->step;
4727230130Smav		hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z);
4728230130Smav	}
4729230130Smav}
4730230130Smav
4731230130Smavstatic void
4732230130Smavhdaa_gpio_commit(struct hdaa_devinfo *devinfo)
4733230130Smav{
4734230130Smav	uint32_t gdata, gmask, gdir;
4735230130Smav	int i, numgpio;
4736230130Smav
4737230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
4738230130Smav	if (devinfo->gpio != 0 && numgpio != 0) {
4739230130Smav		gdata = hda_command(devinfo->dev,
4740230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
4741230130Smav		gmask = hda_command(devinfo->dev,
4742230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
4743230130Smav		gdir = hda_command(devinfo->dev,
4744230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
4745230130Smav		for (i = 0; i < numgpio; i++) {
4746230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4747230130Smav			    HDAA_GPIO_SET(i)) {
4748230130Smav				gdata |= (1 << i);
4749230130Smav				gmask |= (1 << i);
4750230130Smav				gdir |= (1 << i);
4751230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4752230130Smav			    HDAA_GPIO_CLEAR(i)) {
4753230130Smav				gdata &= ~(1 << i);
4754230130Smav				gmask |= (1 << i);
4755230130Smav				gdir |= (1 << i);
4756230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4757230130Smav			    HDAA_GPIO_DISABLE(i)) {
4758230130Smav				gmask &= ~(1 << i);
4759230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4760230130Smav			    HDAA_GPIO_INPUT(i)) {
4761230130Smav				gmask |= (1 << i);
4762230130Smav				gdir &= ~(1 << i);
4763230130Smav			}
4764230130Smav		}
4765230130Smav		HDA_BOOTVERBOSE(
4766230130Smav			device_printf(devinfo->dev, "GPIO commit\n");
4767230130Smav		);
4768230130Smav		hda_command(devinfo->dev,
4769230130Smav		    HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask));
4770230130Smav		hda_command(devinfo->dev,
4771230130Smav		    HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir));
4772230130Smav		hda_command(devinfo->dev,
4773230130Smav		    HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata));
4774230130Smav		HDA_BOOTVERBOSE(
4775230130Smav			hdaa_dump_gpio(devinfo);
4776230130Smav		);
4777230130Smav	}
4778230130Smav}
4779230130Smav
4780230130Smavstatic void
4781230130Smavhdaa_gpo_commit(struct hdaa_devinfo *devinfo)
4782230130Smav{
4783230130Smav	uint32_t gdata;
4784230130Smav	int i, numgpo;
4785230130Smav
4786230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
4787230130Smav	if (devinfo->gpo != 0 && numgpo != 0) {
4788230130Smav		gdata = hda_command(devinfo->dev,
4789230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
4790230130Smav		for (i = 0; i < numgpo; i++) {
4791230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4792230130Smav			    HDAA_GPIO_SET(i)) {
4793230130Smav				gdata |= (1 << i);
4794230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4795230130Smav			    HDAA_GPIO_CLEAR(i)) {
4796230130Smav				gdata &= ~(1 << i);
4797230130Smav			}
4798230130Smav		}
4799230130Smav		HDA_BOOTVERBOSE(
4800230130Smav			device_printf(devinfo->dev, "GPO commit\n");
4801230130Smav		);
4802230130Smav		hda_command(devinfo->dev,
4803230130Smav		    HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata));
4804230130Smav		HDA_BOOTVERBOSE(
4805230130Smav			hdaa_dump_gpo(devinfo);
4806230130Smav		);
4807230130Smav	}
4808230130Smav}
4809230130Smav
4810230130Smavstatic void
4811230130Smavhdaa_audio_commit(struct hdaa_devinfo *devinfo)
4812230130Smav{
4813230130Smav	struct hdaa_widget *w;
4814230130Smav	int i;
4815230130Smav
4816230130Smav	/* Commit controls. */
4817230130Smav	hdaa_audio_ctl_commit(devinfo);
4818230130Smav
4819230130Smav	/* Commit selectors, pins and EAPD. */
4820230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
4821230130Smav		w = &devinfo->widget[i];
4822230130Smav		if (w == NULL)
4823230130Smav			continue;
4824230130Smav		if (w->selconn == -1)
4825230130Smav			w->selconn = 0;
4826230130Smav		if (w->nconns > 0)
4827230130Smav			hdaa_widget_connection_select(w, w->selconn);
4828230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
4829230130Smav		    w->waspin) {
4830230130Smav			hda_command(devinfo->dev,
4831230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid,
4832230130Smav			    w->wclass.pin.ctrl));
4833230130Smav		}
4834230130Smav		if (w->param.eapdbtl != HDA_INVALID) {
4835230130Smav			uint32_t val;
4836230130Smav
4837230130Smav			val = w->param.eapdbtl;
4838230130Smav			if (devinfo->quirks &
4839230130Smav			    HDAA_QUIRK_EAPDINV)
4840230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
4841230130Smav			hda_command(devinfo->dev,
4842230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid,
4843230130Smav			    val));
4844230130Smav		}
4845230130Smav	}
4846230130Smav
4847230130Smav	hdaa_gpio_commit(devinfo);
4848230130Smav	hdaa_gpo_commit(devinfo);
4849230130Smav}
4850230130Smav
4851230130Smavstatic void
4852230130Smavhdaa_powerup(struct hdaa_devinfo *devinfo)
4853230130Smav{
4854230130Smav	int i;
4855230130Smav
4856230130Smav	hda_command(devinfo->dev,
4857230130Smav	    HDA_CMD_SET_POWER_STATE(0,
4858230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D0));
4859230130Smav	DELAY(100);
4860230130Smav
4861230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4862230130Smav		hda_command(devinfo->dev,
4863230130Smav		    HDA_CMD_SET_POWER_STATE(0,
4864230130Smav		    i, HDA_CMD_POWER_STATE_D0));
4865230130Smav	}
4866230130Smav	DELAY(1000);
4867230130Smav}
4868230130Smav
4869230130Smavstatic int
4870230130Smavhdaa_pcmchannel_setup(struct hdaa_chan *ch)
4871230130Smav{
4872230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
4873230130Smav	struct hdaa_audio_as *as = devinfo->as;
4874230130Smav	struct hdaa_widget *w;
4875230130Smav	uint32_t cap, fmtcap, pcmcap;
4876230130Smav	int i, j, ret, channels, onlystereo;
4877230130Smav	uint16_t pinset;
4878230130Smav
4879230130Smav	ch->caps = hdaa_caps;
4880230130Smav	ch->caps.fmtlist = ch->fmtlist;
4881230130Smav	ch->bit16 = 1;
4882230130Smav	ch->bit32 = 0;
4883230130Smav	ch->pcmrates[0] = 48000;
4884230130Smav	ch->pcmrates[1] = 0;
4885232798Smav	ch->stripecap = 0xff;
4886230130Smav
4887230130Smav	ret = 0;
4888230130Smav	channels = 0;
4889230130Smav	onlystereo = 1;
4890230130Smav	pinset = 0;
4891230130Smav	fmtcap = devinfo->supp_stream_formats;
4892230130Smav	pcmcap = devinfo->supp_pcm_size_rate;
4893230130Smav
4894230130Smav	for (i = 0; i < 16; i++) {
4895230130Smav		/* Check as is correct */
4896230130Smav		if (ch->as < 0)
4897230130Smav			break;
4898230130Smav		/* Cound only present DACs */
4899230130Smav		if (as[ch->as].dacs[ch->asindex][i] <= 0)
4900230130Smav			continue;
4901230130Smav		/* Ignore duplicates */
4902230130Smav		for (j = 0; j < ret; j++) {
4903230130Smav			if (ch->io[j] == as[ch->as].dacs[ch->asindex][i])
4904230130Smav				break;
4905230130Smav		}
4906230130Smav		if (j < ret)
4907230130Smav			continue;
4908230130Smav
4909230130Smav		w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]);
4910230130Smav		if (w == NULL || w->enable == 0)
4911230130Smav			continue;
4912230130Smav		cap = w->param.supp_stream_formats;
4913230130Smav		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
4914230130Smav		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
4915230130Smav			continue;
4916230130Smav		/* Many CODECs does not declare AC3 support on SPDIF.
4917230130Smav		   I don't beleave that they doesn't support it! */
4918230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
4919230130Smav			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
4920230130Smav		if (ret == 0) {
4921230130Smav			fmtcap = cap;
4922230130Smav			pcmcap = w->param.supp_pcm_size_rate;
4923230130Smav		} else {
4924230130Smav			fmtcap &= cap;
4925230130Smav			pcmcap &= w->param.supp_pcm_size_rate;
4926230130Smav		}
4927230130Smav		ch->io[ret++] = as[ch->as].dacs[ch->asindex][i];
4928232798Smav		ch->stripecap &= w->wclass.conv.stripecap;
4929230130Smav		/* Do not count redirection pin/dac channels. */
4930230130Smav		if (i == 15 && as[ch->as].hpredir >= 0)
4931230130Smav			continue;
4932230130Smav		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
4933230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
4934230130Smav			onlystereo = 0;
4935230130Smav		pinset |= (1 << i);
4936230130Smav	}
4937230130Smav	ch->io[ret] = -1;
4938230130Smav	ch->channels = channels;
4939230130Smav
4940230130Smav	if (as[ch->as].fakeredir)
4941230130Smav		ret--;
4942230130Smav	/* Standard speaks only about stereo pins and playback, ... */
4943230130Smav	if ((!onlystereo) || as[ch->as].mixed)
4944230130Smav		pinset = 0;
4945230130Smav	/* ..., but there it gives us info about speakers layout. */
4946230130Smav	as[ch->as].pinset = pinset;
4947230130Smav
4948230130Smav	ch->supp_stream_formats = fmtcap;
4949230130Smav	ch->supp_pcm_size_rate = pcmcap;
4950230130Smav
4951230130Smav	/*
4952230130Smav	 *  8bit = 0
4953230130Smav	 * 16bit = 1
4954230130Smav	 * 20bit = 2
4955230130Smav	 * 24bit = 3
4956230130Smav	 * 32bit = 4
4957230130Smav	 */
4958230130Smav	if (ret > 0) {
4959230130Smav		i = 0;
4960230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
4961230130Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
4962230130Smav				ch->bit16 = 1;
4963230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
4964230130Smav				ch->bit16 = 0;
4965232798Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
4966230130Smav				ch->bit32 = 3;
4967230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
4968230130Smav				ch->bit32 = 2;
4969232798Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
4970232798Smav				ch->bit32 = 4;
4971230130Smav			if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) {
4972230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
4973230130Smav				if (ch->bit32)
4974230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
4975230130Smav			}
4976230130Smav			if (channels >= 2) {
4977230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
4978230130Smav				if (ch->bit32)
4979230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
4980230130Smav			}
4981232798Smav			if (channels >= 3 && !onlystereo) {
4982232798Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0);
4983232798Smav				if (ch->bit32)
4984232798Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0);
4985232798Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1);
4986232798Smav				if (ch->bit32)
4987232798Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1);
4988232798Smav			}
4989232798Smav			if (channels >= 4) {
4990230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
4991230130Smav				if (ch->bit32)
4992230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
4993232798Smav				if (!onlystereo) {
4994232798Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1);
4995232798Smav					if (ch->bit32)
4996232798Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1);
4997232798Smav				}
4998230130Smav			}
4999232798Smav			if (channels >= 5 && !onlystereo) {
5000232798Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0);
5001232798Smav				if (ch->bit32)
5002232798Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0);
5003232798Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1);
5004232798Smav				if (ch->bit32)
5005232798Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1);
5006232798Smav			}
5007232798Smav			if (channels >= 6) {
5008230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
5009230130Smav				if (ch->bit32)
5010230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
5011232798Smav				if (!onlystereo) {
5012232798Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0);
5013232798Smav					if (ch->bit32)
5014232798Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0);
5015232798Smav				}
5016230130Smav			}
5017232798Smav			if (channels >= 7 && !onlystereo) {
5018232798Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0);
5019232798Smav				if (ch->bit32)
5020232798Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0);
5021232798Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1);
5022232798Smav				if (ch->bit32)
5023232798Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1);
5024232798Smav			}
5025232798Smav			if (channels >= 8) {
5026230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
5027230130Smav				if (ch->bit32)
5028230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
5029230130Smav			}
5030230130Smav		}
5031230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
5032230130Smav			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
5033232798Smav			if (channels >= 8) {
5034232798Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 0);
5035232798Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 1);
5036232798Smav			}
5037230130Smav		}
5038230130Smav		ch->fmtlist[i] = 0;
5039230130Smav		i = 0;
5040230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
5041230130Smav			ch->pcmrates[i++] = 8000;
5042230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
5043230130Smav			ch->pcmrates[i++] = 11025;
5044230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
5045230130Smav			ch->pcmrates[i++] = 16000;
5046230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
5047230130Smav			ch->pcmrates[i++] = 22050;
5048230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
5049230130Smav			ch->pcmrates[i++] = 32000;
5050230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
5051230130Smav			ch->pcmrates[i++] = 44100;
5052230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
5053230130Smav		ch->pcmrates[i++] = 48000;
5054230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
5055230130Smav			ch->pcmrates[i++] = 88200;
5056230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
5057230130Smav			ch->pcmrates[i++] = 96000;
5058230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
5059230130Smav			ch->pcmrates[i++] = 176400;
5060230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
5061230130Smav			ch->pcmrates[i++] = 192000;
5062230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
5063230130Smav		ch->pcmrates[i] = 0;
5064230130Smav		if (i > 0) {
5065230130Smav			ch->caps.minspeed = ch->pcmrates[0];
5066230130Smav			ch->caps.maxspeed = ch->pcmrates[i - 1];
5067230130Smav		}
5068230130Smav	}
5069230130Smav
5070230130Smav	return (ret);
5071230130Smav}
5072230130Smav
5073230130Smavstatic void
5074232798Smavhdaa_prepare_pcms(struct hdaa_devinfo *devinfo)
5075230130Smav{
5076230130Smav	struct hdaa_audio_as *as = devinfo->as;
5077230130Smav	int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
5078230130Smav
5079230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5080230130Smav		if (as[i].enable == 0)
5081230130Smav			continue;
5082230130Smav		if (as[i].dir == HDAA_CTL_IN) {
5083230130Smav			if (as[i].digital)
5084230130Smav				drdev++;
5085230130Smav			else
5086230130Smav				ardev++;
5087230130Smav		} else {
5088230130Smav			if (as[i].digital)
5089230130Smav				dpdev++;
5090230130Smav			else
5091230130Smav				apdev++;
5092230130Smav		}
5093230130Smav	}
5094230130Smav	devinfo->num_devs =
5095230130Smav	    max(ardev, apdev) + max(drdev, dpdev);
5096230130Smav	devinfo->devs =
5097230130Smav	    (struct hdaa_pcm_devinfo *)malloc(
5098230130Smav	    devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo),
5099230130Smav	    M_HDAA, M_ZERO | M_NOWAIT);
5100230130Smav	if (devinfo->devs == NULL) {
5101230130Smav		device_printf(devinfo->dev,
5102230130Smav		    "Unable to allocate memory for devices\n");
5103230130Smav		return;
5104230130Smav	}
5105230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5106230130Smav		devinfo->devs[i].index = i;
5107230130Smav		devinfo->devs[i].devinfo = devinfo;
5108230130Smav		devinfo->devs[i].playas = -1;
5109230130Smav		devinfo->devs[i].recas = -1;
5110230130Smav		devinfo->devs[i].digital = 255;
5111230130Smav	}
5112230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5113230130Smav		if (as[i].enable == 0)
5114230130Smav			continue;
5115230130Smav		for (j = 0; j < devinfo->num_devs; j++) {
5116230130Smav			if (devinfo->devs[j].digital != 255 &&
5117230130Smav			    (!devinfo->devs[j].digital) !=
5118230130Smav			    (!as[i].digital))
5119230130Smav				continue;
5120230130Smav			if (as[i].dir == HDAA_CTL_IN) {
5121230130Smav				if (devinfo->devs[j].recas >= 0)
5122230130Smav					continue;
5123230130Smav				devinfo->devs[j].recas = i;
5124230130Smav			} else {
5125230130Smav				if (devinfo->devs[j].playas >= 0)
5126230130Smav					continue;
5127230130Smav				devinfo->devs[j].playas = i;
5128230130Smav			}
5129232798Smav			as[i].pdevinfo = &devinfo->devs[j];
5130230130Smav			for (k = 0; k < as[i].num_chans; k++) {
5131230130Smav				devinfo->chans[as[i].chans[k]].pdevinfo =
5132230130Smav				    &devinfo->devs[j];
5133230130Smav			}
5134230130Smav			devinfo->devs[j].digital = as[i].digital;
5135230130Smav			break;
5136230130Smav		}
5137230130Smav	}
5138232798Smav}
5139232798Smav
5140232798Smavstatic void
5141232798Smavhdaa_create_pcms(struct hdaa_devinfo *devinfo)
5142232798Smav{
5143232798Smav	int i;
5144232798Smav
5145230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5146230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
5147230130Smav
5148230130Smav		pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1);
5149230130Smav		device_set_ivars(pdevinfo->dev, (void *)pdevinfo);
5150230130Smav	}
5151230130Smav}
5152230130Smav
5153230130Smavstatic void
5154230130Smavhdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
5155230130Smav{
5156230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5157230130Smav	struct hdaa_audio_ctl *ctl;
5158230130Smav	char buf[64];
5159230130Smav	int i, j, printed;
5160230130Smav
5161230130Smav	if (flag == 0) {
5162230130Smav		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5163230130Smav		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5164230130Smav		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
5165230130Smav		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
5166230130Smav	}
5167230130Smav
5168230130Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
5169230130Smav		if ((flag & (1 << j)) == 0)
5170230130Smav			continue;
5171230130Smav		i = 0;
5172230130Smav		printed = 0;
5173230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5174230130Smav			if (ctl->enable == 0 ||
5175230130Smav			    ctl->widget->enable == 0)
5176230130Smav				continue;
5177230130Smav			if (!((pdevinfo->playas >= 0 &&
5178230130Smav			    ctl->widget->bindas == pdevinfo->playas) ||
5179230130Smav			    (pdevinfo->recas >= 0 &&
5180230130Smav			    ctl->widget->bindas == pdevinfo->recas) ||
5181230130Smav			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
5182230130Smav				continue;
5183230130Smav			if ((ctl->ossmask & (1 << j)) == 0)
5184230130Smav				continue;
5185230130Smav
5186230130Smav			if (printed == 0) {
5187230130Smav				device_printf(pdevinfo->dev, "\n");
5188230130Smav				if (banner != NULL) {
5189230130Smav					device_printf(pdevinfo->dev, "%s", banner);
5190230130Smav				} else {
5191230130Smav					device_printf(pdevinfo->dev, "Unknown Ctl");
5192230130Smav				}
5193232798Smav				printf(" (OSS: %s)",
5194230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(1 << j,
5195230130Smav				    buf, sizeof(buf)));
5196232798Smav				if (pdevinfo->ossmask & (1 << j)) {
5197232798Smav					printf(": %+d/%+ddB\n",
5198232798Smav					    pdevinfo->minamp[j] / 4,
5199232798Smav					    pdevinfo->maxamp[j] / 4);
5200232798Smav				} else
5201232798Smav					printf("\n");
5202230130Smav				device_printf(pdevinfo->dev, "   |\n");
5203230130Smav				printed = 1;
5204230130Smav			}
5205230130Smav			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
5206230130Smav				ctl->widget->nid,
5207230130Smav				(ctl->ndir == HDAA_CTL_IN)?"in ":"out");
5208230130Smav			if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir)
5209230130Smav				printf(" %2d): ", ctl->index);
5210230130Smav			else
5211230130Smav				printf("):    ");
5212230130Smav			if (ctl->step > 0) {
5213230130Smav				printf("%+d/%+ddB (%d steps)%s\n",
5214232798Smav				    MINQDB(ctl) / 4,
5215232798Smav				    MAXQDB(ctl) / 4,
5216230130Smav				    ctl->step + 1,
5217230130Smav				    ctl->mute?" + mute":"");
5218230130Smav			} else
5219230130Smav				printf("%s\n", ctl->mute?"mute":"");
5220230130Smav		}
5221230130Smav	}
5222230130Smav}
5223230130Smav
5224230130Smavstatic void
5225230130Smavhdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
5226230130Smav{
5227230130Smav	uint32_t cap;
5228230130Smav
5229230130Smav	cap = fcap;
5230230130Smav	if (cap != 0) {
5231230130Smav		device_printf(dev, "     Stream cap: 0x%08x\n", cap);
5232230130Smav		device_printf(dev, "                ");
5233230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5234230130Smav			printf(" AC3");
5235230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5236230130Smav			printf(" FLOAT32");
5237230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5238230130Smav			printf(" PCM");
5239230130Smav		printf("\n");
5240230130Smav	}
5241230130Smav	cap = pcmcap;
5242230130Smav	if (cap != 0) {
5243230130Smav		device_printf(dev, "        PCM cap: 0x%08x\n", cap);
5244230130Smav		device_printf(dev, "                ");
5245230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5246230130Smav			printf(" 8");
5247230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5248230130Smav			printf(" 16");
5249230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5250230130Smav			printf(" 20");
5251230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5252230130Smav			printf(" 24");
5253230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5254230130Smav			printf(" 32");
5255230130Smav		printf(" bits,");
5256230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5257230130Smav			printf(" 8");
5258230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5259230130Smav			printf(" 11");
5260230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5261230130Smav			printf(" 16");
5262230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5263230130Smav			printf(" 22");
5264230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5265230130Smav			printf(" 32");
5266230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5267230130Smav			printf(" 44");
5268230130Smav		printf(" 48");
5269230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5270230130Smav			printf(" 88");
5271230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5272230130Smav			printf(" 96");
5273230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5274230130Smav			printf(" 176");
5275230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5276230130Smav			printf(" 192");
5277230130Smav		printf(" KHz\n");
5278230130Smav	}
5279230130Smav}
5280230130Smav
5281230130Smavstatic void
5282230130Smavhdaa_dump_pin(struct hdaa_widget *w)
5283230130Smav{
5284230130Smav	uint32_t pincap;
5285230130Smav
5286230130Smav	pincap = w->wclass.pin.cap;
5287230130Smav
5288230130Smav	device_printf(w->devinfo->dev, "        Pin cap: 0x%08x\n", pincap);
5289230130Smav	device_printf(w->devinfo->dev, "                ");
5290230130Smav	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5291230130Smav		printf(" ISC");
5292230130Smav	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5293230130Smav		printf(" TRQD");
5294230130Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5295230130Smav		printf(" PDC");
5296230130Smav	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5297230130Smav		printf(" HP");
5298230130Smav	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5299230130Smav		printf(" OUT");
5300230130Smav	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5301230130Smav		printf(" IN");
5302230130Smav	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5303230130Smav		printf(" BAL");
5304230130Smav	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
5305230130Smav		printf(" HDMI");
5306230130Smav	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5307230130Smav		printf(" VREF[");
5308230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5309230130Smav			printf(" 50");
5310230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5311230130Smav			printf(" 80");
5312230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5313230130Smav			printf(" 100");
5314230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5315230130Smav			printf(" GROUND");
5316230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5317230130Smav			printf(" HIZ");
5318230130Smav		printf(" ]");
5319230130Smav	}
5320230130Smav	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5321230130Smav		printf(" EAPD");
5322230130Smav	if (HDA_PARAM_PIN_CAP_DP(pincap))
5323230130Smav		printf(" DP");
5324230130Smav	if (HDA_PARAM_PIN_CAP_HBR(pincap))
5325230130Smav		printf(" HBR");
5326230130Smav	printf("\n");
5327230130Smav	device_printf(w->devinfo->dev, "     Pin config: 0x%08x\n",
5328230130Smav	    w->wclass.pin.config);
5329230130Smav	device_printf(w->devinfo->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
5330230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5331230130Smav		printf(" HP");
5332230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5333230130Smav		printf(" IN");
5334230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5335230130Smav		printf(" OUT");
5336232798Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
5337232798Smav		if ((w->wclass.pin.ctrl &
5338232798Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
5339232798Smav			printf(" HBR");
5340232798Smav		else if ((w->wclass.pin.ctrl &
5341232798Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5342232798Smav			printf(" EPTs");
5343232798Smav	} else {
5344232798Smav		if ((w->wclass.pin.ctrl &
5345232798Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5346232798Smav			printf(" VREFs");
5347232798Smav	}
5348230130Smav	printf("\n");
5349230130Smav}
5350230130Smav
5351230130Smavstatic void
5352230130Smavhdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf)
5353230130Smav{
5354230130Smav
5355230130Smav	device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d "
5356230130Smav	    "%-13s %-5s %-7s %-10s %-7s %d%s\n",
5357230130Smav	    w->nid, conf,
5358230130Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
5359230130Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
5360230130Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
5361230130Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
5362230130Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
5363230130Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
5364230130Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
5365230130Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
5366230130Smav	    (w->enable == 0)?" DISA":"");
5367230130Smav}
5368230130Smav
5369230130Smavstatic void
5370230130Smavhdaa_dump_pin_configs(struct hdaa_devinfo *devinfo)
5371230130Smav{
5372230130Smav	struct hdaa_widget *w;
5373230130Smav	int i;
5374230130Smav
5375230130Smav	device_printf(devinfo->dev, "nid   0x    as seq "
5376230130Smav	    "device       conn  jack    loc        color   misc\n");
5377230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5378230130Smav		w = hdaa_widget_get(devinfo, i);
5379230130Smav		if (w == NULL)
5380230130Smav			continue;
5381230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5382230130Smav			continue;
5383230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
5384230130Smav	}
5385230130Smav}
5386230130Smav
5387230130Smavstatic void
5388264962Smariushdaa_dump_amp(device_t dev, uint32_t cap, const char *banner)
5389230130Smav{
5390230130Smav	device_printf(dev, "     %s amp: 0x%08x\n", banner, cap);
5391230130Smav	device_printf(dev, "                 "
5392230130Smav	    "mute=%d step=%d size=%d offset=%d\n",
5393230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5394230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap),
5395230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap),
5396230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap));
5397230130Smav}
5398230130Smav
5399230130Smavstatic void
5400230130Smavhdaa_dump_nodes(struct hdaa_devinfo *devinfo)
5401230130Smav{
5402230130Smav	struct hdaa_widget *w, *cw;
5403230130Smav	char buf[64];
5404230130Smav	int i, j;
5405230130Smav
5406230130Smav	device_printf(devinfo->dev, "\n");
5407230130Smav	device_printf(devinfo->dev, "Default Parameter\n");
5408230130Smav	device_printf(devinfo->dev, "-----------------\n");
5409230130Smav	hdaa_dump_audio_formats(devinfo->dev,
5410230130Smav	    devinfo->supp_stream_formats,
5411230130Smav	    devinfo->supp_pcm_size_rate);
5412230130Smav	device_printf(devinfo->dev, "         IN amp: 0x%08x\n",
5413230130Smav	    devinfo->inamp_cap);
5414230130Smav	device_printf(devinfo->dev, "        OUT amp: 0x%08x\n",
5415230130Smav	    devinfo->outamp_cap);
5416230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5417230130Smav		w = hdaa_widget_get(devinfo, i);
5418230130Smav		if (w == NULL) {
5419230130Smav			device_printf(devinfo->dev, "Ghost widget nid=%d\n", i);
5420230130Smav			continue;
5421230130Smav		}
5422230130Smav		device_printf(devinfo->dev, "\n");
5423230130Smav		device_printf(devinfo->dev, "            nid: %d%s\n", w->nid,
5424230130Smav		    (w->enable == 0) ? " [DISABLED]" : "");
5425230130Smav		device_printf(devinfo->dev, "           Name: %s\n", w->name);
5426230130Smav		device_printf(devinfo->dev, "     Widget cap: 0x%08x\n",
5427230130Smav		    w->param.widget_cap);
5428230130Smav		if (w->param.widget_cap & 0x0ee1) {
5429230130Smav			device_printf(devinfo->dev, "                ");
5430230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
5431230130Smav			    printf(" LRSWAP");
5432230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
5433230130Smav			    printf(" PWR");
5434230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5435230130Smav			    printf(" DIGITAL");
5436230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
5437230130Smav			    printf(" UNSOL");
5438230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
5439230130Smav			    printf(" PROC");
5440230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
5441232798Smav			    printf(" STRIPE(x%d)",
5442232798Smav				1 << (fls(w->wclass.conv.stripecap) - 1));
5443230130Smav			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
5444230130Smav			if (j == 1)
5445230130Smav			    printf(" STEREO");
5446230130Smav			else if (j > 1)
5447230130Smav			    printf(" %dCH", j + 1);
5448230130Smav			printf("\n");
5449230130Smav		}
5450230130Smav		if (w->bindas != -1) {
5451230130Smav			device_printf(devinfo->dev, "    Association: %d (0x%08x)\n",
5452230130Smav			    w->bindas, w->bindseqmask);
5453230130Smav		}
5454230130Smav		if (w->ossmask != 0 || w->ossdev >= 0) {
5455230130Smav			device_printf(devinfo->dev, "            OSS: %s",
5456230130Smav			    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
5457230130Smav			if (w->ossdev >= 0)
5458232798Smav			    printf(" (%s)", ossnames[w->ossdev]);
5459230130Smav			printf("\n");
5460230130Smav		}
5461230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5462230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5463230130Smav			hdaa_dump_audio_formats(devinfo->dev,
5464230130Smav			    w->param.supp_stream_formats,
5465230130Smav			    w->param.supp_pcm_size_rate);
5466230130Smav		} else if (w->type ==
5467230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
5468230130Smav			hdaa_dump_pin(w);
5469230130Smav		if (w->param.eapdbtl != HDA_INVALID)
5470230130Smav			device_printf(devinfo->dev, "           EAPD: 0x%08x\n",
5471230130Smav			    w->param.eapdbtl);
5472230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5473230130Smav		    w->param.outamp_cap != 0)
5474230130Smav			hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output");
5475230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5476230130Smav		    w->param.inamp_cap != 0)
5477230130Smav			hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input");
5478230130Smav		if (w->nconns > 0) {
5479230130Smav			device_printf(devinfo->dev, "    connections: %d\n", w->nconns);
5480230130Smav			device_printf(devinfo->dev, "          |\n");
5481230130Smav		}
5482230130Smav		for (j = 0; j < w->nconns; j++) {
5483230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
5484230130Smav			device_printf(devinfo->dev, "          + %s<- nid=%d [%s]",
5485230130Smav			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
5486230130Smav			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5487230130Smav			if (cw == NULL)
5488230130Smav				printf(" [UNKNOWN]");
5489230130Smav			else if (cw->enable == 0)
5490230130Smav				printf(" [DISABLED]");
5491230130Smav			if (w->nconns > 1 && w->selconn == j && w->type !=
5492230130Smav			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5493230130Smav				printf(" (selected)");
5494230130Smav			printf("\n");
5495230130Smav		}
5496230130Smav	}
5497230130Smav
5498230130Smav}
5499230130Smav
5500230130Smavstatic void
5501230130Smavhdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth)
5502230130Smav{
5503230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5504230130Smav	struct hdaa_widget *w, *cw;
5505230130Smav	char buf[64];
5506230130Smav	int i, printed = 0;
5507230130Smav
5508230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
5509230130Smav		return;
5510230130Smav
5511230130Smav	w = hdaa_widget_get(devinfo, nid);
5512230130Smav	if (w == NULL || w->enable == 0)
5513230130Smav		return;
5514230130Smav
5515230130Smav	if (depth == 0)
5516230130Smav		device_printf(pdevinfo->dev, "%*s", 4, "");
5517230130Smav	else
5518230130Smav		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
5519230130Smav	printf("nid=%d [%s]", w->nid, w->name);
5520230130Smav
5521230130Smav	if (depth > 0) {
5522230130Smav		if (w->ossmask == 0) {
5523230130Smav			printf("\n");
5524230130Smav			return;
5525230130Smav		}
5526230130Smav		printf(" [src: %s]",
5527230130Smav		    hdaa_audio_ctl_ossmixer_mask2allname(
5528230130Smav			w->ossmask, buf, sizeof(buf)));
5529230130Smav		if (w->ossdev >= 0) {
5530230130Smav			printf("\n");
5531230130Smav			return;
5532230130Smav		}
5533230130Smav	}
5534230130Smav	printf("\n");
5535230130Smav
5536230130Smav	for (i = 0; i < w->nconns; i++) {
5537230130Smav		if (w->connsenable[i] == 0)
5538230130Smav			continue;
5539230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
5540230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
5541230130Smav			continue;
5542230130Smav		if (printed == 0) {
5543230130Smav			device_printf(pdevinfo->dev, "%*s  |\n", 4 + (depth) * 7, "");
5544230130Smav			printed = 1;
5545230130Smav		}
5546230130Smav		hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
5547230130Smav	}
5548230130Smav
5549230130Smav}
5550230130Smav
5551230130Smavstatic void
5552230130Smavhdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo)
5553230130Smav{
5554230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5555230130Smav	struct hdaa_audio_as *as;
5556230130Smav	struct hdaa_widget *w;
5557230130Smav	int i, printed = 0;
5558230130Smav
5559230130Smav	if (pdevinfo->playas < 0)
5560230130Smav		return;
5561230130Smav
5562230130Smav	as = &devinfo->as[pdevinfo->playas];
5563230130Smav	for (i = 0; i < 16; i++) {
5564230130Smav		if (as->pins[i] <= 0)
5565230130Smav			continue;
5566230130Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
5567230130Smav		if (w == NULL || w->enable == 0)
5568230130Smav			continue;
5569230130Smav		if (printed == 0) {
5570230130Smav			printed = 1;
5571230130Smav			device_printf(pdevinfo->dev, "\n");
5572230130Smav			device_printf(pdevinfo->dev, "Playback:\n");
5573230130Smav		}
5574230130Smav		device_printf(pdevinfo->dev, "\n");
5575230130Smav		hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0);
5576230130Smav	}
5577230130Smav}
5578230130Smav
5579230130Smavstatic void
5580230130Smavhdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo)
5581230130Smav{
5582230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5583230130Smav	struct hdaa_widget *w;
5584230130Smav	int i;
5585230130Smav	int printed = 0;
5586230130Smav
5587230130Smav	if (pdevinfo->recas < 0)
5588230130Smav		return;
5589230130Smav
5590230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5591230130Smav		w = hdaa_widget_get(devinfo, i);
5592230130Smav		if (w == NULL || w->enable == 0)
5593230130Smav			continue;
5594230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
5595230130Smav			continue;
5596230130Smav		if (w->bindas != pdevinfo->recas)
5597230130Smav			continue;
5598230130Smav		if (printed == 0) {
5599230130Smav			printed = 1;
5600230130Smav			device_printf(pdevinfo->dev, "\n");
5601230130Smav			device_printf(pdevinfo->dev, "Record:\n");
5602230130Smav		}
5603230130Smav		device_printf(pdevinfo->dev, "\n");
5604230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
5605230130Smav	}
5606230130Smav}
5607230130Smav
5608230130Smavstatic void
5609230130Smavhdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo)
5610230130Smav{
5611230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5612230130Smav	struct hdaa_widget *w;
5613230130Smav	int i;
5614230130Smav	int printed = 0;
5615230130Smav
5616230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5617230130Smav		w = hdaa_widget_get(devinfo, i);
5618230130Smav		if (w == NULL || w->enable == 0)
5619230130Smav			continue;
5620230130Smav		if (w->ossdev != SOUND_MIXER_IMIX)
5621230130Smav			continue;
5622232798Smav		if (w->bindas != pdevinfo->recas)
5623232798Smav			continue;
5624230130Smav		if (printed == 0) {
5625230130Smav			printed = 1;
5626230130Smav			device_printf(pdevinfo->dev, "\n");
5627230130Smav			device_printf(pdevinfo->dev, "Input Mix:\n");
5628230130Smav		}
5629230130Smav		device_printf(pdevinfo->dev, "\n");
5630230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
5631230130Smav	}
5632230130Smav}
5633230130Smav
5634230130Smavstatic void
5635230130Smavhdaa_dump_pcmchannels(struct hdaa_pcm_devinfo *pdevinfo)
5636230130Smav{
5637230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5638230130Smav	nid_t *nids;
5639230130Smav	int chid, i;
5640230130Smav
5641230130Smav	if (pdevinfo->playas >= 0) {
5642230130Smav		device_printf(pdevinfo->dev, "\n");
5643230130Smav		device_printf(pdevinfo->dev, "Playback:\n");
5644230130Smav		device_printf(pdevinfo->dev, "\n");
5645230130Smav		chid = devinfo->as[pdevinfo->playas].chans[0];
5646230130Smav		hdaa_dump_audio_formats(pdevinfo->dev,
5647230130Smav		    devinfo->chans[chid].supp_stream_formats,
5648230130Smav		    devinfo->chans[chid].supp_pcm_size_rate);
5649230130Smav		for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) {
5650230130Smav			chid = devinfo->as[pdevinfo->playas].chans[i];
5651230130Smav			device_printf(pdevinfo->dev, "            DAC:");
5652230130Smav			for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5653230130Smav				printf(" %d", *nids);
5654230130Smav			printf("\n");
5655230130Smav		}
5656230130Smav	}
5657230130Smav	if (pdevinfo->recas >= 0) {
5658230130Smav		device_printf(pdevinfo->dev, "\n");
5659230130Smav		device_printf(pdevinfo->dev, "Record:\n");
5660230130Smav		device_printf(pdevinfo->dev, "\n");
5661230130Smav		chid = devinfo->as[pdevinfo->recas].chans[0];
5662230130Smav		hdaa_dump_audio_formats(pdevinfo->dev,
5663230130Smav		    devinfo->chans[chid].supp_stream_formats,
5664230130Smav		    devinfo->chans[chid].supp_pcm_size_rate);
5665230130Smav		for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) {
5666230130Smav			chid = devinfo->as[pdevinfo->recas].chans[i];
5667230130Smav			device_printf(pdevinfo->dev, "            DAC:");
5668230130Smav			for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5669230130Smav				printf(" %d", *nids);
5670230130Smav			printf("\n");
5671230130Smav		}
5672230130Smav	}
5673230130Smav}
5674230130Smav
5675230130Smavstatic void
5676230130Smavhdaa_pindump(device_t dev)
5677230130Smav{
5678230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5679230130Smav	struct hdaa_widget *w;
5680230130Smav	uint32_t res, pincap, delay;
5681230130Smav	int i;
5682230130Smav
5683230130Smav	device_printf(dev, "Dumping AFG pins:\n");
5684230130Smav	device_printf(dev, "nid   0x    as seq "
5685230130Smav	    "device       conn  jack    loc        color   misc\n");
5686230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5687230130Smav		w = hdaa_widget_get(devinfo, i);
5688230130Smav		if (w == NULL || w->type !=
5689230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5690230130Smav			continue;
5691230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
5692230130Smav		pincap = w->wclass.pin.cap;
5693230130Smav		device_printf(dev, "    Caps: %2s %3s %2s %4s %4s",
5694230130Smav		    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
5695230130Smav		    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
5696230130Smav		    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
5697230130Smav		    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
5698230130Smav		    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
5699230130Smav		if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
5700230130Smav		    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
5701230130Smav			if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
5702230130Smav				delay = 0;
5703230130Smav				hda_command(dev,
5704230130Smav				    HDA_CMD_SET_PIN_SENSE(0, w->nid, 0));
5705230130Smav				do {
5706230130Smav					res = hda_command(dev,
5707230130Smav					    HDA_CMD_GET_PIN_SENSE(0, w->nid));
5708230130Smav					if (res != 0x7fffffff && res != 0xffffffff)
5709230130Smav						break;
5710230130Smav					DELAY(10);
5711230130Smav				} while (++delay < 10000);
5712230130Smav			} else {
5713230130Smav				delay = 0;
5714230130Smav				res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0,
5715230130Smav				    w->nid));
5716230130Smav			}
5717232798Smav			printf(" Sense: 0x%08x (%sconnected%s)", res,
5718230130Smav			    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ?
5719232798Smav			     "" : "dis",
5720232798Smav			    (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) &&
5721232798Smav			     (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ?
5722232798Smav			      ", ELD valid" : "");
5723230130Smav			if (delay > 0)
5724230130Smav				printf(" delay %dus", delay * 10);
5725230130Smav		}
5726230130Smav		printf("\n");
5727230130Smav	}
5728230130Smav	device_printf(dev,
5729230130Smav	    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
5730230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
5731230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
5732230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
5733230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
5734230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
5735230130Smav	hdaa_dump_gpi(devinfo);
5736230130Smav	hdaa_dump_gpio(devinfo);
5737230130Smav	hdaa_dump_gpo(devinfo);
5738230130Smav}
5739230130Smav
5740230130Smavstatic void
5741230130Smavhdaa_configure(device_t dev)
5742230130Smav{
5743230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5744230130Smav	struct hdaa_audio_ctl *ctl;
5745230130Smav	int i;
5746230130Smav
5747230130Smav	HDA_BOOTHVERBOSE(
5748230130Smav		device_printf(dev, "Applying built-in patches...\n");
5749230130Smav	);
5750230130Smav	hdaa_patch(devinfo);
5751230130Smav	HDA_BOOTHVERBOSE(
5752230130Smav		device_printf(dev, "Applying local patches...\n");
5753230130Smav	);
5754230130Smav	hdaa_local_patch(devinfo);
5755230130Smav	hdaa_audio_postprocess(devinfo);
5756230130Smav	HDA_BOOTHVERBOSE(
5757230130Smav		device_printf(dev, "Parsing Ctls...\n");
5758230130Smav	);
5759230130Smav	hdaa_audio_ctl_parse(devinfo);
5760230130Smav	HDA_BOOTHVERBOSE(
5761230130Smav		device_printf(dev, "Disabling nonaudio...\n");
5762230130Smav	);
5763230130Smav	hdaa_audio_disable_nonaudio(devinfo);
5764230130Smav	HDA_BOOTHVERBOSE(
5765230130Smav		device_printf(dev, "Disabling useless...\n");
5766230130Smav	);
5767230130Smav	hdaa_audio_disable_useless(devinfo);
5768230130Smav	HDA_BOOTVERBOSE(
5769230130Smav		device_printf(dev, "Patched pins configuration:\n");
5770230130Smav		hdaa_dump_pin_configs(devinfo);
5771230130Smav	);
5772230130Smav	HDA_BOOTHVERBOSE(
5773230130Smav		device_printf(dev, "Parsing pin associations...\n");
5774230130Smav	);
5775230130Smav	hdaa_audio_as_parse(devinfo);
5776230130Smav	HDA_BOOTHVERBOSE(
5777230130Smav		device_printf(dev, "Building AFG tree...\n");
5778230130Smav	);
5779230130Smav	hdaa_audio_build_tree(devinfo);
5780230130Smav	HDA_BOOTHVERBOSE(
5781230130Smav		device_printf(dev, "Disabling unassociated "
5782230130Smav		    "widgets...\n");
5783230130Smav	);
5784230130Smav	hdaa_audio_disable_unas(devinfo);
5785230130Smav	HDA_BOOTHVERBOSE(
5786230130Smav		device_printf(dev, "Disabling nonselected "
5787230130Smav		    "inputs...\n");
5788230130Smav	);
5789230130Smav	hdaa_audio_disable_notselected(devinfo);
5790230130Smav	HDA_BOOTHVERBOSE(
5791230130Smav		device_printf(dev, "Disabling useless...\n");
5792230130Smav	);
5793230130Smav	hdaa_audio_disable_useless(devinfo);
5794230130Smav	HDA_BOOTHVERBOSE(
5795230130Smav		device_printf(dev, "Disabling "
5796230130Smav		    "crossassociatement connections...\n");
5797230130Smav	);
5798230130Smav	hdaa_audio_disable_crossas(devinfo);
5799230130Smav	HDA_BOOTHVERBOSE(
5800230130Smav		device_printf(dev, "Disabling useless...\n");
5801230130Smav	);
5802230130Smav	hdaa_audio_disable_useless(devinfo);
5803230130Smav	HDA_BOOTHVERBOSE(
5804230130Smav		device_printf(dev, "Binding associations to channels...\n");
5805230130Smav	);
5806230130Smav	hdaa_audio_bind_as(devinfo);
5807230130Smav	HDA_BOOTHVERBOSE(
5808230130Smav		device_printf(dev, "Assigning names to signal sources...\n");
5809230130Smav	);
5810230130Smav	hdaa_audio_assign_names(devinfo);
5811230130Smav	HDA_BOOTHVERBOSE(
5812232798Smav		device_printf(dev, "Preparing PCM devices...\n");
5813232798Smav	);
5814232798Smav	hdaa_prepare_pcms(devinfo);
5815232798Smav	HDA_BOOTHVERBOSE(
5816230130Smav		device_printf(dev, "Assigning mixers to the tree...\n");
5817230130Smav	);
5818230130Smav	hdaa_audio_assign_mixers(devinfo);
5819230130Smav	HDA_BOOTHVERBOSE(
5820230130Smav		device_printf(dev, "Preparing pin controls...\n");
5821230130Smav	);
5822230130Smav	hdaa_audio_prepare_pin_ctrl(devinfo);
5823230130Smav	HDA_BOOTHVERBOSE(
5824230130Smav		device_printf(dev, "AFG commit...\n");
5825230130Smav	);
5826230130Smav	hdaa_audio_commit(devinfo);
5827230130Smav	HDA_BOOTHVERBOSE(
5828230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
5829230130Smav	);
5830230130Smav	hdaa_patch_direct(devinfo);
5831230130Smav	HDA_BOOTHVERBOSE(
5832232798Smav		device_printf(dev, "Pin sense init...\n");
5833230130Smav	);
5834232798Smav	hdaa_sense_init(devinfo);
5835230130Smav	HDA_BOOTHVERBOSE(
5836230130Smav		device_printf(dev, "Creating PCM devices...\n");
5837230130Smav	);
5838230130Smav	hdaa_create_pcms(devinfo);
5839230130Smav
5840230130Smav	HDA_BOOTVERBOSE(
5841230130Smav		if (devinfo->quirks != 0) {
5842230130Smav			device_printf(dev, "FG config/quirks:");
5843264962Smarius			for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
5844230130Smav				if ((devinfo->quirks &
5845230130Smav				    hdaa_quirks_tab[i].value) ==
5846230130Smav				    hdaa_quirks_tab[i].value)
5847230130Smav					printf(" %s", hdaa_quirks_tab[i].key);
5848230130Smav			}
5849230130Smav			printf("\n");
5850230130Smav		}
5851230130Smav
5852230130Smav		device_printf(dev, "\n");
5853230130Smav		device_printf(dev, "+-------------------+\n");
5854230130Smav		device_printf(dev, "| DUMPING HDA NODES |\n");
5855230130Smav		device_printf(dev, "+-------------------+\n");
5856230130Smav		hdaa_dump_nodes(devinfo);
5857230130Smav	);
5858230130Smav
5859230130Smav	HDA_BOOTHVERBOSE(
5860230130Smav		device_printf(dev, "\n");
5861230130Smav		device_printf(dev, "+------------------------+\n");
5862230130Smav		device_printf(dev, "| DUMPING HDA AMPLIFIERS |\n");
5863230130Smav		device_printf(dev, "+------------------------+\n");
5864230130Smav		device_printf(dev, "\n");
5865230130Smav		i = 0;
5866230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5867230130Smav			device_printf(dev, "%3d: nid %3d %s (%s) index %d", i,
5868230130Smav			    (ctl->widget != NULL) ? ctl->widget->nid : -1,
5869230130Smav			    (ctl->ndir == HDAA_CTL_IN)?"in ":"out",
5870230130Smav			    (ctl->dir == HDAA_CTL_IN)?"in ":"out",
5871230130Smav			    ctl->index);
5872230130Smav			if (ctl->childwidget != NULL)
5873230130Smav				printf(" cnid %3d", ctl->childwidget->nid);
5874230130Smav			else
5875230130Smav				printf("         ");
5876230130Smav			printf(" ossmask=0x%08x\n",
5877230130Smav			    ctl->ossmask);
5878230130Smav			device_printf(dev,
5879230130Smav			    "       mute: %d step: %3d size: %3d off: %3d%s\n",
5880230130Smav			    ctl->mute, ctl->step, ctl->size, ctl->offset,
5881230130Smav			    (ctl->enable == 0) ? " [DISABLED]" :
5882230130Smav			    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
5883230130Smav		}
5884230130Smav	);
5885230130Smav
5886230130Smav	HDA_BOOTVERBOSE(
5887230130Smav		device_printf(dev, "\n");
5888230130Smav	);
5889230130Smav}
5890230130Smav
5891230130Smavstatic void
5892230130Smavhdaa_unconfigure(device_t dev)
5893230130Smav{
5894230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5895230130Smav	struct hdaa_widget *w;
5896230130Smav	int i, j;
5897230130Smav
5898230130Smav	HDA_BOOTHVERBOSE(
5899232798Smav		device_printf(dev, "Pin sense deinit...\n");
5900230130Smav	);
5901232798Smav	hdaa_sense_deinit(devinfo);
5902230130Smav	free(devinfo->ctl, M_HDAA);
5903230130Smav	devinfo->ctl = NULL;
5904230130Smav	devinfo->ctlcnt = 0;
5905230130Smav	free(devinfo->as, M_HDAA);
5906230130Smav	devinfo->as = NULL;
5907230130Smav	devinfo->ascnt = 0;
5908230130Smav	free(devinfo->devs, M_HDAA);
5909230130Smav	devinfo->devs = NULL;
5910230130Smav	devinfo->num_devs = 0;
5911230130Smav	free(devinfo->chans, M_HDAA);
5912230130Smav	devinfo->chans = NULL;
5913230130Smav	devinfo->num_chans = 0;
5914230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5915230130Smav		w = hdaa_widget_get(devinfo, i);
5916230130Smav		if (w == NULL)
5917230130Smav			continue;
5918230130Smav		w->enable = 1;
5919230130Smav		w->selconn = -1;
5920230130Smav		w->pflags = 0;
5921230130Smav		w->bindas = -1;
5922230130Smav		w->bindseqmask = 0;
5923230130Smav		w->ossdev = -1;
5924230130Smav		w->ossmask = 0;
5925230130Smav		for (j = 0; j < w->nconns; j++)
5926230130Smav			w->connsenable[j] = 1;
5927232798Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5928232798Smav			w->wclass.pin.config = w->wclass.pin.newconf;
5929232798Smav		if (w->eld != NULL) {
5930232798Smav			w->eld_len = 0;
5931232798Smav			free(w->eld, M_HDAA);
5932232798Smav			w->eld = NULL;
5933232798Smav		}
5934230130Smav	}
5935230130Smav}
5936230130Smav
5937230130Smavstatic int
5938230130Smavhdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS)
5939230130Smav{
5940230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5941230130Smav	device_t dev = devinfo->dev;
5942230130Smav	char buf[256];
5943230130Smav	int n = 0, i, numgpi;
5944230130Smav	uint32_t data = 0;
5945230130Smav
5946230130Smav	buf[0] = 0;
5947230130Smav	hdaa_lock(devinfo);
5948230130Smav	numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap);
5949230130Smav	if (numgpi > 0) {
5950230130Smav		data = hda_command(dev,
5951230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
5952230130Smav	}
5953230130Smav	hdaa_unlock(devinfo);
5954230130Smav	for (i = 0; i < numgpi; i++) {
5955230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
5956230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
5957230130Smav	}
5958230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5959230130Smav}
5960230130Smav
5961230130Smavstatic int
5962230130Smavhdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS)
5963230130Smav{
5964230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5965230130Smav	device_t dev = devinfo->dev;
5966230130Smav	char buf[256];
5967230130Smav	int n = 0, i, numgpio;
5968230130Smav	uint32_t data = 0, enable = 0, dir = 0;
5969230130Smav
5970230130Smav	buf[0] = 0;
5971230130Smav	hdaa_lock(devinfo);
5972230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5973230130Smav	if (numgpio > 0) {
5974230130Smav		data = hda_command(dev,
5975230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
5976230130Smav		enable = hda_command(dev,
5977230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
5978230130Smav		dir = hda_command(dev,
5979230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
5980230130Smav	}
5981230130Smav	hdaa_unlock(devinfo);
5982230130Smav	for (i = 0; i < numgpio; i++) {
5983230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=",
5984230130Smav		    n != 0 ? " " : "", i);
5985230130Smav		if ((enable & (1 << i)) == 0) {
5986230130Smav			n += snprintf(buf + n, sizeof(buf) - n, "disabled");
5987230130Smav			continue;
5988230130Smav		}
5989230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)",
5990230130Smav		    ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1));
5991230130Smav	}
5992230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5993230130Smav}
5994230130Smav
5995230130Smavstatic int
5996230130Smavhdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS)
5997230130Smav{
5998230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5999230130Smav	char buf[256];
6000230130Smav	int error, n = 0, i, numgpio;
6001230130Smav	uint32_t gpio, x;
6002230130Smav
6003230130Smav	gpio = devinfo->newgpio;
6004230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6005230130Smav	buf[0] = 0;
6006230130Smav	for (i = 0; i < numgpio; i++) {
6007230130Smav		x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6008230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6009230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6010230130Smav	}
6011230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6012230130Smav	if (error != 0 || req->newptr == NULL)
6013230130Smav		return (error);
6014230130Smav	if (strncmp(buf, "0x", 2) == 0)
6015230130Smav		gpio = strtol(buf + 2, NULL, 16);
6016230130Smav	else
6017230130Smav		gpio = hdaa_gpio_patch(gpio, buf);
6018230130Smav	hdaa_lock(devinfo);
6019230130Smav	devinfo->newgpio = devinfo->gpio = gpio;
6020230130Smav	hdaa_gpio_commit(devinfo);
6021230130Smav	hdaa_unlock(devinfo);
6022230130Smav	return (0);
6023230130Smav}
6024230130Smav
6025230130Smavstatic int
6026230130Smavhdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS)
6027230130Smav{
6028230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6029230130Smav	device_t dev = devinfo->dev;
6030230130Smav	char buf[256];
6031230130Smav	int n = 0, i, numgpo;
6032230130Smav	uint32_t data = 0;
6033230130Smav
6034230130Smav	buf[0] = 0;
6035230130Smav	hdaa_lock(devinfo);
6036230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6037230130Smav	if (numgpo > 0) {
6038230130Smav		data = hda_command(dev,
6039230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
6040230130Smav	}
6041230130Smav	hdaa_unlock(devinfo);
6042230130Smav	for (i = 0; i < numgpo; i++) {
6043230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6044230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
6045230130Smav	}
6046230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6047230130Smav}
6048230130Smav
6049230130Smavstatic int
6050230130Smavhdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS)
6051230130Smav{
6052230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6053230130Smav	char buf[256];
6054230130Smav	int error, n = 0, i, numgpo;
6055230130Smav	uint32_t gpo, x;
6056230130Smav
6057230130Smav	gpo = devinfo->newgpo;
6058230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6059230130Smav	buf[0] = 0;
6060230130Smav	for (i = 0; i < numgpo; i++) {
6061230130Smav		x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6062230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6063230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6064230130Smav	}
6065230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6066230130Smav	if (error != 0 || req->newptr == NULL)
6067230130Smav		return (error);
6068230130Smav	if (strncmp(buf, "0x", 2) == 0)
6069230130Smav		gpo = strtol(buf + 2, NULL, 16);
6070230130Smav	else
6071230130Smav		gpo = hdaa_gpio_patch(gpo, buf);
6072230130Smav	hdaa_lock(devinfo);
6073230130Smav	devinfo->newgpo = devinfo->gpo = gpo;
6074230130Smav	hdaa_gpo_commit(devinfo);
6075230130Smav	hdaa_unlock(devinfo);
6076230130Smav	return (0);
6077230130Smav}
6078230130Smav
6079230130Smavstatic int
6080230130Smavhdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS)
6081230130Smav{
6082230130Smav	device_t dev;
6083230130Smav	struct hdaa_devinfo *devinfo;
6084230130Smav	int error, val;
6085230130Smav
6086230130Smav	dev = oidp->oid_arg1;
6087230130Smav	devinfo = device_get_softc(dev);
6088230130Smav	if (devinfo == NULL)
6089230130Smav		return (EINVAL);
6090230130Smav	val = 0;
6091230130Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6092230130Smav	if (error != 0 || req->newptr == NULL || val == 0)
6093230130Smav		return (error);
6094230130Smav
6095230130Smav	HDA_BOOTHVERBOSE(
6096230130Smav		device_printf(dev, "Reconfiguration...\n");
6097230130Smav	);
6098230130Smav	if ((error = device_delete_children(dev)) != 0)
6099230130Smav		return (error);
6100230130Smav	hdaa_lock(devinfo);
6101230130Smav	hdaa_unconfigure(dev);
6102230130Smav	hdaa_configure(dev);
6103230130Smav	hdaa_unlock(devinfo);
6104230130Smav	bus_generic_attach(dev);
6105230130Smav	HDA_BOOTHVERBOSE(
6106230130Smav		device_printf(dev, "Reconfiguration done\n");
6107230130Smav	);
6108230130Smav	return (0);
6109230130Smav}
6110230130Smav
6111230130Smavstatic int
6112230130Smavhdaa_suspend(device_t dev)
6113230130Smav{
6114230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6115230130Smav	int i;
6116230130Smav
6117230130Smav	HDA_BOOTHVERBOSE(
6118230130Smav		device_printf(dev, "Suspend...\n");
6119230130Smav	);
6120230130Smav	hdaa_lock(devinfo);
6121230130Smav	HDA_BOOTHVERBOSE(
6122230130Smav		device_printf(dev, "Stop streams...\n");
6123230130Smav	);
6124230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6125230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) {
6126230130Smav			devinfo->chans[i].flags |= HDAA_CHN_SUSPEND;
6127230130Smav			hdaa_channel_stop(&devinfo->chans[i]);
6128230130Smav		}
6129230130Smav	}
6130230130Smav	HDA_BOOTHVERBOSE(
6131230130Smav		device_printf(dev, "Power down FG"
6132230130Smav		    " nid=%d to the D3 state...\n",
6133230130Smav		    devinfo->nid);
6134230130Smav	);
6135230130Smav	hda_command(devinfo->dev,
6136230130Smav	    HDA_CMD_SET_POWER_STATE(0,
6137230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D3));
6138230130Smav	callout_stop(&devinfo->poll_jack);
6139230130Smav	hdaa_unlock(devinfo);
6140230130Smav	callout_drain(&devinfo->poll_jack);
6141230130Smav	HDA_BOOTHVERBOSE(
6142230130Smav		device_printf(dev, "Suspend done\n");
6143230130Smav	);
6144230130Smav	return (0);
6145230130Smav}
6146230130Smav
6147230130Smavstatic int
6148230130Smavhdaa_resume(device_t dev)
6149230130Smav{
6150230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6151230130Smav	int i;
6152230130Smav
6153230130Smav	HDA_BOOTHVERBOSE(
6154230130Smav		device_printf(dev, "Resume...\n");
6155230130Smav	);
6156230130Smav	hdaa_lock(devinfo);
6157230130Smav	HDA_BOOTHVERBOSE(
6158230130Smav		device_printf(dev, "Power up audio FG nid=%d...\n",
6159230130Smav		    devinfo->nid);
6160230130Smav	);
6161230130Smav	hdaa_powerup(devinfo);
6162230130Smav	HDA_BOOTHVERBOSE(
6163230130Smav		device_printf(dev, "AFG commit...\n");
6164230130Smav	);
6165230130Smav	hdaa_audio_commit(devinfo);
6166230130Smav	HDA_BOOTHVERBOSE(
6167230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
6168230130Smav	);
6169230130Smav	hdaa_patch_direct(devinfo);
6170230130Smav	HDA_BOOTHVERBOSE(
6171232798Smav		device_printf(dev, "Pin sense init...\n");
6172230130Smav	);
6173232798Smav	hdaa_sense_init(devinfo);
6174230130Smav
6175230130Smav	hdaa_unlock(devinfo);
6176230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
6177230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
6178230130Smav		HDA_BOOTHVERBOSE(
6179230130Smav			device_printf(pdevinfo->dev,
6180230130Smav			    "OSS mixer reinitialization...\n");
6181230130Smav		);
6182230130Smav		if (mixer_reinit(pdevinfo->dev) == -1)
6183230130Smav			device_printf(pdevinfo->dev,
6184230130Smav			    "unable to reinitialize the mixer\n");
6185230130Smav	}
6186230130Smav	hdaa_lock(devinfo);
6187230130Smav	HDA_BOOTHVERBOSE(
6188230130Smav		device_printf(dev, "Start streams...\n");
6189230130Smav	);
6190230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6191230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) {
6192230130Smav			devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND;
6193230130Smav			hdaa_channel_start(&devinfo->chans[i]);
6194230130Smav		}
6195230130Smav	}
6196230130Smav	hdaa_unlock(devinfo);
6197230130Smav	HDA_BOOTHVERBOSE(
6198230130Smav		device_printf(dev, "Resume done\n");
6199230130Smav	);
6200230130Smav	return (0);
6201230130Smav}
6202230130Smav
6203230130Smavstatic int
6204230130Smavhdaa_probe(device_t dev)
6205230130Smav{
6206238137Smav	const char *pdesc;
6207230130Smav	char buf[128];
6208230130Smav
6209230130Smav	if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
6210230130Smav		return (ENXIO);
6211238137Smav	pdesc = device_get_desc(device_get_parent(dev));
6212238137Smav	snprintf(buf, sizeof(buf), "%.*s Audio Function Group",
6213238137Smav	    (int)(strlen(pdesc) - 10), pdesc);
6214230130Smav	device_set_desc_copy(dev, buf);
6215230130Smav	return (BUS_PROBE_DEFAULT);
6216230130Smav}
6217230130Smav
6218230130Smavstatic int
6219230130Smavhdaa_attach(device_t dev)
6220230130Smav{
6221230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6222230130Smav	uint32_t res;
6223230130Smav	nid_t nid = hda_get_node_id(dev);
6224230130Smav
6225230130Smav	devinfo->dev = dev;
6226230130Smav	devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
6227230130Smav	devinfo->nid = nid;
6228230130Smav	devinfo->newquirks = -1;
6229230130Smav	devinfo->newgpio = -1;
6230230130Smav	devinfo->newgpo = -1;
6231230130Smav	callout_init(&devinfo->poll_jack, CALLOUT_MPSAFE);
6232230130Smav	devinfo->poll_ival = hz;
6233230130Smav
6234230130Smav	hdaa_lock(devinfo);
6235230130Smav	res = hda_command(dev,
6236230130Smav	    HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT));
6237230130Smav	hdaa_unlock(devinfo);
6238230130Smav
6239230130Smav	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
6240230130Smav	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
6241230130Smav	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
6242230130Smav
6243230130Smav	HDA_BOOTVERBOSE(
6244243060Smav		device_printf(dev, "Subsystem ID: 0x%08x\n",
6245243060Smav		    hda_get_subsystem_id(dev));
6246243060Smav	);
6247243060Smav	HDA_BOOTHVERBOSE(
6248230130Smav		device_printf(dev,
6249230130Smav		    "Audio Function Group at nid=%d: %d subnodes %d-%d\n",
6250230130Smav		    nid, devinfo->nodecnt,
6251230130Smav		    devinfo->startnode, devinfo->endnode - 1);
6252230130Smav	);
6253230130Smav
6254230130Smav	if (devinfo->nodecnt > 0)
6255230130Smav		devinfo->widget = (struct hdaa_widget *)malloc(
6256230130Smav		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA,
6257230130Smav		    M_WAITOK | M_ZERO);
6258230130Smav	else
6259230130Smav		devinfo->widget = NULL;
6260230130Smav
6261230130Smav	hdaa_lock(devinfo);
6262230130Smav	HDA_BOOTHVERBOSE(
6263230130Smav		device_printf(dev, "Powering up...\n");
6264230130Smav	);
6265230130Smav	hdaa_powerup(devinfo);
6266230130Smav	HDA_BOOTHVERBOSE(
6267230130Smav		device_printf(dev, "Parsing audio FG...\n");
6268230130Smav	);
6269230130Smav	hdaa_audio_parse(devinfo);
6270230130Smav	HDA_BOOTVERBOSE(
6271230130Smav		device_printf(dev, "Original pins configuration:\n");
6272230130Smav		hdaa_dump_pin_configs(devinfo);
6273230130Smav	);
6274230130Smav	hdaa_configure(dev);
6275230130Smav	hdaa_unlock(devinfo);
6276230130Smav
6277230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6278230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6279230130Smav	    "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6280230130Smav	    &devinfo->newquirks, sizeof(&devinfo->newquirks),
6281230130Smav	    hdaa_sysctl_quirks, "A", "Configuration options");
6282230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6283230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6284230130Smav	    "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6285230130Smav	    devinfo, sizeof(devinfo),
6286230130Smav	    hdaa_sysctl_gpi_state, "A", "GPI state");
6287230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6288230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6289230130Smav	    "gpio_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6290230130Smav	    devinfo, sizeof(devinfo),
6291230130Smav	    hdaa_sysctl_gpio_state, "A", "GPIO state");
6292230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6293230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6294230130Smav	    "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6295230130Smav	    devinfo, sizeof(devinfo),
6296230130Smav	    hdaa_sysctl_gpio_config, "A", "GPIO configuration");
6297230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6298230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6299230130Smav	    "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6300230130Smav	    devinfo, sizeof(devinfo),
6301230130Smav	    hdaa_sysctl_gpo_state, "A", "GPO state");
6302230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6303230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6304230130Smav	    "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6305230130Smav	    devinfo, sizeof(devinfo),
6306230130Smav	    hdaa_sysctl_gpo_config, "A", "GPO configuration");
6307230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6308230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6309230130Smav	    "reconfig", CTLTYPE_INT | CTLFLAG_RW,
6310230130Smav	    dev, sizeof(dev),
6311230130Smav	    hdaa_sysctl_reconfig, "I", "Reprocess configuration");
6312230130Smav	bus_generic_attach(dev);
6313230130Smav	return (0);
6314230130Smav}
6315230130Smav
6316230130Smavstatic int
6317230130Smavhdaa_detach(device_t dev)
6318230130Smav{
6319230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6320230130Smav	int error;
6321230130Smav
6322230130Smav	if ((error = device_delete_children(dev)) != 0)
6323230130Smav		return (error);
6324230130Smav
6325230130Smav	hdaa_lock(devinfo);
6326230130Smav	hdaa_unconfigure(dev);
6327230130Smav	devinfo->poll_ival = 0;
6328230130Smav	callout_stop(&devinfo->poll_jack);
6329230130Smav	hdaa_unlock(devinfo);
6330230130Smav	callout_drain(&devinfo->poll_jack);
6331230130Smav
6332230130Smav	free(devinfo->widget, M_HDAA);
6333230130Smav	return (0);
6334230130Smav}
6335230130Smav
6336230130Smavstatic int
6337230130Smavhdaa_print_child(device_t dev, device_t child)
6338230130Smav{
6339230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6340230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6341230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6342230130Smav	struct hdaa_audio_as *as;
6343230130Smav	int retval, first = 1, i;
6344230130Smav
6345230130Smav	retval = bus_print_child_header(dev, child);
6346230130Smav	retval += printf(" at nid ");
6347230130Smav	if (pdevinfo->playas >= 0) {
6348230130Smav		as = &devinfo->as[pdevinfo->playas];
6349230130Smav		for (i = 0; i < 16; i++) {
6350230130Smav			if (as->pins[i] <= 0)
6351230130Smav				continue;
6352230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6353230130Smav			first = 0;
6354230130Smav		}
6355230130Smav	}
6356230130Smav	if (pdevinfo->recas >= 0) {
6357230130Smav		if (pdevinfo->playas >= 0) {
6358230130Smav			retval += printf(" and ");
6359230130Smav			first = 1;
6360230130Smav		}
6361230130Smav		as = &devinfo->as[pdevinfo->recas];
6362230130Smav		for (i = 0; i < 16; i++) {
6363230130Smav			if (as->pins[i] <= 0)
6364230130Smav				continue;
6365230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6366230130Smav			first = 0;
6367230130Smav		}
6368230130Smav	}
6369230130Smav	retval += bus_print_child_footer(dev, child);
6370230130Smav
6371230130Smav	return (retval);
6372230130Smav}
6373230130Smav
6374230130Smavstatic int
6375230130Smavhdaa_child_location_str(device_t dev, device_t child, char *buf,
6376230130Smav    size_t buflen)
6377230130Smav{
6378230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6379230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6380230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6381230130Smav	struct hdaa_audio_as *as;
6382230130Smav	int first = 1, i, len = 0;
6383230130Smav
6384230130Smav	len += snprintf(buf + len, buflen - len, "nid=");
6385230130Smav	if (pdevinfo->playas >= 0) {
6386230130Smav		as = &devinfo->as[pdevinfo->playas];
6387230130Smav		for (i = 0; i < 16; i++) {
6388230130Smav			if (as->pins[i] <= 0)
6389230130Smav				continue;
6390230130Smav			len += snprintf(buf + len, buflen - len,
6391230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6392230130Smav			first = 0;
6393230130Smav		}
6394230130Smav	}
6395230130Smav	if (pdevinfo->recas >= 0) {
6396230130Smav		as = &devinfo->as[pdevinfo->recas];
6397230130Smav		for (i = 0; i < 16; i++) {
6398230130Smav			if (as->pins[i] <= 0)
6399230130Smav				continue;
6400230130Smav			len += snprintf(buf + len, buflen - len,
6401230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6402230130Smav			first = 0;
6403230130Smav		}
6404230130Smav	}
6405230130Smav	return (0);
6406230130Smav}
6407230130Smav
6408230130Smavstatic void
6409230130Smavhdaa_stream_intr(device_t dev, int dir, int stream)
6410230130Smav{
6411230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6412230130Smav	struct hdaa_chan *ch;
6413230130Smav	int i;
6414230130Smav
6415230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6416230130Smav		ch = &devinfo->chans[i];
6417230130Smav		if (!(ch->flags & HDAA_CHN_RUNNING))
6418230130Smav			continue;
6419230130Smav		if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) &&
6420230130Smav		    ch->sid == stream) {
6421230130Smav			hdaa_unlock(devinfo);
6422230130Smav			chn_intr(ch->c);
6423230130Smav			hdaa_lock(devinfo);
6424230130Smav		}
6425230130Smav	}
6426230130Smav}
6427230130Smav
6428230130Smavstatic void
6429230130Smavhdaa_unsol_intr(device_t dev, uint32_t resp)
6430230130Smav{
6431230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6432232798Smav	struct hdaa_widget *w;
6433232798Smav	int i, tag, flags;
6434230130Smav
6435232798Smav	HDA_BOOTHVERBOSE(
6436232798Smav		device_printf(dev, "Unsolicited response %08x\n", resp);
6437232798Smav	);
6438230130Smav	tag = resp >> 26;
6439232798Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6440232798Smav		w = hdaa_widget_get(devinfo, i);
6441232798Smav		if (w == NULL || w->enable == 0 || w->type !=
6442232798Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6443232798Smav			continue;
6444232798Smav		if (w->unsol != tag)
6445232798Smav			continue;
6446232798Smav		if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
6447232798Smav		    HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
6448232798Smav			flags = resp & 0x03;
6449232798Smav		else
6450232798Smav			flags = 0x01;
6451232798Smav		if (flags & 0x01)
6452232798Smav			hdaa_presence_handler(w);
6453232798Smav		if (flags & 0x02)
6454232798Smav			hdaa_eld_handler(w);
6455230130Smav	}
6456230130Smav}
6457230130Smav
6458230130Smavstatic device_method_t hdaa_methods[] = {
6459230130Smav	/* device interface */
6460230130Smav	DEVMETHOD(device_probe,		hdaa_probe),
6461230130Smav	DEVMETHOD(device_attach,	hdaa_attach),
6462230130Smav	DEVMETHOD(device_detach,	hdaa_detach),
6463230130Smav	DEVMETHOD(device_suspend,	hdaa_suspend),
6464230130Smav	DEVMETHOD(device_resume,	hdaa_resume),
6465230130Smav	/* Bus interface */
6466230130Smav	DEVMETHOD(bus_print_child,	hdaa_print_child),
6467230130Smav	DEVMETHOD(bus_child_location_str, hdaa_child_location_str),
6468230130Smav	DEVMETHOD(hdac_stream_intr,	hdaa_stream_intr),
6469230130Smav	DEVMETHOD(hdac_unsol_intr,	hdaa_unsol_intr),
6470230130Smav	DEVMETHOD(hdac_pindump,		hdaa_pindump),
6471264962Smarius	DEVMETHOD_END
6472230130Smav};
6473230130Smav
6474230130Smavstatic driver_t hdaa_driver = {
6475230130Smav	"hdaa",
6476230130Smav	hdaa_methods,
6477230130Smav	sizeof(struct hdaa_devinfo),
6478230130Smav};
6479230130Smav
6480230130Smavstatic devclass_t hdaa_devclass;
6481230130Smav
6482264962SmariusDRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, NULL, NULL);
6483230130Smav
6484230130Smavstatic void
6485230130Smavhdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid,
6486230130Smav    char *buf, int buflen)
6487230130Smav{
6488230130Smav	struct hdaa_audio_as *as;
6489230130Smav	int c;
6490230130Smav
6491230130Smav	as = &devinfo->as[asid];
6492230130Smav	c = devinfo->chans[as->chans[0]].channels;
6493230130Smav	if (c == 1)
6494230130Smav		snprintf(buf, buflen, "mono");
6495232798Smav	else if (c == 2) {
6496232798Smav		if (as->hpredir < 0)
6497232798Smav			buf[0] = 0;
6498232798Smav		else
6499232798Smav			snprintf(buf, buflen, "2.0");
6500232798Smav	} else if (as->pinset == 0x0003)
6501230130Smav		snprintf(buf, buflen, "3.1");
6502230130Smav	else if (as->pinset == 0x0005 || as->pinset == 0x0011)
6503230130Smav		snprintf(buf, buflen, "4.0");
6504230130Smav	else if (as->pinset == 0x0007 || as->pinset == 0x0013)
6505230130Smav		snprintf(buf, buflen, "5.1");
6506230130Smav	else if (as->pinset == 0x0017)
6507230130Smav		snprintf(buf, buflen, "7.1");
6508230130Smav	else
6509230130Smav		snprintf(buf, buflen, "%dch", c);
6510232798Smav	if (as->hpredir >= 0)
6511232798Smav		strlcat(buf, "+HP", buflen);
6512230130Smav}
6513230130Smav
6514230130Smavstatic int
6515232798Smavhdaa_chan_type(struct hdaa_devinfo *devinfo, int asid)
6516232798Smav{
6517232798Smav	struct hdaa_audio_as *as;
6518232798Smav	struct hdaa_widget *w;
6519232798Smav	int i, t = -1, t1;
6520232798Smav
6521232798Smav	as = &devinfo->as[asid];
6522232798Smav	for (i = 0; i < 16; i++) {
6523232798Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
6524232798Smav		if (w == NULL || w->enable == 0 || w->type !=
6525232798Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6526232798Smav			continue;
6527232798Smav		t1 = HDA_CONFIG_DEFAULTCONF_DEVICE(w->wclass.pin.config);
6528232798Smav		if (t == -1)
6529232798Smav			t = t1;
6530232798Smav		else if (t != t1) {
6531232798Smav			t = -2;
6532232798Smav			break;
6533232798Smav		}
6534232798Smav	}
6535232798Smav	return (t);
6536232798Smav}
6537232798Smav
6538232798Smavstatic int
6539232798Smavhdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS)
6540232798Smav{
6541232798Smav	struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1;
6542232798Smav	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
6543232798Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6544232798Smav	struct hdaa_chan *ch;
6545232798Smav	int error, val, i;
6546232798Smav	uint32_t pcmcap;
6547232798Smav
6548232798Smav	ch = &devinfo->chans[as->chans[0]];
6549232798Smav	val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 :
6550232798Smav	    ((ch->bit32 == 2) ? 20 : 0));
6551232798Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6552232798Smav	if (error != 0 || req->newptr == NULL)
6553232798Smav		return (error);
6554232798Smav	pcmcap = ch->supp_pcm_size_rate;
6555232798Smav	if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6556232798Smav		ch->bit32 = 4;
6557232798Smav	else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6558232798Smav		ch->bit32 = 3;
6559232798Smav	else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6560232798Smav		ch->bit32 = 2;
6561232798Smav	else
6562232798Smav		return (EINVAL);
6563232798Smav	for (i = 1; i < as->num_chans; i++)
6564232798Smav		devinfo->chans[as->chans[i]].bit32 = ch->bit32;
6565232798Smav	return (0);
6566232798Smav}
6567232798Smav
6568232798Smavstatic int
6569230130Smavhdaa_pcm_probe(device_t dev)
6570230130Smav{
6571230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6572230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6573230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6574238137Smav	const char *pdesc;
6575230130Smav	char chans1[8], chans2[8];
6576230130Smav	char buf[128];
6577232798Smav	int loc1, loc2, t1, t2;
6578230130Smav
6579230130Smav	if (pdevinfo->playas >= 0)
6580230130Smav		loc1 = devinfo->as[pdevinfo->playas].location;
6581230130Smav	else
6582230130Smav		loc1 = devinfo->as[pdevinfo->recas].location;
6583230130Smav	if (pdevinfo->recas >= 0)
6584230130Smav		loc2 = devinfo->as[pdevinfo->recas].location;
6585230130Smav	else
6586230130Smav		loc2 = loc1;
6587230130Smav	if (loc1 != loc2)
6588230130Smav		loc1 = -2;
6589230130Smav	if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0')
6590230130Smav		loc1 = -2;
6591230130Smav	chans1[0] = 0;
6592230130Smav	chans2[0] = 0;
6593232798Smav	t1 = t2 = -1;
6594232798Smav	if (pdevinfo->playas >= 0) {
6595230130Smav		hdaa_chan_formula(devinfo, pdevinfo->playas,
6596230130Smav		    chans1, sizeof(chans1));
6597232798Smav		t1 = hdaa_chan_type(devinfo, pdevinfo->playas);
6598232798Smav	}
6599232798Smav	if (pdevinfo->recas >= 0) {
6600230130Smav		hdaa_chan_formula(devinfo, pdevinfo->recas,
6601230130Smav		    chans2, sizeof(chans2));
6602232798Smav		t2 = hdaa_chan_type(devinfo, pdevinfo->recas);
6603232798Smav	}
6604230130Smav	if (chans1[0] != 0 || chans2[0] != 0) {
6605230130Smav		if (chans1[0] == 0 && pdevinfo->playas >= 0)
6606230130Smav			snprintf(chans1, sizeof(chans1), "2.0");
6607230130Smav		else if (chans2[0] == 0 && pdevinfo->recas >= 0)
6608230130Smav			snprintf(chans2, sizeof(chans2), "2.0");
6609230130Smav		if (strcmp(chans1, chans2) == 0)
6610230130Smav			chans2[0] = 0;
6611230130Smav	}
6612232798Smav	if (t1 == -1)
6613232798Smav		t1 = t2;
6614232798Smav	else if (t2 == -1)
6615232798Smav		t2 = t1;
6616232798Smav	if (t1 != t2)
6617232798Smav		t1 = -2;
6618232798Smav	if (pdevinfo->digital)
6619232798Smav		t1 = -2;
6620238137Smav	pdesc = device_get_desc(device_get_parent(dev));
6621238137Smav	snprintf(buf, sizeof(buf), "%.*s (%s%s%s%s%s%s%s%s%s)",
6622238137Smav	    (int)(strlen(pdesc) - 21), pdesc,
6623230130Smav	    loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "",
6624232798Smav	    (pdevinfo->digital == 0x7)?"HDMI/DP":
6625232798Smav	    ((pdevinfo->digital == 0x5)?"DisplayPort":
6626232798Smav	    ((pdevinfo->digital == 0x3)?"HDMI":
6627232798Smav	    ((pdevinfo->digital)?"Digital":"Analog"))),
6628230130Smav	    chans1[0] ? " " : "", chans1,
6629232798Smav	    chans2[0] ? "/" : "", chans2,
6630232798Smav	    t1 >= 0 ? " " : "", t1 >= 0 ? HDA_DEVS[t1] : "");
6631230130Smav	device_set_desc_copy(dev, buf);
6632230130Smav	return (BUS_PROBE_SPECIFIC);
6633230130Smav}
6634230130Smav
6635230130Smavstatic int
6636230130Smavhdaa_pcm_attach(device_t dev)
6637230130Smav{
6638230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6639230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6640230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6641230130Smav	struct hdaa_audio_as *as;
6642232798Smav	struct snddev_info *d;
6643230130Smav	char status[SND_STATUSLEN];
6644230130Smav	int i;
6645230130Smav
6646230130Smav	pdevinfo->chan_size = pcm_getbuffersize(dev,
6647230130Smav	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
6648230130Smav
6649230130Smav	HDA_BOOTVERBOSE(
6650230130Smav		device_printf(dev, "+--------------------------------------+\n");
6651230130Smav		device_printf(dev, "| DUMPING PCM Playback/Record Channels |\n");
6652230130Smav		device_printf(dev, "+--------------------------------------+\n");
6653230130Smav		hdaa_dump_pcmchannels(pdevinfo);
6654230130Smav		device_printf(dev, "\n");
6655230130Smav		device_printf(dev, "+-------------------------------+\n");
6656230130Smav		device_printf(dev, "| DUMPING Playback/Record Paths |\n");
6657230130Smav		device_printf(dev, "+-------------------------------+\n");
6658230130Smav		hdaa_dump_dac(pdevinfo);
6659230130Smav		hdaa_dump_adc(pdevinfo);
6660230130Smav		hdaa_dump_mix(pdevinfo);
6661230130Smav		device_printf(dev, "\n");
6662230130Smav		device_printf(dev, "+-------------------------+\n");
6663230130Smav		device_printf(dev, "| DUMPING Volume Controls |\n");
6664230130Smav		device_printf(dev, "+-------------------------+\n");
6665230130Smav		hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
6666230130Smav		hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
6667230130Smav		hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
6668230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
6669230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
6670230130Smav		hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
6671230130Smav		hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
6672230130Smav		hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
6673230130Smav		hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
6674230130Smav		hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
6675230130Smav		hdaa_dump_ctls(pdevinfo, NULL, 0);
6676230130Smav		device_printf(dev, "\n");
6677230130Smav	);
6678230130Smav
6679230130Smav	if (resource_int_value(device_get_name(dev),
6680230130Smav	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
6681230130Smav		i &= HDA_BLK_ALIGN;
6682230130Smav		if (i < HDA_BLK_MIN)
6683230130Smav			i = HDA_BLK_MIN;
6684230130Smav		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
6685230130Smav		i = 0;
6686230130Smav		while (pdevinfo->chan_blkcnt >> i)
6687230130Smav			i++;
6688230130Smav		pdevinfo->chan_blkcnt = 1 << (i - 1);
6689230130Smav		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
6690230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
6691230130Smav		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
6692230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
6693230130Smav	} else
6694230130Smav		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
6695230130Smav
6696230130Smav	/*
6697230130Smav	 * We don't register interrupt handler with snd_setup_intr
6698230130Smav	 * in pcm device. Mark pcm device as MPSAFE manually.
6699230130Smav	 */
6700230130Smav	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
6701230130Smav
6702230130Smav	HDA_BOOTHVERBOSE(
6703230130Smav		device_printf(dev, "OSS mixer initialization...\n");
6704230130Smav	);
6705230130Smav	if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0)
6706230130Smav		device_printf(dev, "Can't register mixer\n");
6707230130Smav
6708230130Smav	HDA_BOOTHVERBOSE(
6709230130Smav		device_printf(dev, "Registering PCM channels...\n");
6710230130Smav	);
6711230130Smav	if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0,
6712230130Smav	    (pdevinfo->recas >= 0)?1:0) != 0)
6713230130Smav		device_printf(dev, "Can't register PCM\n");
6714230130Smav
6715230130Smav	pdevinfo->registered++;
6716230130Smav
6717232798Smav	d = device_get_softc(dev);
6718230130Smav	if (pdevinfo->playas >= 0) {
6719230130Smav		as = &devinfo->as[pdevinfo->playas];
6720230130Smav		for (i = 0; i < as->num_chans; i++)
6721230130Smav			pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class,
6722230130Smav			    &devinfo->chans[as->chans[i]]);
6723232798Smav		SYSCTL_ADD_PROC(&d->play_sysctl_ctx,
6724232798Smav		    SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO,
6725232798Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6726232798Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
6727232798Smav		    "Resolution of 32bit samples (20/24/32bit)");
6728230130Smav	}
6729230130Smav	if (pdevinfo->recas >= 0) {
6730230130Smav		as = &devinfo->as[pdevinfo->recas];
6731230130Smav		for (i = 0; i < as->num_chans; i++)
6732230130Smav			pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class,
6733230130Smav			    &devinfo->chans[as->chans[i]]);
6734232798Smav		SYSCTL_ADD_PROC(&d->rec_sysctl_ctx,
6735232798Smav		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
6736232798Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6737232798Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
6738232798Smav		    "Resolution of 32bit samples (20/24/32bit)");
6739232798Smav		pdevinfo->autorecsrc = 2;
6740232798Smav		resource_int_value(device_get_name(dev), device_get_unit(dev),
6741232798Smav		    "rec.autosrc", &pdevinfo->autorecsrc);
6742232798Smav		SYSCTL_ADD_INT(&d->rec_sysctl_ctx,
6743232798Smav		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
6744232798Smav		    "autosrc", CTLTYPE_INT | CTLFLAG_RW,
6745232798Smav		    &pdevinfo->autorecsrc, 0,
6746232798Smav		    "Automatic recording source selection");
6747230130Smav	}
6748230130Smav
6749232798Smav	if (pdevinfo->mixer != NULL) {
6750232798Smav		hdaa_audio_ctl_set_defaults(pdevinfo);
6751232798Smav		if (pdevinfo->recas >= 0) {
6752232798Smav			as = &devinfo->as[pdevinfo->recas];
6753232798Smav			hdaa_lock(devinfo);
6754232798Smav			hdaa_autorecsrc_handler(as, NULL);
6755232798Smav			hdaa_unlock(devinfo);
6756232798Smav		}
6757232798Smav	}
6758232798Smav
6759230130Smav	snprintf(status, SND_STATUSLEN, "on %s %s",
6760230130Smav	    device_get_nameunit(device_get_parent(dev)),
6761230130Smav	    PCM_KLDSTRING(snd_hda));
6762230130Smav	pcm_setstatus(dev, status);
6763230130Smav
6764230130Smav	return (0);
6765230130Smav}
6766230130Smav
6767230130Smavstatic int
6768230130Smavhdaa_pcm_detach(device_t dev)
6769230130Smav{
6770230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6771230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6772230130Smav	int err;
6773230130Smav
6774230130Smav	if (pdevinfo->registered > 0) {
6775230130Smav		err = pcm_unregister(dev);
6776230130Smav		if (err != 0)
6777230130Smav			return (err);
6778230130Smav	}
6779230130Smav
6780230130Smav	return (0);
6781230130Smav}
6782230130Smav
6783230130Smavstatic device_method_t hdaa_pcm_methods[] = {
6784230130Smav	/* device interface */
6785230130Smav	DEVMETHOD(device_probe,		hdaa_pcm_probe),
6786230130Smav	DEVMETHOD(device_attach,	hdaa_pcm_attach),
6787230130Smav	DEVMETHOD(device_detach,	hdaa_pcm_detach),
6788264962Smarius	DEVMETHOD_END
6789230130Smav};
6790230130Smav
6791230130Smavstatic driver_t hdaa_pcm_driver = {
6792230130Smav	"pcm",
6793230130Smav	hdaa_pcm_methods,
6794230130Smav	PCM_SOFTC_SIZE,
6795230130Smav};
6796230130Smav
6797264962SmariusDRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, NULL, NULL);
6798230130SmavMODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
6799230130SmavMODULE_VERSION(snd_hda, 1);
6800