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