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