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