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