hdaa.c revision 230585
1230130Smav/*-
2230130Smav * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3230130Smav * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4230130Smav * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
5230130Smav * All rights reserved.
6230130Smav *
7230130Smav * Redistribution and use in source and binary forms, with or without
8230130Smav * modification, are permitted provided that the following conditions
9230130Smav * are met:
10230130Smav * 1. Redistributions of source code must retain the above copyright
11230130Smav *    notice, this list of conditions and the following disclaimer.
12230130Smav * 2. Redistributions in binary form must reproduce the above copyright
13230130Smav *    notice, this list of conditions and the following disclaimer in the
14230130Smav *    documentation and/or other materials provided with the distribution.
15230130Smav *
16230130Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17230130Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18230130Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19230130Smav * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20230130Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21230130Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22230130Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23230130Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24230130Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25230130Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26230130Smav * SUCH DAMAGE.
27230130Smav */
28230130Smav
29230130Smav/*
30230130Smav * Intel High Definition Audio (Audio function) driver for FreeBSD.
31230130Smav */
32230130Smav
33230130Smav#ifdef HAVE_KERNEL_OPTION_HEADERS
34230130Smav#include "opt_snd.h"
35230130Smav#endif
36230130Smav
37230130Smav#include <dev/sound/pcm/sound.h>
38230130Smav
39230130Smav#include <sys/ctype.h>
40230130Smav#include <sys/taskqueue.h>
41230130Smav
42230130Smav#include <dev/sound/pci/hda/hdac.h>
43230130Smav#include <dev/sound/pci/hda/hdaa.h>
44230130Smav#include <dev/sound/pci/hda/hda_reg.h>
45230130Smav
46230130Smav#include "mixer_if.h"
47230130Smav
48230130SmavSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/hda/hdaa.c 230585 2012-01-26 12:09:04Z mav $");
49230130Smav
50230130Smav#define hdaa_lock(devinfo)	snd_mtxlock((devinfo)->lock)
51230130Smav#define hdaa_unlock(devinfo)	snd_mtxunlock((devinfo)->lock)
52230130Smav#define hdaa_lockassert(devinfo) snd_mtxassert((devinfo)->lock)
53230130Smav#define hdaa_lockowned(devinfo)	mtx_owned((devinfo)->lock)
54230130Smav
55230130Smavstatic const struct {
56230130Smav	char *key;
57230130Smav	uint32_t value;
58230130Smav} hdaa_quirks_tab[] = {
59230130Smav	{ "softpcmvol", HDAA_QUIRK_SOFTPCMVOL },
60230130Smav	{ "fixedrate", HDAA_QUIRK_FIXEDRATE },
61230130Smav	{ "forcestereo", HDAA_QUIRK_FORCESTEREO },
62230130Smav	{ "eapdinv", HDAA_QUIRK_EAPDINV },
63230130Smav	{ "senseinv", HDAA_QUIRK_SENSEINV },
64230130Smav	{ "ivref50", HDAA_QUIRK_IVREF50 },
65230130Smav	{ "ivref80", HDAA_QUIRK_IVREF80 },
66230130Smav	{ "ivref100", HDAA_QUIRK_IVREF100 },
67230130Smav	{ "ovref50", HDAA_QUIRK_OVREF50 },
68230130Smav	{ "ovref80", HDAA_QUIRK_OVREF80 },
69230130Smav	{ "ovref100", HDAA_QUIRK_OVREF100 },
70230130Smav	{ "ivref", HDAA_QUIRK_IVREF },
71230130Smav	{ "ovref", HDAA_QUIRK_OVREF },
72230130Smav	{ "vref", HDAA_QUIRK_VREF },
73230130Smav};
74230130Smav#define HDAA_QUIRKS_TAB_LEN	\
75230130Smav		(sizeof(hdaa_quirks_tab) / sizeof(hdaa_quirks_tab[0]))
76230130Smav
77230130Smav#define HDA_PARSE_MAXDEPTH	10
78230130Smav
79230130SmavMALLOC_DEFINE(M_HDAA, "hdaa", "HDA Audio");
80230130Smav
81230130Smavconst char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue", "Green", "Red",
82230130Smav    "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B", "Res.C", "Res.D",
83230130Smav    "White", "Other"};
84230130Smav
85230130Smavconst char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD",
86230130Smav    "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in",
87230130Smav    "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"};
88230130Smav
89230130Smavconst char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
90230130Smav
91230130Smavconst char *HDA_CONNECTORS[16] = {
92230130Smav    "Unknown", "1/8", "1/4", "ATAPI", "RCA", "Optical", "Digital", "Analog",
93230130Smav    "DIN", "XLR", "RJ-11", "Combo", "0xc", "0xd", "0xe", "Other" };
94230130Smav
95230130Smavconst char *HDA_LOCS[64] = {
96230130Smav    "0x00", "Rear", "Front", "Left", "Right", "Top", "Bottom", "Rear-panel",
97230130Smav	"Drive-bay", "0x09", "0x0a", "0x0b", "0x0c", "0x0d", "0x0e", "0x0f",
98230130Smav    "Internal", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "Riser",
99230130Smav	"0x18", "Onboard", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f",
100230130Smav    "External", "Ext-Rear", "Ext-Front", "Ext-Left", "Ext-Right", "Ext-Top", "Ext-Bottom", "0x07",
101230130Smav	"0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f",
102230130Smav    "Other", "0x31", "0x32", "0x33", "0x34", "0x35", "Other-Bott", "Lid-In",
103230130Smav	"Lid-Out", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f" };
104230130Smav
105230130Smavconst char *HDA_GPIO_ACTIONS[8] = {
106230130Smav    "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"};
107230130Smav
108230312Smavconst char *HDA_HDMI_CODING_TYPES[18] = {
109230312Smav    "undefined", "LPCM", "AC-3", "MPEG1", "MP3", "MPEG2", "AAC-LC", "DTS",
110230312Smav    "ATRAC", "DSD", "E-AC-3", "DTS-HD", "MLP", "DST", "WMAPro", "HE-AAC",
111230312Smav    "HE-AACv2", "MPEG-Surround"
112230312Smav};
113230312Smav
114230130Smav/* Default */
115230130Smavstatic uint32_t hdaa_fmt[] = {
116230130Smav	SND_FORMAT(AFMT_S16_LE, 2, 0),
117230130Smav	0
118230130Smav};
119230130Smav
120230130Smavstatic struct pcmchan_caps hdaa_caps = {48000, 48000, hdaa_fmt, 0};
121230130Smav
122230130Smavstatic const struct {
123230130Smav	uint32_t	rate;
124230130Smav	int		valid;
125230130Smav	uint16_t	base;
126230130Smav	uint16_t	mul;
127230130Smav	uint16_t	div;
128230130Smav} hda_rate_tab[] = {
129230130Smav	{   8000, 1, 0x0000, 0x0000, 0x0500 },	/* (48000 * 1) / 6 */
130230130Smav	{   9600, 0, 0x0000, 0x0000, 0x0400 },	/* (48000 * 1) / 5 */
131230130Smav	{  12000, 0, 0x0000, 0x0000, 0x0300 },	/* (48000 * 1) / 4 */
132230130Smav	{  16000, 1, 0x0000, 0x0000, 0x0200 },	/* (48000 * 1) / 3 */
133230130Smav	{  18000, 0, 0x0000, 0x1000, 0x0700 },	/* (48000 * 3) / 8 */
134230130Smav	{  19200, 0, 0x0000, 0x0800, 0x0400 },	/* (48000 * 2) / 5 */
135230130Smav	{  24000, 0, 0x0000, 0x0000, 0x0100 },	/* (48000 * 1) / 2 */
136230130Smav	{  28800, 0, 0x0000, 0x1000, 0x0400 },	/* (48000 * 3) / 5 */
137230130Smav	{  32000, 1, 0x0000, 0x0800, 0x0200 },	/* (48000 * 2) / 3 */
138230130Smav	{  36000, 0, 0x0000, 0x1000, 0x0300 },	/* (48000 * 3) / 4 */
139230130Smav	{  38400, 0, 0x0000, 0x1800, 0x0400 },	/* (48000 * 4) / 5 */
140230130Smav	{  48000, 1, 0x0000, 0x0000, 0x0000 },	/* (48000 * 1) / 1 */
141230130Smav	{  64000, 0, 0x0000, 0x1800, 0x0200 },	/* (48000 * 4) / 3 */
142230130Smav	{  72000, 0, 0x0000, 0x1000, 0x0100 },	/* (48000 * 3) / 2 */
143230130Smav	{  96000, 1, 0x0000, 0x0800, 0x0000 },	/* (48000 * 2) / 1 */
144230130Smav	{ 144000, 0, 0x0000, 0x1000, 0x0000 },	/* (48000 * 3) / 1 */
145230130Smav	{ 192000, 1, 0x0000, 0x1800, 0x0000 },	/* (48000 * 4) / 1 */
146230130Smav	{   8820, 0, 0x4000, 0x0000, 0x0400 },	/* (44100 * 1) / 5 */
147230130Smav	{  11025, 1, 0x4000, 0x0000, 0x0300 },	/* (44100 * 1) / 4 */
148230130Smav	{  12600, 0, 0x4000, 0x0800, 0x0600 },	/* (44100 * 2) / 7 */
149230130Smav	{  14700, 0, 0x4000, 0x0000, 0x0200 },	/* (44100 * 1) / 3 */
150230130Smav	{  17640, 0, 0x4000, 0x0800, 0x0400 },	/* (44100 * 2) / 5 */
151230130Smav	{  18900, 0, 0x4000, 0x1000, 0x0600 },	/* (44100 * 3) / 7 */
152230130Smav	{  22050, 1, 0x4000, 0x0000, 0x0100 },	/* (44100 * 1) / 2 */
153230130Smav	{  25200, 0, 0x4000, 0x1800, 0x0600 },	/* (44100 * 4) / 7 */
154230130Smav	{  26460, 0, 0x4000, 0x1000, 0x0400 },	/* (44100 * 3) / 5 */
155230130Smav	{  29400, 0, 0x4000, 0x0800, 0x0200 },	/* (44100 * 2) / 3 */
156230130Smav	{  33075, 0, 0x4000, 0x1000, 0x0300 },	/* (44100 * 3) / 4 */
157230130Smav	{  35280, 0, 0x4000, 0x1800, 0x0400 },	/* (44100 * 4) / 5 */
158230130Smav	{  44100, 1, 0x4000, 0x0000, 0x0000 },	/* (44100 * 1) / 1 */
159230130Smav	{  58800, 0, 0x4000, 0x1800, 0x0200 },	/* (44100 * 4) / 3 */
160230130Smav	{  66150, 0, 0x4000, 0x1000, 0x0100 },	/* (44100 * 3) / 2 */
161230130Smav	{  88200, 1, 0x4000, 0x0800, 0x0000 },	/* (44100 * 2) / 1 */
162230130Smav	{ 132300, 0, 0x4000, 0x1000, 0x0000 },	/* (44100 * 3) / 1 */
163230130Smav	{ 176400, 1, 0x4000, 0x1800, 0x0000 },	/* (44100 * 4) / 1 */
164230130Smav};
165230130Smav#define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
166230130Smav
167230451Smavconst static char *ossnames[] = SOUND_DEVICE_NAMES;
168230451Smav
169230130Smav/****************************************************************************
170230130Smav * Function prototypes
171230130Smav ****************************************************************************/
172230130Smavstatic int	hdaa_pcmchannel_setup(struct hdaa_chan *);
173230130Smav
174230130Smavstatic void	hdaa_widget_connection_select(struct hdaa_widget *, uint8_t);
175230130Smavstatic void	hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *,
176230130Smav						uint32_t, int, int);
177230130Smavstatic struct	hdaa_audio_ctl *hdaa_audio_ctl_amp_get(struct hdaa_devinfo *,
178230130Smav							nid_t, int, int, int);
179230130Smavstatic void	hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *,
180230130Smav				nid_t, int, int, int, int, int, int);
181230130Smav
182230130Smavstatic void	hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf);
183230130Smav
184230130Smavstatic char *
185230130Smavhdaa_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
186230130Smav{
187230130Smav	int i, first = 1;
188230130Smav
189230130Smav	bzero(buf, len);
190230130Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
191230130Smav		if (mask & (1 << i)) {
192230130Smav			if (first == 0)
193230130Smav				strlcat(buf, ", ", len);
194230451Smav			strlcat(buf, ossnames[i], len);
195230130Smav			first = 0;
196230130Smav		}
197230130Smav	}
198230130Smav	return (buf);
199230130Smav}
200230130Smav
201230130Smavstatic struct hdaa_audio_ctl *
202230130Smavhdaa_audio_ctl_each(struct hdaa_devinfo *devinfo, int *index)
203230130Smav{
204230130Smav	if (devinfo == NULL ||
205230130Smav	    index == NULL || devinfo->ctl == NULL ||
206230130Smav	    devinfo->ctlcnt < 1 ||
207230130Smav	    *index < 0 || *index >= devinfo->ctlcnt)
208230130Smav		return (NULL);
209230130Smav	return (&devinfo->ctl[(*index)++]);
210230130Smav}
211230130Smav
212230130Smavstatic struct hdaa_audio_ctl *
213230130Smavhdaa_audio_ctl_amp_get(struct hdaa_devinfo *devinfo, nid_t nid, int dir,
214230130Smav						int index, int cnt)
215230130Smav{
216230130Smav	struct hdaa_audio_ctl *ctl;
217230130Smav	int i, found = 0;
218230130Smav
219230130Smav	if (devinfo == NULL || devinfo->ctl == NULL)
220230130Smav		return (NULL);
221230130Smav
222230130Smav	i = 0;
223230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
224230130Smav		if (ctl->enable == 0)
225230130Smav			continue;
226230130Smav		if (ctl->widget->nid != nid)
227230130Smav			continue;
228230130Smav		if (dir && ctl->ndir != dir)
229230130Smav			continue;
230230130Smav		if (index >= 0 && ctl->ndir == HDAA_CTL_IN &&
231230130Smav		    ctl->dir == ctl->ndir && ctl->index != index)
232230130Smav			continue;
233230130Smav		found++;
234230130Smav		if (found == cnt || cnt <= 0)
235230130Smav			return (ctl);
236230130Smav	}
237230130Smav
238230130Smav	return (NULL);
239230130Smav}
240230130Smav
241230130Smav/*
242230551Smav * Headphones redirection change handler.
243230130Smav */
244230130Smavstatic void
245230551Smavhdaa_hpredir_handler(struct hdaa_widget *w)
246230130Smav{
247230312Smav	struct hdaa_devinfo *devinfo = w->devinfo;
248230551Smav	struct hdaa_audio_as *as = &devinfo->as[w->bindas];
249230312Smav	struct hdaa_widget *w1;
250230130Smav	struct hdaa_audio_ctl *ctl;
251230551Smav	uint32_t val;
252230551Smav	int j, connected = w->wclass.pin.connected;
253230130Smav
254230130Smav	HDA_BOOTVERBOSE(
255230551Smav		device_printf((as->pdevinfo && as->pdevinfo->dev) ?
256230551Smav		    as->pdevinfo->dev : devinfo->dev,
257230551Smav		    "Redirect output to: %s\n",
258230551Smav		    connected ? "headphones": "main");
259230130Smav	);
260230130Smav	/* (Un)Mute headphone pin. */
261230130Smav	ctl = hdaa_audio_ctl_amp_get(devinfo,
262230312Smav	    w->nid, HDAA_CTL_IN, -1, 1);
263230130Smav	if (ctl != NULL && ctl->mute) {
264230130Smav		/* If pin has muter - use it. */
265230551Smav		val = connected ? 0 : 1;
266230130Smav		if (val != ctl->forcemute) {
267230130Smav			ctl->forcemute = val;
268230130Smav			hdaa_audio_ctl_amp_set(ctl,
269230130Smav			    HDAA_AMP_MUTE_DEFAULT,
270230130Smav			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
271230130Smav		}
272230130Smav	} else {
273230130Smav		/* If there is no muter - disable pin output. */
274230551Smav		if (connected)
275230312Smav			val = w->wclass.pin.ctrl |
276230312Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
277230312Smav		else
278230312Smav			val = w->wclass.pin.ctrl &
279230312Smav			    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
280230312Smav		if (val != w->wclass.pin.ctrl) {
281230312Smav			w->wclass.pin.ctrl = val;
282230312Smav			hda_command(devinfo->dev,
283230312Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
284230312Smav			    w->nid, w->wclass.pin.ctrl));
285230130Smav		}
286230130Smav	}
287230130Smav	/* (Un)Mute other pins. */
288230130Smav	for (j = 0; j < 15; j++) {
289230130Smav		if (as->pins[j] <= 0)
290230130Smav			continue;
291230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo,
292230130Smav		    as->pins[j], HDAA_CTL_IN, -1, 1);
293230130Smav		if (ctl != NULL && ctl->mute) {
294230130Smav			/* If pin has muter - use it. */
295230551Smav			val = connected ? 1 : 0;
296230130Smav			if (val == ctl->forcemute)
297230130Smav				continue;
298230130Smav			ctl->forcemute = val;
299230130Smav			hdaa_audio_ctl_amp_set(ctl,
300230130Smav			    HDAA_AMP_MUTE_DEFAULT,
301230130Smav			    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
302230130Smav			continue;
303230130Smav		}
304230130Smav		/* If there is no muter - disable pin output. */
305230312Smav		w1 = hdaa_widget_get(devinfo, as->pins[j]);
306230551Smav		if (w1 != NULL) {
307230551Smav			if (connected)
308230312Smav				val = w1->wclass.pin.ctrl &
309230130Smav				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
310230130Smav			else
311230312Smav				val = w1->wclass.pin.ctrl |
312230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
313230312Smav			if (val != w1->wclass.pin.ctrl) {
314230312Smav				w1->wclass.pin.ctrl = val;
315230130Smav				hda_command(devinfo->dev,
316230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL(0,
317230312Smav				    w1->nid, w1->wclass.pin.ctrl));
318230130Smav			}
319230130Smav		}
320230130Smav	}
321230130Smav}
322230130Smav
323230130Smav/*
324230551Smav * Recording source change handler.
325230130Smav */
326230130Smavstatic void
327230551Smavhdaa_autorecsrc_handler(struct hdaa_audio_as *as, struct hdaa_widget *w)
328230130Smav{
329230551Smav	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
330230551Smav	struct hdaa_devinfo *devinfo;
331230551Smav	struct hdaa_widget *w1;
332230551Smav	int i, mask, fullmask, prio, bestprio;
333230551Smav	char buf[128];
334230130Smav
335230551Smav	if (!as->mixed || pdevinfo == NULL || pdevinfo->mixer == NULL)
336230130Smav		return;
337230551Smav	/* Don't touch anything if we asked not to. */
338230551Smav	if (pdevinfo->autorecsrc == 0 ||
339230551Smav	    (pdevinfo->autorecsrc == 1 && w != NULL))
340230551Smav		return;
341230551Smav	/* Don't touch anything if "mix" or "speaker" selected. */
342230551Smav	if (pdevinfo->recsrc & (SOUND_MASK_IMIX | SOUND_MASK_SPEAKER))
343230551Smav		return;
344230551Smav	/* Don't touch anything if several selected. */
345230551Smav	if (ffs(pdevinfo->recsrc) != fls(pdevinfo->recsrc))
346230551Smav		return;
347230551Smav	devinfo = pdevinfo->devinfo;
348230551Smav	mask = fullmask = 0;
349230551Smav	bestprio = 0;
350230551Smav	for (i = 0; i < 16; i++) {
351230551Smav		if (as->pins[i] <= 0)
352230130Smav			continue;
353230551Smav		w1 = hdaa_widget_get(devinfo, as->pins[i]);
354230551Smav		if (w1 == NULL || w1->enable == 0)
355230312Smav			continue;
356230551Smav		if (w1->wclass.pin.connected == 0)
357230551Smav			continue;
358230551Smav		prio = (w1->wclass.pin.connected == 1) ? 2 : 1;
359230551Smav		if (prio < bestprio)
360230551Smav			continue;
361230551Smav		if (prio > bestprio) {
362230551Smav			mask = 0;
363230551Smav			bestprio = prio;
364230551Smav		}
365230551Smav		mask |= (1 << w1->ossdev);
366230551Smav		fullmask |= (1 << w1->ossdev);
367230130Smav	}
368230551Smav	if (mask == 0)
369230551Smav		return;
370230551Smav	/* Prefer newly connected input. */
371230551Smav	if (w != NULL && (mask & (1 << w->ossdev)))
372230551Smav		mask = (1 << w->ossdev);
373230551Smav	/* Prefer previously selected input */
374230551Smav	if (mask & pdevinfo->recsrc)
375230551Smav		mask &= pdevinfo->recsrc;
376230551Smav	/* Prefer mic. */
377230551Smav	if (mask & SOUND_MASK_MIC)
378230551Smav		mask = SOUND_MASK_MIC;
379230551Smav	/* Prefer monitor (2nd mic). */
380230551Smav	if (mask & SOUND_MASK_MONITOR)
381230551Smav		mask = SOUND_MASK_MONITOR;
382230551Smav	/* Just take first one. */
383230551Smav	mask = (1 << (ffs(mask) - 1));
384230551Smav	HDA_BOOTVERBOSE(
385230551Smav		hdaa_audio_ctl_ossmixer_mask2allname(mask, buf, sizeof(buf));
386230551Smav		device_printf(pdevinfo->dev,
387230551Smav		    "Automatically set rec source to: %s\n", buf);
388230551Smav	);
389230130Smav	hdaa_unlock(devinfo);
390230551Smav	mix_setrecsrc(pdevinfo->mixer, mask);
391230551Smav	hdaa_lock(devinfo);
392230130Smav}
393230130Smav
394230130Smav/*
395230551Smav * Jack presence detection event handler.
396230130Smav */
397230130Smavstatic void
398230551Smavhdaa_presence_handler(struct hdaa_widget *w)
399230130Smav{
400230551Smav	struct hdaa_devinfo *devinfo = w->devinfo;
401230551Smav	struct hdaa_audio_as *as;
402230551Smav	uint32_t res;
403230551Smav	int connected;
404230130Smav
405230551Smav	if (w->enable == 0 || w->type !=
406230551Smav	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
407230551Smav		return;
408230130Smav
409230551Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
410230551Smav	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
411230551Smav		return;
412230551Smav
413230551Smav	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
414230551Smav	connected = (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) != 0;
415230551Smav	if (devinfo->quirks & HDAA_QUIRK_SENSEINV)
416230551Smav		connected = !connected;
417230551Smav	if (connected == w->wclass.pin.connected)
418230551Smav		return;
419230551Smav	w->wclass.pin.connected = connected;
420230551Smav	HDA_BOOTVERBOSE(
421230551Smav		device_printf(devinfo->dev,
422230551Smav		    "Pin sense: nid=%d sence=0x%08x (%sconnected)\n",
423230551Smav		    w->nid, res, !w->wclass.pin.connected ? "dis" : "");
424230551Smav	);
425230551Smav
426230551Smav	as = &devinfo->as[w->bindas];
427230551Smav	if (as->hpredir >= 0 && as->pins[15] == w->nid)
428230551Smav		hdaa_hpredir_handler(w);
429230551Smav	if (as->dir == HDAA_CTL_IN)
430230551Smav		hdaa_autorecsrc_handler(as, w);
431230130Smav}
432230130Smav
433230551Smav/*
434230551Smav * Callback for poll based presence detection.
435230551Smav */
436230130Smavstatic void
437230551Smavhdaa_jack_poll_callback(void *arg)
438230130Smav{
439230551Smav	struct hdaa_devinfo *devinfo = arg;
440230551Smav	struct hdaa_widget *w;
441230551Smav	int i;
442230130Smav
443230551Smav	hdaa_lock(devinfo);
444230551Smav	if (devinfo->poll_ival == 0) {
445230551Smav		hdaa_unlock(devinfo);
446230551Smav		return;
447230551Smav	}
448230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
449230551Smav		if (devinfo->as[i].hpredir < 0)
450230551Smav			continue;
451230551Smav		w = hdaa_widget_get(devinfo, devinfo->as[i].pins[15]);
452230130Smav		if (w == NULL || w->enable == 0 || w->type !=
453230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
454230130Smav			continue;
455230551Smav		hdaa_presence_handler(w);
456230130Smav	}
457230551Smav	callout_reset(&devinfo->poll_jack, devinfo->poll_ival,
458230551Smav	    hdaa_jack_poll_callback, devinfo);
459230551Smav	hdaa_unlock(devinfo);
460230130Smav}
461230130Smav
462230312Smavstatic void
463230312Smavhdaa_eld_dump(struct hdaa_widget *w)
464230312Smav{
465230312Smav	struct hdaa_devinfo *devinfo = w->devinfo;
466230312Smav	device_t dev = devinfo->dev;
467230312Smav	uint8_t *sad;
468230312Smav	int len, mnl, i, sadc, fmt;
469230312Smav
470230312Smav	if (w->eld == NULL || w->eld_len < 4)
471230312Smav		return;
472230312Smav	device_printf(dev,
473230312Smav	    "ELD nid=%d: ELD_Ver=%u Baseline_ELD_Len=%u\n",
474230312Smav	    w->nid, w->eld[0] >> 3, w->eld[2]);
475230312Smav	if ((w->eld[0] >> 3) != 0x02)
476230312Smav		return;
477230312Smav	len = min(w->eld_len, (u_int)w->eld[2] * 4);
478230312Smav	mnl = w->eld[4] & 0x1f;
479230312Smav	device_printf(dev,
480230312Smav	    "ELD nid=%d: CEA_EDID_Ver=%u MNL=%u\n",
481230312Smav	    w->nid, w->eld[4] >> 5, mnl);
482230312Smav	sadc = w->eld[5] >> 4;
483230312Smav	device_printf(dev,
484230312Smav	    "ELD nid=%d: SAD_Count=%u Conn_Type=%u S_AI=%u HDCP=%u\n",
485230312Smav	    w->nid, sadc, (w->eld[5] >> 2) & 0x3,
486230312Smav	    (w->eld[5] >> 1) & 0x1, w->eld[5] & 0x1);
487230312Smav	device_printf(dev,
488230312Smav	    "ELD nid=%d: Aud_Synch_Delay=%ums\n",
489230312Smav	    w->nid, w->eld[6] * 2);
490230312Smav	device_printf(dev,
491230312Smav	    "ELD nid=%d: Channels=0x%b\n",
492230312Smav	    w->nid, w->eld[7],
493230312Smav	    "\020\07RLRC\06FLRC\05RC\04RLR\03FC\02LFE\01FLR");
494230312Smav	device_printf(dev,
495230312Smav	    "ELD nid=%d: Port_ID=0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
496230312Smav	    w->nid, w->eld[8], w->eld[9], w->eld[10], w->eld[11],
497230312Smav	    w->eld[12], w->eld[13], w->eld[14], w->eld[15]);
498230312Smav	device_printf(dev,
499230312Smav	    "ELD nid=%d: Manufacturer_Name=0x%02x%02x\n",
500230312Smav	    w->nid, w->eld[16], w->eld[17]);
501230312Smav	device_printf(dev,
502230312Smav	    "ELD nid=%d: Product_Code=0x%02x%02x\n",
503230312Smav	    w->nid, w->eld[18], w->eld[19]);
504230312Smav	device_printf(dev,
505230312Smav	    "ELD nid=%d: Monitor_Name_String='%.*s'\n",
506230312Smav	    w->nid, mnl, &w->eld[20]);
507230312Smav	for (i = 0; i < sadc; i++) {
508230312Smav		sad = &w->eld[20 + mnl + i * 3];
509230312Smav		fmt = (sad[0] >> 3) & 0x0f;
510230312Smav		if (fmt == HDA_HDMI_CODING_TYPE_REF_CTX) {
511230312Smav			fmt = (sad[2] >> 3) & 0x1f;
512230312Smav			if (fmt < 1 || fmt > 3)
513230312Smav				fmt = 0;
514230312Smav			else
515230312Smav				fmt += 14;
516230312Smav		}
517230312Smav		device_printf(dev,
518230312Smav		    "ELD nid=%d: %s %dch freqs=0x%b",
519230312Smav		    w->nid, HDA_HDMI_CODING_TYPES[fmt], (sad[0] & 0x07) + 1,
520230312Smav		    sad[1], "\020\007192\006176\00596\00488\00348\00244\00132");
521230312Smav		switch (fmt) {
522230312Smav		case HDA_HDMI_CODING_TYPE_LPCM:
523230312Smav			printf(" sizes=0x%b",
524230312Smav			    sad[2] & 0x07, "\020\00324\00220\00116");
525230312Smav			break;
526230312Smav		case HDA_HDMI_CODING_TYPE_AC3:
527230312Smav		case HDA_HDMI_CODING_TYPE_MPEG1:
528230312Smav		case HDA_HDMI_CODING_TYPE_MP3:
529230312Smav		case HDA_HDMI_CODING_TYPE_MPEG2:
530230312Smav		case HDA_HDMI_CODING_TYPE_AACLC:
531230312Smav		case HDA_HDMI_CODING_TYPE_DTS:
532230312Smav		case HDA_HDMI_CODING_TYPE_ATRAC:
533230312Smav			printf(" max_bitrate=%d", sad[2] * 8000);
534230312Smav			break;
535230312Smav		case HDA_HDMI_CODING_TYPE_WMAPRO:
536230312Smav			printf(" profile=%d", sad[2] & 0x07);
537230312Smav			break;
538230312Smav		}
539230312Smav		printf("\n");
540230312Smav	}
541230312Smav}
542230312Smav
543230312Smavstatic void
544230312Smavhdaa_eld_handler(struct hdaa_widget *w)
545230312Smav{
546230312Smav	struct hdaa_devinfo *devinfo = w->devinfo;
547230312Smav	uint32_t res;
548230312Smav	int i;
549230312Smav
550230312Smav	if (w->enable == 0 || w->type !=
551230312Smav	    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
552230312Smav		return;
553230312Smav
554230551Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
555230551Smav	    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
556230551Smav		return;
557230551Smav
558230551Smav	res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
559230551Smav	if ((w->eld != 0) == ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) != 0))
560230551Smav		return;
561230312Smav	if (w->eld != NULL) {
562230312Smav		w->eld_len = 0;
563230312Smav		free(w->eld, M_HDAA);
564230312Smav		w->eld = NULL;
565230312Smav	}
566230312Smav	HDA_BOOTVERBOSE(
567230312Smav		device_printf(devinfo->dev,
568230312Smav		    "Pin sense: nid=%d sence=0x%08x "
569230312Smav		    "(%sconnected, ELD %svalid)\n",
570230312Smav		    w->nid, res,
571230312Smav		    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ? "" : "dis",
572230312Smav		    (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) ? "" : "in");
573230312Smav	);
574230312Smav	if ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) == 0)
575230312Smav		return;
576230312Smav
577230312Smav	res = hda_command(devinfo->dev,
578230312Smav	    HDA_CMD_GET_HDMI_DIP_SIZE(0, w->nid, 0x08));
579230312Smav	if (res == HDA_INVALID)
580230312Smav		return;
581230312Smav	w->eld_len = res & 0xff;
582230312Smav	if (w->eld_len != 0)
583230312Smav		w->eld = malloc(w->eld_len, M_HDAA, M_ZERO | M_NOWAIT);
584230312Smav	if (w->eld == NULL) {
585230312Smav		w->eld_len = 0;
586230312Smav		return;
587230312Smav	}
588230312Smav
589230312Smav	for (i = 0; i < w->eld_len; i++) {
590230312Smav		res = hda_command(devinfo->dev,
591230312Smav		    HDA_CMD_GET_HDMI_ELDD(0, w->nid, i));
592230312Smav		if (res & 0x80000000)
593230312Smav			w->eld[i] = res & 0xff;
594230312Smav	}
595230312Smav	HDA_BOOTVERBOSE(
596230312Smav		hdaa_eld_dump(w);
597230312Smav	);
598230312Smav}
599230312Smav
600230551Smav/*
601230551Smav * Pin sense initializer.
602230551Smav */
603230312Smavstatic void
604230551Smavhdaa_sense_init(struct hdaa_devinfo *devinfo)
605230312Smav{
606230551Smav	struct hdaa_audio_as *as;
607230551Smav	struct hdaa_widget *w;
608230551Smav	int i, poll = 0;
609230312Smav
610230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
611230312Smav		w = hdaa_widget_get(devinfo, i);
612230551Smav		if (w == NULL || w->enable == 0 || w->type !=
613230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
614230312Smav			continue;
615230312Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap) &&
616230312Smav		    w->unsol < 0) {
617230312Smav			w->unsol = HDAC_UNSOL_ALLOC(
618230551Smav			    device_get_parent(devinfo->dev), devinfo->dev, w->nid);
619230312Smav			hda_command(devinfo->dev,
620230312Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid,
621230551Smav			    HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE | w->unsol));
622230312Smav		}
623230551Smav		as = &devinfo->as[w->bindas];
624230551Smav		if (as->hpredir >= 0 && as->pins[15] == w->nid) {
625230551Smav			if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
626230551Smav			    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0) {
627230551Smav				device_printf(devinfo->dev,
628230551Smav				    "No presence detection support at nid %d\n",
629230551Smav				    as[i].pins[15]);
630230551Smav			} else {
631230551Smav				if (w->unsol < 0)
632230551Smav					poll = 1;
633230551Smav				HDA_BOOTVERBOSE(
634230551Smav					device_printf(devinfo->dev,
635230551Smav					    "Headphones redirection for "
636230551Smav					    "association %d nid=%d using %s.\n",
637230551Smav					    w->bindas, w->nid,
638230551Smav					    (poll != 0) ? "polling" :
639230551Smav					    "unsolicited responses");
640230551Smav				);
641230551Smav			};
642230551Smav		}
643230551Smav		hdaa_presence_handler(w);
644230551Smav		if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) &&
645230551Smav		    !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
646230551Smav			continue;
647230312Smav		hdaa_eld_handler(w);
648230312Smav	}
649230551Smav	if (poll) {
650230551Smav		callout_reset(&devinfo->poll_jack, 1,
651230551Smav		    hdaa_jack_poll_callback, devinfo);
652230551Smav	}
653230312Smav}
654230312Smav
655230312Smavstatic void
656230551Smavhdaa_sense_deinit(struct hdaa_devinfo *devinfo)
657230312Smav{
658230312Smav	struct hdaa_widget *w;
659230312Smav	int i;
660230312Smav
661230551Smav	callout_stop(&devinfo->poll_jack);
662230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
663230312Smav		w = hdaa_widget_get(devinfo, i);
664230551Smav		if (w == NULL || w->enable == 0 || w->type !=
665230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
666230312Smav			continue;
667230312Smav		if (w->unsol < 0)
668230312Smav			continue;
669230312Smav		hda_command(devinfo->dev,
670230312Smav		    HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0));
671230312Smav		HDAC_UNSOL_FREE(
672230312Smav		    device_get_parent(devinfo->dev), devinfo->dev,
673230312Smav		    w->unsol);
674230312Smav		w->unsol = -1;
675230312Smav	}
676230312Smav}
677230312Smav
678230130Smavuint32_t
679230130Smavhdaa_widget_pin_patch(uint32_t config, const char *str)
680230130Smav{
681230130Smav	char buf[256];
682230130Smav	char *key, *value, *rest, *bad;
683230130Smav	int ival, i;
684230130Smav
685230130Smav	strlcpy(buf, str, sizeof(buf));
686230130Smav	rest = buf;
687230130Smav	while ((key = strsep(&rest, "=")) != NULL) {
688230130Smav		value = strsep(&rest, " \t");
689230130Smav		if (value == NULL)
690230130Smav			break;
691230130Smav		ival = strtol(value, &bad, 10);
692230130Smav		if (strcmp(key, "seq") == 0) {
693230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK;
694230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_SEQUENCE_SHIFT) &
695230130Smav			    HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK);
696230130Smav		} else if (strcmp(key, "as") == 0) {
697230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK;
698230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT) &
699230130Smav			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK);
700230130Smav		} else if (strcmp(key, "misc") == 0) {
701230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_MISC_MASK;
702230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_MISC_SHIFT) &
703230130Smav			    HDA_CONFIG_DEFAULTCONF_MISC_MASK);
704230130Smav		} else if (strcmp(key, "color") == 0) {
705230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_COLOR_MASK;
706230130Smav			if (bad[0] == 0) {
707230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT) &
708230130Smav				    HDA_CONFIG_DEFAULTCONF_COLOR_MASK);
709230130Smav			};
710230130Smav			for (i = 0; i < 16; i++) {
711230130Smav				if (strcasecmp(HDA_COLORS[i], value) == 0) {
712230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT);
713230130Smav					break;
714230130Smav				}
715230130Smav			}
716230130Smav		} else if (strcmp(key, "ctype") == 0) {
717230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK;
718230130Smav			if (bad[0] == 0) {
719230130Smav			config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT) &
720230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK);
721230130Smav			}
722230130Smav			for (i = 0; i < 16; i++) {
723230130Smav				if (strcasecmp(HDA_CONNECTORS[i], value) == 0) {
724230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT);
725230130Smav					break;
726230130Smav				}
727230130Smav			}
728230130Smav		} else if (strcmp(key, "device") == 0) {
729230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
730230130Smav			if (bad[0] == 0) {
731230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT) &
732230130Smav				    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK);
733230130Smav				continue;
734230130Smav			};
735230130Smav			for (i = 0; i < 16; i++) {
736230130Smav				if (strcasecmp(HDA_DEVS[i], value) == 0) {
737230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT);
738230130Smav					break;
739230130Smav				}
740230130Smav			}
741230130Smav		} else if (strcmp(key, "loc") == 0) {
742230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_LOCATION_MASK;
743230130Smav			if (bad[0] == 0) {
744230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT) &
745230130Smav				    HDA_CONFIG_DEFAULTCONF_LOCATION_MASK);
746230130Smav				continue;
747230130Smav			}
748230130Smav			for (i = 0; i < 64; i++) {
749230130Smav				if (strcasecmp(HDA_LOCS[i], value) == 0) {
750230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT);
751230130Smav					break;
752230130Smav				}
753230130Smav			}
754230130Smav		} else if (strcmp(key, "conn") == 0) {
755230130Smav			config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
756230130Smav			if (bad[0] == 0) {
757230130Smav				config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT) &
758230130Smav				    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
759230130Smav				continue;
760230130Smav			};
761230130Smav			for (i = 0; i < 4; i++) {
762230130Smav				if (strcasecmp(HDA_CONNS[i], value) == 0) {
763230130Smav					config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT);
764230130Smav					break;
765230130Smav				}
766230130Smav			}
767230130Smav		}
768230130Smav	}
769230130Smav	return (config);
770230130Smav}
771230130Smav
772230130Smavuint32_t
773230130Smavhdaa_gpio_patch(uint32_t gpio, const char *str)
774230130Smav{
775230130Smav	char buf[256];
776230130Smav	char *key, *value, *rest;
777230130Smav	int ikey, i;
778230130Smav
779230130Smav	strlcpy(buf, str, sizeof(buf));
780230130Smav	rest = buf;
781230130Smav	while ((key = strsep(&rest, "=")) != NULL) {
782230130Smav		value = strsep(&rest, " \t");
783230130Smav		if (value == NULL)
784230130Smav			break;
785230130Smav		ikey = strtol(key, NULL, 10);
786230130Smav		if (ikey < 0 || ikey > 7)
787230130Smav			continue;
788230130Smav		for (i = 0; i < 7; i++) {
789230130Smav			if (strcasecmp(HDA_GPIO_ACTIONS[i], value) == 0) {
790230130Smav				gpio &= ~HDAA_GPIO_MASK(ikey);
791230130Smav				gpio |= i << HDAA_GPIO_SHIFT(ikey);
792230130Smav				break;
793230130Smav			}
794230130Smav		}
795230130Smav	}
796230130Smav	return (gpio);
797230130Smav}
798230130Smav
799230130Smavstatic void
800230130Smavhdaa_local_patch_pin(struct hdaa_widget *w)
801230130Smav{
802230130Smav	device_t dev = w->devinfo->dev;
803230130Smav	const char *res = NULL;
804230130Smav	uint32_t config, orig;
805230130Smav	char buf[32];
806230130Smav
807230130Smav	config = orig = w->wclass.pin.config;
808230130Smav	snprintf(buf, sizeof(buf), "cad%u.nid%u.config",
809230130Smav	    hda_get_codec_id(dev), w->nid);
810230130Smav	if (resource_string_value(device_get_name(
811230130Smav	    device_get_parent(device_get_parent(dev))),
812230130Smav	    device_get_unit(device_get_parent(device_get_parent(dev))),
813230130Smav	    buf, &res) == 0) {
814230130Smav		if (strncmp(res, "0x", 2) == 0) {
815230130Smav			config = strtol(res + 2, NULL, 16);
816230130Smav		} else {
817230130Smav			config = hdaa_widget_pin_patch(config, res);
818230130Smav		}
819230130Smav	}
820230130Smav	snprintf(buf, sizeof(buf), "nid%u.config", w->nid);
821230130Smav	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
822230130Smav	    buf, &res) == 0) {
823230130Smav		if (strncmp(res, "0x", 2) == 0) {
824230130Smav			config = strtol(res + 2, NULL, 16);
825230130Smav		} else {
826230130Smav			config = hdaa_widget_pin_patch(config, res);
827230130Smav		}
828230130Smav	}
829230130Smav	HDA_BOOTVERBOSE(
830230130Smav		if (config != orig)
831230130Smav			device_printf(w->devinfo->dev,
832230130Smav			    "Patching pin config nid=%u 0x%08x -> 0x%08x\n",
833230130Smav			    w->nid, orig, config);
834230130Smav	);
835230130Smav	w->wclass.pin.newconf = w->wclass.pin.config = config;
836230130Smav}
837230130Smav
838230130Smavstatic int
839230130Smavhdaa_sysctl_config(SYSCTL_HANDLER_ARGS)
840230130Smav{
841230130Smav	char buf[256];
842230130Smav	int error;
843230130Smav	uint32_t conf;
844230130Smav
845230130Smav	conf = *(uint32_t *)oidp->oid_arg1;
846230130Smav	snprintf(buf, sizeof(buf), "0x%08x as=%d seq=%d "
847230130Smav	    "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d",
848230130Smav	    conf,
849230130Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
850230130Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
851230130Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
852230130Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
853230130Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
854230130Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
855230130Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
856230130Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf));
857230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
858230130Smav	if (error != 0 || req->newptr == NULL)
859230130Smav		return (error);
860230130Smav	if (strncmp(buf, "0x", 2) == 0)
861230130Smav		conf = strtol(buf + 2, NULL, 16);
862230130Smav	else
863230130Smav		conf = hdaa_widget_pin_patch(conf, buf);
864230130Smav	*(uint32_t *)oidp->oid_arg1 = conf;
865230130Smav	return (0);
866230130Smav}
867230130Smav
868230130Smavstatic void
869230130Smavhdaa_config_fetch(const char *str, uint32_t *on, uint32_t *off)
870230130Smav{
871230130Smav	int i = 0, j, k, len, inv;
872230130Smav
873230130Smav	for (;;) {
874230130Smav		while (str[i] != '\0' &&
875230130Smav		    (str[i] == ',' || isspace(str[i]) != 0))
876230130Smav			i++;
877230130Smav		if (str[i] == '\0')
878230130Smav			return;
879230130Smav		j = i;
880230130Smav		while (str[j] != '\0' &&
881230130Smav		    !(str[j] == ',' || isspace(str[j]) != 0))
882230130Smav			j++;
883230130Smav		len = j - i;
884230130Smav		if (len > 2 && strncmp(str + i, "no", 2) == 0)
885230130Smav			inv = 2;
886230130Smav		else
887230130Smav			inv = 0;
888230130Smav		for (k = 0; len > inv && k < HDAA_QUIRKS_TAB_LEN; k++) {
889230130Smav			if (strncmp(str + i + inv,
890230130Smav			    hdaa_quirks_tab[k].key, len - inv) != 0)
891230130Smav				continue;
892230130Smav			if (len - inv != strlen(hdaa_quirks_tab[k].key))
893230130Smav				continue;
894230130Smav			if (inv == 0) {
895230130Smav				*on |= hdaa_quirks_tab[k].value;
896230130Smav				*off &= ~hdaa_quirks_tab[k].value;
897230130Smav			} else {
898230130Smav				*off |= hdaa_quirks_tab[k].value;
899230130Smav				*on &= ~hdaa_quirks_tab[k].value;
900230130Smav			}
901230130Smav			break;
902230130Smav		}
903230130Smav		i = j;
904230130Smav	}
905230130Smav}
906230130Smav
907230130Smavstatic int
908230130Smavhdaa_sysctl_quirks(SYSCTL_HANDLER_ARGS)
909230130Smav{
910230130Smav	char buf[256];
911230130Smav	int error, n = 0, i;
912230130Smav	uint32_t quirks, quirks_off;
913230130Smav
914230130Smav	quirks = *(uint32_t *)oidp->oid_arg1;
915230130Smav	buf[0] = 0;
916230130Smav	for (i = 0; i < HDAA_QUIRKS_TAB_LEN; i++) {
917230130Smav		if ((quirks & hdaa_quirks_tab[i].value) != 0)
918230130Smav			n += snprintf(buf + n, sizeof(buf) - n, "%s%s",
919230130Smav			    n != 0 ? "," : "", hdaa_quirks_tab[i].key);
920230130Smav	}
921230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
922230130Smav	if (error != 0 || req->newptr == NULL)
923230130Smav		return (error);
924230130Smav	if (strncmp(buf, "0x", 2) == 0)
925230130Smav		quirks = strtol(buf + 2, NULL, 16);
926230130Smav	else {
927230130Smav		quirks = 0;
928230130Smav		hdaa_config_fetch(buf, &quirks, &quirks_off);
929230130Smav	}
930230130Smav	*(uint32_t *)oidp->oid_arg1 = quirks;
931230130Smav	return (0);
932230130Smav}
933230130Smav
934230130Smavstatic void
935230130Smavhdaa_local_patch(struct hdaa_devinfo *devinfo)
936230130Smav{
937230130Smav	struct hdaa_widget *w;
938230130Smav	const char *res = NULL;
939230130Smav	uint32_t quirks_on = 0, quirks_off = 0, x;
940230130Smav	int i;
941230130Smav
942230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
943230130Smav		w = hdaa_widget_get(devinfo, i);
944230130Smav		if (w == NULL)
945230130Smav			continue;
946230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
947230130Smav			hdaa_local_patch_pin(w);
948230130Smav	}
949230130Smav
950230130Smav	if (resource_string_value(device_get_name(devinfo->dev),
951230130Smav	    device_get_unit(devinfo->dev), "config", &res) == 0) {
952230130Smav		if (res != NULL && strlen(res) > 0)
953230130Smav			hdaa_config_fetch(res, &quirks_on, &quirks_off);
954230130Smav		devinfo->quirks |= quirks_on;
955230130Smav		devinfo->quirks &= ~quirks_off;
956230130Smav	}
957230130Smav	if (devinfo->newquirks == -1)
958230130Smav		devinfo->newquirks = devinfo->quirks;
959230130Smav	else
960230130Smav		devinfo->quirks = devinfo->newquirks;
961230130Smav	HDA_BOOTHVERBOSE(
962230130Smav		device_printf(devinfo->dev,
963230130Smav		    "Config options: 0x%08x\n", devinfo->quirks);
964230130Smav	);
965230130Smav
966230130Smav	if (resource_string_value(device_get_name(devinfo->dev),
967230130Smav	    device_get_unit(devinfo->dev), "gpio_config", &res) == 0) {
968230130Smav		if (strncmp(res, "0x", 2) == 0) {
969230130Smav			devinfo->gpio = strtol(res + 2, NULL, 16);
970230130Smav		} else {
971230130Smav			devinfo->gpio = hdaa_gpio_patch(devinfo->gpio, res);
972230130Smav		}
973230130Smav	}
974230130Smav	if (devinfo->newgpio == -1)
975230130Smav		devinfo->newgpio = devinfo->gpio;
976230130Smav	else
977230130Smav		devinfo->gpio = devinfo->newgpio;
978230130Smav	if (devinfo->newgpo == -1)
979230130Smav		devinfo->newgpo = devinfo->gpo;
980230130Smav	else
981230130Smav		devinfo->gpo = devinfo->newgpo;
982230130Smav	HDA_BOOTHVERBOSE(
983230130Smav		device_printf(devinfo->dev, "GPIO config options:");
984230130Smav		for (i = 0; i < 7; i++) {
985230130Smav			x = (devinfo->gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
986230130Smav			if (x != 0)
987230130Smav				printf(" %d=%s", i, HDA_GPIO_ACTIONS[x]);
988230130Smav		}
989230130Smav		printf("\n");
990230130Smav	);
991230130Smav}
992230130Smav
993230130Smavstatic void
994230130Smavhdaa_widget_connection_parse(struct hdaa_widget *w)
995230130Smav{
996230130Smav	uint32_t res;
997230130Smav	int i, j, max, ents, entnum;
998230130Smav	nid_t nid = w->nid;
999230130Smav	nid_t cnid, addcnid, prevcnid;
1000230130Smav
1001230130Smav	w->nconns = 0;
1002230130Smav
1003230130Smav	res = hda_command(w->devinfo->dev,
1004230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_CONN_LIST_LENGTH));
1005230130Smav
1006230130Smav	ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res);
1007230130Smav
1008230130Smav	if (ents < 1)
1009230130Smav		return;
1010230130Smav
1011230130Smav	entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4;
1012230130Smav	max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1;
1013230130Smav	prevcnid = 0;
1014230130Smav
1015230130Smav#define CONN_RMASK(e)		(1 << ((32 / (e)) - 1))
1016230130Smav#define CONN_NMASK(e)		(CONN_RMASK(e) - 1)
1017230130Smav#define CONN_RESVAL(r, e, n)	((r) >> ((32 / (e)) * (n)))
1018230130Smav#define CONN_RANGE(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_RMASK(e))
1019230130Smav#define CONN_CNID(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_NMASK(e))
1020230130Smav
1021230130Smav	for (i = 0; i < ents; i += entnum) {
1022230130Smav		res = hda_command(w->devinfo->dev,
1023230130Smav		    HDA_CMD_GET_CONN_LIST_ENTRY(0, nid, i));
1024230130Smav		for (j = 0; j < entnum; j++) {
1025230130Smav			cnid = CONN_CNID(res, entnum, j);
1026230130Smav			if (cnid == 0) {
1027230130Smav				if (w->nconns < ents)
1028230130Smav					device_printf(w->devinfo->dev,
1029230130Smav					    "WARNING: nid=%d has zero cnid "
1030230130Smav					    "entnum=%d j=%d index=%d "
1031230130Smav					    "entries=%d found=%d res=0x%08x\n",
1032230130Smav					    nid, entnum, j, i,
1033230130Smav					    ents, w->nconns, res);
1034230130Smav				else
1035230130Smav					goto getconns_out;
1036230130Smav			}
1037230130Smav			if (cnid < w->devinfo->startnode ||
1038230130Smav			    cnid >= w->devinfo->endnode) {
1039230130Smav				HDA_BOOTVERBOSE(
1040230130Smav					device_printf(w->devinfo->dev,
1041230130Smav					    "WARNING: nid=%d has cnid outside "
1042230130Smav					    "of the AFG range j=%d "
1043230130Smav					    "entnum=%d index=%d res=0x%08x\n",
1044230130Smav					    nid, j, entnum, i, res);
1045230130Smav				);
1046230130Smav			}
1047230130Smav			if (CONN_RANGE(res, entnum, j) == 0)
1048230130Smav				addcnid = cnid;
1049230130Smav			else if (prevcnid == 0 || prevcnid >= cnid) {
1050230130Smav				device_printf(w->devinfo->dev,
1051230130Smav				    "WARNING: Invalid child range "
1052230130Smav				    "nid=%d index=%d j=%d entnum=%d "
1053230130Smav				    "prevcnid=%d cnid=%d res=0x%08x\n",
1054230130Smav				    nid, i, j, entnum, prevcnid,
1055230130Smav				    cnid, res);
1056230130Smav				addcnid = cnid;
1057230130Smav			} else
1058230130Smav				addcnid = prevcnid + 1;
1059230130Smav			while (addcnid <= cnid) {
1060230130Smav				if (w->nconns > max) {
1061230130Smav					device_printf(w->devinfo->dev,
1062230130Smav					    "Adding %d (nid=%d): "
1063230130Smav					    "Max connection reached! max=%d\n",
1064230130Smav					    addcnid, nid, max + 1);
1065230130Smav					goto getconns_out;
1066230130Smav				}
1067230130Smav				w->connsenable[w->nconns] = 1;
1068230130Smav				w->conns[w->nconns++] = addcnid++;
1069230130Smav			}
1070230130Smav			prevcnid = cnid;
1071230130Smav		}
1072230130Smav	}
1073230130Smav
1074230130Smavgetconns_out:
1075230130Smav	return;
1076230130Smav}
1077230130Smav
1078230130Smavstatic void
1079230130Smavhdaa_widget_parse(struct hdaa_widget *w)
1080230130Smav{
1081230130Smav	device_t dev = w->devinfo->dev;
1082230130Smav	uint32_t wcap, cap;
1083230130Smav	nid_t nid = w->nid;
1084230130Smav	char buf[64];
1085230130Smav
1086230130Smav	w->param.widget_cap = wcap = hda_command(dev,
1087230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_AUDIO_WIDGET_CAP));
1088230130Smav	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap);
1089230130Smav
1090230130Smav	hdaa_widget_connection_parse(w);
1091230130Smav
1092230130Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) {
1093230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1094230130Smav			w->param.outamp_cap =
1095230130Smav			    hda_command(dev,
1096230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1097230130Smav			    HDA_PARAM_OUTPUT_AMP_CAP));
1098230130Smav		else
1099230130Smav			w->param.outamp_cap =
1100230130Smav			    w->devinfo->outamp_cap;
1101230130Smav	} else
1102230130Smav		w->param.outamp_cap = 0;
1103230130Smav
1104230130Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) {
1105230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1106230130Smav			w->param.inamp_cap =
1107230130Smav			    hda_command(dev,
1108230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1109230130Smav			    HDA_PARAM_INPUT_AMP_CAP));
1110230130Smav		else
1111230130Smav			w->param.inamp_cap =
1112230130Smav			    w->devinfo->inamp_cap;
1113230130Smav	} else
1114230130Smav		w->param.inamp_cap = 0;
1115230130Smav
1116230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1117230130Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1118230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) {
1119230130Smav			cap = hda_command(dev,
1120230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1121230130Smav			    HDA_PARAM_SUPP_STREAM_FORMATS));
1122230130Smav			w->param.supp_stream_formats = (cap != 0) ? cap :
1123230130Smav			    w->devinfo->supp_stream_formats;
1124230130Smav			cap = hda_command(dev,
1125230130Smav			    HDA_CMD_GET_PARAMETER(0, nid,
1126230130Smav			    HDA_PARAM_SUPP_PCM_SIZE_RATE));
1127230130Smav			w->param.supp_pcm_size_rate = (cap != 0) ? cap :
1128230130Smav			    w->devinfo->supp_pcm_size_rate;
1129230130Smav		} else {
1130230130Smav			w->param.supp_stream_formats =
1131230130Smav			    w->devinfo->supp_stream_formats;
1132230130Smav			w->param.supp_pcm_size_rate =
1133230130Smav			    w->devinfo->supp_pcm_size_rate;
1134230130Smav		}
1135230326Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1136230326Smav			w->wclass.conv.stripecap = hda_command(dev,
1137230326Smav			    HDA_CMD_GET_STRIPE_CONTROL(0, w->nid)) >> 20;
1138230326Smav		} else
1139230326Smav			w->wclass.conv.stripecap = 1;
1140230130Smav	} else {
1141230130Smav		w->param.supp_stream_formats = 0;
1142230130Smav		w->param.supp_pcm_size_rate = 0;
1143230130Smav	}
1144230130Smav
1145230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1146230130Smav		w->wclass.pin.original = w->wclass.pin.newconf =
1147230130Smav		    w->wclass.pin.config = hda_command(dev,
1148230130Smav			HDA_CMD_GET_CONFIGURATION_DEFAULT(0, w->nid));
1149230130Smav		w->wclass.pin.cap = hda_command(dev,
1150230130Smav		    HDA_CMD_GET_PARAMETER(0, w->nid, HDA_PARAM_PIN_CAP));;
1151230130Smav		w->wclass.pin.ctrl = hda_command(dev,
1152230130Smav		    HDA_CMD_GET_PIN_WIDGET_CTRL(0, nid));
1153230130Smav		if (HDA_PARAM_PIN_CAP_EAPD_CAP(w->wclass.pin.cap)) {
1154230130Smav			w->param.eapdbtl = hda_command(dev,
1155230130Smav			    HDA_CMD_GET_EAPD_BTL_ENABLE(0, nid));
1156230130Smav			w->param.eapdbtl &= 0x7;
1157230130Smav			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
1158230130Smav		} else
1159230130Smav			w->param.eapdbtl = HDA_INVALID;
1160230130Smav
1161230130Smav		hdaa_unlock(w->devinfo);
1162230130Smav		snprintf(buf, sizeof(buf), "nid%d_config", w->nid);
1163230130Smav		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1164230130Smav		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1165230130Smav		    buf, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1166230130Smav		    &w->wclass.pin.newconf, sizeof(&w->wclass.pin.newconf),
1167230130Smav		    hdaa_sysctl_config, "A", "Current pin configuration");
1168230130Smav		snprintf(buf, sizeof(buf), "nid%d_original", w->nid);
1169230130Smav		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1170230130Smav		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1171230130Smav		    buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1172230130Smav		    &w->wclass.pin.original, sizeof(&w->wclass.pin.original),
1173230130Smav		    hdaa_sysctl_config, "A", "Original pin configuration");
1174230130Smav		hdaa_lock(w->devinfo);
1175230130Smav	}
1176230312Smav	w->unsol = -1;
1177230130Smav}
1178230130Smav
1179230130Smavstatic void
1180230130Smavhdaa_widget_postprocess(struct hdaa_widget *w)
1181230130Smav{
1182230130Smav	char *typestr;
1183230130Smav
1184230130Smav	w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(w->param.widget_cap);
1185230130Smav	switch (w->type) {
1186230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
1187230130Smav		typestr = "audio output";
1188230130Smav		break;
1189230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
1190230130Smav		typestr = "audio input";
1191230130Smav		break;
1192230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
1193230130Smav		typestr = "audio mixer";
1194230130Smav		break;
1195230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
1196230130Smav		typestr = "audio selector";
1197230130Smav		break;
1198230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
1199230130Smav		typestr = "pin";
1200230130Smav		break;
1201230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET:
1202230130Smav		typestr = "power widget";
1203230130Smav		break;
1204230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET:
1205230130Smav		typestr = "volume widget";
1206230130Smav		break;
1207230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
1208230130Smav		typestr = "beep widget";
1209230130Smav		break;
1210230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET:
1211230130Smav		typestr = "vendor widget";
1212230130Smav		break;
1213230130Smav	default:
1214230130Smav		typestr = "unknown type";
1215230130Smav		break;
1216230130Smav	}
1217230130Smav	strlcpy(w->name, typestr, sizeof(w->name));
1218230130Smav
1219230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1220230130Smav		uint32_t config;
1221230130Smav		const char *devstr;
1222230130Smav		int conn, color;
1223230130Smav
1224230130Smav		config = w->wclass.pin.config;
1225230130Smav		devstr = HDA_DEVS[(config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) >>
1226230130Smav		    HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT];
1227230130Smav		conn = (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) >>
1228230130Smav		    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT;
1229230130Smav		color = (config & HDA_CONFIG_DEFAULTCONF_COLOR_MASK) >>
1230230130Smav		    HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT;
1231230130Smav		strlcat(w->name, ": ", sizeof(w->name));
1232230130Smav		strlcat(w->name, devstr, sizeof(w->name));
1233230130Smav		strlcat(w->name, " (", sizeof(w->name));
1234230130Smav		if (conn == 0 && color != 0 && color != 15) {
1235230130Smav			strlcat(w->name, HDA_COLORS[color], sizeof(w->name));
1236230130Smav			strlcat(w->name, " ", sizeof(w->name));
1237230130Smav		}
1238230130Smav		strlcat(w->name, HDA_CONNS[conn], sizeof(w->name));
1239230130Smav		strlcat(w->name, ")", sizeof(w->name));
1240230551Smav
1241230551Smav		if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
1242230551Smav		    (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
1243230551Smav			w->wclass.pin.connected = 2;
1244230130Smav	}
1245230130Smav}
1246230130Smav
1247230130Smavstruct hdaa_widget *
1248230130Smavhdaa_widget_get(struct hdaa_devinfo *devinfo, nid_t nid)
1249230130Smav{
1250230130Smav	if (devinfo == NULL || devinfo->widget == NULL ||
1251230130Smav		    nid < devinfo->startnode || nid >= devinfo->endnode)
1252230130Smav		return (NULL);
1253230130Smav	return (&devinfo->widget[nid - devinfo->startnode]);
1254230130Smav}
1255230130Smav
1256230130Smavstatic void
1257230130Smavhdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *devinfo, nid_t nid,
1258230130Smav					int index, int lmute, int rmute,
1259230130Smav					int left, int right, int dir)
1260230130Smav{
1261230130Smav	uint16_t v = 0;
1262230130Smav
1263230130Smav	HDA_BOOTHVERBOSE(
1264230130Smav		device_printf(devinfo->dev,
1265230130Smav		    "Setting amplifier nid=%d index=%d %s mute=%d/%d vol=%d/%d\n",
1266230130Smav		    nid,index,dir ? "in" : "out",lmute,rmute,left,right);
1267230130Smav	);
1268230130Smav	if (left != right || lmute != rmute) {
1269230130Smav		v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
1270230130Smav		    (lmute << 7) | left;
1271230130Smav		hda_command(devinfo->dev,
1272230130Smav		    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1273230130Smav		v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
1274230130Smav		    (rmute << 7) | right;
1275230130Smav	} else
1276230130Smav		v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
1277230130Smav		    (lmute << 7) | left;
1278230130Smav
1279230130Smav	hda_command(devinfo->dev,
1280230130Smav	    HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1281230130Smav}
1282230130Smav
1283230130Smavstatic void
1284230130Smavhdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *ctl, uint32_t mute,
1285230130Smav						int left, int right)
1286230130Smav{
1287230130Smav	nid_t nid;
1288230130Smav	int lmute, rmute;
1289230130Smav
1290230130Smav	nid = ctl->widget->nid;
1291230130Smav
1292230130Smav	/* Save new values if valid. */
1293230130Smav	if (mute != HDAA_AMP_MUTE_DEFAULT)
1294230130Smav		ctl->muted = mute;
1295230130Smav	if (left != HDAA_AMP_VOL_DEFAULT)
1296230130Smav		ctl->left = left;
1297230130Smav	if (right != HDAA_AMP_VOL_DEFAULT)
1298230130Smav		ctl->right = right;
1299230130Smav	/* Prepare effective values */
1300230130Smav	if (ctl->forcemute) {
1301230130Smav		lmute = 1;
1302230130Smav		rmute = 1;
1303230130Smav		left = 0;
1304230130Smav		right = 0;
1305230130Smav	} else {
1306230130Smav		lmute = HDAA_AMP_LEFT_MUTED(ctl->muted);
1307230130Smav		rmute = HDAA_AMP_RIGHT_MUTED(ctl->muted);
1308230130Smav		left = ctl->left;
1309230130Smav		right = ctl->right;
1310230130Smav	}
1311230130Smav	/* Apply effective values */
1312230130Smav	if (ctl->dir & HDAA_CTL_OUT)
1313230130Smav		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1314230130Smav		    lmute, rmute, left, right, 0);
1315230130Smav	if (ctl->dir & HDAA_CTL_IN)
1316230130Smav		hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1317230130Smav		    lmute, rmute, left, right, 1);
1318230130Smav}
1319230130Smav
1320230130Smavstatic void
1321230130Smavhdaa_widget_connection_select(struct hdaa_widget *w, uint8_t index)
1322230130Smav{
1323230130Smav	if (w == NULL || w->nconns < 1 || index > (w->nconns - 1))
1324230130Smav		return;
1325230130Smav	HDA_BOOTHVERBOSE(
1326230130Smav		device_printf(w->devinfo->dev,
1327230130Smav		    "Setting selector nid=%d index=%d\n", w->nid, index);
1328230130Smav	);
1329230130Smav	hda_command(w->devinfo->dev,
1330230130Smav	    HDA_CMD_SET_CONNECTION_SELECT_CONTROL(0, w->nid, index));
1331230130Smav	w->selconn = index;
1332230130Smav}
1333230130Smav
1334230130Smav/****************************************************************************
1335230130Smav * Device Methods
1336230130Smav ****************************************************************************/
1337230130Smav
1338230130Smavstatic void *
1339230130Smavhdaa_channel_init(kobj_t obj, void *data, struct snd_dbuf *b,
1340230130Smav					struct pcm_channel *c, int dir)
1341230130Smav{
1342230130Smav	struct hdaa_chan *ch = data;
1343230130Smav	struct hdaa_pcm_devinfo *pdevinfo = ch->pdevinfo;
1344230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1345230130Smav
1346230130Smav	hdaa_lock(devinfo);
1347230130Smav	if (devinfo->quirks & HDAA_QUIRK_FIXEDRATE) {
1348230130Smav		ch->caps.minspeed = ch->caps.maxspeed = 48000;
1349230130Smav		ch->pcmrates[0] = 48000;
1350230130Smav		ch->pcmrates[1] = 0;
1351230130Smav	}
1352230130Smav	ch->dir = dir;
1353230130Smav	ch->b = b;
1354230130Smav	ch->c = c;
1355230130Smav	ch->blksz = pdevinfo->chan_size / pdevinfo->chan_blkcnt;
1356230130Smav	ch->blkcnt = pdevinfo->chan_blkcnt;
1357230130Smav	hdaa_unlock(devinfo);
1358230130Smav
1359230130Smav	if (sndbuf_alloc(ch->b, bus_get_dma_tag(devinfo->dev),
1360230130Smav	    hda_get_dma_nocache(devinfo->dev) ? BUS_DMA_NOCACHE : 0,
1361230130Smav	    pdevinfo->chan_size) != 0)
1362230130Smav		return (NULL);
1363230130Smav
1364230130Smav	return (ch);
1365230130Smav}
1366230130Smav
1367230130Smavstatic int
1368230130Smavhdaa_channel_setformat(kobj_t obj, void *data, uint32_t format)
1369230130Smav{
1370230130Smav	struct hdaa_chan *ch = data;
1371230130Smav	int i;
1372230130Smav
1373230130Smav	for (i = 0; ch->caps.fmtlist[i] != 0; i++) {
1374230130Smav		if (format == ch->caps.fmtlist[i]) {
1375230130Smav			ch->fmt = format;
1376230130Smav			return (0);
1377230130Smav		}
1378230130Smav	}
1379230130Smav
1380230130Smav	return (EINVAL);
1381230130Smav}
1382230130Smav
1383230130Smavstatic uint32_t
1384230130Smavhdaa_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
1385230130Smav{
1386230130Smav	struct hdaa_chan *ch = data;
1387230130Smav	uint32_t spd = 0, threshold;
1388230130Smav	int i;
1389230130Smav
1390230130Smav	/* First look for equal or multiple frequency. */
1391230130Smav	for (i = 0; ch->pcmrates[i] != 0; i++) {
1392230130Smav		spd = ch->pcmrates[i];
1393230130Smav		if (speed != 0 && spd / speed * speed == spd) {
1394230130Smav			ch->spd = spd;
1395230130Smav			return (spd);
1396230130Smav		}
1397230130Smav	}
1398230130Smav	/* If no match, just find nearest. */
1399230130Smav	for (i = 0; ch->pcmrates[i] != 0; i++) {
1400230130Smav		spd = ch->pcmrates[i];
1401230130Smav		threshold = spd + ((ch->pcmrates[i + 1] != 0) ?
1402230130Smav		    ((ch->pcmrates[i + 1] - spd) >> 1) : 0);
1403230130Smav		if (speed < threshold)
1404230130Smav			break;
1405230130Smav	}
1406230130Smav	ch->spd = spd;
1407230130Smav	return (spd);
1408230130Smav}
1409230130Smav
1410230130Smavstatic uint16_t
1411230130Smavhdaa_stream_format(struct hdaa_chan *ch)
1412230130Smav{
1413230130Smav	int i;
1414230130Smav	uint16_t fmt;
1415230130Smav
1416230130Smav	fmt = 0;
1417230130Smav	if (ch->fmt & AFMT_S16_LE)
1418230130Smav		fmt |= ch->bit16 << 4;
1419230130Smav	else if (ch->fmt & AFMT_S32_LE)
1420230130Smav		fmt |= ch->bit32 << 4;
1421230130Smav	else
1422230130Smav		fmt |= 1 << 4;
1423230130Smav	for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
1424230130Smav		if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
1425230130Smav			fmt |= hda_rate_tab[i].base;
1426230130Smav			fmt |= hda_rate_tab[i].mul;
1427230130Smav			fmt |= hda_rate_tab[i].div;
1428230130Smav			break;
1429230130Smav		}
1430230130Smav	}
1431230130Smav	fmt |= (AFMT_CHANNEL(ch->fmt) - 1);
1432230130Smav
1433230130Smav	return (fmt);
1434230130Smav}
1435230130Smav
1436230326Smavstatic int
1437230326Smavhdaa_allowed_stripes(uint16_t fmt)
1438230326Smav{
1439230326Smav	static const int bits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1440230326Smav	int size;
1441230326Smav
1442230326Smav	size = bits[(fmt >> 4) & 0x03];
1443230326Smav	size *= (fmt & 0x0f) + 1;
1444230326Smav	size *= ((fmt >> 11) & 0x07) + 1;
1445230326Smav	return (0xffffffffU >> (32 - fls(size / 8)));
1446230326Smav}
1447230326Smav
1448230130Smavstatic void
1449230130Smavhdaa_audio_setup(struct hdaa_chan *ch)
1450230130Smav{
1451230130Smav	struct hdaa_audio_as *as = &ch->devinfo->as[ch->as];
1452230312Smav	struct hdaa_widget *w, *wp;
1453230312Smav	int i, j, k, chn, cchn, totalchn, totalextchn, c;
1454230130Smav	uint16_t fmt, dfmt;
1455230312Smav	/* Mapping channel pairs to codec pins/converters. */
1456230312Smav	const static uint16_t convmap[2][5] =
1457230312Smav	    {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x0231 }, /* 5.1 */
1458230312Smav	     { 0x0010, 0x0001, 0x2001, 0x2031, 0x2431 }};/* 7.1 */
1459230312Smav	/* Mapping formats to HDMI channel allocations. */
1460230312Smav	const static uint8_t hdmica[2][8] =
1461230312Smav	    {{ 0x02, 0x00, 0x04, 0x08, 0x0a, 0x0e, 0x12, 0x12 }, /* x.0 */
1462230312Smav	     { 0x01, 0x03, 0x01, 0x03, 0x09, 0x0b, 0x0f, 0x13 }}; /* x.1 */
1463230312Smav	/* Mapping formats to HDMI channels order. */
1464230312Smav	const static uint32_t hdmich[2][8] =
1465230312Smav	    {{ 0xFFFF0F00, 0xFFFFFF10, 0xFFF2FF10, 0xFF32FF10,
1466230312Smav	       0xFF324F10, 0xF5324F10, 0x54326F10, 0x54326F10 }, /* x.0 */
1467230312Smav	     { 0xFFFFF000, 0xFFFF0100, 0xFFFFF210, 0xFFFF2310,
1468230312Smav	       0xFF32F410, 0xFF324510, 0xF6324510, 0x76325410 }}; /* x.1 */
1469230312Smav	int convmapid = -1;
1470230312Smav	nid_t nid;
1471230312Smav	uint8_t csum;
1472230130Smav
1473230130Smav	totalchn = AFMT_CHANNEL(ch->fmt);
1474230312Smav	totalextchn = AFMT_EXTCHANNEL(ch->fmt);
1475230130Smav	HDA_BOOTHVERBOSE(
1476230130Smav		device_printf(ch->pdevinfo->dev,
1477230312Smav		    "PCMDIR_%s: Stream setup fmt=%08x (%d.%d) speed=%d\n",
1478230130Smav		    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1479230312Smav		    ch->fmt, totalchn - totalextchn, totalextchn, ch->spd);
1480230130Smav	);
1481230130Smav	fmt = hdaa_stream_format(ch);
1482230130Smav
1483230312Smav	/* Set channels to I/O converters mapping for known speaker setups. */
1484230130Smav	if ((as->pinset == 0x0007 || as->pinset == 0x0013)) /* Standard 5.1 */
1485230312Smav		convmapid = 0;
1486230130Smav	else if (as->pinset == 0x0017) /* Standard 7.1 */
1487230312Smav		convmapid = 1;
1488230130Smav
1489230130Smav	dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN;
1490230130Smav	if (ch->fmt & AFMT_AC3)
1491230130Smav		dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO;
1492230130Smav
1493230130Smav	chn = 0;
1494230130Smav	for (i = 0; ch->io[i] != -1; i++) {
1495230130Smav		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1496230130Smav		if (w == NULL)
1497230130Smav			continue;
1498230130Smav
1499230130Smav		/* If HP redirection is enabled, but failed to use same
1500230130Smav		   DAC, make last DAC to duplicate first one. */
1501230130Smav		if (as->fakeredir && i == (as->pincnt - 1)) {
1502230130Smav			c = (ch->sid << 4);
1503230130Smav		} else {
1504230312Smav			/* Map channels to I/O converters, if set. */
1505230312Smav			if (convmapid >= 0)
1506230312Smav				chn = (((convmap[convmapid][totalchn / 2]
1507230312Smav				    >> i * 4) & 0xf) - 1) * 2;
1508230130Smav			if (chn < 0 || chn >= totalchn) {
1509230130Smav				c = 0;
1510230130Smav			} else {
1511230130Smav				c = (ch->sid << 4) | chn;
1512230130Smav			}
1513230130Smav		}
1514230130Smav		hda_command(ch->devinfo->dev,
1515230130Smav		    HDA_CMD_SET_CONV_FMT(0, ch->io[i], fmt));
1516230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1517230130Smav			hda_command(ch->devinfo->dev,
1518230130Smav			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], dfmt));
1519230130Smav		}
1520230130Smav		hda_command(ch->devinfo->dev,
1521230130Smav		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i], c));
1522230326Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1523230326Smav			hda_command(ch->devinfo->dev,
1524230326Smav			    HDA_CMD_SET_STRIPE_CONTROL(0, w->nid, ch->stripectl));
1525230326Smav		}
1526230312Smav		cchn = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1527230312Smav		if (cchn > 1 && chn < totalchn) {
1528230312Smav			cchn = min(cchn, totalchn - chn - 1);
1529230312Smav			hda_command(ch->devinfo->dev,
1530230312Smav			    HDA_CMD_SET_CONV_CHAN_COUNT(0, ch->io[i], cchn));
1531230312Smav		}
1532230312Smav		HDA_BOOTHVERBOSE(
1533230312Smav			device_printf(ch->pdevinfo->dev,
1534230312Smav			    "PCMDIR_%s: Stream setup nid=%d: "
1535230312Smav			    "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x, "
1536230326Smav			    "chan_count=0x%02x, stripe=%d\n",
1537230312Smav			    (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1538230326Smav			    ch->io[i], fmt, dfmt, c, cchn, ch->stripectl);
1539230312Smav		);
1540230312Smav		for (j = 0; j < 16; j++) {
1541230312Smav			if (as->dacs[ch->asindex][j] != ch->io[i])
1542230312Smav				continue;
1543230312Smav			nid = as->pins[j];
1544230312Smav			wp = hdaa_widget_get(ch->devinfo, nid);
1545230312Smav			if (wp == NULL)
1546230312Smav				continue;
1547230312Smav			if (!HDA_PARAM_PIN_CAP_DP(wp->wclass.pin.cap) &&
1548230312Smav			    !HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap))
1549230312Smav				continue;
1550230312Smav
1551230312Smav			/* Set channel mapping. */
1552230312Smav			for (k = 0; k < 8; k++) {
1553230312Smav				hda_command(ch->devinfo->dev,
1554230312Smav				    HDA_CMD_SET_HDMI_CHAN_SLOT(0, nid,
1555230312Smav				    (((hdmich[totalextchn == 0 ? 0 : 1][totalchn - 1]
1556230312Smav				     >> (k * 4)) & 0xf) << 4) | k));
1557230312Smav			}
1558230312Smav
1559230511Smav			/*
1560230511Smav			 * Enable High Bit Rate (HBR) Encoded Packet Type
1561230511Smav			 * (EPT), if supported and needed (8ch data).
1562230511Smav			 */
1563230511Smav			if (HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap) &&
1564230511Smav			    HDA_PARAM_PIN_CAP_HBR(wp->wclass.pin.cap)) {
1565230511Smav				wp->wclass.pin.ctrl &=
1566230511Smav				    ~HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK;
1567230511Smav				if ((ch->fmt & AFMT_AC3) && (cchn == 8))
1568230511Smav					wp->wclass.pin.ctrl |= 0x03;
1569230511Smav				hda_command(ch->devinfo->dev,
1570230511Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL(0, nid,
1571230511Smav				    wp->wclass.pin.ctrl));
1572230511Smav			}
1573230511Smav
1574230312Smav			/* Stop audio infoframe transmission. */
1575230312Smav			hda_command(ch->devinfo->dev,
1576230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1577230312Smav			hda_command(ch->devinfo->dev,
1578230312Smav			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0x00));
1579230312Smav
1580230312Smav			/* Clear audio infoframe buffer. */
1581230312Smav			hda_command(ch->devinfo->dev,
1582230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1583230312Smav			for (k = 0; k < 32; k++)
1584230312Smav				hda_command(ch->devinfo->dev,
1585230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1586230312Smav
1587230312Smav			/* Write HDMI/DisplayPort audio infoframe. */
1588230312Smav			hda_command(ch->devinfo->dev,
1589230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1590230312Smav			if (w->eld != NULL && w->eld_len >= 6 &&
1591230312Smav			    ((w->eld[5] >> 2) & 0x3) == 1) { /* DisplayPort */
1592230312Smav				hda_command(ch->devinfo->dev,
1593230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1594230312Smav				hda_command(ch->devinfo->dev,
1595230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x1b));
1596230312Smav				hda_command(ch->devinfo->dev,
1597230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x44));
1598230312Smav			} else {	/* HDMI */
1599230312Smav				hda_command(ch->devinfo->dev,
1600230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1601230312Smav				hda_command(ch->devinfo->dev,
1602230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x01));
1603230312Smav				hda_command(ch->devinfo->dev,
1604230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x0a));
1605230312Smav				csum = 0;
1606230312Smav				csum -= 0x84 + 0x01 + 0x0a + (totalchn - 1) +
1607230312Smav				    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1];
1608230312Smav				hda_command(ch->devinfo->dev,
1609230312Smav				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, csum));
1610230312Smav			}
1611230312Smav			hda_command(ch->devinfo->dev,
1612230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, totalchn - 1));
1613230312Smav			hda_command(ch->devinfo->dev,
1614230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1615230312Smav			hda_command(ch->devinfo->dev,
1616230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1617230312Smav			hda_command(ch->devinfo->dev,
1618230312Smav			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid,
1619230312Smav			    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1]));
1620230312Smav
1621230312Smav			/* Start audio infoframe transmission. */
1622230312Smav			hda_command(ch->devinfo->dev,
1623230312Smav			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1624230312Smav			hda_command(ch->devinfo->dev,
1625230312Smav			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0xc0));
1626230312Smav		}
1627230312Smav		chn += cchn + 1;
1628230130Smav	}
1629230130Smav}
1630230130Smav
1631230130Smav/*
1632230130Smav * Greatest Common Divisor.
1633230130Smav */
1634230130Smavstatic unsigned
1635230130Smavgcd(unsigned a, unsigned b)
1636230130Smav{
1637230130Smav	u_int c;
1638230130Smav
1639230130Smav	while (b != 0) {
1640230130Smav		c = a;
1641230130Smav		a = b;
1642230130Smav		b = (c % b);
1643230130Smav	}
1644230130Smav	return (a);
1645230130Smav}
1646230130Smav
1647230130Smav/*
1648230130Smav * Least Common Multiple.
1649230130Smav */
1650230130Smavstatic unsigned
1651230130Smavlcm(unsigned a, unsigned b)
1652230130Smav{
1653230130Smav
1654230130Smav	return ((a * b) / gcd(a, b));
1655230130Smav}
1656230130Smav
1657230130Smavstatic int
1658230130Smavhdaa_channel_setfragments(kobj_t obj, void *data,
1659230130Smav					uint32_t blksz, uint32_t blkcnt)
1660230130Smav{
1661230130Smav	struct hdaa_chan *ch = data;
1662230130Smav
1663230130Smav	blksz -= blksz % lcm(HDA_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
1664230130Smav
1665230130Smav	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
1666230130Smav		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
1667230130Smav	if (blksz < HDA_BLK_MIN)
1668230130Smav		blksz = HDA_BLK_MIN;
1669230130Smav	if (blkcnt > HDA_BDL_MAX)
1670230130Smav		blkcnt = HDA_BDL_MAX;
1671230130Smav	if (blkcnt < HDA_BDL_MIN)
1672230130Smav		blkcnt = HDA_BDL_MIN;
1673230130Smav
1674230130Smav	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
1675230130Smav		if ((blkcnt >> 1) >= HDA_BDL_MIN)
1676230130Smav			blkcnt >>= 1;
1677230130Smav		else if ((blksz >> 1) >= HDA_BLK_MIN)
1678230130Smav			blksz >>= 1;
1679230130Smav		else
1680230130Smav			break;
1681230130Smav	}
1682230130Smav
1683230130Smav	if ((sndbuf_getblksz(ch->b) != blksz ||
1684230130Smav	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
1685230130Smav	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
1686230130Smav		device_printf(ch->devinfo->dev, "%s: failed blksz=%u blkcnt=%u\n",
1687230130Smav		    __func__, blksz, blkcnt);
1688230130Smav
1689230130Smav	ch->blksz = sndbuf_getblksz(ch->b);
1690230130Smav	ch->blkcnt = sndbuf_getblkcnt(ch->b);
1691230130Smav
1692230130Smav	return (0);
1693230130Smav}
1694230130Smav
1695230130Smavstatic uint32_t
1696230130Smavhdaa_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
1697230130Smav{
1698230130Smav	struct hdaa_chan *ch = data;
1699230130Smav
1700230130Smav	hdaa_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
1701230130Smav
1702230130Smav	return (ch->blksz);
1703230130Smav}
1704230130Smav
1705230130Smavstatic void
1706230130Smavhdaa_channel_stop(struct hdaa_chan *ch)
1707230130Smav{
1708230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
1709230130Smav	struct hdaa_widget *w;
1710230130Smav	int i;
1711230130Smav
1712230181Smav	if ((ch->flags & HDAA_CHN_RUNNING) == 0)
1713230181Smav		return;
1714230130Smav	ch->flags &= ~HDAA_CHN_RUNNING;
1715230130Smav	HDAC_STREAM_STOP(device_get_parent(devinfo->dev), devinfo->dev,
1716230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1717230130Smav	for (i = 0; ch->io[i] != -1; i++) {
1718230130Smav		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1719230130Smav		if (w == NULL)
1720230130Smav			continue;
1721230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1722230130Smav			hda_command(devinfo->dev,
1723230130Smav			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], 0));
1724230130Smav		}
1725230130Smav		hda_command(devinfo->dev,
1726230130Smav		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i],
1727230130Smav		    0));
1728230130Smav	}
1729230130Smav	HDAC_STREAM_FREE(device_get_parent(devinfo->dev), devinfo->dev,
1730230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1731230130Smav}
1732230130Smav
1733230130Smavstatic int
1734230130Smavhdaa_channel_start(struct hdaa_chan *ch)
1735230130Smav{
1736230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
1737230326Smav	uint32_t fmt;
1738230130Smav
1739230326Smav	fmt = hdaa_stream_format(ch);
1740230326Smav	ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt)) - 1;
1741230130Smav	ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev,
1742230326Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos);
1743230130Smav	if (ch->sid <= 0)
1744230130Smav		return (EBUSY);
1745230130Smav	hdaa_audio_setup(ch);
1746230130Smav	HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev,
1747230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1748230130Smav	HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev,
1749230130Smav	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid,
1750230130Smav	    sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt);
1751230130Smav	ch->flags |= HDAA_CHN_RUNNING;
1752230130Smav	return (0);
1753230130Smav}
1754230130Smav
1755230130Smavstatic int
1756230130Smavhdaa_channel_trigger(kobj_t obj, void *data, int go)
1757230130Smav{
1758230130Smav	struct hdaa_chan *ch = data;
1759230130Smav	int error = 0;
1760230130Smav
1761230130Smav	if (!PCMTRIG_COMMON(go))
1762230130Smav		return (0);
1763230130Smav
1764230130Smav	hdaa_lock(ch->devinfo);
1765230130Smav	switch (go) {
1766230130Smav	case PCMTRIG_START:
1767230130Smav		error = hdaa_channel_start(ch);
1768230130Smav		break;
1769230130Smav	case PCMTRIG_STOP:
1770230130Smav	case PCMTRIG_ABORT:
1771230130Smav		hdaa_channel_stop(ch);
1772230130Smav		break;
1773230130Smav	default:
1774230130Smav		break;
1775230130Smav	}
1776230130Smav	hdaa_unlock(ch->devinfo);
1777230130Smav
1778230130Smav	return (error);
1779230130Smav}
1780230130Smav
1781230130Smavstatic uint32_t
1782230130Smavhdaa_channel_getptr(kobj_t obj, void *data)
1783230130Smav{
1784230130Smav	struct hdaa_chan *ch = data;
1785230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
1786230130Smav	uint32_t ptr;
1787230130Smav
1788230130Smav	hdaa_lock(devinfo);
1789230130Smav	if (ch->dmapos != NULL) {
1790230130Smav		ptr = *(ch->dmapos);
1791230130Smav	} else {
1792230130Smav		ptr = HDAC_STREAM_GETPTR(
1793230130Smav		    device_get_parent(devinfo->dev), devinfo->dev,
1794230130Smav		    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1795230130Smav	}
1796230130Smav	hdaa_unlock(devinfo);
1797230130Smav
1798230130Smav	/*
1799230130Smav	 * Round to available space and force 128 bytes aligment.
1800230130Smav	 */
1801230130Smav	ptr %= ch->blksz * ch->blkcnt;
1802230130Smav	ptr &= HDA_BLK_ALIGN;
1803230130Smav
1804230130Smav	return (ptr);
1805230130Smav}
1806230130Smav
1807230130Smavstatic struct pcmchan_caps *
1808230130Smavhdaa_channel_getcaps(kobj_t obj, void *data)
1809230130Smav{
1810230130Smav	return (&((struct hdaa_chan *)data)->caps);
1811230130Smav}
1812230130Smav
1813230130Smavstatic kobj_method_t hdaa_channel_methods[] = {
1814230130Smav	KOBJMETHOD(channel_init,		hdaa_channel_init),
1815230130Smav	KOBJMETHOD(channel_setformat,		hdaa_channel_setformat),
1816230130Smav	KOBJMETHOD(channel_setspeed,		hdaa_channel_setspeed),
1817230130Smav	KOBJMETHOD(channel_setblocksize,	hdaa_channel_setblocksize),
1818230130Smav	KOBJMETHOD(channel_setfragments,	hdaa_channel_setfragments),
1819230130Smav	KOBJMETHOD(channel_trigger,		hdaa_channel_trigger),
1820230130Smav	KOBJMETHOD(channel_getptr,		hdaa_channel_getptr),
1821230130Smav	KOBJMETHOD(channel_getcaps,		hdaa_channel_getcaps),
1822230130Smav	KOBJMETHOD_END
1823230130Smav};
1824230130SmavCHANNEL_DECLARE(hdaa_channel);
1825230130Smav
1826230130Smavstatic int
1827230130Smavhdaa_audio_ctl_ossmixer_init(struct snd_mixer *m)
1828230130Smav{
1829230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
1830230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1831230130Smav	struct hdaa_widget *w, *cw;
1832230130Smav	uint32_t mask, recmask;
1833230451Smav	int i, j;
1834230130Smav
1835230130Smav	hdaa_lock(devinfo);
1836230451Smav	pdevinfo->mixer = m;
1837230130Smav
1838230130Smav	/* Make sure that in case of soft volume it won't stay muted. */
1839230130Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1840230130Smav		pdevinfo->left[i] = 100;
1841230130Smav		pdevinfo->right[i] = 100;
1842230130Smav	}
1843230130Smav
1844230451Smav	/* Declare volume controls assigned to this association. */
1845230451Smav	mask = pdevinfo->ossmask;
1846230130Smav	if (pdevinfo->playas >= 0) {
1847230451Smav		/* Declate EAPD as ogain control. */
1848230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1849230130Smav			w = hdaa_widget_get(devinfo, i);
1850230130Smav			if (w == NULL || w->enable == 0)
1851230130Smav				continue;
1852230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
1853230130Smav			    w->param.eapdbtl == HDA_INVALID ||
1854230130Smav			    w->bindas != pdevinfo->playas)
1855230130Smav				continue;
1856230130Smav			mask |= SOUND_MASK_OGAIN;
1857230130Smav			break;
1858230130Smav		}
1859230130Smav
1860230451Smav		/* Declare soft PCM volume if needed. */
1861230451Smav		if ((mask & SOUND_MASK_PCM) == 0 ||
1862230451Smav		    (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) ||
1863230451Smav		    pdevinfo->minamp[SOUND_MIXER_PCM] ==
1864230451Smav		     pdevinfo->maxamp[SOUND_MIXER_PCM]) {
1865230451Smav			mask |= SOUND_MASK_PCM;
1866230451Smav			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
1867230451Smav			HDA_BOOTHVERBOSE(
1868230451Smav				device_printf(pdevinfo->dev,
1869230451Smav				    "Forcing Soft PCM volume\n");
1870230451Smav			);
1871230451Smav		}
1872230451Smav
1873230451Smav		/* Declare master volume if needed. */
1874230451Smav		if ((mask & SOUND_MASK_VOLUME) == 0) {
1875230451Smav			mask |= SOUND_MASK_VOLUME;
1876230451Smav			mix_setparentchild(m, SOUND_MIXER_VOLUME,
1877230451Smav			    SOUND_MASK_PCM);
1878230451Smav			mix_setrealdev(m, SOUND_MIXER_VOLUME,
1879230451Smav			    SOUND_MIXER_NONE);
1880230451Smav			HDA_BOOTHVERBOSE(
1881230451Smav				device_printf(pdevinfo->dev,
1882230451Smav				    "Forcing master volume with PCM\n");
1883230451Smav			);
1884230451Smav		}
1885230130Smav	}
1886230130Smav
1887230130Smav	/* Declare record sources available to this association. */
1888230451Smav	recmask = 0;
1889230130Smav	if (pdevinfo->recas >= 0) {
1890230130Smav		for (i = 0; i < 16; i++) {
1891230130Smav			if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0)
1892230130Smav				continue;
1893230130Smav			w = hdaa_widget_get(devinfo,
1894230130Smav			    devinfo->as[pdevinfo->recas].dacs[0][i]);
1895230130Smav			if (w == NULL || w->enable == 0)
1896230130Smav				continue;
1897230130Smav			for (j = 0; j < w->nconns; j++) {
1898230130Smav				if (w->connsenable[j] == 0)
1899230130Smav					continue;
1900230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
1901230130Smav				if (cw == NULL || cw->enable == 0)
1902230130Smav					continue;
1903230130Smav				if (cw->bindas != pdevinfo->recas &&
1904230130Smav				    cw->bindas != -2)
1905230130Smav					continue;
1906230130Smav				recmask |= cw->ossmask;
1907230130Smav			}
1908230130Smav		}
1909230130Smav	}
1910230130Smav
1911230451Smav	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
1912230451Smav	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
1913230451Smav	pdevinfo->ossmask = mask;
1914230451Smav
1915230451Smav	mix_setrecdevs(m, recmask);
1916230451Smav	mix_setdevs(m, mask);
1917230451Smav
1918230451Smav	hdaa_unlock(devinfo);
1919230451Smav
1920230451Smav	return (0);
1921230451Smav}
1922230451Smav
1923230451Smav/*
1924230451Smav * Update amplification per pdevinfo per ossdev, calculate summary coefficient
1925230451Smav * and write it to codec, update *left and *right to reflect remaining error.
1926230451Smav */
1927230451Smavstatic void
1928230451Smavhdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev,
1929230451Smav    int mute, int *left, int *right)
1930230451Smav{
1931230451Smav	int i, zleft, zright, sleft, sright, smute, lval, rval;
1932230451Smav
1933230451Smav	ctl->devleft[ossdev] = *left;
1934230451Smav	ctl->devright[ossdev] = *right;
1935230451Smav	ctl->devmute[ossdev] = mute;
1936230451Smav	smute = sleft = sright = zleft = zright = 0;
1937230451Smav	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1938230451Smav		sleft += ctl->devleft[i];
1939230451Smav		sright += ctl->devright[i];
1940230451Smav		smute |= ctl->devmute[i];
1941230451Smav		if (i == ossdev)
1942230451Smav			continue;
1943230451Smav		zleft += ctl->devleft[i];
1944230451Smav		zright += ctl->devright[i];
1945230451Smav	}
1946230451Smav	lval = QDB2VAL(ctl, sleft);
1947230451Smav	rval = QDB2VAL(ctl, sright);
1948230451Smav	hdaa_audio_ctl_amp_set(ctl, smute, lval, rval);
1949230451Smav	*left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft));
1950230451Smav	*right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright));
1951230451Smav}
1952230451Smav
1953230451Smav/*
1954230451Smav * Trace signal from source, setting volumes on the way.
1955230451Smav */
1956230451Smavstatic void
1957230451Smavhdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo,
1958230451Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
1959230451Smav{
1960230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1961230451Smav	struct hdaa_widget *w, *wc;
1962230451Smav	struct hdaa_audio_ctl *ctl;
1963230451Smav	int i, j, conns = 0;
1964230451Smav
1965230451Smav	if (depth > HDA_PARSE_MAXDEPTH)
1966230451Smav		return;
1967230451Smav
1968230451Smav	w = hdaa_widget_get(devinfo, nid);
1969230451Smav	if (w == NULL || w->enable == 0)
1970230451Smav		return;
1971230451Smav
1972230451Smav	/* Count number of active inputs. */
1973230451Smav	if (depth > 0) {
1974230451Smav		for (j = 0; j < w->nconns; j++) {
1975230451Smav			if (!w->connsenable[j])
1976230451Smav				continue;
1977230451Smav			conns++;
1978230130Smav		}
1979230451Smav	}
1980230130Smav
1981230451Smav	/* If this is not a first step - use input mixer.
1982230451Smav	   Pins have common input ctl so care must be taken. */
1983230451Smav	if (depth > 0 && (conns == 1 ||
1984230451Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
1985230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
1986230451Smav		    index, 1);
1987230451Smav		if (ctl)
1988230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
1989230451Smav	}
1990230451Smav
1991230451Smav	/* If widget has own ossdev - not traverse it.
1992230451Smav	   It will be traversed on it's own. */
1993230451Smav	if (w->ossdev >= 0 && depth > 0)
1994230451Smav		return;
1995230451Smav
1996230451Smav	/* We must not traverse pin */
1997230451Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
1998230451Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
1999230451Smav	    depth > 0)
2000230451Smav		return;
2001230451Smav
2002230451Smav	/*
2003230451Smav	 * If signals mixed, we can't assign controls farther.
2004230451Smav	 * Ignore this on depth zero. Caller must knows why.
2005230451Smav	 */
2006230451Smav	if (conns > 1 &&
2007230451Smav	    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
2008230451Smav	     w->selconn != index))
2009230451Smav		return;
2010230451Smav
2011230451Smav	ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
2012230451Smav	if (ctl)
2013230451Smav		hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2014230451Smav
2015230451Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2016230451Smav		wc = hdaa_widget_get(devinfo, i);
2017230451Smav		if (wc == NULL || wc->enable == 0)
2018230451Smav			continue;
2019230451Smav		for (j = 0; j < wc->nconns; j++) {
2020230451Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
2021230451Smav				hdaa_audio_ctl_source_volume(pdevinfo, ossdev,
2022230451Smav				    wc->nid, j, mute, left, right, depth + 1);
2023230451Smav			}
2024230130Smav		}
2025230130Smav	}
2026230451Smav	return;
2027230451Smav}
2028230130Smav
2029230451Smav/*
2030230451Smav * Trace signal from destination, setting volumes on the way.
2031230451Smav */
2032230451Smavstatic void
2033230451Smavhdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo,
2034230451Smav    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2035230451Smav{
2036230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2037230451Smav	struct hdaa_audio_as *as = devinfo->as;
2038230451Smav	struct hdaa_widget *w, *wc;
2039230451Smav	struct hdaa_audio_ctl *ctl;
2040230451Smav	int i, j, consumers, cleft, cright;
2041230451Smav
2042230451Smav	if (depth > HDA_PARSE_MAXDEPTH)
2043230451Smav		return;
2044230451Smav
2045230451Smav	w = hdaa_widget_get(devinfo, nid);
2046230451Smav	if (w == NULL || w->enable == 0)
2047230451Smav		return;
2048230451Smav
2049230451Smav	if (depth > 0) {
2050230451Smav		/* If this node produce output for several consumers,
2051230451Smav		   we can't touch it. */
2052230451Smav		consumers = 0;
2053230451Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2054230451Smav			wc = hdaa_widget_get(devinfo, i);
2055230451Smav			if (wc == NULL || wc->enable == 0)
2056230451Smav				continue;
2057230451Smav			for (j = 0; j < wc->nconns; j++) {
2058230451Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
2059230451Smav					consumers++;
2060230451Smav			}
2061230130Smav		}
2062230451Smav		/* The only exception is if real HP redirection is configured
2063230451Smav		   and this is a duplication point.
2064230451Smav		   XXX: Actually exception is not completely correct.
2065230451Smav		   XXX: Duplication point check is not perfect. */
2066230451Smav		if ((consumers == 2 && (w->bindas < 0 ||
2067230451Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
2068230451Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
2069230451Smav		    consumers > 2)
2070230451Smav			return;
2071230451Smav
2072230451Smav		/* Else use it's output mixer. */
2073230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2074230451Smav		    HDAA_CTL_OUT, -1, 1);
2075230451Smav		if (ctl)
2076230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2077230130Smav	}
2078230130Smav
2079230451Smav	/* We must not traverse pin */
2080230451Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2081230451Smav	    depth > 0)
2082230451Smav		return;
2083230130Smav
2084230451Smav	for (i = 0; i < w->nconns; i++) {
2085230451Smav		if (w->connsenable[i] == 0)
2086230451Smav			continue;
2087230451Smav		if (index >= 0 && i != index)
2088230451Smav			continue;
2089230451Smav		cleft = left;
2090230451Smav		cright = right;
2091230451Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2092230451Smav		    HDAA_CTL_IN, i, 1);
2093230451Smav		if (ctl)
2094230451Smav			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright);
2095230451Smav		hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1,
2096230451Smav		    mute, cleft, cright, depth + 1);
2097230451Smav	}
2098230451Smav}
2099230130Smav
2100230451Smav/*
2101230451Smav * Set volumes for the specified pdevinfo and ossdev.
2102230451Smav */
2103230451Smavstatic void
2104230451Smavhdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev)
2105230451Smav{
2106230451Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2107230451Smav	struct hdaa_widget *w, *cw;
2108230451Smav	uint32_t mute;
2109230451Smav	int lvol, rvol;
2110230451Smav	int i, j;
2111230130Smav
2112230451Smav	mute = 0;
2113230451Smav	if (pdevinfo->left[dev] == 0) {
2114230451Smav		mute |= HDAA_AMP_MUTE_LEFT;
2115230451Smav		lvol = -4000;
2116230451Smav	} else
2117230451Smav		lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2118230451Smav		    pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev];
2119230451Smav	if (pdevinfo->right[dev] == 0) {
2120230451Smav		mute |= HDAA_AMP_MUTE_RIGHT;
2121230451Smav		rvol = -4000;
2122230451Smav	} else
2123230451Smav		rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2124230451Smav		    pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev];
2125230451Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2126230451Smav		w = hdaa_widget_get(devinfo, i);
2127230451Smav		if (w == NULL || w->enable == 0)
2128230451Smav			continue;
2129230451Smav		if (w->bindas < 0 && pdevinfo->index != 0)
2130230451Smav			continue;
2131230451Smav		if (w->bindas != pdevinfo->playas &&
2132230451Smav		    w->bindas != pdevinfo->recas)
2133230451Smav			continue;
2134230451Smav		if (dev == SOUND_MIXER_RECLEV &&
2135230451Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2136230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2137230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2138230451Smav			continue;
2139230451Smav		}
2140230451Smav		if (dev == SOUND_MIXER_VOLUME &&
2141230451Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2142230451Smav		    devinfo->as[w->bindas].dir == HDAA_CTL_OUT) {
2143230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2144230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2145230451Smav			continue;
2146230451Smav		}
2147230451Smav		if (dev == SOUND_MIXER_IGAIN &&
2148230451Smav		    w->pflags & HDAA_ADC_MONITOR) {
2149230451Smav			for (j = 0; j < w->nconns; j++) {
2150230451Smav				if (!w->connsenable[j])
2151230451Smav				    continue;
2152230451Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
2153230451Smav				if (cw == NULL || cw->enable == 0)
2154230451Smav				    continue;
2155230451Smav				if (cw->bindas == -1)
2156230451Smav				    continue;
2157230451Smav				if (cw->bindas >= 0 &&
2158230451Smav				    devinfo->as[cw->bindas].dir != HDAA_CTL_IN)
2159230451Smav					continue;
2160230451Smav				hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2161230451Smav				    w->nid, j, mute, lvol, rvol, 0);
2162230451Smav			}
2163230451Smav			continue;
2164230451Smav		}
2165230451Smav		if (w->ossdev != dev)
2166230451Smav			continue;
2167230451Smav		hdaa_audio_ctl_source_volume(pdevinfo, dev,
2168230451Smav		    w->nid, -1, mute, lvol, rvol, 0);
2169230451Smav		if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST))
2170230451Smav			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2171230451Smav			    w->nid, -1, mute, lvol, rvol, 0);
2172230451Smav	}
2173230130Smav}
2174230130Smav
2175230451Smav/*
2176230451Smav * OSS Mixer set method.
2177230451Smav */
2178230130Smavstatic int
2179230130Smavhdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
2180230130Smav					unsigned left, unsigned right)
2181230130Smav{
2182230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2183230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2184230130Smav	struct hdaa_widget *w;
2185230451Smav	int i;
2186230130Smav
2187230130Smav	hdaa_lock(devinfo);
2188230451Smav
2189230130Smav	/* Save new values. */
2190230130Smav	pdevinfo->left[dev] = left;
2191230130Smav	pdevinfo->right[dev] = right;
2192230130Smav
2193230130Smav	/* 'ogain' is the special case implemented with EAPD. */
2194230130Smav	if (dev == SOUND_MIXER_OGAIN) {
2195230130Smav		uint32_t orig;
2196230130Smav		w = NULL;
2197230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2198230130Smav			w = hdaa_widget_get(devinfo, i);
2199230130Smav			if (w == NULL || w->enable == 0)
2200230130Smav				continue;
2201230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2202230130Smav			    w->param.eapdbtl == HDA_INVALID)
2203230130Smav				continue;
2204230130Smav			break;
2205230130Smav		}
2206230130Smav		if (i >= devinfo->endnode) {
2207230130Smav			hdaa_unlock(devinfo);
2208230130Smav			return (-1);
2209230130Smav		}
2210230130Smav		orig = w->param.eapdbtl;
2211230130Smav		if (left == 0)
2212230130Smav			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2213230130Smav		else
2214230130Smav			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2215230130Smav		if (orig != w->param.eapdbtl) {
2216230130Smav			uint32_t val;
2217230130Smav
2218230130Smav			val = w->param.eapdbtl;
2219230130Smav			if (devinfo->quirks & HDAA_QUIRK_EAPDINV)
2220230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2221230130Smav			hda_command(devinfo->dev,
2222230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val));
2223230130Smav		}
2224230130Smav		hdaa_unlock(devinfo);
2225230130Smav		return (left | (left << 8));
2226230130Smav	}
2227230130Smav
2228230130Smav	/* Recalculate all controls related to this OSS device. */
2229230451Smav	hdaa_audio_ctl_dev_volume(pdevinfo, dev);
2230230451Smav
2231230451Smav	hdaa_unlock(devinfo);
2232230451Smav	return (left | (right << 8));
2233230451Smav}
2234230451Smav
2235230451Smav/*
2236230451Smav * Set mixer settings to our own default values:
2237230451Smav * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others.
2238230451Smav */
2239230451Smavstatic void
2240230451Smavhdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo)
2241230451Smav{
2242230451Smav	int amp, vol, dev;
2243230451Smav
2244230451Smav	for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) {
2245230451Smav		if ((pdevinfo->ossmask & (1 << dev)) == 0)
2246230130Smav			continue;
2247230451Smav
2248230451Smav		/* If the value was overriden, leave it as is. */
2249230451Smav		if (resource_int_value(device_get_name(pdevinfo->dev),
2250230451Smav		    device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0)
2251230130Smav			continue;
2252230130Smav
2253230451Smav		vol = -1;
2254230451Smav		if (dev == SOUND_MIXER_OGAIN)
2255230451Smav			vol = 100;
2256230451Smav		else if (dev == SOUND_MIXER_IGAIN)
2257230451Smav			vol = 0;
2258230451Smav		else if (dev == SOUND_MIXER_MIC ||
2259230451Smav		    dev == SOUND_MIXER_MONITOR)
2260230451Smav			amp = 20 * 4;	/* +20dB */
2261230451Smav		else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital)
2262230451Smav			amp = -10 * 4;	/* -10dB */
2263230451Smav		else
2264230451Smav			amp = 0;
2265230451Smav		if (vol < 0 &&
2266230451Smav		    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) {
2267230451Smav			vol = 100;
2268230451Smav		} else if (vol < 0) {
2269230451Smav			vol = ((amp - pdevinfo->minamp[dev]) * 100 +
2270230451Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) /
2271230451Smav			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]);
2272230451Smav			vol = imin(imax(vol, 1), 100);
2273230130Smav		}
2274230451Smav		mix_set(pdevinfo->mixer, dev, vol, vol);
2275230130Smav	}
2276230130Smav}
2277230130Smav
2278230130Smav/*
2279230451Smav * Recursively commutate specified record source.
2280230130Smav */
2281230130Smavstatic uint32_t
2282230130Smavhdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
2283230130Smav{
2284230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2285230130Smav	struct hdaa_widget *w, *cw;
2286230130Smav	struct hdaa_audio_ctl *ctl;
2287230130Smav	char buf[64];
2288230130Smav	int i, muted;
2289230130Smav	uint32_t res = 0;
2290230130Smav
2291230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
2292230130Smav		return (0);
2293230130Smav
2294230130Smav	w = hdaa_widget_get(devinfo, nid);
2295230130Smav	if (w == NULL || w->enable == 0)
2296230130Smav		return (0);
2297230130Smav
2298230130Smav	for (i = 0; i < w->nconns; i++) {
2299230130Smav		if (w->connsenable[i] == 0)
2300230130Smav			continue;
2301230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
2302230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
2303230130Smav			continue;
2304230130Smav		/* Call recursively to trace signal to it's source if needed. */
2305230130Smav		if ((src & cw->ossmask) != 0) {
2306230130Smav			if (cw->ossdev < 0) {
2307230130Smav				res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2308230130Smav				    w->conns[i], depth + 1);
2309230130Smav			} else {
2310230130Smav				res |= cw->ossmask;
2311230130Smav			}
2312230130Smav		}
2313230130Smav		/* We have two special cases: mixers and others (selectors). */
2314230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
2315230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo,
2316230130Smav			    w->nid, HDAA_CTL_IN, i, 1);
2317230130Smav			if (ctl == NULL)
2318230130Smav				continue;
2319230130Smav			/* If we have input control on this node mute them
2320230130Smav			 * according to requested sources. */
2321230130Smav			muted = (src & cw->ossmask) ? 0 : 1;
2322230130Smav			if (muted != ctl->forcemute) {
2323230130Smav				ctl->forcemute = muted;
2324230130Smav				hdaa_audio_ctl_amp_set(ctl,
2325230130Smav				    HDAA_AMP_MUTE_DEFAULT,
2326230130Smav				    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
2327230130Smav			}
2328230130Smav			HDA_BOOTHVERBOSE(
2329230130Smav				device_printf(pdevinfo->dev,
2330230130Smav				    "Recsel (%s): nid %d source %d %s\n",
2331230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2332230130Smav				    src, buf, sizeof(buf)),
2333230130Smav				    nid, i, muted?"mute":"unmute");
2334230130Smav			);
2335230130Smav		} else {
2336230130Smav			if (w->nconns == 1)
2337230130Smav				break;
2338230130Smav			if ((src & cw->ossmask) == 0)
2339230130Smav				continue;
2340230130Smav			/* If we found requested source - select it and exit. */
2341230130Smav			hdaa_widget_connection_select(w, i);
2342230130Smav			HDA_BOOTHVERBOSE(
2343230130Smav				device_printf(pdevinfo->dev,
2344230130Smav				    "Recsel (%s): nid %d source %d select\n",
2345230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(
2346230130Smav				    src, buf, sizeof(buf)),
2347230130Smav				    nid, i);
2348230130Smav			);
2349230130Smav			break;
2350230130Smav		}
2351230130Smav	}
2352230130Smav	return (res);
2353230130Smav}
2354230130Smav
2355230130Smavstatic uint32_t
2356230130Smavhdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
2357230130Smav{
2358230130Smav	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2359230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2360230130Smav	struct hdaa_widget *w;
2361230130Smav	struct hdaa_audio_as *as;
2362230451Smav	struct hdaa_audio_ctl *ctl;
2363230130Smav	struct hdaa_chan *ch;
2364230130Smav	int i, j;
2365230130Smav	uint32_t ret = 0xffffffff;
2366230130Smav
2367230130Smav	hdaa_lock(devinfo);
2368230130Smav	if (pdevinfo->recas < 0) {
2369230130Smav		hdaa_unlock(devinfo);
2370230130Smav		return (0);
2371230130Smav	}
2372230130Smav	as = &devinfo->as[pdevinfo->recas];
2373230130Smav
2374230130Smav	/* For non-mixed associations we always recording everything. */
2375230130Smav	if (!as->mixed) {
2376230130Smav		hdaa_unlock(devinfo);
2377230130Smav		return (mix_getrecdevs(m));
2378230130Smav	}
2379230130Smav
2380230130Smav	/* Commutate requested recsrc for each ADC. */
2381230130Smav	for (j = 0; j < as->num_chans; j++) {
2382230130Smav		ch = &devinfo->chans[as->chans[j]];
2383230130Smav		for (i = 0; ch->io[i] >= 0; i++) {
2384230130Smav			w = hdaa_widget_get(devinfo, ch->io[i]);
2385230130Smav			if (w == NULL || w->enable == 0)
2386230130Smav				continue;
2387230130Smav			ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2388230130Smav			    ch->io[i], 0);
2389230130Smav		}
2390230130Smav	}
2391230451Smav	if (ret == 0xffffffff)
2392230451Smav		ret = 0;
2393230130Smav
2394230451Smav	/*
2395230451Smav	 * Some controls could be shared. Reset volumes for controls
2396230451Smav	 * related to previously chosen devices, as they may no longer
2397230451Smav	 * affect the signal.
2398230451Smav	 */
2399230451Smav	i = 0;
2400230451Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
2401230451Smav		if (ctl->enable == 0 ||
2402230451Smav		    !(ctl->ossmask & pdevinfo->recsrc))
2403230451Smav			continue;
2404230451Smav		if (!((pdevinfo->playas >= 0 &&
2405230451Smav		    ctl->widget->bindas == pdevinfo->playas) ||
2406230451Smav		    (pdevinfo->recas >= 0 &&
2407230451Smav		    ctl->widget->bindas == pdevinfo->recas) ||
2408230451Smav		    (pdevinfo->index == 0 &&
2409230451Smav		    ctl->widget->bindas == -2)))
2410230451Smav			continue;
2411230451Smav		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2412230451Smav			if (pdevinfo->recsrc & (1 << j)) {
2413230451Smav				ctl->devleft[j] = 0;
2414230451Smav				ctl->devright[j] = 0;
2415230451Smav				ctl->devmute[j] = 0;
2416230451Smav			}
2417230451Smav		}
2418230451Smav	}
2419230451Smav
2420230451Smav	/*
2421230451Smav	 * Some controls could be shared. Set volumes for controls
2422230451Smav	 * related to devices selected both previously and now.
2423230451Smav	 */
2424230451Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2425230451Smav		if ((ret | pdevinfo->recsrc) & (1 << j))
2426230451Smav			hdaa_audio_ctl_dev_volume(pdevinfo, j);
2427230451Smav	}
2428230451Smav
2429230451Smav	pdevinfo->recsrc = ret;
2430230130Smav	hdaa_unlock(devinfo);
2431230451Smav	return (ret);
2432230130Smav}
2433230130Smav
2434230130Smavstatic kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = {
2435230130Smav	KOBJMETHOD(mixer_init,		hdaa_audio_ctl_ossmixer_init),
2436230130Smav	KOBJMETHOD(mixer_set,		hdaa_audio_ctl_ossmixer_set),
2437230130Smav	KOBJMETHOD(mixer_setrecsrc,	hdaa_audio_ctl_ossmixer_setrecsrc),
2438230130Smav	KOBJMETHOD_END
2439230130Smav};
2440230130SmavMIXER_DECLARE(hdaa_audio_ctl_ossmixer);
2441230130Smav
2442230130Smavstatic void
2443230130Smavhdaa_dump_gpi(struct hdaa_devinfo *devinfo)
2444230130Smav{
2445230130Smav	device_t dev = devinfo->dev;
2446230130Smav	int i;
2447230130Smav	uint32_t data, wake, unsol, sticky;
2448230130Smav
2449230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) {
2450230130Smav		data = hda_command(dev,
2451230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
2452230130Smav		wake = hda_command(dev,
2453230130Smav		    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid));
2454230130Smav		unsol = hda_command(dev,
2455230130Smav		    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2456230130Smav		sticky = hda_command(dev,
2457230130Smav		    HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid));
2458230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) {
2459230130Smav			device_printf(dev, " GPI%d:%s%s%s state=%d", i,
2460230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2461230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2462230130Smav				    (wake & (1 << i)) ? " wake" : "",
2463230130Smav				    (data >> i) & 1);
2464230130Smav		}
2465230130Smav	}
2466230130Smav}
2467230130Smav
2468230130Smavstatic void
2469230130Smavhdaa_dump_gpio(struct hdaa_devinfo *devinfo)
2470230130Smav{
2471230130Smav	device_t dev = devinfo->dev;
2472230130Smav	int i;
2473230130Smav	uint32_t data, dir, enable, wake, unsol, sticky;
2474230130Smav
2475230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) {
2476230130Smav		data = hda_command(dev,
2477230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
2478230130Smav		enable = hda_command(dev,
2479230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
2480230130Smav		dir = hda_command(dev,
2481230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
2482230130Smav		wake = hda_command(dev,
2483230130Smav		    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid));
2484230130Smav		unsol = hda_command(dev,
2485230130Smav		    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2486230130Smav		sticky = hda_command(dev,
2487230130Smav		    HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid));
2488230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) {
2489230130Smav			device_printf(dev, " GPIO%d: ", i);
2490230130Smav			if ((enable & (1 << i)) == 0) {
2491230130Smav				printf("disabled\n");
2492230130Smav				continue;
2493230130Smav			}
2494230130Smav			if ((dir & (1 << i)) == 0) {
2495230130Smav				printf("input%s%s%s",
2496230130Smav				    (sticky & (1 << i)) ? " sticky" : "",
2497230130Smav				    (unsol & (1 << i)) ? " unsol" : "",
2498230130Smav				    (wake & (1 << i)) ? " wake" : "");
2499230130Smav			} else
2500230130Smav				printf("output");
2501230130Smav			printf(" state=%d\n", (data >> i) & 1);
2502230130Smav		}
2503230130Smav	}
2504230130Smav}
2505230130Smav
2506230130Smavstatic void
2507230130Smavhdaa_dump_gpo(struct hdaa_devinfo *devinfo)
2508230130Smav{
2509230130Smav	device_t dev = devinfo->dev;
2510230130Smav	int i;
2511230130Smav	uint32_t data;
2512230130Smav
2513230130Smav	if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) {
2514230130Smav		data = hda_command(dev,
2515230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
2516230130Smav		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) {
2517230130Smav			device_printf(dev, " GPO%d: state=%d", i,
2518230130Smav				    (data >> i) & 1);
2519230130Smav		}
2520230130Smav	}
2521230130Smav}
2522230130Smav
2523230130Smavstatic void
2524230130Smavhdaa_audio_parse(struct hdaa_devinfo *devinfo)
2525230130Smav{
2526230130Smav	struct hdaa_widget *w;
2527230130Smav	uint32_t res;
2528230130Smav	int i;
2529230130Smav	nid_t nid;
2530230130Smav
2531230130Smav	nid = devinfo->nid;
2532230130Smav
2533230130Smav	res = hda_command(devinfo->dev,
2534230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT));
2535230130Smav	devinfo->gpio_cap = res;
2536230130Smav
2537230130Smav	HDA_BOOTVERBOSE(
2538230130Smav		device_printf(devinfo->dev,
2539230130Smav		    "NumGPIO=%d NumGPO=%d "
2540230130Smav		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
2541230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
2542230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
2543230130Smav		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
2544230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
2545230130Smav		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
2546230130Smav		hdaa_dump_gpi(devinfo);
2547230130Smav		hdaa_dump_gpio(devinfo);
2548230130Smav		hdaa_dump_gpo(devinfo);
2549230130Smav	);
2550230130Smav
2551230130Smav	res = hda_command(devinfo->dev,
2552230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS));
2553230130Smav	devinfo->supp_stream_formats = res;
2554230130Smav
2555230130Smav	res = hda_command(devinfo->dev,
2556230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE));
2557230130Smav	devinfo->supp_pcm_size_rate = res;
2558230130Smav
2559230130Smav	res = hda_command(devinfo->dev,
2560230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP));
2561230130Smav	devinfo->outamp_cap = res;
2562230130Smav
2563230130Smav	res = hda_command(devinfo->dev,
2564230130Smav	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP));
2565230130Smav	devinfo->inamp_cap = res;
2566230130Smav
2567230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2568230130Smav		w = hdaa_widget_get(devinfo, i);
2569230130Smav		if (w == NULL)
2570230130Smav			device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i);
2571230130Smav		else {
2572230130Smav			w->devinfo = devinfo;
2573230130Smav			w->nid = i;
2574230130Smav			w->enable = 1;
2575230130Smav			w->selconn = -1;
2576230130Smav			w->pflags = 0;
2577230130Smav			w->ossdev = -1;
2578230130Smav			w->bindas = -1;
2579230130Smav			w->param.eapdbtl = HDA_INVALID;
2580230130Smav			hdaa_widget_parse(w);
2581230130Smav		}
2582230130Smav	}
2583230130Smav}
2584230130Smav
2585230130Smavstatic void
2586230130Smavhdaa_audio_postprocess(struct hdaa_devinfo *devinfo)
2587230130Smav{
2588230130Smav	struct hdaa_widget *w;
2589230130Smav	int i;
2590230130Smav
2591230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2592230130Smav		w = hdaa_widget_get(devinfo, i);
2593230130Smav		if (w == NULL)
2594230130Smav			continue;
2595230130Smav		hdaa_widget_postprocess(w);
2596230130Smav	}
2597230130Smav}
2598230130Smav
2599230130Smavstatic void
2600230130Smavhdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo)
2601230130Smav{
2602230130Smav	struct hdaa_audio_ctl *ctls;
2603230130Smav	struct hdaa_widget *w, *cw;
2604230130Smav	int i, j, cnt, max, ocap, icap;
2605230130Smav	int mute, offset, step, size;
2606230130Smav
2607230130Smav	/* XXX This is redundant */
2608230130Smav	max = 0;
2609230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2610230130Smav		w = hdaa_widget_get(devinfo, i);
2611230130Smav		if (w == NULL || w->enable == 0)
2612230130Smav			continue;
2613230130Smav		if (w->param.outamp_cap != 0)
2614230130Smav			max++;
2615230130Smav		if (w->param.inamp_cap != 0) {
2616230130Smav			switch (w->type) {
2617230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2618230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2619230130Smav				for (j = 0; j < w->nconns; j++) {
2620230130Smav					cw = hdaa_widget_get(devinfo,
2621230130Smav					    w->conns[j]);
2622230130Smav					if (cw == NULL || cw->enable == 0)
2623230130Smav						continue;
2624230130Smav					max++;
2625230130Smav				}
2626230130Smav				break;
2627230130Smav			default:
2628230130Smav				max++;
2629230130Smav				break;
2630230130Smav			}
2631230130Smav		}
2632230130Smav	}
2633230130Smav	devinfo->ctlcnt = max;
2634230130Smav
2635230130Smav	if (max < 1)
2636230130Smav		return;
2637230130Smav
2638230130Smav	ctls = (struct hdaa_audio_ctl *)malloc(
2639230130Smav	    sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT);
2640230130Smav
2641230130Smav	if (ctls == NULL) {
2642230130Smav		/* Blekh! */
2643230130Smav		device_printf(devinfo->dev, "unable to allocate ctls!\n");
2644230130Smav		devinfo->ctlcnt = 0;
2645230130Smav		return;
2646230130Smav	}
2647230130Smav
2648230130Smav	cnt = 0;
2649230130Smav	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
2650230130Smav		if (cnt >= max) {
2651230130Smav			device_printf(devinfo->dev, "%s: Ctl overflow!\n",
2652230130Smav			    __func__);
2653230130Smav			break;
2654230130Smav		}
2655230130Smav		w = hdaa_widget_get(devinfo, i);
2656230130Smav		if (w == NULL || w->enable == 0)
2657230130Smav			continue;
2658230130Smav		ocap = w->param.outamp_cap;
2659230130Smav		icap = w->param.inamp_cap;
2660230130Smav		if (ocap != 0) {
2661230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
2662230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
2663230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
2664230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
2665230130Smav			/*if (offset > step) {
2666230130Smav				HDA_BOOTVERBOSE(
2667230130Smav					device_printf(devinfo->dev,
2668230130Smav					    "BUGGY outamp: nid=%d "
2669230130Smav					    "[offset=%d > step=%d]\n",
2670230130Smav					    w->nid, offset, step);
2671230130Smav				);
2672230130Smav				offset = step;
2673230130Smav			}*/
2674230130Smav			ctls[cnt].enable = 1;
2675230130Smav			ctls[cnt].widget = w;
2676230130Smav			ctls[cnt].mute = mute;
2677230130Smav			ctls[cnt].step = step;
2678230130Smav			ctls[cnt].size = size;
2679230130Smav			ctls[cnt].offset = offset;
2680230130Smav			ctls[cnt].left = offset;
2681230130Smav			ctls[cnt].right = offset;
2682230130Smav			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2683230130Smav			    w->waspin)
2684230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
2685230130Smav			else
2686230130Smav				ctls[cnt].ndir = HDAA_CTL_OUT;
2687230130Smav			ctls[cnt++].dir = HDAA_CTL_OUT;
2688230130Smav		}
2689230130Smav
2690230130Smav		if (icap != 0) {
2691230130Smav			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
2692230130Smav			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
2693230130Smav			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
2694230130Smav			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
2695230130Smav			/*if (offset > step) {
2696230130Smav				HDA_BOOTVERBOSE(
2697230130Smav					device_printf(devinfo->dev,
2698230130Smav					    "BUGGY inamp: nid=%d "
2699230130Smav					    "[offset=%d > step=%d]\n",
2700230130Smav					    w->nid, offset, step);
2701230130Smav				);
2702230130Smav				offset = step;
2703230130Smav			}*/
2704230130Smav			switch (w->type) {
2705230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2706230130Smav			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2707230130Smav				for (j = 0; j < w->nconns; j++) {
2708230130Smav					if (cnt >= max) {
2709230130Smav						device_printf(devinfo->dev,
2710230130Smav						    "%s: Ctl overflow!\n",
2711230130Smav						    __func__);
2712230130Smav						break;
2713230130Smav					}
2714230130Smav					cw = hdaa_widget_get(devinfo,
2715230130Smav					    w->conns[j]);
2716230130Smav					if (cw == NULL || cw->enable == 0)
2717230130Smav						continue;
2718230130Smav					ctls[cnt].enable = 1;
2719230130Smav					ctls[cnt].widget = w;
2720230130Smav					ctls[cnt].childwidget = cw;
2721230130Smav					ctls[cnt].index = j;
2722230130Smav					ctls[cnt].mute = mute;
2723230130Smav					ctls[cnt].step = step;
2724230130Smav					ctls[cnt].size = size;
2725230130Smav					ctls[cnt].offset = offset;
2726230130Smav					ctls[cnt].left = offset;
2727230130Smav					ctls[cnt].right = offset;
2728230130Smav				ctls[cnt].ndir = HDAA_CTL_IN;
2729230130Smav					ctls[cnt++].dir = HDAA_CTL_IN;
2730230130Smav				}
2731230130Smav				break;
2732230130Smav			default:
2733230130Smav				if (cnt >= max) {
2734230130Smav					device_printf(devinfo->dev,
2735230130Smav					    "%s: Ctl overflow!\n",
2736230130Smav					    __func__);
2737230130Smav					break;
2738230130Smav				}
2739230130Smav				ctls[cnt].enable = 1;
2740230130Smav				ctls[cnt].widget = w;
2741230130Smav				ctls[cnt].mute = mute;
2742230130Smav				ctls[cnt].step = step;
2743230130Smav				ctls[cnt].size = size;
2744230130Smav				ctls[cnt].offset = offset;
2745230130Smav				ctls[cnt].left = offset;
2746230130Smav				ctls[cnt].right = offset;
2747230130Smav				if (w->type ==
2748230130Smav				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2749230130Smav					ctls[cnt].ndir = HDAA_CTL_OUT;
2750230130Smav				else
2751230130Smav					ctls[cnt].ndir = HDAA_CTL_IN;
2752230130Smav				ctls[cnt++].dir = HDAA_CTL_IN;
2753230130Smav				break;
2754230130Smav			}
2755230130Smav		}
2756230130Smav	}
2757230130Smav
2758230130Smav	devinfo->ctl = ctls;
2759230130Smav}
2760230130Smav
2761230130Smavstatic void
2762230130Smavhdaa_audio_as_parse(struct hdaa_devinfo *devinfo)
2763230130Smav{
2764230130Smav	struct hdaa_audio_as *as;
2765230130Smav	struct hdaa_widget *w;
2766230130Smav	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
2767230130Smav
2768230130Smav	/* Count present associations */
2769230130Smav	max = 0;
2770230130Smav	for (j = 1; j < 16; j++) {
2771230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2772230130Smav			w = hdaa_widget_get(devinfo, i);
2773230130Smav			if (w == NULL || w->enable == 0)
2774230130Smav				continue;
2775230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2776230130Smav				continue;
2777230130Smav			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
2778230130Smav			    != j)
2779230130Smav				continue;
2780230130Smav			max++;
2781230130Smav			if (j != 15)  /* There could be many 1-pin assocs #15 */
2782230130Smav				break;
2783230130Smav		}
2784230130Smav	}
2785230130Smav
2786230130Smav	devinfo->ascnt = max;
2787230130Smav
2788230130Smav	if (max < 1)
2789230130Smav		return;
2790230130Smav
2791230130Smav	as = (struct hdaa_audio_as *)malloc(
2792230130Smav	    sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT);
2793230130Smav
2794230130Smav	if (as == NULL) {
2795230130Smav		/* Blekh! */
2796230130Smav		device_printf(devinfo->dev, "unable to allocate assocs!\n");
2797230130Smav		devinfo->ascnt = 0;
2798230130Smav		return;
2799230130Smav	}
2800230130Smav
2801230130Smav	for (i = 0; i < max; i++) {
2802230130Smav		as[i].hpredir = -1;
2803230130Smav		as[i].digital = 0;
2804230130Smav		as[i].num_chans = 1;
2805230130Smav		as[i].location = -1;
2806230130Smav	}
2807230130Smav
2808230130Smav	/* Scan associations skipping as=0. */
2809230130Smav	cnt = 0;
2810230130Smav	for (j = 1; j < 16; j++) {
2811230130Smav		first = 16;
2812230130Smav		hpredir = 0;
2813230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2814230130Smav			w = hdaa_widget_get(devinfo, i);
2815230130Smav			if (w == NULL || w->enable == 0)
2816230130Smav				continue;
2817230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2818230130Smav				continue;
2819230130Smav			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
2820230130Smav			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
2821230130Smav			if (assoc != j) {
2822230130Smav				continue;
2823230130Smav			}
2824230130Smav			KASSERT(cnt < max,
2825230130Smav			    ("%s: Associations owerflow (%d of %d)",
2826230130Smav			    __func__, cnt, max));
2827230130Smav			type = w->wclass.pin.config &
2828230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2829230130Smav			/* Get pin direction. */
2830230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
2831230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
2832230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
2833230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
2834230130Smav			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
2835230130Smav				dir = HDAA_CTL_OUT;
2836230130Smav			else
2837230130Smav				dir = HDAA_CTL_IN;
2838230130Smav			/* If this is a first pin - create new association. */
2839230130Smav			if (as[cnt].pincnt == 0) {
2840230130Smav				as[cnt].enable = 1;
2841230130Smav				as[cnt].index = j;
2842230130Smav				as[cnt].dir = dir;
2843230130Smav			}
2844230130Smav			if (seq < first)
2845230130Smav				first = seq;
2846230130Smav			/* Check association correctness. */
2847230130Smav			if (as[cnt].pins[seq] != 0) {
2848230130Smav				device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) "
2849230130Smav				    "in association %d! Disabling association.\n",
2850230130Smav				    __func__, seq, w->nid, j);
2851230130Smav				as[cnt].enable = 0;
2852230130Smav			}
2853230130Smav			if (dir != as[cnt].dir) {
2854230130Smav				device_printf(devinfo->dev, "%s: Pin %d has wrong "
2855230130Smav				    "direction for association %d! Disabling "
2856230130Smav				    "association.\n",
2857230130Smav				    __func__, w->nid, j);
2858230130Smav				as[cnt].enable = 0;
2859230130Smav			}
2860230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
2861230507Smav				as[cnt].digital |= 0x1;
2862230507Smav				if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
2863230507Smav					as[cnt].digital |= 0x2;
2864230130Smav				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
2865230507Smav					as[cnt].digital |= 0x4;
2866230130Smav			}
2867230130Smav			if (as[cnt].location == -1) {
2868230130Smav				as[cnt].location =
2869230130Smav				    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config);
2870230130Smav			} else if (as[cnt].location !=
2871230130Smav			    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) {
2872230130Smav				as[cnt].location = -2;
2873230130Smav			}
2874230130Smav			/* Headphones with seq=15 may mean redirection. */
2875230130Smav			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
2876230130Smav			    seq == 15)
2877230130Smav				hpredir = 1;
2878230130Smav			as[cnt].pins[seq] = w->nid;
2879230130Smav			as[cnt].pincnt++;
2880230130Smav			/* Association 15 is a multiple unassociated pins. */
2881230130Smav			if (j == 15)
2882230130Smav				cnt++;
2883230130Smav		}
2884230130Smav		if (j != 15 && as[cnt].pincnt > 0) {
2885230130Smav			if (hpredir && as[cnt].pincnt > 1)
2886230130Smav				as[cnt].hpredir = first;
2887230130Smav			cnt++;
2888230130Smav		}
2889230130Smav	}
2890230130Smav	for (i = 0; i < max; i++) {
2891230130Smav		if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 ||
2892230130Smav		    as[i].pins[14] > 0 || as[i].pins[15] > 0))
2893230130Smav			as[i].mixed = 1;
2894230130Smav	}
2895230130Smav	HDA_BOOTVERBOSE(
2896230130Smav		device_printf(devinfo->dev,
2897230130Smav		    "%d associations found:\n", max);
2898230130Smav		for (i = 0; i < max; i++) {
2899230130Smav			device_printf(devinfo->dev,
2900230130Smav			    "Association %d (%d) %s%s:\n",
2901230130Smav			    i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out",
2902230130Smav			    as[i].enable?"":" (disabled)");
2903230130Smav			for (j = 0; j < 16; j++) {
2904230130Smav				if (as[i].pins[j] == 0)
2905230130Smav					continue;
2906230130Smav				device_printf(devinfo->dev,
2907230130Smav				    " Pin nid=%d seq=%d\n",
2908230130Smav				    as[i].pins[j], j);
2909230130Smav			}
2910230130Smav		}
2911230130Smav	);
2912230130Smav
2913230130Smav	devinfo->as = as;
2914230130Smav}
2915230130Smav
2916230130Smav/*
2917230130Smav * Trace path from DAC to pin.
2918230130Smav */
2919230130Smavstatic nid_t
2920230130Smavhdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
2921230130Smav    int dupseq, int min, int only, int depth)
2922230130Smav{
2923230130Smav	struct hdaa_widget *w;
2924230130Smav	int i, im = -1;
2925230130Smav	nid_t m = 0, ret;
2926230130Smav
2927230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
2928230130Smav		return (0);
2929230130Smav	w = hdaa_widget_get(devinfo, nid);
2930230130Smav	if (w == NULL || w->enable == 0)
2931230130Smav		return (0);
2932230130Smav	HDA_BOOTHVERBOSE(
2933230130Smav		if (!only) {
2934230130Smav			device_printf(devinfo->dev,
2935230130Smav			    " %*stracing via nid %d\n",
2936230130Smav				depth + 1, "", w->nid);
2937230130Smav		}
2938230130Smav	);
2939230130Smav	/* Use only unused widgets */
2940230130Smav	if (w->bindas >= 0 && w->bindas != as) {
2941230130Smav		HDA_BOOTHVERBOSE(
2942230130Smav			if (!only) {
2943230130Smav				device_printf(devinfo->dev,
2944230130Smav				    " %*snid %d busy by association %d\n",
2945230130Smav					depth + 1, "", w->nid, w->bindas);
2946230130Smav			}
2947230130Smav		);
2948230130Smav		return (0);
2949230130Smav	}
2950230130Smav	if (dupseq < 0) {
2951230130Smav		if (w->bindseqmask != 0) {
2952230130Smav			HDA_BOOTHVERBOSE(
2953230130Smav				if (!only) {
2954230130Smav					device_printf(devinfo->dev,
2955230130Smav					    " %*snid %d busy by seqmask %x\n",
2956230130Smav						depth + 1, "", w->nid, w->bindseqmask);
2957230130Smav				}
2958230130Smav			);
2959230130Smav			return (0);
2960230130Smav		}
2961230130Smav	} else {
2962230130Smav		/* If this is headphones - allow duplicate first pin. */
2963230130Smav		if (w->bindseqmask != 0 &&
2964230130Smav		    (w->bindseqmask & (1 << dupseq)) == 0) {
2965230130Smav			HDA_BOOTHVERBOSE(
2966230130Smav				device_printf(devinfo->dev,
2967230130Smav				    " %*snid %d busy by seqmask %x\n",
2968230130Smav					depth + 1, "", w->nid, w->bindseqmask);
2969230130Smav			);
2970230130Smav			return (0);
2971230130Smav		}
2972230130Smav	}
2973230130Smav
2974230130Smav	switch (w->type) {
2975230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
2976230130Smav		/* Do not traverse input. AD1988 has digital monitor
2977230130Smav		for which we are not ready. */
2978230130Smav		break;
2979230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
2980230130Smav		/* If we are tracing HP take only dac of first pin. */
2981230130Smav		if ((only == 0 || only == w->nid) &&
2982230130Smav		    (w->nid >= min) && (dupseq < 0 || w->nid ==
2983230130Smav		    devinfo->as[as].dacs[0][dupseq]))
2984230130Smav			m = w->nid;
2985230130Smav		break;
2986230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
2987230130Smav		if (depth > 0)
2988230130Smav			break;
2989230130Smav		/* Fall */
2990230130Smav	default:
2991230130Smav		/* Find reachable DACs with smallest nid respecting constraints. */
2992230130Smav		for (i = 0; i < w->nconns; i++) {
2993230130Smav			if (w->connsenable[i] == 0)
2994230130Smav				continue;
2995230130Smav			if (w->selconn != -1 && w->selconn != i)
2996230130Smav				continue;
2997230130Smav			if ((ret = hdaa_audio_trace_dac(devinfo, as, seq,
2998230130Smav			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
2999230130Smav				if (m == 0 || ret < m) {
3000230130Smav					m = ret;
3001230130Smav					im = i;
3002230130Smav				}
3003230130Smav				if (only || dupseq >= 0)
3004230130Smav					break;
3005230130Smav			}
3006230130Smav		}
3007230130Smav		if (im >= 0 && only && ((w->nconns > 1 &&
3008230130Smav		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3009230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3010230130Smav			w->selconn = im;
3011230130Smav		break;
3012230130Smav	}
3013230130Smav	if (m && only) {
3014230130Smav		w->bindas = as;
3015230130Smav		w->bindseqmask |= (1 << seq);
3016230130Smav	}
3017230130Smav	HDA_BOOTHVERBOSE(
3018230130Smav		if (!only) {
3019230130Smav			device_printf(devinfo->dev,
3020230130Smav			    " %*snid %d returned %d\n",
3021230130Smav				depth + 1, "", w->nid, m);
3022230130Smav		}
3023230130Smav	);
3024230130Smav	return (m);
3025230130Smav}
3026230130Smav
3027230130Smav/*
3028230130Smav * Trace path from widget to ADC.
3029230130Smav */
3030230130Smavstatic nid_t
3031230130Smavhdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3032230130Smav    int mixed, int min, int only, int depth, int *length, int onlylength)
3033230130Smav{
3034230130Smav	struct hdaa_widget *w, *wc;
3035230130Smav	int i, j, im, lm = HDA_PARSE_MAXDEPTH;
3036230130Smav	nid_t m = 0, ret;
3037230130Smav
3038230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3039230130Smav		return (0);
3040230130Smav	w = hdaa_widget_get(devinfo, nid);
3041230130Smav	if (w == NULL || w->enable == 0)
3042230130Smav		return (0);
3043230130Smav	HDA_BOOTHVERBOSE(
3044230130Smav		device_printf(devinfo->dev,
3045230130Smav		    " %*stracing via nid %d\n",
3046230130Smav			depth + 1, "", w->nid);
3047230130Smav	);
3048230130Smav	/* Use only unused widgets */
3049230130Smav	if (w->bindas >= 0 && w->bindas != as) {
3050230130Smav		HDA_BOOTHVERBOSE(
3051230130Smav			device_printf(devinfo->dev,
3052230130Smav			    " %*snid %d busy by association %d\n",
3053230130Smav				depth + 1, "", w->nid, w->bindas);
3054230130Smav		);
3055230130Smav		return (0);
3056230130Smav	}
3057230130Smav	if (!mixed && w->bindseqmask != 0) {
3058230130Smav		HDA_BOOTHVERBOSE(
3059230130Smav			device_printf(devinfo->dev,
3060230130Smav			    " %*snid %d busy by seqmask %x\n",
3061230130Smav				depth + 1, "", w->nid, w->bindseqmask);
3062230130Smav		);
3063230130Smav		return (0);
3064230130Smav	}
3065230130Smav	switch (w->type) {
3066230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3067230130Smav		if ((only == 0 || only == w->nid) && (w->nid >= min) &&
3068230130Smav		    (onlylength == 0 || onlylength == depth)) {
3069230130Smav			m = w->nid;
3070230130Smav			if (length != NULL)
3071230130Smav				*length = depth;
3072230130Smav		}
3073230130Smav		break;
3074230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3075230130Smav		if (depth > 0)
3076230130Smav			break;
3077230130Smav		/* Fall */
3078230130Smav	default:
3079230130Smav		/* Try to find reachable ADCs with specified nid. */
3080230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3081230130Smav			wc = hdaa_widget_get(devinfo, j);
3082230130Smav			if (wc == NULL || wc->enable == 0)
3083230130Smav				continue;
3084230130Smav			im = -1;
3085230130Smav			for (i = 0; i < wc->nconns; i++) {
3086230130Smav				if (wc->connsenable[i] == 0)
3087230130Smav					continue;
3088230130Smav				if (wc->conns[i] != nid)
3089230130Smav					continue;
3090230130Smav				if ((ret = hdaa_audio_trace_adc(devinfo, as, seq,
3091230130Smav				    j, mixed, min, only, depth + 1,
3092230130Smav				    length, onlylength)) != 0) {
3093230130Smav					if (m == 0 || ret < m ||
3094230130Smav					    (ret == m && length != NULL &&
3095230130Smav					     *length < lm)) {
3096230130Smav						m = ret;
3097230130Smav						im = i;
3098230130Smav						lm = *length;
3099230130Smav					}
3100230130Smav					if (only)
3101230130Smav						break;
3102230130Smav				}
3103230130Smav			}
3104230130Smav			if (im >= 0 && only && ((wc->nconns > 1 &&
3105230130Smav			    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3106230130Smav			    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3107230130Smav				wc->selconn = im;
3108230130Smav		}
3109230130Smav		break;
3110230130Smav	}
3111230130Smav	if (m && only) {
3112230130Smav		w->bindas = as;
3113230130Smav		w->bindseqmask |= (1 << seq);
3114230130Smav	}
3115230130Smav	HDA_BOOTHVERBOSE(
3116230130Smav		device_printf(devinfo->dev,
3117230130Smav		    " %*snid %d returned %d\n",
3118230130Smav			depth + 1, "", w->nid, m);
3119230130Smav	);
3120230130Smav	return (m);
3121230130Smav}
3122230130Smav
3123230130Smav/*
3124230130Smav * Erase trace path of the specified association.
3125230130Smav */
3126230130Smavstatic void
3127230130Smavhdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq)
3128230130Smav{
3129230130Smav	struct hdaa_widget *w;
3130230130Smav	int i;
3131230130Smav
3132230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3133230130Smav		w = hdaa_widget_get(devinfo, i);
3134230130Smav		if (w == NULL || w->enable == 0)
3135230130Smav			continue;
3136230130Smav		if (w->bindas == as) {
3137230130Smav			if (seq >= 0) {
3138230130Smav				w->bindseqmask &= ~(1 << seq);
3139230130Smav				if (w->bindseqmask == 0) {
3140230130Smav					w->bindas = -1;
3141230130Smav					w->selconn = -1;
3142230130Smav				}
3143230130Smav			} else {
3144230130Smav				w->bindas = -1;
3145230130Smav				w->bindseqmask = 0;
3146230130Smav				w->selconn = -1;
3147230130Smav			}
3148230130Smav		}
3149230130Smav	}
3150230130Smav}
3151230130Smav
3152230130Smav/*
3153230130Smav * Trace association path from DAC to output
3154230130Smav */
3155230130Smavstatic int
3156230130Smavhdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq)
3157230130Smav{
3158230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3159230130Smav	int i, hpredir;
3160230130Smav	nid_t min, res;
3161230130Smav
3162230130Smav	/* Find next pin */
3163230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3164230130Smav		;
3165230130Smav	/* Check if there is no any left. If so - we succeeded. */
3166230130Smav	if (i == 16)
3167230130Smav		return (1);
3168230130Smav
3169230130Smav	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
3170230130Smav	min = 0;
3171230130Smav	do {
3172230130Smav		HDA_BOOTHVERBOSE(
3173230130Smav			device_printf(devinfo->dev,
3174230130Smav			    " Tracing pin %d with min nid %d",
3175230130Smav			    ases[as].pins[i], min);
3176230130Smav			if (hpredir >= 0)
3177230130Smav				printf(" and hpredir %d", hpredir);
3178230130Smav			printf("\n");
3179230130Smav		);
3180230130Smav		/* Trace this pin taking min nid into account. */
3181230130Smav		res = hdaa_audio_trace_dac(devinfo, as, i,
3182230130Smav		    ases[as].pins[i], hpredir, min, 0, 0);
3183230130Smav		if (res == 0) {
3184230130Smav			/* If we failed - return to previous and redo it. */
3185230130Smav			HDA_BOOTVERBOSE(
3186230130Smav				device_printf(devinfo->dev,
3187230130Smav				    " Unable to trace pin %d seq %d with min "
3188230130Smav				    "nid %d",
3189230130Smav				    ases[as].pins[i], i, min);
3190230130Smav				if (hpredir >= 0)
3191230130Smav					printf(" and hpredir %d", hpredir);
3192230130Smav				printf("\n");
3193230130Smav			);
3194230130Smav			return (0);
3195230130Smav		}
3196230130Smav		HDA_BOOTVERBOSE(
3197230130Smav			device_printf(devinfo->dev,
3198230130Smav			    " Pin %d traced to DAC %d",
3199230130Smav			    ases[as].pins[i], res);
3200230130Smav			if (hpredir >= 0)
3201230130Smav				printf(" and hpredir %d", hpredir);
3202230130Smav			if (ases[as].fakeredir)
3203230130Smav				printf(" with fake redirection");
3204230130Smav			printf("\n");
3205230130Smav		);
3206230130Smav		/* Trace again to mark the path */
3207230130Smav		hdaa_audio_trace_dac(devinfo, as, i,
3208230130Smav		    ases[as].pins[i], hpredir, min, res, 0);
3209230130Smav		ases[as].dacs[0][i] = res;
3210230130Smav		/* We succeeded, so call next. */
3211230130Smav		if (hdaa_audio_trace_as_out(devinfo, as, i + 1))
3212230130Smav			return (1);
3213230130Smav		/* If next failed, we should retry with next min */
3214230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3215230130Smav		ases[as].dacs[0][i] = 0;
3216230130Smav		min = res + 1;
3217230130Smav	} while (1);
3218230130Smav}
3219230130Smav
3220230130Smav/*
3221230130Smav * Check equivalency of two DACs.
3222230130Smav */
3223230130Smavstatic int
3224230130Smavhdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3225230130Smav{
3226230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3227230130Smav	struct hdaa_widget *w3;
3228230130Smav	int i, j, c1, c2;
3229230130Smav
3230230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3231230130Smav		return (0);
3232230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3233230130Smav		w3 = hdaa_widget_get(devinfo, i);
3234230130Smav		if (w3 == NULL || w3->enable == 0)
3235230130Smav			continue;
3236230130Smav		if (w3->bindas != w1->bindas)
3237230130Smav			continue;
3238230130Smav		if (w3->nconns == 0)
3239230130Smav			continue;
3240230130Smav		c1 = c2 = -1;
3241230130Smav		for (j = 0; j < w3->nconns; j++) {
3242230130Smav			if (w3->connsenable[j] == 0)
3243230130Smav				continue;
3244230130Smav			if (w3->conns[j] == w1->nid)
3245230130Smav				c1 = j;
3246230130Smav			if (w3->conns[j] == w2->nid)
3247230130Smav				c2 = j;
3248230130Smav		}
3249230130Smav		if (c1 < 0)
3250230130Smav			continue;
3251230130Smav		if (c2 < 0)
3252230130Smav			return (0);
3253230130Smav		if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3254230130Smav			return (0);
3255230130Smav	}
3256230130Smav	return (1);
3257230130Smav}
3258230130Smav
3259230130Smav/*
3260230130Smav * Check equivalency of two ADCs.
3261230130Smav */
3262230130Smavstatic int
3263230130Smavhdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3264230130Smav{
3265230130Smav	struct hdaa_devinfo *devinfo = w1->devinfo;
3266230130Smav	struct hdaa_widget *w3, *w4;
3267230130Smav	int i;
3268230130Smav
3269230130Smav	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3270230130Smav		return (0);
3271230130Smav	if (w1->nconns != 1 || w2->nconns != 1)
3272230130Smav		return (0);
3273230130Smav	if (w1->conns[0] == w2->conns[0])
3274230130Smav		return (1);
3275230130Smav	w3 = hdaa_widget_get(devinfo, w1->conns[0]);
3276230130Smav	if (w3 == NULL || w3->enable == 0)
3277230130Smav		return (0);
3278230130Smav	w4 = hdaa_widget_get(devinfo, w2->conns[0]);
3279230130Smav	if (w4 == NULL || w4->enable == 0)
3280230130Smav		return (0);
3281230130Smav	if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask)
3282230130Smav		return (1);
3283230130Smav	if (w4->bindas >= 0)
3284230130Smav		return (0);
3285230130Smav	if (w3->type != w4->type)
3286230130Smav		return (0);
3287230130Smav	if (memcmp(&w3->param, &w4->param, sizeof(w3->param)))
3288230130Smav		return (0);
3289230130Smav	if (w3->nconns != w4->nconns)
3290230130Smav		return (0);
3291230130Smav	for (i = 0; i < w3->nconns; i++) {
3292230130Smav		if (w3->conns[i] != w4->conns[i])
3293230130Smav			return (0);
3294230130Smav	}
3295230130Smav	return (1);
3296230130Smav}
3297230130Smav
3298230130Smav/*
3299230130Smav * Look for equivalent DAC/ADC to implement second channel.
3300230130Smav */
3301230130Smavstatic void
3302230130Smavhdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid)
3303230130Smav{
3304230130Smav	struct hdaa_audio_as *as = &devinfo->as[asid];
3305230130Smav	struct hdaa_widget *w1, *w2;
3306230130Smav	int i, pos;
3307230130Smav	nid_t nid1, nid2;
3308230130Smav
3309230130Smav	HDA_BOOTVERBOSE(
3310230130Smav		device_printf(devinfo->dev,
3311230130Smav		    "Looking for additional %sC "
3312230130Smav		    "for association %d (%d)\n",
3313230130Smav		    (as->dir == HDAA_CTL_OUT) ? "DA" : "AD",
3314230130Smav		    asid, as->index);
3315230130Smav	);
3316230130Smav
3317230130Smav	/* Find the exisitng DAC position and return if found more the one. */
3318230130Smav	pos = -1;
3319230130Smav	for (i = 0; i < 16; i++) {
3320230130Smav		if (as->dacs[0][i] <= 0)
3321230130Smav			continue;
3322230130Smav		if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos])
3323230130Smav			return;
3324230130Smav		pos = i;
3325230130Smav	}
3326230130Smav
3327230130Smav	nid1 = as->dacs[0][pos];
3328230130Smav	w1 = hdaa_widget_get(devinfo, nid1);
3329230130Smav	w2 = NULL;
3330230130Smav	for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) {
3331230130Smav		w2 = hdaa_widget_get(devinfo, nid2);
3332230130Smav		if (w2 == NULL || w2->enable == 0)
3333230130Smav			continue;
3334230130Smav		if (w2->bindas >= 0)
3335230130Smav			continue;
3336230130Smav		if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
3337230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
3338230130Smav				continue;
3339230130Smav			if (hdaa_audio_dacs_equal(w1, w2))
3340230130Smav				break;
3341230130Smav		} else {
3342230130Smav			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3343230130Smav				continue;
3344230130Smav			if (hdaa_audio_adcs_equal(w1, w2))
3345230130Smav				break;
3346230130Smav		}
3347230130Smav	}
3348230130Smav	if (nid2 >= devinfo->endnode)
3349230130Smav		return;
3350230130Smav	w2->bindas = w1->bindas;
3351230130Smav	w2->bindseqmask = w1->bindseqmask;
3352230130Smav	if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
3353230130Smav		HDA_BOOTVERBOSE(
3354230130Smav			device_printf(devinfo->dev,
3355230130Smav			    " ADC %d considered equal to ADC %d\n", nid2, nid1);
3356230130Smav		);
3357230130Smav		w1 = hdaa_widget_get(devinfo, w1->conns[0]);
3358230130Smav		w2 = hdaa_widget_get(devinfo, w2->conns[0]);
3359230130Smav		w2->bindas = w1->bindas;
3360230130Smav		w2->bindseqmask = w1->bindseqmask;
3361230130Smav	} else {
3362230130Smav		HDA_BOOTVERBOSE(
3363230130Smav			device_printf(devinfo->dev,
3364230130Smav			    " DAC %d considered equal to DAC %d\n", nid2, nid1);
3365230130Smav		);
3366230130Smav	}
3367230130Smav	for (i = 0; i < 16; i++) {
3368230130Smav		if (as->dacs[0][i] <= 0)
3369230130Smav			continue;
3370230130Smav		as->dacs[as->num_chans][i] = nid2;
3371230130Smav	}
3372230130Smav	as->num_chans++;
3373230130Smav}
3374230130Smav
3375230130Smav/*
3376230130Smav * Trace association path from input to ADC
3377230130Smav */
3378230130Smavstatic int
3379230130Smavhdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as)
3380230130Smav{
3381230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3382230130Smav	struct hdaa_widget *w;
3383230130Smav	int i, j, k, length;
3384230130Smav
3385230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3386230130Smav		w = hdaa_widget_get(devinfo, j);
3387230130Smav		if (w == NULL || w->enable == 0)
3388230130Smav			continue;
3389230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3390230130Smav			continue;
3391230130Smav		if (w->bindas >= 0 && w->bindas != as)
3392230130Smav			continue;
3393230130Smav
3394230130Smav		/* Find next pin */
3395230130Smav		for (i = 0; i < 16; i++) {
3396230130Smav			if (ases[as].pins[i] == 0)
3397230130Smav				continue;
3398230130Smav
3399230130Smav			HDA_BOOTHVERBOSE(
3400230130Smav				device_printf(devinfo->dev,
3401230130Smav				    " Tracing pin %d to ADC %d\n",
3402230130Smav				    ases[as].pins[i], j);
3403230130Smav			);
3404230130Smav			/* Trace this pin taking goal into account. */
3405230130Smav			if (hdaa_audio_trace_adc(devinfo, as, i,
3406230130Smav			    ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) {
3407230130Smav				/* If we failed - return to previous and redo it. */
3408230130Smav				HDA_BOOTVERBOSE(
3409230130Smav					device_printf(devinfo->dev,
3410230130Smav					    " Unable to trace pin %d to ADC %d, undo traces\n",
3411230130Smav					    ases[as].pins[i], j);
3412230130Smav				);
3413230130Smav				hdaa_audio_undo_trace(devinfo, as, -1);
3414230130Smav				for (k = 0; k < 16; k++)
3415230130Smav					ases[as].dacs[0][k] = 0;
3416230130Smav				break;
3417230130Smav			}
3418230130Smav			HDA_BOOTVERBOSE(
3419230130Smav				device_printf(devinfo->dev,
3420230130Smav				    " Pin %d traced to ADC %d\n",
3421230130Smav				    ases[as].pins[i], j);
3422230130Smav			);
3423230130Smav			ases[as].dacs[0][i] = j;
3424230130Smav		}
3425230130Smav		if (i == 16)
3426230130Smav			return (1);
3427230130Smav	}
3428230130Smav	return (0);
3429230130Smav}
3430230130Smav
3431230130Smav/*
3432230130Smav * Trace association path from input to multiple ADCs
3433230130Smav */
3434230130Smavstatic int
3435230130Smavhdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq)
3436230130Smav{
3437230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3438230130Smav	int i, length;
3439230130Smav	nid_t min, res;
3440230130Smav
3441230130Smav	/* Find next pin */
3442230130Smav	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3443230130Smav		;
3444230130Smav	/* Check if there is no any left. If so - we succeeded. */
3445230130Smav	if (i == 16)
3446230130Smav		return (1);
3447230130Smav
3448230130Smav	min = 0;
3449230130Smav	do {
3450230130Smav		HDA_BOOTHVERBOSE(
3451230130Smav			device_printf(devinfo->dev,
3452230130Smav			    " Tracing pin %d with min nid %d",
3453230130Smav			    ases[as].pins[i], min);
3454230130Smav			printf("\n");
3455230130Smav		);
3456230130Smav		/* Trace this pin taking min nid into account. */
3457230130Smav		res = hdaa_audio_trace_adc(devinfo, as, i,
3458230130Smav		    ases[as].pins[i], 0, min, 0, 0, &length, 0);
3459230130Smav		if (res == 0) {
3460230130Smav			/* If we failed - return to previous and redo it. */
3461230130Smav			HDA_BOOTVERBOSE(
3462230130Smav				device_printf(devinfo->dev,
3463230130Smav				    " Unable to trace pin %d seq %d with min "
3464230130Smav				    "nid %d",
3465230130Smav				    ases[as].pins[i], i, min);
3466230130Smav				printf("\n");
3467230130Smav			);
3468230130Smav			return (0);
3469230130Smav		}
3470230130Smav		HDA_BOOTVERBOSE(
3471230130Smav			device_printf(devinfo->dev,
3472230130Smav			    " Pin %d traced to ADC %d\n",
3473230130Smav			    ases[as].pins[i], res);
3474230130Smav		);
3475230130Smav		/* Trace again to mark the path */
3476230130Smav		hdaa_audio_trace_adc(devinfo, as, i,
3477230130Smav		    ases[as].pins[i], 0, min, res, 0, &length, length);
3478230130Smav		ases[as].dacs[0][i] = res;
3479230130Smav		/* We succeeded, so call next. */
3480230130Smav		if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1))
3481230130Smav			return (1);
3482230130Smav		/* If next failed, we should retry with next min */
3483230130Smav		hdaa_audio_undo_trace(devinfo, as, i);
3484230130Smav		ases[as].dacs[0][i] = 0;
3485230130Smav		min = res + 1;
3486230130Smav	} while (1);
3487230130Smav}
3488230130Smav
3489230130Smav/*
3490230130Smav * Trace input monitor path from mixer to output association.
3491230130Smav */
3492230130Smavstatic int
3493230130Smavhdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth)
3494230130Smav{
3495230130Smav	struct hdaa_audio_as *ases = devinfo->as;
3496230130Smav	struct hdaa_widget *w, *wc;
3497230130Smav	int i, j;
3498230130Smav	nid_t res = 0;
3499230130Smav
3500230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
3501230130Smav		return (0);
3502230130Smav	w = hdaa_widget_get(devinfo, nid);
3503230130Smav	if (w == NULL || w->enable == 0)
3504230130Smav		return (0);
3505230130Smav	HDA_BOOTHVERBOSE(
3506230130Smav		device_printf(devinfo->dev,
3507230130Smav		    " %*stracing via nid %d\n",
3508230130Smav			depth + 1, "", w->nid);
3509230130Smav	);
3510230130Smav	/* Use only unused widgets */
3511230130Smav	if (depth > 0 && w->bindas != -1) {
3512230130Smav		if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) {
3513230130Smav			HDA_BOOTHVERBOSE(
3514230130Smav				device_printf(devinfo->dev,
3515230130Smav				    " %*snid %d found output association %d\n",
3516230130Smav					depth + 1, "", w->nid, w->bindas);
3517230130Smav			);
3518230130Smav			if (w->bindas >= 0)
3519230130Smav				w->pflags |= HDAA_ADC_MONITOR;
3520230130Smav			return (1);
3521230130Smav		} else {
3522230130Smav			HDA_BOOTHVERBOSE(
3523230130Smav				device_printf(devinfo->dev,
3524230130Smav				    " %*snid %d busy by input association %d\n",
3525230130Smav					depth + 1, "", w->nid, w->bindas);
3526230130Smav			);
3527230130Smav			return (0);
3528230130Smav		}
3529230130Smav	}
3530230130Smav
3531230130Smav	switch (w->type) {
3532230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3533230130Smav		/* Do not traverse input. AD1988 has digital monitor
3534230130Smav		for which we are not ready. */
3535230130Smav		break;
3536230130Smav	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3537230130Smav		if (depth > 0)
3538230130Smav			break;
3539230130Smav		/* Fall */
3540230130Smav	default:
3541230130Smav		/* Try to find reachable ADCs with specified nid. */
3542230130Smav		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3543230130Smav			wc = hdaa_widget_get(devinfo, j);
3544230130Smav			if (wc == NULL || wc->enable == 0)
3545230130Smav				continue;
3546230130Smav			for (i = 0; i < wc->nconns; i++) {
3547230130Smav				if (wc->connsenable[i] == 0)
3548230130Smav					continue;
3549230130Smav				if (wc->conns[i] != nid)
3550230130Smav					continue;
3551230130Smav				if (hdaa_audio_trace_to_out(devinfo,
3552230130Smav				    j, depth + 1) != 0) {
3553230130Smav					res = 1;
3554230130Smav					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3555230130Smav					    wc->selconn == -1)
3556230130Smav						wc->selconn = i;
3557230130Smav				}
3558230130Smav			}
3559230130Smav		}
3560230130Smav		break;
3561230130Smav	}
3562230130Smav	if (res && w->bindas == -1)
3563230130Smav		w->bindas = -2;
3564230130Smav
3565230130Smav	HDA_BOOTHVERBOSE(
3566230130Smav		device_printf(devinfo->dev,
3567230130Smav		    " %*snid %d returned %d\n",
3568230130Smav			depth + 1, "", w->nid, res);
3569230130Smav	);
3570230130Smav	return (res);
3571230130Smav}
3572230130Smav
3573230130Smav/*
3574230130Smav * Trace extra associations (beeper, monitor)
3575230130Smav */
3576230130Smavstatic void
3577230130Smavhdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo)
3578230130Smav{
3579230130Smav	struct hdaa_audio_as *as = devinfo->as;
3580230130Smav	struct hdaa_widget *w;
3581230130Smav	int j;
3582230130Smav
3583230130Smav	/* Input monitor */
3584230130Smav	/* Find mixer associated with input, but supplying signal
3585230130Smav	   for output associations. Hope it will be input monitor. */
3586230130Smav	HDA_BOOTVERBOSE(
3587230130Smav		device_printf(devinfo->dev,
3588230130Smav		    "Tracing input monitor\n");
3589230130Smav	);
3590230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3591230130Smav		w = hdaa_widget_get(devinfo, j);
3592230130Smav		if (w == NULL || w->enable == 0)
3593230130Smav			continue;
3594230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3595230130Smav			continue;
3596230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3597230130Smav			continue;
3598230130Smav		HDA_BOOTVERBOSE(
3599230130Smav			device_printf(devinfo->dev,
3600230130Smav			    " Tracing nid %d to out\n",
3601230130Smav			    j);
3602230130Smav		);
3603230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3604230130Smav			HDA_BOOTVERBOSE(
3605230130Smav				device_printf(devinfo->dev,
3606230130Smav				    " nid %d is input monitor\n",
3607230130Smav					w->nid);
3608230130Smav			);
3609230130Smav			w->ossdev = SOUND_MIXER_IMIX;
3610230130Smav		}
3611230130Smav	}
3612230130Smav
3613230130Smav	/* Other inputs monitor */
3614230130Smav	/* Find input pins supplying signal for output associations.
3615230130Smav	   Hope it will be input monitoring. */
3616230130Smav	HDA_BOOTVERBOSE(
3617230130Smav		device_printf(devinfo->dev,
3618230130Smav		    "Tracing other input monitors\n");
3619230130Smav	);
3620230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3621230130Smav		w = hdaa_widget_get(devinfo, j);
3622230130Smav		if (w == NULL || w->enable == 0)
3623230130Smav			continue;
3624230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3625230130Smav			continue;
3626230130Smav		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3627230130Smav			continue;
3628230130Smav		HDA_BOOTVERBOSE(
3629230130Smav			device_printf(devinfo->dev,
3630230130Smav			    " Tracing nid %d to out\n",
3631230130Smav			    j);
3632230130Smav		);
3633230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3634230130Smav			HDA_BOOTVERBOSE(
3635230130Smav				device_printf(devinfo->dev,
3636230130Smav				    " nid %d is input monitor\n",
3637230130Smav					w->nid);
3638230130Smav			);
3639230130Smav		}
3640230130Smav	}
3641230130Smav
3642230130Smav	/* Beeper */
3643230130Smav	HDA_BOOTVERBOSE(
3644230130Smav		device_printf(devinfo->dev,
3645230130Smav		    "Tracing beeper\n");
3646230130Smav	);
3647230130Smav	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3648230130Smav		w = hdaa_widget_get(devinfo, j);
3649230130Smav		if (w == NULL || w->enable == 0)
3650230130Smav			continue;
3651230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
3652230130Smav			continue;
3653230130Smav		HDA_BOOTHVERBOSE(
3654230130Smav			device_printf(devinfo->dev,
3655230130Smav			    " Tracing nid %d to out\n",
3656230130Smav			    j);
3657230130Smav		);
3658230130Smav		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3659230130Smav			HDA_BOOTVERBOSE(
3660230130Smav				device_printf(devinfo->dev,
3661230130Smav				    " nid %d traced to out\n",
3662230130Smav				    j);
3663230130Smav			);
3664230130Smav		}
3665230130Smav		w->bindas = -2;
3666230130Smav	}
3667230130Smav}
3668230130Smav
3669230130Smav/*
3670230130Smav * Bind assotiations to PCM channels
3671230130Smav */
3672230130Smavstatic void
3673230130Smavhdaa_audio_bind_as(struct hdaa_devinfo *devinfo)
3674230130Smav{
3675230130Smav	struct hdaa_audio_as *as = devinfo->as;
3676230130Smav	int i, j, cnt = 0, free;
3677230130Smav
3678230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
3679230130Smav		if (as[j].enable)
3680230130Smav			cnt += as[j].num_chans;
3681230130Smav	}
3682230130Smav	if (devinfo->num_chans == 0) {
3683230130Smav		devinfo->chans = (struct hdaa_chan *)malloc(
3684230130Smav		    sizeof(struct hdaa_chan) * cnt,
3685230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
3686230130Smav		if (devinfo->chans == NULL) {
3687230130Smav			device_printf(devinfo->dev,
3688230130Smav			    "Channels memory allocation failed!\n");
3689230130Smav			return;
3690230130Smav		}
3691230130Smav	} else {
3692230130Smav		devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans,
3693230130Smav		    sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt),
3694230130Smav		    M_HDAA, M_ZERO | M_NOWAIT);
3695230130Smav		if (devinfo->chans == NULL) {
3696230130Smav			devinfo->num_chans = 0;
3697230130Smav			device_printf(devinfo->dev,
3698230130Smav			    "Channels memory allocation failed!\n");
3699230130Smav			return;
3700230130Smav		}
3701230130Smav		/* Fixup relative pointers after realloc */
3702230130Smav		for (j = 0; j < devinfo->num_chans; j++)
3703230130Smav			devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist;
3704230130Smav	}
3705230130Smav	free = devinfo->num_chans;
3706230130Smav	devinfo->num_chans += cnt;
3707230130Smav
3708230130Smav	for (j = free; j < free + cnt; j++) {
3709230130Smav		devinfo->chans[j].devinfo = devinfo;
3710230130Smav		devinfo->chans[j].as = -1;
3711230130Smav	}
3712230130Smav
3713230130Smav	/* Assign associations in order of their numbers, */
3714230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
3715230130Smav		if (as[j].enable == 0)
3716230130Smav			continue;
3717230130Smav		for (i = 0; i < as[j].num_chans; i++) {
3718230130Smav			devinfo->chans[free].as = j;
3719230130Smav			devinfo->chans[free].asindex = i;
3720230130Smav			devinfo->chans[free].dir =
3721230130Smav			    (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
3722230130Smav			hdaa_pcmchannel_setup(&devinfo->chans[free]);
3723230130Smav			as[j].chans[i] = free;
3724230130Smav			free++;
3725230130Smav		}
3726230130Smav	}
3727230130Smav}
3728230130Smav
3729230130Smavstatic void
3730230130Smavhdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo)
3731230130Smav{
3732230130Smav	struct hdaa_widget *w;
3733230130Smav	int i;
3734230130Smav
3735230130Smav	/* Disable power and volume widgets. */
3736230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3737230130Smav		w = hdaa_widget_get(devinfo, i);
3738230130Smav		if (w == NULL || w->enable == 0)
3739230130Smav			continue;
3740230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
3741230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
3742230130Smav			w->enable = 0;
3743230130Smav			HDA_BOOTHVERBOSE(
3744230130Smav				device_printf(devinfo->dev,
3745230130Smav				    " Disabling nid %d due to it's"
3746230130Smav				    " non-audio type.\n",
3747230130Smav				    w->nid);
3748230130Smav			);
3749230130Smav		}
3750230130Smav	}
3751230130Smav}
3752230130Smav
3753230130Smavstatic void
3754230130Smavhdaa_audio_disable_useless(struct hdaa_devinfo *devinfo)
3755230130Smav{
3756230130Smav	struct hdaa_widget *w, *cw;
3757230130Smav	struct hdaa_audio_ctl *ctl;
3758230130Smav	int done, found, i, j, k;
3759230130Smav
3760230130Smav	/* Disable useless pins. */
3761230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3762230130Smav		w = hdaa_widget_get(devinfo, i);
3763230130Smav		if (w == NULL || w->enable == 0)
3764230130Smav			continue;
3765230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
3766230130Smav			if ((w->wclass.pin.config &
3767230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
3768230130Smav			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
3769230130Smav				w->enable = 0;
3770230130Smav				HDA_BOOTHVERBOSE(
3771230130Smav					device_printf(devinfo->dev,
3772230130Smav					    " Disabling pin nid %d due"
3773230130Smav					    " to None connectivity.\n",
3774230130Smav					    w->nid);
3775230130Smav				);
3776230130Smav			} else if ((w->wclass.pin.config &
3777230130Smav			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
3778230130Smav				w->enable = 0;
3779230130Smav				HDA_BOOTHVERBOSE(
3780230130Smav					device_printf(devinfo->dev,
3781230130Smav					    " Disabling unassociated"
3782230130Smav					    " pin nid %d.\n",
3783230130Smav					    w->nid);
3784230130Smav				);
3785230130Smav			}
3786230130Smav		}
3787230130Smav	}
3788230130Smav	do {
3789230130Smav		done = 1;
3790230130Smav		/* Disable and mute controls for disabled widgets. */
3791230130Smav		i = 0;
3792230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
3793230130Smav			if (ctl->enable == 0)
3794230130Smav				continue;
3795230130Smav			if (ctl->widget->enable == 0 ||
3796230130Smav			    (ctl->childwidget != NULL &&
3797230130Smav			    ctl->childwidget->enable == 0)) {
3798230130Smav				ctl->forcemute = 1;
3799230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
3800230130Smav				ctl->left = 0;
3801230130Smav				ctl->right = 0;
3802230130Smav				ctl->enable = 0;
3803230130Smav				if (ctl->ndir == HDAA_CTL_IN)
3804230130Smav					ctl->widget->connsenable[ctl->index] = 0;
3805230130Smav				done = 0;
3806230130Smav				HDA_BOOTHVERBOSE(
3807230130Smav					device_printf(devinfo->dev,
3808230130Smav					    " Disabling ctl %d nid %d cnid %d due"
3809230130Smav					    " to disabled widget.\n", i,
3810230130Smav					    ctl->widget->nid,
3811230130Smav					    (ctl->childwidget != NULL)?
3812230130Smav					    ctl->childwidget->nid:-1);
3813230130Smav				);
3814230130Smav			}
3815230130Smav		}
3816230130Smav		/* Disable useless widgets. */
3817230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3818230130Smav			w = hdaa_widget_get(devinfo, i);
3819230130Smav			if (w == NULL || w->enable == 0)
3820230130Smav				continue;
3821230130Smav			/* Disable inputs with disabled child widgets. */
3822230130Smav			for (j = 0; j < w->nconns; j++) {
3823230130Smav				if (w->connsenable[j]) {
3824230130Smav					cw = hdaa_widget_get(devinfo, w->conns[j]);
3825230130Smav					if (cw == NULL || cw->enable == 0) {
3826230130Smav						w->connsenable[j] = 0;
3827230130Smav						HDA_BOOTHVERBOSE(
3828230130Smav							device_printf(devinfo->dev,
3829230130Smav							    " Disabling nid %d connection %d due"
3830230130Smav							    " to disabled child widget.\n",
3831230130Smav							    i, j);
3832230130Smav						);
3833230130Smav					}
3834230130Smav				}
3835230130Smav			}
3836230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3837230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3838230130Smav				continue;
3839230130Smav			/* Disable mixers and selectors without inputs. */
3840230130Smav			found = 0;
3841230130Smav			for (j = 0; j < w->nconns; j++) {
3842230130Smav				if (w->connsenable[j]) {
3843230130Smav					found = 1;
3844230130Smav					break;
3845230130Smav				}
3846230130Smav			}
3847230130Smav			if (found == 0) {
3848230130Smav				w->enable = 0;
3849230130Smav				done = 0;
3850230130Smav				HDA_BOOTHVERBOSE(
3851230130Smav					device_printf(devinfo->dev,
3852230130Smav					    " Disabling nid %d due to all it's"
3853230130Smav					    " inputs disabled.\n", w->nid);
3854230130Smav				);
3855230130Smav			}
3856230130Smav			/* Disable nodes without consumers. */
3857230130Smav			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3858230130Smav			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3859230130Smav				continue;
3860230130Smav			found = 0;
3861230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
3862230130Smav				cw = hdaa_widget_get(devinfo, k);
3863230130Smav				if (cw == NULL || cw->enable == 0)
3864230130Smav					continue;
3865230130Smav				for (j = 0; j < cw->nconns; j++) {
3866230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
3867230130Smav						found = 1;
3868230130Smav						break;
3869230130Smav					}
3870230130Smav				}
3871230130Smav			}
3872230130Smav			if (found == 0) {
3873230130Smav				w->enable = 0;
3874230130Smav				done = 0;
3875230130Smav				HDA_BOOTHVERBOSE(
3876230130Smav					device_printf(devinfo->dev,
3877230130Smav					    " Disabling nid %d due to all it's"
3878230130Smav					    " consumers disabled.\n", w->nid);
3879230130Smav				);
3880230130Smav			}
3881230130Smav		}
3882230130Smav	} while (done == 0);
3883230130Smav
3884230130Smav}
3885230130Smav
3886230130Smavstatic void
3887230130Smavhdaa_audio_disable_unas(struct hdaa_devinfo *devinfo)
3888230130Smav{
3889230130Smav	struct hdaa_audio_as *as = devinfo->as;
3890230130Smav	struct hdaa_widget *w, *cw;
3891230130Smav	struct hdaa_audio_ctl *ctl;
3892230130Smav	int i, j, k;
3893230130Smav
3894230130Smav	/* Disable unassosiated widgets. */
3895230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3896230130Smav		w = hdaa_widget_get(devinfo, i);
3897230130Smav		if (w == NULL || w->enable == 0)
3898230130Smav			continue;
3899230130Smav		if (w->bindas == -1) {
3900230130Smav			w->enable = 0;
3901230130Smav			HDA_BOOTHVERBOSE(
3902230130Smav				device_printf(devinfo->dev,
3903230130Smav				    " Disabling unassociated nid %d.\n",
3904230130Smav				    w->nid);
3905230130Smav			);
3906230130Smav		}
3907230130Smav	}
3908230130Smav	/* Disable input connections on input pin and
3909230130Smav	 * output on output. */
3910230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3911230130Smav		w = hdaa_widget_get(devinfo, i);
3912230130Smav		if (w == NULL || w->enable == 0)
3913230130Smav			continue;
3914230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3915230130Smav			continue;
3916230130Smav		if (w->bindas < 0)
3917230130Smav			continue;
3918230130Smav		if (as[w->bindas].dir == HDAA_CTL_IN) {
3919230130Smav			for (j = 0; j < w->nconns; j++) {
3920230130Smav				if (w->connsenable[j] == 0)
3921230130Smav					continue;
3922230130Smav				w->connsenable[j] = 0;
3923230130Smav				HDA_BOOTHVERBOSE(
3924230130Smav					device_printf(devinfo->dev,
3925230130Smav					    " Disabling connection to input pin "
3926230130Smav					    "nid %d conn %d.\n",
3927230130Smav					    i, j);
3928230130Smav				);
3929230130Smav			}
3930230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
3931230130Smav			    HDAA_CTL_IN, -1, 1);
3932230130Smav			if (ctl && ctl->enable) {
3933230130Smav				ctl->forcemute = 1;
3934230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
3935230130Smav				ctl->left = 0;
3936230130Smav				ctl->right = 0;
3937230130Smav				ctl->enable = 0;
3938230130Smav			}
3939230130Smav		} else {
3940230130Smav			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
3941230130Smav			    HDAA_CTL_OUT, -1, 1);
3942230130Smav			if (ctl && ctl->enable) {
3943230130Smav				ctl->forcemute = 1;
3944230130Smav				ctl->muted = HDAA_AMP_MUTE_ALL;
3945230130Smav				ctl->left = 0;
3946230130Smav				ctl->right = 0;
3947230130Smav				ctl->enable = 0;
3948230130Smav			}
3949230130Smav			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
3950230130Smav				cw = hdaa_widget_get(devinfo, k);
3951230130Smav				if (cw == NULL || cw->enable == 0)
3952230130Smav					continue;
3953230130Smav				for (j = 0; j < cw->nconns; j++) {
3954230130Smav					if (cw->connsenable[j] && cw->conns[j] == i) {
3955230130Smav						cw->connsenable[j] = 0;
3956230130Smav						HDA_BOOTHVERBOSE(
3957230130Smav							device_printf(devinfo->dev,
3958230130Smav							    " Disabling connection from output pin "
3959230130Smav							    "nid %d conn %d cnid %d.\n",
3960230130Smav							    k, j, i);
3961230130Smav						);
3962230130Smav						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
3963230130Smav						    cw->nconns > 1)
3964230130Smav							continue;
3965230130Smav						ctl = hdaa_audio_ctl_amp_get(devinfo, k,
3966230130Smav					    HDAA_CTL_IN, j, 1);
3967230130Smav						if (ctl && ctl->enable) {
3968230130Smav							ctl->forcemute = 1;
3969230130Smav							ctl->muted = HDAA_AMP_MUTE_ALL;
3970230130Smav							ctl->left = 0;
3971230130Smav							ctl->right = 0;
3972230130Smav							ctl->enable = 0;
3973230130Smav						}
3974230130Smav					}
3975230130Smav				}
3976230130Smav			}
3977230130Smav		}
3978230130Smav	}
3979230130Smav}
3980230130Smav
3981230130Smavstatic void
3982230130Smavhdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo)
3983230130Smav{
3984230130Smav	struct hdaa_audio_as *as = devinfo->as;
3985230130Smav	struct hdaa_widget *w;
3986230130Smav	int i, j;
3987230130Smav
3988230130Smav	/* On playback path we can safely disable all unseleted inputs. */
3989230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3990230130Smav		w = hdaa_widget_get(devinfo, i);
3991230130Smav		if (w == NULL || w->enable == 0)
3992230130Smav			continue;
3993230130Smav		if (w->nconns <= 1)
3994230130Smav			continue;
3995230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3996230130Smav			continue;
3997230130Smav		if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN)
3998230130Smav			continue;
3999230130Smav		for (j = 0; j < w->nconns; j++) {
4000230130Smav			if (w->connsenable[j] == 0)
4001230130Smav				continue;
4002230130Smav			if (w->selconn < 0 || w->selconn == j)
4003230130Smav				continue;
4004230130Smav			w->connsenable[j] = 0;
4005230130Smav			HDA_BOOTHVERBOSE(
4006230130Smav				device_printf(devinfo->dev,
4007230130Smav				    " Disabling unselected connection "
4008230130Smav				    "nid %d conn %d.\n",
4009230130Smav				    i, j);
4010230130Smav			);
4011230130Smav		}
4012230130Smav	}
4013230130Smav}
4014230130Smav
4015230130Smavstatic void
4016230130Smavhdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo)
4017230130Smav{
4018230130Smav	struct hdaa_audio_as *ases = devinfo->as;
4019230130Smav	struct hdaa_widget *w, *cw;
4020230130Smav	struct hdaa_audio_ctl *ctl;
4021230130Smav	int i, j;
4022230130Smav
4023230130Smav	/* Disable crossassociatement and unwanted crosschannel connections. */
4024230130Smav	/* ... using selectors */
4025230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4026230130Smav		w = hdaa_widget_get(devinfo, i);
4027230130Smav		if (w == NULL || w->enable == 0)
4028230130Smav			continue;
4029230130Smav		if (w->nconns <= 1)
4030230130Smav			continue;
4031230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4032230130Smav			continue;
4033230130Smav		/* Allow any -> mix */
4034230130Smav		if (w->bindas == -2)
4035230130Smav			continue;
4036230130Smav		for (j = 0; j < w->nconns; j++) {
4037230130Smav			if (w->connsenable[j] == 0)
4038230130Smav				continue;
4039230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
4040230130Smav			if (cw == NULL || w->enable == 0)
4041230130Smav				continue;
4042230130Smav			/* Allow mix -> out. */
4043230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
4044230130Smav			    ases[w->bindas].dir == HDAA_CTL_OUT)
4045230130Smav				continue;
4046230130Smav			/* Allow mix -> mixed-in. */
4047230130Smav			if (cw->bindas == -2 && w->bindas >= 0 &&
4048230130Smav			    ases[w->bindas].mixed)
4049230130Smav				continue;
4050230130Smav			/* Allow in -> mix. */
4051230130Smav			if ((w->pflags & HDAA_ADC_MONITOR) &&
4052230130Smav			     cw->bindas >= 0 &&
4053230130Smav			     ases[cw->bindas].dir == HDAA_CTL_IN)
4054230130Smav				continue;
4055230130Smav			/* Allow if have common as/seqs. */
4056230130Smav			if (w->bindas == cw->bindas &&
4057230130Smav			    (w->bindseqmask & cw->bindseqmask) != 0)
4058230130Smav				continue;
4059230130Smav			w->connsenable[j] = 0;
4060230130Smav			HDA_BOOTHVERBOSE(
4061230130Smav				device_printf(devinfo->dev,
4062230130Smav				    " Disabling crossassociatement connection "
4063230130Smav				    "nid %d conn %d cnid %d.\n",
4064230130Smav				    i, j, cw->nid);
4065230130Smav			);
4066230130Smav		}
4067230130Smav	}
4068230130Smav	/* ... using controls */
4069230130Smav	i = 0;
4070230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4071230130Smav		if (ctl->enable == 0 || ctl->childwidget == NULL)
4072230130Smav			continue;
4073230130Smav		/* Allow any -> mix */
4074230130Smav		if (ctl->widget->bindas == -2)
4075230130Smav			continue;
4076230130Smav		/* Allow mix -> out. */
4077230130Smav		if (ctl->childwidget->bindas == -2 &&
4078230130Smav		    ctl->widget->bindas >= 0 &&
4079230130Smav		    ases[ctl->widget->bindas].dir == HDAA_CTL_OUT)
4080230130Smav			continue;
4081230130Smav		/* Allow mix -> mixed-in. */
4082230130Smav		if (ctl->childwidget->bindas == -2 &&
4083230130Smav		    ctl->widget->bindas >= 0 &&
4084230130Smav		    ases[ctl->widget->bindas].mixed)
4085230130Smav			continue;
4086230130Smav		/* Allow in -> mix. */
4087230130Smav		if ((ctl->widget->pflags & HDAA_ADC_MONITOR) &&
4088230130Smav		    ctl->childwidget->bindas >= 0 &&
4089230130Smav		    ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN)
4090230130Smav			continue;
4091230130Smav		/* Allow if have common as/seqs. */
4092230130Smav		if (ctl->widget->bindas == ctl->childwidget->bindas &&
4093230130Smav		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0)
4094230130Smav			continue;
4095230130Smav		ctl->forcemute = 1;
4096230130Smav		ctl->muted = HDAA_AMP_MUTE_ALL;
4097230130Smav		ctl->left = 0;
4098230130Smav		ctl->right = 0;
4099230130Smav		ctl->enable = 0;
4100230130Smav		if (ctl->ndir == HDAA_CTL_IN)
4101230130Smav			ctl->widget->connsenable[ctl->index] = 0;
4102230130Smav		HDA_BOOTHVERBOSE(
4103230130Smav			device_printf(devinfo->dev,
4104230130Smav			    " Disabling crossassociatement connection "
4105230130Smav			    "ctl %d nid %d cnid %d.\n", i,
4106230130Smav			    ctl->widget->nid,
4107230130Smav			    ctl->childwidget->nid);
4108230130Smav		);
4109230130Smav	}
4110230130Smav
4111230130Smav}
4112230130Smav
4113230130Smav/*
4114230451Smav * Find controls to control amplification for source and calculate possible
4115230451Smav * amplification range.
4116230130Smav */
4117230130Smavstatic int
4118230130Smavhdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4119230451Smav    int ossdev, int ctlable, int depth, int *minamp, int *maxamp)
4120230130Smav{
4121230130Smav	struct hdaa_widget *w, *wc;
4122230130Smav	struct hdaa_audio_ctl *ctl;
4123230451Smav	int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4124230130Smav
4125230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4126230451Smav		return (found);
4127230130Smav
4128230130Smav	w = hdaa_widget_get(devinfo, nid);
4129230130Smav	if (w == NULL || w->enable == 0)
4130230451Smav		return (found);
4131230130Smav
4132230130Smav	/* Count number of active inputs. */
4133230130Smav	if (depth > 0) {
4134230130Smav		for (j = 0; j < w->nconns; j++) {
4135230451Smav			if (!w->connsenable[j])
4136230451Smav				continue;
4137230451Smav			conns++;
4138230130Smav		}
4139230130Smav	}
4140230130Smav
4141230130Smav	/* If this is not a first step - use input mixer.
4142230130Smav	   Pins have common input ctl so care must be taken. */
4143230130Smav	if (depth > 0 && ctlable && (conns == 1 ||
4144230130Smav	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
4145230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
4146230130Smav		    index, 1);
4147230130Smav		if (ctl) {
4148230451Smav			ctl->ossmask |= (1 << ossdev);
4149230451Smav			found++;
4150230451Smav			if (*minamp == *maxamp) {
4151230451Smav				*minamp += MINQDB(ctl);
4152230451Smav				*maxamp += MAXQDB(ctl);
4153230451Smav			}
4154230130Smav		}
4155230130Smav	}
4156230130Smav
4157230130Smav	/* If widget has own ossdev - not traverse it.
4158230130Smav	   It will be traversed on it's own. */
4159230130Smav	if (w->ossdev >= 0 && depth > 0)
4160230451Smav		return (found);
4161230130Smav
4162230130Smav	/* We must not traverse pin */
4163230130Smav	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4164230130Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
4165230130Smav	    depth > 0)
4166230451Smav		return (found);
4167230130Smav
4168230130Smav	/* record that this widget exports such signal, */
4169230130Smav	w->ossmask |= (1 << ossdev);
4170230130Smav
4171230451Smav	/*
4172230451Smav	 * If signals mixed, we can't assign controls farther.
4173230130Smav	 * Ignore this on depth zero. Caller must knows why.
4174230130Smav	 */
4175230451Smav	if (conns > 1 &&
4176230451Smav	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4177230130Smav		ctlable = 0;
4178230130Smav
4179230130Smav	if (ctlable) {
4180230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
4181230130Smav		if (ctl) {
4182230451Smav			ctl->ossmask |= (1 << ossdev);
4183230451Smav			found++;
4184230451Smav			if (*minamp == *maxamp) {
4185230451Smav				*minamp += MINQDB(ctl);
4186230451Smav				*maxamp += MAXQDB(ctl);
4187230451Smav			}
4188230130Smav		}
4189230130Smav	}
4190230130Smav
4191230451Smav	cminamp = cmaxamp = 0;
4192230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4193230130Smav		wc = hdaa_widget_get(devinfo, i);
4194230130Smav		if (wc == NULL || wc->enable == 0)
4195230130Smav			continue;
4196230130Smav		for (j = 0; j < wc->nconns; j++) {
4197230130Smav			if (wc->connsenable[j] && wc->conns[j] == nid) {
4198230451Smav				tminamp = tmaxamp = 0;
4199230451Smav				found += hdaa_audio_ctl_source_amp(devinfo,
4200230451Smav				    wc->nid, j, ossdev, ctlable, depth + 1,
4201230451Smav				    &tminamp, &tmaxamp);
4202230451Smav				if (cminamp == 0 && cmaxamp == 0) {
4203230451Smav					cminamp = tminamp;
4204230451Smav					cmaxamp = tmaxamp;
4205230451Smav				} else if (tminamp != tmaxamp) {
4206230451Smav					cminamp = imax(cminamp, tminamp);
4207230451Smav					cmaxamp = imin(cmaxamp, tmaxamp);
4208230451Smav				}
4209230130Smav			}
4210230130Smav		}
4211230130Smav	}
4212230451Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4213230451Smav		*minamp += cminamp;
4214230451Smav		*maxamp += cmaxamp;
4215230451Smav	}
4216230451Smav	return (found);
4217230130Smav}
4218230130Smav
4219230130Smav/*
4220230451Smav * Find controls to control amplification for destination and calculate
4221230451Smav * possible amplification range.
4222230130Smav */
4223230451Smavstatic int
4224230130Smavhdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4225230451Smav    int ossdev, int depth, int *minamp, int *maxamp)
4226230130Smav{
4227230130Smav	struct hdaa_audio_as *as = devinfo->as;
4228230130Smav	struct hdaa_widget *w, *wc;
4229230130Smav	struct hdaa_audio_ctl *ctl;
4230230451Smav	int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4231230130Smav
4232230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
4233230451Smav		return (found);
4234230130Smav
4235230130Smav	w = hdaa_widget_get(devinfo, nid);
4236230130Smav	if (w == NULL || w->enable == 0)
4237230451Smav		return (found);
4238230130Smav
4239230130Smav	if (depth > 0) {
4240230130Smav		/* If this node produce output for several consumers,
4241230130Smav		   we can't touch it. */
4242230130Smav		consumers = 0;
4243230130Smav		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4244230130Smav			wc = hdaa_widget_get(devinfo, i);
4245230130Smav			if (wc == NULL || wc->enable == 0)
4246230130Smav				continue;
4247230130Smav			for (j = 0; j < wc->nconns; j++) {
4248230130Smav				if (wc->connsenable[j] && wc->conns[j] == nid)
4249230130Smav					consumers++;
4250230130Smav			}
4251230130Smav		}
4252230130Smav		/* The only exception is if real HP redirection is configured
4253230130Smav		   and this is a duplication point.
4254230130Smav		   XXX: Actually exception is not completely correct.
4255230130Smav		   XXX: Duplication point check is not perfect. */
4256230130Smav		if ((consumers == 2 && (w->bindas < 0 ||
4257230130Smav		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
4258230130Smav		    (w->bindseqmask & (1 << 15)) == 0)) ||
4259230130Smav		    consumers > 2)
4260230451Smav			return (found);
4261230130Smav
4262230130Smav		/* Else use it's output mixer. */
4263230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4264230130Smav		    HDAA_CTL_OUT, -1, 1);
4265230130Smav		if (ctl) {
4266230451Smav			ctl->ossmask |= (1 << ossdev);
4267230451Smav			found++;
4268230451Smav			if (*minamp == *maxamp) {
4269230451Smav				*minamp += MINQDB(ctl);
4270230451Smav				*maxamp += MAXQDB(ctl);
4271230451Smav			}
4272230130Smav		}
4273230130Smav	}
4274230130Smav
4275230130Smav	/* We must not traverse pin */
4276230130Smav	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4277230130Smav	    depth > 0)
4278230451Smav		return (found);
4279230130Smav
4280230451Smav	cminamp = cmaxamp = 0;
4281230130Smav	for (i = 0; i < w->nconns; i++) {
4282230130Smav		if (w->connsenable[i] == 0)
4283230130Smav			continue;
4284230130Smav		if (index >= 0 && i != index)
4285230130Smav			continue;
4286230451Smav		tminamp = tmaxamp = 0;
4287230130Smav		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4288230130Smav		    HDAA_CTL_IN, i, 1);
4289230130Smav		if (ctl) {
4290230451Smav			ctl->ossmask |= (1 << ossdev);
4291230451Smav			found++;
4292230451Smav			if (*minamp == *maxamp) {
4293230451Smav				tminamp += MINQDB(ctl);
4294230451Smav				tmaxamp += MAXQDB(ctl);
4295230451Smav			}
4296230130Smav		}
4297230451Smav		found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
4298230451Smav		    depth + 1, &tminamp, &tmaxamp);
4299230451Smav		if (cminamp == 0 && cmaxamp == 0) {
4300230451Smav			cminamp = tminamp;
4301230451Smav			cmaxamp = tmaxamp;
4302230451Smav		} else if (tminamp != tmaxamp) {
4303230451Smav			cminamp = imax(cminamp, tminamp);
4304230451Smav			cmaxamp = imin(cmaxamp, tmaxamp);
4305230451Smav		}
4306230130Smav	}
4307230451Smav	if (*minamp == *maxamp && cminamp < cmaxamp) {
4308230451Smav		*minamp += cminamp;
4309230451Smav		*maxamp += cmaxamp;
4310230451Smav	}
4311230451Smav	return (found);
4312230130Smav}
4313230130Smav
4314230130Smav/*
4315230130Smav * Assign OSS names to sound sources
4316230130Smav */
4317230130Smavstatic void
4318230130Smavhdaa_audio_assign_names(struct hdaa_devinfo *devinfo)
4319230130Smav{
4320230130Smav	struct hdaa_audio_as *as = devinfo->as;
4321230130Smav	struct hdaa_widget *w;
4322230130Smav	int i, j;
4323230130Smav	int type = -1, use, used = 0;
4324230130Smav	static const int types[7][13] = {
4325230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4326230130Smav	      SOUND_MIXER_LINE3, -1 },	/* line */
4327230130Smav	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
4328230130Smav	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
4329230130Smav	    { SOUND_MIXER_CD, -1 },	/* cd */
4330230130Smav	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
4331230130Smav	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
4332230130Smav	      -1 },	/* digital */
4333230130Smav	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4334230130Smav	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
4335230130Smav	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
4336230130Smav	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
4337230130Smav	      -1 }	/* others */
4338230130Smav	};
4339230130Smav
4340230130Smav	/* Surely known names */
4341230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4342230130Smav		w = hdaa_widget_get(devinfo, i);
4343230130Smav		if (w == NULL || w->enable == 0)
4344230130Smav			continue;
4345230130Smav		if (w->bindas == -1)
4346230130Smav			continue;
4347230130Smav		use = -1;
4348230130Smav		switch (w->type) {
4349230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4350230130Smav			if (as[w->bindas].dir == HDAA_CTL_OUT)
4351230130Smav				break;
4352230130Smav			type = -1;
4353230130Smav			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4354230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4355230130Smav				type = 0;
4356230130Smav				break;
4357230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4358230130Smav				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
4359230130Smav				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4360230130Smav					break;
4361230130Smav				type = 1;
4362230130Smav				break;
4363230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4364230130Smav				type = 3;
4365230130Smav				break;
4366230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4367230130Smav				type = 4;
4368230130Smav				break;
4369230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
4370230130Smav			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
4371230130Smav				type = 5;
4372230130Smav				break;
4373230130Smav			}
4374230130Smav			if (type == -1)
4375230130Smav				break;
4376230130Smav			j = 0;
4377230130Smav			while (types[type][j] >= 0 &&
4378230130Smav			    (used & (1 << types[type][j])) != 0) {
4379230130Smav				j++;
4380230130Smav			}
4381230130Smav			if (types[type][j] >= 0)
4382230130Smav				use = types[type][j];
4383230130Smav			break;
4384230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4385230130Smav			use = SOUND_MIXER_PCM;
4386230130Smav			break;
4387230130Smav		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
4388230130Smav			use = SOUND_MIXER_SPEAKER;
4389230130Smav			break;
4390230130Smav		default:
4391230130Smav			break;
4392230130Smav		}
4393230130Smav		if (use >= 0) {
4394230130Smav			w->ossdev = use;
4395230130Smav			used |= (1 << use);
4396230130Smav		}
4397230130Smav	}
4398230130Smav	/* Semi-known names */
4399230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4400230130Smav		w = hdaa_widget_get(devinfo, i);
4401230130Smav		if (w == NULL || w->enable == 0)
4402230130Smav			continue;
4403230130Smav		if (w->ossdev >= 0)
4404230130Smav			continue;
4405230130Smav		if (w->bindas == -1)
4406230130Smav			continue;
4407230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4408230130Smav			continue;
4409230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4410230130Smav			continue;
4411230130Smav		type = -1;
4412230130Smav		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4413230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
4414230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4415230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
4416230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
4417230130Smav			type = 0;
4418230130Smav			break;
4419230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4420230130Smav			type = 2;
4421230130Smav			break;
4422230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
4423230130Smav		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
4424230130Smav			type = 5;
4425230130Smav			break;
4426230130Smav		}
4427230130Smav		if (type == -1)
4428230130Smav			break;
4429230130Smav		j = 0;
4430230130Smav		while (types[type][j] >= 0 &&
4431230130Smav		    (used & (1 << types[type][j])) != 0) {
4432230130Smav			j++;
4433230130Smav		}
4434230130Smav		if (types[type][j] >= 0) {
4435230130Smav			w->ossdev = types[type][j];
4436230130Smav			used |= (1 << types[type][j]);
4437230130Smav		}
4438230130Smav	}
4439230130Smav	/* Others */
4440230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4441230130Smav		w = hdaa_widget_get(devinfo, i);
4442230130Smav		if (w == NULL || w->enable == 0)
4443230130Smav			continue;
4444230130Smav		if (w->ossdev >= 0)
4445230130Smav			continue;
4446230130Smav		if (w->bindas == -1)
4447230130Smav			continue;
4448230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4449230130Smav			continue;
4450230130Smav		if (as[w->bindas].dir == HDAA_CTL_OUT)
4451230130Smav			continue;
4452230130Smav		j = 0;
4453230130Smav		while (types[6][j] >= 0 &&
4454230130Smav		    (used & (1 << types[6][j])) != 0) {
4455230130Smav			j++;
4456230130Smav		}
4457230130Smav		if (types[6][j] >= 0) {
4458230130Smav			w->ossdev = types[6][j];
4459230130Smav			used |= (1 << types[6][j]);
4460230130Smav		}
4461230130Smav	}
4462230130Smav}
4463230130Smav
4464230130Smavstatic void
4465230130Smavhdaa_audio_build_tree(struct hdaa_devinfo *devinfo)
4466230130Smav{
4467230130Smav	struct hdaa_audio_as *as = devinfo->as;
4468230130Smav	int j, res;
4469230130Smav
4470230130Smav	/* Trace all associations in order of their numbers. */
4471230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4472230130Smav		if (as[j].enable == 0)
4473230130Smav			continue;
4474230130Smav		HDA_BOOTVERBOSE(
4475230130Smav			device_printf(devinfo->dev,
4476230130Smav			    "Tracing association %d (%d)\n", j, as[j].index);
4477230130Smav		);
4478230130Smav		if (as[j].dir == HDAA_CTL_OUT) {
4479230130Smavretry:
4480230130Smav			res = hdaa_audio_trace_as_out(devinfo, j, 0);
4481230130Smav			if (res == 0 && as[j].hpredir >= 0 &&
4482230130Smav			    as[j].fakeredir == 0) {
4483230130Smav				/* If CODEC can't do analog HP redirection
4484230130Smav				   try to make it using one more DAC. */
4485230130Smav				as[j].fakeredir = 1;
4486230130Smav				goto retry;
4487230130Smav			}
4488230130Smav		} else if (as[j].mixed)
4489230130Smav			res = hdaa_audio_trace_as_in(devinfo, j);
4490230130Smav		else
4491230130Smav			res = hdaa_audio_trace_as_in_mch(devinfo, j, 0);
4492230130Smav		if (res) {
4493230130Smav			HDA_BOOTVERBOSE(
4494230130Smav				device_printf(devinfo->dev,
4495230130Smav				    "Association %d (%d) trace succeeded\n",
4496230130Smav				    j, as[j].index);
4497230130Smav			);
4498230130Smav		} else {
4499230130Smav			HDA_BOOTVERBOSE(
4500230130Smav				device_printf(devinfo->dev,
4501230130Smav				    "Association %d (%d) trace failed\n",
4502230130Smav				    j, as[j].index);
4503230130Smav			);
4504230130Smav			as[j].enable = 0;
4505230130Smav		}
4506230130Smav	}
4507230130Smav
4508230130Smav	/* Look for additional DACs/ADCs. */
4509230130Smav	for (j = 0; j < devinfo->ascnt; j++) {
4510230130Smav		if (as[j].enable == 0)
4511230130Smav			continue;
4512230130Smav		hdaa_audio_adddac(devinfo, j);
4513230130Smav	}
4514230130Smav
4515230130Smav	/* Trace mixer and beeper pseudo associations. */
4516230130Smav	hdaa_audio_trace_as_extra(devinfo);
4517230130Smav}
4518230130Smav
4519230451Smav/*
4520230451Smav * Store in pdevinfo new data about whether and how we can control signal
4521230451Smav * for OSS device to/from specified widget.
4522230451Smav */
4523230130Smavstatic void
4524230451Smavhdaa_adjust_amp(struct hdaa_widget *w, int ossdev,
4525230451Smav    int found, int minamp, int maxamp)
4526230451Smav{
4527230451Smav	struct hdaa_devinfo *devinfo = w->devinfo;
4528230451Smav	struct hdaa_pcm_devinfo *pdevinfo;
4529230451Smav
4530230451Smav	if (w->bindas >= 0)
4531230451Smav		pdevinfo = devinfo->as[w->bindas].pdevinfo;
4532230451Smav	else
4533230451Smav		pdevinfo = &devinfo->devs[0];
4534230451Smav	if (found)
4535230451Smav		pdevinfo->ossmask |= (1 << ossdev);
4536230451Smav	if (minamp == 0 && maxamp == 0)
4537230451Smav		return;
4538230451Smav	if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) {
4539230451Smav		pdevinfo->minamp[ossdev] = minamp;
4540230451Smav		pdevinfo->maxamp[ossdev] = maxamp;
4541230451Smav	} else {
4542230451Smav		pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp);
4543230451Smav		pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp);
4544230451Smav	}
4545230451Smav}
4546230451Smav
4547230451Smav/*
4548230451Smav * Trace signals from/to all possible sources/destionstions to find possible
4549230451Smav * recording sources, OSS device control ranges and to assign controls.
4550230451Smav */
4551230451Smavstatic void
4552230130Smavhdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo)
4553230130Smav{
4554230130Smav	struct hdaa_audio_as *as = devinfo->as;
4555230130Smav	struct hdaa_widget *w, *cw;
4556230451Smav	int i, j, minamp, maxamp, found;
4557230130Smav
4558230130Smav	/* Assign mixers to the tree. */
4559230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4560230130Smav		w = hdaa_widget_get(devinfo, i);
4561230130Smav		if (w == NULL || w->enable == 0)
4562230130Smav			continue;
4563230451Smav		minamp = maxamp = 0;
4564230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
4565230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
4566230130Smav		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4567230130Smav		    as[w->bindas].dir == HDAA_CTL_IN)) {
4568230130Smav			if (w->ossdev < 0)
4569230130Smav				continue;
4570230451Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4571230451Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4572230451Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4573230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
4574230451Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4575230451Smav			    SOUND_MIXER_RECLEV, 0, &minamp, &maxamp);
4576230451Smav			hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp);
4577230130Smav		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4578230130Smav		    as[w->bindas].dir == HDAA_CTL_OUT) {
4579230451Smav			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4580230451Smav			    SOUND_MIXER_VOLUME, 0, &minamp, &maxamp);
4581230451Smav			hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp);
4582230130Smav		}
4583230130Smav		if (w->ossdev == SOUND_MIXER_IMIX) {
4584230451Smav			minamp = maxamp = 0;
4585230451Smav			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4586230451Smav			    w->ossdev, 1, 0, &minamp, &maxamp);
4587230451Smav			if (minamp == maxamp) {
4588230130Smav				/* If we are unable to control input monitor
4589230130Smav				   as source - try to control it as destination. */
4590230451Smav				found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4591230451Smav				    w->ossdev, 0, &minamp, &maxamp);
4592230451Smav				w->pflags |= HDAA_IMIX_AS_DST;
4593230130Smav			}
4594230451Smav			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4595230130Smav		}
4596230130Smav		if (w->pflags & HDAA_ADC_MONITOR) {
4597230130Smav			for (j = 0; j < w->nconns; j++) {
4598230130Smav				if (!w->connsenable[j])
4599230130Smav				    continue;
4600230130Smav				cw = hdaa_widget_get(devinfo, w->conns[j]);
4601230130Smav				if (cw == NULL || cw->enable == 0)
4602230130Smav				    continue;
4603230130Smav				if (cw->bindas == -1)
4604230130Smav				    continue;
4605230130Smav				if (cw->bindas >= 0 &&
4606230130Smav				    as[cw->bindas].dir != HDAA_CTL_IN)
4607230130Smav					continue;
4608230451Smav				minamp = maxamp = 0;
4609230451Smav				found = hdaa_audio_ctl_dest_amp(devinfo,
4610230451Smav				    w->nid, j, SOUND_MIXER_IGAIN, 0,
4611230451Smav				    &minamp, &maxamp);
4612230451Smav				hdaa_adjust_amp(w, SOUND_MIXER_IGAIN,
4613230451Smav				    found, minamp, maxamp);
4614230130Smav			}
4615230130Smav		}
4616230130Smav	}
4617230130Smav}
4618230130Smav
4619230130Smavstatic void
4620230130Smavhdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo)
4621230130Smav{
4622230130Smav	struct hdaa_audio_as *as = devinfo->as;
4623230130Smav	struct hdaa_widget *w;
4624230130Smav	uint32_t pincap;
4625230130Smav	int i;
4626230130Smav
4627230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
4628230130Smav		w = &devinfo->widget[i];
4629230130Smav		if (w == NULL)
4630230130Smav			continue;
4631230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4632230130Smav		    w->waspin == 0)
4633230130Smav			continue;
4634230130Smav
4635230130Smav		pincap = w->wclass.pin.cap;
4636230130Smav
4637230130Smav		/* Disable everything. */
4638230130Smav		w->wclass.pin.ctrl &= ~(
4639230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
4640230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
4641230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
4642230130Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
4643230130Smav
4644230130Smav		if (w->enable == 0) {
4645230130Smav			/* Pin is unused so left it disabled. */
4646230130Smav			continue;
4647230130Smav		} else if (w->waspin) {
4648230130Smav			/* Enable input for beeper input. */
4649230130Smav			w->wclass.pin.ctrl |=
4650230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
4651230130Smav		} else if (w->bindas < 0 || as[w->bindas].enable == 0) {
4652230130Smav			/* Pin is unused so left it disabled. */
4653230130Smav			continue;
4654230130Smav		} else if (as[w->bindas].dir == HDAA_CTL_IN) {
4655230130Smav			/* Input pin, configure for input. */
4656230130Smav			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
4657230130Smav				w->wclass.pin.ctrl |=
4658230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
4659230130Smav
4660230130Smav			if ((devinfo->quirks & HDAA_QUIRK_IVREF100) &&
4661230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4662230130Smav				w->wclass.pin.ctrl |=
4663230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4664230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4665230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) &&
4666230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4667230130Smav				w->wclass.pin.ctrl |=
4668230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4669230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4670230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) &&
4671230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4672230130Smav				w->wclass.pin.ctrl |=
4673230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4674230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4675230130Smav		} else {
4676230130Smav			/* Output pin, configure for output. */
4677230130Smav			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
4678230130Smav				w->wclass.pin.ctrl |=
4679230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
4680230130Smav
4681230130Smav			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
4682230130Smav			    (w->wclass.pin.config &
4683230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
4684230130Smav			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
4685230130Smav				w->wclass.pin.ctrl |=
4686230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
4687230130Smav
4688230130Smav			if ((devinfo->quirks & HDAA_QUIRK_OVREF100) &&
4689230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4690230130Smav				w->wclass.pin.ctrl |=
4691230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4692230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4693230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) &&
4694230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4695230130Smav				w->wclass.pin.ctrl |=
4696230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4697230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4698230130Smav			else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) &&
4699230130Smav			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4700230130Smav				w->wclass.pin.ctrl |=
4701230130Smav				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4702230130Smav				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4703230130Smav		}
4704230130Smav	}
4705230130Smav}
4706230130Smav
4707230130Smavstatic void
4708230130Smavhdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo)
4709230130Smav{
4710230130Smav	struct hdaa_audio_ctl *ctl;
4711230130Smav	int i, z;
4712230130Smav
4713230130Smav	i = 0;
4714230130Smav	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4715230130Smav		if (ctl->enable == 0 || ctl->ossmask != 0) {
4716230130Smav			/* Mute disabled and mixer controllable controls.
4717230130Smav			 * Last will be initialized by mixer_init().
4718230130Smav			 * This expected to reduce click on startup. */
4719230130Smav			hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0);
4720230130Smav			continue;
4721230130Smav		}
4722230130Smav		/* Init fixed controls to 0dB amplification. */
4723230130Smav		z = ctl->offset;
4724230130Smav		if (z > ctl->step)
4725230130Smav			z = ctl->step;
4726230130Smav		hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z);
4727230130Smav	}
4728230130Smav}
4729230130Smav
4730230130Smavstatic void
4731230130Smavhdaa_gpio_commit(struct hdaa_devinfo *devinfo)
4732230130Smav{
4733230130Smav	uint32_t gdata, gmask, gdir;
4734230130Smav	int i, numgpio;
4735230130Smav
4736230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
4737230130Smav	if (devinfo->gpio != 0 && numgpio != 0) {
4738230130Smav		gdata = hda_command(devinfo->dev,
4739230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
4740230130Smav		gmask = hda_command(devinfo->dev,
4741230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
4742230130Smav		gdir = hda_command(devinfo->dev,
4743230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
4744230130Smav		for (i = 0; i < numgpio; i++) {
4745230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4746230130Smav			    HDAA_GPIO_SET(i)) {
4747230130Smav				gdata |= (1 << i);
4748230130Smav				gmask |= (1 << i);
4749230130Smav				gdir |= (1 << i);
4750230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4751230130Smav			    HDAA_GPIO_CLEAR(i)) {
4752230130Smav				gdata &= ~(1 << i);
4753230130Smav				gmask |= (1 << i);
4754230130Smav				gdir |= (1 << i);
4755230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4756230130Smav			    HDAA_GPIO_DISABLE(i)) {
4757230130Smav				gmask &= ~(1 << i);
4758230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4759230130Smav			    HDAA_GPIO_INPUT(i)) {
4760230130Smav				gmask |= (1 << i);
4761230130Smav				gdir &= ~(1 << i);
4762230130Smav			}
4763230130Smav		}
4764230130Smav		HDA_BOOTVERBOSE(
4765230130Smav			device_printf(devinfo->dev, "GPIO commit\n");
4766230130Smav		);
4767230130Smav		hda_command(devinfo->dev,
4768230130Smav		    HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask));
4769230130Smav		hda_command(devinfo->dev,
4770230130Smav		    HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir));
4771230130Smav		hda_command(devinfo->dev,
4772230130Smav		    HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata));
4773230130Smav		HDA_BOOTVERBOSE(
4774230130Smav			hdaa_dump_gpio(devinfo);
4775230130Smav		);
4776230130Smav	}
4777230130Smav}
4778230130Smav
4779230130Smavstatic void
4780230130Smavhdaa_gpo_commit(struct hdaa_devinfo *devinfo)
4781230130Smav{
4782230130Smav	uint32_t gdata;
4783230130Smav	int i, numgpo;
4784230130Smav
4785230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
4786230130Smav	if (devinfo->gpo != 0 && numgpo != 0) {
4787230130Smav		gdata = hda_command(devinfo->dev,
4788230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
4789230130Smav		for (i = 0; i < numgpo; i++) {
4790230130Smav			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4791230130Smav			    HDAA_GPIO_SET(i)) {
4792230130Smav				gdata |= (1 << i);
4793230130Smav			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4794230130Smav			    HDAA_GPIO_CLEAR(i)) {
4795230130Smav				gdata &= ~(1 << i);
4796230130Smav			}
4797230130Smav		}
4798230130Smav		HDA_BOOTVERBOSE(
4799230130Smav			device_printf(devinfo->dev, "GPO commit\n");
4800230130Smav		);
4801230130Smav		hda_command(devinfo->dev,
4802230130Smav		    HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata));
4803230130Smav		HDA_BOOTVERBOSE(
4804230130Smav			hdaa_dump_gpo(devinfo);
4805230130Smav		);
4806230130Smav	}
4807230130Smav}
4808230130Smav
4809230130Smavstatic void
4810230130Smavhdaa_audio_commit(struct hdaa_devinfo *devinfo)
4811230130Smav{
4812230130Smav	struct hdaa_widget *w;
4813230130Smav	int i;
4814230130Smav
4815230130Smav	/* Commit controls. */
4816230130Smav	hdaa_audio_ctl_commit(devinfo);
4817230130Smav
4818230130Smav	/* Commit selectors, pins and EAPD. */
4819230130Smav	for (i = 0; i < devinfo->nodecnt; i++) {
4820230130Smav		w = &devinfo->widget[i];
4821230130Smav		if (w == NULL)
4822230130Smav			continue;
4823230130Smav		if (w->selconn == -1)
4824230130Smav			w->selconn = 0;
4825230130Smav		if (w->nconns > 0)
4826230130Smav			hdaa_widget_connection_select(w, w->selconn);
4827230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
4828230130Smav		    w->waspin) {
4829230130Smav			hda_command(devinfo->dev,
4830230130Smav			    HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid,
4831230130Smav			    w->wclass.pin.ctrl));
4832230130Smav		}
4833230130Smav		if (w->param.eapdbtl != HDA_INVALID) {
4834230130Smav			uint32_t val;
4835230130Smav
4836230130Smav			val = w->param.eapdbtl;
4837230130Smav			if (devinfo->quirks &
4838230130Smav			    HDAA_QUIRK_EAPDINV)
4839230130Smav				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
4840230130Smav			hda_command(devinfo->dev,
4841230130Smav			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid,
4842230130Smav			    val));
4843230130Smav		}
4844230130Smav	}
4845230130Smav
4846230130Smav	hdaa_gpio_commit(devinfo);
4847230130Smav	hdaa_gpo_commit(devinfo);
4848230130Smav}
4849230130Smav
4850230130Smavstatic void
4851230130Smavhdaa_powerup(struct hdaa_devinfo *devinfo)
4852230130Smav{
4853230130Smav	int i;
4854230130Smav
4855230130Smav	hda_command(devinfo->dev,
4856230130Smav	    HDA_CMD_SET_POWER_STATE(0,
4857230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D0));
4858230130Smav	DELAY(100);
4859230130Smav
4860230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4861230130Smav		hda_command(devinfo->dev,
4862230130Smav		    HDA_CMD_SET_POWER_STATE(0,
4863230130Smav		    i, HDA_CMD_POWER_STATE_D0));
4864230130Smav	}
4865230130Smav	DELAY(1000);
4866230130Smav}
4867230130Smav
4868230130Smavstatic int
4869230130Smavhdaa_pcmchannel_setup(struct hdaa_chan *ch)
4870230130Smav{
4871230130Smav	struct hdaa_devinfo *devinfo = ch->devinfo;
4872230130Smav	struct hdaa_audio_as *as = devinfo->as;
4873230130Smav	struct hdaa_widget *w;
4874230130Smav	uint32_t cap, fmtcap, pcmcap;
4875230130Smav	int i, j, ret, channels, onlystereo;
4876230130Smav	uint16_t pinset;
4877230130Smav
4878230130Smav	ch->caps = hdaa_caps;
4879230130Smav	ch->caps.fmtlist = ch->fmtlist;
4880230130Smav	ch->bit16 = 1;
4881230130Smav	ch->bit32 = 0;
4882230130Smav	ch->pcmrates[0] = 48000;
4883230130Smav	ch->pcmrates[1] = 0;
4884230326Smav	ch->stripecap = 0xff;
4885230130Smav
4886230130Smav	ret = 0;
4887230130Smav	channels = 0;
4888230130Smav	onlystereo = 1;
4889230130Smav	pinset = 0;
4890230130Smav	fmtcap = devinfo->supp_stream_formats;
4891230130Smav	pcmcap = devinfo->supp_pcm_size_rate;
4892230130Smav
4893230130Smav	for (i = 0; i < 16; i++) {
4894230130Smav		/* Check as is correct */
4895230130Smav		if (ch->as < 0)
4896230130Smav			break;
4897230130Smav		/* Cound only present DACs */
4898230130Smav		if (as[ch->as].dacs[ch->asindex][i] <= 0)
4899230130Smav			continue;
4900230130Smav		/* Ignore duplicates */
4901230130Smav		for (j = 0; j < ret; j++) {
4902230130Smav			if (ch->io[j] == as[ch->as].dacs[ch->asindex][i])
4903230130Smav				break;
4904230130Smav		}
4905230130Smav		if (j < ret)
4906230130Smav			continue;
4907230130Smav
4908230130Smav		w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]);
4909230130Smav		if (w == NULL || w->enable == 0)
4910230130Smav			continue;
4911230130Smav		cap = w->param.supp_stream_formats;
4912230130Smav		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
4913230130Smav		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
4914230130Smav			continue;
4915230130Smav		/* Many CODECs does not declare AC3 support on SPDIF.
4916230130Smav		   I don't beleave that they doesn't support it! */
4917230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
4918230130Smav			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
4919230130Smav		if (ret == 0) {
4920230130Smav			fmtcap = cap;
4921230130Smav			pcmcap = w->param.supp_pcm_size_rate;
4922230130Smav		} else {
4923230130Smav			fmtcap &= cap;
4924230130Smav			pcmcap &= w->param.supp_pcm_size_rate;
4925230130Smav		}
4926230130Smav		ch->io[ret++] = as[ch->as].dacs[ch->asindex][i];
4927230326Smav		ch->stripecap &= w->wclass.conv.stripecap;
4928230130Smav		/* Do not count redirection pin/dac channels. */
4929230130Smav		if (i == 15 && as[ch->as].hpredir >= 0)
4930230130Smav			continue;
4931230130Smav		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
4932230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
4933230130Smav			onlystereo = 0;
4934230130Smav		pinset |= (1 << i);
4935230130Smav	}
4936230130Smav	ch->io[ret] = -1;
4937230130Smav	ch->channels = channels;
4938230130Smav
4939230130Smav	if (as[ch->as].fakeredir)
4940230130Smav		ret--;
4941230130Smav	/* Standard speaks only about stereo pins and playback, ... */
4942230130Smav	if ((!onlystereo) || as[ch->as].mixed)
4943230130Smav		pinset = 0;
4944230130Smav	/* ..., but there it gives us info about speakers layout. */
4945230130Smav	as[ch->as].pinset = pinset;
4946230130Smav
4947230130Smav	ch->supp_stream_formats = fmtcap;
4948230130Smav	ch->supp_pcm_size_rate = pcmcap;
4949230130Smav
4950230130Smav	/*
4951230130Smav	 *  8bit = 0
4952230130Smav	 * 16bit = 1
4953230130Smav	 * 20bit = 2
4954230130Smav	 * 24bit = 3
4955230130Smav	 * 32bit = 4
4956230130Smav	 */
4957230130Smav	if (ret > 0) {
4958230130Smav		i = 0;
4959230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
4960230130Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
4961230130Smav				ch->bit16 = 1;
4962230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
4963230130Smav				ch->bit16 = 0;
4964230488Smav			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
4965230130Smav				ch->bit32 = 3;
4966230130Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
4967230130Smav				ch->bit32 = 2;
4968230488Smav			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
4969230488Smav				ch->bit32 = 4;
4970230130Smav			if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) {
4971230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
4972230130Smav				if (ch->bit32)
4973230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
4974230130Smav			}
4975230130Smav			if (channels >= 2) {
4976230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
4977230130Smav				if (ch->bit32)
4978230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
4979230130Smav			}
4980230312Smav			if (channels >= 3 && !onlystereo) {
4981230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0);
4982230312Smav				if (ch->bit32)
4983230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0);
4984230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1);
4985230312Smav				if (ch->bit32)
4986230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1);
4987230312Smav			}
4988230312Smav			if (channels >= 4) {
4989230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
4990230130Smav				if (ch->bit32)
4991230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
4992230312Smav				if (!onlystereo) {
4993230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1);
4994230312Smav					if (ch->bit32)
4995230312Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1);
4996230312Smav				}
4997230130Smav			}
4998230312Smav			if (channels >= 5 && !onlystereo) {
4999230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0);
5000230312Smav				if (ch->bit32)
5001230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0);
5002230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1);
5003230312Smav				if (ch->bit32)
5004230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1);
5005230312Smav			}
5006230312Smav			if (channels >= 6) {
5007230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
5008230130Smav				if (ch->bit32)
5009230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
5010230312Smav				if (!onlystereo) {
5011230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0);
5012230312Smav					if (ch->bit32)
5013230312Smav						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0);
5014230312Smav				}
5015230130Smav			}
5016230312Smav			if (channels >= 7 && !onlystereo) {
5017230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0);
5018230312Smav				if (ch->bit32)
5019230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0);
5020230312Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1);
5021230312Smav				if (ch->bit32)
5022230312Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1);
5023230312Smav			}
5024230312Smav			if (channels >= 8) {
5025230130Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
5026230130Smav				if (ch->bit32)
5027230130Smav					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
5028230130Smav			}
5029230130Smav		}
5030230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
5031230130Smav			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
5032230513Smav			if (channels >= 8) {
5033230513Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 0);
5034230513Smav				ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 1);
5035230513Smav			}
5036230130Smav		}
5037230130Smav		ch->fmtlist[i] = 0;
5038230130Smav		i = 0;
5039230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
5040230130Smav			ch->pcmrates[i++] = 8000;
5041230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
5042230130Smav			ch->pcmrates[i++] = 11025;
5043230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
5044230130Smav			ch->pcmrates[i++] = 16000;
5045230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
5046230130Smav			ch->pcmrates[i++] = 22050;
5047230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
5048230130Smav			ch->pcmrates[i++] = 32000;
5049230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
5050230130Smav			ch->pcmrates[i++] = 44100;
5051230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
5052230130Smav		ch->pcmrates[i++] = 48000;
5053230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
5054230130Smav			ch->pcmrates[i++] = 88200;
5055230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
5056230130Smav			ch->pcmrates[i++] = 96000;
5057230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
5058230130Smav			ch->pcmrates[i++] = 176400;
5059230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
5060230130Smav			ch->pcmrates[i++] = 192000;
5061230130Smav		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
5062230130Smav		ch->pcmrates[i] = 0;
5063230130Smav		if (i > 0) {
5064230130Smav			ch->caps.minspeed = ch->pcmrates[0];
5065230130Smav			ch->caps.maxspeed = ch->pcmrates[i - 1];
5066230130Smav		}
5067230130Smav	}
5068230130Smav
5069230130Smav	return (ret);
5070230130Smav}
5071230130Smav
5072230130Smavstatic void
5073230451Smavhdaa_prepare_pcms(struct hdaa_devinfo *devinfo)
5074230130Smav{
5075230130Smav	struct hdaa_audio_as *as = devinfo->as;
5076230130Smav	int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
5077230130Smav
5078230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5079230130Smav		if (as[i].enable == 0)
5080230130Smav			continue;
5081230130Smav		if (as[i].dir == HDAA_CTL_IN) {
5082230130Smav			if (as[i].digital)
5083230130Smav				drdev++;
5084230130Smav			else
5085230130Smav				ardev++;
5086230130Smav		} else {
5087230130Smav			if (as[i].digital)
5088230130Smav				dpdev++;
5089230130Smav			else
5090230130Smav				apdev++;
5091230130Smav		}
5092230130Smav	}
5093230130Smav	devinfo->num_devs =
5094230130Smav	    max(ardev, apdev) + max(drdev, dpdev);
5095230130Smav	devinfo->devs =
5096230130Smav	    (struct hdaa_pcm_devinfo *)malloc(
5097230130Smav	    devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo),
5098230130Smav	    M_HDAA, M_ZERO | M_NOWAIT);
5099230130Smav	if (devinfo->devs == NULL) {
5100230130Smav		device_printf(devinfo->dev,
5101230130Smav		    "Unable to allocate memory for devices\n");
5102230130Smav		return;
5103230130Smav	}
5104230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5105230130Smav		devinfo->devs[i].index = i;
5106230130Smav		devinfo->devs[i].devinfo = devinfo;
5107230130Smav		devinfo->devs[i].playas = -1;
5108230130Smav		devinfo->devs[i].recas = -1;
5109230130Smav		devinfo->devs[i].digital = 255;
5110230130Smav	}
5111230130Smav	for (i = 0; i < devinfo->ascnt; i++) {
5112230130Smav		if (as[i].enable == 0)
5113230130Smav			continue;
5114230130Smav		for (j = 0; j < devinfo->num_devs; j++) {
5115230130Smav			if (devinfo->devs[j].digital != 255 &&
5116230130Smav			    (!devinfo->devs[j].digital) !=
5117230130Smav			    (!as[i].digital))
5118230130Smav				continue;
5119230130Smav			if (as[i].dir == HDAA_CTL_IN) {
5120230130Smav				if (devinfo->devs[j].recas >= 0)
5121230130Smav					continue;
5122230130Smav				devinfo->devs[j].recas = i;
5123230130Smav			} else {
5124230130Smav				if (devinfo->devs[j].playas >= 0)
5125230130Smav					continue;
5126230130Smav				devinfo->devs[j].playas = i;
5127230130Smav			}
5128230451Smav			as[i].pdevinfo = &devinfo->devs[j];
5129230130Smav			for (k = 0; k < as[i].num_chans; k++) {
5130230130Smav				devinfo->chans[as[i].chans[k]].pdevinfo =
5131230130Smav				    &devinfo->devs[j];
5132230130Smav			}
5133230130Smav			devinfo->devs[j].digital = as[i].digital;
5134230130Smav			break;
5135230130Smav		}
5136230130Smav	}
5137230451Smav}
5138230451Smav
5139230451Smavstatic void
5140230451Smavhdaa_create_pcms(struct hdaa_devinfo *devinfo)
5141230451Smav{
5142230451Smav	int i;
5143230451Smav
5144230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
5145230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
5146230130Smav
5147230130Smav		pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1);
5148230130Smav		device_set_ivars(pdevinfo->dev, (void *)pdevinfo);
5149230130Smav	}
5150230130Smav}
5151230130Smav
5152230130Smavstatic void
5153230130Smavhdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
5154230130Smav{
5155230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5156230130Smav	struct hdaa_audio_ctl *ctl;
5157230130Smav	char buf[64];
5158230130Smav	int i, j, printed;
5159230130Smav
5160230130Smav	if (flag == 0) {
5161230130Smav		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5162230130Smav		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5163230130Smav		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
5164230130Smav		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
5165230130Smav	}
5166230130Smav
5167230130Smav	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
5168230130Smav		if ((flag & (1 << j)) == 0)
5169230130Smav			continue;
5170230130Smav		i = 0;
5171230130Smav		printed = 0;
5172230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5173230130Smav			if (ctl->enable == 0 ||
5174230130Smav			    ctl->widget->enable == 0)
5175230130Smav				continue;
5176230130Smav			if (!((pdevinfo->playas >= 0 &&
5177230130Smav			    ctl->widget->bindas == pdevinfo->playas) ||
5178230130Smav			    (pdevinfo->recas >= 0 &&
5179230130Smav			    ctl->widget->bindas == pdevinfo->recas) ||
5180230130Smav			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
5181230130Smav				continue;
5182230130Smav			if ((ctl->ossmask & (1 << j)) == 0)
5183230130Smav				continue;
5184230130Smav
5185230130Smav			if (printed == 0) {
5186230130Smav				device_printf(pdevinfo->dev, "\n");
5187230130Smav				if (banner != NULL) {
5188230130Smav					device_printf(pdevinfo->dev, "%s", banner);
5189230130Smav				} else {
5190230130Smav					device_printf(pdevinfo->dev, "Unknown Ctl");
5191230130Smav				}
5192230451Smav				printf(" (OSS: %s)",
5193230130Smav				    hdaa_audio_ctl_ossmixer_mask2allname(1 << j,
5194230130Smav				    buf, sizeof(buf)));
5195230451Smav				if (pdevinfo->ossmask & (1 << j)) {
5196230451Smav					printf(": %+d/%+ddB\n",
5197230451Smav					    pdevinfo->minamp[j] / 4,
5198230451Smav					    pdevinfo->maxamp[j] / 4);
5199230451Smav				} else
5200230451Smav					printf("\n");
5201230130Smav				device_printf(pdevinfo->dev, "   |\n");
5202230130Smav				printed = 1;
5203230130Smav			}
5204230130Smav			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
5205230130Smav				ctl->widget->nid,
5206230130Smav				(ctl->ndir == HDAA_CTL_IN)?"in ":"out");
5207230130Smav			if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir)
5208230130Smav				printf(" %2d): ", ctl->index);
5209230130Smav			else
5210230130Smav				printf("):    ");
5211230130Smav			if (ctl->step > 0) {
5212230130Smav				printf("%+d/%+ddB (%d steps)%s\n",
5213230451Smav				    MINQDB(ctl) / 4,
5214230451Smav				    MAXQDB(ctl) / 4,
5215230130Smav				    ctl->step + 1,
5216230130Smav				    ctl->mute?" + mute":"");
5217230130Smav			} else
5218230130Smav				printf("%s\n", ctl->mute?"mute":"");
5219230130Smav		}
5220230130Smav	}
5221230130Smav}
5222230130Smav
5223230130Smavstatic void
5224230130Smavhdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
5225230130Smav{
5226230130Smav	uint32_t cap;
5227230130Smav
5228230130Smav	cap = fcap;
5229230130Smav	if (cap != 0) {
5230230130Smav		device_printf(dev, "     Stream cap: 0x%08x\n", cap);
5231230130Smav		device_printf(dev, "                ");
5232230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5233230130Smav			printf(" AC3");
5234230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5235230130Smav			printf(" FLOAT32");
5236230130Smav		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5237230130Smav			printf(" PCM");
5238230130Smav		printf("\n");
5239230130Smav	}
5240230130Smav	cap = pcmcap;
5241230130Smav	if (cap != 0) {
5242230130Smav		device_printf(dev, "        PCM cap: 0x%08x\n", cap);
5243230130Smav		device_printf(dev, "                ");
5244230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5245230130Smav			printf(" 8");
5246230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5247230130Smav			printf(" 16");
5248230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5249230130Smav			printf(" 20");
5250230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5251230130Smav			printf(" 24");
5252230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5253230130Smav			printf(" 32");
5254230130Smav		printf(" bits,");
5255230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5256230130Smav			printf(" 8");
5257230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5258230130Smav			printf(" 11");
5259230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5260230130Smav			printf(" 16");
5261230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5262230130Smav			printf(" 22");
5263230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5264230130Smav			printf(" 32");
5265230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5266230130Smav			printf(" 44");
5267230130Smav		printf(" 48");
5268230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5269230130Smav			printf(" 88");
5270230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5271230130Smav			printf(" 96");
5272230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5273230130Smav			printf(" 176");
5274230130Smav		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5275230130Smav			printf(" 192");
5276230130Smav		printf(" KHz\n");
5277230130Smav	}
5278230130Smav}
5279230130Smav
5280230130Smavstatic void
5281230130Smavhdaa_dump_pin(struct hdaa_widget *w)
5282230130Smav{
5283230130Smav	uint32_t pincap;
5284230130Smav
5285230130Smav	pincap = w->wclass.pin.cap;
5286230130Smav
5287230130Smav	device_printf(w->devinfo->dev, "        Pin cap: 0x%08x\n", pincap);
5288230130Smav	device_printf(w->devinfo->dev, "                ");
5289230130Smav	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5290230130Smav		printf(" ISC");
5291230130Smav	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5292230130Smav		printf(" TRQD");
5293230130Smav	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5294230130Smav		printf(" PDC");
5295230130Smav	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5296230130Smav		printf(" HP");
5297230130Smav	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5298230130Smav		printf(" OUT");
5299230130Smav	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5300230130Smav		printf(" IN");
5301230130Smav	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5302230130Smav		printf(" BAL");
5303230130Smav	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
5304230130Smav		printf(" HDMI");
5305230130Smav	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5306230130Smav		printf(" VREF[");
5307230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5308230130Smav			printf(" 50");
5309230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5310230130Smav			printf(" 80");
5311230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5312230130Smav			printf(" 100");
5313230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5314230130Smav			printf(" GROUND");
5315230130Smav		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5316230130Smav			printf(" HIZ");
5317230130Smav		printf(" ]");
5318230130Smav	}
5319230130Smav	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5320230130Smav		printf(" EAPD");
5321230130Smav	if (HDA_PARAM_PIN_CAP_DP(pincap))
5322230130Smav		printf(" DP");
5323230130Smav	if (HDA_PARAM_PIN_CAP_HBR(pincap))
5324230130Smav		printf(" HBR");
5325230130Smav	printf("\n");
5326230130Smav	device_printf(w->devinfo->dev, "     Pin config: 0x%08x\n",
5327230130Smav	    w->wclass.pin.config);
5328230130Smav	device_printf(w->devinfo->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
5329230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5330230130Smav		printf(" HP");
5331230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5332230130Smav		printf(" IN");
5333230130Smav	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5334230130Smav		printf(" OUT");
5335230312Smav	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
5336230312Smav		if ((w->wclass.pin.ctrl &
5337230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
5338230312Smav			printf(" HBR");
5339230312Smav		else if ((w->wclass.pin.ctrl &
5340230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5341230312Smav			printf(" EPTs");
5342230312Smav	} else {
5343230312Smav		if ((w->wclass.pin.ctrl &
5344230312Smav		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5345230312Smav			printf(" VREFs");
5346230312Smav	}
5347230130Smav	printf("\n");
5348230130Smav}
5349230130Smav
5350230130Smavstatic void
5351230130Smavhdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf)
5352230130Smav{
5353230130Smav
5354230130Smav	device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d "
5355230130Smav	    "%-13s %-5s %-7s %-10s %-7s %d%s\n",
5356230130Smav	    w->nid, conf,
5357230130Smav	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
5358230130Smav	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
5359230130Smav	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
5360230130Smav	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
5361230130Smav	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
5362230130Smav	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
5363230130Smav	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
5364230130Smav	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
5365230130Smav	    (w->enable == 0)?" DISA":"");
5366230130Smav}
5367230130Smav
5368230130Smavstatic void
5369230130Smavhdaa_dump_pin_configs(struct hdaa_devinfo *devinfo)
5370230130Smav{
5371230130Smav	struct hdaa_widget *w;
5372230130Smav	int i;
5373230130Smav
5374230130Smav	device_printf(devinfo->dev, "nid   0x    as seq "
5375230130Smav	    "device       conn  jack    loc        color   misc\n");
5376230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5377230130Smav		w = hdaa_widget_get(devinfo, i);
5378230130Smav		if (w == NULL)
5379230130Smav			continue;
5380230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5381230130Smav			continue;
5382230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
5383230130Smav	}
5384230130Smav}
5385230130Smav
5386230130Smavstatic void
5387230130Smavhdaa_dump_amp(device_t dev, uint32_t cap, char *banner)
5388230130Smav{
5389230130Smav	device_printf(dev, "     %s amp: 0x%08x\n", banner, cap);
5390230130Smav	device_printf(dev, "                 "
5391230130Smav	    "mute=%d step=%d size=%d offset=%d\n",
5392230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5393230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap),
5394230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap),
5395230130Smav	    HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap));
5396230130Smav}
5397230130Smav
5398230130Smavstatic void
5399230130Smavhdaa_dump_nodes(struct hdaa_devinfo *devinfo)
5400230130Smav{
5401230130Smav	struct hdaa_widget *w, *cw;
5402230130Smav	char buf[64];
5403230130Smav	int i, j;
5404230130Smav
5405230130Smav	device_printf(devinfo->dev, "\n");
5406230130Smav	device_printf(devinfo->dev, "Default Parameter\n");
5407230130Smav	device_printf(devinfo->dev, "-----------------\n");
5408230130Smav	hdaa_dump_audio_formats(devinfo->dev,
5409230130Smav	    devinfo->supp_stream_formats,
5410230130Smav	    devinfo->supp_pcm_size_rate);
5411230130Smav	device_printf(devinfo->dev, "         IN amp: 0x%08x\n",
5412230130Smav	    devinfo->inamp_cap);
5413230130Smav	device_printf(devinfo->dev, "        OUT amp: 0x%08x\n",
5414230130Smav	    devinfo->outamp_cap);
5415230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5416230130Smav		w = hdaa_widget_get(devinfo, i);
5417230130Smav		if (w == NULL) {
5418230130Smav			device_printf(devinfo->dev, "Ghost widget nid=%d\n", i);
5419230130Smav			continue;
5420230130Smav		}
5421230130Smav		device_printf(devinfo->dev, "\n");
5422230130Smav		device_printf(devinfo->dev, "            nid: %d%s\n", w->nid,
5423230130Smav		    (w->enable == 0) ? " [DISABLED]" : "");
5424230130Smav		device_printf(devinfo->dev, "           Name: %s\n", w->name);
5425230130Smav		device_printf(devinfo->dev, "     Widget cap: 0x%08x\n",
5426230130Smav		    w->param.widget_cap);
5427230130Smav		if (w->param.widget_cap & 0x0ee1) {
5428230130Smav			device_printf(devinfo->dev, "                ");
5429230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
5430230130Smav			    printf(" LRSWAP");
5431230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
5432230130Smav			    printf(" PWR");
5433230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5434230130Smav			    printf(" DIGITAL");
5435230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
5436230130Smav			    printf(" UNSOL");
5437230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
5438230130Smav			    printf(" PROC");
5439230130Smav			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
5440230326Smav			    printf(" STRIPE(x%d)",
5441230326Smav				1 << (fls(w->wclass.conv.stripecap) - 1));
5442230130Smav			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
5443230130Smav			if (j == 1)
5444230130Smav			    printf(" STEREO");
5445230130Smav			else if (j > 1)
5446230130Smav			    printf(" %dCH", j + 1);
5447230130Smav			printf("\n");
5448230130Smav		}
5449230130Smav		if (w->bindas != -1) {
5450230130Smav			device_printf(devinfo->dev, "    Association: %d (0x%08x)\n",
5451230130Smav			    w->bindas, w->bindseqmask);
5452230130Smav		}
5453230130Smav		if (w->ossmask != 0 || w->ossdev >= 0) {
5454230130Smav			device_printf(devinfo->dev, "            OSS: %s",
5455230130Smav			    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
5456230130Smav			if (w->ossdev >= 0)
5457230451Smav			    printf(" (%s)", ossnames[w->ossdev]);
5458230130Smav			printf("\n");
5459230130Smav		}
5460230130Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5461230130Smav		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5462230130Smav			hdaa_dump_audio_formats(devinfo->dev,
5463230130Smav			    w->param.supp_stream_formats,
5464230130Smav			    w->param.supp_pcm_size_rate);
5465230130Smav		} else if (w->type ==
5466230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
5467230130Smav			hdaa_dump_pin(w);
5468230130Smav		if (w->param.eapdbtl != HDA_INVALID)
5469230130Smav			device_printf(devinfo->dev, "           EAPD: 0x%08x\n",
5470230130Smav			    w->param.eapdbtl);
5471230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5472230130Smav		    w->param.outamp_cap != 0)
5473230130Smav			hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output");
5474230130Smav		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5475230130Smav		    w->param.inamp_cap != 0)
5476230130Smav			hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input");
5477230130Smav		if (w->nconns > 0) {
5478230130Smav			device_printf(devinfo->dev, "    connections: %d\n", w->nconns);
5479230130Smav			device_printf(devinfo->dev, "          |\n");
5480230130Smav		}
5481230130Smav		for (j = 0; j < w->nconns; j++) {
5482230130Smav			cw = hdaa_widget_get(devinfo, w->conns[j]);
5483230130Smav			device_printf(devinfo->dev, "          + %s<- nid=%d [%s]",
5484230130Smav			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
5485230130Smav			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5486230130Smav			if (cw == NULL)
5487230130Smav				printf(" [UNKNOWN]");
5488230130Smav			else if (cw->enable == 0)
5489230130Smav				printf(" [DISABLED]");
5490230130Smav			if (w->nconns > 1 && w->selconn == j && w->type !=
5491230130Smav			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5492230130Smav				printf(" (selected)");
5493230130Smav			printf("\n");
5494230130Smav		}
5495230130Smav	}
5496230130Smav
5497230130Smav}
5498230130Smav
5499230130Smavstatic void
5500230130Smavhdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth)
5501230130Smav{
5502230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5503230130Smav	struct hdaa_widget *w, *cw;
5504230130Smav	char buf[64];
5505230130Smav	int i, printed = 0;
5506230130Smav
5507230130Smav	if (depth > HDA_PARSE_MAXDEPTH)
5508230130Smav		return;
5509230130Smav
5510230130Smav	w = hdaa_widget_get(devinfo, nid);
5511230130Smav	if (w == NULL || w->enable == 0)
5512230130Smav		return;
5513230130Smav
5514230130Smav	if (depth == 0)
5515230130Smav		device_printf(pdevinfo->dev, "%*s", 4, "");
5516230130Smav	else
5517230130Smav		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
5518230130Smav	printf("nid=%d [%s]", w->nid, w->name);
5519230130Smav
5520230130Smav	if (depth > 0) {
5521230130Smav		if (w->ossmask == 0) {
5522230130Smav			printf("\n");
5523230130Smav			return;
5524230130Smav		}
5525230130Smav		printf(" [src: %s]",
5526230130Smav		    hdaa_audio_ctl_ossmixer_mask2allname(
5527230130Smav			w->ossmask, buf, sizeof(buf)));
5528230130Smav		if (w->ossdev >= 0) {
5529230130Smav			printf("\n");
5530230130Smav			return;
5531230130Smav		}
5532230130Smav	}
5533230130Smav	printf("\n");
5534230130Smav
5535230130Smav	for (i = 0; i < w->nconns; i++) {
5536230130Smav		if (w->connsenable[i] == 0)
5537230130Smav			continue;
5538230130Smav		cw = hdaa_widget_get(devinfo, w->conns[i]);
5539230130Smav		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
5540230130Smav			continue;
5541230130Smav		if (printed == 0) {
5542230130Smav			device_printf(pdevinfo->dev, "%*s  |\n", 4 + (depth) * 7, "");
5543230130Smav			printed = 1;
5544230130Smav		}
5545230130Smav		hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
5546230130Smav	}
5547230130Smav
5548230130Smav}
5549230130Smav
5550230130Smavstatic void
5551230130Smavhdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo)
5552230130Smav{
5553230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5554230130Smav	struct hdaa_audio_as *as;
5555230130Smav	struct hdaa_widget *w;
5556230130Smav	int i, printed = 0;
5557230130Smav
5558230130Smav	if (pdevinfo->playas < 0)
5559230130Smav		return;
5560230130Smav
5561230130Smav	as = &devinfo->as[pdevinfo->playas];
5562230130Smav	for (i = 0; i < 16; i++) {
5563230130Smav		if (as->pins[i] <= 0)
5564230130Smav			continue;
5565230130Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
5566230130Smav		if (w == NULL || w->enable == 0)
5567230130Smav			continue;
5568230130Smav		if (printed == 0) {
5569230130Smav			printed = 1;
5570230130Smav			device_printf(pdevinfo->dev, "\n");
5571230130Smav			device_printf(pdevinfo->dev, "Playback:\n");
5572230130Smav		}
5573230130Smav		device_printf(pdevinfo->dev, "\n");
5574230130Smav		hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0);
5575230130Smav	}
5576230130Smav}
5577230130Smav
5578230130Smavstatic void
5579230130Smavhdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo)
5580230130Smav{
5581230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5582230130Smav	struct hdaa_widget *w;
5583230130Smav	int i;
5584230130Smav	int printed = 0;
5585230130Smav
5586230130Smav	if (pdevinfo->recas < 0)
5587230130Smav		return;
5588230130Smav
5589230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5590230130Smav		w = hdaa_widget_get(devinfo, i);
5591230130Smav		if (w == NULL || w->enable == 0)
5592230130Smav			continue;
5593230130Smav		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
5594230130Smav			continue;
5595230130Smav		if (w->bindas != pdevinfo->recas)
5596230130Smav			continue;
5597230130Smav		if (printed == 0) {
5598230130Smav			printed = 1;
5599230130Smav			device_printf(pdevinfo->dev, "\n");
5600230130Smav			device_printf(pdevinfo->dev, "Record:\n");
5601230130Smav		}
5602230130Smav		device_printf(pdevinfo->dev, "\n");
5603230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
5604230130Smav	}
5605230130Smav}
5606230130Smav
5607230130Smavstatic void
5608230130Smavhdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo)
5609230130Smav{
5610230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5611230130Smav	struct hdaa_widget *w;
5612230130Smav	int i;
5613230130Smav	int printed = 0;
5614230130Smav
5615230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5616230130Smav		w = hdaa_widget_get(devinfo, i);
5617230130Smav		if (w == NULL || w->enable == 0)
5618230130Smav			continue;
5619230130Smav		if (w->ossdev != SOUND_MIXER_IMIX)
5620230130Smav			continue;
5621230451Smav		if (w->bindas != pdevinfo->recas)
5622230451Smav			continue;
5623230130Smav		if (printed == 0) {
5624230130Smav			printed = 1;
5625230130Smav			device_printf(pdevinfo->dev, "\n");
5626230130Smav			device_printf(pdevinfo->dev, "Input Mix:\n");
5627230130Smav		}
5628230130Smav		device_printf(pdevinfo->dev, "\n");
5629230130Smav		hdaa_dump_dst_nid(pdevinfo, i, 0);
5630230130Smav	}
5631230130Smav}
5632230130Smav
5633230130Smavstatic void
5634230130Smavhdaa_dump_pcmchannels(struct hdaa_pcm_devinfo *pdevinfo)
5635230130Smav{
5636230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5637230130Smav	nid_t *nids;
5638230130Smav	int chid, i;
5639230130Smav
5640230130Smav	if (pdevinfo->playas >= 0) {
5641230130Smav		device_printf(pdevinfo->dev, "\n");
5642230130Smav		device_printf(pdevinfo->dev, "Playback:\n");
5643230130Smav		device_printf(pdevinfo->dev, "\n");
5644230130Smav		chid = devinfo->as[pdevinfo->playas].chans[0];
5645230130Smav		hdaa_dump_audio_formats(pdevinfo->dev,
5646230130Smav		    devinfo->chans[chid].supp_stream_formats,
5647230130Smav		    devinfo->chans[chid].supp_pcm_size_rate);
5648230130Smav		for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) {
5649230130Smav			chid = devinfo->as[pdevinfo->playas].chans[i];
5650230130Smav			device_printf(pdevinfo->dev, "            DAC:");
5651230130Smav			for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5652230130Smav				printf(" %d", *nids);
5653230130Smav			printf("\n");
5654230130Smav		}
5655230130Smav	}
5656230130Smav	if (pdevinfo->recas >= 0) {
5657230130Smav		device_printf(pdevinfo->dev, "\n");
5658230130Smav		device_printf(pdevinfo->dev, "Record:\n");
5659230130Smav		device_printf(pdevinfo->dev, "\n");
5660230130Smav		chid = devinfo->as[pdevinfo->recas].chans[0];
5661230130Smav		hdaa_dump_audio_formats(pdevinfo->dev,
5662230130Smav		    devinfo->chans[chid].supp_stream_formats,
5663230130Smav		    devinfo->chans[chid].supp_pcm_size_rate);
5664230130Smav		for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) {
5665230130Smav			chid = devinfo->as[pdevinfo->recas].chans[i];
5666230130Smav			device_printf(pdevinfo->dev, "            DAC:");
5667230130Smav			for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5668230130Smav				printf(" %d", *nids);
5669230130Smav			printf("\n");
5670230130Smav		}
5671230130Smav	}
5672230130Smav}
5673230130Smav
5674230130Smavstatic void
5675230130Smavhdaa_pindump(device_t dev)
5676230130Smav{
5677230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5678230130Smav	struct hdaa_widget *w;
5679230130Smav	uint32_t res, pincap, delay;
5680230130Smav	int i;
5681230130Smav
5682230130Smav	device_printf(dev, "Dumping AFG pins:\n");
5683230130Smav	device_printf(dev, "nid   0x    as seq "
5684230130Smav	    "device       conn  jack    loc        color   misc\n");
5685230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5686230130Smav		w = hdaa_widget_get(devinfo, i);
5687230130Smav		if (w == NULL || w->type !=
5688230130Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5689230130Smav			continue;
5690230130Smav		hdaa_dump_pin_config(w, w->wclass.pin.config);
5691230130Smav		pincap = w->wclass.pin.cap;
5692230130Smav		device_printf(dev, "    Caps: %2s %3s %2s %4s %4s",
5693230130Smav		    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
5694230130Smav		    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
5695230130Smav		    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
5696230130Smav		    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
5697230130Smav		    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
5698230130Smav		if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
5699230130Smav		    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
5700230130Smav			if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
5701230130Smav				delay = 0;
5702230130Smav				hda_command(dev,
5703230130Smav				    HDA_CMD_SET_PIN_SENSE(0, w->nid, 0));
5704230130Smav				do {
5705230130Smav					res = hda_command(dev,
5706230130Smav					    HDA_CMD_GET_PIN_SENSE(0, w->nid));
5707230130Smav					if (res != 0x7fffffff && res != 0xffffffff)
5708230130Smav						break;
5709230130Smav					DELAY(10);
5710230130Smav				} while (++delay < 10000);
5711230130Smav			} else {
5712230130Smav				delay = 0;
5713230130Smav				res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0,
5714230130Smav				    w->nid));
5715230130Smav			}
5716230312Smav			printf(" Sense: 0x%08x (%sconnected%s)", res,
5717230130Smav			    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ?
5718230312Smav			     "" : "dis",
5719230312Smav			    (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) &&
5720230312Smav			     (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ?
5721230312Smav			      ", ELD valid" : "");
5722230130Smav			if (delay > 0)
5723230130Smav				printf(" delay %dus", delay * 10);
5724230130Smav		}
5725230130Smav		printf("\n");
5726230130Smav	}
5727230130Smav	device_printf(dev,
5728230130Smav	    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
5729230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
5730230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
5731230130Smav	    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
5732230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
5733230130Smav	    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
5734230130Smav	hdaa_dump_gpi(devinfo);
5735230130Smav	hdaa_dump_gpio(devinfo);
5736230130Smav	hdaa_dump_gpo(devinfo);
5737230130Smav}
5738230130Smav
5739230130Smavstatic void
5740230130Smavhdaa_configure(device_t dev)
5741230130Smav{
5742230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5743230130Smav	struct hdaa_audio_ctl *ctl;
5744230130Smav	int i;
5745230130Smav
5746230130Smav	HDA_BOOTHVERBOSE(
5747230130Smav		device_printf(dev, "Applying built-in patches...\n");
5748230130Smav	);
5749230130Smav	hdaa_patch(devinfo);
5750230130Smav	HDA_BOOTHVERBOSE(
5751230130Smav		device_printf(dev, "Applying local patches...\n");
5752230130Smav	);
5753230130Smav	hdaa_local_patch(devinfo);
5754230130Smav	hdaa_audio_postprocess(devinfo);
5755230130Smav	HDA_BOOTHVERBOSE(
5756230130Smav		device_printf(dev, "Parsing Ctls...\n");
5757230130Smav	);
5758230130Smav	hdaa_audio_ctl_parse(devinfo);
5759230130Smav	HDA_BOOTHVERBOSE(
5760230130Smav		device_printf(dev, "Disabling nonaudio...\n");
5761230130Smav	);
5762230130Smav	hdaa_audio_disable_nonaudio(devinfo);
5763230130Smav	HDA_BOOTHVERBOSE(
5764230130Smav		device_printf(dev, "Disabling useless...\n");
5765230130Smav	);
5766230130Smav	hdaa_audio_disable_useless(devinfo);
5767230130Smav	HDA_BOOTVERBOSE(
5768230130Smav		device_printf(dev, "Patched pins configuration:\n");
5769230130Smav		hdaa_dump_pin_configs(devinfo);
5770230130Smav	);
5771230130Smav	HDA_BOOTHVERBOSE(
5772230130Smav		device_printf(dev, "Parsing pin associations...\n");
5773230130Smav	);
5774230130Smav	hdaa_audio_as_parse(devinfo);
5775230130Smav	HDA_BOOTHVERBOSE(
5776230130Smav		device_printf(dev, "Building AFG tree...\n");
5777230130Smav	);
5778230130Smav	hdaa_audio_build_tree(devinfo);
5779230130Smav	HDA_BOOTHVERBOSE(
5780230130Smav		device_printf(dev, "Disabling unassociated "
5781230130Smav		    "widgets...\n");
5782230130Smav	);
5783230130Smav	hdaa_audio_disable_unas(devinfo);
5784230130Smav	HDA_BOOTHVERBOSE(
5785230130Smav		device_printf(dev, "Disabling nonselected "
5786230130Smav		    "inputs...\n");
5787230130Smav	);
5788230130Smav	hdaa_audio_disable_notselected(devinfo);
5789230130Smav	HDA_BOOTHVERBOSE(
5790230130Smav		device_printf(dev, "Disabling useless...\n");
5791230130Smav	);
5792230130Smav	hdaa_audio_disable_useless(devinfo);
5793230130Smav	HDA_BOOTHVERBOSE(
5794230130Smav		device_printf(dev, "Disabling "
5795230130Smav		    "crossassociatement connections...\n");
5796230130Smav	);
5797230130Smav	hdaa_audio_disable_crossas(devinfo);
5798230130Smav	HDA_BOOTHVERBOSE(
5799230130Smav		device_printf(dev, "Disabling useless...\n");
5800230130Smav	);
5801230130Smav	hdaa_audio_disable_useless(devinfo);
5802230130Smav	HDA_BOOTHVERBOSE(
5803230130Smav		device_printf(dev, "Binding associations to channels...\n");
5804230130Smav	);
5805230130Smav	hdaa_audio_bind_as(devinfo);
5806230130Smav	HDA_BOOTHVERBOSE(
5807230130Smav		device_printf(dev, "Assigning names to signal sources...\n");
5808230130Smav	);
5809230130Smav	hdaa_audio_assign_names(devinfo);
5810230130Smav	HDA_BOOTHVERBOSE(
5811230451Smav		device_printf(dev, "Preparing PCM devices...\n");
5812230451Smav	);
5813230451Smav	hdaa_prepare_pcms(devinfo);
5814230451Smav	HDA_BOOTHVERBOSE(
5815230130Smav		device_printf(dev, "Assigning mixers to the tree...\n");
5816230130Smav	);
5817230130Smav	hdaa_audio_assign_mixers(devinfo);
5818230130Smav	HDA_BOOTHVERBOSE(
5819230130Smav		device_printf(dev, "Preparing pin controls...\n");
5820230130Smav	);
5821230130Smav	hdaa_audio_prepare_pin_ctrl(devinfo);
5822230130Smav	HDA_BOOTHVERBOSE(
5823230130Smav		device_printf(dev, "AFG commit...\n");
5824230130Smav	);
5825230130Smav	hdaa_audio_commit(devinfo);
5826230130Smav	HDA_BOOTHVERBOSE(
5827230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
5828230130Smav	);
5829230130Smav	hdaa_patch_direct(devinfo);
5830230130Smav	HDA_BOOTHVERBOSE(
5831230551Smav		device_printf(dev, "Pin sense init...\n");
5832230312Smav	);
5833230551Smav	hdaa_sense_init(devinfo);
5834230312Smav	HDA_BOOTHVERBOSE(
5835230130Smav		device_printf(dev, "Creating PCM devices...\n");
5836230130Smav	);
5837230130Smav	hdaa_create_pcms(devinfo);
5838230130Smav
5839230130Smav	HDA_BOOTVERBOSE(
5840230130Smav		if (devinfo->quirks != 0) {
5841230130Smav			device_printf(dev, "FG config/quirks:");
5842230130Smav			for (i = 0; i < HDAA_QUIRKS_TAB_LEN; i++) {
5843230130Smav				if ((devinfo->quirks &
5844230130Smav				    hdaa_quirks_tab[i].value) ==
5845230130Smav				    hdaa_quirks_tab[i].value)
5846230130Smav					printf(" %s", hdaa_quirks_tab[i].key);
5847230130Smav			}
5848230130Smav			printf("\n");
5849230130Smav		}
5850230130Smav
5851230130Smav		device_printf(dev, "\n");
5852230130Smav		device_printf(dev, "+-------------------+\n");
5853230130Smav		device_printf(dev, "| DUMPING HDA NODES |\n");
5854230130Smav		device_printf(dev, "+-------------------+\n");
5855230130Smav		hdaa_dump_nodes(devinfo);
5856230130Smav	);
5857230130Smav
5858230130Smav	HDA_BOOTHVERBOSE(
5859230130Smav		device_printf(dev, "\n");
5860230130Smav		device_printf(dev, "+------------------------+\n");
5861230130Smav		device_printf(dev, "| DUMPING HDA AMPLIFIERS |\n");
5862230130Smav		device_printf(dev, "+------------------------+\n");
5863230130Smav		device_printf(dev, "\n");
5864230130Smav		i = 0;
5865230130Smav		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5866230130Smav			device_printf(dev, "%3d: nid %3d %s (%s) index %d", i,
5867230130Smav			    (ctl->widget != NULL) ? ctl->widget->nid : -1,
5868230130Smav			    (ctl->ndir == HDAA_CTL_IN)?"in ":"out",
5869230130Smav			    (ctl->dir == HDAA_CTL_IN)?"in ":"out",
5870230130Smav			    ctl->index);
5871230130Smav			if (ctl->childwidget != NULL)
5872230130Smav				printf(" cnid %3d", ctl->childwidget->nid);
5873230130Smav			else
5874230130Smav				printf("         ");
5875230130Smav			printf(" ossmask=0x%08x\n",
5876230130Smav			    ctl->ossmask);
5877230130Smav			device_printf(dev,
5878230130Smav			    "       mute: %d step: %3d size: %3d off: %3d%s\n",
5879230130Smav			    ctl->mute, ctl->step, ctl->size, ctl->offset,
5880230130Smav			    (ctl->enable == 0) ? " [DISABLED]" :
5881230130Smav			    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
5882230130Smav		}
5883230130Smav	);
5884230130Smav
5885230130Smav	HDA_BOOTVERBOSE(
5886230130Smav		device_printf(dev, "\n");
5887230130Smav	);
5888230130Smav}
5889230130Smav
5890230130Smavstatic void
5891230130Smavhdaa_unconfigure(device_t dev)
5892230130Smav{
5893230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5894230130Smav	struct hdaa_widget *w;
5895230130Smav	int i, j;
5896230130Smav
5897230130Smav	HDA_BOOTHVERBOSE(
5898230551Smav		device_printf(dev, "Pin sense deinit...\n");
5899230130Smav	);
5900230551Smav	hdaa_sense_deinit(devinfo);
5901230130Smav	free(devinfo->ctl, M_HDAA);
5902230130Smav	devinfo->ctl = NULL;
5903230130Smav	devinfo->ctlcnt = 0;
5904230130Smav	free(devinfo->as, M_HDAA);
5905230130Smav	devinfo->as = NULL;
5906230130Smav	devinfo->ascnt = 0;
5907230130Smav	free(devinfo->devs, M_HDAA);
5908230130Smav	devinfo->devs = NULL;
5909230130Smav	devinfo->num_devs = 0;
5910230130Smav	free(devinfo->chans, M_HDAA);
5911230130Smav	devinfo->chans = NULL;
5912230130Smav	devinfo->num_chans = 0;
5913230130Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5914230130Smav		w = hdaa_widget_get(devinfo, i);
5915230130Smav		if (w == NULL)
5916230130Smav			continue;
5917230130Smav		w->enable = 1;
5918230130Smav		w->selconn = -1;
5919230130Smav		w->pflags = 0;
5920230130Smav		w->bindas = -1;
5921230130Smav		w->bindseqmask = 0;
5922230130Smav		w->ossdev = -1;
5923230130Smav		w->ossmask = 0;
5924230130Smav		for (j = 0; j < w->nconns; j++)
5925230130Smav			w->connsenable[j] = 1;
5926230451Smav		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5927230451Smav			w->wclass.pin.config = w->wclass.pin.newconf;
5928230312Smav		if (w->eld != NULL) {
5929230312Smav			w->eld_len = 0;
5930230312Smav			free(w->eld, M_HDAA);
5931230312Smav			w->eld = NULL;
5932230312Smav		}
5933230130Smav	}
5934230130Smav}
5935230130Smav
5936230130Smavstatic int
5937230130Smavhdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS)
5938230130Smav{
5939230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5940230130Smav	device_t dev = devinfo->dev;
5941230130Smav	char buf[256];
5942230130Smav	int n = 0, i, numgpi;
5943230130Smav	uint32_t data = 0;
5944230130Smav
5945230130Smav	buf[0] = 0;
5946230130Smav	hdaa_lock(devinfo);
5947230130Smav	numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap);
5948230130Smav	if (numgpi > 0) {
5949230130Smav		data = hda_command(dev,
5950230130Smav		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
5951230130Smav	}
5952230130Smav	hdaa_unlock(devinfo);
5953230130Smav	for (i = 0; i < numgpi; i++) {
5954230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
5955230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
5956230130Smav	}
5957230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5958230130Smav}
5959230130Smav
5960230130Smavstatic int
5961230130Smavhdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS)
5962230130Smav{
5963230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5964230130Smav	device_t dev = devinfo->dev;
5965230130Smav	char buf[256];
5966230130Smav	int n = 0, i, numgpio;
5967230130Smav	uint32_t data = 0, enable = 0, dir = 0;
5968230130Smav
5969230130Smav	buf[0] = 0;
5970230130Smav	hdaa_lock(devinfo);
5971230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5972230130Smav	if (numgpio > 0) {
5973230130Smav		data = hda_command(dev,
5974230130Smav		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
5975230130Smav		enable = hda_command(dev,
5976230130Smav		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
5977230130Smav		dir = hda_command(dev,
5978230130Smav		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
5979230130Smav	}
5980230130Smav	hdaa_unlock(devinfo);
5981230130Smav	for (i = 0; i < numgpio; i++) {
5982230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=",
5983230130Smav		    n != 0 ? " " : "", i);
5984230130Smav		if ((enable & (1 << i)) == 0) {
5985230130Smav			n += snprintf(buf + n, sizeof(buf) - n, "disabled");
5986230130Smav			continue;
5987230130Smav		}
5988230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)",
5989230130Smav		    ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1));
5990230130Smav	}
5991230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5992230130Smav}
5993230130Smav
5994230130Smavstatic int
5995230130Smavhdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS)
5996230130Smav{
5997230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5998230130Smav	char buf[256];
5999230130Smav	int error, n = 0, i, numgpio;
6000230130Smav	uint32_t gpio, x;
6001230130Smav
6002230130Smav	gpio = devinfo->newgpio;
6003230130Smav	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6004230130Smav	buf[0] = 0;
6005230130Smav	for (i = 0; i < numgpio; i++) {
6006230130Smav		x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6007230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6008230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6009230130Smav	}
6010230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6011230130Smav	if (error != 0 || req->newptr == NULL)
6012230130Smav		return (error);
6013230130Smav	if (strncmp(buf, "0x", 2) == 0)
6014230130Smav		gpio = strtol(buf + 2, NULL, 16);
6015230130Smav	else
6016230130Smav		gpio = hdaa_gpio_patch(gpio, buf);
6017230130Smav	hdaa_lock(devinfo);
6018230130Smav	devinfo->newgpio = devinfo->gpio = gpio;
6019230130Smav	hdaa_gpio_commit(devinfo);
6020230130Smav	hdaa_unlock(devinfo);
6021230130Smav	return (0);
6022230130Smav}
6023230130Smav
6024230130Smavstatic int
6025230130Smavhdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS)
6026230130Smav{
6027230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6028230130Smav	device_t dev = devinfo->dev;
6029230130Smav	char buf[256];
6030230130Smav	int n = 0, i, numgpo;
6031230130Smav	uint32_t data = 0;
6032230130Smav
6033230130Smav	buf[0] = 0;
6034230130Smav	hdaa_lock(devinfo);
6035230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6036230130Smav	if (numgpo > 0) {
6037230130Smav		data = hda_command(dev,
6038230130Smav		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
6039230130Smav	}
6040230130Smav	hdaa_unlock(devinfo);
6041230130Smav	for (i = 0; i < numgpo; i++) {
6042230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6043230130Smav		    n != 0 ? " " : "", i, ((data >> i) & 1));
6044230130Smav	}
6045230130Smav	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6046230130Smav}
6047230130Smav
6048230130Smavstatic int
6049230130Smavhdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS)
6050230130Smav{
6051230130Smav	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6052230130Smav	char buf[256];
6053230130Smav	int error, n = 0, i, numgpo;
6054230130Smav	uint32_t gpo, x;
6055230130Smav
6056230130Smav	gpo = devinfo->newgpo;
6057230130Smav	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6058230130Smav	buf[0] = 0;
6059230130Smav	for (i = 0; i < numgpo; i++) {
6060230130Smav		x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6061230130Smav		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6062230130Smav		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6063230130Smav	}
6064230130Smav	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6065230130Smav	if (error != 0 || req->newptr == NULL)
6066230130Smav		return (error);
6067230130Smav	if (strncmp(buf, "0x", 2) == 0)
6068230130Smav		gpo = strtol(buf + 2, NULL, 16);
6069230130Smav	else
6070230130Smav		gpo = hdaa_gpio_patch(gpo, buf);
6071230130Smav	hdaa_lock(devinfo);
6072230130Smav	devinfo->newgpo = devinfo->gpo = gpo;
6073230130Smav	hdaa_gpo_commit(devinfo);
6074230130Smav	hdaa_unlock(devinfo);
6075230130Smav	return (0);
6076230130Smav}
6077230130Smav
6078230130Smavstatic int
6079230130Smavhdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS)
6080230130Smav{
6081230130Smav	device_t dev;
6082230130Smav	struct hdaa_devinfo *devinfo;
6083230130Smav	int error, val;
6084230130Smav
6085230130Smav	dev = oidp->oid_arg1;
6086230130Smav	devinfo = device_get_softc(dev);
6087230130Smav	if (devinfo == NULL)
6088230130Smav		return (EINVAL);
6089230130Smav	val = 0;
6090230130Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6091230130Smav	if (error != 0 || req->newptr == NULL || val == 0)
6092230130Smav		return (error);
6093230130Smav
6094230130Smav	HDA_BOOTHVERBOSE(
6095230130Smav		device_printf(dev, "Reconfiguration...\n");
6096230130Smav	);
6097230130Smav	if ((error = device_delete_children(dev)) != 0)
6098230130Smav		return (error);
6099230130Smav	hdaa_lock(devinfo);
6100230130Smav	hdaa_unconfigure(dev);
6101230130Smav	hdaa_configure(dev);
6102230130Smav	hdaa_unlock(devinfo);
6103230130Smav	bus_generic_attach(dev);
6104230130Smav	HDA_BOOTHVERBOSE(
6105230130Smav		device_printf(dev, "Reconfiguration done\n");
6106230130Smav	);
6107230130Smav	return (0);
6108230130Smav}
6109230130Smav
6110230130Smavstatic int
6111230130Smavhdaa_suspend(device_t dev)
6112230130Smav{
6113230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6114230130Smav	int i;
6115230130Smav
6116230130Smav	HDA_BOOTHVERBOSE(
6117230130Smav		device_printf(dev, "Suspend...\n");
6118230130Smav	);
6119230130Smav	hdaa_lock(devinfo);
6120230130Smav	HDA_BOOTHVERBOSE(
6121230130Smav		device_printf(dev, "Stop streams...\n");
6122230130Smav	);
6123230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6124230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) {
6125230130Smav			devinfo->chans[i].flags |= HDAA_CHN_SUSPEND;
6126230130Smav			hdaa_channel_stop(&devinfo->chans[i]);
6127230130Smav		}
6128230130Smav	}
6129230130Smav	HDA_BOOTHVERBOSE(
6130230130Smav		device_printf(dev, "Power down FG"
6131230130Smav		    " nid=%d to the D3 state...\n",
6132230130Smav		    devinfo->nid);
6133230130Smav	);
6134230130Smav	hda_command(devinfo->dev,
6135230130Smav	    HDA_CMD_SET_POWER_STATE(0,
6136230130Smav	    devinfo->nid, HDA_CMD_POWER_STATE_D3));
6137230130Smav	callout_stop(&devinfo->poll_jack);
6138230130Smav	hdaa_unlock(devinfo);
6139230130Smav	callout_drain(&devinfo->poll_jack);
6140230130Smav	HDA_BOOTHVERBOSE(
6141230130Smav		device_printf(dev, "Suspend done\n");
6142230130Smav	);
6143230130Smav	return (0);
6144230130Smav}
6145230130Smav
6146230130Smavstatic int
6147230130Smavhdaa_resume(device_t dev)
6148230130Smav{
6149230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6150230130Smav	int i;
6151230130Smav
6152230130Smav	HDA_BOOTHVERBOSE(
6153230130Smav		device_printf(dev, "Resume...\n");
6154230130Smav	);
6155230130Smav	hdaa_lock(devinfo);
6156230130Smav	HDA_BOOTHVERBOSE(
6157230130Smav		device_printf(dev, "Power up audio FG nid=%d...\n",
6158230130Smav		    devinfo->nid);
6159230130Smav	);
6160230130Smav	hdaa_powerup(devinfo);
6161230130Smav	HDA_BOOTHVERBOSE(
6162230130Smav		device_printf(dev, "AFG commit...\n");
6163230130Smav	);
6164230130Smav	hdaa_audio_commit(devinfo);
6165230130Smav	HDA_BOOTHVERBOSE(
6166230130Smav		device_printf(dev, "Applying direct built-in patches...\n");
6167230130Smav	);
6168230130Smav	hdaa_patch_direct(devinfo);
6169230130Smav	HDA_BOOTHVERBOSE(
6170230551Smav		device_printf(dev, "Pin sense init...\n");
6171230130Smav	);
6172230551Smav	hdaa_sense_init(devinfo);
6173230130Smav
6174230130Smav	hdaa_unlock(devinfo);
6175230130Smav	for (i = 0; i < devinfo->num_devs; i++) {
6176230130Smav		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
6177230130Smav		HDA_BOOTHVERBOSE(
6178230130Smav			device_printf(pdevinfo->dev,
6179230130Smav			    "OSS mixer reinitialization...\n");
6180230130Smav		);
6181230130Smav		if (mixer_reinit(pdevinfo->dev) == -1)
6182230130Smav			device_printf(pdevinfo->dev,
6183230130Smav			    "unable to reinitialize the mixer\n");
6184230130Smav	}
6185230130Smav	hdaa_lock(devinfo);
6186230130Smav	HDA_BOOTHVERBOSE(
6187230130Smav		device_printf(dev, "Start streams...\n");
6188230130Smav	);
6189230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6190230130Smav		if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) {
6191230130Smav			devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND;
6192230130Smav			hdaa_channel_start(&devinfo->chans[i]);
6193230130Smav		}
6194230130Smav	}
6195230130Smav	hdaa_unlock(devinfo);
6196230130Smav	HDA_BOOTHVERBOSE(
6197230130Smav		device_printf(dev, "Resume done\n");
6198230130Smav	);
6199230130Smav	return (0);
6200230130Smav}
6201230130Smav
6202230130Smavstatic int
6203230130Smavhdaa_probe(device_t dev)
6204230130Smav{
6205230130Smav	char buf[128];
6206230130Smav
6207230130Smav	if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
6208230130Smav		return (ENXIO);
6209230130Smav	snprintf(buf, sizeof(buf), "%s Audio Function Group",
6210230130Smav	    device_get_desc(device_get_parent(dev)));
6211230130Smav	device_set_desc_copy(dev, buf);
6212230130Smav	return (BUS_PROBE_DEFAULT);
6213230130Smav}
6214230130Smav
6215230130Smavstatic int
6216230130Smavhdaa_attach(device_t dev)
6217230130Smav{
6218230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6219230130Smav	uint32_t res;
6220230130Smav	nid_t nid = hda_get_node_id(dev);
6221230130Smav
6222230130Smav	devinfo->dev = dev;
6223230130Smav	devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
6224230130Smav	devinfo->nid = nid;
6225230130Smav	devinfo->newquirks = -1;
6226230130Smav	devinfo->newgpio = -1;
6227230130Smav	devinfo->newgpo = -1;
6228230130Smav	callout_init(&devinfo->poll_jack, CALLOUT_MPSAFE);
6229230130Smav	devinfo->poll_ival = hz;
6230230130Smav
6231230130Smav	hdaa_lock(devinfo);
6232230130Smav	res = hda_command(dev,
6233230130Smav	    HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT));
6234230130Smav	hdaa_unlock(devinfo);
6235230130Smav
6236230130Smav	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
6237230130Smav	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
6238230130Smav	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
6239230130Smav
6240230130Smav	HDA_BOOTVERBOSE(
6241230130Smav		device_printf(dev,
6242230130Smav		    "Audio Function Group at nid=%d: %d subnodes %d-%d\n",
6243230130Smav		    nid, devinfo->nodecnt,
6244230130Smav		    devinfo->startnode, devinfo->endnode - 1);
6245230130Smav	);
6246230130Smav
6247230130Smav	if (devinfo->nodecnt > 0)
6248230130Smav		devinfo->widget = (struct hdaa_widget *)malloc(
6249230130Smav		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA,
6250230130Smav		    M_WAITOK | M_ZERO);
6251230130Smav	else
6252230130Smav		devinfo->widget = NULL;
6253230130Smav
6254230130Smav	hdaa_lock(devinfo);
6255230130Smav	HDA_BOOTHVERBOSE(
6256230130Smav		device_printf(dev, "Powering up...\n");
6257230130Smav	);
6258230130Smav	hdaa_powerup(devinfo);
6259230130Smav	HDA_BOOTHVERBOSE(
6260230130Smav		device_printf(dev, "Parsing audio FG...\n");
6261230130Smav	);
6262230130Smav	hdaa_audio_parse(devinfo);
6263230130Smav	HDA_BOOTVERBOSE(
6264230130Smav		device_printf(dev, "Original pins configuration:\n");
6265230130Smav		hdaa_dump_pin_configs(devinfo);
6266230130Smav	);
6267230130Smav	hdaa_configure(dev);
6268230130Smav	hdaa_unlock(devinfo);
6269230130Smav
6270230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6271230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6272230130Smav	    "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6273230130Smav	    &devinfo->newquirks, sizeof(&devinfo->newquirks),
6274230130Smav	    hdaa_sysctl_quirks, "A", "Configuration options");
6275230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6276230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6277230130Smav	    "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6278230130Smav	    devinfo, sizeof(devinfo),
6279230130Smav	    hdaa_sysctl_gpi_state, "A", "GPI state");
6280230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6281230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6282230130Smav	    "gpio_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6283230130Smav	    devinfo, sizeof(devinfo),
6284230130Smav	    hdaa_sysctl_gpio_state, "A", "GPIO state");
6285230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6286230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6287230130Smav	    "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6288230130Smav	    devinfo, sizeof(devinfo),
6289230130Smav	    hdaa_sysctl_gpio_config, "A", "GPIO configuration");
6290230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6291230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6292230130Smav	    "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6293230130Smav	    devinfo, sizeof(devinfo),
6294230130Smav	    hdaa_sysctl_gpo_state, "A", "GPO state");
6295230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6296230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6297230130Smav	    "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6298230130Smav	    devinfo, sizeof(devinfo),
6299230130Smav	    hdaa_sysctl_gpo_config, "A", "GPO configuration");
6300230130Smav	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6301230130Smav	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6302230130Smav	    "reconfig", CTLTYPE_INT | CTLFLAG_RW,
6303230130Smav	    dev, sizeof(dev),
6304230130Smav	    hdaa_sysctl_reconfig, "I", "Reprocess configuration");
6305230130Smav	bus_generic_attach(dev);
6306230130Smav	return (0);
6307230130Smav}
6308230130Smav
6309230130Smavstatic int
6310230130Smavhdaa_detach(device_t dev)
6311230130Smav{
6312230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6313230130Smav	int error;
6314230130Smav
6315230130Smav	if ((error = device_delete_children(dev)) != 0)
6316230130Smav		return (error);
6317230130Smav
6318230130Smav	hdaa_lock(devinfo);
6319230130Smav	hdaa_unconfigure(dev);
6320230130Smav	devinfo->poll_ival = 0;
6321230130Smav	callout_stop(&devinfo->poll_jack);
6322230130Smav	hdaa_unlock(devinfo);
6323230130Smav	callout_drain(&devinfo->poll_jack);
6324230130Smav
6325230130Smav	free(devinfo->widget, M_HDAA);
6326230130Smav	return (0);
6327230130Smav}
6328230130Smav
6329230130Smavstatic int
6330230130Smavhdaa_print_child(device_t dev, device_t child)
6331230130Smav{
6332230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6333230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6334230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6335230130Smav	struct hdaa_audio_as *as;
6336230130Smav	int retval, first = 1, i;
6337230130Smav
6338230130Smav	retval = bus_print_child_header(dev, child);
6339230130Smav	retval += printf(" at nid ");
6340230130Smav	if (pdevinfo->playas >= 0) {
6341230130Smav		as = &devinfo->as[pdevinfo->playas];
6342230130Smav		for (i = 0; i < 16; i++) {
6343230130Smav			if (as->pins[i] <= 0)
6344230130Smav				continue;
6345230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6346230130Smav			first = 0;
6347230130Smav		}
6348230130Smav	}
6349230130Smav	if (pdevinfo->recas >= 0) {
6350230130Smav		if (pdevinfo->playas >= 0) {
6351230130Smav			retval += printf(" and ");
6352230130Smav			first = 1;
6353230130Smav		}
6354230130Smav		as = &devinfo->as[pdevinfo->recas];
6355230130Smav		for (i = 0; i < 16; i++) {
6356230130Smav			if (as->pins[i] <= 0)
6357230130Smav				continue;
6358230130Smav			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6359230130Smav			first = 0;
6360230130Smav		}
6361230130Smav	}
6362230130Smav	retval += bus_print_child_footer(dev, child);
6363230130Smav
6364230130Smav	return (retval);
6365230130Smav}
6366230130Smav
6367230130Smavstatic int
6368230130Smavhdaa_child_location_str(device_t dev, device_t child, char *buf,
6369230130Smav    size_t buflen)
6370230130Smav{
6371230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6372230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6373230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6374230130Smav	struct hdaa_audio_as *as;
6375230130Smav	int first = 1, i, len = 0;
6376230130Smav
6377230130Smav	len += snprintf(buf + len, buflen - len, "nid=");
6378230130Smav	if (pdevinfo->playas >= 0) {
6379230130Smav		as = &devinfo->as[pdevinfo->playas];
6380230130Smav		for (i = 0; i < 16; i++) {
6381230130Smav			if (as->pins[i] <= 0)
6382230130Smav				continue;
6383230130Smav			len += snprintf(buf + len, buflen - len,
6384230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6385230130Smav			first = 0;
6386230130Smav		}
6387230130Smav	}
6388230130Smav	if (pdevinfo->recas >= 0) {
6389230130Smav		as = &devinfo->as[pdevinfo->recas];
6390230130Smav		for (i = 0; i < 16; i++) {
6391230130Smav			if (as->pins[i] <= 0)
6392230130Smav				continue;
6393230130Smav			len += snprintf(buf + len, buflen - len,
6394230130Smav			    "%s%d", first ? "" : ",", as->pins[i]);
6395230130Smav			first = 0;
6396230130Smav		}
6397230130Smav	}
6398230130Smav	return (0);
6399230130Smav}
6400230130Smav
6401230130Smavstatic void
6402230130Smavhdaa_stream_intr(device_t dev, int dir, int stream)
6403230130Smav{
6404230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6405230130Smav	struct hdaa_chan *ch;
6406230130Smav	int i;
6407230130Smav
6408230130Smav	for (i = 0; i < devinfo->num_chans; i++) {
6409230130Smav		ch = &devinfo->chans[i];
6410230130Smav		if (!(ch->flags & HDAA_CHN_RUNNING))
6411230130Smav			continue;
6412230130Smav		if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) &&
6413230130Smav		    ch->sid == stream) {
6414230130Smav			hdaa_unlock(devinfo);
6415230130Smav			chn_intr(ch->c);
6416230130Smav			hdaa_lock(devinfo);
6417230130Smav		}
6418230130Smav	}
6419230130Smav}
6420230130Smav
6421230130Smavstatic void
6422230130Smavhdaa_unsol_intr(device_t dev, uint32_t resp)
6423230130Smav{
6424230130Smav	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6425230312Smav	struct hdaa_widget *w;
6426230312Smav	int i, tag, flags;
6427230130Smav
6428230312Smav	HDA_BOOTHVERBOSE(
6429230312Smav		device_printf(dev, "Unsolicited response %08x\n", resp);
6430230312Smav	);
6431230130Smav	tag = resp >> 26;
6432230312Smav	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6433230312Smav		w = hdaa_widget_get(devinfo, i);
6434230312Smav		if (w == NULL || w->enable == 0 || w->type !=
6435230312Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6436230312Smav			continue;
6437230312Smav		if (w->unsol != tag)
6438230312Smav			continue;
6439230312Smav		if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
6440230312Smav		    HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
6441230312Smav			flags = resp & 0x03;
6442230312Smav		else
6443230312Smav			flags = 0x01;
6444230312Smav		if (flags & 0x01)
6445230551Smav			hdaa_presence_handler(w);
6446230312Smav		if (flags & 0x02)
6447230312Smav			hdaa_eld_handler(w);
6448230130Smav	}
6449230130Smav}
6450230130Smav
6451230130Smavstatic device_method_t hdaa_methods[] = {
6452230130Smav	/* device interface */
6453230130Smav	DEVMETHOD(device_probe,		hdaa_probe),
6454230130Smav	DEVMETHOD(device_attach,	hdaa_attach),
6455230130Smav	DEVMETHOD(device_detach,	hdaa_detach),
6456230130Smav	DEVMETHOD(device_suspend,	hdaa_suspend),
6457230130Smav	DEVMETHOD(device_resume,	hdaa_resume),
6458230130Smav	/* Bus interface */
6459230130Smav	DEVMETHOD(bus_print_child,	hdaa_print_child),
6460230130Smav	DEVMETHOD(bus_child_location_str, hdaa_child_location_str),
6461230130Smav	DEVMETHOD(hdac_stream_intr,	hdaa_stream_intr),
6462230130Smav	DEVMETHOD(hdac_unsol_intr,	hdaa_unsol_intr),
6463230130Smav	DEVMETHOD(hdac_pindump,		hdaa_pindump),
6464230130Smav	{ 0, 0 }
6465230130Smav};
6466230130Smav
6467230130Smavstatic driver_t hdaa_driver = {
6468230130Smav	"hdaa",
6469230130Smav	hdaa_methods,
6470230130Smav	sizeof(struct hdaa_devinfo),
6471230130Smav};
6472230130Smav
6473230130Smavstatic devclass_t hdaa_devclass;
6474230130Smav
6475230130SmavDRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, 0, 0);
6476230130Smav
6477230130Smavstatic void
6478230130Smavhdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid,
6479230130Smav    char *buf, int buflen)
6480230130Smav{
6481230130Smav	struct hdaa_audio_as *as;
6482230130Smav	int c;
6483230130Smav
6484230130Smav	as = &devinfo->as[asid];
6485230130Smav	c = devinfo->chans[as->chans[0]].channels;
6486230130Smav	if (c == 1)
6487230130Smav		snprintf(buf, buflen, "mono");
6488230585Smav	else if (c == 2) {
6489230585Smav		if (as->hpredir < 0)
6490230585Smav			buf[0] = 0;
6491230585Smav		else
6492230585Smav			snprintf(buf, buflen, "2.0");
6493230585Smav	} else if (as->pinset == 0x0003)
6494230130Smav		snprintf(buf, buflen, "3.1");
6495230130Smav	else if (as->pinset == 0x0005 || as->pinset == 0x0011)
6496230130Smav		snprintf(buf, buflen, "4.0");
6497230130Smav	else if (as->pinset == 0x0007 || as->pinset == 0x0013)
6498230130Smav		snprintf(buf, buflen, "5.1");
6499230130Smav	else if (as->pinset == 0x0017)
6500230130Smav		snprintf(buf, buflen, "7.1");
6501230130Smav	else
6502230130Smav		snprintf(buf, buflen, "%dch", c);
6503230585Smav	if (as->hpredir >= 0)
6504230585Smav		strlcat(buf, "+HP", buflen);
6505230130Smav}
6506230130Smav
6507230130Smavstatic int
6508230585Smavhdaa_chan_type(struct hdaa_devinfo *devinfo, int asid)
6509230585Smav{
6510230585Smav	struct hdaa_audio_as *as;
6511230585Smav	struct hdaa_widget *w;
6512230585Smav	int i, t = -1, t1;
6513230585Smav
6514230585Smav	as = &devinfo->as[asid];
6515230585Smav	for (i = 0; i < 16; i++) {
6516230585Smav		w = hdaa_widget_get(devinfo, as->pins[i]);
6517230585Smav		if (w == NULL || w->enable == 0 || w->type !=
6518230585Smav		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6519230585Smav			continue;
6520230585Smav		t1 = HDA_CONFIG_DEFAULTCONF_DEVICE(w->wclass.pin.config);
6521230585Smav		if (t == -1)
6522230585Smav			t = t1;
6523230585Smav		else if (t != t1) {
6524230585Smav			t = -2;
6525230585Smav			break;
6526230585Smav		}
6527230585Smav	}
6528230585Smav	return (t);
6529230585Smav}
6530230585Smav
6531230585Smavstatic int
6532230488Smavhdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS)
6533230488Smav{
6534230488Smav	struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1;
6535230488Smav	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
6536230488Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6537230488Smav	struct hdaa_chan *ch;
6538230488Smav	int error, val, i;
6539230488Smav	uint32_t pcmcap;
6540230488Smav
6541230488Smav	ch = &devinfo->chans[as->chans[0]];
6542230488Smav	val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 :
6543230488Smav	    ((ch->bit32 == 2) ? 20 : 0));
6544230488Smav	error = sysctl_handle_int(oidp, &val, 0, req);
6545230488Smav	if (error != 0 || req->newptr == NULL)
6546230488Smav		return (error);
6547230488Smav	pcmcap = ch->supp_pcm_size_rate;
6548230488Smav	if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6549230488Smav		ch->bit32 = 4;
6550230488Smav	else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6551230488Smav		ch->bit32 = 3;
6552230488Smav	else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6553230488Smav		ch->bit32 = 2;
6554230488Smav	else
6555230488Smav		return (EINVAL);
6556230488Smav	for (i = 1; i < as->num_chans; i++)
6557230488Smav		devinfo->chans[as->chans[i]].bit32 = ch->bit32;
6558230488Smav	return (0);
6559230488Smav}
6560230488Smav
6561230488Smavstatic int
6562230130Smavhdaa_pcm_probe(device_t dev)
6563230130Smav{
6564230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6565230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6566230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6567230130Smav	char chans1[8], chans2[8];
6568230130Smav	char buf[128];
6569230585Smav	int loc1, loc2, t1, t2;
6570230130Smav
6571230130Smav	if (pdevinfo->playas >= 0)
6572230130Smav		loc1 = devinfo->as[pdevinfo->playas].location;
6573230130Smav	else
6574230130Smav		loc1 = devinfo->as[pdevinfo->recas].location;
6575230130Smav	if (pdevinfo->recas >= 0)
6576230130Smav		loc2 = devinfo->as[pdevinfo->recas].location;
6577230130Smav	else
6578230130Smav		loc2 = loc1;
6579230130Smav	if (loc1 != loc2)
6580230130Smav		loc1 = -2;
6581230130Smav	if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0')
6582230130Smav		loc1 = -2;
6583230130Smav	chans1[0] = 0;
6584230130Smav	chans2[0] = 0;
6585230585Smav	t1 = t2 = -1;
6586230585Smav	if (pdevinfo->playas >= 0) {
6587230130Smav		hdaa_chan_formula(devinfo, pdevinfo->playas,
6588230130Smav		    chans1, sizeof(chans1));
6589230585Smav		t1 = hdaa_chan_type(devinfo, pdevinfo->playas);
6590230585Smav	}
6591230585Smav	if (pdevinfo->recas >= 0) {
6592230130Smav		hdaa_chan_formula(devinfo, pdevinfo->recas,
6593230130Smav		    chans2, sizeof(chans2));
6594230585Smav		t2 = hdaa_chan_type(devinfo, pdevinfo->recas);
6595230585Smav	}
6596230130Smav	if (chans1[0] != 0 || chans2[0] != 0) {
6597230130Smav		if (chans1[0] == 0 && pdevinfo->playas >= 0)
6598230130Smav			snprintf(chans1, sizeof(chans1), "2.0");
6599230130Smav		else if (chans2[0] == 0 && pdevinfo->recas >= 0)
6600230130Smav			snprintf(chans2, sizeof(chans2), "2.0");
6601230130Smav		if (strcmp(chans1, chans2) == 0)
6602230130Smav			chans2[0] = 0;
6603230130Smav	}
6604230585Smav	if (t1 == -1)
6605230585Smav		t1 = t2;
6606230585Smav	else if (t2 == -1)
6607230585Smav		t2 = t1;
6608230585Smav	if (t1 != t2)
6609230585Smav		t1 = -2;
6610230585Smav	if (pdevinfo->digital)
6611230585Smav		t1 = -2;
6612230585Smav	snprintf(buf, sizeof(buf), "%s PCM (%s%s%s%s%s%s%s%s%s)",
6613230130Smav	    device_get_desc(device_get_parent(device_get_parent(dev))),
6614230130Smav	    loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "",
6615230507Smav	    (pdevinfo->digital == 0x7)?"HDMI/DP":
6616230507Smav	    ((pdevinfo->digital == 0x5)?"DisplayPort":
6617230507Smav	    ((pdevinfo->digital == 0x3)?"HDMI":
6618230507Smav	    ((pdevinfo->digital)?"Digital":"Analog"))),
6619230130Smav	    chans1[0] ? " " : "", chans1,
6620230585Smav	    chans2[0] ? "/" : "", chans2,
6621230585Smav	    t1 >= 0 ? " " : "", t1 >= 0 ? HDA_DEVS[t1] : "");
6622230130Smav	device_set_desc_copy(dev, buf);
6623230130Smav	return (BUS_PROBE_SPECIFIC);
6624230130Smav}
6625230130Smav
6626230130Smavstatic int
6627230130Smavhdaa_pcm_attach(device_t dev)
6628230130Smav{
6629230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6630230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6631230130Smav	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6632230130Smav	struct hdaa_audio_as *as;
6633230488Smav	struct snddev_info *d;
6634230130Smav	char status[SND_STATUSLEN];
6635230130Smav	int i;
6636230130Smav
6637230130Smav	pdevinfo->chan_size = pcm_getbuffersize(dev,
6638230130Smav	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
6639230130Smav
6640230130Smav	HDA_BOOTVERBOSE(
6641230130Smav		device_printf(dev, "+--------------------------------------+\n");
6642230130Smav		device_printf(dev, "| DUMPING PCM Playback/Record Channels |\n");
6643230130Smav		device_printf(dev, "+--------------------------------------+\n");
6644230130Smav		hdaa_dump_pcmchannels(pdevinfo);
6645230130Smav		device_printf(dev, "\n");
6646230130Smav		device_printf(dev, "+-------------------------------+\n");
6647230130Smav		device_printf(dev, "| DUMPING Playback/Record Paths |\n");
6648230130Smav		device_printf(dev, "+-------------------------------+\n");
6649230130Smav		hdaa_dump_dac(pdevinfo);
6650230130Smav		hdaa_dump_adc(pdevinfo);
6651230130Smav		hdaa_dump_mix(pdevinfo);
6652230130Smav		device_printf(dev, "\n");
6653230130Smav		device_printf(dev, "+-------------------------+\n");
6654230130Smav		device_printf(dev, "| DUMPING Volume Controls |\n");
6655230130Smav		device_printf(dev, "+-------------------------+\n");
6656230130Smav		hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
6657230130Smav		hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
6658230130Smav		hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
6659230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
6660230130Smav		hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
6661230130Smav		hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
6662230130Smav		hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
6663230130Smav		hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
6664230130Smav		hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
6665230130Smav		hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
6666230130Smav		hdaa_dump_ctls(pdevinfo, NULL, 0);
6667230130Smav		device_printf(dev, "\n");
6668230130Smav	);
6669230130Smav
6670230130Smav	if (resource_int_value(device_get_name(dev),
6671230130Smav	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
6672230130Smav		i &= HDA_BLK_ALIGN;
6673230130Smav		if (i < HDA_BLK_MIN)
6674230130Smav			i = HDA_BLK_MIN;
6675230130Smav		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
6676230130Smav		i = 0;
6677230130Smav		while (pdevinfo->chan_blkcnt >> i)
6678230130Smav			i++;
6679230130Smav		pdevinfo->chan_blkcnt = 1 << (i - 1);
6680230130Smav		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
6681230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
6682230130Smav		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
6683230130Smav			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
6684230130Smav	} else
6685230130Smav		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
6686230130Smav
6687230130Smav	/*
6688230130Smav	 * We don't register interrupt handler with snd_setup_intr
6689230130Smav	 * in pcm device. Mark pcm device as MPSAFE manually.
6690230130Smav	 */
6691230130Smav	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
6692230130Smav
6693230130Smav	HDA_BOOTHVERBOSE(
6694230130Smav		device_printf(dev, "OSS mixer initialization...\n");
6695230130Smav	);
6696230130Smav	if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0)
6697230130Smav		device_printf(dev, "Can't register mixer\n");
6698230130Smav
6699230130Smav	HDA_BOOTHVERBOSE(
6700230130Smav		device_printf(dev, "Registering PCM channels...\n");
6701230130Smav	);
6702230130Smav	if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0,
6703230130Smav	    (pdevinfo->recas >= 0)?1:0) != 0)
6704230130Smav		device_printf(dev, "Can't register PCM\n");
6705230130Smav
6706230130Smav	pdevinfo->registered++;
6707230130Smav
6708230488Smav	d = device_get_softc(dev);
6709230130Smav	if (pdevinfo->playas >= 0) {
6710230130Smav		as = &devinfo->as[pdevinfo->playas];
6711230130Smav		for (i = 0; i < as->num_chans; i++)
6712230130Smav			pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class,
6713230130Smav			    &devinfo->chans[as->chans[i]]);
6714230488Smav		SYSCTL_ADD_PROC(&d->play_sysctl_ctx,
6715230488Smav		    SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO,
6716230488Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6717230488Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
6718230488Smav		    "Resolution of 32bit samples (20/24/32bit)");
6719230130Smav	}
6720230130Smav	if (pdevinfo->recas >= 0) {
6721230130Smav		as = &devinfo->as[pdevinfo->recas];
6722230130Smav		for (i = 0; i < as->num_chans; i++)
6723230130Smav			pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class,
6724230130Smav			    &devinfo->chans[as->chans[i]]);
6725230488Smav		SYSCTL_ADD_PROC(&d->rec_sysctl_ctx,
6726230488Smav		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
6727230488Smav		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6728230488Smav		    as, sizeof(as), hdaa_sysctl_32bit, "I",
6729230488Smav		    "Resolution of 32bit samples (20/24/32bit)");
6730230551Smav		pdevinfo->autorecsrc = 2;
6731230554Smav		resource_int_value(device_get_name(dev), device_get_unit(dev),
6732230554Smav		    "rec.autosrc", &pdevinfo->autorecsrc);
6733230551Smav		SYSCTL_ADD_INT(&d->rec_sysctl_ctx,
6734230551Smav		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
6735230551Smav		    "autosrc", CTLTYPE_INT | CTLFLAG_RW,
6736230551Smav		    &pdevinfo->autorecsrc, 0,
6737230551Smav		    "Automatic recording source selection");
6738230130Smav	}
6739230130Smav
6740230551Smav	if (pdevinfo->mixer != NULL) {
6741230551Smav		hdaa_audio_ctl_set_defaults(pdevinfo);
6742230551Smav		if (pdevinfo->recas >= 0) {
6743230551Smav			as = &devinfo->as[pdevinfo->recas];
6744230551Smav			hdaa_lock(devinfo);
6745230551Smav			hdaa_autorecsrc_handler(as, NULL);
6746230551Smav			hdaa_unlock(devinfo);
6747230551Smav		}
6748230551Smav	}
6749230551Smav
6750230130Smav	snprintf(status, SND_STATUSLEN, "on %s %s",
6751230130Smav	    device_get_nameunit(device_get_parent(dev)),
6752230130Smav	    PCM_KLDSTRING(snd_hda));
6753230130Smav	pcm_setstatus(dev, status);
6754230130Smav
6755230130Smav	return (0);
6756230130Smav}
6757230130Smav
6758230130Smavstatic int
6759230130Smavhdaa_pcm_detach(device_t dev)
6760230130Smav{
6761230130Smav	struct hdaa_pcm_devinfo *pdevinfo =
6762230130Smav	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6763230130Smav	int err;
6764230130Smav
6765230130Smav	if (pdevinfo->registered > 0) {
6766230130Smav		err = pcm_unregister(dev);
6767230130Smav		if (err != 0)
6768230130Smav			return (err);
6769230130Smav	}
6770230130Smav
6771230130Smav	return (0);
6772230130Smav}
6773230130Smav
6774230130Smavstatic device_method_t hdaa_pcm_methods[] = {
6775230130Smav	/* device interface */
6776230130Smav	DEVMETHOD(device_probe,		hdaa_pcm_probe),
6777230130Smav	DEVMETHOD(device_attach,	hdaa_pcm_attach),
6778230130Smav	DEVMETHOD(device_detach,	hdaa_pcm_detach),
6779230130Smav	{ 0, 0 }
6780230130Smav};
6781230130Smav
6782230130Smavstatic driver_t hdaa_pcm_driver = {
6783230130Smav	"pcm",
6784230130Smav	hdaa_pcm_methods,
6785230130Smav	PCM_SOFTC_SIZE,
6786230130Smav};
6787230130Smav
6788230130SmavDRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, 0, 0);
6789230130SmavMODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
6790230130SmavMODULE_VERSION(snd_hda, 1);
6791