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