hdaa.c revision 230507
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 230507 2012-01-24 14:17:13Z mav $");
49
50#define hdaa_lock(devinfo)	snd_mtxlock((devinfo)->lock)
51#define hdaa_unlock(devinfo)	snd_mtxunlock((devinfo)->lock)
52#define hdaa_lockassert(devinfo) snd_mtxassert((devinfo)->lock)
53#define hdaa_lockowned(devinfo)	mtx_owned((devinfo)->lock)
54
55static const struct {
56	char *key;
57	uint32_t value;
58} hdaa_quirks_tab[] = {
59	{ "softpcmvol", HDAA_QUIRK_SOFTPCMVOL },
60	{ "fixedrate", HDAA_QUIRK_FIXEDRATE },
61	{ "forcestereo", HDAA_QUIRK_FORCESTEREO },
62	{ "eapdinv", HDAA_QUIRK_EAPDINV },
63	{ "senseinv", HDAA_QUIRK_SENSEINV },
64	{ "ivref50", HDAA_QUIRK_IVREF50 },
65	{ "ivref80", HDAA_QUIRK_IVREF80 },
66	{ "ivref100", HDAA_QUIRK_IVREF100 },
67	{ "ovref50", HDAA_QUIRK_OVREF50 },
68	{ "ovref80", HDAA_QUIRK_OVREF80 },
69	{ "ovref100", HDAA_QUIRK_OVREF100 },
70	{ "ivref", HDAA_QUIRK_IVREF },
71	{ "ovref", HDAA_QUIRK_OVREF },
72	{ "vref", HDAA_QUIRK_VREF },
73};
74#define HDAA_QUIRKS_TAB_LEN	\
75		(sizeof(hdaa_quirks_tab) / sizeof(hdaa_quirks_tab[0]))
76
77#define HDA_PARSE_MAXDEPTH	10
78
79MALLOC_DEFINE(M_HDAA, "hdaa", "HDA Audio");
80
81const char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue", "Green", "Red",
82    "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B", "Res.C", "Res.D",
83    "White", "Other"};
84
85const char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD",
86    "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in",
87    "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"};
88
89const char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
90
91const char *HDA_CONNECTORS[16] = {
92    "Unknown", "1/8", "1/4", "ATAPI", "RCA", "Optical", "Digital", "Analog",
93    "DIN", "XLR", "RJ-11", "Combo", "0xc", "0xd", "0xe", "Other" };
94
95const char *HDA_LOCS[64] = {
96    "0x00", "Rear", "Front", "Left", "Right", "Top", "Bottom", "Rear-panel",
97	"Drive-bay", "0x09", "0x0a", "0x0b", "0x0c", "0x0d", "0x0e", "0x0f",
98    "Internal", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "Riser",
99	"0x18", "Onboard", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f",
100    "External", "Ext-Rear", "Ext-Front", "Ext-Left", "Ext-Right", "Ext-Top", "Ext-Bottom", "0x07",
101	"0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f",
102    "Other", "0x31", "0x32", "0x33", "0x34", "0x35", "Other-Bott", "Lid-In",
103	"Lid-Out", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f" };
104
105const char *HDA_GPIO_ACTIONS[8] = {
106    "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"};
107
108const char *HDA_HDMI_CODING_TYPES[18] = {
109    "undefined", "LPCM", "AC-3", "MPEG1", "MP3", "MPEG2", "AAC-LC", "DTS",
110    "ATRAC", "DSD", "E-AC-3", "DTS-HD", "MLP", "DST", "WMAPro", "HE-AAC",
111    "HE-AACv2", "MPEG-Surround"
112};
113
114/* Default */
115static uint32_t hdaa_fmt[] = {
116	SND_FORMAT(AFMT_S16_LE, 2, 0),
117	0
118};
119
120static struct pcmchan_caps hdaa_caps = {48000, 48000, hdaa_fmt, 0};
121
122static const struct {
123	uint32_t	rate;
124	int		valid;
125	uint16_t	base;
126	uint16_t	mul;
127	uint16_t	div;
128} hda_rate_tab[] = {
129	{   8000, 1, 0x0000, 0x0000, 0x0500 },	/* (48000 * 1) / 6 */
130	{   9600, 0, 0x0000, 0x0000, 0x0400 },	/* (48000 * 1) / 5 */
131	{  12000, 0, 0x0000, 0x0000, 0x0300 },	/* (48000 * 1) / 4 */
132	{  16000, 1, 0x0000, 0x0000, 0x0200 },	/* (48000 * 1) / 3 */
133	{  18000, 0, 0x0000, 0x1000, 0x0700 },	/* (48000 * 3) / 8 */
134	{  19200, 0, 0x0000, 0x0800, 0x0400 },	/* (48000 * 2) / 5 */
135	{  24000, 0, 0x0000, 0x0000, 0x0100 },	/* (48000 * 1) / 2 */
136	{  28800, 0, 0x0000, 0x1000, 0x0400 },	/* (48000 * 3) / 5 */
137	{  32000, 1, 0x0000, 0x0800, 0x0200 },	/* (48000 * 2) / 3 */
138	{  36000, 0, 0x0000, 0x1000, 0x0300 },	/* (48000 * 3) / 4 */
139	{  38400, 0, 0x0000, 0x1800, 0x0400 },	/* (48000 * 4) / 5 */
140	{  48000, 1, 0x0000, 0x0000, 0x0000 },	/* (48000 * 1) / 1 */
141	{  64000, 0, 0x0000, 0x1800, 0x0200 },	/* (48000 * 4) / 3 */
142	{  72000, 0, 0x0000, 0x1000, 0x0100 },	/* (48000 * 3) / 2 */
143	{  96000, 1, 0x0000, 0x0800, 0x0000 },	/* (48000 * 2) / 1 */
144	{ 144000, 0, 0x0000, 0x1000, 0x0000 },	/* (48000 * 3) / 1 */
145	{ 192000, 1, 0x0000, 0x1800, 0x0000 },	/* (48000 * 4) / 1 */
146	{   8820, 0, 0x4000, 0x0000, 0x0400 },	/* (44100 * 1) / 5 */
147	{  11025, 1, 0x4000, 0x0000, 0x0300 },	/* (44100 * 1) / 4 */
148	{  12600, 0, 0x4000, 0x0800, 0x0600 },	/* (44100 * 2) / 7 */
149	{  14700, 0, 0x4000, 0x0000, 0x0200 },	/* (44100 * 1) / 3 */
150	{  17640, 0, 0x4000, 0x0800, 0x0400 },	/* (44100 * 2) / 5 */
151	{  18900, 0, 0x4000, 0x1000, 0x0600 },	/* (44100 * 3) / 7 */
152	{  22050, 1, 0x4000, 0x0000, 0x0100 },	/* (44100 * 1) / 2 */
153	{  25200, 0, 0x4000, 0x1800, 0x0600 },	/* (44100 * 4) / 7 */
154	{  26460, 0, 0x4000, 0x1000, 0x0400 },	/* (44100 * 3) / 5 */
155	{  29400, 0, 0x4000, 0x0800, 0x0200 },	/* (44100 * 2) / 3 */
156	{  33075, 0, 0x4000, 0x1000, 0x0300 },	/* (44100 * 3) / 4 */
157	{  35280, 0, 0x4000, 0x1800, 0x0400 },	/* (44100 * 4) / 5 */
158	{  44100, 1, 0x4000, 0x0000, 0x0000 },	/* (44100 * 1) / 1 */
159	{  58800, 0, 0x4000, 0x1800, 0x0200 },	/* (44100 * 4) / 3 */
160	{  66150, 0, 0x4000, 0x1000, 0x0100 },	/* (44100 * 3) / 2 */
161	{  88200, 1, 0x4000, 0x0800, 0x0000 },	/* (44100 * 2) / 1 */
162	{ 132300, 0, 0x4000, 0x1000, 0x0000 },	/* (44100 * 3) / 1 */
163	{ 176400, 1, 0x4000, 0x1800, 0x0000 },	/* (44100 * 4) / 1 */
164};
165#define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
166
167const static char *ossnames[] = SOUND_DEVICE_NAMES;
168
169/****************************************************************************
170 * Function prototypes
171 ****************************************************************************/
172static int	hdaa_pcmchannel_setup(struct hdaa_chan *);
173
174static void	hdaa_widget_connection_select(struct hdaa_widget *, uint8_t);
175static void	hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *,
176						uint32_t, int, int);
177static struct	hdaa_audio_ctl *hdaa_audio_ctl_amp_get(struct hdaa_devinfo *,
178							nid_t, int, int, int);
179static void	hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *,
180				nid_t, int, int, int, int, int, int);
181
182static void	hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf);
183
184static char *
185hdaa_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
186{
187	int i, first = 1;
188
189	bzero(buf, len);
190	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
191		if (mask & (1 << i)) {
192			if (first == 0)
193				strlcat(buf, ", ", len);
194			strlcat(buf, ossnames[i], len);
195			first = 0;
196		}
197	}
198	return (buf);
199}
200
201static struct hdaa_audio_ctl *
202hdaa_audio_ctl_each(struct hdaa_devinfo *devinfo, int *index)
203{
204	if (devinfo == NULL ||
205	    index == NULL || devinfo->ctl == NULL ||
206	    devinfo->ctlcnt < 1 ||
207	    *index < 0 || *index >= devinfo->ctlcnt)
208		return (NULL);
209	return (&devinfo->ctl[(*index)++]);
210}
211
212static struct hdaa_audio_ctl *
213hdaa_audio_ctl_amp_get(struct hdaa_devinfo *devinfo, nid_t nid, int dir,
214						int index, int cnt)
215{
216	struct hdaa_audio_ctl *ctl;
217	int i, found = 0;
218
219	if (devinfo == NULL || devinfo->ctl == NULL)
220		return (NULL);
221
222	i = 0;
223	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
224		if (ctl->enable == 0)
225			continue;
226		if (ctl->widget->nid != nid)
227			continue;
228		if (dir && ctl->ndir != dir)
229			continue;
230		if (index >= 0 && ctl->ndir == HDAA_CTL_IN &&
231		    ctl->dir == ctl->ndir && ctl->index != index)
232			continue;
233		found++;
234		if (found == cnt || cnt <= 0)
235			return (ctl);
236	}
237
238	return (NULL);
239}
240
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			/* Stop audio infoframe transmission. */
1510			hda_command(ch->devinfo->dev,
1511			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1512			hda_command(ch->devinfo->dev,
1513			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0x00));
1514
1515			/* Clear audio infoframe buffer. */
1516			hda_command(ch->devinfo->dev,
1517			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1518			for (k = 0; k < 32; k++)
1519				hda_command(ch->devinfo->dev,
1520				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1521
1522			/* Write HDMI/DisplayPort audio infoframe. */
1523			hda_command(ch->devinfo->dev,
1524			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1525			if (w->eld != NULL && w->eld_len >= 6 &&
1526			    ((w->eld[5] >> 2) & 0x3) == 1) { /* DisplayPort */
1527				hda_command(ch->devinfo->dev,
1528				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1529				hda_command(ch->devinfo->dev,
1530				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x1b));
1531				hda_command(ch->devinfo->dev,
1532				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x44));
1533			} else {	/* HDMI */
1534				hda_command(ch->devinfo->dev,
1535				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1536				hda_command(ch->devinfo->dev,
1537				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x01));
1538				hda_command(ch->devinfo->dev,
1539				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x0a));
1540				csum = 0;
1541				csum -= 0x84 + 0x01 + 0x0a + (totalchn - 1) +
1542				    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1];
1543				hda_command(ch->devinfo->dev,
1544				    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, csum));
1545			}
1546			hda_command(ch->devinfo->dev,
1547			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, totalchn - 1));
1548			hda_command(ch->devinfo->dev,
1549			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1550			hda_command(ch->devinfo->dev,
1551			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1552			hda_command(ch->devinfo->dev,
1553			    HDA_CMD_SET_HDMI_DIP_DATA(0, nid,
1554			    hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1]));
1555
1556			/* Start audio infoframe transmission. */
1557			hda_command(ch->devinfo->dev,
1558			    HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1559			hda_command(ch->devinfo->dev,
1560			    HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0xc0));
1561		}
1562		chn += cchn + 1;
1563	}
1564}
1565
1566/*
1567 * Greatest Common Divisor.
1568 */
1569static unsigned
1570gcd(unsigned a, unsigned b)
1571{
1572	u_int c;
1573
1574	while (b != 0) {
1575		c = a;
1576		a = b;
1577		b = (c % b);
1578	}
1579	return (a);
1580}
1581
1582/*
1583 * Least Common Multiple.
1584 */
1585static unsigned
1586lcm(unsigned a, unsigned b)
1587{
1588
1589	return ((a * b) / gcd(a, b));
1590}
1591
1592static int
1593hdaa_channel_setfragments(kobj_t obj, void *data,
1594					uint32_t blksz, uint32_t blkcnt)
1595{
1596	struct hdaa_chan *ch = data;
1597
1598	blksz -= blksz % lcm(HDA_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
1599
1600	if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
1601		blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
1602	if (blksz < HDA_BLK_MIN)
1603		blksz = HDA_BLK_MIN;
1604	if (blkcnt > HDA_BDL_MAX)
1605		blkcnt = HDA_BDL_MAX;
1606	if (blkcnt < HDA_BDL_MIN)
1607		blkcnt = HDA_BDL_MIN;
1608
1609	while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
1610		if ((blkcnt >> 1) >= HDA_BDL_MIN)
1611			blkcnt >>= 1;
1612		else if ((blksz >> 1) >= HDA_BLK_MIN)
1613			blksz >>= 1;
1614		else
1615			break;
1616	}
1617
1618	if ((sndbuf_getblksz(ch->b) != blksz ||
1619	    sndbuf_getblkcnt(ch->b) != blkcnt) &&
1620	    sndbuf_resize(ch->b, blkcnt, blksz) != 0)
1621		device_printf(ch->devinfo->dev, "%s: failed blksz=%u blkcnt=%u\n",
1622		    __func__, blksz, blkcnt);
1623
1624	ch->blksz = sndbuf_getblksz(ch->b);
1625	ch->blkcnt = sndbuf_getblkcnt(ch->b);
1626
1627	return (0);
1628}
1629
1630static uint32_t
1631hdaa_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
1632{
1633	struct hdaa_chan *ch = data;
1634
1635	hdaa_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
1636
1637	return (ch->blksz);
1638}
1639
1640static void
1641hdaa_channel_stop(struct hdaa_chan *ch)
1642{
1643	struct hdaa_devinfo *devinfo = ch->devinfo;
1644	struct hdaa_widget *w;
1645	int i;
1646
1647	if ((ch->flags & HDAA_CHN_RUNNING) == 0)
1648		return;
1649	ch->flags &= ~HDAA_CHN_RUNNING;
1650	HDAC_STREAM_STOP(device_get_parent(devinfo->dev), devinfo->dev,
1651	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1652	for (i = 0; ch->io[i] != -1; i++) {
1653		w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1654		if (w == NULL)
1655			continue;
1656		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1657			hda_command(devinfo->dev,
1658			    HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], 0));
1659		}
1660		hda_command(devinfo->dev,
1661		    HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i],
1662		    0));
1663	}
1664	HDAC_STREAM_FREE(device_get_parent(devinfo->dev), devinfo->dev,
1665	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1666}
1667
1668static int
1669hdaa_channel_start(struct hdaa_chan *ch)
1670{
1671	struct hdaa_devinfo *devinfo = ch->devinfo;
1672	uint32_t fmt;
1673
1674	fmt = hdaa_stream_format(ch);
1675	ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt)) - 1;
1676	ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev,
1677	    ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos);
1678	if (ch->sid <= 0)
1679		return (EBUSY);
1680	hdaa_audio_setup(ch);
1681	HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev,
1682	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1683	HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev,
1684	    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid,
1685	    sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt);
1686	ch->flags |= HDAA_CHN_RUNNING;
1687	return (0);
1688}
1689
1690static int
1691hdaa_channel_trigger(kobj_t obj, void *data, int go)
1692{
1693	struct hdaa_chan *ch = data;
1694	int error = 0;
1695
1696	if (!PCMTRIG_COMMON(go))
1697		return (0);
1698
1699	hdaa_lock(ch->devinfo);
1700	switch (go) {
1701	case PCMTRIG_START:
1702		error = hdaa_channel_start(ch);
1703		break;
1704	case PCMTRIG_STOP:
1705	case PCMTRIG_ABORT:
1706		hdaa_channel_stop(ch);
1707		break;
1708	default:
1709		break;
1710	}
1711	hdaa_unlock(ch->devinfo);
1712
1713	return (error);
1714}
1715
1716static uint32_t
1717hdaa_channel_getptr(kobj_t obj, void *data)
1718{
1719	struct hdaa_chan *ch = data;
1720	struct hdaa_devinfo *devinfo = ch->devinfo;
1721	uint32_t ptr;
1722
1723	hdaa_lock(devinfo);
1724	if (ch->dmapos != NULL) {
1725		ptr = *(ch->dmapos);
1726	} else {
1727		ptr = HDAC_STREAM_GETPTR(
1728		    device_get_parent(devinfo->dev), devinfo->dev,
1729		    ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
1730	}
1731	hdaa_unlock(devinfo);
1732
1733	/*
1734	 * Round to available space and force 128 bytes aligment.
1735	 */
1736	ptr %= ch->blksz * ch->blkcnt;
1737	ptr &= HDA_BLK_ALIGN;
1738
1739	return (ptr);
1740}
1741
1742static struct pcmchan_caps *
1743hdaa_channel_getcaps(kobj_t obj, void *data)
1744{
1745	return (&((struct hdaa_chan *)data)->caps);
1746}
1747
1748static kobj_method_t hdaa_channel_methods[] = {
1749	KOBJMETHOD(channel_init,		hdaa_channel_init),
1750	KOBJMETHOD(channel_setformat,		hdaa_channel_setformat),
1751	KOBJMETHOD(channel_setspeed,		hdaa_channel_setspeed),
1752	KOBJMETHOD(channel_setblocksize,	hdaa_channel_setblocksize),
1753	KOBJMETHOD(channel_setfragments,	hdaa_channel_setfragments),
1754	KOBJMETHOD(channel_trigger,		hdaa_channel_trigger),
1755	KOBJMETHOD(channel_getptr,		hdaa_channel_getptr),
1756	KOBJMETHOD(channel_getcaps,		hdaa_channel_getcaps),
1757	KOBJMETHOD_END
1758};
1759CHANNEL_DECLARE(hdaa_channel);
1760
1761static int
1762hdaa_audio_ctl_ossmixer_init(struct snd_mixer *m)
1763{
1764	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
1765	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1766	struct hdaa_widget *w, *cw;
1767	uint32_t mask, recmask;
1768	int i, j;
1769
1770	hdaa_lock(devinfo);
1771	pdevinfo->mixer = m;
1772
1773	/* Make sure that in case of soft volume it won't stay muted. */
1774	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1775		pdevinfo->left[i] = 100;
1776		pdevinfo->right[i] = 100;
1777	}
1778
1779	/* Declare volume controls assigned to this association. */
1780	mask = pdevinfo->ossmask;
1781	if (pdevinfo->playas >= 0) {
1782		/* Declate EAPD as ogain control. */
1783		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1784			w = hdaa_widget_get(devinfo, i);
1785			if (w == NULL || w->enable == 0)
1786				continue;
1787			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
1788			    w->param.eapdbtl == HDA_INVALID ||
1789			    w->bindas != pdevinfo->playas)
1790				continue;
1791			mask |= SOUND_MASK_OGAIN;
1792			break;
1793		}
1794
1795		/* Declare soft PCM volume if needed. */
1796		if ((mask & SOUND_MASK_PCM) == 0 ||
1797		    (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) ||
1798		    pdevinfo->minamp[SOUND_MIXER_PCM] ==
1799		     pdevinfo->maxamp[SOUND_MIXER_PCM]) {
1800			mask |= SOUND_MASK_PCM;
1801			pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
1802			HDA_BOOTHVERBOSE(
1803				device_printf(pdevinfo->dev,
1804				    "Forcing Soft PCM volume\n");
1805			);
1806		}
1807
1808		/* Declare master volume if needed. */
1809		if ((mask & SOUND_MASK_VOLUME) == 0) {
1810			mask |= SOUND_MASK_VOLUME;
1811			mix_setparentchild(m, SOUND_MIXER_VOLUME,
1812			    SOUND_MASK_PCM);
1813			mix_setrealdev(m, SOUND_MIXER_VOLUME,
1814			    SOUND_MIXER_NONE);
1815			HDA_BOOTHVERBOSE(
1816				device_printf(pdevinfo->dev,
1817				    "Forcing master volume with PCM\n");
1818			);
1819		}
1820	}
1821
1822	/* Declare record sources available to this association. */
1823	recmask = 0;
1824	if (pdevinfo->recas >= 0) {
1825		for (i = 0; i < 16; i++) {
1826			if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0)
1827				continue;
1828			w = hdaa_widget_get(devinfo,
1829			    devinfo->as[pdevinfo->recas].dacs[0][i]);
1830			if (w == NULL || w->enable == 0)
1831				continue;
1832			for (j = 0; j < w->nconns; j++) {
1833				if (w->connsenable[j] == 0)
1834					continue;
1835				cw = hdaa_widget_get(devinfo, w->conns[j]);
1836				if (cw == NULL || cw->enable == 0)
1837					continue;
1838				if (cw->bindas != pdevinfo->recas &&
1839				    cw->bindas != -2)
1840					continue;
1841				recmask |= cw->ossmask;
1842			}
1843		}
1844	}
1845
1846	recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
1847	mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
1848	pdevinfo->ossmask = mask;
1849
1850	mix_setrecdevs(m, recmask);
1851	mix_setdevs(m, mask);
1852
1853	hdaa_unlock(devinfo);
1854
1855	return (0);
1856}
1857
1858/*
1859 * Update amplification per pdevinfo per ossdev, calculate summary coefficient
1860 * and write it to codec, update *left and *right to reflect remaining error.
1861 */
1862static void
1863hdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev,
1864    int mute, int *left, int *right)
1865{
1866	int i, zleft, zright, sleft, sright, smute, lval, rval;
1867
1868	ctl->devleft[ossdev] = *left;
1869	ctl->devright[ossdev] = *right;
1870	ctl->devmute[ossdev] = mute;
1871	smute = sleft = sright = zleft = zright = 0;
1872	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1873		sleft += ctl->devleft[i];
1874		sright += ctl->devright[i];
1875		smute |= ctl->devmute[i];
1876		if (i == ossdev)
1877			continue;
1878		zleft += ctl->devleft[i];
1879		zright += ctl->devright[i];
1880	}
1881	lval = QDB2VAL(ctl, sleft);
1882	rval = QDB2VAL(ctl, sright);
1883	hdaa_audio_ctl_amp_set(ctl, smute, lval, rval);
1884	*left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft));
1885	*right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright));
1886}
1887
1888/*
1889 * Trace signal from source, setting volumes on the way.
1890 */
1891static void
1892hdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo,
1893    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
1894{
1895	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1896	struct hdaa_widget *w, *wc;
1897	struct hdaa_audio_ctl *ctl;
1898	int i, j, conns = 0;
1899
1900	if (depth > HDA_PARSE_MAXDEPTH)
1901		return;
1902
1903	w = hdaa_widget_get(devinfo, nid);
1904	if (w == NULL || w->enable == 0)
1905		return;
1906
1907	/* Count number of active inputs. */
1908	if (depth > 0) {
1909		for (j = 0; j < w->nconns; j++) {
1910			if (!w->connsenable[j])
1911				continue;
1912			conns++;
1913		}
1914	}
1915
1916	/* If this is not a first step - use input mixer.
1917	   Pins have common input ctl so care must be taken. */
1918	if (depth > 0 && (conns == 1 ||
1919	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
1920		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
1921		    index, 1);
1922		if (ctl)
1923			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
1924	}
1925
1926	/* If widget has own ossdev - not traverse it.
1927	   It will be traversed on it's own. */
1928	if (w->ossdev >= 0 && depth > 0)
1929		return;
1930
1931	/* We must not traverse pin */
1932	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
1933	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
1934	    depth > 0)
1935		return;
1936
1937	/*
1938	 * If signals mixed, we can't assign controls farther.
1939	 * Ignore this on depth zero. Caller must knows why.
1940	 */
1941	if (conns > 1 &&
1942	    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
1943	     w->selconn != index))
1944		return;
1945
1946	ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
1947	if (ctl)
1948		hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
1949
1950	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1951		wc = hdaa_widget_get(devinfo, i);
1952		if (wc == NULL || wc->enable == 0)
1953			continue;
1954		for (j = 0; j < wc->nconns; j++) {
1955			if (wc->connsenable[j] && wc->conns[j] == nid) {
1956				hdaa_audio_ctl_source_volume(pdevinfo, ossdev,
1957				    wc->nid, j, mute, left, right, depth + 1);
1958			}
1959		}
1960	}
1961	return;
1962}
1963
1964/*
1965 * Trace signal from destination, setting volumes on the way.
1966 */
1967static void
1968hdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo,
1969    int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
1970{
1971	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1972	struct hdaa_audio_as *as = devinfo->as;
1973	struct hdaa_widget *w, *wc;
1974	struct hdaa_audio_ctl *ctl;
1975	int i, j, consumers, cleft, cright;
1976
1977	if (depth > HDA_PARSE_MAXDEPTH)
1978		return;
1979
1980	w = hdaa_widget_get(devinfo, nid);
1981	if (w == NULL || w->enable == 0)
1982		return;
1983
1984	if (depth > 0) {
1985		/* If this node produce output for several consumers,
1986		   we can't touch it. */
1987		consumers = 0;
1988		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1989			wc = hdaa_widget_get(devinfo, i);
1990			if (wc == NULL || wc->enable == 0)
1991				continue;
1992			for (j = 0; j < wc->nconns; j++) {
1993				if (wc->connsenable[j] && wc->conns[j] == nid)
1994					consumers++;
1995			}
1996		}
1997		/* The only exception is if real HP redirection is configured
1998		   and this is a duplication point.
1999		   XXX: Actually exception is not completely correct.
2000		   XXX: Duplication point check is not perfect. */
2001		if ((consumers == 2 && (w->bindas < 0 ||
2002		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
2003		    (w->bindseqmask & (1 << 15)) == 0)) ||
2004		    consumers > 2)
2005			return;
2006
2007		/* Else use it's output mixer. */
2008		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2009		    HDAA_CTL_OUT, -1, 1);
2010		if (ctl)
2011			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2012	}
2013
2014	/* We must not traverse pin */
2015	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2016	    depth > 0)
2017		return;
2018
2019	for (i = 0; i < w->nconns; i++) {
2020		if (w->connsenable[i] == 0)
2021			continue;
2022		if (index >= 0 && i != index)
2023			continue;
2024		cleft = left;
2025		cright = right;
2026		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2027		    HDAA_CTL_IN, i, 1);
2028		if (ctl)
2029			hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright);
2030		hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1,
2031		    mute, cleft, cright, depth + 1);
2032	}
2033}
2034
2035/*
2036 * Set volumes for the specified pdevinfo and ossdev.
2037 */
2038static void
2039hdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev)
2040{
2041	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2042	struct hdaa_widget *w, *cw;
2043	uint32_t mute;
2044	int lvol, rvol;
2045	int i, j;
2046
2047	mute = 0;
2048	if (pdevinfo->left[dev] == 0) {
2049		mute |= HDAA_AMP_MUTE_LEFT;
2050		lvol = -4000;
2051	} else
2052		lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2053		    pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev];
2054	if (pdevinfo->right[dev] == 0) {
2055		mute |= HDAA_AMP_MUTE_RIGHT;
2056		rvol = -4000;
2057	} else
2058		rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2059		    pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev];
2060	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2061		w = hdaa_widget_get(devinfo, i);
2062		if (w == NULL || w->enable == 0)
2063			continue;
2064		if (w->bindas < 0 && pdevinfo->index != 0)
2065			continue;
2066		if (w->bindas != pdevinfo->playas &&
2067		    w->bindas != pdevinfo->recas)
2068			continue;
2069		if (dev == SOUND_MIXER_RECLEV &&
2070		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2071			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2072			    w->nid, -1, mute, lvol, rvol, 0);
2073			continue;
2074		}
2075		if (dev == SOUND_MIXER_VOLUME &&
2076		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2077		    devinfo->as[w->bindas].dir == HDAA_CTL_OUT) {
2078			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2079			    w->nid, -1, mute, lvol, rvol, 0);
2080			continue;
2081		}
2082		if (dev == SOUND_MIXER_IGAIN &&
2083		    w->pflags & HDAA_ADC_MONITOR) {
2084			for (j = 0; j < w->nconns; j++) {
2085				if (!w->connsenable[j])
2086				    continue;
2087				cw = hdaa_widget_get(devinfo, w->conns[j]);
2088				if (cw == NULL || cw->enable == 0)
2089				    continue;
2090				if (cw->bindas == -1)
2091				    continue;
2092				if (cw->bindas >= 0 &&
2093				    devinfo->as[cw->bindas].dir != HDAA_CTL_IN)
2094					continue;
2095				hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2096				    w->nid, j, mute, lvol, rvol, 0);
2097			}
2098			continue;
2099		}
2100		if (w->ossdev != dev)
2101			continue;
2102		hdaa_audio_ctl_source_volume(pdevinfo, dev,
2103		    w->nid, -1, mute, lvol, rvol, 0);
2104		if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST))
2105			hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2106			    w->nid, -1, mute, lvol, rvol, 0);
2107	}
2108}
2109
2110/*
2111 * OSS Mixer set method.
2112 */
2113static int
2114hdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
2115					unsigned left, unsigned right)
2116{
2117	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2118	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2119	struct hdaa_widget *w;
2120	int i;
2121
2122	hdaa_lock(devinfo);
2123
2124	/* Save new values. */
2125	pdevinfo->left[dev] = left;
2126	pdevinfo->right[dev] = right;
2127
2128	/* 'ogain' is the special case implemented with EAPD. */
2129	if (dev == SOUND_MIXER_OGAIN) {
2130		uint32_t orig;
2131		w = NULL;
2132		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2133			w = hdaa_widget_get(devinfo, i);
2134			if (w == NULL || w->enable == 0)
2135				continue;
2136			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2137			    w->param.eapdbtl == HDA_INVALID)
2138				continue;
2139			break;
2140		}
2141		if (i >= devinfo->endnode) {
2142			hdaa_unlock(devinfo);
2143			return (-1);
2144		}
2145		orig = w->param.eapdbtl;
2146		if (left == 0)
2147			w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2148		else
2149			w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2150		if (orig != w->param.eapdbtl) {
2151			uint32_t val;
2152
2153			val = w->param.eapdbtl;
2154			if (devinfo->quirks & HDAA_QUIRK_EAPDINV)
2155				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2156			hda_command(devinfo->dev,
2157			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val));
2158		}
2159		hdaa_unlock(devinfo);
2160		return (left | (left << 8));
2161	}
2162
2163	/* Recalculate all controls related to this OSS device. */
2164	hdaa_audio_ctl_dev_volume(pdevinfo, dev);
2165
2166	hdaa_unlock(devinfo);
2167	return (left | (right << 8));
2168}
2169
2170/*
2171 * Set mixer settings to our own default values:
2172 * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others.
2173 */
2174static void
2175hdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo)
2176{
2177	int amp, vol, dev;
2178
2179	for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) {
2180		if ((pdevinfo->ossmask & (1 << dev)) == 0)
2181			continue;
2182
2183		/* If the value was overriden, leave it as is. */
2184		if (resource_int_value(device_get_name(pdevinfo->dev),
2185		    device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0)
2186			continue;
2187
2188		vol = -1;
2189		if (dev == SOUND_MIXER_OGAIN)
2190			vol = 100;
2191		else if (dev == SOUND_MIXER_IGAIN)
2192			vol = 0;
2193		else if (dev == SOUND_MIXER_MIC ||
2194		    dev == SOUND_MIXER_MONITOR)
2195			amp = 20 * 4;	/* +20dB */
2196		else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital)
2197			amp = -10 * 4;	/* -10dB */
2198		else
2199			amp = 0;
2200		if (vol < 0 &&
2201		    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) {
2202			vol = 100;
2203		} else if (vol < 0) {
2204			vol = ((amp - pdevinfo->minamp[dev]) * 100 +
2205			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) /
2206			    (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]);
2207			vol = imin(imax(vol, 1), 100);
2208		}
2209		mix_set(pdevinfo->mixer, dev, vol, vol);
2210	}
2211}
2212
2213/*
2214 * Recursively commutate specified record source.
2215 */
2216static uint32_t
2217hdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
2218{
2219	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2220	struct hdaa_widget *w, *cw;
2221	struct hdaa_audio_ctl *ctl;
2222	char buf[64];
2223	int i, muted;
2224	uint32_t res = 0;
2225
2226	if (depth > HDA_PARSE_MAXDEPTH)
2227		return (0);
2228
2229	w = hdaa_widget_get(devinfo, nid);
2230	if (w == NULL || w->enable == 0)
2231		return (0);
2232
2233	for (i = 0; i < w->nconns; i++) {
2234		if (w->connsenable[i] == 0)
2235			continue;
2236		cw = hdaa_widget_get(devinfo, w->conns[i]);
2237		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
2238			continue;
2239		/* Call recursively to trace signal to it's source if needed. */
2240		if ((src & cw->ossmask) != 0) {
2241			if (cw->ossdev < 0) {
2242				res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2243				    w->conns[i], depth + 1);
2244			} else {
2245				res |= cw->ossmask;
2246			}
2247		}
2248		/* We have two special cases: mixers and others (selectors). */
2249		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
2250			ctl = hdaa_audio_ctl_amp_get(devinfo,
2251			    w->nid, HDAA_CTL_IN, i, 1);
2252			if (ctl == NULL)
2253				continue;
2254			/* If we have input control on this node mute them
2255			 * according to requested sources. */
2256			muted = (src & cw->ossmask) ? 0 : 1;
2257			if (muted != ctl->forcemute) {
2258				ctl->forcemute = muted;
2259				hdaa_audio_ctl_amp_set(ctl,
2260				    HDAA_AMP_MUTE_DEFAULT,
2261				    HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
2262			}
2263			HDA_BOOTHVERBOSE(
2264				device_printf(pdevinfo->dev,
2265				    "Recsel (%s): nid %d source %d %s\n",
2266				    hdaa_audio_ctl_ossmixer_mask2allname(
2267				    src, buf, sizeof(buf)),
2268				    nid, i, muted?"mute":"unmute");
2269			);
2270		} else {
2271			if (w->nconns == 1)
2272				break;
2273			if ((src & cw->ossmask) == 0)
2274				continue;
2275			/* If we found requested source - select it and exit. */
2276			hdaa_widget_connection_select(w, i);
2277			HDA_BOOTHVERBOSE(
2278				device_printf(pdevinfo->dev,
2279				    "Recsel (%s): nid %d source %d select\n",
2280				    hdaa_audio_ctl_ossmixer_mask2allname(
2281				    src, buf, sizeof(buf)),
2282				    nid, i);
2283			);
2284			break;
2285		}
2286	}
2287	return (res);
2288}
2289
2290static uint32_t
2291hdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
2292{
2293	struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2294	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2295	struct hdaa_widget *w;
2296	struct hdaa_audio_as *as;
2297	struct hdaa_audio_ctl *ctl;
2298	struct hdaa_chan *ch;
2299	int i, j;
2300	uint32_t ret = 0xffffffff;
2301
2302	hdaa_lock(devinfo);
2303	if (pdevinfo->recas < 0) {
2304		hdaa_unlock(devinfo);
2305		return (0);
2306	}
2307	as = &devinfo->as[pdevinfo->recas];
2308
2309	/* For non-mixed associations we always recording everything. */
2310	if (!as->mixed) {
2311		hdaa_unlock(devinfo);
2312		return (mix_getrecdevs(m));
2313	}
2314
2315	/* Commutate requested recsrc for each ADC. */
2316	for (j = 0; j < as->num_chans; j++) {
2317		ch = &devinfo->chans[as->chans[j]];
2318		for (i = 0; ch->io[i] >= 0; i++) {
2319			w = hdaa_widget_get(devinfo, ch->io[i]);
2320			if (w == NULL || w->enable == 0)
2321				continue;
2322			ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2323			    ch->io[i], 0);
2324		}
2325	}
2326	if (ret == 0xffffffff)
2327		ret = 0;
2328
2329	/*
2330	 * Some controls could be shared. Reset volumes for controls
2331	 * related to previously chosen devices, as they may no longer
2332	 * affect the signal.
2333	 */
2334	i = 0;
2335	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
2336		if (ctl->enable == 0 ||
2337		    !(ctl->ossmask & pdevinfo->recsrc))
2338			continue;
2339		if (!((pdevinfo->playas >= 0 &&
2340		    ctl->widget->bindas == pdevinfo->playas) ||
2341		    (pdevinfo->recas >= 0 &&
2342		    ctl->widget->bindas == pdevinfo->recas) ||
2343		    (pdevinfo->index == 0 &&
2344		    ctl->widget->bindas == -2)))
2345			continue;
2346		for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2347			if (pdevinfo->recsrc & (1 << j)) {
2348				ctl->devleft[j] = 0;
2349				ctl->devright[j] = 0;
2350				ctl->devmute[j] = 0;
2351			}
2352		}
2353	}
2354
2355	/*
2356	 * Some controls could be shared. Set volumes for controls
2357	 * related to devices selected both previously and now.
2358	 */
2359	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2360		if ((ret | pdevinfo->recsrc) & (1 << j))
2361			hdaa_audio_ctl_dev_volume(pdevinfo, j);
2362	}
2363
2364	pdevinfo->recsrc = ret;
2365	hdaa_unlock(devinfo);
2366	return (ret);
2367}
2368
2369static kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = {
2370	KOBJMETHOD(mixer_init,		hdaa_audio_ctl_ossmixer_init),
2371	KOBJMETHOD(mixer_set,		hdaa_audio_ctl_ossmixer_set),
2372	KOBJMETHOD(mixer_setrecsrc,	hdaa_audio_ctl_ossmixer_setrecsrc),
2373	KOBJMETHOD_END
2374};
2375MIXER_DECLARE(hdaa_audio_ctl_ossmixer);
2376
2377static void
2378hdaa_dump_gpi(struct hdaa_devinfo *devinfo)
2379{
2380	device_t dev = devinfo->dev;
2381	int i;
2382	uint32_t data, wake, unsol, sticky;
2383
2384	if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) {
2385		data = hda_command(dev,
2386		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
2387		wake = hda_command(dev,
2388		    HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid));
2389		unsol = hda_command(dev,
2390		    HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2391		sticky = hda_command(dev,
2392		    HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid));
2393		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) {
2394			device_printf(dev, " GPI%d:%s%s%s state=%d", i,
2395				    (sticky & (1 << i)) ? " sticky" : "",
2396				    (unsol & (1 << i)) ? " unsol" : "",
2397				    (wake & (1 << i)) ? " wake" : "",
2398				    (data >> i) & 1);
2399		}
2400	}
2401}
2402
2403static void
2404hdaa_dump_gpio(struct hdaa_devinfo *devinfo)
2405{
2406	device_t dev = devinfo->dev;
2407	int i;
2408	uint32_t data, dir, enable, wake, unsol, sticky;
2409
2410	if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) {
2411		data = hda_command(dev,
2412		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
2413		enable = hda_command(dev,
2414		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
2415		dir = hda_command(dev,
2416		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
2417		wake = hda_command(dev,
2418		    HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid));
2419		unsol = hda_command(dev,
2420		    HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2421		sticky = hda_command(dev,
2422		    HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid));
2423		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) {
2424			device_printf(dev, " GPIO%d: ", i);
2425			if ((enable & (1 << i)) == 0) {
2426				printf("disabled\n");
2427				continue;
2428			}
2429			if ((dir & (1 << i)) == 0) {
2430				printf("input%s%s%s",
2431				    (sticky & (1 << i)) ? " sticky" : "",
2432				    (unsol & (1 << i)) ? " unsol" : "",
2433				    (wake & (1 << i)) ? " wake" : "");
2434			} else
2435				printf("output");
2436			printf(" state=%d\n", (data >> i) & 1);
2437		}
2438	}
2439}
2440
2441static void
2442hdaa_dump_gpo(struct hdaa_devinfo *devinfo)
2443{
2444	device_t dev = devinfo->dev;
2445	int i;
2446	uint32_t data;
2447
2448	if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) {
2449		data = hda_command(dev,
2450		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
2451		for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) {
2452			device_printf(dev, " GPO%d: state=%d", i,
2453				    (data >> i) & 1);
2454		}
2455	}
2456}
2457
2458static void
2459hdaa_audio_parse(struct hdaa_devinfo *devinfo)
2460{
2461	struct hdaa_widget *w;
2462	uint32_t res;
2463	int i;
2464	nid_t nid;
2465
2466	nid = devinfo->nid;
2467
2468	res = hda_command(devinfo->dev,
2469	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT));
2470	devinfo->gpio_cap = res;
2471
2472	HDA_BOOTVERBOSE(
2473		device_printf(devinfo->dev,
2474		    "NumGPIO=%d NumGPO=%d "
2475		    "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
2476		    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
2477		    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
2478		    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
2479		    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
2480		    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
2481		hdaa_dump_gpi(devinfo);
2482		hdaa_dump_gpio(devinfo);
2483		hdaa_dump_gpo(devinfo);
2484	);
2485
2486	res = hda_command(devinfo->dev,
2487	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS));
2488	devinfo->supp_stream_formats = res;
2489
2490	res = hda_command(devinfo->dev,
2491	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE));
2492	devinfo->supp_pcm_size_rate = res;
2493
2494	res = hda_command(devinfo->dev,
2495	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP));
2496	devinfo->outamp_cap = res;
2497
2498	res = hda_command(devinfo->dev,
2499	    HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP));
2500	devinfo->inamp_cap = res;
2501
2502	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2503		w = hdaa_widget_get(devinfo, i);
2504		if (w == NULL)
2505			device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i);
2506		else {
2507			w->devinfo = devinfo;
2508			w->nid = i;
2509			w->enable = 1;
2510			w->selconn = -1;
2511			w->pflags = 0;
2512			w->ossdev = -1;
2513			w->bindas = -1;
2514			w->param.eapdbtl = HDA_INVALID;
2515			hdaa_widget_parse(w);
2516		}
2517	}
2518}
2519
2520static void
2521hdaa_audio_postprocess(struct hdaa_devinfo *devinfo)
2522{
2523	struct hdaa_widget *w;
2524	int i;
2525
2526	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2527		w = hdaa_widget_get(devinfo, i);
2528		if (w == NULL)
2529			continue;
2530		hdaa_widget_postprocess(w);
2531	}
2532}
2533
2534static void
2535hdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo)
2536{
2537	struct hdaa_audio_ctl *ctls;
2538	struct hdaa_widget *w, *cw;
2539	int i, j, cnt, max, ocap, icap;
2540	int mute, offset, step, size;
2541
2542	/* XXX This is redundant */
2543	max = 0;
2544	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2545		w = hdaa_widget_get(devinfo, i);
2546		if (w == NULL || w->enable == 0)
2547			continue;
2548		if (w->param.outamp_cap != 0)
2549			max++;
2550		if (w->param.inamp_cap != 0) {
2551			switch (w->type) {
2552			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2553			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2554				for (j = 0; j < w->nconns; j++) {
2555					cw = hdaa_widget_get(devinfo,
2556					    w->conns[j]);
2557					if (cw == NULL || cw->enable == 0)
2558						continue;
2559					max++;
2560				}
2561				break;
2562			default:
2563				max++;
2564				break;
2565			}
2566		}
2567	}
2568	devinfo->ctlcnt = max;
2569
2570	if (max < 1)
2571		return;
2572
2573	ctls = (struct hdaa_audio_ctl *)malloc(
2574	    sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT);
2575
2576	if (ctls == NULL) {
2577		/* Blekh! */
2578		device_printf(devinfo->dev, "unable to allocate ctls!\n");
2579		devinfo->ctlcnt = 0;
2580		return;
2581	}
2582
2583	cnt = 0;
2584	for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
2585		if (cnt >= max) {
2586			device_printf(devinfo->dev, "%s: Ctl overflow!\n",
2587			    __func__);
2588			break;
2589		}
2590		w = hdaa_widget_get(devinfo, i);
2591		if (w == NULL || w->enable == 0)
2592			continue;
2593		ocap = w->param.outamp_cap;
2594		icap = w->param.inamp_cap;
2595		if (ocap != 0) {
2596			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
2597			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
2598			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
2599			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
2600			/*if (offset > step) {
2601				HDA_BOOTVERBOSE(
2602					device_printf(devinfo->dev,
2603					    "BUGGY outamp: nid=%d "
2604					    "[offset=%d > step=%d]\n",
2605					    w->nid, offset, step);
2606				);
2607				offset = step;
2608			}*/
2609			ctls[cnt].enable = 1;
2610			ctls[cnt].widget = w;
2611			ctls[cnt].mute = mute;
2612			ctls[cnt].step = step;
2613			ctls[cnt].size = size;
2614			ctls[cnt].offset = offset;
2615			ctls[cnt].left = offset;
2616			ctls[cnt].right = offset;
2617			if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2618			    w->waspin)
2619				ctls[cnt].ndir = HDAA_CTL_IN;
2620			else
2621				ctls[cnt].ndir = HDAA_CTL_OUT;
2622			ctls[cnt++].dir = HDAA_CTL_OUT;
2623		}
2624
2625		if (icap != 0) {
2626			mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
2627			step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
2628			size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
2629			offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
2630			/*if (offset > step) {
2631				HDA_BOOTVERBOSE(
2632					device_printf(devinfo->dev,
2633					    "BUGGY inamp: nid=%d "
2634					    "[offset=%d > step=%d]\n",
2635					    w->nid, offset, step);
2636				);
2637				offset = step;
2638			}*/
2639			switch (w->type) {
2640			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
2641			case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
2642				for (j = 0; j < w->nconns; j++) {
2643					if (cnt >= max) {
2644						device_printf(devinfo->dev,
2645						    "%s: Ctl overflow!\n",
2646						    __func__);
2647						break;
2648					}
2649					cw = hdaa_widget_get(devinfo,
2650					    w->conns[j]);
2651					if (cw == NULL || cw->enable == 0)
2652						continue;
2653					ctls[cnt].enable = 1;
2654					ctls[cnt].widget = w;
2655					ctls[cnt].childwidget = cw;
2656					ctls[cnt].index = j;
2657					ctls[cnt].mute = mute;
2658					ctls[cnt].step = step;
2659					ctls[cnt].size = size;
2660					ctls[cnt].offset = offset;
2661					ctls[cnt].left = offset;
2662					ctls[cnt].right = offset;
2663				ctls[cnt].ndir = HDAA_CTL_IN;
2664					ctls[cnt++].dir = HDAA_CTL_IN;
2665				}
2666				break;
2667			default:
2668				if (cnt >= max) {
2669					device_printf(devinfo->dev,
2670					    "%s: Ctl overflow!\n",
2671					    __func__);
2672					break;
2673				}
2674				ctls[cnt].enable = 1;
2675				ctls[cnt].widget = w;
2676				ctls[cnt].mute = mute;
2677				ctls[cnt].step = step;
2678				ctls[cnt].size = size;
2679				ctls[cnt].offset = offset;
2680				ctls[cnt].left = offset;
2681				ctls[cnt].right = offset;
2682				if (w->type ==
2683				    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2684					ctls[cnt].ndir = HDAA_CTL_OUT;
2685				else
2686					ctls[cnt].ndir = HDAA_CTL_IN;
2687				ctls[cnt++].dir = HDAA_CTL_IN;
2688				break;
2689			}
2690		}
2691	}
2692
2693	devinfo->ctl = ctls;
2694}
2695
2696static void
2697hdaa_audio_as_parse(struct hdaa_devinfo *devinfo)
2698{
2699	struct hdaa_audio_as *as;
2700	struct hdaa_widget *w;
2701	int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
2702
2703	/* Count present associations */
2704	max = 0;
2705	for (j = 1; j < 16; j++) {
2706		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2707			w = hdaa_widget_get(devinfo, i);
2708			if (w == NULL || w->enable == 0)
2709				continue;
2710			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2711				continue;
2712			if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
2713			    != j)
2714				continue;
2715			max++;
2716			if (j != 15)  /* There could be many 1-pin assocs #15 */
2717				break;
2718		}
2719	}
2720
2721	devinfo->ascnt = max;
2722
2723	if (max < 1)
2724		return;
2725
2726	as = (struct hdaa_audio_as *)malloc(
2727	    sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT);
2728
2729	if (as == NULL) {
2730		/* Blekh! */
2731		device_printf(devinfo->dev, "unable to allocate assocs!\n");
2732		devinfo->ascnt = 0;
2733		return;
2734	}
2735
2736	for (i = 0; i < max; i++) {
2737		as[i].hpredir = -1;
2738		as[i].digital = 0;
2739		as[i].num_chans = 1;
2740		as[i].location = -1;
2741	}
2742
2743	/* Scan associations skipping as=0. */
2744	cnt = 0;
2745	for (j = 1; j < 16; j++) {
2746		first = 16;
2747		hpredir = 0;
2748		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2749			w = hdaa_widget_get(devinfo, i);
2750			if (w == NULL || w->enable == 0)
2751				continue;
2752			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
2753				continue;
2754			assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
2755			seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
2756			if (assoc != j) {
2757				continue;
2758			}
2759			KASSERT(cnt < max,
2760			    ("%s: Associations owerflow (%d of %d)",
2761			    __func__, cnt, max));
2762			type = w->wclass.pin.config &
2763			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
2764			/* Get pin direction. */
2765			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
2766			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
2767			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
2768			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
2769			    type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
2770				dir = HDAA_CTL_OUT;
2771			else
2772				dir = HDAA_CTL_IN;
2773			/* If this is a first pin - create new association. */
2774			if (as[cnt].pincnt == 0) {
2775				as[cnt].enable = 1;
2776				as[cnt].index = j;
2777				as[cnt].dir = dir;
2778			}
2779			if (seq < first)
2780				first = seq;
2781			/* Check association correctness. */
2782			if (as[cnt].pins[seq] != 0) {
2783				device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) "
2784				    "in association %d! Disabling association.\n",
2785				    __func__, seq, w->nid, j);
2786				as[cnt].enable = 0;
2787			}
2788			if (dir != as[cnt].dir) {
2789				device_printf(devinfo->dev, "%s: Pin %d has wrong "
2790				    "direction for association %d! Disabling "
2791				    "association.\n",
2792				    __func__, w->nid, j);
2793				as[cnt].enable = 0;
2794			}
2795			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
2796				as[cnt].digital |= 0x1;
2797				if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
2798					as[cnt].digital |= 0x2;
2799				if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
2800					as[cnt].digital |= 0x4;
2801			}
2802			if (as[cnt].location == -1) {
2803				as[cnt].location =
2804				    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config);
2805			} else if (as[cnt].location !=
2806			    HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) {
2807				as[cnt].location = -2;
2808			}
2809			/* Headphones with seq=15 may mean redirection. */
2810			if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
2811			    seq == 15)
2812				hpredir = 1;
2813			as[cnt].pins[seq] = w->nid;
2814			as[cnt].pincnt++;
2815			/* Association 15 is a multiple unassociated pins. */
2816			if (j == 15)
2817				cnt++;
2818		}
2819		if (j != 15 && as[cnt].pincnt > 0) {
2820			if (hpredir && as[cnt].pincnt > 1)
2821				as[cnt].hpredir = first;
2822			cnt++;
2823		}
2824	}
2825	for (i = 0; i < max; i++) {
2826		if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 ||
2827		    as[i].pins[14] > 0 || as[i].pins[15] > 0))
2828			as[i].mixed = 1;
2829	}
2830	HDA_BOOTVERBOSE(
2831		device_printf(devinfo->dev,
2832		    "%d associations found:\n", max);
2833		for (i = 0; i < max; i++) {
2834			device_printf(devinfo->dev,
2835			    "Association %d (%d) %s%s:\n",
2836			    i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out",
2837			    as[i].enable?"":" (disabled)");
2838			for (j = 0; j < 16; j++) {
2839				if (as[i].pins[j] == 0)
2840					continue;
2841				device_printf(devinfo->dev,
2842				    " Pin nid=%d seq=%d\n",
2843				    as[i].pins[j], j);
2844			}
2845		}
2846	);
2847
2848	devinfo->as = as;
2849}
2850
2851/*
2852 * Trace path from DAC to pin.
2853 */
2854static nid_t
2855hdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
2856    int dupseq, int min, int only, int depth)
2857{
2858	struct hdaa_widget *w;
2859	int i, im = -1;
2860	nid_t m = 0, ret;
2861
2862	if (depth > HDA_PARSE_MAXDEPTH)
2863		return (0);
2864	w = hdaa_widget_get(devinfo, nid);
2865	if (w == NULL || w->enable == 0)
2866		return (0);
2867	HDA_BOOTHVERBOSE(
2868		if (!only) {
2869			device_printf(devinfo->dev,
2870			    " %*stracing via nid %d\n",
2871				depth + 1, "", w->nid);
2872		}
2873	);
2874	/* Use only unused widgets */
2875	if (w->bindas >= 0 && w->bindas != as) {
2876		HDA_BOOTHVERBOSE(
2877			if (!only) {
2878				device_printf(devinfo->dev,
2879				    " %*snid %d busy by association %d\n",
2880					depth + 1, "", w->nid, w->bindas);
2881			}
2882		);
2883		return (0);
2884	}
2885	if (dupseq < 0) {
2886		if (w->bindseqmask != 0) {
2887			HDA_BOOTHVERBOSE(
2888				if (!only) {
2889					device_printf(devinfo->dev,
2890					    " %*snid %d busy by seqmask %x\n",
2891						depth + 1, "", w->nid, w->bindseqmask);
2892				}
2893			);
2894			return (0);
2895		}
2896	} else {
2897		/* If this is headphones - allow duplicate first pin. */
2898		if (w->bindseqmask != 0 &&
2899		    (w->bindseqmask & (1 << dupseq)) == 0) {
2900			HDA_BOOTHVERBOSE(
2901				device_printf(devinfo->dev,
2902				    " %*snid %d busy by seqmask %x\n",
2903					depth + 1, "", w->nid, w->bindseqmask);
2904			);
2905			return (0);
2906		}
2907	}
2908
2909	switch (w->type) {
2910	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
2911		/* Do not traverse input. AD1988 has digital monitor
2912		for which we are not ready. */
2913		break;
2914	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
2915		/* If we are tracing HP take only dac of first pin. */
2916		if ((only == 0 || only == w->nid) &&
2917		    (w->nid >= min) && (dupseq < 0 || w->nid ==
2918		    devinfo->as[as].dacs[0][dupseq]))
2919			m = w->nid;
2920		break;
2921	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
2922		if (depth > 0)
2923			break;
2924		/* Fall */
2925	default:
2926		/* Find reachable DACs with smallest nid respecting constraints. */
2927		for (i = 0; i < w->nconns; i++) {
2928			if (w->connsenable[i] == 0)
2929				continue;
2930			if (w->selconn != -1 && w->selconn != i)
2931				continue;
2932			if ((ret = hdaa_audio_trace_dac(devinfo, as, seq,
2933			    w->conns[i], dupseq, min, only, depth + 1)) != 0) {
2934				if (m == 0 || ret < m) {
2935					m = ret;
2936					im = i;
2937				}
2938				if (only || dupseq >= 0)
2939					break;
2940			}
2941		}
2942		if (im >= 0 && only && ((w->nconns > 1 &&
2943		    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
2944		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
2945			w->selconn = im;
2946		break;
2947	}
2948	if (m && only) {
2949		w->bindas = as;
2950		w->bindseqmask |= (1 << seq);
2951	}
2952	HDA_BOOTHVERBOSE(
2953		if (!only) {
2954			device_printf(devinfo->dev,
2955			    " %*snid %d returned %d\n",
2956				depth + 1, "", w->nid, m);
2957		}
2958	);
2959	return (m);
2960}
2961
2962/*
2963 * Trace path from widget to ADC.
2964 */
2965static nid_t
2966hdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
2967    int mixed, int min, int only, int depth, int *length, int onlylength)
2968{
2969	struct hdaa_widget *w, *wc;
2970	int i, j, im, lm = HDA_PARSE_MAXDEPTH;
2971	nid_t m = 0, ret;
2972
2973	if (depth > HDA_PARSE_MAXDEPTH)
2974		return (0);
2975	w = hdaa_widget_get(devinfo, nid);
2976	if (w == NULL || w->enable == 0)
2977		return (0);
2978	HDA_BOOTHVERBOSE(
2979		device_printf(devinfo->dev,
2980		    " %*stracing via nid %d\n",
2981			depth + 1, "", w->nid);
2982	);
2983	/* Use only unused widgets */
2984	if (w->bindas >= 0 && w->bindas != as) {
2985		HDA_BOOTHVERBOSE(
2986			device_printf(devinfo->dev,
2987			    " %*snid %d busy by association %d\n",
2988				depth + 1, "", w->nid, w->bindas);
2989		);
2990		return (0);
2991	}
2992	if (!mixed && w->bindseqmask != 0) {
2993		HDA_BOOTHVERBOSE(
2994			device_printf(devinfo->dev,
2995			    " %*snid %d busy by seqmask %x\n",
2996				depth + 1, "", w->nid, w->bindseqmask);
2997		);
2998		return (0);
2999	}
3000	switch (w->type) {
3001	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3002		if ((only == 0 || only == w->nid) && (w->nid >= min) &&
3003		    (onlylength == 0 || onlylength == depth)) {
3004			m = w->nid;
3005			if (length != NULL)
3006				*length = depth;
3007		}
3008		break;
3009	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3010		if (depth > 0)
3011			break;
3012		/* Fall */
3013	default:
3014		/* Try to find reachable ADCs with specified nid. */
3015		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3016			wc = hdaa_widget_get(devinfo, j);
3017			if (wc == NULL || wc->enable == 0)
3018				continue;
3019			im = -1;
3020			for (i = 0; i < wc->nconns; i++) {
3021				if (wc->connsenable[i] == 0)
3022					continue;
3023				if (wc->conns[i] != nid)
3024					continue;
3025				if ((ret = hdaa_audio_trace_adc(devinfo, as, seq,
3026				    j, mixed, min, only, depth + 1,
3027				    length, onlylength)) != 0) {
3028					if (m == 0 || ret < m ||
3029					    (ret == m && length != NULL &&
3030					     *length < lm)) {
3031						m = ret;
3032						im = i;
3033						lm = *length;
3034					}
3035					if (only)
3036						break;
3037				}
3038			}
3039			if (im >= 0 && only && ((wc->nconns > 1 &&
3040			    wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3041			    wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3042				wc->selconn = im;
3043		}
3044		break;
3045	}
3046	if (m && only) {
3047		w->bindas = as;
3048		w->bindseqmask |= (1 << seq);
3049	}
3050	HDA_BOOTHVERBOSE(
3051		device_printf(devinfo->dev,
3052		    " %*snid %d returned %d\n",
3053			depth + 1, "", w->nid, m);
3054	);
3055	return (m);
3056}
3057
3058/*
3059 * Erase trace path of the specified association.
3060 */
3061static void
3062hdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq)
3063{
3064	struct hdaa_widget *w;
3065	int i;
3066
3067	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3068		w = hdaa_widget_get(devinfo, i);
3069		if (w == NULL || w->enable == 0)
3070			continue;
3071		if (w->bindas == as) {
3072			if (seq >= 0) {
3073				w->bindseqmask &= ~(1 << seq);
3074				if (w->bindseqmask == 0) {
3075					w->bindas = -1;
3076					w->selconn = -1;
3077				}
3078			} else {
3079				w->bindas = -1;
3080				w->bindseqmask = 0;
3081				w->selconn = -1;
3082			}
3083		}
3084	}
3085}
3086
3087/*
3088 * Trace association path from DAC to output
3089 */
3090static int
3091hdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq)
3092{
3093	struct hdaa_audio_as *ases = devinfo->as;
3094	int i, hpredir;
3095	nid_t min, res;
3096
3097	/* Find next pin */
3098	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3099		;
3100	/* Check if there is no any left. If so - we succeeded. */
3101	if (i == 16)
3102		return (1);
3103
3104	hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
3105	min = 0;
3106	do {
3107		HDA_BOOTHVERBOSE(
3108			device_printf(devinfo->dev,
3109			    " Tracing pin %d with min nid %d",
3110			    ases[as].pins[i], min);
3111			if (hpredir >= 0)
3112				printf(" and hpredir %d", hpredir);
3113			printf("\n");
3114		);
3115		/* Trace this pin taking min nid into account. */
3116		res = hdaa_audio_trace_dac(devinfo, as, i,
3117		    ases[as].pins[i], hpredir, min, 0, 0);
3118		if (res == 0) {
3119			/* If we failed - return to previous and redo it. */
3120			HDA_BOOTVERBOSE(
3121				device_printf(devinfo->dev,
3122				    " Unable to trace pin %d seq %d with min "
3123				    "nid %d",
3124				    ases[as].pins[i], i, min);
3125				if (hpredir >= 0)
3126					printf(" and hpredir %d", hpredir);
3127				printf("\n");
3128			);
3129			return (0);
3130		}
3131		HDA_BOOTVERBOSE(
3132			device_printf(devinfo->dev,
3133			    " Pin %d traced to DAC %d",
3134			    ases[as].pins[i], res);
3135			if (hpredir >= 0)
3136				printf(" and hpredir %d", hpredir);
3137			if (ases[as].fakeredir)
3138				printf(" with fake redirection");
3139			printf("\n");
3140		);
3141		/* Trace again to mark the path */
3142		hdaa_audio_trace_dac(devinfo, as, i,
3143		    ases[as].pins[i], hpredir, min, res, 0);
3144		ases[as].dacs[0][i] = res;
3145		/* We succeeded, so call next. */
3146		if (hdaa_audio_trace_as_out(devinfo, as, i + 1))
3147			return (1);
3148		/* If next failed, we should retry with next min */
3149		hdaa_audio_undo_trace(devinfo, as, i);
3150		ases[as].dacs[0][i] = 0;
3151		min = res + 1;
3152	} while (1);
3153}
3154
3155/*
3156 * Check equivalency of two DACs.
3157 */
3158static int
3159hdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3160{
3161	struct hdaa_devinfo *devinfo = w1->devinfo;
3162	struct hdaa_widget *w3;
3163	int i, j, c1, c2;
3164
3165	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3166		return (0);
3167	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3168		w3 = hdaa_widget_get(devinfo, i);
3169		if (w3 == NULL || w3->enable == 0)
3170			continue;
3171		if (w3->bindas != w1->bindas)
3172			continue;
3173		if (w3->nconns == 0)
3174			continue;
3175		c1 = c2 = -1;
3176		for (j = 0; j < w3->nconns; j++) {
3177			if (w3->connsenable[j] == 0)
3178				continue;
3179			if (w3->conns[j] == w1->nid)
3180				c1 = j;
3181			if (w3->conns[j] == w2->nid)
3182				c2 = j;
3183		}
3184		if (c1 < 0)
3185			continue;
3186		if (c2 < 0)
3187			return (0);
3188		if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3189			return (0);
3190	}
3191	return (1);
3192}
3193
3194/*
3195 * Check equivalency of two ADCs.
3196 */
3197static int
3198hdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3199{
3200	struct hdaa_devinfo *devinfo = w1->devinfo;
3201	struct hdaa_widget *w3, *w4;
3202	int i;
3203
3204	if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3205		return (0);
3206	if (w1->nconns != 1 || w2->nconns != 1)
3207		return (0);
3208	if (w1->conns[0] == w2->conns[0])
3209		return (1);
3210	w3 = hdaa_widget_get(devinfo, w1->conns[0]);
3211	if (w3 == NULL || w3->enable == 0)
3212		return (0);
3213	w4 = hdaa_widget_get(devinfo, w2->conns[0]);
3214	if (w4 == NULL || w4->enable == 0)
3215		return (0);
3216	if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask)
3217		return (1);
3218	if (w4->bindas >= 0)
3219		return (0);
3220	if (w3->type != w4->type)
3221		return (0);
3222	if (memcmp(&w3->param, &w4->param, sizeof(w3->param)))
3223		return (0);
3224	if (w3->nconns != w4->nconns)
3225		return (0);
3226	for (i = 0; i < w3->nconns; i++) {
3227		if (w3->conns[i] != w4->conns[i])
3228			return (0);
3229	}
3230	return (1);
3231}
3232
3233/*
3234 * Look for equivalent DAC/ADC to implement second channel.
3235 */
3236static void
3237hdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid)
3238{
3239	struct hdaa_audio_as *as = &devinfo->as[asid];
3240	struct hdaa_widget *w1, *w2;
3241	int i, pos;
3242	nid_t nid1, nid2;
3243
3244	HDA_BOOTVERBOSE(
3245		device_printf(devinfo->dev,
3246		    "Looking for additional %sC "
3247		    "for association %d (%d)\n",
3248		    (as->dir == HDAA_CTL_OUT) ? "DA" : "AD",
3249		    asid, as->index);
3250	);
3251
3252	/* Find the exisitng DAC position and return if found more the one. */
3253	pos = -1;
3254	for (i = 0; i < 16; i++) {
3255		if (as->dacs[0][i] <= 0)
3256			continue;
3257		if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos])
3258			return;
3259		pos = i;
3260	}
3261
3262	nid1 = as->dacs[0][pos];
3263	w1 = hdaa_widget_get(devinfo, nid1);
3264	w2 = NULL;
3265	for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) {
3266		w2 = hdaa_widget_get(devinfo, nid2);
3267		if (w2 == NULL || w2->enable == 0)
3268			continue;
3269		if (w2->bindas >= 0)
3270			continue;
3271		if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
3272			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
3273				continue;
3274			if (hdaa_audio_dacs_equal(w1, w2))
3275				break;
3276		} else {
3277			if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3278				continue;
3279			if (hdaa_audio_adcs_equal(w1, w2))
3280				break;
3281		}
3282	}
3283	if (nid2 >= devinfo->endnode)
3284		return;
3285	w2->bindas = w1->bindas;
3286	w2->bindseqmask = w1->bindseqmask;
3287	if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
3288		HDA_BOOTVERBOSE(
3289			device_printf(devinfo->dev,
3290			    " ADC %d considered equal to ADC %d\n", nid2, nid1);
3291		);
3292		w1 = hdaa_widget_get(devinfo, w1->conns[0]);
3293		w2 = hdaa_widget_get(devinfo, w2->conns[0]);
3294		w2->bindas = w1->bindas;
3295		w2->bindseqmask = w1->bindseqmask;
3296	} else {
3297		HDA_BOOTVERBOSE(
3298			device_printf(devinfo->dev,
3299			    " DAC %d considered equal to DAC %d\n", nid2, nid1);
3300		);
3301	}
3302	for (i = 0; i < 16; i++) {
3303		if (as->dacs[0][i] <= 0)
3304			continue;
3305		as->dacs[as->num_chans][i] = nid2;
3306	}
3307	as->num_chans++;
3308}
3309
3310/*
3311 * Trace association path from input to ADC
3312 */
3313static int
3314hdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as)
3315{
3316	struct hdaa_audio_as *ases = devinfo->as;
3317	struct hdaa_widget *w;
3318	int i, j, k, length;
3319
3320	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3321		w = hdaa_widget_get(devinfo, j);
3322		if (w == NULL || w->enable == 0)
3323			continue;
3324		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3325			continue;
3326		if (w->bindas >= 0 && w->bindas != as)
3327			continue;
3328
3329		/* Find next pin */
3330		for (i = 0; i < 16; i++) {
3331			if (ases[as].pins[i] == 0)
3332				continue;
3333
3334			HDA_BOOTHVERBOSE(
3335				device_printf(devinfo->dev,
3336				    " Tracing pin %d to ADC %d\n",
3337				    ases[as].pins[i], j);
3338			);
3339			/* Trace this pin taking goal into account. */
3340			if (hdaa_audio_trace_adc(devinfo, as, i,
3341			    ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) {
3342				/* If we failed - return to previous and redo it. */
3343				HDA_BOOTVERBOSE(
3344					device_printf(devinfo->dev,
3345					    " Unable to trace pin %d to ADC %d, undo traces\n",
3346					    ases[as].pins[i], j);
3347				);
3348				hdaa_audio_undo_trace(devinfo, as, -1);
3349				for (k = 0; k < 16; k++)
3350					ases[as].dacs[0][k] = 0;
3351				break;
3352			}
3353			HDA_BOOTVERBOSE(
3354				device_printf(devinfo->dev,
3355				    " Pin %d traced to ADC %d\n",
3356				    ases[as].pins[i], j);
3357			);
3358			ases[as].dacs[0][i] = j;
3359		}
3360		if (i == 16)
3361			return (1);
3362	}
3363	return (0);
3364}
3365
3366/*
3367 * Trace association path from input to multiple ADCs
3368 */
3369static int
3370hdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq)
3371{
3372	struct hdaa_audio_as *ases = devinfo->as;
3373	int i, length;
3374	nid_t min, res;
3375
3376	/* Find next pin */
3377	for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3378		;
3379	/* Check if there is no any left. If so - we succeeded. */
3380	if (i == 16)
3381		return (1);
3382
3383	min = 0;
3384	do {
3385		HDA_BOOTHVERBOSE(
3386			device_printf(devinfo->dev,
3387			    " Tracing pin %d with min nid %d",
3388			    ases[as].pins[i], min);
3389			printf("\n");
3390		);
3391		/* Trace this pin taking min nid into account. */
3392		res = hdaa_audio_trace_adc(devinfo, as, i,
3393		    ases[as].pins[i], 0, min, 0, 0, &length, 0);
3394		if (res == 0) {
3395			/* If we failed - return to previous and redo it. */
3396			HDA_BOOTVERBOSE(
3397				device_printf(devinfo->dev,
3398				    " Unable to trace pin %d seq %d with min "
3399				    "nid %d",
3400				    ases[as].pins[i], i, min);
3401				printf("\n");
3402			);
3403			return (0);
3404		}
3405		HDA_BOOTVERBOSE(
3406			device_printf(devinfo->dev,
3407			    " Pin %d traced to ADC %d\n",
3408			    ases[as].pins[i], res);
3409		);
3410		/* Trace again to mark the path */
3411		hdaa_audio_trace_adc(devinfo, as, i,
3412		    ases[as].pins[i], 0, min, res, 0, &length, length);
3413		ases[as].dacs[0][i] = res;
3414		/* We succeeded, so call next. */
3415		if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1))
3416			return (1);
3417		/* If next failed, we should retry with next min */
3418		hdaa_audio_undo_trace(devinfo, as, i);
3419		ases[as].dacs[0][i] = 0;
3420		min = res + 1;
3421	} while (1);
3422}
3423
3424/*
3425 * Trace input monitor path from mixer to output association.
3426 */
3427static int
3428hdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth)
3429{
3430	struct hdaa_audio_as *ases = devinfo->as;
3431	struct hdaa_widget *w, *wc;
3432	int i, j;
3433	nid_t res = 0;
3434
3435	if (depth > HDA_PARSE_MAXDEPTH)
3436		return (0);
3437	w = hdaa_widget_get(devinfo, nid);
3438	if (w == NULL || w->enable == 0)
3439		return (0);
3440	HDA_BOOTHVERBOSE(
3441		device_printf(devinfo->dev,
3442		    " %*stracing via nid %d\n",
3443			depth + 1, "", w->nid);
3444	);
3445	/* Use only unused widgets */
3446	if (depth > 0 && w->bindas != -1) {
3447		if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) {
3448			HDA_BOOTHVERBOSE(
3449				device_printf(devinfo->dev,
3450				    " %*snid %d found output association %d\n",
3451					depth + 1, "", w->nid, w->bindas);
3452			);
3453			if (w->bindas >= 0)
3454				w->pflags |= HDAA_ADC_MONITOR;
3455			return (1);
3456		} else {
3457			HDA_BOOTHVERBOSE(
3458				device_printf(devinfo->dev,
3459				    " %*snid %d busy by input association %d\n",
3460					depth + 1, "", w->nid, w->bindas);
3461			);
3462			return (0);
3463		}
3464	}
3465
3466	switch (w->type) {
3467	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3468		/* Do not traverse input. AD1988 has digital monitor
3469		for which we are not ready. */
3470		break;
3471	case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3472		if (depth > 0)
3473			break;
3474		/* Fall */
3475	default:
3476		/* Try to find reachable ADCs with specified nid. */
3477		for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3478			wc = hdaa_widget_get(devinfo, j);
3479			if (wc == NULL || wc->enable == 0)
3480				continue;
3481			for (i = 0; i < wc->nconns; i++) {
3482				if (wc->connsenable[i] == 0)
3483					continue;
3484				if (wc->conns[i] != nid)
3485					continue;
3486				if (hdaa_audio_trace_to_out(devinfo,
3487				    j, depth + 1) != 0) {
3488					res = 1;
3489					if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3490					    wc->selconn == -1)
3491						wc->selconn = i;
3492				}
3493			}
3494		}
3495		break;
3496	}
3497	if (res && w->bindas == -1)
3498		w->bindas = -2;
3499
3500	HDA_BOOTHVERBOSE(
3501		device_printf(devinfo->dev,
3502		    " %*snid %d returned %d\n",
3503			depth + 1, "", w->nid, res);
3504	);
3505	return (res);
3506}
3507
3508/*
3509 * Trace extra associations (beeper, monitor)
3510 */
3511static void
3512hdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo)
3513{
3514	struct hdaa_audio_as *as = devinfo->as;
3515	struct hdaa_widget *w;
3516	int j;
3517
3518	/* Input monitor */
3519	/* Find mixer associated with input, but supplying signal
3520	   for output associations. Hope it will be input monitor. */
3521	HDA_BOOTVERBOSE(
3522		device_printf(devinfo->dev,
3523		    "Tracing input monitor\n");
3524	);
3525	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3526		w = hdaa_widget_get(devinfo, j);
3527		if (w == NULL || w->enable == 0)
3528			continue;
3529		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3530			continue;
3531		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3532			continue;
3533		HDA_BOOTVERBOSE(
3534			device_printf(devinfo->dev,
3535			    " Tracing nid %d to out\n",
3536			    j);
3537		);
3538		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3539			HDA_BOOTVERBOSE(
3540				device_printf(devinfo->dev,
3541				    " nid %d is input monitor\n",
3542					w->nid);
3543			);
3544			w->ossdev = SOUND_MIXER_IMIX;
3545		}
3546	}
3547
3548	/* Other inputs monitor */
3549	/* Find input pins supplying signal for output associations.
3550	   Hope it will be input monitoring. */
3551	HDA_BOOTVERBOSE(
3552		device_printf(devinfo->dev,
3553		    "Tracing other input monitors\n");
3554	);
3555	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3556		w = hdaa_widget_get(devinfo, j);
3557		if (w == NULL || w->enable == 0)
3558			continue;
3559		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3560			continue;
3561		if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3562			continue;
3563		HDA_BOOTVERBOSE(
3564			device_printf(devinfo->dev,
3565			    " Tracing nid %d to out\n",
3566			    j);
3567		);
3568		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3569			HDA_BOOTVERBOSE(
3570				device_printf(devinfo->dev,
3571				    " nid %d is input monitor\n",
3572					w->nid);
3573			);
3574		}
3575	}
3576
3577	/* Beeper */
3578	HDA_BOOTVERBOSE(
3579		device_printf(devinfo->dev,
3580		    "Tracing beeper\n");
3581	);
3582	for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3583		w = hdaa_widget_get(devinfo, j);
3584		if (w == NULL || w->enable == 0)
3585			continue;
3586		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
3587			continue;
3588		HDA_BOOTHVERBOSE(
3589			device_printf(devinfo->dev,
3590			    " Tracing nid %d to out\n",
3591			    j);
3592		);
3593		if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
3594			HDA_BOOTVERBOSE(
3595				device_printf(devinfo->dev,
3596				    " nid %d traced to out\n",
3597				    j);
3598			);
3599		}
3600		w->bindas = -2;
3601	}
3602}
3603
3604/*
3605 * Bind assotiations to PCM channels
3606 */
3607static void
3608hdaa_audio_bind_as(struct hdaa_devinfo *devinfo)
3609{
3610	struct hdaa_audio_as *as = devinfo->as;
3611	int i, j, cnt = 0, free;
3612
3613	for (j = 0; j < devinfo->ascnt; j++) {
3614		if (as[j].enable)
3615			cnt += as[j].num_chans;
3616	}
3617	if (devinfo->num_chans == 0) {
3618		devinfo->chans = (struct hdaa_chan *)malloc(
3619		    sizeof(struct hdaa_chan) * cnt,
3620		    M_HDAA, M_ZERO | M_NOWAIT);
3621		if (devinfo->chans == NULL) {
3622			device_printf(devinfo->dev,
3623			    "Channels memory allocation failed!\n");
3624			return;
3625		}
3626	} else {
3627		devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans,
3628		    sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt),
3629		    M_HDAA, M_ZERO | M_NOWAIT);
3630		if (devinfo->chans == NULL) {
3631			devinfo->num_chans = 0;
3632			device_printf(devinfo->dev,
3633			    "Channels memory allocation failed!\n");
3634			return;
3635		}
3636		/* Fixup relative pointers after realloc */
3637		for (j = 0; j < devinfo->num_chans; j++)
3638			devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist;
3639	}
3640	free = devinfo->num_chans;
3641	devinfo->num_chans += cnt;
3642
3643	for (j = free; j < free + cnt; j++) {
3644		devinfo->chans[j].devinfo = devinfo;
3645		devinfo->chans[j].as = -1;
3646	}
3647
3648	/* Assign associations in order of their numbers, */
3649	for (j = 0; j < devinfo->ascnt; j++) {
3650		if (as[j].enable == 0)
3651			continue;
3652		for (i = 0; i < as[j].num_chans; i++) {
3653			devinfo->chans[free].as = j;
3654			devinfo->chans[free].asindex = i;
3655			devinfo->chans[free].dir =
3656			    (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
3657			hdaa_pcmchannel_setup(&devinfo->chans[free]);
3658			as[j].chans[i] = free;
3659			free++;
3660		}
3661	}
3662}
3663
3664static void
3665hdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo)
3666{
3667	struct hdaa_widget *w;
3668	int i;
3669
3670	/* Disable power and volume widgets. */
3671	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3672		w = hdaa_widget_get(devinfo, i);
3673		if (w == NULL || w->enable == 0)
3674			continue;
3675		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
3676		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
3677			w->enable = 0;
3678			HDA_BOOTHVERBOSE(
3679				device_printf(devinfo->dev,
3680				    " Disabling nid %d due to it's"
3681				    " non-audio type.\n",
3682				    w->nid);
3683			);
3684		}
3685	}
3686}
3687
3688static void
3689hdaa_audio_disable_useless(struct hdaa_devinfo *devinfo)
3690{
3691	struct hdaa_widget *w, *cw;
3692	struct hdaa_audio_ctl *ctl;
3693	int done, found, i, j, k;
3694
3695	/* Disable useless pins. */
3696	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3697		w = hdaa_widget_get(devinfo, i);
3698		if (w == NULL || w->enable == 0)
3699			continue;
3700		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
3701			if ((w->wclass.pin.config &
3702			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
3703			    HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
3704				w->enable = 0;
3705				HDA_BOOTHVERBOSE(
3706					device_printf(devinfo->dev,
3707					    " Disabling pin nid %d due"
3708					    " to None connectivity.\n",
3709					    w->nid);
3710				);
3711			} else if ((w->wclass.pin.config &
3712			    HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
3713				w->enable = 0;
3714				HDA_BOOTHVERBOSE(
3715					device_printf(devinfo->dev,
3716					    " Disabling unassociated"
3717					    " pin nid %d.\n",
3718					    w->nid);
3719				);
3720			}
3721		}
3722	}
3723	do {
3724		done = 1;
3725		/* Disable and mute controls for disabled widgets. */
3726		i = 0;
3727		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
3728			if (ctl->enable == 0)
3729				continue;
3730			if (ctl->widget->enable == 0 ||
3731			    (ctl->childwidget != NULL &&
3732			    ctl->childwidget->enable == 0)) {
3733				ctl->forcemute = 1;
3734				ctl->muted = HDAA_AMP_MUTE_ALL;
3735				ctl->left = 0;
3736				ctl->right = 0;
3737				ctl->enable = 0;
3738				if (ctl->ndir == HDAA_CTL_IN)
3739					ctl->widget->connsenable[ctl->index] = 0;
3740				done = 0;
3741				HDA_BOOTHVERBOSE(
3742					device_printf(devinfo->dev,
3743					    " Disabling ctl %d nid %d cnid %d due"
3744					    " to disabled widget.\n", i,
3745					    ctl->widget->nid,
3746					    (ctl->childwidget != NULL)?
3747					    ctl->childwidget->nid:-1);
3748				);
3749			}
3750		}
3751		/* Disable useless widgets. */
3752		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3753			w = hdaa_widget_get(devinfo, i);
3754			if (w == NULL || w->enable == 0)
3755				continue;
3756			/* Disable inputs with disabled child widgets. */
3757			for (j = 0; j < w->nconns; j++) {
3758				if (w->connsenable[j]) {
3759					cw = hdaa_widget_get(devinfo, w->conns[j]);
3760					if (cw == NULL || cw->enable == 0) {
3761						w->connsenable[j] = 0;
3762						HDA_BOOTHVERBOSE(
3763							device_printf(devinfo->dev,
3764							    " Disabling nid %d connection %d due"
3765							    " to disabled child widget.\n",
3766							    i, j);
3767						);
3768					}
3769				}
3770			}
3771			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3772			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3773				continue;
3774			/* Disable mixers and selectors without inputs. */
3775			found = 0;
3776			for (j = 0; j < w->nconns; j++) {
3777				if (w->connsenable[j]) {
3778					found = 1;
3779					break;
3780				}
3781			}
3782			if (found == 0) {
3783				w->enable = 0;
3784				done = 0;
3785				HDA_BOOTHVERBOSE(
3786					device_printf(devinfo->dev,
3787					    " Disabling nid %d due to all it's"
3788					    " inputs disabled.\n", w->nid);
3789				);
3790			}
3791			/* Disable nodes without consumers. */
3792			if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3793			    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3794				continue;
3795			found = 0;
3796			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
3797				cw = hdaa_widget_get(devinfo, k);
3798				if (cw == NULL || cw->enable == 0)
3799					continue;
3800				for (j = 0; j < cw->nconns; j++) {
3801					if (cw->connsenable[j] && cw->conns[j] == i) {
3802						found = 1;
3803						break;
3804					}
3805				}
3806			}
3807			if (found == 0) {
3808				w->enable = 0;
3809				done = 0;
3810				HDA_BOOTHVERBOSE(
3811					device_printf(devinfo->dev,
3812					    " Disabling nid %d due to all it's"
3813					    " consumers disabled.\n", w->nid);
3814				);
3815			}
3816		}
3817	} while (done == 0);
3818
3819}
3820
3821static void
3822hdaa_audio_disable_unas(struct hdaa_devinfo *devinfo)
3823{
3824	struct hdaa_audio_as *as = devinfo->as;
3825	struct hdaa_widget *w, *cw;
3826	struct hdaa_audio_ctl *ctl;
3827	int i, j, k;
3828
3829	/* Disable unassosiated widgets. */
3830	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3831		w = hdaa_widget_get(devinfo, i);
3832		if (w == NULL || w->enable == 0)
3833			continue;
3834		if (w->bindas == -1) {
3835			w->enable = 0;
3836			HDA_BOOTHVERBOSE(
3837				device_printf(devinfo->dev,
3838				    " Disabling unassociated nid %d.\n",
3839				    w->nid);
3840			);
3841		}
3842	}
3843	/* Disable input connections on input pin and
3844	 * output on output. */
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->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3850			continue;
3851		if (w->bindas < 0)
3852			continue;
3853		if (as[w->bindas].dir == HDAA_CTL_IN) {
3854			for (j = 0; j < w->nconns; j++) {
3855				if (w->connsenable[j] == 0)
3856					continue;
3857				w->connsenable[j] = 0;
3858				HDA_BOOTHVERBOSE(
3859					device_printf(devinfo->dev,
3860					    " Disabling connection to input pin "
3861					    "nid %d conn %d.\n",
3862					    i, j);
3863				);
3864			}
3865			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
3866			    HDAA_CTL_IN, -1, 1);
3867			if (ctl && ctl->enable) {
3868				ctl->forcemute = 1;
3869				ctl->muted = HDAA_AMP_MUTE_ALL;
3870				ctl->left = 0;
3871				ctl->right = 0;
3872				ctl->enable = 0;
3873			}
3874		} else {
3875			ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
3876			    HDAA_CTL_OUT, -1, 1);
3877			if (ctl && ctl->enable) {
3878				ctl->forcemute = 1;
3879				ctl->muted = HDAA_AMP_MUTE_ALL;
3880				ctl->left = 0;
3881				ctl->right = 0;
3882				ctl->enable = 0;
3883			}
3884			for (k = devinfo->startnode; k < devinfo->endnode; k++) {
3885				cw = hdaa_widget_get(devinfo, k);
3886				if (cw == NULL || cw->enable == 0)
3887					continue;
3888				for (j = 0; j < cw->nconns; j++) {
3889					if (cw->connsenable[j] && cw->conns[j] == i) {
3890						cw->connsenable[j] = 0;
3891						HDA_BOOTHVERBOSE(
3892							device_printf(devinfo->dev,
3893							    " Disabling connection from output pin "
3894							    "nid %d conn %d cnid %d.\n",
3895							    k, j, i);
3896						);
3897						if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
3898						    cw->nconns > 1)
3899							continue;
3900						ctl = hdaa_audio_ctl_amp_get(devinfo, k,
3901					    HDAA_CTL_IN, j, 1);
3902						if (ctl && ctl->enable) {
3903							ctl->forcemute = 1;
3904							ctl->muted = HDAA_AMP_MUTE_ALL;
3905							ctl->left = 0;
3906							ctl->right = 0;
3907							ctl->enable = 0;
3908						}
3909					}
3910				}
3911			}
3912		}
3913	}
3914}
3915
3916static void
3917hdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo)
3918{
3919	struct hdaa_audio_as *as = devinfo->as;
3920	struct hdaa_widget *w;
3921	int i, j;
3922
3923	/* On playback path we can safely disable all unseleted inputs. */
3924	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3925		w = hdaa_widget_get(devinfo, i);
3926		if (w == NULL || w->enable == 0)
3927			continue;
3928		if (w->nconns <= 1)
3929			continue;
3930		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3931			continue;
3932		if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN)
3933			continue;
3934		for (j = 0; j < w->nconns; j++) {
3935			if (w->connsenable[j] == 0)
3936				continue;
3937			if (w->selconn < 0 || w->selconn == j)
3938				continue;
3939			w->connsenable[j] = 0;
3940			HDA_BOOTHVERBOSE(
3941				device_printf(devinfo->dev,
3942				    " Disabling unselected connection "
3943				    "nid %d conn %d.\n",
3944				    i, j);
3945			);
3946		}
3947	}
3948}
3949
3950static void
3951hdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo)
3952{
3953	struct hdaa_audio_as *ases = devinfo->as;
3954	struct hdaa_widget *w, *cw;
3955	struct hdaa_audio_ctl *ctl;
3956	int i, j;
3957
3958	/* Disable crossassociatement and unwanted crosschannel connections. */
3959	/* ... using selectors */
3960	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3961		w = hdaa_widget_get(devinfo, i);
3962		if (w == NULL || w->enable == 0)
3963			continue;
3964		if (w->nconns <= 1)
3965			continue;
3966		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3967			continue;
3968		/* Allow any -> mix */
3969		if (w->bindas == -2)
3970			continue;
3971		for (j = 0; j < w->nconns; j++) {
3972			if (w->connsenable[j] == 0)
3973				continue;
3974			cw = hdaa_widget_get(devinfo, w->conns[j]);
3975			if (cw == NULL || w->enable == 0)
3976				continue;
3977			/* Allow mix -> out. */
3978			if (cw->bindas == -2 && w->bindas >= 0 &&
3979			    ases[w->bindas].dir == HDAA_CTL_OUT)
3980				continue;
3981			/* Allow mix -> mixed-in. */
3982			if (cw->bindas == -2 && w->bindas >= 0 &&
3983			    ases[w->bindas].mixed)
3984				continue;
3985			/* Allow in -> mix. */
3986			if ((w->pflags & HDAA_ADC_MONITOR) &&
3987			     cw->bindas >= 0 &&
3988			     ases[cw->bindas].dir == HDAA_CTL_IN)
3989				continue;
3990			/* Allow if have common as/seqs. */
3991			if (w->bindas == cw->bindas &&
3992			    (w->bindseqmask & cw->bindseqmask) != 0)
3993				continue;
3994			w->connsenable[j] = 0;
3995			HDA_BOOTHVERBOSE(
3996				device_printf(devinfo->dev,
3997				    " Disabling crossassociatement connection "
3998				    "nid %d conn %d cnid %d.\n",
3999				    i, j, cw->nid);
4000			);
4001		}
4002	}
4003	/* ... using controls */
4004	i = 0;
4005	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4006		if (ctl->enable == 0 || ctl->childwidget == NULL)
4007			continue;
4008		/* Allow any -> mix */
4009		if (ctl->widget->bindas == -2)
4010			continue;
4011		/* Allow mix -> out. */
4012		if (ctl->childwidget->bindas == -2 &&
4013		    ctl->widget->bindas >= 0 &&
4014		    ases[ctl->widget->bindas].dir == HDAA_CTL_OUT)
4015			continue;
4016		/* Allow mix -> mixed-in. */
4017		if (ctl->childwidget->bindas == -2 &&
4018		    ctl->widget->bindas >= 0 &&
4019		    ases[ctl->widget->bindas].mixed)
4020			continue;
4021		/* Allow in -> mix. */
4022		if ((ctl->widget->pflags & HDAA_ADC_MONITOR) &&
4023		    ctl->childwidget->bindas >= 0 &&
4024		    ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN)
4025			continue;
4026		/* Allow if have common as/seqs. */
4027		if (ctl->widget->bindas == ctl->childwidget->bindas &&
4028		    (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0)
4029			continue;
4030		ctl->forcemute = 1;
4031		ctl->muted = HDAA_AMP_MUTE_ALL;
4032		ctl->left = 0;
4033		ctl->right = 0;
4034		ctl->enable = 0;
4035		if (ctl->ndir == HDAA_CTL_IN)
4036			ctl->widget->connsenable[ctl->index] = 0;
4037		HDA_BOOTHVERBOSE(
4038			device_printf(devinfo->dev,
4039			    " Disabling crossassociatement connection "
4040			    "ctl %d nid %d cnid %d.\n", i,
4041			    ctl->widget->nid,
4042			    ctl->childwidget->nid);
4043		);
4044	}
4045
4046}
4047
4048/*
4049 * Find controls to control amplification for source and calculate possible
4050 * amplification range.
4051 */
4052static int
4053hdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4054    int ossdev, int ctlable, int depth, int *minamp, int *maxamp)
4055{
4056	struct hdaa_widget *w, *wc;
4057	struct hdaa_audio_ctl *ctl;
4058	int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4059
4060	if (depth > HDA_PARSE_MAXDEPTH)
4061		return (found);
4062
4063	w = hdaa_widget_get(devinfo, nid);
4064	if (w == NULL || w->enable == 0)
4065		return (found);
4066
4067	/* Count number of active inputs. */
4068	if (depth > 0) {
4069		for (j = 0; j < w->nconns; j++) {
4070			if (!w->connsenable[j])
4071				continue;
4072			conns++;
4073		}
4074	}
4075
4076	/* If this is not a first step - use input mixer.
4077	   Pins have common input ctl so care must be taken. */
4078	if (depth > 0 && ctlable && (conns == 1 ||
4079	    w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
4080		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
4081		    index, 1);
4082		if (ctl) {
4083			ctl->ossmask |= (1 << ossdev);
4084			found++;
4085			if (*minamp == *maxamp) {
4086				*minamp += MINQDB(ctl);
4087				*maxamp += MAXQDB(ctl);
4088			}
4089		}
4090	}
4091
4092	/* If widget has own ossdev - not traverse it.
4093	   It will be traversed on it's own. */
4094	if (w->ossdev >= 0 && depth > 0)
4095		return (found);
4096
4097	/* We must not traverse pin */
4098	if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4099	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
4100	    depth > 0)
4101		return (found);
4102
4103	/* record that this widget exports such signal, */
4104	w->ossmask |= (1 << ossdev);
4105
4106	/*
4107	 * If signals mixed, we can't assign controls farther.
4108	 * Ignore this on depth zero. Caller must knows why.
4109	 */
4110	if (conns > 1 &&
4111	    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4112		ctlable = 0;
4113
4114	if (ctlable) {
4115		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
4116		if (ctl) {
4117			ctl->ossmask |= (1 << ossdev);
4118			found++;
4119			if (*minamp == *maxamp) {
4120				*minamp += MINQDB(ctl);
4121				*maxamp += MAXQDB(ctl);
4122			}
4123		}
4124	}
4125
4126	cminamp = cmaxamp = 0;
4127	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4128		wc = hdaa_widget_get(devinfo, i);
4129		if (wc == NULL || wc->enable == 0)
4130			continue;
4131		for (j = 0; j < wc->nconns; j++) {
4132			if (wc->connsenable[j] && wc->conns[j] == nid) {
4133				tminamp = tmaxamp = 0;
4134				found += hdaa_audio_ctl_source_amp(devinfo,
4135				    wc->nid, j, ossdev, ctlable, depth + 1,
4136				    &tminamp, &tmaxamp);
4137				if (cminamp == 0 && cmaxamp == 0) {
4138					cminamp = tminamp;
4139					cmaxamp = tmaxamp;
4140				} else if (tminamp != tmaxamp) {
4141					cminamp = imax(cminamp, tminamp);
4142					cmaxamp = imin(cmaxamp, tmaxamp);
4143				}
4144			}
4145		}
4146	}
4147	if (*minamp == *maxamp && cminamp < cmaxamp) {
4148		*minamp += cminamp;
4149		*maxamp += cmaxamp;
4150	}
4151	return (found);
4152}
4153
4154/*
4155 * Find controls to control amplification for destination and calculate
4156 * possible amplification range.
4157 */
4158static int
4159hdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4160    int ossdev, int depth, int *minamp, int *maxamp)
4161{
4162	struct hdaa_audio_as *as = devinfo->as;
4163	struct hdaa_widget *w, *wc;
4164	struct hdaa_audio_ctl *ctl;
4165	int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4166
4167	if (depth > HDA_PARSE_MAXDEPTH)
4168		return (found);
4169
4170	w = hdaa_widget_get(devinfo, nid);
4171	if (w == NULL || w->enable == 0)
4172		return (found);
4173
4174	if (depth > 0) {
4175		/* If this node produce output for several consumers,
4176		   we can't touch it. */
4177		consumers = 0;
4178		for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4179			wc = hdaa_widget_get(devinfo, i);
4180			if (wc == NULL || wc->enable == 0)
4181				continue;
4182			for (j = 0; j < wc->nconns; j++) {
4183				if (wc->connsenable[j] && wc->conns[j] == nid)
4184					consumers++;
4185			}
4186		}
4187		/* The only exception is if real HP redirection is configured
4188		   and this is a duplication point.
4189		   XXX: Actually exception is not completely correct.
4190		   XXX: Duplication point check is not perfect. */
4191		if ((consumers == 2 && (w->bindas < 0 ||
4192		    as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
4193		    (w->bindseqmask & (1 << 15)) == 0)) ||
4194		    consumers > 2)
4195			return (found);
4196
4197		/* Else use it's output mixer. */
4198		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4199		    HDAA_CTL_OUT, -1, 1);
4200		if (ctl) {
4201			ctl->ossmask |= (1 << ossdev);
4202			found++;
4203			if (*minamp == *maxamp) {
4204				*minamp += MINQDB(ctl);
4205				*maxamp += MAXQDB(ctl);
4206			}
4207		}
4208	}
4209
4210	/* We must not traverse pin */
4211	if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4212	    depth > 0)
4213		return (found);
4214
4215	cminamp = cmaxamp = 0;
4216	for (i = 0; i < w->nconns; i++) {
4217		if (w->connsenable[i] == 0)
4218			continue;
4219		if (index >= 0 && i != index)
4220			continue;
4221		tminamp = tmaxamp = 0;
4222		ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4223		    HDAA_CTL_IN, i, 1);
4224		if (ctl) {
4225			ctl->ossmask |= (1 << ossdev);
4226			found++;
4227			if (*minamp == *maxamp) {
4228				tminamp += MINQDB(ctl);
4229				tmaxamp += MAXQDB(ctl);
4230			}
4231		}
4232		found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
4233		    depth + 1, &tminamp, &tmaxamp);
4234		if (cminamp == 0 && cmaxamp == 0) {
4235			cminamp = tminamp;
4236			cmaxamp = tmaxamp;
4237		} else if (tminamp != tmaxamp) {
4238			cminamp = imax(cminamp, tminamp);
4239			cmaxamp = imin(cmaxamp, tmaxamp);
4240		}
4241	}
4242	if (*minamp == *maxamp && cminamp < cmaxamp) {
4243		*minamp += cminamp;
4244		*maxamp += cmaxamp;
4245	}
4246	return (found);
4247}
4248
4249/*
4250 * Assign OSS names to sound sources
4251 */
4252static void
4253hdaa_audio_assign_names(struct hdaa_devinfo *devinfo)
4254{
4255	struct hdaa_audio_as *as = devinfo->as;
4256	struct hdaa_widget *w;
4257	int i, j;
4258	int type = -1, use, used = 0;
4259	static const int types[7][13] = {
4260	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4261	      SOUND_MIXER_LINE3, -1 },	/* line */
4262	    { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
4263	    { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
4264	    { SOUND_MIXER_CD, -1 },	/* cd */
4265	    { SOUND_MIXER_SPEAKER, -1 },	/* speaker */
4266	    { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
4267	      -1 },	/* digital */
4268	    { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4269	      SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
4270	      SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
4271	      SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
4272	      -1 }	/* others */
4273	};
4274
4275	/* Surely known names */
4276	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4277		w = hdaa_widget_get(devinfo, i);
4278		if (w == NULL || w->enable == 0)
4279			continue;
4280		if (w->bindas == -1)
4281			continue;
4282		use = -1;
4283		switch (w->type) {
4284		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4285			if (as[w->bindas].dir == HDAA_CTL_OUT)
4286				break;
4287			type = -1;
4288			switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4289			case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4290				type = 0;
4291				break;
4292			case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4293				if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
4294				    == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4295					break;
4296				type = 1;
4297				break;
4298			case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4299				type = 3;
4300				break;
4301			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4302				type = 4;
4303				break;
4304			case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
4305			case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
4306				type = 5;
4307				break;
4308			}
4309			if (type == -1)
4310				break;
4311			j = 0;
4312			while (types[type][j] >= 0 &&
4313			    (used & (1 << types[type][j])) != 0) {
4314				j++;
4315			}
4316			if (types[type][j] >= 0)
4317				use = types[type][j];
4318			break;
4319		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4320			use = SOUND_MIXER_PCM;
4321			break;
4322		case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
4323			use = SOUND_MIXER_SPEAKER;
4324			break;
4325		default:
4326			break;
4327		}
4328		if (use >= 0) {
4329			w->ossdev = use;
4330			used |= (1 << use);
4331		}
4332	}
4333	/* Semi-known names */
4334	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4335		w = hdaa_widget_get(devinfo, i);
4336		if (w == NULL || w->enable == 0)
4337			continue;
4338		if (w->ossdev >= 0)
4339			continue;
4340		if (w->bindas == -1)
4341			continue;
4342		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4343			continue;
4344		if (as[w->bindas].dir == HDAA_CTL_OUT)
4345			continue;
4346		type = -1;
4347		switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4348		case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
4349		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4350		case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
4351		case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
4352			type = 0;
4353			break;
4354		case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4355			type = 2;
4356			break;
4357		case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
4358		case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
4359			type = 5;
4360			break;
4361		}
4362		if (type == -1)
4363			break;
4364		j = 0;
4365		while (types[type][j] >= 0 &&
4366		    (used & (1 << types[type][j])) != 0) {
4367			j++;
4368		}
4369		if (types[type][j] >= 0) {
4370			w->ossdev = types[type][j];
4371			used |= (1 << types[type][j]);
4372		}
4373	}
4374	/* Others */
4375	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4376		w = hdaa_widget_get(devinfo, i);
4377		if (w == NULL || w->enable == 0)
4378			continue;
4379		if (w->ossdev >= 0)
4380			continue;
4381		if (w->bindas == -1)
4382			continue;
4383		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4384			continue;
4385		if (as[w->bindas].dir == HDAA_CTL_OUT)
4386			continue;
4387		j = 0;
4388		while (types[6][j] >= 0 &&
4389		    (used & (1 << types[6][j])) != 0) {
4390			j++;
4391		}
4392		if (types[6][j] >= 0) {
4393			w->ossdev = types[6][j];
4394			used |= (1 << types[6][j]);
4395		}
4396	}
4397}
4398
4399static void
4400hdaa_audio_build_tree(struct hdaa_devinfo *devinfo)
4401{
4402	struct hdaa_audio_as *as = devinfo->as;
4403	int j, res;
4404
4405	/* Trace all associations in order of their numbers. */
4406	for (j = 0; j < devinfo->ascnt; j++) {
4407		if (as[j].enable == 0)
4408			continue;
4409		HDA_BOOTVERBOSE(
4410			device_printf(devinfo->dev,
4411			    "Tracing association %d (%d)\n", j, as[j].index);
4412		);
4413		if (as[j].dir == HDAA_CTL_OUT) {
4414retry:
4415			res = hdaa_audio_trace_as_out(devinfo, j, 0);
4416			if (res == 0 && as[j].hpredir >= 0 &&
4417			    as[j].fakeredir == 0) {
4418				/* If CODEC can't do analog HP redirection
4419				   try to make it using one more DAC. */
4420				as[j].fakeredir = 1;
4421				goto retry;
4422			}
4423		} else if (as[j].mixed)
4424			res = hdaa_audio_trace_as_in(devinfo, j);
4425		else
4426			res = hdaa_audio_trace_as_in_mch(devinfo, j, 0);
4427		if (res) {
4428			HDA_BOOTVERBOSE(
4429				device_printf(devinfo->dev,
4430				    "Association %d (%d) trace succeeded\n",
4431				    j, as[j].index);
4432			);
4433		} else {
4434			HDA_BOOTVERBOSE(
4435				device_printf(devinfo->dev,
4436				    "Association %d (%d) trace failed\n",
4437				    j, as[j].index);
4438			);
4439			as[j].enable = 0;
4440		}
4441	}
4442
4443	/* Look for additional DACs/ADCs. */
4444	for (j = 0; j < devinfo->ascnt; j++) {
4445		if (as[j].enable == 0)
4446			continue;
4447		hdaa_audio_adddac(devinfo, j);
4448	}
4449
4450	/* Trace mixer and beeper pseudo associations. */
4451	hdaa_audio_trace_as_extra(devinfo);
4452}
4453
4454/*
4455 * Store in pdevinfo new data about whether and how we can control signal
4456 * for OSS device to/from specified widget.
4457 */
4458static void
4459hdaa_adjust_amp(struct hdaa_widget *w, int ossdev,
4460    int found, int minamp, int maxamp)
4461{
4462	struct hdaa_devinfo *devinfo = w->devinfo;
4463	struct hdaa_pcm_devinfo *pdevinfo;
4464
4465	if (w->bindas >= 0)
4466		pdevinfo = devinfo->as[w->bindas].pdevinfo;
4467	else
4468		pdevinfo = &devinfo->devs[0];
4469	if (found)
4470		pdevinfo->ossmask |= (1 << ossdev);
4471	if (minamp == 0 && maxamp == 0)
4472		return;
4473	if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) {
4474		pdevinfo->minamp[ossdev] = minamp;
4475		pdevinfo->maxamp[ossdev] = maxamp;
4476	} else {
4477		pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp);
4478		pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp);
4479	}
4480}
4481
4482/*
4483 * Trace signals from/to all possible sources/destionstions to find possible
4484 * recording sources, OSS device control ranges and to assign controls.
4485 */
4486static void
4487hdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo)
4488{
4489	struct hdaa_audio_as *as = devinfo->as;
4490	struct hdaa_widget *w, *cw;
4491	int i, j, minamp, maxamp, found;
4492
4493	/* Assign mixers to the tree. */
4494	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4495		w = hdaa_widget_get(devinfo, i);
4496		if (w == NULL || w->enable == 0)
4497			continue;
4498		minamp = maxamp = 0;
4499		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
4500		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
4501		    (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4502		    as[w->bindas].dir == HDAA_CTL_IN)) {
4503			if (w->ossdev < 0)
4504				continue;
4505			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4506			    w->ossdev, 1, 0, &minamp, &maxamp);
4507			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4508		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
4509			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4510			    SOUND_MIXER_RECLEV, 0, &minamp, &maxamp);
4511			hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp);
4512		} else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4513		    as[w->bindas].dir == HDAA_CTL_OUT) {
4514			found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4515			    SOUND_MIXER_VOLUME, 0, &minamp, &maxamp);
4516			hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp);
4517		}
4518		if (w->ossdev == SOUND_MIXER_IMIX) {
4519			minamp = maxamp = 0;
4520			found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4521			    w->ossdev, 1, 0, &minamp, &maxamp);
4522			if (minamp == maxamp) {
4523				/* If we are unable to control input monitor
4524				   as source - try to control it as destination. */
4525				found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4526				    w->ossdev, 0, &minamp, &maxamp);
4527				w->pflags |= HDAA_IMIX_AS_DST;
4528			}
4529			hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4530		}
4531		if (w->pflags & HDAA_ADC_MONITOR) {
4532			for (j = 0; j < w->nconns; j++) {
4533				if (!w->connsenable[j])
4534				    continue;
4535				cw = hdaa_widget_get(devinfo, w->conns[j]);
4536				if (cw == NULL || cw->enable == 0)
4537				    continue;
4538				if (cw->bindas == -1)
4539				    continue;
4540				if (cw->bindas >= 0 &&
4541				    as[cw->bindas].dir != HDAA_CTL_IN)
4542					continue;
4543				minamp = maxamp = 0;
4544				found = hdaa_audio_ctl_dest_amp(devinfo,
4545				    w->nid, j, SOUND_MIXER_IGAIN, 0,
4546				    &minamp, &maxamp);
4547				hdaa_adjust_amp(w, SOUND_MIXER_IGAIN,
4548				    found, minamp, maxamp);
4549			}
4550		}
4551	}
4552}
4553
4554static void
4555hdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo)
4556{
4557	struct hdaa_audio_as *as = devinfo->as;
4558	struct hdaa_widget *w;
4559	uint32_t pincap;
4560	int i;
4561
4562	for (i = 0; i < devinfo->nodecnt; i++) {
4563		w = &devinfo->widget[i];
4564		if (w == NULL)
4565			continue;
4566		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4567		    w->waspin == 0)
4568			continue;
4569
4570		pincap = w->wclass.pin.cap;
4571
4572		/* Disable everything. */
4573		w->wclass.pin.ctrl &= ~(
4574		    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
4575		    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
4576		    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
4577		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
4578
4579		if (w->enable == 0) {
4580			/* Pin is unused so left it disabled. */
4581			continue;
4582		} else if (w->waspin) {
4583			/* Enable input for beeper input. */
4584			w->wclass.pin.ctrl |=
4585			    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
4586		} else if (w->bindas < 0 || as[w->bindas].enable == 0) {
4587			/* Pin is unused so left it disabled. */
4588			continue;
4589		} else if (as[w->bindas].dir == HDAA_CTL_IN) {
4590			/* Input pin, configure for input. */
4591			if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
4592				w->wclass.pin.ctrl |=
4593				    HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
4594
4595			if ((devinfo->quirks & HDAA_QUIRK_IVREF100) &&
4596			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4597				w->wclass.pin.ctrl |=
4598				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4599				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4600			else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) &&
4601			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4602				w->wclass.pin.ctrl |=
4603				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4604				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4605			else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) &&
4606			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4607				w->wclass.pin.ctrl |=
4608				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4609				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4610		} else {
4611			/* Output pin, configure for output. */
4612			if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
4613				w->wclass.pin.ctrl |=
4614				    HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
4615
4616			if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
4617			    (w->wclass.pin.config &
4618			    HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
4619			    HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
4620				w->wclass.pin.ctrl |=
4621				    HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
4622
4623			if ((devinfo->quirks & HDAA_QUIRK_OVREF100) &&
4624			    HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
4625				w->wclass.pin.ctrl |=
4626				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4627				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
4628			else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) &&
4629			    HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
4630				w->wclass.pin.ctrl |=
4631				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4632				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
4633			else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) &&
4634			    HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
4635				w->wclass.pin.ctrl |=
4636				    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
4637				    HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
4638		}
4639	}
4640}
4641
4642static void
4643hdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo)
4644{
4645	struct hdaa_audio_ctl *ctl;
4646	int i, z;
4647
4648	i = 0;
4649	while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4650		if (ctl->enable == 0 || ctl->ossmask != 0) {
4651			/* Mute disabled and mixer controllable controls.
4652			 * Last will be initialized by mixer_init().
4653			 * This expected to reduce click on startup. */
4654			hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0);
4655			continue;
4656		}
4657		/* Init fixed controls to 0dB amplification. */
4658		z = ctl->offset;
4659		if (z > ctl->step)
4660			z = ctl->step;
4661		hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z);
4662	}
4663}
4664
4665static void
4666hdaa_gpio_commit(struct hdaa_devinfo *devinfo)
4667{
4668	uint32_t gdata, gmask, gdir;
4669	int i, numgpio;
4670
4671	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
4672	if (devinfo->gpio != 0 && numgpio != 0) {
4673		gdata = hda_command(devinfo->dev,
4674		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
4675		gmask = hda_command(devinfo->dev,
4676		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
4677		gdir = hda_command(devinfo->dev,
4678		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
4679		for (i = 0; i < numgpio; i++) {
4680			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4681			    HDAA_GPIO_SET(i)) {
4682				gdata |= (1 << i);
4683				gmask |= (1 << i);
4684				gdir |= (1 << i);
4685			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4686			    HDAA_GPIO_CLEAR(i)) {
4687				gdata &= ~(1 << i);
4688				gmask |= (1 << i);
4689				gdir |= (1 << i);
4690			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4691			    HDAA_GPIO_DISABLE(i)) {
4692				gmask &= ~(1 << i);
4693			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4694			    HDAA_GPIO_INPUT(i)) {
4695				gmask |= (1 << i);
4696				gdir &= ~(1 << i);
4697			}
4698		}
4699		HDA_BOOTVERBOSE(
4700			device_printf(devinfo->dev, "GPIO commit\n");
4701		);
4702		hda_command(devinfo->dev,
4703		    HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask));
4704		hda_command(devinfo->dev,
4705		    HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir));
4706		hda_command(devinfo->dev,
4707		    HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata));
4708		HDA_BOOTVERBOSE(
4709			hdaa_dump_gpio(devinfo);
4710		);
4711	}
4712}
4713
4714static void
4715hdaa_gpo_commit(struct hdaa_devinfo *devinfo)
4716{
4717	uint32_t gdata;
4718	int i, numgpo;
4719
4720	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
4721	if (devinfo->gpo != 0 && numgpo != 0) {
4722		gdata = hda_command(devinfo->dev,
4723		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
4724		for (i = 0; i < numgpo; i++) {
4725			if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4726			    HDAA_GPIO_SET(i)) {
4727				gdata |= (1 << i);
4728			} else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
4729			    HDAA_GPIO_CLEAR(i)) {
4730				gdata &= ~(1 << i);
4731			}
4732		}
4733		HDA_BOOTVERBOSE(
4734			device_printf(devinfo->dev, "GPO commit\n");
4735		);
4736		hda_command(devinfo->dev,
4737		    HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata));
4738		HDA_BOOTVERBOSE(
4739			hdaa_dump_gpo(devinfo);
4740		);
4741	}
4742}
4743
4744static void
4745hdaa_audio_commit(struct hdaa_devinfo *devinfo)
4746{
4747	struct hdaa_widget *w;
4748	int i;
4749
4750	/* Commit controls. */
4751	hdaa_audio_ctl_commit(devinfo);
4752
4753	/* Commit selectors, pins and EAPD. */
4754	for (i = 0; i < devinfo->nodecnt; i++) {
4755		w = &devinfo->widget[i];
4756		if (w == NULL)
4757			continue;
4758		if (w->selconn == -1)
4759			w->selconn = 0;
4760		if (w->nconns > 0)
4761			hdaa_widget_connection_select(w, w->selconn);
4762		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
4763		    w->waspin) {
4764			hda_command(devinfo->dev,
4765			    HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid,
4766			    w->wclass.pin.ctrl));
4767		}
4768		if (w->param.eapdbtl != HDA_INVALID) {
4769			uint32_t val;
4770
4771			val = w->param.eapdbtl;
4772			if (devinfo->quirks &
4773			    HDAA_QUIRK_EAPDINV)
4774				val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
4775			hda_command(devinfo->dev,
4776			    HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid,
4777			    val));
4778		}
4779	}
4780
4781	hdaa_gpio_commit(devinfo);
4782	hdaa_gpo_commit(devinfo);
4783}
4784
4785static void
4786hdaa_powerup(struct hdaa_devinfo *devinfo)
4787{
4788	int i;
4789
4790	hda_command(devinfo->dev,
4791	    HDA_CMD_SET_POWER_STATE(0,
4792	    devinfo->nid, HDA_CMD_POWER_STATE_D0));
4793	DELAY(100);
4794
4795	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4796		hda_command(devinfo->dev,
4797		    HDA_CMD_SET_POWER_STATE(0,
4798		    i, HDA_CMD_POWER_STATE_D0));
4799	}
4800	DELAY(1000);
4801}
4802
4803static int
4804hdaa_pcmchannel_setup(struct hdaa_chan *ch)
4805{
4806	struct hdaa_devinfo *devinfo = ch->devinfo;
4807	struct hdaa_audio_as *as = devinfo->as;
4808	struct hdaa_widget *w;
4809	uint32_t cap, fmtcap, pcmcap;
4810	int i, j, ret, channels, onlystereo;
4811	uint16_t pinset;
4812
4813	ch->caps = hdaa_caps;
4814	ch->caps.fmtlist = ch->fmtlist;
4815	ch->bit16 = 1;
4816	ch->bit32 = 0;
4817	ch->pcmrates[0] = 48000;
4818	ch->pcmrates[1] = 0;
4819	ch->stripecap = 0xff;
4820
4821	ret = 0;
4822	channels = 0;
4823	onlystereo = 1;
4824	pinset = 0;
4825	fmtcap = devinfo->supp_stream_formats;
4826	pcmcap = devinfo->supp_pcm_size_rate;
4827
4828	for (i = 0; i < 16; i++) {
4829		/* Check as is correct */
4830		if (ch->as < 0)
4831			break;
4832		/* Cound only present DACs */
4833		if (as[ch->as].dacs[ch->asindex][i] <= 0)
4834			continue;
4835		/* Ignore duplicates */
4836		for (j = 0; j < ret; j++) {
4837			if (ch->io[j] == as[ch->as].dacs[ch->asindex][i])
4838				break;
4839		}
4840		if (j < ret)
4841			continue;
4842
4843		w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]);
4844		if (w == NULL || w->enable == 0)
4845			continue;
4846		cap = w->param.supp_stream_formats;
4847		if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
4848		    !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
4849			continue;
4850		/* Many CODECs does not declare AC3 support on SPDIF.
4851		   I don't beleave that they doesn't support it! */
4852		if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
4853			cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
4854		if (ret == 0) {
4855			fmtcap = cap;
4856			pcmcap = w->param.supp_pcm_size_rate;
4857		} else {
4858			fmtcap &= cap;
4859			pcmcap &= w->param.supp_pcm_size_rate;
4860		}
4861		ch->io[ret++] = as[ch->as].dacs[ch->asindex][i];
4862		ch->stripecap &= w->wclass.conv.stripecap;
4863		/* Do not count redirection pin/dac channels. */
4864		if (i == 15 && as[ch->as].hpredir >= 0)
4865			continue;
4866		channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
4867		if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
4868			onlystereo = 0;
4869		pinset |= (1 << i);
4870	}
4871	ch->io[ret] = -1;
4872	ch->channels = channels;
4873
4874	if (as[ch->as].fakeredir)
4875		ret--;
4876	/* Standard speaks only about stereo pins and playback, ... */
4877	if ((!onlystereo) || as[ch->as].mixed)
4878		pinset = 0;
4879	/* ..., but there it gives us info about speakers layout. */
4880	as[ch->as].pinset = pinset;
4881
4882	ch->supp_stream_formats = fmtcap;
4883	ch->supp_pcm_size_rate = pcmcap;
4884
4885	/*
4886	 *  8bit = 0
4887	 * 16bit = 1
4888	 * 20bit = 2
4889	 * 24bit = 3
4890	 * 32bit = 4
4891	 */
4892	if (ret > 0) {
4893		i = 0;
4894		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
4895			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
4896				ch->bit16 = 1;
4897			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
4898				ch->bit16 = 0;
4899			if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
4900				ch->bit32 = 3;
4901			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
4902				ch->bit32 = 2;
4903			else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
4904				ch->bit32 = 4;
4905			if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) {
4906				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
4907				if (ch->bit32)
4908					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
4909			}
4910			if (channels >= 2) {
4911				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
4912				if (ch->bit32)
4913					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
4914			}
4915			if (channels >= 3 && !onlystereo) {
4916				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0);
4917				if (ch->bit32)
4918					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0);
4919				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1);
4920				if (ch->bit32)
4921					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1);
4922			}
4923			if (channels >= 4) {
4924				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
4925				if (ch->bit32)
4926					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
4927				if (!onlystereo) {
4928					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1);
4929					if (ch->bit32)
4930						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1);
4931				}
4932			}
4933			if (channels >= 5 && !onlystereo) {
4934				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0);
4935				if (ch->bit32)
4936					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0);
4937				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1);
4938				if (ch->bit32)
4939					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1);
4940			}
4941			if (channels >= 6) {
4942				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
4943				if (ch->bit32)
4944					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
4945				if (!onlystereo) {
4946					ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0);
4947					if (ch->bit32)
4948						ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0);
4949				}
4950			}
4951			if (channels >= 7 && !onlystereo) {
4952				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0);
4953				if (ch->bit32)
4954					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0);
4955				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1);
4956				if (ch->bit32)
4957					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1);
4958			}
4959			if (channels >= 8) {
4960				ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
4961				if (ch->bit32)
4962					ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
4963			}
4964		}
4965		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
4966			ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
4967		}
4968		ch->fmtlist[i] = 0;
4969		i = 0;
4970		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
4971			ch->pcmrates[i++] = 8000;
4972		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
4973			ch->pcmrates[i++] = 11025;
4974		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
4975			ch->pcmrates[i++] = 16000;
4976		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
4977			ch->pcmrates[i++] = 22050;
4978		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
4979			ch->pcmrates[i++] = 32000;
4980		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
4981			ch->pcmrates[i++] = 44100;
4982		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
4983		ch->pcmrates[i++] = 48000;
4984		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
4985			ch->pcmrates[i++] = 88200;
4986		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
4987			ch->pcmrates[i++] = 96000;
4988		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
4989			ch->pcmrates[i++] = 176400;
4990		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
4991			ch->pcmrates[i++] = 192000;
4992		/* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
4993		ch->pcmrates[i] = 0;
4994		if (i > 0) {
4995			ch->caps.minspeed = ch->pcmrates[0];
4996			ch->caps.maxspeed = ch->pcmrates[i - 1];
4997		}
4998	}
4999
5000	return (ret);
5001}
5002
5003static void
5004hdaa_prepare_pcms(struct hdaa_devinfo *devinfo)
5005{
5006	struct hdaa_audio_as *as = devinfo->as;
5007	int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
5008
5009	for (i = 0; i < devinfo->ascnt; i++) {
5010		if (as[i].enable == 0)
5011			continue;
5012		if (as[i].dir == HDAA_CTL_IN) {
5013			if (as[i].digital)
5014				drdev++;
5015			else
5016				ardev++;
5017		} else {
5018			if (as[i].digital)
5019				dpdev++;
5020			else
5021				apdev++;
5022		}
5023	}
5024	devinfo->num_devs =
5025	    max(ardev, apdev) + max(drdev, dpdev);
5026	devinfo->devs =
5027	    (struct hdaa_pcm_devinfo *)malloc(
5028	    devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo),
5029	    M_HDAA, M_ZERO | M_NOWAIT);
5030	if (devinfo->devs == NULL) {
5031		device_printf(devinfo->dev,
5032		    "Unable to allocate memory for devices\n");
5033		return;
5034	}
5035	for (i = 0; i < devinfo->num_devs; i++) {
5036		devinfo->devs[i].index = i;
5037		devinfo->devs[i].devinfo = devinfo;
5038		devinfo->devs[i].playas = -1;
5039		devinfo->devs[i].recas = -1;
5040		devinfo->devs[i].digital = 255;
5041	}
5042	for (i = 0; i < devinfo->ascnt; i++) {
5043		if (as[i].enable == 0)
5044			continue;
5045		for (j = 0; j < devinfo->num_devs; j++) {
5046			if (devinfo->devs[j].digital != 255 &&
5047			    (!devinfo->devs[j].digital) !=
5048			    (!as[i].digital))
5049				continue;
5050			if (as[i].dir == HDAA_CTL_IN) {
5051				if (devinfo->devs[j].recas >= 0)
5052					continue;
5053				devinfo->devs[j].recas = i;
5054			} else {
5055				if (devinfo->devs[j].playas >= 0)
5056					continue;
5057				devinfo->devs[j].playas = i;
5058			}
5059			as[i].pdevinfo = &devinfo->devs[j];
5060			for (k = 0; k < as[i].num_chans; k++) {
5061				devinfo->chans[as[i].chans[k]].pdevinfo =
5062				    &devinfo->devs[j];
5063			}
5064			devinfo->devs[j].digital = as[i].digital;
5065			break;
5066		}
5067	}
5068}
5069
5070static void
5071hdaa_create_pcms(struct hdaa_devinfo *devinfo)
5072{
5073	int i;
5074
5075	for (i = 0; i < devinfo->num_devs; i++) {
5076		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
5077
5078		pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1);
5079		device_set_ivars(pdevinfo->dev, (void *)pdevinfo);
5080	}
5081}
5082
5083static void
5084hdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
5085{
5086	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5087	struct hdaa_audio_ctl *ctl;
5088	char buf[64];
5089	int i, j, printed;
5090
5091	if (flag == 0) {
5092		flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5093		    SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5094		    SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
5095		    SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
5096	}
5097
5098	for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
5099		if ((flag & (1 << j)) == 0)
5100			continue;
5101		i = 0;
5102		printed = 0;
5103		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5104			if (ctl->enable == 0 ||
5105			    ctl->widget->enable == 0)
5106				continue;
5107			if (!((pdevinfo->playas >= 0 &&
5108			    ctl->widget->bindas == pdevinfo->playas) ||
5109			    (pdevinfo->recas >= 0 &&
5110			    ctl->widget->bindas == pdevinfo->recas) ||
5111			    (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
5112				continue;
5113			if ((ctl->ossmask & (1 << j)) == 0)
5114				continue;
5115
5116			if (printed == 0) {
5117				device_printf(pdevinfo->dev, "\n");
5118				if (banner != NULL) {
5119					device_printf(pdevinfo->dev, "%s", banner);
5120				} else {
5121					device_printf(pdevinfo->dev, "Unknown Ctl");
5122				}
5123				printf(" (OSS: %s)",
5124				    hdaa_audio_ctl_ossmixer_mask2allname(1 << j,
5125				    buf, sizeof(buf)));
5126				if (pdevinfo->ossmask & (1 << j)) {
5127					printf(": %+d/%+ddB\n",
5128					    pdevinfo->minamp[j] / 4,
5129					    pdevinfo->maxamp[j] / 4);
5130				} else
5131					printf("\n");
5132				device_printf(pdevinfo->dev, "   |\n");
5133				printed = 1;
5134			}
5135			device_printf(pdevinfo->dev, "   +- ctl %2d (nid %3d %s", i,
5136				ctl->widget->nid,
5137				(ctl->ndir == HDAA_CTL_IN)?"in ":"out");
5138			if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir)
5139				printf(" %2d): ", ctl->index);
5140			else
5141				printf("):    ");
5142			if (ctl->step > 0) {
5143				printf("%+d/%+ddB (%d steps)%s\n",
5144				    MINQDB(ctl) / 4,
5145				    MAXQDB(ctl) / 4,
5146				    ctl->step + 1,
5147				    ctl->mute?" + mute":"");
5148			} else
5149				printf("%s\n", ctl->mute?"mute":"");
5150		}
5151	}
5152}
5153
5154static void
5155hdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
5156{
5157	uint32_t cap;
5158
5159	cap = fcap;
5160	if (cap != 0) {
5161		device_printf(dev, "     Stream cap: 0x%08x\n", cap);
5162		device_printf(dev, "                ");
5163		if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5164			printf(" AC3");
5165		if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5166			printf(" FLOAT32");
5167		if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5168			printf(" PCM");
5169		printf("\n");
5170	}
5171	cap = pcmcap;
5172	if (cap != 0) {
5173		device_printf(dev, "        PCM cap: 0x%08x\n", cap);
5174		device_printf(dev, "                ");
5175		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5176			printf(" 8");
5177		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5178			printf(" 16");
5179		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5180			printf(" 20");
5181		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5182			printf(" 24");
5183		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5184			printf(" 32");
5185		printf(" bits,");
5186		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5187			printf(" 8");
5188		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5189			printf(" 11");
5190		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5191			printf(" 16");
5192		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5193			printf(" 22");
5194		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5195			printf(" 32");
5196		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5197			printf(" 44");
5198		printf(" 48");
5199		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5200			printf(" 88");
5201		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5202			printf(" 96");
5203		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5204			printf(" 176");
5205		if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5206			printf(" 192");
5207		printf(" KHz\n");
5208	}
5209}
5210
5211static void
5212hdaa_dump_pin(struct hdaa_widget *w)
5213{
5214	uint32_t pincap;
5215
5216	pincap = w->wclass.pin.cap;
5217
5218	device_printf(w->devinfo->dev, "        Pin cap: 0x%08x\n", pincap);
5219	device_printf(w->devinfo->dev, "                ");
5220	if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5221		printf(" ISC");
5222	if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5223		printf(" TRQD");
5224	if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5225		printf(" PDC");
5226	if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5227		printf(" HP");
5228	if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5229		printf(" OUT");
5230	if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5231		printf(" IN");
5232	if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5233		printf(" BAL");
5234	if (HDA_PARAM_PIN_CAP_HDMI(pincap))
5235		printf(" HDMI");
5236	if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5237		printf(" VREF[");
5238		if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5239			printf(" 50");
5240		if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5241			printf(" 80");
5242		if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5243			printf(" 100");
5244		if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5245			printf(" GROUND");
5246		if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5247			printf(" HIZ");
5248		printf(" ]");
5249	}
5250	if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5251		printf(" EAPD");
5252	if (HDA_PARAM_PIN_CAP_DP(pincap))
5253		printf(" DP");
5254	if (HDA_PARAM_PIN_CAP_HBR(pincap))
5255		printf(" HBR");
5256	printf("\n");
5257	device_printf(w->devinfo->dev, "     Pin config: 0x%08x\n",
5258	    w->wclass.pin.config);
5259	device_printf(w->devinfo->dev, "    Pin control: 0x%08x", w->wclass.pin.ctrl);
5260	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5261		printf(" HP");
5262	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5263		printf(" IN");
5264	if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5265		printf(" OUT");
5266	if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
5267		if ((w->wclass.pin.ctrl &
5268		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
5269			printf(" HBR");
5270		else if ((w->wclass.pin.ctrl &
5271		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5272			printf(" EPTs");
5273	} else {
5274		if ((w->wclass.pin.ctrl &
5275		    HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5276			printf(" VREFs");
5277	}
5278	printf("\n");
5279}
5280
5281static void
5282hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf)
5283{
5284
5285	device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d "
5286	    "%-13s %-5s %-7s %-10s %-7s %d%s\n",
5287	    w->nid, conf,
5288	    HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
5289	    HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
5290	    HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
5291	    HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
5292	    HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
5293	    HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
5294	    HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
5295	    HDA_CONFIG_DEFAULTCONF_MISC(conf),
5296	    (w->enable == 0)?" DISA":"");
5297}
5298
5299static void
5300hdaa_dump_pin_configs(struct hdaa_devinfo *devinfo)
5301{
5302	struct hdaa_widget *w;
5303	int i;
5304
5305	device_printf(devinfo->dev, "nid   0x    as seq "
5306	    "device       conn  jack    loc        color   misc\n");
5307	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5308		w = hdaa_widget_get(devinfo, i);
5309		if (w == NULL)
5310			continue;
5311		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5312			continue;
5313		hdaa_dump_pin_config(w, w->wclass.pin.config);
5314	}
5315}
5316
5317static void
5318hdaa_dump_amp(device_t dev, uint32_t cap, char *banner)
5319{
5320	device_printf(dev, "     %s amp: 0x%08x\n", banner, cap);
5321	device_printf(dev, "                 "
5322	    "mute=%d step=%d size=%d offset=%d\n",
5323	    HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5324	    HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap),
5325	    HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap),
5326	    HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap));
5327}
5328
5329static void
5330hdaa_dump_nodes(struct hdaa_devinfo *devinfo)
5331{
5332	struct hdaa_widget *w, *cw;
5333	char buf[64];
5334	int i, j;
5335
5336	device_printf(devinfo->dev, "\n");
5337	device_printf(devinfo->dev, "Default Parameter\n");
5338	device_printf(devinfo->dev, "-----------------\n");
5339	hdaa_dump_audio_formats(devinfo->dev,
5340	    devinfo->supp_stream_formats,
5341	    devinfo->supp_pcm_size_rate);
5342	device_printf(devinfo->dev, "         IN amp: 0x%08x\n",
5343	    devinfo->inamp_cap);
5344	device_printf(devinfo->dev, "        OUT amp: 0x%08x\n",
5345	    devinfo->outamp_cap);
5346	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5347		w = hdaa_widget_get(devinfo, i);
5348		if (w == NULL) {
5349			device_printf(devinfo->dev, "Ghost widget nid=%d\n", i);
5350			continue;
5351		}
5352		device_printf(devinfo->dev, "\n");
5353		device_printf(devinfo->dev, "            nid: %d%s\n", w->nid,
5354		    (w->enable == 0) ? " [DISABLED]" : "");
5355		device_printf(devinfo->dev, "           Name: %s\n", w->name);
5356		device_printf(devinfo->dev, "     Widget cap: 0x%08x\n",
5357		    w->param.widget_cap);
5358		if (w->param.widget_cap & 0x0ee1) {
5359			device_printf(devinfo->dev, "                ");
5360			if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
5361			    printf(" LRSWAP");
5362			if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
5363			    printf(" PWR");
5364			if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5365			    printf(" DIGITAL");
5366			if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
5367			    printf(" UNSOL");
5368			if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
5369			    printf(" PROC");
5370			if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
5371			    printf(" STRIPE(x%d)",
5372				1 << (fls(w->wclass.conv.stripecap) - 1));
5373			j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
5374			if (j == 1)
5375			    printf(" STEREO");
5376			else if (j > 1)
5377			    printf(" %dCH", j + 1);
5378			printf("\n");
5379		}
5380		if (w->bindas != -1) {
5381			device_printf(devinfo->dev, "    Association: %d (0x%08x)\n",
5382			    w->bindas, w->bindseqmask);
5383		}
5384		if (w->ossmask != 0 || w->ossdev >= 0) {
5385			device_printf(devinfo->dev, "            OSS: %s",
5386			    hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
5387			if (w->ossdev >= 0)
5388			    printf(" (%s)", ossnames[w->ossdev]);
5389			printf("\n");
5390		}
5391		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5392		    w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5393			hdaa_dump_audio_formats(devinfo->dev,
5394			    w->param.supp_stream_formats,
5395			    w->param.supp_pcm_size_rate);
5396		} else if (w->type ==
5397		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
5398			hdaa_dump_pin(w);
5399		if (w->param.eapdbtl != HDA_INVALID)
5400			device_printf(devinfo->dev, "           EAPD: 0x%08x\n",
5401			    w->param.eapdbtl);
5402		if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5403		    w->param.outamp_cap != 0)
5404			hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output");
5405		if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5406		    w->param.inamp_cap != 0)
5407			hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input");
5408		if (w->nconns > 0) {
5409			device_printf(devinfo->dev, "    connections: %d\n", w->nconns);
5410			device_printf(devinfo->dev, "          |\n");
5411		}
5412		for (j = 0; j < w->nconns; j++) {
5413			cw = hdaa_widget_get(devinfo, w->conns[j]);
5414			device_printf(devinfo->dev, "          + %s<- nid=%d [%s]",
5415			    (w->connsenable[j] == 0)?"[DISABLED] ":"",
5416			    w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5417			if (cw == NULL)
5418				printf(" [UNKNOWN]");
5419			else if (cw->enable == 0)
5420				printf(" [DISABLED]");
5421			if (w->nconns > 1 && w->selconn == j && w->type !=
5422			    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5423				printf(" (selected)");
5424			printf("\n");
5425		}
5426	}
5427
5428}
5429
5430static void
5431hdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth)
5432{
5433	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5434	struct hdaa_widget *w, *cw;
5435	char buf[64];
5436	int i, printed = 0;
5437
5438	if (depth > HDA_PARSE_MAXDEPTH)
5439		return;
5440
5441	w = hdaa_widget_get(devinfo, nid);
5442	if (w == NULL || w->enable == 0)
5443		return;
5444
5445	if (depth == 0)
5446		device_printf(pdevinfo->dev, "%*s", 4, "");
5447	else
5448		device_printf(pdevinfo->dev, "%*s  + <- ", 4 + (depth - 1) * 7, "");
5449	printf("nid=%d [%s]", w->nid, w->name);
5450
5451	if (depth > 0) {
5452		if (w->ossmask == 0) {
5453			printf("\n");
5454			return;
5455		}
5456		printf(" [src: %s]",
5457		    hdaa_audio_ctl_ossmixer_mask2allname(
5458			w->ossmask, buf, sizeof(buf)));
5459		if (w->ossdev >= 0) {
5460			printf("\n");
5461			return;
5462		}
5463	}
5464	printf("\n");
5465
5466	for (i = 0; i < w->nconns; i++) {
5467		if (w->connsenable[i] == 0)
5468			continue;
5469		cw = hdaa_widget_get(devinfo, w->conns[i]);
5470		if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
5471			continue;
5472		if (printed == 0) {
5473			device_printf(pdevinfo->dev, "%*s  |\n", 4 + (depth) * 7, "");
5474			printed = 1;
5475		}
5476		hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
5477	}
5478
5479}
5480
5481static void
5482hdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo)
5483{
5484	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5485	struct hdaa_audio_as *as;
5486	struct hdaa_widget *w;
5487	int i, printed = 0;
5488
5489	if (pdevinfo->playas < 0)
5490		return;
5491
5492	as = &devinfo->as[pdevinfo->playas];
5493	for (i = 0; i < 16; i++) {
5494		if (as->pins[i] <= 0)
5495			continue;
5496		w = hdaa_widget_get(devinfo, as->pins[i]);
5497		if (w == NULL || w->enable == 0)
5498			continue;
5499		if (printed == 0) {
5500			printed = 1;
5501			device_printf(pdevinfo->dev, "\n");
5502			device_printf(pdevinfo->dev, "Playback:\n");
5503		}
5504		device_printf(pdevinfo->dev, "\n");
5505		hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0);
5506	}
5507}
5508
5509static void
5510hdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo)
5511{
5512	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5513	struct hdaa_widget *w;
5514	int i;
5515	int printed = 0;
5516
5517	if (pdevinfo->recas < 0)
5518		return;
5519
5520	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5521		w = hdaa_widget_get(devinfo, i);
5522		if (w == NULL || w->enable == 0)
5523			continue;
5524		if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
5525			continue;
5526		if (w->bindas != pdevinfo->recas)
5527			continue;
5528		if (printed == 0) {
5529			printed = 1;
5530			device_printf(pdevinfo->dev, "\n");
5531			device_printf(pdevinfo->dev, "Record:\n");
5532		}
5533		device_printf(pdevinfo->dev, "\n");
5534		hdaa_dump_dst_nid(pdevinfo, i, 0);
5535	}
5536}
5537
5538static void
5539hdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo)
5540{
5541	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5542	struct hdaa_widget *w;
5543	int i;
5544	int printed = 0;
5545
5546	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5547		w = hdaa_widget_get(devinfo, i);
5548		if (w == NULL || w->enable == 0)
5549			continue;
5550		if (w->ossdev != SOUND_MIXER_IMIX)
5551			continue;
5552		if (w->bindas != pdevinfo->recas)
5553			continue;
5554		if (printed == 0) {
5555			printed = 1;
5556			device_printf(pdevinfo->dev, "\n");
5557			device_printf(pdevinfo->dev, "Input Mix:\n");
5558		}
5559		device_printf(pdevinfo->dev, "\n");
5560		hdaa_dump_dst_nid(pdevinfo, i, 0);
5561	}
5562}
5563
5564static void
5565hdaa_dump_pcmchannels(struct hdaa_pcm_devinfo *pdevinfo)
5566{
5567	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5568	nid_t *nids;
5569	int chid, i;
5570
5571	if (pdevinfo->playas >= 0) {
5572		device_printf(pdevinfo->dev, "\n");
5573		device_printf(pdevinfo->dev, "Playback:\n");
5574		device_printf(pdevinfo->dev, "\n");
5575		chid = devinfo->as[pdevinfo->playas].chans[0];
5576		hdaa_dump_audio_formats(pdevinfo->dev,
5577		    devinfo->chans[chid].supp_stream_formats,
5578		    devinfo->chans[chid].supp_pcm_size_rate);
5579		for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) {
5580			chid = devinfo->as[pdevinfo->playas].chans[i];
5581			device_printf(pdevinfo->dev, "            DAC:");
5582			for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5583				printf(" %d", *nids);
5584			printf("\n");
5585		}
5586	}
5587	if (pdevinfo->recas >= 0) {
5588		device_printf(pdevinfo->dev, "\n");
5589		device_printf(pdevinfo->dev, "Record:\n");
5590		device_printf(pdevinfo->dev, "\n");
5591		chid = devinfo->as[pdevinfo->recas].chans[0];
5592		hdaa_dump_audio_formats(pdevinfo->dev,
5593		    devinfo->chans[chid].supp_stream_formats,
5594		    devinfo->chans[chid].supp_pcm_size_rate);
5595		for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) {
5596			chid = devinfo->as[pdevinfo->recas].chans[i];
5597			device_printf(pdevinfo->dev, "            DAC:");
5598			for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5599				printf(" %d", *nids);
5600			printf("\n");
5601		}
5602	}
5603}
5604
5605static void
5606hdaa_pindump(device_t dev)
5607{
5608	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5609	struct hdaa_widget *w;
5610	uint32_t res, pincap, delay;
5611	int i;
5612
5613	device_printf(dev, "Dumping AFG pins:\n");
5614	device_printf(dev, "nid   0x    as seq "
5615	    "device       conn  jack    loc        color   misc\n");
5616	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5617		w = hdaa_widget_get(devinfo, i);
5618		if (w == NULL || w->type !=
5619		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5620			continue;
5621		hdaa_dump_pin_config(w, w->wclass.pin.config);
5622		pincap = w->wclass.pin.cap;
5623		device_printf(dev, "    Caps: %2s %3s %2s %4s %4s",
5624		    HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
5625		    HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
5626		    HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
5627		    HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
5628		    HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
5629		if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
5630		    HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
5631			if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
5632				delay = 0;
5633				hda_command(dev,
5634				    HDA_CMD_SET_PIN_SENSE(0, w->nid, 0));
5635				do {
5636					res = hda_command(dev,
5637					    HDA_CMD_GET_PIN_SENSE(0, w->nid));
5638					if (res != 0x7fffffff && res != 0xffffffff)
5639						break;
5640					DELAY(10);
5641				} while (++delay < 10000);
5642			} else {
5643				delay = 0;
5644				res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0,
5645				    w->nid));
5646			}
5647			printf(" Sense: 0x%08x (%sconnected%s)", res,
5648			    (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ?
5649			     "" : "dis",
5650			    (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) &&
5651			     (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ?
5652			      ", ELD valid" : "");
5653			if (delay > 0)
5654				printf(" delay %dus", delay * 10);
5655		}
5656		printf("\n");
5657	}
5658	device_printf(dev,
5659	    "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
5660	    HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
5661	    HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
5662	    HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
5663	    HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
5664	    HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
5665	hdaa_dump_gpi(devinfo);
5666	hdaa_dump_gpio(devinfo);
5667	hdaa_dump_gpo(devinfo);
5668}
5669
5670static void
5671hdaa_configure(device_t dev)
5672{
5673	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5674	struct hdaa_audio_ctl *ctl;
5675	int i;
5676
5677	HDA_BOOTHVERBOSE(
5678		device_printf(dev, "Applying built-in patches...\n");
5679	);
5680	hdaa_patch(devinfo);
5681	HDA_BOOTHVERBOSE(
5682		device_printf(dev, "Applying local patches...\n");
5683	);
5684	hdaa_local_patch(devinfo);
5685	hdaa_audio_postprocess(devinfo);
5686	HDA_BOOTHVERBOSE(
5687		device_printf(dev, "Parsing Ctls...\n");
5688	);
5689	hdaa_audio_ctl_parse(devinfo);
5690	HDA_BOOTHVERBOSE(
5691		device_printf(dev, "Disabling nonaudio...\n");
5692	);
5693	hdaa_audio_disable_nonaudio(devinfo);
5694	HDA_BOOTHVERBOSE(
5695		device_printf(dev, "Disabling useless...\n");
5696	);
5697	hdaa_audio_disable_useless(devinfo);
5698	HDA_BOOTVERBOSE(
5699		device_printf(dev, "Patched pins configuration:\n");
5700		hdaa_dump_pin_configs(devinfo);
5701	);
5702	HDA_BOOTHVERBOSE(
5703		device_printf(dev, "Parsing pin associations...\n");
5704	);
5705	hdaa_audio_as_parse(devinfo);
5706	HDA_BOOTHVERBOSE(
5707		device_printf(dev, "Building AFG tree...\n");
5708	);
5709	hdaa_audio_build_tree(devinfo);
5710	HDA_BOOTHVERBOSE(
5711		device_printf(dev, "Disabling unassociated "
5712		    "widgets...\n");
5713	);
5714	hdaa_audio_disable_unas(devinfo);
5715	HDA_BOOTHVERBOSE(
5716		device_printf(dev, "Disabling nonselected "
5717		    "inputs...\n");
5718	);
5719	hdaa_audio_disable_notselected(devinfo);
5720	HDA_BOOTHVERBOSE(
5721		device_printf(dev, "Disabling useless...\n");
5722	);
5723	hdaa_audio_disable_useless(devinfo);
5724	HDA_BOOTHVERBOSE(
5725		device_printf(dev, "Disabling "
5726		    "crossassociatement connections...\n");
5727	);
5728	hdaa_audio_disable_crossas(devinfo);
5729	HDA_BOOTHVERBOSE(
5730		device_printf(dev, "Disabling useless...\n");
5731	);
5732	hdaa_audio_disable_useless(devinfo);
5733	HDA_BOOTHVERBOSE(
5734		device_printf(dev, "Binding associations to channels...\n");
5735	);
5736	hdaa_audio_bind_as(devinfo);
5737	HDA_BOOTHVERBOSE(
5738		device_printf(dev, "Assigning names to signal sources...\n");
5739	);
5740	hdaa_audio_assign_names(devinfo);
5741	HDA_BOOTHVERBOSE(
5742		device_printf(dev, "Preparing PCM devices...\n");
5743	);
5744	hdaa_prepare_pcms(devinfo);
5745	HDA_BOOTHVERBOSE(
5746		device_printf(dev, "Assigning mixers to the tree...\n");
5747	);
5748	hdaa_audio_assign_mixers(devinfo);
5749	HDA_BOOTHVERBOSE(
5750		device_printf(dev, "Preparing pin controls...\n");
5751	);
5752	hdaa_audio_prepare_pin_ctrl(devinfo);
5753	HDA_BOOTHVERBOSE(
5754		device_printf(dev, "AFG commit...\n");
5755	);
5756	hdaa_audio_commit(devinfo);
5757	HDA_BOOTHVERBOSE(
5758		device_printf(dev, "Applying direct built-in patches...\n");
5759	);
5760	hdaa_patch_direct(devinfo);
5761	HDA_BOOTHVERBOSE(
5762		device_printf(dev, "ELD init...\n");
5763	);
5764	hdaa_eld_init(devinfo);
5765	HDA_BOOTHVERBOSE(
5766		device_printf(dev, "HP switch init...\n");
5767	);
5768	hdaa_hp_switch_init(devinfo);
5769	HDA_BOOTHVERBOSE(
5770		device_printf(dev, "Creating PCM devices...\n");
5771	);
5772	hdaa_create_pcms(devinfo);
5773
5774	HDA_BOOTVERBOSE(
5775		if (devinfo->quirks != 0) {
5776			device_printf(dev, "FG config/quirks:");
5777			for (i = 0; i < HDAA_QUIRKS_TAB_LEN; i++) {
5778				if ((devinfo->quirks &
5779				    hdaa_quirks_tab[i].value) ==
5780				    hdaa_quirks_tab[i].value)
5781					printf(" %s", hdaa_quirks_tab[i].key);
5782			}
5783			printf("\n");
5784		}
5785
5786		device_printf(dev, "\n");
5787		device_printf(dev, "+-------------------+\n");
5788		device_printf(dev, "| DUMPING HDA NODES |\n");
5789		device_printf(dev, "+-------------------+\n");
5790		hdaa_dump_nodes(devinfo);
5791	);
5792
5793	HDA_BOOTHVERBOSE(
5794		device_printf(dev, "\n");
5795		device_printf(dev, "+------------------------+\n");
5796		device_printf(dev, "| DUMPING HDA AMPLIFIERS |\n");
5797		device_printf(dev, "+------------------------+\n");
5798		device_printf(dev, "\n");
5799		i = 0;
5800		while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5801			device_printf(dev, "%3d: nid %3d %s (%s) index %d", i,
5802			    (ctl->widget != NULL) ? ctl->widget->nid : -1,
5803			    (ctl->ndir == HDAA_CTL_IN)?"in ":"out",
5804			    (ctl->dir == HDAA_CTL_IN)?"in ":"out",
5805			    ctl->index);
5806			if (ctl->childwidget != NULL)
5807				printf(" cnid %3d", ctl->childwidget->nid);
5808			else
5809				printf("         ");
5810			printf(" ossmask=0x%08x\n",
5811			    ctl->ossmask);
5812			device_printf(dev,
5813			    "       mute: %d step: %3d size: %3d off: %3d%s\n",
5814			    ctl->mute, ctl->step, ctl->size, ctl->offset,
5815			    (ctl->enable == 0) ? " [DISABLED]" :
5816			    ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
5817		}
5818	);
5819
5820	HDA_BOOTVERBOSE(
5821		device_printf(dev, "\n");
5822	);
5823}
5824
5825static void
5826hdaa_unconfigure(device_t dev)
5827{
5828	struct hdaa_devinfo *devinfo = device_get_softc(dev);
5829	struct hdaa_widget *w;
5830	int i, j;
5831
5832	HDA_BOOTHVERBOSE(
5833		device_printf(dev, "HP switch deinit...\n");
5834	);
5835	hdaa_hp_switch_deinit(devinfo);
5836	HDA_BOOTHVERBOSE(
5837		device_printf(dev, "ELD deinit...\n");
5838	);
5839	hdaa_eld_deinit(devinfo);
5840	free(devinfo->ctl, M_HDAA);
5841	devinfo->ctl = NULL;
5842	devinfo->ctlcnt = 0;
5843	free(devinfo->as, M_HDAA);
5844	devinfo->as = NULL;
5845	devinfo->ascnt = 0;
5846	free(devinfo->devs, M_HDAA);
5847	devinfo->devs = NULL;
5848	devinfo->num_devs = 0;
5849	free(devinfo->chans, M_HDAA);
5850	devinfo->chans = NULL;
5851	devinfo->num_chans = 0;
5852	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5853		w = hdaa_widget_get(devinfo, i);
5854		if (w == NULL)
5855			continue;
5856		w->enable = 1;
5857		w->selconn = -1;
5858		w->pflags = 0;
5859		w->bindas = -1;
5860		w->bindseqmask = 0;
5861		w->ossdev = -1;
5862		w->ossmask = 0;
5863		for (j = 0; j < w->nconns; j++)
5864			w->connsenable[j] = 1;
5865		if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5866			w->wclass.pin.config = w->wclass.pin.newconf;
5867		if (w->eld != NULL) {
5868			w->eld_len = 0;
5869			free(w->eld, M_HDAA);
5870			w->eld = NULL;
5871		}
5872	}
5873}
5874
5875static int
5876hdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS)
5877{
5878	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5879	device_t dev = devinfo->dev;
5880	char buf[256];
5881	int n = 0, i, numgpi;
5882	uint32_t data = 0;
5883
5884	buf[0] = 0;
5885	hdaa_lock(devinfo);
5886	numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap);
5887	if (numgpi > 0) {
5888		data = hda_command(dev,
5889		    HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
5890	}
5891	hdaa_unlock(devinfo);
5892	for (i = 0; i < numgpi; i++) {
5893		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
5894		    n != 0 ? " " : "", i, ((data >> i) & 1));
5895	}
5896	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5897}
5898
5899static int
5900hdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS)
5901{
5902	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5903	device_t dev = devinfo->dev;
5904	char buf[256];
5905	int n = 0, i, numgpio;
5906	uint32_t data = 0, enable = 0, dir = 0;
5907
5908	buf[0] = 0;
5909	hdaa_lock(devinfo);
5910	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5911	if (numgpio > 0) {
5912		data = hda_command(dev,
5913		    HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
5914		enable = hda_command(dev,
5915		    HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
5916		dir = hda_command(dev,
5917		    HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
5918	}
5919	hdaa_unlock(devinfo);
5920	for (i = 0; i < numgpio; i++) {
5921		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=",
5922		    n != 0 ? " " : "", i);
5923		if ((enable & (1 << i)) == 0) {
5924			n += snprintf(buf + n, sizeof(buf) - n, "disabled");
5925			continue;
5926		}
5927		n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)",
5928		    ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1));
5929	}
5930	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5931}
5932
5933static int
5934hdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS)
5935{
5936	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5937	char buf[256];
5938	int error, n = 0, i, numgpio;
5939	uint32_t gpio, x;
5940
5941	gpio = devinfo->newgpio;
5942	numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5943	buf[0] = 0;
5944	for (i = 0; i < numgpio; i++) {
5945		x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
5946		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
5947		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
5948	}
5949	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
5950	if (error != 0 || req->newptr == NULL)
5951		return (error);
5952	if (strncmp(buf, "0x", 2) == 0)
5953		gpio = strtol(buf + 2, NULL, 16);
5954	else
5955		gpio = hdaa_gpio_patch(gpio, buf);
5956	hdaa_lock(devinfo);
5957	devinfo->newgpio = devinfo->gpio = gpio;
5958	hdaa_gpio_commit(devinfo);
5959	hdaa_unlock(devinfo);
5960	return (0);
5961}
5962
5963static int
5964hdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS)
5965{
5966	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5967	device_t dev = devinfo->dev;
5968	char buf[256];
5969	int n = 0, i, numgpo;
5970	uint32_t data = 0;
5971
5972	buf[0] = 0;
5973	hdaa_lock(devinfo);
5974	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
5975	if (numgpo > 0) {
5976		data = hda_command(dev,
5977		    HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
5978	}
5979	hdaa_unlock(devinfo);
5980	for (i = 0; i < numgpo; i++) {
5981		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
5982		    n != 0 ? " " : "", i, ((data >> i) & 1));
5983	}
5984	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
5985}
5986
5987static int
5988hdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS)
5989{
5990	struct hdaa_devinfo *devinfo = oidp->oid_arg1;
5991	char buf[256];
5992	int error, n = 0, i, numgpo;
5993	uint32_t gpo, x;
5994
5995	gpo = devinfo->newgpo;
5996	numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
5997	buf[0] = 0;
5998	for (i = 0; i < numgpo; i++) {
5999		x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6000		n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6001		    n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6002	}
6003	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6004	if (error != 0 || req->newptr == NULL)
6005		return (error);
6006	if (strncmp(buf, "0x", 2) == 0)
6007		gpo = strtol(buf + 2, NULL, 16);
6008	else
6009		gpo = hdaa_gpio_patch(gpo, buf);
6010	hdaa_lock(devinfo);
6011	devinfo->newgpo = devinfo->gpo = gpo;
6012	hdaa_gpo_commit(devinfo);
6013	hdaa_unlock(devinfo);
6014	return (0);
6015}
6016
6017static int
6018hdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS)
6019{
6020	device_t dev;
6021	struct hdaa_devinfo *devinfo;
6022	int error, val;
6023
6024	dev = oidp->oid_arg1;
6025	devinfo = device_get_softc(dev);
6026	if (devinfo == NULL)
6027		return (EINVAL);
6028	val = 0;
6029	error = sysctl_handle_int(oidp, &val, 0, req);
6030	if (error != 0 || req->newptr == NULL || val == 0)
6031		return (error);
6032
6033	HDA_BOOTHVERBOSE(
6034		device_printf(dev, "Reconfiguration...\n");
6035	);
6036	if ((error = device_delete_children(dev)) != 0)
6037		return (error);
6038	hdaa_lock(devinfo);
6039	hdaa_unconfigure(dev);
6040	hdaa_configure(dev);
6041	hdaa_unlock(devinfo);
6042	bus_generic_attach(dev);
6043	HDA_BOOTHVERBOSE(
6044		device_printf(dev, "Reconfiguration done\n");
6045	);
6046	return (0);
6047}
6048
6049static int
6050hdaa_suspend(device_t dev)
6051{
6052	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6053	int i;
6054
6055	HDA_BOOTHVERBOSE(
6056		device_printf(dev, "Suspend...\n");
6057	);
6058	hdaa_lock(devinfo);
6059	HDA_BOOTHVERBOSE(
6060		device_printf(dev, "Stop streams...\n");
6061	);
6062	for (i = 0; i < devinfo->num_chans; i++) {
6063		if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) {
6064			devinfo->chans[i].flags |= HDAA_CHN_SUSPEND;
6065			hdaa_channel_stop(&devinfo->chans[i]);
6066		}
6067	}
6068	HDA_BOOTHVERBOSE(
6069		device_printf(dev, "HP switch deinit...\n");
6070	);
6071	hdaa_hp_switch_deinit(devinfo);
6072	HDA_BOOTHVERBOSE(
6073		device_printf(dev, "Power down FG"
6074		    " nid=%d to the D3 state...\n",
6075		    devinfo->nid);
6076	);
6077	hda_command(devinfo->dev,
6078	    HDA_CMD_SET_POWER_STATE(0,
6079	    devinfo->nid, HDA_CMD_POWER_STATE_D3));
6080	callout_stop(&devinfo->poll_jack);
6081	hdaa_unlock(devinfo);
6082	callout_drain(&devinfo->poll_jack);
6083	HDA_BOOTHVERBOSE(
6084		device_printf(dev, "Suspend done\n");
6085	);
6086	return (0);
6087}
6088
6089static int
6090hdaa_resume(device_t dev)
6091{
6092	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6093	int i;
6094
6095	HDA_BOOTHVERBOSE(
6096		device_printf(dev, "Resume...\n");
6097	);
6098	hdaa_lock(devinfo);
6099	HDA_BOOTHVERBOSE(
6100		device_printf(dev, "Power up audio FG nid=%d...\n",
6101		    devinfo->nid);
6102	);
6103	hdaa_powerup(devinfo);
6104	HDA_BOOTHVERBOSE(
6105		device_printf(dev, "AFG commit...\n");
6106	);
6107	hdaa_audio_commit(devinfo);
6108	HDA_BOOTHVERBOSE(
6109		device_printf(dev, "Applying direct built-in patches...\n");
6110	);
6111	hdaa_patch_direct(devinfo);
6112	HDA_BOOTHVERBOSE(
6113		device_printf(dev, "HP switch init...\n");
6114	);
6115	hdaa_hp_switch_init(devinfo);
6116
6117	hdaa_unlock(devinfo);
6118	for (i = 0; i < devinfo->num_devs; i++) {
6119		struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
6120		HDA_BOOTHVERBOSE(
6121			device_printf(pdevinfo->dev,
6122			    "OSS mixer reinitialization...\n");
6123		);
6124		if (mixer_reinit(pdevinfo->dev) == -1)
6125			device_printf(pdevinfo->dev,
6126			    "unable to reinitialize the mixer\n");
6127	}
6128	hdaa_lock(devinfo);
6129	HDA_BOOTHVERBOSE(
6130		device_printf(dev, "Start streams...\n");
6131	);
6132	for (i = 0; i < devinfo->num_chans; i++) {
6133		if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) {
6134			devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND;
6135			hdaa_channel_start(&devinfo->chans[i]);
6136		}
6137	}
6138	hdaa_unlock(devinfo);
6139	HDA_BOOTHVERBOSE(
6140		device_printf(dev, "Resume done\n");
6141	);
6142	return (0);
6143}
6144
6145static int
6146hdaa_probe(device_t dev)
6147{
6148	char buf[128];
6149
6150	if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
6151		return (ENXIO);
6152	snprintf(buf, sizeof(buf), "%s Audio Function Group",
6153	    device_get_desc(device_get_parent(dev)));
6154	device_set_desc_copy(dev, buf);
6155	return (BUS_PROBE_DEFAULT);
6156}
6157
6158static int
6159hdaa_attach(device_t dev)
6160{
6161	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6162	uint32_t res;
6163	nid_t nid = hda_get_node_id(dev);
6164
6165	devinfo->dev = dev;
6166	devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
6167	devinfo->nid = nid;
6168	devinfo->newquirks = -1;
6169	devinfo->newgpio = -1;
6170	devinfo->newgpo = -1;
6171	callout_init(&devinfo->poll_jack, CALLOUT_MPSAFE);
6172	devinfo->poll_ival = hz;
6173
6174	hdaa_lock(devinfo);
6175	res = hda_command(dev,
6176	    HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT));
6177	hdaa_unlock(devinfo);
6178
6179	devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
6180	devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
6181	devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
6182
6183	HDA_BOOTVERBOSE(
6184		device_printf(dev,
6185		    "Audio Function Group at nid=%d: %d subnodes %d-%d\n",
6186		    nid, devinfo->nodecnt,
6187		    devinfo->startnode, devinfo->endnode - 1);
6188	);
6189
6190	if (devinfo->nodecnt > 0)
6191		devinfo->widget = (struct hdaa_widget *)malloc(
6192		    sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA,
6193		    M_WAITOK | M_ZERO);
6194	else
6195		devinfo->widget = NULL;
6196
6197	hdaa_lock(devinfo);
6198	HDA_BOOTHVERBOSE(
6199		device_printf(dev, "Powering up...\n");
6200	);
6201	hdaa_powerup(devinfo);
6202	HDA_BOOTHVERBOSE(
6203		device_printf(dev, "Parsing audio FG...\n");
6204	);
6205	hdaa_audio_parse(devinfo);
6206	HDA_BOOTVERBOSE(
6207		device_printf(dev, "Original pins configuration:\n");
6208		hdaa_dump_pin_configs(devinfo);
6209	);
6210	hdaa_configure(dev);
6211	hdaa_unlock(devinfo);
6212
6213	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6214	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6215	    "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6216	    &devinfo->newquirks, sizeof(&devinfo->newquirks),
6217	    hdaa_sysctl_quirks, "A", "Configuration options");
6218	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6219	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6220	    "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6221	    devinfo, sizeof(devinfo),
6222	    hdaa_sysctl_gpi_state, "A", "GPI state");
6223	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6224	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6225	    "gpio_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6226	    devinfo, sizeof(devinfo),
6227	    hdaa_sysctl_gpio_state, "A", "GPIO state");
6228	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6229	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6230	    "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6231	    devinfo, sizeof(devinfo),
6232	    hdaa_sysctl_gpio_config, "A", "GPIO configuration");
6233	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6234	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6235	    "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6236	    devinfo, sizeof(devinfo),
6237	    hdaa_sysctl_gpo_state, "A", "GPO state");
6238	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6239	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6240	    "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6241	    devinfo, sizeof(devinfo),
6242	    hdaa_sysctl_gpo_config, "A", "GPO configuration");
6243	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6244	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6245	    "reconfig", CTLTYPE_INT | CTLFLAG_RW,
6246	    dev, sizeof(dev),
6247	    hdaa_sysctl_reconfig, "I", "Reprocess configuration");
6248	bus_generic_attach(dev);
6249	return (0);
6250}
6251
6252static int
6253hdaa_detach(device_t dev)
6254{
6255	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6256	int error;
6257
6258	if ((error = device_delete_children(dev)) != 0)
6259		return (error);
6260
6261	hdaa_lock(devinfo);
6262	hdaa_unconfigure(dev);
6263	devinfo->poll_ival = 0;
6264	callout_stop(&devinfo->poll_jack);
6265	hdaa_unlock(devinfo);
6266	callout_drain(&devinfo->poll_jack);
6267
6268	free(devinfo->widget, M_HDAA);
6269	return (0);
6270}
6271
6272static int
6273hdaa_print_child(device_t dev, device_t child)
6274{
6275	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6276	struct hdaa_pcm_devinfo *pdevinfo =
6277	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6278	struct hdaa_audio_as *as;
6279	int retval, first = 1, i;
6280
6281	retval = bus_print_child_header(dev, child);
6282	retval += printf(" at nid ");
6283	if (pdevinfo->playas >= 0) {
6284		as = &devinfo->as[pdevinfo->playas];
6285		for (i = 0; i < 16; i++) {
6286			if (as->pins[i] <= 0)
6287				continue;
6288			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6289			first = 0;
6290		}
6291	}
6292	if (pdevinfo->recas >= 0) {
6293		if (pdevinfo->playas >= 0) {
6294			retval += printf(" and ");
6295			first = 1;
6296		}
6297		as = &devinfo->as[pdevinfo->recas];
6298		for (i = 0; i < 16; i++) {
6299			if (as->pins[i] <= 0)
6300				continue;
6301			retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6302			first = 0;
6303		}
6304	}
6305	retval += bus_print_child_footer(dev, child);
6306
6307	return (retval);
6308}
6309
6310static int
6311hdaa_child_location_str(device_t dev, device_t child, char *buf,
6312    size_t buflen)
6313{
6314	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6315	struct hdaa_pcm_devinfo *pdevinfo =
6316	    (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6317	struct hdaa_audio_as *as;
6318	int first = 1, i, len = 0;
6319
6320	len += snprintf(buf + len, buflen - len, "nid=");
6321	if (pdevinfo->playas >= 0) {
6322		as = &devinfo->as[pdevinfo->playas];
6323		for (i = 0; i < 16; i++) {
6324			if (as->pins[i] <= 0)
6325				continue;
6326			len += snprintf(buf + len, buflen - len,
6327			    "%s%d", first ? "" : ",", as->pins[i]);
6328			first = 0;
6329		}
6330	}
6331	if (pdevinfo->recas >= 0) {
6332		as = &devinfo->as[pdevinfo->recas];
6333		for (i = 0; i < 16; i++) {
6334			if (as->pins[i] <= 0)
6335				continue;
6336			len += snprintf(buf + len, buflen - len,
6337			    "%s%d", first ? "" : ",", as->pins[i]);
6338			first = 0;
6339		}
6340	}
6341	return (0);
6342}
6343
6344static void
6345hdaa_stream_intr(device_t dev, int dir, int stream)
6346{
6347	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6348	struct hdaa_chan *ch;
6349	int i;
6350
6351	for (i = 0; i < devinfo->num_chans; i++) {
6352		ch = &devinfo->chans[i];
6353		if (!(ch->flags & HDAA_CHN_RUNNING))
6354			continue;
6355		if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) &&
6356		    ch->sid == stream) {
6357			hdaa_unlock(devinfo);
6358			chn_intr(ch->c);
6359			hdaa_lock(devinfo);
6360		}
6361	}
6362}
6363
6364static void
6365hdaa_unsol_intr(device_t dev, uint32_t resp)
6366{
6367	struct hdaa_devinfo *devinfo = device_get_softc(dev);
6368	struct hdaa_widget *w;
6369	int i, tag, flags;
6370
6371	HDA_BOOTHVERBOSE(
6372		device_printf(dev, "Unsolicited response %08x\n", resp);
6373	);
6374	tag = resp >> 26;
6375	for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6376		w = hdaa_widget_get(devinfo, i);
6377		if (w == NULL || w->enable == 0 || w->type !=
6378		    HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6379			continue;
6380		if (w->unsol != tag)
6381			continue;
6382		if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
6383		    HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
6384			flags = resp & 0x03;
6385		else
6386			flags = 0x01;
6387		if (flags & 0x01)
6388			hdaa_hp_switch_handler(w);
6389		if (flags & 0x02)
6390			hdaa_eld_handler(w);
6391	}
6392}
6393
6394static device_method_t hdaa_methods[] = {
6395	/* device interface */
6396	DEVMETHOD(device_probe,		hdaa_probe),
6397	DEVMETHOD(device_attach,	hdaa_attach),
6398	DEVMETHOD(device_detach,	hdaa_detach),
6399	DEVMETHOD(device_suspend,	hdaa_suspend),
6400	DEVMETHOD(device_resume,	hdaa_resume),
6401	/* Bus interface */
6402	DEVMETHOD(bus_print_child,	hdaa_print_child),
6403	DEVMETHOD(bus_child_location_str, hdaa_child_location_str),
6404	DEVMETHOD(hdac_stream_intr,	hdaa_stream_intr),
6405	DEVMETHOD(hdac_unsol_intr,	hdaa_unsol_intr),
6406	DEVMETHOD(hdac_pindump,		hdaa_pindump),
6407	{ 0, 0 }
6408};
6409
6410static driver_t hdaa_driver = {
6411	"hdaa",
6412	hdaa_methods,
6413	sizeof(struct hdaa_devinfo),
6414};
6415
6416static devclass_t hdaa_devclass;
6417
6418DRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, 0, 0);
6419
6420static void
6421hdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid,
6422    char *buf, int buflen)
6423{
6424	struct hdaa_audio_as *as;
6425	int c;
6426
6427	as = &devinfo->as[asid];
6428	c = devinfo->chans[as->chans[0]].channels;
6429	if (c == 1)
6430		snprintf(buf, buflen, "mono");
6431	else if (c == 2)
6432		buf[0] = 0;
6433	else if (as->pinset == 0x0003)
6434		snprintf(buf, buflen, "3.1");
6435	else if (as->pinset == 0x0005 || as->pinset == 0x0011)
6436		snprintf(buf, buflen, "4.0");
6437	else if (as->pinset == 0x0007 || as->pinset == 0x0013)
6438		snprintf(buf, buflen, "5.1");
6439	else if (as->pinset == 0x0017)
6440		snprintf(buf, buflen, "7.1");
6441	else
6442		snprintf(buf, buflen, "%dch", c);
6443}
6444
6445static int
6446hdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS)
6447{
6448	struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1;
6449	struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
6450	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6451	struct hdaa_chan *ch;
6452	int error, val, i;
6453	uint32_t pcmcap;
6454
6455	ch = &devinfo->chans[as->chans[0]];
6456	val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 :
6457	    ((ch->bit32 == 2) ? 20 : 0));
6458	error = sysctl_handle_int(oidp, &val, 0, req);
6459	if (error != 0 || req->newptr == NULL)
6460		return (error);
6461	pcmcap = ch->supp_pcm_size_rate;
6462	if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6463		ch->bit32 = 4;
6464	else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6465		ch->bit32 = 3;
6466	else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6467		ch->bit32 = 2;
6468	else
6469		return (EINVAL);
6470	for (i = 1; i < as->num_chans; i++)
6471		devinfo->chans[as->chans[i]].bit32 = ch->bit32;
6472	return (0);
6473}
6474
6475static int
6476hdaa_pcm_probe(device_t dev)
6477{
6478	struct hdaa_pcm_devinfo *pdevinfo =
6479	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6480	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6481	char chans1[8], chans2[8];
6482	char buf[128];
6483	int loc1, loc2;
6484
6485	if (pdevinfo->playas >= 0)
6486		loc1 = devinfo->as[pdevinfo->playas].location;
6487	else
6488		loc1 = devinfo->as[pdevinfo->recas].location;
6489	if (pdevinfo->recas >= 0)
6490		loc2 = devinfo->as[pdevinfo->recas].location;
6491	else
6492		loc2 = loc1;
6493	if (loc1 != loc2)
6494		loc1 = -2;
6495	if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0')
6496		loc1 = -2;
6497	chans1[0] = 0;
6498	chans2[0] = 0;
6499	if (pdevinfo->playas >= 0)
6500		hdaa_chan_formula(devinfo, pdevinfo->playas,
6501		    chans1, sizeof(chans1));
6502	if (pdevinfo->recas >= 0)
6503		hdaa_chan_formula(devinfo, pdevinfo->recas,
6504		    chans2, sizeof(chans2));
6505	if (chans1[0] != 0 || chans2[0] != 0) {
6506		if (chans1[0] == 0 && pdevinfo->playas >= 0)
6507			snprintf(chans1, sizeof(chans1), "2.0");
6508		else if (chans2[0] == 0 && pdevinfo->recas >= 0)
6509			snprintf(chans2, sizeof(chans2), "2.0");
6510		if (strcmp(chans1, chans2) == 0)
6511			chans2[0] = 0;
6512	}
6513	snprintf(buf, sizeof(buf), "%s PCM (%s%s%s%s%s%s%s)",
6514	    device_get_desc(device_get_parent(device_get_parent(dev))),
6515	    loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "",
6516	    (pdevinfo->digital == 0x7)?"HDMI/DP":
6517	    ((pdevinfo->digital == 0x5)?"DisplayPort":
6518	    ((pdevinfo->digital == 0x3)?"HDMI":
6519	    ((pdevinfo->digital)?"Digital":"Analog"))),
6520	    chans1[0] ? " " : "", chans1,
6521	    chans2[0] ? "/" : "", chans2);
6522	device_set_desc_copy(dev, buf);
6523	return (BUS_PROBE_SPECIFIC);
6524}
6525
6526static int
6527hdaa_pcm_attach(device_t dev)
6528{
6529	struct hdaa_pcm_devinfo *pdevinfo =
6530	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6531	struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6532	struct hdaa_audio_as *as;
6533	struct snddev_info *d;
6534	char status[SND_STATUSLEN];
6535	int i;
6536
6537	pdevinfo->chan_size = pcm_getbuffersize(dev,
6538	    HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
6539
6540	HDA_BOOTVERBOSE(
6541		device_printf(dev, "+--------------------------------------+\n");
6542		device_printf(dev, "| DUMPING PCM Playback/Record Channels |\n");
6543		device_printf(dev, "+--------------------------------------+\n");
6544		hdaa_dump_pcmchannels(pdevinfo);
6545		device_printf(dev, "\n");
6546		device_printf(dev, "+-------------------------------+\n");
6547		device_printf(dev, "| DUMPING Playback/Record Paths |\n");
6548		device_printf(dev, "+-------------------------------+\n");
6549		hdaa_dump_dac(pdevinfo);
6550		hdaa_dump_adc(pdevinfo);
6551		hdaa_dump_mix(pdevinfo);
6552		device_printf(dev, "\n");
6553		device_printf(dev, "+-------------------------+\n");
6554		device_printf(dev, "| DUMPING Volume Controls |\n");
6555		device_printf(dev, "+-------------------------+\n");
6556		hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
6557		hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
6558		hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
6559		hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
6560		hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
6561		hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
6562		hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
6563		hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
6564		hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
6565		hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
6566		hdaa_dump_ctls(pdevinfo, NULL, 0);
6567		device_printf(dev, "\n");
6568	);
6569
6570	if (resource_int_value(device_get_name(dev),
6571	    device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
6572		i &= HDA_BLK_ALIGN;
6573		if (i < HDA_BLK_MIN)
6574			i = HDA_BLK_MIN;
6575		pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
6576		i = 0;
6577		while (pdevinfo->chan_blkcnt >> i)
6578			i++;
6579		pdevinfo->chan_blkcnt = 1 << (i - 1);
6580		if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
6581			pdevinfo->chan_blkcnt = HDA_BDL_MIN;
6582		else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
6583			pdevinfo->chan_blkcnt = HDA_BDL_MAX;
6584	} else
6585		pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
6586
6587	/*
6588	 * We don't register interrupt handler with snd_setup_intr
6589	 * in pcm device. Mark pcm device as MPSAFE manually.
6590	 */
6591	pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
6592
6593	HDA_BOOTHVERBOSE(
6594		device_printf(dev, "OSS mixer initialization...\n");
6595	);
6596	if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0)
6597		device_printf(dev, "Can't register mixer\n");
6598	else
6599		hdaa_audio_ctl_set_defaults(pdevinfo);
6600
6601	HDA_BOOTHVERBOSE(
6602		device_printf(dev, "Registering PCM channels...\n");
6603	);
6604	if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0,
6605	    (pdevinfo->recas >= 0)?1:0) != 0)
6606		device_printf(dev, "Can't register PCM\n");
6607
6608	pdevinfo->registered++;
6609
6610	d = device_get_softc(dev);
6611	if (pdevinfo->playas >= 0) {
6612		as = &devinfo->as[pdevinfo->playas];
6613		for (i = 0; i < as->num_chans; i++)
6614			pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class,
6615			    &devinfo->chans[as->chans[i]]);
6616		SYSCTL_ADD_PROC(&d->play_sysctl_ctx,
6617		    SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO,
6618		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6619		    as, sizeof(as), hdaa_sysctl_32bit, "I",
6620		    "Resolution of 32bit samples (20/24/32bit)");
6621	}
6622	if (pdevinfo->recas >= 0) {
6623		as = &devinfo->as[pdevinfo->recas];
6624		for (i = 0; i < as->num_chans; i++)
6625			pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class,
6626			    &devinfo->chans[as->chans[i]]);
6627		SYSCTL_ADD_PROC(&d->rec_sysctl_ctx,
6628		    SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
6629		    "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6630		    as, sizeof(as), hdaa_sysctl_32bit, "I",
6631		    "Resolution of 32bit samples (20/24/32bit)");
6632	}
6633
6634	snprintf(status, SND_STATUSLEN, "on %s %s",
6635	    device_get_nameunit(device_get_parent(dev)),
6636	    PCM_KLDSTRING(snd_hda));
6637	pcm_setstatus(dev, status);
6638
6639	return (0);
6640}
6641
6642static int
6643hdaa_pcm_detach(device_t dev)
6644{
6645	struct hdaa_pcm_devinfo *pdevinfo =
6646	    (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6647	int err;
6648
6649	if (pdevinfo->registered > 0) {
6650		err = pcm_unregister(dev);
6651		if (err != 0)
6652			return (err);
6653	}
6654
6655	return (0);
6656}
6657
6658static device_method_t hdaa_pcm_methods[] = {
6659	/* device interface */
6660	DEVMETHOD(device_probe,		hdaa_pcm_probe),
6661	DEVMETHOD(device_attach,	hdaa_pcm_attach),
6662	DEVMETHOD(device_detach,	hdaa_pcm_detach),
6663	{ 0, 0 }
6664};
6665
6666static driver_t hdaa_pcm_driver = {
6667	"pcm",
6668	hdaa_pcm_methods,
6669	PCM_SOFTC_SIZE,
6670};
6671
6672DRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, 0, 0);
6673MODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
6674MODULE_VERSION(snd_hda, 1);
6675