The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/sound/pci/hda/hdaa.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 9a94558810d14f96e57f8c6113490c7e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.