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