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