2 * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3 * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4 * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Intel High Definition Audio (Audio function) driver for FreeBSD.
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
37 #include <dev/sound/pcm/sound.h>
39 #include <sys/ctype.h>
40 #include <sys/taskqueue.h>
42 #include <dev/sound/pci/hda/hdac.h>
43 #include <dev/sound/pci/hda/hdaa.h>
44 #include <dev/sound/pci/hda/hda_reg.h>
48 SND_DECLARE_FILE("$FreeBSD$");
50 #define hdaa_lock(devinfo) snd_mtxlock((devinfo)->lock)
51 #define hdaa_unlock(devinfo) snd_mtxunlock((devinfo)->lock)
52 #define hdaa_lockassert(devinfo) snd_mtxassert((devinfo)->lock)
53 #define hdaa_lockowned(devinfo) mtx_owned((devinfo)->lock)
58 } hdaa_quirks_tab[] = {
59 { "softpcmvol", HDAA_QUIRK_SOFTPCMVOL },
60 { "fixedrate", HDAA_QUIRK_FIXEDRATE },
61 { "forcestereo", HDAA_QUIRK_FORCESTEREO },
62 { "eapdinv", HDAA_QUIRK_EAPDINV },
63 { "senseinv", HDAA_QUIRK_SENSEINV },
64 { "ivref50", HDAA_QUIRK_IVREF50 },
65 { "ivref80", HDAA_QUIRK_IVREF80 },
66 { "ivref100", HDAA_QUIRK_IVREF100 },
67 { "ovref50", HDAA_QUIRK_OVREF50 },
68 { "ovref80", HDAA_QUIRK_OVREF80 },
69 { "ovref100", HDAA_QUIRK_OVREF100 },
70 { "ivref", HDAA_QUIRK_IVREF },
71 { "ovref", HDAA_QUIRK_OVREF },
72 { "vref", HDAA_QUIRK_VREF },
75 #define HDA_PARSE_MAXDEPTH 10
77 MALLOC_DEFINE(M_HDAA, "hdaa", "HDA Audio");
79 static const char *HDA_COLORS[16] = {"Unknown", "Black", "Grey", "Blue",
80 "Green", "Red", "Orange", "Yellow", "Purple", "Pink", "Res.A", "Res.B",
81 "Res.C", "Res.D", "White", "Other"};
83 static const char *HDA_DEVS[16] = {"Line-out", "Speaker", "Headphones", "CD",
84 "SPDIF-out", "Digital-out", "Modem-line", "Modem-handset", "Line-in",
85 "AUX", "Mic", "Telephony", "SPDIF-in", "Digital-in", "Res.E", "Other"};
87 static const char *HDA_CONNS[4] = {"Jack", "None", "Fixed", "Both"};
89 static const char *HDA_CONNECTORS[16] = {
90 "Unknown", "1/8", "1/4", "ATAPI", "RCA", "Optical", "Digital", "Analog",
91 "DIN", "XLR", "RJ-11", "Combo", "0xc", "0xd", "0xe", "Other" };
93 static const char *HDA_LOCS[64] = {
94 "0x00", "Rear", "Front", "Left", "Right", "Top", "Bottom", "Rear-panel",
95 "Drive-bay", "0x09", "0x0a", "0x0b", "0x0c", "0x0d", "0x0e", "0x0f",
96 "Internal", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "Riser",
97 "0x18", "Onboard", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f",
98 "External", "Ext-Rear", "Ext-Front", "Ext-Left", "Ext-Right", "Ext-Top", "Ext-Bottom", "0x07",
99 "0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f",
100 "Other", "0x31", "0x32", "0x33", "0x34", "0x35", "Other-Bott", "Lid-In",
101 "Lid-Out", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f" };
103 static const char *HDA_GPIO_ACTIONS[8] = {
104 "keep", "set", "clear", "disable", "input", "0x05", "0x06", "0x07"};
106 static const char *HDA_HDMI_CODING_TYPES[18] = {
107 "undefined", "LPCM", "AC-3", "MPEG1", "MP3", "MPEG2", "AAC-LC", "DTS",
108 "ATRAC", "DSD", "E-AC-3", "DTS-HD", "MLP", "DST", "WMAPro", "HE-AAC",
109 "HE-AACv2", "MPEG-Surround"
113 static uint32_t hdaa_fmt[] = {
114 SND_FORMAT(AFMT_S16_LE, 2, 0),
118 static struct pcmchan_caps hdaa_caps = {48000, 48000, hdaa_fmt, 0};
120 static const struct {
127 { 8000, 1, 0x0000, 0x0000, 0x0500 }, /* (48000 * 1) / 6 */
128 { 9600, 0, 0x0000, 0x0000, 0x0400 }, /* (48000 * 1) / 5 */
129 { 12000, 0, 0x0000, 0x0000, 0x0300 }, /* (48000 * 1) / 4 */
130 { 16000, 1, 0x0000, 0x0000, 0x0200 }, /* (48000 * 1) / 3 */
131 { 18000, 0, 0x0000, 0x1000, 0x0700 }, /* (48000 * 3) / 8 */
132 { 19200, 0, 0x0000, 0x0800, 0x0400 }, /* (48000 * 2) / 5 */
133 { 24000, 0, 0x0000, 0x0000, 0x0100 }, /* (48000 * 1) / 2 */
134 { 28800, 0, 0x0000, 0x1000, 0x0400 }, /* (48000 * 3) / 5 */
135 { 32000, 1, 0x0000, 0x0800, 0x0200 }, /* (48000 * 2) / 3 */
136 { 36000, 0, 0x0000, 0x1000, 0x0300 }, /* (48000 * 3) / 4 */
137 { 38400, 0, 0x0000, 0x1800, 0x0400 }, /* (48000 * 4) / 5 */
138 { 48000, 1, 0x0000, 0x0000, 0x0000 }, /* (48000 * 1) / 1 */
139 { 64000, 0, 0x0000, 0x1800, 0x0200 }, /* (48000 * 4) / 3 */
140 { 72000, 0, 0x0000, 0x1000, 0x0100 }, /* (48000 * 3) / 2 */
141 { 96000, 1, 0x0000, 0x0800, 0x0000 }, /* (48000 * 2) / 1 */
142 { 144000, 0, 0x0000, 0x1000, 0x0000 }, /* (48000 * 3) / 1 */
143 { 192000, 1, 0x0000, 0x1800, 0x0000 }, /* (48000 * 4) / 1 */
144 { 8820, 0, 0x4000, 0x0000, 0x0400 }, /* (44100 * 1) / 5 */
145 { 11025, 1, 0x4000, 0x0000, 0x0300 }, /* (44100 * 1) / 4 */
146 { 12600, 0, 0x4000, 0x0800, 0x0600 }, /* (44100 * 2) / 7 */
147 { 14700, 0, 0x4000, 0x0000, 0x0200 }, /* (44100 * 1) / 3 */
148 { 17640, 0, 0x4000, 0x0800, 0x0400 }, /* (44100 * 2) / 5 */
149 { 18900, 0, 0x4000, 0x1000, 0x0600 }, /* (44100 * 3) / 7 */
150 { 22050, 1, 0x4000, 0x0000, 0x0100 }, /* (44100 * 1) / 2 */
151 { 25200, 0, 0x4000, 0x1800, 0x0600 }, /* (44100 * 4) / 7 */
152 { 26460, 0, 0x4000, 0x1000, 0x0400 }, /* (44100 * 3) / 5 */
153 { 29400, 0, 0x4000, 0x0800, 0x0200 }, /* (44100 * 2) / 3 */
154 { 33075, 0, 0x4000, 0x1000, 0x0300 }, /* (44100 * 3) / 4 */
155 { 35280, 0, 0x4000, 0x1800, 0x0400 }, /* (44100 * 4) / 5 */
156 { 44100, 1, 0x4000, 0x0000, 0x0000 }, /* (44100 * 1) / 1 */
157 { 58800, 0, 0x4000, 0x1800, 0x0200 }, /* (44100 * 4) / 3 */
158 { 66150, 0, 0x4000, 0x1000, 0x0100 }, /* (44100 * 3) / 2 */
159 { 88200, 1, 0x4000, 0x0800, 0x0000 }, /* (44100 * 2) / 1 */
160 { 132300, 0, 0x4000, 0x1000, 0x0000 }, /* (44100 * 3) / 1 */
161 { 176400, 1, 0x4000, 0x1800, 0x0000 }, /* (44100 * 4) / 1 */
163 #define HDA_RATE_TAB_LEN (sizeof(hda_rate_tab) / sizeof(hda_rate_tab[0]))
165 const static char *ossnames[] = SOUND_DEVICE_NAMES;
167 /****************************************************************************
168 * Function prototypes
169 ****************************************************************************/
170 static int hdaa_pcmchannel_setup(struct hdaa_chan *);
172 static void hdaa_widget_connection_select(struct hdaa_widget *, uint8_t);
173 static void hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *,
175 static struct hdaa_audio_ctl *hdaa_audio_ctl_amp_get(struct hdaa_devinfo *,
176 nid_t, int, int, int);
177 static void hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *,
178 nid_t, int, int, int, int, int, int);
180 static void hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf);
183 hdaa_audio_ctl_ossmixer_mask2allname(uint32_t mask, char *buf, size_t len)
188 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
189 if (mask & (1 << i)) {
191 strlcat(buf, ", ", len);
192 strlcat(buf, ossnames[i], len);
199 static struct hdaa_audio_ctl *
200 hdaa_audio_ctl_each(struct hdaa_devinfo *devinfo, int *index)
202 if (devinfo == NULL ||
203 index == NULL || devinfo->ctl == NULL ||
204 devinfo->ctlcnt < 1 ||
205 *index < 0 || *index >= devinfo->ctlcnt)
207 return (&devinfo->ctl[(*index)++]);
210 static struct hdaa_audio_ctl *
211 hdaa_audio_ctl_amp_get(struct hdaa_devinfo *devinfo, nid_t nid, int dir,
214 struct hdaa_audio_ctl *ctl;
217 if (devinfo == NULL || devinfo->ctl == NULL)
221 while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
222 if (ctl->enable == 0)
224 if (ctl->widget->nid != nid)
226 if (dir && ctl->ndir != dir)
228 if (index >= 0 && ctl->ndir == HDAA_CTL_IN &&
229 ctl->dir == ctl->ndir && ctl->index != index)
232 if (found == cnt || cnt <= 0)
239 static const struct matrix {
240 struct pcmchan_matrix m;
243 { SND_CHN_MATRIX_MAP_1_0, 1 },
244 { SND_CHN_MATRIX_MAP_2_0, 1 },
245 { SND_CHN_MATRIX_MAP_2_1, 0 },
246 { SND_CHN_MATRIX_MAP_3_0, 0 },
247 { SND_CHN_MATRIX_MAP_3_1, 0 },
248 { SND_CHN_MATRIX_MAP_4_0, 1 },
249 { SND_CHN_MATRIX_MAP_4_1, 0 },
250 { SND_CHN_MATRIX_MAP_5_0, 0 },
251 { SND_CHN_MATRIX_MAP_5_1, 1 },
252 { SND_CHN_MATRIX_MAP_6_0, 0 },
253 { SND_CHN_MATRIX_MAP_6_1, 0 },
254 { SND_CHN_MATRIX_MAP_7_0, 0 },
255 { SND_CHN_MATRIX_MAP_7_1, 1 },
258 static const char *channel_names[] = SND_CHN_T_NAMES;
261 * Connected channels change handler.
264 hdaa_channels_handler(struct hdaa_audio_as *as)
266 struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
267 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
268 struct hdaa_chan *ch = &devinfo->chans[as->chans[0]];
269 struct hdaa_widget *w;
271 int i, total, sub, assume, channels;
272 uint16_t cpins, upins, tpins;
276 for (i = 0; i < 16; i++) {
277 if (as->pins[i] <= 0)
279 w = hdaa_widget_get(devinfo, as->pins[i]);
282 if (w->wclass.pin.connected == 1)
284 else if (w->wclass.pin.connected != 0)
286 if (w->eld != NULL && w->eld_len >= 8)
289 tpins = cpins | upins;
290 if (as->hpredir >= 0)
295 total = sub = assume = channels = 0;
297 /* Map CEA speakers to sound(4) channels. */
298 if (eld[7] & 0x01) /* Front Left/Right */
299 channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
300 if (eld[7] & 0x02) /* Low Frequency Effect */
301 channels |= SND_CHN_T_MASK_LF;
302 if (eld[7] & 0x04) /* Front Center */
303 channels |= SND_CHN_T_MASK_FC;
304 if (eld[7] & 0x08) { /* Rear Left/Right */
305 /* If we have both RLR and RLRC, report RLR as side. */
306 if (eld[7] & 0x40) /* Rear Left/Right Center */
307 channels |= SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR;
309 channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
311 if (eld[7] & 0x10) /* Rear center */
312 channels |= SND_CHN_T_MASK_BC;
313 if (eld[7] & 0x20) /* Front Left/Right Center */
314 channels |= SND_CHN_T_MASK_FLC | SND_CHN_T_MASK_FRC;
315 if (eld[7] & 0x40) /* Rear Left/Right Center */
316 channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
317 } else if (as->pinset != 0 && (tpins & 0xffe0) == 0) {
318 /* Map UAA speakers to sound(4) channels. */
320 channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
322 channels |= SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF;
324 channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
326 channels |= SND_CHN_T_MASK_FLC | SND_CHN_T_MASK_FRC;
327 if (tpins & 0x0010) {
328 /* If there is no back pin, report side as back. */
329 if ((as->pinset & 0x0004) == 0)
330 channels |= SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR;
332 channels |= SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR;
334 } else if (as->mixed) {
335 /* Mixed assoc can be only stereo or theoretically mono. */
336 if (ch->channels == 1)
337 channels |= SND_CHN_T_MASK_FC;
339 channels |= SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
341 if (channels) { /* We have some usable channels info. */
343 device_printf(pdevinfo->dev, "%s channel set is: ",
344 as->dir == HDAA_CTL_OUT ? "Playback" : "Recording");
345 for (i = 0; i < SND_CHN_T_MAX; i++)
346 if (channels & (1 << i))
347 printf("%s, ", channel_names[i]);
350 /* Look for maximal fitting matrix. */
351 for (i = 0; i < sizeof(matrixes) / sizeof(struct matrix); i++) {
352 if (as->pinset != 0 && matrixes[i].analog == 0)
354 if ((matrixes[i].m.mask & ~channels) == 0) {
355 total = matrixes[i].m.channels;
356 sub = matrixes[i].m.ext;
362 total = ch->channels;
363 sub = (total == 6 || total == 8) ? 1 : 0;
366 device_printf(pdevinfo->dev,
367 "%s channel matrix is: %s%d.%d (%s)\n",
368 as->dir == HDAA_CTL_OUT ? "Playback" : "Recording",
369 assume ? "unknown, assuming " : "", total - sub, sub,
370 cpins != 0 ? "connected" :
371 (upins != 0 ? "unknown" : "disconnected"));
376 * Headphones redirection change handler.
379 hdaa_hpredir_handler(struct hdaa_widget *w)
381 struct hdaa_devinfo *devinfo = w->devinfo;
382 struct hdaa_audio_as *as = &devinfo->as[w->bindas];
383 struct hdaa_widget *w1;
384 struct hdaa_audio_ctl *ctl;
386 int j, connected = w->wclass.pin.connected;
389 device_printf((as->pdevinfo && as->pdevinfo->dev) ?
390 as->pdevinfo->dev : devinfo->dev,
391 "Redirect output to: %s\n",
392 connected ? "headphones": "main");
394 /* (Un)Mute headphone pin. */
395 ctl = hdaa_audio_ctl_amp_get(devinfo,
396 w->nid, HDAA_CTL_IN, -1, 1);
397 if (ctl != NULL && ctl->mute) {
398 /* If pin has muter - use it. */
399 val = connected ? 0 : 1;
400 if (val != ctl->forcemute) {
401 ctl->forcemute = val;
402 hdaa_audio_ctl_amp_set(ctl,
403 HDAA_AMP_MUTE_DEFAULT,
404 HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
407 /* If there is no muter - disable pin output. */
409 val = w->wclass.pin.ctrl |
410 HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
412 val = w->wclass.pin.ctrl &
413 ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
414 if (val != w->wclass.pin.ctrl) {
415 w->wclass.pin.ctrl = val;
416 hda_command(devinfo->dev,
417 HDA_CMD_SET_PIN_WIDGET_CTRL(0,
418 w->nid, w->wclass.pin.ctrl));
421 /* (Un)Mute other pins. */
422 for (j = 0; j < 15; j++) {
423 if (as->pins[j] <= 0)
425 ctl = hdaa_audio_ctl_amp_get(devinfo,
426 as->pins[j], HDAA_CTL_IN, -1, 1);
427 if (ctl != NULL && ctl->mute) {
428 /* If pin has muter - use it. */
429 val = connected ? 1 : 0;
430 if (val == ctl->forcemute)
432 ctl->forcemute = val;
433 hdaa_audio_ctl_amp_set(ctl,
434 HDAA_AMP_MUTE_DEFAULT,
435 HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
438 /* If there is no muter - disable pin output. */
439 w1 = hdaa_widget_get(devinfo, as->pins[j]);
442 val = w1->wclass.pin.ctrl &
443 ~HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
445 val = w1->wclass.pin.ctrl |
446 HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
447 if (val != w1->wclass.pin.ctrl) {
448 w1->wclass.pin.ctrl = val;
449 hda_command(devinfo->dev,
450 HDA_CMD_SET_PIN_WIDGET_CTRL(0,
451 w1->nid, w1->wclass.pin.ctrl));
458 * Recording source change handler.
461 hdaa_autorecsrc_handler(struct hdaa_audio_as *as, struct hdaa_widget *w)
463 struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
464 struct hdaa_devinfo *devinfo;
465 struct hdaa_widget *w1;
466 int i, mask, fullmask, prio, bestprio;
469 if (!as->mixed || pdevinfo == NULL || pdevinfo->mixer == NULL)
471 /* Don't touch anything if we asked not to. */
472 if (pdevinfo->autorecsrc == 0 ||
473 (pdevinfo->autorecsrc == 1 && w != NULL))
475 /* Don't touch anything if "mix" or "speaker" selected. */
476 if (pdevinfo->recsrc & (SOUND_MASK_IMIX | SOUND_MASK_SPEAKER))
478 /* Don't touch anything if several selected. */
479 if (ffs(pdevinfo->recsrc) != fls(pdevinfo->recsrc))
481 devinfo = pdevinfo->devinfo;
484 for (i = 0; i < 16; i++) {
485 if (as->pins[i] <= 0)
487 w1 = hdaa_widget_get(devinfo, as->pins[i]);
488 if (w1 == NULL || w1->enable == 0)
490 if (w1->wclass.pin.connected == 0)
492 prio = (w1->wclass.pin.connected == 1) ? 2 : 1;
495 if (prio > bestprio) {
499 mask |= (1 << w1->ossdev);
500 fullmask |= (1 << w1->ossdev);
504 /* Prefer newly connected input. */
505 if (w != NULL && (mask & (1 << w->ossdev)))
506 mask = (1 << w->ossdev);
507 /* Prefer previously selected input */
508 if (mask & pdevinfo->recsrc)
509 mask &= pdevinfo->recsrc;
511 if (mask & SOUND_MASK_MIC)
512 mask = SOUND_MASK_MIC;
513 /* Prefer monitor (2nd mic). */
514 if (mask & SOUND_MASK_MONITOR)
515 mask = SOUND_MASK_MONITOR;
516 /* Just take first one. */
517 mask = (1 << (ffs(mask) - 1));
519 hdaa_audio_ctl_ossmixer_mask2allname(mask, buf, sizeof(buf));
520 device_printf(pdevinfo->dev,
521 "Automatically set rec source to: %s\n", buf);
523 hdaa_unlock(devinfo);
524 mix_setrecsrc(pdevinfo->mixer, mask);
529 * Jack presence detection event handler.
532 hdaa_presence_handler(struct hdaa_widget *w)
534 struct hdaa_devinfo *devinfo = w->devinfo;
535 struct hdaa_audio_as *as;
539 if (w->enable == 0 || w->type !=
540 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
543 if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
544 (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
547 res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
548 connected = (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) != 0;
549 if (devinfo->quirks & HDAA_QUIRK_SENSEINV)
550 connected = !connected;
551 old = w->wclass.pin.connected;
552 if (connected == old)
554 w->wclass.pin.connected = connected;
556 if (connected || old != 2) {
557 device_printf(devinfo->dev,
558 "Pin sense: nid=%d sense=0x%08x (%sconnected)\n",
559 w->nid, res, !connected ? "dis" : "");
563 as = &devinfo->as[w->bindas];
564 if (as->hpredir >= 0 && as->pins[15] == w->nid)
565 hdaa_hpredir_handler(w);
566 if (as->dir == HDAA_CTL_IN && old != 2)
567 hdaa_autorecsrc_handler(as, w);
569 hdaa_channels_handler(as);
573 * Callback for poll based presence detection.
576 hdaa_jack_poll_callback(void *arg)
578 struct hdaa_devinfo *devinfo = arg;
579 struct hdaa_widget *w;
583 if (devinfo->poll_ival == 0) {
584 hdaa_unlock(devinfo);
587 for (i = 0; i < devinfo->ascnt; i++) {
588 if (devinfo->as[i].hpredir < 0)
590 w = hdaa_widget_get(devinfo, devinfo->as[i].pins[15]);
591 if (w == NULL || w->enable == 0 || w->type !=
592 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
594 hdaa_presence_handler(w);
596 callout_reset(&devinfo->poll_jack, devinfo->poll_ival,
597 hdaa_jack_poll_callback, devinfo);
598 hdaa_unlock(devinfo);
602 hdaa_eld_dump(struct hdaa_widget *w)
604 struct hdaa_devinfo *devinfo = w->devinfo;
605 device_t dev = devinfo->dev;
607 int len, mnl, i, sadc, fmt;
609 if (w->eld == NULL || w->eld_len < 4)
612 "ELD nid=%d: ELD_Ver=%u Baseline_ELD_Len=%u\n",
613 w->nid, w->eld[0] >> 3, w->eld[2]);
614 if ((w->eld[0] >> 3) != 0x02)
616 len = min(w->eld_len, (u_int)w->eld[2] * 4);
617 mnl = w->eld[4] & 0x1f;
619 "ELD nid=%d: CEA_EDID_Ver=%u MNL=%u\n",
620 w->nid, w->eld[4] >> 5, mnl);
621 sadc = w->eld[5] >> 4;
623 "ELD nid=%d: SAD_Count=%u Conn_Type=%u S_AI=%u HDCP=%u\n",
624 w->nid, sadc, (w->eld[5] >> 2) & 0x3,
625 (w->eld[5] >> 1) & 0x1, w->eld[5] & 0x1);
627 "ELD nid=%d: Aud_Synch_Delay=%ums\n",
628 w->nid, w->eld[6] * 2);
630 "ELD nid=%d: Channels=0x%b\n",
632 "\020\07RLRC\06FLRC\05RC\04RLR\03FC\02LFE\01FLR");
634 "ELD nid=%d: Port_ID=0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
635 w->nid, w->eld[8], w->eld[9], w->eld[10], w->eld[11],
636 w->eld[12], w->eld[13], w->eld[14], w->eld[15]);
638 "ELD nid=%d: Manufacturer_Name=0x%02x%02x\n",
639 w->nid, w->eld[16], w->eld[17]);
641 "ELD nid=%d: Product_Code=0x%02x%02x\n",
642 w->nid, w->eld[18], w->eld[19]);
644 "ELD nid=%d: Monitor_Name_String='%.*s'\n",
645 w->nid, mnl, &w->eld[20]);
646 for (i = 0; i < sadc; i++) {
647 sad = &w->eld[20 + mnl + i * 3];
648 fmt = (sad[0] >> 3) & 0x0f;
649 if (fmt == HDA_HDMI_CODING_TYPE_REF_CTX) {
650 fmt = (sad[2] >> 3) & 0x1f;
651 if (fmt < 1 || fmt > 3)
657 "ELD nid=%d: %s %dch freqs=0x%b",
658 w->nid, HDA_HDMI_CODING_TYPES[fmt], (sad[0] & 0x07) + 1,
659 sad[1], "\020\007192\006176\00596\00488\00348\00244\00132");
661 case HDA_HDMI_CODING_TYPE_LPCM:
662 printf(" sizes=0x%b",
663 sad[2] & 0x07, "\020\00324\00220\00116");
665 case HDA_HDMI_CODING_TYPE_AC3:
666 case HDA_HDMI_CODING_TYPE_MPEG1:
667 case HDA_HDMI_CODING_TYPE_MP3:
668 case HDA_HDMI_CODING_TYPE_MPEG2:
669 case HDA_HDMI_CODING_TYPE_AACLC:
670 case HDA_HDMI_CODING_TYPE_DTS:
671 case HDA_HDMI_CODING_TYPE_ATRAC:
672 printf(" max_bitrate=%d", sad[2] * 8000);
674 case HDA_HDMI_CODING_TYPE_WMAPRO:
675 printf(" profile=%d", sad[2] & 0x07);
683 hdaa_eld_handler(struct hdaa_widget *w)
685 struct hdaa_devinfo *devinfo = w->devinfo;
689 if (w->enable == 0 || w->type !=
690 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
693 if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
694 (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0)
697 res = hda_command(devinfo->dev, HDA_CMD_GET_PIN_SENSE(0, w->nid));
698 if ((w->eld != 0) == ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) != 0))
700 if (w->eld != NULL) {
702 free(w->eld, M_HDAA);
706 device_printf(devinfo->dev,
707 "Pin sense: nid=%d sense=0x%08x "
708 "(%sconnected, ELD %svalid)\n",
710 (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ? "" : "dis",
711 (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) ? "" : "in");
713 if ((res & HDA_CMD_GET_PIN_SENSE_ELD_VALID) == 0)
716 res = hda_command(devinfo->dev,
717 HDA_CMD_GET_HDMI_DIP_SIZE(0, w->nid, 0x08));
718 if (res == HDA_INVALID)
720 w->eld_len = res & 0xff;
722 w->eld = malloc(w->eld_len, M_HDAA, M_ZERO | M_NOWAIT);
723 if (w->eld == NULL) {
728 for (i = 0; i < w->eld_len; i++) {
729 res = hda_command(devinfo->dev,
730 HDA_CMD_GET_HDMI_ELDD(0, w->nid, i));
731 if (res & 0x80000000)
732 w->eld[i] = res & 0xff;
737 hdaa_channels_handler(&devinfo->as[w->bindas]);
741 * Pin sense initializer.
744 hdaa_sense_init(struct hdaa_devinfo *devinfo)
746 struct hdaa_audio_as *as;
747 struct hdaa_widget *w;
750 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
751 w = hdaa_widget_get(devinfo, i);
752 if (w == NULL || w->enable == 0 || w->type !=
753 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
755 if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap)) {
757 w->unsol = HDAC_UNSOL_ALLOC(
758 device_get_parent(devinfo->dev),
759 devinfo->dev, w->nid);
760 hda_command(devinfo->dev,
761 HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid,
762 HDA_CMD_SET_UNSOLICITED_RESPONSE_ENABLE | w->unsol));
764 as = &devinfo->as[w->bindas];
765 if (as->hpredir >= 0 && as->pins[15] == w->nid) {
766 if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(w->wclass.pin.cap) == 0 ||
767 (HDA_CONFIG_DEFAULTCONF_MISC(w->wclass.pin.config) & 1) != 0) {
768 device_printf(devinfo->dev,
769 "No presence detection support at nid %d\n",
775 device_printf(devinfo->dev,
776 "Headphones redirection for "
777 "association %d nid=%d using %s.\n",
779 (w->unsol < 0) ? "polling" :
780 "unsolicited responses");
784 hdaa_presence_handler(w);
785 if (!HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) &&
786 !HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
791 callout_reset(&devinfo->poll_jack, 1,
792 hdaa_jack_poll_callback, devinfo);
797 hdaa_sense_deinit(struct hdaa_devinfo *devinfo)
799 struct hdaa_widget *w;
802 callout_stop(&devinfo->poll_jack);
803 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
804 w = hdaa_widget_get(devinfo, i);
805 if (w == NULL || w->enable == 0 || w->type !=
806 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
810 hda_command(devinfo->dev,
811 HDA_CMD_SET_UNSOLICITED_RESPONSE(0, w->nid, 0));
813 device_get_parent(devinfo->dev), devinfo->dev,
820 hdaa_widget_pin_patch(uint32_t config, const char *str)
823 char *key, *value, *rest, *bad;
826 strlcpy(buf, str, sizeof(buf));
828 while ((key = strsep(&rest, "=")) != NULL) {
829 value = strsep(&rest, " \t");
832 ival = strtol(value, &bad, 10);
833 if (strcmp(key, "seq") == 0) {
834 config &= ~HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK;
835 config |= ((ival << HDA_CONFIG_DEFAULTCONF_SEQUENCE_SHIFT) &
836 HDA_CONFIG_DEFAULTCONF_SEQUENCE_MASK);
837 } else if (strcmp(key, "as") == 0) {
838 config &= ~HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK;
839 config |= ((ival << HDA_CONFIG_DEFAULTCONF_ASSOCIATION_SHIFT) &
840 HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK);
841 } else if (strcmp(key, "misc") == 0) {
842 config &= ~HDA_CONFIG_DEFAULTCONF_MISC_MASK;
843 config |= ((ival << HDA_CONFIG_DEFAULTCONF_MISC_SHIFT) &
844 HDA_CONFIG_DEFAULTCONF_MISC_MASK);
845 } else if (strcmp(key, "color") == 0) {
846 config &= ~HDA_CONFIG_DEFAULTCONF_COLOR_MASK;
848 config |= ((ival << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT) &
849 HDA_CONFIG_DEFAULTCONF_COLOR_MASK);
851 for (i = 0; i < 16; i++) {
852 if (strcasecmp(HDA_COLORS[i], value) == 0) {
853 config |= (i << HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT);
857 } else if (strcmp(key, "ctype") == 0) {
858 config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK;
860 config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT) &
861 HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_MASK);
863 for (i = 0; i < 16; i++) {
864 if (strcasecmp(HDA_CONNECTORS[i], value) == 0) {
865 config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE_SHIFT);
869 } else if (strcmp(key, "device") == 0) {
870 config &= ~HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
872 config |= ((ival << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT) &
873 HDA_CONFIG_DEFAULTCONF_DEVICE_MASK);
876 for (i = 0; i < 16; i++) {
877 if (strcasecmp(HDA_DEVS[i], value) == 0) {
878 config |= (i << HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT);
882 } else if (strcmp(key, "loc") == 0) {
883 config &= ~HDA_CONFIG_DEFAULTCONF_LOCATION_MASK;
885 config |= ((ival << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT) &
886 HDA_CONFIG_DEFAULTCONF_LOCATION_MASK);
889 for (i = 0; i < 64; i++) {
890 if (strcasecmp(HDA_LOCS[i], value) == 0) {
891 config |= (i << HDA_CONFIG_DEFAULTCONF_LOCATION_SHIFT);
895 } else if (strcmp(key, "conn") == 0) {
896 config &= ~HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK;
898 config |= ((ival << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT) &
899 HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK);
902 for (i = 0; i < 4; i++) {
903 if (strcasecmp(HDA_CONNS[i], value) == 0) {
904 config |= (i << HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT);
914 hdaa_gpio_patch(uint32_t gpio, const char *str)
917 char *key, *value, *rest;
920 strlcpy(buf, str, sizeof(buf));
922 while ((key = strsep(&rest, "=")) != NULL) {
923 value = strsep(&rest, " \t");
926 ikey = strtol(key, NULL, 10);
927 if (ikey < 0 || ikey > 7)
929 for (i = 0; i < 7; i++) {
930 if (strcasecmp(HDA_GPIO_ACTIONS[i], value) == 0) {
931 gpio &= ~HDAA_GPIO_MASK(ikey);
932 gpio |= i << HDAA_GPIO_SHIFT(ikey);
941 hdaa_local_patch_pin(struct hdaa_widget *w)
943 device_t dev = w->devinfo->dev;
944 const char *res = NULL;
945 uint32_t config, orig;
948 config = orig = w->wclass.pin.config;
949 snprintf(buf, sizeof(buf), "cad%u.nid%u.config",
950 hda_get_codec_id(dev), w->nid);
951 if (resource_string_value(device_get_name(
952 device_get_parent(device_get_parent(dev))),
953 device_get_unit(device_get_parent(device_get_parent(dev))),
955 if (strncmp(res, "0x", 2) == 0) {
956 config = strtol(res + 2, NULL, 16);
958 config = hdaa_widget_pin_patch(config, res);
961 snprintf(buf, sizeof(buf), "nid%u.config", w->nid);
962 if (resource_string_value(device_get_name(dev), device_get_unit(dev),
964 if (strncmp(res, "0x", 2) == 0) {
965 config = strtol(res + 2, NULL, 16);
967 config = hdaa_widget_pin_patch(config, res);
972 device_printf(w->devinfo->dev,
973 "Patching pin config nid=%u 0x%08x -> 0x%08x\n",
974 w->nid, orig, config);
976 w->wclass.pin.newconf = w->wclass.pin.config = config;
980 hdaa_dump_audio_formats_sb(struct sbuf *sb, uint32_t fcap, uint32_t pcmcap)
986 sbuf_printf(sb, " Stream cap: 0x%08x", cap);
987 if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
988 sbuf_printf(sb, " AC3");
989 if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
990 sbuf_printf(sb, " FLOAT32");
991 if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
992 sbuf_printf(sb, " PCM");
993 sbuf_printf(sb, "\n");
997 sbuf_printf(sb, " PCM cap: 0x%08x", cap);
998 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
999 sbuf_printf(sb, " 8");
1000 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
1001 sbuf_printf(sb, " 16");
1002 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
1003 sbuf_printf(sb, " 20");
1004 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
1005 sbuf_printf(sb, " 24");
1006 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
1007 sbuf_printf(sb, " 32");
1008 sbuf_printf(sb, " bits,");
1009 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
1010 sbuf_printf(sb, " 8");
1011 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
1012 sbuf_printf(sb, " 11");
1013 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
1014 sbuf_printf(sb, " 16");
1015 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
1016 sbuf_printf(sb, " 22");
1017 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
1018 sbuf_printf(sb, " 32");
1019 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
1020 sbuf_printf(sb, " 44");
1021 sbuf_printf(sb, " 48");
1022 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
1023 sbuf_printf(sb, " 88");
1024 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
1025 sbuf_printf(sb, " 96");
1026 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
1027 sbuf_printf(sb, " 176");
1028 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
1029 sbuf_printf(sb, " 192");
1030 sbuf_printf(sb, " KHz\n");
1035 hdaa_dump_pin_sb(struct sbuf *sb, struct hdaa_widget *w)
1037 uint32_t pincap, conf;
1039 pincap = w->wclass.pin.cap;
1041 sbuf_printf(sb, " Pin cap: 0x%08x", pincap);
1042 if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
1043 sbuf_printf(sb, " ISC");
1044 if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
1045 sbuf_printf(sb, " TRQD");
1046 if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
1047 sbuf_printf(sb, " PDC");
1048 if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
1049 sbuf_printf(sb, " HP");
1050 if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
1051 sbuf_printf(sb, " OUT");
1052 if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
1053 sbuf_printf(sb, " IN");
1054 if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
1055 sbuf_printf(sb, " BAL");
1056 if (HDA_PARAM_PIN_CAP_HDMI(pincap))
1057 sbuf_printf(sb, " HDMI");
1058 if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
1059 sbuf_printf(sb, " VREF[");
1060 if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
1061 sbuf_printf(sb, " 50");
1062 if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
1063 sbuf_printf(sb, " 80");
1064 if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
1065 sbuf_printf(sb, " 100");
1066 if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
1067 sbuf_printf(sb, " GROUND");
1068 if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
1069 sbuf_printf(sb, " HIZ");
1070 sbuf_printf(sb, " ]");
1072 if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
1073 sbuf_printf(sb, " EAPD");
1074 if (HDA_PARAM_PIN_CAP_DP(pincap))
1075 sbuf_printf(sb, " DP");
1076 if (HDA_PARAM_PIN_CAP_HBR(pincap))
1077 sbuf_printf(sb, " HBR");
1078 sbuf_printf(sb, "\n");
1079 conf = w->wclass.pin.config;
1080 sbuf_printf(sb, " Pin config: 0x%08x", conf);
1081 sbuf_printf(sb, " as=%d seq=%d "
1082 "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d\n",
1083 HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
1084 HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
1085 HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
1086 HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
1087 HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
1088 HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
1089 HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
1090 HDA_CONFIG_DEFAULTCONF_MISC(conf));
1091 sbuf_printf(sb, " Pin control: 0x%08x", w->wclass.pin.ctrl);
1092 if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
1093 sbuf_printf(sb, " HP");
1094 if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
1095 sbuf_printf(sb, " IN");
1096 if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
1097 sbuf_printf(sb, " OUT");
1098 if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1099 if ((w->wclass.pin.ctrl &
1100 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
1101 sbuf_printf(sb, " HBR");
1102 else if ((w->wclass.pin.ctrl &
1103 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
1104 sbuf_printf(sb, " EPTs");
1106 if ((w->wclass.pin.ctrl &
1107 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
1108 sbuf_printf(sb, " VREFs");
1110 sbuf_printf(sb, "\n");
1114 hdaa_dump_amp_sb(struct sbuf *sb, uint32_t cap, const char *banner)
1116 int offset, size, step;
1118 offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap);
1119 size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap);
1120 step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap);
1121 sbuf_printf(sb, " %s amp: 0x%08x "
1122 "mute=%d step=%d size=%d offset=%d (%+d/%+ddB)\n",
1124 HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
1126 ((0 - offset) * (size + 1)) / 4,
1127 ((step - offset) * (size + 1)) / 4);
1132 hdaa_sysctl_caps(SYSCTL_HANDLER_ARGS)
1134 struct hdaa_devinfo *devinfo;
1135 struct hdaa_widget *w, *cw;
1140 w = (struct hdaa_widget *)oidp->oid_arg1;
1141 devinfo = w->devinfo;
1142 sbuf_new_for_sysctl(&sb, NULL, 256, req);
1144 sbuf_printf(&sb, "%s%s\n", w->name,
1145 (w->enable == 0) ? " [DISABLED]" : "");
1146 sbuf_printf(&sb, " Widget cap: 0x%08x",
1147 w->param.widget_cap);
1148 if (w->param.widget_cap & 0x0ee1) {
1149 if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
1150 sbuf_printf(&sb, " LRSWAP");
1151 if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
1152 sbuf_printf(&sb, " PWR");
1153 if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
1154 sbuf_printf(&sb, " DIGITAL");
1155 if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
1156 sbuf_printf(&sb, " UNSOL");
1157 if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
1158 sbuf_printf(&sb, " PROC");
1159 if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
1160 sbuf_printf(&sb, " STRIPE(x%d)",
1161 1 << (fls(w->wclass.conv.stripecap) - 1));
1162 j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1164 sbuf_printf(&sb, " STEREO");
1166 sbuf_printf(&sb, " %dCH", j + 1);
1168 sbuf_printf(&sb, "\n");
1169 if (w->bindas != -1) {
1170 sbuf_printf(&sb, " Association: %d (0x%04x)\n",
1171 w->bindas, w->bindseqmask);
1173 if (w->ossmask != 0 || w->ossdev >= 0) {
1174 sbuf_printf(&sb, " OSS: %s",
1175 hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
1177 sbuf_printf(&sb, " (%s)", ossnames[w->ossdev]);
1178 sbuf_printf(&sb, "\n");
1180 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1181 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1182 hdaa_dump_audio_formats_sb(&sb,
1183 w->param.supp_stream_formats,
1184 w->param.supp_pcm_size_rate);
1185 } else if (w->type ==
1186 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
1187 hdaa_dump_pin_sb(&sb, w);
1188 if (w->param.eapdbtl != HDA_INVALID) {
1189 sbuf_printf(&sb, " EAPD: 0x%08x%s%s%s\n",
1191 (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_LR_SWAP) ?
1193 (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD) ?
1195 (w->param.eapdbtl & HDA_CMD_SET_EAPD_BTL_ENABLE_BTL) ?
1198 if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
1199 w->param.outamp_cap != 0)
1200 hdaa_dump_amp_sb(&sb, w->param.outamp_cap, "Output");
1201 if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
1202 w->param.inamp_cap != 0)
1203 hdaa_dump_amp_sb(&sb, w->param.inamp_cap, " Input");
1205 sbuf_printf(&sb, " Connections: %d\n", w->nconns);
1206 for (j = 0; j < w->nconns; j++) {
1207 cw = hdaa_widget_get(devinfo, w->conns[j]);
1208 sbuf_printf(&sb, " + %s<- nid=%d [%s]",
1209 (w->connsenable[j] == 0)?"[DISABLED] ":"",
1210 w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
1212 sbuf_printf(&sb, " [UNKNOWN]");
1213 else if (cw->enable == 0)
1214 sbuf_printf(&sb, " [DISABLED]");
1215 if (w->nconns > 1 && w->selconn == j && w->type !=
1216 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
1217 sbuf_printf(&sb, " (selected)");
1218 sbuf_printf(&sb, "\n");
1220 error = sbuf_finish(&sb);
1226 hdaa_sysctl_config(SYSCTL_HANDLER_ARGS)
1232 conf = *(uint32_t *)oidp->oid_arg1;
1233 snprintf(buf, sizeof(buf), "0x%08x as=%d seq=%d "
1234 "device=%s conn=%s ctype=%s loc=%s color=%s misc=%d",
1236 HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
1237 HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
1238 HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
1239 HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
1240 HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
1241 HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
1242 HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
1243 HDA_CONFIG_DEFAULTCONF_MISC(conf));
1244 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1245 if (error != 0 || req->newptr == NULL)
1247 if (strncmp(buf, "0x", 2) == 0)
1248 conf = strtol(buf + 2, NULL, 16);
1250 conf = hdaa_widget_pin_patch(conf, buf);
1251 *(uint32_t *)oidp->oid_arg1 = conf;
1256 hdaa_config_fetch(const char *str, uint32_t *on, uint32_t *off)
1258 int i = 0, j, k, len, inv;
1261 while (str[i] != '\0' &&
1262 (str[i] == ',' || isspace(str[i]) != 0))
1267 while (str[j] != '\0' &&
1268 !(str[j] == ',' || isspace(str[j]) != 0))
1271 if (len > 2 && strncmp(str + i, "no", 2) == 0)
1275 for (k = 0; len > inv && k < nitems(hdaa_quirks_tab); k++) {
1276 if (strncmp(str + i + inv,
1277 hdaa_quirks_tab[k].key, len - inv) != 0)
1279 if (len - inv != strlen(hdaa_quirks_tab[k].key))
1282 *on |= hdaa_quirks_tab[k].value;
1283 *off &= ~hdaa_quirks_tab[k].value;
1285 *off |= hdaa_quirks_tab[k].value;
1286 *on &= ~hdaa_quirks_tab[k].value;
1295 hdaa_sysctl_quirks(SYSCTL_HANDLER_ARGS)
1298 int error, n = 0, i;
1299 uint32_t quirks, quirks_off;
1301 quirks = *(uint32_t *)oidp->oid_arg1;
1303 for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
1304 if ((quirks & hdaa_quirks_tab[i].value) != 0)
1305 n += snprintf(buf + n, sizeof(buf) - n, "%s%s",
1306 n != 0 ? "," : "", hdaa_quirks_tab[i].key);
1308 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1309 if (error != 0 || req->newptr == NULL)
1311 if (strncmp(buf, "0x", 2) == 0)
1312 quirks = strtol(buf + 2, NULL, 16);
1315 hdaa_config_fetch(buf, &quirks, &quirks_off);
1317 *(uint32_t *)oidp->oid_arg1 = quirks;
1322 hdaa_local_patch(struct hdaa_devinfo *devinfo)
1324 struct hdaa_widget *w;
1325 const char *res = NULL;
1326 uint32_t quirks_on = 0, quirks_off = 0, x;
1329 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
1330 w = hdaa_widget_get(devinfo, i);
1333 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
1334 hdaa_local_patch_pin(w);
1337 if (resource_string_value(device_get_name(devinfo->dev),
1338 device_get_unit(devinfo->dev), "config", &res) == 0) {
1339 if (res != NULL && strlen(res) > 0)
1340 hdaa_config_fetch(res, &quirks_on, &quirks_off);
1341 devinfo->quirks |= quirks_on;
1342 devinfo->quirks &= ~quirks_off;
1344 if (devinfo->newquirks == -1)
1345 devinfo->newquirks = devinfo->quirks;
1347 devinfo->quirks = devinfo->newquirks;
1349 device_printf(devinfo->dev,
1350 "Config options: 0x%08x\n", devinfo->quirks);
1353 if (resource_string_value(device_get_name(devinfo->dev),
1354 device_get_unit(devinfo->dev), "gpio_config", &res) == 0) {
1355 if (strncmp(res, "0x", 2) == 0) {
1356 devinfo->gpio = strtol(res + 2, NULL, 16);
1358 devinfo->gpio = hdaa_gpio_patch(devinfo->gpio, res);
1361 if (devinfo->newgpio == -1)
1362 devinfo->newgpio = devinfo->gpio;
1364 devinfo->gpio = devinfo->newgpio;
1365 if (devinfo->newgpo == -1)
1366 devinfo->newgpo = devinfo->gpo;
1368 devinfo->gpo = devinfo->newgpo;
1370 device_printf(devinfo->dev, "GPIO config options:");
1371 for (i = 0; i < 7; i++) {
1372 x = (devinfo->gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
1374 printf(" %d=%s", i, HDA_GPIO_ACTIONS[x]);
1381 hdaa_widget_connection_parse(struct hdaa_widget *w)
1384 int i, j, max, ents, entnum;
1386 nid_t cnid, addcnid, prevcnid;
1390 res = hda_command(w->devinfo->dev,
1391 HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_CONN_LIST_LENGTH));
1393 ents = HDA_PARAM_CONN_LIST_LENGTH_LIST_LENGTH(res);
1398 entnum = HDA_PARAM_CONN_LIST_LENGTH_LONG_FORM(res) ? 2 : 4;
1399 max = (sizeof(w->conns) / sizeof(w->conns[0])) - 1;
1402 #define CONN_RMASK(e) (1 << ((32 / (e)) - 1))
1403 #define CONN_NMASK(e) (CONN_RMASK(e) - 1)
1404 #define CONN_RESVAL(r, e, n) ((r) >> ((32 / (e)) * (n)))
1405 #define CONN_RANGE(r, e, n) (CONN_RESVAL(r, e, n) & CONN_RMASK(e))
1406 #define CONN_CNID(r, e, n) (CONN_RESVAL(r, e, n) & CONN_NMASK(e))
1408 for (i = 0; i < ents; i += entnum) {
1409 res = hda_command(w->devinfo->dev,
1410 HDA_CMD_GET_CONN_LIST_ENTRY(0, nid, i));
1411 for (j = 0; j < entnum; j++) {
1412 cnid = CONN_CNID(res, entnum, j);
1414 if (w->nconns < ents)
1415 device_printf(w->devinfo->dev,
1416 "WARNING: nid=%d has zero cnid "
1417 "entnum=%d j=%d index=%d "
1418 "entries=%d found=%d res=0x%08x\n",
1420 ents, w->nconns, res);
1424 if (cnid < w->devinfo->startnode ||
1425 cnid >= w->devinfo->endnode) {
1427 device_printf(w->devinfo->dev,
1428 "WARNING: nid=%d has cnid outside "
1429 "of the AFG range j=%d "
1430 "entnum=%d index=%d res=0x%08x\n",
1431 nid, j, entnum, i, res);
1434 if (CONN_RANGE(res, entnum, j) == 0)
1436 else if (prevcnid == 0 || prevcnid >= cnid) {
1437 device_printf(w->devinfo->dev,
1438 "WARNING: Invalid child range "
1439 "nid=%d index=%d j=%d entnum=%d "
1440 "prevcnid=%d cnid=%d res=0x%08x\n",
1441 nid, i, j, entnum, prevcnid,
1445 addcnid = prevcnid + 1;
1446 while (addcnid <= cnid) {
1447 if (w->nconns > max) {
1448 device_printf(w->devinfo->dev,
1449 "Adding %d (nid=%d): "
1450 "Max connection reached! max=%d\n",
1451 addcnid, nid, max + 1);
1454 w->connsenable[w->nconns] = 1;
1455 w->conns[w->nconns++] = addcnid++;
1466 hdaa_widget_parse(struct hdaa_widget *w)
1468 device_t dev = w->devinfo->dev;
1473 w->param.widget_cap = wcap = hda_command(dev,
1474 HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_AUDIO_WIDGET_CAP));
1475 w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(wcap);
1477 hdaa_widget_connection_parse(w);
1479 if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(wcap)) {
1480 if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1481 w->param.outamp_cap =
1483 HDA_CMD_GET_PARAMETER(0, nid,
1484 HDA_PARAM_OUTPUT_AMP_CAP));
1486 w->param.outamp_cap =
1487 w->devinfo->outamp_cap;
1489 w->param.outamp_cap = 0;
1491 if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(wcap)) {
1492 if (HDA_PARAM_AUDIO_WIDGET_CAP_AMP_OVR(wcap))
1493 w->param.inamp_cap =
1495 HDA_CMD_GET_PARAMETER(0, nid,
1496 HDA_PARAM_INPUT_AMP_CAP));
1498 w->param.inamp_cap =
1499 w->devinfo->inamp_cap;
1501 w->param.inamp_cap = 0;
1503 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
1504 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
1505 if (HDA_PARAM_AUDIO_WIDGET_CAP_FORMAT_OVR(wcap)) {
1506 cap = hda_command(dev,
1507 HDA_CMD_GET_PARAMETER(0, nid,
1508 HDA_PARAM_SUPP_STREAM_FORMATS));
1509 w->param.supp_stream_formats = (cap != 0) ? cap :
1510 w->devinfo->supp_stream_formats;
1511 cap = hda_command(dev,
1512 HDA_CMD_GET_PARAMETER(0, nid,
1513 HDA_PARAM_SUPP_PCM_SIZE_RATE));
1514 w->param.supp_pcm_size_rate = (cap != 0) ? cap :
1515 w->devinfo->supp_pcm_size_rate;
1517 w->param.supp_stream_formats =
1518 w->devinfo->supp_stream_formats;
1519 w->param.supp_pcm_size_rate =
1520 w->devinfo->supp_pcm_size_rate;
1522 if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1523 w->wclass.conv.stripecap = hda_command(dev,
1524 HDA_CMD_GET_STRIPE_CONTROL(0, w->nid)) >> 20;
1526 w->wclass.conv.stripecap = 1;
1528 w->param.supp_stream_formats = 0;
1529 w->param.supp_pcm_size_rate = 0;
1532 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1533 w->wclass.pin.original = w->wclass.pin.newconf =
1534 w->wclass.pin.config = hda_command(dev,
1535 HDA_CMD_GET_CONFIGURATION_DEFAULT(0, w->nid));
1536 w->wclass.pin.cap = hda_command(dev,
1537 HDA_CMD_GET_PARAMETER(0, w->nid, HDA_PARAM_PIN_CAP));
1538 w->wclass.pin.ctrl = hda_command(dev,
1539 HDA_CMD_GET_PIN_WIDGET_CTRL(0, nid));
1540 w->wclass.pin.connected = 2;
1541 if (HDA_PARAM_PIN_CAP_EAPD_CAP(w->wclass.pin.cap)) {
1542 w->param.eapdbtl = hda_command(dev,
1543 HDA_CMD_GET_EAPD_BTL_ENABLE(0, nid));
1544 w->param.eapdbtl &= 0x7;
1545 w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
1547 w->param.eapdbtl = HDA_INVALID;
1551 hdaa_unlock(w->devinfo);
1552 snprintf(buf, sizeof(buf), "nid%d", w->nid);
1553 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1554 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1555 buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1556 w, sizeof(w), hdaa_sysctl_caps, "A", "Node capabilities");
1557 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1558 snprintf(buf, sizeof(buf), "nid%d_config", w->nid);
1559 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1560 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1561 buf, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1562 &w->wclass.pin.newconf, sizeof(&w->wclass.pin.newconf),
1563 hdaa_sysctl_config, "A", "Current pin configuration");
1564 snprintf(buf, sizeof(buf), "nid%d_original", w->nid);
1565 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1566 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1567 buf, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1568 &w->wclass.pin.original, sizeof(&w->wclass.pin.original),
1569 hdaa_sysctl_config, "A", "Original pin configuration");
1571 hdaa_lock(w->devinfo);
1575 hdaa_widget_postprocess(struct hdaa_widget *w)
1577 const char *typestr;
1579 w->type = HDA_PARAM_AUDIO_WIDGET_CAP_TYPE(w->param.widget_cap);
1581 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
1582 typestr = "audio output";
1584 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
1585 typestr = "audio input";
1587 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
1588 typestr = "audio mixer";
1590 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
1591 typestr = "audio selector";
1593 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
1596 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET:
1597 typestr = "power widget";
1599 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET:
1600 typestr = "volume widget";
1602 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
1603 typestr = "beep widget";
1605 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VENDOR_WIDGET:
1606 typestr = "vendor widget";
1609 typestr = "unknown type";
1612 strlcpy(w->name, typestr, sizeof(w->name));
1614 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
1619 config = w->wclass.pin.config;
1620 devstr = HDA_DEVS[(config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) >>
1621 HDA_CONFIG_DEFAULTCONF_DEVICE_SHIFT];
1622 conn = (config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) >>
1623 HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_SHIFT;
1624 color = (config & HDA_CONFIG_DEFAULTCONF_COLOR_MASK) >>
1625 HDA_CONFIG_DEFAULTCONF_COLOR_SHIFT;
1626 strlcat(w->name, ": ", sizeof(w->name));
1627 strlcat(w->name, devstr, sizeof(w->name));
1628 strlcat(w->name, " (", sizeof(w->name));
1629 if (conn == 0 && color != 0 && color != 15) {
1630 strlcat(w->name, HDA_COLORS[color], sizeof(w->name));
1631 strlcat(w->name, " ", sizeof(w->name));
1633 strlcat(w->name, HDA_CONNS[conn], sizeof(w->name));
1634 strlcat(w->name, ")", sizeof(w->name));
1638 struct hdaa_widget *
1639 hdaa_widget_get(struct hdaa_devinfo *devinfo, nid_t nid)
1641 if (devinfo == NULL || devinfo->widget == NULL ||
1642 nid < devinfo->startnode || nid >= devinfo->endnode)
1644 return (&devinfo->widget[nid - devinfo->startnode]);
1648 hdaa_audio_ctl_amp_set_internal(struct hdaa_devinfo *devinfo, nid_t nid,
1649 int index, int lmute, int rmute,
1650 int left, int right, int dir)
1655 device_printf(devinfo->dev,
1656 "Setting amplifier nid=%d index=%d %s mute=%d/%d vol=%d/%d\n",
1657 nid,index,dir ? "in" : "out",lmute,rmute,left,right);
1659 if (left != right || lmute != rmute) {
1660 v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
1661 (lmute << 7) | left;
1662 hda_command(devinfo->dev,
1663 HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1664 v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
1665 (rmute << 7) | right;
1667 v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
1668 (lmute << 7) | left;
1670 hda_command(devinfo->dev,
1671 HDA_CMD_SET_AMP_GAIN_MUTE(0, nid, v));
1675 hdaa_audio_ctl_amp_set(struct hdaa_audio_ctl *ctl, uint32_t mute,
1676 int left, int right)
1681 nid = ctl->widget->nid;
1683 /* Save new values if valid. */
1684 if (mute != HDAA_AMP_MUTE_DEFAULT)
1686 if (left != HDAA_AMP_VOL_DEFAULT)
1688 if (right != HDAA_AMP_VOL_DEFAULT)
1690 /* Prepare effective values */
1691 if (ctl->forcemute) {
1697 lmute = HDAA_AMP_LEFT_MUTED(ctl->muted);
1698 rmute = HDAA_AMP_RIGHT_MUTED(ctl->muted);
1702 /* Apply effective values */
1703 if (ctl->dir & HDAA_CTL_OUT)
1704 hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1705 lmute, rmute, left, right, 0);
1706 if (ctl->dir & HDAA_CTL_IN)
1707 hdaa_audio_ctl_amp_set_internal(ctl->widget->devinfo, nid, ctl->index,
1708 lmute, rmute, left, right, 1);
1712 hdaa_widget_connection_select(struct hdaa_widget *w, uint8_t index)
1714 if (w == NULL || w->nconns < 1 || index > (w->nconns - 1))
1717 device_printf(w->devinfo->dev,
1718 "Setting selector nid=%d index=%d\n", w->nid, index);
1720 hda_command(w->devinfo->dev,
1721 HDA_CMD_SET_CONNECTION_SELECT_CONTROL(0, w->nid, index));
1725 /****************************************************************************
1727 ****************************************************************************/
1730 hdaa_channel_init(kobj_t obj, void *data, struct snd_dbuf *b,
1731 struct pcm_channel *c, int dir)
1733 struct hdaa_chan *ch = data;
1734 struct hdaa_pcm_devinfo *pdevinfo = ch->pdevinfo;
1735 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
1738 if (devinfo->quirks & HDAA_QUIRK_FIXEDRATE) {
1739 ch->caps.minspeed = ch->caps.maxspeed = 48000;
1740 ch->pcmrates[0] = 48000;
1741 ch->pcmrates[1] = 0;
1746 ch->blksz = pdevinfo->chan_size / pdevinfo->chan_blkcnt;
1747 ch->blkcnt = pdevinfo->chan_blkcnt;
1748 hdaa_unlock(devinfo);
1750 if (sndbuf_alloc(ch->b, bus_get_dma_tag(devinfo->dev),
1751 hda_get_dma_nocache(devinfo->dev) ? BUS_DMA_NOCACHE : 0,
1752 pdevinfo->chan_size) != 0)
1759 hdaa_channel_setformat(kobj_t obj, void *data, uint32_t format)
1761 struct hdaa_chan *ch = data;
1764 for (i = 0; ch->caps.fmtlist[i] != 0; i++) {
1765 if (format == ch->caps.fmtlist[i]) {
1775 hdaa_channel_setspeed(kobj_t obj, void *data, uint32_t speed)
1777 struct hdaa_chan *ch = data;
1778 uint32_t spd = 0, threshold;
1781 /* First look for equal or multiple frequency. */
1782 for (i = 0; ch->pcmrates[i] != 0; i++) {
1783 spd = ch->pcmrates[i];
1784 if (speed != 0 && spd / speed * speed == spd) {
1789 /* If no match, just find nearest. */
1790 for (i = 0; ch->pcmrates[i] != 0; i++) {
1791 spd = ch->pcmrates[i];
1792 threshold = spd + ((ch->pcmrates[i + 1] != 0) ?
1793 ((ch->pcmrates[i + 1] - spd) >> 1) : 0);
1794 if (speed < threshold)
1802 hdaa_stream_format(struct hdaa_chan *ch)
1808 if (ch->fmt & AFMT_S16_LE)
1809 fmt |= ch->bit16 << 4;
1810 else if (ch->fmt & AFMT_S32_LE)
1811 fmt |= ch->bit32 << 4;
1814 for (i = 0; i < HDA_RATE_TAB_LEN; i++) {
1815 if (hda_rate_tab[i].valid && ch->spd == hda_rate_tab[i].rate) {
1816 fmt |= hda_rate_tab[i].base;
1817 fmt |= hda_rate_tab[i].mul;
1818 fmt |= hda_rate_tab[i].div;
1822 fmt |= (AFMT_CHANNEL(ch->fmt) - 1);
1828 hdaa_allowed_stripes(uint16_t fmt)
1830 static const int bits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1833 size = bits[(fmt >> 4) & 0x03];
1834 size *= (fmt & 0x0f) + 1;
1835 size *= ((fmt >> 11) & 0x07) + 1;
1836 return (0xffffffffU >> (32 - fls(size / 8)));
1840 hdaa_audio_setup(struct hdaa_chan *ch)
1842 struct hdaa_audio_as *as = &ch->devinfo->as[ch->as];
1843 struct hdaa_widget *w, *wp;
1844 int i, j, k, chn, cchn, totalchn, totalextchn, c;
1846 /* Mapping channel pairs to codec pins/converters. */
1847 const static uint16_t convmap[2][5] =
1848 /* 1.0 2.0 4.0 5.1 7.1 */
1849 {{ 0x0010, 0x0001, 0x0201, 0x0231, 0x4231 }, /* no dup. */
1850 { 0x0010, 0x0001, 0x2201, 0x2231, 0x4231 }}; /* side dup. */
1851 /* Mapping formats to HDMI channel allocations. */
1852 const static uint8_t hdmica[2][8] =
1853 /* 1 2 3 4 5 6 7 8 */
1854 {{ 0x02, 0x00, 0x04, 0x08, 0x0a, 0x0e, 0x12, 0x12 }, /* x.0 */
1855 { 0x01, 0x03, 0x01, 0x03, 0x09, 0x0b, 0x0f, 0x13 }}; /* x.1 */
1856 /* Mapping formats to HDMI channels order. */
1857 const static uint32_t hdmich[2][8] =
1858 /* 1 / 5 2 / 6 3 / 7 4 / 8 */
1859 {{ 0xFFFF0F00, 0xFFFFFF10, 0xFFF2FF10, 0xFF32FF10,
1860 0xFF324F10, 0xF5324F10, 0x54326F10, 0x54326F10 }, /* x.0 */
1861 { 0xFFFFF000, 0xFFFF0100, 0xFFFFF210, 0xFFFF2310,
1862 0xFF32F410, 0xFF324510, 0xF6324510, 0x76325410 }}; /* x.1 */
1867 totalchn = AFMT_CHANNEL(ch->fmt);
1868 totalextchn = AFMT_EXTCHANNEL(ch->fmt);
1870 device_printf(ch->pdevinfo->dev,
1871 "PCMDIR_%s: Stream setup fmt=%08x (%d.%d) speed=%d\n",
1872 (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1873 ch->fmt, totalchn - totalextchn, totalextchn, ch->spd);
1875 fmt = hdaa_stream_format(ch);
1877 /* Set channels to I/O converters mapping for known speaker setups. */
1878 if ((as->pinset == 0x0007 || as->pinset == 0x0013) || /* Standard 5.1 */
1879 (as->pinset == 0x0017)) /* Standard 7.1 */
1880 convmapid = (ch->dir == PCMDIR_PLAY);
1882 dfmt = HDA_CMD_SET_DIGITAL_CONV_FMT1_DIGEN;
1883 if (ch->fmt & AFMT_AC3)
1884 dfmt |= HDA_CMD_SET_DIGITAL_CONV_FMT1_NAUDIO;
1887 for (i = 0; ch->io[i] != -1; i++) {
1888 w = hdaa_widget_get(ch->devinfo, ch->io[i]);
1892 /* If HP redirection is enabled, but failed to use same
1893 DAC, make last DAC to duplicate first one. */
1894 if (as->fakeredir && i == (as->pincnt - 1)) {
1897 /* Map channels to I/O converters, if set. */
1899 chn = (((convmap[convmapid][totalchn / 2]
1900 >> i * 4) & 0xf) - 1) * 2;
1901 if (chn < 0 || chn >= totalchn) {
1904 c = (ch->sid << 4) | chn;
1907 hda_command(ch->devinfo->dev,
1908 HDA_CMD_SET_CONV_FMT(0, ch->io[i], fmt));
1909 if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
1910 hda_command(ch->devinfo->dev,
1911 HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], dfmt));
1913 hda_command(ch->devinfo->dev,
1914 HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i], c));
1915 if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap)) {
1916 hda_command(ch->devinfo->dev,
1917 HDA_CMD_SET_STRIPE_CONTROL(0, w->nid, ch->stripectl));
1919 cchn = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
1920 if (cchn > 1 && chn < totalchn) {
1921 cchn = min(cchn, totalchn - chn - 1);
1922 hda_command(ch->devinfo->dev,
1923 HDA_CMD_SET_CONV_CHAN_COUNT(0, ch->io[i], cchn));
1926 device_printf(ch->pdevinfo->dev,
1927 "PCMDIR_%s: Stream setup nid=%d: "
1928 "fmt=0x%04x, dfmt=0x%04x, chan=0x%04x, "
1929 "chan_count=0x%02x, stripe=%d\n",
1930 (ch->dir == PCMDIR_PLAY) ? "PLAY" : "REC",
1931 ch->io[i], fmt, dfmt, c, cchn, ch->stripectl);
1933 for (j = 0; j < 16; j++) {
1934 if (as->dacs[ch->asindex][j] != ch->io[i])
1937 wp = hdaa_widget_get(ch->devinfo, nid);
1940 if (!HDA_PARAM_PIN_CAP_DP(wp->wclass.pin.cap) &&
1941 !HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap))
1944 /* Set channel mapping. */
1945 for (k = 0; k < 8; k++) {
1946 hda_command(ch->devinfo->dev,
1947 HDA_CMD_SET_HDMI_CHAN_SLOT(0, nid,
1948 (((hdmich[totalextchn == 0 ? 0 : 1][totalchn - 1]
1949 >> (k * 4)) & 0xf) << 4) | k));
1953 * Enable High Bit Rate (HBR) Encoded Packet Type
1954 * (EPT), if supported and needed (8ch data).
1956 if (HDA_PARAM_PIN_CAP_HDMI(wp->wclass.pin.cap) &&
1957 HDA_PARAM_PIN_CAP_HBR(wp->wclass.pin.cap)) {
1958 wp->wclass.pin.ctrl &=
1959 ~HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK;
1960 if ((ch->fmt & AFMT_AC3) && (cchn == 7))
1961 wp->wclass.pin.ctrl |= 0x03;
1962 hda_command(ch->devinfo->dev,
1963 HDA_CMD_SET_PIN_WIDGET_CTRL(0, nid,
1964 wp->wclass.pin.ctrl));
1967 /* Stop audio infoframe transmission. */
1968 hda_command(ch->devinfo->dev,
1969 HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1970 hda_command(ch->devinfo->dev,
1971 HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0x00));
1973 /* Clear audio infoframe buffer. */
1974 hda_command(ch->devinfo->dev,
1975 HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1976 for (k = 0; k < 32; k++)
1977 hda_command(ch->devinfo->dev,
1978 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
1980 /* Write HDMI/DisplayPort audio infoframe. */
1981 hda_command(ch->devinfo->dev,
1982 HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
1983 if (w->eld != NULL && w->eld_len >= 6 &&
1984 ((w->eld[5] >> 2) & 0x3) == 1) { /* DisplayPort */
1985 hda_command(ch->devinfo->dev,
1986 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1987 hda_command(ch->devinfo->dev,
1988 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x1b));
1989 hda_command(ch->devinfo->dev,
1990 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x44));
1992 hda_command(ch->devinfo->dev,
1993 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x84));
1994 hda_command(ch->devinfo->dev,
1995 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x01));
1996 hda_command(ch->devinfo->dev,
1997 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x0a));
1999 csum -= 0x84 + 0x01 + 0x0a + (totalchn - 1) +
2000 hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1];
2001 hda_command(ch->devinfo->dev,
2002 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, csum));
2004 hda_command(ch->devinfo->dev,
2005 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, totalchn - 1));
2006 hda_command(ch->devinfo->dev,
2007 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
2008 hda_command(ch->devinfo->dev,
2009 HDA_CMD_SET_HDMI_DIP_DATA(0, nid, 0x00));
2010 hda_command(ch->devinfo->dev,
2011 HDA_CMD_SET_HDMI_DIP_DATA(0, nid,
2012 hdmica[totalextchn == 0 ? 0 : 1][totalchn - 1]));
2014 /* Start audio infoframe transmission. */
2015 hda_command(ch->devinfo->dev,
2016 HDA_CMD_SET_HDMI_DIP_INDEX(0, nid, 0x00));
2017 hda_command(ch->devinfo->dev,
2018 HDA_CMD_SET_HDMI_DIP_XMIT(0, nid, 0xc0));
2025 * Greatest Common Divisor.
2028 gcd(unsigned a, unsigned b)
2041 * Least Common Multiple.
2044 lcm(unsigned a, unsigned b)
2047 return ((a * b) / gcd(a, b));
2051 hdaa_channel_setfragments(kobj_t obj, void *data,
2052 uint32_t blksz, uint32_t blkcnt)
2054 struct hdaa_chan *ch = data;
2056 blksz -= blksz % lcm(HDA_DMA_ALIGNMENT, sndbuf_getalign(ch->b));
2058 if (blksz > (sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN))
2059 blksz = sndbuf_getmaxsize(ch->b) / HDA_BDL_MIN;
2060 if (blksz < HDA_BLK_MIN)
2061 blksz = HDA_BLK_MIN;
2062 if (blkcnt > HDA_BDL_MAX)
2063 blkcnt = HDA_BDL_MAX;
2064 if (blkcnt < HDA_BDL_MIN)
2065 blkcnt = HDA_BDL_MIN;
2067 while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->b)) {
2068 if ((blkcnt >> 1) >= HDA_BDL_MIN)
2070 else if ((blksz >> 1) >= HDA_BLK_MIN)
2076 if ((sndbuf_getblksz(ch->b) != blksz ||
2077 sndbuf_getblkcnt(ch->b) != blkcnt) &&
2078 sndbuf_resize(ch->b, blkcnt, blksz) != 0)
2079 device_printf(ch->devinfo->dev, "%s: failed blksz=%u blkcnt=%u\n",
2080 __func__, blksz, blkcnt);
2082 ch->blksz = sndbuf_getblksz(ch->b);
2083 ch->blkcnt = sndbuf_getblkcnt(ch->b);
2089 hdaa_channel_setblocksize(kobj_t obj, void *data, uint32_t blksz)
2091 struct hdaa_chan *ch = data;
2093 hdaa_channel_setfragments(obj, data, blksz, ch->pdevinfo->chan_blkcnt);
2099 hdaa_channel_stop(struct hdaa_chan *ch)
2101 struct hdaa_devinfo *devinfo = ch->devinfo;
2102 struct hdaa_widget *w;
2105 if ((ch->flags & HDAA_CHN_RUNNING) == 0)
2107 ch->flags &= ~HDAA_CHN_RUNNING;
2108 HDAC_STREAM_STOP(device_get_parent(devinfo->dev), devinfo->dev,
2109 ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2110 for (i = 0; ch->io[i] != -1; i++) {
2111 w = hdaa_widget_get(ch->devinfo, ch->io[i]);
2114 if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
2115 hda_command(devinfo->dev,
2116 HDA_CMD_SET_DIGITAL_CONV_FMT1(0, ch->io[i], 0));
2118 hda_command(devinfo->dev,
2119 HDA_CMD_SET_CONV_STREAM_CHAN(0, ch->io[i],
2122 HDAC_STREAM_FREE(device_get_parent(devinfo->dev), devinfo->dev,
2123 ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2127 hdaa_channel_start(struct hdaa_chan *ch)
2129 struct hdaa_devinfo *devinfo = ch->devinfo;
2132 fmt = hdaa_stream_format(ch);
2133 ch->stripectl = fls(ch->stripecap & hdaa_allowed_stripes(fmt)) - 1;
2134 ch->sid = HDAC_STREAM_ALLOC(device_get_parent(devinfo->dev), devinfo->dev,
2135 ch->dir == PCMDIR_PLAY ? 1 : 0, fmt, ch->stripectl, &ch->dmapos);
2138 hdaa_audio_setup(ch);
2139 HDAC_STREAM_RESET(device_get_parent(devinfo->dev), devinfo->dev,
2140 ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2141 HDAC_STREAM_START(device_get_parent(devinfo->dev), devinfo->dev,
2142 ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid,
2143 sndbuf_getbufaddr(ch->b), ch->blksz, ch->blkcnt);
2144 ch->flags |= HDAA_CHN_RUNNING;
2149 hdaa_channel_trigger(kobj_t obj, void *data, int go)
2151 struct hdaa_chan *ch = data;
2154 if (!PCMTRIG_COMMON(go))
2157 hdaa_lock(ch->devinfo);
2160 error = hdaa_channel_start(ch);
2164 hdaa_channel_stop(ch);
2169 hdaa_unlock(ch->devinfo);
2175 hdaa_channel_getptr(kobj_t obj, void *data)
2177 struct hdaa_chan *ch = data;
2178 struct hdaa_devinfo *devinfo = ch->devinfo;
2182 if (ch->dmapos != NULL) {
2183 ptr = *(ch->dmapos);
2185 ptr = HDAC_STREAM_GETPTR(
2186 device_get_parent(devinfo->dev), devinfo->dev,
2187 ch->dir == PCMDIR_PLAY ? 1 : 0, ch->sid);
2189 hdaa_unlock(devinfo);
2192 * Round to available space and force 128 bytes aligment.
2194 ptr %= ch->blksz * ch->blkcnt;
2195 ptr &= HDA_BLK_ALIGN;
2200 static struct pcmchan_caps *
2201 hdaa_channel_getcaps(kobj_t obj, void *data)
2203 return (&((struct hdaa_chan *)data)->caps);
2206 static kobj_method_t hdaa_channel_methods[] = {
2207 KOBJMETHOD(channel_init, hdaa_channel_init),
2208 KOBJMETHOD(channel_setformat, hdaa_channel_setformat),
2209 KOBJMETHOD(channel_setspeed, hdaa_channel_setspeed),
2210 KOBJMETHOD(channel_setblocksize, hdaa_channel_setblocksize),
2211 KOBJMETHOD(channel_setfragments, hdaa_channel_setfragments),
2212 KOBJMETHOD(channel_trigger, hdaa_channel_trigger),
2213 KOBJMETHOD(channel_getptr, hdaa_channel_getptr),
2214 KOBJMETHOD(channel_getcaps, hdaa_channel_getcaps),
2217 CHANNEL_DECLARE(hdaa_channel);
2220 hdaa_audio_ctl_ossmixer_init(struct snd_mixer *m)
2222 struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2223 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2224 struct hdaa_widget *w, *cw;
2225 uint32_t mask, recmask;
2229 pdevinfo->mixer = m;
2231 /* Make sure that in case of soft volume it won't stay muted. */
2232 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2233 pdevinfo->left[i] = 100;
2234 pdevinfo->right[i] = 100;
2237 /* Declare volume controls assigned to this association. */
2238 mask = pdevinfo->ossmask;
2239 if (pdevinfo->playas >= 0) {
2240 /* Declate EAPD as ogain control. */
2241 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2242 w = hdaa_widget_get(devinfo, i);
2243 if (w == NULL || w->enable == 0)
2245 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2246 w->param.eapdbtl == HDA_INVALID ||
2247 w->bindas != pdevinfo->playas)
2249 mask |= SOUND_MASK_OGAIN;
2253 /* Declare soft PCM volume if needed. */
2254 if ((mask & SOUND_MASK_PCM) == 0 ||
2255 (devinfo->quirks & HDAA_QUIRK_SOFTPCMVOL) ||
2256 pdevinfo->minamp[SOUND_MIXER_PCM] ==
2257 pdevinfo->maxamp[SOUND_MIXER_PCM]) {
2258 mask |= SOUND_MASK_PCM;
2259 pcm_setflags(pdevinfo->dev, pcm_getflags(pdevinfo->dev) | SD_F_SOFTPCMVOL);
2261 device_printf(pdevinfo->dev,
2262 "Forcing Soft PCM volume\n");
2266 /* Declare master volume if needed. */
2267 if ((mask & SOUND_MASK_VOLUME) == 0) {
2268 mask |= SOUND_MASK_VOLUME;
2269 mix_setparentchild(m, SOUND_MIXER_VOLUME,
2271 mix_setrealdev(m, SOUND_MIXER_VOLUME,
2274 device_printf(pdevinfo->dev,
2275 "Forcing master volume with PCM\n");
2280 /* Declare record sources available to this association. */
2282 if (pdevinfo->recas >= 0) {
2283 for (i = 0; i < 16; i++) {
2284 if (devinfo->as[pdevinfo->recas].dacs[0][i] < 0)
2286 w = hdaa_widget_get(devinfo,
2287 devinfo->as[pdevinfo->recas].dacs[0][i]);
2288 if (w == NULL || w->enable == 0)
2290 for (j = 0; j < w->nconns; j++) {
2291 if (w->connsenable[j] == 0)
2293 cw = hdaa_widget_get(devinfo, w->conns[j]);
2294 if (cw == NULL || cw->enable == 0)
2296 if (cw->bindas != pdevinfo->recas &&
2299 recmask |= cw->ossmask;
2304 recmask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2305 mask &= (1 << SOUND_MIXER_NRDEVICES) - 1;
2306 pdevinfo->ossmask = mask;
2308 mix_setrecdevs(m, recmask);
2309 mix_setdevs(m, mask);
2311 hdaa_unlock(devinfo);
2317 * Update amplification per pdevinfo per ossdev, calculate summary coefficient
2318 * and write it to codec, update *left and *right to reflect remaining error.
2321 hdaa_audio_ctl_dev_set(struct hdaa_audio_ctl *ctl, int ossdev,
2322 int mute, int *left, int *right)
2324 int i, zleft, zright, sleft, sright, smute, lval, rval;
2326 ctl->devleft[ossdev] = *left;
2327 ctl->devright[ossdev] = *right;
2328 ctl->devmute[ossdev] = mute;
2329 smute = sleft = sright = zleft = zright = 0;
2330 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
2331 sleft += ctl->devleft[i];
2332 sright += ctl->devright[i];
2333 smute |= ctl->devmute[i];
2336 zleft += ctl->devleft[i];
2337 zright += ctl->devright[i];
2339 lval = QDB2VAL(ctl, sleft);
2340 rval = QDB2VAL(ctl, sright);
2341 hdaa_audio_ctl_amp_set(ctl, smute, lval, rval);
2342 *left -= VAL2QDB(ctl, lval) - VAL2QDB(ctl, QDB2VAL(ctl, zleft));
2343 *right -= VAL2QDB(ctl, rval) - VAL2QDB(ctl, QDB2VAL(ctl, zright));
2347 * Trace signal from source, setting volumes on the way.
2350 hdaa_audio_ctl_source_volume(struct hdaa_pcm_devinfo *pdevinfo,
2351 int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2353 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2354 struct hdaa_widget *w, *wc;
2355 struct hdaa_audio_ctl *ctl;
2356 int i, j, conns = 0;
2358 if (depth > HDA_PARSE_MAXDEPTH)
2361 w = hdaa_widget_get(devinfo, nid);
2362 if (w == NULL || w->enable == 0)
2365 /* Count number of active inputs. */
2367 for (j = 0; j < w->nconns; j++) {
2368 if (!w->connsenable[j])
2374 /* If this is not a first step - use input mixer.
2375 Pins have common input ctl so care must be taken. */
2376 if (depth > 0 && (conns == 1 ||
2377 w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
2378 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
2381 hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2384 /* If widget has own ossdev - not traverse it.
2385 It will be traversed on it's own. */
2386 if (w->ossdev >= 0 && depth > 0)
2389 /* We must not traverse pin */
2390 if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
2391 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
2396 * If signals mixed, we can't assign controls farther.
2397 * Ignore this on depth zero. Caller must knows why.
2400 (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER ||
2401 w->selconn != index))
2404 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
2406 hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2408 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2409 wc = hdaa_widget_get(devinfo, i);
2410 if (wc == NULL || wc->enable == 0)
2412 for (j = 0; j < wc->nconns; j++) {
2413 if (wc->connsenable[j] && wc->conns[j] == nid) {
2414 hdaa_audio_ctl_source_volume(pdevinfo, ossdev,
2415 wc->nid, j, mute, left, right, depth + 1);
2423 * Trace signal from destination, setting volumes on the way.
2426 hdaa_audio_ctl_dest_volume(struct hdaa_pcm_devinfo *pdevinfo,
2427 int ossdev, nid_t nid, int index, int mute, int left, int right, int depth)
2429 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2430 struct hdaa_audio_as *as = devinfo->as;
2431 struct hdaa_widget *w, *wc;
2432 struct hdaa_audio_ctl *ctl;
2433 int i, j, consumers, cleft, cright;
2435 if (depth > HDA_PARSE_MAXDEPTH)
2438 w = hdaa_widget_get(devinfo, nid);
2439 if (w == NULL || w->enable == 0)
2443 /* If this node produce output for several consumers,
2444 we can't touch it. */
2446 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2447 wc = hdaa_widget_get(devinfo, i);
2448 if (wc == NULL || wc->enable == 0)
2450 for (j = 0; j < wc->nconns; j++) {
2451 if (wc->connsenable[j] && wc->conns[j] == nid)
2455 /* The only exception is if real HP redirection is configured
2456 and this is a duplication point.
2457 XXX: Actually exception is not completely correct.
2458 XXX: Duplication point check is not perfect. */
2459 if ((consumers == 2 && (w->bindas < 0 ||
2460 as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
2461 (w->bindseqmask & (1 << 15)) == 0)) ||
2465 /* Else use it's output mixer. */
2466 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2467 HDAA_CTL_OUT, -1, 1);
2469 hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &left, &right);
2472 /* We must not traverse pin */
2473 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2477 for (i = 0; i < w->nconns; i++) {
2478 if (w->connsenable[i] == 0)
2480 if (index >= 0 && i != index)
2484 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
2487 hdaa_audio_ctl_dev_set(ctl, ossdev, mute, &cleft, &cright);
2488 hdaa_audio_ctl_dest_volume(pdevinfo, ossdev, w->conns[i], -1,
2489 mute, cleft, cright, depth + 1);
2494 * Set volumes for the specified pdevinfo and ossdev.
2497 hdaa_audio_ctl_dev_volume(struct hdaa_pcm_devinfo *pdevinfo, unsigned dev)
2499 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2500 struct hdaa_widget *w, *cw;
2506 if (pdevinfo->left[dev] == 0) {
2507 mute |= HDAA_AMP_MUTE_LEFT;
2510 lvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2511 pdevinfo->left[dev] + 50) / 100 + pdevinfo->minamp[dev];
2512 if (pdevinfo->right[dev] == 0) {
2513 mute |= HDAA_AMP_MUTE_RIGHT;
2516 rvol = ((pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) *
2517 pdevinfo->right[dev] + 50) / 100 + pdevinfo->minamp[dev];
2518 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2519 w = hdaa_widget_get(devinfo, i);
2520 if (w == NULL || w->enable == 0)
2522 if (w->bindas < 0) {
2523 if (pdevinfo->index != 0)
2526 if (w->bindas != pdevinfo->playas &&
2527 w->bindas != pdevinfo->recas)
2530 if (dev == SOUND_MIXER_RECLEV &&
2531 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
2532 hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2533 w->nid, -1, mute, lvol, rvol, 0);
2536 if (dev == SOUND_MIXER_VOLUME &&
2537 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
2538 devinfo->as[w->bindas].dir == HDAA_CTL_OUT) {
2539 hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2540 w->nid, -1, mute, lvol, rvol, 0);
2543 if (dev == SOUND_MIXER_IGAIN &&
2544 w->pflags & HDAA_ADC_MONITOR) {
2545 for (j = 0; j < w->nconns; j++) {
2546 if (!w->connsenable[j])
2548 cw = hdaa_widget_get(devinfo, w->conns[j]);
2549 if (cw == NULL || cw->enable == 0)
2551 if (cw->bindas == -1)
2553 if (cw->bindas >= 0 &&
2554 devinfo->as[cw->bindas].dir != HDAA_CTL_IN)
2556 hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2557 w->nid, j, mute, lvol, rvol, 0);
2561 if (w->ossdev != dev)
2563 hdaa_audio_ctl_source_volume(pdevinfo, dev,
2564 w->nid, -1, mute, lvol, rvol, 0);
2565 if (dev == SOUND_MIXER_IMIX && (w->pflags & HDAA_IMIX_AS_DST))
2566 hdaa_audio_ctl_dest_volume(pdevinfo, dev,
2567 w->nid, -1, mute, lvol, rvol, 0);
2572 * OSS Mixer set method.
2575 hdaa_audio_ctl_ossmixer_set(struct snd_mixer *m, unsigned dev,
2576 unsigned left, unsigned right)
2578 struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2579 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2580 struct hdaa_widget *w;
2585 /* Save new values. */
2586 pdevinfo->left[dev] = left;
2587 pdevinfo->right[dev] = right;
2589 /* 'ogain' is the special case implemented with EAPD. */
2590 if (dev == SOUND_MIXER_OGAIN) {
2593 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2594 w = hdaa_widget_get(devinfo, i);
2595 if (w == NULL || w->enable == 0)
2597 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
2598 w->param.eapdbtl == HDA_INVALID)
2602 if (i >= devinfo->endnode) {
2603 hdaa_unlock(devinfo);
2606 orig = w->param.eapdbtl;
2608 w->param.eapdbtl &= ~HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2610 w->param.eapdbtl |= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2611 if (orig != w->param.eapdbtl) {
2614 val = w->param.eapdbtl;
2615 if (devinfo->quirks & HDAA_QUIRK_EAPDINV)
2616 val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
2617 hda_command(devinfo->dev,
2618 HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid, val));
2620 hdaa_unlock(devinfo);
2621 return (left | (left << 8));
2624 /* Recalculate all controls related to this OSS device. */
2625 hdaa_audio_ctl_dev_volume(pdevinfo, dev);
2627 hdaa_unlock(devinfo);
2628 return (left | (right << 8));
2632 * Set mixer settings to our own default values:
2633 * +20dB for mics, -10dB for analog vol, mute for igain, 0dB for others.
2636 hdaa_audio_ctl_set_defaults(struct hdaa_pcm_devinfo *pdevinfo)
2640 for (dev = 0; dev < SOUND_MIXER_NRDEVICES; dev++) {
2641 if ((pdevinfo->ossmask & (1 << dev)) == 0)
2644 /* If the value was overriden, leave it as is. */
2645 if (resource_int_value(device_get_name(pdevinfo->dev),
2646 device_get_unit(pdevinfo->dev), ossnames[dev], &vol) == 0)
2650 if (dev == SOUND_MIXER_OGAIN)
2652 else if (dev == SOUND_MIXER_IGAIN)
2654 else if (dev == SOUND_MIXER_MIC ||
2655 dev == SOUND_MIXER_MONITOR)
2656 amp = 20 * 4; /* +20dB */
2657 else if (dev == SOUND_MIXER_VOLUME && !pdevinfo->digital)
2658 amp = -10 * 4; /* -10dB */
2662 (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) <= 0) {
2664 } else if (vol < 0) {
2665 vol = ((amp - pdevinfo->minamp[dev]) * 100 +
2666 (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]) / 2) /
2667 (pdevinfo->maxamp[dev] - pdevinfo->minamp[dev]);
2668 vol = imin(imax(vol, 1), 100);
2670 mix_set(pdevinfo->mixer, dev, vol, vol);
2675 * Recursively commutate specified record source.
2678 hdaa_audio_ctl_recsel_comm(struct hdaa_pcm_devinfo *pdevinfo, uint32_t src, nid_t nid, int depth)
2680 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2681 struct hdaa_widget *w, *cw;
2682 struct hdaa_audio_ctl *ctl;
2687 if (depth > HDA_PARSE_MAXDEPTH)
2690 w = hdaa_widget_get(devinfo, nid);
2691 if (w == NULL || w->enable == 0)
2694 for (i = 0; i < w->nconns; i++) {
2695 if (w->connsenable[i] == 0)
2697 cw = hdaa_widget_get(devinfo, w->conns[i]);
2698 if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
2700 /* Call recursively to trace signal to it's source if needed. */
2701 if ((src & cw->ossmask) != 0) {
2702 if (cw->ossdev < 0) {
2703 res |= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2704 w->conns[i], depth + 1);
2709 /* We have two special cases: mixers and others (selectors). */
2710 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) {
2711 ctl = hdaa_audio_ctl_amp_get(devinfo,
2712 w->nid, HDAA_CTL_IN, i, 1);
2715 /* If we have input control on this node mute them
2716 * according to requested sources. */
2717 muted = (src & cw->ossmask) ? 0 : 1;
2718 if (muted != ctl->forcemute) {
2719 ctl->forcemute = muted;
2720 hdaa_audio_ctl_amp_set(ctl,
2721 HDAA_AMP_MUTE_DEFAULT,
2722 HDAA_AMP_VOL_DEFAULT, HDAA_AMP_VOL_DEFAULT);
2725 device_printf(pdevinfo->dev,
2726 "Recsel (%s): nid %d source %d %s\n",
2727 hdaa_audio_ctl_ossmixer_mask2allname(
2728 src, buf, sizeof(buf)),
2729 nid, i, muted?"mute":"unmute");
2734 if ((src & cw->ossmask) == 0)
2736 /* If we found requested source - select it and exit. */
2737 hdaa_widget_connection_select(w, i);
2739 device_printf(pdevinfo->dev,
2740 "Recsel (%s): nid %d source %d select\n",
2741 hdaa_audio_ctl_ossmixer_mask2allname(
2742 src, buf, sizeof(buf)),
2752 hdaa_audio_ctl_ossmixer_setrecsrc(struct snd_mixer *m, uint32_t src)
2754 struct hdaa_pcm_devinfo *pdevinfo = mix_getdevinfo(m);
2755 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
2756 struct hdaa_widget *w;
2757 struct hdaa_audio_as *as;
2758 struct hdaa_audio_ctl *ctl;
2759 struct hdaa_chan *ch;
2761 uint32_t ret = 0xffffffff;
2764 if (pdevinfo->recas < 0) {
2765 hdaa_unlock(devinfo);
2768 as = &devinfo->as[pdevinfo->recas];
2770 /* For non-mixed associations we always recording everything. */
2772 hdaa_unlock(devinfo);
2773 return (mix_getrecdevs(m));
2776 /* Commutate requested recsrc for each ADC. */
2777 for (j = 0; j < as->num_chans; j++) {
2778 ch = &devinfo->chans[as->chans[j]];
2779 for (i = 0; ch->io[i] >= 0; i++) {
2780 w = hdaa_widget_get(devinfo, ch->io[i]);
2781 if (w == NULL || w->enable == 0)
2783 ret &= hdaa_audio_ctl_recsel_comm(pdevinfo, src,
2787 if (ret == 0xffffffff)
2791 * Some controls could be shared. Reset volumes for controls
2792 * related to previously chosen devices, as they may no longer
2793 * affect the signal.
2796 while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
2797 if (ctl->enable == 0 ||
2798 !(ctl->ossmask & pdevinfo->recsrc))
2800 if (!((pdevinfo->playas >= 0 &&
2801 ctl->widget->bindas == pdevinfo->playas) ||
2802 (pdevinfo->recas >= 0 &&
2803 ctl->widget->bindas == pdevinfo->recas) ||
2804 (pdevinfo->index == 0 &&
2805 ctl->widget->bindas == -2)))
2807 for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2808 if (pdevinfo->recsrc & (1 << j)) {
2809 ctl->devleft[j] = 0;
2810 ctl->devright[j] = 0;
2811 ctl->devmute[j] = 0;
2817 * Some controls could be shared. Set volumes for controls
2818 * related to devices selected both previously and now.
2820 for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
2821 if ((ret | pdevinfo->recsrc) & (1 << j))
2822 hdaa_audio_ctl_dev_volume(pdevinfo, j);
2825 pdevinfo->recsrc = ret;
2826 hdaa_unlock(devinfo);
2830 static kobj_method_t hdaa_audio_ctl_ossmixer_methods[] = {
2831 KOBJMETHOD(mixer_init, hdaa_audio_ctl_ossmixer_init),
2832 KOBJMETHOD(mixer_set, hdaa_audio_ctl_ossmixer_set),
2833 KOBJMETHOD(mixer_setrecsrc, hdaa_audio_ctl_ossmixer_setrecsrc),
2836 MIXER_DECLARE(hdaa_audio_ctl_ossmixer);
2839 hdaa_dump_gpi(struct hdaa_devinfo *devinfo)
2841 device_t dev = devinfo->dev;
2843 uint32_t data, wake, unsol, sticky;
2845 if (HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap) > 0) {
2846 data = hda_command(dev,
2847 HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
2848 wake = hda_command(dev,
2849 HDA_CMD_GET_GPI_WAKE_ENABLE_MASK(0, devinfo->nid));
2850 unsol = hda_command(dev,
2851 HDA_CMD_GET_GPI_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2852 sticky = hda_command(dev,
2853 HDA_CMD_GET_GPI_STICKY_MASK(0, devinfo->nid));
2854 for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap); i++) {
2855 device_printf(dev, " GPI%d:%s%s%s state=%d", i,
2856 (sticky & (1 << i)) ? " sticky" : "",
2857 (unsol & (1 << i)) ? " unsol" : "",
2858 (wake & (1 << i)) ? " wake" : "",
2865 hdaa_dump_gpio(struct hdaa_devinfo *devinfo)
2867 device_t dev = devinfo->dev;
2869 uint32_t data, dir, enable, wake, unsol, sticky;
2871 if (HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap) > 0) {
2872 data = hda_command(dev,
2873 HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
2874 enable = hda_command(dev,
2875 HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
2876 dir = hda_command(dev,
2877 HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
2878 wake = hda_command(dev,
2879 HDA_CMD_GET_GPIO_WAKE_ENABLE_MASK(0, devinfo->nid));
2880 unsol = hda_command(dev,
2881 HDA_CMD_GET_GPIO_UNSOLICITED_ENABLE_MASK(0, devinfo->nid));
2882 sticky = hda_command(dev,
2883 HDA_CMD_GET_GPIO_STICKY_MASK(0, devinfo->nid));
2884 for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap); i++) {
2885 device_printf(dev, " GPIO%d: ", i);
2886 if ((enable & (1 << i)) == 0) {
2887 printf("disabled\n");
2890 if ((dir & (1 << i)) == 0) {
2891 printf("input%s%s%s",
2892 (sticky & (1 << i)) ? " sticky" : "",
2893 (unsol & (1 << i)) ? " unsol" : "",
2894 (wake & (1 << i)) ? " wake" : "");
2897 printf(" state=%d\n", (data >> i) & 1);
2903 hdaa_dump_gpo(struct hdaa_devinfo *devinfo)
2905 device_t dev = devinfo->dev;
2909 if (HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap) > 0) {
2910 data = hda_command(dev,
2911 HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
2912 for (i = 0; i < HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap); i++) {
2913 device_printf(dev, " GPO%d: state=%d", i,
2920 hdaa_audio_parse(struct hdaa_devinfo *devinfo)
2922 struct hdaa_widget *w;
2929 res = hda_command(devinfo->dev,
2930 HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_GPIO_COUNT));
2931 devinfo->gpio_cap = res;
2934 device_printf(devinfo->dev,
2935 "NumGPIO=%d NumGPO=%d "
2936 "NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
2937 HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
2938 HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
2939 HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
2940 HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
2941 HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
2942 hdaa_dump_gpi(devinfo);
2943 hdaa_dump_gpio(devinfo);
2944 hdaa_dump_gpo(devinfo);
2947 res = hda_command(devinfo->dev,
2948 HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_STREAM_FORMATS));
2949 devinfo->supp_stream_formats = res;
2951 res = hda_command(devinfo->dev,
2952 HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_SUPP_PCM_SIZE_RATE));
2953 devinfo->supp_pcm_size_rate = res;
2955 res = hda_command(devinfo->dev,
2956 HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_OUTPUT_AMP_CAP));
2957 devinfo->outamp_cap = res;
2959 res = hda_command(devinfo->dev,
2960 HDA_CMD_GET_PARAMETER(0, nid, HDA_PARAM_INPUT_AMP_CAP));
2961 devinfo->inamp_cap = res;
2963 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2964 w = hdaa_widget_get(devinfo, i);
2966 device_printf(devinfo->dev, "Ghost widget! nid=%d!\n", i);
2968 w->devinfo = devinfo;
2975 w->param.eapdbtl = HDA_INVALID;
2976 hdaa_widget_parse(w);
2982 hdaa_audio_postprocess(struct hdaa_devinfo *devinfo)
2984 struct hdaa_widget *w;
2987 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
2988 w = hdaa_widget_get(devinfo, i);
2991 hdaa_widget_postprocess(w);
2996 hdaa_audio_ctl_parse(struct hdaa_devinfo *devinfo)
2998 struct hdaa_audio_ctl *ctls;
2999 struct hdaa_widget *w, *cw;
3000 int i, j, cnt, max, ocap, icap;
3001 int mute, offset, step, size;
3003 /* XXX This is redundant */
3005 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3006 w = hdaa_widget_get(devinfo, i);
3007 if (w == NULL || w->enable == 0)
3009 if (w->param.outamp_cap != 0)
3011 if (w->param.inamp_cap != 0) {
3013 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3014 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3015 for (j = 0; j < w->nconns; j++) {
3016 cw = hdaa_widget_get(devinfo,
3018 if (cw == NULL || cw->enable == 0)
3029 devinfo->ctlcnt = max;
3034 ctls = (struct hdaa_audio_ctl *)malloc(
3035 sizeof(*ctls) * max, M_HDAA, M_ZERO | M_NOWAIT);
3039 device_printf(devinfo->dev, "unable to allocate ctls!\n");
3040 devinfo->ctlcnt = 0;
3045 for (i = devinfo->startnode; cnt < max && i < devinfo->endnode; i++) {
3047 device_printf(devinfo->dev, "%s: Ctl overflow!\n",
3051 w = hdaa_widget_get(devinfo, i);
3052 if (w == NULL || w->enable == 0)
3054 ocap = w->param.outamp_cap;
3055 icap = w->param.inamp_cap;
3057 mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(ocap);
3058 step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(ocap);
3059 size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(ocap);
3060 offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(ocap);
3061 /*if (offset > step) {
3063 device_printf(devinfo->dev,
3064 "BUGGY outamp: nid=%d "
3065 "[offset=%d > step=%d]\n",
3066 w->nid, offset, step);
3070 ctls[cnt].enable = 1;
3071 ctls[cnt].widget = w;
3072 ctls[cnt].mute = mute;
3073 ctls[cnt].step = step;
3074 ctls[cnt].size = size;
3075 ctls[cnt].offset = offset;
3076 ctls[cnt].left = offset;
3077 ctls[cnt].right = offset;
3078 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
3080 ctls[cnt].ndir = HDAA_CTL_IN;
3082 ctls[cnt].ndir = HDAA_CTL_OUT;
3083 ctls[cnt++].dir = HDAA_CTL_OUT;
3087 mute = HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(icap);
3088 step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(icap);
3089 size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(icap);
3090 offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(icap);
3091 /*if (offset > step) {
3093 device_printf(devinfo->dev,
3094 "BUGGY inamp: nid=%d "
3095 "[offset=%d > step=%d]\n",
3096 w->nid, offset, step);
3101 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR:
3102 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER:
3103 for (j = 0; j < w->nconns; j++) {
3105 device_printf(devinfo->dev,
3106 "%s: Ctl overflow!\n",
3110 cw = hdaa_widget_get(devinfo,
3112 if (cw == NULL || cw->enable == 0)
3114 ctls[cnt].enable = 1;
3115 ctls[cnt].widget = w;
3116 ctls[cnt].childwidget = cw;
3117 ctls[cnt].index = j;
3118 ctls[cnt].mute = mute;
3119 ctls[cnt].step = step;
3120 ctls[cnt].size = size;
3121 ctls[cnt].offset = offset;
3122 ctls[cnt].left = offset;
3123 ctls[cnt].right = offset;
3124 ctls[cnt].ndir = HDAA_CTL_IN;
3125 ctls[cnt++].dir = HDAA_CTL_IN;
3130 device_printf(devinfo->dev,
3131 "%s: Ctl overflow!\n",
3135 ctls[cnt].enable = 1;
3136 ctls[cnt].widget = w;
3137 ctls[cnt].mute = mute;
3138 ctls[cnt].step = step;
3139 ctls[cnt].size = size;
3140 ctls[cnt].offset = offset;
3141 ctls[cnt].left = offset;
3142 ctls[cnt].right = offset;
3144 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3145 ctls[cnt].ndir = HDAA_CTL_OUT;
3147 ctls[cnt].ndir = HDAA_CTL_IN;
3148 ctls[cnt++].dir = HDAA_CTL_IN;
3154 devinfo->ctl = ctls;
3158 hdaa_audio_as_parse(struct hdaa_devinfo *devinfo)
3160 struct hdaa_audio_as *as;
3161 struct hdaa_widget *w;
3162 int i, j, cnt, max, type, dir, assoc, seq, first, hpredir;
3164 /* Count present associations */
3166 for (j = 1; j < 16; j++) {
3167 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3168 w = hdaa_widget_get(devinfo, i);
3169 if (w == NULL || w->enable == 0)
3171 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3173 if (HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config)
3177 if (j != 15) /* There could be many 1-pin assocs #15 */
3182 devinfo->ascnt = max;
3187 as = (struct hdaa_audio_as *)malloc(
3188 sizeof(*as) * max, M_HDAA, M_ZERO | M_NOWAIT);
3192 device_printf(devinfo->dev, "unable to allocate assocs!\n");
3197 for (i = 0; i < max; i++) {
3200 as[i].num_chans = 1;
3201 as[i].location = -1;
3204 /* Scan associations skipping as=0. */
3206 for (j = 1; j < 16 && cnt < max; j++) {
3209 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3210 w = hdaa_widget_get(devinfo, i);
3211 if (w == NULL || w->enable == 0)
3213 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
3215 assoc = HDA_CONFIG_DEFAULTCONF_ASSOCIATION(w->wclass.pin.config);
3216 seq = HDA_CONFIG_DEFAULTCONF_SEQUENCE(w->wclass.pin.config);
3221 ("%s: Associations owerflow (%d of %d)",
3222 __func__, cnt, max));
3223 type = w->wclass.pin.config &
3224 HDA_CONFIG_DEFAULTCONF_DEVICE_MASK;
3225 /* Get pin direction. */
3226 if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT ||
3227 type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER ||
3228 type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT ||
3229 type == HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT ||
3230 type == HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT)
3234 /* If this is a first pin - create new association. */
3235 if (as[cnt].pincnt == 0) {
3242 /* Check association correctness. */
3243 if (as[cnt].pins[seq] != 0) {
3244 device_printf(devinfo->dev, "%s: Duplicate pin %d (%d) "
3245 "in association %d! Disabling association.\n",
3246 __func__, seq, w->nid, j);
3249 if (dir != as[cnt].dir) {
3250 device_printf(devinfo->dev, "%s: Pin %d has wrong "
3251 "direction for association %d! Disabling "
3253 __func__, w->nid, j);
3256 if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
3257 as[cnt].digital |= 0x1;
3258 if (HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
3259 as[cnt].digital |= 0x2;
3260 if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap))
3261 as[cnt].digital |= 0x4;
3263 if (as[cnt].location == -1) {
3265 HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config);
3266 } else if (as[cnt].location !=
3267 HDA_CONFIG_DEFAULTCONF_LOCATION(w->wclass.pin.config)) {
3268 as[cnt].location = -2;
3270 /* Headphones with seq=15 may mean redirection. */
3271 if (type == HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT &&
3274 as[cnt].pins[seq] = w->nid;
3276 /* Association 15 is a multiple unassociated pins. */
3280 if (j != 15 && as[cnt].pincnt > 0) {
3281 if (hpredir && as[cnt].pincnt > 1)
3282 as[cnt].hpredir = first;
3286 for (i = 0; i < max; i++) {
3287 if (as[i].dir == HDAA_CTL_IN && (as[i].pincnt == 1 ||
3288 as[i].pins[14] > 0 || as[i].pins[15] > 0))
3292 device_printf(devinfo->dev,
3293 "%d associations found:\n", max);
3294 for (i = 0; i < max; i++) {
3295 device_printf(devinfo->dev,
3296 "Association %d (%d) %s%s:\n",
3297 i, as[i].index, (as[i].dir == HDAA_CTL_IN)?"in":"out",
3298 as[i].enable?"":" (disabled)");
3299 for (j = 0; j < 16; j++) {
3300 if (as[i].pins[j] == 0)
3302 device_printf(devinfo->dev,
3303 " Pin nid=%d seq=%d\n",
3313 * Trace path from DAC to pin.
3316 hdaa_audio_trace_dac(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3317 int dupseq, int min, int only, int depth)
3319 struct hdaa_widget *w;
3323 if (depth > HDA_PARSE_MAXDEPTH)
3325 w = hdaa_widget_get(devinfo, nid);
3326 if (w == NULL || w->enable == 0)
3330 device_printf(devinfo->dev,
3331 " %*stracing via nid %d\n",
3332 depth + 1, "", w->nid);
3335 /* Use only unused widgets */
3336 if (w->bindas >= 0 && w->bindas != as) {
3339 device_printf(devinfo->dev,
3340 " %*snid %d busy by association %d\n",
3341 depth + 1, "", w->nid, w->bindas);
3347 if (w->bindseqmask != 0) {
3350 device_printf(devinfo->dev,
3351 " %*snid %d busy by seqmask %x\n",
3352 depth + 1, "", w->nid, w->bindseqmask);
3358 /* If this is headphones - allow duplicate first pin. */
3359 if (w->bindseqmask != 0 &&
3360 (w->bindseqmask & (1 << dupseq)) == 0) {
3362 device_printf(devinfo->dev,
3363 " %*snid %d busy by seqmask %x\n",
3364 depth + 1, "", w->nid, w->bindseqmask);
3371 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3372 /* Do not traverse input. AD1988 has digital monitor
3373 for which we are not ready. */
3375 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
3376 /* If we are tracing HP take only dac of first pin. */
3377 if ((only == 0 || only == w->nid) &&
3378 (w->nid >= min) && (dupseq < 0 || w->nid ==
3379 devinfo->as[as].dacs[0][dupseq]))
3382 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3387 /* Find reachable DACs with smallest nid respecting constraints. */
3388 for (i = 0; i < w->nconns; i++) {
3389 if (w->connsenable[i] == 0)
3391 if (w->selconn != -1 && w->selconn != i)
3393 if ((ret = hdaa_audio_trace_dac(devinfo, as, seq,
3394 w->conns[i], dupseq, min, only, depth + 1)) != 0) {
3395 if (m == 0 || ret < m) {
3399 if (only || dupseq >= 0)
3403 if (im >= 0 && only && ((w->nconns > 1 &&
3404 w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3405 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3411 w->bindseqmask |= (1 << seq);
3415 device_printf(devinfo->dev,
3416 " %*snid %d returned %d\n",
3417 depth + 1, "", w->nid, m);
3424 * Trace path from widget to ADC.
3427 hdaa_audio_trace_adc(struct hdaa_devinfo *devinfo, int as, int seq, nid_t nid,
3428 int mixed, int min, int only, int depth, int *length, int onlylength)
3430 struct hdaa_widget *w, *wc;
3431 int i, j, im, lm = HDA_PARSE_MAXDEPTH;
3434 if (depth > HDA_PARSE_MAXDEPTH)
3436 w = hdaa_widget_get(devinfo, nid);
3437 if (w == NULL || w->enable == 0)
3440 device_printf(devinfo->dev,
3441 " %*stracing via nid %d\n",
3442 depth + 1, "", w->nid);
3444 /* Use only unused widgets */
3445 if (w->bindas >= 0 && w->bindas != as) {
3447 device_printf(devinfo->dev,
3448 " %*snid %d busy by association %d\n",
3449 depth + 1, "", w->nid, w->bindas);
3453 if (!mixed && w->bindseqmask != 0) {
3455 device_printf(devinfo->dev,
3456 " %*snid %d busy by seqmask %x\n",
3457 depth + 1, "", w->nid, w->bindseqmask);
3462 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3463 if ((only == 0 || only == w->nid) && (w->nid >= min) &&
3464 (onlylength == 0 || onlylength == depth)) {
3469 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3474 /* Try to find reachable ADCs with specified nid. */
3475 for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3476 wc = hdaa_widget_get(devinfo, j);
3477 if (wc == NULL || wc->enable == 0)
3480 for (i = 0; i < wc->nconns; i++) {
3481 if (wc->connsenable[i] == 0)
3483 if (wc->conns[i] != nid)
3485 if ((ret = hdaa_audio_trace_adc(devinfo, as, seq,
3486 j, mixed, min, only, depth + 1,
3487 length, onlylength)) != 0) {
3488 if (m == 0 || ret < m ||
3489 (ret == m && *length < lm)) {
3499 if (im >= 0 && only && ((wc->nconns > 1 &&
3500 wc->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER) ||
3501 wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR))
3508 w->bindseqmask |= (1 << seq);
3511 device_printf(devinfo->dev,
3512 " %*snid %d returned %d\n",
3513 depth + 1, "", w->nid, m);
3519 * Erase trace path of the specified association.
3522 hdaa_audio_undo_trace(struct hdaa_devinfo *devinfo, int as, int seq)
3524 struct hdaa_widget *w;
3527 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3528 w = hdaa_widget_get(devinfo, i);
3529 if (w == NULL || w->enable == 0)
3531 if (w->bindas == as) {
3533 w->bindseqmask &= ~(1 << seq);
3534 if (w->bindseqmask == 0) {
3548 * Trace association path from DAC to output
3551 hdaa_audio_trace_as_out(struct hdaa_devinfo *devinfo, int as, int seq)
3553 struct hdaa_audio_as *ases = devinfo->as;
3558 for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3560 /* Check if there is no any left. If so - we succeeded. */
3564 hpredir = (i == 15 && ases[as].fakeredir == 0)?ases[as].hpredir:-1;
3568 device_printf(devinfo->dev,
3569 " Tracing pin %d with min nid %d",
3570 ases[as].pins[i], min);
3572 printf(" and hpredir %d", hpredir);
3575 /* Trace this pin taking min nid into account. */
3576 res = hdaa_audio_trace_dac(devinfo, as, i,
3577 ases[as].pins[i], hpredir, min, 0, 0);
3579 /* If we failed - return to previous and redo it. */
3581 device_printf(devinfo->dev,
3582 " Unable to trace pin %d seq %d with min "
3584 ases[as].pins[i], i, min);
3586 printf(" and hpredir %d", hpredir);
3592 device_printf(devinfo->dev,
3593 " Pin %d traced to DAC %d",
3594 ases[as].pins[i], res);
3596 printf(" and hpredir %d", hpredir);
3597 if (ases[as].fakeredir)
3598 printf(" with fake redirection");
3601 /* Trace again to mark the path */
3602 hdaa_audio_trace_dac(devinfo, as, i,
3603 ases[as].pins[i], hpredir, min, res, 0);
3604 ases[as].dacs[0][i] = res;
3605 /* We succeeded, so call next. */
3606 if (hdaa_audio_trace_as_out(devinfo, as, i + 1))
3608 /* If next failed, we should retry with next min */
3609 hdaa_audio_undo_trace(devinfo, as, i);
3610 ases[as].dacs[0][i] = 0;
3616 * Check equivalency of two DACs.
3619 hdaa_audio_dacs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3621 struct hdaa_devinfo *devinfo = w1->devinfo;
3622 struct hdaa_widget *w3;
3625 if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3627 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
3628 w3 = hdaa_widget_get(devinfo, i);
3629 if (w3 == NULL || w3->enable == 0)
3631 if (w3->bindas != w1->bindas)
3633 if (w3->nconns == 0)
3636 for (j = 0; j < w3->nconns; j++) {
3637 if (w3->connsenable[j] == 0)
3639 if (w3->conns[j] == w1->nid)
3641 if (w3->conns[j] == w2->nid)
3648 if (w3->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3655 * Check equivalency of two ADCs.
3658 hdaa_audio_adcs_equal(struct hdaa_widget *w1, struct hdaa_widget *w2)
3660 struct hdaa_devinfo *devinfo = w1->devinfo;
3661 struct hdaa_widget *w3, *w4;
3664 if (memcmp(&w1->param, &w2->param, sizeof(w1->param)))
3666 if (w1->nconns != 1 || w2->nconns != 1)
3668 if (w1->conns[0] == w2->conns[0])
3670 w3 = hdaa_widget_get(devinfo, w1->conns[0]);
3671 if (w3 == NULL || w3->enable == 0)
3673 w4 = hdaa_widget_get(devinfo, w2->conns[0]);
3674 if (w4 == NULL || w4->enable == 0)
3676 if (w3->bindas == w4->bindas && w3->bindseqmask == w4->bindseqmask)
3678 if (w4->bindas >= 0)
3680 if (w3->type != w4->type)
3682 if (memcmp(&w3->param, &w4->param, sizeof(w3->param)))
3684 if (w3->nconns != w4->nconns)
3686 for (i = 0; i < w3->nconns; i++) {
3687 if (w3->conns[i] != w4->conns[i])
3694 * Look for equivalent DAC/ADC to implement second channel.
3697 hdaa_audio_adddac(struct hdaa_devinfo *devinfo, int asid)
3699 struct hdaa_audio_as *as = &devinfo->as[asid];
3700 struct hdaa_widget *w1, *w2;
3705 device_printf(devinfo->dev,
3706 "Looking for additional %sC "
3707 "for association %d (%d)\n",
3708 (as->dir == HDAA_CTL_OUT) ? "DA" : "AD",
3712 /* Find the exisitng DAC position and return if found more the one. */
3714 for (i = 0; i < 16; i++) {
3715 if (as->dacs[0][i] <= 0)
3717 if (pos >= 0 && as->dacs[0][i] != as->dacs[0][pos])
3722 nid1 = as->dacs[0][pos];
3723 w1 = hdaa_widget_get(devinfo, nid1);
3725 for (nid2 = devinfo->startnode; nid2 < devinfo->endnode; nid2++) {
3726 w2 = hdaa_widget_get(devinfo, nid2);
3727 if (w2 == NULL || w2->enable == 0)
3729 if (w2->bindas >= 0)
3731 if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT) {
3732 if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT)
3734 if (hdaa_audio_dacs_equal(w1, w2))
3737 if (w2->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3739 if (hdaa_audio_adcs_equal(w1, w2))
3743 if (nid2 >= devinfo->endnode)
3745 w2->bindas = w1->bindas;
3746 w2->bindseqmask = w1->bindseqmask;
3747 if (w1->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
3749 device_printf(devinfo->dev,
3750 " ADC %d considered equal to ADC %d\n", nid2, nid1);
3752 w1 = hdaa_widget_get(devinfo, w1->conns[0]);
3753 w2 = hdaa_widget_get(devinfo, w2->conns[0]);
3754 w2->bindas = w1->bindas;
3755 w2->bindseqmask = w1->bindseqmask;
3758 device_printf(devinfo->dev,
3759 " DAC %d considered equal to DAC %d\n", nid2, nid1);
3762 for (i = 0; i < 16; i++) {
3763 if (as->dacs[0][i] <= 0)
3765 as->dacs[as->num_chans][i] = nid2;
3771 * Trace association path from input to ADC
3774 hdaa_audio_trace_as_in(struct hdaa_devinfo *devinfo, int as)
3776 struct hdaa_audio_as *ases = devinfo->as;
3777 struct hdaa_widget *w;
3778 int i, j, k, length;
3780 for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3781 w = hdaa_widget_get(devinfo, j);
3782 if (w == NULL || w->enable == 0)
3784 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
3786 if (w->bindas >= 0 && w->bindas != as)
3790 for (i = 0; i < 16; i++) {
3791 if (ases[as].pins[i] == 0)
3795 device_printf(devinfo->dev,
3796 " Tracing pin %d to ADC %d\n",
3797 ases[as].pins[i], j);
3799 /* Trace this pin taking goal into account. */
3800 if (hdaa_audio_trace_adc(devinfo, as, i,
3801 ases[as].pins[i], 1, 0, j, 0, &length, 0) == 0) {
3802 /* If we failed - return to previous and redo it. */
3804 device_printf(devinfo->dev,
3805 " Unable to trace pin %d to ADC %d, undo traces\n",
3806 ases[as].pins[i], j);
3808 hdaa_audio_undo_trace(devinfo, as, -1);
3809 for (k = 0; k < 16; k++)
3810 ases[as].dacs[0][k] = 0;
3814 device_printf(devinfo->dev,
3815 " Pin %d traced to ADC %d\n",
3816 ases[as].pins[i], j);
3818 ases[as].dacs[0][i] = j;
3827 * Trace association path from input to multiple ADCs
3830 hdaa_audio_trace_as_in_mch(struct hdaa_devinfo *devinfo, int as, int seq)
3832 struct hdaa_audio_as *ases = devinfo->as;
3837 for (i = seq; i < 16 && ases[as].pins[i] == 0; i++)
3839 /* Check if there is no any left. If so - we succeeded. */
3846 device_printf(devinfo->dev,
3847 " Tracing pin %d with min nid %d",
3848 ases[as].pins[i], min);
3851 /* Trace this pin taking min nid into account. */
3852 res = hdaa_audio_trace_adc(devinfo, as, i,
3853 ases[as].pins[i], 0, min, 0, 0, &length, 0);
3855 /* If we failed - return to previous and redo it. */
3857 device_printf(devinfo->dev,
3858 " Unable to trace pin %d seq %d with min "
3860 ases[as].pins[i], i, min);
3866 device_printf(devinfo->dev,
3867 " Pin %d traced to ADC %d\n",
3868 ases[as].pins[i], res);
3870 /* Trace again to mark the path */
3871 hdaa_audio_trace_adc(devinfo, as, i,
3872 ases[as].pins[i], 0, min, res, 0, &length, length);
3873 ases[as].dacs[0][i] = res;
3874 /* We succeeded, so call next. */
3875 if (hdaa_audio_trace_as_in_mch(devinfo, as, i + 1))
3877 /* If next failed, we should retry with next min */
3878 hdaa_audio_undo_trace(devinfo, as, i);
3879 ases[as].dacs[0][i] = 0;
3885 * Trace input monitor path from mixer to output association.
3888 hdaa_audio_trace_to_out(struct hdaa_devinfo *devinfo, nid_t nid, int depth)
3890 struct hdaa_audio_as *ases = devinfo->as;
3891 struct hdaa_widget *w, *wc;
3895 if (depth > HDA_PARSE_MAXDEPTH)
3897 w = hdaa_widget_get(devinfo, nid);
3898 if (w == NULL || w->enable == 0)
3901 device_printf(devinfo->dev,
3902 " %*stracing via nid %d\n",
3903 depth + 1, "", w->nid);
3905 /* Use only unused widgets */
3906 if (depth > 0 && w->bindas != -1) {
3907 if (w->bindas < 0 || ases[w->bindas].dir == HDAA_CTL_OUT) {
3909 device_printf(devinfo->dev,
3910 " %*snid %d found output association %d\n",
3911 depth + 1, "", w->nid, w->bindas);
3914 w->pflags |= HDAA_ADC_MONITOR;
3918 device_printf(devinfo->dev,
3919 " %*snid %d busy by input association %d\n",
3920 depth + 1, "", w->nid, w->bindas);
3927 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT:
3928 /* Do not traverse input. AD1988 has digital monitor
3929 for which we are not ready. */
3931 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
3936 /* Try to find reachable ADCs with specified nid. */
3937 for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3938 wc = hdaa_widget_get(devinfo, j);
3939 if (wc == NULL || wc->enable == 0)
3941 for (i = 0; i < wc->nconns; i++) {
3942 if (wc->connsenable[i] == 0)
3944 if (wc->conns[i] != nid)
3946 if (hdaa_audio_trace_to_out(devinfo,
3947 j, depth + 1) != 0) {
3949 if (wc->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
3957 if (res && w->bindas == -1)
3961 device_printf(devinfo->dev,
3962 " %*snid %d returned %d\n",
3963 depth + 1, "", w->nid, res);
3969 * Trace extra associations (beeper, monitor)
3972 hdaa_audio_trace_as_extra(struct hdaa_devinfo *devinfo)
3974 struct hdaa_audio_as *as = devinfo->as;
3975 struct hdaa_widget *w;
3979 /* Find mixer associated with input, but supplying signal
3980 for output associations. Hope it will be input monitor. */
3982 device_printf(devinfo->dev,
3983 "Tracing input monitor\n");
3985 for (j = devinfo->startnode; j < devinfo->endnode; j++) {
3986 w = hdaa_widget_get(devinfo, j);
3987 if (w == NULL || w->enable == 0)
3989 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
3991 if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
3994 device_printf(devinfo->dev,
3995 " Tracing nid %d to out\n",
3998 if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4000 device_printf(devinfo->dev,
4001 " nid %d is input monitor\n",
4004 w->ossdev = SOUND_MIXER_IMIX;
4008 /* Other inputs monitor */
4009 /* Find input pins supplying signal for output associations.
4010 Hope it will be input monitoring. */
4012 device_printf(devinfo->dev,
4013 "Tracing other input monitors\n");
4015 for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4016 w = hdaa_widget_get(devinfo, j);
4017 if (w == NULL || w->enable == 0)
4019 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4021 if (w->bindas < 0 || as[w->bindas].dir != HDAA_CTL_IN)
4024 device_printf(devinfo->dev,
4025 " Tracing nid %d to out\n",
4028 if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4030 device_printf(devinfo->dev,
4031 " nid %d is input monitor\n",
4039 device_printf(devinfo->dev,
4040 "Tracing beeper\n");
4042 for (j = devinfo->startnode; j < devinfo->endnode; j++) {
4043 w = hdaa_widget_get(devinfo, j);
4044 if (w == NULL || w->enable == 0)
4046 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET)
4049 device_printf(devinfo->dev,
4050 " Tracing nid %d to out\n",
4053 if (hdaa_audio_trace_to_out(devinfo, w->nid, 0)) {
4055 device_printf(devinfo->dev,
4056 " nid %d traced to out\n",
4065 * Bind assotiations to PCM channels
4068 hdaa_audio_bind_as(struct hdaa_devinfo *devinfo)
4070 struct hdaa_audio_as *as = devinfo->as;
4071 int i, j, cnt = 0, free;
4073 for (j = 0; j < devinfo->ascnt; j++) {
4075 cnt += as[j].num_chans;
4077 if (devinfo->num_chans == 0) {
4078 devinfo->chans = (struct hdaa_chan *)malloc(
4079 sizeof(struct hdaa_chan) * cnt,
4080 M_HDAA, M_ZERO | M_NOWAIT);
4081 if (devinfo->chans == NULL) {
4082 device_printf(devinfo->dev,
4083 "Channels memory allocation failed!\n");
4087 devinfo->chans = (struct hdaa_chan *)realloc(devinfo->chans,
4088 sizeof(struct hdaa_chan) * (devinfo->num_chans + cnt),
4089 M_HDAA, M_ZERO | M_NOWAIT);
4090 if (devinfo->chans == NULL) {
4091 devinfo->num_chans = 0;
4092 device_printf(devinfo->dev,
4093 "Channels memory allocation failed!\n");
4096 /* Fixup relative pointers after realloc */
4097 for (j = 0; j < devinfo->num_chans; j++)
4098 devinfo->chans[j].caps.fmtlist = devinfo->chans[j].fmtlist;
4100 free = devinfo->num_chans;
4101 devinfo->num_chans += cnt;
4103 for (j = free; j < free + cnt; j++) {
4104 devinfo->chans[j].devinfo = devinfo;
4105 devinfo->chans[j].as = -1;
4108 /* Assign associations in order of their numbers, */
4109 for (j = 0; j < devinfo->ascnt; j++) {
4110 if (as[j].enable == 0)
4112 for (i = 0; i < as[j].num_chans; i++) {
4113 devinfo->chans[free].as = j;
4114 devinfo->chans[free].asindex = i;
4115 devinfo->chans[free].dir =
4116 (as[j].dir == HDAA_CTL_IN) ? PCMDIR_REC : PCMDIR_PLAY;
4117 hdaa_pcmchannel_setup(&devinfo->chans[free]);
4118 as[j].chans[i] = free;
4125 hdaa_audio_disable_nonaudio(struct hdaa_devinfo *devinfo)
4127 struct hdaa_widget *w;
4130 /* Disable power and volume widgets. */
4131 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4132 w = hdaa_widget_get(devinfo, i);
4133 if (w == NULL || w->enable == 0)
4135 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_POWER_WIDGET ||
4136 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_VOLUME_WIDGET) {
4139 device_printf(devinfo->dev,
4140 " Disabling nid %d due to it's"
4141 " non-audio type.\n",
4149 hdaa_audio_disable_useless(struct hdaa_devinfo *devinfo)
4151 struct hdaa_widget *w, *cw;
4152 struct hdaa_audio_ctl *ctl;
4153 int done, found, i, j, k;
4155 /* Disable useless pins. */
4156 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4157 w = hdaa_widget_get(devinfo, i);
4158 if (w == NULL || w->enable == 0)
4160 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) {
4161 if ((w->wclass.pin.config &
4162 HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK) ==
4163 HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_NONE) {
4166 device_printf(devinfo->dev,
4167 " Disabling pin nid %d due"
4168 " to None connectivity.\n",
4171 } else if ((w->wclass.pin.config &
4172 HDA_CONFIG_DEFAULTCONF_ASSOCIATION_MASK) == 0) {
4175 device_printf(devinfo->dev,
4176 " Disabling unassociated"
4185 /* Disable and mute controls for disabled widgets. */
4187 while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4188 if (ctl->enable == 0)
4190 if (ctl->widget->enable == 0 ||
4191 (ctl->childwidget != NULL &&
4192 ctl->childwidget->enable == 0)) {
4194 ctl->muted = HDAA_AMP_MUTE_ALL;
4198 if (ctl->ndir == HDAA_CTL_IN)
4199 ctl->widget->connsenable[ctl->index] = 0;
4202 device_printf(devinfo->dev,
4203 " Disabling ctl %d nid %d cnid %d due"
4204 " to disabled widget.\n", i,
4206 (ctl->childwidget != NULL)?
4207 ctl->childwidget->nid:-1);
4211 /* Disable useless widgets. */
4212 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4213 w = hdaa_widget_get(devinfo, i);
4214 if (w == NULL || w->enable == 0)
4216 /* Disable inputs with disabled child widgets. */
4217 for (j = 0; j < w->nconns; j++) {
4218 if (w->connsenable[j]) {
4219 cw = hdaa_widget_get(devinfo, w->conns[j]);
4220 if (cw == NULL || cw->enable == 0) {
4221 w->connsenable[j] = 0;
4223 device_printf(devinfo->dev,
4224 " Disabling nid %d connection %d due"
4225 " to disabled child widget.\n",
4231 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4232 w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4234 /* Disable mixers and selectors without inputs. */
4236 for (j = 0; j < w->nconns; j++) {
4237 if (w->connsenable[j]) {
4246 device_printf(devinfo->dev,
4247 " Disabling nid %d due to all it's"
4248 " inputs disabled.\n", w->nid);
4251 /* Disable nodes without consumers. */
4252 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_SELECTOR &&
4253 w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4256 for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4257 cw = hdaa_widget_get(devinfo, k);
4258 if (cw == NULL || cw->enable == 0)
4260 for (j = 0; j < cw->nconns; j++) {
4261 if (cw->connsenable[j] && cw->conns[j] == i) {
4271 device_printf(devinfo->dev,
4272 " Disabling nid %d due to all it's"
4273 " consumers disabled.\n", w->nid);
4277 } while (done == 0);
4282 hdaa_audio_disable_unas(struct hdaa_devinfo *devinfo)
4284 struct hdaa_audio_as *as = devinfo->as;
4285 struct hdaa_widget *w, *cw;
4286 struct hdaa_audio_ctl *ctl;
4289 /* Disable unassosiated widgets. */
4290 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4291 w = hdaa_widget_get(devinfo, i);
4292 if (w == NULL || w->enable == 0)
4294 if (w->bindas == -1) {
4297 device_printf(devinfo->dev,
4298 " Disabling unassociated nid %d.\n",
4303 /* Disable input connections on input pin and
4304 * output on output. */
4305 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4306 w = hdaa_widget_get(devinfo, i);
4307 if (w == NULL || w->enable == 0)
4309 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4313 if (as[w->bindas].dir == HDAA_CTL_IN) {
4314 for (j = 0; j < w->nconns; j++) {
4315 if (w->connsenable[j] == 0)
4317 w->connsenable[j] = 0;
4319 device_printf(devinfo->dev,
4320 " Disabling connection to input pin "
4321 "nid %d conn %d.\n",
4325 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4326 HDAA_CTL_IN, -1, 1);
4327 if (ctl && ctl->enable) {
4329 ctl->muted = HDAA_AMP_MUTE_ALL;
4335 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4336 HDAA_CTL_OUT, -1, 1);
4337 if (ctl && ctl->enable) {
4339 ctl->muted = HDAA_AMP_MUTE_ALL;
4344 for (k = devinfo->startnode; k < devinfo->endnode; k++) {
4345 cw = hdaa_widget_get(devinfo, k);
4346 if (cw == NULL || cw->enable == 0)
4348 for (j = 0; j < cw->nconns; j++) {
4349 if (cw->connsenable[j] && cw->conns[j] == i) {
4350 cw->connsenable[j] = 0;
4352 device_printf(devinfo->dev,
4353 " Disabling connection from output pin "
4354 "nid %d conn %d cnid %d.\n",
4357 if (cw->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4360 ctl = hdaa_audio_ctl_amp_get(devinfo, k,
4362 if (ctl && ctl->enable) {
4364 ctl->muted = HDAA_AMP_MUTE_ALL;
4377 hdaa_audio_disable_notselected(struct hdaa_devinfo *devinfo)
4379 struct hdaa_audio_as *as = devinfo->as;
4380 struct hdaa_widget *w;
4383 /* On playback path we can safely disable all unseleted inputs. */
4384 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4385 w = hdaa_widget_get(devinfo, i);
4386 if (w == NULL || w->enable == 0)
4390 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4392 if (w->bindas < 0 || as[w->bindas].dir == HDAA_CTL_IN)
4394 for (j = 0; j < w->nconns; j++) {
4395 if (w->connsenable[j] == 0)
4397 if (w->selconn < 0 || w->selconn == j)
4399 w->connsenable[j] = 0;
4401 device_printf(devinfo->dev,
4402 " Disabling unselected connection "
4403 "nid %d conn %d.\n",
4411 hdaa_audio_disable_crossas(struct hdaa_devinfo *devinfo)
4413 struct hdaa_audio_as *ases = devinfo->as;
4414 struct hdaa_widget *w, *cw;
4415 struct hdaa_audio_ctl *ctl;
4418 /* Disable crossassociatement and unwanted crosschannel connections. */
4419 /* ... using selectors */
4420 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4421 w = hdaa_widget_get(devinfo, i);
4422 if (w == NULL || w->enable == 0)
4426 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4428 /* Allow any -> mix */
4429 if (w->bindas == -2)
4431 for (j = 0; j < w->nconns; j++) {
4432 if (w->connsenable[j] == 0)
4434 cw = hdaa_widget_get(devinfo, w->conns[j]);
4435 if (cw == NULL || w->enable == 0)
4437 /* Allow mix -> out. */
4438 if (cw->bindas == -2 && w->bindas >= 0 &&
4439 ases[w->bindas].dir == HDAA_CTL_OUT)
4441 /* Allow mix -> mixed-in. */
4442 if (cw->bindas == -2 && w->bindas >= 0 &&
4443 ases[w->bindas].mixed)
4445 /* Allow in -> mix. */
4446 if ((w->pflags & HDAA_ADC_MONITOR) &&
4448 ases[cw->bindas].dir == HDAA_CTL_IN)
4450 /* Allow if have common as/seqs. */
4451 if (w->bindas == cw->bindas &&
4452 (w->bindseqmask & cw->bindseqmask) != 0)
4454 w->connsenable[j] = 0;
4456 device_printf(devinfo->dev,
4457 " Disabling crossassociatement connection "
4458 "nid %d conn %d cnid %d.\n",
4463 /* ... using controls */
4465 while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
4466 if (ctl->enable == 0 || ctl->childwidget == NULL)
4468 /* Allow any -> mix */
4469 if (ctl->widget->bindas == -2)
4471 /* Allow mix -> out. */
4472 if (ctl->childwidget->bindas == -2 &&
4473 ctl->widget->bindas >= 0 &&
4474 ases[ctl->widget->bindas].dir == HDAA_CTL_OUT)
4476 /* Allow mix -> mixed-in. */
4477 if (ctl->childwidget->bindas == -2 &&
4478 ctl->widget->bindas >= 0 &&
4479 ases[ctl->widget->bindas].mixed)
4481 /* Allow in -> mix. */
4482 if ((ctl->widget->pflags & HDAA_ADC_MONITOR) &&
4483 ctl->childwidget->bindas >= 0 &&
4484 ases[ctl->childwidget->bindas].dir == HDAA_CTL_IN)
4486 /* Allow if have common as/seqs. */
4487 if (ctl->widget->bindas == ctl->childwidget->bindas &&
4488 (ctl->widget->bindseqmask & ctl->childwidget->bindseqmask) != 0)
4491 ctl->muted = HDAA_AMP_MUTE_ALL;
4495 if (ctl->ndir == HDAA_CTL_IN)
4496 ctl->widget->connsenable[ctl->index] = 0;
4498 device_printf(devinfo->dev,
4499 " Disabling crossassociatement connection "
4500 "ctl %d nid %d cnid %d.\n", i,
4502 ctl->childwidget->nid);
4509 * Find controls to control amplification for source and calculate possible
4510 * amplification range.
4513 hdaa_audio_ctl_source_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4514 int ossdev, int ctlable, int depth, int *minamp, int *maxamp)
4516 struct hdaa_widget *w, *wc;
4517 struct hdaa_audio_ctl *ctl;
4518 int i, j, conns = 0, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4520 if (depth > HDA_PARSE_MAXDEPTH)
4523 w = hdaa_widget_get(devinfo, nid);
4524 if (w == NULL || w->enable == 0)
4527 /* Count number of active inputs. */
4529 for (j = 0; j < w->nconns; j++) {
4530 if (!w->connsenable[j])
4536 /* If this is not a first step - use input mixer.
4537 Pins have common input ctl so care must be taken. */
4538 if (depth > 0 && ctlable && (conns == 1 ||
4539 w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)) {
4540 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_IN,
4543 ctl->ossmask |= (1 << ossdev);
4545 if (*minamp == *maxamp) {
4546 *minamp += MINQDB(ctl);
4547 *maxamp += MAXQDB(ctl);
4552 /* If widget has own ossdev - not traverse it.
4553 It will be traversed on it's own. */
4554 if (w->ossdev >= 0 && depth > 0)
4557 /* We must not traverse pin */
4558 if ((w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT ||
4559 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX) &&
4563 /* record that this widget exports such signal, */
4564 w->ossmask |= (1 << ossdev);
4567 * If signals mixed, we can't assign controls farther.
4568 * Ignore this on depth zero. Caller must knows why.
4571 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
4575 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid, HDAA_CTL_OUT, -1, 1);
4577 ctl->ossmask |= (1 << ossdev);
4579 if (*minamp == *maxamp) {
4580 *minamp += MINQDB(ctl);
4581 *maxamp += MAXQDB(ctl);
4586 cminamp = cmaxamp = 0;
4587 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4588 wc = hdaa_widget_get(devinfo, i);
4589 if (wc == NULL || wc->enable == 0)
4591 for (j = 0; j < wc->nconns; j++) {
4592 if (wc->connsenable[j] && wc->conns[j] == nid) {
4593 tminamp = tmaxamp = 0;
4594 found += hdaa_audio_ctl_source_amp(devinfo,
4595 wc->nid, j, ossdev, ctlable, depth + 1,
4596 &tminamp, &tmaxamp);
4597 if (cminamp == 0 && cmaxamp == 0) {
4600 } else if (tminamp != tmaxamp) {
4601 cminamp = imax(cminamp, tminamp);
4602 cmaxamp = imin(cmaxamp, tmaxamp);
4607 if (*minamp == *maxamp && cminamp < cmaxamp) {
4615 * Find controls to control amplification for destination and calculate
4616 * possible amplification range.
4619 hdaa_audio_ctl_dest_amp(struct hdaa_devinfo *devinfo, nid_t nid, int index,
4620 int ossdev, int depth, int *minamp, int *maxamp)
4622 struct hdaa_audio_as *as = devinfo->as;
4623 struct hdaa_widget *w, *wc;
4624 struct hdaa_audio_ctl *ctl;
4625 int i, j, consumers, tminamp, tmaxamp, cminamp, cmaxamp, found = 0;
4627 if (depth > HDA_PARSE_MAXDEPTH)
4630 w = hdaa_widget_get(devinfo, nid);
4631 if (w == NULL || w->enable == 0)
4635 /* If this node produce output for several consumers,
4636 we can't touch it. */
4638 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4639 wc = hdaa_widget_get(devinfo, i);
4640 if (wc == NULL || wc->enable == 0)
4642 for (j = 0; j < wc->nconns; j++) {
4643 if (wc->connsenable[j] && wc->conns[j] == nid)
4647 /* The only exception is if real HP redirection is configured
4648 and this is a duplication point.
4649 XXX: Actually exception is not completely correct.
4650 XXX: Duplication point check is not perfect. */
4651 if ((consumers == 2 && (w->bindas < 0 ||
4652 as[w->bindas].hpredir < 0 || as[w->bindas].fakeredir ||
4653 (w->bindseqmask & (1 << 15)) == 0)) ||
4657 /* Else use it's output mixer. */
4658 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4659 HDAA_CTL_OUT, -1, 1);
4661 ctl->ossmask |= (1 << ossdev);
4663 if (*minamp == *maxamp) {
4664 *minamp += MINQDB(ctl);
4665 *maxamp += MAXQDB(ctl);
4670 /* We must not traverse pin */
4671 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4675 cminamp = cmaxamp = 0;
4676 for (i = 0; i < w->nconns; i++) {
4677 if (w->connsenable[i] == 0)
4679 if (index >= 0 && i != index)
4681 tminamp = tmaxamp = 0;
4682 ctl = hdaa_audio_ctl_amp_get(devinfo, w->nid,
4685 ctl->ossmask |= (1 << ossdev);
4687 if (*minamp == *maxamp) {
4688 tminamp += MINQDB(ctl);
4689 tmaxamp += MAXQDB(ctl);
4692 found += hdaa_audio_ctl_dest_amp(devinfo, w->conns[i], -1, ossdev,
4693 depth + 1, &tminamp, &tmaxamp);
4694 if (cminamp == 0 && cmaxamp == 0) {
4697 } else if (tminamp != tmaxamp) {
4698 cminamp = imax(cminamp, tminamp);
4699 cmaxamp = imin(cmaxamp, tmaxamp);
4702 if (*minamp == *maxamp && cminamp < cmaxamp) {
4710 * Assign OSS names to sound sources
4713 hdaa_audio_assign_names(struct hdaa_devinfo *devinfo)
4715 struct hdaa_audio_as *as = devinfo->as;
4716 struct hdaa_widget *w;
4718 int type = -1, use, used = 0;
4719 static const int types[7][13] = {
4720 { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4721 SOUND_MIXER_LINE3, -1 }, /* line */
4722 { SOUND_MIXER_MONITOR, SOUND_MIXER_MIC, -1 }, /* int mic */
4723 { SOUND_MIXER_MIC, SOUND_MIXER_MONITOR, -1 }, /* ext mic */
4724 { SOUND_MIXER_CD, -1 }, /* cd */
4725 { SOUND_MIXER_SPEAKER, -1 }, /* speaker */
4726 { SOUND_MIXER_DIGITAL1, SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3,
4728 { SOUND_MIXER_LINE, SOUND_MIXER_LINE1, SOUND_MIXER_LINE2,
4729 SOUND_MIXER_LINE3, SOUND_MIXER_PHONEIN, SOUND_MIXER_PHONEOUT,
4730 SOUND_MIXER_VIDEO, SOUND_MIXER_RADIO, SOUND_MIXER_DIGITAL1,
4731 SOUND_MIXER_DIGITAL2, SOUND_MIXER_DIGITAL3, SOUND_MIXER_MONITOR,
4735 /* Surely known names */
4736 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4737 w = hdaa_widget_get(devinfo, i);
4738 if (w == NULL || w->enable == 0)
4740 if (w->bindas == -1)
4744 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX:
4745 if (as[w->bindas].dir == HDAA_CTL_OUT)
4748 switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4749 case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_IN:
4752 case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4753 if ((w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_MASK)
4754 == HDA_CONFIG_DEFAULTCONF_CONNECTIVITY_JACK)
4758 case HDA_CONFIG_DEFAULTCONF_DEVICE_CD:
4761 case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4764 case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_IN:
4765 case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_IN:
4772 while (types[type][j] >= 0 &&
4773 (used & (1 << types[type][j])) != 0) {
4776 if (types[type][j] >= 0)
4777 use = types[type][j];
4779 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT:
4780 use = SOUND_MIXER_PCM;
4782 case HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET:
4783 use = SOUND_MIXER_SPEAKER;
4793 /* Semi-known names */
4794 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4795 w = hdaa_widget_get(devinfo, i);
4796 if (w == NULL || w->enable == 0)
4800 if (w->bindas == -1)
4802 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4804 if (as[w->bindas].dir == HDAA_CTL_OUT)
4807 switch (w->wclass.pin.config & HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) {
4808 case HDA_CONFIG_DEFAULTCONF_DEVICE_LINE_OUT:
4809 case HDA_CONFIG_DEFAULTCONF_DEVICE_SPEAKER:
4810 case HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT:
4811 case HDA_CONFIG_DEFAULTCONF_DEVICE_AUX:
4814 case HDA_CONFIG_DEFAULTCONF_DEVICE_MIC_IN:
4817 case HDA_CONFIG_DEFAULTCONF_DEVICE_SPDIF_OUT:
4818 case HDA_CONFIG_DEFAULTCONF_DEVICE_DIGITAL_OTHER_OUT:
4825 while (types[type][j] >= 0 &&
4826 (used & (1 << types[type][j])) != 0) {
4829 if (types[type][j] >= 0) {
4830 w->ossdev = types[type][j];
4831 used |= (1 << types[type][j]);
4835 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4836 w = hdaa_widget_get(devinfo, i);
4837 if (w == NULL || w->enable == 0)
4841 if (w->bindas == -1)
4843 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
4845 if (as[w->bindas].dir == HDAA_CTL_OUT)
4848 while (types[6][j] >= 0 &&
4849 (used & (1 << types[6][j])) != 0) {
4852 if (types[6][j] >= 0) {
4853 w->ossdev = types[6][j];
4854 used |= (1 << types[6][j]);
4860 hdaa_audio_build_tree(struct hdaa_devinfo *devinfo)
4862 struct hdaa_audio_as *as = devinfo->as;
4865 /* Trace all associations in order of their numbers. */
4866 for (j = 0; j < devinfo->ascnt; j++) {
4867 if (as[j].enable == 0)
4870 device_printf(devinfo->dev,
4871 "Tracing association %d (%d)\n", j, as[j].index);
4873 if (as[j].dir == HDAA_CTL_OUT) {
4875 res = hdaa_audio_trace_as_out(devinfo, j, 0);
4876 if (res == 0 && as[j].hpredir >= 0 &&
4877 as[j].fakeredir == 0) {
4878 /* If CODEC can't do analog HP redirection
4879 try to make it using one more DAC. */
4880 as[j].fakeredir = 1;
4883 } else if (as[j].mixed)
4884 res = hdaa_audio_trace_as_in(devinfo, j);
4886 res = hdaa_audio_trace_as_in_mch(devinfo, j, 0);
4889 device_printf(devinfo->dev,
4890 "Association %d (%d) trace succeeded\n",
4895 device_printf(devinfo->dev,
4896 "Association %d (%d) trace failed\n",
4903 /* Look for additional DACs/ADCs. */
4904 for (j = 0; j < devinfo->ascnt; j++) {
4905 if (as[j].enable == 0)
4907 hdaa_audio_adddac(devinfo, j);
4910 /* Trace mixer and beeper pseudo associations. */
4911 hdaa_audio_trace_as_extra(devinfo);
4915 * Store in pdevinfo new data about whether and how we can control signal
4916 * for OSS device to/from specified widget.
4919 hdaa_adjust_amp(struct hdaa_widget *w, int ossdev,
4920 int found, int minamp, int maxamp)
4922 struct hdaa_devinfo *devinfo = w->devinfo;
4923 struct hdaa_pcm_devinfo *pdevinfo;
4926 pdevinfo = devinfo->as[w->bindas].pdevinfo;
4928 pdevinfo = &devinfo->devs[0];
4930 pdevinfo->ossmask |= (1 << ossdev);
4931 if (minamp == 0 && maxamp == 0)
4933 if (pdevinfo->minamp[ossdev] == 0 && pdevinfo->maxamp[ossdev] == 0) {
4934 pdevinfo->minamp[ossdev] = minamp;
4935 pdevinfo->maxamp[ossdev] = maxamp;
4937 pdevinfo->minamp[ossdev] = imax(pdevinfo->minamp[ossdev], minamp);
4938 pdevinfo->maxamp[ossdev] = imin(pdevinfo->maxamp[ossdev], maxamp);
4943 * Trace signals from/to all possible sources/destionstions to find possible
4944 * recording sources, OSS device control ranges and to assign controls.
4947 hdaa_audio_assign_mixers(struct hdaa_devinfo *devinfo)
4949 struct hdaa_audio_as *as = devinfo->as;
4950 struct hdaa_widget *w, *cw;
4951 int i, j, minamp, maxamp, found;
4953 /* Assign mixers to the tree. */
4954 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
4955 w = hdaa_widget_get(devinfo, i);
4956 if (w == NULL || w->enable == 0)
4958 minamp = maxamp = 0;
4959 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
4960 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_BEEP_WIDGET ||
4961 (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4962 as[w->bindas].dir == HDAA_CTL_IN)) {
4965 found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4966 w->ossdev, 1, 0, &minamp, &maxamp);
4967 hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4968 } else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
4969 found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4970 SOUND_MIXER_RECLEV, 0, &minamp, &maxamp);
4971 hdaa_adjust_amp(w, SOUND_MIXER_RECLEV, found, minamp, maxamp);
4972 } else if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
4973 as[w->bindas].dir == HDAA_CTL_OUT) {
4974 found = hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4975 SOUND_MIXER_VOLUME, 0, &minamp, &maxamp);
4976 hdaa_adjust_amp(w, SOUND_MIXER_VOLUME, found, minamp, maxamp);
4978 if (w->ossdev == SOUND_MIXER_IMIX) {
4979 minamp = maxamp = 0;
4980 found = hdaa_audio_ctl_source_amp(devinfo, w->nid, -1,
4981 w->ossdev, 1, 0, &minamp, &maxamp);
4982 if (minamp == maxamp) {
4983 /* If we are unable to control input monitor
4984 as source - try to control it as destination. */
4985 found += hdaa_audio_ctl_dest_amp(devinfo, w->nid, -1,
4986 w->ossdev, 0, &minamp, &maxamp);
4987 w->pflags |= HDAA_IMIX_AS_DST;
4989 hdaa_adjust_amp(w, w->ossdev, found, minamp, maxamp);
4991 if (w->pflags & HDAA_ADC_MONITOR) {
4992 for (j = 0; j < w->nconns; j++) {
4993 if (!w->connsenable[j])
4995 cw = hdaa_widget_get(devinfo, w->conns[j]);
4996 if (cw == NULL || cw->enable == 0)
4998 if (cw->bindas == -1)
5000 if (cw->bindas >= 0 &&
5001 as[cw->bindas].dir != HDAA_CTL_IN)
5003 minamp = maxamp = 0;
5004 found = hdaa_audio_ctl_dest_amp(devinfo,
5005 w->nid, j, SOUND_MIXER_IGAIN, 0,
5007 hdaa_adjust_amp(w, SOUND_MIXER_IGAIN,
5008 found, minamp, maxamp);
5015 hdaa_audio_prepare_pin_ctrl(struct hdaa_devinfo *devinfo)
5017 struct hdaa_audio_as *as = devinfo->as;
5018 struct hdaa_widget *w;
5022 for (i = 0; i < devinfo->nodecnt; i++) {
5023 w = &devinfo->widget[i];
5026 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX &&
5030 pincap = w->wclass.pin.cap;
5032 /* Disable everything. */
5033 w->wclass.pin.ctrl &= ~(
5034 HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE |
5035 HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE |
5036 HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE |
5037 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK);
5039 if (w->enable == 0) {
5040 /* Pin is unused so left it disabled. */
5042 } else if (w->waspin) {
5043 /* Enable input for beeper input. */
5044 w->wclass.pin.ctrl |=
5045 HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5046 } else if (w->bindas < 0 || as[w->bindas].enable == 0) {
5047 /* Pin is unused so left it disabled. */
5049 } else if (as[w->bindas].dir == HDAA_CTL_IN) {
5050 /* Input pin, configure for input. */
5051 if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5052 w->wclass.pin.ctrl |=
5053 HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE;
5055 if ((devinfo->quirks & HDAA_QUIRK_IVREF100) &&
5056 HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5057 w->wclass.pin.ctrl |=
5058 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5059 HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5060 else if ((devinfo->quirks & HDAA_QUIRK_IVREF80) &&
5061 HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5062 w->wclass.pin.ctrl |=
5063 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5064 HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5065 else if ((devinfo->quirks & HDAA_QUIRK_IVREF50) &&
5066 HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5067 w->wclass.pin.ctrl |=
5068 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5069 HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5071 /* Output pin, configure for output. */
5072 if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5073 w->wclass.pin.ctrl |=
5074 HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE;
5076 if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap) &&
5077 (w->wclass.pin.config &
5078 HDA_CONFIG_DEFAULTCONF_DEVICE_MASK) ==
5079 HDA_CONFIG_DEFAULTCONF_DEVICE_HP_OUT)
5080 w->wclass.pin.ctrl |=
5081 HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE;
5083 if ((devinfo->quirks & HDAA_QUIRK_OVREF100) &&
5084 HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5085 w->wclass.pin.ctrl |=
5086 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5087 HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_100);
5088 else if ((devinfo->quirks & HDAA_QUIRK_OVREF80) &&
5089 HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5090 w->wclass.pin.ctrl |=
5091 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5092 HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_80);
5093 else if ((devinfo->quirks & HDAA_QUIRK_OVREF50) &&
5094 HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5095 w->wclass.pin.ctrl |=
5096 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE(
5097 HDA_CMD_PIN_WIDGET_CTRL_VREF_ENABLE_50);
5103 hdaa_audio_ctl_commit(struct hdaa_devinfo *devinfo)
5105 struct hdaa_audio_ctl *ctl;
5109 while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5110 if (ctl->enable == 0 || ctl->ossmask != 0) {
5111 /* Mute disabled and mixer controllable controls.
5112 * Last will be initialized by mixer_init().
5113 * This expected to reduce click on startup. */
5114 hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_ALL, 0, 0);
5117 /* Init fixed controls to 0dB amplification. */
5121 hdaa_audio_ctl_amp_set(ctl, HDAA_AMP_MUTE_NONE, z, z);
5126 hdaa_gpio_commit(struct hdaa_devinfo *devinfo)
5128 uint32_t gdata, gmask, gdir;
5131 numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
5132 if (devinfo->gpio != 0 && numgpio != 0) {
5133 gdata = hda_command(devinfo->dev,
5134 HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
5135 gmask = hda_command(devinfo->dev,
5136 HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
5137 gdir = hda_command(devinfo->dev,
5138 HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
5139 for (i = 0; i < numgpio; i++) {
5140 if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5145 } else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5146 HDAA_GPIO_CLEAR(i)) {
5150 } else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5151 HDAA_GPIO_DISABLE(i)) {
5153 } else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5154 HDAA_GPIO_INPUT(i)) {
5160 device_printf(devinfo->dev, "GPIO commit\n");
5162 hda_command(devinfo->dev,
5163 HDA_CMD_SET_GPIO_ENABLE_MASK(0, devinfo->nid, gmask));
5164 hda_command(devinfo->dev,
5165 HDA_CMD_SET_GPIO_DIRECTION(0, devinfo->nid, gdir));
5166 hda_command(devinfo->dev,
5167 HDA_CMD_SET_GPIO_DATA(0, devinfo->nid, gdata));
5169 hdaa_dump_gpio(devinfo);
5175 hdaa_gpo_commit(struct hdaa_devinfo *devinfo)
5180 numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
5181 if (devinfo->gpo != 0 && numgpo != 0) {
5182 gdata = hda_command(devinfo->dev,
5183 HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
5184 for (i = 0; i < numgpo; i++) {
5185 if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5188 } else if ((devinfo->gpio & HDAA_GPIO_MASK(i)) ==
5189 HDAA_GPIO_CLEAR(i)) {
5194 device_printf(devinfo->dev, "GPO commit\n");
5196 hda_command(devinfo->dev,
5197 HDA_CMD_SET_GPO_DATA(0, devinfo->nid, gdata));
5199 hdaa_dump_gpo(devinfo);
5205 hdaa_audio_commit(struct hdaa_devinfo *devinfo)
5207 struct hdaa_widget *w;
5210 /* Commit controls. */
5211 hdaa_audio_ctl_commit(devinfo);
5213 /* Commit selectors, pins and EAPD. */
5214 for (i = 0; i < devinfo->nodecnt; i++) {
5215 w = &devinfo->widget[i];
5218 if (w->selconn == -1)
5221 hdaa_widget_connection_select(w, w->selconn);
5222 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX ||
5224 hda_command(devinfo->dev,
5225 HDA_CMD_SET_PIN_WIDGET_CTRL(0, w->nid,
5226 w->wclass.pin.ctrl));
5228 if (w->param.eapdbtl != HDA_INVALID) {
5231 val = w->param.eapdbtl;
5232 if (devinfo->quirks &
5234 val ^= HDA_CMD_SET_EAPD_BTL_ENABLE_EAPD;
5235 hda_command(devinfo->dev,
5236 HDA_CMD_SET_EAPD_BTL_ENABLE(0, w->nid,
5241 hdaa_gpio_commit(devinfo);
5242 hdaa_gpo_commit(devinfo);
5246 hdaa_powerup(struct hdaa_devinfo *devinfo)
5250 hda_command(devinfo->dev,
5251 HDA_CMD_SET_POWER_STATE(0,
5252 devinfo->nid, HDA_CMD_POWER_STATE_D0));
5255 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5256 hda_command(devinfo->dev,
5257 HDA_CMD_SET_POWER_STATE(0,
5258 i, HDA_CMD_POWER_STATE_D0));
5264 hdaa_pcmchannel_setup(struct hdaa_chan *ch)
5266 struct hdaa_devinfo *devinfo = ch->devinfo;
5267 struct hdaa_audio_as *as = devinfo->as;
5268 struct hdaa_widget *w;
5269 uint32_t cap, fmtcap, pcmcap;
5270 int i, j, ret, channels, onlystereo;
5273 ch->caps = hdaa_caps;
5274 ch->caps.fmtlist = ch->fmtlist;
5277 ch->pcmrates[0] = 48000;
5278 ch->pcmrates[1] = 0;
5279 ch->stripecap = 0xff;
5285 fmtcap = devinfo->supp_stream_formats;
5286 pcmcap = devinfo->supp_pcm_size_rate;
5288 for (i = 0; i < 16; i++) {
5289 /* Check as is correct */
5292 /* Cound only present DACs */
5293 if (as[ch->as].dacs[ch->asindex][i] <= 0)
5295 /* Ignore duplicates */
5296 for (j = 0; j < ret; j++) {
5297 if (ch->io[j] == as[ch->as].dacs[ch->asindex][i])
5303 w = hdaa_widget_get(devinfo, as[ch->as].dacs[ch->asindex][i]);
5304 if (w == NULL || w->enable == 0)
5306 cap = w->param.supp_stream_formats;
5307 if (!HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap) &&
5308 !HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5310 /* Many CODECs does not declare AC3 support on SPDIF.
5311 I don't beleave that they doesn't support it! */
5312 if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5313 cap |= HDA_PARAM_SUPP_STREAM_FORMATS_AC3_MASK;
5316 pcmcap = w->param.supp_pcm_size_rate;
5319 pcmcap &= w->param.supp_pcm_size_rate;
5321 ch->io[ret++] = as[ch->as].dacs[ch->asindex][i];
5322 ch->stripecap &= w->wclass.conv.stripecap;
5323 /* Do not count redirection pin/dac channels. */
5324 if (i == 15 && as[ch->as].hpredir >= 0)
5326 channels += HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) + 1;
5327 if (HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap) != 1)
5332 ch->channels = channels;
5334 if (as[ch->as].fakeredir)
5336 /* Standard speaks only about stereo pins and playback, ... */
5337 if ((!onlystereo) || as[ch->as].mixed)
5339 /* ..., but there it gives us info about speakers layout. */
5340 as[ch->as].pinset = pinset;
5342 ch->supp_stream_formats = fmtcap;
5343 ch->supp_pcm_size_rate = pcmcap;
5354 if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(fmtcap)) {
5355 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(pcmcap))
5357 else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(pcmcap))
5359 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
5361 else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
5363 else if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
5365 if (!(devinfo->quirks & HDAA_QUIRK_FORCESTEREO)) {
5366 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 1, 0);
5368 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 1, 0);
5370 if (channels >= 2) {
5371 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 2, 0);
5373 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 2, 0);
5375 if (channels >= 3 && !onlystereo) {
5376 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 0);
5378 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 0);
5379 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 3, 1);
5381 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 3, 1);
5383 if (channels >= 4) {
5384 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 0);
5386 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 0);
5388 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 4, 1);
5390 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 4, 1);
5393 if (channels >= 5 && !onlystereo) {
5394 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 0);
5396 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 0);
5397 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 5, 1);
5399 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 5, 1);
5401 if (channels >= 6) {
5402 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 1);
5404 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 1);
5406 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 6, 0);
5408 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 6, 0);
5411 if (channels >= 7 && !onlystereo) {
5412 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 0);
5414 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 0);
5415 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 7, 1);
5417 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 7, 1);
5419 if (channels >= 8) {
5420 ch->fmtlist[i++] = SND_FORMAT(AFMT_S16_LE, 8, 1);
5422 ch->fmtlist[i++] = SND_FORMAT(AFMT_S32_LE, 8, 1);
5425 if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(fmtcap)) {
5426 ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 2, 0);
5427 if (channels >= 8) {
5428 ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 0);
5429 ch->fmtlist[i++] = SND_FORMAT(AFMT_AC3, 8, 1);
5434 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(pcmcap))
5435 ch->pcmrates[i++] = 8000;
5436 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(pcmcap))
5437 ch->pcmrates[i++] = 11025;
5438 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(pcmcap))
5439 ch->pcmrates[i++] = 16000;
5440 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(pcmcap))
5441 ch->pcmrates[i++] = 22050;
5442 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(pcmcap))
5443 ch->pcmrates[i++] = 32000;
5444 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(pcmcap))
5445 ch->pcmrates[i++] = 44100;
5446 /* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_48KHZ(pcmcap)) */
5447 ch->pcmrates[i++] = 48000;
5448 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(pcmcap))
5449 ch->pcmrates[i++] = 88200;
5450 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(pcmcap))
5451 ch->pcmrates[i++] = 96000;
5452 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(pcmcap))
5453 ch->pcmrates[i++] = 176400;
5454 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(pcmcap))
5455 ch->pcmrates[i++] = 192000;
5456 /* if (HDA_PARAM_SUPP_PCM_SIZE_RATE_384KHZ(pcmcap)) */
5457 ch->pcmrates[i] = 0;
5459 ch->caps.minspeed = ch->pcmrates[0];
5460 ch->caps.maxspeed = ch->pcmrates[i - 1];
5468 hdaa_prepare_pcms(struct hdaa_devinfo *devinfo)
5470 struct hdaa_audio_as *as = devinfo->as;
5471 int i, j, k, apdev = 0, ardev = 0, dpdev = 0, drdev = 0;
5473 for (i = 0; i < devinfo->ascnt; i++) {
5474 if (as[i].enable == 0)
5476 if (as[i].dir == HDAA_CTL_IN) {
5489 max(ardev, apdev) + max(drdev, dpdev);
5491 (struct hdaa_pcm_devinfo *)malloc(
5492 devinfo->num_devs * sizeof(struct hdaa_pcm_devinfo),
5493 M_HDAA, M_ZERO | M_NOWAIT);
5494 if (devinfo->devs == NULL) {
5495 device_printf(devinfo->dev,
5496 "Unable to allocate memory for devices\n");
5499 for (i = 0; i < devinfo->num_devs; i++) {
5500 devinfo->devs[i].index = i;
5501 devinfo->devs[i].devinfo = devinfo;
5502 devinfo->devs[i].playas = -1;
5503 devinfo->devs[i].recas = -1;
5504 devinfo->devs[i].digital = 255;
5506 for (i = 0; i < devinfo->ascnt; i++) {
5507 if (as[i].enable == 0)
5509 for (j = 0; j < devinfo->num_devs; j++) {
5510 if (devinfo->devs[j].digital != 255 &&
5511 (!devinfo->devs[j].digital) !=
5514 if (as[i].dir == HDAA_CTL_IN) {
5515 if (devinfo->devs[j].recas >= 0)
5517 devinfo->devs[j].recas = i;
5519 if (devinfo->devs[j].playas >= 0)
5521 devinfo->devs[j].playas = i;
5523 as[i].pdevinfo = &devinfo->devs[j];
5524 for (k = 0; k < as[i].num_chans; k++) {
5525 devinfo->chans[as[i].chans[k]].pdevinfo =
5528 devinfo->devs[j].digital = as[i].digital;
5535 hdaa_create_pcms(struct hdaa_devinfo *devinfo)
5539 for (i = 0; i < devinfo->num_devs; i++) {
5540 struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
5542 pdevinfo->dev = device_add_child(devinfo->dev, "pcm", -1);
5543 device_set_ivars(pdevinfo->dev, (void *)pdevinfo);
5548 hdaa_dump_ctls(struct hdaa_pcm_devinfo *pdevinfo, const char *banner, uint32_t flag)
5550 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5551 struct hdaa_audio_ctl *ctl;
5553 int i, j, printed = 0;
5556 flag = ~(SOUND_MASK_VOLUME | SOUND_MASK_PCM |
5557 SOUND_MASK_CD | SOUND_MASK_LINE | SOUND_MASK_RECLEV |
5558 SOUND_MASK_MIC | SOUND_MASK_SPEAKER | SOUND_MASK_IGAIN |
5559 SOUND_MASK_OGAIN | SOUND_MASK_IMIX | SOUND_MASK_MONITOR);
5562 for (j = 0; j < SOUND_MIXER_NRDEVICES; j++) {
5563 if ((flag & (1 << j)) == 0)
5567 while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
5568 if (ctl->enable == 0 ||
5569 ctl->widget->enable == 0)
5571 if (!((pdevinfo->playas >= 0 &&
5572 ctl->widget->bindas == pdevinfo->playas) ||
5573 (pdevinfo->recas >= 0 &&
5574 ctl->widget->bindas == pdevinfo->recas) ||
5575 (ctl->widget->bindas == -2 && pdevinfo->index == 0)))
5577 if ((ctl->ossmask & (1 << j)) == 0)
5581 if (banner != NULL) {
5582 device_printf(pdevinfo->dev, "%s", banner);
5584 device_printf(pdevinfo->dev, "Unknown Ctl");
5586 printf(" (OSS: %s)",
5587 hdaa_audio_ctl_ossmixer_mask2allname(1 << j,
5589 if (pdevinfo->ossmask & (1 << j)) {
5590 printf(": %+d/%+ddB\n",
5591 pdevinfo->minamp[j] / 4,
5592 pdevinfo->maxamp[j] / 4);
5597 device_printf(pdevinfo->dev, " +- ctl %2d (nid %3d %s", i,
5599 (ctl->ndir == HDAA_CTL_IN)?"in ":"out");
5600 if (ctl->ndir == HDAA_CTL_IN && ctl->ndir == ctl->dir)
5601 printf(" %2d): ", ctl->index);
5604 if (ctl->step > 0) {
5605 printf("%+d/%+ddB (%d steps)%s\n",
5609 ctl->mute?" + mute":"");
5611 printf("%s\n", ctl->mute?"mute":"");
5615 device_printf(pdevinfo->dev, "\n");
5619 hdaa_dump_audio_formats(device_t dev, uint32_t fcap, uint32_t pcmcap)
5625 device_printf(dev, " Stream cap: 0x%08x", cap);
5626 if (HDA_PARAM_SUPP_STREAM_FORMATS_AC3(cap))
5628 if (HDA_PARAM_SUPP_STREAM_FORMATS_FLOAT32(cap))
5630 if (HDA_PARAM_SUPP_STREAM_FORMATS_PCM(cap))
5636 device_printf(dev, " PCM cap: 0x%08x", cap);
5637 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8BIT(cap))
5639 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16BIT(cap))
5641 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(cap))
5643 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(cap))
5645 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(cap))
5648 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_8KHZ(cap))
5650 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_11KHZ(cap))
5652 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_16KHZ(cap))
5654 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_22KHZ(cap))
5656 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_32KHZ(cap))
5658 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_44KHZ(cap))
5661 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_88KHZ(cap))
5663 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_96KHZ(cap))
5665 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_176KHZ(cap))
5667 if (HDA_PARAM_SUPP_PCM_SIZE_RATE_192KHZ(cap))
5674 hdaa_dump_pin(struct hdaa_widget *w)
5678 pincap = w->wclass.pin.cap;
5680 device_printf(w->devinfo->dev, " Pin cap: 0x%08x", pincap);
5681 if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap))
5683 if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap))
5685 if (HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap))
5687 if (HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap))
5689 if (HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap))
5691 if (HDA_PARAM_PIN_CAP_INPUT_CAP(pincap))
5693 if (HDA_PARAM_PIN_CAP_BALANCED_IO_PINS(pincap))
5695 if (HDA_PARAM_PIN_CAP_HDMI(pincap))
5697 if (HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)) {
5699 if (HDA_PARAM_PIN_CAP_VREF_CTRL_50(pincap))
5701 if (HDA_PARAM_PIN_CAP_VREF_CTRL_80(pincap))
5703 if (HDA_PARAM_PIN_CAP_VREF_CTRL_100(pincap))
5705 if (HDA_PARAM_PIN_CAP_VREF_CTRL_GROUND(pincap))
5707 if (HDA_PARAM_PIN_CAP_VREF_CTRL_HIZ(pincap))
5711 if (HDA_PARAM_PIN_CAP_EAPD_CAP(pincap))
5713 if (HDA_PARAM_PIN_CAP_DP(pincap))
5715 if (HDA_PARAM_PIN_CAP_HBR(pincap))
5718 device_printf(w->devinfo->dev, " Pin config: 0x%08x\n",
5719 w->wclass.pin.config);
5720 device_printf(w->devinfo->dev, " Pin control: 0x%08x", w->wclass.pin.ctrl);
5721 if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_HPHN_ENABLE)
5723 if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_IN_ENABLE)
5725 if (w->wclass.pin.ctrl & HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE)
5727 if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap)) {
5728 if ((w->wclass.pin.ctrl &
5729 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) == 0x03)
5731 else if ((w->wclass.pin.ctrl &
5732 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5735 if ((w->wclass.pin.ctrl &
5736 HDA_CMD_SET_PIN_WIDGET_CTRL_VREF_ENABLE_MASK) != 0)
5743 hdaa_dump_pin_config(struct hdaa_widget *w, uint32_t conf)
5746 device_printf(w->devinfo->dev, "%2d %08x %-2d %-2d "
5747 "%-13s %-5s %-7s %-10s %-7s %d%s\n",
5749 HDA_CONFIG_DEFAULTCONF_ASSOCIATION(conf),
5750 HDA_CONFIG_DEFAULTCONF_SEQUENCE(conf),
5751 HDA_DEVS[HDA_CONFIG_DEFAULTCONF_DEVICE(conf)],
5752 HDA_CONNS[HDA_CONFIG_DEFAULTCONF_CONNECTIVITY(conf)],
5753 HDA_CONNECTORS[HDA_CONFIG_DEFAULTCONF_CONNECTION_TYPE(conf)],
5754 HDA_LOCS[HDA_CONFIG_DEFAULTCONF_LOCATION(conf)],
5755 HDA_COLORS[HDA_CONFIG_DEFAULTCONF_COLOR(conf)],
5756 HDA_CONFIG_DEFAULTCONF_MISC(conf),
5757 (w->enable == 0)?" DISA":"");
5761 hdaa_dump_pin_configs(struct hdaa_devinfo *devinfo)
5763 struct hdaa_widget *w;
5766 device_printf(devinfo->dev, "nid 0x as seq "
5767 "device conn jack loc color misc\n");
5768 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5769 w = hdaa_widget_get(devinfo, i);
5772 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
5774 hdaa_dump_pin_config(w, w->wclass.pin.config);
5779 hdaa_dump_amp(device_t dev, uint32_t cap, const char *banner)
5781 int offset, size, step;
5783 offset = HDA_PARAM_OUTPUT_AMP_CAP_OFFSET(cap);
5784 size = HDA_PARAM_OUTPUT_AMP_CAP_STEPSIZE(cap);
5785 step = HDA_PARAM_OUTPUT_AMP_CAP_NUMSTEPS(cap);
5786 device_printf(dev, " %s amp: 0x%08x "
5787 "mute=%d step=%d size=%d offset=%d (%+d/%+ddB)\n",
5789 HDA_PARAM_OUTPUT_AMP_CAP_MUTE_CAP(cap),
5791 ((0 - offset) * (size + 1)) / 4,
5792 ((step - offset) * (size + 1)) / 4);
5796 hdaa_dump_nodes(struct hdaa_devinfo *devinfo)
5798 struct hdaa_widget *w, *cw;
5802 device_printf(devinfo->dev, "\n");
5803 device_printf(devinfo->dev, "Default parameters:\n");
5804 hdaa_dump_audio_formats(devinfo->dev,
5805 devinfo->supp_stream_formats,
5806 devinfo->supp_pcm_size_rate);
5807 hdaa_dump_amp(devinfo->dev, devinfo->inamp_cap, " Input");
5808 hdaa_dump_amp(devinfo->dev, devinfo->outamp_cap, "Output");
5809 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
5810 w = hdaa_widget_get(devinfo, i);
5812 device_printf(devinfo->dev, "Ghost widget nid=%d\n", i);
5815 device_printf(devinfo->dev, "\n");
5816 device_printf(devinfo->dev, " nid: %d%s\n", w->nid,
5817 (w->enable == 0) ? " [DISABLED]" : "");
5818 device_printf(devinfo->dev, " Name: %s\n", w->name);
5819 device_printf(devinfo->dev, " Widget cap: 0x%08x",
5820 w->param.widget_cap);
5821 if (w->param.widget_cap & 0x0ee1) {
5822 if (HDA_PARAM_AUDIO_WIDGET_CAP_LR_SWAP(w->param.widget_cap))
5824 if (HDA_PARAM_AUDIO_WIDGET_CAP_POWER_CTRL(w->param.widget_cap))
5826 if (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap))
5828 if (HDA_PARAM_AUDIO_WIDGET_CAP_UNSOL_CAP(w->param.widget_cap))
5830 if (HDA_PARAM_AUDIO_WIDGET_CAP_PROC_WIDGET(w->param.widget_cap))
5832 if (HDA_PARAM_AUDIO_WIDGET_CAP_STRIPE(w->param.widget_cap))
5833 printf(" STRIPE(x%d)",
5834 1 << (fls(w->wclass.conv.stripecap) - 1));
5835 j = HDA_PARAM_AUDIO_WIDGET_CAP_CC(w->param.widget_cap);
5839 printf(" %dCH", j + 1);
5842 if (w->bindas != -1) {
5843 device_printf(devinfo->dev, " Association: %d (0x%04x)\n",
5844 w->bindas, w->bindseqmask);
5846 if (w->ossmask != 0 || w->ossdev >= 0) {
5847 device_printf(devinfo->dev, " OSS: %s",
5848 hdaa_audio_ctl_ossmixer_mask2allname(w->ossmask, buf, sizeof(buf)));
5850 printf(" (%s)", ossnames[w->ossdev]);
5853 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_OUTPUT ||
5854 w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT) {
5855 hdaa_dump_audio_formats(devinfo->dev,
5856 w->param.supp_stream_formats,
5857 w->param.supp_pcm_size_rate);
5858 } else if (w->type ==
5859 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX || w->waspin)
5861 if (w->param.eapdbtl != HDA_INVALID)
5862 device_printf(devinfo->dev, " EAPD: 0x%08x\n",
5864 if (HDA_PARAM_AUDIO_WIDGET_CAP_OUT_AMP(w->param.widget_cap) &&
5865 w->param.outamp_cap != 0)
5866 hdaa_dump_amp(devinfo->dev, w->param.outamp_cap, "Output");
5867 if (HDA_PARAM_AUDIO_WIDGET_CAP_IN_AMP(w->param.widget_cap) &&
5868 w->param.inamp_cap != 0)
5869 hdaa_dump_amp(devinfo->dev, w->param.inamp_cap, " Input");
5871 device_printf(devinfo->dev, " Connections: %d\n", w->nconns);
5872 for (j = 0; j < w->nconns; j++) {
5873 cw = hdaa_widget_get(devinfo, w->conns[j]);
5874 device_printf(devinfo->dev, " + %s<- nid=%d [%s]",
5875 (w->connsenable[j] == 0)?"[DISABLED] ":"",
5876 w->conns[j], (cw == NULL) ? "GHOST!" : cw->name);
5878 printf(" [UNKNOWN]");
5879 else if (cw->enable == 0)
5880 printf(" [DISABLED]");
5881 if (w->nconns > 1 && w->selconn == j && w->type !=
5882 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_MIXER)
5883 printf(" (selected)");
5891 hdaa_dump_dst_nid(struct hdaa_pcm_devinfo *pdevinfo, nid_t nid, int depth)
5893 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5894 struct hdaa_widget *w, *cw;
5898 if (depth > HDA_PARSE_MAXDEPTH)
5901 w = hdaa_widget_get(devinfo, nid);
5902 if (w == NULL || w->enable == 0)
5906 device_printf(pdevinfo->dev, "%*s", 4, "");
5908 device_printf(pdevinfo->dev, "%*s + <- ", 4 + (depth - 1) * 7, "");
5909 printf("nid=%d [%s]", w->nid, w->name);
5912 if (w->ossmask == 0) {
5916 printf(" [src: %s]",
5917 hdaa_audio_ctl_ossmixer_mask2allname(
5918 w->ossmask, buf, sizeof(buf)));
5919 if (w->ossdev >= 0) {
5926 for (i = 0; i < w->nconns; i++) {
5927 if (w->connsenable[i] == 0)
5929 cw = hdaa_widget_get(devinfo, w->conns[i]);
5930 if (cw == NULL || cw->enable == 0 || cw->bindas == -1)
5932 hdaa_dump_dst_nid(pdevinfo, w->conns[i], depth + 1);
5938 hdaa_dump_dac(struct hdaa_pcm_devinfo *pdevinfo)
5940 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5941 struct hdaa_audio_as *as;
5942 struct hdaa_widget *w;
5946 if (pdevinfo->playas < 0)
5949 device_printf(pdevinfo->dev, "Playback:\n");
5951 chid = devinfo->as[pdevinfo->playas].chans[0];
5952 hdaa_dump_audio_formats(pdevinfo->dev,
5953 devinfo->chans[chid].supp_stream_formats,
5954 devinfo->chans[chid].supp_pcm_size_rate);
5955 for (i = 0; i < devinfo->as[pdevinfo->playas].num_chans; i++) {
5956 chid = devinfo->as[pdevinfo->playas].chans[i];
5957 device_printf(pdevinfo->dev, " DAC:");
5958 for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5959 printf(" %d", *nids);
5963 as = &devinfo->as[pdevinfo->playas];
5964 for (i = 0; i < 16; i++) {
5965 if (as->pins[i] <= 0)
5967 w = hdaa_widget_get(devinfo, as->pins[i]);
5968 if (w == NULL || w->enable == 0)
5970 device_printf(pdevinfo->dev, "\n");
5971 hdaa_dump_dst_nid(pdevinfo, as->pins[i], 0);
5973 device_printf(pdevinfo->dev, "\n");
5977 hdaa_dump_adc(struct hdaa_pcm_devinfo *pdevinfo)
5979 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
5980 struct hdaa_widget *w;
5984 if (pdevinfo->recas < 0)
5987 device_printf(pdevinfo->dev, "Record:\n");
5989 chid = devinfo->as[pdevinfo->recas].chans[0];
5990 hdaa_dump_audio_formats(pdevinfo->dev,
5991 devinfo->chans[chid].supp_stream_formats,
5992 devinfo->chans[chid].supp_pcm_size_rate);
5993 for (i = 0; i < devinfo->as[pdevinfo->recas].num_chans; i++) {
5994 chid = devinfo->as[pdevinfo->recas].chans[i];
5995 device_printf(pdevinfo->dev, " ADC:");
5996 for (nids = devinfo->chans[chid].io; *nids != -1; nids++)
5997 printf(" %d", *nids);
6001 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6002 w = hdaa_widget_get(devinfo, i);
6003 if (w == NULL || w->enable == 0)
6005 if (w->type != HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_AUDIO_INPUT)
6007 if (w->bindas != pdevinfo->recas)
6009 device_printf(pdevinfo->dev, "\n");
6010 hdaa_dump_dst_nid(pdevinfo, i, 0);
6012 device_printf(pdevinfo->dev, "\n");
6016 hdaa_dump_mix(struct hdaa_pcm_devinfo *pdevinfo)
6018 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6019 struct hdaa_widget *w;
6023 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6024 w = hdaa_widget_get(devinfo, i);
6025 if (w == NULL || w->enable == 0)
6027 if (w->ossdev != SOUND_MIXER_IMIX)
6029 if (w->bindas != pdevinfo->recas)
6033 device_printf(pdevinfo->dev, "Input Mix:\n");
6035 device_printf(pdevinfo->dev, "\n");
6036 hdaa_dump_dst_nid(pdevinfo, i, 0);
6039 device_printf(pdevinfo->dev, "\n");
6043 hdaa_pindump(device_t dev)
6045 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6046 struct hdaa_widget *w;
6047 uint32_t res, pincap, delay;
6050 device_printf(dev, "Dumping AFG pins:\n");
6051 device_printf(dev, "nid 0x as seq "
6052 "device conn jack loc color misc\n");
6053 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6054 w = hdaa_widget_get(devinfo, i);
6055 if (w == NULL || w->type !=
6056 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6058 hdaa_dump_pin_config(w, w->wclass.pin.config);
6059 pincap = w->wclass.pin.cap;
6060 device_printf(dev, " Caps: %2s %3s %2s %4s %4s",
6061 HDA_PARAM_PIN_CAP_INPUT_CAP(pincap)?"IN":"",
6062 HDA_PARAM_PIN_CAP_OUTPUT_CAP(pincap)?"OUT":"",
6063 HDA_PARAM_PIN_CAP_HEADPHONE_CAP(pincap)?"HP":"",
6064 HDA_PARAM_PIN_CAP_EAPD_CAP(pincap)?"EAPD":"",
6065 HDA_PARAM_PIN_CAP_VREF_CTRL(pincap)?"VREF":"");
6066 if (HDA_PARAM_PIN_CAP_IMP_SENSE_CAP(pincap) ||
6067 HDA_PARAM_PIN_CAP_PRESENCE_DETECT_CAP(pincap)) {
6068 if (HDA_PARAM_PIN_CAP_TRIGGER_REQD(pincap)) {
6071 HDA_CMD_SET_PIN_SENSE(0, w->nid, 0));
6073 res = hda_command(dev,
6074 HDA_CMD_GET_PIN_SENSE(0, w->nid));
6075 if (res != 0x7fffffff && res != 0xffffffff)
6078 } while (++delay < 10000);
6081 res = hda_command(dev, HDA_CMD_GET_PIN_SENSE(0,
6084 printf(" Sense: 0x%08x (%sconnected%s)", res,
6085 (res & HDA_CMD_GET_PIN_SENSE_PRESENCE_DETECT) ?
6087 (HDA_PARAM_AUDIO_WIDGET_CAP_DIGITAL(w->param.widget_cap) &&
6088 (res & HDA_CMD_GET_PIN_SENSE_ELD_VALID)) ?
6089 ", ELD valid" : "");
6091 printf(" delay %dus", delay * 10);
6096 "NumGPIO=%d NumGPO=%d NumGPI=%d GPIWake=%d GPIUnsol=%d\n",
6097 HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap),
6098 HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap),
6099 HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap),
6100 HDA_PARAM_GPIO_COUNT_GPI_WAKE(devinfo->gpio_cap),
6101 HDA_PARAM_GPIO_COUNT_GPI_UNSOL(devinfo->gpio_cap));
6102 hdaa_dump_gpi(devinfo);
6103 hdaa_dump_gpio(devinfo);
6104 hdaa_dump_gpo(devinfo);
6108 hdaa_configure(device_t dev)
6110 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6111 struct hdaa_audio_ctl *ctl;
6115 device_printf(dev, "Applying built-in patches...\n");
6117 hdaa_patch(devinfo);
6119 device_printf(dev, "Applying local patches...\n");
6121 hdaa_local_patch(devinfo);
6122 hdaa_audio_postprocess(devinfo);
6124 device_printf(dev, "Parsing Ctls...\n");
6126 hdaa_audio_ctl_parse(devinfo);
6128 device_printf(dev, "Disabling nonaudio...\n");
6130 hdaa_audio_disable_nonaudio(devinfo);
6132 device_printf(dev, "Disabling useless...\n");
6134 hdaa_audio_disable_useless(devinfo);
6136 device_printf(dev, "Patched pins configuration:\n");
6137 hdaa_dump_pin_configs(devinfo);
6140 device_printf(dev, "Parsing pin associations...\n");
6142 hdaa_audio_as_parse(devinfo);
6144 device_printf(dev, "Building AFG tree...\n");
6146 hdaa_audio_build_tree(devinfo);
6148 device_printf(dev, "Disabling unassociated "
6151 hdaa_audio_disable_unas(devinfo);
6153 device_printf(dev, "Disabling nonselected "
6156 hdaa_audio_disable_notselected(devinfo);
6158 device_printf(dev, "Disabling useless...\n");
6160 hdaa_audio_disable_useless(devinfo);
6162 device_printf(dev, "Disabling "
6163 "crossassociatement connections...\n");
6165 hdaa_audio_disable_crossas(devinfo);
6167 device_printf(dev, "Disabling useless...\n");
6169 hdaa_audio_disable_useless(devinfo);
6171 device_printf(dev, "Binding associations to channels...\n");
6173 hdaa_audio_bind_as(devinfo);
6175 device_printf(dev, "Assigning names to signal sources...\n");
6177 hdaa_audio_assign_names(devinfo);
6179 device_printf(dev, "Preparing PCM devices...\n");
6181 hdaa_prepare_pcms(devinfo);
6183 device_printf(dev, "Assigning mixers to the tree...\n");
6185 hdaa_audio_assign_mixers(devinfo);
6187 device_printf(dev, "Preparing pin controls...\n");
6189 hdaa_audio_prepare_pin_ctrl(devinfo);
6191 device_printf(dev, "AFG commit...\n");
6193 hdaa_audio_commit(devinfo);
6195 device_printf(dev, "Applying direct built-in patches...\n");
6197 hdaa_patch_direct(devinfo);
6199 device_printf(dev, "Pin sense init...\n");
6201 hdaa_sense_init(devinfo);
6203 device_printf(dev, "Creating PCM devices...\n");
6205 hdaa_create_pcms(devinfo);
6208 if (devinfo->quirks != 0) {
6209 device_printf(dev, "FG config/quirks:");
6210 for (i = 0; i < nitems(hdaa_quirks_tab); i++) {
6211 if ((devinfo->quirks &
6212 hdaa_quirks_tab[i].value) ==
6213 hdaa_quirks_tab[i].value)
6214 printf(" %s", hdaa_quirks_tab[i].key);
6221 device_printf(dev, "\n");
6222 device_printf(dev, "+-----------+\n");
6223 device_printf(dev, "| HDA NODES |\n");
6224 device_printf(dev, "+-----------+\n");
6225 hdaa_dump_nodes(devinfo);
6227 device_printf(dev, "\n");
6228 device_printf(dev, "+----------------+\n");
6229 device_printf(dev, "| HDA AMPLIFIERS |\n");
6230 device_printf(dev, "+----------------+\n");
6231 device_printf(dev, "\n");
6233 while ((ctl = hdaa_audio_ctl_each(devinfo, &i)) != NULL) {
6234 device_printf(dev, "%3d: nid %3d %s (%s) index %d", i,
6235 (ctl->widget != NULL) ? ctl->widget->nid : -1,
6236 (ctl->ndir == HDAA_CTL_IN)?"in ":"out",
6237 (ctl->dir == HDAA_CTL_IN)?"in ":"out",
6239 if (ctl->childwidget != NULL)
6240 printf(" cnid %3d", ctl->childwidget->nid);
6243 printf(" ossmask=0x%08x\n",
6246 " mute: %d step: %3d size: %3d off: %3d%s\n",
6247 ctl->mute, ctl->step, ctl->size, ctl->offset,
6248 (ctl->enable == 0) ? " [DISABLED]" :
6249 ((ctl->ossmask == 0) ? " [UNUSED]" : ""));
6251 device_printf(dev, "\n");
6256 hdaa_unconfigure(device_t dev)
6258 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6259 struct hdaa_widget *w;
6263 device_printf(dev, "Pin sense deinit...\n");
6265 hdaa_sense_deinit(devinfo);
6266 free(devinfo->ctl, M_HDAA);
6267 devinfo->ctl = NULL;
6268 devinfo->ctlcnt = 0;
6269 free(devinfo->as, M_HDAA);
6272 free(devinfo->devs, M_HDAA);
6273 devinfo->devs = NULL;
6274 devinfo->num_devs = 0;
6275 free(devinfo->chans, M_HDAA);
6276 devinfo->chans = NULL;
6277 devinfo->num_chans = 0;
6278 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6279 w = hdaa_widget_get(devinfo, i);
6289 for (j = 0; j < w->nconns; j++)
6290 w->connsenable[j] = 1;
6291 if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6292 w->wclass.pin.config = w->wclass.pin.newconf;
6293 if (w->eld != NULL) {
6295 free(w->eld, M_HDAA);
6302 hdaa_sysctl_gpi_state(SYSCTL_HANDLER_ARGS)
6304 struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6305 device_t dev = devinfo->dev;
6307 int n = 0, i, numgpi;
6312 numgpi = HDA_PARAM_GPIO_COUNT_NUM_GPI(devinfo->gpio_cap);
6314 data = hda_command(dev,
6315 HDA_CMD_GET_GPI_DATA(0, devinfo->nid));
6317 hdaa_unlock(devinfo);
6318 for (i = 0; i < numgpi; i++) {
6319 n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6320 n != 0 ? " " : "", i, ((data >> i) & 1));
6322 return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6326 hdaa_sysctl_gpio_state(SYSCTL_HANDLER_ARGS)
6328 struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6329 device_t dev = devinfo->dev;
6331 int n = 0, i, numgpio;
6332 uint32_t data = 0, enable = 0, dir = 0;
6336 numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6338 data = hda_command(dev,
6339 HDA_CMD_GET_GPIO_DATA(0, devinfo->nid));
6340 enable = hda_command(dev,
6341 HDA_CMD_GET_GPIO_ENABLE_MASK(0, devinfo->nid));
6342 dir = hda_command(dev,
6343 HDA_CMD_GET_GPIO_DIRECTION(0, devinfo->nid));
6345 hdaa_unlock(devinfo);
6346 for (i = 0; i < numgpio; i++) {
6347 n += snprintf(buf + n, sizeof(buf) - n, "%s%d=",
6348 n != 0 ? " " : "", i);
6349 if ((enable & (1 << i)) == 0) {
6350 n += snprintf(buf + n, sizeof(buf) - n, "disabled");
6353 n += snprintf(buf + n, sizeof(buf) - n, "%sput(%d)",
6354 ((dir >> i) & 1) ? "out" : "in", ((data >> i) & 1));
6356 return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6360 hdaa_sysctl_gpio_config(SYSCTL_HANDLER_ARGS)
6362 struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6364 int error, n = 0, i, numgpio;
6367 gpio = devinfo->newgpio;
6368 numgpio = HDA_PARAM_GPIO_COUNT_NUM_GPIO(devinfo->gpio_cap);
6370 for (i = 0; i < numgpio; i++) {
6371 x = (gpio & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6372 n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6373 n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6375 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6376 if (error != 0 || req->newptr == NULL)
6378 if (strncmp(buf, "0x", 2) == 0)
6379 gpio = strtol(buf + 2, NULL, 16);
6381 gpio = hdaa_gpio_patch(gpio, buf);
6383 devinfo->newgpio = devinfo->gpio = gpio;
6384 hdaa_gpio_commit(devinfo);
6385 hdaa_unlock(devinfo);
6390 hdaa_sysctl_gpo_state(SYSCTL_HANDLER_ARGS)
6392 struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6393 device_t dev = devinfo->dev;
6395 int n = 0, i, numgpo;
6400 numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6402 data = hda_command(dev,
6403 HDA_CMD_GET_GPO_DATA(0, devinfo->nid));
6405 hdaa_unlock(devinfo);
6406 for (i = 0; i < numgpo; i++) {
6407 n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%d",
6408 n != 0 ? " " : "", i, ((data >> i) & 1));
6410 return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
6414 hdaa_sysctl_gpo_config(SYSCTL_HANDLER_ARGS)
6416 struct hdaa_devinfo *devinfo = oidp->oid_arg1;
6418 int error, n = 0, i, numgpo;
6421 gpo = devinfo->newgpo;
6422 numgpo = HDA_PARAM_GPIO_COUNT_NUM_GPO(devinfo->gpio_cap);
6424 for (i = 0; i < numgpo; i++) {
6425 x = (gpo & HDAA_GPIO_MASK(i)) >> HDAA_GPIO_SHIFT(i);
6426 n += snprintf(buf + n, sizeof(buf) - n, "%s%d=%s",
6427 n != 0 ? " " : "", i, HDA_GPIO_ACTIONS[x]);
6429 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6430 if (error != 0 || req->newptr == NULL)
6432 if (strncmp(buf, "0x", 2) == 0)
6433 gpo = strtol(buf + 2, NULL, 16);
6435 gpo = hdaa_gpio_patch(gpo, buf);
6437 devinfo->newgpo = devinfo->gpo = gpo;
6438 hdaa_gpo_commit(devinfo);
6439 hdaa_unlock(devinfo);
6444 hdaa_sysctl_reconfig(SYSCTL_HANDLER_ARGS)
6447 struct hdaa_devinfo *devinfo;
6450 dev = oidp->oid_arg1;
6451 devinfo = device_get_softc(dev);
6452 if (devinfo == NULL)
6455 error = sysctl_handle_int(oidp, &val, 0, req);
6456 if (error != 0 || req->newptr == NULL || val == 0)
6460 device_printf(dev, "Reconfiguration...\n");
6462 if ((error = device_delete_children(dev)) != 0)
6465 hdaa_unconfigure(dev);
6466 hdaa_configure(dev);
6467 hdaa_unlock(devinfo);
6468 bus_generic_attach(dev);
6470 device_printf(dev, "Reconfiguration done\n");
6476 hdaa_suspend(device_t dev)
6478 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6482 device_printf(dev, "Suspend...\n");
6486 device_printf(dev, "Stop streams...\n");
6488 for (i = 0; i < devinfo->num_chans; i++) {
6489 if (devinfo->chans[i].flags & HDAA_CHN_RUNNING) {
6490 devinfo->chans[i].flags |= HDAA_CHN_SUSPEND;
6491 hdaa_channel_stop(&devinfo->chans[i]);
6495 device_printf(dev, "Power down FG"
6496 " nid=%d to the D3 state...\n",
6499 hda_command(devinfo->dev,
6500 HDA_CMD_SET_POWER_STATE(0,
6501 devinfo->nid, HDA_CMD_POWER_STATE_D3));
6502 callout_stop(&devinfo->poll_jack);
6503 hdaa_unlock(devinfo);
6504 callout_drain(&devinfo->poll_jack);
6506 device_printf(dev, "Suspend done\n");
6512 hdaa_resume(device_t dev)
6514 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6518 device_printf(dev, "Resume...\n");
6522 device_printf(dev, "Power up audio FG nid=%d...\n",
6525 hdaa_powerup(devinfo);
6527 device_printf(dev, "AFG commit...\n");
6529 hdaa_audio_commit(devinfo);
6531 device_printf(dev, "Applying direct built-in patches...\n");
6533 hdaa_patch_direct(devinfo);
6535 device_printf(dev, "Pin sense init...\n");
6537 hdaa_sense_init(devinfo);
6539 hdaa_unlock(devinfo);
6540 for (i = 0; i < devinfo->num_devs; i++) {
6541 struct hdaa_pcm_devinfo *pdevinfo = &devinfo->devs[i];
6543 device_printf(pdevinfo->dev,
6544 "OSS mixer reinitialization...\n");
6546 if (mixer_reinit(pdevinfo->dev) == -1)
6547 device_printf(pdevinfo->dev,
6548 "unable to reinitialize the mixer\n");
6552 device_printf(dev, "Start streams...\n");
6554 for (i = 0; i < devinfo->num_chans; i++) {
6555 if (devinfo->chans[i].flags & HDAA_CHN_SUSPEND) {
6556 devinfo->chans[i].flags &= ~HDAA_CHN_SUSPEND;
6557 hdaa_channel_start(&devinfo->chans[i]);
6560 hdaa_unlock(devinfo);
6562 device_printf(dev, "Resume done\n");
6568 hdaa_probe(device_t dev)
6573 if (hda_get_node_type(dev) != HDA_PARAM_FCT_GRP_TYPE_NODE_TYPE_AUDIO)
6575 pdesc = device_get_desc(device_get_parent(dev));
6576 snprintf(buf, sizeof(buf), "%.*s Audio Function Group",
6577 (int)(strlen(pdesc) - 10), pdesc);
6578 device_set_desc_copy(dev, buf);
6579 return (BUS_PROBE_DEFAULT);
6583 hdaa_attach(device_t dev)
6585 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6587 nid_t nid = hda_get_node_id(dev);
6590 devinfo->lock = HDAC_GET_MTX(device_get_parent(dev), dev);
6592 devinfo->newquirks = -1;
6593 devinfo->newgpio = -1;
6594 devinfo->newgpo = -1;
6595 callout_init(&devinfo->poll_jack, CALLOUT_MPSAFE);
6596 devinfo->poll_ival = hz;
6599 res = hda_command(dev,
6600 HDA_CMD_GET_PARAMETER(0 , nid, HDA_PARAM_SUB_NODE_COUNT));
6601 hdaa_unlock(devinfo);
6603 devinfo->nodecnt = HDA_PARAM_SUB_NODE_COUNT_TOTAL(res);
6604 devinfo->startnode = HDA_PARAM_SUB_NODE_COUNT_START(res);
6605 devinfo->endnode = devinfo->startnode + devinfo->nodecnt;
6608 device_printf(dev, "Subsystem ID: 0x%08x\n",
6609 hda_get_subsystem_id(dev));
6613 "Audio Function Group at nid=%d: %d subnodes %d-%d\n",
6614 nid, devinfo->nodecnt,
6615 devinfo->startnode, devinfo->endnode - 1);
6618 if (devinfo->nodecnt > 0)
6619 devinfo->widget = (struct hdaa_widget *)malloc(
6620 sizeof(*(devinfo->widget)) * devinfo->nodecnt, M_HDAA,
6623 devinfo->widget = NULL;
6627 device_printf(dev, "Powering up...\n");
6629 hdaa_powerup(devinfo);
6631 device_printf(dev, "Parsing audio FG...\n");
6633 hdaa_audio_parse(devinfo);
6635 device_printf(dev, "Original pins configuration:\n");
6636 hdaa_dump_pin_configs(devinfo);
6638 hdaa_configure(dev);
6639 hdaa_unlock(devinfo);
6641 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6642 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6643 "config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6644 &devinfo->newquirks, sizeof(&devinfo->newquirks),
6645 hdaa_sysctl_quirks, "A", "Configuration options");
6646 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6647 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6648 "gpi_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6649 devinfo, sizeof(devinfo),
6650 hdaa_sysctl_gpi_state, "A", "GPI state");
6651 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6652 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6653 "gpio_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6654 devinfo, sizeof(devinfo),
6655 hdaa_sysctl_gpio_state, "A", "GPIO state");
6656 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6657 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6658 "gpio_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6659 devinfo, sizeof(devinfo),
6660 hdaa_sysctl_gpio_config, "A", "GPIO configuration");
6661 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6662 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6663 "gpo_state", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6664 devinfo, sizeof(devinfo),
6665 hdaa_sysctl_gpo_state, "A", "GPO state");
6666 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6667 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6668 "gpo_config", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
6669 devinfo, sizeof(devinfo),
6670 hdaa_sysctl_gpo_config, "A", "GPO configuration");
6671 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
6672 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
6673 "reconfig", CTLTYPE_INT | CTLFLAG_RW,
6675 hdaa_sysctl_reconfig, "I", "Reprocess configuration");
6676 bus_generic_attach(dev);
6681 hdaa_detach(device_t dev)
6683 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6686 if ((error = device_delete_children(dev)) != 0)
6690 hdaa_unconfigure(dev);
6691 devinfo->poll_ival = 0;
6692 callout_stop(&devinfo->poll_jack);
6693 hdaa_unlock(devinfo);
6694 callout_drain(&devinfo->poll_jack);
6696 free(devinfo->widget, M_HDAA);
6701 hdaa_print_child(device_t dev, device_t child)
6703 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6704 struct hdaa_pcm_devinfo *pdevinfo =
6705 (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6706 struct hdaa_audio_as *as;
6707 int retval, first = 1, i;
6709 retval = bus_print_child_header(dev, child);
6710 retval += printf(" at nid ");
6711 if (pdevinfo->playas >= 0) {
6712 as = &devinfo->as[pdevinfo->playas];
6713 for (i = 0; i < 16; i++) {
6714 if (as->pins[i] <= 0)
6716 retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6720 if (pdevinfo->recas >= 0) {
6721 if (pdevinfo->playas >= 0) {
6722 retval += printf(" and ");
6725 as = &devinfo->as[pdevinfo->recas];
6726 for (i = 0; i < 16; i++) {
6727 if (as->pins[i] <= 0)
6729 retval += printf("%s%d", first ? "" : ",", as->pins[i]);
6733 retval += bus_print_child_footer(dev, child);
6739 hdaa_child_location_str(device_t dev, device_t child, char *buf,
6742 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6743 struct hdaa_pcm_devinfo *pdevinfo =
6744 (struct hdaa_pcm_devinfo *)device_get_ivars(child);
6745 struct hdaa_audio_as *as;
6746 int first = 1, i, len = 0;
6748 len += snprintf(buf + len, buflen - len, "nid=");
6749 if (pdevinfo->playas >= 0) {
6750 as = &devinfo->as[pdevinfo->playas];
6751 for (i = 0; i < 16; i++) {
6752 if (as->pins[i] <= 0)
6754 len += snprintf(buf + len, buflen - len,
6755 "%s%d", first ? "" : ",", as->pins[i]);
6759 if (pdevinfo->recas >= 0) {
6760 as = &devinfo->as[pdevinfo->recas];
6761 for (i = 0; i < 16; i++) {
6762 if (as->pins[i] <= 0)
6764 len += snprintf(buf + len, buflen - len,
6765 "%s%d", first ? "" : ",", as->pins[i]);
6773 hdaa_stream_intr(device_t dev, int dir, int stream)
6775 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6776 struct hdaa_chan *ch;
6779 for (i = 0; i < devinfo->num_chans; i++) {
6780 ch = &devinfo->chans[i];
6781 if (!(ch->flags & HDAA_CHN_RUNNING))
6783 if (ch->dir == ((dir == 1) ? PCMDIR_PLAY : PCMDIR_REC) &&
6784 ch->sid == stream) {
6785 hdaa_unlock(devinfo);
6793 hdaa_unsol_intr(device_t dev, uint32_t resp)
6795 struct hdaa_devinfo *devinfo = device_get_softc(dev);
6796 struct hdaa_widget *w;
6800 device_printf(dev, "Unsolicited response %08x\n", resp);
6803 for (i = devinfo->startnode; i < devinfo->endnode; i++) {
6804 w = hdaa_widget_get(devinfo, i);
6805 if (w == NULL || w->enable == 0 || w->type !=
6806 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6808 if (w->unsol != tag)
6810 if (HDA_PARAM_PIN_CAP_DP(w->wclass.pin.cap) ||
6811 HDA_PARAM_PIN_CAP_HDMI(w->wclass.pin.cap))
6812 flags = resp & 0x03;
6816 hdaa_presence_handler(w);
6818 hdaa_eld_handler(w);
6822 static device_method_t hdaa_methods[] = {
6823 /* device interface */
6824 DEVMETHOD(device_probe, hdaa_probe),
6825 DEVMETHOD(device_attach, hdaa_attach),
6826 DEVMETHOD(device_detach, hdaa_detach),
6827 DEVMETHOD(device_suspend, hdaa_suspend),
6828 DEVMETHOD(device_resume, hdaa_resume),
6830 DEVMETHOD(bus_print_child, hdaa_print_child),
6831 DEVMETHOD(bus_child_location_str, hdaa_child_location_str),
6832 DEVMETHOD(hdac_stream_intr, hdaa_stream_intr),
6833 DEVMETHOD(hdac_unsol_intr, hdaa_unsol_intr),
6834 DEVMETHOD(hdac_pindump, hdaa_pindump),
6838 static driver_t hdaa_driver = {
6841 sizeof(struct hdaa_devinfo),
6844 static devclass_t hdaa_devclass;
6846 DRIVER_MODULE(snd_hda, hdacc, hdaa_driver, hdaa_devclass, NULL, NULL);
6849 hdaa_chan_formula(struct hdaa_devinfo *devinfo, int asid,
6850 char *buf, int buflen)
6852 struct hdaa_audio_as *as;
6855 as = &devinfo->as[asid];
6856 c = devinfo->chans[as->chans[0]].channels;
6858 snprintf(buf, buflen, "mono");
6860 if (as->hpredir < 0)
6863 snprintf(buf, buflen, "2.0");
6864 } else if (as->pinset == 0x0003)
6865 snprintf(buf, buflen, "3.1");
6866 else if (as->pinset == 0x0005 || as->pinset == 0x0011)
6867 snprintf(buf, buflen, "4.0");
6868 else if (as->pinset == 0x0007 || as->pinset == 0x0013)
6869 snprintf(buf, buflen, "5.1");
6870 else if (as->pinset == 0x0017)
6871 snprintf(buf, buflen, "7.1");
6873 snprintf(buf, buflen, "%dch", c);
6874 if (as->hpredir >= 0)
6875 strlcat(buf, "+HP", buflen);
6879 hdaa_chan_type(struct hdaa_devinfo *devinfo, int asid)
6881 struct hdaa_audio_as *as;
6882 struct hdaa_widget *w;
6885 as = &devinfo->as[asid];
6886 for (i = 0; i < 16; i++) {
6887 w = hdaa_widget_get(devinfo, as->pins[i]);
6888 if (w == NULL || w->enable == 0 || w->type !=
6889 HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
6891 t1 = HDA_CONFIG_DEFAULTCONF_DEVICE(w->wclass.pin.config);
6903 hdaa_sysctl_32bit(SYSCTL_HANDLER_ARGS)
6905 struct hdaa_audio_as *as = (struct hdaa_audio_as *)oidp->oid_arg1;
6906 struct hdaa_pcm_devinfo *pdevinfo = as->pdevinfo;
6907 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6908 struct hdaa_chan *ch;
6912 ch = &devinfo->chans[as->chans[0]];
6913 val = (ch->bit32 == 4) ? 32 : ((ch->bit32 == 3) ? 24 :
6914 ((ch->bit32 == 2) ? 20 : 0));
6915 error = sysctl_handle_int(oidp, &val, 0, req);
6916 if (error != 0 || req->newptr == NULL)
6918 pcmcap = ch->supp_pcm_size_rate;
6919 if (val == 32 && HDA_PARAM_SUPP_PCM_SIZE_RATE_32BIT(pcmcap))
6921 else if (val == 24 && HDA_PARAM_SUPP_PCM_SIZE_RATE_24BIT(pcmcap))
6923 else if (val == 20 && HDA_PARAM_SUPP_PCM_SIZE_RATE_20BIT(pcmcap))
6927 for (i = 1; i < as->num_chans; i++)
6928 devinfo->chans[as->chans[i]].bit32 = ch->bit32;
6933 hdaa_pcm_probe(device_t dev)
6935 struct hdaa_pcm_devinfo *pdevinfo =
6936 (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
6937 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
6939 char chans1[8], chans2[8];
6941 int loc1, loc2, t1, t2;
6943 if (pdevinfo->playas >= 0)
6944 loc1 = devinfo->as[pdevinfo->playas].location;
6946 loc1 = devinfo->as[pdevinfo->recas].location;
6947 if (pdevinfo->recas >= 0)
6948 loc2 = devinfo->as[pdevinfo->recas].location;
6953 if (loc1 >= 0 && HDA_LOCS[loc1][0] == '0')
6958 if (pdevinfo->playas >= 0) {
6959 hdaa_chan_formula(devinfo, pdevinfo->playas,
6960 chans1, sizeof(chans1));
6961 t1 = hdaa_chan_type(devinfo, pdevinfo->playas);
6963 if (pdevinfo->recas >= 0) {
6964 hdaa_chan_formula(devinfo, pdevinfo->recas,
6965 chans2, sizeof(chans2));
6966 t2 = hdaa_chan_type(devinfo, pdevinfo->recas);
6968 if (chans1[0] != 0 || chans2[0] != 0) {
6969 if (chans1[0] == 0 && pdevinfo->playas >= 0)
6970 snprintf(chans1, sizeof(chans1), "2.0");
6971 else if (chans2[0] == 0 && pdevinfo->recas >= 0)
6972 snprintf(chans2, sizeof(chans2), "2.0");
6973 if (strcmp(chans1, chans2) == 0)
6982 if (pdevinfo->digital)
6984 pdesc = device_get_desc(device_get_parent(dev));
6985 snprintf(buf, sizeof(buf), "%.*s (%s%s%s%s%s%s%s%s%s)",
6986 (int)(strlen(pdesc) - 21), pdesc,
6987 loc1 >= 0 ? HDA_LOCS[loc1] : "", loc1 >= 0 ? " " : "",
6988 (pdevinfo->digital == 0x7)?"HDMI/DP":
6989 ((pdevinfo->digital == 0x5)?"DisplayPort":
6990 ((pdevinfo->digital == 0x3)?"HDMI":
6991 ((pdevinfo->digital)?"Digital":"Analog"))),
6992 chans1[0] ? " " : "", chans1,
6993 chans2[0] ? "/" : "", chans2,
6994 t1 >= 0 ? " " : "", t1 >= 0 ? HDA_DEVS[t1] : "");
6995 device_set_desc_copy(dev, buf);
6996 return (BUS_PROBE_SPECIFIC);
7000 hdaa_pcm_attach(device_t dev)
7002 struct hdaa_pcm_devinfo *pdevinfo =
7003 (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
7004 struct hdaa_devinfo *devinfo = pdevinfo->devinfo;
7005 struct hdaa_audio_as *as;
7006 struct snddev_info *d;
7007 char status[SND_STATUSLEN];
7010 pdevinfo->chan_size = pcm_getbuffersize(dev,
7011 HDA_BUFSZ_MIN, HDA_BUFSZ_DEFAULT, HDA_BUFSZ_MAX);
7014 hdaa_dump_dac(pdevinfo);
7015 hdaa_dump_adc(pdevinfo);
7016 hdaa_dump_mix(pdevinfo);
7017 hdaa_dump_ctls(pdevinfo, "Master Volume", SOUND_MASK_VOLUME);
7018 hdaa_dump_ctls(pdevinfo, "PCM Volume", SOUND_MASK_PCM);
7019 hdaa_dump_ctls(pdevinfo, "CD Volume", SOUND_MASK_CD);
7020 hdaa_dump_ctls(pdevinfo, "Microphone Volume", SOUND_MASK_MIC);
7021 hdaa_dump_ctls(pdevinfo, "Microphone2 Volume", SOUND_MASK_MONITOR);
7022 hdaa_dump_ctls(pdevinfo, "Line-in Volume", SOUND_MASK_LINE);
7023 hdaa_dump_ctls(pdevinfo, "Speaker/Beep Volume", SOUND_MASK_SPEAKER);
7024 hdaa_dump_ctls(pdevinfo, "Recording Level", SOUND_MASK_RECLEV);
7025 hdaa_dump_ctls(pdevinfo, "Input Mix Level", SOUND_MASK_IMIX);
7026 hdaa_dump_ctls(pdevinfo, "Input Monitoring Level", SOUND_MASK_IGAIN);
7027 hdaa_dump_ctls(pdevinfo, NULL, 0);
7030 if (resource_int_value(device_get_name(dev),
7031 device_get_unit(dev), "blocksize", &i) == 0 && i > 0) {
7033 if (i < HDA_BLK_MIN)
7035 pdevinfo->chan_blkcnt = pdevinfo->chan_size / i;
7037 while (pdevinfo->chan_blkcnt >> i)
7039 pdevinfo->chan_blkcnt = 1 << (i - 1);
7040 if (pdevinfo->chan_blkcnt < HDA_BDL_MIN)
7041 pdevinfo->chan_blkcnt = HDA_BDL_MIN;
7042 else if (pdevinfo->chan_blkcnt > HDA_BDL_MAX)
7043 pdevinfo->chan_blkcnt = HDA_BDL_MAX;
7045 pdevinfo->chan_blkcnt = HDA_BDL_DEFAULT;
7048 * We don't register interrupt handler with snd_setup_intr
7049 * in pcm device. Mark pcm device as MPSAFE manually.
7051 pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE);
7054 device_printf(dev, "OSS mixer initialization...\n");
7056 if (mixer_init(dev, &hdaa_audio_ctl_ossmixer_class, pdevinfo) != 0)
7057 device_printf(dev, "Can't register mixer\n");
7060 device_printf(dev, "Registering PCM channels...\n");
7062 if (pcm_register(dev, pdevinfo, (pdevinfo->playas >= 0)?1:0,
7063 (pdevinfo->recas >= 0)?1:0) != 0)
7064 device_printf(dev, "Can't register PCM\n");
7066 pdevinfo->registered++;
7068 d = device_get_softc(dev);
7069 if (pdevinfo->playas >= 0) {
7070 as = &devinfo->as[pdevinfo->playas];
7071 for (i = 0; i < as->num_chans; i++)
7072 pcm_addchan(dev, PCMDIR_PLAY, &hdaa_channel_class,
7073 &devinfo->chans[as->chans[i]]);
7074 SYSCTL_ADD_PROC(&d->play_sysctl_ctx,
7075 SYSCTL_CHILDREN(d->play_sysctl_tree), OID_AUTO,
7076 "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7077 as, sizeof(as), hdaa_sysctl_32bit, "I",
7078 "Resolution of 32bit samples (20/24/32bit)");
7080 if (pdevinfo->recas >= 0) {
7081 as = &devinfo->as[pdevinfo->recas];
7082 for (i = 0; i < as->num_chans; i++)
7083 pcm_addchan(dev, PCMDIR_REC, &hdaa_channel_class,
7084 &devinfo->chans[as->chans[i]]);
7085 SYSCTL_ADD_PROC(&d->rec_sysctl_ctx,
7086 SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7087 "32bit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7088 as, sizeof(as), hdaa_sysctl_32bit, "I",
7089 "Resolution of 32bit samples (20/24/32bit)");
7090 pdevinfo->autorecsrc = 2;
7091 resource_int_value(device_get_name(dev), device_get_unit(dev),
7092 "rec.autosrc", &pdevinfo->autorecsrc);
7093 SYSCTL_ADD_INT(&d->rec_sysctl_ctx,
7094 SYSCTL_CHILDREN(d->rec_sysctl_tree), OID_AUTO,
7095 "autosrc", CTLFLAG_RW,
7096 &pdevinfo->autorecsrc, 0,
7097 "Automatic recording source selection");
7100 if (pdevinfo->mixer != NULL) {
7101 hdaa_audio_ctl_set_defaults(pdevinfo);
7103 if (pdevinfo->playas >= 0) {
7104 as = &devinfo->as[pdevinfo->playas];
7105 hdaa_channels_handler(as);
7107 if (pdevinfo->recas >= 0) {
7108 as = &devinfo->as[pdevinfo->recas];
7109 hdaa_autorecsrc_handler(as, NULL);
7110 hdaa_channels_handler(as);
7112 hdaa_unlock(devinfo);
7115 snprintf(status, SND_STATUSLEN, "on %s %s",
7116 device_get_nameunit(device_get_parent(dev)),
7117 PCM_KLDSTRING(snd_hda));
7118 pcm_setstatus(dev, status);
7124 hdaa_pcm_detach(device_t dev)
7126 struct hdaa_pcm_devinfo *pdevinfo =
7127 (struct hdaa_pcm_devinfo *)device_get_ivars(dev);
7130 if (pdevinfo->registered > 0) {
7131 err = pcm_unregister(dev);
7139 static device_method_t hdaa_pcm_methods[] = {
7140 /* device interface */
7141 DEVMETHOD(device_probe, hdaa_pcm_probe),
7142 DEVMETHOD(device_attach, hdaa_pcm_attach),
7143 DEVMETHOD(device_detach, hdaa_pcm_detach),
7147 static driver_t hdaa_pcm_driver = {
7153 DRIVER_MODULE(snd_hda_pcm, hdaa, hdaa_pcm_driver, pcm_devclass, NULL, NULL);
7154 MODULE_DEPEND(snd_hda, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
7155 MODULE_VERSION(snd_hda, 1);