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