Home | History | Annotate | Line # | Download | only in hdaudio
hdafg.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: hdafg.c,v 1.1 2015/03/28 14:09:59 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*
      4  1.1  jmcneill  * Copyright (c) 2009 Precedence Technologies Ltd <support (at) precedence.co.uk>
      5  1.1  jmcneill  * Copyright (c) 2009-2011 Jared D. McNeill <jmcneill (at) invisible.ca>
      6  1.1  jmcneill  * All rights reserved.
      7  1.1  jmcneill  *
      8  1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      9  1.1  jmcneill  * by Precedence Technologies Ltd
     10  1.1  jmcneill  *
     11  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     12  1.1  jmcneill  * modification, are permitted provided that the following conditions
     13  1.1  jmcneill  * are met:
     14  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     15  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     16  1.1  jmcneill  * 2. The name of the author may not be used to endorse or promote products
     17  1.1  jmcneill  *    derived from this software without specific prior written permission.
     18  1.1  jmcneill  *
     19  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  jmcneill  * SUCH DAMAGE.
     30  1.1  jmcneill  */
     31  1.1  jmcneill 
     32  1.1  jmcneill /*
     33  1.1  jmcneill  * Widget parsing from FreeBSD hdac.c:
     34  1.1  jmcneill  *
     35  1.1  jmcneill  * Copyright (c) 2006 Stephane E. Potvin <sepotvin (at) videotron.ca>
     36  1.1  jmcneill  * Copyright (c) 2006 Ariff Abdullah <ariff (at) FreeBSD.org>
     37  1.1  jmcneill  * Copyright (c) 2008 Alexander Motin <mav (at) FreeBSD.org>
     38  1.1  jmcneill  * All rights reserved.
     39  1.1  jmcneill  *
     40  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     41  1.1  jmcneill  * modification, are permitted provided that the following conditions
     42  1.1  jmcneill  * are met:
     43  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     44  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     45  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     46  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     47  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     48  1.1  jmcneill  *
     49  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     50  1.1  jmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  1.1  jmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  1.1  jmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     53  1.1  jmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  1.1  jmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  1.1  jmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  1.1  jmcneill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  1.1  jmcneill  * SUCH DAMAGE.
     60  1.1  jmcneill  */
     61  1.1  jmcneill 
     62  1.1  jmcneill #include <sys/cdefs.h>
     63  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: hdafg.c,v 1.1 2015/03/28 14:09:59 jmcneill Exp $");
     64  1.1  jmcneill 
     65  1.1  jmcneill #include <sys/types.h>
     66  1.1  jmcneill #include <sys/param.h>
     67  1.1  jmcneill #include <sys/systm.h>
     68  1.1  jmcneill #include <sys/kernel.h>
     69  1.1  jmcneill #include <sys/device.h>
     70  1.1  jmcneill #include <sys/conf.h>
     71  1.1  jmcneill #include <sys/bus.h>
     72  1.1  jmcneill #include <sys/kmem.h>
     73  1.1  jmcneill #include <sys/module.h>
     74  1.1  jmcneill 
     75  1.1  jmcneill #include <sys/audioio.h>
     76  1.1  jmcneill #include <dev/audio_if.h>
     77  1.1  jmcneill #include <dev/auconv.h>
     78  1.1  jmcneill 
     79  1.1  jmcneill #include "hdaudio_config.h"
     80  1.1  jmcneill 
     81  1.1  jmcneill #include "hdaudiovar.h"
     82  1.1  jmcneill #include "hdaudioreg.h"
     83  1.1  jmcneill #include "hdaudio_mixer.h"
     84  1.1  jmcneill #include "hdaudioio.h"
     85  1.1  jmcneill #include "hdaudio_verbose.h"
     86  1.1  jmcneill #include "hdaudiodevs.h"
     87  1.1  jmcneill #include "hdafg_dd.h"
     88  1.1  jmcneill #include "hdmireg.h"
     89  1.1  jmcneill 
     90  1.1  jmcneill #ifndef AUFMT_SURROUND_7_1
     91  1.1  jmcneill #define	AUFMT_SURROUND_7_1 (AUFMT_DOLBY_5_1|AUFMT_SIDE_LEFT|AUFMT_SIDE_RIGHT)
     92  1.1  jmcneill #endif
     93  1.1  jmcneill 
     94  1.1  jmcneill #if defined(HDAFG_DEBUG)
     95  1.1  jmcneill static int hdafg_debug = HDAFG_DEBUG;
     96  1.1  jmcneill #else
     97  1.1  jmcneill static int hdafg_debug = 0;
     98  1.1  jmcneill #endif
     99  1.1  jmcneill 
    100  1.1  jmcneill #define	hda_debug(sc, ...)		\
    101  1.1  jmcneill 	if (hdafg_debug) hda_print(sc, __VA_ARGS__)
    102  1.1  jmcneill #define	hda_debug1(sc, ...)		\
    103  1.1  jmcneill 	if (hdafg_debug) hda_print1(sc, __VA_ARGS__)
    104  1.1  jmcneill 
    105  1.1  jmcneill #define HDAUDIO_MIXER_CLASS_OUTPUTS	0
    106  1.1  jmcneill #define	HDAUDIO_MIXER_CLASS_INPUTS	1
    107  1.1  jmcneill #define	HDAUDIO_MIXER_CLASS_RECORD	2
    108  1.1  jmcneill #define	HDAUDIO_MIXER_CLASS_LAST	HDAUDIO_MIXER_CLASS_RECORD
    109  1.1  jmcneill 
    110  1.1  jmcneill #define	HDAUDIO_GPIO_MASK	0
    111  1.1  jmcneill #define	HDAUDIO_GPIO_DIR	1
    112  1.1  jmcneill #define	HDAUDIO_GPIO_DATA	2
    113  1.1  jmcneill 
    114  1.1  jmcneill #define	HDAUDIO_UNSOLTAG_EVENT_HP	0x01
    115  1.1  jmcneill #define	HDAUDIO_UNSOLTAG_EVENT_DD	0x02
    116  1.1  jmcneill 
    117  1.1  jmcneill #define	HDAUDIO_HP_SENSE_PERIOD		hz
    118  1.1  jmcneill 
    119  1.1  jmcneill const u_int hdafg_possible_rates[] = {
    120  1.1  jmcneill 	8000, 11025, 16000, 22050, 32000, 44100,
    121  1.1  jmcneill 	48000, 88200, 96000, 176500, 192000, /* 384000, */
    122  1.1  jmcneill };
    123  1.1  jmcneill 
    124  1.1  jmcneill static const char *hdafg_mixer_names[] = HDAUDIO_DEVICE_NAMES;
    125  1.1  jmcneill 
    126  1.1  jmcneill static const char *hdafg_port_connectivity[] = {
    127  1.1  jmcneill 	"Jack",
    128  1.1  jmcneill 	"Unconnected",
    129  1.1  jmcneill 	"Built-In",
    130  1.1  jmcneill 	"Jack & Built-In"
    131  1.1  jmcneill };
    132  1.1  jmcneill static const char *hdafg_default_device[] = {
    133  1.1  jmcneill 	"Line Out",
    134  1.1  jmcneill 	"Speaker",
    135  1.1  jmcneill 	"HP Out",
    136  1.1  jmcneill 	"CD",
    137  1.1  jmcneill 	"SPDIF Out",
    138  1.1  jmcneill 	"Digital Out",
    139  1.1  jmcneill 	"Modem Line Side",
    140  1.1  jmcneill 	"Modem Handset Side",
    141  1.1  jmcneill 	"Line In",
    142  1.1  jmcneill 	"AUX",
    143  1.1  jmcneill 	"Mic In",
    144  1.1  jmcneill 	"Telephony",
    145  1.1  jmcneill 	"SPDIF In",
    146  1.1  jmcneill 	"Digital In",
    147  1.1  jmcneill 	"Reserved",
    148  1.1  jmcneill 	"Other"
    149  1.1  jmcneill };
    150  1.1  jmcneill static const char *hdafg_color[] = {
    151  1.1  jmcneill 	"Unknown",
    152  1.1  jmcneill 	"Black",
    153  1.1  jmcneill 	"Grey",
    154  1.1  jmcneill 	"Blue",
    155  1.1  jmcneill 	"Green",
    156  1.1  jmcneill 	"Red",
    157  1.1  jmcneill 	"Orange",
    158  1.1  jmcneill 	"Yellow",
    159  1.1  jmcneill 	"Purple",
    160  1.1  jmcneill 	"Pink",
    161  1.1  jmcneill 	"ReservedA",
    162  1.1  jmcneill 	"ReservedB",
    163  1.1  jmcneill 	"ReservedC",
    164  1.1  jmcneill 	"ReservedD",
    165  1.1  jmcneill 	"White",
    166  1.1  jmcneill 	"Other"
    167  1.1  jmcneill };
    168  1.1  jmcneill 
    169  1.1  jmcneill #define	HDAUDIO_MAXFORMATS	24
    170  1.1  jmcneill #define	HDAUDIO_MAXCONNECTIONS	32
    171  1.1  jmcneill #define	HDAUDIO_MAXPINS		16
    172  1.1  jmcneill #define	HDAUDIO_PARSE_MAXDEPTH	10
    173  1.1  jmcneill 
    174  1.1  jmcneill #define	HDAUDIO_AMP_VOL_DEFAULT	(-1)
    175  1.1  jmcneill #define	HDAUDIO_AMP_MUTE_DEFAULT (0xffffffff)
    176  1.1  jmcneill #define	HDAUDIO_AMP_MUTE_NONE	0
    177  1.1  jmcneill #define	HDAUDIO_AMP_MUTE_LEFT	(1 << 0)
    178  1.1  jmcneill #define	HDAUDIO_AMP_MUTE_RIGHT	(1 << 1)
    179  1.1  jmcneill #define	HDAUDIO_AMP_MUTE_ALL	(HDAUDIO_AMP_MUTE_LEFT | HDAUDIO_AMP_MUTE_RIGHT)
    180  1.1  jmcneill #define	HDAUDIO_AMP_LEFT_MUTED(x)	((x) & HDAUDIO_AMP_MUTE_LEFT)
    181  1.1  jmcneill #define	HDAUDIO_AMP_RIGHT_MUTED(x)	(((x) & HDAUDIO_AMP_MUTE_RIGHT) >> 1)
    182  1.1  jmcneill 
    183  1.1  jmcneill #define	HDAUDIO_ADC_MONITOR	1
    184  1.1  jmcneill 
    185  1.1  jmcneill enum hdaudio_pindir {
    186  1.1  jmcneill 	HDAUDIO_PINDIR_NONE = 0,
    187  1.1  jmcneill 	HDAUDIO_PINDIR_OUT = 1,
    188  1.1  jmcneill 	HDAUDIO_PINDIR_IN = 2,
    189  1.1  jmcneill 	HDAUDIO_PINDIR_INOUT = 3,
    190  1.1  jmcneill };
    191  1.1  jmcneill 
    192  1.1  jmcneill #define	hda_get_param(sc, cop)					\
    193  1.1  jmcneill 	hdaudio_command((sc)->sc_codec, (sc)->sc_nid,		\
    194  1.1  jmcneill 	  CORB_GET_PARAMETER, COP_##cop)
    195  1.1  jmcneill #define	hda_get_wparam(w, cop)					\
    196  1.1  jmcneill 	hdaudio_command((w)->w_afg->sc_codec, (w)->w_nid,	\
    197  1.1  jmcneill 	  CORB_GET_PARAMETER, COP_##cop)
    198  1.1  jmcneill 
    199  1.1  jmcneill struct hdaudio_assoc {
    200  1.1  jmcneill 	bool			as_enable;
    201  1.1  jmcneill 	bool			as_activated;
    202  1.1  jmcneill 	u_char			as_index;
    203  1.1  jmcneill 	enum hdaudio_pindir	as_dir;
    204  1.1  jmcneill 	u_char			as_pincnt;
    205  1.1  jmcneill 	u_char			as_fakeredir;
    206  1.1  jmcneill 	int			as_digital;
    207  1.1  jmcneill #define	HDAFG_AS_ANALOG		0
    208  1.1  jmcneill #define	HDAFG_AS_SPDIF		1
    209  1.1  jmcneill #define	HDAFG_AS_HDMI		2
    210  1.1  jmcneill #define	HDAFG_AS_DISPLAYPORT	3
    211  1.1  jmcneill 	bool			as_displaydev;
    212  1.1  jmcneill 	int			as_hpredir;
    213  1.1  jmcneill 	int			as_pins[HDAUDIO_MAXPINS];
    214  1.1  jmcneill 	int			as_dacs[HDAUDIO_MAXPINS];
    215  1.1  jmcneill };
    216  1.1  jmcneill 
    217  1.1  jmcneill struct hdaudio_widget {
    218  1.1  jmcneill 	struct hdafg_softc	*w_afg;
    219  1.1  jmcneill 	char				w_name[32];
    220  1.1  jmcneill 	int				w_nid;
    221  1.1  jmcneill 	bool				w_enable;
    222  1.1  jmcneill 	bool				w_waspin;
    223  1.1  jmcneill 	int				w_selconn;
    224  1.1  jmcneill 	int				w_bindas;
    225  1.1  jmcneill 	int				w_bindseqmask;
    226  1.1  jmcneill 	int				w_pflags;
    227  1.1  jmcneill 	int				w_audiodev;
    228  1.1  jmcneill 	uint32_t			w_audiomask;
    229  1.1  jmcneill 
    230  1.1  jmcneill 	int				w_nconns;
    231  1.1  jmcneill 	int				w_conns[HDAUDIO_MAXCONNECTIONS];
    232  1.1  jmcneill 	bool				w_connsenable[HDAUDIO_MAXCONNECTIONS];
    233  1.1  jmcneill 
    234  1.1  jmcneill 	int				w_type;
    235  1.1  jmcneill 	struct {
    236  1.1  jmcneill 		uint32_t		aw_cap;
    237  1.1  jmcneill 		uint32_t		pcm_size_rate;
    238  1.1  jmcneill 		uint32_t		stream_format;
    239  1.1  jmcneill 		uint32_t		outamp_cap;
    240  1.1  jmcneill 		uint32_t		inamp_cap;
    241  1.1  jmcneill 		uint32_t		eapdbtl;
    242  1.1  jmcneill 	} w_p;
    243  1.1  jmcneill 	struct {
    244  1.1  jmcneill 		uint32_t		config;
    245  1.1  jmcneill 		uint32_t		biosconfig;
    246  1.1  jmcneill 		uint32_t		cap;
    247  1.1  jmcneill 		uint32_t		ctrl;
    248  1.1  jmcneill 	} w_pin;
    249  1.1  jmcneill };
    250  1.1  jmcneill 
    251  1.1  jmcneill struct hdaudio_control {
    252  1.1  jmcneill 	struct hdaudio_widget	*ctl_widget, *ctl_childwidget;
    253  1.1  jmcneill 	bool			ctl_enable;
    254  1.1  jmcneill 	int			ctl_index;
    255  1.1  jmcneill 	enum hdaudio_pindir	ctl_dir, ctl_ndir;
    256  1.1  jmcneill 	int			ctl_mute, ctl_step, ctl_size, ctl_offset;
    257  1.1  jmcneill 	int			ctl_left, ctl_right, ctl_forcemute;
    258  1.1  jmcneill 	uint32_t		ctl_muted;
    259  1.1  jmcneill 	uint32_t		ctl_audiomask, ctl_paudiomask;
    260  1.1  jmcneill };
    261  1.1  jmcneill 
    262  1.1  jmcneill #define	HDAUDIO_CONTROL_GIVE(ctl)	((ctl)->ctl_step ? 1 : 0)
    263  1.1  jmcneill 
    264  1.1  jmcneill struct hdaudio_mixer {
    265  1.1  jmcneill 	struct hdaudio_control		*mx_ctl;
    266  1.1  jmcneill 	mixer_devinfo_t			mx_di;
    267  1.1  jmcneill };
    268  1.1  jmcneill 
    269  1.1  jmcneill struct hdaudio_audiodev {
    270  1.1  jmcneill 	struct hdafg_softc	*ad_sc;
    271  1.1  jmcneill 	device_t			ad_audiodev;
    272  1.1  jmcneill 	struct audio_encoding_set	*ad_encodings;
    273  1.1  jmcneill 	int				ad_nformats;
    274  1.1  jmcneill 	struct audio_format		ad_formats[HDAUDIO_MAXFORMATS];
    275  1.1  jmcneill 
    276  1.1  jmcneill 	struct hdaudio_stream		*ad_playback;
    277  1.1  jmcneill 	void				(*ad_playbackintr)(void *);
    278  1.1  jmcneill 	void				*ad_playbackintrarg;
    279  1.1  jmcneill 	int				ad_playbacknid[HDAUDIO_MAXPINS];
    280  1.1  jmcneill 	struct hdaudio_assoc		*ad_playbackassoc;
    281  1.1  jmcneill 	struct hdaudio_stream		*ad_capture;
    282  1.1  jmcneill 	void				(*ad_captureintr)(void *);
    283  1.1  jmcneill 	void				*ad_captureintrarg;
    284  1.1  jmcneill 	int				ad_capturenid[HDAUDIO_MAXPINS];
    285  1.1  jmcneill 	struct hdaudio_assoc		*ad_captureassoc;
    286  1.1  jmcneill };
    287  1.1  jmcneill 
    288  1.1  jmcneill struct hdafg_softc {
    289  1.1  jmcneill 	device_t			sc_dev;
    290  1.1  jmcneill 	kmutex_t			sc_lock;
    291  1.1  jmcneill 	kmutex_t			sc_intr_lock;
    292  1.1  jmcneill 	struct hdaudio_softc		*sc_host;
    293  1.1  jmcneill 	struct hdaudio_codec		*sc_codec;
    294  1.1  jmcneill 	struct hdaudio_function_group	*sc_fg;
    295  1.1  jmcneill 	int				sc_nid;
    296  1.1  jmcneill 	uint16_t			sc_vendor, sc_product;
    297  1.1  jmcneill 
    298  1.1  jmcneill 	prop_array_t			sc_config;
    299  1.1  jmcneill 
    300  1.1  jmcneill 	int				sc_startnode, sc_endnode;
    301  1.1  jmcneill 	int				sc_nwidgets;
    302  1.1  jmcneill 	struct hdaudio_widget		*sc_widgets;
    303  1.1  jmcneill 	int				sc_nassocs;
    304  1.1  jmcneill 	struct hdaudio_assoc		*sc_assocs;
    305  1.1  jmcneill 	int				sc_nctls;
    306  1.1  jmcneill 	struct hdaudio_control		*sc_ctls;
    307  1.1  jmcneill 	int				sc_nmixers;
    308  1.1  jmcneill 	struct hdaudio_mixer		*sc_mixers;
    309  1.1  jmcneill 	bool				sc_has_beepgen;
    310  1.1  jmcneill 
    311  1.1  jmcneill 	int				sc_pchan, sc_rchan;
    312  1.1  jmcneill 	audio_params_t			sc_pparam, sc_rparam;
    313  1.1  jmcneill 
    314  1.1  jmcneill 	struct callout			sc_jack_callout;
    315  1.1  jmcneill 	bool				sc_jack_polling;
    316  1.1  jmcneill 
    317  1.1  jmcneill 	struct {
    318  1.1  jmcneill 		uint32_t		afg_cap;
    319  1.1  jmcneill 		uint32_t		pcm_size_rate;
    320  1.1  jmcneill 		uint32_t		stream_format;
    321  1.1  jmcneill 		uint32_t		outamp_cap;
    322  1.1  jmcneill 		uint32_t		inamp_cap;
    323  1.1  jmcneill 		uint32_t		power_states;
    324  1.1  jmcneill 		uint32_t		gpio_cnt;
    325  1.1  jmcneill 	} sc_p;
    326  1.1  jmcneill 
    327  1.1  jmcneill 	struct hdaudio_audiodev		sc_audiodev;
    328  1.1  jmcneill };
    329  1.1  jmcneill 
    330  1.1  jmcneill static int	hdafg_match(device_t, cfdata_t, void *);
    331  1.1  jmcneill static void	hdafg_attach(device_t, device_t, void *);
    332  1.1  jmcneill static int	hdafg_detach(device_t, int);
    333  1.1  jmcneill static void	hdafg_childdet(device_t, device_t);
    334  1.1  jmcneill static bool	hdafg_suspend(device_t, const pmf_qual_t *);
    335  1.1  jmcneill static bool	hdafg_resume(device_t, const pmf_qual_t *);
    336  1.1  jmcneill 
    337  1.1  jmcneill static int	hdafg_unsol(device_t, uint8_t);
    338  1.1  jmcneill static int	hdafg_widget_info(void *, prop_dictionary_t,
    339  1.1  jmcneill 					prop_dictionary_t);
    340  1.1  jmcneill static int	hdafg_codec_info(void *, prop_dictionary_t,
    341  1.1  jmcneill 				       prop_dictionary_t);
    342  1.1  jmcneill static void	hdafg_enable_analog_beep(struct hdafg_softc *);
    343  1.1  jmcneill 
    344  1.1  jmcneill CFATTACH_DECL2_NEW(
    345  1.1  jmcneill     hdafg,
    346  1.1  jmcneill     sizeof(struct hdafg_softc),
    347  1.1  jmcneill     hdafg_match,
    348  1.1  jmcneill     hdafg_attach,
    349  1.1  jmcneill     hdafg_detach,
    350  1.1  jmcneill     NULL,
    351  1.1  jmcneill     NULL,
    352  1.1  jmcneill     hdafg_childdet
    353  1.1  jmcneill );
    354  1.1  jmcneill 
    355  1.1  jmcneill static int	hdafg_query_encoding(void *, struct audio_encoding *);
    356  1.1  jmcneill static int	hdafg_set_params(void *, int, int,
    357  1.1  jmcneill 				   audio_params_t *,
    358  1.1  jmcneill 				   audio_params_t *,
    359  1.1  jmcneill 				   stream_filter_list_t *,
    360  1.1  jmcneill 				   stream_filter_list_t *);
    361  1.1  jmcneill static int	hdafg_round_blocksize(void *, int, int,
    362  1.1  jmcneill 					const audio_params_t *);
    363  1.1  jmcneill static int	hdafg_commit_settings(void *);
    364  1.1  jmcneill static int	hdafg_halt_output(void *);
    365  1.1  jmcneill static int	hdafg_halt_input(void *);
    366  1.1  jmcneill static int	hdafg_set_port(void *, mixer_ctrl_t *);
    367  1.1  jmcneill static int	hdafg_get_port(void *, mixer_ctrl_t *);
    368  1.1  jmcneill static int	hdafg_query_devinfo(void *, mixer_devinfo_t *);
    369  1.1  jmcneill static void *	hdafg_allocm(void *, int, size_t);
    370  1.1  jmcneill static void	hdafg_freem(void *, void *, size_t);
    371  1.1  jmcneill static int	hdafg_getdev(void *, struct audio_device *);
    372  1.1  jmcneill static size_t	hdafg_round_buffersize(void *, int, size_t);
    373  1.1  jmcneill static paddr_t	hdafg_mappage(void *, void *, off_t, int);
    374  1.1  jmcneill static int	hdafg_get_props(void *);
    375  1.1  jmcneill static int	hdafg_trigger_output(void *, void *, void *, int,
    376  1.1  jmcneill 				       void (*)(void *), void *,
    377  1.1  jmcneill 				       const audio_params_t *);
    378  1.1  jmcneill static int	hdafg_trigger_input(void *, void *, void *, int,
    379  1.1  jmcneill 				      void (*)(void *), void *,
    380  1.1  jmcneill 				      const audio_params_t *);
    381  1.1  jmcneill static void	hdafg_get_locks(void *, kmutex_t **, kmutex_t **);
    382  1.1  jmcneill 
    383  1.1  jmcneill static const struct audio_hw_if hdafg_hw_if = {
    384  1.1  jmcneill 	.query_encoding		= hdafg_query_encoding,
    385  1.1  jmcneill 	.set_params		= hdafg_set_params,
    386  1.1  jmcneill 	.round_blocksize	= hdafg_round_blocksize,
    387  1.1  jmcneill 	.commit_settings	= hdafg_commit_settings,
    388  1.1  jmcneill 	.halt_output		= hdafg_halt_output,
    389  1.1  jmcneill 	.halt_input		= hdafg_halt_input,
    390  1.1  jmcneill 	.getdev			= hdafg_getdev,
    391  1.1  jmcneill 	.set_port		= hdafg_set_port,
    392  1.1  jmcneill 	.get_port		= hdafg_get_port,
    393  1.1  jmcneill 	.query_devinfo		= hdafg_query_devinfo,
    394  1.1  jmcneill 	.allocm			= hdafg_allocm,
    395  1.1  jmcneill 	.freem			= hdafg_freem,
    396  1.1  jmcneill 	.round_buffersize	= hdafg_round_buffersize,
    397  1.1  jmcneill 	.mappage		= hdafg_mappage,
    398  1.1  jmcneill 	.get_props		= hdafg_get_props,
    399  1.1  jmcneill 	.trigger_output		= hdafg_trigger_output,
    400  1.1  jmcneill 	.trigger_input		= hdafg_trigger_input,
    401  1.1  jmcneill 	.get_locks		= hdafg_get_locks,
    402  1.1  jmcneill };
    403  1.1  jmcneill 
    404  1.1  jmcneill static int
    405  1.1  jmcneill hdafg_append_formats(struct hdaudio_audiodev *ad,
    406  1.1  jmcneill     const struct audio_format *format)
    407  1.1  jmcneill {
    408  1.1  jmcneill 	if (ad->ad_nformats + 1 >= HDAUDIO_MAXFORMATS) {
    409  1.1  jmcneill 		hda_print1(ad->ad_sc, "[ENOMEM] ");
    410  1.1  jmcneill 		return ENOMEM;
    411  1.1  jmcneill 	}
    412  1.1  jmcneill 	ad->ad_formats[ad->ad_nformats++] = *format;
    413  1.1  jmcneill 
    414  1.1  jmcneill 	return 0;
    415  1.1  jmcneill }
    416  1.1  jmcneill 
    417  1.1  jmcneill static struct hdaudio_widget *
    418  1.1  jmcneill hdafg_widget_lookup(struct hdafg_softc *sc, int nid)
    419  1.1  jmcneill {
    420  1.1  jmcneill 	if (sc->sc_widgets == NULL || sc->sc_nwidgets == 0) {
    421  1.1  jmcneill 		hda_error(sc, "lookup failed; widgets %p nwidgets %d\n",
    422  1.1  jmcneill 		    sc->sc_widgets, sc->sc_nwidgets);
    423  1.1  jmcneill 		return NULL;
    424  1.1  jmcneill 	}
    425  1.1  jmcneill 	if (nid < sc->sc_startnode || nid >= sc->sc_endnode) {
    426  1.1  jmcneill 		hda_debug(sc, "nid %02X out of range (%02X-%02X)\n",
    427  1.1  jmcneill 		    nid, sc->sc_startnode, sc->sc_endnode);
    428  1.1  jmcneill 		return NULL;
    429  1.1  jmcneill 	}
    430  1.1  jmcneill 	return &sc->sc_widgets[nid - sc->sc_startnode];
    431  1.1  jmcneill }
    432  1.1  jmcneill 
    433  1.1  jmcneill static struct hdaudio_control *
    434  1.1  jmcneill hdafg_control_lookup(struct hdafg_softc *sc, int nid,
    435  1.1  jmcneill     enum hdaudio_pindir dir, int index, int cnt)
    436  1.1  jmcneill {
    437  1.1  jmcneill 	struct hdaudio_control *ctl;
    438  1.1  jmcneill 	int i, found = 0;
    439  1.1  jmcneill 
    440  1.1  jmcneill 	if (sc->sc_ctls == NULL)
    441  1.1  jmcneill 		return NULL;
    442  1.1  jmcneill 	for (i = 0; i < sc->sc_nctls; i++) {
    443  1.1  jmcneill 		ctl = &sc->sc_ctls[i];
    444  1.1  jmcneill 		if (ctl->ctl_enable == false)
    445  1.1  jmcneill 			continue;
    446  1.1  jmcneill 		if (ctl->ctl_widget->w_nid != nid)
    447  1.1  jmcneill 			continue;
    448  1.1  jmcneill 		if (dir && ctl->ctl_ndir != dir)
    449  1.1  jmcneill 			continue;
    450  1.1  jmcneill 		if (index >= 0 && ctl->ctl_ndir == HDAUDIO_PINDIR_IN &&
    451  1.1  jmcneill 		    ctl->ctl_dir == ctl->ctl_ndir && ctl->ctl_index != index)
    452  1.1  jmcneill 			continue;
    453  1.1  jmcneill 		found++;
    454  1.1  jmcneill 		if (found == cnt || cnt <= 0)
    455  1.1  jmcneill 			return ctl;
    456  1.1  jmcneill 	}
    457  1.1  jmcneill 
    458  1.1  jmcneill 	return NULL;
    459  1.1  jmcneill }
    460  1.1  jmcneill 
    461  1.1  jmcneill static void
    462  1.1  jmcneill hdafg_widget_connection_parse(struct hdaudio_widget *w)
    463  1.1  jmcneill {
    464  1.1  jmcneill 	struct hdafg_softc *sc = w->w_afg;
    465  1.1  jmcneill 	uint32_t res;
    466  1.1  jmcneill 	int i, j, maxconns, ents, entnum;
    467  1.1  jmcneill 	int cnid, addcnid, prevcnid;
    468  1.1  jmcneill 
    469  1.1  jmcneill 	w->w_nconns = 0;
    470  1.1  jmcneill 
    471  1.1  jmcneill 	res = hda_get_wparam(w, CONNECTION_LIST_LENGTH);
    472  1.1  jmcneill 	ents = COP_CONNECTION_LIST_LENGTH_LEN(res);
    473  1.1  jmcneill 	if (ents < 1)
    474  1.1  jmcneill 		return;
    475  1.1  jmcneill 	if (res & COP_CONNECTION_LIST_LENGTH_LONG_FORM)
    476  1.1  jmcneill 		entnum = 2;
    477  1.1  jmcneill 	else
    478  1.1  jmcneill 		entnum = 4;
    479  1.1  jmcneill 	maxconns = (sizeof(w->w_conns) / sizeof(w->w_conns[0])) - 1;
    480  1.1  jmcneill 	prevcnid = 0;
    481  1.1  jmcneill 
    482  1.1  jmcneill #define	CONN_RMASK(e)		(1 << ((32 / (e)) - 1))
    483  1.1  jmcneill #define	CONN_NMASK(e)		(CONN_RMASK(e) - 1)
    484  1.1  jmcneill #define	CONN_RESVAL(r, e, n)	((r) >> ((32 / (e)) * (n)))
    485  1.1  jmcneill #define	CONN_RANGE(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_RMASK(e))
    486  1.1  jmcneill #define	CONN_CNID(r, e, n)	(CONN_RESVAL(r, e, n) & CONN_NMASK(e))
    487  1.1  jmcneill 
    488  1.1  jmcneill 	for (i = 0; i < ents; i += entnum) {
    489  1.1  jmcneill 		res = hdaudio_command(sc->sc_codec, w->w_nid,
    490  1.1  jmcneill 		    CORB_GET_CONNECTION_LIST_ENTRY, i);
    491  1.1  jmcneill 		for (j = 0; j < entnum; j++) {
    492  1.1  jmcneill 			cnid = CONN_CNID(res, entnum, j);
    493  1.1  jmcneill 			if (cnid == 0) {
    494  1.1  jmcneill 				if (w->w_nconns < ents) {
    495  1.1  jmcneill 					hda_error(sc, "WARNING: zero cnid\n");
    496  1.1  jmcneill 				} else {
    497  1.1  jmcneill 					goto getconns_out;
    498  1.1  jmcneill 				}
    499  1.1  jmcneill 			}
    500  1.1  jmcneill 			if (cnid < sc->sc_startnode || cnid >= sc->sc_endnode)
    501  1.1  jmcneill 				hda_debug(sc, "ghost nid=%02X\n", cnid);
    502  1.1  jmcneill 			if (CONN_RANGE(res, entnum, j) == 0)
    503  1.1  jmcneill 				addcnid = cnid;
    504  1.1  jmcneill 			else if (prevcnid == 0 || prevcnid >= cnid) {
    505  1.1  jmcneill 				hda_error(sc, "invalid child range\n");
    506  1.1  jmcneill 				addcnid = cnid;
    507  1.1  jmcneill 			} else
    508  1.1  jmcneill 				addcnid = prevcnid + 1;
    509  1.1  jmcneill 			while (addcnid <= cnid) {
    510  1.1  jmcneill 				if (w->w_nconns > maxconns) {
    511  1.1  jmcneill 					hda_error(sc,
    512  1.1  jmcneill 					    "max connections reached\n");
    513  1.1  jmcneill 					goto getconns_out;
    514  1.1  jmcneill 				}
    515  1.1  jmcneill 				w->w_connsenable[w->w_nconns] = true;
    516  1.1  jmcneill 				w->w_conns[w->w_nconns++] = addcnid++;
    517  1.1  jmcneill 				hda_trace(sc, "add connection %02X->%02X\n",
    518  1.1  jmcneill 				    w->w_nid, addcnid - 1);
    519  1.1  jmcneill 			}
    520  1.1  jmcneill 			prevcnid = cnid;
    521  1.1  jmcneill 		}
    522  1.1  jmcneill 	}
    523  1.1  jmcneill #undef CONN_RMASK
    524  1.1  jmcneill #undef CONN_NMASK
    525  1.1  jmcneill #undef CONN_RESVAL
    526  1.1  jmcneill #undef CONN_RANGE
    527  1.1  jmcneill #undef CONN_CNID
    528  1.1  jmcneill 
    529  1.1  jmcneill getconns_out:
    530  1.1  jmcneill 	return;
    531  1.1  jmcneill }
    532  1.1  jmcneill 
    533  1.1  jmcneill static void
    534  1.1  jmcneill hdafg_widget_pin_dump(struct hdafg_softc *sc)
    535  1.1  jmcneill {
    536  1.1  jmcneill 	struct hdaudio_widget *w;
    537  1.1  jmcneill 	int i, conn;
    538  1.1  jmcneill 
    539  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
    540  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
    541  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
    542  1.1  jmcneill 			continue;
    543  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
    544  1.1  jmcneill 			continue;
    545  1.1  jmcneill 		conn = COP_CFG_PORT_CONNECTIVITY(w->w_pin.config);
    546  1.1  jmcneill 		if (conn != 1) {
    547  1.1  jmcneill #ifdef HDAUDIO_DEBUG
    548  1.1  jmcneill 			int color = COP_CFG_COLOR(w->w_pin.config);
    549  1.1  jmcneill 			int defdev = COP_CFG_DEFAULT_DEVICE(w->w_pin.config);
    550  1.1  jmcneill 			hda_trace(sc, "io %02X: %s (%s, %s)\n",
    551  1.1  jmcneill 			    w->w_nid,
    552  1.1  jmcneill 			    hdafg_default_device[defdev],
    553  1.1  jmcneill 			    hdafg_color[color],
    554  1.1  jmcneill 			    hdafg_port_connectivity[conn]);
    555  1.1  jmcneill #endif
    556  1.1  jmcneill 		}
    557  1.1  jmcneill 	}
    558  1.1  jmcneill }
    559  1.1  jmcneill 
    560  1.1  jmcneill static void
    561  1.1  jmcneill hdafg_widget_setconfig(struct hdaudio_widget *w, uint32_t cfg)
    562  1.1  jmcneill {
    563  1.1  jmcneill 	struct hdafg_softc *sc = w->w_afg;
    564  1.1  jmcneill 
    565  1.1  jmcneill 	hdaudio_command(sc->sc_codec, w->w_nid,
    566  1.1  jmcneill 	    CORB_SET_CONFIGURATION_DEFAULT_1, (cfg >>  0) & 0xff);
    567  1.1  jmcneill 	hdaudio_command(sc->sc_codec, w->w_nid,
    568  1.1  jmcneill 	    CORB_SET_CONFIGURATION_DEFAULT_2, (cfg >>  8) & 0xff);
    569  1.1  jmcneill 	hdaudio_command(sc->sc_codec, w->w_nid,
    570  1.1  jmcneill 	    CORB_SET_CONFIGURATION_DEFAULT_3, (cfg >> 16) & 0xff);
    571  1.1  jmcneill 	hdaudio_command(sc->sc_codec, w->w_nid,
    572  1.1  jmcneill 	    CORB_SET_CONFIGURATION_DEFAULT_4, (cfg >> 24) & 0xff);
    573  1.1  jmcneill }
    574  1.1  jmcneill 
    575  1.1  jmcneill static uint32_t
    576  1.1  jmcneill hdafg_widget_getconfig(struct hdaudio_widget *w)
    577  1.1  jmcneill {
    578  1.1  jmcneill 	struct hdafg_softc *sc = w->w_afg;
    579  1.1  jmcneill 	uint32_t config = 0;
    580  1.1  jmcneill 	prop_object_iterator_t iter;
    581  1.1  jmcneill 	prop_dictionary_t dict;
    582  1.1  jmcneill 	prop_object_t obj;
    583  1.1  jmcneill 	int16_t nid;
    584  1.1  jmcneill 
    585  1.1  jmcneill 	if (sc->sc_config == NULL)
    586  1.1  jmcneill 		goto biosconfig;
    587  1.1  jmcneill 
    588  1.1  jmcneill 	iter = prop_array_iterator(sc->sc_config);
    589  1.1  jmcneill 	if (iter == NULL)
    590  1.1  jmcneill 		goto biosconfig;
    591  1.1  jmcneill 	prop_object_iterator_reset(iter);
    592  1.1  jmcneill 	while ((obj = prop_object_iterator_next(iter)) != NULL) {
    593  1.1  jmcneill 		if (prop_object_type(obj) != PROP_TYPE_DICTIONARY)
    594  1.1  jmcneill 			continue;
    595  1.1  jmcneill 		dict = (prop_dictionary_t)obj;
    596  1.1  jmcneill 		if (!prop_dictionary_get_int16(dict, "nid", &nid) ||
    597  1.1  jmcneill 		    !prop_dictionary_get_uint32(dict, "config", &config))
    598  1.1  jmcneill 			continue;
    599  1.1  jmcneill 		if (nid == w->w_nid)
    600  1.1  jmcneill 			return config;
    601  1.1  jmcneill 	}
    602  1.1  jmcneill 
    603  1.1  jmcneill biosconfig:
    604  1.1  jmcneill 	return hdaudio_command(sc->sc_codec, w->w_nid,
    605  1.1  jmcneill 	    CORB_GET_CONFIGURATION_DEFAULT, 0);
    606  1.1  jmcneill }
    607  1.1  jmcneill 
    608  1.1  jmcneill static void
    609  1.1  jmcneill hdafg_widget_pin_parse(struct hdaudio_widget *w)
    610  1.1  jmcneill {
    611  1.1  jmcneill 	struct hdafg_softc *sc = w->w_afg;
    612  1.1  jmcneill 	int conn, color, defdev;
    613  1.1  jmcneill 
    614  1.1  jmcneill 	w->w_pin.cap = hda_get_wparam(w, PIN_CAPABILITIES);
    615  1.1  jmcneill 	w->w_pin.config = hdafg_widget_getconfig(w);
    616  1.1  jmcneill 	w->w_pin.biosconfig = hdaudio_command(sc->sc_codec, w->w_nid,
    617  1.1  jmcneill 	    CORB_GET_CONFIGURATION_DEFAULT, 0);
    618  1.1  jmcneill 	w->w_pin.ctrl = hdaudio_command(sc->sc_codec, w->w_nid,
    619  1.1  jmcneill 	    CORB_GET_PIN_WIDGET_CONTROL, 0);
    620  1.1  jmcneill 
    621  1.1  jmcneill 	/* treat line-out as speaker, unless connection type is RCA */
    622  1.1  jmcneill 	if (COP_CFG_DEFAULT_DEVICE(w->w_pin.config) == COP_DEVICE_LINE_OUT &&
    623  1.1  jmcneill 	    COP_CFG_CONNECTION_TYPE(w->w_pin.config) != COP_CONN_TYPE_RCA) {
    624  1.1  jmcneill 		w->w_pin.config &= ~COP_DEVICE_MASK;
    625  1.1  jmcneill 		w->w_pin.config |= (COP_DEVICE_SPEAKER << COP_DEVICE_SHIFT);
    626  1.1  jmcneill 	}
    627  1.1  jmcneill 
    628  1.1  jmcneill 	if (w->w_pin.cap & COP_PINCAP_EAPD_CAPABLE) {
    629  1.1  jmcneill 		w->w_p.eapdbtl = hdaudio_command(sc->sc_codec, w->w_nid,
    630  1.1  jmcneill 		    CORB_GET_EAPD_BTL_ENABLE, 0);
    631  1.1  jmcneill 		w->w_p.eapdbtl &= 0x7;
    632  1.1  jmcneill 		w->w_p.eapdbtl |= COP_EAPD_ENABLE_EAPD;
    633  1.1  jmcneill 	} else
    634  1.1  jmcneill 		w->w_p.eapdbtl = 0xffffffff;
    635  1.1  jmcneill 
    636  1.1  jmcneill #if 0
    637  1.1  jmcneill 	/* XXX VT1708 */
    638  1.1  jmcneill 	if (COP_CFG_DEFAULT_DEVICE(w->w_pin.config) == COP_DEVICE_SPEAKER &&
    639  1.1  jmcneill 	    COP_CFG_DEFAULT_ASSOCIATION(w->w_pin.config) == 15) {
    640  1.1  jmcneill 		hda_trace(sc, "forcing speaker nid %02X to assoc=14\n",
    641  1.1  jmcneill 		    w->w_nid);
    642  1.1  jmcneill 		/* set assoc=14 */
    643  1.1  jmcneill 		w->w_pin.config &= ~0xf0;
    644  1.1  jmcneill 		w->w_pin.config |= 0xe0;
    645  1.1  jmcneill 	}
    646  1.1  jmcneill 	if (COP_CFG_DEFAULT_DEVICE(w->w_pin.config) == COP_DEVICE_HP_OUT &&
    647  1.1  jmcneill 	    COP_CFG_PORT_CONNECTIVITY(w->w_pin.config) == COP_PORT_NONE) {
    648  1.1  jmcneill 		hda_trace(sc, "forcing hp out nid %02X to assoc=14\n",
    649  1.1  jmcneill 		    w->w_nid);
    650  1.1  jmcneill 		/* set connectivity to 'jack' */
    651  1.1  jmcneill 		w->w_pin.config &= ~(COP_PORT_BOTH << 30);
    652  1.1  jmcneill 		w->w_pin.config |= (COP_PORT_JACK << 30);
    653  1.1  jmcneill 		/* set seq=15 */
    654  1.1  jmcneill 		w->w_pin.config &= ~0xf;
    655  1.1  jmcneill 		w->w_pin.config |= 15;
    656  1.1  jmcneill 		/* set assoc=14 */
    657  1.1  jmcneill 		w->w_pin.config &= ~0xf0;
    658  1.1  jmcneill 		w->w_pin.config |= 0xe0;
    659  1.1  jmcneill 	}
    660  1.1  jmcneill #endif
    661  1.1  jmcneill 
    662  1.1  jmcneill 	conn = COP_CFG_PORT_CONNECTIVITY(w->w_pin.config);
    663  1.1  jmcneill 	color = COP_CFG_COLOR(w->w_pin.config);
    664  1.1  jmcneill 	defdev = COP_CFG_DEFAULT_DEVICE(w->w_pin.config);
    665  1.1  jmcneill 
    666  1.1  jmcneill 	strlcat(w->w_name, ": ", sizeof(w->w_name));
    667  1.1  jmcneill 	strlcat(w->w_name, hdafg_default_device[defdev], sizeof(w->w_name));
    668  1.1  jmcneill 	strlcat(w->w_name, " (", sizeof(w->w_name));
    669  1.1  jmcneill 	if (conn == 0 && color != 0 && color != 15) {
    670  1.1  jmcneill 		strlcat(w->w_name, hdafg_color[color], sizeof(w->w_name));
    671  1.1  jmcneill 		strlcat(w->w_name, " ", sizeof(w->w_name));
    672  1.1  jmcneill 	}
    673  1.1  jmcneill 	strlcat(w->w_name, hdafg_port_connectivity[conn], sizeof(w->w_name));
    674  1.1  jmcneill 	strlcat(w->w_name, ")", sizeof(w->w_name));
    675  1.1  jmcneill }
    676  1.1  jmcneill 
    677  1.1  jmcneill static uint32_t
    678  1.1  jmcneill hdafg_widget_getcaps(struct hdaudio_widget *w)
    679  1.1  jmcneill {
    680  1.1  jmcneill 	struct hdafg_softc *sc = w->w_afg;
    681  1.1  jmcneill 	uint32_t wcap, config;
    682  1.1  jmcneill 	bool pcbeep = false;
    683  1.1  jmcneill 
    684  1.1  jmcneill 	wcap = hda_get_wparam(w, AUDIO_WIDGET_CAPABILITIES);
    685  1.1  jmcneill 	config = hdafg_widget_getconfig(w);
    686  1.1  jmcneill 
    687  1.1  jmcneill 	w->w_waspin = false;
    688  1.1  jmcneill 
    689  1.1  jmcneill 	switch (sc->sc_vendor) {
    690  1.1  jmcneill 	case HDAUDIO_VENDOR_ANALOG:
    691  1.1  jmcneill 		/*
    692  1.1  jmcneill 		 * help the parser by marking the analog
    693  1.1  jmcneill 		 * beeper as a beep generator
    694  1.1  jmcneill 		 */
    695  1.1  jmcneill 		if (w->w_nid == 0x1a &&
    696  1.1  jmcneill 		    COP_CFG_SEQUENCE(config) == 0x0 &&
    697  1.1  jmcneill 		    COP_CFG_DEFAULT_ASSOCIATION(config) == 0xf &&
    698  1.1  jmcneill 		    COP_CFG_PORT_CONNECTIVITY(config) ==
    699  1.1  jmcneill 		      COP_PORT_FIXED_FUNCTION &&
    700  1.1  jmcneill 		    COP_CFG_DEFAULT_DEVICE(config) ==
    701  1.1  jmcneill 		      COP_DEVICE_OTHER) {
    702  1.1  jmcneill 			pcbeep = true;
    703  1.1  jmcneill 		}
    704  1.1  jmcneill 		break;
    705  1.1  jmcneill 	}
    706  1.1  jmcneill 
    707  1.1  jmcneill 	if (pcbeep ||
    708  1.1  jmcneill 	    (sc->sc_has_beepgen == false &&
    709  1.1  jmcneill 	    COP_CFG_DEFAULT_DEVICE(config) == COP_DEVICE_SPEAKER &&
    710  1.1  jmcneill 	    (wcap & (COP_AWCAP_INAMP_PRESENT|COP_AWCAP_OUTAMP_PRESENT)) == 0)) {
    711  1.1  jmcneill 		wcap &= ~COP_AWCAP_TYPE_MASK;
    712  1.1  jmcneill 		wcap |= (COP_AWCAP_TYPE_BEEP_GENERATOR << COP_AWCAP_TYPE_SHIFT);
    713  1.1  jmcneill 		w->w_waspin = true;
    714  1.1  jmcneill 	}
    715  1.1  jmcneill 
    716  1.1  jmcneill 	return wcap;
    717  1.1  jmcneill }
    718  1.1  jmcneill 
    719  1.1  jmcneill static void
    720  1.1  jmcneill hdafg_widget_parse(struct hdaudio_widget *w)
    721  1.1  jmcneill {
    722  1.1  jmcneill 	struct hdafg_softc *sc = w->w_afg;
    723  1.1  jmcneill 	const char *tstr;
    724  1.1  jmcneill 
    725  1.1  jmcneill 	w->w_p.aw_cap = hdafg_widget_getcaps(w);
    726  1.1  jmcneill 	w->w_type = COP_AWCAP_TYPE(w->w_p.aw_cap);
    727  1.1  jmcneill 
    728  1.1  jmcneill 	switch (w->w_type) {
    729  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_OUTPUT:	tstr = "audio output"; break;
    730  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_INPUT:	tstr = "audio input"; break;
    731  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_MIXER:	tstr = "audio mixer"; break;
    732  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_SELECTOR:	tstr = "audio selector"; break;
    733  1.1  jmcneill 	case COP_AWCAP_TYPE_PIN_COMPLEX:	tstr = "pin"; break;
    734  1.1  jmcneill 	case COP_AWCAP_TYPE_POWER_WIDGET:	tstr = "power widget"; break;
    735  1.1  jmcneill 	case COP_AWCAP_TYPE_VOLUME_KNOB:	tstr = "volume knob"; break;
    736  1.1  jmcneill 	case COP_AWCAP_TYPE_BEEP_GENERATOR:	tstr = "beep generator"; break;
    737  1.1  jmcneill 	case COP_AWCAP_TYPE_VENDOR_DEFINED:	tstr = "vendor defined"; break;
    738  1.1  jmcneill 	default:				tstr = "unknown"; break;
    739  1.1  jmcneill 	}
    740  1.1  jmcneill 
    741  1.1  jmcneill 	strlcpy(w->w_name, tstr, sizeof(w->w_name));
    742  1.1  jmcneill 
    743  1.1  jmcneill 	hdafg_widget_connection_parse(w);
    744  1.1  jmcneill 
    745  1.1  jmcneill 	if (w->w_p.aw_cap & COP_AWCAP_INAMP_PRESENT) {
    746  1.1  jmcneill 		if (w->w_p.aw_cap & COP_AWCAP_AMP_PARAM_OVERRIDE)
    747  1.1  jmcneill 			w->w_p.inamp_cap = hda_get_wparam(w,
    748  1.1  jmcneill 			    AMPLIFIER_CAPABILITIES_INAMP);
    749  1.1  jmcneill 		else
    750  1.1  jmcneill 			w->w_p.inamp_cap = sc->sc_p.inamp_cap;
    751  1.1  jmcneill 	}
    752  1.1  jmcneill 	if (w->w_p.aw_cap & COP_AWCAP_OUTAMP_PRESENT) {
    753  1.1  jmcneill 		if (w->w_p.aw_cap & COP_AWCAP_AMP_PARAM_OVERRIDE)
    754  1.1  jmcneill 			w->w_p.outamp_cap = hda_get_wparam(w,
    755  1.1  jmcneill 			    AMPLIFIER_CAPABILITIES_OUTAMP);
    756  1.1  jmcneill 		else
    757  1.1  jmcneill 			w->w_p.outamp_cap = sc->sc_p.outamp_cap;
    758  1.1  jmcneill 	}
    759  1.1  jmcneill 
    760  1.1  jmcneill 	w->w_p.stream_format = 0;
    761  1.1  jmcneill 	w->w_p.pcm_size_rate = 0;
    762  1.1  jmcneill 	switch (w->w_type) {
    763  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_OUTPUT:
    764  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_INPUT:
    765  1.1  jmcneill 		if (w->w_p.aw_cap & COP_AWCAP_FORMAT_OVERRIDE) {
    766  1.1  jmcneill 			w->w_p.stream_format = hda_get_wparam(w,
    767  1.1  jmcneill 			    SUPPORTED_STREAM_FORMATS);
    768  1.1  jmcneill 			w->w_p.pcm_size_rate = hda_get_wparam(w,
    769  1.1  jmcneill 			    SUPPORTED_PCM_SIZE_RATES);
    770  1.1  jmcneill 		} else {
    771  1.1  jmcneill 			w->w_p.stream_format = sc->sc_p.stream_format;
    772  1.1  jmcneill 			w->w_p.pcm_size_rate = sc->sc_p.pcm_size_rate;
    773  1.1  jmcneill 		}
    774  1.1  jmcneill 		break;
    775  1.1  jmcneill 	case COP_AWCAP_TYPE_PIN_COMPLEX:
    776  1.1  jmcneill 		hdafg_widget_pin_parse(w);
    777  1.1  jmcneill 		hdafg_widget_setconfig(w, w->w_pin.config);
    778  1.1  jmcneill 		break;
    779  1.1  jmcneill 	}
    780  1.1  jmcneill }
    781  1.1  jmcneill 
    782  1.1  jmcneill static int
    783  1.1  jmcneill hdafg_assoc_count_channels(struct hdafg_softc *sc,
    784  1.1  jmcneill     struct hdaudio_assoc *as, enum hdaudio_pindir dir)
    785  1.1  jmcneill {
    786  1.1  jmcneill 	struct hdaudio_widget *w;
    787  1.1  jmcneill 	int *dacmap;
    788  1.1  jmcneill 	int i, dacmapsz = sizeof(*dacmap) * sc->sc_endnode;
    789  1.1  jmcneill 	int nchans = 0;
    790  1.1  jmcneill 
    791  1.1  jmcneill 	if (as->as_enable == false || as->as_dir != dir)
    792  1.1  jmcneill 		return 0;
    793  1.1  jmcneill 
    794  1.1  jmcneill 	dacmap = kmem_zalloc(dacmapsz, KM_SLEEP);
    795  1.1  jmcneill 	if (dacmap == NULL)
    796  1.1  jmcneill 		return 0;
    797  1.1  jmcneill 
    798  1.1  jmcneill 	for (i = 0; i < HDAUDIO_MAXPINS; i++)
    799  1.1  jmcneill 		if (as->as_dacs[i])
    800  1.1  jmcneill 			dacmap[as->as_dacs[i]] = 1;
    801  1.1  jmcneill 
    802  1.1  jmcneill 	for (i = 1; i < sc->sc_endnode; i++) {
    803  1.1  jmcneill 		if (!dacmap[i])
    804  1.1  jmcneill 			continue;
    805  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
    806  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
    807  1.1  jmcneill 			continue;
    808  1.1  jmcneill 		nchans += COP_AWCAP_CHANNEL_COUNT(w->w_p.aw_cap);
    809  1.1  jmcneill 	}
    810  1.1  jmcneill 
    811  1.1  jmcneill 	kmem_free(dacmap, dacmapsz);
    812  1.1  jmcneill 
    813  1.1  jmcneill 	return nchans;
    814  1.1  jmcneill }
    815  1.1  jmcneill 
    816  1.1  jmcneill static const char *
    817  1.1  jmcneill hdafg_assoc_type_string(struct hdaudio_assoc *as)
    818  1.1  jmcneill {
    819  1.1  jmcneill 	switch (as->as_digital) {
    820  1.1  jmcneill 	case HDAFG_AS_ANALOG:
    821  1.1  jmcneill 		return as->as_dir == HDAUDIO_PINDIR_IN ?
    822  1.1  jmcneill 		    "ADC" : "DAC";
    823  1.1  jmcneill 	case HDAFG_AS_SPDIF:
    824  1.1  jmcneill 		return as->as_dir == HDAUDIO_PINDIR_IN ?
    825  1.1  jmcneill 		    "DIG-In" : "DIG";
    826  1.1  jmcneill 	case HDAFG_AS_HDMI:
    827  1.1  jmcneill 		return as->as_dir == HDAUDIO_PINDIR_IN ?
    828  1.1  jmcneill 		    "HDMI-In" : "HDMI";
    829  1.1  jmcneill 	case HDAFG_AS_DISPLAYPORT:
    830  1.1  jmcneill 		return as->as_dir == HDAUDIO_PINDIR_IN ?
    831  1.1  jmcneill 		    "DP-In" : "DP";
    832  1.1  jmcneill 	default:
    833  1.1  jmcneill 		return as->as_dir == HDAUDIO_PINDIR_IN ?
    834  1.1  jmcneill 		    "Unknown-In" : "Unknown-Out";
    835  1.1  jmcneill 	}
    836  1.1  jmcneill }
    837  1.1  jmcneill 
    838  1.1  jmcneill static void
    839  1.1  jmcneill hdafg_assoc_dump_dd(struct hdafg_softc *sc, struct hdaudio_assoc *as, int pin,
    840  1.1  jmcneill 	int lock)
    841  1.1  jmcneill {
    842  1.1  jmcneill 	struct hdafg_dd_info hdi;
    843  1.1  jmcneill 	struct hdaudio_widget *w;
    844  1.1  jmcneill 	uint8_t elddata[256];
    845  1.1  jmcneill 	unsigned int elddatalen = 0, i;
    846  1.1  jmcneill 	uint32_t res;
    847  1.1  jmcneill 	uint32_t (*cmd)(struct hdaudio_codec *, int, uint32_t, uint32_t) =
    848  1.1  jmcneill 	    lock ? hdaudio_command : hdaudio_command_unlocked;
    849  1.1  jmcneill 
    850  1.1  jmcneill 	w = hdafg_widget_lookup(sc, as->as_pins[pin]);
    851  1.1  jmcneill 
    852  1.1  jmcneill 	if (w->w_pin.cap & COP_PINCAP_TRIGGER_REQD) {
    853  1.1  jmcneill 		(*cmd)(sc->sc_codec, as->as_pins[pin],
    854  1.1  jmcneill 		    CORB_SET_PIN_SENSE, 0);
    855  1.1  jmcneill 	}
    856  1.1  jmcneill 	res = (*cmd)(sc->sc_codec, as->as_pins[pin],
    857  1.1  jmcneill 	    CORB_GET_PIN_SENSE, 0);
    858  1.1  jmcneill 
    859  1.1  jmcneill #ifdef HDAFG_HDMI_DEBUG
    860  1.1  jmcneill 	hda_print(sc, "Display Device, pin=%02X\n", as->as_pins[pin]);
    861  1.1  jmcneill 	hda_print(sc, "  COP_GET_PIN_SENSE_PRESENSE_DETECT=%d\n",
    862  1.1  jmcneill 	    !!(res & COP_GET_PIN_SENSE_PRESENSE_DETECT));
    863  1.1  jmcneill 	hda_print(sc, "  COP_GET_PIN_SENSE_ELD_VALID=%d\n",
    864  1.1  jmcneill 	    !!(res & COP_GET_PIN_SENSE_ELD_VALID));
    865  1.1  jmcneill #endif
    866  1.1  jmcneill 
    867  1.1  jmcneill 	if ((res &
    868  1.1  jmcneill 	    (COP_GET_PIN_SENSE_PRESENSE_DETECT|COP_GET_PIN_SENSE_ELD_VALID)) ==
    869  1.1  jmcneill 	    (COP_GET_PIN_SENSE_PRESENSE_DETECT|COP_GET_PIN_SENSE_ELD_VALID)) {
    870  1.1  jmcneill 		res = (*cmd)(sc->sc_codec, as->as_pins[pin],
    871  1.1  jmcneill 		    CORB_GET_HDMI_DIP_SIZE, COP_DIP_ELD_SIZE);
    872  1.1  jmcneill 		elddatalen = COP_DIP_BUFFER_SIZE(res);
    873  1.1  jmcneill 		if (elddatalen == 0)
    874  1.1  jmcneill 			elddatalen = sizeof(elddata); /* paranoid */
    875  1.1  jmcneill 		for (i = 0; i < elddatalen; i++) {
    876  1.1  jmcneill 			res = (*cmd)(sc->sc_codec, as->as_pins[pin],
    877  1.1  jmcneill 			    CORB_GET_HDMI_ELD_DATA, i);
    878  1.1  jmcneill 			if (!(res & COP_ELD_VALID)) {
    879  1.1  jmcneill 				hda_error(sc, "bad ELD size (%u/%u)\n",
    880  1.1  jmcneill 				    i, elddatalen);
    881  1.1  jmcneill 				break;
    882  1.1  jmcneill 			}
    883  1.1  jmcneill 			elddata[i] = COP_ELD_DATA(res);
    884  1.1  jmcneill 		}
    885  1.1  jmcneill 
    886  1.1  jmcneill 		if (hdafg_dd_parse_info(elddata, elddatalen, &hdi) != 0) {
    887  1.1  jmcneill 			hda_error(sc, "failed to parse ELD data\n");
    888  1.1  jmcneill 			return;
    889  1.1  jmcneill 		}
    890  1.1  jmcneill 
    891  1.1  jmcneill 		hda_print(sc, "  ELD version=0x%x", ELD_VER(&hdi.eld));
    892  1.1  jmcneill 		hda_print1(sc, ",len=%u", hdi.eld.header.baseline_eld_len * 4);
    893  1.1  jmcneill 		hda_print1(sc, ",edid=0x%x", ELD_CEA_EDID_VER(&hdi.eld));
    894  1.1  jmcneill 		hda_print1(sc, ",port=0x%" PRIx64, hdi.eld.port_id);
    895  1.1  jmcneill 		hda_print1(sc, ",vendor=0x%04x", hdi.eld.vendor);
    896  1.1  jmcneill 		hda_print1(sc, ",product=0x%04x", hdi.eld.product);
    897  1.1  jmcneill 		hda_print1(sc, "\n");
    898  1.1  jmcneill 		hda_print(sc, "  Monitor = '%s'\n", hdi.monitor);
    899  1.1  jmcneill 		for (i = 0; i < hdi.nsad; i++) {
    900  1.1  jmcneill 			hda_print(sc, "  SAD id=%u", i);
    901  1.1  jmcneill 			hda_print1(sc, ",format=%u",
    902  1.1  jmcneill 			    CEA_AUDIO_FORMAT(&hdi.sad[i]));
    903  1.1  jmcneill 			hda_print1(sc, ",channels=%u",
    904  1.1  jmcneill 			    CEA_MAX_CHANNELS(&hdi.sad[i]));
    905  1.1  jmcneill 			hda_print1(sc, ",rate=0x%02x",
    906  1.1  jmcneill 			    CEA_SAMPLE_RATE(&hdi.sad[i]));
    907  1.1  jmcneill 			if (CEA_AUDIO_FORMAT(&hdi.sad[i]) ==
    908  1.1  jmcneill 			    CEA_AUDIO_FORMAT_LPCM)
    909  1.1  jmcneill 				hda_print1(sc, ",precision=0x%x",
    910  1.1  jmcneill 				    CEA_PRECISION(&hdi.sad[i]));
    911  1.1  jmcneill 			else
    912  1.1  jmcneill 				hda_print1(sc, ",maxbitrate=%u",
    913  1.1  jmcneill 				    CEA_MAX_BITRATE(&hdi.sad[i]));
    914  1.1  jmcneill 			hda_print1(sc, "\n");
    915  1.1  jmcneill 		}
    916  1.1  jmcneill 	}
    917  1.1  jmcneill }
    918  1.1  jmcneill 
    919  1.1  jmcneill static char *
    920  1.1  jmcneill hdafg_mixer_mask2allname(uint32_t mask, char *buf, size_t len)
    921  1.1  jmcneill {
    922  1.1  jmcneill 	static const char *audioname[] = HDAUDIO_DEVICE_NAMES;
    923  1.1  jmcneill 	int i, first = 1;
    924  1.1  jmcneill 
    925  1.1  jmcneill 	memset(buf, 0, len);
    926  1.1  jmcneill 	for (i = 0; i < HDAUDIO_MIXER_NRDEVICES; i++) {
    927  1.1  jmcneill 		if (mask & (1 << i)) {
    928  1.1  jmcneill 			if (first == 0)
    929  1.1  jmcneill 				strlcat(buf, ", ", len);
    930  1.1  jmcneill 			strlcat(buf, audioname[i], len);
    931  1.1  jmcneill 			first = 0;
    932  1.1  jmcneill 		}
    933  1.1  jmcneill 	}
    934  1.1  jmcneill 
    935  1.1  jmcneill 	return buf;
    936  1.1  jmcneill }
    937  1.1  jmcneill 
    938  1.1  jmcneill static void
    939  1.1  jmcneill hdafg_dump_dst_nid(struct hdafg_softc *sc, int nid, int depth)
    940  1.1  jmcneill {
    941  1.1  jmcneill 	struct hdaudio_widget *w, *cw;
    942  1.1  jmcneill 	char buf[64];
    943  1.1  jmcneill 	int i;
    944  1.1  jmcneill 
    945  1.1  jmcneill 	if (depth > HDAUDIO_PARSE_MAXDEPTH)
    946  1.1  jmcneill 		return;
    947  1.1  jmcneill 
    948  1.1  jmcneill 	w = hdafg_widget_lookup(sc, nid);
    949  1.1  jmcneill 	if (w == NULL || w->w_enable == false)
    950  1.1  jmcneill 		return;
    951  1.1  jmcneill 
    952  1.1  jmcneill 	aprint_debug("%*s", 4 + depth * 7, "");
    953  1.1  jmcneill 	aprint_debug("nid=%02X [%s]", w->w_nid, w->w_name);
    954  1.1  jmcneill 
    955  1.1  jmcneill 	if (depth > 0) {
    956  1.1  jmcneill 		if (w->w_audiomask == 0) {
    957  1.1  jmcneill 			aprint_debug("\n");
    958  1.1  jmcneill 			return;
    959  1.1  jmcneill 		}
    960  1.1  jmcneill 		aprint_debug(" [source: %s]",
    961  1.1  jmcneill 		    hdafg_mixer_mask2allname(w->w_audiomask, buf, sizeof(buf)));
    962  1.1  jmcneill 		if (w->w_audiodev >= 0) {
    963  1.1  jmcneill 			aprint_debug("\n");
    964  1.1  jmcneill 			return;
    965  1.1  jmcneill 		}
    966  1.1  jmcneill 	}
    967  1.1  jmcneill 
    968  1.1  jmcneill 	aprint_debug("\n");
    969  1.1  jmcneill 
    970  1.1  jmcneill 	for (i = 0; i < w->w_nconns; i++) {
    971  1.1  jmcneill 		if (w->w_connsenable[i] == 0)
    972  1.1  jmcneill 			continue;
    973  1.1  jmcneill 		cw = hdafg_widget_lookup(sc, w->w_conns[i]);
    974  1.1  jmcneill 		if (cw == NULL || cw->w_enable == false || cw->w_bindas == -1)
    975  1.1  jmcneill 			continue;
    976  1.1  jmcneill 		hdafg_dump_dst_nid(sc, w->w_conns[i], depth + 1);
    977  1.1  jmcneill 	}
    978  1.1  jmcneill }
    979  1.1  jmcneill 
    980  1.1  jmcneill static void
    981  1.1  jmcneill hdafg_assoc_dump(struct hdafg_softc *sc)
    982  1.1  jmcneill {
    983  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
    984  1.1  jmcneill 	struct hdaudio_widget *w;
    985  1.1  jmcneill 	uint32_t conn, defdev, curdev, curport;
    986  1.1  jmcneill 	int maxassocs = sc->sc_nassocs;
    987  1.1  jmcneill 	int i, j;
    988  1.1  jmcneill 
    989  1.1  jmcneill 	for (i = 0; i < maxassocs; i++) {
    990  1.1  jmcneill 		uint32_t devmask = 0, portmask = 0;
    991  1.1  jmcneill 		bool firstdev = true;
    992  1.1  jmcneill 		int nchan;
    993  1.1  jmcneill 
    994  1.1  jmcneill 		if (as[i].as_enable == false)
    995  1.1  jmcneill 			continue;
    996  1.1  jmcneill 
    997  1.1  jmcneill 		hda_print(sc, "%s%02X",
    998  1.1  jmcneill 		    hdafg_assoc_type_string(&as[i]), i);
    999  1.1  jmcneill 
   1000  1.1  jmcneill 		nchan = hdafg_assoc_count_channels(sc, &as[i],
   1001  1.1  jmcneill 		    as[i].as_dir);
   1002  1.1  jmcneill 		hda_print1(sc, " %dch:", nchan);
   1003  1.1  jmcneill 
   1004  1.1  jmcneill 		for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   1005  1.1  jmcneill 			if (as[i].as_dacs[j] == 0)
   1006  1.1  jmcneill 				continue;
   1007  1.1  jmcneill 			w = hdafg_widget_lookup(sc, as[i].as_pins[j]);
   1008  1.1  jmcneill 			if (w == NULL)
   1009  1.1  jmcneill 				continue;
   1010  1.1  jmcneill 			conn = COP_CFG_PORT_CONNECTIVITY(w->w_pin.config);
   1011  1.1  jmcneill 			defdev = COP_CFG_DEFAULT_DEVICE(w->w_pin.config);
   1012  1.1  jmcneill 			if (conn != COP_PORT_NONE) {
   1013  1.1  jmcneill 				devmask |= (1 << defdev);
   1014  1.1  jmcneill 				portmask |= (1 << conn);
   1015  1.1  jmcneill 			}
   1016  1.1  jmcneill 		}
   1017  1.1  jmcneill 		for (curdev = 0; curdev < 16; curdev++) {
   1018  1.1  jmcneill 			bool firstport = true;
   1019  1.1  jmcneill 			if ((devmask & (1 << curdev)) == 0)
   1020  1.1  jmcneill 				continue;
   1021  1.1  jmcneill 
   1022  1.1  jmcneill 			if (firstdev == false)
   1023  1.1  jmcneill 				hda_print1(sc, ",");
   1024  1.1  jmcneill 			firstdev = false;
   1025  1.1  jmcneill 			hda_print1(sc, " %s",
   1026  1.1  jmcneill 			    hdafg_default_device[curdev]);
   1027  1.1  jmcneill 
   1028  1.1  jmcneill 			for (curport = 0; curport < 4; curport++) {
   1029  1.1  jmcneill 				bool devonport = false;
   1030  1.1  jmcneill 				if ((portmask & (1 << curport)) == 0)
   1031  1.1  jmcneill 					continue;
   1032  1.1  jmcneill 
   1033  1.1  jmcneill 				for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   1034  1.1  jmcneill 					if (as[i].as_dacs[j] == 0)
   1035  1.1  jmcneill 						continue;
   1036  1.1  jmcneill 
   1037  1.1  jmcneill 					w = hdafg_widget_lookup(sc,
   1038  1.1  jmcneill 					    as[i].as_pins[j]);
   1039  1.1  jmcneill 					if (w == NULL)
   1040  1.1  jmcneill 						continue;
   1041  1.1  jmcneill 					conn = COP_CFG_PORT_CONNECTIVITY(w->w_pin.config);
   1042  1.1  jmcneill 					defdev = COP_CFG_DEFAULT_DEVICE(w->w_pin.config);
   1043  1.1  jmcneill 					if (conn != curport || defdev != curdev)
   1044  1.1  jmcneill 						continue;
   1045  1.1  jmcneill 
   1046  1.1  jmcneill 					devonport = true;
   1047  1.1  jmcneill 				}
   1048  1.1  jmcneill 
   1049  1.1  jmcneill 				if (devonport == false)
   1050  1.1  jmcneill 					continue;
   1051  1.1  jmcneill 
   1052  1.1  jmcneill 				hda_print1(sc, " [%s",
   1053  1.1  jmcneill 				    hdafg_port_connectivity[curport]);
   1054  1.1  jmcneill 				for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   1055  1.1  jmcneill 					if (as[i].as_dacs[j] == 0)
   1056  1.1  jmcneill 						continue;
   1057  1.1  jmcneill 
   1058  1.1  jmcneill 					w = hdafg_widget_lookup(sc,
   1059  1.1  jmcneill 					    as[i].as_pins[j]);
   1060  1.1  jmcneill 					if (w == NULL)
   1061  1.1  jmcneill 						continue;
   1062  1.1  jmcneill 					conn = COP_CFG_PORT_CONNECTIVITY(w->w_pin.config);
   1063  1.1  jmcneill 					defdev = COP_CFG_DEFAULT_DEVICE(w->w_pin.config);
   1064  1.1  jmcneill 					if (conn != curport || defdev != curdev)
   1065  1.1  jmcneill 						continue;
   1066  1.1  jmcneill 
   1067  1.1  jmcneill 					if (firstport == false)
   1068  1.1  jmcneill 						hda_trace1(sc, ",");
   1069  1.1  jmcneill 					else
   1070  1.1  jmcneill 						hda_trace1(sc, " ");
   1071  1.1  jmcneill 					firstport = false;
   1072  1.1  jmcneill #ifdef HDAUDIO_DEBUG
   1073  1.1  jmcneill 					int color =
   1074  1.1  jmcneill 					    COP_CFG_COLOR(w->w_pin.config);
   1075  1.1  jmcneill 					hda_trace1(sc, "%s",
   1076  1.1  jmcneill 					    hdafg_color[color]);
   1077  1.1  jmcneill #endif
   1078  1.1  jmcneill 					hda_trace1(sc, "(%02X)", w->w_nid);
   1079  1.1  jmcneill 				}
   1080  1.1  jmcneill 				hda_print1(sc, "]");
   1081  1.1  jmcneill 			}
   1082  1.1  jmcneill 		}
   1083  1.1  jmcneill 		hda_print1(sc, "\n");
   1084  1.1  jmcneill 
   1085  1.1  jmcneill 		for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   1086  1.1  jmcneill 			if (as[i].as_pins[j] == 0)
   1087  1.1  jmcneill 				continue;
   1088  1.1  jmcneill 			hdafg_dump_dst_nid(sc, as[i].as_pins[j], 0);
   1089  1.1  jmcneill 		}
   1090  1.1  jmcneill 
   1091  1.1  jmcneill 		if (as[i].as_displaydev == true) {
   1092  1.1  jmcneill 			for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   1093  1.1  jmcneill 				if (as[i].as_pins[j] == 0)
   1094  1.1  jmcneill 					continue;
   1095  1.1  jmcneill 				hdafg_assoc_dump_dd(sc, &as[i], j, 1);
   1096  1.1  jmcneill 			}
   1097  1.1  jmcneill 		}
   1098  1.1  jmcneill 	}
   1099  1.1  jmcneill }
   1100  1.1  jmcneill 
   1101  1.1  jmcneill static void
   1102  1.1  jmcneill hdafg_assoc_parse(struct hdafg_softc *sc)
   1103  1.1  jmcneill {
   1104  1.1  jmcneill 	struct hdaudio_assoc *as;
   1105  1.1  jmcneill 	struct hdaudio_widget *w;
   1106  1.1  jmcneill 	int i, j, cnt, maxassocs, type, assoc, seq, first, hpredir;
   1107  1.1  jmcneill 	enum hdaudio_pindir dir;
   1108  1.1  jmcneill 
   1109  1.1  jmcneill 	hda_debug(sc, "  count present associations\n");
   1110  1.1  jmcneill 	/* Count present associations */
   1111  1.1  jmcneill 	maxassocs = 0;
   1112  1.1  jmcneill 	for (j = 1; j < HDAUDIO_MAXPINS; j++) {
   1113  1.1  jmcneill 		for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   1114  1.1  jmcneill 			w = hdafg_widget_lookup(sc, i);
   1115  1.1  jmcneill 			if (w == NULL || w->w_enable == false)
   1116  1.1  jmcneill 				continue;
   1117  1.1  jmcneill 			if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   1118  1.1  jmcneill 				continue;
   1119  1.1  jmcneill 			if (COP_CFG_DEFAULT_ASSOCIATION(w->w_pin.config) != j)
   1120  1.1  jmcneill 				continue;
   1121  1.1  jmcneill 			maxassocs++;
   1122  1.1  jmcneill 			if (j != 15) /* There could be many 1-pin assocs #15 */
   1123  1.1  jmcneill 				break;
   1124  1.1  jmcneill 		}
   1125  1.1  jmcneill 	}
   1126  1.1  jmcneill 
   1127  1.1  jmcneill 	hda_debug(sc, "  maxassocs %d\n", maxassocs);
   1128  1.1  jmcneill 	sc->sc_nassocs = maxassocs;
   1129  1.1  jmcneill 
   1130  1.1  jmcneill 	if (maxassocs < 1)
   1131  1.1  jmcneill 		return;
   1132  1.1  jmcneill 
   1133  1.1  jmcneill 	hda_debug(sc, "  allocating memory\n");
   1134  1.1  jmcneill 	as = kmem_zalloc(maxassocs * sizeof(*as), KM_SLEEP);
   1135  1.1  jmcneill 	for (i = 0; i < maxassocs; i++) {
   1136  1.1  jmcneill 		as[i].as_hpredir = -1;
   1137  1.1  jmcneill 		/* as[i].as_chan = NULL; */
   1138  1.1  jmcneill 		as[i].as_digital = HDAFG_AS_SPDIF;
   1139  1.1  jmcneill 	}
   1140  1.1  jmcneill 
   1141  1.1  jmcneill 	hda_debug(sc, "  scan associations, skipping as=0\n");
   1142  1.1  jmcneill 	/* Scan associations skipping as=0 */
   1143  1.1  jmcneill 	cnt = 0;
   1144  1.1  jmcneill 	for (j = 1; j < HDAUDIO_MAXPINS && cnt < maxassocs; j++) {
   1145  1.1  jmcneill 		first = 16;
   1146  1.1  jmcneill 		hpredir = 0;
   1147  1.1  jmcneill 		for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   1148  1.1  jmcneill 			w = hdafg_widget_lookup(sc, i);
   1149  1.1  jmcneill 			if (w == NULL || w->w_enable == false)
   1150  1.1  jmcneill 				continue;
   1151  1.1  jmcneill 			if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   1152  1.1  jmcneill 				continue;
   1153  1.1  jmcneill 			assoc = COP_CFG_DEFAULT_ASSOCIATION(w->w_pin.config);
   1154  1.1  jmcneill 			seq = COP_CFG_SEQUENCE(w->w_pin.config);
   1155  1.1  jmcneill 			if (assoc != j)
   1156  1.1  jmcneill 				continue;
   1157  1.1  jmcneill 			KASSERT(cnt < maxassocs);
   1158  1.1  jmcneill 			type = COP_CFG_DEFAULT_DEVICE(w->w_pin.config);
   1159  1.1  jmcneill 			/* Get pin direction */
   1160  1.1  jmcneill 			switch (type) {
   1161  1.1  jmcneill 			case COP_DEVICE_LINE_OUT:
   1162  1.1  jmcneill 			case COP_DEVICE_SPEAKER:
   1163  1.1  jmcneill 			case COP_DEVICE_HP_OUT:
   1164  1.1  jmcneill 			case COP_DEVICE_SPDIF_OUT:
   1165  1.1  jmcneill 			case COP_DEVICE_DIGITAL_OTHER_OUT:
   1166  1.1  jmcneill 				dir = HDAUDIO_PINDIR_OUT;
   1167  1.1  jmcneill 				break;
   1168  1.1  jmcneill 			default:
   1169  1.1  jmcneill 				dir = HDAUDIO_PINDIR_IN;
   1170  1.1  jmcneill 				break;
   1171  1.1  jmcneill 			}
   1172  1.1  jmcneill 			/* If this is a first pin, create new association */
   1173  1.1  jmcneill 			if (as[cnt].as_pincnt == 0) {
   1174  1.1  jmcneill 				as[cnt].as_enable = true;
   1175  1.1  jmcneill 				as[cnt].as_activated = true;
   1176  1.1  jmcneill 				as[cnt].as_index = j;
   1177  1.1  jmcneill 				as[cnt].as_dir = dir;
   1178  1.1  jmcneill 			}
   1179  1.1  jmcneill 			if (seq < first)
   1180  1.1  jmcneill 				first = seq;
   1181  1.1  jmcneill 			/* Check association correctness */
   1182  1.1  jmcneill 			if (as[cnt].as_pins[seq] != 0) {
   1183  1.1  jmcneill 				hda_error(sc, "duplicate pin in association\n");
   1184  1.1  jmcneill 				as[cnt].as_enable = false;
   1185  1.1  jmcneill 			}
   1186  1.1  jmcneill 			if (dir != as[cnt].as_dir) {
   1187  1.1  jmcneill 				hda_error(sc,
   1188  1.1  jmcneill 				    "pin %02X has wrong direction for %02X\n",
   1189  1.1  jmcneill 				    w->w_nid, j);
   1190  1.1  jmcneill 				as[cnt].as_enable = false;
   1191  1.1  jmcneill 			}
   1192  1.1  jmcneill 			if ((w->w_p.aw_cap & COP_AWCAP_DIGITAL) == 0)
   1193  1.1  jmcneill 				as[cnt].as_digital = HDAFG_AS_ANALOG;
   1194  1.1  jmcneill 			if (w->w_pin.cap & (COP_PINCAP_HDMI|COP_PINCAP_DP))
   1195  1.1  jmcneill 				as[cnt].as_displaydev = true;
   1196  1.1  jmcneill 			if (w->w_pin.cap & COP_PINCAP_HDMI)
   1197  1.1  jmcneill 				as[cnt].as_digital = HDAFG_AS_HDMI;
   1198  1.1  jmcneill 			if (w->w_pin.cap & COP_PINCAP_DP)
   1199  1.1  jmcneill 				as[cnt].as_digital = HDAFG_AS_DISPLAYPORT;
   1200  1.1  jmcneill 			/* Headphones with seq=15 may mean redirection */
   1201  1.1  jmcneill 			if (type == COP_DEVICE_HP_OUT && seq == 15)
   1202  1.1  jmcneill 				hpredir = 1;
   1203  1.1  jmcneill 			as[cnt].as_pins[seq] = w->w_nid;
   1204  1.1  jmcneill 			as[cnt].as_pincnt++;
   1205  1.1  jmcneill 			if (j == 15)
   1206  1.1  jmcneill 				cnt++;
   1207  1.1  jmcneill 		}
   1208  1.1  jmcneill 		if (j != 15 && cnt < maxassocs && as[cnt].as_pincnt > 0) {
   1209  1.1  jmcneill 			if (hpredir && as[cnt].as_pincnt > 1)
   1210  1.1  jmcneill 				as[cnt].as_hpredir = first;
   1211  1.1  jmcneill 			cnt++;
   1212  1.1  jmcneill 		}
   1213  1.1  jmcneill 	}
   1214  1.1  jmcneill 
   1215  1.1  jmcneill 	hda_debug(sc, "  all done\n");
   1216  1.1  jmcneill 	sc->sc_assocs = as;
   1217  1.1  jmcneill }
   1218  1.1  jmcneill 
   1219  1.1  jmcneill static void
   1220  1.1  jmcneill hdafg_control_parse(struct hdafg_softc *sc)
   1221  1.1  jmcneill {
   1222  1.1  jmcneill 	struct hdaudio_control *ctl;
   1223  1.1  jmcneill 	struct hdaudio_widget *w, *cw;
   1224  1.1  jmcneill 	int i, j, cnt, maxctls, ocap, icap;
   1225  1.1  jmcneill 	int mute, offset, step, size;
   1226  1.1  jmcneill 
   1227  1.1  jmcneill 	maxctls = 0;
   1228  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   1229  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   1230  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   1231  1.1  jmcneill 			continue;
   1232  1.1  jmcneill 		if (w->w_p.outamp_cap)
   1233  1.1  jmcneill 			maxctls++;
   1234  1.1  jmcneill 		if (w->w_p.inamp_cap) {
   1235  1.1  jmcneill 			switch (w->w_type) {
   1236  1.1  jmcneill 			case COP_AWCAP_TYPE_AUDIO_SELECTOR:
   1237  1.1  jmcneill 			case COP_AWCAP_TYPE_AUDIO_MIXER:
   1238  1.1  jmcneill 				for (j = 0; j < w->w_nconns; j++) {
   1239  1.1  jmcneill 					cw = hdafg_widget_lookup(sc,
   1240  1.1  jmcneill 					    w->w_conns[j]);
   1241  1.1  jmcneill 					if (cw == NULL || cw->w_enable == false)
   1242  1.1  jmcneill 						continue;
   1243  1.1  jmcneill 					maxctls++;
   1244  1.1  jmcneill 				}
   1245  1.1  jmcneill 				break;
   1246  1.1  jmcneill 			default:
   1247  1.1  jmcneill 				maxctls++;
   1248  1.1  jmcneill 				break;
   1249  1.1  jmcneill 			}
   1250  1.1  jmcneill 		}
   1251  1.1  jmcneill 	}
   1252  1.1  jmcneill 
   1253  1.1  jmcneill 	sc->sc_nctls = maxctls;
   1254  1.1  jmcneill 	if (maxctls < 1)
   1255  1.1  jmcneill 		return;
   1256  1.1  jmcneill 
   1257  1.1  jmcneill 	ctl = kmem_zalloc(sc->sc_nctls * sizeof(*ctl), KM_SLEEP);
   1258  1.1  jmcneill 
   1259  1.1  jmcneill 	cnt = 0;
   1260  1.1  jmcneill 	for (i = sc->sc_startnode; cnt < maxctls && i < sc->sc_endnode; i++) {
   1261  1.1  jmcneill 		if (cnt >= maxctls) {
   1262  1.1  jmcneill 			hda_error(sc, "ctl overflow\n");
   1263  1.1  jmcneill 			break;
   1264  1.1  jmcneill 		}
   1265  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   1266  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   1267  1.1  jmcneill 			continue;
   1268  1.1  jmcneill 		ocap = w->w_p.outamp_cap;
   1269  1.1  jmcneill 		icap = w->w_p.inamp_cap;
   1270  1.1  jmcneill 		if (ocap) {
   1271  1.1  jmcneill 			hda_trace(sc, "add ctrl outamp %d:%02X:FF\n",
   1272  1.1  jmcneill 			    cnt, w->w_nid);
   1273  1.1  jmcneill 			mute = COP_AMPCAP_MUTE_CAPABLE(ocap);
   1274  1.1  jmcneill 			step = COP_AMPCAP_NUM_STEPS(ocap);
   1275  1.1  jmcneill 			size = COP_AMPCAP_STEP_SIZE(ocap);
   1276  1.1  jmcneill 			offset = COP_AMPCAP_OFFSET(ocap);
   1277  1.1  jmcneill 			ctl[cnt].ctl_enable = true;
   1278  1.1  jmcneill 			ctl[cnt].ctl_widget = w;
   1279  1.1  jmcneill 			ctl[cnt].ctl_mute = mute;
   1280  1.1  jmcneill 			ctl[cnt].ctl_step = step;
   1281  1.1  jmcneill 			ctl[cnt].ctl_size = size;
   1282  1.1  jmcneill 			ctl[cnt].ctl_offset = offset;
   1283  1.1  jmcneill 			ctl[cnt].ctl_left = offset;
   1284  1.1  jmcneill 			ctl[cnt].ctl_right = offset;
   1285  1.1  jmcneill 			if (w->w_type == COP_AWCAP_TYPE_PIN_COMPLEX ||
   1286  1.1  jmcneill 			    w->w_waspin == true)
   1287  1.1  jmcneill 				ctl[cnt].ctl_ndir = HDAUDIO_PINDIR_IN;
   1288  1.1  jmcneill 			else
   1289  1.1  jmcneill 				ctl[cnt].ctl_ndir = HDAUDIO_PINDIR_OUT;
   1290  1.1  jmcneill 			ctl[cnt++].ctl_dir = HDAUDIO_PINDIR_OUT;
   1291  1.1  jmcneill 		}
   1292  1.1  jmcneill 		if (icap) {
   1293  1.1  jmcneill 			mute = COP_AMPCAP_MUTE_CAPABLE(icap);
   1294  1.1  jmcneill 			step = COP_AMPCAP_NUM_STEPS(icap);
   1295  1.1  jmcneill 			size = COP_AMPCAP_STEP_SIZE(icap);
   1296  1.1  jmcneill 			offset = COP_AMPCAP_OFFSET(icap);
   1297  1.1  jmcneill 			switch (w->w_type) {
   1298  1.1  jmcneill 			case COP_AWCAP_TYPE_AUDIO_SELECTOR:
   1299  1.1  jmcneill 			case COP_AWCAP_TYPE_AUDIO_MIXER:
   1300  1.1  jmcneill 				for (j = 0; j < w->w_nconns; j++) {
   1301  1.1  jmcneill 					if (cnt >= maxctls)
   1302  1.1  jmcneill 						break;
   1303  1.1  jmcneill 					cw = hdafg_widget_lookup(sc,
   1304  1.1  jmcneill 					    w->w_conns[j]);
   1305  1.1  jmcneill 					if (cw == NULL || cw->w_enable == false)
   1306  1.1  jmcneill 						continue;
   1307  1.1  jmcneill 					hda_trace(sc, "add ctrl inamp selmix "
   1308  1.1  jmcneill 					    "%d:%02X:%02X\n", cnt, w->w_nid,
   1309  1.1  jmcneill 					    cw->w_nid);
   1310  1.1  jmcneill 					ctl[cnt].ctl_enable = true;
   1311  1.1  jmcneill 					ctl[cnt].ctl_widget = w;
   1312  1.1  jmcneill 					ctl[cnt].ctl_childwidget = cw;
   1313  1.1  jmcneill 					ctl[cnt].ctl_index = j;
   1314  1.1  jmcneill 					ctl[cnt].ctl_mute = mute;
   1315  1.1  jmcneill 					ctl[cnt].ctl_step = step;
   1316  1.1  jmcneill 					ctl[cnt].ctl_size = size;
   1317  1.1  jmcneill 					ctl[cnt].ctl_offset = offset;
   1318  1.1  jmcneill 					ctl[cnt].ctl_left = offset;
   1319  1.1  jmcneill 					ctl[cnt].ctl_right = offset;
   1320  1.1  jmcneill 					ctl[cnt].ctl_ndir = HDAUDIO_PINDIR_IN;
   1321  1.1  jmcneill 					ctl[cnt++].ctl_dir = HDAUDIO_PINDIR_IN;
   1322  1.1  jmcneill 				}
   1323  1.1  jmcneill 				break;
   1324  1.1  jmcneill 			default:
   1325  1.1  jmcneill 				if (cnt >= maxctls)
   1326  1.1  jmcneill 					break;
   1327  1.1  jmcneill 				hda_trace(sc, "add ctrl inamp "
   1328  1.1  jmcneill 				    "%d:%02X:FF\n", cnt, w->w_nid);
   1329  1.1  jmcneill 				ctl[cnt].ctl_enable = true;
   1330  1.1  jmcneill 				ctl[cnt].ctl_widget = w;
   1331  1.1  jmcneill 				ctl[cnt].ctl_mute = mute;
   1332  1.1  jmcneill 				ctl[cnt].ctl_step = step;
   1333  1.1  jmcneill 				ctl[cnt].ctl_size = size;
   1334  1.1  jmcneill 				ctl[cnt].ctl_offset = offset;
   1335  1.1  jmcneill 				ctl[cnt].ctl_left = offset;
   1336  1.1  jmcneill 				ctl[cnt].ctl_right = offset;
   1337  1.1  jmcneill 				if (w->w_type == COP_AWCAP_TYPE_PIN_COMPLEX)
   1338  1.1  jmcneill 					ctl[cnt].ctl_ndir = HDAUDIO_PINDIR_OUT;
   1339  1.1  jmcneill 				else
   1340  1.1  jmcneill 					ctl[cnt].ctl_ndir = HDAUDIO_PINDIR_IN;
   1341  1.1  jmcneill 				ctl[cnt++].ctl_dir = HDAUDIO_PINDIR_IN;
   1342  1.1  jmcneill 				break;
   1343  1.1  jmcneill 			}
   1344  1.1  jmcneill 		}
   1345  1.1  jmcneill 	}
   1346  1.1  jmcneill 
   1347  1.1  jmcneill 	sc->sc_ctls = ctl;
   1348  1.1  jmcneill }
   1349  1.1  jmcneill 
   1350  1.1  jmcneill static void
   1351  1.1  jmcneill hdafg_parse(struct hdafg_softc *sc)
   1352  1.1  jmcneill {
   1353  1.1  jmcneill 	struct hdaudio_widget *w;
   1354  1.1  jmcneill 	uint32_t nodecnt, wcap;
   1355  1.1  jmcneill 	int nid;
   1356  1.1  jmcneill 
   1357  1.1  jmcneill 	nodecnt = hda_get_param(sc, SUBORDINATE_NODE_COUNT);
   1358  1.1  jmcneill 	sc->sc_startnode = COP_NODECNT_STARTNODE(nodecnt);
   1359  1.1  jmcneill 	sc->sc_nwidgets = COP_NODECNT_NUMNODES(nodecnt);
   1360  1.1  jmcneill 	sc->sc_endnode = sc->sc_startnode + sc->sc_nwidgets;
   1361  1.1  jmcneill 	hda_debug(sc, "afg start %02X end %02X nwidgets %d\n",
   1362  1.1  jmcneill 	    sc->sc_startnode, sc->sc_endnode, sc->sc_nwidgets);
   1363  1.1  jmcneill 
   1364  1.1  jmcneill 	hda_debug(sc, "powering up widgets\n");
   1365  1.1  jmcneill 	hdaudio_command(sc->sc_codec, sc->sc_nid,
   1366  1.1  jmcneill 	    CORB_SET_POWER_STATE, COP_POWER_STATE_D0);
   1367  1.1  jmcneill 	hda_delay(100);
   1368  1.1  jmcneill 	for (nid = sc->sc_startnode; nid < sc->sc_endnode; nid++)
   1369  1.1  jmcneill 		hdaudio_command(sc->sc_codec, nid,
   1370  1.1  jmcneill 		    CORB_SET_POWER_STATE, COP_POWER_STATE_D0);
   1371  1.1  jmcneill 	hda_delay(1000);
   1372  1.1  jmcneill 
   1373  1.1  jmcneill 	sc->sc_p.afg_cap = hda_get_param(sc, AUDIO_FUNCTION_GROUP_CAPABILITIES);
   1374  1.1  jmcneill 	sc->sc_p.stream_format = hda_get_param(sc, SUPPORTED_STREAM_FORMATS);
   1375  1.1  jmcneill 	sc->sc_p.pcm_size_rate = hda_get_param(sc, SUPPORTED_PCM_SIZE_RATES);
   1376  1.1  jmcneill 	sc->sc_p.outamp_cap = hda_get_param(sc, AMPLIFIER_CAPABILITIES_OUTAMP);
   1377  1.1  jmcneill 	sc->sc_p.inamp_cap = hda_get_param(sc, AMPLIFIER_CAPABILITIES_INAMP);
   1378  1.1  jmcneill 	sc->sc_p.power_states = hda_get_param(sc, SUPPORTED_POWER_STATES);
   1379  1.1  jmcneill 	sc->sc_p.gpio_cnt = hda_get_param(sc, GPIO_COUNT);
   1380  1.1  jmcneill 
   1381  1.1  jmcneill 	sc->sc_widgets = kmem_zalloc(sc->sc_nwidgets * sizeof(*w), KM_SLEEP);
   1382  1.1  jmcneill 	hda_debug(sc, "afg widgets %p-%p\n",
   1383  1.1  jmcneill 	    sc->sc_widgets, sc->sc_widgets + sc->sc_nwidgets);
   1384  1.1  jmcneill 
   1385  1.1  jmcneill 	for (nid = sc->sc_startnode; nid < sc->sc_endnode; nid++) {
   1386  1.1  jmcneill 		w = hdafg_widget_lookup(sc, nid);
   1387  1.1  jmcneill 		if (w == NULL)
   1388  1.1  jmcneill 			continue;
   1389  1.1  jmcneill 		wcap = hdaudio_command(sc->sc_codec, nid, CORB_GET_PARAMETER,
   1390  1.1  jmcneill 		    COP_AUDIO_WIDGET_CAPABILITIES);
   1391  1.1  jmcneill 		switch (COP_AWCAP_TYPE(wcap)) {
   1392  1.1  jmcneill 		case COP_AWCAP_TYPE_BEEP_GENERATOR:
   1393  1.1  jmcneill 			sc->sc_has_beepgen = true;
   1394  1.1  jmcneill 			break;
   1395  1.1  jmcneill 		}
   1396  1.1  jmcneill 	}
   1397  1.1  jmcneill 
   1398  1.1  jmcneill 	for (nid = sc->sc_startnode; nid < sc->sc_endnode; nid++) {
   1399  1.1  jmcneill 		w = hdafg_widget_lookup(sc, nid);
   1400  1.1  jmcneill 		if (w == NULL)
   1401  1.1  jmcneill 			continue;
   1402  1.1  jmcneill 		w->w_afg = sc;
   1403  1.1  jmcneill 		w->w_nid = nid;
   1404  1.1  jmcneill 		w->w_enable = true;
   1405  1.1  jmcneill 		w->w_pflags = 0;
   1406  1.1  jmcneill 		w->w_audiodev = -1;
   1407  1.1  jmcneill 		w->w_selconn = -1;
   1408  1.1  jmcneill 		w->w_bindas = -1;
   1409  1.1  jmcneill 		w->w_p.eapdbtl = 0xffffffff;
   1410  1.1  jmcneill 		hdafg_widget_parse(w);
   1411  1.1  jmcneill 	}
   1412  1.1  jmcneill }
   1413  1.1  jmcneill 
   1414  1.1  jmcneill static void
   1415  1.1  jmcneill hdafg_disable_nonaudio(struct hdafg_softc *sc)
   1416  1.1  jmcneill {
   1417  1.1  jmcneill 	struct hdaudio_widget *w;
   1418  1.1  jmcneill 	int i;
   1419  1.1  jmcneill 
   1420  1.1  jmcneill 	/* Disable power and volume widgets */
   1421  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   1422  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   1423  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   1424  1.1  jmcneill 			continue;
   1425  1.1  jmcneill 		if (w->w_type == COP_AWCAP_TYPE_POWER_WIDGET ||
   1426  1.1  jmcneill 		    w->w_type == COP_AWCAP_TYPE_VOLUME_KNOB) {
   1427  1.1  jmcneill 			hda_trace(w->w_afg, "disable %02X [nonaudio]\n",
   1428  1.1  jmcneill 			    w->w_nid);
   1429  1.1  jmcneill 		    	w->w_enable = false;
   1430  1.1  jmcneill 		}
   1431  1.1  jmcneill 	}
   1432  1.1  jmcneill }
   1433  1.1  jmcneill 
   1434  1.1  jmcneill static void
   1435  1.1  jmcneill hdafg_disable_useless(struct hdafg_softc *sc)
   1436  1.1  jmcneill {
   1437  1.1  jmcneill 	struct hdaudio_widget *w, *cw;
   1438  1.1  jmcneill 	struct hdaudio_control *ctl;
   1439  1.1  jmcneill 	int done, found, i, j, k;
   1440  1.1  jmcneill 	int conn, assoc;
   1441  1.1  jmcneill 
   1442  1.1  jmcneill 	/* Disable useless pins */
   1443  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   1444  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   1445  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   1446  1.1  jmcneill 			continue;
   1447  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   1448  1.1  jmcneill 			continue;
   1449  1.1  jmcneill 		conn = COP_CFG_PORT_CONNECTIVITY(w->w_pin.config);
   1450  1.1  jmcneill 		assoc = COP_CFG_DEFAULT_ASSOCIATION(w->w_pin.config);
   1451  1.1  jmcneill 		if (conn == COP_PORT_NONE) {
   1452  1.1  jmcneill 			hda_trace(w->w_afg, "disable %02X [no connectivity]\n",
   1453  1.1  jmcneill 			    w->w_nid);
   1454  1.1  jmcneill 			w->w_enable = false;
   1455  1.1  jmcneill 		}
   1456  1.1  jmcneill 		if (assoc == 0) {
   1457  1.1  jmcneill 			hda_trace(w->w_afg, "disable %02X [no association]\n",
   1458  1.1  jmcneill 			    w->w_nid);
   1459  1.1  jmcneill 			w->w_enable = false;
   1460  1.1  jmcneill 		}
   1461  1.1  jmcneill 	}
   1462  1.1  jmcneill 
   1463  1.1  jmcneill 	do {
   1464  1.1  jmcneill 		done = 1;
   1465  1.1  jmcneill 		/* Disable and mute controls for disabled widgets */
   1466  1.1  jmcneill 		i = 0;
   1467  1.1  jmcneill 		for (i = 0; i < sc->sc_nctls; i++) {
   1468  1.1  jmcneill 			ctl = &sc->sc_ctls[i];
   1469  1.1  jmcneill 			if (ctl->ctl_enable == false)
   1470  1.1  jmcneill 				continue;
   1471  1.1  jmcneill 			if (ctl->ctl_widget->w_enable == false ||
   1472  1.1  jmcneill 			    (ctl->ctl_childwidget != NULL &&
   1473  1.1  jmcneill 			     ctl->ctl_childwidget->w_enable == false)) {
   1474  1.1  jmcneill 				ctl->ctl_forcemute = 1;
   1475  1.1  jmcneill 				ctl->ctl_muted = HDAUDIO_AMP_MUTE_ALL;
   1476  1.1  jmcneill 				ctl->ctl_left = ctl->ctl_right = 0;
   1477  1.1  jmcneill 				ctl->ctl_enable = false;
   1478  1.1  jmcneill 				if (ctl->ctl_ndir == HDAUDIO_PINDIR_IN)
   1479  1.1  jmcneill 					ctl->ctl_widget->w_connsenable[
   1480  1.1  jmcneill 					    ctl->ctl_index] = false;
   1481  1.1  jmcneill 				done = 0;
   1482  1.1  jmcneill 				hda_trace(ctl->ctl_widget->w_afg,
   1483  1.1  jmcneill 				    "disable ctl %d:%02X:%02X [widget disabled]\n",
   1484  1.1  jmcneill 				    i, ctl->ctl_widget->w_nid,
   1485  1.1  jmcneill 				    ctl->ctl_childwidget ?
   1486  1.1  jmcneill 				    ctl->ctl_childwidget->w_nid : 0xff);
   1487  1.1  jmcneill 			}
   1488  1.1  jmcneill 		}
   1489  1.1  jmcneill 		/* Disable useless widgets */
   1490  1.1  jmcneill 		for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   1491  1.1  jmcneill 			w = hdafg_widget_lookup(sc, i);
   1492  1.1  jmcneill 			if (w == NULL || w->w_enable == false)
   1493  1.1  jmcneill 				continue;
   1494  1.1  jmcneill 			/* Disable inputs with disabled child widgets */
   1495  1.1  jmcneill 			for (j = 0; j < w->w_nconns; j++) {
   1496  1.1  jmcneill 				if (!w->w_connsenable[j])
   1497  1.1  jmcneill 					continue;
   1498  1.1  jmcneill 				cw = hdafg_widget_lookup(sc,
   1499  1.1  jmcneill 				    w->w_conns[j]);
   1500  1.1  jmcneill 				if (cw == NULL || cw->w_enable == false) {
   1501  1.1  jmcneill 					w->w_connsenable[j] = false;
   1502  1.1  jmcneill 					hda_trace(w->w_afg,
   1503  1.1  jmcneill 					    "disable conn %02X->%02X "
   1504  1.1  jmcneill 					    "[disabled child]\n",
   1505  1.1  jmcneill 					    w->w_nid, w->w_conns[j]);
   1506  1.1  jmcneill 				}
   1507  1.1  jmcneill 			}
   1508  1.1  jmcneill 			if (w->w_type != COP_AWCAP_TYPE_AUDIO_SELECTOR &&
   1509  1.1  jmcneill 			    w->w_type != COP_AWCAP_TYPE_AUDIO_MIXER)
   1510  1.1  jmcneill 				continue;
   1511  1.1  jmcneill 			/* Disable mixers and selectors without inputs */
   1512  1.1  jmcneill 			found = 0;
   1513  1.1  jmcneill 			for (j = 0; j < w->w_nconns; j++)
   1514  1.1  jmcneill 				if (w->w_connsenable[j]) {
   1515  1.1  jmcneill 					found = 1;
   1516  1.1  jmcneill 					break;
   1517  1.1  jmcneill 				}
   1518  1.1  jmcneill 			if (found == 0) {
   1519  1.1  jmcneill 				w->w_enable = false;
   1520  1.1  jmcneill 				done = 0;
   1521  1.1  jmcneill 				hda_trace(w->w_afg,
   1522  1.1  jmcneill 				    "disable %02X [inputs disabled]\n",
   1523  1.1  jmcneill 				    w->w_nid);
   1524  1.1  jmcneill 			}
   1525  1.1  jmcneill 			/* Disable nodes without consumers */
   1526  1.1  jmcneill 			if (w->w_type != COP_AWCAP_TYPE_AUDIO_SELECTOR &&
   1527  1.1  jmcneill 			    w->w_type != COP_AWCAP_TYPE_AUDIO_MIXER)
   1528  1.1  jmcneill 				continue;
   1529  1.1  jmcneill 			found = 0;
   1530  1.1  jmcneill 			for (k = sc->sc_startnode; k < sc->sc_endnode; k++) {
   1531  1.1  jmcneill 				cw = hdafg_widget_lookup(sc, k);
   1532  1.1  jmcneill 				if (cw == NULL || cw->w_enable == false)
   1533  1.1  jmcneill 					continue;
   1534  1.1  jmcneill 				for (j = 0; j < cw->w_nconns; j++) {
   1535  1.1  jmcneill 					if (cw->w_connsenable[j] &&
   1536  1.1  jmcneill 					    cw->w_conns[j] == i) {
   1537  1.1  jmcneill 						found = 1;
   1538  1.1  jmcneill 						break;
   1539  1.1  jmcneill 					}
   1540  1.1  jmcneill 				}
   1541  1.1  jmcneill 			}
   1542  1.1  jmcneill 			if (found == 0) {
   1543  1.1  jmcneill 				w->w_enable = false;
   1544  1.1  jmcneill 				done = 0;
   1545  1.1  jmcneill 				hda_trace(w->w_afg,
   1546  1.1  jmcneill 				    "disable %02X [consumers disabled]\n",
   1547  1.1  jmcneill 				    w->w_nid);
   1548  1.1  jmcneill 			}
   1549  1.1  jmcneill 		}
   1550  1.1  jmcneill 	} while (done == 0);
   1551  1.1  jmcneill }
   1552  1.1  jmcneill 
   1553  1.1  jmcneill static void
   1554  1.1  jmcneill hdafg_assoc_trace_undo(struct hdafg_softc *sc, int as, int seq)
   1555  1.1  jmcneill {
   1556  1.1  jmcneill 	struct hdaudio_widget *w;
   1557  1.1  jmcneill 	int i;
   1558  1.1  jmcneill 
   1559  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   1560  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   1561  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   1562  1.1  jmcneill 			continue;
   1563  1.1  jmcneill 		if (w->w_bindas != as)
   1564  1.1  jmcneill 			continue;
   1565  1.1  jmcneill 		if (seq >= 0) {
   1566  1.1  jmcneill 			w->w_bindseqmask &= ~(1 << seq);
   1567  1.1  jmcneill 			if (w->w_bindseqmask == 0) {
   1568  1.1  jmcneill 				w->w_bindas = -1;
   1569  1.1  jmcneill 				w->w_selconn = -1;
   1570  1.1  jmcneill 			}
   1571  1.1  jmcneill 		} else {
   1572  1.1  jmcneill 			w->w_bindas = -1;
   1573  1.1  jmcneill 			w->w_bindseqmask = 0;
   1574  1.1  jmcneill 			w->w_selconn = -1;
   1575  1.1  jmcneill 		}
   1576  1.1  jmcneill 	}
   1577  1.1  jmcneill }
   1578  1.1  jmcneill 
   1579  1.1  jmcneill static int
   1580  1.1  jmcneill hdafg_assoc_trace_dac(struct hdafg_softc *sc, int as, int seq,
   1581  1.1  jmcneill     int nid, int dupseq, int minassoc, int only, int depth)
   1582  1.1  jmcneill {
   1583  1.1  jmcneill 	struct hdaudio_widget *w;
   1584  1.1  jmcneill 	int i, im = -1;
   1585  1.1  jmcneill 	int m = 0, ret;
   1586  1.1  jmcneill 
   1587  1.1  jmcneill 	if (depth >= HDAUDIO_PARSE_MAXDEPTH)
   1588  1.1  jmcneill 		return 0;
   1589  1.1  jmcneill 	w = hdafg_widget_lookup(sc, nid);
   1590  1.1  jmcneill 	if (w == NULL || w->w_enable == false)
   1591  1.1  jmcneill 		return 0;
   1592  1.1  jmcneill 	/* We use only unused widgets */
   1593  1.1  jmcneill 	if (w->w_bindas >= 0 && w->w_bindas != as) {
   1594  1.1  jmcneill 		if (!only)
   1595  1.1  jmcneill 			hda_trace(sc, "depth %d nid %02X busy by assoc %d\n",
   1596  1.1  jmcneill 			    depth + 1, nid, w->w_bindas);
   1597  1.1  jmcneill 		return 0;
   1598  1.1  jmcneill 	}
   1599  1.1  jmcneill 	if (dupseq < 0) {
   1600  1.1  jmcneill 		if (w->w_bindseqmask != 0) {
   1601  1.1  jmcneill 			if (!only)
   1602  1.1  jmcneill 				hda_trace(sc,
   1603  1.1  jmcneill 				    "depth %d nid %02X busy by seqmask %x\n",
   1604  1.1  jmcneill 				    depth + 1, nid, w->w_bindas);
   1605  1.1  jmcneill 			return 0;
   1606  1.1  jmcneill 		}
   1607  1.1  jmcneill 	} else {
   1608  1.1  jmcneill 		/* If this is headphones, allow duplicate first pin */
   1609  1.1  jmcneill 		if (w->w_bindseqmask != 0 &&
   1610  1.1  jmcneill 		    (w->w_bindseqmask & (1 << dupseq)) == 0)
   1611  1.1  jmcneill 			return 0;
   1612  1.1  jmcneill 	}
   1613  1.1  jmcneill 
   1614  1.1  jmcneill 	switch (w->w_type) {
   1615  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_INPUT:
   1616  1.1  jmcneill 		break;
   1617  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_OUTPUT:
   1618  1.1  jmcneill 		/* If we are tracing HP take only dac of first pin */
   1619  1.1  jmcneill 		if ((only == 0 || only == w->w_nid) &&
   1620  1.1  jmcneill 		    (w->w_nid >= minassoc) && (dupseq < 0 || w->w_nid ==
   1621  1.1  jmcneill 		    sc->sc_assocs[as].as_dacs[dupseq]))
   1622  1.1  jmcneill 			m = w->w_nid;
   1623  1.1  jmcneill 		break;
   1624  1.1  jmcneill 	case COP_AWCAP_TYPE_PIN_COMPLEX:
   1625  1.1  jmcneill 		if (depth > 0)
   1626  1.1  jmcneill 			break;
   1627  1.1  jmcneill 		/* FALLTHROUGH */
   1628  1.1  jmcneill 	default:
   1629  1.1  jmcneill 		for (i = 0; i < w->w_nconns; i++) {
   1630  1.1  jmcneill 			if (w->w_connsenable[i] == false)
   1631  1.1  jmcneill 				continue;
   1632  1.1  jmcneill 			if (w->w_selconn != -1 && w->w_selconn != i)
   1633  1.1  jmcneill 				continue;
   1634  1.1  jmcneill 			ret = hdafg_assoc_trace_dac(sc, as, seq,
   1635  1.1  jmcneill 			    w->w_conns[i], dupseq, minassoc, only, depth + 1);
   1636  1.1  jmcneill 			if (ret) {
   1637  1.1  jmcneill 				if (m == 0 || ret < m) {
   1638  1.1  jmcneill 					m = ret;
   1639  1.1  jmcneill 					im = i;
   1640  1.1  jmcneill 				}
   1641  1.1  jmcneill 				if (only || dupseq >= 0)
   1642  1.1  jmcneill 					break;
   1643  1.1  jmcneill 			}
   1644  1.1  jmcneill 		}
   1645  1.1  jmcneill 		if (m && only && ((w->w_nconns > 1 &&
   1646  1.1  jmcneill 		    w->w_type != COP_AWCAP_TYPE_AUDIO_MIXER) ||
   1647  1.1  jmcneill 		    w->w_type == COP_AWCAP_TYPE_AUDIO_SELECTOR))
   1648  1.1  jmcneill 			w->w_selconn = im;
   1649  1.1  jmcneill 		break;
   1650  1.1  jmcneill 	}
   1651  1.1  jmcneill 	if (m && only) {
   1652  1.1  jmcneill 		w->w_bindas = as;
   1653  1.1  jmcneill 		w->w_bindseqmask |= (1 << seq);
   1654  1.1  jmcneill 	}
   1655  1.1  jmcneill 	if (!only)
   1656  1.1  jmcneill 		hda_trace(sc, "depth %d nid %02X dupseq %d returned %02X\n",
   1657  1.1  jmcneill 		    depth + 1, nid, dupseq, m);
   1658  1.1  jmcneill 
   1659  1.1  jmcneill 	return m;
   1660  1.1  jmcneill }
   1661  1.1  jmcneill 
   1662  1.1  jmcneill static int
   1663  1.1  jmcneill hdafg_assoc_trace_out(struct hdafg_softc *sc, int as, int seq)
   1664  1.1  jmcneill {
   1665  1.1  jmcneill 	struct hdaudio_assoc *assocs = sc->sc_assocs;
   1666  1.1  jmcneill 	int i, hpredir;
   1667  1.1  jmcneill 	int minassoc, res;
   1668  1.1  jmcneill 
   1669  1.1  jmcneill 	/* Find next pin */
   1670  1.1  jmcneill 	for (i = seq; i < HDAUDIO_MAXPINS && assocs[as].as_pins[i] == 0; i++)
   1671  1.1  jmcneill 		;
   1672  1.1  jmcneill 	/* Check if there is any left, if not then we have succeeded */
   1673  1.1  jmcneill 	if (i == HDAUDIO_MAXPINS)
   1674  1.1  jmcneill 		return 1;
   1675  1.1  jmcneill 
   1676  1.1  jmcneill 	hpredir = (i == 15 && assocs[as].as_fakeredir == 0) ?
   1677  1.1  jmcneill 	    assocs[as].as_hpredir : -1;
   1678  1.1  jmcneill 	minassoc = res = 0;
   1679  1.1  jmcneill 	do {
   1680  1.1  jmcneill 		/* Trace this pin taking min nid into account */
   1681  1.1  jmcneill 		res = hdafg_assoc_trace_dac(sc, as, i,
   1682  1.1  jmcneill 		    assocs[as].as_pins[i], hpredir, minassoc, 0, 0);
   1683  1.1  jmcneill 		if (res == 0) {
   1684  1.1  jmcneill 			/* If we failed, return to previous and redo it */
   1685  1.1  jmcneill 			hda_trace(sc, "  trace failed as=%d seq=%d pin=%02X "
   1686  1.1  jmcneill 			    "hpredir=%d minassoc=%d\n",
   1687  1.1  jmcneill 			    as, seq, assocs[as].as_pins[i], hpredir, minassoc);
   1688  1.1  jmcneill 			return 0;
   1689  1.1  jmcneill 		}
   1690  1.1  jmcneill 		/* Trace again to mark the path */
   1691  1.1  jmcneill 		hdafg_assoc_trace_dac(sc, as, i,
   1692  1.1  jmcneill 		    assocs[as].as_pins[i], hpredir, minassoc, res, 0);
   1693  1.1  jmcneill 		assocs[as].as_dacs[i] = res;
   1694  1.1  jmcneill 		/* We succeeded, so call next */
   1695  1.1  jmcneill 		if (hdafg_assoc_trace_out(sc, as, i + 1))
   1696  1.1  jmcneill 			return 1;
   1697  1.1  jmcneill 		/* If next failed, we should retry with next min */
   1698  1.1  jmcneill 		hdafg_assoc_trace_undo(sc, as, i);
   1699  1.1  jmcneill 		assocs[as].as_dacs[i] = 0;
   1700  1.1  jmcneill 		minassoc = res + 1;
   1701  1.1  jmcneill 	} while (1);
   1702  1.1  jmcneill }
   1703  1.1  jmcneill 
   1704  1.1  jmcneill static int
   1705  1.1  jmcneill hdafg_assoc_trace_adc(struct hdafg_softc *sc, int assoc, int seq,
   1706  1.1  jmcneill     int nid, int only, int depth)
   1707  1.1  jmcneill {
   1708  1.1  jmcneill 	struct hdaudio_widget *w, *wc;
   1709  1.1  jmcneill 	int i, j;
   1710  1.1  jmcneill 	int res = 0;
   1711  1.1  jmcneill 
   1712  1.1  jmcneill 	if (depth > HDAUDIO_PARSE_MAXDEPTH)
   1713  1.1  jmcneill 		return 0;
   1714  1.1  jmcneill 	w = hdafg_widget_lookup(sc, nid);
   1715  1.1  jmcneill 	if (w == NULL || w->w_enable == false)
   1716  1.1  jmcneill 		return 0;
   1717  1.1  jmcneill 	/* Use only unused widgets */
   1718  1.1  jmcneill 	if (w->w_bindas >= 0 && w->w_bindas != assoc)
   1719  1.1  jmcneill 		return 0;
   1720  1.1  jmcneill 
   1721  1.1  jmcneill 	switch (w->w_type) {
   1722  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_INPUT:
   1723  1.1  jmcneill 		if (only == w->w_nid)
   1724  1.1  jmcneill 			res = 1;
   1725  1.1  jmcneill 		break;
   1726  1.1  jmcneill 	case COP_AWCAP_TYPE_PIN_COMPLEX:
   1727  1.1  jmcneill 		if (depth > 0)
   1728  1.1  jmcneill 			break;
   1729  1.1  jmcneill 		/* FALLTHROUGH */
   1730  1.1  jmcneill 	default:
   1731  1.1  jmcneill 		/* Try to find reachable ADCs with specified nid */
   1732  1.1  jmcneill 		for (j = sc->sc_startnode; j < sc->sc_endnode; j++) {
   1733  1.1  jmcneill 			wc = hdafg_widget_lookup(sc, j);
   1734  1.1  jmcneill 			if (w == NULL || w->w_enable == false)
   1735  1.1  jmcneill 				continue;
   1736  1.1  jmcneill 			for (i = 0; i < wc->w_nconns; i++) {
   1737  1.1  jmcneill 				if (wc->w_connsenable[i] == false)
   1738  1.1  jmcneill 					continue;
   1739  1.1  jmcneill 				if (wc->w_conns[i] != nid)
   1740  1.1  jmcneill 					continue;
   1741  1.1  jmcneill 				if (hdafg_assoc_trace_adc(sc, assoc, seq,
   1742  1.1  jmcneill 				    j, only, depth + 1) != 0) {
   1743  1.1  jmcneill 					res = 1;
   1744  1.1  jmcneill 					if (((wc->w_nconns > 1 &&
   1745  1.1  jmcneill 					    wc->w_type != COP_AWCAP_TYPE_AUDIO_MIXER) ||
   1746  1.1  jmcneill 					    wc->w_type != COP_AWCAP_TYPE_AUDIO_SELECTOR)
   1747  1.1  jmcneill 					    && wc->w_selconn == -1)
   1748  1.1  jmcneill 						wc->w_selconn = i;
   1749  1.1  jmcneill 				}
   1750  1.1  jmcneill 			}
   1751  1.1  jmcneill 		}
   1752  1.1  jmcneill 		break;
   1753  1.1  jmcneill 	}
   1754  1.1  jmcneill 	if (res) {
   1755  1.1  jmcneill 		w->w_bindas = assoc;
   1756  1.1  jmcneill 		w->w_bindseqmask |= (1 << seq);
   1757  1.1  jmcneill 	}
   1758  1.1  jmcneill 	return res;
   1759  1.1  jmcneill }
   1760  1.1  jmcneill 
   1761  1.1  jmcneill static int
   1762  1.1  jmcneill hdafg_assoc_trace_in(struct hdafg_softc *sc, int assoc)
   1763  1.1  jmcneill {
   1764  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   1765  1.1  jmcneill 	struct hdaudio_widget *w;
   1766  1.1  jmcneill 	int i, j, k;
   1767  1.1  jmcneill 
   1768  1.1  jmcneill 	for (j = sc->sc_startnode; j < sc->sc_endnode; j++) {
   1769  1.1  jmcneill 		w = hdafg_widget_lookup(sc, j);
   1770  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   1771  1.1  jmcneill 			continue;
   1772  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_AUDIO_INPUT)
   1773  1.1  jmcneill 			continue;
   1774  1.1  jmcneill 		if (w->w_bindas >= 0 && w->w_bindas != assoc)
   1775  1.1  jmcneill 			continue;
   1776  1.1  jmcneill 
   1777  1.1  jmcneill 		/* Find next pin */
   1778  1.1  jmcneill 		for (i = 0; i < HDAUDIO_MAXPINS; i++) {
   1779  1.1  jmcneill 			if (as[assoc].as_pins[i] == 0)
   1780  1.1  jmcneill 				continue;
   1781  1.1  jmcneill 			/* Trace this pin taking goal into account */
   1782  1.1  jmcneill 			if (hdafg_assoc_trace_adc(sc, assoc, i,
   1783  1.1  jmcneill 			    as[assoc].as_pins[i], j, 0) == 0) {
   1784  1.1  jmcneill 				hdafg_assoc_trace_undo(sc, assoc, -1);
   1785  1.1  jmcneill 				for (k = 0; k < HDAUDIO_MAXPINS; k++)
   1786  1.1  jmcneill 					as[assoc].as_dacs[k] = 0;
   1787  1.1  jmcneill 				break;
   1788  1.1  jmcneill 			}
   1789  1.1  jmcneill 			as[assoc].as_dacs[i] = j;
   1790  1.1  jmcneill 		}
   1791  1.1  jmcneill 		if (i == HDAUDIO_MAXPINS)
   1792  1.1  jmcneill 			return 1;
   1793  1.1  jmcneill 	}
   1794  1.1  jmcneill 	return 0;
   1795  1.1  jmcneill }
   1796  1.1  jmcneill 
   1797  1.1  jmcneill static int
   1798  1.1  jmcneill hdafg_assoc_trace_to_out(struct hdafg_softc *sc, int nid, int depth)
   1799  1.1  jmcneill {
   1800  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   1801  1.1  jmcneill 	struct hdaudio_widget *w, *wc;
   1802  1.1  jmcneill 	int i, j;
   1803  1.1  jmcneill 	int res = 0;
   1804  1.1  jmcneill 
   1805  1.1  jmcneill 	if (depth > HDAUDIO_PARSE_MAXDEPTH)
   1806  1.1  jmcneill 		return 0;
   1807  1.1  jmcneill 	w = hdafg_widget_lookup(sc, nid);
   1808  1.1  jmcneill 	if (w == NULL || w->w_enable == false)
   1809  1.1  jmcneill 		return 0;
   1810  1.1  jmcneill 
   1811  1.1  jmcneill 	/* Use only unused widgets */
   1812  1.1  jmcneill 	if (depth > 0 && w->w_bindas != -1) {
   1813  1.1  jmcneill 		if (w->w_bindas < 0 ||
   1814  1.1  jmcneill 		    as[w->w_bindas].as_dir == HDAUDIO_PINDIR_OUT) {
   1815  1.1  jmcneill 			return 1;
   1816  1.1  jmcneill 		} else {
   1817  1.1  jmcneill 			return 0;
   1818  1.1  jmcneill 		}
   1819  1.1  jmcneill 	}
   1820  1.1  jmcneill 
   1821  1.1  jmcneill 	switch (w->w_type) {
   1822  1.1  jmcneill 	case COP_AWCAP_TYPE_AUDIO_INPUT:
   1823  1.1  jmcneill 		/* Do not traverse input (not yet supported) */
   1824  1.1  jmcneill 		break;
   1825  1.1  jmcneill 	case COP_AWCAP_TYPE_PIN_COMPLEX:
   1826  1.1  jmcneill 		if (depth > 0)
   1827  1.1  jmcneill 			break;
   1828  1.1  jmcneill 		/* FALLTHROUGH */
   1829  1.1  jmcneill 	default:
   1830  1.1  jmcneill 		/* Try to find reachable ADCs with specified nid */
   1831  1.1  jmcneill 		for (j = sc->sc_startnode; j < sc->sc_endnode; j++) {
   1832  1.1  jmcneill 			wc = hdafg_widget_lookup(sc, j);
   1833  1.1  jmcneill 			if (wc == NULL || wc->w_enable == false)
   1834  1.1  jmcneill 				continue;
   1835  1.1  jmcneill 			for (i = 0; i < wc->w_nconns; i++) {
   1836  1.1  jmcneill 				if (wc->w_connsenable[i] == false)
   1837  1.1  jmcneill 					continue;
   1838  1.1  jmcneill 				if (wc->w_conns[i] != nid)
   1839  1.1  jmcneill 					continue;
   1840  1.1  jmcneill 				if (hdafg_assoc_trace_to_out(sc,
   1841  1.1  jmcneill 				    j, depth + 1) != 0) {
   1842  1.1  jmcneill 					res = 1;
   1843  1.1  jmcneill 					if (wc->w_type ==
   1844  1.1  jmcneill 					    COP_AWCAP_TYPE_AUDIO_SELECTOR &&
   1845  1.1  jmcneill 					    wc->w_selconn == -1)
   1846  1.1  jmcneill 						wc->w_selconn = i;
   1847  1.1  jmcneill 				}
   1848  1.1  jmcneill 			}
   1849  1.1  jmcneill 		}
   1850  1.1  jmcneill 		break;
   1851  1.1  jmcneill 	}
   1852  1.1  jmcneill 	if (res)
   1853  1.1  jmcneill 		w->w_bindas = -2;
   1854  1.1  jmcneill 	return res;
   1855  1.1  jmcneill }
   1856  1.1  jmcneill 
   1857  1.1  jmcneill static void
   1858  1.1  jmcneill hdafg_assoc_trace_misc(struct hdafg_softc *sc)
   1859  1.1  jmcneill {
   1860  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   1861  1.1  jmcneill 	struct hdaudio_widget *w;
   1862  1.1  jmcneill 	int j;
   1863  1.1  jmcneill 
   1864  1.1  jmcneill 	/* Input monitor */
   1865  1.1  jmcneill 	/*
   1866  1.1  jmcneill 	 * Find mixer associated with input, but supplying signal
   1867  1.1  jmcneill 	 * for output associations. Hope it will be input monitor.
   1868  1.1  jmcneill 	 */
   1869  1.1  jmcneill 	for (j = sc->sc_startnode; j < sc->sc_endnode; j++) {
   1870  1.1  jmcneill 		w = hdafg_widget_lookup(sc, j);
   1871  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   1872  1.1  jmcneill 			continue;
   1873  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_AUDIO_MIXER)
   1874  1.1  jmcneill 			continue;
   1875  1.1  jmcneill 		if (w->w_bindas < 0 ||
   1876  1.1  jmcneill 		    as[w->w_bindas].as_dir != HDAUDIO_PINDIR_IN)
   1877  1.1  jmcneill 			continue;
   1878  1.1  jmcneill 		if (hdafg_assoc_trace_to_out(sc, w->w_nid, 0)) {
   1879  1.1  jmcneill 			w->w_pflags |= HDAUDIO_ADC_MONITOR;
   1880  1.1  jmcneill 			w->w_audiodev = HDAUDIO_MIXER_IMIX;
   1881  1.1  jmcneill 		}
   1882  1.1  jmcneill 	}
   1883  1.1  jmcneill 
   1884  1.1  jmcneill 	/* Beeper */
   1885  1.1  jmcneill 	for (j = sc->sc_startnode; j < sc->sc_endnode; j++) {
   1886  1.1  jmcneill 		w = hdafg_widget_lookup(sc, j);
   1887  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   1888  1.1  jmcneill 			continue;
   1889  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_BEEP_GENERATOR)
   1890  1.1  jmcneill 			continue;
   1891  1.1  jmcneill 		if (hdafg_assoc_trace_to_out(sc, w->w_nid, 0)) {
   1892  1.1  jmcneill 			hda_debug(sc, "beeper %02X traced to out\n", w->w_nid);
   1893  1.1  jmcneill 		}
   1894  1.1  jmcneill 		w->w_bindas = -2;
   1895  1.1  jmcneill 	}
   1896  1.1  jmcneill }
   1897  1.1  jmcneill 
   1898  1.1  jmcneill static void
   1899  1.1  jmcneill hdafg_build_tree(struct hdafg_softc *sc)
   1900  1.1  jmcneill {
   1901  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   1902  1.1  jmcneill 	int i, j, res;
   1903  1.1  jmcneill 
   1904  1.1  jmcneill 	/* Trace all associations in order of their numbers */
   1905  1.1  jmcneill 
   1906  1.1  jmcneill 	/* Trace DACs first */
   1907  1.1  jmcneill 	for (j = 0; j < sc->sc_nassocs; j++) {
   1908  1.1  jmcneill 		if (as[j].as_enable == false)
   1909  1.1  jmcneill 			continue;
   1910  1.1  jmcneill 		if (as[j].as_dir != HDAUDIO_PINDIR_OUT)
   1911  1.1  jmcneill 			continue;
   1912  1.1  jmcneill retry:
   1913  1.1  jmcneill 		res = hdafg_assoc_trace_out(sc, j, 0);
   1914  1.1  jmcneill 		if (res == 0 && as[j].as_hpredir >= 0 &&
   1915  1.1  jmcneill 		    as[j].as_fakeredir == 0) {
   1916  1.1  jmcneill 		    	/*
   1917  1.1  jmcneill 		    	 * If codec can't do analog HP redirection
   1918  1.1  jmcneill 			 * try to make it using one more DAC
   1919  1.1  jmcneill 			 */
   1920  1.1  jmcneill 			as[j].as_fakeredir = 1;
   1921  1.1  jmcneill 			goto retry;
   1922  1.1  jmcneill 		}
   1923  1.1  jmcneill 		if (!res) {
   1924  1.1  jmcneill 			hda_debug(sc, "disable assoc %d (%d) [trace failed]\n",
   1925  1.1  jmcneill 			    j, as[j].as_index);
   1926  1.1  jmcneill 			for (i = 0; i < HDAUDIO_MAXPINS; i++) {
   1927  1.1  jmcneill 				if (as[j].as_pins[i] == 0)
   1928  1.1  jmcneill 					continue;
   1929  1.1  jmcneill 				hda_debug(sc, "  assoc %d pin%d: %02X\n", j, i,
   1930  1.1  jmcneill 				    as[j].as_pins[i]);
   1931  1.1  jmcneill 			}
   1932  1.1  jmcneill 			for (i = 0; i < HDAUDIO_MAXPINS; i++) {
   1933  1.1  jmcneill 				if (as[j].as_dacs[i] == 0)
   1934  1.1  jmcneill 					continue;
   1935  1.1  jmcneill 				hda_debug(sc, "  assoc %d dac%d: %02X\n", j, i,
   1936  1.1  jmcneill 				    as[j].as_dacs[i]);
   1937  1.1  jmcneill 			}
   1938  1.1  jmcneill 
   1939  1.1  jmcneill 			as[j].as_enable = false;
   1940  1.1  jmcneill 		}
   1941  1.1  jmcneill 	}
   1942  1.1  jmcneill 
   1943  1.1  jmcneill 	/* Trace ADCs */
   1944  1.1  jmcneill 	for (j = 0; j < sc->sc_nassocs; j++) {
   1945  1.1  jmcneill 		if (as[j].as_enable == false)
   1946  1.1  jmcneill 			continue;
   1947  1.1  jmcneill 		if (as[j].as_dir != HDAUDIO_PINDIR_IN)
   1948  1.1  jmcneill 			continue;
   1949  1.1  jmcneill 		res = hdafg_assoc_trace_in(sc, j);
   1950  1.1  jmcneill 		if (!res) {
   1951  1.1  jmcneill 			hda_debug(sc, "disable assoc %d (%d) [trace failed]\n",
   1952  1.1  jmcneill 			    j, as[j].as_index);
   1953  1.1  jmcneill 			for (i = 0; i < HDAUDIO_MAXPINS; i++) {
   1954  1.1  jmcneill 				if (as[j].as_pins[i] == 0)
   1955  1.1  jmcneill 					continue;
   1956  1.1  jmcneill 				hda_debug(sc, "  assoc %d pin%d: %02X\n", j, i,
   1957  1.1  jmcneill 				    as[j].as_pins[i]);
   1958  1.1  jmcneill 			}
   1959  1.1  jmcneill 			for (i = 0; i < HDAUDIO_MAXPINS; i++) {
   1960  1.1  jmcneill 				if (as[j].as_dacs[i] == 0)
   1961  1.1  jmcneill 					continue;
   1962  1.1  jmcneill 				hda_debug(sc, "  assoc %d adc%d: %02X\n", j, i,
   1963  1.1  jmcneill 				    as[j].as_dacs[i]);
   1964  1.1  jmcneill 			}
   1965  1.1  jmcneill 
   1966  1.1  jmcneill 			as[j].as_enable = false;
   1967  1.1  jmcneill 		}
   1968  1.1  jmcneill 	}
   1969  1.1  jmcneill 
   1970  1.1  jmcneill 	/* Trace mixer and beeper pseudo associations */
   1971  1.1  jmcneill 	hdafg_assoc_trace_misc(sc);
   1972  1.1  jmcneill }
   1973  1.1  jmcneill 
   1974  1.1  jmcneill static void
   1975  1.1  jmcneill hdafg_prepare_pin_controls(struct hdafg_softc *sc)
   1976  1.1  jmcneill {
   1977  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   1978  1.1  jmcneill 	struct hdaudio_widget *w;
   1979  1.1  jmcneill 	uint32_t pincap;
   1980  1.1  jmcneill 	int i;
   1981  1.1  jmcneill 
   1982  1.1  jmcneill 	hda_debug(sc, "*** prepare pin controls, nwidgets = %d\n",
   1983  1.1  jmcneill 	    sc->sc_nwidgets);
   1984  1.1  jmcneill 
   1985  1.1  jmcneill 	for (i = 0; i < sc->sc_nwidgets; i++) {
   1986  1.1  jmcneill 		w = &sc->sc_widgets[i];
   1987  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX) {
   1988  1.1  jmcneill 			hda_debug(sc, "  skipping pin %02X type 0x%x\n",
   1989  1.1  jmcneill 			    w->w_nid, w->w_type);
   1990  1.1  jmcneill 			continue;
   1991  1.1  jmcneill 		}
   1992  1.1  jmcneill 		pincap = w->w_pin.cap;
   1993  1.1  jmcneill 
   1994  1.1  jmcneill 		/* Disable everything */
   1995  1.1  jmcneill 		w->w_pin.ctrl &= ~(
   1996  1.1  jmcneill 		    COP_PWC_VREF_ENABLE_MASK |
   1997  1.1  jmcneill 		    COP_PWC_IN_ENABLE |
   1998  1.1  jmcneill 		    COP_PWC_OUT_ENABLE |
   1999  1.1  jmcneill 		    COP_PWC_HPHN_ENABLE);
   2000  1.1  jmcneill 
   2001  1.1  jmcneill 		if (w->w_enable == false ||
   2002  1.1  jmcneill 		    w->w_bindas < 0 || as[w->w_bindas].as_enable == false) {
   2003  1.1  jmcneill 			/* Pin is unused so leave it disabled */
   2004  1.1  jmcneill 			if ((pincap & (COP_PINCAP_OUTPUT_CAPABLE |
   2005  1.1  jmcneill 			    COP_PINCAP_INPUT_CAPABLE)) ==
   2006  1.1  jmcneill 			    (COP_PINCAP_OUTPUT_CAPABLE |
   2007  1.1  jmcneill 			    COP_PINCAP_INPUT_CAPABLE)) {
   2008  1.1  jmcneill 				hda_debug(sc, "pin %02X off, "
   2009  1.1  jmcneill 				    "in/out capable (bindas=%d "
   2010  1.1  jmcneill 				    "enable=%d as_enable=%d)\n",
   2011  1.1  jmcneill 				    w->w_nid, w->w_bindas, w->w_enable,
   2012  1.1  jmcneill 				    w->w_bindas >= 0 ?
   2013  1.1  jmcneill 				    as[w->w_bindas].as_enable : -1);
   2014  1.1  jmcneill 				w->w_pin.ctrl |= COP_PWC_OUT_ENABLE;
   2015  1.1  jmcneill 			} else
   2016  1.1  jmcneill 				hda_debug(sc, "pin %02X off\n", w->w_nid);
   2017  1.1  jmcneill 			continue;
   2018  1.1  jmcneill 		} else if (as[w->w_bindas].as_dir == HDAUDIO_PINDIR_IN) {
   2019  1.1  jmcneill 			/* Input pin, configure for input */
   2020  1.1  jmcneill 			if (pincap & COP_PINCAP_INPUT_CAPABLE)
   2021  1.1  jmcneill 				w->w_pin.ctrl |= COP_PWC_IN_ENABLE;
   2022  1.1  jmcneill 
   2023  1.1  jmcneill 			hda_debug(sc, "pin %02X in ctrl 0x%x\n", w->w_nid,
   2024  1.1  jmcneill 			    w->w_pin.ctrl);
   2025  1.1  jmcneill 
   2026  1.1  jmcneill 			if (COP_CFG_DEFAULT_DEVICE(w->w_pin.config) !=
   2027  1.1  jmcneill 			    COP_DEVICE_MIC_IN)
   2028  1.1  jmcneill 				continue;
   2029  1.1  jmcneill 			if (COP_PINCAP_VREF_CONTROL(pincap) & COP_VREF_80)
   2030  1.1  jmcneill 				w->w_pin.ctrl |= COP_PWC_VREF_80;
   2031  1.1  jmcneill 			else if (COP_PINCAP_VREF_CONTROL(pincap) & COP_VREF_50)
   2032  1.1  jmcneill 				w->w_pin.ctrl |= COP_PWC_VREF_50;
   2033  1.1  jmcneill 		} else {
   2034  1.1  jmcneill 			/* Output pin, configure for output */
   2035  1.1  jmcneill 			if (pincap & COP_PINCAP_OUTPUT_CAPABLE)
   2036  1.1  jmcneill 				w->w_pin.ctrl |= COP_PWC_OUT_ENABLE;
   2037  1.1  jmcneill 			if ((pincap & COP_PINCAP_HEADPHONE_DRIVE_CAPABLE) &&
   2038  1.1  jmcneill 			    (COP_CFG_DEFAULT_DEVICE(w->w_pin.config) ==
   2039  1.1  jmcneill 			    COP_DEVICE_HP_OUT))
   2040  1.1  jmcneill 				w->w_pin.ctrl |= COP_PWC_HPHN_ENABLE;
   2041  1.1  jmcneill 			/* XXX VREF */
   2042  1.1  jmcneill 			hda_debug(sc, "pin %02X out ctrl 0x%x\n", w->w_nid,
   2043  1.1  jmcneill 			    w->w_pin.ctrl);
   2044  1.1  jmcneill 		}
   2045  1.1  jmcneill 	}
   2046  1.1  jmcneill }
   2047  1.1  jmcneill 
   2048  1.1  jmcneill static void
   2049  1.1  jmcneill hdafg_dump(struct hdafg_softc *sc)
   2050  1.1  jmcneill {
   2051  1.1  jmcneill #if defined(HDAFG_DEBUG) && HDAFG_DEBUG > 1
   2052  1.1  jmcneill 	struct hdaudio_control *ctl;
   2053  1.1  jmcneill 	int i, type;
   2054  1.1  jmcneill 
   2055  1.1  jmcneill 	for (i = 0; i < sc->sc_nctls; i++) {
   2056  1.1  jmcneill 		ctl = &sc->sc_ctls[i];
   2057  1.1  jmcneill 		type = (ctl->ctl_widget ? ctl->ctl_widget->w_type : -1);
   2058  1.1  jmcneill 		hda_print(sc, "%03X: nid %02X type %d %s (%s) index %d",
   2059  1.1  jmcneill 		    i, (ctl->ctl_widget ? ctl->ctl_widget->w_nid : -1), type,
   2060  1.1  jmcneill 		    (ctl->ctl_ndir == HDAUDIO_PINDIR_IN) ? "in " : "out",
   2061  1.1  jmcneill 		    (ctl->ctl_dir == HDAUDIO_PINDIR_IN) ? "in " : "out",
   2062  1.1  jmcneill 		    ctl->ctl_index);
   2063  1.1  jmcneill 		if (ctl->ctl_childwidget)
   2064  1.1  jmcneill 			hda_print1(sc, " cnid %02X",
   2065  1.1  jmcneill 			    ctl->ctl_childwidget->w_nid);
   2066  1.1  jmcneill 		else
   2067  1.1  jmcneill 			hda_print1(sc, "          ");
   2068  1.1  jmcneill 		hda_print1(sc, "\n");
   2069  1.1  jmcneill 		hda_print(sc, "     mute: %d step: %3d size: %3d off: %3d%s\n",
   2070  1.1  jmcneill 		    ctl->ctl_mute, ctl->ctl_step, ctl->ctl_size,
   2071  1.1  jmcneill 		    ctl->ctl_offset,
   2072  1.1  jmcneill 		    (ctl->ctl_enable == false) ? " [DISABLED]" : "");
   2073  1.1  jmcneill 	}
   2074  1.1  jmcneill #endif
   2075  1.1  jmcneill }
   2076  1.1  jmcneill 
   2077  1.1  jmcneill static int
   2078  1.1  jmcneill hdafg_match(device_t parent, cfdata_t match, void *opaque)
   2079  1.1  jmcneill {
   2080  1.1  jmcneill 	prop_dictionary_t args = opaque;
   2081  1.1  jmcneill 	uint8_t fgtype;
   2082  1.1  jmcneill 	bool rv;
   2083  1.1  jmcneill 
   2084  1.1  jmcneill 	rv = prop_dictionary_get_uint8(args, "function-group-type", &fgtype);
   2085  1.1  jmcneill 	if (rv == false || fgtype != HDAUDIO_GROUP_TYPE_AFG)
   2086  1.1  jmcneill 		return 0;
   2087  1.1  jmcneill 
   2088  1.1  jmcneill 	return 1;
   2089  1.1  jmcneill }
   2090  1.1  jmcneill 
   2091  1.1  jmcneill static void
   2092  1.1  jmcneill hdafg_disable_unassoc(struct hdafg_softc *sc)
   2093  1.1  jmcneill {
   2094  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   2095  1.1  jmcneill 	struct hdaudio_widget *w, *cw;
   2096  1.1  jmcneill 	struct hdaudio_control *ctl;
   2097  1.1  jmcneill 	int i, j, k;
   2098  1.1  jmcneill 
   2099  1.1  jmcneill 	/* Disable unassociated widgets */
   2100  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2101  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   2102  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   2103  1.1  jmcneill 			continue;
   2104  1.1  jmcneill 		if (w->w_bindas == -1) {
   2105  1.1  jmcneill 			w->w_enable = 0;
   2106  1.1  jmcneill 			hda_trace(sc, "disable %02X [unassociated]\n",
   2107  1.1  jmcneill 			    w->w_nid);
   2108  1.1  jmcneill 		}
   2109  1.1  jmcneill 	}
   2110  1.1  jmcneill 
   2111  1.1  jmcneill 	/* Disable input connections on input pin and output on output */
   2112  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2113  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   2114  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   2115  1.1  jmcneill 			continue;
   2116  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   2117  1.1  jmcneill 			continue;
   2118  1.1  jmcneill 		if (w->w_bindas < 0)
   2119  1.1  jmcneill 			continue;
   2120  1.1  jmcneill 		if (as[w->w_bindas].as_dir == HDAUDIO_PINDIR_IN) {
   2121  1.1  jmcneill 			hda_trace(sc, "disable %02X input connections\n",
   2122  1.1  jmcneill 			    w->w_nid);
   2123  1.1  jmcneill 			for (j = 0; j < w->w_nconns; j++)
   2124  1.1  jmcneill 				w->w_connsenable[j] = false;
   2125  1.1  jmcneill 			ctl = hdafg_control_lookup(sc, w->w_nid,
   2126  1.1  jmcneill 			    HDAUDIO_PINDIR_IN, -1, 1);
   2127  1.1  jmcneill 			if (ctl && ctl->ctl_enable == true) {
   2128  1.1  jmcneill 				ctl->ctl_forcemute = 1;
   2129  1.1  jmcneill 				ctl->ctl_muted = HDAUDIO_AMP_MUTE_ALL;
   2130  1.1  jmcneill 				ctl->ctl_left = ctl->ctl_right = 0;
   2131  1.1  jmcneill 				ctl->ctl_enable = false;
   2132  1.1  jmcneill 			}
   2133  1.1  jmcneill 		} else {
   2134  1.1  jmcneill 			ctl = hdafg_control_lookup(sc, w->w_nid,
   2135  1.1  jmcneill 			    HDAUDIO_PINDIR_OUT, -1, 1);
   2136  1.1  jmcneill 			if (ctl && ctl->ctl_enable == true) {
   2137  1.1  jmcneill 				ctl->ctl_forcemute = 1;
   2138  1.1  jmcneill 				ctl->ctl_muted = HDAUDIO_AMP_MUTE_ALL;
   2139  1.1  jmcneill 				ctl->ctl_left = ctl->ctl_right = 0;
   2140  1.1  jmcneill 				ctl->ctl_enable = false;
   2141  1.1  jmcneill 			}
   2142  1.1  jmcneill 			for (k = sc->sc_startnode; k < sc->sc_endnode; k++) {
   2143  1.1  jmcneill 				cw = hdafg_widget_lookup(sc, k);
   2144  1.1  jmcneill 				if (cw == NULL || cw->w_enable == false)
   2145  1.1  jmcneill 					continue;
   2146  1.1  jmcneill 				for (j = 0; j < cw->w_nconns; j++) {
   2147  1.1  jmcneill 					if (!cw->w_connsenable[j])
   2148  1.1  jmcneill 						continue;
   2149  1.1  jmcneill 					if (cw->w_conns[j] != i)
   2150  1.1  jmcneill 						continue;
   2151  1.1  jmcneill 					hda_trace(sc, "disable %02X -> %02X "
   2152  1.1  jmcneill 					    "output connection\n",
   2153  1.1  jmcneill 					    cw->w_nid, cw->w_conns[j]);
   2154  1.1  jmcneill 					cw->w_connsenable[j] = false;
   2155  1.1  jmcneill 					if (cw->w_type ==
   2156  1.1  jmcneill 					    COP_AWCAP_TYPE_PIN_COMPLEX &&
   2157  1.1  jmcneill 					    cw->w_nconns > 1)
   2158  1.1  jmcneill 						continue;
   2159  1.1  jmcneill 					ctl = hdafg_control_lookup(sc,
   2160  1.1  jmcneill 					    k, HDAUDIO_PINDIR_IN, j, 1);
   2161  1.1  jmcneill 					if (ctl && ctl->ctl_enable == true) {
   2162  1.1  jmcneill 						ctl->ctl_forcemute = 1;
   2163  1.1  jmcneill 						ctl->ctl_muted =
   2164  1.1  jmcneill 						    HDAUDIO_AMP_MUTE_ALL;
   2165  1.1  jmcneill 						ctl->ctl_left =
   2166  1.1  jmcneill 						    ctl->ctl_right = 0;
   2167  1.1  jmcneill 						ctl->ctl_enable = false;
   2168  1.1  jmcneill 					}
   2169  1.1  jmcneill 				}
   2170  1.1  jmcneill 			}
   2171  1.1  jmcneill 		}
   2172  1.1  jmcneill 	}
   2173  1.1  jmcneill }
   2174  1.1  jmcneill 
   2175  1.1  jmcneill static void
   2176  1.1  jmcneill hdafg_disable_unsel(struct hdafg_softc *sc)
   2177  1.1  jmcneill {
   2178  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   2179  1.1  jmcneill 	struct hdaudio_widget *w;
   2180  1.1  jmcneill 	int i, j;
   2181  1.1  jmcneill 
   2182  1.1  jmcneill 	/* On playback path we can safely disable all unselected inputs */
   2183  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2184  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   2185  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   2186  1.1  jmcneill 			continue;
   2187  1.1  jmcneill 		if (w->w_nconns <= 1)
   2188  1.1  jmcneill 			continue;
   2189  1.1  jmcneill 		if (w->w_type == COP_AWCAP_TYPE_AUDIO_MIXER)
   2190  1.1  jmcneill 			continue;
   2191  1.1  jmcneill 		if (w->w_bindas < 0 ||
   2192  1.1  jmcneill 		    as[w->w_bindas].as_dir == HDAUDIO_PINDIR_IN)
   2193  1.1  jmcneill 			continue;
   2194  1.1  jmcneill 		for (j = 0; j < w->w_nconns; j++) {
   2195  1.1  jmcneill 			if (w->w_connsenable[j] == false)
   2196  1.1  jmcneill 				continue;
   2197  1.1  jmcneill 			if (w->w_selconn < 0 || w->w_selconn == j)
   2198  1.1  jmcneill 				continue;
   2199  1.1  jmcneill 			hda_trace(sc, "disable %02X->%02X [unselected]\n",
   2200  1.1  jmcneill 			    w->w_nid, w->w_conns[j]);
   2201  1.1  jmcneill 			w->w_connsenable[j] = false;
   2202  1.1  jmcneill 		}
   2203  1.1  jmcneill 	}
   2204  1.1  jmcneill }
   2205  1.1  jmcneill 
   2206  1.1  jmcneill static void
   2207  1.1  jmcneill hdafg_disable_crossassoc(struct hdafg_softc *sc)
   2208  1.1  jmcneill {
   2209  1.1  jmcneill 	struct hdaudio_widget *w, *cw;
   2210  1.1  jmcneill 	struct hdaudio_control *ctl;
   2211  1.1  jmcneill 	int i, j;
   2212  1.1  jmcneill 
   2213  1.1  jmcneill 	/* Disable cross associated and unwanted cross channel connections */
   2214  1.1  jmcneill 
   2215  1.1  jmcneill 	/* ... using selectors */
   2216  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2217  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   2218  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   2219  1.1  jmcneill 			continue;
   2220  1.1  jmcneill 		if (w->w_nconns <= 1)
   2221  1.1  jmcneill 			continue;
   2222  1.1  jmcneill 		if (w->w_type == COP_AWCAP_TYPE_AUDIO_MIXER)
   2223  1.1  jmcneill 			continue;
   2224  1.1  jmcneill 		if (w->w_bindas == -2)
   2225  1.1  jmcneill 			continue;
   2226  1.1  jmcneill 		for (j = 0; j < w->w_nconns; j++) {
   2227  1.1  jmcneill 			if (w->w_connsenable[j] == false)
   2228  1.1  jmcneill 				continue;
   2229  1.1  jmcneill 			cw = hdafg_widget_lookup(sc, w->w_conns[j]);
   2230  1.1  jmcneill 			if (cw == NULL || cw->w_enable == false)
   2231  1.1  jmcneill 				continue;
   2232  1.1  jmcneill 			if (cw->w_bindas == -2)
   2233  1.1  jmcneill 				continue;
   2234  1.1  jmcneill 			if (w->w_bindas == cw->w_bindas &&
   2235  1.1  jmcneill 			    (w->w_bindseqmask & cw->w_bindseqmask) != 0)
   2236  1.1  jmcneill 				continue;
   2237  1.1  jmcneill 			hda_trace(sc, "disable %02X->%02X [crossassoc]\n",
   2238  1.1  jmcneill 			    w->w_nid, w->w_conns[j]);
   2239  1.1  jmcneill 			w->w_connsenable[j] = false;
   2240  1.1  jmcneill 		}
   2241  1.1  jmcneill 	}
   2242  1.1  jmcneill 	/* ... using controls */
   2243  1.1  jmcneill 	for (i = 0; i < sc->sc_nctls; i++) {
   2244  1.1  jmcneill 		ctl = &sc->sc_ctls[i];
   2245  1.1  jmcneill 		if (ctl->ctl_enable == false || ctl->ctl_childwidget == NULL)
   2246  1.1  jmcneill 			continue;
   2247  1.1  jmcneill 		if (ctl->ctl_widget->w_bindas == -2 ||
   2248  1.1  jmcneill 		    ctl->ctl_childwidget->w_bindas == -2)
   2249  1.1  jmcneill 			continue;
   2250  1.1  jmcneill 		if (ctl->ctl_widget->w_bindas !=
   2251  1.1  jmcneill 		    ctl->ctl_childwidget->w_bindas ||
   2252  1.1  jmcneill 		    (ctl->ctl_widget->w_bindseqmask &
   2253  1.1  jmcneill 		    ctl->ctl_childwidget->w_bindseqmask) == 0) {
   2254  1.1  jmcneill 			ctl->ctl_forcemute = 1;
   2255  1.1  jmcneill 			ctl->ctl_muted = HDAUDIO_AMP_MUTE_ALL;
   2256  1.1  jmcneill 			ctl->ctl_left = ctl->ctl_right = 0;
   2257  1.1  jmcneill 			ctl->ctl_enable = false;
   2258  1.1  jmcneill 			if (ctl->ctl_ndir == HDAUDIO_PINDIR_IN) {
   2259  1.1  jmcneill 				hda_trace(sc, "disable ctl %d:%02X:%02X "
   2260  1.1  jmcneill 				    "[crossassoc]\n",
   2261  1.1  jmcneill 			    	    i, ctl->ctl_widget->w_nid,
   2262  1.1  jmcneill 				    ctl->ctl_widget->w_conns[ctl->ctl_index]);
   2263  1.1  jmcneill 				ctl->ctl_widget->w_connsenable[
   2264  1.1  jmcneill 				    ctl->ctl_index] = false;
   2265  1.1  jmcneill 			}
   2266  1.1  jmcneill 		}
   2267  1.1  jmcneill 	}
   2268  1.1  jmcneill }
   2269  1.1  jmcneill 
   2270  1.1  jmcneill static struct hdaudio_control *
   2271  1.1  jmcneill hdafg_control_amp_get(struct hdafg_softc *sc, int nid,
   2272  1.1  jmcneill     enum hdaudio_pindir dir, int index, int cnt)
   2273  1.1  jmcneill {
   2274  1.1  jmcneill 	struct hdaudio_control *ctl;
   2275  1.1  jmcneill 	int i, found = 0;
   2276  1.1  jmcneill 
   2277  1.1  jmcneill 	for (i = 0; i < sc->sc_nctls; i++) {
   2278  1.1  jmcneill 		ctl = &sc->sc_ctls[i];
   2279  1.1  jmcneill 		if (ctl->ctl_enable == false)
   2280  1.1  jmcneill 			continue;
   2281  1.1  jmcneill 		if (ctl->ctl_widget->w_nid != nid)
   2282  1.1  jmcneill 			continue;
   2283  1.1  jmcneill 		if (dir && ctl->ctl_ndir != dir)
   2284  1.1  jmcneill 			continue;
   2285  1.1  jmcneill 		if (index >= 0 && ctl->ctl_ndir == HDAUDIO_PINDIR_IN &&
   2286  1.1  jmcneill 		    ctl->ctl_dir == ctl->ctl_ndir &&
   2287  1.1  jmcneill 		    ctl->ctl_index != index)
   2288  1.1  jmcneill 			continue;
   2289  1.1  jmcneill 		++found;
   2290  1.1  jmcneill 		if (found == cnt || cnt <= 0)
   2291  1.1  jmcneill 			return ctl;
   2292  1.1  jmcneill 	}
   2293  1.1  jmcneill 
   2294  1.1  jmcneill 	return NULL;
   2295  1.1  jmcneill }
   2296  1.1  jmcneill 
   2297  1.1  jmcneill static void
   2298  1.1  jmcneill hdafg_control_amp_set1(struct hdaudio_control *ctl, int lmute, int rmute,
   2299  1.1  jmcneill     int left, int right, int dir)
   2300  1.1  jmcneill {
   2301  1.1  jmcneill 	struct hdafg_softc *sc = ctl->ctl_widget->w_afg;
   2302  1.1  jmcneill 	int index = ctl->ctl_index;
   2303  1.1  jmcneill 	uint16_t v = 0;
   2304  1.1  jmcneill 
   2305  1.1  jmcneill 	if (left != right || lmute != rmute) {
   2306  1.1  jmcneill 		v = (1 << (15 - dir)) | (1 << 13) | (index << 8) |
   2307  1.1  jmcneill 		    (lmute << 7) | left;
   2308  1.1  jmcneill 		hdaudio_command(sc->sc_codec, ctl->ctl_widget->w_nid,
   2309  1.1  jmcneill 		    CORB_SET_AMPLIFIER_GAIN_MUTE, v);
   2310  1.1  jmcneill 		v = (1 << (15 - dir)) | (1 << 12) | (index << 8) |
   2311  1.1  jmcneill 		    (rmute << 7) | right;
   2312  1.1  jmcneill 	} else
   2313  1.1  jmcneill 		v = (1 << (15 - dir)) | (3 << 12) | (index << 8) |
   2314  1.1  jmcneill 		    (lmute << 7) | left;
   2315  1.1  jmcneill 	hdaudio_command(sc->sc_codec, ctl->ctl_widget->w_nid,
   2316  1.1  jmcneill 	    CORB_SET_AMPLIFIER_GAIN_MUTE, v);
   2317  1.1  jmcneill }
   2318  1.1  jmcneill 
   2319  1.1  jmcneill static void
   2320  1.1  jmcneill hdafg_control_amp_set(struct hdaudio_control *ctl, uint32_t mute,
   2321  1.1  jmcneill     int left, int right)
   2322  1.1  jmcneill {
   2323  1.1  jmcneill 	int lmute, rmute;
   2324  1.1  jmcneill 
   2325  1.1  jmcneill 	/* Save new values if valid */
   2326  1.1  jmcneill 	if (mute != HDAUDIO_AMP_MUTE_DEFAULT)
   2327  1.1  jmcneill 		ctl->ctl_muted = mute;
   2328  1.1  jmcneill 	if (left != HDAUDIO_AMP_VOL_DEFAULT)
   2329  1.1  jmcneill 		ctl->ctl_left = left;
   2330  1.1  jmcneill 	if (right != HDAUDIO_AMP_VOL_DEFAULT)
   2331  1.1  jmcneill 		ctl->ctl_right = right;
   2332  1.1  jmcneill 
   2333  1.1  jmcneill 	/* Prepare effective values */
   2334  1.1  jmcneill 	if (ctl->ctl_forcemute) {
   2335  1.1  jmcneill 		lmute = rmute = 1;
   2336  1.1  jmcneill 		left = right = 0;
   2337  1.1  jmcneill 	} else {
   2338  1.1  jmcneill 		lmute = HDAUDIO_AMP_LEFT_MUTED(ctl->ctl_muted);
   2339  1.1  jmcneill 		rmute = HDAUDIO_AMP_RIGHT_MUTED(ctl->ctl_muted);
   2340  1.1  jmcneill 		left = ctl->ctl_left;
   2341  1.1  jmcneill 		right = ctl->ctl_right;
   2342  1.1  jmcneill 	}
   2343  1.1  jmcneill 
   2344  1.1  jmcneill 	/* Apply effective values */
   2345  1.1  jmcneill 	if (ctl->ctl_dir & HDAUDIO_PINDIR_OUT)
   2346  1.1  jmcneill 		hdafg_control_amp_set1(ctl, lmute, rmute, left, right, 0);
   2347  1.1  jmcneill 	if (ctl->ctl_dir & HDAUDIO_PINDIR_IN)
   2348  1.1  jmcneill 		hdafg_control_amp_set1(ctl, lmute, rmute, left, right, 1);
   2349  1.1  jmcneill }
   2350  1.1  jmcneill 
   2351  1.1  jmcneill static void
   2352  1.1  jmcneill hdafg_control_commit(struct hdafg_softc *sc)
   2353  1.1  jmcneill {
   2354  1.1  jmcneill 	struct hdaudio_control *ctl;
   2355  1.1  jmcneill 	int i, z;
   2356  1.1  jmcneill 
   2357  1.1  jmcneill 	for (i = 0; i < sc->sc_nctls; i++) {
   2358  1.1  jmcneill 		ctl = &sc->sc_ctls[i];
   2359  1.1  jmcneill 		//if (ctl->ctl_enable == false || ctl->ctl_audiomask != 0)
   2360  1.1  jmcneill 		if (ctl->ctl_enable == false)
   2361  1.1  jmcneill 			continue;
   2362  1.1  jmcneill 		/* Init fixed controls to 0dB amplification */
   2363  1.1  jmcneill 		z = ctl->ctl_offset;
   2364  1.1  jmcneill 		if (z > ctl->ctl_step)
   2365  1.1  jmcneill 			z = ctl->ctl_step;
   2366  1.1  jmcneill 		hdafg_control_amp_set(ctl, HDAUDIO_AMP_MUTE_NONE, z, z);
   2367  1.1  jmcneill 	}
   2368  1.1  jmcneill }
   2369  1.1  jmcneill 
   2370  1.1  jmcneill static void
   2371  1.1  jmcneill hdafg_widget_connection_select(struct hdaudio_widget *w, uint8_t index)
   2372  1.1  jmcneill {
   2373  1.1  jmcneill 	struct hdafg_softc *sc = w->w_afg;
   2374  1.1  jmcneill 
   2375  1.1  jmcneill 	if (w->w_nconns < 1 || index > (w->w_nconns - 1))
   2376  1.1  jmcneill 		return;
   2377  1.1  jmcneill 
   2378  1.1  jmcneill 	hdaudio_command(sc->sc_codec, w->w_nid,
   2379  1.1  jmcneill 	    CORB_SET_CONNECTION_SELECT_CONTROL, index);
   2380  1.1  jmcneill 	w->w_selconn = index;
   2381  1.1  jmcneill }
   2382  1.1  jmcneill 
   2383  1.1  jmcneill static void
   2384  1.1  jmcneill hdafg_assign_names(struct hdafg_softc *sc)
   2385  1.1  jmcneill {
   2386  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   2387  1.1  jmcneill 	struct hdaudio_widget *w;
   2388  1.1  jmcneill 	int i, j;
   2389  1.1  jmcneill 	int type = -1, use, used =0;
   2390  1.1  jmcneill 	static const int types[7][13] = {
   2391  1.1  jmcneill 	    { HDAUDIO_MIXER_LINE, HDAUDIO_MIXER_LINE1, HDAUDIO_MIXER_LINE2,
   2392  1.1  jmcneill 	      HDAUDIO_MIXER_LINE3, -1 },
   2393  1.1  jmcneill 	    { HDAUDIO_MIXER_MONITOR, HDAUDIO_MIXER_MIC, -1 }, /* int mic */
   2394  1.1  jmcneill 	    { HDAUDIO_MIXER_MIC, HDAUDIO_MIXER_MONITOR, -1 }, /* ext mic */
   2395  1.1  jmcneill 	    { HDAUDIO_MIXER_CD, -1 },
   2396  1.1  jmcneill 	    { HDAUDIO_MIXER_SPEAKER, -1 },
   2397  1.1  jmcneill 	    { HDAUDIO_MIXER_DIGITAL1, HDAUDIO_MIXER_DIGITAL2,
   2398  1.1  jmcneill 	      HDAUDIO_MIXER_DIGITAL3, -1 },
   2399  1.1  jmcneill 	    { HDAUDIO_MIXER_LINE, HDAUDIO_MIXER_LINE1, HDAUDIO_MIXER_LINE2,
   2400  1.1  jmcneill 	      HDAUDIO_MIXER_LINE3, HDAUDIO_MIXER_PHONEIN,
   2401  1.1  jmcneill 	      HDAUDIO_MIXER_PHONEOUT, HDAUDIO_MIXER_VIDEO, HDAUDIO_MIXER_RADIO,
   2402  1.1  jmcneill 	      HDAUDIO_MIXER_DIGITAL1, HDAUDIO_MIXER_DIGITAL2,
   2403  1.1  jmcneill 	      HDAUDIO_MIXER_DIGITAL3, HDAUDIO_MIXER_MONITOR, -1 } /* others */
   2404  1.1  jmcneill 	};
   2405  1.1  jmcneill 
   2406  1.1  jmcneill 	/* Surely known names */
   2407  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2408  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   2409  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   2410  1.1  jmcneill 			continue;
   2411  1.1  jmcneill 		if (w->w_bindas == -1)
   2412  1.1  jmcneill 			continue;
   2413  1.1  jmcneill 		use = -1;
   2414  1.1  jmcneill 		switch (w->w_type) {
   2415  1.1  jmcneill 		case COP_AWCAP_TYPE_PIN_COMPLEX:
   2416  1.1  jmcneill 			if (as[w->w_bindas].as_dir == HDAUDIO_PINDIR_OUT)
   2417  1.1  jmcneill 				break;
   2418  1.1  jmcneill 			type = -1;
   2419  1.1  jmcneill 			switch (COP_CFG_DEFAULT_DEVICE(w->w_pin.config)) {
   2420  1.1  jmcneill 			case COP_DEVICE_LINE_IN:
   2421  1.1  jmcneill 				type = 0;
   2422  1.1  jmcneill 				break;
   2423  1.1  jmcneill 			case COP_DEVICE_MIC_IN:
   2424  1.1  jmcneill 				if (COP_CFG_PORT_CONNECTIVITY(w->w_pin.config)
   2425  1.1  jmcneill 				    == COP_PORT_JACK)
   2426  1.1  jmcneill 					break;
   2427  1.1  jmcneill 				type = 1;
   2428  1.1  jmcneill 				break;
   2429  1.1  jmcneill 			case COP_DEVICE_CD:
   2430  1.1  jmcneill 				type = 3;
   2431  1.1  jmcneill 				break;
   2432  1.1  jmcneill 			case COP_DEVICE_SPEAKER:
   2433  1.1  jmcneill 				type = 4;
   2434  1.1  jmcneill 				break;
   2435  1.1  jmcneill 			case COP_DEVICE_SPDIF_IN:
   2436  1.1  jmcneill 			case COP_DEVICE_DIGITAL_OTHER_IN:
   2437  1.1  jmcneill 				type = 5;
   2438  1.1  jmcneill 				break;
   2439  1.1  jmcneill 			}
   2440  1.1  jmcneill 			if (type == -1)
   2441  1.1  jmcneill 				break;
   2442  1.1  jmcneill 			j = 0;
   2443  1.1  jmcneill 			while (types[type][j] >= 0 &&
   2444  1.1  jmcneill 			    (used & (1 << types[type][j])) != 0) {
   2445  1.1  jmcneill 				j++;
   2446  1.1  jmcneill 			}
   2447  1.1  jmcneill 			if (types[type][j] >= 0)
   2448  1.1  jmcneill 				use = types[type][j];
   2449  1.1  jmcneill 			break;
   2450  1.1  jmcneill 		case COP_AWCAP_TYPE_AUDIO_OUTPUT:
   2451  1.1  jmcneill 			use = HDAUDIO_MIXER_PCM;
   2452  1.1  jmcneill 			break;
   2453  1.1  jmcneill 		case COP_AWCAP_TYPE_BEEP_GENERATOR:
   2454  1.1  jmcneill 			use = HDAUDIO_MIXER_SPEAKER;
   2455  1.1  jmcneill 			break;
   2456  1.1  jmcneill 		default:
   2457  1.1  jmcneill 			break;
   2458  1.1  jmcneill 		}
   2459  1.1  jmcneill 		if (use >= 0) {
   2460  1.1  jmcneill 			w->w_audiodev = use;
   2461  1.1  jmcneill 			used |= (1 << use);
   2462  1.1  jmcneill 		}
   2463  1.1  jmcneill 	}
   2464  1.1  jmcneill 	/* Semi-known names */
   2465  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2466  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   2467  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   2468  1.1  jmcneill 			continue;
   2469  1.1  jmcneill 		if (w->w_audiodev >= 0)
   2470  1.1  jmcneill 			continue;
   2471  1.1  jmcneill 		if (w->w_bindas == -1)
   2472  1.1  jmcneill 			continue;
   2473  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   2474  1.1  jmcneill 			continue;
   2475  1.1  jmcneill 		if (as[w->w_bindas].as_dir == HDAUDIO_PINDIR_OUT)
   2476  1.1  jmcneill 			continue;
   2477  1.1  jmcneill 		type = -1;
   2478  1.1  jmcneill 		switch (COP_CFG_DEFAULT_DEVICE(w->w_pin.config)) {
   2479  1.1  jmcneill 		case COP_DEVICE_LINE_OUT:
   2480  1.1  jmcneill 		case COP_DEVICE_SPEAKER:
   2481  1.1  jmcneill 		case COP_DEVICE_HP_OUT:
   2482  1.1  jmcneill 		case COP_DEVICE_AUX:
   2483  1.1  jmcneill 			type = 0;
   2484  1.1  jmcneill 			break;
   2485  1.1  jmcneill 		case COP_DEVICE_MIC_IN:
   2486  1.1  jmcneill 			type = 2;
   2487  1.1  jmcneill 			break;
   2488  1.1  jmcneill 		case COP_DEVICE_SPDIF_OUT:
   2489  1.1  jmcneill 		case COP_DEVICE_DIGITAL_OTHER_OUT:
   2490  1.1  jmcneill 			type = 5;
   2491  1.1  jmcneill 			break;
   2492  1.1  jmcneill 		}
   2493  1.1  jmcneill 		if (type == -1)
   2494  1.1  jmcneill 			break;
   2495  1.1  jmcneill 		j = 0;
   2496  1.1  jmcneill 		while (types[type][j] >= 0 &&
   2497  1.1  jmcneill 		    (used & (1 << types[type][j])) != 0) {
   2498  1.1  jmcneill 			j++;
   2499  1.1  jmcneill 		}
   2500  1.1  jmcneill 		if (types[type][j] >= 0) {
   2501  1.1  jmcneill 			w->w_audiodev = types[type][j];
   2502  1.1  jmcneill 			used |= (1 << types[type][j]);
   2503  1.1  jmcneill 		}
   2504  1.1  jmcneill 	}
   2505  1.1  jmcneill 	/* Others */
   2506  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2507  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   2508  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   2509  1.1  jmcneill 			continue;
   2510  1.1  jmcneill 		if (w->w_audiodev >= 0)
   2511  1.1  jmcneill 			continue;
   2512  1.1  jmcneill 		if (w->w_bindas == -1)
   2513  1.1  jmcneill 			continue;
   2514  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   2515  1.1  jmcneill 			continue;
   2516  1.1  jmcneill 		if (as[w->w_bindas].as_dir == HDAUDIO_PINDIR_OUT)
   2517  1.1  jmcneill 			continue;
   2518  1.1  jmcneill 		j = 0;
   2519  1.1  jmcneill 		while (types[6][j] >= 0 &&
   2520  1.1  jmcneill 		    (used & (1 << types[6][j])) != 0) {
   2521  1.1  jmcneill 			j++;
   2522  1.1  jmcneill 		}
   2523  1.1  jmcneill 		if (types[6][j] >= 0) {
   2524  1.1  jmcneill 			w->w_audiodev = types[6][j];
   2525  1.1  jmcneill 			used |= (1 << types[6][j]);
   2526  1.1  jmcneill 		}
   2527  1.1  jmcneill 	}
   2528  1.1  jmcneill }
   2529  1.1  jmcneill 
   2530  1.1  jmcneill static int
   2531  1.1  jmcneill hdafg_control_source_amp(struct hdafg_softc *sc, int nid, int index,
   2532  1.1  jmcneill     int audiodev, int ctlable, int depth, int need)
   2533  1.1  jmcneill {
   2534  1.1  jmcneill 	struct hdaudio_widget *w, *wc;
   2535  1.1  jmcneill 	struct hdaudio_control *ctl;
   2536  1.1  jmcneill 	int i, j, conns = 0, rneed;
   2537  1.1  jmcneill 
   2538  1.1  jmcneill 	if (depth >= HDAUDIO_PARSE_MAXDEPTH)
   2539  1.1  jmcneill 		return need;
   2540  1.1  jmcneill 
   2541  1.1  jmcneill 	w = hdafg_widget_lookup(sc, nid);
   2542  1.1  jmcneill 	if (w == NULL || w->w_enable == false)
   2543  1.1  jmcneill 		return need;
   2544  1.1  jmcneill 
   2545  1.1  jmcneill 	/* Count number of active inputs */
   2546  1.1  jmcneill 	if (depth > 0) {
   2547  1.1  jmcneill 		for (j = 0; j < w->w_nconns; j++) {
   2548  1.1  jmcneill 			if (w->w_connsenable[j])
   2549  1.1  jmcneill 				++conns;
   2550  1.1  jmcneill 		}
   2551  1.1  jmcneill 	}
   2552  1.1  jmcneill 
   2553  1.1  jmcneill 	/*
   2554  1.1  jmcneill 	 * If this is not a first step, use input mixer. Pins have common
   2555  1.1  jmcneill 	 * input ctl so care must be taken
   2556  1.1  jmcneill 	 */
   2557  1.1  jmcneill 	if (depth > 0 && ctlable && (conns == 1 ||
   2558  1.1  jmcneill 	    w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)) {
   2559  1.1  jmcneill 		ctl = hdafg_control_amp_get(sc, w->w_nid,
   2560  1.1  jmcneill 		    HDAUDIO_PINDIR_IN, index, 1);
   2561  1.1  jmcneill 		if (ctl) {
   2562  1.1  jmcneill 			if (HDAUDIO_CONTROL_GIVE(ctl) & need)
   2563  1.1  jmcneill 				ctl->ctl_audiomask |= (1 << audiodev);
   2564  1.1  jmcneill 			else
   2565  1.1  jmcneill 				ctl->ctl_paudiomask |= (1 << audiodev);
   2566  1.1  jmcneill 			need &= ~HDAUDIO_CONTROL_GIVE(ctl);
   2567  1.1  jmcneill 		}
   2568  1.1  jmcneill 	}
   2569  1.1  jmcneill 
   2570  1.1  jmcneill 	/* If widget has own audiodev, don't traverse it. */
   2571  1.1  jmcneill 	if (w->w_audiodev >= 0 && depth > 0)
   2572  1.1  jmcneill 		return need;
   2573  1.1  jmcneill 
   2574  1.1  jmcneill 	/* We must not traverse pins */
   2575  1.1  jmcneill 	if ((w->w_type == COP_AWCAP_TYPE_AUDIO_INPUT ||
   2576  1.1  jmcneill 	    w->w_type == COP_AWCAP_TYPE_PIN_COMPLEX) && depth > 0)
   2577  1.1  jmcneill 		return need;
   2578  1.1  jmcneill 
   2579  1.1  jmcneill 	/* Record that this widget exports such signal */
   2580  1.1  jmcneill 	w->w_audiomask |= (1 << audiodev);
   2581  1.1  jmcneill 
   2582  1.1  jmcneill 	/*
   2583  1.1  jmcneill 	 * If signals mixed, we can't assign controls further. Ignore this
   2584  1.1  jmcneill 	 * on depth zero. Caller must know why. Ignore this for static
   2585  1.1  jmcneill 	 * selectors if this input is selected.
   2586  1.1  jmcneill 	 */
   2587  1.1  jmcneill 	if (conns > 1)
   2588  1.1  jmcneill 		ctlable = 0;
   2589  1.1  jmcneill 
   2590  1.1  jmcneill 	if (ctlable) {
   2591  1.1  jmcneill 		ctl = hdafg_control_amp_get(sc, w->w_nid,
   2592  1.1  jmcneill 		    HDAUDIO_PINDIR_OUT, -1, 1);
   2593  1.1  jmcneill 		if (ctl) {
   2594  1.1  jmcneill 			if (HDAUDIO_CONTROL_GIVE(ctl) & need)
   2595  1.1  jmcneill 				ctl->ctl_audiomask |= (1 << audiodev);
   2596  1.1  jmcneill 			else
   2597  1.1  jmcneill 				ctl->ctl_paudiomask |= (1 << audiodev);
   2598  1.1  jmcneill 			need &= ~HDAUDIO_CONTROL_GIVE(ctl);
   2599  1.1  jmcneill 		}
   2600  1.1  jmcneill 	}
   2601  1.1  jmcneill 
   2602  1.1  jmcneill 	rneed = 0;
   2603  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2604  1.1  jmcneill 		wc = hdafg_widget_lookup(sc, i);
   2605  1.1  jmcneill 		if (wc == NULL || wc->w_enable == false)
   2606  1.1  jmcneill 			continue;
   2607  1.1  jmcneill 		for (j = 0; j < wc->w_nconns; j++) {
   2608  1.1  jmcneill 			if (wc->w_connsenable[j] && wc->w_conns[j] == nid) {
   2609  1.1  jmcneill 				rneed |= hdafg_control_source_amp(sc,
   2610  1.1  jmcneill 				    wc->w_nid, j, audiodev, ctlable, depth + 1,
   2611  1.1  jmcneill 				    need);
   2612  1.1  jmcneill 			}
   2613  1.1  jmcneill 		}
   2614  1.1  jmcneill 	}
   2615  1.1  jmcneill 	rneed &= need;
   2616  1.1  jmcneill 
   2617  1.1  jmcneill 	return rneed;
   2618  1.1  jmcneill }
   2619  1.1  jmcneill 
   2620  1.1  jmcneill static void
   2621  1.1  jmcneill hdafg_control_dest_amp(struct hdafg_softc *sc, int nid,
   2622  1.1  jmcneill     int audiodev, int depth, int need)
   2623  1.1  jmcneill {
   2624  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   2625  1.1  jmcneill 	struct hdaudio_widget *w, *wc;
   2626  1.1  jmcneill 	struct hdaudio_control *ctl;
   2627  1.1  jmcneill 	int i, j, consumers;
   2628  1.1  jmcneill 
   2629  1.1  jmcneill 	if (depth > HDAUDIO_PARSE_MAXDEPTH)
   2630  1.1  jmcneill 		return;
   2631  1.1  jmcneill 
   2632  1.1  jmcneill 	w = hdafg_widget_lookup(sc, nid);
   2633  1.1  jmcneill 	if (w == NULL || w->w_enable == false)
   2634  1.1  jmcneill 		return;
   2635  1.1  jmcneill 
   2636  1.1  jmcneill 	if (depth > 0) {
   2637  1.1  jmcneill 		/*
   2638  1.1  jmcneill 		 * If this node produces output for several consumers,
   2639  1.1  jmcneill 		 * we can't touch it
   2640  1.1  jmcneill 		 */
   2641  1.1  jmcneill 		consumers = 0;
   2642  1.1  jmcneill 		for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2643  1.1  jmcneill 			wc = hdafg_widget_lookup(sc, i);
   2644  1.1  jmcneill 			if (wc == NULL || wc->w_enable == false)
   2645  1.1  jmcneill 				continue;
   2646  1.1  jmcneill 			for (j = 0; j < wc->w_nconns; j++) {
   2647  1.1  jmcneill 				if (wc->w_connsenable[j] &&
   2648  1.1  jmcneill 				    wc->w_conns[j] == nid)
   2649  1.1  jmcneill 					++consumers;
   2650  1.1  jmcneill 			}
   2651  1.1  jmcneill 		}
   2652  1.1  jmcneill 		/*
   2653  1.1  jmcneill 		 * The only exception is if real HP redirection is configured
   2654  1.1  jmcneill 		 * and this is a duplication point.
   2655  1.1  jmcneill 		 * XXX: Not completely correct.
   2656  1.1  jmcneill 		 */
   2657  1.1  jmcneill 		if ((consumers == 2 && (w->w_bindas < 0 ||
   2658  1.1  jmcneill 		    as[w->w_bindas].as_hpredir < 0 ||
   2659  1.1  jmcneill 		    as[w->w_bindas].as_fakeredir ||
   2660  1.1  jmcneill 		    (w->w_bindseqmask & (1 << 15)) == 0)) ||
   2661  1.1  jmcneill 		    consumers > 2)
   2662  1.1  jmcneill 			return;
   2663  1.1  jmcneill 
   2664  1.1  jmcneill 		/* Else use its output mixer */
   2665  1.1  jmcneill 		ctl = hdafg_control_amp_get(sc, w->w_nid,
   2666  1.1  jmcneill 		    HDAUDIO_PINDIR_OUT, -1, 1);
   2667  1.1  jmcneill 		if (ctl) {
   2668  1.1  jmcneill 			if (HDAUDIO_CONTROL_GIVE(ctl) & need)
   2669  1.1  jmcneill 				ctl->ctl_audiomask |= (1 << audiodev);
   2670  1.1  jmcneill 			else
   2671  1.1  jmcneill 				ctl->ctl_paudiomask |= (1 << audiodev);
   2672  1.1  jmcneill 			need &= ~HDAUDIO_CONTROL_GIVE(ctl);
   2673  1.1  jmcneill 		}
   2674  1.1  jmcneill 	}
   2675  1.1  jmcneill 
   2676  1.1  jmcneill 	/* We must not traverse pin */
   2677  1.1  jmcneill 	if (w->w_type == COP_AWCAP_TYPE_PIN_COMPLEX && depth > 0)
   2678  1.1  jmcneill 		return;
   2679  1.1  jmcneill 
   2680  1.1  jmcneill 	for (i = 0; i < w->w_nconns; i++) {
   2681  1.1  jmcneill 		int tneed = need;
   2682  1.1  jmcneill 		if (w->w_connsenable[i] == false)
   2683  1.1  jmcneill 			continue;
   2684  1.1  jmcneill 		ctl = hdafg_control_amp_get(sc, w->w_nid,
   2685  1.1  jmcneill 		    HDAUDIO_PINDIR_IN, i, 1);
   2686  1.1  jmcneill 		if (ctl) {
   2687  1.1  jmcneill 			if (HDAUDIO_CONTROL_GIVE(ctl) & tneed)
   2688  1.1  jmcneill 				ctl->ctl_audiomask |= (1 << audiodev);
   2689  1.1  jmcneill 			else
   2690  1.1  jmcneill 				ctl->ctl_paudiomask |= (1 << audiodev);
   2691  1.1  jmcneill 			tneed &= ~HDAUDIO_CONTROL_GIVE(ctl);
   2692  1.1  jmcneill 		}
   2693  1.1  jmcneill 		hdafg_control_dest_amp(sc, w->w_conns[i], audiodev,
   2694  1.1  jmcneill 		    depth + 1, tneed);
   2695  1.1  jmcneill 	}
   2696  1.1  jmcneill }
   2697  1.1  jmcneill 
   2698  1.1  jmcneill static void
   2699  1.1  jmcneill hdafg_assign_mixers(struct hdafg_softc *sc)
   2700  1.1  jmcneill {
   2701  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   2702  1.1  jmcneill 	struct hdaudio_control *ctl;
   2703  1.1  jmcneill 	struct hdaudio_widget *w;
   2704  1.1  jmcneill 	int i;
   2705  1.1  jmcneill 
   2706  1.1  jmcneill 	/* Assign mixers to the tree */
   2707  1.1  jmcneill 	for (i = sc->sc_startnode; i < sc->sc_endnode; i++) {
   2708  1.1  jmcneill 		w = hdafg_widget_lookup(sc, i);
   2709  1.1  jmcneill 		if (w == NULL || w->w_enable == FALSE)
   2710  1.1  jmcneill 			continue;
   2711  1.1  jmcneill 		if (w->w_type == COP_AWCAP_TYPE_AUDIO_OUTPUT ||
   2712  1.1  jmcneill 		    w->w_type == COP_AWCAP_TYPE_BEEP_GENERATOR ||
   2713  1.1  jmcneill 		    (w->w_type == COP_AWCAP_TYPE_PIN_COMPLEX &&
   2714  1.1  jmcneill 		    as[w->w_bindas].as_dir == HDAUDIO_PINDIR_IN)) {
   2715  1.1  jmcneill 			if (w->w_audiodev < 0)
   2716  1.1  jmcneill 				continue;
   2717  1.1  jmcneill 			hdafg_control_source_amp(sc, w->w_nid, -1,
   2718  1.1  jmcneill 			    w->w_audiodev, 1, 0, 1);
   2719  1.1  jmcneill 		} else if (w->w_pflags & HDAUDIO_ADC_MONITOR) {
   2720  1.1  jmcneill 			if (w->w_audiodev < 0)
   2721  1.1  jmcneill 				continue;
   2722  1.1  jmcneill 			if (hdafg_control_source_amp(sc, w->w_nid, -1,
   2723  1.1  jmcneill 			    w->w_audiodev, 1, 0, 1)) {
   2724  1.1  jmcneill 				/* If we are unable to control input monitor
   2725  1.1  jmcneill 				   as source, try to control it as dest */
   2726  1.1  jmcneill 				hdafg_control_dest_amp(sc, w->w_nid,
   2727  1.1  jmcneill 				    w->w_audiodev, 0, 1);
   2728  1.1  jmcneill 			}
   2729  1.1  jmcneill 		} else if (w->w_type == COP_AWCAP_TYPE_AUDIO_INPUT) {
   2730  1.1  jmcneill 			hdafg_control_dest_amp(sc, w->w_nid,
   2731  1.1  jmcneill 			    HDAUDIO_MIXER_RECLEV, 0, 1);
   2732  1.1  jmcneill 		} else if (w->w_type == COP_AWCAP_TYPE_PIN_COMPLEX &&
   2733  1.1  jmcneill 		    as[w->w_bindas].as_dir == HDAUDIO_PINDIR_OUT) {
   2734  1.1  jmcneill 			hdafg_control_dest_amp(sc, w->w_nid,
   2735  1.1  jmcneill 			    HDAUDIO_MIXER_VOLUME, 0, 1);
   2736  1.1  jmcneill 		}
   2737  1.1  jmcneill 	}
   2738  1.1  jmcneill 	/* Treat unrequired as possible */
   2739  1.1  jmcneill 	i = 0;
   2740  1.1  jmcneill 	for (i = 0; i < sc->sc_nctls; i++) {
   2741  1.1  jmcneill 		ctl = &sc->sc_ctls[i];
   2742  1.1  jmcneill 		if (ctl->ctl_audiomask == 0)
   2743  1.1  jmcneill 			ctl->ctl_audiomask = ctl->ctl_paudiomask;
   2744  1.1  jmcneill 	}
   2745  1.1  jmcneill }
   2746  1.1  jmcneill 
   2747  1.1  jmcneill static void
   2748  1.1  jmcneill hdafg_build_mixers(struct hdafg_softc *sc)
   2749  1.1  jmcneill {
   2750  1.1  jmcneill 	struct hdaudio_mixer *mx;
   2751  1.1  jmcneill 	struct hdaudio_control *ctl, *masterctl = NULL;
   2752  1.1  jmcneill 	uint32_t audiomask = 0;
   2753  1.1  jmcneill 	int nmixers = 0;
   2754  1.1  jmcneill 	int i, j, index = 0;
   2755  1.1  jmcneill 	int ndac, nadc;
   2756  1.1  jmcneill 	int ctrlcnt[HDAUDIO_MIXER_NRDEVICES];
   2757  1.1  jmcneill 
   2758  1.1  jmcneill 	memset(ctrlcnt, 0, sizeof(ctrlcnt));
   2759  1.1  jmcneill 
   2760  1.1  jmcneill 	/* Count the number of required mixers */
   2761  1.1  jmcneill 	for (i = 0; i < sc->sc_nctls; i++) {
   2762  1.1  jmcneill 		ctl = &sc->sc_ctls[i];
   2763  1.1  jmcneill 		if (ctl->ctl_enable == false ||
   2764  1.1  jmcneill 		    ctl->ctl_audiomask == 0)
   2765  1.1  jmcneill 			continue;
   2766  1.1  jmcneill 		audiomask |= ctl->ctl_audiomask;
   2767  1.1  jmcneill 		++nmixers;
   2768  1.1  jmcneill 		if (ctl->ctl_mute)
   2769  1.1  jmcneill 			++nmixers;
   2770  1.1  jmcneill 	}
   2771  1.1  jmcneill 
   2772  1.1  jmcneill 	/* XXXJDM TODO: softvol */
   2773  1.1  jmcneill 	/* Declare master volume if needed */
   2774  1.1  jmcneill 	if ((audiomask & (HDAUDIO_MASK(VOLUME) | HDAUDIO_MASK(PCM))) ==
   2775  1.1  jmcneill 	    HDAUDIO_MASK(PCM)) {
   2776  1.1  jmcneill 		audiomask |= HDAUDIO_MASK(VOLUME);
   2777  1.1  jmcneill 		for (i = 0; i < sc->sc_nctls; i++) {
   2778  1.1  jmcneill 			if (sc->sc_ctls[i].ctl_audiomask == HDAUDIO_MASK(PCM)) {
   2779  1.1  jmcneill 				masterctl = &sc->sc_ctls[i];
   2780  1.1  jmcneill 				++nmixers;
   2781  1.1  jmcneill 				if (masterctl->ctl_mute)
   2782  1.1  jmcneill 					++nmixers;
   2783  1.1  jmcneill 				break;
   2784  1.1  jmcneill 			}
   2785  1.1  jmcneill 		}
   2786  1.1  jmcneill 	}
   2787  1.1  jmcneill 
   2788  1.1  jmcneill 	/* Make room for mixer classes */
   2789  1.1  jmcneill 	nmixers += (HDAUDIO_MIXER_CLASS_LAST + 1);
   2790  1.1  jmcneill 
   2791  1.1  jmcneill 	/* count DACs and ADCs for selectors */
   2792  1.1  jmcneill 	ndac = nadc = 0;
   2793  1.1  jmcneill 	for (i = 0; i < sc->sc_nassocs; i++) {
   2794  1.1  jmcneill 		if (sc->sc_assocs[i].as_enable == false)
   2795  1.1  jmcneill 			continue;
   2796  1.1  jmcneill 		if (sc->sc_assocs[i].as_dir == HDAUDIO_PINDIR_OUT)
   2797  1.1  jmcneill 			++ndac;
   2798  1.1  jmcneill 		else if (sc->sc_assocs[i].as_dir == HDAUDIO_PINDIR_IN)
   2799  1.1  jmcneill 			++nadc;
   2800  1.1  jmcneill 	}
   2801  1.1  jmcneill 
   2802  1.1  jmcneill 	/* Make room for selectors */
   2803  1.1  jmcneill 	if (ndac > 0)
   2804  1.1  jmcneill 		++nmixers;
   2805  1.1  jmcneill 	if (nadc > 0)
   2806  1.1  jmcneill 		++nmixers;
   2807  1.1  jmcneill 
   2808  1.1  jmcneill 	hda_trace(sc, "  need %d mixers (3 classes%s)\n",
   2809  1.1  jmcneill 	    nmixers, masterctl ? " + fake master" : "");
   2810  1.1  jmcneill 
   2811  1.1  jmcneill 	/* Allocate memory for the mixers */
   2812  1.1  jmcneill 	mx = kmem_zalloc(nmixers * sizeof(*mx), KM_SLEEP);
   2813  1.1  jmcneill 	sc->sc_nmixers = nmixers;
   2814  1.1  jmcneill 
   2815  1.1  jmcneill 	/* Build class mixers */
   2816  1.1  jmcneill 	for (i = 0; i <= HDAUDIO_MIXER_CLASS_LAST; i++) {
   2817  1.1  jmcneill 		mx[index].mx_ctl = NULL;
   2818  1.1  jmcneill 		mx[index].mx_di.index = index;
   2819  1.1  jmcneill 		mx[index].mx_di.type = AUDIO_MIXER_CLASS;
   2820  1.1  jmcneill 		mx[index].mx_di.mixer_class = i;
   2821  1.1  jmcneill 		mx[index].mx_di.next = mx[index].mx_di.prev = AUDIO_MIXER_LAST;
   2822  1.1  jmcneill 		switch (i) {
   2823  1.1  jmcneill 		case HDAUDIO_MIXER_CLASS_OUTPUTS:
   2824  1.1  jmcneill 			strcpy(mx[index].mx_di.label.name, AudioCoutputs);
   2825  1.1  jmcneill 			break;
   2826  1.1  jmcneill 		case HDAUDIO_MIXER_CLASS_INPUTS:
   2827  1.1  jmcneill 			strcpy(mx[index].mx_di.label.name, AudioCinputs);
   2828  1.1  jmcneill 			break;
   2829  1.1  jmcneill 		case HDAUDIO_MIXER_CLASS_RECORD:
   2830  1.1  jmcneill 			strcpy(mx[index].mx_di.label.name, AudioCrecord);
   2831  1.1  jmcneill 			break;
   2832  1.1  jmcneill 		}
   2833  1.1  jmcneill 		++index;
   2834  1.1  jmcneill 	}
   2835  1.1  jmcneill 
   2836  1.1  jmcneill 	/* Shadow master control */
   2837  1.1  jmcneill 	if (masterctl != NULL) {
   2838  1.1  jmcneill 		mx[index].mx_ctl = masterctl;
   2839  1.1  jmcneill 		mx[index].mx_di.index = index;
   2840  1.1  jmcneill 		mx[index].mx_di.type = AUDIO_MIXER_VALUE;
   2841  1.1  jmcneill 		mx[index].mx_di.prev = mx[index].mx_di.next = AUDIO_MIXER_LAST;
   2842  1.1  jmcneill 		mx[index].mx_di.un.v.num_channels = 2;	/* XXX */
   2843  1.1  jmcneill 		mx[index].mx_di.mixer_class = HDAUDIO_MIXER_CLASS_OUTPUTS;
   2844  1.1  jmcneill 		mx[index].mx_di.un.v.delta = 256 / (masterctl->ctl_step + 1);
   2845  1.1  jmcneill 		strcpy(mx[index].mx_di.label.name, AudioNmaster);
   2846  1.1  jmcneill 		strcpy(mx[index].mx_di.un.v.units.name, AudioNvolume);
   2847  1.1  jmcneill 		hda_trace(sc, "  adding outputs.%s\n",
   2848  1.1  jmcneill 		    mx[index].mx_di.label.name);
   2849  1.1  jmcneill 		++index;
   2850  1.1  jmcneill 		if (masterctl->ctl_mute) {
   2851  1.1  jmcneill 			mx[index] = mx[index - 1];
   2852  1.1  jmcneill 			mx[index].mx_di.index = index;
   2853  1.1  jmcneill 			mx[index].mx_di.type = AUDIO_MIXER_ENUM;
   2854  1.1  jmcneill 			mx[index].mx_di.prev = mx[index].mx_di.next = AUDIO_MIXER_LAST;
   2855  1.1  jmcneill 			strcpy(mx[index].mx_di.label.name, AudioNmaster "." AudioNmute);
   2856  1.1  jmcneill 			mx[index].mx_di.un.e.num_mem = 2;
   2857  1.1  jmcneill 			strcpy(mx[index].mx_di.un.e.member[0].label.name, AudioNoff);
   2858  1.1  jmcneill 			mx[index].mx_di.un.e.member[0].ord = 0;
   2859  1.1  jmcneill 			strcpy(mx[index].mx_di.un.e.member[1].label.name, AudioNon);
   2860  1.1  jmcneill 			mx[index].mx_di.un.e.member[1].ord = 1;
   2861  1.1  jmcneill 			++index;
   2862  1.1  jmcneill 		}
   2863  1.1  jmcneill 	}
   2864  1.1  jmcneill 
   2865  1.1  jmcneill 	/* Build volume mixers */
   2866  1.1  jmcneill 	for (i = 0; i < sc->sc_nctls; i++) {
   2867  1.1  jmcneill 		uint32_t audiodev;
   2868  1.1  jmcneill 
   2869  1.1  jmcneill 		ctl = &sc->sc_ctls[i];
   2870  1.1  jmcneill 		if (ctl->ctl_enable == false ||
   2871  1.1  jmcneill 		    ctl->ctl_audiomask == 0)
   2872  1.1  jmcneill 			continue;
   2873  1.1  jmcneill 		audiodev = ffs(ctl->ctl_audiomask) - 1;
   2874  1.1  jmcneill 		mx[index].mx_ctl = ctl;
   2875  1.1  jmcneill 		mx[index].mx_di.index = index;
   2876  1.1  jmcneill 		mx[index].mx_di.type = AUDIO_MIXER_VALUE;
   2877  1.1  jmcneill 		mx[index].mx_di.prev = mx[index].mx_di.next = AUDIO_MIXER_LAST;
   2878  1.1  jmcneill 		mx[index].mx_di.un.v.num_channels = 2;	/* XXX */
   2879  1.1  jmcneill 		mx[index].mx_di.un.v.delta = 256 / (ctl->ctl_step + 1);
   2880  1.1  jmcneill 		if (ctrlcnt[audiodev] > 0)
   2881  1.1  jmcneill 			snprintf(mx[index].mx_di.label.name,
   2882  1.1  jmcneill 			    sizeof(mx[index].mx_di.label.name),
   2883  1.1  jmcneill 			    "%s%d",
   2884  1.1  jmcneill 			    hdafg_mixer_names[audiodev],
   2885  1.1  jmcneill 			    ctrlcnt[audiodev] + 1);
   2886  1.1  jmcneill 		else
   2887  1.1  jmcneill 			strcpy(mx[index].mx_di.label.name,
   2888  1.1  jmcneill 			    hdafg_mixer_names[audiodev]);
   2889  1.1  jmcneill 		ctrlcnt[audiodev]++;
   2890  1.1  jmcneill 
   2891  1.1  jmcneill 		switch (audiodev) {
   2892  1.1  jmcneill 		case HDAUDIO_MIXER_VOLUME:
   2893  1.1  jmcneill 		case HDAUDIO_MIXER_BASS:
   2894  1.1  jmcneill 		case HDAUDIO_MIXER_TREBLE:
   2895  1.1  jmcneill 		case HDAUDIO_MIXER_OGAIN:
   2896  1.1  jmcneill 			mx[index].mx_di.mixer_class =
   2897  1.1  jmcneill 			    HDAUDIO_MIXER_CLASS_OUTPUTS;
   2898  1.1  jmcneill 			hda_trace(sc, "  adding outputs.%s\n",
   2899  1.1  jmcneill 			    mx[index].mx_di.label.name);
   2900  1.1  jmcneill 			break;
   2901  1.1  jmcneill 		case HDAUDIO_MIXER_MIC:
   2902  1.1  jmcneill 		case HDAUDIO_MIXER_MONITOR:
   2903  1.1  jmcneill 			mx[index].mx_di.mixer_class =
   2904  1.1  jmcneill 			    HDAUDIO_MIXER_CLASS_RECORD;
   2905  1.1  jmcneill 			hda_trace(sc, "  adding record.%s\n",
   2906  1.1  jmcneill 			    mx[index].mx_di.label.name);
   2907  1.1  jmcneill 			break;
   2908  1.1  jmcneill 		default:
   2909  1.1  jmcneill 			mx[index].mx_di.mixer_class =
   2910  1.1  jmcneill 			    HDAUDIO_MIXER_CLASS_INPUTS;
   2911  1.1  jmcneill 			hda_trace(sc, "  adding inputs.%s\n",
   2912  1.1  jmcneill 			    mx[index].mx_di.label.name);
   2913  1.1  jmcneill 			break;
   2914  1.1  jmcneill 		}
   2915  1.1  jmcneill 		strcpy(mx[index].mx_di.un.v.units.name, AudioNvolume);
   2916  1.1  jmcneill 
   2917  1.1  jmcneill 		++index;
   2918  1.1  jmcneill 
   2919  1.1  jmcneill 		if (ctl->ctl_mute) {
   2920  1.1  jmcneill 			mx[index] = mx[index - 1];
   2921  1.1  jmcneill 			mx[index].mx_di.index = index;
   2922  1.1  jmcneill 			mx[index].mx_di.type = AUDIO_MIXER_ENUM;
   2923  1.1  jmcneill 			mx[index].mx_di.prev = mx[index].mx_di.next = AUDIO_MIXER_LAST;
   2924  1.1  jmcneill 			snprintf(mx[index].mx_di.label.name,
   2925  1.1  jmcneill 			    sizeof(mx[index].mx_di.label.name),
   2926  1.1  jmcneill 			    "%s." AudioNmute,
   2927  1.1  jmcneill 			    mx[index - 1].mx_di.label.name);
   2928  1.1  jmcneill 			mx[index].mx_di.un.e.num_mem = 2;
   2929  1.1  jmcneill 			strcpy(mx[index].mx_di.un.e.member[0].label.name, AudioNoff);
   2930  1.1  jmcneill 			mx[index].mx_di.un.e.member[0].ord = 0;
   2931  1.1  jmcneill 			strcpy(mx[index].mx_di.un.e.member[1].label.name, AudioNon);
   2932  1.1  jmcneill 			mx[index].mx_di.un.e.member[1].ord = 1;
   2933  1.1  jmcneill 			++index;
   2934  1.1  jmcneill 		}
   2935  1.1  jmcneill 	}
   2936  1.1  jmcneill 
   2937  1.1  jmcneill 	/* DAC selector */
   2938  1.1  jmcneill 	if (ndac > 0) {
   2939  1.1  jmcneill 		mx[index].mx_ctl = NULL;
   2940  1.1  jmcneill 		mx[index].mx_di.index = index;
   2941  1.1  jmcneill 		mx[index].mx_di.type = AUDIO_MIXER_SET;
   2942  1.1  jmcneill 		mx[index].mx_di.mixer_class = HDAUDIO_MIXER_CLASS_OUTPUTS;
   2943  1.1  jmcneill 		mx[index].mx_di.prev = mx[index].mx_di.next = AUDIO_MIXER_LAST;
   2944  1.1  jmcneill 		strcpy(mx[index].mx_di.label.name, "dacsel"); /* AudioNselect */
   2945  1.1  jmcneill 		mx[index].mx_di.un.s.num_mem = ndac;
   2946  1.1  jmcneill 		for (i = 0, j = 0; i < sc->sc_nassocs; i++) {
   2947  1.1  jmcneill 			if (sc->sc_assocs[i].as_enable == false)
   2948  1.1  jmcneill 				continue;
   2949  1.1  jmcneill 			if (sc->sc_assocs[i].as_dir != HDAUDIO_PINDIR_OUT)
   2950  1.1  jmcneill 				continue;
   2951  1.1  jmcneill 			mx[index].mx_di.un.s.member[j].mask = 1 << i;
   2952  1.1  jmcneill 			snprintf(mx[index].mx_di.un.s.member[j].label.name,
   2953  1.1  jmcneill 			    sizeof(mx[index].mx_di.un.s.member[j].label.name),
   2954  1.1  jmcneill 			    "%s%02X",
   2955  1.1  jmcneill 			    hdafg_assoc_type_string(&sc->sc_assocs[i]), i);
   2956  1.1  jmcneill 			++j;
   2957  1.1  jmcneill 		}
   2958  1.1  jmcneill 		++index;
   2959  1.1  jmcneill 	}
   2960  1.1  jmcneill 
   2961  1.1  jmcneill 	/* ADC selector */
   2962  1.1  jmcneill 	if (nadc > 0) {
   2963  1.1  jmcneill 		mx[index].mx_ctl = NULL;
   2964  1.1  jmcneill 		mx[index].mx_di.index = index;
   2965  1.1  jmcneill 		mx[index].mx_di.type = AUDIO_MIXER_SET;
   2966  1.1  jmcneill 		mx[index].mx_di.mixer_class = HDAUDIO_MIXER_CLASS_RECORD;
   2967  1.1  jmcneill 		mx[index].mx_di.prev = mx[index].mx_di.next = AUDIO_MIXER_LAST;
   2968  1.1  jmcneill 		strcpy(mx[index].mx_di.label.name, AudioNsource);
   2969  1.1  jmcneill 		mx[index].mx_di.un.s.num_mem = nadc;
   2970  1.1  jmcneill 		for (i = 0, j = 0; i < sc->sc_nassocs; i++) {
   2971  1.1  jmcneill 			if (sc->sc_assocs[i].as_enable == false)
   2972  1.1  jmcneill 				continue;
   2973  1.1  jmcneill 			if (sc->sc_assocs[i].as_dir != HDAUDIO_PINDIR_IN)
   2974  1.1  jmcneill 				continue;
   2975  1.1  jmcneill 			mx[index].mx_di.un.s.member[j].mask = 1 << i;
   2976  1.1  jmcneill 			snprintf(mx[index].mx_di.un.s.member[j].label.name,
   2977  1.1  jmcneill 			    sizeof(mx[index].mx_di.un.s.member[j].label.name),
   2978  1.1  jmcneill 			    "%s%02X",
   2979  1.1  jmcneill 			    hdafg_assoc_type_string(&sc->sc_assocs[i]), i);
   2980  1.1  jmcneill 			++j;
   2981  1.1  jmcneill 		}
   2982  1.1  jmcneill 		++index;
   2983  1.1  jmcneill 	}
   2984  1.1  jmcneill 
   2985  1.1  jmcneill 	sc->sc_mixers = mx;
   2986  1.1  jmcneill }
   2987  1.1  jmcneill 
   2988  1.1  jmcneill static void
   2989  1.1  jmcneill hdafg_commit(struct hdafg_softc *sc)
   2990  1.1  jmcneill {
   2991  1.1  jmcneill 	struct hdaudio_widget *w;
   2992  1.1  jmcneill 	uint32_t gdata, gmask, gdir;
   2993  1.1  jmcneill 	int commitgpio;
   2994  1.1  jmcneill 	int i;
   2995  1.1  jmcneill 
   2996  1.1  jmcneill 	/* Commit controls */
   2997  1.1  jmcneill 	hdafg_control_commit(sc);
   2998  1.1  jmcneill 
   2999  1.1  jmcneill 	/* Commit selectors, pins, and EAPD */
   3000  1.1  jmcneill 	for (i = 0; i < sc->sc_nwidgets; i++) {
   3001  1.1  jmcneill 		w = &sc->sc_widgets[i];
   3002  1.1  jmcneill 		if (w->w_selconn == -1)
   3003  1.1  jmcneill 			w->w_selconn = 0;
   3004  1.1  jmcneill 		if (w->w_nconns > 0)
   3005  1.1  jmcneill 			hdafg_widget_connection_select(w, w->w_selconn);
   3006  1.1  jmcneill 		if (w->w_type == COP_AWCAP_TYPE_PIN_COMPLEX)
   3007  1.1  jmcneill 			hdaudio_command(sc->sc_codec, w->w_nid,
   3008  1.1  jmcneill 			    CORB_SET_PIN_WIDGET_CONTROL, w->w_pin.ctrl);
   3009  1.1  jmcneill 		if (w->w_p.eapdbtl != 0xffffffff)
   3010  1.1  jmcneill 			hdaudio_command(sc->sc_codec, w->w_nid,
   3011  1.1  jmcneill 			    CORB_SET_EAPD_BTL_ENABLE, w->w_p.eapdbtl);
   3012  1.1  jmcneill 	}
   3013  1.1  jmcneill 
   3014  1.1  jmcneill 	gdata = gmask = gdir = commitgpio = 0;
   3015  1.1  jmcneill #ifdef notyet
   3016  1.1  jmcneill 	int numgpio = COP_GPIO_COUNT_NUM_GPIO(sc->sc_p.gpio_cnt);
   3017  1.1  jmcneill 
   3018  1.1  jmcneill 	hda_trace(sc, "found %d GPIOs\n", numgpio);
   3019  1.1  jmcneill 	for (i = 0; i < numgpio && i < 8; i++) {
   3020  1.1  jmcneill 		if (commitgpio == 0)
   3021  1.1  jmcneill 			commitgpio = 1;
   3022  1.1  jmcneill 		gdata |= 1 << i;
   3023  1.1  jmcneill 		gmask |= 1 << i;
   3024  1.1  jmcneill 		gdir |= 1 << i;
   3025  1.1  jmcneill 	}
   3026  1.1  jmcneill #endif
   3027  1.1  jmcneill 
   3028  1.1  jmcneill 	if (commitgpio) {
   3029  1.1  jmcneill 		hda_trace(sc, "GPIO commit: data=%08X mask=%08X dir=%08X\n",
   3030  1.1  jmcneill 		    gdata, gmask, gdir);
   3031  1.1  jmcneill 		hdaudio_command(sc->sc_codec, sc->sc_nid,
   3032  1.1  jmcneill 		    CORB_SET_GPIO_ENABLE_MASK, gmask);
   3033  1.1  jmcneill 		hdaudio_command(sc->sc_codec, sc->sc_nid,
   3034  1.1  jmcneill 		    CORB_SET_GPIO_DIRECTION, gdir);
   3035  1.1  jmcneill 		hdaudio_command(sc->sc_codec, sc->sc_nid,
   3036  1.1  jmcneill 		    CORB_SET_GPIO_DATA, gdata);
   3037  1.1  jmcneill 	}
   3038  1.1  jmcneill }
   3039  1.1  jmcneill 
   3040  1.1  jmcneill static void
   3041  1.1  jmcneill hdafg_stream_connect_hdmi(struct hdafg_softc *sc, struct hdaudio_assoc *as,
   3042  1.1  jmcneill     struct hdaudio_widget *w, int maxchan)
   3043  1.1  jmcneill {
   3044  1.1  jmcneill 	struct hdmi_audio_infoframe hdmi;
   3045  1.1  jmcneill 	/* TODO struct displayport_audio_infoframe dp; */
   3046  1.1  jmcneill 	uint8_t *dip = NULL;
   3047  1.1  jmcneill 	size_t diplen = 0;
   3048  1.1  jmcneill 	int i;
   3049  1.1  jmcneill 
   3050  1.1  jmcneill #ifdef HDAFG_HDMI_DEBUG
   3051  1.1  jmcneill 	uint32_t res;
   3052  1.1  jmcneill 	res = hdaudio_command(sc->sc_codec, w->w_nid,
   3053  1.1  jmcneill 	    CORB_GET_HDMI_DIP_XMIT_CTRL, 0);
   3054  1.1  jmcneill 	hda_print(sc, "connect HDMI nid %02X, xmitctrl = 0x%08X\n",
   3055  1.1  jmcneill 	    w->w_nid, res);
   3056  1.1  jmcneill #endif
   3057  1.1  jmcneill 
   3058  1.1  jmcneill 	/* disable infoframe transmission */
   3059  1.1  jmcneill 	hdaudio_command(sc->sc_codec, w->w_nid,
   3060  1.1  jmcneill 	    CORB_SET_HDMI_DIP_XMIT_CTRL, COP_DIP_XMIT_CTRL_DISABLE);
   3061  1.1  jmcneill 
   3062  1.1  jmcneill 	/* build new infoframe */
   3063  1.1  jmcneill 	if (as->as_digital == HDAFG_AS_HDMI) {
   3064  1.1  jmcneill 		dip = (uint8_t *)&hdmi;
   3065  1.1  jmcneill 		diplen = sizeof(hdmi);
   3066  1.1  jmcneill 		memset(&hdmi, 0, sizeof(hdmi));
   3067  1.1  jmcneill 		hdmi.header.packet_type = HDMI_AI_PACKET_TYPE;
   3068  1.1  jmcneill 		hdmi.header.version = HDMI_AI_VERSION;
   3069  1.1  jmcneill 		hdmi.header.length = HDMI_AI_LENGTH;
   3070  1.1  jmcneill 		hdmi.ct_cc = maxchan - 1;
   3071  1.1  jmcneill 		hdafg_dd_hdmi_ai_cksum(&hdmi);
   3072  1.1  jmcneill 	}
   3073  1.1  jmcneill 	/* update data island with new audio infoframe */
   3074  1.1  jmcneill 	if (dip) {
   3075  1.1  jmcneill 		hdaudio_command(sc->sc_codec, w->w_nid,
   3076  1.1  jmcneill 		    CORB_SET_HDMI_DIP_INDEX, 0);
   3077  1.1  jmcneill 		for (i = 0; i < diplen; i++) {
   3078  1.1  jmcneill 			hdaudio_command(sc->sc_codec, w->w_nid,
   3079  1.1  jmcneill 			    CORB_SET_HDMI_DIP_DATA, dip[i]);
   3080  1.1  jmcneill 		}
   3081  1.1  jmcneill 	}
   3082  1.1  jmcneill 
   3083  1.1  jmcneill 	/* enable infoframe transmission */
   3084  1.1  jmcneill 	hdaudio_command(sc->sc_codec, w->w_nid,
   3085  1.1  jmcneill 	    CORB_SET_HDMI_DIP_XMIT_CTRL, COP_DIP_XMIT_CTRL_BEST_EFFORT);
   3086  1.1  jmcneill }
   3087  1.1  jmcneill 
   3088  1.1  jmcneill static void
   3089  1.1  jmcneill hdafg_stream_connect(struct hdafg_softc *sc, int mode)
   3090  1.1  jmcneill {
   3091  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   3092  1.1  jmcneill 	struct hdaudio_widget *w;
   3093  1.1  jmcneill 	uint16_t fmt, dfmt;
   3094  1.1  jmcneill 	int tag, chn, maxchan, c;
   3095  1.1  jmcneill 	int i, j, k;
   3096  1.1  jmcneill 
   3097  1.1  jmcneill 	KASSERT(mode == AUMODE_PLAY || mode == AUMODE_RECORD);
   3098  1.1  jmcneill 
   3099  1.1  jmcneill 	if (mode == AUMODE_PLAY)
   3100  1.1  jmcneill 		fmt = hdaudio_stream_param(sc->sc_audiodev.ad_playback,
   3101  1.1  jmcneill 		    &sc->sc_pparam);
   3102  1.1  jmcneill 	else
   3103  1.1  jmcneill 		fmt = hdaudio_stream_param(sc->sc_audiodev.ad_capture,
   3104  1.1  jmcneill 		    &sc->sc_rparam);
   3105  1.1  jmcneill 
   3106  1.1  jmcneill 
   3107  1.1  jmcneill 	for (i = 0; i < sc->sc_nassocs; i++) {
   3108  1.1  jmcneill 		if (as[i].as_enable == false)
   3109  1.1  jmcneill 			continue;
   3110  1.1  jmcneill 
   3111  1.1  jmcneill 		if (mode == AUMODE_PLAY && as[i].as_dir != HDAUDIO_PINDIR_OUT)
   3112  1.1  jmcneill 			continue;
   3113  1.1  jmcneill 		if (mode == AUMODE_RECORD && as[i].as_dir != HDAUDIO_PINDIR_IN)
   3114  1.1  jmcneill 			continue;
   3115  1.1  jmcneill 
   3116  1.1  jmcneill 		fmt &= ~HDAUDIO_FMT_CHAN_MASK;
   3117  1.1  jmcneill 		if (as[i].as_dir == HDAUDIO_PINDIR_OUT &&
   3118  1.1  jmcneill 		    sc->sc_audiodev.ad_playback != NULL) {
   3119  1.1  jmcneill 			tag = hdaudio_stream_tag(sc->sc_audiodev.ad_playback);
   3120  1.1  jmcneill 			fmt |= HDAUDIO_FMT_CHAN(sc->sc_pparam.channels);
   3121  1.1  jmcneill 			maxchan = sc->sc_pparam.channels;
   3122  1.1  jmcneill 		} else if (as[i].as_dir == HDAUDIO_PINDIR_IN &&
   3123  1.1  jmcneill 		    sc->sc_audiodev.ad_capture != NULL) {
   3124  1.1  jmcneill 			tag = hdaudio_stream_tag(sc->sc_audiodev.ad_capture);
   3125  1.1  jmcneill 			fmt |= HDAUDIO_FMT_CHAN(sc->sc_rparam.channels);
   3126  1.1  jmcneill 			maxchan = sc->sc_rparam.channels;
   3127  1.1  jmcneill 		} else {
   3128  1.1  jmcneill 			tag = 0;
   3129  1.1  jmcneill 			if (as[i].as_dir == HDAUDIO_PINDIR_OUT) {
   3130  1.1  jmcneill 				fmt |= HDAUDIO_FMT_CHAN(sc->sc_pchan);
   3131  1.1  jmcneill 				maxchan = sc->sc_pchan;
   3132  1.1  jmcneill 			} else {
   3133  1.1  jmcneill 				fmt |= HDAUDIO_FMT_CHAN(sc->sc_rchan);
   3134  1.1  jmcneill 				maxchan = sc->sc_rchan;
   3135  1.1  jmcneill 			}
   3136  1.1  jmcneill 		}
   3137  1.1  jmcneill 
   3138  1.1  jmcneill 		chn = 0;
   3139  1.1  jmcneill 		for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   3140  1.1  jmcneill 			if (as[i].as_dacs[j] == 0)
   3141  1.1  jmcneill 				continue;
   3142  1.1  jmcneill 			w = hdafg_widget_lookup(sc, as[i].as_dacs[j]);
   3143  1.1  jmcneill 			if (w == NULL || w->w_enable == FALSE)
   3144  1.1  jmcneill 				continue;
   3145  1.1  jmcneill 			if (as[i].as_hpredir >= 0 && i == as[i].as_pincnt)
   3146  1.1  jmcneill 				chn = 0;
   3147  1.1  jmcneill 			if (chn >= maxchan)
   3148  1.1  jmcneill 				chn = 0;	/* XXX */
   3149  1.1  jmcneill 			c = (tag << 4) | chn;
   3150  1.1  jmcneill 
   3151  1.1  jmcneill 			if (as[i].as_activated == false)
   3152  1.1  jmcneill 				c = 0;
   3153  1.1  jmcneill 
   3154  1.1  jmcneill 			/*
   3155  1.1  jmcneill 			 * If a non-PCM stream is being connected, and the
   3156  1.1  jmcneill 			 * analog converter doesn't support non-PCM streams,
   3157  1.1  jmcneill 			 * then don't decode it
   3158  1.1  jmcneill 			 */
   3159  1.1  jmcneill 			if (!(w->w_p.aw_cap & COP_AWCAP_DIGITAL) &&
   3160  1.1  jmcneill 			    !(w->w_p.stream_format & COP_STREAM_FORMAT_AC3) &&
   3161  1.1  jmcneill 			    (fmt & HDAUDIO_FMT_TYPE_NONPCM)) {
   3162  1.1  jmcneill 				hdaudio_command(sc->sc_codec, w->w_nid,
   3163  1.1  jmcneill 				    CORB_SET_CONVERTER_STREAM_CHANNEL, 0);
   3164  1.1  jmcneill 				continue;
   3165  1.1  jmcneill 			}
   3166  1.1  jmcneill 
   3167  1.1  jmcneill 			hdaudio_command(sc->sc_codec, w->w_nid,
   3168  1.1  jmcneill 			    CORB_SET_CONVERTER_FORMAT, fmt);
   3169  1.1  jmcneill 			if (w->w_p.aw_cap & COP_AWCAP_DIGITAL) {
   3170  1.1  jmcneill 				dfmt = hdaudio_command(sc->sc_codec, w->w_nid,
   3171  1.1  jmcneill 				    CORB_GET_DIGITAL_CONVERTER_CONTROL, 0) &
   3172  1.1  jmcneill 				    0xff;
   3173  1.1  jmcneill 				dfmt |= COP_DIGITAL_CONVCTRL1_DIGEN;
   3174  1.1  jmcneill 				if (fmt & HDAUDIO_FMT_TYPE_NONPCM)
   3175  1.1  jmcneill 					dfmt |= COP_DIGITAL_CONVCTRL1_NAUDIO;
   3176  1.1  jmcneill 				else
   3177  1.1  jmcneill 					dfmt &= ~COP_DIGITAL_CONVCTRL1_NAUDIO;
   3178  1.1  jmcneill 				hdaudio_command(sc->sc_codec, w->w_nid,
   3179  1.1  jmcneill 				    CORB_SET_DIGITAL_CONVERTER_CONTROL_1, dfmt);
   3180  1.1  jmcneill 			}
   3181  1.1  jmcneill 			if (w->w_pin.cap & (COP_PINCAP_HDMI|COP_PINCAP_DP)) {
   3182  1.1  jmcneill 				hdaudio_command(sc->sc_codec, w->w_nid,
   3183  1.1  jmcneill 				    CORB_SET_CONVERTER_CHANNEL_COUNT,
   3184  1.1  jmcneill 				    maxchan - 1);
   3185  1.1  jmcneill 				for (k = 0; k < maxchan; k++) {
   3186  1.1  jmcneill 					hdaudio_command(sc->sc_codec, w->w_nid,
   3187  1.1  jmcneill 					    CORB_ASP_SET_CHANNEL_MAPPING,
   3188  1.1  jmcneill 					    (k << 4) | k);
   3189  1.1  jmcneill 				}
   3190  1.1  jmcneill 			}
   3191  1.1  jmcneill 			hdaudio_command(sc->sc_codec, w->w_nid,
   3192  1.1  jmcneill 			    CORB_SET_CONVERTER_STREAM_CHANNEL, c);
   3193  1.1  jmcneill 			chn += COP_AWCAP_CHANNEL_COUNT(w->w_p.aw_cap);
   3194  1.1  jmcneill 		}
   3195  1.1  jmcneill 
   3196  1.1  jmcneill 		for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   3197  1.1  jmcneill 			if (as[i].as_pins[j] == 0)
   3198  1.1  jmcneill 				continue;
   3199  1.1  jmcneill 			w = hdafg_widget_lookup(sc, as[i].as_pins[j]);
   3200  1.1  jmcneill 			if (w == NULL || w->w_enable == FALSE)
   3201  1.1  jmcneill 				continue;
   3202  1.1  jmcneill 			if (w->w_pin.cap & (COP_PINCAP_HDMI|COP_PINCAP_DP))
   3203  1.1  jmcneill 				hdafg_stream_connect_hdmi(sc, &as[i],
   3204  1.1  jmcneill 				    w, maxchan);
   3205  1.1  jmcneill 		}
   3206  1.1  jmcneill 	}
   3207  1.1  jmcneill }
   3208  1.1  jmcneill 
   3209  1.1  jmcneill static int
   3210  1.1  jmcneill hdafg_stream_intr(struct hdaudio_stream *st)
   3211  1.1  jmcneill {
   3212  1.1  jmcneill 	struct hdaudio_audiodev *ad = st->st_cookie;
   3213  1.1  jmcneill 	int handled = 0;
   3214  1.1  jmcneill 
   3215  1.1  jmcneill 	(void)hda_read1(ad->ad_sc->sc_host, HDAUDIO_SD_STS(st->st_shift));
   3216  1.1  jmcneill 	hda_write1(ad->ad_sc->sc_host, HDAUDIO_SD_STS(st->st_shift),
   3217  1.1  jmcneill 	    HDAUDIO_STS_DESE | HDAUDIO_STS_FIFOE | HDAUDIO_STS_BCIS);
   3218  1.1  jmcneill 
   3219  1.1  jmcneill 	mutex_spin_enter(&ad->ad_sc->sc_intr_lock);
   3220  1.1  jmcneill 	/* XXX test (sts & HDAUDIO_STS_BCIS)? */
   3221  1.1  jmcneill 	if (st == ad->ad_playback && ad->ad_playbackintr) {
   3222  1.1  jmcneill 		ad->ad_playbackintr(ad->ad_playbackintrarg);
   3223  1.1  jmcneill 		handled = 1;
   3224  1.1  jmcneill 	} else if (st == ad->ad_capture && ad->ad_captureintr) {
   3225  1.1  jmcneill 		ad->ad_captureintr(ad->ad_captureintrarg);
   3226  1.1  jmcneill 		handled = 1;
   3227  1.1  jmcneill 	}
   3228  1.1  jmcneill 	mutex_spin_exit(&ad->ad_sc->sc_intr_lock);
   3229  1.1  jmcneill 
   3230  1.1  jmcneill 	return handled;
   3231  1.1  jmcneill }
   3232  1.1  jmcneill 
   3233  1.1  jmcneill static bool
   3234  1.1  jmcneill hdafg_rate_supported(struct hdafg_softc *sc, u_int frequency)
   3235  1.1  jmcneill {
   3236  1.1  jmcneill 	uint32_t caps = sc->sc_p.pcm_size_rate;
   3237  1.1  jmcneill 
   3238  1.1  jmcneill #define ISFREQOK(shift)	((caps & (1 << (shift))) ? true : false)
   3239  1.1  jmcneill 	switch (frequency) {
   3240  1.1  jmcneill 	case 8000:
   3241  1.1  jmcneill 		return ISFREQOK(0);
   3242  1.1  jmcneill 	case 11025:
   3243  1.1  jmcneill 		return ISFREQOK(1);
   3244  1.1  jmcneill 	case 16000:
   3245  1.1  jmcneill 		return ISFREQOK(2);
   3246  1.1  jmcneill 	case 22050:
   3247  1.1  jmcneill 		return ISFREQOK(3);
   3248  1.1  jmcneill 	case 32000:
   3249  1.1  jmcneill 		return ISFREQOK(4);
   3250  1.1  jmcneill 	case 44100:
   3251  1.1  jmcneill 		return ISFREQOK(5);
   3252  1.1  jmcneill 	case 48000:
   3253  1.1  jmcneill 		return true;	/* Must be supported by all codecs */
   3254  1.1  jmcneill 	case 88200:
   3255  1.1  jmcneill 		return ISFREQOK(7);
   3256  1.1  jmcneill 	case 96000:
   3257  1.1  jmcneill 		return ISFREQOK(8);
   3258  1.1  jmcneill 	case 176400:
   3259  1.1  jmcneill 		return ISFREQOK(9);
   3260  1.1  jmcneill 	case 192000:
   3261  1.1  jmcneill 		return ISFREQOK(10);
   3262  1.1  jmcneill 	case 384000:
   3263  1.1  jmcneill 		return ISFREQOK(11);
   3264  1.1  jmcneill 	default:
   3265  1.1  jmcneill 		return false;
   3266  1.1  jmcneill 	}
   3267  1.1  jmcneill #undef ISFREQOK
   3268  1.1  jmcneill }
   3269  1.1  jmcneill 
   3270  1.1  jmcneill static bool
   3271  1.1  jmcneill hdafg_bits_supported(struct hdafg_softc *sc, u_int bits)
   3272  1.1  jmcneill {
   3273  1.1  jmcneill 	uint32_t caps = sc->sc_p.pcm_size_rate;
   3274  1.1  jmcneill #define ISBITSOK(shift)	((caps & (1 << (shift))) ? true : false)
   3275  1.1  jmcneill 	switch (bits) {
   3276  1.1  jmcneill 	case 8:
   3277  1.1  jmcneill 		return ISBITSOK(16);
   3278  1.1  jmcneill 	case 16:
   3279  1.1  jmcneill 		return ISBITSOK(17);
   3280  1.1  jmcneill 	case 20:
   3281  1.1  jmcneill 		return ISBITSOK(18);
   3282  1.1  jmcneill 	case 24:
   3283  1.1  jmcneill 		return ISBITSOK(19);
   3284  1.1  jmcneill 	case 32:
   3285  1.1  jmcneill 		return ISBITSOK(20);
   3286  1.1  jmcneill 	default:
   3287  1.1  jmcneill 		return false;
   3288  1.1  jmcneill 	}
   3289  1.1  jmcneill #undef ISBITSOK
   3290  1.1  jmcneill }
   3291  1.1  jmcneill 
   3292  1.1  jmcneill static bool
   3293  1.1  jmcneill hdafg_probe_encoding(struct hdafg_softc *sc,
   3294  1.1  jmcneill     u_int validbits, u_int precision, int encoding, bool force)
   3295  1.1  jmcneill {
   3296  1.1  jmcneill 	struct audio_format f;
   3297  1.1  jmcneill 	int i;
   3298  1.1  jmcneill 
   3299  1.1  jmcneill 	if (!force && hdafg_bits_supported(sc, validbits) == false)
   3300  1.1  jmcneill 		return false;
   3301  1.1  jmcneill 
   3302  1.1  jmcneill 	memset(&f, 0, sizeof(f));
   3303  1.1  jmcneill 	f.driver_data = NULL;
   3304  1.1  jmcneill 	f.mode = 0;
   3305  1.1  jmcneill 	f.encoding = encoding;
   3306  1.1  jmcneill 	f.validbits = validbits;
   3307  1.1  jmcneill 	f.precision = precision;
   3308  1.1  jmcneill 	f.channels = 0;
   3309  1.1  jmcneill 	f.channel_mask = 0;
   3310  1.1  jmcneill 	f.frequency_type = 0;
   3311  1.1  jmcneill 	for (i = 0; i < __arraycount(hdafg_possible_rates); i++) {
   3312  1.1  jmcneill 		u_int rate = hdafg_possible_rates[i];
   3313  1.1  jmcneill 		if (hdafg_rate_supported(sc, rate))
   3314  1.1  jmcneill 			f.frequency[f.frequency_type++] = rate;
   3315  1.1  jmcneill 	}
   3316  1.1  jmcneill 
   3317  1.1  jmcneill #define HDAUDIO_INITFMT(ch, chmask)			\
   3318  1.1  jmcneill 	do {						\
   3319  1.1  jmcneill 		f.channels = (ch);			\
   3320  1.1  jmcneill 		f.channel_mask = (chmask);		\
   3321  1.1  jmcneill 		f.mode = 0;				\
   3322  1.1  jmcneill 		if (sc->sc_pchan >= (ch))		\
   3323  1.1  jmcneill 			f.mode |= AUMODE_PLAY;		\
   3324  1.1  jmcneill 		if (sc->sc_rchan >= (ch))		\
   3325  1.1  jmcneill 			f.mode |= AUMODE_RECORD;	\
   3326  1.1  jmcneill 		if (f.mode != 0)			\
   3327  1.1  jmcneill 			hdafg_append_formats(&sc->sc_audiodev, &f); \
   3328  1.1  jmcneill 	} while (0)
   3329  1.1  jmcneill 
   3330  1.1  jmcneill 	/* Commented out, otherwise monaural samples play through left
   3331  1.1  jmcneill 	 * channel only
   3332  1.1  jmcneill 	 */
   3333  1.1  jmcneill 	/* HDAUDIO_INITFMT(1, AUFMT_MONAURAL); */
   3334  1.1  jmcneill 	HDAUDIO_INITFMT(2, AUFMT_STEREO);
   3335  1.1  jmcneill 	HDAUDIO_INITFMT(4, AUFMT_SURROUND4);
   3336  1.1  jmcneill 	HDAUDIO_INITFMT(6, AUFMT_DOLBY_5_1);
   3337  1.1  jmcneill 	HDAUDIO_INITFMT(8, AUFMT_SURROUND_7_1);
   3338  1.1  jmcneill 
   3339  1.1  jmcneill #undef HDAUDIO_INITFMT
   3340  1.1  jmcneill 
   3341  1.1  jmcneill 	return true;
   3342  1.1  jmcneill }
   3343  1.1  jmcneill 
   3344  1.1  jmcneill 
   3345  1.1  jmcneill static void
   3346  1.1  jmcneill hdafg_configure_encodings(struct hdafg_softc *sc)
   3347  1.1  jmcneill {
   3348  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   3349  1.1  jmcneill 	struct hdaudio_widget *w;
   3350  1.1  jmcneill 	struct audio_format f;
   3351  1.1  jmcneill 	uint32_t stream_format, caps;
   3352  1.1  jmcneill 	int nchan, i, nid;
   3353  1.1  jmcneill 
   3354  1.1  jmcneill 	sc->sc_pchan = sc->sc_rchan = 0;
   3355  1.1  jmcneill 
   3356  1.1  jmcneill 	for (nchan = 0, i = 0; i < sc->sc_nassocs; i++) {
   3357  1.1  jmcneill 		nchan = hdafg_assoc_count_channels(sc, &as[i],
   3358  1.1  jmcneill 		    HDAUDIO_PINDIR_OUT);
   3359  1.1  jmcneill 		if (nchan > sc->sc_pchan)
   3360  1.1  jmcneill 			sc->sc_pchan = nchan;
   3361  1.1  jmcneill 	}
   3362  1.1  jmcneill 	for (nchan = 0, i = 0; i < sc->sc_nassocs; i++) {
   3363  1.1  jmcneill 		nchan = hdafg_assoc_count_channels(sc, &as[i],
   3364  1.1  jmcneill 		    HDAUDIO_PINDIR_IN);
   3365  1.1  jmcneill 		if (nchan > sc->sc_rchan)
   3366  1.1  jmcneill 			sc->sc_rchan = nchan;
   3367  1.1  jmcneill 	}
   3368  1.1  jmcneill 	hda_print(sc, "%dch/%dch", sc->sc_pchan, sc->sc_rchan);
   3369  1.1  jmcneill 
   3370  1.1  jmcneill 	for (i = 0; i < __arraycount(hdafg_possible_rates); i++)
   3371  1.1  jmcneill 		if (hdafg_rate_supported(sc,
   3372  1.1  jmcneill 		    hdafg_possible_rates[i]))
   3373  1.1  jmcneill 			hda_print1(sc, " %uHz", hdafg_possible_rates[i]);
   3374  1.1  jmcneill 
   3375  1.1  jmcneill 	stream_format = sc->sc_p.stream_format;
   3376  1.1  jmcneill 	caps = 0;
   3377  1.1  jmcneill 	for (nid = sc->sc_startnode; nid < sc->sc_endnode; nid++) {
   3378  1.1  jmcneill 		w = hdafg_widget_lookup(sc, nid);
   3379  1.1  jmcneill 		if (w == NULL)
   3380  1.1  jmcneill 			continue;
   3381  1.1  jmcneill 		stream_format |= w->w_p.stream_format;
   3382  1.1  jmcneill 		caps |= w->w_p.aw_cap;
   3383  1.1  jmcneill 	}
   3384  1.1  jmcneill 	if (stream_format == 0) {
   3385  1.1  jmcneill 		hda_print(sc,
   3386  1.1  jmcneill 		    "WARNING: unsupported stream format mask 0x%X, assuming PCM\n",
   3387  1.1  jmcneill 		    stream_format);
   3388  1.1  jmcneill 		stream_format |= COP_STREAM_FORMAT_PCM;
   3389  1.1  jmcneill 	}
   3390  1.1  jmcneill 
   3391  1.1  jmcneill 	if (stream_format & COP_STREAM_FORMAT_PCM) {
   3392  1.1  jmcneill 		int e = AUDIO_ENCODING_SLINEAR_LE;
   3393  1.1  jmcneill 		if (hdafg_probe_encoding(sc, 8, 16, e, false))
   3394  1.1  jmcneill 			hda_print1(sc, " PCM8");
   3395  1.1  jmcneill 		if (hdafg_probe_encoding(sc, 16, 16, e, false))
   3396  1.1  jmcneill 			hda_print1(sc, " PCM16");
   3397  1.1  jmcneill 		if (hdafg_probe_encoding(sc, 20, 32, e, false))
   3398  1.1  jmcneill 			hda_print1(sc, " PCM20");
   3399  1.1  jmcneill 		if (hdafg_probe_encoding(sc, 24, 32, e, false))
   3400  1.1  jmcneill 			hda_print1(sc, " PCM24");
   3401  1.1  jmcneill 		if (hdafg_probe_encoding(sc, 32, 32, e, false))
   3402  1.1  jmcneill 			hda_print1(sc, " PCM32");
   3403  1.1  jmcneill 	}
   3404  1.1  jmcneill 
   3405  1.1  jmcneill 	if ((stream_format & COP_STREAM_FORMAT_AC3) ||
   3406  1.1  jmcneill 	    (caps & COP_AWCAP_DIGITAL)) {
   3407  1.1  jmcneill 		int e = AUDIO_ENCODING_AC3;
   3408  1.1  jmcneill 		if (hdafg_probe_encoding(sc, 16, 16, e, false))
   3409  1.1  jmcneill 			hda_print1(sc, " AC3");
   3410  1.1  jmcneill 	}
   3411  1.1  jmcneill 
   3412  1.1  jmcneill 	if (sc->sc_audiodev.ad_nformats == 0) {
   3413  1.1  jmcneill 		hdafg_probe_encoding(sc, 16, 16, AUDIO_ENCODING_SLINEAR_LE, true);
   3414  1.1  jmcneill 		hda_print1(sc, " PCM16*");
   3415  1.1  jmcneill 	}
   3416  1.1  jmcneill 
   3417  1.1  jmcneill 	/*
   3418  1.1  jmcneill 	 * XXX JDM 20090614
   3419  1.1  jmcneill 	 * MI audio assumes that at least one playback and one capture format
   3420  1.1  jmcneill 	 * is reported by the hw driver; until this bug is resolved just
   3421  1.1  jmcneill 	 * report 2ch capabilities if the function group does not support
   3422  1.1  jmcneill 	 * the direction.
   3423  1.1  jmcneill 	 */
   3424  1.1  jmcneill 	if (sc->sc_rchan == 0 || sc->sc_pchan == 0) {
   3425  1.1  jmcneill 		memset(&f, 0, sizeof(f));
   3426  1.1  jmcneill 		f.driver_data = NULL;
   3427  1.1  jmcneill 		f.mode = 0;
   3428  1.1  jmcneill 		f.encoding = AUDIO_ENCODING_SLINEAR_LE;
   3429  1.1  jmcneill 		f.validbits = 16;
   3430  1.1  jmcneill 		f.precision = 16;
   3431  1.1  jmcneill 		f.channels = 2;
   3432  1.1  jmcneill 		f.channel_mask = AUFMT_STEREO;
   3433  1.1  jmcneill 		f.frequency_type = 0;
   3434  1.1  jmcneill 		f.frequency[0] = f.frequency[1] = 48000;
   3435  1.1  jmcneill 		f.mode = AUMODE_PLAY|AUMODE_RECORD;
   3436  1.1  jmcneill 		hdafg_append_formats(&sc->sc_audiodev, &f);
   3437  1.1  jmcneill 	}
   3438  1.1  jmcneill 
   3439  1.1  jmcneill 	hda_print1(sc, "\n");
   3440  1.1  jmcneill }
   3441  1.1  jmcneill 
   3442  1.1  jmcneill static void
   3443  1.1  jmcneill hdafg_hp_switch_handler(void *opaque)
   3444  1.1  jmcneill {
   3445  1.1  jmcneill 	struct hdafg_softc *sc = opaque;
   3446  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   3447  1.1  jmcneill 	struct hdaudio_widget *w;
   3448  1.1  jmcneill 	uint32_t res = 0;
   3449  1.1  jmcneill 	int i, j;
   3450  1.1  jmcneill 
   3451  1.1  jmcneill 	if (!device_is_active(sc->sc_dev))
   3452  1.1  jmcneill 		goto resched;
   3453  1.1  jmcneill 
   3454  1.1  jmcneill 	for (i = 0; i < sc->sc_nassocs; i++) {
   3455  1.1  jmcneill 		if (as[i].as_digital != HDAFG_AS_ANALOG &&
   3456  1.1  jmcneill 		    as[i].as_digital != HDAFG_AS_SPDIF)
   3457  1.1  jmcneill 			continue;
   3458  1.1  jmcneill 		for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   3459  1.1  jmcneill 			if (as[i].as_pins[j] == 0)
   3460  1.1  jmcneill 				continue;
   3461  1.1  jmcneill 			w = hdafg_widget_lookup(sc, as[i].as_pins[j]);
   3462  1.1  jmcneill 			if (w == NULL || w->w_enable == false)
   3463  1.1  jmcneill 				continue;
   3464  1.1  jmcneill 			if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   3465  1.1  jmcneill 				continue;
   3466  1.1  jmcneill 			if (COP_CFG_DEFAULT_DEVICE(w->w_pin.config) !=
   3467  1.1  jmcneill 			    COP_DEVICE_HP_OUT)
   3468  1.1  jmcneill 				continue;
   3469  1.1  jmcneill 			res |= hdaudio_command(sc->sc_codec, as[i].as_pins[j],
   3470  1.1  jmcneill 			    CORB_GET_PIN_SENSE, 0) &
   3471  1.1  jmcneill 			    COP_GET_PIN_SENSE_PRESENSE_DETECT;
   3472  1.1  jmcneill 		}
   3473  1.1  jmcneill 	}
   3474  1.1  jmcneill 
   3475  1.1  jmcneill 	for (i = 0; i < sc->sc_nassocs; i++) {
   3476  1.1  jmcneill 		if (as[i].as_digital != HDAFG_AS_ANALOG &&
   3477  1.1  jmcneill 		    as[i].as_digital != HDAFG_AS_SPDIF)
   3478  1.1  jmcneill 			continue;
   3479  1.1  jmcneill 		for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   3480  1.1  jmcneill 			if (as[i].as_pins[j] == 0)
   3481  1.1  jmcneill 				continue;
   3482  1.1  jmcneill 			w = hdafg_widget_lookup(sc, as[i].as_pins[j]);
   3483  1.1  jmcneill 			if (w == NULL || w->w_enable == false)
   3484  1.1  jmcneill 				continue;
   3485  1.1  jmcneill 			if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   3486  1.1  jmcneill 				continue;
   3487  1.1  jmcneill 			switch (COP_CFG_DEFAULT_DEVICE(w->w_pin.config)) {
   3488  1.1  jmcneill 			case COP_DEVICE_HP_OUT:
   3489  1.1  jmcneill 				if (res & COP_GET_PIN_SENSE_PRESENSE_DETECT)
   3490  1.1  jmcneill 					w->w_pin.ctrl |= COP_PWC_OUT_ENABLE;
   3491  1.1  jmcneill 				else
   3492  1.1  jmcneill 					w->w_pin.ctrl &= ~COP_PWC_OUT_ENABLE;
   3493  1.1  jmcneill 				hdaudio_command(sc->sc_codec, w->w_nid,
   3494  1.1  jmcneill 				    CORB_SET_PIN_WIDGET_CONTROL, w->w_pin.ctrl);
   3495  1.1  jmcneill 				break;
   3496  1.1  jmcneill 			case COP_DEVICE_LINE_OUT:
   3497  1.1  jmcneill 			case COP_DEVICE_SPEAKER:
   3498  1.1  jmcneill 			case COP_DEVICE_AUX:
   3499  1.1  jmcneill 				if (res & COP_GET_PIN_SENSE_PRESENSE_DETECT)
   3500  1.1  jmcneill 					w->w_pin.ctrl &= ~COP_PWC_OUT_ENABLE;
   3501  1.1  jmcneill 				else
   3502  1.1  jmcneill 					w->w_pin.ctrl |= COP_PWC_OUT_ENABLE;
   3503  1.1  jmcneill 				hdaudio_command(sc->sc_codec, w->w_nid,
   3504  1.1  jmcneill 				    CORB_SET_PIN_WIDGET_CONTROL, w->w_pin.ctrl);
   3505  1.1  jmcneill 				break;
   3506  1.1  jmcneill 			default:
   3507  1.1  jmcneill 				break;
   3508  1.1  jmcneill 			}
   3509  1.1  jmcneill 		}
   3510  1.1  jmcneill 	}
   3511  1.1  jmcneill 
   3512  1.1  jmcneill resched:
   3513  1.1  jmcneill 	callout_schedule(&sc->sc_jack_callout, HDAUDIO_HP_SENSE_PERIOD);
   3514  1.1  jmcneill }
   3515  1.1  jmcneill 
   3516  1.1  jmcneill static void
   3517  1.1  jmcneill hdafg_hp_switch_init(struct hdafg_softc *sc)
   3518  1.1  jmcneill {
   3519  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   3520  1.1  jmcneill 	struct hdaudio_widget *w;
   3521  1.1  jmcneill 	bool enable = false;
   3522  1.1  jmcneill 	int i, j;
   3523  1.1  jmcneill 
   3524  1.1  jmcneill 	for (i = 0; i < sc->sc_nassocs; i++) {
   3525  1.1  jmcneill 		if (as[i].as_hpredir < 0 && as[i].as_displaydev == false)
   3526  1.1  jmcneill 			continue;
   3527  1.1  jmcneill 		if (as[i].as_displaydev == false)
   3528  1.1  jmcneill 			w = hdafg_widget_lookup(sc, as[i].as_pins[15]);
   3529  1.1  jmcneill 		else {
   3530  1.1  jmcneill 			w = NULL;
   3531  1.1  jmcneill 			for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   3532  1.1  jmcneill 				if (as[i].as_pins[j] == 0)
   3533  1.1  jmcneill 					continue;
   3534  1.1  jmcneill 				w = hdafg_widget_lookup(sc, as[i].as_pins[j]);
   3535  1.1  jmcneill 				if (w && w->w_enable &&
   3536  1.1  jmcneill 				    w->w_type == COP_AWCAP_TYPE_PIN_COMPLEX)
   3537  1.1  jmcneill 					break;
   3538  1.1  jmcneill 				w = NULL;
   3539  1.1  jmcneill 			}
   3540  1.1  jmcneill 		}
   3541  1.1  jmcneill 		if (w == NULL || w->w_enable == false)
   3542  1.1  jmcneill 			continue;
   3543  1.1  jmcneill 		if (w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   3544  1.1  jmcneill 			continue;
   3545  1.1  jmcneill 		if (!(w->w_pin.cap & COP_PINCAP_PRESENSE_DETECT_CAPABLE)) {
   3546  1.1  jmcneill 			continue;
   3547  1.1  jmcneill 		}
   3548  1.1  jmcneill 		if (COP_CFG_MISC(w->w_pin.config) & 1) {
   3549  1.1  jmcneill 			hda_trace(sc, "no presence detect on pin %02X\n",
   3550  1.1  jmcneill 			    w->w_nid);
   3551  1.1  jmcneill 			continue;
   3552  1.1  jmcneill 		}
   3553  1.1  jmcneill 		if ((w->w_pin.cap & (COP_PINCAP_HDMI|COP_PINCAP_DP)) == 0)
   3554  1.1  jmcneill 			enable = true;
   3555  1.1  jmcneill 
   3556  1.1  jmcneill 		if (w->w_p.aw_cap & COP_AWCAP_UNSOL_CAPABLE) {
   3557  1.1  jmcneill 			uint8_t val = COP_SET_UNSOLICITED_RESPONSE_ENABLE;
   3558  1.1  jmcneill 			if (w->w_pin.cap & (COP_PINCAP_HDMI|COP_PINCAP_DP))
   3559  1.1  jmcneill 				val |= HDAUDIO_UNSOLTAG_EVENT_DD;
   3560  1.1  jmcneill 			else
   3561  1.1  jmcneill 				val |= HDAUDIO_UNSOLTAG_EVENT_HP;
   3562  1.1  jmcneill 
   3563  1.1  jmcneill 			hdaudio_command(sc->sc_codec, w->w_nid,
   3564  1.1  jmcneill 			    CORB_SET_UNSOLICITED_RESPONSE, val);
   3565  1.1  jmcneill 
   3566  1.1  jmcneill 			hdaudio_command(sc->sc_codec, w->w_nid,
   3567  1.1  jmcneill 			    CORB_SET_AMPLIFIER_GAIN_MUTE, 0xb000);
   3568  1.1  jmcneill 		}
   3569  1.1  jmcneill 
   3570  1.1  jmcneill 		hda_trace(sc, "presence detect [pin=%02X,%s",
   3571  1.1  jmcneill 		    w->w_nid,
   3572  1.1  jmcneill 		    (w->w_p.aw_cap & COP_AWCAP_UNSOL_CAPABLE) ?
   3573  1.1  jmcneill 		     "unsol" : "poll"
   3574  1.1  jmcneill 		    );
   3575  1.1  jmcneill 		if (w->w_pin.cap & COP_PINCAP_HDMI)
   3576  1.1  jmcneill 			hda_trace1(sc, ",hdmi");
   3577  1.1  jmcneill 		if (w->w_pin.cap & COP_PINCAP_DP)
   3578  1.1  jmcneill 			hda_trace1(sc, ",displayport");
   3579  1.1  jmcneill 		hda_trace1(sc, "]\n");
   3580  1.1  jmcneill 	}
   3581  1.1  jmcneill 	if (enable) {
   3582  1.1  jmcneill 		sc->sc_jack_polling = true;
   3583  1.1  jmcneill 		hdafg_hp_switch_handler(sc);
   3584  1.1  jmcneill 	} else
   3585  1.1  jmcneill 		hda_trace(sc, "jack detect not enabled\n");
   3586  1.1  jmcneill }
   3587  1.1  jmcneill 
   3588  1.1  jmcneill static void
   3589  1.1  jmcneill hdafg_attach(device_t parent, device_t self, void *opaque)
   3590  1.1  jmcneill {
   3591  1.1  jmcneill 	struct hdafg_softc *sc = device_private(self);
   3592  1.1  jmcneill 	audio_params_t defparams;
   3593  1.1  jmcneill 	prop_dictionary_t args = opaque;
   3594  1.1  jmcneill 	char vendor[16], product[16];
   3595  1.1  jmcneill 	uint64_t fgptr = 0;
   3596  1.1  jmcneill 	uint32_t astype = 0;
   3597  1.1  jmcneill 	uint8_t nid = 0;
   3598  1.1  jmcneill 	int err, i;
   3599  1.1  jmcneill 	bool rv;
   3600  1.1  jmcneill 
   3601  1.1  jmcneill 	aprint_naive("\n");
   3602  1.1  jmcneill 	sc->sc_dev = self;
   3603  1.1  jmcneill 
   3604  1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
   3605  1.1  jmcneill 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
   3606  1.1  jmcneill 
   3607  1.1  jmcneill 	callout_init(&sc->sc_jack_callout, 0);
   3608  1.1  jmcneill 	callout_setfunc(&sc->sc_jack_callout,
   3609  1.1  jmcneill 	    hdafg_hp_switch_handler, sc);
   3610  1.1  jmcneill 
   3611  1.1  jmcneill 	if (!pmf_device_register(self, hdafg_suspend, hdafg_resume))
   3612  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
   3613  1.1  jmcneill 
   3614  1.1  jmcneill 	sc->sc_config = prop_dictionary_get(args, "pin-config");
   3615  1.1  jmcneill 	if (sc->sc_config && prop_object_type(sc->sc_config) != PROP_TYPE_ARRAY)
   3616  1.1  jmcneill 		sc->sc_config = NULL;
   3617  1.1  jmcneill 
   3618  1.1  jmcneill 	prop_dictionary_get_uint16(args, "vendor-id", &sc->sc_vendor);
   3619  1.1  jmcneill 	prop_dictionary_get_uint16(args, "product-id", &sc->sc_product);
   3620  1.1  jmcneill 	hdaudio_findvendor(vendor, sizeof(vendor), sc->sc_vendor);
   3621  1.1  jmcneill 	hdaudio_findproduct(product, sizeof(product), sc->sc_vendor,
   3622  1.1  jmcneill 	    sc->sc_product);
   3623  1.1  jmcneill 	hda_print1(sc, ": %s %s%s\n", vendor, product,
   3624  1.1  jmcneill 	    sc->sc_config ? " (custom configuration)" : "");
   3625  1.1  jmcneill 
   3626  1.1  jmcneill 	rv = prop_dictionary_get_uint64(args, "function-group", &fgptr);
   3627  1.1  jmcneill 	if (rv == false || fgptr == 0) {
   3628  1.1  jmcneill 		hda_error(sc, "missing function-group property\n");
   3629  1.1  jmcneill 		return;
   3630  1.1  jmcneill 	}
   3631  1.1  jmcneill 	rv = prop_dictionary_get_uint8(args, "node-id", &nid);
   3632  1.1  jmcneill 	if (rv == false || nid == 0) {
   3633  1.1  jmcneill 		hda_error(sc, "missing node-id property\n");
   3634  1.1  jmcneill 		return;
   3635  1.1  jmcneill 	}
   3636  1.1  jmcneill 
   3637  1.1  jmcneill 	prop_dictionary_set_uint64(device_properties(self),
   3638  1.1  jmcneill 	    "codecinfo-callback",
   3639  1.1  jmcneill 	    (uint64_t)(uintptr_t)hdafg_codec_info);
   3640  1.1  jmcneill 	prop_dictionary_set_uint64(device_properties(self),
   3641  1.1  jmcneill 	    "widgetinfo-callback",
   3642  1.1  jmcneill 	    (uint64_t)(uintptr_t)hdafg_widget_info);
   3643  1.1  jmcneill 
   3644  1.1  jmcneill 	sc->sc_nid = nid;
   3645  1.1  jmcneill 	sc->sc_fg = (struct hdaudio_function_group *)(vaddr_t)fgptr;
   3646  1.1  jmcneill 	sc->sc_fg->fg_unsol = hdafg_unsol;
   3647  1.1  jmcneill 	sc->sc_codec = sc->sc_fg->fg_codec;
   3648  1.1  jmcneill 	KASSERT(sc->sc_codec != NULL);
   3649  1.1  jmcneill 	sc->sc_host = sc->sc_codec->co_host;
   3650  1.1  jmcneill 	KASSERT(sc->sc_host != NULL);
   3651  1.1  jmcneill 
   3652  1.1  jmcneill 	hda_debug(sc, "parsing widgets\n");
   3653  1.1  jmcneill 	hdafg_parse(sc);
   3654  1.1  jmcneill 	hda_debug(sc, "parsing controls\n");
   3655  1.1  jmcneill 	hdafg_control_parse(sc);
   3656  1.1  jmcneill 	hda_debug(sc, "disabling non-audio devices\n");
   3657  1.1  jmcneill 	hdafg_disable_nonaudio(sc);
   3658  1.1  jmcneill 	hda_debug(sc, "disabling useless devices\n");
   3659  1.1  jmcneill 	hdafg_disable_useless(sc);
   3660  1.1  jmcneill 	hda_debug(sc, "parsing associations\n");
   3661  1.1  jmcneill 	hdafg_assoc_parse(sc);
   3662  1.1  jmcneill 	hda_debug(sc, "building tree\n");
   3663  1.1  jmcneill 	hdafg_build_tree(sc);
   3664  1.1  jmcneill 	hda_debug(sc, "disabling unassociated pins\n");
   3665  1.1  jmcneill 	hdafg_disable_unassoc(sc);
   3666  1.1  jmcneill 	hda_debug(sc, "disabling unselected pins\n");
   3667  1.1  jmcneill 	hdafg_disable_unsel(sc);
   3668  1.1  jmcneill 	hda_debug(sc, "disabling useless devices\n");
   3669  1.1  jmcneill 	hdafg_disable_useless(sc);
   3670  1.1  jmcneill 	hda_debug(sc, "disabling cross-associated pins\n");
   3671  1.1  jmcneill 	hdafg_disable_crossassoc(sc);
   3672  1.1  jmcneill 	hda_debug(sc, "disabling useless devices\n");
   3673  1.1  jmcneill 	hdafg_disable_useless(sc);
   3674  1.1  jmcneill 
   3675  1.1  jmcneill 	hda_debug(sc, "assigning mixer names to sound sources\n");
   3676  1.1  jmcneill 	hdafg_assign_names(sc);
   3677  1.1  jmcneill 	hda_debug(sc, "assigning mixers to device tree\n");
   3678  1.1  jmcneill 	hdafg_assign_mixers(sc);
   3679  1.1  jmcneill 
   3680  1.1  jmcneill 	hda_debug(sc, "preparing pin controls\n");
   3681  1.1  jmcneill 	hdafg_prepare_pin_controls(sc);
   3682  1.1  jmcneill 	hda_debug(sc, "commiting settings\n");
   3683  1.1  jmcneill 	hdafg_commit(sc);
   3684  1.1  jmcneill 
   3685  1.1  jmcneill 	hda_debug(sc, "setup jack sensing\n");
   3686  1.1  jmcneill 	hdafg_hp_switch_init(sc);
   3687  1.1  jmcneill 
   3688  1.1  jmcneill 	hda_debug(sc, "building mixer controls\n");
   3689  1.1  jmcneill 	hdafg_build_mixers(sc);
   3690  1.1  jmcneill 
   3691  1.1  jmcneill 	hdafg_dump(sc);
   3692  1.1  jmcneill 	if (1) hdafg_widget_pin_dump(sc);
   3693  1.1  jmcneill 	hdafg_assoc_dump(sc);
   3694  1.1  jmcneill 
   3695  1.1  jmcneill 	hda_debug(sc, "enabling analog beep\n");
   3696  1.1  jmcneill 	hdafg_enable_analog_beep(sc);
   3697  1.1  jmcneill 
   3698  1.1  jmcneill 	hda_debug(sc, "configuring encodings\n");
   3699  1.1  jmcneill 	sc->sc_audiodev.ad_sc = sc;
   3700  1.1  jmcneill 	hdafg_configure_encodings(sc);
   3701  1.1  jmcneill 	err = auconv_create_encodings(sc->sc_audiodev.ad_formats,
   3702  1.1  jmcneill 	    sc->sc_audiodev.ad_nformats, &sc->sc_audiodev.ad_encodings);
   3703  1.1  jmcneill 	if (err) {
   3704  1.1  jmcneill 		hda_error(sc, "couldn't create encodings\n");
   3705  1.1  jmcneill 		return;
   3706  1.1  jmcneill 	}
   3707  1.1  jmcneill 
   3708  1.1  jmcneill 	hda_debug(sc, "reserving streams\n");
   3709  1.1  jmcneill 	sc->sc_audiodev.ad_capture = hdaudio_stream_establish(sc->sc_host,
   3710  1.1  jmcneill 	    HDAUDIO_STREAM_ISS, hdafg_stream_intr, &sc->sc_audiodev);
   3711  1.1  jmcneill 	sc->sc_audiodev.ad_playback = hdaudio_stream_establish(sc->sc_host,
   3712  1.1  jmcneill 	    HDAUDIO_STREAM_OSS, hdafg_stream_intr, &sc->sc_audiodev);
   3713  1.1  jmcneill 
   3714  1.1  jmcneill 	hda_debug(sc, "connecting streams\n");
   3715  1.1  jmcneill 	defparams.channels = 2;
   3716  1.1  jmcneill 	defparams.sample_rate = 48000;
   3717  1.1  jmcneill 	defparams.precision = defparams.validbits = 16;
   3718  1.1  jmcneill 	defparams.encoding = AUDIO_ENCODING_SLINEAR_LE;
   3719  1.1  jmcneill 	sc->sc_pparam = sc->sc_rparam = defparams;
   3720  1.1  jmcneill 	hdafg_stream_connect(sc, AUMODE_PLAY);
   3721  1.1  jmcneill 	hdafg_stream_connect(sc, AUMODE_RECORD);
   3722  1.1  jmcneill 
   3723  1.1  jmcneill 	for (i = 0; i < sc->sc_nassocs; i++) {
   3724  1.1  jmcneill 		astype |= (1 << sc->sc_assocs[i].as_digital);
   3725  1.1  jmcneill 	}
   3726  1.1  jmcneill 	hda_debug(sc, "assoc type mask: %x\n", astype);
   3727  1.1  jmcneill 
   3728  1.1  jmcneill #ifndef HDAUDIO_ENABLE_SPDIF
   3729  1.1  jmcneill 	astype &= ~(1 << HDAFG_AS_SPDIF);
   3730  1.1  jmcneill #endif
   3731  1.1  jmcneill #ifndef HDAUDIO_ENABLE_HDMI
   3732  1.1  jmcneill 	astype &= ~(1 << HDAFG_AS_HDMI);
   3733  1.1  jmcneill #endif
   3734  1.1  jmcneill #ifndef HDAUDIO_ENABLE_DISPLAYPORT
   3735  1.1  jmcneill 	astype &= ~(1 << HDAFG_AS_DISPLAYPORT);
   3736  1.1  jmcneill #endif
   3737  1.1  jmcneill 
   3738  1.1  jmcneill 	if (astype == 0)
   3739  1.1  jmcneill 		return;
   3740  1.1  jmcneill 
   3741  1.1  jmcneill 	hda_debug(sc, "attaching audio device\n");
   3742  1.1  jmcneill 	sc->sc_audiodev.ad_audiodev = audio_attach_mi(&hdafg_hw_if,
   3743  1.1  jmcneill 	    &sc->sc_audiodev, self);
   3744  1.1  jmcneill }
   3745  1.1  jmcneill 
   3746  1.1  jmcneill static int
   3747  1.1  jmcneill hdafg_detach(device_t self, int flags)
   3748  1.1  jmcneill {
   3749  1.1  jmcneill 	struct hdafg_softc *sc = device_private(self);
   3750  1.1  jmcneill 	struct hdaudio_widget *wl, *w = sc->sc_widgets;
   3751  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   3752  1.1  jmcneill 	struct hdaudio_control *ctl = sc->sc_ctls;
   3753  1.1  jmcneill 	struct hdaudio_mixer *mx = sc->sc_mixers;
   3754  1.1  jmcneill 	int nid;
   3755  1.1  jmcneill 
   3756  1.1  jmcneill 	callout_halt(&sc->sc_jack_callout, NULL);
   3757  1.1  jmcneill 	callout_destroy(&sc->sc_jack_callout);
   3758  1.1  jmcneill 
   3759  1.1  jmcneill 	if (sc->sc_config)
   3760  1.1  jmcneill 		prop_object_release(sc->sc_config);
   3761  1.1  jmcneill 	if (sc->sc_audiodev.ad_audiodev)
   3762  1.1  jmcneill 		config_detach(sc->sc_audiodev.ad_audiodev, flags);
   3763  1.1  jmcneill 	if (sc->sc_audiodev.ad_encodings)
   3764  1.1  jmcneill 		auconv_delete_encodings(sc->sc_audiodev.ad_encodings);
   3765  1.1  jmcneill 	if (sc->sc_audiodev.ad_playback)
   3766  1.1  jmcneill 		hdaudio_stream_disestablish(sc->sc_audiodev.ad_playback);
   3767  1.1  jmcneill 	if (sc->sc_audiodev.ad_capture)
   3768  1.1  jmcneill 		hdaudio_stream_disestablish(sc->sc_audiodev.ad_capture);
   3769  1.1  jmcneill 
   3770  1.1  jmcneill 	/* restore bios pin widget configuration */
   3771  1.1  jmcneill 	for (nid = sc->sc_startnode; nid < sc->sc_endnode; nid++) {
   3772  1.1  jmcneill 		wl = hdafg_widget_lookup(sc, nid);
   3773  1.1  jmcneill 		if (wl == NULL || wl->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   3774  1.1  jmcneill 			continue;
   3775  1.1  jmcneill 		hdafg_widget_setconfig(wl, wl->w_pin.biosconfig);
   3776  1.1  jmcneill 	}
   3777  1.1  jmcneill 
   3778  1.1  jmcneill 	if (w)
   3779  1.1  jmcneill 		kmem_free(w, sc->sc_nwidgets * sizeof(*w));
   3780  1.1  jmcneill 	if (as)
   3781  1.1  jmcneill 		kmem_free(as, sc->sc_nassocs * sizeof(*as));
   3782  1.1  jmcneill 	if (ctl)
   3783  1.1  jmcneill 		kmem_free(ctl, sc->sc_nctls * sizeof(*ctl));
   3784  1.1  jmcneill 	if (mx)
   3785  1.1  jmcneill 		kmem_free(mx, sc->sc_nmixers * sizeof(*mx));
   3786  1.1  jmcneill 
   3787  1.1  jmcneill 	mutex_destroy(&sc->sc_lock);
   3788  1.1  jmcneill 	mutex_destroy(&sc->sc_intr_lock);
   3789  1.1  jmcneill 
   3790  1.1  jmcneill 	pmf_device_deregister(self);
   3791  1.1  jmcneill 
   3792  1.1  jmcneill 	return 0;
   3793  1.1  jmcneill }
   3794  1.1  jmcneill 
   3795  1.1  jmcneill static void
   3796  1.1  jmcneill hdafg_childdet(device_t self, device_t child)
   3797  1.1  jmcneill {
   3798  1.1  jmcneill 	struct hdafg_softc *sc = device_private(self);
   3799  1.1  jmcneill 
   3800  1.1  jmcneill 	if (child == sc->sc_audiodev.ad_audiodev)
   3801  1.1  jmcneill 		sc->sc_audiodev.ad_audiodev = NULL;
   3802  1.1  jmcneill }
   3803  1.1  jmcneill 
   3804  1.1  jmcneill static bool
   3805  1.1  jmcneill hdafg_suspend(device_t self, const pmf_qual_t *qual)
   3806  1.1  jmcneill {
   3807  1.1  jmcneill 	struct hdafg_softc *sc = device_private(self);
   3808  1.1  jmcneill 
   3809  1.1  jmcneill 	callout_halt(&sc->sc_jack_callout, NULL);
   3810  1.1  jmcneill 
   3811  1.1  jmcneill 	return true;
   3812  1.1  jmcneill }
   3813  1.1  jmcneill 
   3814  1.1  jmcneill static bool
   3815  1.1  jmcneill hdafg_resume(device_t self, const pmf_qual_t *qual)
   3816  1.1  jmcneill {
   3817  1.1  jmcneill 	struct hdafg_softc *sc = device_private(self);
   3818  1.1  jmcneill 	struct hdaudio_widget *w;
   3819  1.1  jmcneill 	int nid;
   3820  1.1  jmcneill 
   3821  1.1  jmcneill 	hdaudio_command(sc->sc_codec, sc->sc_nid,
   3822  1.1  jmcneill 	    CORB_SET_POWER_STATE, COP_POWER_STATE_D0);
   3823  1.1  jmcneill 	hda_delay(100);
   3824  1.1  jmcneill 	for (nid = sc->sc_startnode; nid < sc->sc_endnode; nid++) {
   3825  1.1  jmcneill 		hdaudio_command(sc->sc_codec, nid,
   3826  1.1  jmcneill 		    CORB_SET_POWER_STATE, COP_POWER_STATE_D0);
   3827  1.1  jmcneill 		w = hdafg_widget_lookup(sc, nid);
   3828  1.1  jmcneill 
   3829  1.1  jmcneill 		/* restore pin widget configuration */
   3830  1.1  jmcneill 		if (w == NULL || w->w_type != COP_AWCAP_TYPE_PIN_COMPLEX)
   3831  1.1  jmcneill 			continue;
   3832  1.1  jmcneill 		hdafg_widget_setconfig(w, w->w_pin.config);
   3833  1.1  jmcneill 	}
   3834  1.1  jmcneill 	hda_delay(1000);
   3835  1.1  jmcneill 
   3836  1.1  jmcneill 	hdafg_commit(sc);
   3837  1.1  jmcneill 	hdafg_stream_connect(sc, AUMODE_PLAY);
   3838  1.1  jmcneill 	hdafg_stream_connect(sc, AUMODE_RECORD);
   3839  1.1  jmcneill 
   3840  1.1  jmcneill 	if (sc->sc_jack_polling)
   3841  1.1  jmcneill 		hdafg_hp_switch_handler(sc);
   3842  1.1  jmcneill 
   3843  1.1  jmcneill 	return true;
   3844  1.1  jmcneill }
   3845  1.1  jmcneill 
   3846  1.1  jmcneill static int
   3847  1.1  jmcneill hdafg_query_encoding(void *opaque, struct audio_encoding *ae)
   3848  1.1  jmcneill {
   3849  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   3850  1.1  jmcneill 	return auconv_query_encoding(ad->ad_encodings, ae);
   3851  1.1  jmcneill }
   3852  1.1  jmcneill 
   3853  1.1  jmcneill static int
   3854  1.1  jmcneill hdafg_set_params(void *opaque, int setmode, int usemode,
   3855  1.1  jmcneill     audio_params_t *play, audio_params_t *rec,
   3856  1.1  jmcneill     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
   3857  1.1  jmcneill {
   3858  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   3859  1.1  jmcneill 	int index;
   3860  1.1  jmcneill 
   3861  1.1  jmcneill 	if (play && (setmode & AUMODE_PLAY)) {
   3862  1.1  jmcneill 		index = auconv_set_converter(ad->ad_formats, ad->ad_nformats,
   3863  1.1  jmcneill 		    AUMODE_PLAY, play, TRUE, pfil);
   3864  1.1  jmcneill 		if (index < 0)
   3865  1.1  jmcneill 			return EINVAL;
   3866  1.1  jmcneill 		ad->ad_sc->sc_pparam = *play;
   3867  1.1  jmcneill 		hdafg_stream_connect(ad->ad_sc, AUMODE_PLAY);
   3868  1.1  jmcneill 	}
   3869  1.1  jmcneill 	if (rec && (setmode & AUMODE_RECORD)) {
   3870  1.1  jmcneill 		index = auconv_set_converter(ad->ad_formats, ad->ad_nformats,
   3871  1.1  jmcneill 		    AUMODE_RECORD, rec, TRUE, rfil);
   3872  1.1  jmcneill 		if (index < 0)
   3873  1.1  jmcneill 			return EINVAL;
   3874  1.1  jmcneill 		ad->ad_sc->sc_rparam = *rec;
   3875  1.1  jmcneill 		hdafg_stream_connect(ad->ad_sc, AUMODE_RECORD);
   3876  1.1  jmcneill 	}
   3877  1.1  jmcneill 	return 0;
   3878  1.1  jmcneill }
   3879  1.1  jmcneill 
   3880  1.1  jmcneill static int
   3881  1.1  jmcneill hdafg_round_blocksize(void *opaque, int blksize, int mode,
   3882  1.1  jmcneill     const audio_params_t *param)
   3883  1.1  jmcneill {
   3884  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   3885  1.1  jmcneill 	struct hdaudio_stream *st;
   3886  1.1  jmcneill 	int bufsize, nblksize;
   3887  1.1  jmcneill 
   3888  1.1  jmcneill 	st = (mode == AUMODE_PLAY) ? ad->ad_playback : ad->ad_capture;
   3889  1.1  jmcneill 	if (st == NULL) {
   3890  1.1  jmcneill 		hda_trace(ad->ad_sc,
   3891  1.1  jmcneill 		    "round_blocksize called for invalid stream\n");
   3892  1.1  jmcneill 		return 128;
   3893  1.1  jmcneill 	}
   3894  1.1  jmcneill 
   3895  1.1  jmcneill 	if (blksize > 8192)
   3896  1.1  jmcneill 		blksize = 8192;
   3897  1.1  jmcneill 	else if (blksize < 0)
   3898  1.1  jmcneill 		blksize = 128;
   3899  1.1  jmcneill 
   3900  1.1  jmcneill 	/* HD audio wants a multiple of 128, and OSS wants a power of 2 */
   3901  1.1  jmcneill 	for (nblksize = 128; nblksize < blksize; nblksize <<= 1)
   3902  1.1  jmcneill 		;
   3903  1.1  jmcneill 
   3904  1.1  jmcneill 	/* Make sure there are enough BDL descriptors */
   3905  1.1  jmcneill 	bufsize = st->st_data.dma_size;
   3906  1.1  jmcneill 	if (bufsize > HDAUDIO_BDL_MAX * nblksize) {
   3907  1.1  jmcneill 		blksize = bufsize / HDAUDIO_BDL_MAX;
   3908  1.1  jmcneill 		for (nblksize = 128; nblksize < blksize; nblksize <<= 1)
   3909  1.1  jmcneill 			;
   3910  1.1  jmcneill 	}
   3911  1.1  jmcneill 
   3912  1.1  jmcneill 	return nblksize;
   3913  1.1  jmcneill }
   3914  1.1  jmcneill 
   3915  1.1  jmcneill static int
   3916  1.1  jmcneill hdafg_commit_settings(void *opaque)
   3917  1.1  jmcneill {
   3918  1.1  jmcneill 	return 0;
   3919  1.1  jmcneill }
   3920  1.1  jmcneill 
   3921  1.1  jmcneill static int
   3922  1.1  jmcneill hdafg_halt_output(void *opaque)
   3923  1.1  jmcneill {
   3924  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   3925  1.1  jmcneill 	struct hdafg_softc *sc = ad->ad_sc;
   3926  1.1  jmcneill 	struct hdaudio_assoc *as = ad->ad_sc->sc_assocs;
   3927  1.1  jmcneill 	struct hdaudio_widget *w;
   3928  1.1  jmcneill 	uint16_t dfmt;
   3929  1.1  jmcneill 	int i, j;
   3930  1.1  jmcneill 
   3931  1.1  jmcneill 	/* Disable digital outputs */
   3932  1.1  jmcneill 	for (i = 0; i < sc->sc_nassocs; i++) {
   3933  1.1  jmcneill 		if (as[i].as_enable == false)
   3934  1.1  jmcneill 			continue;
   3935  1.1  jmcneill 		if (as[i].as_dir != HDAUDIO_PINDIR_OUT)
   3936  1.1  jmcneill 			continue;
   3937  1.1  jmcneill 		for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   3938  1.1  jmcneill 			if (as[i].as_dacs[j] == 0)
   3939  1.1  jmcneill 				continue;
   3940  1.1  jmcneill 			w = hdafg_widget_lookup(sc, as[i].as_dacs[j]);
   3941  1.1  jmcneill 			if (w == NULL || w->w_enable == false)
   3942  1.1  jmcneill 				continue;
   3943  1.1  jmcneill 			if (w->w_p.aw_cap & COP_AWCAP_DIGITAL) {
   3944  1.1  jmcneill 				dfmt = hdaudio_command(sc->sc_codec, w->w_nid,
   3945  1.1  jmcneill 				    CORB_GET_DIGITAL_CONVERTER_CONTROL, 0) &
   3946  1.1  jmcneill 				    0xff;
   3947  1.1  jmcneill 				dfmt &= ~COP_DIGITAL_CONVCTRL1_DIGEN;
   3948  1.1  jmcneill 				hdaudio_command(sc->sc_codec, w->w_nid,
   3949  1.1  jmcneill 				    CORB_SET_DIGITAL_CONVERTER_CONTROL_1, dfmt);
   3950  1.1  jmcneill 			}
   3951  1.1  jmcneill 		}
   3952  1.1  jmcneill 	}
   3953  1.1  jmcneill 
   3954  1.1  jmcneill 	hdaudio_stream_stop(ad->ad_playback);
   3955  1.1  jmcneill 
   3956  1.1  jmcneill 	return 0;
   3957  1.1  jmcneill }
   3958  1.1  jmcneill 
   3959  1.1  jmcneill static int
   3960  1.1  jmcneill hdafg_halt_input(void *opaque)
   3961  1.1  jmcneill {
   3962  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   3963  1.1  jmcneill 
   3964  1.1  jmcneill 	hdaudio_stream_stop(ad->ad_capture);
   3965  1.1  jmcneill 
   3966  1.1  jmcneill 	return 0;
   3967  1.1  jmcneill }
   3968  1.1  jmcneill 
   3969  1.1  jmcneill static int
   3970  1.1  jmcneill hdafg_getdev(void *opaque, struct audio_device *audiodev)
   3971  1.1  jmcneill {
   3972  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   3973  1.1  jmcneill 	struct hdafg_softc *sc = ad->ad_sc;
   3974  1.1  jmcneill 
   3975  1.1  jmcneill 	hdaudio_findvendor(audiodev->name, sizeof(audiodev->name),
   3976  1.1  jmcneill 	    sc->sc_vendor);
   3977  1.1  jmcneill 	hdaudio_findproduct(audiodev->version, sizeof(audiodev->version),
   3978  1.1  jmcneill 	    sc->sc_vendor, sc->sc_product);
   3979  1.1  jmcneill 	snprintf(audiodev->config, sizeof(audiodev->config) - 1,
   3980  1.1  jmcneill 	    "%02Xh", sc->sc_nid);
   3981  1.1  jmcneill 
   3982  1.1  jmcneill 	return 0;
   3983  1.1  jmcneill }
   3984  1.1  jmcneill 
   3985  1.1  jmcneill static int
   3986  1.1  jmcneill hdafg_set_port(void *opaque, mixer_ctrl_t *mc)
   3987  1.1  jmcneill {
   3988  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   3989  1.1  jmcneill 	struct hdafg_softc *sc = ad->ad_sc;
   3990  1.1  jmcneill 	struct hdaudio_mixer *mx;
   3991  1.1  jmcneill 	struct hdaudio_control *ctl;
   3992  1.1  jmcneill 	int i, divisor;
   3993  1.1  jmcneill 
   3994  1.1  jmcneill 	if (mc->dev < 0 || mc->dev >= sc->sc_nmixers)
   3995  1.1  jmcneill 		return EINVAL;
   3996  1.1  jmcneill 	mx = &sc->sc_mixers[mc->dev];
   3997  1.1  jmcneill 	ctl = mx->mx_ctl;
   3998  1.1  jmcneill 	if (ctl == NULL) {
   3999  1.1  jmcneill 		if (mx->mx_di.type != AUDIO_MIXER_SET)
   4000  1.1  jmcneill 			return ENXIO;
   4001  1.1  jmcneill 		if (mx->mx_di.mixer_class != HDAUDIO_MIXER_CLASS_OUTPUTS &&
   4002  1.1  jmcneill 		    mx->mx_di.mixer_class != HDAUDIO_MIXER_CLASS_RECORD)
   4003  1.1  jmcneill 			return ENXIO;
   4004  1.1  jmcneill 		for (i = 0; i < sc->sc_nassocs; i++) {
   4005  1.1  jmcneill 			if (sc->sc_assocs[i].as_dir != HDAUDIO_PINDIR_OUT &&
   4006  1.1  jmcneill 			    mx->mx_di.mixer_class ==
   4007  1.1  jmcneill 			    HDAUDIO_MIXER_CLASS_OUTPUTS)
   4008  1.1  jmcneill 				continue;
   4009  1.1  jmcneill 			if (sc->sc_assocs[i].as_dir != HDAUDIO_PINDIR_IN &&
   4010  1.1  jmcneill 			    mx->mx_di.mixer_class ==
   4011  1.1  jmcneill 			    HDAUDIO_MIXER_CLASS_RECORD)
   4012  1.1  jmcneill 				continue;
   4013  1.1  jmcneill 			sc->sc_assocs[i].as_activated =
   4014  1.1  jmcneill 			    (mc->un.mask & (1 << i)) ? true : false;
   4015  1.1  jmcneill 		}
   4016  1.1  jmcneill 		hdafg_stream_connect(ad->ad_sc,
   4017  1.1  jmcneill 		    mx->mx_di.mixer_class == HDAUDIO_MIXER_CLASS_OUTPUTS ?
   4018  1.1  jmcneill 		    AUMODE_PLAY : AUMODE_RECORD);
   4019  1.1  jmcneill 		return 0;
   4020  1.1  jmcneill 	}
   4021  1.1  jmcneill 
   4022  1.1  jmcneill 	switch (mx->mx_di.type) {
   4023  1.1  jmcneill 	case AUDIO_MIXER_VALUE:
   4024  1.1  jmcneill 		if (ctl->ctl_step == 0)
   4025  1.1  jmcneill 			divisor = 128; /* ??? - just avoid div by 0 */
   4026  1.1  jmcneill 		else
   4027  1.1  jmcneill 			divisor = 255 / ctl->ctl_step;
   4028  1.1  jmcneill 
   4029  1.1  jmcneill 		hdafg_control_amp_set(ctl, HDAUDIO_AMP_MUTE_NONE,
   4030  1.1  jmcneill 		  mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] / divisor,
   4031  1.1  jmcneill 		  mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] / divisor);
   4032  1.1  jmcneill 		break;
   4033  1.1  jmcneill 	case AUDIO_MIXER_ENUM:
   4034  1.1  jmcneill 		hdafg_control_amp_set(ctl,
   4035  1.1  jmcneill 		    mc->un.ord ? HDAUDIO_AMP_MUTE_ALL : HDAUDIO_AMP_MUTE_NONE,
   4036  1.1  jmcneill 		    ctl->ctl_left, ctl->ctl_right);
   4037  1.1  jmcneill 		break;
   4038  1.1  jmcneill 	default:
   4039  1.1  jmcneill 		return ENXIO;
   4040  1.1  jmcneill 	}
   4041  1.1  jmcneill 
   4042  1.1  jmcneill 	return 0;
   4043  1.1  jmcneill }
   4044  1.1  jmcneill 
   4045  1.1  jmcneill static int
   4046  1.1  jmcneill hdafg_get_port(void *opaque, mixer_ctrl_t *mc)
   4047  1.1  jmcneill {
   4048  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   4049  1.1  jmcneill 	struct hdafg_softc *sc = ad->ad_sc;
   4050  1.1  jmcneill 	struct hdaudio_mixer *mx;
   4051  1.1  jmcneill 	struct hdaudio_control *ctl;
   4052  1.1  jmcneill 	u_int mask = 0;
   4053  1.1  jmcneill 	int i, factor;
   4054  1.1  jmcneill 
   4055  1.1  jmcneill 	if (mc->dev < 0 || mc->dev >= sc->sc_nmixers)
   4056  1.1  jmcneill 		return EINVAL;
   4057  1.1  jmcneill 	mx = &sc->sc_mixers[mc->dev];
   4058  1.1  jmcneill 	ctl = mx->mx_ctl;
   4059  1.1  jmcneill 	if (ctl == NULL) {
   4060  1.1  jmcneill 		if (mx->mx_di.type != AUDIO_MIXER_SET)
   4061  1.1  jmcneill 			return ENXIO;
   4062  1.1  jmcneill 		if (mx->mx_di.mixer_class != HDAUDIO_MIXER_CLASS_OUTPUTS &&
   4063  1.1  jmcneill 		    mx->mx_di.mixer_class != HDAUDIO_MIXER_CLASS_RECORD)
   4064  1.1  jmcneill 			return ENXIO;
   4065  1.1  jmcneill 		for (i = 0; i < sc->sc_nassocs; i++) {
   4066  1.1  jmcneill 			if (sc->sc_assocs[i].as_enable == false)
   4067  1.1  jmcneill 				continue;
   4068  1.1  jmcneill 			if (sc->sc_assocs[i].as_activated == false)
   4069  1.1  jmcneill 				continue;
   4070  1.1  jmcneill 			if (sc->sc_assocs[i].as_dir == HDAUDIO_PINDIR_OUT &&
   4071  1.1  jmcneill 			    mx->mx_di.mixer_class ==
   4072  1.1  jmcneill 			    HDAUDIO_MIXER_CLASS_OUTPUTS)
   4073  1.1  jmcneill 				mask |= (1 << i);
   4074  1.1  jmcneill 			if (sc->sc_assocs[i].as_dir == HDAUDIO_PINDIR_IN &&
   4075  1.1  jmcneill 			    mx->mx_di.mixer_class ==
   4076  1.1  jmcneill 			    HDAUDIO_MIXER_CLASS_RECORD)
   4077  1.1  jmcneill 				mask |= (1 << i);
   4078  1.1  jmcneill 		}
   4079  1.1  jmcneill 		mc->un.mask = mask;
   4080  1.1  jmcneill 		return 0;
   4081  1.1  jmcneill 	}
   4082  1.1  jmcneill 
   4083  1.1  jmcneill 	switch (mx->mx_di.type) {
   4084  1.1  jmcneill 	case AUDIO_MIXER_VALUE:
   4085  1.1  jmcneill 		if (ctl->ctl_step == 0)
   4086  1.1  jmcneill 			factor = 128; /* ??? - just avoid div by 0 */
   4087  1.1  jmcneill 		else
   4088  1.1  jmcneill 			factor = 255 / ctl->ctl_step;
   4089  1.1  jmcneill 
   4090  1.1  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = ctl->ctl_left * factor;
   4091  1.1  jmcneill 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = ctl->ctl_right * factor;
   4092  1.1  jmcneill 		break;
   4093  1.1  jmcneill 	case AUDIO_MIXER_ENUM:
   4094  1.1  jmcneill 		mc->un.ord = (ctl->ctl_muted || ctl->ctl_forcemute) ? 1 : 0;
   4095  1.1  jmcneill 		break;
   4096  1.1  jmcneill 	default:
   4097  1.1  jmcneill 		return ENXIO;
   4098  1.1  jmcneill 	}
   4099  1.1  jmcneill 	return 0;
   4100  1.1  jmcneill }
   4101  1.1  jmcneill 
   4102  1.1  jmcneill static int
   4103  1.1  jmcneill hdafg_query_devinfo(void *opaque, mixer_devinfo_t *di)
   4104  1.1  jmcneill {
   4105  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   4106  1.1  jmcneill 	struct hdafg_softc *sc = ad->ad_sc;
   4107  1.1  jmcneill 
   4108  1.1  jmcneill 	if (di->index < 0 || di->index >= sc->sc_nmixers)
   4109  1.1  jmcneill 		return ENXIO;
   4110  1.1  jmcneill 
   4111  1.1  jmcneill 	*di = sc->sc_mixers[di->index].mx_di;
   4112  1.1  jmcneill 
   4113  1.1  jmcneill 	return 0;
   4114  1.1  jmcneill }
   4115  1.1  jmcneill 
   4116  1.1  jmcneill static void *
   4117  1.1  jmcneill hdafg_allocm(void *opaque, int direction, size_t size)
   4118  1.1  jmcneill {
   4119  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   4120  1.1  jmcneill 	struct hdaudio_stream *st;
   4121  1.1  jmcneill 	int err;
   4122  1.1  jmcneill 
   4123  1.1  jmcneill 	st = (direction == AUMODE_PLAY) ? ad->ad_playback : ad->ad_capture;
   4124  1.1  jmcneill 	if (st == NULL)
   4125  1.1  jmcneill 		return NULL;
   4126  1.1  jmcneill 
   4127  1.1  jmcneill 	if (st->st_data.dma_valid == true)
   4128  1.1  jmcneill 		hda_error(ad->ad_sc, "WARNING: allocm leak\n");
   4129  1.1  jmcneill 
   4130  1.1  jmcneill 	st->st_data.dma_size = size;
   4131  1.1  jmcneill 	err = hdaudio_dma_alloc(st->st_host, &st->st_data,
   4132  1.1  jmcneill 	    BUS_DMA_COHERENT | BUS_DMA_NOCACHE);
   4133  1.1  jmcneill 	if (err || st->st_data.dma_valid == false)
   4134  1.1  jmcneill 		return NULL;
   4135  1.1  jmcneill 
   4136  1.1  jmcneill 	return DMA_KERNADDR(&st->st_data);
   4137  1.1  jmcneill }
   4138  1.1  jmcneill 
   4139  1.1  jmcneill static void
   4140  1.1  jmcneill hdafg_freem(void *opaque, void *addr, size_t size)
   4141  1.1  jmcneill {
   4142  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   4143  1.1  jmcneill 	struct hdaudio_stream *st;
   4144  1.1  jmcneill 
   4145  1.1  jmcneill 	if (addr == DMA_KERNADDR(&ad->ad_playback->st_data))
   4146  1.1  jmcneill 		st = ad->ad_playback;
   4147  1.1  jmcneill 	else if (addr == DMA_KERNADDR(&ad->ad_capture->st_data))
   4148  1.1  jmcneill 		st = ad->ad_capture;
   4149  1.1  jmcneill 	else
   4150  1.1  jmcneill 		return;
   4151  1.1  jmcneill 
   4152  1.1  jmcneill 	hdaudio_dma_free(st->st_host, &st->st_data);
   4153  1.1  jmcneill }
   4154  1.1  jmcneill 
   4155  1.1  jmcneill static size_t
   4156  1.1  jmcneill hdafg_round_buffersize(void *opaque, int direction, size_t bufsize)
   4157  1.1  jmcneill {
   4158  1.1  jmcneill 	/* Multiple of 128 */
   4159  1.1  jmcneill 	bufsize &= ~127;
   4160  1.1  jmcneill 	if (bufsize <= 0)
   4161  1.1  jmcneill 		bufsize = 128;
   4162  1.1  jmcneill 	return bufsize;
   4163  1.1  jmcneill }
   4164  1.1  jmcneill 
   4165  1.1  jmcneill static paddr_t
   4166  1.1  jmcneill hdafg_mappage(void *opaque, void *addr, off_t off, int prot)
   4167  1.1  jmcneill {
   4168  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   4169  1.1  jmcneill 	struct hdaudio_stream *st;
   4170  1.1  jmcneill 
   4171  1.1  jmcneill 	if (addr == DMA_KERNADDR(&ad->ad_playback->st_data))
   4172  1.1  jmcneill 		st = ad->ad_playback;
   4173  1.1  jmcneill 	else if (addr == DMA_KERNADDR(&ad->ad_capture->st_data))
   4174  1.1  jmcneill 		st = ad->ad_capture;
   4175  1.1  jmcneill 	else
   4176  1.1  jmcneill 		return -1;
   4177  1.1  jmcneill 
   4178  1.1  jmcneill 	if (st->st_data.dma_valid == false)
   4179  1.1  jmcneill 		return -1;
   4180  1.1  jmcneill 
   4181  1.1  jmcneill 	return bus_dmamem_mmap(st->st_host->sc_dmat, st->st_data.dma_segs,
   4182  1.1  jmcneill 	    st->st_data.dma_nsegs, off, prot, BUS_DMA_WAITOK);
   4183  1.1  jmcneill }
   4184  1.1  jmcneill 
   4185  1.1  jmcneill static int
   4186  1.1  jmcneill hdafg_get_props(void *opaque)
   4187  1.1  jmcneill {
   4188  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   4189  1.1  jmcneill 	int props = AUDIO_PROP_MMAP;
   4190  1.1  jmcneill 
   4191  1.1  jmcneill 	if (ad->ad_playback)
   4192  1.1  jmcneill 		props |= AUDIO_PROP_PLAYBACK;
   4193  1.1  jmcneill 	if (ad->ad_capture)
   4194  1.1  jmcneill 		props |= AUDIO_PROP_CAPTURE;
   4195  1.1  jmcneill 	if (ad->ad_playback && ad->ad_capture) {
   4196  1.1  jmcneill 		props |= AUDIO_PROP_FULLDUPLEX;
   4197  1.1  jmcneill 		props |= AUDIO_PROP_INDEPENDENT;
   4198  1.1  jmcneill 	}
   4199  1.1  jmcneill 
   4200  1.1  jmcneill 	return props;
   4201  1.1  jmcneill }
   4202  1.1  jmcneill 
   4203  1.1  jmcneill static int
   4204  1.1  jmcneill hdafg_trigger_output(void *opaque, void *start, void *end, int blksize,
   4205  1.1  jmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *param)
   4206  1.1  jmcneill {
   4207  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   4208  1.1  jmcneill 	bus_size_t dmasize;
   4209  1.1  jmcneill 
   4210  1.1  jmcneill 	if (ad->ad_playback == NULL)
   4211  1.1  jmcneill 		return ENXIO;
   4212  1.1  jmcneill 	if (ad->ad_playback->st_data.dma_valid == false)
   4213  1.1  jmcneill 		return ENOMEM;
   4214  1.1  jmcneill 
   4215  1.1  jmcneill 	ad->ad_playbackintr = intr;
   4216  1.1  jmcneill 	ad->ad_playbackintrarg = intrarg;
   4217  1.1  jmcneill 
   4218  1.1  jmcneill 	dmasize = (char *)end - (char *)start;
   4219  1.1  jmcneill 	ad->ad_sc->sc_pparam = *param;
   4220  1.1  jmcneill 	hdafg_stream_connect(ad->ad_sc, AUMODE_PLAY);
   4221  1.1  jmcneill 	hdaudio_stream_start(ad->ad_playback, blksize, dmasize, param);
   4222  1.1  jmcneill 
   4223  1.1  jmcneill 	return 0;
   4224  1.1  jmcneill }
   4225  1.1  jmcneill 
   4226  1.1  jmcneill static int
   4227  1.1  jmcneill hdafg_trigger_input(void *opaque, void *start, void *end, int blksize,
   4228  1.1  jmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *param)
   4229  1.1  jmcneill {
   4230  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   4231  1.1  jmcneill 	bus_size_t dmasize;
   4232  1.1  jmcneill 
   4233  1.1  jmcneill 	if (ad->ad_capture == NULL)
   4234  1.1  jmcneill 		return ENXIO;
   4235  1.1  jmcneill 	if (ad->ad_capture->st_data.dma_valid == false)
   4236  1.1  jmcneill 		return ENOMEM;
   4237  1.1  jmcneill 
   4238  1.1  jmcneill 	ad->ad_captureintr = intr;
   4239  1.1  jmcneill 	ad->ad_captureintrarg = intrarg;
   4240  1.1  jmcneill 
   4241  1.1  jmcneill 	dmasize = (char *)end - (char *)start;
   4242  1.1  jmcneill 	ad->ad_sc->sc_rparam = *param;
   4243  1.1  jmcneill 	hdafg_stream_connect(ad->ad_sc, AUMODE_RECORD);
   4244  1.1  jmcneill 	hdaudio_stream_start(ad->ad_capture, blksize, dmasize, param);
   4245  1.1  jmcneill 
   4246  1.1  jmcneill 	return 0;
   4247  1.1  jmcneill }
   4248  1.1  jmcneill 
   4249  1.1  jmcneill static void
   4250  1.1  jmcneill hdafg_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
   4251  1.1  jmcneill {
   4252  1.1  jmcneill 	struct hdaudio_audiodev *ad = opaque;
   4253  1.1  jmcneill 
   4254  1.1  jmcneill 	*intr = &ad->ad_sc->sc_intr_lock;
   4255  1.1  jmcneill 	*thread = &ad->ad_sc->sc_lock;
   4256  1.1  jmcneill }
   4257  1.1  jmcneill 
   4258  1.1  jmcneill static int
   4259  1.1  jmcneill hdafg_unsol(device_t self, uint8_t tag)
   4260  1.1  jmcneill {
   4261  1.1  jmcneill 	struct hdafg_softc *sc = device_private(self);
   4262  1.1  jmcneill 	struct hdaudio_assoc *as = sc->sc_assocs;
   4263  1.1  jmcneill 	int i, j;
   4264  1.1  jmcneill 
   4265  1.1  jmcneill 	switch (tag) {
   4266  1.1  jmcneill 	case HDAUDIO_UNSOLTAG_EVENT_DD:
   4267  1.1  jmcneill 		hda_print(sc, "unsol: display device hotplug\n");
   4268  1.1  jmcneill 		for (i = 0; i < sc->sc_nassocs; i++) {
   4269  1.1  jmcneill 			if (as[i].as_displaydev == false)
   4270  1.1  jmcneill 				continue;
   4271  1.1  jmcneill 			for (j = 0; j < HDAUDIO_MAXPINS; j++) {
   4272  1.1  jmcneill 				if (as[i].as_pins[j] == 0)
   4273  1.1  jmcneill 					continue;
   4274  1.1  jmcneill 				hdafg_assoc_dump_dd(sc, &as[i], j, 0);
   4275  1.1  jmcneill 			}
   4276  1.1  jmcneill 		}
   4277  1.1  jmcneill 		break;
   4278  1.1  jmcneill 	default:
   4279  1.1  jmcneill 		hda_print(sc, "unsol: tag=%u\n", tag);
   4280  1.1  jmcneill 		break;
   4281  1.1  jmcneill 	}
   4282  1.1  jmcneill 
   4283  1.1  jmcneill 	return 0;
   4284  1.1  jmcneill }
   4285  1.1  jmcneill 
   4286  1.1  jmcneill static int
   4287  1.1  jmcneill hdafg_widget_info(void *opaque, prop_dictionary_t request,
   4288  1.1  jmcneill     prop_dictionary_t response)
   4289  1.1  jmcneill {
   4290  1.1  jmcneill 	struct hdafg_softc *sc = opaque;
   4291  1.1  jmcneill 	struct hdaudio_widget *w;
   4292  1.1  jmcneill 	prop_array_t connlist;
   4293  1.1  jmcneill 	uint32_t config, wcap;
   4294  1.1  jmcneill 	uint16_t index;
   4295  1.1  jmcneill 	int nid;
   4296  1.1  jmcneill 	int i;
   4297  1.1  jmcneill 
   4298  1.1  jmcneill 	if (prop_dictionary_get_uint16(request, "index", &index) == false)
   4299  1.1  jmcneill 		return EINVAL;
   4300  1.1  jmcneill 
   4301  1.1  jmcneill 	nid = sc->sc_startnode + index;
   4302  1.1  jmcneill 	if (nid >= sc->sc_endnode)
   4303  1.1  jmcneill 		return EINVAL;
   4304  1.1  jmcneill 
   4305  1.1  jmcneill 	w = hdafg_widget_lookup(sc, nid);
   4306  1.1  jmcneill 	if (w == NULL)
   4307  1.1  jmcneill 		return ENXIO;
   4308  1.1  jmcneill 	wcap = hda_get_wparam(w, PIN_CAPABILITIES);
   4309  1.1  jmcneill 	config = hdaudio_command(sc->sc_codec, w->w_nid,
   4310  1.1  jmcneill 	    CORB_GET_CONFIGURATION_DEFAULT, 0);
   4311  1.1  jmcneill 	prop_dictionary_set_cstring_nocopy(response, "name", w->w_name);
   4312  1.1  jmcneill 	prop_dictionary_set_bool(response, "enable", w->w_enable);
   4313  1.1  jmcneill 	prop_dictionary_set_uint8(response, "nid", w->w_nid);
   4314  1.1  jmcneill 	prop_dictionary_set_uint8(response, "type", w->w_type);
   4315  1.1  jmcneill 	prop_dictionary_set_uint32(response, "config", config);
   4316  1.1  jmcneill 	prop_dictionary_set_uint32(response, "cap", wcap);
   4317  1.1  jmcneill 	if (w->w_nconns == 0)
   4318  1.1  jmcneill 		return 0;
   4319  1.1  jmcneill 	connlist = prop_array_create();
   4320  1.1  jmcneill 	for (i = 0; i < w->w_nconns; i++) {
   4321  1.1  jmcneill 		if (w->w_conns[i] == 0)
   4322  1.1  jmcneill 			continue;
   4323  1.1  jmcneill 		prop_array_add(connlist,
   4324  1.1  jmcneill 		    prop_number_create_unsigned_integer(w->w_conns[i]));
   4325  1.1  jmcneill 	}
   4326  1.1  jmcneill 	prop_dictionary_set(response, "connlist", connlist);
   4327  1.1  jmcneill 	prop_object_release(connlist);
   4328  1.1  jmcneill 	return 0;
   4329  1.1  jmcneill }
   4330  1.1  jmcneill 
   4331  1.1  jmcneill static int
   4332  1.1  jmcneill hdafg_codec_info(void *opaque, prop_dictionary_t request,
   4333  1.1  jmcneill     prop_dictionary_t response)
   4334  1.1  jmcneill {
   4335  1.1  jmcneill 	struct hdafg_softc *sc = opaque;
   4336  1.1  jmcneill 	prop_dictionary_set_uint16(response, "vendor-id",
   4337  1.1  jmcneill 	    sc->sc_vendor);
   4338  1.1  jmcneill 	prop_dictionary_set_uint16(response, "product-id",
   4339  1.1  jmcneill 	    sc->sc_product);
   4340  1.1  jmcneill 	return 0;
   4341  1.1  jmcneill }
   4342  1.1  jmcneill 
   4343  1.1  jmcneill MODULE(MODULE_CLASS_DRIVER, hdafg, "hdaudio");
   4344  1.1  jmcneill 
   4345  1.1  jmcneill #ifdef _MODULE
   4346  1.1  jmcneill #include "ioconf.c"
   4347  1.1  jmcneill #endif
   4348  1.1  jmcneill 
   4349  1.1  jmcneill static int
   4350  1.1  jmcneill hdafg_modcmd(modcmd_t cmd, void *opaque)
   4351  1.1  jmcneill {
   4352  1.1  jmcneill 	int error = 0;
   4353  1.1  jmcneill 
   4354  1.1  jmcneill 	switch (cmd) {
   4355  1.1  jmcneill 	case MODULE_CMD_INIT:
   4356  1.1  jmcneill #ifdef _MODULE
   4357  1.1  jmcneill 		error = config_init_component(cfdriver_ioconf_hdafg,
   4358  1.1  jmcneill 		    cfattach_ioconf_hdafg, cfdata_ioconf_hdafg);
   4359  1.1  jmcneill #endif
   4360  1.1  jmcneill 		return error;
   4361  1.1  jmcneill 	case MODULE_CMD_FINI:
   4362  1.1  jmcneill #ifdef _MODULE
   4363  1.1  jmcneill 		error = config_fini_component(cfdriver_ioconf_hdafg,
   4364  1.1  jmcneill 		    cfattach_ioconf_hdafg, cfdata_ioconf_hdafg);
   4365  1.1  jmcneill #endif
   4366  1.1  jmcneill 		return error;
   4367  1.1  jmcneill 	default:
   4368  1.1  jmcneill 		return ENOTTY;
   4369  1.1  jmcneill 	}
   4370  1.1  jmcneill }
   4371  1.1  jmcneill 
   4372  1.1  jmcneill #define HDAFG_GET_ANACTRL 		0xfe0
   4373  1.1  jmcneill #define HDAFG_SET_ANACTRL 		0x7e0
   4374  1.1  jmcneill #define HDAFG_ANALOG_BEEP_EN		__BIT(5)
   4375  1.1  jmcneill #define HDAFG_ALC231_MONO_OUT_MIXER 	0xf
   4376  1.1  jmcneill #define HDAFG_STAC9200_AFG		0x1
   4377  1.1  jmcneill #define HDAFG_STAC9200_GET_ANACTRL_PAYLOAD	0x0
   4378  1.1  jmcneill #define HDAFG_ALC231_INPUT_BOTH_CHANNELS_UNMUTE	0x7100
   4379  1.1  jmcneill 
   4380  1.1  jmcneill static void
   4381  1.1  jmcneill hdafg_enable_analog_beep(struct hdafg_softc *sc)
   4382  1.1  jmcneill {
   4383  1.1  jmcneill 	int nid;
   4384  1.1  jmcneill 	uint32_t response;
   4385  1.1  jmcneill 
   4386  1.1  jmcneill 	switch (sc->sc_vendor) {
   4387  1.1  jmcneill 	case HDAUDIO_VENDOR_SIGMATEL:
   4388  1.1  jmcneill 		switch (sc->sc_product) {
   4389  1.1  jmcneill 		case HDAUDIO_PRODUCT_SIGMATEL_STAC9200:
   4390  1.1  jmcneill 		case HDAUDIO_PRODUCT_SIGMATEL_STAC9200D:
   4391  1.1  jmcneill 		case HDAUDIO_PRODUCT_SIGMATEL_STAC9202:
   4392  1.1  jmcneill 		case HDAUDIO_PRODUCT_SIGMATEL_STAC9202D:
   4393  1.1  jmcneill 		case HDAUDIO_PRODUCT_SIGMATEL_STAC9204:
   4394  1.1  jmcneill 		case HDAUDIO_PRODUCT_SIGMATEL_STAC9204D:
   4395  1.1  jmcneill 		case HDAUDIO_PRODUCT_SIGMATEL_STAC9205:
   4396  1.1  jmcneill 		case HDAUDIO_PRODUCT_SIGMATEL_STAC9205_1:
   4397  1.1  jmcneill 		case HDAUDIO_PRODUCT_SIGMATEL_STAC9205D:
   4398  1.1  jmcneill 			nid = HDAFG_STAC9200_AFG;
   4399  1.1  jmcneill 
   4400  1.1  jmcneill 			response = hdaudio_command(sc->sc_codec, nid,
   4401  1.1  jmcneill 			    HDAFG_GET_ANACTRL,
   4402  1.1  jmcneill 			    HDAFG_STAC9200_GET_ANACTRL_PAYLOAD);
   4403  1.1  jmcneill 			hda_delay(100);
   4404  1.1  jmcneill 
   4405  1.1  jmcneill 			response |= HDAFG_ANALOG_BEEP_EN;
   4406  1.1  jmcneill 
   4407  1.1  jmcneill 			hdaudio_command(sc->sc_codec, nid, HDAFG_SET_ANACTRL,
   4408  1.1  jmcneill 			    response);
   4409  1.1  jmcneill 			hda_delay(100);
   4410  1.1  jmcneill 			break;
   4411  1.1  jmcneill 		default:
   4412  1.1  jmcneill 			break;
   4413  1.1  jmcneill 		}
   4414  1.1  jmcneill 		break;
   4415  1.1  jmcneill 	case HDAUDIO_VENDOR_REALTEK:
   4416  1.1  jmcneill 		switch (sc->sc_product) {
   4417  1.1  jmcneill 		case HDAUDIO_PRODUCT_REALTEK_ALC269:
   4418  1.1  jmcneill 			/* The Panasonic Toughbook CF19 - Mk 5 uses a Realtek
   4419  1.1  jmcneill 			 * ALC231 that identifies as an ALC269.
   4420  1.1  jmcneill 			 * This unmutes the PCBEEP on the speaker.
   4421  1.1  jmcneill 			 */
   4422  1.1  jmcneill  			nid = HDAFG_ALC231_MONO_OUT_MIXER;
   4423  1.1  jmcneill 			response = hdaudio_command(sc->sc_codec, nid,
   4424  1.1  jmcneill 			    CORB_SET_AMPLIFIER_GAIN_MUTE,
   4425  1.1  jmcneill 			    HDAFG_ALC231_INPUT_BOTH_CHANNELS_UNMUTE);
   4426  1.1  jmcneill 			hda_delay(100);
   4427  1.1  jmcneill 			break;
   4428  1.1  jmcneill 		default:
   4429  1.1  jmcneill 			break;
   4430  1.1  jmcneill 		}
   4431  1.1  jmcneill 	default:
   4432  1.1  jmcneill 		break;
   4433  1.1  jmcneill 	}
   4434  1.1  jmcneill }
   4435