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