hdaa.c revision 230488
1230130Smav/*- 2230130Smav * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca> 3230130Smav * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org> 4230130Smav * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org> 5230130Smav * All rights reserved. 6230130Smav * 7230130Smav * Redistribution and use in source and binary forms, with or without 8230130Smav * modification, are permitted provided that the following conditions 9230130Smav * are met: 10230130Smav * 1. Redistributions of source code must retain the above copyright 11230130Smav * notice, this list of conditions and the following disclaimer. 12230130Smav * 2. Redistributions in binary form must reproduce the above copyright 13230130Smav * notice, this list of conditions and the following disclaimer in the 14230130Smav * documentation and/or other materials provided with the distribution. 15230130Smav * 16230130Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17230130Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18230130Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19230130Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20230130Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21230130Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22230130Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23230130Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24230130Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25230130Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26230130Smav * SUCH DAMAGE. 27230130Smav */ 28230130Smav 29230130Smav/* 30230130Smav * Intel High Definition Audio (Audio function) driver for FreeBSD. 31230130Smav */ 32230130Smav 33230130Smav#ifdef HAVE_KERNEL_OPTION_HEADERS 34230130Smav#include "opt_snd.h" 35230130Smav#endif 36230130Smav 37230130Smav#include <dev/sound/pcm/sound.h> 38230130Smav 39230130Smav#include <sys/ctype.h> 40230130Smav#include <sys/taskqueue.h> 41230130Smav 42230130Smav#include <dev/sound/pci/hda/hdac.h> 43230130Smav#include <dev/sound/pci/hda/hdaa.h> 44230130Smav#include <dev/sound/pci/hda/hda_reg.h> 45230130Smav 46230130Smav#include "mixer_if.h" 47230130Smav 48230130SmavSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/hda/hdaa.c 230488 2012-01-23 17:05:11Z mav $"); 49230130Smav 50230130Smav#define hdaa_lock(devinfo) snd_mtxlock((devinfo)->lock) 51230130Smav#define hdaa_unlock(devinfo) snd_mtxunlock((devinfo)->lock) 52230130Smav#define hdaa_lockassert(devinfo) snd_mtxassert((devinfo)->lock) 53230130Smav#define hdaa_lockowned(devinfo) mtx_owned((devinfo)->lock) 54230130Smav 55230130Smavstatic const struct { 56230130Smav char *key; 57230130Smav uint32_t value; 58230130Smav} hdaa_quirks_tab[] = { 59230130Smav { "softpcmvol", HDAA_QUIRK_SOFTPCMVOL }, 60230130Smav { "fixedrate", HDAA_QUIRK_FIXEDRATE }, 61230130Smav { "forcestereo", HDAA_QUIRK_FORCESTEREO }, 62230130Smav { "eapdinv", HDAA_QUIRK_EAPDINV }, 63230130Smav { "senseinv", HDAA_QUIRK_SENSEINV }, 64230130Smav { "ivref50", HDAA_QUIRK_IVREF50 }, 65230130Smav { "ivref80", HDAA_QUIRK_IVREF80 }, 66230130Smav { "ivref100", HDAA_QUIRK_IVREF100 }, 67230130Smav { "ovref50", HDAA_QUIRK_OVREF50 }, 68230130Smav { "ovref80", HDAA_QUIRK_OVREF80 }, 69230130Smav { "ovref100", HDAA_QUIRK_OVREF100 }, 70230130Smav { "ivref", HDAA_QUIRK_IVREF }, 71230130Smav { "ovref", HDAA_QUIRK_OVREF }, 72230130Smav { "vref", HDAA_QUIRK_VREF }, 73230130Smav}; 74230130Smav#define HDAA_QUIRKS_TAB_LEN \ 75230130Smav (sizeof(hdaa_quirks_tab) / sizeof(hdaa_quirks_tab[0])) 76230130Smav 77230130Smav#define HDA_PARSE_MAXDEPTH 10 78230130Smav 79230130SmavMALLOC_DEFINE(M_HDAA, "hdaa", "HDA Audio"); 80230130Smav 81230130Smavconst char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue", "Green", "Red", 82230130Smav "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B", "Res.C", "Res.D", 83230130Smav "White", "Other"}; 84230130Smav 85230130Smavconst char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD", 86230130Smav "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in", 87230130Smav "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"}; 88230130Smav 89230130Smavconst char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"}; 90230130Smav 91230130Smavconst char *HDA_CONNECTORS[16] = { 92230130Smav "Unknown", "1/8", "1/4", "ATAPI", "RCA", "Optical", "Digital", "Analog", 93230130Smav "DIN", "XLR", "RJ-11", "Combo", "0xc", "0xd", "0xe", "Other" }; 94230130Smav 95230130Smavconst char *HDA_LOCS[64] = { 96230130Smav "0x00", "Rear", "Front", "Left", "Right", "Top", "Bottom", "Rear-panel", 97230130Smav "Drive-bay", "0x09", "0x0a", "0x0b", "0x0c", "0x0d", "0x0e", "0x0f", 98230130Smav "Internal", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "Riser", 99230130Smav "0x18", "Onboard", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f", 100230130Smav "External", "Ext-Rear", "Ext-Front", "Ext-Left", "Ext-Right", "Ext-Top", "Ext-Bottom", "0x07", 101230130Smav "0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f", 102230130Smav "Other", "0x31", "0x32", "0x33", "0x34", "0x35", "Other-Bott", "Lid-In", 103230130Smav "Lid-Out", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f" }; 104230130Smav 105230130Smavconst char *HDA_GPIO_ACTIONS[8] = { 106230130Smav "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"}; 107230130Smav 108230312Smavconst char *HDA_HDMI_CODING_TYPES[18] = { 109230312Smav "undefined", "LPCM", "AC-3", "MPEG1", "MP3", "MPEG2", "AAC-LC", "DTS", 110230312Smav "ATRAC", "DSD", "E-AC-3", "DTS-HD", "MLP", "DST", "WMAPro", "HE-AAC", 111230312Smav "HE-AACv2", "MPEG-Surround" 112230312Smav}; 113230312Smav 114230130Smav/* Default */ 115230130Smavstatic uint32_t hdaa_fmt[] = { 116230130Smav SND_FORMAT(AFMT_S16_LE, 2, 0), 117230130Smav 0 118230130Smav}; 119230130Smav 120230130Smavstatic struct pcmchan_caps hdaa_caps = {48000, 48000, hdaa_fmt, 0}; 121230130Smav 122230130Smavstatic const struct { 123230130Smav uint32_t rate; 124230130Smav int valid; 125230130Smav uint16_t base; 126230130Smav uint16_t mul; 127230130Smav uint16_t div; 128230130Smav} hda_rate_tab[] = { 129230130Smav { 8000, 1, 0x0000, 0x0000, 0x0500 }, /* (48000 * 1) / 6 */ 130230130Smav { 9600, 0, 0x0000, 0x0000, 0x0400 }, /* (48000 * 1) / 5 */ 131230130Smav { 12000, 0, 0x0000, 0x0000, 0x0300 }, /* (48000 * 1) / 4 */ 132230130Smav { 16000, 1, 0x0000, 0x0000, 0x0200 }, /* (48000 * 1) / 3 */ 133230130Smav { 18000, 0, 0x0000, 0x1000, 0x0700 }, /* (48000 * 3) / 8 */ 134230130Smav { 19200, 0, 0x0000, 0x0800, 0x0400 }, /* (48000 * 2) / 5 */ 135230130Smav { 24000, 0, 0x0000, 0x0000, 0x0100 }, /* (48000 * 1) / 2 */ 136230130Smav { 28800, 0, 0x0000, 0x1000, 0x0400 }, /* (48000 * 3) / 5 */ 137230130Smav { 32000, 1, 0x0000, 0x0800, 0x0200 }, /* (48000 * 2) / 3 */ 138230130Smav { 36000, 0, 0x0000, 0x1000, 0x0300 }, /* (48000 * 3) / 4 */ 139230130Smav { 38400, 0, 0x0000, 0x1800, 0x0400 }, /* (48000 * 4) / 5 */ 140230130Smav { 48000, 1, 0x0000, 0x0000, 0x0000 }, /* (48000 * 1) / 1 */ 141230130Smav { 64000, 0, 0x0000, 0x1800, 0x0200 }, /* (48000 * 4) / 3 */ 142230130Smav { 72000, 0, 0x0000, 0x1000, 0x0100 }, /* (48000 * 3) / 2 */ 143230130Smav { 96000, 1, 0x0000, 0x0800, 0x0000 }, /* (48000 * 2) / 1 */ 144230130Smav { 144000, 0, 0x0000, 0x1000, 0x0000 }, /* (48000 * 3) / 1 */ 145230130Smav { 192000, 1, 0x0000, 0x1800, 0x0000 }, /* (48000 * 4) / 1 */ 146230130Smav { 8820, 0, 0x4000, 0x0000, 0x0400 }, /* (44100 * 1) / 5 */ 147230130Smav { 11025, 1, 0x4000, 0x0000, 0x0300 }, /* (44100 * 1) / 4 */ 148230130Smav { 12600, 0, 0x4000, 0x0800, 0x0600 }, /* (44100 * 2) / 7 */ 149230130Smav { 14700, 0, 0x4000, 0x0000, 0x0200 }, /* (44100 * 1) / 3 */ 150230130Smav { 17640, 0, 0x4000, 0x0800, 0x0400 }, /* (44100 * 2) / 5 */ 151230130Smav { 18900, 0, 0x4000, 0x1000, 0x0600 }, /* (44100 * 3) / 7 */ 152230130Smav { 22050, 1, 0x4000, 0x0000, 0x0100 }, /* (44100 * 1) / 2 */ 153230130Smav { 25200, 0, 0x4000, 0x1800, 0x0600 }, /* (44100 * 4) / 7 */ 154230130Smav { 26460, 0, 0x4000, 0x1000, 0x0400 }, /* (44100 * 3) / 5 */ 155230130Smav { 29400, 0, 0x4000, 0x0800, 0x0200 }, /* (44100 * 2) / 3 */ 156230130Smav { 33075, 0, 0x4000, 0x1000, 0x0300 }, /* (44100 * 3) / 4 */ 157230130Smav { 35280, 0, 0x4000, 0x1800, 0x0400 }, /* (44100 * 4) / 5 */ 158230130Smav { 44100, 1, 0x4000, 0x0000, 0x0000 }, /* (44100 * 1) / 1 */ 159230130Smav { 58800, 0, 0x4000, 0x1800, 0x0200 }, /* (44100 * 4) / 3 */ 160230130Smav { 66150, 0, 0x4000, 0x1000, 0x0100 }, /* (44100 * 3) / 2 */ 161230130Smav { 88200, 1, 0x4000, 0x0800, 0x0000 }, /* (44100 * 2) / 1 */ 162230130Smav { 132300, 0, 0x4000, 0x1000, 0x0000 }, /* (44100 * 3) / 1 */ 163230130Smav { 176400, 1, 0x4000, 0x1800, 0x0000 }, /* (44100 * 4) / 1 */ 164230130Smav}; 165230130Smav#define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0])) 166230130Smav 167230451Smavconst static char *ossnames[] = SOUND_DEVICE_NAMES; 168230451Smav 169230130Smav/**************************************************************************** 170230130Smav * Function prototypes 171230130Smav ****************************************************************************/ 172230130Smavstatic int hdaa_pcmchannel_setup(struct hdaa_chan *); 173230130Smav 174230130Smavstatic void hdaa_widget_connection_select(struct hdaa_widget *, uint8_t); 175230130Smavstatic void hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *, 176230130Smav uint32_t, int, int); 177230130Smavstatic struct hdaa_audio_ctl *hdaa_audio_ctl_amp_get(struct hdaa_devinfo *, 178230130Smav nid_t, int, int, int); 179230130Smavstatic void hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *, 180230130Smav nid_t, int, int, int, int, int, int); 181230130Smav 182230130Smavstatic void hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf); 183230130Smav 184230130Smavstatic char * 185230130Smavhdaa_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len) 186230130Smav{ 187230130Smav int i, first = 1; 188230130Smav 189230130Smav bzero(buf, len); 190230130Smav for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 191230130Smav if (mask & (1 << i)) { 192230130Smav if (first == 0) 193230130Smav strlcat(buf, ", ", len); 194230451Smav strlcat(buf, ossnames[i], len); 195230130Smav first = 0; 196230130Smav } 197230130Smav } 198230130Smav return (buf); 199230130Smav} 200230130Smav 201230130Smavstatic struct hdaa_audio_ctl * 202230130Smavhdaa_audio_ctl_each(struct hdaa_devinfo *devinfo, int *index) 203230130Smav{ 204230130Smav if (devinfo == NULL || 205230130Smav index == NULL || devinfo->ctl == NULL || 206230130Smav devinfo->ctlcnt < 1 || 207230130Smav *index < 0 || *index >= devinfo->ctlcnt) 208230130Smav return (NULL); 209230130Smav return (&devinfo->ctl[(*index)++]); 210230130Smav} 211230130Smav 212230130Smavstatic struct hdaa_audio_ctl * 213230130Smavhdaa_audio_ctl_amp_get(struct hdaa_devinfo *devinfo, nid_t nid, int dir, 214230130Smav int index, int cnt) 215230130Smav{ 216230130Smav struct hdaa_audio_ctl *ctl; 217230130Smav int i, found = 0; 218230130Smav 219230130Smav if (devinfo == NULL || devinfo->ctl == NULL) 220230130Smav return (NULL); 221230130Smav 222230130Smav i = 0; 223230130Smav while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) { 224230130Smav if (ctl->enable == 0) 225230130Smav continue; 226230130Smav if (ctl->widget->nid != nid) 227230130Smav continue; 228230130Smav if (dir && ctl->ndir != dir) 229230130Smav continue; 230230130Smav if (index >= 0 && ctl->ndir == HDAA_CTL_IN && 231230130Smav ctl->dir == ctl->ndir && ctl->index != index) 232230130Smav continue; 233230130Smav found++; 234230130Smav if (found == cnt || cnt <= 0) 235230130Smav return (ctl); 236230130Smav } 237230130Smav 238230130Smav return (NULL); 239230130Smav} 240230130Smav 241230130Smav/* 242230130Smav * Jack detection (Speaker/HP redirection) event handler. 243230130Smav */ 244230130Smavstatic void 245230312Smavhdaa_hp_switch_handler(struct hdaa_widget *w) 246230130Smav{ 247230312Smav struct hdaa_devinfo *devinfo = w->devinfo; 248230130Smav struct hdaa_audio_as *as; 249230312Smav struct hdaa_widget *w1; 250230130Smav struct hdaa_audio_ctl *ctl; 251230130Smav uint32_t val, res; 252230130Smav int j; 253230130Smav 254230312Smav if (w->enable == 0 || w->type != 255230130Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 256230130Smav return; 257230130Smav 258230312Smav res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid)); 259230130Smav HDA_BOOTVERBOSE( 260230130Smav device_printf(devinfo->dev, 261230312Smav "Pin sense: nid=%d sence=0x%08x", w->nid, res); 262230130Smav ); 263230130Smav res = (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) != 0; 264230130Smav if (devinfo->quirks & HDAA_QUIRK_SENSEINV) 265230130Smav res ^= 1; 266230130Smav HDA_BOOTVERBOSE( 267230312Smav printf(" (%sconnected)\n", res == 0 ? "dis" : ""); 268230130Smav ); 269230130Smav 270230312Smav as = &devinfo->as[w->bindas]; 271230312Smav if (as->hpredir < 0 || as->pins[15] != w->nid) 272230312Smav return; 273230312Smav 274230130Smav /* (Un)Mute headphone pin. */ 275230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, 276230312Smav w->nid, HDAA_CTL_IN, -1, 1); 277230130Smav if (ctl != NULL && ctl->mute) { 278230130Smav /* If pin has muter - use it. */ 279230130Smav val = (res != 0) ? 0 : 1; 280230130Smav if (val != ctl->forcemute) { 281230130Smav ctl->forcemute = val; 282230130Smav hdaa_audio_ctl_amp_set(ctl, 283230130Smav HDAA_AMP_MUTE_DEFAULT, 284230130Smav HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT); 285230130Smav } 286230130Smav } else { 287230130Smav /* If there is no muter - disable pin output. */ 288230312Smav if (res != 0) 289230312Smav val = w->wclass.pin.ctrl | 290230312Smav HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE; 291230312Smav else 292230312Smav val = w->wclass.pin.ctrl & 293230312Smav ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE; 294230312Smav if (val != w->wclass.pin.ctrl) { 295230312Smav w->wclass.pin.ctrl = val; 296230312Smav hda_command(devinfo->dev, 297230312Smav HDA_CMD_SET_PIN_WIDGET_CTRL(0, 298230312Smav w->nid, w->wclass.pin.ctrl)); 299230130Smav } 300230130Smav } 301230130Smav /* (Un)Mute other pins. */ 302230130Smav for (j = 0; j < 15; j++) { 303230130Smav if (as->pins[j] <= 0) 304230130Smav continue; 305230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, 306230130Smav as->pins[j], HDAA_CTL_IN, -1, 1); 307230130Smav if (ctl != NULL && ctl->mute) { 308230130Smav /* If pin has muter - use it. */ 309230130Smav val = (res != 0) ? 1 : 0; 310230130Smav if (val == ctl->forcemute) 311230130Smav continue; 312230130Smav ctl->forcemute = val; 313230130Smav hdaa_audio_ctl_amp_set(ctl, 314230130Smav HDAA_AMP_MUTE_DEFAULT, 315230130Smav HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT); 316230130Smav continue; 317230130Smav } 318230130Smav /* If there is no muter - disable pin output. */ 319230312Smav w1 = hdaa_widget_get(devinfo, as->pins[j]); 320230312Smav if (w1 != NULL && w1->type == 321230130Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) { 322230130Smav if (res != 0) 323230312Smav val = w1->wclass.pin.ctrl & 324230130Smav ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE; 325230130Smav else 326230312Smav val = w1->wclass.pin.ctrl | 327230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE; 328230312Smav if (val != w1->wclass.pin.ctrl) { 329230312Smav w1->wclass.pin.ctrl = val; 330230130Smav hda_command(devinfo->dev, 331230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL(0, 332230312Smav w1->nid, w1->wclass.pin.ctrl)); 333230130Smav } 334230130Smav } 335230130Smav } 336230130Smav} 337230130Smav 338230130Smav/* 339230130Smav * Callback for poll based jack detection. 340230130Smav */ 341230130Smavstatic void 342230130Smavhdaa_jack_poll_callback(void *arg) 343230130Smav{ 344230130Smav struct hdaa_devinfo *devinfo = arg; 345230312Smav struct hdaa_widget *w; 346230130Smav int i; 347230130Smav 348230130Smav hdaa_lock(devinfo); 349230130Smav if (devinfo->poll_ival == 0) { 350230130Smav hdaa_unlock(devinfo); 351230130Smav return; 352230130Smav } 353230130Smav for (i = 0; i < devinfo->ascnt; i++) { 354230130Smav if (devinfo->as[i].hpredir < 0) 355230130Smav continue; 356230312Smav w = hdaa_widget_get(devinfo, devinfo->as[i].pins[15]); 357230312Smav if (w == NULL || w->enable == 0 || w->type != 358230312Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 359230312Smav continue; 360230312Smav hdaa_hp_switch_handler(w); 361230130Smav } 362230130Smav callout_reset(&devinfo->poll_jack, devinfo->poll_ival, 363230130Smav hdaa_jack_poll_callback, devinfo); 364230130Smav hdaa_unlock(devinfo); 365230130Smav} 366230130Smav 367230130Smav/* 368230130Smav * Jack detection initializer. 369230130Smav */ 370230130Smavstatic void 371230130Smavhdaa_hp_switch_init(struct hdaa_devinfo *devinfo) 372230130Smav{ 373230130Smav struct hdaa_audio_as *as = devinfo->as; 374230130Smav struct hdaa_widget *w; 375230130Smav int i, poll = 0; 376230130Smav 377230130Smav for (i = 0; i < devinfo->ascnt; i++) { 378230130Smav if (as[i].hpredir < 0) 379230130Smav continue; 380230130Smav 381230130Smav w = hdaa_widget_get(devinfo, as[i].pins[15]); 382230130Smav if (w == NULL || w->enable == 0 || w->type != 383230130Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 384230130Smav continue; 385230130Smav if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 || 386230130Smav (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0) { 387230130Smav device_printf(devinfo->dev, 388230130Smav "No jack detection support at pin %d\n", 389230130Smav as[i].pins[15]); 390230130Smav continue; 391230130Smav } 392230312Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap) && 393230312Smav w->unsol < 0) { 394230312Smav w->unsol = HDAC_UNSOL_ALLOC( 395230130Smav device_get_parent(devinfo->dev), devinfo->dev, 396230130Smav w->nid); 397230130Smav hda_command(devinfo->dev, 398230130Smav HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 399230130Smav HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE | 400230312Smav w->unsol)); 401230312Smav } 402230312Smav if (w->unsol < 0) 403230130Smav poll = 1; 404230130Smav HDA_BOOTVERBOSE( 405230130Smav device_printf(devinfo->dev, 406230130Smav "Headphones redirection " 407230130Smav "for as=%d nid=%d using %s.\n", 408230130Smav i, w->nid, 409230130Smav (poll != 0) ? "polling" : "unsolicited responses"); 410230130Smav ); 411230312Smav hdaa_hp_switch_handler(w); 412230130Smav } 413230130Smav if (poll) { 414230130Smav callout_reset(&devinfo->poll_jack, 1, 415230130Smav hdaa_jack_poll_callback, devinfo); 416230130Smav } 417230130Smav} 418230130Smav 419230130Smavstatic void 420230130Smavhdaa_hp_switch_deinit(struct hdaa_devinfo *devinfo) 421230130Smav{ 422230130Smav struct hdaa_audio_as *as = devinfo->as; 423230130Smav struct hdaa_widget *w; 424230130Smav int i; 425230130Smav 426230130Smav for (i = 0; i < devinfo->ascnt; i++) { 427230130Smav w = hdaa_widget_get(devinfo, as[i].pins[15]); 428230130Smav if (w == NULL || w->enable == 0 || w->type != 429230130Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 430230130Smav continue; 431230312Smav if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) || 432230312Smav HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap)) 433230312Smav continue; 434230312Smav if (w->unsol < 0) 435230312Smav continue; 436230130Smav hda_command(devinfo->dev, 437230130Smav HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0)); 438230130Smav HDAC_UNSOL_FREE( 439230130Smav device_get_parent(devinfo->dev), devinfo->dev, 440230312Smav w->unsol); 441230312Smav w->unsol = -1; 442230130Smav } 443230130Smav} 444230130Smav 445230312Smavstatic void 446230312Smavhdaa_eld_dump(struct hdaa_widget *w) 447230312Smav{ 448230312Smav struct hdaa_devinfo *devinfo = w->devinfo; 449230312Smav device_t dev = devinfo->dev; 450230312Smav uint8_t *sad; 451230312Smav int len, mnl, i, sadc, fmt; 452230312Smav 453230312Smav if (w->eld == NULL || w->eld_len < 4) 454230312Smav return; 455230312Smav device_printf(dev, 456230312Smav "ELD nid=%d: ELD_Ver=%u Baseline_ELD_Len=%u\n", 457230312Smav w->nid, w->eld[0] >> 3, w->eld[2]); 458230312Smav if ((w->eld[0] >> 3) != 0x02) 459230312Smav return; 460230312Smav len = min(w->eld_len, (u_int)w->eld[2] * 4); 461230312Smav mnl = w->eld[4] & 0x1f; 462230312Smav device_printf(dev, 463230312Smav "ELD nid=%d: CEA_EDID_Ver=%u MNL=%u\n", 464230312Smav w->nid, w->eld[4] >> 5, mnl); 465230312Smav sadc = w->eld[5] >> 4; 466230312Smav device_printf(dev, 467230312Smav "ELD nid=%d: SAD_Count=%u Conn_Type=%u S_AI=%u HDCP=%u\n", 468230312Smav w->nid, sadc, (w->eld[5] >> 2) & 0x3, 469230312Smav (w->eld[5] >> 1) & 0x1, w->eld[5] & 0x1); 470230312Smav device_printf(dev, 471230312Smav "ELD nid=%d: Aud_Synch_Delay=%ums\n", 472230312Smav w->nid, w->eld[6] * 2); 473230312Smav device_printf(dev, 474230312Smav "ELD nid=%d: Channels=0x%b\n", 475230312Smav w->nid, w->eld[7], 476230312Smav "\020\07RLRC\06FLRC\05RC\04RLR\03FC\02LFE\01FLR"); 477230312Smav device_printf(dev, 478230312Smav "ELD nid=%d: Port_ID=0x%02x%02x%02x%02x%02x%02x%02x%02x\n", 479230312Smav w->nid, w->eld[8], w->eld[9], w->eld[10], w->eld[11], 480230312Smav w->eld[12], w->eld[13], w->eld[14], w->eld[15]); 481230312Smav device_printf(dev, 482230312Smav "ELD nid=%d: Manufacturer_Name=0x%02x%02x\n", 483230312Smav w->nid, w->eld[16], w->eld[17]); 484230312Smav device_printf(dev, 485230312Smav "ELD nid=%d: Product_Code=0x%02x%02x\n", 486230312Smav w->nid, w->eld[18], w->eld[19]); 487230312Smav device_printf(dev, 488230312Smav "ELD nid=%d: Monitor_Name_String='%.*s'\n", 489230312Smav w->nid, mnl, &w->eld[20]); 490230312Smav for (i = 0; i < sadc; i++) { 491230312Smav sad = &w->eld[20 + mnl + i * 3]; 492230312Smav fmt = (sad[0] >> 3) & 0x0f; 493230312Smav if (fmt == HDA_HDMI_CODING_TYPE_REF_CTX) { 494230312Smav fmt = (sad[2] >> 3) & 0x1f; 495230312Smav if (fmt < 1 || fmt > 3) 496230312Smav fmt = 0; 497230312Smav else 498230312Smav fmt += 14; 499230312Smav } 500230312Smav device_printf(dev, 501230312Smav "ELD nid=%d: %s %dch freqs=0x%b", 502230312Smav w->nid, HDA_HDMI_CODING_TYPES[fmt], (sad[0] & 0x07) + 1, 503230312Smav sad[1], "\020\007192\006176\00596\00488\00348\00244\00132"); 504230312Smav switch (fmt) { 505230312Smav case HDA_HDMI_CODING_TYPE_LPCM: 506230312Smav printf(" sizes=0x%b", 507230312Smav sad[2] & 0x07, "\020\00324\00220\00116"); 508230312Smav break; 509230312Smav case HDA_HDMI_CODING_TYPE_AC3: 510230312Smav case HDA_HDMI_CODING_TYPE_MPEG1: 511230312Smav case HDA_HDMI_CODING_TYPE_MP3: 512230312Smav case HDA_HDMI_CODING_TYPE_MPEG2: 513230312Smav case HDA_HDMI_CODING_TYPE_AACLC: 514230312Smav case HDA_HDMI_CODING_TYPE_DTS: 515230312Smav case HDA_HDMI_CODING_TYPE_ATRAC: 516230312Smav printf(" max_bitrate=%d", sad[2] * 8000); 517230312Smav break; 518230312Smav case HDA_HDMI_CODING_TYPE_WMAPRO: 519230312Smav printf(" profile=%d", sad[2] & 0x07); 520230312Smav break; 521230312Smav } 522230312Smav printf("\n"); 523230312Smav } 524230312Smav} 525230312Smav 526230312Smavstatic void 527230312Smavhdaa_eld_handler(struct hdaa_widget *w) 528230312Smav{ 529230312Smav struct hdaa_devinfo *devinfo = w->devinfo; 530230312Smav uint32_t res; 531230312Smav int i; 532230312Smav 533230312Smav if (w->enable == 0 || w->type != 534230312Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 535230312Smav return; 536230312Smav 537230312Smav if (w->eld != NULL) { 538230312Smav w->eld_len = 0; 539230312Smav free(w->eld, M_HDAA); 540230312Smav w->eld = NULL; 541230312Smav } 542230312Smav 543230312Smav res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid)); 544230312Smav HDA_BOOTVERBOSE( 545230312Smav device_printf(devinfo->dev, 546230312Smav "Pin sense: nid=%d sence=0x%08x " 547230312Smav "(%sconnected, ELD %svalid)\n", 548230312Smav w->nid, res, 549230312Smav (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ? "" : "dis", 550230312Smav (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) ? "" : "in"); 551230312Smav ); 552230312Smav if ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) == 0) 553230312Smav return; 554230312Smav 555230312Smav res = hda_command(devinfo->dev, 556230312Smav HDA_CMD_GET_HDMI_DIP_SIZE(0, w->nid, 0x08)); 557230312Smav if (res == HDA_INVALID) 558230312Smav return; 559230312Smav w->eld_len = res & 0xff; 560230312Smav if (w->eld_len != 0) 561230312Smav w->eld = malloc(w->eld_len, M_HDAA, M_ZERO | M_NOWAIT); 562230312Smav if (w->eld == NULL) { 563230312Smav w->eld_len = 0; 564230312Smav return; 565230312Smav } 566230312Smav 567230312Smav for (i = 0; i < w->eld_len; i++) { 568230312Smav res = hda_command(devinfo->dev, 569230312Smav HDA_CMD_GET_HDMI_ELDD(0, w->nid, i)); 570230312Smav if (res & 0x80000000) 571230312Smav w->eld[i] = res & 0xff; 572230312Smav } 573230312Smav HDA_BOOTVERBOSE( 574230312Smav hdaa_eld_dump(w); 575230312Smav ); 576230312Smav} 577230312Smav 578230312Smav 579230312Smavstatic void 580230312Smavhdaa_eld_init(struct hdaa_devinfo *devinfo) 581230312Smav{ 582230312Smav struct hdaa_widget *w; 583230312Smav int i; 584230312Smav 585230312Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 586230312Smav w = hdaa_widget_get(devinfo, i); 587230312Smav if (w == NULL || w->type != 588230312Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 589230312Smav continue; 590230312Smav if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) && 591230312Smav !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap)) 592230312Smav continue; 593230312Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap) && 594230312Smav w->unsol < 0) { 595230312Smav w->unsol = HDAC_UNSOL_ALLOC( 596230312Smav device_get_parent(devinfo->dev), devinfo->dev, 597230312Smav w->nid); 598230312Smav hda_command(devinfo->dev, 599230312Smav HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 600230312Smav HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE | 601230312Smav w->unsol)); 602230312Smav } 603230312Smav hdaa_eld_handler(w); 604230312Smav } 605230312Smav} 606230312Smav 607230312Smavstatic void 608230312Smavhdaa_eld_deinit(struct hdaa_devinfo *devinfo) 609230312Smav{ 610230312Smav struct hdaa_widget *w; 611230312Smav int i; 612230312Smav 613230312Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 614230312Smav w = hdaa_widget_get(devinfo, i); 615230312Smav if (w == NULL || w->type != 616230312Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 617230312Smav continue; 618230312Smav if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) && 619230312Smav !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap)) 620230312Smav continue; 621230312Smav if (w->unsol < 0) 622230312Smav continue; 623230312Smav hda_command(devinfo->dev, 624230312Smav HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0)); 625230312Smav HDAC_UNSOL_FREE( 626230312Smav device_get_parent(devinfo->dev), devinfo->dev, 627230312Smav w->unsol); 628230312Smav w->unsol = -1; 629230312Smav } 630230312Smav} 631230312Smav 632230130Smavuint32_t 633230130Smavhdaa_widget_pin_patch(uint32_t config, const char *str) 634230130Smav{ 635230130Smav char buf[256]; 636230130Smav char *key, *value, *rest, *bad; 637230130Smav int ival, i; 638230130Smav 639230130Smav strlcpy(buf, str, sizeof(buf)); 640230130Smav rest = buf; 641230130Smav while ((key = strsep(&rest, "=")) != NULL) { 642230130Smav value = strsep(&rest, " \t"); 643230130Smav if (value == NULL) 644230130Smav break; 645230130Smav ival = strtol(value, &bad, 10); 646230130Smav if (strcmp(key, "seq") == 0) { 647230130Smav config &= ~HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK; 648230130Smav config |= ((ival << HDA_CONFIG_DEFAULTCONF_SEQUENCE_SHIFT) & 649230130Smav HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK); 650230130Smav } else if (strcmp(key, "as") == 0) { 651230130Smav config &= ~HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK; 652230130Smav config |= ((ival << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT) & 653230130Smav HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK); 654230130Smav } else if (strcmp(key, "misc") == 0) { 655230130Smav config &= ~HDA_CONFIG_DEFAULTCONF_MISC_MASK; 656230130Smav config |= ((ival << HDA_CONFIG_DEFAULTCONF_MISC_SHIFT) & 657230130Smav HDA_CONFIG_DEFAULTCONF_MISC_MASK); 658230130Smav } else if (strcmp(key, "color") == 0) { 659230130Smav config &= ~HDA_CONFIG_DEFAULTCONF_COLOR_MASK; 660230130Smav if (bad[0] == 0) { 661230130Smav config |= ((ival << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT) & 662230130Smav HDA_CONFIG_DEFAULTCONF_COLOR_MASK); 663230130Smav }; 664230130Smav for (i = 0; i < 16; i++) { 665230130Smav if (strcasecmp(HDA_COLORS[i], value) == 0) { 666230130Smav config |= (i << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT); 667230130Smav break; 668230130Smav } 669230130Smav } 670230130Smav } else if (strcmp(key, "ctype") == 0) { 671230130Smav config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK; 672230130Smav if (bad[0] == 0) { 673230130Smav config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT) & 674230130Smav HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK); 675230130Smav } 676230130Smav for (i = 0; i < 16; i++) { 677230130Smav if (strcasecmp(HDA_CONNECTORS[i], value) == 0) { 678230130Smav config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT); 679230130Smav break; 680230130Smav } 681230130Smav } 682230130Smav } else if (strcmp(key, "device") == 0) { 683230130Smav config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK; 684230130Smav if (bad[0] == 0) { 685230130Smav config |= ((ival << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT) & 686230130Smav HDA_CONFIG_DEFAULTCONF_DEVICE_MASK); 687230130Smav continue; 688230130Smav }; 689230130Smav for (i = 0; i < 16; i++) { 690230130Smav if (strcasecmp(HDA_DEVS[i], value) == 0) { 691230130Smav config |= (i << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT); 692230130Smav break; 693230130Smav } 694230130Smav } 695230130Smav } else if (strcmp(key, "loc") == 0) { 696230130Smav config &= ~HDA_CONFIG_DEFAULTCONF_LOCATION_MASK; 697230130Smav if (bad[0] == 0) { 698230130Smav config |= ((ival << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT) & 699230130Smav HDA_CONFIG_DEFAULTCONF_LOCATION_MASK); 700230130Smav continue; 701230130Smav } 702230130Smav for (i = 0; i < 64; i++) { 703230130Smav if (strcasecmp(HDA_LOCS[i], value) == 0) { 704230130Smav config |= (i << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT); 705230130Smav break; 706230130Smav } 707230130Smav } 708230130Smav } else if (strcmp(key, "conn") == 0) { 709230130Smav config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK; 710230130Smav if (bad[0] == 0) { 711230130Smav config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT) & 712230130Smav HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK); 713230130Smav continue; 714230130Smav }; 715230130Smav for (i = 0; i < 4; i++) { 716230130Smav if (strcasecmp(HDA_CONNS[i], value) == 0) { 717230130Smav config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT); 718230130Smav break; 719230130Smav } 720230130Smav } 721230130Smav } 722230130Smav } 723230130Smav return (config); 724230130Smav} 725230130Smav 726230130Smavuint32_t 727230130Smavhdaa_gpio_patch(uint32_t gpio, const char *str) 728230130Smav{ 729230130Smav char buf[256]; 730230130Smav char *key, *value, *rest; 731230130Smav int ikey, i; 732230130Smav 733230130Smav strlcpy(buf, str, sizeof(buf)); 734230130Smav rest = buf; 735230130Smav while ((key = strsep(&rest, "=")) != NULL) { 736230130Smav value = strsep(&rest, " \t"); 737230130Smav if (value == NULL) 738230130Smav break; 739230130Smav ikey = strtol(key, NULL, 10); 740230130Smav if (ikey < 0 || ikey > 7) 741230130Smav continue; 742230130Smav for (i = 0; i < 7; i++) { 743230130Smav if (strcasecmp(HDA_GPIO_ACTIONS[i], value) == 0) { 744230130Smav gpio &= ~HDAA_GPIO_MASK(ikey); 745230130Smav gpio |= i << HDAA_GPIO_SHIFT(ikey); 746230130Smav break; 747230130Smav } 748230130Smav } 749230130Smav } 750230130Smav return (gpio); 751230130Smav} 752230130Smav 753230130Smavstatic void 754230130Smavhdaa_local_patch_pin(struct hdaa_widget *w) 755230130Smav{ 756230130Smav device_t dev = w->devinfo->dev; 757230130Smav const char *res = NULL; 758230130Smav uint32_t config, orig; 759230130Smav char buf[32]; 760230130Smav 761230130Smav config = orig = w->wclass.pin.config; 762230130Smav snprintf(buf, sizeof(buf), "cad%u.nid%u.config", 763230130Smav hda_get_codec_id(dev), w->nid); 764230130Smav if (resource_string_value(device_get_name( 765230130Smav device_get_parent(device_get_parent(dev))), 766230130Smav device_get_unit(device_get_parent(device_get_parent(dev))), 767230130Smav buf, &res) == 0) { 768230130Smav if (strncmp(res, "0x", 2) == 0) { 769230130Smav config = strtol(res + 2, NULL, 16); 770230130Smav } else { 771230130Smav config = hdaa_widget_pin_patch(config, res); 772230130Smav } 773230130Smav } 774230130Smav snprintf(buf, sizeof(buf), "nid%u.config", w->nid); 775230130Smav if (resource_string_value(device_get_name(dev), device_get_unit(dev), 776230130Smav buf, &res) == 0) { 777230130Smav if (strncmp(res, "0x", 2) == 0) { 778230130Smav config = strtol(res + 2, NULL, 16); 779230130Smav } else { 780230130Smav config = hdaa_widget_pin_patch(config, res); 781230130Smav } 782230130Smav } 783230130Smav HDA_BOOTVERBOSE( 784230130Smav if (config != orig) 785230130Smav device_printf(w->devinfo->dev, 786230130Smav "Patching pin config nid=%u 0x%08x -> 0x%08x\n", 787230130Smav w->nid, orig, config); 788230130Smav ); 789230130Smav w->wclass.pin.newconf = w->wclass.pin.config = config; 790230130Smav} 791230130Smav 792230130Smavstatic int 793230130Smavhdaa_sysctl_config(SYSCTL_HANDLER_ARGS) 794230130Smav{ 795230130Smav char buf[256]; 796230130Smav int error; 797230130Smav uint32_t conf; 798230130Smav 799230130Smav conf = *(uint32_t *)oidp->oid_arg1; 800230130Smav snprintf(buf, sizeof(buf), "0x%08x as=%d seq=%d " 801230130Smav "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d", 802230130Smav conf, 803230130Smav HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf), 804230130Smav HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf), 805230130Smav HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)], 806230130Smav HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)], 807230130Smav HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)], 808230130Smav HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)], 809230130Smav HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)], 810230130Smav HDA_CONFIG_DEFAULTCONF_MISC(conf)); 811230130Smav error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 812230130Smav if (error != 0 || req->newptr == NULL) 813230130Smav return (error); 814230130Smav if (strncmp(buf, "0x", 2) == 0) 815230130Smav conf = strtol(buf + 2, NULL, 16); 816230130Smav else 817230130Smav conf = hdaa_widget_pin_patch(conf, buf); 818230130Smav *(uint32_t *)oidp->oid_arg1 = conf; 819230130Smav return (0); 820230130Smav} 821230130Smav 822230130Smavstatic void 823230130Smavhdaa_config_fetch(const char *str, uint32_t *on, uint32_t *off) 824230130Smav{ 825230130Smav int i = 0, j, k, len, inv; 826230130Smav 827230130Smav for (;;) { 828230130Smav while (str[i] != '\0' && 829230130Smav (str[i] == ',' || isspace(str[i]) != 0)) 830230130Smav i++; 831230130Smav if (str[i] == '\0') 832230130Smav return; 833230130Smav j = i; 834230130Smav while (str[j] != '\0' && 835230130Smav !(str[j] == ',' || isspace(str[j]) != 0)) 836230130Smav j++; 837230130Smav len = j - i; 838230130Smav if (len > 2 && strncmp(str + i, "no", 2) == 0) 839230130Smav inv = 2; 840230130Smav else 841230130Smav inv = 0; 842230130Smav for (k = 0; len > inv && k < HDAA_QUIRKS_TAB_LEN; k++) { 843230130Smav if (strncmp(str + i + inv, 844230130Smav hdaa_quirks_tab[k].key, len - inv) != 0) 845230130Smav continue; 846230130Smav if (len - inv != strlen(hdaa_quirks_tab[k].key)) 847230130Smav continue; 848230130Smav if (inv == 0) { 849230130Smav *on |= hdaa_quirks_tab[k].value; 850230130Smav *off &= ~hdaa_quirks_tab[k].value; 851230130Smav } else { 852230130Smav *off |= hdaa_quirks_tab[k].value; 853230130Smav *on &= ~hdaa_quirks_tab[k].value; 854230130Smav } 855230130Smav break; 856230130Smav } 857230130Smav i = j; 858230130Smav } 859230130Smav} 860230130Smav 861230130Smavstatic int 862230130Smavhdaa_sysctl_quirks(SYSCTL_HANDLER_ARGS) 863230130Smav{ 864230130Smav char buf[256]; 865230130Smav int error, n = 0, i; 866230130Smav uint32_t quirks, quirks_off; 867230130Smav 868230130Smav quirks = *(uint32_t *)oidp->oid_arg1; 869230130Smav buf[0] = 0; 870230130Smav for (i = 0; i < HDAA_QUIRKS_TAB_LEN; i++) { 871230130Smav if ((quirks & hdaa_quirks_tab[i].value) != 0) 872230130Smav n += snprintf(buf + n, sizeof(buf) - n, "%s%s", 873230130Smav n != 0 ? "," : "", hdaa_quirks_tab[i].key); 874230130Smav } 875230130Smav error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 876230130Smav if (error != 0 || req->newptr == NULL) 877230130Smav return (error); 878230130Smav if (strncmp(buf, "0x", 2) == 0) 879230130Smav quirks = strtol(buf + 2, NULL, 16); 880230130Smav else { 881230130Smav quirks = 0; 882230130Smav hdaa_config_fetch(buf, &quirks, &quirks_off); 883230130Smav } 884230130Smav *(uint32_t *)oidp->oid_arg1 = quirks; 885230130Smav return (0); 886230130Smav} 887230130Smav 888230130Smavstatic void 889230130Smavhdaa_local_patch(struct hdaa_devinfo *devinfo) 890230130Smav{ 891230130Smav struct hdaa_widget *w; 892230130Smav const char *res = NULL; 893230130Smav uint32_t quirks_on = 0, quirks_off = 0, x; 894230130Smav int i; 895230130Smav 896230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 897230130Smav w = hdaa_widget_get(devinfo, i); 898230130Smav if (w == NULL) 899230130Smav continue; 900230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 901230130Smav hdaa_local_patch_pin(w); 902230130Smav } 903230130Smav 904230130Smav if (resource_string_value(device_get_name(devinfo->dev), 905230130Smav device_get_unit(devinfo->dev), "config", &res) == 0) { 906230130Smav if (res != NULL && strlen(res) > 0) 907230130Smav hdaa_config_fetch(res, &quirks_on, &quirks_off); 908230130Smav devinfo->quirks |= quirks_on; 909230130Smav devinfo->quirks &= ~quirks_off; 910230130Smav } 911230130Smav if (devinfo->newquirks == -1) 912230130Smav devinfo->newquirks = devinfo->quirks; 913230130Smav else 914230130Smav devinfo->quirks = devinfo->newquirks; 915230130Smav HDA_BOOTHVERBOSE( 916230130Smav device_printf(devinfo->dev, 917230130Smav "Config options: 0x%08x\n", devinfo->quirks); 918230130Smav ); 919230130Smav 920230130Smav if (resource_string_value(device_get_name(devinfo->dev), 921230130Smav device_get_unit(devinfo->dev), "gpio_config", &res) == 0) { 922230130Smav if (strncmp(res, "0x", 2) == 0) { 923230130Smav devinfo->gpio = strtol(res + 2, NULL, 16); 924230130Smav } else { 925230130Smav devinfo->gpio = hdaa_gpio_patch(devinfo->gpio, res); 926230130Smav } 927230130Smav } 928230130Smav if (devinfo->newgpio == -1) 929230130Smav devinfo->newgpio = devinfo->gpio; 930230130Smav else 931230130Smav devinfo->gpio = devinfo->newgpio; 932230130Smav if (devinfo->newgpo == -1) 933230130Smav devinfo->newgpo = devinfo->gpo; 934230130Smav else 935230130Smav devinfo->gpo = devinfo->newgpo; 936230130Smav HDA_BOOTHVERBOSE( 937230130Smav device_printf(devinfo->dev, "GPIO config options:"); 938230130Smav for (i = 0; i < 7; i++) { 939230130Smav x = (devinfo->gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i); 940230130Smav if (x != 0) 941230130Smav printf(" %d=%s", i, HDA_GPIO_ACTIONS[x]); 942230130Smav } 943230130Smav printf("\n"); 944230130Smav ); 945230130Smav} 946230130Smav 947230130Smavstatic void 948230130Smavhdaa_widget_connection_parse(struct hdaa_widget *w) 949230130Smav{ 950230130Smav uint32_t res; 951230130Smav int i, j, max, ents, entnum; 952230130Smav nid_t nid = w->nid; 953230130Smav nid_t cnid, addcnid, prevcnid; 954230130Smav 955230130Smav w->nconns = 0; 956230130Smav 957230130Smav res = hda_command(w->devinfo->dev, 958230130Smav HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_CONN_LIST_LENGTH)); 959230130Smav 960230130Smav ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res); 961230130Smav 962230130Smav if (ents < 1) 963230130Smav return; 964230130Smav 965230130Smav entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4; 966230130Smav max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1; 967230130Smav prevcnid = 0; 968230130Smav 969230130Smav#define CONN_RMASK(e) (1 << ((32 / (e)) - 1)) 970230130Smav#define CONN_NMASK(e) (CONN_RMASK(e) - 1) 971230130Smav#define CONN_RESVAL(r, e, n) ((r) >> ((32 / (e)) * (n))) 972230130Smav#define CONN_RANGE(r, e, n) (CONN_RESVAL(r, e, n) & CONN_RMASK(e)) 973230130Smav#define CONN_CNID(r, e, n) (CONN_RESVAL(r, e, n) & CONN_NMASK(e)) 974230130Smav 975230130Smav for (i = 0; i < ents; i += entnum) { 976230130Smav res = hda_command(w->devinfo->dev, 977230130Smav HDA_CMD_GET_CONN_LIST_ENTRY(0, nid, i)); 978230130Smav for (j = 0; j < entnum; j++) { 979230130Smav cnid = CONN_CNID(res, entnum, j); 980230130Smav if (cnid == 0) { 981230130Smav if (w->nconns < ents) 982230130Smav device_printf(w->devinfo->dev, 983230130Smav "WARNING: nid=%d has zero cnid " 984230130Smav "entnum=%d j=%d index=%d " 985230130Smav "entries=%d found=%d res=0x%08x\n", 986230130Smav nid, entnum, j, i, 987230130Smav ents, w->nconns, res); 988230130Smav else 989230130Smav goto getconns_out; 990230130Smav } 991230130Smav if (cnid < w->devinfo->startnode || 992230130Smav cnid >= w->devinfo->endnode) { 993230130Smav HDA_BOOTVERBOSE( 994230130Smav device_printf(w->devinfo->dev, 995230130Smav "WARNING: nid=%d has cnid outside " 996230130Smav "of the AFG range j=%d " 997230130Smav "entnum=%d index=%d res=0x%08x\n", 998230130Smav nid, j, entnum, i, res); 999230130Smav ); 1000230130Smav } 1001230130Smav if (CONN_RANGE(res, entnum, j) == 0) 1002230130Smav addcnid = cnid; 1003230130Smav else if (prevcnid == 0 || prevcnid >= cnid) { 1004230130Smav device_printf(w->devinfo->dev, 1005230130Smav "WARNING: Invalid child range " 1006230130Smav "nid=%d index=%d j=%d entnum=%d " 1007230130Smav "prevcnid=%d cnid=%d res=0x%08x\n", 1008230130Smav nid, i, j, entnum, prevcnid, 1009230130Smav cnid, res); 1010230130Smav addcnid = cnid; 1011230130Smav } else 1012230130Smav addcnid = prevcnid + 1; 1013230130Smav while (addcnid <= cnid) { 1014230130Smav if (w->nconns > max) { 1015230130Smav device_printf(w->devinfo->dev, 1016230130Smav "Adding %d (nid=%d): " 1017230130Smav "Max connection reached! max=%d\n", 1018230130Smav addcnid, nid, max + 1); 1019230130Smav goto getconns_out; 1020230130Smav } 1021230130Smav w->connsenable[w->nconns] = 1; 1022230130Smav w->conns[w->nconns++] = addcnid++; 1023230130Smav } 1024230130Smav prevcnid = cnid; 1025230130Smav } 1026230130Smav } 1027230130Smav 1028230130Smavgetconns_out: 1029230130Smav return; 1030230130Smav} 1031230130Smav 1032230130Smavstatic void 1033230130Smavhdaa_widget_parse(struct hdaa_widget *w) 1034230130Smav{ 1035230130Smav device_t dev = w->devinfo->dev; 1036230130Smav uint32_t wcap, cap; 1037230130Smav nid_t nid = w->nid; 1038230130Smav char buf[64]; 1039230130Smav 1040230130Smav w->param.widget_cap = wcap = hda_command(dev, 1041230130Smav HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_AUDIO_WIDGET_CAP)); 1042230130Smav w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap); 1043230130Smav 1044230130Smav hdaa_widget_connection_parse(w); 1045230130Smav 1046230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) { 1047230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap)) 1048230130Smav w->param.outamp_cap = 1049230130Smav hda_command(dev, 1050230130Smav HDA_CMD_GET_PARAMETER(0, nid, 1051230130Smav HDA_PARAM_OUTPUT_AMP_CAP)); 1052230130Smav else 1053230130Smav w->param.outamp_cap = 1054230130Smav w->devinfo->outamp_cap; 1055230130Smav } else 1056230130Smav w->param.outamp_cap = 0; 1057230130Smav 1058230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) { 1059230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap)) 1060230130Smav w->param.inamp_cap = 1061230130Smav hda_command(dev, 1062230130Smav HDA_CMD_GET_PARAMETER(0, nid, 1063230130Smav HDA_PARAM_INPUT_AMP_CAP)); 1064230130Smav else 1065230130Smav w->param.inamp_cap = 1066230130Smav w->devinfo->inamp_cap; 1067230130Smav } else 1068230130Smav w->param.inamp_cap = 0; 1069230130Smav 1070230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT || 1071230130Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) { 1072230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) { 1073230130Smav cap = hda_command(dev, 1074230130Smav HDA_CMD_GET_PARAMETER(0, nid, 1075230130Smav HDA_PARAM_SUPP_STREAM_FORMATS)); 1076230130Smav w->param.supp_stream_formats = (cap != 0) ? cap : 1077230130Smav w->devinfo->supp_stream_formats; 1078230130Smav cap = hda_command(dev, 1079230130Smav HDA_CMD_GET_PARAMETER(0, nid, 1080230130Smav HDA_PARAM_SUPP_PCM_SIZE_RATE)); 1081230130Smav w->param.supp_pcm_size_rate = (cap != 0) ? cap : 1082230130Smav w->devinfo->supp_pcm_size_rate; 1083230130Smav } else { 1084230130Smav w->param.supp_stream_formats = 1085230130Smav w->devinfo->supp_stream_formats; 1086230130Smav w->param.supp_pcm_size_rate = 1087230130Smav w->devinfo->supp_pcm_size_rate; 1088230130Smav } 1089230326Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) { 1090230326Smav w->wclass.conv.stripecap = hda_command(dev, 1091230326Smav HDA_CMD_GET_STRIPE_CONTROL(0, w->nid)) >> 20; 1092230326Smav } else 1093230326Smav w->wclass.conv.stripecap = 1; 1094230130Smav } else { 1095230130Smav w->param.supp_stream_formats = 0; 1096230130Smav w->param.supp_pcm_size_rate = 0; 1097230130Smav } 1098230130Smav 1099230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) { 1100230130Smav w->wclass.pin.original = w->wclass.pin.newconf = 1101230130Smav w->wclass.pin.config = hda_command(dev, 1102230130Smav HDA_CMD_GET_CONFIGURATION_DEFAULT(0, w->nid)); 1103230130Smav w->wclass.pin.cap = hda_command(dev, 1104230130Smav HDA_CMD_GET_PARAMETER(0, w->nid, HDA_PARAM_PIN_CAP));; 1105230130Smav w->wclass.pin.ctrl = hda_command(dev, 1106230130Smav HDA_CMD_GET_PIN_WIDGET_CTRL(0, nid)); 1107230130Smav if (HDA_PARAM_PIN_CAP_EAPD_CAP(w->wclass.pin.cap)) { 1108230130Smav w->param.eapdbtl = hda_command(dev, 1109230130Smav HDA_CMD_GET_EAPD_BTL_ENABLE(0, nid)); 1110230130Smav w->param.eapdbtl &= 0x7; 1111230130Smav w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD; 1112230130Smav } else 1113230130Smav w->param.eapdbtl = HDA_INVALID; 1114230130Smav 1115230130Smav hdaa_unlock(w->devinfo); 1116230130Smav snprintf(buf, sizeof(buf), "nid%d_config", w->nid); 1117230130Smav SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1118230130Smav SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 1119230130Smav buf, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 1120230130Smav &w->wclass.pin.newconf, sizeof(&w->wclass.pin.newconf), 1121230130Smav hdaa_sysctl_config, "A", "Current pin configuration"); 1122230130Smav snprintf(buf, sizeof(buf), "nid%d_original", w->nid); 1123230130Smav SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1124230130Smav SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 1125230130Smav buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 1126230130Smav &w->wclass.pin.original, sizeof(&w->wclass.pin.original), 1127230130Smav hdaa_sysctl_config, "A", "Original pin configuration"); 1128230130Smav hdaa_lock(w->devinfo); 1129230130Smav } 1130230312Smav w->unsol = -1; 1131230130Smav} 1132230130Smav 1133230130Smavstatic void 1134230130Smavhdaa_widget_postprocess(struct hdaa_widget *w) 1135230130Smav{ 1136230130Smav char *typestr; 1137230130Smav 1138230130Smav w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(w->param.widget_cap); 1139230130Smav switch (w->type) { 1140230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT: 1141230130Smav typestr = "audio output"; 1142230130Smav break; 1143230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT: 1144230130Smav typestr = "audio input"; 1145230130Smav break; 1146230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER: 1147230130Smav typestr = "audio mixer"; 1148230130Smav break; 1149230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR: 1150230130Smav typestr = "audio selector"; 1151230130Smav break; 1152230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX: 1153230130Smav typestr = "pin"; 1154230130Smav break; 1155230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET: 1156230130Smav typestr = "power widget"; 1157230130Smav break; 1158230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET: 1159230130Smav typestr = "volume widget"; 1160230130Smav break; 1161230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET: 1162230130Smav typestr = "beep widget"; 1163230130Smav break; 1164230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET: 1165230130Smav typestr = "vendor widget"; 1166230130Smav break; 1167230130Smav default: 1168230130Smav typestr = "unknown type"; 1169230130Smav break; 1170230130Smav } 1171230130Smav strlcpy(w->name, typestr, sizeof(w->name)); 1172230130Smav 1173230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) { 1174230130Smav uint32_t config; 1175230130Smav const char *devstr; 1176230130Smav int conn, color; 1177230130Smav 1178230130Smav config = w->wclass.pin.config; 1179230130Smav devstr = HDA_DEVS[(config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) >> 1180230130Smav HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT]; 1181230130Smav conn = (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) >> 1182230130Smav HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT; 1183230130Smav color = (config & HDA_CONFIG_DEFAULTCONF_COLOR_MASK) >> 1184230130Smav HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT; 1185230130Smav strlcat(w->name, ": ", sizeof(w->name)); 1186230130Smav strlcat(w->name, devstr, sizeof(w->name)); 1187230130Smav strlcat(w->name, " (", sizeof(w->name)); 1188230130Smav if (conn == 0 && color != 0 && color != 15) { 1189230130Smav strlcat(w->name, HDA_COLORS[color], sizeof(w->name)); 1190230130Smav strlcat(w->name, " ", sizeof(w->name)); 1191230130Smav } 1192230130Smav strlcat(w->name, HDA_CONNS[conn], sizeof(w->name)); 1193230130Smav strlcat(w->name, ")", sizeof(w->name)); 1194230130Smav } 1195230130Smav} 1196230130Smav 1197230130Smavstruct hdaa_widget * 1198230130Smavhdaa_widget_get(struct hdaa_devinfo *devinfo, nid_t nid) 1199230130Smav{ 1200230130Smav if (devinfo == NULL || devinfo->widget == NULL || 1201230130Smav nid < devinfo->startnode || nid >= devinfo->endnode) 1202230130Smav return (NULL); 1203230130Smav return (&devinfo->widget[nid - devinfo->startnode]); 1204230130Smav} 1205230130Smav 1206230130Smavstatic void 1207230130Smavhdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *devinfo, nid_t nid, 1208230130Smav int index, int lmute, int rmute, 1209230130Smav int left, int right, int dir) 1210230130Smav{ 1211230130Smav uint16_t v = 0; 1212230130Smav 1213230130Smav HDA_BOOTHVERBOSE( 1214230130Smav device_printf(devinfo->dev, 1215230130Smav "Setting amplifier nid=%d index=%d %s mute=%d/%d vol=%d/%d\n", 1216230130Smav nid,index,dir ? "in" : "out",lmute,rmute,left,right); 1217230130Smav ); 1218230130Smav if (left != right || lmute != rmute) { 1219230130Smav v = (1 << (15 - dir)) | (1 << 13) | (index << 8) | 1220230130Smav (lmute << 7) | left; 1221230130Smav hda_command(devinfo->dev, 1222230130Smav HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v)); 1223230130Smav v = (1 << (15 - dir)) | (1 << 12) | (index << 8) | 1224230130Smav (rmute << 7) | right; 1225230130Smav } else 1226230130Smav v = (1 << (15 - dir)) | (3 << 12) | (index << 8) | 1227230130Smav (lmute << 7) | left; 1228230130Smav 1229230130Smav hda_command(devinfo->dev, 1230230130Smav HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v)); 1231230130Smav} 1232230130Smav 1233230130Smavstatic void 1234230130Smavhdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *ctl, uint32_t mute, 1235230130Smav int left, int right) 1236230130Smav{ 1237230130Smav nid_t nid; 1238230130Smav int lmute, rmute; 1239230130Smav 1240230130Smav nid = ctl->widget->nid; 1241230130Smav 1242230130Smav /* Save new values if valid. */ 1243230130Smav if (mute != HDAA_AMP_MUTE_DEFAULT) 1244230130Smav ctl->muted = mute; 1245230130Smav if (left != HDAA_AMP_VOL_DEFAULT) 1246230130Smav ctl->left = left; 1247230130Smav if (right != HDAA_AMP_VOL_DEFAULT) 1248230130Smav ctl->right = right; 1249230130Smav /* Prepare effective values */ 1250230130Smav if (ctl->forcemute) { 1251230130Smav lmute = 1; 1252230130Smav rmute = 1; 1253230130Smav left = 0; 1254230130Smav right = 0; 1255230130Smav } else { 1256230130Smav lmute = HDAA_AMP_LEFT_MUTED(ctl->muted); 1257230130Smav rmute = HDAA_AMP_RIGHT_MUTED(ctl->muted); 1258230130Smav left = ctl->left; 1259230130Smav right = ctl->right; 1260230130Smav } 1261230130Smav /* Apply effective values */ 1262230130Smav if (ctl->dir & HDAA_CTL_OUT) 1263230130Smav hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index, 1264230130Smav lmute, rmute, left, right, 0); 1265230130Smav if (ctl->dir & HDAA_CTL_IN) 1266230130Smav hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index, 1267230130Smav lmute, rmute, left, right, 1); 1268230130Smav} 1269230130Smav 1270230130Smavstatic void 1271230130Smavhdaa_widget_connection_select(struct hdaa_widget *w, uint8_t index) 1272230130Smav{ 1273230130Smav if (w == NULL || w->nconns < 1 || index > (w->nconns - 1)) 1274230130Smav return; 1275230130Smav HDA_BOOTHVERBOSE( 1276230130Smav device_printf(w->devinfo->dev, 1277230130Smav "Setting selector nid=%d index=%d\n", w->nid, index); 1278230130Smav ); 1279230130Smav hda_command(w->devinfo->dev, 1280230130Smav HDA_CMD_SET_CONNECTION_SELECT_CONTROL(0, w->nid, index)); 1281230130Smav w->selconn = index; 1282230130Smav} 1283230130Smav 1284230130Smav/**************************************************************************** 1285230130Smav * Device Methods 1286230130Smav ****************************************************************************/ 1287230130Smav 1288230130Smavstatic void * 1289230130Smavhdaa_channel_init(kobj_t obj, void *data, struct snd_dbuf *b, 1290230130Smav struct pcm_channel *c, int dir) 1291230130Smav{ 1292230130Smav struct hdaa_chan *ch = data; 1293230130Smav struct hdaa_pcm_devinfo *pdevinfo = ch->pdevinfo; 1294230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 1295230130Smav 1296230130Smav hdaa_lock(devinfo); 1297230130Smav if (devinfo->quirks & HDAA_QUIRK_FIXEDRATE) { 1298230130Smav ch->caps.minspeed = ch->caps.maxspeed = 48000; 1299230130Smav ch->pcmrates[0] = 48000; 1300230130Smav ch->pcmrates[1] = 0; 1301230130Smav } 1302230130Smav ch->dir = dir; 1303230130Smav ch->b = b; 1304230130Smav ch->c = c; 1305230130Smav ch->blksz = pdevinfo->chan_size / pdevinfo->chan_blkcnt; 1306230130Smav ch->blkcnt = pdevinfo->chan_blkcnt; 1307230130Smav hdaa_unlock(devinfo); 1308230130Smav 1309230130Smav if (sndbuf_alloc(ch->b, bus_get_dma_tag(devinfo->dev), 1310230130Smav hda_get_dma_nocache(devinfo->dev) ? BUS_DMA_NOCACHE : 0, 1311230130Smav pdevinfo->chan_size) != 0) 1312230130Smav return (NULL); 1313230130Smav 1314230130Smav return (ch); 1315230130Smav} 1316230130Smav 1317230130Smavstatic int 1318230130Smavhdaa_channel_setformat(kobj_t obj, void *data, uint32_t format) 1319230130Smav{ 1320230130Smav struct hdaa_chan *ch = data; 1321230130Smav int i; 1322230130Smav 1323230130Smav for (i = 0; ch->caps.fmtlist[i] != 0; i++) { 1324230130Smav if (format == ch->caps.fmtlist[i]) { 1325230130Smav ch->fmt = format; 1326230130Smav return (0); 1327230130Smav } 1328230130Smav } 1329230130Smav 1330230130Smav return (EINVAL); 1331230130Smav} 1332230130Smav 1333230130Smavstatic uint32_t 1334230130Smavhdaa_channel_setspeed(kobj_t obj, void *data, uint32_t speed) 1335230130Smav{ 1336230130Smav struct hdaa_chan *ch = data; 1337230130Smav uint32_t spd = 0, threshold; 1338230130Smav int i; 1339230130Smav 1340230130Smav /* First look for equal or multiple frequency. */ 1341230130Smav for (i = 0; ch->pcmrates[i] != 0; i++) { 1342230130Smav spd = ch->pcmrates[i]; 1343230130Smav if (speed != 0 && spd / speed * speed == spd) { 1344230130Smav ch->spd = spd; 1345230130Smav return (spd); 1346230130Smav } 1347230130Smav } 1348230130Smav /* If no match, just find nearest. */ 1349230130Smav for (i = 0; ch->pcmrates[i] != 0; i++) { 1350230130Smav spd = ch->pcmrates[i]; 1351230130Smav threshold = spd + ((ch->pcmrates[i + 1] != 0) ? 1352230130Smav ((ch->pcmrates[i + 1] - spd) >> 1) : 0); 1353230130Smav if (speed < threshold) 1354230130Smav break; 1355230130Smav } 1356230130Smav ch->spd = spd; 1357230130Smav return (spd); 1358230130Smav} 1359230130Smav 1360230130Smavstatic uint16_t 1361230130Smavhdaa_stream_format(struct hdaa_chan *ch) 1362230130Smav{ 1363230130Smav int i; 1364230130Smav uint16_t fmt; 1365230130Smav 1366230130Smav fmt = 0; 1367230130Smav if (ch->fmt & AFMT_S16_LE) 1368230130Smav fmt |= ch->bit16 << 4; 1369230130Smav else if (ch->fmt & AFMT_S32_LE) 1370230130Smav fmt |= ch->bit32 << 4; 1371230130Smav else 1372230130Smav fmt |= 1 << 4; 1373230130Smav for (i = 0; i < HDA_RATE_TAB_LEN; i++) { 1374230130Smav if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) { 1375230130Smav fmt |= hda_rate_tab[i].base; 1376230130Smav fmt |= hda_rate_tab[i].mul; 1377230130Smav fmt |= hda_rate_tab[i].div; 1378230130Smav break; 1379230130Smav } 1380230130Smav } 1381230130Smav fmt |= (AFMT_CHANNEL(ch->fmt) - 1); 1382230130Smav 1383230130Smav return (fmt); 1384230130Smav} 1385230130Smav 1386230326Smavstatic int 1387230326Smavhdaa_allowed_stripes(uint16_t fmt) 1388230326Smav{ 1389230326Smav static const int bits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 }; 1390230326Smav int size; 1391230326Smav 1392230326Smav size = bits[(fmt >> 4) & 0x03]; 1393230326Smav size *= (fmt & 0x0f) + 1; 1394230326Smav size *= ((fmt >> 11) & 0x07) + 1; 1395230326Smav return (0xffffffffU >> (32 - fls(size / 8))); 1396230326Smav} 1397230326Smav 1398230130Smavstatic void 1399230130Smavhdaa_audio_setup(struct hdaa_chan *ch) 1400230130Smav{ 1401230130Smav struct hdaa_audio_as *as = &ch->devinfo->as[ch->as]; 1402230312Smav struct hdaa_widget *w, *wp; 1403230312Smav int i, j, k, chn, cchn, totalchn, totalextchn, c; 1404230130Smav uint16_t fmt, dfmt; 1405230312Smav /* Mapping channel pairs to codec pins/converters. */ 1406230312Smav const static uint16_t convmap[2][5] = 1407230312Smav {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x0231 }, /* 5.1 */ 1408230312Smav { 0x0010, 0x0001, 0x2001, 0x2031, 0x2431 }};/* 7.1 */ 1409230312Smav /* Mapping formats to HDMI channel allocations. */ 1410230312Smav const static uint8_t hdmica[2][8] = 1411230312Smav {{ 0x02, 0x00, 0x04, 0x08, 0x0a, 0x0e, 0x12, 0x12 }, /* x.0 */ 1412230312Smav { 0x01, 0x03, 0x01, 0x03, 0x09, 0x0b, 0x0f, 0x13 }}; /* x.1 */ 1413230312Smav /* Mapping formats to HDMI channels order. */ 1414230312Smav const static uint32_t hdmich[2][8] = 1415230312Smav {{ 0xFFFF0F00, 0xFFFFFF10, 0xFFF2FF10, 0xFF32FF10, 1416230312Smav 0xFF324F10, 0xF5324F10, 0x54326F10, 0x54326F10 }, /* x.0 */ 1417230312Smav { 0xFFFFF000, 0xFFFF0100, 0xFFFFF210, 0xFFFF2310, 1418230312Smav 0xFF32F410, 0xFF324510, 0xF6324510, 0x76325410 }}; /* x.1 */ 1419230312Smav int convmapid = -1; 1420230312Smav nid_t nid; 1421230312Smav uint8_t csum; 1422230130Smav 1423230130Smav totalchn = AFMT_CHANNEL(ch->fmt); 1424230312Smav totalextchn = AFMT_EXTCHANNEL(ch->fmt); 1425230130Smav HDA_BOOTHVERBOSE( 1426230130Smav device_printf(ch->pdevinfo->dev, 1427230312Smav "PCMDIR_%s: Stream setup fmt=%08x (%d.%d) speed=%d\n", 1428230130Smav (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC", 1429230312Smav ch->fmt, totalchn - totalextchn, totalextchn, ch->spd); 1430230130Smav ); 1431230130Smav fmt = hdaa_stream_format(ch); 1432230130Smav 1433230312Smav /* Set channels to I/O converters mapping for known speaker setups. */ 1434230130Smav if ((as->pinset == 0x0007 || as->pinset == 0x0013)) /* Standard 5.1 */ 1435230312Smav convmapid = 0; 1436230130Smav else if (as->pinset == 0x0017) /* Standard 7.1 */ 1437230312Smav convmapid = 1; 1438230130Smav 1439230130Smav dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN; 1440230130Smav if (ch->fmt & AFMT_AC3) 1441230130Smav dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO; 1442230130Smav 1443230130Smav chn = 0; 1444230130Smav for (i = 0; ch->io[i] != -1; i++) { 1445230130Smav w = hdaa_widget_get(ch->devinfo, ch->io[i]); 1446230130Smav if (w == NULL) 1447230130Smav continue; 1448230130Smav 1449230130Smav /* If HP redirection is enabled, but failed to use same 1450230130Smav DAC, make last DAC to duplicate first one. */ 1451230130Smav if (as->fakeredir && i == (as->pincnt - 1)) { 1452230130Smav c = (ch->sid << 4); 1453230130Smav } else { 1454230312Smav /* Map channels to I/O converters, if set. */ 1455230312Smav if (convmapid >= 0) 1456230312Smav chn = (((convmap[convmapid][totalchn / 2] 1457230312Smav >> i * 4) & 0xf) - 1) * 2; 1458230130Smav if (chn < 0 || chn >= totalchn) { 1459230130Smav c = 0; 1460230130Smav } else { 1461230130Smav c = (ch->sid << 4) | chn; 1462230130Smav } 1463230130Smav } 1464230130Smav hda_command(ch->devinfo->dev, 1465230130Smav HDA_CMD_SET_CONV_FMT(0, ch->io[i], fmt)); 1466230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) { 1467230130Smav hda_command(ch->devinfo->dev, 1468230130Smav HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], dfmt)); 1469230130Smav } 1470230130Smav hda_command(ch->devinfo->dev, 1471230130Smav HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i], c)); 1472230326Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) { 1473230326Smav hda_command(ch->devinfo->dev, 1474230326Smav HDA_CMD_SET_STRIPE_CONTROL(0, w->nid, ch->stripectl)); 1475230326Smav } 1476230312Smav cchn = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap); 1477230312Smav if (cchn > 1 && chn < totalchn) { 1478230312Smav cchn = min(cchn, totalchn - chn - 1); 1479230312Smav hda_command(ch->devinfo->dev, 1480230312Smav HDA_CMD_SET_CONV_CHAN_COUNT(0, ch->io[i], cchn)); 1481230312Smav } 1482230312Smav HDA_BOOTHVERBOSE( 1483230312Smav device_printf(ch->pdevinfo->dev, 1484230312Smav "PCMDIR_%s: Stream setup nid=%d: " 1485230312Smav "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x, " 1486230326Smav "chan_count=0x%02x, stripe=%d\n", 1487230312Smav (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC", 1488230326Smav ch->io[i], fmt, dfmt, c, cchn, ch->stripectl); 1489230312Smav ); 1490230312Smav for (j = 0; j < 16; j++) { 1491230312Smav if (as->dacs[ch->asindex][j] != ch->io[i]) 1492230312Smav continue; 1493230312Smav nid = as->pins[j]; 1494230312Smav wp = hdaa_widget_get(ch->devinfo, nid); 1495230312Smav if (wp == NULL) 1496230312Smav continue; 1497230312Smav if (!HDA_PARAM_PIN_CAP_DP(wp->wclass.pin.cap) && 1498230312Smav !HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap)) 1499230312Smav continue; 1500230312Smav 1501230312Smav /* Set channel mapping. */ 1502230312Smav for (k = 0; k < 8; k++) { 1503230312Smav hda_command(ch->devinfo->dev, 1504230312Smav HDA_CMD_SET_HDMI_CHAN_SLOT(0, nid, 1505230312Smav (((hdmich[totalextchn == 0 ? 0 : 1][totalchn - 1] 1506230312Smav >> (k * 4)) & 0xf) << 4) | k)); 1507230312Smav } 1508230312Smav 1509230312Smav /* Stop audio infoframe transmission. */ 1510230312Smav hda_command(ch->devinfo->dev, 1511230312Smav HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00)); 1512230312Smav hda_command(ch->devinfo->dev, 1513230312Smav HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0x00)); 1514230312Smav 1515230312Smav /* Clear audio infoframe buffer. */ 1516230312Smav hda_command(ch->devinfo->dev, 1517230312Smav HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00)); 1518230312Smav for (k = 0; k < 32; k++) 1519230312Smav hda_command(ch->devinfo->dev, 1520230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00)); 1521230312Smav 1522230312Smav /* Write HDMI/DisplayPort audio infoframe. */ 1523230312Smav hda_command(ch->devinfo->dev, 1524230312Smav HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00)); 1525230312Smav if (w->eld != NULL && w->eld_len >= 6 && 1526230312Smav ((w->eld[5] >> 2) & 0x3) == 1) { /* DisplayPort */ 1527230312Smav hda_command(ch->devinfo->dev, 1528230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84)); 1529230312Smav hda_command(ch->devinfo->dev, 1530230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x1b)); 1531230312Smav hda_command(ch->devinfo->dev, 1532230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x44)); 1533230312Smav } else { /* HDMI */ 1534230312Smav hda_command(ch->devinfo->dev, 1535230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84)); 1536230312Smav hda_command(ch->devinfo->dev, 1537230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x01)); 1538230312Smav hda_command(ch->devinfo->dev, 1539230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x0a)); 1540230312Smav csum = 0; 1541230312Smav csum -= 0x84 + 0x01 + 0x0a + (totalchn - 1) + 1542230312Smav hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1]; 1543230312Smav hda_command(ch->devinfo->dev, 1544230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, csum)); 1545230312Smav } 1546230312Smav hda_command(ch->devinfo->dev, 1547230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, totalchn - 1)); 1548230312Smav hda_command(ch->devinfo->dev, 1549230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00)); 1550230312Smav hda_command(ch->devinfo->dev, 1551230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00)); 1552230312Smav hda_command(ch->devinfo->dev, 1553230312Smav HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 1554230312Smav hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1])); 1555230312Smav 1556230312Smav /* Start audio infoframe transmission. */ 1557230312Smav hda_command(ch->devinfo->dev, 1558230312Smav HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00)); 1559230312Smav hda_command(ch->devinfo->dev, 1560230312Smav HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0xc0)); 1561230312Smav } 1562230312Smav chn += cchn + 1; 1563230130Smav } 1564230130Smav} 1565230130Smav 1566230130Smav/* 1567230130Smav * Greatest Common Divisor. 1568230130Smav */ 1569230130Smavstatic unsigned 1570230130Smavgcd(unsigned a, unsigned b) 1571230130Smav{ 1572230130Smav u_int c; 1573230130Smav 1574230130Smav while (b != 0) { 1575230130Smav c = a; 1576230130Smav a = b; 1577230130Smav b = (c % b); 1578230130Smav } 1579230130Smav return (a); 1580230130Smav} 1581230130Smav 1582230130Smav/* 1583230130Smav * Least Common Multiple. 1584230130Smav */ 1585230130Smavstatic unsigned 1586230130Smavlcm(unsigned a, unsigned b) 1587230130Smav{ 1588230130Smav 1589230130Smav return ((a * b) / gcd(a, b)); 1590230130Smav} 1591230130Smav 1592230130Smavstatic int 1593230130Smavhdaa_channel_setfragments(kobj_t obj, void *data, 1594230130Smav uint32_t blksz, uint32_t blkcnt) 1595230130Smav{ 1596230130Smav struct hdaa_chan *ch = data; 1597230130Smav 1598230130Smav blksz -= blksz % lcm(HDA_DMA_ALIGNMENT, sndbuf_getalign(ch->b)); 1599230130Smav 1600230130Smav if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN)) 1601230130Smav blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN; 1602230130Smav if (blksz < HDA_BLK_MIN) 1603230130Smav blksz = HDA_BLK_MIN; 1604230130Smav if (blkcnt > HDA_BDL_MAX) 1605230130Smav blkcnt = HDA_BDL_MAX; 1606230130Smav if (blkcnt < HDA_BDL_MIN) 1607230130Smav blkcnt = HDA_BDL_MIN; 1608230130Smav 1609230130Smav while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) { 1610230130Smav if ((blkcnt >> 1) >= HDA_BDL_MIN) 1611230130Smav blkcnt >>= 1; 1612230130Smav else if ((blksz >> 1) >= HDA_BLK_MIN) 1613230130Smav blksz >>= 1; 1614230130Smav else 1615230130Smav break; 1616230130Smav } 1617230130Smav 1618230130Smav if ((sndbuf_getblksz(ch->b) != blksz || 1619230130Smav sndbuf_getblkcnt(ch->b) != blkcnt) && 1620230130Smav sndbuf_resize(ch->b, blkcnt, blksz) != 0) 1621230130Smav device_printf(ch->devinfo->dev, "%s: failed blksz=%u blkcnt=%u\n", 1622230130Smav __func__, blksz, blkcnt); 1623230130Smav 1624230130Smav ch->blksz = sndbuf_getblksz(ch->b); 1625230130Smav ch->blkcnt = sndbuf_getblkcnt(ch->b); 1626230130Smav 1627230130Smav return (0); 1628230130Smav} 1629230130Smav 1630230130Smavstatic uint32_t 1631230130Smavhdaa_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz) 1632230130Smav{ 1633230130Smav struct hdaa_chan *ch = data; 1634230130Smav 1635230130Smav hdaa_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt); 1636230130Smav 1637230130Smav return (ch->blksz); 1638230130Smav} 1639230130Smav 1640230130Smavstatic void 1641230130Smavhdaa_channel_stop(struct hdaa_chan *ch) 1642230130Smav{ 1643230130Smav struct hdaa_devinfo *devinfo = ch->devinfo; 1644230130Smav struct hdaa_widget *w; 1645230130Smav int i; 1646230130Smav 1647230181Smav if ((ch->flags & HDAA_CHN_RUNNING) == 0) 1648230181Smav return; 1649230130Smav ch->flags &= ~HDAA_CHN_RUNNING; 1650230130Smav HDAC_STREAM_STOP(device_get_parent(devinfo->dev), devinfo->dev, 1651230130Smav ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid); 1652230130Smav for (i = 0; ch->io[i] != -1; i++) { 1653230130Smav w = hdaa_widget_get(ch->devinfo, ch->io[i]); 1654230130Smav if (w == NULL) 1655230130Smav continue; 1656230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) { 1657230130Smav hda_command(devinfo->dev, 1658230130Smav HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], 0)); 1659230130Smav } 1660230130Smav hda_command(devinfo->dev, 1661230130Smav HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i], 1662230130Smav 0)); 1663230130Smav } 1664230130Smav HDAC_STREAM_FREE(device_get_parent(devinfo->dev), devinfo->dev, 1665230130Smav ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid); 1666230130Smav} 1667230130Smav 1668230130Smavstatic int 1669230130Smavhdaa_channel_start(struct hdaa_chan *ch) 1670230130Smav{ 1671230130Smav struct hdaa_devinfo *devinfo = ch->devinfo; 1672230326Smav uint32_t fmt; 1673230130Smav 1674230326Smav fmt = hdaa_stream_format(ch); 1675230326Smav ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt)) - 1; 1676230130Smav ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev, 1677230326Smav ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos); 1678230130Smav if (ch->sid <= 0) 1679230130Smav return (EBUSY); 1680230130Smav hdaa_audio_setup(ch); 1681230130Smav HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev, 1682230130Smav ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid); 1683230130Smav HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev, 1684230130Smav ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid, 1685230130Smav sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt); 1686230130Smav ch->flags |= HDAA_CHN_RUNNING; 1687230130Smav return (0); 1688230130Smav} 1689230130Smav 1690230130Smavstatic int 1691230130Smavhdaa_channel_trigger(kobj_t obj, void *data, int go) 1692230130Smav{ 1693230130Smav struct hdaa_chan *ch = data; 1694230130Smav int error = 0; 1695230130Smav 1696230130Smav if (!PCMTRIG_COMMON(go)) 1697230130Smav return (0); 1698230130Smav 1699230130Smav hdaa_lock(ch->devinfo); 1700230130Smav switch (go) { 1701230130Smav case PCMTRIG_START: 1702230130Smav error = hdaa_channel_start(ch); 1703230130Smav break; 1704230130Smav case PCMTRIG_STOP: 1705230130Smav case PCMTRIG_ABORT: 1706230130Smav hdaa_channel_stop(ch); 1707230130Smav break; 1708230130Smav default: 1709230130Smav break; 1710230130Smav } 1711230130Smav hdaa_unlock(ch->devinfo); 1712230130Smav 1713230130Smav return (error); 1714230130Smav} 1715230130Smav 1716230130Smavstatic uint32_t 1717230130Smavhdaa_channel_getptr(kobj_t obj, void *data) 1718230130Smav{ 1719230130Smav struct hdaa_chan *ch = data; 1720230130Smav struct hdaa_devinfo *devinfo = ch->devinfo; 1721230130Smav uint32_t ptr; 1722230130Smav 1723230130Smav hdaa_lock(devinfo); 1724230130Smav if (ch->dmapos != NULL) { 1725230130Smav ptr = *(ch->dmapos); 1726230130Smav } else { 1727230130Smav ptr = HDAC_STREAM_GETPTR( 1728230130Smav device_get_parent(devinfo->dev), devinfo->dev, 1729230130Smav ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid); 1730230130Smav } 1731230130Smav hdaa_unlock(devinfo); 1732230130Smav 1733230130Smav /* 1734230130Smav * Round to available space and force 128 bytes aligment. 1735230130Smav */ 1736230130Smav ptr %= ch->blksz * ch->blkcnt; 1737230130Smav ptr &= HDA_BLK_ALIGN; 1738230130Smav 1739230130Smav return (ptr); 1740230130Smav} 1741230130Smav 1742230130Smavstatic struct pcmchan_caps * 1743230130Smavhdaa_channel_getcaps(kobj_t obj, void *data) 1744230130Smav{ 1745230130Smav return (&((struct hdaa_chan *)data)->caps); 1746230130Smav} 1747230130Smav 1748230130Smavstatic kobj_method_t hdaa_channel_methods[] = { 1749230130Smav KOBJMETHOD(channel_init, hdaa_channel_init), 1750230130Smav KOBJMETHOD(channel_setformat, hdaa_channel_setformat), 1751230130Smav KOBJMETHOD(channel_setspeed, hdaa_channel_setspeed), 1752230130Smav KOBJMETHOD(channel_setblocksize, hdaa_channel_setblocksize), 1753230130Smav KOBJMETHOD(channel_setfragments, hdaa_channel_setfragments), 1754230130Smav KOBJMETHOD(channel_trigger, hdaa_channel_trigger), 1755230130Smav KOBJMETHOD(channel_getptr, hdaa_channel_getptr), 1756230130Smav KOBJMETHOD(channel_getcaps, hdaa_channel_getcaps), 1757230130Smav KOBJMETHOD_END 1758230130Smav}; 1759230130SmavCHANNEL_DECLARE(hdaa_channel); 1760230130Smav 1761230130Smavstatic int 1762230130Smavhdaa_audio_ctl_ossmixer_init(struct snd_mixer *m) 1763230130Smav{ 1764230130Smav struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m); 1765230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 1766230130Smav struct hdaa_widget *w, *cw; 1767230130Smav uint32_t mask, recmask; 1768230451Smav int i, j; 1769230130Smav 1770230130Smav hdaa_lock(devinfo); 1771230451Smav pdevinfo->mixer = m; 1772230130Smav 1773230130Smav /* Make sure that in case of soft volume it won't stay muted. */ 1774230130Smav for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 1775230130Smav pdevinfo->left[i] = 100; 1776230130Smav pdevinfo->right[i] = 100; 1777230130Smav } 1778230130Smav 1779230451Smav /* Declare volume controls assigned to this association. */ 1780230451Smav mask = pdevinfo->ossmask; 1781230130Smav if (pdevinfo->playas >= 0) { 1782230451Smav /* Declate EAPD as ogain control. */ 1783230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 1784230130Smav w = hdaa_widget_get(devinfo, i); 1785230130Smav if (w == NULL || w->enable == 0) 1786230130Smav continue; 1787230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || 1788230130Smav w->param.eapdbtl == HDA_INVALID || 1789230130Smav w->bindas != pdevinfo->playas) 1790230130Smav continue; 1791230130Smav mask |= SOUND_MASK_OGAIN; 1792230130Smav break; 1793230130Smav } 1794230130Smav 1795230451Smav /* Declare soft PCM volume if needed. */ 1796230451Smav if ((mask & SOUND_MASK_PCM) == 0 || 1797230451Smav (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) || 1798230451Smav pdevinfo->minamp[SOUND_MIXER_PCM] == 1799230451Smav pdevinfo->maxamp[SOUND_MIXER_PCM]) { 1800230451Smav mask |= SOUND_MASK_PCM; 1801230451Smav pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL); 1802230451Smav HDA_BOOTHVERBOSE( 1803230451Smav device_printf(pdevinfo->dev, 1804230451Smav "Forcing Soft PCM volume\n"); 1805230451Smav ); 1806230451Smav } 1807230451Smav 1808230451Smav /* Declare master volume if needed. */ 1809230451Smav if ((mask & SOUND_MASK_VOLUME) == 0) { 1810230451Smav mask |= SOUND_MASK_VOLUME; 1811230451Smav mix_setparentchild(m, SOUND_MIXER_VOLUME, 1812230451Smav SOUND_MASK_PCM); 1813230451Smav mix_setrealdev(m, SOUND_MIXER_VOLUME, 1814230451Smav SOUND_MIXER_NONE); 1815230451Smav HDA_BOOTHVERBOSE( 1816230451Smav device_printf(pdevinfo->dev, 1817230451Smav "Forcing master volume with PCM\n"); 1818230451Smav ); 1819230451Smav } 1820230130Smav } 1821230130Smav 1822230130Smav /* Declare record sources available to this association. */ 1823230451Smav recmask = 0; 1824230130Smav if (pdevinfo->recas >= 0) { 1825230130Smav for (i = 0; i < 16; i++) { 1826230130Smav if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0) 1827230130Smav continue; 1828230130Smav w = hdaa_widget_get(devinfo, 1829230130Smav devinfo->as[pdevinfo->recas].dacs[0][i]); 1830230130Smav if (w == NULL || w->enable == 0) 1831230130Smav continue; 1832230130Smav for (j = 0; j < w->nconns; j++) { 1833230130Smav if (w->connsenable[j] == 0) 1834230130Smav continue; 1835230130Smav cw = hdaa_widget_get(devinfo, w->conns[j]); 1836230130Smav if (cw == NULL || cw->enable == 0) 1837230130Smav continue; 1838230130Smav if (cw->bindas != pdevinfo->recas && 1839230130Smav cw->bindas != -2) 1840230130Smav continue; 1841230130Smav recmask |= cw->ossmask; 1842230130Smav } 1843230130Smav } 1844230130Smav } 1845230130Smav 1846230451Smav recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1; 1847230451Smav mask &= (1 << SOUND_MIXER_NRDEVICES) - 1; 1848230451Smav pdevinfo->ossmask = mask; 1849230451Smav 1850230451Smav mix_setrecdevs(m, recmask); 1851230451Smav mix_setdevs(m, mask); 1852230451Smav 1853230451Smav hdaa_unlock(devinfo); 1854230451Smav 1855230451Smav return (0); 1856230451Smav} 1857230451Smav 1858230451Smav/* 1859230451Smav * Update amplification per pdevinfo per ossdev, calculate summary coefficient 1860230451Smav * and write it to codec, update *left and *right to reflect remaining error. 1861230451Smav */ 1862230451Smavstatic void 1863230451Smavhdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev, 1864230451Smav int mute, int *left, int *right) 1865230451Smav{ 1866230451Smav int i, zleft, zright, sleft, sright, smute, lval, rval; 1867230451Smav 1868230451Smav ctl->devleft[ossdev] = *left; 1869230451Smav ctl->devright[ossdev] = *right; 1870230451Smav ctl->devmute[ossdev] = mute; 1871230451Smav smute = sleft = sright = zleft = zright = 0; 1872230451Smav for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 1873230451Smav sleft += ctl->devleft[i]; 1874230451Smav sright += ctl->devright[i]; 1875230451Smav smute |= ctl->devmute[i]; 1876230451Smav if (i == ossdev) 1877230451Smav continue; 1878230451Smav zleft += ctl->devleft[i]; 1879230451Smav zright += ctl->devright[i]; 1880230451Smav } 1881230451Smav lval = QDB2VAL(ctl, sleft); 1882230451Smav rval = QDB2VAL(ctl, sright); 1883230451Smav hdaa_audio_ctl_amp_set(ctl, smute, lval, rval); 1884230451Smav *left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft)); 1885230451Smav *right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright)); 1886230451Smav} 1887230451Smav 1888230451Smav/* 1889230451Smav * Trace signal from source, setting volumes on the way. 1890230451Smav */ 1891230451Smavstatic void 1892230451Smavhdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo, 1893230451Smav int ossdev, nid_t nid, int index, int mute, int left, int right, int depth) 1894230451Smav{ 1895230451Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 1896230451Smav struct hdaa_widget *w, *wc; 1897230451Smav struct hdaa_audio_ctl *ctl; 1898230451Smav int i, j, conns = 0; 1899230451Smav 1900230451Smav if (depth > HDA_PARSE_MAXDEPTH) 1901230451Smav return; 1902230451Smav 1903230451Smav w = hdaa_widget_get(devinfo, nid); 1904230451Smav if (w == NULL || w->enable == 0) 1905230451Smav return; 1906230451Smav 1907230451Smav /* Count number of active inputs. */ 1908230451Smav if (depth > 0) { 1909230451Smav for (j = 0; j < w->nconns; j++) { 1910230451Smav if (!w->connsenable[j]) 1911230451Smav continue; 1912230451Smav conns++; 1913230130Smav } 1914230451Smav } 1915230130Smav 1916230451Smav /* If this is not a first step - use input mixer. 1917230451Smav Pins have common input ctl so care must be taken. */ 1918230451Smav if (depth > 0 && (conns == 1 || 1919230451Smav w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) { 1920230451Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN, 1921230451Smav index, 1); 1922230451Smav if (ctl) 1923230451Smav hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right); 1924230451Smav } 1925230451Smav 1926230451Smav /* If widget has own ossdev - not traverse it. 1927230451Smav It will be traversed on it's own. */ 1928230451Smav if (w->ossdev >= 0 && depth > 0) 1929230451Smav return; 1930230451Smav 1931230451Smav /* We must not traverse pin */ 1932230451Smav if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT || 1933230451Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) && 1934230451Smav depth > 0) 1935230451Smav return; 1936230451Smav 1937230451Smav /* 1938230451Smav * If signals mixed, we can't assign controls farther. 1939230451Smav * Ignore this on depth zero. Caller must knows why. 1940230451Smav */ 1941230451Smav if (conns > 1 && 1942230451Smav (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER || 1943230451Smav w->selconn != index)) 1944230451Smav return; 1945230451Smav 1946230451Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1); 1947230451Smav if (ctl) 1948230451Smav hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right); 1949230451Smav 1950230451Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 1951230451Smav wc = hdaa_widget_get(devinfo, i); 1952230451Smav if (wc == NULL || wc->enable == 0) 1953230451Smav continue; 1954230451Smav for (j = 0; j < wc->nconns; j++) { 1955230451Smav if (wc->connsenable[j] && wc->conns[j] == nid) { 1956230451Smav hdaa_audio_ctl_source_volume(pdevinfo, ossdev, 1957230451Smav wc->nid, j, mute, left, right, depth + 1); 1958230451Smav } 1959230130Smav } 1960230130Smav } 1961230451Smav return; 1962230451Smav} 1963230130Smav 1964230451Smav/* 1965230451Smav * Trace signal from destination, setting volumes on the way. 1966230451Smav */ 1967230451Smavstatic void 1968230451Smavhdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo, 1969230451Smav int ossdev, nid_t nid, int index, int mute, int left, int right, int depth) 1970230451Smav{ 1971230451Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 1972230451Smav struct hdaa_audio_as *as = devinfo->as; 1973230451Smav struct hdaa_widget *w, *wc; 1974230451Smav struct hdaa_audio_ctl *ctl; 1975230451Smav int i, j, consumers, cleft, cright; 1976230451Smav 1977230451Smav if (depth > HDA_PARSE_MAXDEPTH) 1978230451Smav return; 1979230451Smav 1980230451Smav w = hdaa_widget_get(devinfo, nid); 1981230451Smav if (w == NULL || w->enable == 0) 1982230451Smav return; 1983230451Smav 1984230451Smav if (depth > 0) { 1985230451Smav /* If this node produce output for several consumers, 1986230451Smav we can't touch it. */ 1987230451Smav consumers = 0; 1988230451Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 1989230451Smav wc = hdaa_widget_get(devinfo, i); 1990230451Smav if (wc == NULL || wc->enable == 0) 1991230451Smav continue; 1992230451Smav for (j = 0; j < wc->nconns; j++) { 1993230451Smav if (wc->connsenable[j] && wc->conns[j] == nid) 1994230451Smav consumers++; 1995230451Smav } 1996230130Smav } 1997230451Smav /* The only exception is if real HP redirection is configured 1998230451Smav and this is a duplication point. 1999230451Smav XXX: Actually exception is not completely correct. 2000230451Smav XXX: Duplication point check is not perfect. */ 2001230451Smav if ((consumers == 2 && (w->bindas < 0 || 2002230451Smav as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir || 2003230451Smav (w->bindseqmask & (1 << 15)) == 0)) || 2004230451Smav consumers > 2) 2005230451Smav return; 2006230451Smav 2007230451Smav /* Else use it's output mixer. */ 2008230451Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, 2009230451Smav HDAA_CTL_OUT, -1, 1); 2010230451Smav if (ctl) 2011230451Smav hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right); 2012230130Smav } 2013230130Smav 2014230451Smav /* We must not traverse pin */ 2015230451Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX && 2016230451Smav depth > 0) 2017230451Smav return; 2018230130Smav 2019230451Smav for (i = 0; i < w->nconns; i++) { 2020230451Smav if (w->connsenable[i] == 0) 2021230451Smav continue; 2022230451Smav if (index >= 0 && i != index) 2023230451Smav continue; 2024230451Smav cleft = left; 2025230451Smav cright = right; 2026230451Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, 2027230451Smav HDAA_CTL_IN, i, 1); 2028230451Smav if (ctl) 2029230451Smav hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright); 2030230451Smav hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1, 2031230451Smav mute, cleft, cright, depth + 1); 2032230451Smav } 2033230451Smav} 2034230130Smav 2035230451Smav/* 2036230451Smav * Set volumes for the specified pdevinfo and ossdev. 2037230451Smav */ 2038230451Smavstatic void 2039230451Smavhdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev) 2040230451Smav{ 2041230451Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 2042230451Smav struct hdaa_widget *w, *cw; 2043230451Smav uint32_t mute; 2044230451Smav int lvol, rvol; 2045230451Smav int i, j; 2046230130Smav 2047230451Smav mute = 0; 2048230451Smav if (pdevinfo->left[dev] == 0) { 2049230451Smav mute |= HDAA_AMP_MUTE_LEFT; 2050230451Smav lvol = -4000; 2051230451Smav } else 2052230451Smav lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) * 2053230451Smav pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev]; 2054230451Smav if (pdevinfo->right[dev] == 0) { 2055230451Smav mute |= HDAA_AMP_MUTE_RIGHT; 2056230451Smav rvol = -4000; 2057230451Smav } else 2058230451Smav rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) * 2059230451Smav pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev]; 2060230451Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 2061230451Smav w = hdaa_widget_get(devinfo, i); 2062230451Smav if (w == NULL || w->enable == 0) 2063230451Smav continue; 2064230451Smav if (w->bindas < 0 && pdevinfo->index != 0) 2065230451Smav continue; 2066230451Smav if (w->bindas != pdevinfo->playas && 2067230451Smav w->bindas != pdevinfo->recas) 2068230451Smav continue; 2069230451Smav if (dev == SOUND_MIXER_RECLEV && 2070230451Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) { 2071230451Smav hdaa_audio_ctl_dest_volume(pdevinfo, dev, 2072230451Smav w->nid, -1, mute, lvol, rvol, 0); 2073230451Smav continue; 2074230451Smav } 2075230451Smav if (dev == SOUND_MIXER_VOLUME && 2076230451Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX && 2077230451Smav devinfo->as[w->bindas].dir == HDAA_CTL_OUT) { 2078230451Smav hdaa_audio_ctl_dest_volume(pdevinfo, dev, 2079230451Smav w->nid, -1, mute, lvol, rvol, 0); 2080230451Smav continue; 2081230451Smav } 2082230451Smav if (dev == SOUND_MIXER_IGAIN && 2083230451Smav w->pflags & HDAA_ADC_MONITOR) { 2084230451Smav for (j = 0; j < w->nconns; j++) { 2085230451Smav if (!w->connsenable[j]) 2086230451Smav continue; 2087230451Smav cw = hdaa_widget_get(devinfo, w->conns[j]); 2088230451Smav if (cw == NULL || cw->enable == 0) 2089230451Smav continue; 2090230451Smav if (cw->bindas == -1) 2091230451Smav continue; 2092230451Smav if (cw->bindas >= 0 && 2093230451Smav devinfo->as[cw->bindas].dir != HDAA_CTL_IN) 2094230451Smav continue; 2095230451Smav hdaa_audio_ctl_dest_volume(pdevinfo, dev, 2096230451Smav w->nid, j, mute, lvol, rvol, 0); 2097230451Smav } 2098230451Smav continue; 2099230451Smav } 2100230451Smav if (w->ossdev != dev) 2101230451Smav continue; 2102230451Smav hdaa_audio_ctl_source_volume(pdevinfo, dev, 2103230451Smav w->nid, -1, mute, lvol, rvol, 0); 2104230451Smav if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST)) 2105230451Smav hdaa_audio_ctl_dest_volume(pdevinfo, dev, 2106230451Smav w->nid, -1, mute, lvol, rvol, 0); 2107230451Smav } 2108230130Smav} 2109230130Smav 2110230451Smav/* 2111230451Smav * OSS Mixer set method. 2112230451Smav */ 2113230130Smavstatic int 2114230130Smavhdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev, 2115230130Smav unsigned left, unsigned right) 2116230130Smav{ 2117230130Smav struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m); 2118230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 2119230130Smav struct hdaa_widget *w; 2120230451Smav int i; 2121230130Smav 2122230130Smav hdaa_lock(devinfo); 2123230451Smav 2124230130Smav /* Save new values. */ 2125230130Smav pdevinfo->left[dev] = left; 2126230130Smav pdevinfo->right[dev] = right; 2127230130Smav 2128230130Smav /* 'ogain' is the special case implemented with EAPD. */ 2129230130Smav if (dev == SOUND_MIXER_OGAIN) { 2130230130Smav uint32_t orig; 2131230130Smav w = NULL; 2132230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 2133230130Smav w = hdaa_widget_get(devinfo, i); 2134230130Smav if (w == NULL || w->enable == 0) 2135230130Smav continue; 2136230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || 2137230130Smav w->param.eapdbtl == HDA_INVALID) 2138230130Smav continue; 2139230130Smav break; 2140230130Smav } 2141230130Smav if (i >= devinfo->endnode) { 2142230130Smav hdaa_unlock(devinfo); 2143230130Smav return (-1); 2144230130Smav } 2145230130Smav orig = w->param.eapdbtl; 2146230130Smav if (left == 0) 2147230130Smav w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD; 2148230130Smav else 2149230130Smav w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD; 2150230130Smav if (orig != w->param.eapdbtl) { 2151230130Smav uint32_t val; 2152230130Smav 2153230130Smav val = w->param.eapdbtl; 2154230130Smav if (devinfo->quirks & HDAA_QUIRK_EAPDINV) 2155230130Smav val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD; 2156230130Smav hda_command(devinfo->dev, 2157230130Smav HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val)); 2158230130Smav } 2159230130Smav hdaa_unlock(devinfo); 2160230130Smav return (left | (left << 8)); 2161230130Smav } 2162230130Smav 2163230130Smav /* Recalculate all controls related to this OSS device. */ 2164230451Smav hdaa_audio_ctl_dev_volume(pdevinfo, dev); 2165230451Smav 2166230451Smav hdaa_unlock(devinfo); 2167230451Smav return (left | (right << 8)); 2168230451Smav} 2169230451Smav 2170230451Smav/* 2171230451Smav * Set mixer settings to our own default values: 2172230451Smav * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others. 2173230451Smav */ 2174230451Smavstatic void 2175230451Smavhdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo) 2176230451Smav{ 2177230451Smav int amp, vol, dev; 2178230451Smav 2179230451Smav for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) { 2180230451Smav if ((pdevinfo->ossmask & (1 << dev)) == 0) 2181230130Smav continue; 2182230451Smav 2183230451Smav /* If the value was overriden, leave it as is. */ 2184230451Smav if (resource_int_value(device_get_name(pdevinfo->dev), 2185230451Smav device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0) 2186230130Smav continue; 2187230130Smav 2188230451Smav vol = -1; 2189230451Smav if (dev == SOUND_MIXER_OGAIN) 2190230451Smav vol = 100; 2191230451Smav else if (dev == SOUND_MIXER_IGAIN) 2192230451Smav vol = 0; 2193230451Smav else if (dev == SOUND_MIXER_MIC || 2194230451Smav dev == SOUND_MIXER_MONITOR) 2195230451Smav amp = 20 * 4; /* +20dB */ 2196230451Smav else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital) 2197230451Smav amp = -10 * 4; /* -10dB */ 2198230451Smav else 2199230451Smav amp = 0; 2200230451Smav if (vol < 0 && 2201230451Smav (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) { 2202230451Smav vol = 100; 2203230451Smav } else if (vol < 0) { 2204230451Smav vol = ((amp - pdevinfo->minamp[dev]) * 100 + 2205230451Smav (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) / 2206230451Smav (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]); 2207230451Smav vol = imin(imax(vol, 1), 100); 2208230130Smav } 2209230451Smav mix_set(pdevinfo->mixer, dev, vol, vol); 2210230130Smav } 2211230130Smav} 2212230130Smav 2213230130Smav/* 2214230451Smav * Recursively commutate specified record source. 2215230130Smav */ 2216230130Smavstatic uint32_t 2217230130Smavhdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth) 2218230130Smav{ 2219230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 2220230130Smav struct hdaa_widget *w, *cw; 2221230130Smav struct hdaa_audio_ctl *ctl; 2222230130Smav char buf[64]; 2223230130Smav int i, muted; 2224230130Smav uint32_t res = 0; 2225230130Smav 2226230130Smav if (depth > HDA_PARSE_MAXDEPTH) 2227230130Smav return (0); 2228230130Smav 2229230130Smav w = hdaa_widget_get(devinfo, nid); 2230230130Smav if (w == NULL || w->enable == 0) 2231230130Smav return (0); 2232230130Smav 2233230130Smav for (i = 0; i < w->nconns; i++) { 2234230130Smav if (w->connsenable[i] == 0) 2235230130Smav continue; 2236230130Smav cw = hdaa_widget_get(devinfo, w->conns[i]); 2237230130Smav if (cw == NULL || cw->enable == 0 || cw->bindas == -1) 2238230130Smav continue; 2239230130Smav /* Call recursively to trace signal to it's source if needed. */ 2240230130Smav if ((src & cw->ossmask) != 0) { 2241230130Smav if (cw->ossdev < 0) { 2242230130Smav res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src, 2243230130Smav w->conns[i], depth + 1); 2244230130Smav } else { 2245230130Smav res |= cw->ossmask; 2246230130Smav } 2247230130Smav } 2248230130Smav /* We have two special cases: mixers and others (selectors). */ 2249230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) { 2250230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, 2251230130Smav w->nid, HDAA_CTL_IN, i, 1); 2252230130Smav if (ctl == NULL) 2253230130Smav continue; 2254230130Smav /* If we have input control on this node mute them 2255230130Smav * according to requested sources. */ 2256230130Smav muted = (src & cw->ossmask) ? 0 : 1; 2257230130Smav if (muted != ctl->forcemute) { 2258230130Smav ctl->forcemute = muted; 2259230130Smav hdaa_audio_ctl_amp_set(ctl, 2260230130Smav HDAA_AMP_MUTE_DEFAULT, 2261230130Smav HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT); 2262230130Smav } 2263230130Smav HDA_BOOTHVERBOSE( 2264230130Smav device_printf(pdevinfo->dev, 2265230130Smav "Recsel (%s): nid %d source %d %s\n", 2266230130Smav hdaa_audio_ctl_ossmixer_mask2allname( 2267230130Smav src, buf, sizeof(buf)), 2268230130Smav nid, i, muted?"mute":"unmute"); 2269230130Smav ); 2270230130Smav } else { 2271230130Smav if (w->nconns == 1) 2272230130Smav break; 2273230130Smav if ((src & cw->ossmask) == 0) 2274230130Smav continue; 2275230130Smav /* If we found requested source - select it and exit. */ 2276230130Smav hdaa_widget_connection_select(w, i); 2277230130Smav HDA_BOOTHVERBOSE( 2278230130Smav device_printf(pdevinfo->dev, 2279230130Smav "Recsel (%s): nid %d source %d select\n", 2280230130Smav hdaa_audio_ctl_ossmixer_mask2allname( 2281230130Smav src, buf, sizeof(buf)), 2282230130Smav nid, i); 2283230130Smav ); 2284230130Smav break; 2285230130Smav } 2286230130Smav } 2287230130Smav return (res); 2288230130Smav} 2289230130Smav 2290230130Smavstatic uint32_t 2291230130Smavhdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src) 2292230130Smav{ 2293230130Smav struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m); 2294230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 2295230130Smav struct hdaa_widget *w; 2296230130Smav struct hdaa_audio_as *as; 2297230451Smav struct hdaa_audio_ctl *ctl; 2298230130Smav struct hdaa_chan *ch; 2299230130Smav int i, j; 2300230130Smav uint32_t ret = 0xffffffff; 2301230130Smav 2302230130Smav hdaa_lock(devinfo); 2303230130Smav if (pdevinfo->recas < 0) { 2304230130Smav hdaa_unlock(devinfo); 2305230130Smav return (0); 2306230130Smav } 2307230130Smav as = &devinfo->as[pdevinfo->recas]; 2308230130Smav 2309230130Smav /* For non-mixed associations we always recording everything. */ 2310230130Smav if (!as->mixed) { 2311230130Smav hdaa_unlock(devinfo); 2312230130Smav return (mix_getrecdevs(m)); 2313230130Smav } 2314230130Smav 2315230130Smav /* Commutate requested recsrc for each ADC. */ 2316230130Smav for (j = 0; j < as->num_chans; j++) { 2317230130Smav ch = &devinfo->chans[as->chans[j]]; 2318230130Smav for (i = 0; ch->io[i] >= 0; i++) { 2319230130Smav w = hdaa_widget_get(devinfo, ch->io[i]); 2320230130Smav if (w == NULL || w->enable == 0) 2321230130Smav continue; 2322230130Smav ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src, 2323230130Smav ch->io[i], 0); 2324230130Smav } 2325230130Smav } 2326230451Smav if (ret == 0xffffffff) 2327230451Smav ret = 0; 2328230130Smav 2329230451Smav /* 2330230451Smav * Some controls could be shared. Reset volumes for controls 2331230451Smav * related to previously chosen devices, as they may no longer 2332230451Smav * affect the signal. 2333230451Smav */ 2334230451Smav i = 0; 2335230451Smav while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) { 2336230451Smav if (ctl->enable == 0 || 2337230451Smav !(ctl->ossmask & pdevinfo->recsrc)) 2338230451Smav continue; 2339230451Smav if (!((pdevinfo->playas >= 0 && 2340230451Smav ctl->widget->bindas == pdevinfo->playas) || 2341230451Smav (pdevinfo->recas >= 0 && 2342230451Smav ctl->widget->bindas == pdevinfo->recas) || 2343230451Smav (pdevinfo->index == 0 && 2344230451Smav ctl->widget->bindas == -2))) 2345230451Smav continue; 2346230451Smav for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) { 2347230451Smav if (pdevinfo->recsrc & (1 << j)) { 2348230451Smav ctl->devleft[j] = 0; 2349230451Smav ctl->devright[j] = 0; 2350230451Smav ctl->devmute[j] = 0; 2351230451Smav } 2352230451Smav } 2353230451Smav } 2354230451Smav 2355230451Smav /* 2356230451Smav * Some controls could be shared. Set volumes for controls 2357230451Smav * related to devices selected both previously and now. 2358230451Smav */ 2359230451Smav for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) { 2360230451Smav if ((ret | pdevinfo->recsrc) & (1 << j)) 2361230451Smav hdaa_audio_ctl_dev_volume(pdevinfo, j); 2362230451Smav } 2363230451Smav 2364230451Smav pdevinfo->recsrc = ret; 2365230130Smav hdaa_unlock(devinfo); 2366230451Smav return (ret); 2367230130Smav} 2368230130Smav 2369230130Smavstatic kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = { 2370230130Smav KOBJMETHOD(mixer_init, hdaa_audio_ctl_ossmixer_init), 2371230130Smav KOBJMETHOD(mixer_set, hdaa_audio_ctl_ossmixer_set), 2372230130Smav KOBJMETHOD(mixer_setrecsrc, hdaa_audio_ctl_ossmixer_setrecsrc), 2373230130Smav KOBJMETHOD_END 2374230130Smav}; 2375230130SmavMIXER_DECLARE(hdaa_audio_ctl_ossmixer); 2376230130Smav 2377230130Smavstatic void 2378230130Smavhdaa_dump_gpi(struct hdaa_devinfo *devinfo) 2379230130Smav{ 2380230130Smav device_t dev = devinfo->dev; 2381230130Smav int i; 2382230130Smav uint32_t data, wake, unsol, sticky; 2383230130Smav 2384230130Smav if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) { 2385230130Smav data = hda_command(dev, 2386230130Smav HDA_CMD_GET_GPI_DATA(0, devinfo->nid)); 2387230130Smav wake = hda_command(dev, 2388230130Smav HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid)); 2389230130Smav unsol = hda_command(dev, 2390230130Smav HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid)); 2391230130Smav sticky = hda_command(dev, 2392230130Smav HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid)); 2393230130Smav for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) { 2394230130Smav device_printf(dev, " GPI%d:%s%s%s state=%d", i, 2395230130Smav (sticky & (1 << i)) ? " sticky" : "", 2396230130Smav (unsol & (1 << i)) ? " unsol" : "", 2397230130Smav (wake & (1 << i)) ? " wake" : "", 2398230130Smav (data >> i) & 1); 2399230130Smav } 2400230130Smav } 2401230130Smav} 2402230130Smav 2403230130Smavstatic void 2404230130Smavhdaa_dump_gpio(struct hdaa_devinfo *devinfo) 2405230130Smav{ 2406230130Smav device_t dev = devinfo->dev; 2407230130Smav int i; 2408230130Smav uint32_t data, dir, enable, wake, unsol, sticky; 2409230130Smav 2410230130Smav if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) { 2411230130Smav data = hda_command(dev, 2412230130Smav HDA_CMD_GET_GPIO_DATA(0, devinfo->nid)); 2413230130Smav enable = hda_command(dev, 2414230130Smav HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid)); 2415230130Smav dir = hda_command(dev, 2416230130Smav HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid)); 2417230130Smav wake = hda_command(dev, 2418230130Smav HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid)); 2419230130Smav unsol = hda_command(dev, 2420230130Smav HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid)); 2421230130Smav sticky = hda_command(dev, 2422230130Smav HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid)); 2423230130Smav for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) { 2424230130Smav device_printf(dev, " GPIO%d: ", i); 2425230130Smav if ((enable & (1 << i)) == 0) { 2426230130Smav printf("disabled\n"); 2427230130Smav continue; 2428230130Smav } 2429230130Smav if ((dir & (1 << i)) == 0) { 2430230130Smav printf("input%s%s%s", 2431230130Smav (sticky & (1 << i)) ? " sticky" : "", 2432230130Smav (unsol & (1 << i)) ? " unsol" : "", 2433230130Smav (wake & (1 << i)) ? " wake" : ""); 2434230130Smav } else 2435230130Smav printf("output"); 2436230130Smav printf(" state=%d\n", (data >> i) & 1); 2437230130Smav } 2438230130Smav } 2439230130Smav} 2440230130Smav 2441230130Smavstatic void 2442230130Smavhdaa_dump_gpo(struct hdaa_devinfo *devinfo) 2443230130Smav{ 2444230130Smav device_t dev = devinfo->dev; 2445230130Smav int i; 2446230130Smav uint32_t data; 2447230130Smav 2448230130Smav if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) { 2449230130Smav data = hda_command(dev, 2450230130Smav HDA_CMD_GET_GPO_DATA(0, devinfo->nid)); 2451230130Smav for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) { 2452230130Smav device_printf(dev, " GPO%d: state=%d", i, 2453230130Smav (data >> i) & 1); 2454230130Smav } 2455230130Smav } 2456230130Smav} 2457230130Smav 2458230130Smavstatic void 2459230130Smavhdaa_audio_parse(struct hdaa_devinfo *devinfo) 2460230130Smav{ 2461230130Smav struct hdaa_widget *w; 2462230130Smav uint32_t res; 2463230130Smav int i; 2464230130Smav nid_t nid; 2465230130Smav 2466230130Smav nid = devinfo->nid; 2467230130Smav 2468230130Smav res = hda_command(devinfo->dev, 2469230130Smav HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT)); 2470230130Smav devinfo->gpio_cap = res; 2471230130Smav 2472230130Smav HDA_BOOTVERBOSE( 2473230130Smav device_printf(devinfo->dev, 2474230130Smav "NumGPIO=%d NumGPO=%d " 2475230130Smav "NumGPI=%d GPIWake=%d GPIUnsol=%d\n", 2476230130Smav HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap), 2477230130Smav HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap), 2478230130Smav HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap), 2479230130Smav HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap), 2480230130Smav HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap)); 2481230130Smav hdaa_dump_gpi(devinfo); 2482230130Smav hdaa_dump_gpio(devinfo); 2483230130Smav hdaa_dump_gpo(devinfo); 2484230130Smav ); 2485230130Smav 2486230130Smav res = hda_command(devinfo->dev, 2487230130Smav HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS)); 2488230130Smav devinfo->supp_stream_formats = res; 2489230130Smav 2490230130Smav res = hda_command(devinfo->dev, 2491230130Smav HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE)); 2492230130Smav devinfo->supp_pcm_size_rate = res; 2493230130Smav 2494230130Smav res = hda_command(devinfo->dev, 2495230130Smav HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP)); 2496230130Smav devinfo->outamp_cap = res; 2497230130Smav 2498230130Smav res = hda_command(devinfo->dev, 2499230130Smav HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP)); 2500230130Smav devinfo->inamp_cap = res; 2501230130Smav 2502230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 2503230130Smav w = hdaa_widget_get(devinfo, i); 2504230130Smav if (w == NULL) 2505230130Smav device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i); 2506230130Smav else { 2507230130Smav w->devinfo = devinfo; 2508230130Smav w->nid = i; 2509230130Smav w->enable = 1; 2510230130Smav w->selconn = -1; 2511230130Smav w->pflags = 0; 2512230130Smav w->ossdev = -1; 2513230130Smav w->bindas = -1; 2514230130Smav w->param.eapdbtl = HDA_INVALID; 2515230130Smav hdaa_widget_parse(w); 2516230130Smav } 2517230130Smav } 2518230130Smav} 2519230130Smav 2520230130Smavstatic void 2521230130Smavhdaa_audio_postprocess(struct hdaa_devinfo *devinfo) 2522230130Smav{ 2523230130Smav struct hdaa_widget *w; 2524230130Smav int i; 2525230130Smav 2526230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 2527230130Smav w = hdaa_widget_get(devinfo, i); 2528230130Smav if (w == NULL) 2529230130Smav continue; 2530230130Smav hdaa_widget_postprocess(w); 2531230130Smav } 2532230130Smav} 2533230130Smav 2534230130Smavstatic void 2535230130Smavhdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo) 2536230130Smav{ 2537230130Smav struct hdaa_audio_ctl *ctls; 2538230130Smav struct hdaa_widget *w, *cw; 2539230130Smav int i, j, cnt, max, ocap, icap; 2540230130Smav int mute, offset, step, size; 2541230130Smav 2542230130Smav /* XXX This is redundant */ 2543230130Smav max = 0; 2544230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 2545230130Smav w = hdaa_widget_get(devinfo, i); 2546230130Smav if (w == NULL || w->enable == 0) 2547230130Smav continue; 2548230130Smav if (w->param.outamp_cap != 0) 2549230130Smav max++; 2550230130Smav if (w->param.inamp_cap != 0) { 2551230130Smav switch (w->type) { 2552230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR: 2553230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER: 2554230130Smav for (j = 0; j < w->nconns; j++) { 2555230130Smav cw = hdaa_widget_get(devinfo, 2556230130Smav w->conns[j]); 2557230130Smav if (cw == NULL || cw->enable == 0) 2558230130Smav continue; 2559230130Smav max++; 2560230130Smav } 2561230130Smav break; 2562230130Smav default: 2563230130Smav max++; 2564230130Smav break; 2565230130Smav } 2566230130Smav } 2567230130Smav } 2568230130Smav devinfo->ctlcnt = max; 2569230130Smav 2570230130Smav if (max < 1) 2571230130Smav return; 2572230130Smav 2573230130Smav ctls = (struct hdaa_audio_ctl *)malloc( 2574230130Smav sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT); 2575230130Smav 2576230130Smav if (ctls == NULL) { 2577230130Smav /* Blekh! */ 2578230130Smav device_printf(devinfo->dev, "unable to allocate ctls!\n"); 2579230130Smav devinfo->ctlcnt = 0; 2580230130Smav return; 2581230130Smav } 2582230130Smav 2583230130Smav cnt = 0; 2584230130Smav for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) { 2585230130Smav if (cnt >= max) { 2586230130Smav device_printf(devinfo->dev, "%s: Ctl overflow!\n", 2587230130Smav __func__); 2588230130Smav break; 2589230130Smav } 2590230130Smav w = hdaa_widget_get(devinfo, i); 2591230130Smav if (w == NULL || w->enable == 0) 2592230130Smav continue; 2593230130Smav ocap = w->param.outamp_cap; 2594230130Smav icap = w->param.inamp_cap; 2595230130Smav if (ocap != 0) { 2596230130Smav mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap); 2597230130Smav step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap); 2598230130Smav size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap); 2599230130Smav offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap); 2600230130Smav /*if (offset > step) { 2601230130Smav HDA_BOOTVERBOSE( 2602230130Smav device_printf(devinfo->dev, 2603230130Smav "BUGGY outamp: nid=%d " 2604230130Smav "[offset=%d > step=%d]\n", 2605230130Smav w->nid, offset, step); 2606230130Smav ); 2607230130Smav offset = step; 2608230130Smav }*/ 2609230130Smav ctls[cnt].enable = 1; 2610230130Smav ctls[cnt].widget = w; 2611230130Smav ctls[cnt].mute = mute; 2612230130Smav ctls[cnt].step = step; 2613230130Smav ctls[cnt].size = size; 2614230130Smav ctls[cnt].offset = offset; 2615230130Smav ctls[cnt].left = offset; 2616230130Smav ctls[cnt].right = offset; 2617230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || 2618230130Smav w->waspin) 2619230130Smav ctls[cnt].ndir = HDAA_CTL_IN; 2620230130Smav else 2621230130Smav ctls[cnt].ndir = HDAA_CTL_OUT; 2622230130Smav ctls[cnt++].dir = HDAA_CTL_OUT; 2623230130Smav } 2624230130Smav 2625230130Smav if (icap != 0) { 2626230130Smav mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap); 2627230130Smav step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap); 2628230130Smav size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap); 2629230130Smav offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap); 2630230130Smav /*if (offset > step) { 2631230130Smav HDA_BOOTVERBOSE( 2632230130Smav device_printf(devinfo->dev, 2633230130Smav "BUGGY inamp: nid=%d " 2634230130Smav "[offset=%d > step=%d]\n", 2635230130Smav w->nid, offset, step); 2636230130Smav ); 2637230130Smav offset = step; 2638230130Smav }*/ 2639230130Smav switch (w->type) { 2640230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR: 2641230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER: 2642230130Smav for (j = 0; j < w->nconns; j++) { 2643230130Smav if (cnt >= max) { 2644230130Smav device_printf(devinfo->dev, 2645230130Smav "%s: Ctl overflow!\n", 2646230130Smav __func__); 2647230130Smav break; 2648230130Smav } 2649230130Smav cw = hdaa_widget_get(devinfo, 2650230130Smav w->conns[j]); 2651230130Smav if (cw == NULL || cw->enable == 0) 2652230130Smav continue; 2653230130Smav ctls[cnt].enable = 1; 2654230130Smav ctls[cnt].widget = w; 2655230130Smav ctls[cnt].childwidget = cw; 2656230130Smav ctls[cnt].index = j; 2657230130Smav ctls[cnt].mute = mute; 2658230130Smav ctls[cnt].step = step; 2659230130Smav ctls[cnt].size = size; 2660230130Smav ctls[cnt].offset = offset; 2661230130Smav ctls[cnt].left = offset; 2662230130Smav ctls[cnt].right = offset; 2663230130Smav ctls[cnt].ndir = HDAA_CTL_IN; 2664230130Smav ctls[cnt++].dir = HDAA_CTL_IN; 2665230130Smav } 2666230130Smav break; 2667230130Smav default: 2668230130Smav if (cnt >= max) { 2669230130Smav device_printf(devinfo->dev, 2670230130Smav "%s: Ctl overflow!\n", 2671230130Smav __func__); 2672230130Smav break; 2673230130Smav } 2674230130Smav ctls[cnt].enable = 1; 2675230130Smav ctls[cnt].widget = w; 2676230130Smav ctls[cnt].mute = mute; 2677230130Smav ctls[cnt].step = step; 2678230130Smav ctls[cnt].size = size; 2679230130Smav ctls[cnt].offset = offset; 2680230130Smav ctls[cnt].left = offset; 2681230130Smav ctls[cnt].right = offset; 2682230130Smav if (w->type == 2683230130Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 2684230130Smav ctls[cnt].ndir = HDAA_CTL_OUT; 2685230130Smav else 2686230130Smav ctls[cnt].ndir = HDAA_CTL_IN; 2687230130Smav ctls[cnt++].dir = HDAA_CTL_IN; 2688230130Smav break; 2689230130Smav } 2690230130Smav } 2691230130Smav } 2692230130Smav 2693230130Smav devinfo->ctl = ctls; 2694230130Smav} 2695230130Smav 2696230130Smavstatic void 2697230130Smavhdaa_audio_as_parse(struct hdaa_devinfo *devinfo) 2698230130Smav{ 2699230130Smav struct hdaa_audio_as *as; 2700230130Smav struct hdaa_widget *w; 2701230130Smav int i, j, cnt, max, type, dir, assoc, seq, first, hpredir; 2702230130Smav 2703230130Smav /* Count present associations */ 2704230130Smav max = 0; 2705230130Smav for (j = 1; j < 16; j++) { 2706230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 2707230130Smav w = hdaa_widget_get(devinfo, i); 2708230130Smav if (w == NULL || w->enable == 0) 2709230130Smav continue; 2710230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 2711230130Smav continue; 2712230130Smav if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config) 2713230130Smav != j) 2714230130Smav continue; 2715230130Smav max++; 2716230130Smav if (j != 15) /* There could be many 1-pin assocs #15 */ 2717230130Smav break; 2718230130Smav } 2719230130Smav } 2720230130Smav 2721230130Smav devinfo->ascnt = max; 2722230130Smav 2723230130Smav if (max < 1) 2724230130Smav return; 2725230130Smav 2726230130Smav as = (struct hdaa_audio_as *)malloc( 2727230130Smav sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT); 2728230130Smav 2729230130Smav if (as == NULL) { 2730230130Smav /* Blekh! */ 2731230130Smav device_printf(devinfo->dev, "unable to allocate assocs!\n"); 2732230130Smav devinfo->ascnt = 0; 2733230130Smav return; 2734230130Smav } 2735230130Smav 2736230130Smav for (i = 0; i < max; i++) { 2737230130Smav as[i].hpredir = -1; 2738230130Smav as[i].digital = 0; 2739230130Smav as[i].num_chans = 1; 2740230130Smav as[i].location = -1; 2741230130Smav } 2742230130Smav 2743230130Smav /* Scan associations skipping as=0. */ 2744230130Smav cnt = 0; 2745230130Smav for (j = 1; j < 16; j++) { 2746230130Smav first = 16; 2747230130Smav hpredir = 0; 2748230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 2749230130Smav w = hdaa_widget_get(devinfo, i); 2750230130Smav if (w == NULL || w->enable == 0) 2751230130Smav continue; 2752230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 2753230130Smav continue; 2754230130Smav assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config); 2755230130Smav seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config); 2756230130Smav if (assoc != j) { 2757230130Smav continue; 2758230130Smav } 2759230130Smav KASSERT(cnt < max, 2760230130Smav ("%s: Associations owerflow (%d of %d)", 2761230130Smav __func__, cnt, max)); 2762230130Smav type = w->wclass.pin.config & 2763230130Smav HDA_CONFIG_DEFAULTCONF_DEVICE_MASK; 2764230130Smav /* Get pin direction. */ 2765230130Smav if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT || 2766230130Smav type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER || 2767230130Smav type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT || 2768230130Smav type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT || 2769230130Smav type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT) 2770230130Smav dir = HDAA_CTL_OUT; 2771230130Smav else 2772230130Smav dir = HDAA_CTL_IN; 2773230130Smav /* If this is a first pin - create new association. */ 2774230130Smav if (as[cnt].pincnt == 0) { 2775230130Smav as[cnt].enable = 1; 2776230130Smav as[cnt].index = j; 2777230130Smav as[cnt].dir = dir; 2778230130Smav } 2779230130Smav if (seq < first) 2780230130Smav first = seq; 2781230130Smav /* Check association correctness. */ 2782230130Smav if (as[cnt].pins[seq] != 0) { 2783230130Smav device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) " 2784230130Smav "in association %d! Disabling association.\n", 2785230130Smav __func__, seq, w->nid, j); 2786230130Smav as[cnt].enable = 0; 2787230130Smav } 2788230130Smav if (dir != as[cnt].dir) { 2789230130Smav device_printf(devinfo->dev, "%s: Pin %d has wrong " 2790230130Smav "direction for association %d! Disabling " 2791230130Smav "association.\n", 2792230130Smav __func__, w->nid, j); 2793230130Smav as[cnt].enable = 0; 2794230130Smav } 2795230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) { 2796230130Smav if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap)) 2797230130Smav as[cnt].digital = 3; 2798230130Smav else if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap)) 2799230130Smav as[cnt].digital = 2; 2800230130Smav else 2801230130Smav as[cnt].digital = 1; 2802230130Smav } 2803230130Smav if (as[cnt].location == -1) { 2804230130Smav as[cnt].location = 2805230130Smav HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config); 2806230130Smav } else if (as[cnt].location != 2807230130Smav HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) { 2808230130Smav as[cnt].location = -2; 2809230130Smav } 2810230130Smav /* Headphones with seq=15 may mean redirection. */ 2811230130Smav if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT && 2812230130Smav seq == 15) 2813230130Smav hpredir = 1; 2814230130Smav as[cnt].pins[seq] = w->nid; 2815230130Smav as[cnt].pincnt++; 2816230130Smav /* Association 15 is a multiple unassociated pins. */ 2817230130Smav if (j == 15) 2818230130Smav cnt++; 2819230130Smav } 2820230130Smav if (j != 15 && as[cnt].pincnt > 0) { 2821230130Smav if (hpredir && as[cnt].pincnt > 1) 2822230130Smav as[cnt].hpredir = first; 2823230130Smav cnt++; 2824230130Smav } 2825230130Smav } 2826230130Smav for (i = 0; i < max; i++) { 2827230130Smav if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 || 2828230130Smav as[i].pins[14] > 0 || as[i].pins[15] > 0)) 2829230130Smav as[i].mixed = 1; 2830230130Smav } 2831230130Smav HDA_BOOTVERBOSE( 2832230130Smav device_printf(devinfo->dev, 2833230130Smav "%d associations found:\n", max); 2834230130Smav for (i = 0; i < max; i++) { 2835230130Smav device_printf(devinfo->dev, 2836230130Smav "Association %d (%d) %s%s:\n", 2837230130Smav i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out", 2838230130Smav as[i].enable?"":" (disabled)"); 2839230130Smav for (j = 0; j < 16; j++) { 2840230130Smav if (as[i].pins[j] == 0) 2841230130Smav continue; 2842230130Smav device_printf(devinfo->dev, 2843230130Smav " Pin nid=%d seq=%d\n", 2844230130Smav as[i].pins[j], j); 2845230130Smav } 2846230130Smav } 2847230130Smav ); 2848230130Smav 2849230130Smav devinfo->as = as; 2850230130Smav} 2851230130Smav 2852230130Smav/* 2853230130Smav * Trace path from DAC to pin. 2854230130Smav */ 2855230130Smavstatic nid_t 2856230130Smavhdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid, 2857230130Smav int dupseq, int min, int only, int depth) 2858230130Smav{ 2859230130Smav struct hdaa_widget *w; 2860230130Smav int i, im = -1; 2861230130Smav nid_t m = 0, ret; 2862230130Smav 2863230130Smav if (depth > HDA_PARSE_MAXDEPTH) 2864230130Smav return (0); 2865230130Smav w = hdaa_widget_get(devinfo, nid); 2866230130Smav if (w == NULL || w->enable == 0) 2867230130Smav return (0); 2868230130Smav HDA_BOOTHVERBOSE( 2869230130Smav if (!only) { 2870230130Smav device_printf(devinfo->dev, 2871230130Smav " %*stracing via nid %d\n", 2872230130Smav depth + 1, "", w->nid); 2873230130Smav } 2874230130Smav ); 2875230130Smav /* Use only unused widgets */ 2876230130Smav if (w->bindas >= 0 && w->bindas != as) { 2877230130Smav HDA_BOOTHVERBOSE( 2878230130Smav if (!only) { 2879230130Smav device_printf(devinfo->dev, 2880230130Smav " %*snid %d busy by association %d\n", 2881230130Smav depth + 1, "", w->nid, w->bindas); 2882230130Smav } 2883230130Smav ); 2884230130Smav return (0); 2885230130Smav } 2886230130Smav if (dupseq < 0) { 2887230130Smav if (w->bindseqmask != 0) { 2888230130Smav HDA_BOOTHVERBOSE( 2889230130Smav if (!only) { 2890230130Smav device_printf(devinfo->dev, 2891230130Smav " %*snid %d busy by seqmask %x\n", 2892230130Smav depth + 1, "", w->nid, w->bindseqmask); 2893230130Smav } 2894230130Smav ); 2895230130Smav return (0); 2896230130Smav } 2897230130Smav } else { 2898230130Smav /* If this is headphones - allow duplicate first pin. */ 2899230130Smav if (w->bindseqmask != 0 && 2900230130Smav (w->bindseqmask & (1 << dupseq)) == 0) { 2901230130Smav HDA_BOOTHVERBOSE( 2902230130Smav device_printf(devinfo->dev, 2903230130Smav " %*snid %d busy by seqmask %x\n", 2904230130Smav depth + 1, "", w->nid, w->bindseqmask); 2905230130Smav ); 2906230130Smav return (0); 2907230130Smav } 2908230130Smav } 2909230130Smav 2910230130Smav switch (w->type) { 2911230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT: 2912230130Smav /* Do not traverse input. AD1988 has digital monitor 2913230130Smav for which we are not ready. */ 2914230130Smav break; 2915230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT: 2916230130Smav /* If we are tracing HP take only dac of first pin. */ 2917230130Smav if ((only == 0 || only == w->nid) && 2918230130Smav (w->nid >= min) && (dupseq < 0 || w->nid == 2919230130Smav devinfo->as[as].dacs[0][dupseq])) 2920230130Smav m = w->nid; 2921230130Smav break; 2922230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX: 2923230130Smav if (depth > 0) 2924230130Smav break; 2925230130Smav /* Fall */ 2926230130Smav default: 2927230130Smav /* Find reachable DACs with smallest nid respecting constraints. */ 2928230130Smav for (i = 0; i < w->nconns; i++) { 2929230130Smav if (w->connsenable[i] == 0) 2930230130Smav continue; 2931230130Smav if (w->selconn != -1 && w->selconn != i) 2932230130Smav continue; 2933230130Smav if ((ret = hdaa_audio_trace_dac(devinfo, as, seq, 2934230130Smav w->conns[i], dupseq, min, only, depth + 1)) != 0) { 2935230130Smav if (m == 0 || ret < m) { 2936230130Smav m = ret; 2937230130Smav im = i; 2938230130Smav } 2939230130Smav if (only || dupseq >= 0) 2940230130Smav break; 2941230130Smav } 2942230130Smav } 2943230130Smav if (im >= 0 && only && ((w->nconns > 1 && 2944230130Smav w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) || 2945230130Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR)) 2946230130Smav w->selconn = im; 2947230130Smav break; 2948230130Smav } 2949230130Smav if (m && only) { 2950230130Smav w->bindas = as; 2951230130Smav w->bindseqmask |= (1 << seq); 2952230130Smav } 2953230130Smav HDA_BOOTHVERBOSE( 2954230130Smav if (!only) { 2955230130Smav device_printf(devinfo->dev, 2956230130Smav " %*snid %d returned %d\n", 2957230130Smav depth + 1, "", w->nid, m); 2958230130Smav } 2959230130Smav ); 2960230130Smav return (m); 2961230130Smav} 2962230130Smav 2963230130Smav/* 2964230130Smav * Trace path from widget to ADC. 2965230130Smav */ 2966230130Smavstatic nid_t 2967230130Smavhdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid, 2968230130Smav int mixed, int min, int only, int depth, int *length, int onlylength) 2969230130Smav{ 2970230130Smav struct hdaa_widget *w, *wc; 2971230130Smav int i, j, im, lm = HDA_PARSE_MAXDEPTH; 2972230130Smav nid_t m = 0, ret; 2973230130Smav 2974230130Smav if (depth > HDA_PARSE_MAXDEPTH) 2975230130Smav return (0); 2976230130Smav w = hdaa_widget_get(devinfo, nid); 2977230130Smav if (w == NULL || w->enable == 0) 2978230130Smav return (0); 2979230130Smav HDA_BOOTHVERBOSE( 2980230130Smav device_printf(devinfo->dev, 2981230130Smav " %*stracing via nid %d\n", 2982230130Smav depth + 1, "", w->nid); 2983230130Smav ); 2984230130Smav /* Use only unused widgets */ 2985230130Smav if (w->bindas >= 0 && w->bindas != as) { 2986230130Smav HDA_BOOTHVERBOSE( 2987230130Smav device_printf(devinfo->dev, 2988230130Smav " %*snid %d busy by association %d\n", 2989230130Smav depth + 1, "", w->nid, w->bindas); 2990230130Smav ); 2991230130Smav return (0); 2992230130Smav } 2993230130Smav if (!mixed && w->bindseqmask != 0) { 2994230130Smav HDA_BOOTHVERBOSE( 2995230130Smav device_printf(devinfo->dev, 2996230130Smav " %*snid %d busy by seqmask %x\n", 2997230130Smav depth + 1, "", w->nid, w->bindseqmask); 2998230130Smav ); 2999230130Smav return (0); 3000230130Smav } 3001230130Smav switch (w->type) { 3002230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT: 3003230130Smav if ((only == 0 || only == w->nid) && (w->nid >= min) && 3004230130Smav (onlylength == 0 || onlylength == depth)) { 3005230130Smav m = w->nid; 3006230130Smav if (length != NULL) 3007230130Smav *length = depth; 3008230130Smav } 3009230130Smav break; 3010230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX: 3011230130Smav if (depth > 0) 3012230130Smav break; 3013230130Smav /* Fall */ 3014230130Smav default: 3015230130Smav /* Try to find reachable ADCs with specified nid. */ 3016230130Smav for (j = devinfo->startnode; j < devinfo->endnode; j++) { 3017230130Smav wc = hdaa_widget_get(devinfo, j); 3018230130Smav if (wc == NULL || wc->enable == 0) 3019230130Smav continue; 3020230130Smav im = -1; 3021230130Smav for (i = 0; i < wc->nconns; i++) { 3022230130Smav if (wc->connsenable[i] == 0) 3023230130Smav continue; 3024230130Smav if (wc->conns[i] != nid) 3025230130Smav continue; 3026230130Smav if ((ret = hdaa_audio_trace_adc(devinfo, as, seq, 3027230130Smav j, mixed, min, only, depth + 1, 3028230130Smav length, onlylength)) != 0) { 3029230130Smav if (m == 0 || ret < m || 3030230130Smav (ret == m && length != NULL && 3031230130Smav *length < lm)) { 3032230130Smav m = ret; 3033230130Smav im = i; 3034230130Smav lm = *length; 3035230130Smav } 3036230130Smav if (only) 3037230130Smav break; 3038230130Smav } 3039230130Smav } 3040230130Smav if (im >= 0 && only && ((wc->nconns > 1 && 3041230130Smav wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) || 3042230130Smav wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR)) 3043230130Smav wc->selconn = im; 3044230130Smav } 3045230130Smav break; 3046230130Smav } 3047230130Smav if (m && only) { 3048230130Smav w->bindas = as; 3049230130Smav w->bindseqmask |= (1 << seq); 3050230130Smav } 3051230130Smav HDA_BOOTHVERBOSE( 3052230130Smav device_printf(devinfo->dev, 3053230130Smav " %*snid %d returned %d\n", 3054230130Smav depth + 1, "", w->nid, m); 3055230130Smav ); 3056230130Smav return (m); 3057230130Smav} 3058230130Smav 3059230130Smav/* 3060230130Smav * Erase trace path of the specified association. 3061230130Smav */ 3062230130Smavstatic void 3063230130Smavhdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq) 3064230130Smav{ 3065230130Smav struct hdaa_widget *w; 3066230130Smav int i; 3067230130Smav 3068230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 3069230130Smav w = hdaa_widget_get(devinfo, i); 3070230130Smav if (w == NULL || w->enable == 0) 3071230130Smav continue; 3072230130Smav if (w->bindas == as) { 3073230130Smav if (seq >= 0) { 3074230130Smav w->bindseqmask &= ~(1 << seq); 3075230130Smav if (w->bindseqmask == 0) { 3076230130Smav w->bindas = -1; 3077230130Smav w->selconn = -1; 3078230130Smav } 3079230130Smav } else { 3080230130Smav w->bindas = -1; 3081230130Smav w->bindseqmask = 0; 3082230130Smav w->selconn = -1; 3083230130Smav } 3084230130Smav } 3085230130Smav } 3086230130Smav} 3087230130Smav 3088230130Smav/* 3089230130Smav * Trace association path from DAC to output 3090230130Smav */ 3091230130Smavstatic int 3092230130Smavhdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq) 3093230130Smav{ 3094230130Smav struct hdaa_audio_as *ases = devinfo->as; 3095230130Smav int i, hpredir; 3096230130Smav nid_t min, res; 3097230130Smav 3098230130Smav /* Find next pin */ 3099230130Smav for (i = seq; i < 16 && ases[as].pins[i] == 0; i++) 3100230130Smav ; 3101230130Smav /* Check if there is no any left. If so - we succeeded. */ 3102230130Smav if (i == 16) 3103230130Smav return (1); 3104230130Smav 3105230130Smav hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1; 3106230130Smav min = 0; 3107230130Smav do { 3108230130Smav HDA_BOOTHVERBOSE( 3109230130Smav device_printf(devinfo->dev, 3110230130Smav " Tracing pin %d with min nid %d", 3111230130Smav ases[as].pins[i], min); 3112230130Smav if (hpredir >= 0) 3113230130Smav printf(" and hpredir %d", hpredir); 3114230130Smav printf("\n"); 3115230130Smav ); 3116230130Smav /* Trace this pin taking min nid into account. */ 3117230130Smav res = hdaa_audio_trace_dac(devinfo, as, i, 3118230130Smav ases[as].pins[i], hpredir, min, 0, 0); 3119230130Smav if (res == 0) { 3120230130Smav /* If we failed - return to previous and redo it. */ 3121230130Smav HDA_BOOTVERBOSE( 3122230130Smav device_printf(devinfo->dev, 3123230130Smav " Unable to trace pin %d seq %d with min " 3124230130Smav "nid %d", 3125230130Smav ases[as].pins[i], i, min); 3126230130Smav if (hpredir >= 0) 3127230130Smav printf(" and hpredir %d", hpredir); 3128230130Smav printf("\n"); 3129230130Smav ); 3130230130Smav return (0); 3131230130Smav } 3132230130Smav HDA_BOOTVERBOSE( 3133230130Smav device_printf(devinfo->dev, 3134230130Smav " Pin %d traced to DAC %d", 3135230130Smav ases[as].pins[i], res); 3136230130Smav if (hpredir >= 0) 3137230130Smav printf(" and hpredir %d", hpredir); 3138230130Smav if (ases[as].fakeredir) 3139230130Smav printf(" with fake redirection"); 3140230130Smav printf("\n"); 3141230130Smav ); 3142230130Smav /* Trace again to mark the path */ 3143230130Smav hdaa_audio_trace_dac(devinfo, as, i, 3144230130Smav ases[as].pins[i], hpredir, min, res, 0); 3145230130Smav ases[as].dacs[0][i] = res; 3146230130Smav /* We succeeded, so call next. */ 3147230130Smav if (hdaa_audio_trace_as_out(devinfo, as, i + 1)) 3148230130Smav return (1); 3149230130Smav /* If next failed, we should retry with next min */ 3150230130Smav hdaa_audio_undo_trace(devinfo, as, i); 3151230130Smav ases[as].dacs[0][i] = 0; 3152230130Smav min = res + 1; 3153230130Smav } while (1); 3154230130Smav} 3155230130Smav 3156230130Smav/* 3157230130Smav * Check equivalency of two DACs. 3158230130Smav */ 3159230130Smavstatic int 3160230130Smavhdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2) 3161230130Smav{ 3162230130Smav struct hdaa_devinfo *devinfo = w1->devinfo; 3163230130Smav struct hdaa_widget *w3; 3164230130Smav int i, j, c1, c2; 3165230130Smav 3166230130Smav if (memcmp(&w1->param, &w2->param, sizeof(w1->param))) 3167230130Smav return (0); 3168230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 3169230130Smav w3 = hdaa_widget_get(devinfo, i); 3170230130Smav if (w3 == NULL || w3->enable == 0) 3171230130Smav continue; 3172230130Smav if (w3->bindas != w1->bindas) 3173230130Smav continue; 3174230130Smav if (w3->nconns == 0) 3175230130Smav continue; 3176230130Smav c1 = c2 = -1; 3177230130Smav for (j = 0; j < w3->nconns; j++) { 3178230130Smav if (w3->connsenable[j] == 0) 3179230130Smav continue; 3180230130Smav if (w3->conns[j] == w1->nid) 3181230130Smav c1 = j; 3182230130Smav if (w3->conns[j] == w2->nid) 3183230130Smav c2 = j; 3184230130Smav } 3185230130Smav if (c1 < 0) 3186230130Smav continue; 3187230130Smav if (c2 < 0) 3188230130Smav return (0); 3189230130Smav if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) 3190230130Smav return (0); 3191230130Smav } 3192230130Smav return (1); 3193230130Smav} 3194230130Smav 3195230130Smav/* 3196230130Smav * Check equivalency of two ADCs. 3197230130Smav */ 3198230130Smavstatic int 3199230130Smavhdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2) 3200230130Smav{ 3201230130Smav struct hdaa_devinfo *devinfo = w1->devinfo; 3202230130Smav struct hdaa_widget *w3, *w4; 3203230130Smav int i; 3204230130Smav 3205230130Smav if (memcmp(&w1->param, &w2->param, sizeof(w1->param))) 3206230130Smav return (0); 3207230130Smav if (w1->nconns != 1 || w2->nconns != 1) 3208230130Smav return (0); 3209230130Smav if (w1->conns[0] == w2->conns[0]) 3210230130Smav return (1); 3211230130Smav w3 = hdaa_widget_get(devinfo, w1->conns[0]); 3212230130Smav if (w3 == NULL || w3->enable == 0) 3213230130Smav return (0); 3214230130Smav w4 = hdaa_widget_get(devinfo, w2->conns[0]); 3215230130Smav if (w4 == NULL || w4->enable == 0) 3216230130Smav return (0); 3217230130Smav if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask) 3218230130Smav return (1); 3219230130Smav if (w4->bindas >= 0) 3220230130Smav return (0); 3221230130Smav if (w3->type != w4->type) 3222230130Smav return (0); 3223230130Smav if (memcmp(&w3->param, &w4->param, sizeof(w3->param))) 3224230130Smav return (0); 3225230130Smav if (w3->nconns != w4->nconns) 3226230130Smav return (0); 3227230130Smav for (i = 0; i < w3->nconns; i++) { 3228230130Smav if (w3->conns[i] != w4->conns[i]) 3229230130Smav return (0); 3230230130Smav } 3231230130Smav return (1); 3232230130Smav} 3233230130Smav 3234230130Smav/* 3235230130Smav * Look for equivalent DAC/ADC to implement second channel. 3236230130Smav */ 3237230130Smavstatic void 3238230130Smavhdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid) 3239230130Smav{ 3240230130Smav struct hdaa_audio_as *as = &devinfo->as[asid]; 3241230130Smav struct hdaa_widget *w1, *w2; 3242230130Smav int i, pos; 3243230130Smav nid_t nid1, nid2; 3244230130Smav 3245230130Smav HDA_BOOTVERBOSE( 3246230130Smav device_printf(devinfo->dev, 3247230130Smav "Looking for additional %sC " 3248230130Smav "for association %d (%d)\n", 3249230130Smav (as->dir == HDAA_CTL_OUT) ? "DA" : "AD", 3250230130Smav asid, as->index); 3251230130Smav ); 3252230130Smav 3253230130Smav /* Find the exisitng DAC position and return if found more the one. */ 3254230130Smav pos = -1; 3255230130Smav for (i = 0; i < 16; i++) { 3256230130Smav if (as->dacs[0][i] <= 0) 3257230130Smav continue; 3258230130Smav if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos]) 3259230130Smav return; 3260230130Smav pos = i; 3261230130Smav } 3262230130Smav 3263230130Smav nid1 = as->dacs[0][pos]; 3264230130Smav w1 = hdaa_widget_get(devinfo, nid1); 3265230130Smav w2 = NULL; 3266230130Smav for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) { 3267230130Smav w2 = hdaa_widget_get(devinfo, nid2); 3268230130Smav if (w2 == NULL || w2->enable == 0) 3269230130Smav continue; 3270230130Smav if (w2->bindas >= 0) 3271230130Smav continue; 3272230130Smav if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) { 3273230130Smav if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) 3274230130Smav continue; 3275230130Smav if (hdaa_audio_dacs_equal(w1, w2)) 3276230130Smav break; 3277230130Smav } else { 3278230130Smav if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) 3279230130Smav continue; 3280230130Smav if (hdaa_audio_adcs_equal(w1, w2)) 3281230130Smav break; 3282230130Smav } 3283230130Smav } 3284230130Smav if (nid2 >= devinfo->endnode) 3285230130Smav return; 3286230130Smav w2->bindas = w1->bindas; 3287230130Smav w2->bindseqmask = w1->bindseqmask; 3288230130Smav if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) { 3289230130Smav HDA_BOOTVERBOSE( 3290230130Smav device_printf(devinfo->dev, 3291230130Smav " ADC %d considered equal to ADC %d\n", nid2, nid1); 3292230130Smav ); 3293230130Smav w1 = hdaa_widget_get(devinfo, w1->conns[0]); 3294230130Smav w2 = hdaa_widget_get(devinfo, w2->conns[0]); 3295230130Smav w2->bindas = w1->bindas; 3296230130Smav w2->bindseqmask = w1->bindseqmask; 3297230130Smav } else { 3298230130Smav HDA_BOOTVERBOSE( 3299230130Smav device_printf(devinfo->dev, 3300230130Smav " DAC %d considered equal to DAC %d\n", nid2, nid1); 3301230130Smav ); 3302230130Smav } 3303230130Smav for (i = 0; i < 16; i++) { 3304230130Smav if (as->dacs[0][i] <= 0) 3305230130Smav continue; 3306230130Smav as->dacs[as->num_chans][i] = nid2; 3307230130Smav } 3308230130Smav as->num_chans++; 3309230130Smav} 3310230130Smav 3311230130Smav/* 3312230130Smav * Trace association path from input to ADC 3313230130Smav */ 3314230130Smavstatic int 3315230130Smavhdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as) 3316230130Smav{ 3317230130Smav struct hdaa_audio_as *ases = devinfo->as; 3318230130Smav struct hdaa_widget *w; 3319230130Smav int i, j, k, length; 3320230130Smav 3321230130Smav for (j = devinfo->startnode; j < devinfo->endnode; j++) { 3322230130Smav w = hdaa_widget_get(devinfo, j); 3323230130Smav if (w == NULL || w->enable == 0) 3324230130Smav continue; 3325230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) 3326230130Smav continue; 3327230130Smav if (w->bindas >= 0 && w->bindas != as) 3328230130Smav continue; 3329230130Smav 3330230130Smav /* Find next pin */ 3331230130Smav for (i = 0; i < 16; i++) { 3332230130Smav if (ases[as].pins[i] == 0) 3333230130Smav continue; 3334230130Smav 3335230130Smav HDA_BOOTHVERBOSE( 3336230130Smav device_printf(devinfo->dev, 3337230130Smav " Tracing pin %d to ADC %d\n", 3338230130Smav ases[as].pins[i], j); 3339230130Smav ); 3340230130Smav /* Trace this pin taking goal into account. */ 3341230130Smav if (hdaa_audio_trace_adc(devinfo, as, i, 3342230130Smav ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) { 3343230130Smav /* If we failed - return to previous and redo it. */ 3344230130Smav HDA_BOOTVERBOSE( 3345230130Smav device_printf(devinfo->dev, 3346230130Smav " Unable to trace pin %d to ADC %d, undo traces\n", 3347230130Smav ases[as].pins[i], j); 3348230130Smav ); 3349230130Smav hdaa_audio_undo_trace(devinfo, as, -1); 3350230130Smav for (k = 0; k < 16; k++) 3351230130Smav ases[as].dacs[0][k] = 0; 3352230130Smav break; 3353230130Smav } 3354230130Smav HDA_BOOTVERBOSE( 3355230130Smav device_printf(devinfo->dev, 3356230130Smav " Pin %d traced to ADC %d\n", 3357230130Smav ases[as].pins[i], j); 3358230130Smav ); 3359230130Smav ases[as].dacs[0][i] = j; 3360230130Smav } 3361230130Smav if (i == 16) 3362230130Smav return (1); 3363230130Smav } 3364230130Smav return (0); 3365230130Smav} 3366230130Smav 3367230130Smav/* 3368230130Smav * Trace association path from input to multiple ADCs 3369230130Smav */ 3370230130Smavstatic int 3371230130Smavhdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq) 3372230130Smav{ 3373230130Smav struct hdaa_audio_as *ases = devinfo->as; 3374230130Smav int i, length; 3375230130Smav nid_t min, res; 3376230130Smav 3377230130Smav /* Find next pin */ 3378230130Smav for (i = seq; i < 16 && ases[as].pins[i] == 0; i++) 3379230130Smav ; 3380230130Smav /* Check if there is no any left. If so - we succeeded. */ 3381230130Smav if (i == 16) 3382230130Smav return (1); 3383230130Smav 3384230130Smav min = 0; 3385230130Smav do { 3386230130Smav HDA_BOOTHVERBOSE( 3387230130Smav device_printf(devinfo->dev, 3388230130Smav " Tracing pin %d with min nid %d", 3389230130Smav ases[as].pins[i], min); 3390230130Smav printf("\n"); 3391230130Smav ); 3392230130Smav /* Trace this pin taking min nid into account. */ 3393230130Smav res = hdaa_audio_trace_adc(devinfo, as, i, 3394230130Smav ases[as].pins[i], 0, min, 0, 0, &length, 0); 3395230130Smav if (res == 0) { 3396230130Smav /* If we failed - return to previous and redo it. */ 3397230130Smav HDA_BOOTVERBOSE( 3398230130Smav device_printf(devinfo->dev, 3399230130Smav " Unable to trace pin %d seq %d with min " 3400230130Smav "nid %d", 3401230130Smav ases[as].pins[i], i, min); 3402230130Smav printf("\n"); 3403230130Smav ); 3404230130Smav return (0); 3405230130Smav } 3406230130Smav HDA_BOOTVERBOSE( 3407230130Smav device_printf(devinfo->dev, 3408230130Smav " Pin %d traced to ADC %d\n", 3409230130Smav ases[as].pins[i], res); 3410230130Smav ); 3411230130Smav /* Trace again to mark the path */ 3412230130Smav hdaa_audio_trace_adc(devinfo, as, i, 3413230130Smav ases[as].pins[i], 0, min, res, 0, &length, length); 3414230130Smav ases[as].dacs[0][i] = res; 3415230130Smav /* We succeeded, so call next. */ 3416230130Smav if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1)) 3417230130Smav return (1); 3418230130Smav /* If next failed, we should retry with next min */ 3419230130Smav hdaa_audio_undo_trace(devinfo, as, i); 3420230130Smav ases[as].dacs[0][i] = 0; 3421230130Smav min = res + 1; 3422230130Smav } while (1); 3423230130Smav} 3424230130Smav 3425230130Smav/* 3426230130Smav * Trace input monitor path from mixer to output association. 3427230130Smav */ 3428230130Smavstatic int 3429230130Smavhdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth) 3430230130Smav{ 3431230130Smav struct hdaa_audio_as *ases = devinfo->as; 3432230130Smav struct hdaa_widget *w, *wc; 3433230130Smav int i, j; 3434230130Smav nid_t res = 0; 3435230130Smav 3436230130Smav if (depth > HDA_PARSE_MAXDEPTH) 3437230130Smav return (0); 3438230130Smav w = hdaa_widget_get(devinfo, nid); 3439230130Smav if (w == NULL || w->enable == 0) 3440230130Smav return (0); 3441230130Smav HDA_BOOTHVERBOSE( 3442230130Smav device_printf(devinfo->dev, 3443230130Smav " %*stracing via nid %d\n", 3444230130Smav depth + 1, "", w->nid); 3445230130Smav ); 3446230130Smav /* Use only unused widgets */ 3447230130Smav if (depth > 0 && w->bindas != -1) { 3448230130Smav if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) { 3449230130Smav HDA_BOOTHVERBOSE( 3450230130Smav device_printf(devinfo->dev, 3451230130Smav " %*snid %d found output association %d\n", 3452230130Smav depth + 1, "", w->nid, w->bindas); 3453230130Smav ); 3454230130Smav if (w->bindas >= 0) 3455230130Smav w->pflags |= HDAA_ADC_MONITOR; 3456230130Smav return (1); 3457230130Smav } else { 3458230130Smav HDA_BOOTHVERBOSE( 3459230130Smav device_printf(devinfo->dev, 3460230130Smav " %*snid %d busy by input association %d\n", 3461230130Smav depth + 1, "", w->nid, w->bindas); 3462230130Smav ); 3463230130Smav return (0); 3464230130Smav } 3465230130Smav } 3466230130Smav 3467230130Smav switch (w->type) { 3468230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT: 3469230130Smav /* Do not traverse input. AD1988 has digital monitor 3470230130Smav for which we are not ready. */ 3471230130Smav break; 3472230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX: 3473230130Smav if (depth > 0) 3474230130Smav break; 3475230130Smav /* Fall */ 3476230130Smav default: 3477230130Smav /* Try to find reachable ADCs with specified nid. */ 3478230130Smav for (j = devinfo->startnode; j < devinfo->endnode; j++) { 3479230130Smav wc = hdaa_widget_get(devinfo, j); 3480230130Smav if (wc == NULL || wc->enable == 0) 3481230130Smav continue; 3482230130Smav for (i = 0; i < wc->nconns; i++) { 3483230130Smav if (wc->connsenable[i] == 0) 3484230130Smav continue; 3485230130Smav if (wc->conns[i] != nid) 3486230130Smav continue; 3487230130Smav if (hdaa_audio_trace_to_out(devinfo, 3488230130Smav j, depth + 1) != 0) { 3489230130Smav res = 1; 3490230130Smav if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR && 3491230130Smav wc->selconn == -1) 3492230130Smav wc->selconn = i; 3493230130Smav } 3494230130Smav } 3495230130Smav } 3496230130Smav break; 3497230130Smav } 3498230130Smav if (res && w->bindas == -1) 3499230130Smav w->bindas = -2; 3500230130Smav 3501230130Smav HDA_BOOTHVERBOSE( 3502230130Smav device_printf(devinfo->dev, 3503230130Smav " %*snid %d returned %d\n", 3504230130Smav depth + 1, "", w->nid, res); 3505230130Smav ); 3506230130Smav return (res); 3507230130Smav} 3508230130Smav 3509230130Smav/* 3510230130Smav * Trace extra associations (beeper, monitor) 3511230130Smav */ 3512230130Smavstatic void 3513230130Smavhdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo) 3514230130Smav{ 3515230130Smav struct hdaa_audio_as *as = devinfo->as; 3516230130Smav struct hdaa_widget *w; 3517230130Smav int j; 3518230130Smav 3519230130Smav /* Input monitor */ 3520230130Smav /* Find mixer associated with input, but supplying signal 3521230130Smav for output associations. Hope it will be input monitor. */ 3522230130Smav HDA_BOOTVERBOSE( 3523230130Smav device_printf(devinfo->dev, 3524230130Smav "Tracing input monitor\n"); 3525230130Smav ); 3526230130Smav for (j = devinfo->startnode; j < devinfo->endnode; j++) { 3527230130Smav w = hdaa_widget_get(devinfo, j); 3528230130Smav if (w == NULL || w->enable == 0) 3529230130Smav continue; 3530230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) 3531230130Smav continue; 3532230130Smav if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN) 3533230130Smav continue; 3534230130Smav HDA_BOOTVERBOSE( 3535230130Smav device_printf(devinfo->dev, 3536230130Smav " Tracing nid %d to out\n", 3537230130Smav j); 3538230130Smav ); 3539230130Smav if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) { 3540230130Smav HDA_BOOTVERBOSE( 3541230130Smav device_printf(devinfo->dev, 3542230130Smav " nid %d is input monitor\n", 3543230130Smav w->nid); 3544230130Smav ); 3545230130Smav w->ossdev = SOUND_MIXER_IMIX; 3546230130Smav } 3547230130Smav } 3548230130Smav 3549230130Smav /* Other inputs monitor */ 3550230130Smav /* Find input pins supplying signal for output associations. 3551230130Smav Hope it will be input monitoring. */ 3552230130Smav HDA_BOOTVERBOSE( 3553230130Smav device_printf(devinfo->dev, 3554230130Smav "Tracing other input monitors\n"); 3555230130Smav ); 3556230130Smav for (j = devinfo->startnode; j < devinfo->endnode; j++) { 3557230130Smav w = hdaa_widget_get(devinfo, j); 3558230130Smav if (w == NULL || w->enable == 0) 3559230130Smav continue; 3560230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 3561230130Smav continue; 3562230130Smav if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN) 3563230130Smav continue; 3564230130Smav HDA_BOOTVERBOSE( 3565230130Smav device_printf(devinfo->dev, 3566230130Smav " Tracing nid %d to out\n", 3567230130Smav j); 3568230130Smav ); 3569230130Smav if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) { 3570230130Smav HDA_BOOTVERBOSE( 3571230130Smav device_printf(devinfo->dev, 3572230130Smav " nid %d is input monitor\n", 3573230130Smav w->nid); 3574230130Smav ); 3575230130Smav } 3576230130Smav } 3577230130Smav 3578230130Smav /* Beeper */ 3579230130Smav HDA_BOOTVERBOSE( 3580230130Smav device_printf(devinfo->dev, 3581230130Smav "Tracing beeper\n"); 3582230130Smav ); 3583230130Smav for (j = devinfo->startnode; j < devinfo->endnode; j++) { 3584230130Smav w = hdaa_widget_get(devinfo, j); 3585230130Smav if (w == NULL || w->enable == 0) 3586230130Smav continue; 3587230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET) 3588230130Smav continue; 3589230130Smav HDA_BOOTHVERBOSE( 3590230130Smav device_printf(devinfo->dev, 3591230130Smav " Tracing nid %d to out\n", 3592230130Smav j); 3593230130Smav ); 3594230130Smav if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) { 3595230130Smav HDA_BOOTVERBOSE( 3596230130Smav device_printf(devinfo->dev, 3597230130Smav " nid %d traced to out\n", 3598230130Smav j); 3599230130Smav ); 3600230130Smav } 3601230130Smav w->bindas = -2; 3602230130Smav } 3603230130Smav} 3604230130Smav 3605230130Smav/* 3606230130Smav * Bind assotiations to PCM channels 3607230130Smav */ 3608230130Smavstatic void 3609230130Smavhdaa_audio_bind_as(struct hdaa_devinfo *devinfo) 3610230130Smav{ 3611230130Smav struct hdaa_audio_as *as = devinfo->as; 3612230130Smav int i, j, cnt = 0, free; 3613230130Smav 3614230130Smav for (j = 0; j < devinfo->ascnt; j++) { 3615230130Smav if (as[j].enable) 3616230130Smav cnt += as[j].num_chans; 3617230130Smav } 3618230130Smav if (devinfo->num_chans == 0) { 3619230130Smav devinfo->chans = (struct hdaa_chan *)malloc( 3620230130Smav sizeof(struct hdaa_chan) * cnt, 3621230130Smav M_HDAA, M_ZERO | M_NOWAIT); 3622230130Smav if (devinfo->chans == NULL) { 3623230130Smav device_printf(devinfo->dev, 3624230130Smav "Channels memory allocation failed!\n"); 3625230130Smav return; 3626230130Smav } 3627230130Smav } else { 3628230130Smav devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans, 3629230130Smav sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt), 3630230130Smav M_HDAA, M_ZERO | M_NOWAIT); 3631230130Smav if (devinfo->chans == NULL) { 3632230130Smav devinfo->num_chans = 0; 3633230130Smav device_printf(devinfo->dev, 3634230130Smav "Channels memory allocation failed!\n"); 3635230130Smav return; 3636230130Smav } 3637230130Smav /* Fixup relative pointers after realloc */ 3638230130Smav for (j = 0; j < devinfo->num_chans; j++) 3639230130Smav devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist; 3640230130Smav } 3641230130Smav free = devinfo->num_chans; 3642230130Smav devinfo->num_chans += cnt; 3643230130Smav 3644230130Smav for (j = free; j < free + cnt; j++) { 3645230130Smav devinfo->chans[j].devinfo = devinfo; 3646230130Smav devinfo->chans[j].as = -1; 3647230130Smav } 3648230130Smav 3649230130Smav /* Assign associations in order of their numbers, */ 3650230130Smav for (j = 0; j < devinfo->ascnt; j++) { 3651230130Smav if (as[j].enable == 0) 3652230130Smav continue; 3653230130Smav for (i = 0; i < as[j].num_chans; i++) { 3654230130Smav devinfo->chans[free].as = j; 3655230130Smav devinfo->chans[free].asindex = i; 3656230130Smav devinfo->chans[free].dir = 3657230130Smav (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY; 3658230130Smav hdaa_pcmchannel_setup(&devinfo->chans[free]); 3659230130Smav as[j].chans[i] = free; 3660230130Smav free++; 3661230130Smav } 3662230130Smav } 3663230130Smav} 3664230130Smav 3665230130Smavstatic void 3666230130Smavhdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo) 3667230130Smav{ 3668230130Smav struct hdaa_widget *w; 3669230130Smav int i; 3670230130Smav 3671230130Smav /* Disable power and volume widgets. */ 3672230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 3673230130Smav w = hdaa_widget_get(devinfo, i); 3674230130Smav if (w == NULL || w->enable == 0) 3675230130Smav continue; 3676230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET || 3677230130Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) { 3678230130Smav w->enable = 0; 3679230130Smav HDA_BOOTHVERBOSE( 3680230130Smav device_printf(devinfo->dev, 3681230130Smav " Disabling nid %d due to it's" 3682230130Smav " non-audio type.\n", 3683230130Smav w->nid); 3684230130Smav ); 3685230130Smav } 3686230130Smav } 3687230130Smav} 3688230130Smav 3689230130Smavstatic void 3690230130Smavhdaa_audio_disable_useless(struct hdaa_devinfo *devinfo) 3691230130Smav{ 3692230130Smav struct hdaa_widget *w, *cw; 3693230130Smav struct hdaa_audio_ctl *ctl; 3694230130Smav int done, found, i, j, k; 3695230130Smav 3696230130Smav /* Disable useless pins. */ 3697230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 3698230130Smav w = hdaa_widget_get(devinfo, i); 3699230130Smav if (w == NULL || w->enable == 0) 3700230130Smav continue; 3701230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) { 3702230130Smav if ((w->wclass.pin.config & 3703230130Smav HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) == 3704230130Smav HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) { 3705230130Smav w->enable = 0; 3706230130Smav HDA_BOOTHVERBOSE( 3707230130Smav device_printf(devinfo->dev, 3708230130Smav " Disabling pin nid %d due" 3709230130Smav " to None connectivity.\n", 3710230130Smav w->nid); 3711230130Smav ); 3712230130Smav } else if ((w->wclass.pin.config & 3713230130Smav HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) { 3714230130Smav w->enable = 0; 3715230130Smav HDA_BOOTHVERBOSE( 3716230130Smav device_printf(devinfo->dev, 3717230130Smav " Disabling unassociated" 3718230130Smav " pin nid %d.\n", 3719230130Smav w->nid); 3720230130Smav ); 3721230130Smav } 3722230130Smav } 3723230130Smav } 3724230130Smav do { 3725230130Smav done = 1; 3726230130Smav /* Disable and mute controls for disabled widgets. */ 3727230130Smav i = 0; 3728230130Smav while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) { 3729230130Smav if (ctl->enable == 0) 3730230130Smav continue; 3731230130Smav if (ctl->widget->enable == 0 || 3732230130Smav (ctl->childwidget != NULL && 3733230130Smav ctl->childwidget->enable == 0)) { 3734230130Smav ctl->forcemute = 1; 3735230130Smav ctl->muted = HDAA_AMP_MUTE_ALL; 3736230130Smav ctl->left = 0; 3737230130Smav ctl->right = 0; 3738230130Smav ctl->enable = 0; 3739230130Smav if (ctl->ndir == HDAA_CTL_IN) 3740230130Smav ctl->widget->connsenable[ctl->index] = 0; 3741230130Smav done = 0; 3742230130Smav HDA_BOOTHVERBOSE( 3743230130Smav device_printf(devinfo->dev, 3744230130Smav " Disabling ctl %d nid %d cnid %d due" 3745230130Smav " to disabled widget.\n", i, 3746230130Smav ctl->widget->nid, 3747230130Smav (ctl->childwidget != NULL)? 3748230130Smav ctl->childwidget->nid:-1); 3749230130Smav ); 3750230130Smav } 3751230130Smav } 3752230130Smav /* Disable useless widgets. */ 3753230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 3754230130Smav w = hdaa_widget_get(devinfo, i); 3755230130Smav if (w == NULL || w->enable == 0) 3756230130Smav continue; 3757230130Smav /* Disable inputs with disabled child widgets. */ 3758230130Smav for (j = 0; j < w->nconns; j++) { 3759230130Smav if (w->connsenable[j]) { 3760230130Smav cw = hdaa_widget_get(devinfo, w->conns[j]); 3761230130Smav if (cw == NULL || cw->enable == 0) { 3762230130Smav w->connsenable[j] = 0; 3763230130Smav HDA_BOOTHVERBOSE( 3764230130Smav device_printf(devinfo->dev, 3765230130Smav " Disabling nid %d connection %d due" 3766230130Smav " to disabled child widget.\n", 3767230130Smav i, j); 3768230130Smav ); 3769230130Smav } 3770230130Smav } 3771230130Smav } 3772230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR && 3773230130Smav w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) 3774230130Smav continue; 3775230130Smav /* Disable mixers and selectors without inputs. */ 3776230130Smav found = 0; 3777230130Smav for (j = 0; j < w->nconns; j++) { 3778230130Smav if (w->connsenable[j]) { 3779230130Smav found = 1; 3780230130Smav break; 3781230130Smav } 3782230130Smav } 3783230130Smav if (found == 0) { 3784230130Smav w->enable = 0; 3785230130Smav done = 0; 3786230130Smav HDA_BOOTHVERBOSE( 3787230130Smav device_printf(devinfo->dev, 3788230130Smav " Disabling nid %d due to all it's" 3789230130Smav " inputs disabled.\n", w->nid); 3790230130Smav ); 3791230130Smav } 3792230130Smav /* Disable nodes without consumers. */ 3793230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR && 3794230130Smav w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) 3795230130Smav continue; 3796230130Smav found = 0; 3797230130Smav for (k = devinfo->startnode; k < devinfo->endnode; k++) { 3798230130Smav cw = hdaa_widget_get(devinfo, k); 3799230130Smav if (cw == NULL || cw->enable == 0) 3800230130Smav continue; 3801230130Smav for (j = 0; j < cw->nconns; j++) { 3802230130Smav if (cw->connsenable[j] && cw->conns[j] == i) { 3803230130Smav found = 1; 3804230130Smav break; 3805230130Smav } 3806230130Smav } 3807230130Smav } 3808230130Smav if (found == 0) { 3809230130Smav w->enable = 0; 3810230130Smav done = 0; 3811230130Smav HDA_BOOTHVERBOSE( 3812230130Smav device_printf(devinfo->dev, 3813230130Smav " Disabling nid %d due to all it's" 3814230130Smav " consumers disabled.\n", w->nid); 3815230130Smav ); 3816230130Smav } 3817230130Smav } 3818230130Smav } while (done == 0); 3819230130Smav 3820230130Smav} 3821230130Smav 3822230130Smavstatic void 3823230130Smavhdaa_audio_disable_unas(struct hdaa_devinfo *devinfo) 3824230130Smav{ 3825230130Smav struct hdaa_audio_as *as = devinfo->as; 3826230130Smav struct hdaa_widget *w, *cw; 3827230130Smav struct hdaa_audio_ctl *ctl; 3828230130Smav int i, j, k; 3829230130Smav 3830230130Smav /* Disable unassosiated widgets. */ 3831230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 3832230130Smav w = hdaa_widget_get(devinfo, i); 3833230130Smav if (w == NULL || w->enable == 0) 3834230130Smav continue; 3835230130Smav if (w->bindas == -1) { 3836230130Smav w->enable = 0; 3837230130Smav HDA_BOOTHVERBOSE( 3838230130Smav device_printf(devinfo->dev, 3839230130Smav " Disabling unassociated nid %d.\n", 3840230130Smav w->nid); 3841230130Smav ); 3842230130Smav } 3843230130Smav } 3844230130Smav /* Disable input connections on input pin and 3845230130Smav * output on output. */ 3846230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 3847230130Smav w = hdaa_widget_get(devinfo, i); 3848230130Smav if (w == NULL || w->enable == 0) 3849230130Smav continue; 3850230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 3851230130Smav continue; 3852230130Smav if (w->bindas < 0) 3853230130Smav continue; 3854230130Smav if (as[w->bindas].dir == HDAA_CTL_IN) { 3855230130Smav for (j = 0; j < w->nconns; j++) { 3856230130Smav if (w->connsenable[j] == 0) 3857230130Smav continue; 3858230130Smav w->connsenable[j] = 0; 3859230130Smav HDA_BOOTHVERBOSE( 3860230130Smav device_printf(devinfo->dev, 3861230130Smav " Disabling connection to input pin " 3862230130Smav "nid %d conn %d.\n", 3863230130Smav i, j); 3864230130Smav ); 3865230130Smav } 3866230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, 3867230130Smav HDAA_CTL_IN, -1, 1); 3868230130Smav if (ctl && ctl->enable) { 3869230130Smav ctl->forcemute = 1; 3870230130Smav ctl->muted = HDAA_AMP_MUTE_ALL; 3871230130Smav ctl->left = 0; 3872230130Smav ctl->right = 0; 3873230130Smav ctl->enable = 0; 3874230130Smav } 3875230130Smav } else { 3876230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, 3877230130Smav HDAA_CTL_OUT, -1, 1); 3878230130Smav if (ctl && ctl->enable) { 3879230130Smav ctl->forcemute = 1; 3880230130Smav ctl->muted = HDAA_AMP_MUTE_ALL; 3881230130Smav ctl->left = 0; 3882230130Smav ctl->right = 0; 3883230130Smav ctl->enable = 0; 3884230130Smav } 3885230130Smav for (k = devinfo->startnode; k < devinfo->endnode; k++) { 3886230130Smav cw = hdaa_widget_get(devinfo, k); 3887230130Smav if (cw == NULL || cw->enable == 0) 3888230130Smav continue; 3889230130Smav for (j = 0; j < cw->nconns; j++) { 3890230130Smav if (cw->connsenable[j] && cw->conns[j] == i) { 3891230130Smav cw->connsenable[j] = 0; 3892230130Smav HDA_BOOTHVERBOSE( 3893230130Smav device_printf(devinfo->dev, 3894230130Smav " Disabling connection from output pin " 3895230130Smav "nid %d conn %d cnid %d.\n", 3896230130Smav k, j, i); 3897230130Smav ); 3898230130Smav if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX && 3899230130Smav cw->nconns > 1) 3900230130Smav continue; 3901230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, k, 3902230130Smav HDAA_CTL_IN, j, 1); 3903230130Smav if (ctl && ctl->enable) { 3904230130Smav ctl->forcemute = 1; 3905230130Smav ctl->muted = HDAA_AMP_MUTE_ALL; 3906230130Smav ctl->left = 0; 3907230130Smav ctl->right = 0; 3908230130Smav ctl->enable = 0; 3909230130Smav } 3910230130Smav } 3911230130Smav } 3912230130Smav } 3913230130Smav } 3914230130Smav } 3915230130Smav} 3916230130Smav 3917230130Smavstatic void 3918230130Smavhdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo) 3919230130Smav{ 3920230130Smav struct hdaa_audio_as *as = devinfo->as; 3921230130Smav struct hdaa_widget *w; 3922230130Smav int i, j; 3923230130Smav 3924230130Smav /* On playback path we can safely disable all unseleted inputs. */ 3925230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 3926230130Smav w = hdaa_widget_get(devinfo, i); 3927230130Smav if (w == NULL || w->enable == 0) 3928230130Smav continue; 3929230130Smav if (w->nconns <= 1) 3930230130Smav continue; 3931230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) 3932230130Smav continue; 3933230130Smav if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN) 3934230130Smav continue; 3935230130Smav for (j = 0; j < w->nconns; j++) { 3936230130Smav if (w->connsenable[j] == 0) 3937230130Smav continue; 3938230130Smav if (w->selconn < 0 || w->selconn == j) 3939230130Smav continue; 3940230130Smav w->connsenable[j] = 0; 3941230130Smav HDA_BOOTHVERBOSE( 3942230130Smav device_printf(devinfo->dev, 3943230130Smav " Disabling unselected connection " 3944230130Smav "nid %d conn %d.\n", 3945230130Smav i, j); 3946230130Smav ); 3947230130Smav } 3948230130Smav } 3949230130Smav} 3950230130Smav 3951230130Smavstatic void 3952230130Smavhdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo) 3953230130Smav{ 3954230130Smav struct hdaa_audio_as *ases = devinfo->as; 3955230130Smav struct hdaa_widget *w, *cw; 3956230130Smav struct hdaa_audio_ctl *ctl; 3957230130Smav int i, j; 3958230130Smav 3959230130Smav /* Disable crossassociatement and unwanted crosschannel connections. */ 3960230130Smav /* ... using selectors */ 3961230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 3962230130Smav w = hdaa_widget_get(devinfo, i); 3963230130Smav if (w == NULL || w->enable == 0) 3964230130Smav continue; 3965230130Smav if (w->nconns <= 1) 3966230130Smav continue; 3967230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) 3968230130Smav continue; 3969230130Smav /* Allow any -> mix */ 3970230130Smav if (w->bindas == -2) 3971230130Smav continue; 3972230130Smav for (j = 0; j < w->nconns; j++) { 3973230130Smav if (w->connsenable[j] == 0) 3974230130Smav continue; 3975230130Smav cw = hdaa_widget_get(devinfo, w->conns[j]); 3976230130Smav if (cw == NULL || w->enable == 0) 3977230130Smav continue; 3978230130Smav /* Allow mix -> out. */ 3979230130Smav if (cw->bindas == -2 && w->bindas >= 0 && 3980230130Smav ases[w->bindas].dir == HDAA_CTL_OUT) 3981230130Smav continue; 3982230130Smav /* Allow mix -> mixed-in. */ 3983230130Smav if (cw->bindas == -2 && w->bindas >= 0 && 3984230130Smav ases[w->bindas].mixed) 3985230130Smav continue; 3986230130Smav /* Allow in -> mix. */ 3987230130Smav if ((w->pflags & HDAA_ADC_MONITOR) && 3988230130Smav cw->bindas >= 0 && 3989230130Smav ases[cw->bindas].dir == HDAA_CTL_IN) 3990230130Smav continue; 3991230130Smav /* Allow if have common as/seqs. */ 3992230130Smav if (w->bindas == cw->bindas && 3993230130Smav (w->bindseqmask & cw->bindseqmask) != 0) 3994230130Smav continue; 3995230130Smav w->connsenable[j] = 0; 3996230130Smav HDA_BOOTHVERBOSE( 3997230130Smav device_printf(devinfo->dev, 3998230130Smav " Disabling crossassociatement connection " 3999230130Smav "nid %d conn %d cnid %d.\n", 4000230130Smav i, j, cw->nid); 4001230130Smav ); 4002230130Smav } 4003230130Smav } 4004230130Smav /* ... using controls */ 4005230130Smav i = 0; 4006230130Smav while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) { 4007230130Smav if (ctl->enable == 0 || ctl->childwidget == NULL) 4008230130Smav continue; 4009230130Smav /* Allow any -> mix */ 4010230130Smav if (ctl->widget->bindas == -2) 4011230130Smav continue; 4012230130Smav /* Allow mix -> out. */ 4013230130Smav if (ctl->childwidget->bindas == -2 && 4014230130Smav ctl->widget->bindas >= 0 && 4015230130Smav ases[ctl->widget->bindas].dir == HDAA_CTL_OUT) 4016230130Smav continue; 4017230130Smav /* Allow mix -> mixed-in. */ 4018230130Smav if (ctl->childwidget->bindas == -2 && 4019230130Smav ctl->widget->bindas >= 0 && 4020230130Smav ases[ctl->widget->bindas].mixed) 4021230130Smav continue; 4022230130Smav /* Allow in -> mix. */ 4023230130Smav if ((ctl->widget->pflags & HDAA_ADC_MONITOR) && 4024230130Smav ctl->childwidget->bindas >= 0 && 4025230130Smav ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN) 4026230130Smav continue; 4027230130Smav /* Allow if have common as/seqs. */ 4028230130Smav if (ctl->widget->bindas == ctl->childwidget->bindas && 4029230130Smav (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0) 4030230130Smav continue; 4031230130Smav ctl->forcemute = 1; 4032230130Smav ctl->muted = HDAA_AMP_MUTE_ALL; 4033230130Smav ctl->left = 0; 4034230130Smav ctl->right = 0; 4035230130Smav ctl->enable = 0; 4036230130Smav if (ctl->ndir == HDAA_CTL_IN) 4037230130Smav ctl->widget->connsenable[ctl->index] = 0; 4038230130Smav HDA_BOOTHVERBOSE( 4039230130Smav device_printf(devinfo->dev, 4040230130Smav " Disabling crossassociatement connection " 4041230130Smav "ctl %d nid %d cnid %d.\n", i, 4042230130Smav ctl->widget->nid, 4043230130Smav ctl->childwidget->nid); 4044230130Smav ); 4045230130Smav } 4046230130Smav 4047230130Smav} 4048230130Smav 4049230130Smav/* 4050230451Smav * Find controls to control amplification for source and calculate possible 4051230451Smav * amplification range. 4052230130Smav */ 4053230130Smavstatic int 4054230130Smavhdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index, 4055230451Smav int ossdev, int ctlable, int depth, int *minamp, int *maxamp) 4056230130Smav{ 4057230130Smav struct hdaa_widget *w, *wc; 4058230130Smav struct hdaa_audio_ctl *ctl; 4059230451Smav int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0; 4060230130Smav 4061230130Smav if (depth > HDA_PARSE_MAXDEPTH) 4062230451Smav return (found); 4063230130Smav 4064230130Smav w = hdaa_widget_get(devinfo, nid); 4065230130Smav if (w == NULL || w->enable == 0) 4066230451Smav return (found); 4067230130Smav 4068230130Smav /* Count number of active inputs. */ 4069230130Smav if (depth > 0) { 4070230130Smav for (j = 0; j < w->nconns; j++) { 4071230451Smav if (!w->connsenable[j]) 4072230451Smav continue; 4073230451Smav conns++; 4074230130Smav } 4075230130Smav } 4076230130Smav 4077230130Smav /* If this is not a first step - use input mixer. 4078230130Smav Pins have common input ctl so care must be taken. */ 4079230130Smav if (depth > 0 && ctlable && (conns == 1 || 4080230130Smav w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) { 4081230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN, 4082230130Smav index, 1); 4083230130Smav if (ctl) { 4084230451Smav ctl->ossmask |= (1 << ossdev); 4085230451Smav found++; 4086230451Smav if (*minamp == *maxamp) { 4087230451Smav *minamp += MINQDB(ctl); 4088230451Smav *maxamp += MAXQDB(ctl); 4089230451Smav } 4090230130Smav } 4091230130Smav } 4092230130Smav 4093230130Smav /* If widget has own ossdev - not traverse it. 4094230130Smav It will be traversed on it's own. */ 4095230130Smav if (w->ossdev >= 0 && depth > 0) 4096230451Smav return (found); 4097230130Smav 4098230130Smav /* We must not traverse pin */ 4099230130Smav if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT || 4100230130Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) && 4101230130Smav depth > 0) 4102230451Smav return (found); 4103230130Smav 4104230130Smav /* record that this widget exports such signal, */ 4105230130Smav w->ossmask |= (1 << ossdev); 4106230130Smav 4107230451Smav /* 4108230451Smav * If signals mixed, we can't assign controls farther. 4109230130Smav * Ignore this on depth zero. Caller must knows why. 4110230130Smav */ 4111230451Smav if (conns > 1 && 4112230451Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) 4113230130Smav ctlable = 0; 4114230130Smav 4115230130Smav if (ctlable) { 4116230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1); 4117230130Smav if (ctl) { 4118230451Smav ctl->ossmask |= (1 << ossdev); 4119230451Smav found++; 4120230451Smav if (*minamp == *maxamp) { 4121230451Smav *minamp += MINQDB(ctl); 4122230451Smav *maxamp += MAXQDB(ctl); 4123230451Smav } 4124230130Smav } 4125230130Smav } 4126230130Smav 4127230451Smav cminamp = cmaxamp = 0; 4128230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 4129230130Smav wc = hdaa_widget_get(devinfo, i); 4130230130Smav if (wc == NULL || wc->enable == 0) 4131230130Smav continue; 4132230130Smav for (j = 0; j < wc->nconns; j++) { 4133230130Smav if (wc->connsenable[j] && wc->conns[j] == nid) { 4134230451Smav tminamp = tmaxamp = 0; 4135230451Smav found += hdaa_audio_ctl_source_amp(devinfo, 4136230451Smav wc->nid, j, ossdev, ctlable, depth + 1, 4137230451Smav &tminamp, &tmaxamp); 4138230451Smav if (cminamp == 0 && cmaxamp == 0) { 4139230451Smav cminamp = tminamp; 4140230451Smav cmaxamp = tmaxamp; 4141230451Smav } else if (tminamp != tmaxamp) { 4142230451Smav cminamp = imax(cminamp, tminamp); 4143230451Smav cmaxamp = imin(cmaxamp, tmaxamp); 4144230451Smav } 4145230130Smav } 4146230130Smav } 4147230130Smav } 4148230451Smav if (*minamp == *maxamp && cminamp < cmaxamp) { 4149230451Smav *minamp += cminamp; 4150230451Smav *maxamp += cmaxamp; 4151230451Smav } 4152230451Smav return (found); 4153230130Smav} 4154230130Smav 4155230130Smav/* 4156230451Smav * Find controls to control amplification for destination and calculate 4157230451Smav * possible amplification range. 4158230130Smav */ 4159230451Smavstatic int 4160230130Smavhdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index, 4161230451Smav int ossdev, int depth, int *minamp, int *maxamp) 4162230130Smav{ 4163230130Smav struct hdaa_audio_as *as = devinfo->as; 4164230130Smav struct hdaa_widget *w, *wc; 4165230130Smav struct hdaa_audio_ctl *ctl; 4166230451Smav int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0; 4167230130Smav 4168230130Smav if (depth > HDA_PARSE_MAXDEPTH) 4169230451Smav return (found); 4170230130Smav 4171230130Smav w = hdaa_widget_get(devinfo, nid); 4172230130Smav if (w == NULL || w->enable == 0) 4173230451Smav return (found); 4174230130Smav 4175230130Smav if (depth > 0) { 4176230130Smav /* If this node produce output for several consumers, 4177230130Smav we can't touch it. */ 4178230130Smav consumers = 0; 4179230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 4180230130Smav wc = hdaa_widget_get(devinfo, i); 4181230130Smav if (wc == NULL || wc->enable == 0) 4182230130Smav continue; 4183230130Smav for (j = 0; j < wc->nconns; j++) { 4184230130Smav if (wc->connsenable[j] && wc->conns[j] == nid) 4185230130Smav consumers++; 4186230130Smav } 4187230130Smav } 4188230130Smav /* The only exception is if real HP redirection is configured 4189230130Smav and this is a duplication point. 4190230130Smav XXX: Actually exception is not completely correct. 4191230130Smav XXX: Duplication point check is not perfect. */ 4192230130Smav if ((consumers == 2 && (w->bindas < 0 || 4193230130Smav as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir || 4194230130Smav (w->bindseqmask & (1 << 15)) == 0)) || 4195230130Smav consumers > 2) 4196230451Smav return (found); 4197230130Smav 4198230130Smav /* Else use it's output mixer. */ 4199230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, 4200230130Smav HDAA_CTL_OUT, -1, 1); 4201230130Smav if (ctl) { 4202230451Smav ctl->ossmask |= (1 << ossdev); 4203230451Smav found++; 4204230451Smav if (*minamp == *maxamp) { 4205230451Smav *minamp += MINQDB(ctl); 4206230451Smav *maxamp += MAXQDB(ctl); 4207230451Smav } 4208230130Smav } 4209230130Smav } 4210230130Smav 4211230130Smav /* We must not traverse pin */ 4212230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX && 4213230130Smav depth > 0) 4214230451Smav return (found); 4215230130Smav 4216230451Smav cminamp = cmaxamp = 0; 4217230130Smav for (i = 0; i < w->nconns; i++) { 4218230130Smav if (w->connsenable[i] == 0) 4219230130Smav continue; 4220230130Smav if (index >= 0 && i != index) 4221230130Smav continue; 4222230451Smav tminamp = tmaxamp = 0; 4223230130Smav ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, 4224230130Smav HDAA_CTL_IN, i, 1); 4225230130Smav if (ctl) { 4226230451Smav ctl->ossmask |= (1 << ossdev); 4227230451Smav found++; 4228230451Smav if (*minamp == *maxamp) { 4229230451Smav tminamp += MINQDB(ctl); 4230230451Smav tmaxamp += MAXQDB(ctl); 4231230451Smav } 4232230130Smav } 4233230451Smav found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev, 4234230451Smav depth + 1, &tminamp, &tmaxamp); 4235230451Smav if (cminamp == 0 && cmaxamp == 0) { 4236230451Smav cminamp = tminamp; 4237230451Smav cmaxamp = tmaxamp; 4238230451Smav } else if (tminamp != tmaxamp) { 4239230451Smav cminamp = imax(cminamp, tminamp); 4240230451Smav cmaxamp = imin(cmaxamp, tmaxamp); 4241230451Smav } 4242230130Smav } 4243230451Smav if (*minamp == *maxamp && cminamp < cmaxamp) { 4244230451Smav *minamp += cminamp; 4245230451Smav *maxamp += cmaxamp; 4246230451Smav } 4247230451Smav return (found); 4248230130Smav} 4249230130Smav 4250230130Smav/* 4251230130Smav * Assign OSS names to sound sources 4252230130Smav */ 4253230130Smavstatic void 4254230130Smavhdaa_audio_assign_names(struct hdaa_devinfo *devinfo) 4255230130Smav{ 4256230130Smav struct hdaa_audio_as *as = devinfo->as; 4257230130Smav struct hdaa_widget *w; 4258230130Smav int i, j; 4259230130Smav int type = -1, use, used = 0; 4260230130Smav static const int types[7][13] = { 4261230130Smav { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2, 4262230130Smav SOUND_MIXER_LINE3, -1 }, /* line */ 4263230130Smav { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */ 4264230130Smav { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */ 4265230130Smav { SOUND_MIXER_CD, -1 }, /* cd */ 4266230130Smav { SOUND_MIXER_SPEAKER, -1 }, /* speaker */ 4267230130Smav { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, 4268230130Smav -1 }, /* digital */ 4269230130Smav { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2, 4270230130Smav SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT, 4271230130Smav SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1, 4272230130Smav SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR, 4273230130Smav -1 } /* others */ 4274230130Smav }; 4275230130Smav 4276230130Smav /* Surely known names */ 4277230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 4278230130Smav w = hdaa_widget_get(devinfo, i); 4279230130Smav if (w == NULL || w->enable == 0) 4280230130Smav continue; 4281230130Smav if (w->bindas == -1) 4282230130Smav continue; 4283230130Smav use = -1; 4284230130Smav switch (w->type) { 4285230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX: 4286230130Smav if (as[w->bindas].dir == HDAA_CTL_OUT) 4287230130Smav break; 4288230130Smav type = -1; 4289230130Smav switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) { 4290230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN: 4291230130Smav type = 0; 4292230130Smav break; 4293230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN: 4294230130Smav if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) 4295230130Smav == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK) 4296230130Smav break; 4297230130Smav type = 1; 4298230130Smav break; 4299230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_CD: 4300230130Smav type = 3; 4301230130Smav break; 4302230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER: 4303230130Smav type = 4; 4304230130Smav break; 4305230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN: 4306230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN: 4307230130Smav type = 5; 4308230130Smav break; 4309230130Smav } 4310230130Smav if (type == -1) 4311230130Smav break; 4312230130Smav j = 0; 4313230130Smav while (types[type][j] >= 0 && 4314230130Smav (used & (1 << types[type][j])) != 0) { 4315230130Smav j++; 4316230130Smav } 4317230130Smav if (types[type][j] >= 0) 4318230130Smav use = types[type][j]; 4319230130Smav break; 4320230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT: 4321230130Smav use = SOUND_MIXER_PCM; 4322230130Smav break; 4323230130Smav case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET: 4324230130Smav use = SOUND_MIXER_SPEAKER; 4325230130Smav break; 4326230130Smav default: 4327230130Smav break; 4328230130Smav } 4329230130Smav if (use >= 0) { 4330230130Smav w->ossdev = use; 4331230130Smav used |= (1 << use); 4332230130Smav } 4333230130Smav } 4334230130Smav /* Semi-known names */ 4335230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 4336230130Smav w = hdaa_widget_get(devinfo, i); 4337230130Smav if (w == NULL || w->enable == 0) 4338230130Smav continue; 4339230130Smav if (w->ossdev >= 0) 4340230130Smav continue; 4341230130Smav if (w->bindas == -1) 4342230130Smav continue; 4343230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 4344230130Smav continue; 4345230130Smav if (as[w->bindas].dir == HDAA_CTL_OUT) 4346230130Smav continue; 4347230130Smav type = -1; 4348230130Smav switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) { 4349230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT: 4350230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER: 4351230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT: 4352230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX: 4353230130Smav type = 0; 4354230130Smav break; 4355230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN: 4356230130Smav type = 2; 4357230130Smav break; 4358230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT: 4359230130Smav case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT: 4360230130Smav type = 5; 4361230130Smav break; 4362230130Smav } 4363230130Smav if (type == -1) 4364230130Smav break; 4365230130Smav j = 0; 4366230130Smav while (types[type][j] >= 0 && 4367230130Smav (used & (1 << types[type][j])) != 0) { 4368230130Smav j++; 4369230130Smav } 4370230130Smav if (types[type][j] >= 0) { 4371230130Smav w->ossdev = types[type][j]; 4372230130Smav used |= (1 << types[type][j]); 4373230130Smav } 4374230130Smav } 4375230130Smav /* Others */ 4376230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 4377230130Smav w = hdaa_widget_get(devinfo, i); 4378230130Smav if (w == NULL || w->enable == 0) 4379230130Smav continue; 4380230130Smav if (w->ossdev >= 0) 4381230130Smav continue; 4382230130Smav if (w->bindas == -1) 4383230130Smav continue; 4384230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 4385230130Smav continue; 4386230130Smav if (as[w->bindas].dir == HDAA_CTL_OUT) 4387230130Smav continue; 4388230130Smav j = 0; 4389230130Smav while (types[6][j] >= 0 && 4390230130Smav (used & (1 << types[6][j])) != 0) { 4391230130Smav j++; 4392230130Smav } 4393230130Smav if (types[6][j] >= 0) { 4394230130Smav w->ossdev = types[6][j]; 4395230130Smav used |= (1 << types[6][j]); 4396230130Smav } 4397230130Smav } 4398230130Smav} 4399230130Smav 4400230130Smavstatic void 4401230130Smavhdaa_audio_build_tree(struct hdaa_devinfo *devinfo) 4402230130Smav{ 4403230130Smav struct hdaa_audio_as *as = devinfo->as; 4404230130Smav int j, res; 4405230130Smav 4406230130Smav /* Trace all associations in order of their numbers. */ 4407230130Smav for (j = 0; j < devinfo->ascnt; j++) { 4408230130Smav if (as[j].enable == 0) 4409230130Smav continue; 4410230130Smav HDA_BOOTVERBOSE( 4411230130Smav device_printf(devinfo->dev, 4412230130Smav "Tracing association %d (%d)\n", j, as[j].index); 4413230130Smav ); 4414230130Smav if (as[j].dir == HDAA_CTL_OUT) { 4415230130Smavretry: 4416230130Smav res = hdaa_audio_trace_as_out(devinfo, j, 0); 4417230130Smav if (res == 0 && as[j].hpredir >= 0 && 4418230130Smav as[j].fakeredir == 0) { 4419230130Smav /* If CODEC can't do analog HP redirection 4420230130Smav try to make it using one more DAC. */ 4421230130Smav as[j].fakeredir = 1; 4422230130Smav goto retry; 4423230130Smav } 4424230130Smav } else if (as[j].mixed) 4425230130Smav res = hdaa_audio_trace_as_in(devinfo, j); 4426230130Smav else 4427230130Smav res = hdaa_audio_trace_as_in_mch(devinfo, j, 0); 4428230130Smav if (res) { 4429230130Smav HDA_BOOTVERBOSE( 4430230130Smav device_printf(devinfo->dev, 4431230130Smav "Association %d (%d) trace succeeded\n", 4432230130Smav j, as[j].index); 4433230130Smav ); 4434230130Smav } else { 4435230130Smav HDA_BOOTVERBOSE( 4436230130Smav device_printf(devinfo->dev, 4437230130Smav "Association %d (%d) trace failed\n", 4438230130Smav j, as[j].index); 4439230130Smav ); 4440230130Smav as[j].enable = 0; 4441230130Smav } 4442230130Smav } 4443230130Smav 4444230130Smav /* Look for additional DACs/ADCs. */ 4445230130Smav for (j = 0; j < devinfo->ascnt; j++) { 4446230130Smav if (as[j].enable == 0) 4447230130Smav continue; 4448230130Smav hdaa_audio_adddac(devinfo, j); 4449230130Smav } 4450230130Smav 4451230130Smav /* Trace mixer and beeper pseudo associations. */ 4452230130Smav hdaa_audio_trace_as_extra(devinfo); 4453230130Smav} 4454230130Smav 4455230451Smav/* 4456230451Smav * Store in pdevinfo new data about whether and how we can control signal 4457230451Smav * for OSS device to/from specified widget. 4458230451Smav */ 4459230130Smavstatic void 4460230451Smavhdaa_adjust_amp(struct hdaa_widget *w, int ossdev, 4461230451Smav int found, int minamp, int maxamp) 4462230451Smav{ 4463230451Smav struct hdaa_devinfo *devinfo = w->devinfo; 4464230451Smav struct hdaa_pcm_devinfo *pdevinfo; 4465230451Smav 4466230451Smav if (w->bindas >= 0) 4467230451Smav pdevinfo = devinfo->as[w->bindas].pdevinfo; 4468230451Smav else 4469230451Smav pdevinfo = &devinfo->devs[0]; 4470230451Smav if (found) 4471230451Smav pdevinfo->ossmask |= (1 << ossdev); 4472230451Smav if (minamp == 0 && maxamp == 0) 4473230451Smav return; 4474230451Smav if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) { 4475230451Smav pdevinfo->minamp[ossdev] = minamp; 4476230451Smav pdevinfo->maxamp[ossdev] = maxamp; 4477230451Smav } else { 4478230451Smav pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp); 4479230451Smav pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp); 4480230451Smav } 4481230451Smav} 4482230451Smav 4483230451Smav/* 4484230451Smav * Trace signals from/to all possible sources/destionstions to find possible 4485230451Smav * recording sources, OSS device control ranges and to assign controls. 4486230451Smav */ 4487230451Smavstatic void 4488230130Smavhdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo) 4489230130Smav{ 4490230130Smav struct hdaa_audio_as *as = devinfo->as; 4491230130Smav struct hdaa_widget *w, *cw; 4492230451Smav int i, j, minamp, maxamp, found; 4493230130Smav 4494230130Smav /* Assign mixers to the tree. */ 4495230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 4496230130Smav w = hdaa_widget_get(devinfo, i); 4497230130Smav if (w == NULL || w->enable == 0) 4498230130Smav continue; 4499230451Smav minamp = maxamp = 0; 4500230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT || 4501230130Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET || 4502230130Smav (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX && 4503230130Smav as[w->bindas].dir == HDAA_CTL_IN)) { 4504230130Smav if (w->ossdev < 0) 4505230130Smav continue; 4506230451Smav found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1, 4507230451Smav w->ossdev, 1, 0, &minamp, &maxamp); 4508230451Smav hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp); 4509230130Smav } else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) { 4510230451Smav found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1, 4511230451Smav SOUND_MIXER_RECLEV, 0, &minamp, &maxamp); 4512230451Smav hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp); 4513230130Smav } else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX && 4514230130Smav as[w->bindas].dir == HDAA_CTL_OUT) { 4515230451Smav found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1, 4516230451Smav SOUND_MIXER_VOLUME, 0, &minamp, &maxamp); 4517230451Smav hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp); 4518230130Smav } 4519230130Smav if (w->ossdev == SOUND_MIXER_IMIX) { 4520230451Smav minamp = maxamp = 0; 4521230451Smav found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1, 4522230451Smav w->ossdev, 1, 0, &minamp, &maxamp); 4523230451Smav if (minamp == maxamp) { 4524230130Smav /* If we are unable to control input monitor 4525230130Smav as source - try to control it as destination. */ 4526230451Smav found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1, 4527230451Smav w->ossdev, 0, &minamp, &maxamp); 4528230451Smav w->pflags |= HDAA_IMIX_AS_DST; 4529230130Smav } 4530230451Smav hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp); 4531230130Smav } 4532230130Smav if (w->pflags & HDAA_ADC_MONITOR) { 4533230130Smav for (j = 0; j < w->nconns; j++) { 4534230130Smav if (!w->connsenable[j]) 4535230130Smav continue; 4536230130Smav cw = hdaa_widget_get(devinfo, w->conns[j]); 4537230130Smav if (cw == NULL || cw->enable == 0) 4538230130Smav continue; 4539230130Smav if (cw->bindas == -1) 4540230130Smav continue; 4541230130Smav if (cw->bindas >= 0 && 4542230130Smav as[cw->bindas].dir != HDAA_CTL_IN) 4543230130Smav continue; 4544230451Smav minamp = maxamp = 0; 4545230451Smav found = hdaa_audio_ctl_dest_amp(devinfo, 4546230451Smav w->nid, j, SOUND_MIXER_IGAIN, 0, 4547230451Smav &minamp, &maxamp); 4548230451Smav hdaa_adjust_amp(w, SOUND_MIXER_IGAIN, 4549230451Smav found, minamp, maxamp); 4550230130Smav } 4551230130Smav } 4552230130Smav } 4553230130Smav} 4554230130Smav 4555230130Smavstatic void 4556230130Smavhdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo) 4557230130Smav{ 4558230130Smav struct hdaa_audio_as *as = devinfo->as; 4559230130Smav struct hdaa_widget *w; 4560230130Smav uint32_t pincap; 4561230130Smav int i; 4562230130Smav 4563230130Smav for (i = 0; i < devinfo->nodecnt; i++) { 4564230130Smav w = &devinfo->widget[i]; 4565230130Smav if (w == NULL) 4566230130Smav continue; 4567230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX && 4568230130Smav w->waspin == 0) 4569230130Smav continue; 4570230130Smav 4571230130Smav pincap = w->wclass.pin.cap; 4572230130Smav 4573230130Smav /* Disable everything. */ 4574230130Smav w->wclass.pin.ctrl &= ~( 4575230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE | 4576230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE | 4577230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE | 4578230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK); 4579230130Smav 4580230130Smav if (w->enable == 0) { 4581230130Smav /* Pin is unused so left it disabled. */ 4582230130Smav continue; 4583230130Smav } else if (w->waspin) { 4584230130Smav /* Enable input for beeper input. */ 4585230130Smav w->wclass.pin.ctrl |= 4586230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE; 4587230130Smav } else if (w->bindas < 0 || as[w->bindas].enable == 0) { 4588230130Smav /* Pin is unused so left it disabled. */ 4589230130Smav continue; 4590230130Smav } else if (as[w->bindas].dir == HDAA_CTL_IN) { 4591230130Smav /* Input pin, configure for input. */ 4592230130Smav if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)) 4593230130Smav w->wclass.pin.ctrl |= 4594230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE; 4595230130Smav 4596230130Smav if ((devinfo->quirks & HDAA_QUIRK_IVREF100) && 4597230130Smav HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap)) 4598230130Smav w->wclass.pin.ctrl |= 4599230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE( 4600230130Smav HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100); 4601230130Smav else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) && 4602230130Smav HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap)) 4603230130Smav w->wclass.pin.ctrl |= 4604230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE( 4605230130Smav HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80); 4606230130Smav else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) && 4607230130Smav HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap)) 4608230130Smav w->wclass.pin.ctrl |= 4609230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE( 4610230130Smav HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50); 4611230130Smav } else { 4612230130Smav /* Output pin, configure for output. */ 4613230130Smav if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)) 4614230130Smav w->wclass.pin.ctrl |= 4615230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE; 4616230130Smav 4617230130Smav if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) && 4618230130Smav (w->wclass.pin.config & 4619230130Smav HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) == 4620230130Smav HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT) 4621230130Smav w->wclass.pin.ctrl |= 4622230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE; 4623230130Smav 4624230130Smav if ((devinfo->quirks & HDAA_QUIRK_OVREF100) && 4625230130Smav HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap)) 4626230130Smav w->wclass.pin.ctrl |= 4627230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE( 4628230130Smav HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100); 4629230130Smav else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) && 4630230130Smav HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap)) 4631230130Smav w->wclass.pin.ctrl |= 4632230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE( 4633230130Smav HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80); 4634230130Smav else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) && 4635230130Smav HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap)) 4636230130Smav w->wclass.pin.ctrl |= 4637230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE( 4638230130Smav HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50); 4639230130Smav } 4640230130Smav } 4641230130Smav} 4642230130Smav 4643230130Smavstatic void 4644230130Smavhdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo) 4645230130Smav{ 4646230130Smav struct hdaa_audio_ctl *ctl; 4647230130Smav int i, z; 4648230130Smav 4649230130Smav i = 0; 4650230130Smav while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) { 4651230130Smav if (ctl->enable == 0 || ctl->ossmask != 0) { 4652230130Smav /* Mute disabled and mixer controllable controls. 4653230130Smav * Last will be initialized by mixer_init(). 4654230130Smav * This expected to reduce click on startup. */ 4655230130Smav hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0); 4656230130Smav continue; 4657230130Smav } 4658230130Smav /* Init fixed controls to 0dB amplification. */ 4659230130Smav z = ctl->offset; 4660230130Smav if (z > ctl->step) 4661230130Smav z = ctl->step; 4662230130Smav hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z); 4663230130Smav } 4664230130Smav} 4665230130Smav 4666230130Smavstatic void 4667230130Smavhdaa_gpio_commit(struct hdaa_devinfo *devinfo) 4668230130Smav{ 4669230130Smav uint32_t gdata, gmask, gdir; 4670230130Smav int i, numgpio; 4671230130Smav 4672230130Smav numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); 4673230130Smav if (devinfo->gpio != 0 && numgpio != 0) { 4674230130Smav gdata = hda_command(devinfo->dev, 4675230130Smav HDA_CMD_GET_GPIO_DATA(0, devinfo->nid)); 4676230130Smav gmask = hda_command(devinfo->dev, 4677230130Smav HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid)); 4678230130Smav gdir = hda_command(devinfo->dev, 4679230130Smav HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid)); 4680230130Smav for (i = 0; i < numgpio; i++) { 4681230130Smav if ((devinfo->gpio & HDAA_GPIO_MASK(i)) == 4682230130Smav HDAA_GPIO_SET(i)) { 4683230130Smav gdata |= (1 << i); 4684230130Smav gmask |= (1 << i); 4685230130Smav gdir |= (1 << i); 4686230130Smav } else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) == 4687230130Smav HDAA_GPIO_CLEAR(i)) { 4688230130Smav gdata &= ~(1 << i); 4689230130Smav gmask |= (1 << i); 4690230130Smav gdir |= (1 << i); 4691230130Smav } else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) == 4692230130Smav HDAA_GPIO_DISABLE(i)) { 4693230130Smav gmask &= ~(1 << i); 4694230130Smav } else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) == 4695230130Smav HDAA_GPIO_INPUT(i)) { 4696230130Smav gmask |= (1 << i); 4697230130Smav gdir &= ~(1 << i); 4698230130Smav } 4699230130Smav } 4700230130Smav HDA_BOOTVERBOSE( 4701230130Smav device_printf(devinfo->dev, "GPIO commit\n"); 4702230130Smav ); 4703230130Smav hda_command(devinfo->dev, 4704230130Smav HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask)); 4705230130Smav hda_command(devinfo->dev, 4706230130Smav HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir)); 4707230130Smav hda_command(devinfo->dev, 4708230130Smav HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata)); 4709230130Smav HDA_BOOTVERBOSE( 4710230130Smav hdaa_dump_gpio(devinfo); 4711230130Smav ); 4712230130Smav } 4713230130Smav} 4714230130Smav 4715230130Smavstatic void 4716230130Smavhdaa_gpo_commit(struct hdaa_devinfo *devinfo) 4717230130Smav{ 4718230130Smav uint32_t gdata; 4719230130Smav int i, numgpo; 4720230130Smav 4721230130Smav numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); 4722230130Smav if (devinfo->gpo != 0 && numgpo != 0) { 4723230130Smav gdata = hda_command(devinfo->dev, 4724230130Smav HDA_CMD_GET_GPO_DATA(0, devinfo->nid)); 4725230130Smav for (i = 0; i < numgpo; i++) { 4726230130Smav if ((devinfo->gpio & HDAA_GPIO_MASK(i)) == 4727230130Smav HDAA_GPIO_SET(i)) { 4728230130Smav gdata |= (1 << i); 4729230130Smav } else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) == 4730230130Smav HDAA_GPIO_CLEAR(i)) { 4731230130Smav gdata &= ~(1 << i); 4732230130Smav } 4733230130Smav } 4734230130Smav HDA_BOOTVERBOSE( 4735230130Smav device_printf(devinfo->dev, "GPO commit\n"); 4736230130Smav ); 4737230130Smav hda_command(devinfo->dev, 4738230130Smav HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata)); 4739230130Smav HDA_BOOTVERBOSE( 4740230130Smav hdaa_dump_gpo(devinfo); 4741230130Smav ); 4742230130Smav } 4743230130Smav} 4744230130Smav 4745230130Smavstatic void 4746230130Smavhdaa_audio_commit(struct hdaa_devinfo *devinfo) 4747230130Smav{ 4748230130Smav struct hdaa_widget *w; 4749230130Smav int i; 4750230130Smav 4751230130Smav /* Commit controls. */ 4752230130Smav hdaa_audio_ctl_commit(devinfo); 4753230130Smav 4754230130Smav /* Commit selectors, pins and EAPD. */ 4755230130Smav for (i = 0; i < devinfo->nodecnt; i++) { 4756230130Smav w = &devinfo->widget[i]; 4757230130Smav if (w == NULL) 4758230130Smav continue; 4759230130Smav if (w->selconn == -1) 4760230130Smav w->selconn = 0; 4761230130Smav if (w->nconns > 0) 4762230130Smav hdaa_widget_connection_select(w, w->selconn); 4763230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || 4764230130Smav w->waspin) { 4765230130Smav hda_command(devinfo->dev, 4766230130Smav HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid, 4767230130Smav w->wclass.pin.ctrl)); 4768230130Smav } 4769230130Smav if (w->param.eapdbtl != HDA_INVALID) { 4770230130Smav uint32_t val; 4771230130Smav 4772230130Smav val = w->param.eapdbtl; 4773230130Smav if (devinfo->quirks & 4774230130Smav HDAA_QUIRK_EAPDINV) 4775230130Smav val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD; 4776230130Smav hda_command(devinfo->dev, 4777230130Smav HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, 4778230130Smav val)); 4779230130Smav } 4780230130Smav } 4781230130Smav 4782230130Smav hdaa_gpio_commit(devinfo); 4783230130Smav hdaa_gpo_commit(devinfo); 4784230130Smav} 4785230130Smav 4786230130Smavstatic void 4787230130Smavhdaa_powerup(struct hdaa_devinfo *devinfo) 4788230130Smav{ 4789230130Smav int i; 4790230130Smav 4791230130Smav hda_command(devinfo->dev, 4792230130Smav HDA_CMD_SET_POWER_STATE(0, 4793230130Smav devinfo->nid, HDA_CMD_POWER_STATE_D0)); 4794230130Smav DELAY(100); 4795230130Smav 4796230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 4797230130Smav hda_command(devinfo->dev, 4798230130Smav HDA_CMD_SET_POWER_STATE(0, 4799230130Smav i, HDA_CMD_POWER_STATE_D0)); 4800230130Smav } 4801230130Smav DELAY(1000); 4802230130Smav} 4803230130Smav 4804230130Smavstatic int 4805230130Smavhdaa_pcmchannel_setup(struct hdaa_chan *ch) 4806230130Smav{ 4807230130Smav struct hdaa_devinfo *devinfo = ch->devinfo; 4808230130Smav struct hdaa_audio_as *as = devinfo->as; 4809230130Smav struct hdaa_widget *w; 4810230130Smav uint32_t cap, fmtcap, pcmcap; 4811230130Smav int i, j, ret, channels, onlystereo; 4812230130Smav uint16_t pinset; 4813230130Smav 4814230130Smav ch->caps = hdaa_caps; 4815230130Smav ch->caps.fmtlist = ch->fmtlist; 4816230130Smav ch->bit16 = 1; 4817230130Smav ch->bit32 = 0; 4818230130Smav ch->pcmrates[0] = 48000; 4819230130Smav ch->pcmrates[1] = 0; 4820230326Smav ch->stripecap = 0xff; 4821230130Smav 4822230130Smav ret = 0; 4823230130Smav channels = 0; 4824230130Smav onlystereo = 1; 4825230130Smav pinset = 0; 4826230130Smav fmtcap = devinfo->supp_stream_formats; 4827230130Smav pcmcap = devinfo->supp_pcm_size_rate; 4828230130Smav 4829230130Smav for (i = 0; i < 16; i++) { 4830230130Smav /* Check as is correct */ 4831230130Smav if (ch->as < 0) 4832230130Smav break; 4833230130Smav /* Cound only present DACs */ 4834230130Smav if (as[ch->as].dacs[ch->asindex][i] <= 0) 4835230130Smav continue; 4836230130Smav /* Ignore duplicates */ 4837230130Smav for (j = 0; j < ret; j++) { 4838230130Smav if (ch->io[j] == as[ch->as].dacs[ch->asindex][i]) 4839230130Smav break; 4840230130Smav } 4841230130Smav if (j < ret) 4842230130Smav continue; 4843230130Smav 4844230130Smav w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]); 4845230130Smav if (w == NULL || w->enable == 0) 4846230130Smav continue; 4847230130Smav cap = w->param.supp_stream_formats; 4848230130Smav if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) && 4849230130Smav !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap)) 4850230130Smav continue; 4851230130Smav /* Many CODECs does not declare AC3 support on SPDIF. 4852230130Smav I don't beleave that they doesn't support it! */ 4853230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) 4854230130Smav cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK; 4855230130Smav if (ret == 0) { 4856230130Smav fmtcap = cap; 4857230130Smav pcmcap = w->param.supp_pcm_size_rate; 4858230130Smav } else { 4859230130Smav fmtcap &= cap; 4860230130Smav pcmcap &= w->param.supp_pcm_size_rate; 4861230130Smav } 4862230130Smav ch->io[ret++] = as[ch->as].dacs[ch->asindex][i]; 4863230326Smav ch->stripecap &= w->wclass.conv.stripecap; 4864230130Smav /* Do not count redirection pin/dac channels. */ 4865230130Smav if (i == 15 && as[ch->as].hpredir >= 0) 4866230130Smav continue; 4867230130Smav channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1; 4868230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1) 4869230130Smav onlystereo = 0; 4870230130Smav pinset |= (1 << i); 4871230130Smav } 4872230130Smav ch->io[ret] = -1; 4873230130Smav ch->channels = channels; 4874230130Smav 4875230130Smav if (as[ch->as].fakeredir) 4876230130Smav ret--; 4877230130Smav /* Standard speaks only about stereo pins and playback, ... */ 4878230130Smav if ((!onlystereo) || as[ch->as].mixed) 4879230130Smav pinset = 0; 4880230130Smav /* ..., but there it gives us info about speakers layout. */ 4881230130Smav as[ch->as].pinset = pinset; 4882230130Smav 4883230130Smav ch->supp_stream_formats = fmtcap; 4884230130Smav ch->supp_pcm_size_rate = pcmcap; 4885230130Smav 4886230130Smav /* 4887230130Smav * 8bit = 0 4888230130Smav * 16bit = 1 4889230130Smav * 20bit = 2 4890230130Smav * 24bit = 3 4891230130Smav * 32bit = 4 4892230130Smav */ 4893230130Smav if (ret > 0) { 4894230130Smav i = 0; 4895230130Smav if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) { 4896230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap)) 4897230130Smav ch->bit16 = 1; 4898230130Smav else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap)) 4899230130Smav ch->bit16 = 0; 4900230488Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap)) 4901230130Smav ch->bit32 = 3; 4902230130Smav else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap)) 4903230130Smav ch->bit32 = 2; 4904230488Smav else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap)) 4905230488Smav ch->bit32 = 4; 4906230130Smav if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) { 4907230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0); 4908230130Smav if (ch->bit32) 4909230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0); 4910230130Smav } 4911230130Smav if (channels >= 2) { 4912230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0); 4913230130Smav if (ch->bit32) 4914230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0); 4915230130Smav } 4916230312Smav if (channels >= 3 && !onlystereo) { 4917230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0); 4918230312Smav if (ch->bit32) 4919230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0); 4920230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1); 4921230312Smav if (ch->bit32) 4922230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1); 4923230312Smav } 4924230312Smav if (channels >= 4) { 4925230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0); 4926230130Smav if (ch->bit32) 4927230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0); 4928230312Smav if (!onlystereo) { 4929230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1); 4930230312Smav if (ch->bit32) 4931230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1); 4932230312Smav } 4933230130Smav } 4934230312Smav if (channels >= 5 && !onlystereo) { 4935230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0); 4936230312Smav if (ch->bit32) 4937230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0); 4938230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1); 4939230312Smav if (ch->bit32) 4940230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1); 4941230312Smav } 4942230312Smav if (channels >= 6) { 4943230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1); 4944230130Smav if (ch->bit32) 4945230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1); 4946230312Smav if (!onlystereo) { 4947230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0); 4948230312Smav if (ch->bit32) 4949230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0); 4950230312Smav } 4951230130Smav } 4952230312Smav if (channels >= 7 && !onlystereo) { 4953230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0); 4954230312Smav if (ch->bit32) 4955230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0); 4956230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1); 4957230312Smav if (ch->bit32) 4958230312Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1); 4959230312Smav } 4960230312Smav if (channels >= 8) { 4961230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1); 4962230130Smav if (ch->bit32) 4963230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1); 4964230130Smav } 4965230130Smav } 4966230130Smav if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) { 4967230130Smav ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0); 4968230130Smav } 4969230130Smav ch->fmtlist[i] = 0; 4970230130Smav i = 0; 4971230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap)) 4972230130Smav ch->pcmrates[i++] = 8000; 4973230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap)) 4974230130Smav ch->pcmrates[i++] = 11025; 4975230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap)) 4976230130Smav ch->pcmrates[i++] = 16000; 4977230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap)) 4978230130Smav ch->pcmrates[i++] = 22050; 4979230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap)) 4980230130Smav ch->pcmrates[i++] = 32000; 4981230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap)) 4982230130Smav ch->pcmrates[i++] = 44100; 4983230130Smav /* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */ 4984230130Smav ch->pcmrates[i++] = 48000; 4985230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap)) 4986230130Smav ch->pcmrates[i++] = 88200; 4987230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap)) 4988230130Smav ch->pcmrates[i++] = 96000; 4989230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap)) 4990230130Smav ch->pcmrates[i++] = 176400; 4991230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap)) 4992230130Smav ch->pcmrates[i++] = 192000; 4993230130Smav /* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */ 4994230130Smav ch->pcmrates[i] = 0; 4995230130Smav if (i > 0) { 4996230130Smav ch->caps.minspeed = ch->pcmrates[0]; 4997230130Smav ch->caps.maxspeed = ch->pcmrates[i - 1]; 4998230130Smav } 4999230130Smav } 5000230130Smav 5001230130Smav return (ret); 5002230130Smav} 5003230130Smav 5004230130Smavstatic void 5005230451Smavhdaa_prepare_pcms(struct hdaa_devinfo *devinfo) 5006230130Smav{ 5007230130Smav struct hdaa_audio_as *as = devinfo->as; 5008230130Smav int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0; 5009230130Smav 5010230130Smav for (i = 0; i < devinfo->ascnt; i++) { 5011230130Smav if (as[i].enable == 0) 5012230130Smav continue; 5013230130Smav if (as[i].dir == HDAA_CTL_IN) { 5014230130Smav if (as[i].digital) 5015230130Smav drdev++; 5016230130Smav else 5017230130Smav ardev++; 5018230130Smav } else { 5019230130Smav if (as[i].digital) 5020230130Smav dpdev++; 5021230130Smav else 5022230130Smav apdev++; 5023230130Smav } 5024230130Smav } 5025230130Smav devinfo->num_devs = 5026230130Smav max(ardev, apdev) + max(drdev, dpdev); 5027230130Smav devinfo->devs = 5028230130Smav (struct hdaa_pcm_devinfo *)malloc( 5029230130Smav devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo), 5030230130Smav M_HDAA, M_ZERO | M_NOWAIT); 5031230130Smav if (devinfo->devs == NULL) { 5032230130Smav device_printf(devinfo->dev, 5033230130Smav "Unable to allocate memory for devices\n"); 5034230130Smav return; 5035230130Smav } 5036230130Smav for (i = 0; i < devinfo->num_devs; i++) { 5037230130Smav devinfo->devs[i].index = i; 5038230130Smav devinfo->devs[i].devinfo = devinfo; 5039230130Smav devinfo->devs[i].playas = -1; 5040230130Smav devinfo->devs[i].recas = -1; 5041230130Smav devinfo->devs[i].digital = 255; 5042230130Smav } 5043230130Smav for (i = 0; i < devinfo->ascnt; i++) { 5044230130Smav if (as[i].enable == 0) 5045230130Smav continue; 5046230130Smav for (j = 0; j < devinfo->num_devs; j++) { 5047230130Smav if (devinfo->devs[j].digital != 255 && 5048230130Smav (!devinfo->devs[j].digital) != 5049230130Smav (!as[i].digital)) 5050230130Smav continue; 5051230130Smav if (as[i].dir == HDAA_CTL_IN) { 5052230130Smav if (devinfo->devs[j].recas >= 0) 5053230130Smav continue; 5054230130Smav devinfo->devs[j].recas = i; 5055230130Smav } else { 5056230130Smav if (devinfo->devs[j].playas >= 0) 5057230130Smav continue; 5058230130Smav devinfo->devs[j].playas = i; 5059230130Smav } 5060230451Smav as[i].pdevinfo = &devinfo->devs[j]; 5061230130Smav for (k = 0; k < as[i].num_chans; k++) { 5062230130Smav devinfo->chans[as[i].chans[k]].pdevinfo = 5063230130Smav &devinfo->devs[j]; 5064230130Smav } 5065230130Smav devinfo->devs[j].digital = as[i].digital; 5066230130Smav break; 5067230130Smav } 5068230130Smav } 5069230451Smav} 5070230451Smav 5071230451Smavstatic void 5072230451Smavhdaa_create_pcms(struct hdaa_devinfo *devinfo) 5073230451Smav{ 5074230451Smav int i; 5075230451Smav 5076230130Smav for (i = 0; i < devinfo->num_devs; i++) { 5077230130Smav struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i]; 5078230130Smav 5079230130Smav pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1); 5080230130Smav device_set_ivars(pdevinfo->dev, (void *)pdevinfo); 5081230130Smav } 5082230130Smav} 5083230130Smav 5084230130Smavstatic void 5085230130Smavhdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag) 5086230130Smav{ 5087230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 5088230130Smav struct hdaa_audio_ctl *ctl; 5089230130Smav char buf[64]; 5090230130Smav int i, j, printed; 5091230130Smav 5092230130Smav if (flag == 0) { 5093230130Smav flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM | 5094230130Smav SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV | 5095230130Smav SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN | 5096230130Smav SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR); 5097230130Smav } 5098230130Smav 5099230130Smav for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) { 5100230130Smav if ((flag & (1 << j)) == 0) 5101230130Smav continue; 5102230130Smav i = 0; 5103230130Smav printed = 0; 5104230130Smav while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) { 5105230130Smav if (ctl->enable == 0 || 5106230130Smav ctl->widget->enable == 0) 5107230130Smav continue; 5108230130Smav if (!((pdevinfo->playas >= 0 && 5109230130Smav ctl->widget->bindas == pdevinfo->playas) || 5110230130Smav (pdevinfo->recas >= 0 && 5111230130Smav ctl->widget->bindas == pdevinfo->recas) || 5112230130Smav (ctl->widget->bindas == -2 && pdevinfo->index == 0))) 5113230130Smav continue; 5114230130Smav if ((ctl->ossmask & (1 << j)) == 0) 5115230130Smav continue; 5116230130Smav 5117230130Smav if (printed == 0) { 5118230130Smav device_printf(pdevinfo->dev, "\n"); 5119230130Smav if (banner != NULL) { 5120230130Smav device_printf(pdevinfo->dev, "%s", banner); 5121230130Smav } else { 5122230130Smav device_printf(pdevinfo->dev, "Unknown Ctl"); 5123230130Smav } 5124230451Smav printf(" (OSS: %s)", 5125230130Smav hdaa_audio_ctl_ossmixer_mask2allname(1 << j, 5126230130Smav buf, sizeof(buf))); 5127230451Smav if (pdevinfo->ossmask & (1 << j)) { 5128230451Smav printf(": %+d/%+ddB\n", 5129230451Smav pdevinfo->minamp[j] / 4, 5130230451Smav pdevinfo->maxamp[j] / 4); 5131230451Smav } else 5132230451Smav printf("\n"); 5133230130Smav device_printf(pdevinfo->dev, " |\n"); 5134230130Smav printed = 1; 5135230130Smav } 5136230130Smav device_printf(pdevinfo->dev, " +- ctl %2d (nid %3d %s", i, 5137230130Smav ctl->widget->nid, 5138230130Smav (ctl->ndir == HDAA_CTL_IN)?"in ":"out"); 5139230130Smav if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir) 5140230130Smav printf(" %2d): ", ctl->index); 5141230130Smav else 5142230130Smav printf("): "); 5143230130Smav if (ctl->step > 0) { 5144230130Smav printf("%+d/%+ddB (%d steps)%s\n", 5145230451Smav MINQDB(ctl) / 4, 5146230451Smav MAXQDB(ctl) / 4, 5147230130Smav ctl->step + 1, 5148230130Smav ctl->mute?" + mute":""); 5149230130Smav } else 5150230130Smav printf("%s\n", ctl->mute?"mute":""); 5151230130Smav } 5152230130Smav } 5153230130Smav} 5154230130Smav 5155230130Smavstatic void 5156230130Smavhdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap) 5157230130Smav{ 5158230130Smav uint32_t cap; 5159230130Smav 5160230130Smav cap = fcap; 5161230130Smav if (cap != 0) { 5162230130Smav device_printf(dev, " Stream cap: 0x%08x\n", cap); 5163230130Smav device_printf(dev, " "); 5164230130Smav if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap)) 5165230130Smav printf(" AC3"); 5166230130Smav if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap)) 5167230130Smav printf(" FLOAT32"); 5168230130Smav if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap)) 5169230130Smav printf(" PCM"); 5170230130Smav printf("\n"); 5171230130Smav } 5172230130Smav cap = pcmcap; 5173230130Smav if (cap != 0) { 5174230130Smav device_printf(dev, " PCM cap: 0x%08x\n", cap); 5175230130Smav device_printf(dev, " "); 5176230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap)) 5177230130Smav printf(" 8"); 5178230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap)) 5179230130Smav printf(" 16"); 5180230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap)) 5181230130Smav printf(" 20"); 5182230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap)) 5183230130Smav printf(" 24"); 5184230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap)) 5185230130Smav printf(" 32"); 5186230130Smav printf(" bits,"); 5187230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap)) 5188230130Smav printf(" 8"); 5189230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap)) 5190230130Smav printf(" 11"); 5191230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap)) 5192230130Smav printf(" 16"); 5193230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap)) 5194230130Smav printf(" 22"); 5195230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap)) 5196230130Smav printf(" 32"); 5197230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap)) 5198230130Smav printf(" 44"); 5199230130Smav printf(" 48"); 5200230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap)) 5201230130Smav printf(" 88"); 5202230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap)) 5203230130Smav printf(" 96"); 5204230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap)) 5205230130Smav printf(" 176"); 5206230130Smav if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap)) 5207230130Smav printf(" 192"); 5208230130Smav printf(" KHz\n"); 5209230130Smav } 5210230130Smav} 5211230130Smav 5212230130Smavstatic void 5213230130Smavhdaa_dump_pin(struct hdaa_widget *w) 5214230130Smav{ 5215230130Smav uint32_t pincap; 5216230130Smav 5217230130Smav pincap = w->wclass.pin.cap; 5218230130Smav 5219230130Smav device_printf(w->devinfo->dev, " Pin cap: 0x%08x\n", pincap); 5220230130Smav device_printf(w->devinfo->dev, " "); 5221230130Smav if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap)) 5222230130Smav printf(" ISC"); 5223230130Smav if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) 5224230130Smav printf(" TRQD"); 5225230130Smav if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) 5226230130Smav printf(" PDC"); 5227230130Smav if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)) 5228230130Smav printf(" HP"); 5229230130Smav if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)) 5230230130Smav printf(" OUT"); 5231230130Smav if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)) 5232230130Smav printf(" IN"); 5233230130Smav if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap)) 5234230130Smav printf(" BAL"); 5235230130Smav if (HDA_PARAM_PIN_CAP_HDMI(pincap)) 5236230130Smav printf(" HDMI"); 5237230130Smav if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) { 5238230130Smav printf(" VREF["); 5239230130Smav if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap)) 5240230130Smav printf(" 50"); 5241230130Smav if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap)) 5242230130Smav printf(" 80"); 5243230130Smav if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap)) 5244230130Smav printf(" 100"); 5245230130Smav if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap)) 5246230130Smav printf(" GROUND"); 5247230130Smav if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap)) 5248230130Smav printf(" HIZ"); 5249230130Smav printf(" ]"); 5250230130Smav } 5251230130Smav if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)) 5252230130Smav printf(" EAPD"); 5253230130Smav if (HDA_PARAM_PIN_CAP_DP(pincap)) 5254230130Smav printf(" DP"); 5255230130Smav if (HDA_PARAM_PIN_CAP_HBR(pincap)) 5256230130Smav printf(" HBR"); 5257230130Smav printf("\n"); 5258230130Smav device_printf(w->devinfo->dev, " Pin config: 0x%08x\n", 5259230130Smav w->wclass.pin.config); 5260230130Smav device_printf(w->devinfo->dev, " Pin control: 0x%08x", w->wclass.pin.ctrl); 5261230130Smav if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE) 5262230130Smav printf(" HP"); 5263230130Smav if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE) 5264230130Smav printf(" IN"); 5265230130Smav if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE) 5266230130Smav printf(" OUT"); 5267230312Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) { 5268230312Smav if ((w->wclass.pin.ctrl & 5269230312Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03) 5270230312Smav printf(" HBR"); 5271230312Smav else if ((w->wclass.pin.ctrl & 5272230312Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0) 5273230312Smav printf(" EPTs"); 5274230312Smav } else { 5275230312Smav if ((w->wclass.pin.ctrl & 5276230312Smav HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0) 5277230312Smav printf(" VREFs"); 5278230312Smav } 5279230130Smav printf("\n"); 5280230130Smav} 5281230130Smav 5282230130Smavstatic void 5283230130Smavhdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf) 5284230130Smav{ 5285230130Smav 5286230130Smav device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d " 5287230130Smav "%-13s %-5s %-7s %-10s %-7s %d%s\n", 5288230130Smav w->nid, conf, 5289230130Smav HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf), 5290230130Smav HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf), 5291230130Smav HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)], 5292230130Smav HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)], 5293230130Smav HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)], 5294230130Smav HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)], 5295230130Smav HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)], 5296230130Smav HDA_CONFIG_DEFAULTCONF_MISC(conf), 5297230130Smav (w->enable == 0)?" DISA":""); 5298230130Smav} 5299230130Smav 5300230130Smavstatic void 5301230130Smavhdaa_dump_pin_configs(struct hdaa_devinfo *devinfo) 5302230130Smav{ 5303230130Smav struct hdaa_widget *w; 5304230130Smav int i; 5305230130Smav 5306230130Smav device_printf(devinfo->dev, "nid 0x as seq " 5307230130Smav "device conn jack loc color misc\n"); 5308230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 5309230130Smav w = hdaa_widget_get(devinfo, i); 5310230130Smav if (w == NULL) 5311230130Smav continue; 5312230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 5313230130Smav continue; 5314230130Smav hdaa_dump_pin_config(w, w->wclass.pin.config); 5315230130Smav } 5316230130Smav} 5317230130Smav 5318230130Smavstatic void 5319230130Smavhdaa_dump_amp(device_t dev, uint32_t cap, char *banner) 5320230130Smav{ 5321230130Smav device_printf(dev, " %s amp: 0x%08x\n", banner, cap); 5322230130Smav device_printf(dev, " " 5323230130Smav "mute=%d step=%d size=%d offset=%d\n", 5324230130Smav HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap), 5325230130Smav HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap), 5326230130Smav HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap), 5327230130Smav HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap)); 5328230130Smav} 5329230130Smav 5330230130Smavstatic void 5331230130Smavhdaa_dump_nodes(struct hdaa_devinfo *devinfo) 5332230130Smav{ 5333230130Smav struct hdaa_widget *w, *cw; 5334230130Smav char buf[64]; 5335230130Smav int i, j; 5336230130Smav 5337230130Smav device_printf(devinfo->dev, "\n"); 5338230130Smav device_printf(devinfo->dev, "Default Parameter\n"); 5339230130Smav device_printf(devinfo->dev, "-----------------\n"); 5340230130Smav hdaa_dump_audio_formats(devinfo->dev, 5341230130Smav devinfo->supp_stream_formats, 5342230130Smav devinfo->supp_pcm_size_rate); 5343230130Smav device_printf(devinfo->dev, " IN amp: 0x%08x\n", 5344230130Smav devinfo->inamp_cap); 5345230130Smav device_printf(devinfo->dev, " OUT amp: 0x%08x\n", 5346230130Smav devinfo->outamp_cap); 5347230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 5348230130Smav w = hdaa_widget_get(devinfo, i); 5349230130Smav if (w == NULL) { 5350230130Smav device_printf(devinfo->dev, "Ghost widget nid=%d\n", i); 5351230130Smav continue; 5352230130Smav } 5353230130Smav device_printf(devinfo->dev, "\n"); 5354230130Smav device_printf(devinfo->dev, " nid: %d%s\n", w->nid, 5355230130Smav (w->enable == 0) ? " [DISABLED]" : ""); 5356230130Smav device_printf(devinfo->dev, " Name: %s\n", w->name); 5357230130Smav device_printf(devinfo->dev, " Widget cap: 0x%08x\n", 5358230130Smav w->param.widget_cap); 5359230130Smav if (w->param.widget_cap & 0x0ee1) { 5360230130Smav device_printf(devinfo->dev, " "); 5361230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap)) 5362230130Smav printf(" LRSWAP"); 5363230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap)) 5364230130Smav printf(" PWR"); 5365230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) 5366230130Smav printf(" DIGITAL"); 5367230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap)) 5368230130Smav printf(" UNSOL"); 5369230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap)) 5370230130Smav printf(" PROC"); 5371230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) 5372230326Smav printf(" STRIPE(x%d)", 5373230326Smav 1 << (fls(w->wclass.conv.stripecap) - 1)); 5374230130Smav j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap); 5375230130Smav if (j == 1) 5376230130Smav printf(" STEREO"); 5377230130Smav else if (j > 1) 5378230130Smav printf(" %dCH", j + 1); 5379230130Smav printf("\n"); 5380230130Smav } 5381230130Smav if (w->bindas != -1) { 5382230130Smav device_printf(devinfo->dev, " Association: %d (0x%08x)\n", 5383230130Smav w->bindas, w->bindseqmask); 5384230130Smav } 5385230130Smav if (w->ossmask != 0 || w->ossdev >= 0) { 5386230130Smav device_printf(devinfo->dev, " OSS: %s", 5387230130Smav hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf))); 5388230130Smav if (w->ossdev >= 0) 5389230451Smav printf(" (%s)", ossnames[w->ossdev]); 5390230130Smav printf("\n"); 5391230130Smav } 5392230130Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT || 5393230130Smav w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) { 5394230130Smav hdaa_dump_audio_formats(devinfo->dev, 5395230130Smav w->param.supp_stream_formats, 5396230130Smav w->param.supp_pcm_size_rate); 5397230130Smav } else if (w->type == 5398230130Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin) 5399230130Smav hdaa_dump_pin(w); 5400230130Smav if (w->param.eapdbtl != HDA_INVALID) 5401230130Smav device_printf(devinfo->dev, " EAPD: 0x%08x\n", 5402230130Smav w->param.eapdbtl); 5403230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) && 5404230130Smav w->param.outamp_cap != 0) 5405230130Smav hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output"); 5406230130Smav if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) && 5407230130Smav w->param.inamp_cap != 0) 5408230130Smav hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input"); 5409230130Smav if (w->nconns > 0) { 5410230130Smav device_printf(devinfo->dev, " connections: %d\n", w->nconns); 5411230130Smav device_printf(devinfo->dev, " |\n"); 5412230130Smav } 5413230130Smav for (j = 0; j < w->nconns; j++) { 5414230130Smav cw = hdaa_widget_get(devinfo, w->conns[j]); 5415230130Smav device_printf(devinfo->dev, " + %s<- nid=%d [%s]", 5416230130Smav (w->connsenable[j] == 0)?"[DISABLED] ":"", 5417230130Smav w->conns[j], (cw == NULL) ? "GHOST!" : cw->name); 5418230130Smav if (cw == NULL) 5419230130Smav printf(" [UNKNOWN]"); 5420230130Smav else if (cw->enable == 0) 5421230130Smav printf(" [DISABLED]"); 5422230130Smav if (w->nconns > 1 && w->selconn == j && w->type != 5423230130Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) 5424230130Smav printf(" (selected)"); 5425230130Smav printf("\n"); 5426230130Smav } 5427230130Smav } 5428230130Smav 5429230130Smav} 5430230130Smav 5431230130Smavstatic void 5432230130Smavhdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth) 5433230130Smav{ 5434230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 5435230130Smav struct hdaa_widget *w, *cw; 5436230130Smav char buf[64]; 5437230130Smav int i, printed = 0; 5438230130Smav 5439230130Smav if (depth > HDA_PARSE_MAXDEPTH) 5440230130Smav return; 5441230130Smav 5442230130Smav w = hdaa_widget_get(devinfo, nid); 5443230130Smav if (w == NULL || w->enable == 0) 5444230130Smav return; 5445230130Smav 5446230130Smav if (depth == 0) 5447230130Smav device_printf(pdevinfo->dev, "%*s", 4, ""); 5448230130Smav else 5449230130Smav device_printf(pdevinfo->dev, "%*s + <- ", 4 + (depth - 1) * 7, ""); 5450230130Smav printf("nid=%d [%s]", w->nid, w->name); 5451230130Smav 5452230130Smav if (depth > 0) { 5453230130Smav if (w->ossmask == 0) { 5454230130Smav printf("\n"); 5455230130Smav return; 5456230130Smav } 5457230130Smav printf(" [src: %s]", 5458230130Smav hdaa_audio_ctl_ossmixer_mask2allname( 5459230130Smav w->ossmask, buf, sizeof(buf))); 5460230130Smav if (w->ossdev >= 0) { 5461230130Smav printf("\n"); 5462230130Smav return; 5463230130Smav } 5464230130Smav } 5465230130Smav printf("\n"); 5466230130Smav 5467230130Smav for (i = 0; i < w->nconns; i++) { 5468230130Smav if (w->connsenable[i] == 0) 5469230130Smav continue; 5470230130Smav cw = hdaa_widget_get(devinfo, w->conns[i]); 5471230130Smav if (cw == NULL || cw->enable == 0 || cw->bindas == -1) 5472230130Smav continue; 5473230130Smav if (printed == 0) { 5474230130Smav device_printf(pdevinfo->dev, "%*s |\n", 4 + (depth) * 7, ""); 5475230130Smav printed = 1; 5476230130Smav } 5477230130Smav hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1); 5478230130Smav } 5479230130Smav 5480230130Smav} 5481230130Smav 5482230130Smavstatic void 5483230130Smavhdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo) 5484230130Smav{ 5485230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 5486230130Smav struct hdaa_audio_as *as; 5487230130Smav struct hdaa_widget *w; 5488230130Smav int i, printed = 0; 5489230130Smav 5490230130Smav if (pdevinfo->playas < 0) 5491230130Smav return; 5492230130Smav 5493230130Smav as = &devinfo->as[pdevinfo->playas]; 5494230130Smav for (i = 0; i < 16; i++) { 5495230130Smav if (as->pins[i] <= 0) 5496230130Smav continue; 5497230130Smav w = hdaa_widget_get(devinfo, as->pins[i]); 5498230130Smav if (w == NULL || w->enable == 0) 5499230130Smav continue; 5500230130Smav if (printed == 0) { 5501230130Smav printed = 1; 5502230130Smav device_printf(pdevinfo->dev, "\n"); 5503230130Smav device_printf(pdevinfo->dev, "Playback:\n"); 5504230130Smav } 5505230130Smav device_printf(pdevinfo->dev, "\n"); 5506230130Smav hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0); 5507230130Smav } 5508230130Smav} 5509230130Smav 5510230130Smavstatic void 5511230130Smavhdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo) 5512230130Smav{ 5513230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 5514230130Smav struct hdaa_widget *w; 5515230130Smav int i; 5516230130Smav int printed = 0; 5517230130Smav 5518230130Smav if (pdevinfo->recas < 0) 5519230130Smav return; 5520230130Smav 5521230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 5522230130Smav w = hdaa_widget_get(devinfo, i); 5523230130Smav if (w == NULL || w->enable == 0) 5524230130Smav continue; 5525230130Smav if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) 5526230130Smav continue; 5527230130Smav if (w->bindas != pdevinfo->recas) 5528230130Smav continue; 5529230130Smav if (printed == 0) { 5530230130Smav printed = 1; 5531230130Smav device_printf(pdevinfo->dev, "\n"); 5532230130Smav device_printf(pdevinfo->dev, "Record:\n"); 5533230130Smav } 5534230130Smav device_printf(pdevinfo->dev, "\n"); 5535230130Smav hdaa_dump_dst_nid(pdevinfo, i, 0); 5536230130Smav } 5537230130Smav} 5538230130Smav 5539230130Smavstatic void 5540230130Smavhdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo) 5541230130Smav{ 5542230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 5543230130Smav struct hdaa_widget *w; 5544230130Smav int i; 5545230130Smav int printed = 0; 5546230130Smav 5547230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 5548230130Smav w = hdaa_widget_get(devinfo, i); 5549230130Smav if (w == NULL || w->enable == 0) 5550230130Smav continue; 5551230130Smav if (w->ossdev != SOUND_MIXER_IMIX) 5552230130Smav continue; 5553230451Smav if (w->bindas != pdevinfo->recas) 5554230451Smav continue; 5555230130Smav if (printed == 0) { 5556230130Smav printed = 1; 5557230130Smav device_printf(pdevinfo->dev, "\n"); 5558230130Smav device_printf(pdevinfo->dev, "Input Mix:\n"); 5559230130Smav } 5560230130Smav device_printf(pdevinfo->dev, "\n"); 5561230130Smav hdaa_dump_dst_nid(pdevinfo, i, 0); 5562230130Smav } 5563230130Smav} 5564230130Smav 5565230130Smavstatic void 5566230130Smavhdaa_dump_pcmchannels(struct hdaa_pcm_devinfo *pdevinfo) 5567230130Smav{ 5568230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 5569230130Smav nid_t *nids; 5570230130Smav int chid, i; 5571230130Smav 5572230130Smav if (pdevinfo->playas >= 0) { 5573230130Smav device_printf(pdevinfo->dev, "\n"); 5574230130Smav device_printf(pdevinfo->dev, "Playback:\n"); 5575230130Smav device_printf(pdevinfo->dev, "\n"); 5576230130Smav chid = devinfo->as[pdevinfo->playas].chans[0]; 5577230130Smav hdaa_dump_audio_formats(pdevinfo->dev, 5578230130Smav devinfo->chans[chid].supp_stream_formats, 5579230130Smav devinfo->chans[chid].supp_pcm_size_rate); 5580230130Smav for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) { 5581230130Smav chid = devinfo->as[pdevinfo->playas].chans[i]; 5582230130Smav device_printf(pdevinfo->dev, " DAC:"); 5583230130Smav for (nids = devinfo->chans[chid].io; *nids != -1; nids++) 5584230130Smav printf(" %d", *nids); 5585230130Smav printf("\n"); 5586230130Smav } 5587230130Smav } 5588230130Smav if (pdevinfo->recas >= 0) { 5589230130Smav device_printf(pdevinfo->dev, "\n"); 5590230130Smav device_printf(pdevinfo->dev, "Record:\n"); 5591230130Smav device_printf(pdevinfo->dev, "\n"); 5592230130Smav chid = devinfo->as[pdevinfo->recas].chans[0]; 5593230130Smav hdaa_dump_audio_formats(pdevinfo->dev, 5594230130Smav devinfo->chans[chid].supp_stream_formats, 5595230130Smav devinfo->chans[chid].supp_pcm_size_rate); 5596230130Smav for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) { 5597230130Smav chid = devinfo->as[pdevinfo->recas].chans[i]; 5598230130Smav device_printf(pdevinfo->dev, " DAC:"); 5599230130Smav for (nids = devinfo->chans[chid].io; *nids != -1; nids++) 5600230130Smav printf(" %d", *nids); 5601230130Smav printf("\n"); 5602230130Smav } 5603230130Smav } 5604230130Smav} 5605230130Smav 5606230130Smavstatic void 5607230130Smavhdaa_pindump(device_t dev) 5608230130Smav{ 5609230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 5610230130Smav struct hdaa_widget *w; 5611230130Smav uint32_t res, pincap, delay; 5612230130Smav int i; 5613230130Smav 5614230130Smav device_printf(dev, "Dumping AFG pins:\n"); 5615230130Smav device_printf(dev, "nid 0x as seq " 5616230130Smav "device conn jack loc color misc\n"); 5617230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 5618230130Smav w = hdaa_widget_get(devinfo, i); 5619230130Smav if (w == NULL || w->type != 5620230130Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 5621230130Smav continue; 5622230130Smav hdaa_dump_pin_config(w, w->wclass.pin.config); 5623230130Smav pincap = w->wclass.pin.cap; 5624230130Smav device_printf(dev, " Caps: %2s %3s %2s %4s %4s", 5625230130Smav HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"", 5626230130Smav HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"", 5627230130Smav HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"", 5628230130Smav HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"", 5629230130Smav HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":""); 5630230130Smav if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) || 5631230130Smav HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) { 5632230130Smav if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) { 5633230130Smav delay = 0; 5634230130Smav hda_command(dev, 5635230130Smav HDA_CMD_SET_PIN_SENSE(0, w->nid, 0)); 5636230130Smav do { 5637230130Smav res = hda_command(dev, 5638230130Smav HDA_CMD_GET_PIN_SENSE(0, w->nid)); 5639230130Smav if (res != 0x7fffffff && res != 0xffffffff) 5640230130Smav break; 5641230130Smav DELAY(10); 5642230130Smav } while (++delay < 10000); 5643230130Smav } else { 5644230130Smav delay = 0; 5645230130Smav res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0, 5646230130Smav w->nid)); 5647230130Smav } 5648230312Smav printf(" Sense: 0x%08x (%sconnected%s)", res, 5649230130Smav (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ? 5650230312Smav "" : "dis", 5651230312Smav (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) && 5652230312Smav (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ? 5653230312Smav ", ELD valid" : ""); 5654230130Smav if (delay > 0) 5655230130Smav printf(" delay %dus", delay * 10); 5656230130Smav } 5657230130Smav printf("\n"); 5658230130Smav } 5659230130Smav device_printf(dev, 5660230130Smav "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n", 5661230130Smav HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap), 5662230130Smav HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap), 5663230130Smav HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap), 5664230130Smav HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap), 5665230130Smav HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap)); 5666230130Smav hdaa_dump_gpi(devinfo); 5667230130Smav hdaa_dump_gpio(devinfo); 5668230130Smav hdaa_dump_gpo(devinfo); 5669230130Smav} 5670230130Smav 5671230130Smavstatic void 5672230130Smavhdaa_configure(device_t dev) 5673230130Smav{ 5674230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 5675230130Smav struct hdaa_audio_ctl *ctl; 5676230130Smav int i; 5677230130Smav 5678230130Smav HDA_BOOTHVERBOSE( 5679230130Smav device_printf(dev, "Applying built-in patches...\n"); 5680230130Smav ); 5681230130Smav hdaa_patch(devinfo); 5682230130Smav HDA_BOOTHVERBOSE( 5683230130Smav device_printf(dev, "Applying local patches...\n"); 5684230130Smav ); 5685230130Smav hdaa_local_patch(devinfo); 5686230130Smav hdaa_audio_postprocess(devinfo); 5687230130Smav HDA_BOOTHVERBOSE( 5688230130Smav device_printf(dev, "Parsing Ctls...\n"); 5689230130Smav ); 5690230130Smav hdaa_audio_ctl_parse(devinfo); 5691230130Smav HDA_BOOTHVERBOSE( 5692230130Smav device_printf(dev, "Disabling nonaudio...\n"); 5693230130Smav ); 5694230130Smav hdaa_audio_disable_nonaudio(devinfo); 5695230130Smav HDA_BOOTHVERBOSE( 5696230130Smav device_printf(dev, "Disabling useless...\n"); 5697230130Smav ); 5698230130Smav hdaa_audio_disable_useless(devinfo); 5699230130Smav HDA_BOOTVERBOSE( 5700230130Smav device_printf(dev, "Patched pins configuration:\n"); 5701230130Smav hdaa_dump_pin_configs(devinfo); 5702230130Smav ); 5703230130Smav HDA_BOOTHVERBOSE( 5704230130Smav device_printf(dev, "Parsing pin associations...\n"); 5705230130Smav ); 5706230130Smav hdaa_audio_as_parse(devinfo); 5707230130Smav HDA_BOOTHVERBOSE( 5708230130Smav device_printf(dev, "Building AFG tree...\n"); 5709230130Smav ); 5710230130Smav hdaa_audio_build_tree(devinfo); 5711230130Smav HDA_BOOTHVERBOSE( 5712230130Smav device_printf(dev, "Disabling unassociated " 5713230130Smav "widgets...\n"); 5714230130Smav ); 5715230130Smav hdaa_audio_disable_unas(devinfo); 5716230130Smav HDA_BOOTHVERBOSE( 5717230130Smav device_printf(dev, "Disabling nonselected " 5718230130Smav "inputs...\n"); 5719230130Smav ); 5720230130Smav hdaa_audio_disable_notselected(devinfo); 5721230130Smav HDA_BOOTHVERBOSE( 5722230130Smav device_printf(dev, "Disabling useless...\n"); 5723230130Smav ); 5724230130Smav hdaa_audio_disable_useless(devinfo); 5725230130Smav HDA_BOOTHVERBOSE( 5726230130Smav device_printf(dev, "Disabling " 5727230130Smav "crossassociatement connections...\n"); 5728230130Smav ); 5729230130Smav hdaa_audio_disable_crossas(devinfo); 5730230130Smav HDA_BOOTHVERBOSE( 5731230130Smav device_printf(dev, "Disabling useless...\n"); 5732230130Smav ); 5733230130Smav hdaa_audio_disable_useless(devinfo); 5734230130Smav HDA_BOOTHVERBOSE( 5735230130Smav device_printf(dev, "Binding associations to channels...\n"); 5736230130Smav ); 5737230130Smav hdaa_audio_bind_as(devinfo); 5738230130Smav HDA_BOOTHVERBOSE( 5739230130Smav device_printf(dev, "Assigning names to signal sources...\n"); 5740230130Smav ); 5741230130Smav hdaa_audio_assign_names(devinfo); 5742230130Smav HDA_BOOTHVERBOSE( 5743230451Smav device_printf(dev, "Preparing PCM devices...\n"); 5744230451Smav ); 5745230451Smav hdaa_prepare_pcms(devinfo); 5746230451Smav HDA_BOOTHVERBOSE( 5747230130Smav device_printf(dev, "Assigning mixers to the tree...\n"); 5748230130Smav ); 5749230130Smav hdaa_audio_assign_mixers(devinfo); 5750230130Smav HDA_BOOTHVERBOSE( 5751230130Smav device_printf(dev, "Preparing pin controls...\n"); 5752230130Smav ); 5753230130Smav hdaa_audio_prepare_pin_ctrl(devinfo); 5754230130Smav HDA_BOOTHVERBOSE( 5755230130Smav device_printf(dev, "AFG commit...\n"); 5756230130Smav ); 5757230130Smav hdaa_audio_commit(devinfo); 5758230130Smav HDA_BOOTHVERBOSE( 5759230130Smav device_printf(dev, "Applying direct built-in patches...\n"); 5760230130Smav ); 5761230130Smav hdaa_patch_direct(devinfo); 5762230130Smav HDA_BOOTHVERBOSE( 5763230312Smav device_printf(dev, "ELD init...\n"); 5764230312Smav ); 5765230312Smav hdaa_eld_init(devinfo); 5766230312Smav HDA_BOOTHVERBOSE( 5767230130Smav device_printf(dev, "HP switch init...\n"); 5768230130Smav ); 5769230130Smav hdaa_hp_switch_init(devinfo); 5770230130Smav HDA_BOOTHVERBOSE( 5771230130Smav device_printf(dev, "Creating PCM devices...\n"); 5772230130Smav ); 5773230130Smav hdaa_create_pcms(devinfo); 5774230130Smav 5775230130Smav HDA_BOOTVERBOSE( 5776230130Smav if (devinfo->quirks != 0) { 5777230130Smav device_printf(dev, "FG config/quirks:"); 5778230130Smav for (i = 0; i < HDAA_QUIRKS_TAB_LEN; i++) { 5779230130Smav if ((devinfo->quirks & 5780230130Smav hdaa_quirks_tab[i].value) == 5781230130Smav hdaa_quirks_tab[i].value) 5782230130Smav printf(" %s", hdaa_quirks_tab[i].key); 5783230130Smav } 5784230130Smav printf("\n"); 5785230130Smav } 5786230130Smav 5787230130Smav device_printf(dev, "\n"); 5788230130Smav device_printf(dev, "+-------------------+\n"); 5789230130Smav device_printf(dev, "| DUMPING HDA NODES |\n"); 5790230130Smav device_printf(dev, "+-------------------+\n"); 5791230130Smav hdaa_dump_nodes(devinfo); 5792230130Smav ); 5793230130Smav 5794230130Smav HDA_BOOTHVERBOSE( 5795230130Smav device_printf(dev, "\n"); 5796230130Smav device_printf(dev, "+------------------------+\n"); 5797230130Smav device_printf(dev, "| DUMPING HDA AMPLIFIERS |\n"); 5798230130Smav device_printf(dev, "+------------------------+\n"); 5799230130Smav device_printf(dev, "\n"); 5800230130Smav i = 0; 5801230130Smav while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) { 5802230130Smav device_printf(dev, "%3d: nid %3d %s (%s) index %d", i, 5803230130Smav (ctl->widget != NULL) ? ctl->widget->nid : -1, 5804230130Smav (ctl->ndir == HDAA_CTL_IN)?"in ":"out", 5805230130Smav (ctl->dir == HDAA_CTL_IN)?"in ":"out", 5806230130Smav ctl->index); 5807230130Smav if (ctl->childwidget != NULL) 5808230130Smav printf(" cnid %3d", ctl->childwidget->nid); 5809230130Smav else 5810230130Smav printf(" "); 5811230130Smav printf(" ossmask=0x%08x\n", 5812230130Smav ctl->ossmask); 5813230130Smav device_printf(dev, 5814230130Smav " mute: %d step: %3d size: %3d off: %3d%s\n", 5815230130Smav ctl->mute, ctl->step, ctl->size, ctl->offset, 5816230130Smav (ctl->enable == 0) ? " [DISABLED]" : 5817230130Smav ((ctl->ossmask == 0) ? " [UNUSED]" : "")); 5818230130Smav } 5819230130Smav ); 5820230130Smav 5821230130Smav HDA_BOOTVERBOSE( 5822230130Smav device_printf(dev, "\n"); 5823230130Smav ); 5824230130Smav} 5825230130Smav 5826230130Smavstatic void 5827230130Smavhdaa_unconfigure(device_t dev) 5828230130Smav{ 5829230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 5830230130Smav struct hdaa_widget *w; 5831230130Smav int i, j; 5832230130Smav 5833230130Smav HDA_BOOTHVERBOSE( 5834230130Smav device_printf(dev, "HP switch deinit...\n"); 5835230130Smav ); 5836230130Smav hdaa_hp_switch_deinit(devinfo); 5837230312Smav HDA_BOOTHVERBOSE( 5838230312Smav device_printf(dev, "ELD deinit...\n"); 5839230312Smav ); 5840230312Smav hdaa_eld_deinit(devinfo); 5841230130Smav free(devinfo->ctl, M_HDAA); 5842230130Smav devinfo->ctl = NULL; 5843230130Smav devinfo->ctlcnt = 0; 5844230130Smav free(devinfo->as, M_HDAA); 5845230130Smav devinfo->as = NULL; 5846230130Smav devinfo->ascnt = 0; 5847230130Smav free(devinfo->devs, M_HDAA); 5848230130Smav devinfo->devs = NULL; 5849230130Smav devinfo->num_devs = 0; 5850230130Smav free(devinfo->chans, M_HDAA); 5851230130Smav devinfo->chans = NULL; 5852230130Smav devinfo->num_chans = 0; 5853230130Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 5854230130Smav w = hdaa_widget_get(devinfo, i); 5855230130Smav if (w == NULL) 5856230130Smav continue; 5857230130Smav w->enable = 1; 5858230130Smav w->selconn = -1; 5859230130Smav w->pflags = 0; 5860230130Smav w->bindas = -1; 5861230130Smav w->bindseqmask = 0; 5862230130Smav w->ossdev = -1; 5863230130Smav w->ossmask = 0; 5864230130Smav for (j = 0; j < w->nconns; j++) 5865230130Smav w->connsenable[j] = 1; 5866230451Smav if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 5867230451Smav w->wclass.pin.config = w->wclass.pin.newconf; 5868230312Smav if (w->eld != NULL) { 5869230312Smav w->eld_len = 0; 5870230312Smav free(w->eld, M_HDAA); 5871230312Smav w->eld = NULL; 5872230312Smav } 5873230130Smav } 5874230130Smav} 5875230130Smav 5876230130Smavstatic int 5877230130Smavhdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS) 5878230130Smav{ 5879230130Smav struct hdaa_devinfo *devinfo = oidp->oid_arg1; 5880230130Smav device_t dev = devinfo->dev; 5881230130Smav char buf[256]; 5882230130Smav int n = 0, i, numgpi; 5883230130Smav uint32_t data = 0; 5884230130Smav 5885230130Smav buf[0] = 0; 5886230130Smav hdaa_lock(devinfo); 5887230130Smav numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); 5888230130Smav if (numgpi > 0) { 5889230130Smav data = hda_command(dev, 5890230130Smav HDA_CMD_GET_GPI_DATA(0, devinfo->nid)); 5891230130Smav } 5892230130Smav hdaa_unlock(devinfo); 5893230130Smav for (i = 0; i < numgpi; i++) { 5894230130Smav n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d", 5895230130Smav n != 0 ? " " : "", i, ((data >> i) & 1)); 5896230130Smav } 5897230130Smav return (sysctl_handle_string(oidp, buf, sizeof(buf), req)); 5898230130Smav} 5899230130Smav 5900230130Smavstatic int 5901230130Smavhdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS) 5902230130Smav{ 5903230130Smav struct hdaa_devinfo *devinfo = oidp->oid_arg1; 5904230130Smav device_t dev = devinfo->dev; 5905230130Smav char buf[256]; 5906230130Smav int n = 0, i, numgpio; 5907230130Smav uint32_t data = 0, enable = 0, dir = 0; 5908230130Smav 5909230130Smav buf[0] = 0; 5910230130Smav hdaa_lock(devinfo); 5911230130Smav numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); 5912230130Smav if (numgpio > 0) { 5913230130Smav data = hda_command(dev, 5914230130Smav HDA_CMD_GET_GPIO_DATA(0, devinfo->nid)); 5915230130Smav enable = hda_command(dev, 5916230130Smav HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid)); 5917230130Smav dir = hda_command(dev, 5918230130Smav HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid)); 5919230130Smav } 5920230130Smav hdaa_unlock(devinfo); 5921230130Smav for (i = 0; i < numgpio; i++) { 5922230130Smav n += snprintf(buf + n, sizeof(buf) - n, "%s%d=", 5923230130Smav n != 0 ? " " : "", i); 5924230130Smav if ((enable & (1 << i)) == 0) { 5925230130Smav n += snprintf(buf + n, sizeof(buf) - n, "disabled"); 5926230130Smav continue; 5927230130Smav } 5928230130Smav n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)", 5929230130Smav ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1)); 5930230130Smav } 5931230130Smav return (sysctl_handle_string(oidp, buf, sizeof(buf), req)); 5932230130Smav} 5933230130Smav 5934230130Smavstatic int 5935230130Smavhdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS) 5936230130Smav{ 5937230130Smav struct hdaa_devinfo *devinfo = oidp->oid_arg1; 5938230130Smav char buf[256]; 5939230130Smav int error, n = 0, i, numgpio; 5940230130Smav uint32_t gpio, x; 5941230130Smav 5942230130Smav gpio = devinfo->newgpio; 5943230130Smav numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); 5944230130Smav buf[0] = 0; 5945230130Smav for (i = 0; i < numgpio; i++) { 5946230130Smav x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i); 5947230130Smav n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s", 5948230130Smav n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]); 5949230130Smav } 5950230130Smav error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 5951230130Smav if (error != 0 || req->newptr == NULL) 5952230130Smav return (error); 5953230130Smav if (strncmp(buf, "0x", 2) == 0) 5954230130Smav gpio = strtol(buf + 2, NULL, 16); 5955230130Smav else 5956230130Smav gpio = hdaa_gpio_patch(gpio, buf); 5957230130Smav hdaa_lock(devinfo); 5958230130Smav devinfo->newgpio = devinfo->gpio = gpio; 5959230130Smav hdaa_gpio_commit(devinfo); 5960230130Smav hdaa_unlock(devinfo); 5961230130Smav return (0); 5962230130Smav} 5963230130Smav 5964230130Smavstatic int 5965230130Smavhdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS) 5966230130Smav{ 5967230130Smav struct hdaa_devinfo *devinfo = oidp->oid_arg1; 5968230130Smav device_t dev = devinfo->dev; 5969230130Smav char buf[256]; 5970230130Smav int n = 0, i, numgpo; 5971230130Smav uint32_t data = 0; 5972230130Smav 5973230130Smav buf[0] = 0; 5974230130Smav hdaa_lock(devinfo); 5975230130Smav numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); 5976230130Smav if (numgpo > 0) { 5977230130Smav data = hda_command(dev, 5978230130Smav HDA_CMD_GET_GPO_DATA(0, devinfo->nid)); 5979230130Smav } 5980230130Smav hdaa_unlock(devinfo); 5981230130Smav for (i = 0; i < numgpo; i++) { 5982230130Smav n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d", 5983230130Smav n != 0 ? " " : "", i, ((data >> i) & 1)); 5984230130Smav } 5985230130Smav return (sysctl_handle_string(oidp, buf, sizeof(buf), req)); 5986230130Smav} 5987230130Smav 5988230130Smavstatic int 5989230130Smavhdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS) 5990230130Smav{ 5991230130Smav struct hdaa_devinfo *devinfo = oidp->oid_arg1; 5992230130Smav char buf[256]; 5993230130Smav int error, n = 0, i, numgpo; 5994230130Smav uint32_t gpo, x; 5995230130Smav 5996230130Smav gpo = devinfo->newgpo; 5997230130Smav numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); 5998230130Smav buf[0] = 0; 5999230130Smav for (i = 0; i < numgpo; i++) { 6000230130Smav x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i); 6001230130Smav n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s", 6002230130Smav n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]); 6003230130Smav } 6004230130Smav error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 6005230130Smav if (error != 0 || req->newptr == NULL) 6006230130Smav return (error); 6007230130Smav if (strncmp(buf, "0x", 2) == 0) 6008230130Smav gpo = strtol(buf + 2, NULL, 16); 6009230130Smav else 6010230130Smav gpo = hdaa_gpio_patch(gpo, buf); 6011230130Smav hdaa_lock(devinfo); 6012230130Smav devinfo->newgpo = devinfo->gpo = gpo; 6013230130Smav hdaa_gpo_commit(devinfo); 6014230130Smav hdaa_unlock(devinfo); 6015230130Smav return (0); 6016230130Smav} 6017230130Smav 6018230130Smavstatic int 6019230130Smavhdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS) 6020230130Smav{ 6021230130Smav device_t dev; 6022230130Smav struct hdaa_devinfo *devinfo; 6023230130Smav int error, val; 6024230130Smav 6025230130Smav dev = oidp->oid_arg1; 6026230130Smav devinfo = device_get_softc(dev); 6027230130Smav if (devinfo == NULL) 6028230130Smav return (EINVAL); 6029230130Smav val = 0; 6030230130Smav error = sysctl_handle_int(oidp, &val, 0, req); 6031230130Smav if (error != 0 || req->newptr == NULL || val == 0) 6032230130Smav return (error); 6033230130Smav 6034230130Smav HDA_BOOTHVERBOSE( 6035230130Smav device_printf(dev, "Reconfiguration...\n"); 6036230130Smav ); 6037230130Smav if ((error = device_delete_children(dev)) != 0) 6038230130Smav return (error); 6039230130Smav hdaa_lock(devinfo); 6040230130Smav hdaa_unconfigure(dev); 6041230130Smav hdaa_configure(dev); 6042230130Smav hdaa_unlock(devinfo); 6043230130Smav bus_generic_attach(dev); 6044230130Smav HDA_BOOTHVERBOSE( 6045230130Smav device_printf(dev, "Reconfiguration done\n"); 6046230130Smav ); 6047230130Smav return (0); 6048230130Smav} 6049230130Smav 6050230130Smavstatic int 6051230130Smavhdaa_suspend(device_t dev) 6052230130Smav{ 6053230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 6054230130Smav int i; 6055230130Smav 6056230130Smav HDA_BOOTHVERBOSE( 6057230130Smav device_printf(dev, "Suspend...\n"); 6058230130Smav ); 6059230130Smav hdaa_lock(devinfo); 6060230130Smav HDA_BOOTHVERBOSE( 6061230130Smav device_printf(dev, "Stop streams...\n"); 6062230130Smav ); 6063230130Smav for (i = 0; i < devinfo->num_chans; i++) { 6064230130Smav if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) { 6065230130Smav devinfo->chans[i].flags |= HDAA_CHN_SUSPEND; 6066230130Smav hdaa_channel_stop(&devinfo->chans[i]); 6067230130Smav } 6068230130Smav } 6069230130Smav HDA_BOOTHVERBOSE( 6070230130Smav device_printf(dev, "HP switch deinit...\n"); 6071230130Smav ); 6072230130Smav hdaa_hp_switch_deinit(devinfo); 6073230130Smav HDA_BOOTHVERBOSE( 6074230130Smav device_printf(dev, "Power down FG" 6075230130Smav " nid=%d to the D3 state...\n", 6076230130Smav devinfo->nid); 6077230130Smav ); 6078230130Smav hda_command(devinfo->dev, 6079230130Smav HDA_CMD_SET_POWER_STATE(0, 6080230130Smav devinfo->nid, HDA_CMD_POWER_STATE_D3)); 6081230130Smav callout_stop(&devinfo->poll_jack); 6082230130Smav hdaa_unlock(devinfo); 6083230130Smav callout_drain(&devinfo->poll_jack); 6084230130Smav HDA_BOOTHVERBOSE( 6085230130Smav device_printf(dev, "Suspend done\n"); 6086230130Smav ); 6087230130Smav return (0); 6088230130Smav} 6089230130Smav 6090230130Smavstatic int 6091230130Smavhdaa_resume(device_t dev) 6092230130Smav{ 6093230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 6094230130Smav int i; 6095230130Smav 6096230130Smav HDA_BOOTHVERBOSE( 6097230130Smav device_printf(dev, "Resume...\n"); 6098230130Smav ); 6099230130Smav hdaa_lock(devinfo); 6100230130Smav HDA_BOOTHVERBOSE( 6101230130Smav device_printf(dev, "Power up audio FG nid=%d...\n", 6102230130Smav devinfo->nid); 6103230130Smav ); 6104230130Smav hdaa_powerup(devinfo); 6105230130Smav HDA_BOOTHVERBOSE( 6106230130Smav device_printf(dev, "AFG commit...\n"); 6107230130Smav ); 6108230130Smav hdaa_audio_commit(devinfo); 6109230130Smav HDA_BOOTHVERBOSE( 6110230130Smav device_printf(dev, "Applying direct built-in patches...\n"); 6111230130Smav ); 6112230130Smav hdaa_patch_direct(devinfo); 6113230130Smav HDA_BOOTHVERBOSE( 6114230130Smav device_printf(dev, "HP switch init...\n"); 6115230130Smav ); 6116230130Smav hdaa_hp_switch_init(devinfo); 6117230130Smav 6118230130Smav hdaa_unlock(devinfo); 6119230130Smav for (i = 0; i < devinfo->num_devs; i++) { 6120230130Smav struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i]; 6121230130Smav HDA_BOOTHVERBOSE( 6122230130Smav device_printf(pdevinfo->dev, 6123230130Smav "OSS mixer reinitialization...\n"); 6124230130Smav ); 6125230130Smav if (mixer_reinit(pdevinfo->dev) == -1) 6126230130Smav device_printf(pdevinfo->dev, 6127230130Smav "unable to reinitialize the mixer\n"); 6128230130Smav } 6129230130Smav hdaa_lock(devinfo); 6130230130Smav HDA_BOOTHVERBOSE( 6131230130Smav device_printf(dev, "Start streams...\n"); 6132230130Smav ); 6133230130Smav for (i = 0; i < devinfo->num_chans; i++) { 6134230130Smav if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) { 6135230130Smav devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND; 6136230130Smav hdaa_channel_start(&devinfo->chans[i]); 6137230130Smav } 6138230130Smav } 6139230130Smav hdaa_unlock(devinfo); 6140230130Smav HDA_BOOTHVERBOSE( 6141230130Smav device_printf(dev, "Resume done\n"); 6142230130Smav ); 6143230130Smav return (0); 6144230130Smav} 6145230130Smav 6146230130Smavstatic int 6147230130Smavhdaa_probe(device_t dev) 6148230130Smav{ 6149230130Smav char buf[128]; 6150230130Smav 6151230130Smav if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO) 6152230130Smav return (ENXIO); 6153230130Smav snprintf(buf, sizeof(buf), "%s Audio Function Group", 6154230130Smav device_get_desc(device_get_parent(dev))); 6155230130Smav device_set_desc_copy(dev, buf); 6156230130Smav return (BUS_PROBE_DEFAULT); 6157230130Smav} 6158230130Smav 6159230130Smavstatic int 6160230130Smavhdaa_attach(device_t dev) 6161230130Smav{ 6162230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 6163230130Smav uint32_t res; 6164230130Smav nid_t nid = hda_get_node_id(dev); 6165230130Smav 6166230130Smav devinfo->dev = dev; 6167230130Smav devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev); 6168230130Smav devinfo->nid = nid; 6169230130Smav devinfo->newquirks = -1; 6170230130Smav devinfo->newgpio = -1; 6171230130Smav devinfo->newgpo = -1; 6172230130Smav callout_init(&devinfo->poll_jack, CALLOUT_MPSAFE); 6173230130Smav devinfo->poll_ival = hz; 6174230130Smav 6175230130Smav hdaa_lock(devinfo); 6176230130Smav res = hda_command(dev, 6177230130Smav HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT)); 6178230130Smav hdaa_unlock(devinfo); 6179230130Smav 6180230130Smav devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res); 6181230130Smav devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res); 6182230130Smav devinfo->endnode = devinfo->startnode + devinfo->nodecnt; 6183230130Smav 6184230130Smav HDA_BOOTVERBOSE( 6185230130Smav device_printf(dev, 6186230130Smav "Audio Function Group at nid=%d: %d subnodes %d-%d\n", 6187230130Smav nid, devinfo->nodecnt, 6188230130Smav devinfo->startnode, devinfo->endnode - 1); 6189230130Smav ); 6190230130Smav 6191230130Smav if (devinfo->nodecnt > 0) 6192230130Smav devinfo->widget = (struct hdaa_widget *)malloc( 6193230130Smav sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA, 6194230130Smav M_WAITOK | M_ZERO); 6195230130Smav else 6196230130Smav devinfo->widget = NULL; 6197230130Smav 6198230130Smav hdaa_lock(devinfo); 6199230130Smav HDA_BOOTHVERBOSE( 6200230130Smav device_printf(dev, "Powering up...\n"); 6201230130Smav ); 6202230130Smav hdaa_powerup(devinfo); 6203230130Smav HDA_BOOTHVERBOSE( 6204230130Smav device_printf(dev, "Parsing audio FG...\n"); 6205230130Smav ); 6206230130Smav hdaa_audio_parse(devinfo); 6207230130Smav HDA_BOOTVERBOSE( 6208230130Smav device_printf(dev, "Original pins configuration:\n"); 6209230130Smav hdaa_dump_pin_configs(devinfo); 6210230130Smav ); 6211230130Smav hdaa_configure(dev); 6212230130Smav hdaa_unlock(devinfo); 6213230130Smav 6214230130Smav SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 6215230130Smav SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 6216230130Smav "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 6217230130Smav &devinfo->newquirks, sizeof(&devinfo->newquirks), 6218230130Smav hdaa_sysctl_quirks, "A", "Configuration options"); 6219230130Smav SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 6220230130Smav SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 6221230130Smav "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 6222230130Smav devinfo, sizeof(devinfo), 6223230130Smav hdaa_sysctl_gpi_state, "A", "GPI state"); 6224230130Smav SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 6225230130Smav SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 6226230130Smav "gpio_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 6227230130Smav devinfo, sizeof(devinfo), 6228230130Smav hdaa_sysctl_gpio_state, "A", "GPIO state"); 6229230130Smav SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 6230230130Smav SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 6231230130Smav "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 6232230130Smav devinfo, sizeof(devinfo), 6233230130Smav hdaa_sysctl_gpio_config, "A", "GPIO configuration"); 6234230130Smav SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 6235230130Smav SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 6236230130Smav "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 6237230130Smav devinfo, sizeof(devinfo), 6238230130Smav hdaa_sysctl_gpo_state, "A", "GPO state"); 6239230130Smav SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 6240230130Smav SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 6241230130Smav "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 6242230130Smav devinfo, sizeof(devinfo), 6243230130Smav hdaa_sysctl_gpo_config, "A", "GPO configuration"); 6244230130Smav SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 6245230130Smav SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 6246230130Smav "reconfig", CTLTYPE_INT | CTLFLAG_RW, 6247230130Smav dev, sizeof(dev), 6248230130Smav hdaa_sysctl_reconfig, "I", "Reprocess configuration"); 6249230130Smav bus_generic_attach(dev); 6250230130Smav return (0); 6251230130Smav} 6252230130Smav 6253230130Smavstatic int 6254230130Smavhdaa_detach(device_t dev) 6255230130Smav{ 6256230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 6257230130Smav int error; 6258230130Smav 6259230130Smav if ((error = device_delete_children(dev)) != 0) 6260230130Smav return (error); 6261230130Smav 6262230130Smav hdaa_lock(devinfo); 6263230130Smav hdaa_unconfigure(dev); 6264230130Smav devinfo->poll_ival = 0; 6265230130Smav callout_stop(&devinfo->poll_jack); 6266230130Smav hdaa_unlock(devinfo); 6267230130Smav callout_drain(&devinfo->poll_jack); 6268230130Smav 6269230130Smav free(devinfo->widget, M_HDAA); 6270230130Smav return (0); 6271230130Smav} 6272230130Smav 6273230130Smavstatic int 6274230130Smavhdaa_print_child(device_t dev, device_t child) 6275230130Smav{ 6276230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 6277230130Smav struct hdaa_pcm_devinfo *pdevinfo = 6278230130Smav (struct hdaa_pcm_devinfo *)device_get_ivars(child); 6279230130Smav struct hdaa_audio_as *as; 6280230130Smav int retval, first = 1, i; 6281230130Smav 6282230130Smav retval = bus_print_child_header(dev, child); 6283230130Smav retval += printf(" at nid "); 6284230130Smav if (pdevinfo->playas >= 0) { 6285230130Smav as = &devinfo->as[pdevinfo->playas]; 6286230130Smav for (i = 0; i < 16; i++) { 6287230130Smav if (as->pins[i] <= 0) 6288230130Smav continue; 6289230130Smav retval += printf("%s%d", first ? "" : ",", as->pins[i]); 6290230130Smav first = 0; 6291230130Smav } 6292230130Smav } 6293230130Smav if (pdevinfo->recas >= 0) { 6294230130Smav if (pdevinfo->playas >= 0) { 6295230130Smav retval += printf(" and "); 6296230130Smav first = 1; 6297230130Smav } 6298230130Smav as = &devinfo->as[pdevinfo->recas]; 6299230130Smav for (i = 0; i < 16; i++) { 6300230130Smav if (as->pins[i] <= 0) 6301230130Smav continue; 6302230130Smav retval += printf("%s%d", first ? "" : ",", as->pins[i]); 6303230130Smav first = 0; 6304230130Smav } 6305230130Smav } 6306230130Smav retval += bus_print_child_footer(dev, child); 6307230130Smav 6308230130Smav return (retval); 6309230130Smav} 6310230130Smav 6311230130Smavstatic int 6312230130Smavhdaa_child_location_str(device_t dev, device_t child, char *buf, 6313230130Smav size_t buflen) 6314230130Smav{ 6315230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 6316230130Smav struct hdaa_pcm_devinfo *pdevinfo = 6317230130Smav (struct hdaa_pcm_devinfo *)device_get_ivars(child); 6318230130Smav struct hdaa_audio_as *as; 6319230130Smav int first = 1, i, len = 0; 6320230130Smav 6321230130Smav len += snprintf(buf + len, buflen - len, "nid="); 6322230130Smav if (pdevinfo->playas >= 0) { 6323230130Smav as = &devinfo->as[pdevinfo->playas]; 6324230130Smav for (i = 0; i < 16; i++) { 6325230130Smav if (as->pins[i] <= 0) 6326230130Smav continue; 6327230130Smav len += snprintf(buf + len, buflen - len, 6328230130Smav "%s%d", first ? "" : ",", as->pins[i]); 6329230130Smav first = 0; 6330230130Smav } 6331230130Smav } 6332230130Smav if (pdevinfo->recas >= 0) { 6333230130Smav as = &devinfo->as[pdevinfo->recas]; 6334230130Smav for (i = 0; i < 16; i++) { 6335230130Smav if (as->pins[i] <= 0) 6336230130Smav continue; 6337230130Smav len += snprintf(buf + len, buflen - len, 6338230130Smav "%s%d", first ? "" : ",", as->pins[i]); 6339230130Smav first = 0; 6340230130Smav } 6341230130Smav } 6342230130Smav return (0); 6343230130Smav} 6344230130Smav 6345230130Smavstatic void 6346230130Smavhdaa_stream_intr(device_t dev, int dir, int stream) 6347230130Smav{ 6348230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 6349230130Smav struct hdaa_chan *ch; 6350230130Smav int i; 6351230130Smav 6352230130Smav for (i = 0; i < devinfo->num_chans; i++) { 6353230130Smav ch = &devinfo->chans[i]; 6354230130Smav if (!(ch->flags & HDAA_CHN_RUNNING)) 6355230130Smav continue; 6356230130Smav if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) && 6357230130Smav ch->sid == stream) { 6358230130Smav hdaa_unlock(devinfo); 6359230130Smav chn_intr(ch->c); 6360230130Smav hdaa_lock(devinfo); 6361230130Smav } 6362230130Smav } 6363230130Smav} 6364230130Smav 6365230130Smavstatic void 6366230130Smavhdaa_unsol_intr(device_t dev, uint32_t resp) 6367230130Smav{ 6368230130Smav struct hdaa_devinfo *devinfo = device_get_softc(dev); 6369230312Smav struct hdaa_widget *w; 6370230312Smav int i, tag, flags; 6371230130Smav 6372230312Smav HDA_BOOTHVERBOSE( 6373230312Smav device_printf(dev, "Unsolicited response %08x\n", resp); 6374230312Smav ); 6375230130Smav tag = resp >> 26; 6376230312Smav for (i = devinfo->startnode; i < devinfo->endnode; i++) { 6377230312Smav w = hdaa_widget_get(devinfo, i); 6378230312Smav if (w == NULL || w->enable == 0 || w->type != 6379230312Smav HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) 6380230312Smav continue; 6381230312Smav if (w->unsol != tag) 6382230312Smav continue; 6383230312Smav if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) || 6384230312Smav HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap)) 6385230312Smav flags = resp & 0x03; 6386230312Smav else 6387230312Smav flags = 0x01; 6388230312Smav if (flags & 0x01) 6389230312Smav hdaa_hp_switch_handler(w); 6390230312Smav if (flags & 0x02) 6391230312Smav hdaa_eld_handler(w); 6392230130Smav } 6393230130Smav} 6394230130Smav 6395230130Smavstatic device_method_t hdaa_methods[] = { 6396230130Smav /* device interface */ 6397230130Smav DEVMETHOD(device_probe, hdaa_probe), 6398230130Smav DEVMETHOD(device_attach, hdaa_attach), 6399230130Smav DEVMETHOD(device_detach, hdaa_detach), 6400230130Smav DEVMETHOD(device_suspend, hdaa_suspend), 6401230130Smav DEVMETHOD(device_resume, hdaa_resume), 6402230130Smav /* Bus interface */ 6403230130Smav DEVMETHOD(bus_print_child, hdaa_print_child), 6404230130Smav DEVMETHOD(bus_child_location_str, hdaa_child_location_str), 6405230130Smav DEVMETHOD(hdac_stream_intr, hdaa_stream_intr), 6406230130Smav DEVMETHOD(hdac_unsol_intr, hdaa_unsol_intr), 6407230130Smav DEVMETHOD(hdac_pindump, hdaa_pindump), 6408230130Smav { 0, 0 } 6409230130Smav}; 6410230130Smav 6411230130Smavstatic driver_t hdaa_driver = { 6412230130Smav "hdaa", 6413230130Smav hdaa_methods, 6414230130Smav sizeof(struct hdaa_devinfo), 6415230130Smav}; 6416230130Smav 6417230130Smavstatic devclass_t hdaa_devclass; 6418230130Smav 6419230130SmavDRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, 0, 0); 6420230130Smav 6421230130Smavstatic void 6422230130Smavhdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid, 6423230130Smav char *buf, int buflen) 6424230130Smav{ 6425230130Smav struct hdaa_audio_as *as; 6426230130Smav int c; 6427230130Smav 6428230130Smav as = &devinfo->as[asid]; 6429230130Smav c = devinfo->chans[as->chans[0]].channels; 6430230130Smav if (c == 1) 6431230130Smav snprintf(buf, buflen, "mono"); 6432230130Smav else if (c == 2) 6433230130Smav buf[0] = 0; 6434230130Smav else if (as->pinset == 0x0003) 6435230130Smav snprintf(buf, buflen, "3.1"); 6436230130Smav else if (as->pinset == 0x0005 || as->pinset == 0x0011) 6437230130Smav snprintf(buf, buflen, "4.0"); 6438230130Smav else if (as->pinset == 0x0007 || as->pinset == 0x0013) 6439230130Smav snprintf(buf, buflen, "5.1"); 6440230130Smav else if (as->pinset == 0x0017) 6441230130Smav snprintf(buf, buflen, "7.1"); 6442230130Smav else 6443230130Smav snprintf(buf, buflen, "%dch", c); 6444230130Smav} 6445230130Smav 6446230130Smavstatic int 6447230488Smavhdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS) 6448230488Smav{ 6449230488Smav struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1; 6450230488Smav struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo; 6451230488Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 6452230488Smav struct hdaa_chan *ch; 6453230488Smav int error, val, i; 6454230488Smav uint32_t pcmcap; 6455230488Smav 6456230488Smav ch = &devinfo->chans[as->chans[0]]; 6457230488Smav val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 : 6458230488Smav ((ch->bit32 == 2) ? 20 : 0)); 6459230488Smav error = sysctl_handle_int(oidp, &val, 0, req); 6460230488Smav if (error != 0 || req->newptr == NULL) 6461230488Smav return (error); 6462230488Smav pcmcap = ch->supp_pcm_size_rate; 6463230488Smav if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap)) 6464230488Smav ch->bit32 = 4; 6465230488Smav else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap)) 6466230488Smav ch->bit32 = 3; 6467230488Smav else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap)) 6468230488Smav ch->bit32 = 2; 6469230488Smav else 6470230488Smav return (EINVAL); 6471230488Smav for (i = 1; i < as->num_chans; i++) 6472230488Smav devinfo->chans[as->chans[i]].bit32 = ch->bit32; 6473230488Smav return (0); 6474230488Smav} 6475230488Smav 6476230488Smavstatic int 6477230130Smavhdaa_pcm_probe(device_t dev) 6478230130Smav{ 6479230130Smav struct hdaa_pcm_devinfo *pdevinfo = 6480230130Smav (struct hdaa_pcm_devinfo *)device_get_ivars(dev); 6481230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 6482230130Smav char chans1[8], chans2[8]; 6483230130Smav char buf[128]; 6484230130Smav int loc1, loc2; 6485230130Smav 6486230130Smav if (pdevinfo->playas >= 0) 6487230130Smav loc1 = devinfo->as[pdevinfo->playas].location; 6488230130Smav else 6489230130Smav loc1 = devinfo->as[pdevinfo->recas].location; 6490230130Smav if (pdevinfo->recas >= 0) 6491230130Smav loc2 = devinfo->as[pdevinfo->recas].location; 6492230130Smav else 6493230130Smav loc2 = loc1; 6494230130Smav if (loc1 != loc2) 6495230130Smav loc1 = -2; 6496230130Smav if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0') 6497230130Smav loc1 = -2; 6498230130Smav chans1[0] = 0; 6499230130Smav chans2[0] = 0; 6500230130Smav if (pdevinfo->playas >= 0) 6501230130Smav hdaa_chan_formula(devinfo, pdevinfo->playas, 6502230130Smav chans1, sizeof(chans1)); 6503230130Smav if (pdevinfo->recas >= 0) 6504230130Smav hdaa_chan_formula(devinfo, pdevinfo->recas, 6505230130Smav chans2, sizeof(chans2)); 6506230130Smav if (chans1[0] != 0 || chans2[0] != 0) { 6507230130Smav if (chans1[0] == 0 && pdevinfo->playas >= 0) 6508230130Smav snprintf(chans1, sizeof(chans1), "2.0"); 6509230130Smav else if (chans2[0] == 0 && pdevinfo->recas >= 0) 6510230130Smav snprintf(chans2, sizeof(chans2), "2.0"); 6511230130Smav if (strcmp(chans1, chans2) == 0) 6512230130Smav chans2[0] = 0; 6513230130Smav } 6514230130Smav snprintf(buf, sizeof(buf), "%s PCM (%s%s%s%s%s%s%s)", 6515230130Smav device_get_desc(device_get_parent(device_get_parent(dev))), 6516230130Smav loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "", 6517230130Smav (pdevinfo->digital == 3)?"DisplayPort": 6518230130Smav ((pdevinfo->digital == 2)?"HDMI": 6519230130Smav ((pdevinfo->digital)?"Digital":"Analog")), 6520230130Smav chans1[0] ? " " : "", chans1, 6521230130Smav chans2[0] ? "/" : "", chans2); 6522230130Smav device_set_desc_copy(dev, buf); 6523230130Smav return (BUS_PROBE_SPECIFIC); 6524230130Smav} 6525230130Smav 6526230130Smavstatic int 6527230130Smavhdaa_pcm_attach(device_t dev) 6528230130Smav{ 6529230130Smav struct hdaa_pcm_devinfo *pdevinfo = 6530230130Smav (struct hdaa_pcm_devinfo *)device_get_ivars(dev); 6531230130Smav struct hdaa_devinfo *devinfo = pdevinfo->devinfo; 6532230130Smav struct hdaa_audio_as *as; 6533230488Smav struct snddev_info *d; 6534230130Smav char status[SND_STATUSLEN]; 6535230130Smav int i; 6536230130Smav 6537230130Smav pdevinfo->chan_size = pcm_getbuffersize(dev, 6538230130Smav HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX); 6539230130Smav 6540230130Smav HDA_BOOTVERBOSE( 6541230130Smav device_printf(dev, "+--------------------------------------+\n"); 6542230130Smav device_printf(dev, "| DUMPING PCM Playback/Record Channels |\n"); 6543230130Smav device_printf(dev, "+--------------------------------------+\n"); 6544230130Smav hdaa_dump_pcmchannels(pdevinfo); 6545230130Smav device_printf(dev, "\n"); 6546230130Smav device_printf(dev, "+-------------------------------+\n"); 6547230130Smav device_printf(dev, "| DUMPING Playback/Record Paths |\n"); 6548230130Smav device_printf(dev, "+-------------------------------+\n"); 6549230130Smav hdaa_dump_dac(pdevinfo); 6550230130Smav hdaa_dump_adc(pdevinfo); 6551230130Smav hdaa_dump_mix(pdevinfo); 6552230130Smav device_printf(dev, "\n"); 6553230130Smav device_printf(dev, "+-------------------------+\n"); 6554230130Smav device_printf(dev, "| DUMPING Volume Controls |\n"); 6555230130Smav device_printf(dev, "+-------------------------+\n"); 6556230130Smav hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME); 6557230130Smav hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM); 6558230130Smav hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD); 6559230130Smav hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC); 6560230130Smav hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR); 6561230130Smav hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE); 6562230130Smav hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER); 6563230130Smav hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV); 6564230130Smav hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX); 6565230130Smav hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN); 6566230130Smav hdaa_dump_ctls(pdevinfo, NULL, 0); 6567230130Smav device_printf(dev, "\n"); 6568230130Smav ); 6569230130Smav 6570230130Smav if (resource_int_value(device_get_name(dev), 6571230130Smav device_get_unit(dev), "blocksize", &i) == 0 && i > 0) { 6572230130Smav i &= HDA_BLK_ALIGN; 6573230130Smav if (i < HDA_BLK_MIN) 6574230130Smav i = HDA_BLK_MIN; 6575230130Smav pdevinfo->chan_blkcnt = pdevinfo->chan_size / i; 6576230130Smav i = 0; 6577230130Smav while (pdevinfo->chan_blkcnt >> i) 6578230130Smav i++; 6579230130Smav pdevinfo->chan_blkcnt = 1 << (i - 1); 6580230130Smav if (pdevinfo->chan_blkcnt < HDA_BDL_MIN) 6581230130Smav pdevinfo->chan_blkcnt = HDA_BDL_MIN; 6582230130Smav else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX) 6583230130Smav pdevinfo->chan_blkcnt = HDA_BDL_MAX; 6584230130Smav } else 6585230130Smav pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT; 6586230130Smav 6587230130Smav /* 6588230130Smav * We don't register interrupt handler with snd_setup_intr 6589230130Smav * in pcm device. Mark pcm device as MPSAFE manually. 6590230130Smav */ 6591230130Smav pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE); 6592230130Smav 6593230130Smav HDA_BOOTHVERBOSE( 6594230130Smav device_printf(dev, "OSS mixer initialization...\n"); 6595230130Smav ); 6596230130Smav if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0) 6597230130Smav device_printf(dev, "Can't register mixer\n"); 6598230451Smav else 6599230451Smav hdaa_audio_ctl_set_defaults(pdevinfo); 6600230130Smav 6601230130Smav HDA_BOOTHVERBOSE( 6602230130Smav device_printf(dev, "Registering PCM channels...\n"); 6603230130Smav ); 6604230130Smav if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0, 6605230130Smav (pdevinfo->recas >= 0)?1:0) != 0) 6606230130Smav device_printf(dev, "Can't register PCM\n"); 6607230130Smav 6608230130Smav pdevinfo->registered++; 6609230130Smav 6610230488Smav d = device_get_softc(dev); 6611230130Smav if (pdevinfo->playas >= 0) { 6612230130Smav as = &devinfo->as[pdevinfo->playas]; 6613230130Smav for (i = 0; i < as->num_chans; i++) 6614230130Smav pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class, 6615230130Smav &devinfo->chans[as->chans[i]]); 6616230488Smav SYSCTL_ADD_PROC(&d->play_sysctl_ctx, 6617230488Smav SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO, 6618230488Smav "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 6619230488Smav as, sizeof(as), hdaa_sysctl_32bit, "I", 6620230488Smav "Resolution of 32bit samples (20/24/32bit)"); 6621230130Smav } 6622230130Smav if (pdevinfo->recas >= 0) { 6623230130Smav as = &devinfo->as[pdevinfo->recas]; 6624230130Smav for (i = 0; i < as->num_chans; i++) 6625230130Smav pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class, 6626230130Smav &devinfo->chans[as->chans[i]]); 6627230488Smav SYSCTL_ADD_PROC(&d->rec_sysctl_ctx, 6628230488Smav SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO, 6629230488Smav "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 6630230488Smav as, sizeof(as), hdaa_sysctl_32bit, "I", 6631230488Smav "Resolution of 32bit samples (20/24/32bit)"); 6632230130Smav } 6633230130Smav 6634230130Smav snprintf(status, SND_STATUSLEN, "on %s %s", 6635230130Smav device_get_nameunit(device_get_parent(dev)), 6636230130Smav PCM_KLDSTRING(snd_hda)); 6637230130Smav pcm_setstatus(dev, status); 6638230130Smav 6639230130Smav return (0); 6640230130Smav} 6641230130Smav 6642230130Smavstatic int 6643230130Smavhdaa_pcm_detach(device_t dev) 6644230130Smav{ 6645230130Smav struct hdaa_pcm_devinfo *pdevinfo = 6646230130Smav (struct hdaa_pcm_devinfo *)device_get_ivars(dev); 6647230130Smav int err; 6648230130Smav 6649230130Smav if (pdevinfo->registered > 0) { 6650230130Smav err = pcm_unregister(dev); 6651230130Smav if (err != 0) 6652230130Smav return (err); 6653230130Smav } 6654230130Smav 6655230130Smav return (0); 6656230130Smav} 6657230130Smav 6658230130Smavstatic device_method_t hdaa_pcm_methods[] = { 6659230130Smav /* device interface */ 6660230130Smav DEVMETHOD(device_probe, hdaa_pcm_probe), 6661230130Smav DEVMETHOD(device_attach, hdaa_pcm_attach), 6662230130Smav DEVMETHOD(device_detach, hdaa_pcm_detach), 6663230130Smav { 0, 0 } 6664230130Smav}; 6665230130Smav 6666230130Smavstatic driver_t hdaa_pcm_driver = { 6667230130Smav "pcm", 6668230130Smav hdaa_pcm_methods, 6669230130Smav PCM_SOFTC_SIZE, 6670230130Smav}; 6671230130Smav 6672230130SmavDRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, 0, 0); 6673230130SmavMODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 6674230130SmavMODULE_VERSION(snd_hda, 1); 6675