Home | History | Annotate | Line # | Download | only in ic
ac97.c revision 1.65
      1  1.65      kent /*      $NetBSD: ac97.c,v 1.65 2004/11/08 14:24:17 kent Exp $ */
      2  1.14   thorpej /*	$OpenBSD: ac97.c,v 1.8 2000/07/19 09:01:35 csapuntz Exp $	*/
      3   1.1  augustss 
      4   1.1  augustss /*
      5  1.14   thorpej  * Copyright (c) 1999, 2000 Constantine Sapuntzakis
      6   1.1  augustss  *
      7   1.1  augustss  * Author:        Constantine Sapuntzakis <csapuntz (at) stanford.edu>
      8   1.1  augustss  *
      9   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     10   1.1  augustss  * modification, are permitted provided that the following conditions
     11   1.1  augustss  * are met:
     12   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     13   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     14   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     17  1.14   thorpej  * 3. The name of the author may not be used to endorse or promote
     18  1.14   thorpej  *    products derived from this software without specific prior written
     19  1.14   thorpej  *    permission.
     20  1.14   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
     21  1.14   thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22  1.14   thorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.14   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
     24  1.14   thorpej  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.14   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
     26  1.14   thorpej  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     27  1.14   thorpej  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  1.14   thorpej  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.14   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     30  1.14   thorpej  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
     31  1.14   thorpej  * DAMAGE
     32   1.1  augustss  */
     33   1.1  augustss 
     34   1.1  augustss /* Partially inspired by FreeBSD's sys/dev/pcm/ac97.c. It came with
     35   1.1  augustss    the following copyright */
     36   1.1  augustss 
     37   1.1  augustss /*
     38   1.1  augustss  * Copyright (c) 1999 Cameron Grant <gandalf (at) vilnya.demon.co.uk>
     39   1.1  augustss  * All rights reserved.
     40   1.1  augustss  *
     41   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     42   1.1  augustss  * modification, are permitted provided that the following conditions
     43   1.1  augustss  * are met:
     44   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     45   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     46   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     48   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     49   1.1  augustss  *
     50   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     51   1.1  augustss  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52   1.1  augustss  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53   1.1  augustss  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     54   1.1  augustss  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55   1.1  augustss  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56   1.1  augustss  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57   1.1  augustss  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58   1.1  augustss  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59   1.1  augustss  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60   1.1  augustss  * SUCH DAMAGE.
     61   1.1  augustss  *
     62   1.1  augustss  * $FreeBSD$
     63   1.1  augustss  */
     64  1.21     lukem 
     65  1.21     lukem #include <sys/cdefs.h>
     66  1.65      kent __KERNEL_RCSID(0, "$NetBSD: ac97.c,v 1.65 2004/11/08 14:24:17 kent Exp $");
     67   1.1  augustss 
     68   1.1  augustss #include <sys/param.h>
     69   1.1  augustss #include <sys/systm.h>
     70   1.1  augustss #include <sys/kernel.h>
     71   1.1  augustss #include <sys/malloc.h>
     72   1.1  augustss #include <sys/device.h>
     73   1.1  augustss 
     74   1.1  augustss #include <sys/audioio.h>
     75   1.1  augustss #include <dev/audio_if.h>
     76  1.10   thorpej 
     77  1.10   thorpej #include <dev/ic/ac97reg.h>
     78   1.9   thorpej #include <dev/ic/ac97var.h>
     79   1.1  augustss 
     80  1.50      kent struct ac97_softc;
     81  1.50      kent struct ac97_source_info;
     82  1.60   thorpej static int	ac97_mixer_get_port(struct ac97_codec_if *, mixer_ctrl_t *);
     83  1.60   thorpej static int	ac97_mixer_set_port(struct ac97_codec_if *, mixer_ctrl_t *);
     84  1.65      kent static void	ac97_detach(struct ac97_codec_if *);
     85  1.60   thorpej static int	ac97_query_devinfo(struct ac97_codec_if *, mixer_devinfo_t *);
     86  1.60   thorpej static int	ac97_get_portnum_by_name(struct ac97_codec_if *, const char *,
     87  1.60   thorpej 					 const char *, const char *);
     88  1.60   thorpej static void	ac97_restore_shadow(struct ac97_codec_if *);
     89  1.60   thorpej static int	ac97_set_rate(struct ac97_codec_if *, int, u_long *);
     90  1.60   thorpej static void	ac97_set_clock(struct ac97_codec_if *, unsigned int);
     91  1.60   thorpej static u_int16_t ac97_get_extcaps(struct ac97_codec_if *);
     92  1.60   thorpej static int	ac97_add_port(struct ac97_softc *,
     93  1.60   thorpej 			      const struct ac97_source_info *);
     94  1.60   thorpej static int	ac97_str_equal(const char *, const char *);
     95  1.60   thorpej static int	ac97_check_capability(struct ac97_softc *, int);
     96  1.60   thorpej static void	ac97_setup_source_info(struct ac97_softc *);
     97  1.60   thorpej static void	ac97_read(struct ac97_softc *, u_int8_t, u_int16_t *);
     98  1.60   thorpej static void	ac97_setup_defaults(struct ac97_softc *);
     99  1.60   thorpej static int	ac97_write(struct ac97_softc *, u_int8_t, u_int16_t);
    100  1.60   thorpej 
    101  1.60   thorpej static void	ac97_ad198x_init(struct ac97_softc *);
    102  1.60   thorpej static void	ac97_alc650_init(struct ac97_softc *);
    103  1.60   thorpej static void	ac97_vt1616_init(struct ac97_softc *);
    104  1.50      kent 
    105  1.33      kent #define Ac97Nphone	"phone"
    106  1.33      kent 
    107  1.50      kent static const struct audio_mixer_enum
    108  1.50      kent ac97_on_off = { 2, { { { AudioNoff } , 0 },
    109  1.50      kent 		     { { AudioNon }  , 1 } } };
    110  1.50      kent 
    111  1.50      kent static const struct audio_mixer_enum
    112  1.50      kent ac97_mic_select = { 2, { { { AudioNmicrophone "0" }, 0 },
    113  1.50      kent 			 { { AudioNmicrophone "1" }, 1 } } };
    114  1.50      kent 
    115  1.50      kent static const struct audio_mixer_enum
    116  1.50      kent ac97_mono_select = { 2, { { { AudioNmixerout }, 0 },
    117  1.50      kent 			  { { AudioNmicrophone }, 1 } } };
    118  1.50      kent 
    119  1.50      kent static const struct audio_mixer_enum
    120  1.50      kent ac97_source = { 8, { { { AudioNmicrophone } , 0 },
    121  1.50      kent 		     { { AudioNcd }, 1 },
    122  1.50      kent 		     { { AudioNvideo }, 2 },
    123  1.50      kent 		     { { AudioNaux }, 3 },
    124  1.50      kent 		     { { AudioNline }, 4 },
    125  1.50      kent 		     { { AudioNmixerout }, 5 },
    126  1.50      kent 		     { { AudioNmixerout AudioNmono }, 6 },
    127  1.50      kent 		     { { Ac97Nphone }, 7 } } };
    128   1.1  augustss 
    129  1.19       erh /*
    130  1.36      kent  * Due to different values for each source that uses these structures,
    131  1.19       erh  * the ac97_query_devinfo function sets delta in mixer_devinfo_t using
    132  1.19       erh  * ac97_source_info.bits.
    133  1.19       erh  */
    134  1.50      kent static const struct audio_mixer_value
    135  1.50      kent ac97_volume_stereo = { { AudioNvolume }, 2 };
    136   1.1  augustss 
    137  1.50      kent static const struct audio_mixer_value
    138  1.50      kent ac97_volume_mono = { { AudioNvolume }, 1 };
    139   1.1  augustss 
    140   1.1  augustss #define WRAP(a)  &a, sizeof(a)
    141   1.1  augustss 
    142  1.60   thorpej static const struct ac97_source_info {
    143  1.18  jdolecek 	const char *class;
    144  1.18  jdolecek 	const char *device;
    145  1.18  jdolecek 	const char *qualifier;
    146  1.33      kent 
    147   1.1  augustss 	int  type;
    148  1.18  jdolecek 	const void *info;
    149   1.1  augustss 	int  info_size;
    150   1.1  augustss 
    151   1.1  augustss 	u_int8_t  reg;
    152  1.14   thorpej 	u_int16_t default_value;
    153   1.1  augustss 	u_int8_t  bits:3;
    154   1.1  augustss 	u_int8_t  ofs:4;
    155   1.1  augustss 	u_int8_t  mute:1;
    156   1.1  augustss 	u_int8_t  polarity:1;   /* Does 0 == MAX or MIN */
    157  1.33      kent 	enum {
    158  1.33      kent 		CHECK_NONE = 0,
    159  1.33      kent 		CHECK_SURROUND,
    160  1.33      kent 		CHECK_CENTER,
    161  1.33      kent 		CHECK_LFE,
    162  1.33      kent 		CHECK_HEADPHONES,
    163  1.33      kent 		CHECK_TONE,
    164  1.33      kent 		CHECK_MIC,
    165  1.33      kent 		CHECK_LOUDNESS,
    166  1.33      kent 		CHECK_3D
    167  1.33      kent 	} req_feature;
    168   1.1  augustss 
    169   1.1  augustss 	int  prev;
    170  1.33      kent 	int  next;
    171   1.1  augustss 	int  mixer_class;
    172   1.1  augustss } source_info[] = {
    173  1.33      kent 	{ AudioCinputs,		NULL,		NULL,
    174  1.33      kent 	  AUDIO_MIXER_CLASS, },
    175  1.33      kent 	{ AudioCoutputs,	NULL,		NULL,
    176  1.33      kent 	  AUDIO_MIXER_CLASS, },
    177  1.33      kent 	{ AudioCrecord,		NULL,		NULL,
    178  1.33      kent 	  AUDIO_MIXER_CLASS, },
    179   1.1  augustss 	/* Stereo master volume*/
    180  1.33      kent 	{ AudioCoutputs,	AudioNmaster,	NULL,
    181  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    182  1.14   thorpej 	  AC97_REG_MASTER_VOLUME, 0x8000, 5, 0, 1,
    183   1.1  augustss 	},
    184   1.1  augustss 	/* Mono volume */
    185  1.33      kent 	{ AudioCoutputs,	AudioNmono,	NULL,
    186  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    187  1.14   thorpej 	  AC97_REG_MASTER_VOLUME_MONO, 0x8000, 6, 0, 1,
    188   1.1  augustss 	},
    189  1.33      kent 	{ AudioCoutputs,	AudioNmono,	AudioNsource,
    190  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_mono_select),
    191  1.14   thorpej 	  AC97_REG_GP, 0x0000, 1, 9, 0,
    192   1.1  augustss 	},
    193   1.1  augustss 	/* Headphone volume */
    194  1.33      kent 	{ AudioCoutputs,	AudioNheadphone, NULL,
    195  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    196  1.33      kent 	  AC97_REG_HEADPHONE_VOLUME, 0x8000, 6, 0, 1, 0, CHECK_HEADPHONES
    197  1.33      kent 	},
    198  1.33      kent 	/* Surround volume - logic hard coded for mute */
    199  1.33      kent 	{ AudioCoutputs,	AudioNsurround,	NULL,
    200  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    201  1.33      kent 	  AC97_REG_SURR_MASTER, 0x8080, 5, 0, 1, 0, CHECK_SURROUND
    202  1.33      kent 	},
    203  1.33      kent 	/* Center volume*/
    204  1.33      kent 	{ AudioCoutputs,	AudioNcenter,	NULL,
    205  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    206  1.33      kent 	  AC97_REG_CENTER_LFE_MASTER, 0x8080, 5, 0, 0, 0, CHECK_CENTER
    207  1.33      kent 	},
    208  1.33      kent 	{ AudioCoutputs,	AudioNcenter,	AudioNmute,
    209  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    210  1.33      kent 	  AC97_REG_CENTER_LFE_MASTER, 0x8080, 1, 7, 0, 0, CHECK_CENTER
    211  1.33      kent 	},
    212  1.33      kent 	/* LFE volume*/
    213  1.33      kent 	{ AudioCoutputs,	AudioNlfe,	NULL,
    214  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    215  1.33      kent 	  AC97_REG_CENTER_LFE_MASTER, 0x8080, 5, 8, 0, 0, CHECK_LFE
    216  1.33      kent 	},
    217  1.33      kent 	{ AudioCoutputs,	AudioNlfe,	AudioNmute,
    218  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    219  1.33      kent 	  AC97_REG_CENTER_LFE_MASTER, 0x8080, 1, 15, 0, 0, CHECK_LFE
    220   1.1  augustss 	},
    221  1.63      kent 	/* Tone - bass */
    222  1.63      kent 	{ AudioCoutputs,	AudioNbass,	NULL,
    223  1.63      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    224  1.63      kent 	  AC97_REG_MASTER_TONE, 0x0f0f, 4, 8, 0, 0, CHECK_TONE
    225  1.63      kent 	},
    226  1.63      kent 	/* Tone - treble */
    227  1.63      kent 	{ AudioCoutputs,	AudioNtreble,	NULL,
    228  1.63      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    229  1.33      kent 	  AC97_REG_MASTER_TONE, 0x0f0f, 4, 0, 0, 0, CHECK_TONE
    230   1.1  augustss 	},
    231   1.1  augustss 	/* PC Beep Volume */
    232  1.33      kent 	{ AudioCinputs,		AudioNspeaker,	NULL,
    233  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    234  1.14   thorpej 	  AC97_REG_PCBEEP_VOLUME, 0x0000, 4, 1, 1,
    235   1.1  augustss 	},
    236  1.33      kent 
    237   1.1  augustss 	/* Phone */
    238  1.33      kent 	{ AudioCinputs,		Ac97Nphone,	NULL,
    239  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    240  1.14   thorpej 	  AC97_REG_PHONE_VOLUME, 0x8008, 5, 0, 1,
    241   1.1  augustss 	},
    242   1.1  augustss 	/* Mic Volume */
    243  1.33      kent 	{ AudioCinputs,		AudioNmicrophone, NULL,
    244  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    245  1.14   thorpej 	  AC97_REG_MIC_VOLUME, 0x8008, 5, 0, 1,
    246   1.1  augustss 	},
    247  1.33      kent 	{ AudioCinputs,		AudioNmicrophone, AudioNpreamp,
    248  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    249  1.14   thorpej 	  AC97_REG_MIC_VOLUME, 0x8008, 1, 6, 0,
    250   1.1  augustss 	},
    251  1.33      kent 	{ AudioCinputs,		AudioNmicrophone, AudioNsource,
    252  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_mic_select),
    253  1.14   thorpej 	  AC97_REG_GP, 0x0000, 1, 8, 0,
    254   1.1  augustss 	},
    255   1.1  augustss 	/* Line in Volume */
    256  1.33      kent 	{ AudioCinputs,		AudioNline,	NULL,
    257  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    258  1.14   thorpej 	  AC97_REG_LINEIN_VOLUME, 0x8808, 5, 0, 1,
    259   1.1  augustss 	},
    260   1.1  augustss 	/* CD Volume */
    261  1.33      kent 	{ AudioCinputs,		AudioNcd,	NULL,
    262  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    263  1.14   thorpej 	  AC97_REG_CD_VOLUME, 0x8808, 5, 0, 1,
    264   1.1  augustss 	},
    265   1.1  augustss 	/* Video Volume */
    266  1.33      kent 	{ AudioCinputs,		AudioNvideo,	NULL,
    267  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    268  1.14   thorpej 	  AC97_REG_VIDEO_VOLUME, 0x8808, 5, 0, 1,
    269   1.1  augustss 	},
    270   1.1  augustss 	/* AUX volume */
    271  1.33      kent 	{ AudioCinputs,		AudioNaux,	NULL,
    272  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    273  1.14   thorpej 	  AC97_REG_AUX_VOLUME, 0x8808, 5, 0, 1,
    274   1.1  augustss 	},
    275   1.1  augustss 	/* PCM out volume */
    276  1.33      kent 	{ AudioCinputs,		AudioNdac,	NULL,
    277  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    278  1.14   thorpej 	  AC97_REG_PCMOUT_VOLUME, 0x8808, 5, 0, 1,
    279   1.1  augustss 	},
    280   1.1  augustss 	/* Record Source - some logic for this is hard coded - see below */
    281  1.33      kent 	{ AudioCrecord,		AudioNsource,	NULL,
    282  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_source),
    283  1.14   thorpej 	  AC97_REG_RECORD_SELECT, 0x0000, 3, 0, 0,
    284   1.1  augustss 	},
    285   1.1  augustss 	/* Record Gain */
    286  1.33      kent 	{ AudioCrecord,		AudioNvolume,	NULL,
    287  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    288  1.37      kent 	  AC97_REG_RECORD_GAIN, 0x8000, 4, 0, 1, 1,
    289   1.1  augustss 	},
    290   1.1  augustss 	/* Record Gain mic */
    291  1.33      kent 	{ AudioCrecord,		AudioNmicrophone, NULL,
    292  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    293  1.33      kent 	  AC97_REG_RECORD_GAIN_MIC, 0x8000, 4, 0, 1, 1, CHECK_MIC
    294   1.1  augustss 	},
    295   1.1  augustss 	/* */
    296  1.33      kent 	{ AudioCoutputs,	AudioNloudness,	NULL,
    297  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    298  1.33      kent 	  AC97_REG_GP, 0x0000, 1, 12, 0, 0, CHECK_LOUDNESS
    299  1.33      kent 	},
    300  1.33      kent 	{ AudioCoutputs,	AudioNspatial,	NULL,
    301  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    302  1.33      kent 	  AC97_REG_GP, 0x0000, 1, 13, 0, 1, CHECK_3D
    303  1.33      kent 	},
    304  1.33      kent 	{ AudioCoutputs,	AudioNspatial,	"center",
    305  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    306  1.33      kent 	  AC97_REG_3D_CONTROL, 0x0000, 4, 8, 0, 1, CHECK_3D
    307  1.33      kent 	},
    308  1.33      kent 	{ AudioCoutputs,	AudioNspatial,	"depth",
    309  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    310  1.33      kent 	  AC97_REG_3D_CONTROL, 0x0000, 4, 0, 0, 1, CHECK_3D
    311   1.1  augustss 	},
    312   1.1  augustss 
    313   1.1  augustss 	/* Missing features: Simulated Stereo, POP, Loopback mode */
    314  1.60   thorpej };
    315   1.1  augustss 
    316   1.1  augustss #define SOURCE_INFO_SIZE (sizeof(source_info)/sizeof(source_info[0]))
    317   1.1  augustss 
    318   1.1  augustss /*
    319   1.1  augustss  * Check out http://developer.intel.com/pc-supp/platform/ac97/ for
    320   1.1  augustss  * information on AC-97
    321   1.1  augustss  */
    322   1.1  augustss 
    323   1.1  augustss struct ac97_softc {
    324  1.28      kent 	/* ac97_codec_if must be at the first of ac97_softc. */
    325  1.14   thorpej 	struct ac97_codec_if codec_if;
    326   1.1  augustss 
    327  1.14   thorpej 	struct ac97_host_if *host_if;
    328   1.1  augustss 
    329  1.33      kent #define MAX_SOURCES	(2 * SOURCE_INFO_SIZE)
    330  1.33      kent 	struct ac97_source_info source_info[MAX_SOURCES];
    331   1.1  augustss 	int num_source_info;
    332  1.14   thorpej 
    333  1.14   thorpej 	enum ac97_host_flags host_flags;
    334  1.28      kent 	unsigned int ac97_clock; /* usually 48000 */
    335  1.28      kent #define AC97_STANDARD_CLOCK	48000U
    336  1.28      kent 	u_int16_t caps;		/* -> AC97_REG_RESET */
    337  1.28      kent 	u_int16_t ext_id;	/* -> AC97_REG_EXT_AUDIO_ID */
    338  1.14   thorpej 	u_int16_t shadow_reg[128];
    339   1.1  augustss };
    340   1.1  augustss 
    341  1.60   thorpej static struct ac97_codec_if_vtbl ac97civ = {
    342  1.28      kent 	ac97_mixer_get_port,
    343   1.1  augustss 	ac97_mixer_set_port,
    344   1.1  augustss 	ac97_query_devinfo,
    345  1.14   thorpej 	ac97_get_portnum_by_name,
    346  1.14   thorpej 	ac97_restore_shadow,
    347  1.28      kent 	ac97_get_extcaps,
    348  1.28      kent 	ac97_set_rate,
    349  1.28      kent 	ac97_set_clock,
    350  1.65      kent 	ac97_detach,
    351   1.1  augustss };
    352   1.7   thorpej 
    353   1.7   thorpej static const struct ac97_codecid {
    354   1.1  augustss 	u_int32_t id;
    355  1.31      kent 	u_int32_t mask;
    356   1.7   thorpej 	const char *name;
    357  1.33      kent 	void (*init)(struct ac97_softc *);
    358   1.1  augustss } ac97codecid[] = {
    359  1.36      kent 	/*
    360  1.36      kent 	 * Analog Devices SoundMAX
    361  1.36      kent 	 * http://www.soundmax.com/products/information/codecs.html
    362  1.36      kent 	 * http://www.analog.com/productSelection/pdf/AD1881A_0.pdf
    363  1.36      kent 	 * http://www.analog.com/productSelection/pdf/AD1885_0.pdf
    364  1.44      kent 	 * http://www.analog.com/UploadedFiles/Data_Sheets/206585810AD1980_0.pdf
    365  1.36      kent 	 * http://www.analog.com/productSelection/pdf/AD1981A_0.pdf
    366  1.36      kent 	 * http://www.analog.com/productSelection/pdf/AD1981B_0.pdf
    367  1.48      kent 	 * http://www.analog.com/UploadedFiles/Data_Sheets/180644528AD1985_0.pdf
    368  1.36      kent 	 */
    369  1.31      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 3),
    370  1.31      kent 	  0xffffffff,			"Analog Devices AD1819B" },
    371  1.34      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0x40),
    372  1.31      kent 	  0xffffffff,			"Analog Devices AD1881" },
    373  1.34      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0x48),
    374  1.31      kent 	  0xffffffff,			"Analog Devices AD1881A" },
    375  1.34      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0x60),
    376  1.31      kent 	  0xffffffff,			"Analog Devices AD1885" },
    377  1.34      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0x61),
    378  1.34      kent 	  0xffffffff,			"Analog Devices AD1886" },
    379  1.34      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0x63),
    380  1.31      kent 	  0xffffffff,			"Analog Devices AD1886A" },
    381  1.55      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0x68),
    382  1.55      kent 	  0xffffffff,			"Analog Devices AD1888", ac97_ad198x_init },
    383  1.38      matt 	{ AC97_CODEC_ID('A', 'D', 'S', 0x70),
    384  1.51      kent 	  0xffffffff,			"Analog Devices AD1980", ac97_ad198x_init },
    385  1.34      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0x72),
    386  1.34      kent 	  0xffffffff,			"Analog Devices AD1981A" },
    387  1.36      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0x74),
    388  1.36      kent 	  0xffffffff,			"Analog Devices AD1981B" },
    389  1.48      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0x75),
    390  1.51      kent 	  0xffffffff,			"Analog Devices AD1985", ac97_ad198x_init },
    391  1.33      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0),
    392  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Analog Devices unknown" },
    393  1.31      kent 
    394  1.33      kent 	/*
    395  1.33      kent 	 * Datasheets:
    396  1.33      kent 	 *	http://www.asahi-kasei.co.jp/akm/usa/product/ak4541/ek4541.pdf
    397  1.33      kent 	 *	http://www.asahi-kasei.co.jp/akm/usa/product/ak4543/ek4543.pdf
    398  1.33      kent 	 *	http://www.asahi-kasei.co.jp/akm/usa/product/ak4544a/ek4544a.pdf
    399  1.33      kent 	 *	http://www.asahi-kasei.co.jp/akm/usa/product/ak4545/ek4545.pdf
    400  1.33      kent 	 */
    401  1.31      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 0),
    402  1.31      kent 	  0xffffffff,			"Asahi Kasei AK4540"	},
    403  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 1),
    404  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4542"	},
    405  1.31      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 2),
    406  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4541/AK4543" },
    407  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 5),
    408  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4544" },
    409  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 6),
    410  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4544A" },
    411  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 7),
    412  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4545" },
    413  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 0),
    414  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Asahi Kasei unknown" },
    415  1.31      kent 
    416  1.33      kent 	/*
    417  1.33      kent 	 * Realtek & Avance Logic
    418  1.33      kent 	 *	http://www.realtek.com.tw/downloads/downloads1-3.aspx?lineid=5&famid=All&series=All&Spec=True
    419  1.54      kent 	 *
    420  1.54      kent 	 * ALC650 and ALC658 support VRA, but it supports only 8000, 11025,
    421  1.54      kent 	 * 12000, 16000, 22050, 24000, 32000, 44100, and 48000 Hz.
    422  1.33      kent 	 */
    423  1.34      kent 	{ AC97_CODEC_ID('A', 'L', 'C', 0x00),
    424  1.34      kent 	  0xfffffff0,			"Realtek RL5306"	},
    425  1.34      kent 	{ AC97_CODEC_ID('A', 'L', 'C', 0x10),
    426  1.34      kent 	  0xfffffff0,			"Realtek RL5382"	},
    427  1.33      kent 	{ AC97_CODEC_ID('A', 'L', 'C', 0x20),
    428  1.34      kent 	  0xfffffff0,			"Realtek RL5383/RL5522/ALC100"	},
    429  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x10),
    430  1.33      kent 	  0xffffffff,			"Avance Logic ALC200/ALC201"	},
    431  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x20),
    432  1.54      kent 	  0xfffffff0,			"Avance Logic ALC650", ac97_alc650_init },
    433  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x30),
    434  1.33      kent 	  0xffffffff,			"Avance Logic ALC101"	},
    435  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x40),
    436  1.33      kent 	  0xffffffff,			"Avance Logic ALC202"	},
    437  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x50),
    438  1.33      kent 	  0xffffffff,			"Avance Logic ALC250"	},
    439  1.54      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x60),
    440  1.54      kent 	  0xfffffff0,			"Avance Logic ALC655"	},
    441  1.54      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x80),
    442  1.54      kent 	  0xfffffff0,			"Avance Logic ALC658"	},
    443  1.54      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x90),
    444  1.54      kent 	  0xfffffff0,			"Avance Logic ALC850"	},
    445  1.33      kent 	{ AC97_CODEC_ID('A', 'L', 'C', 0),
    446  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Realtek unknown"	},
    447  1.33      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0),
    448  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Avance Logic unknown"	},
    449  1.31      kent 
    450  1.56      kent 	/**
    451  1.56      kent 	 * C-Media Electronics Inc.
    452  1.56      kent 	 * http://www.cmedia.com.tw/doc/CMI9739%206CH%20Audio%20Codec%20SPEC_Ver12.pdf
    453  1.56      kent 	 */
    454  1.56      kent 	{ AC97_CODEC_ID('C', 'M', 'I', 0x61),
    455  1.57      kent 	  0xffffffff,			"C-Media CMI9739"	},
    456  1.56      kent 	{ AC97_CODEC_ID('C', 'M', 'I', 0),
    457  1.56      kent 	  AC97_VENDOR_ID_MASK,		"C-Media unknown"	},
    458  1.56      kent 
    459  1.31      kent 	/* Cirrus Logic, Crystal series:
    460  1.31      kent 	 *  'C' 'R' 'Y' 0x0[0-7]  - CS4297
    461  1.31      kent 	 *              0x1[0-7]  - CS4297A
    462  1.31      kent 	 *              0x2[0-7]  - CS4298
    463  1.31      kent 	 *              0x2[8-f]  - CS4294
    464  1.31      kent 	 *              0x3[0-7]  - CS4299
    465  1.31      kent 	 *              0x4[8-f]  - CS4201
    466  1.31      kent 	 *              0x5[8-f]  - CS4205
    467  1.31      kent 	 *              0x6[0-7]  - CS4291
    468  1.31      kent 	 *              0x7[0-7]  - CS4202
    469  1.31      kent 	 * Datasheets:
    470  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4297A-5.pdf?DocumentID=593
    471  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4294.pdf?DocumentID=32
    472  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4299-5.pdf?DocumentID=594
    473  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4201-2.pdf?DocumentID=492
    474  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4205-2.pdf?DocumentID=492
    475  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4202-1.pdf?DocumentID=852
    476  1.31      kent 	 */
    477  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x00),
    478  1.31      kent 	  0xfffffff8,			"Crystal CS4297",	},
    479  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x10),
    480  1.31      kent 	  0xfffffff8,			"Crystal CS4297A",	},
    481  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x20),
    482  1.31      kent 	  0xfffffff8,			"Crystal CS4298",	},
    483  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x28),
    484  1.31      kent 	  0xfffffff8,			"Crystal CS4294",	},
    485  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x30),
    486  1.31      kent 	  0xfffffff8,			"Crystal CS4299",	},
    487  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x48),
    488  1.31      kent 	  0xfffffff8,			"Crystal CS4201",	},
    489  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x58),
    490  1.31      kent 	  0xfffffff8,			"Crystal CS4205",	},
    491  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x60),
    492  1.31      kent 	  0xfffffff8,			"Crystal CS4291",	},
    493  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x70),
    494  1.31      kent 	  0xfffffff8,			"Crystal CS4202",	},
    495  1.33      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0),
    496  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Cirrus Logic unknown",	},
    497  1.33      kent 
    498  1.33      kent 	{ 0x45838308, 0xffffffff,	"ESS Technology ES1921", },
    499  1.33      kent 	{ 0x45838300, AC97_VENDOR_ID_MASK, "ESS Technology unknown", },
    500  1.31      kent 
    501  1.34      kent 	{ AC97_CODEC_ID('H', 'R', 'S', 0),
    502  1.34      kent 	  0xffffffff,			"Intersil HMP9701",	},
    503  1.34      kent 	{ AC97_CODEC_ID('H', 'R', 'S', 0),
    504  1.34      kent 	  AC97_VENDOR_ID_MASK,		"Intersil unknown",	},
    505  1.34      kent 
    506  1.35      kent 	/*
    507  1.35      kent 	 * IC Ensemble (VIA)
    508  1.35      kent 	 *	http://www.viatech.com/en/datasheet/DS1616.pdf
    509  1.35      kent 	 */
    510  1.34      kent 	{ AC97_CODEC_ID('I', 'C', 'E', 0x01),
    511  1.35      kent 	  0xffffffff,			"ICEnsemble ICE1230/VT1611",	},
    512  1.34      kent 	{ AC97_CODEC_ID('I', 'C', 'E', 0x11),
    513  1.35      kent 	  0xffffffff,			"ICEnsemble ICE1232/VT1611A",	},
    514  1.35      kent 	{ AC97_CODEC_ID('I', 'C', 'E', 0x14),
    515  1.35      kent 	  0xffffffff,			"ICEnsemble ICE1232A",	},
    516  1.35      kent 	{ AC97_CODEC_ID('I', 'C', 'E', 0x51),
    517  1.35      kent 	  0xffffffff,			"VIA Technologies VT1616", ac97_vt1616_init },
    518  1.58      kent 	{ AC97_CODEC_ID('I', 'C', 'E', 0x52),
    519  1.58      kent 	  0xffffffff,			"VIA Technologies VT1616i", ac97_vt1616_init },
    520  1.34      kent 	{ AC97_CODEC_ID('I', 'C', 'E', 0),
    521  1.58      kent 	  AC97_VENDOR_ID_MASK,		"ICEnsemble/VIA unknown",	},
    522  1.34      kent 
    523  1.34      kent 	{ AC97_CODEC_ID('N', 'S', 'C', 0),
    524  1.34      kent 	  0xffffffff,			"National Semiconductor LM454[03568]", },
    525  1.31      kent 	{ AC97_CODEC_ID('N', 'S', 'C', 49),
    526  1.31      kent 	  0xffffffff,			"National Semiconductor LM4549", },
    527  1.33      kent 	{ AC97_CODEC_ID('N', 'S', 'C', 0),
    528  1.33      kent 	  AC97_VENDOR_ID_MASK,		"National Semiconductor unknown", },
    529  1.41       bsh 
    530  1.41       bsh 	{ AC97_CODEC_ID('P', 'S', 'C', 4),
    531  1.41       bsh 	  0xffffffff,			"Philips Semiconductor UCB1400", },
    532  1.41       bsh 	{ AC97_CODEC_ID('P', 'S', 'C', 0),
    533  1.41       bsh 	  AC97_VENDOR_ID_MASK,		"Philips Semiconductor unknown", },
    534  1.33      kent 
    535  1.31      kent 	{ AC97_CODEC_ID('S', 'I', 'L', 34),
    536  1.31      kent 	  0xffffffff,			"Silicon Laboratory Si3036", },
    537  1.31      kent 	{ AC97_CODEC_ID('S', 'I', 'L', 35),
    538  1.31      kent 	  0xffffffff,			"Silicon Laboratory Si3038", },
    539  1.33      kent 	{ AC97_CODEC_ID('S', 'I', 'L', 0),
    540  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Silicon Laboratory unknown", },
    541  1.33      kent 
    542  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 2),
    543  1.31      kent 	  0xffffffff,			"TriTech TR28022",	},
    544  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 3),
    545  1.31      kent 	  0xffffffff,			"TriTech TR28023",	},
    546  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 6),
    547  1.31      kent 	  0xffffffff,			"TriTech TR28026",	},
    548  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 8),
    549  1.31      kent 	  0xffffffff,			"TriTech TR28028",	},
    550  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 35),
    551  1.31      kent 	  0xffffffff,			"TriTech TR28602",	},
    552  1.33      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 0),
    553  1.33      kent 	  AC97_VENDOR_ID_MASK,		"TriTech unknown",	},
    554  1.33      kent 
    555  1.34      kent 	{ AC97_CODEC_ID('T', 'X', 'N', 0x20),
    556  1.34      kent 	  0xffffffff,			"Texas Instruments TLC320AD9xC", },
    557  1.34      kent 	{ AC97_CODEC_ID('T', 'X', 'N', 0),
    558  1.34      kent 	  AC97_VENDOR_ID_MASK,		"Texas Instruments unknown", },
    559  1.34      kent 
    560  1.34      kent 	/*
    561  1.34      kent 	 * VIA
    562  1.34      kent 	 * http://www.viatech.com/en/multimedia/audio.jsp
    563  1.34      kent 	 */
    564  1.34      kent 	{ AC97_CODEC_ID('V', 'I', 'A', 0x61),
    565  1.34      kent 	  0xffffffff,			"VIA Technologies VT1612A", },
    566  1.34      kent 	{ AC97_CODEC_ID('V', 'I', 'A', 0),
    567  1.34      kent 	  AC97_VENDOR_ID_MASK,		"VIA Technologies unknown", },
    568  1.34      kent 
    569  1.34      kent 	{ AC97_CODEC_ID('W', 'E', 'C', 1),
    570  1.34      kent 	  0xffffffff,			"Winbond W83971D",	},
    571  1.34      kent 	{ AC97_CODEC_ID('W', 'E', 'C', 0),
    572  1.34      kent 	  AC97_VENDOR_ID_MASK,		"Winbond unknown",	},
    573  1.34      kent 
    574  1.33      kent 	/*
    575  1.33      kent 	 * http://www.wolfsonmicro.com/product_list.asp?cid=64
    576  1.34      kent 	 *	http://www.wolfsonmicro.com/download.asp/did.56/WM9701A.pdf - 00
    577  1.34      kent 	 *	http://www.wolfsonmicro.com/download.asp/did.57/WM9703.pdf  - 03
    578  1.34      kent 	 *	http://www.wolfsonmicro.com/download.asp/did.58/WM9704M.pdf - 04
    579  1.34      kent 	 *	http://www.wolfsonmicro.com/download.asp/did.59/WM9704Q.pdf - 04
    580  1.34      kent 	 *	http://www.wolfsonmicro.com/download.asp/did.184/WM9705_Rev34.pdf - 05
    581  1.34      kent 	 *	http://www.wolfsonmicro.com/download.asp/did.60/WM9707.pdf  - 03
    582  1.34      kent 	 *	http://www.wolfsonmicro.com/download.asp/did.136/WM9708.pdf - 03
    583  1.34      kent 	 *	http://www.wolfsonmicro.com/download.asp/did.243/WM9710.pdf - 05
    584  1.33      kent 	 */
    585  1.31      kent 	{ AC97_CODEC_ID('W', 'M', 'L', 0),
    586  1.34      kent 	  0xffffffff,			"Wolfson WM9701A",	},
    587  1.34      kent 	{ AC97_CODEC_ID('W', 'M', 'L', 3),
    588  1.34      kent 	  0xffffffff,			"Wolfson WM9703/WM9707/WM9708",	},
    589  1.34      kent 	{ AC97_CODEC_ID('W', 'M', 'L', 4),
    590  1.31      kent 	  0xffffffff,			"Wolfson WM9704",	},
    591  1.34      kent 	{ AC97_CODEC_ID('W', 'M', 'L', 5),
    592  1.34      kent 	  0xffffffff,			"Wolfson WM9705/WM9710", },
    593  1.33      kent 	{ AC97_CODEC_ID('W', 'M', 'L', 0),
    594  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Wolfson unknown",	},
    595  1.33      kent 
    596  1.33      kent 	/*
    597  1.33      kent 	 * http://www.yamaha.co.jp/english/product/lsi/us/products/pcaudio.html
    598  1.33      kent 	 * Datasheets:
    599  1.33      kent 	 *	http://www.yamaha.co.jp/english/product/lsi/us/products/pdf/4MF743A20.pdf
    600  1.33      kent 	 *	http://www.yamaha.co.jp/english/product/lsi/us/products/pdf/4MF753A20.pdf
    601  1.33      kent 	 */
    602  1.31      kent 	{ AC97_CODEC_ID('Y', 'M', 'H', 0),
    603  1.31      kent 	  0xffffffff,			"Yamaha YMF743-S",	},
    604  1.31      kent 	{ AC97_CODEC_ID('Y', 'M', 'H', 3),
    605  1.31      kent 	  0xffffffff,			"Yamaha YMF753-S",	},
    606  1.33      kent 	{ AC97_CODEC_ID('Y', 'M', 'H', 0),
    607  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Yamaha unknown",	},
    608  1.33      kent 
    609  1.33      kent 	/*
    610  1.53       mrg 	 * http://www.sigmatel.com/products/technical_docs.htm
    611  1.53       mrg 	 * and
    612  1.53       mrg 	 * http://www.sigmatel.com/documents/c-major-brochure-9-0.pdf
    613  1.33      kent 	 */
    614  1.31      kent 	{ 0x83847600, 0xffffffff,	"SigmaTel STAC9700",	},
    615  1.31      kent 	{ 0x83847604, 0xffffffff,	"SigmaTel STAC9701/3/4/5", },
    616  1.31      kent 	{ 0x83847605, 0xffffffff,	"SigmaTel STAC9704",	},
    617  1.31      kent 	{ 0x83847608, 0xffffffff,	"SigmaTel STAC9708",	},
    618  1.31      kent 	{ 0x83847609, 0xffffffff,	"SigmaTel STAC9721/23",	},
    619  1.31      kent 	{ 0x83847644, 0xffffffff,	"SigmaTel STAC9744/45",	},
    620  1.36      kent 	{ 0x83847650, 0xffffffff,	"SigmaTel STAC9750/51",	},
    621  1.34      kent 	{ 0x83847656, 0xffffffff,	"SigmaTel STAC9756/57",	},
    622  1.53       mrg 	{ 0x83847658, 0xffffffff,	"SigmaTel STAC9758/59",	},
    623  1.36      kent 	{ 0x83847666, 0xffffffff,	"SigmaTel STAC9766/67",	},
    624  1.31      kent 	{ 0x83847684, 0xffffffff,	"SigmaTel STAC9783/84",	},
    625  1.33      kent 	{ 0x83847600, AC97_VENDOR_ID_MASK, "SigmaTel unknown",	},
    626  1.33      kent 
    627  1.31      kent 	{ 0,
    628  1.31      kent 	  0,			NULL,			}
    629   1.1  augustss };
    630   1.1  augustss 
    631  1.18  jdolecek static const char * const ac97enhancement[] = {
    632   1.2     soren 	"no 3D stereo",
    633   1.1  augustss 	"Analog Devices Phat Stereo",
    634  1.24    ichiro 	"Creative",
    635   1.1  augustss 	"National Semi 3D",
    636   1.1  augustss 	"Yamaha Ymersion",
    637   1.1  augustss 	"BBE 3D",
    638  1.24    ichiro 	"Crystal Semi 3D",
    639   1.1  augustss 	"Qsound QXpander",
    640   1.1  augustss 	"Spatializer 3D",
    641   1.1  augustss 	"SRS 3D",
    642   1.1  augustss 	"Platform Tech 3D",
    643   1.1  augustss 	"AKM 3D",
    644   1.1  augustss 	"Aureal",
    645   1.1  augustss 	"AZTECH 3D",
    646   1.1  augustss 	"Binaura 3D",
    647   1.1  augustss 	"ESS Technology",
    648   1.1  augustss 	"Harman International VMAx",
    649   1.1  augustss 	"Nvidea 3D",
    650   1.1  augustss 	"Philips Incredible Sound",
    651   1.1  augustss 	"Texas Instruments' 3D",
    652   1.1  augustss 	"VLSI Technology 3D",
    653   1.1  augustss 	"TriTech 3D",
    654   1.1  augustss 	"Realtek 3D",
    655   1.1  augustss 	"Samsung 3D",
    656   1.1  augustss 	"Wolfson Microelectronics 3D",
    657   1.1  augustss 	"Delta Integration 3D",
    658   1.1  augustss 	"SigmaTel 3D",
    659  1.33      kent 	"KS Waves 3D",
    660   1.1  augustss 	"Rockwell 3D",
    661   1.1  augustss 	"Unknown 3D",
    662   1.1  augustss 	"Unknown 3D",
    663   1.1  augustss 	"Unknown 3D",
    664   1.1  augustss };
    665   1.1  augustss 
    666  1.18  jdolecek static const char * const ac97feature[] = {
    667  1.27      kent 	"dedicated mic channel",
    668   1.1  augustss 	"reserved",
    669   1.1  augustss 	"tone",
    670   1.1  augustss 	"simulated stereo",
    671   1.1  augustss 	"headphone",
    672   1.1  augustss 	"bass boost",
    673   1.1  augustss 	"18 bit DAC",
    674   1.1  augustss 	"20 bit DAC",
    675   1.1  augustss 	"18 bit ADC",
    676   1.1  augustss 	"20 bit ADC"
    677   1.1  augustss };
    678   1.1  augustss 
    679   1.1  augustss 
    680   1.1  augustss /* #define AC97_DEBUG 10 */
    681  1.61      kent /* #define AC97_IO_DEBUG */
    682   1.1  augustss 
    683   1.1  augustss #ifdef AUDIO_DEBUG
    684   1.1  augustss #define DPRINTF(x)	if (ac97debug) printf x
    685   1.1  augustss #define DPRINTFN(n,x)	if (ac97debug>(n)) printf x
    686   1.1  augustss #ifdef AC97_DEBUG
    687   1.1  augustss int	ac97debug = AC97_DEBUG;
    688   1.1  augustss #else
    689   1.1  augustss int	ac97debug = 0;
    690   1.1  augustss #endif
    691   1.1  augustss #else
    692   1.1  augustss #define DPRINTF(x)
    693   1.1  augustss #define DPRINTFN(n,x)
    694   1.1  augustss #endif
    695   1.1  augustss 
    696  1.61      kent #ifdef AC97_IO_DEBUG
    697  1.61      kent static const char *ac97_register_names[0x80 / 2] = {
    698  1.61      kent 	"RESET", "MASTER_VOLUME", "HEADPHONE_VOLUME", "MASTER_VOLUME_MONO",
    699  1.61      kent 	"MASTER_TONE", "PCBEEP_VOLUME", "PHONE_VOLUME", "MIC_VOLUME",
    700  1.61      kent 	"LINEIN_VOLUME", "CD_VOLUME", "VIDEO_VOLUME", "AUX_VOLUME",
    701  1.61      kent 	"PCMOUT_VOLUME", "RECORD_SELECT", "RECORD_GATIN", "RECORD_GAIN_MIC",
    702  1.61      kent 	"GP", "3D_CONTROL", "AUDIO_INT", "POWER",
    703  1.61      kent 	"EXT_AUDIO_ID", "EXT_AUDIO_CTRL", "PCM_FRONT_DAC_RATE", "PCM_SURR_DAC_RATE",
    704  1.61      kent 	"PCM_LFE_DAC_RATE", "PCM_LR_ADC_RATE", "PCM_MIC_ADC_RATE", "CENTER_LFE_MASTER",
    705  1.61      kent 	"SURR_MASTER", "SPDIF_CTRL", "EXT_MODEM_ID", "EXT_MODEM_CTRL",
    706  1.61      kent 	"LINE1_RATE", "LINE2_RATE", "HANDSET_RATE", "LINE1_LEVEL",
    707  1.61      kent 	"LINE2_LEVEL", "HANDSET_LEVEL", "GPIO_PIN_CONFIG", "GPIO_PIN_POLARITY",
    708  1.61      kent 	"GPIO_PIN_STICKY", "GPIO_PIN_WAKEUP", "GPIO_PIN_STATUS", "MISC_MODEM_CTRL",
    709  1.61      kent 	"0x58", "VENDOR-5A", "VENDOR-5C", "VENDOR-5E",
    710  1.61      kent 	"0x60", "0x62", "0x64", "0x66",
    711  1.61      kent 	"0x68", "0x6a", "0x6c", "0x6e",
    712  1.61      kent 	"VENDOR-70", "VENDOR-72", "VENDOR-74", "VENDOR-76",
    713  1.61      kent 	"VENDOR-78", "VENDOR-7A", "VENDOR_ID1", "VENDOR_ID2"
    714  1.61      kent };
    715  1.61      kent #endif
    716  1.61      kent 
    717  1.60   thorpej static void
    718  1.50      kent ac97_read(struct ac97_softc *as, u_int8_t reg, u_int16_t *val)
    719  1.14   thorpej {
    720  1.14   thorpej 	if (as->host_flags & AC97_HOST_DONT_READ &&
    721  1.14   thorpej 	    (reg != AC97_REG_VENDOR_ID1 && reg != AC97_REG_VENDOR_ID2 &&
    722  1.14   thorpej 	     reg != AC97_REG_RESET)) {
    723  1.14   thorpej 		*val = as->shadow_reg[reg >> 1];
    724  1.14   thorpej 		return;
    725  1.14   thorpej 	}
    726  1.14   thorpej 
    727  1.39    simonb 	if (as->host_if->read(as->host_if->arg, reg, val)) {
    728  1.14   thorpej 		*val = as->shadow_reg[reg >> 1];
    729  1.14   thorpej 	}
    730  1.14   thorpej }
    731  1.14   thorpej 
    732  1.60   thorpej static int
    733  1.50      kent ac97_write(struct ac97_softc *as, u_int8_t reg, u_int16_t val)
    734  1.14   thorpej {
    735  1.61      kent #ifndef AC97_IO_DEBUG
    736  1.14   thorpej 	as->shadow_reg[reg >> 1] = val;
    737  1.50      kent 	return as->host_if->write(as->host_if->arg, reg, val);
    738  1.61      kent #else
    739  1.61      kent 	int ret;
    740  1.61      kent 	uint16_t actual;
    741  1.61      kent 
    742  1.61      kent 	as->shadow_reg[reg >> 1] = val;
    743  1.61      kent 	ret = as->host_if->write(as->host_if->arg, reg, val);
    744  1.61      kent 	as->host_if->read(as->host_if->arg, reg, &actual);
    745  1.61      kent 	if (val != actual && reg < 0x80) {
    746  1.61      kent 		printf("ac97_write: reg=%s, written=0x%04x, read=0x%04x\n",
    747  1.61      kent 		       ac97_register_names[reg / 2], val, actual);
    748  1.61      kent 	}
    749  1.61      kent 	return ret;
    750  1.61      kent #endif
    751  1.14   thorpej }
    752  1.14   thorpej 
    753  1.60   thorpej static void
    754  1.50      kent ac97_setup_defaults(struct ac97_softc *as)
    755  1.14   thorpej {
    756  1.14   thorpej 	int idx;
    757  1.18  jdolecek 	const struct ac97_source_info *si;
    758  1.14   thorpej 
    759  1.14   thorpej 	memset(as->shadow_reg, 0, sizeof(as->shadow_reg));
    760  1.14   thorpej 
    761  1.14   thorpej 	for (idx = 0; idx < SOURCE_INFO_SIZE; idx++) {
    762  1.14   thorpej 		si = &source_info[idx];
    763  1.14   thorpej 		ac97_write(as, si->reg, si->default_value);
    764  1.14   thorpej 	}
    765  1.14   thorpej }
    766  1.14   thorpej 
    767  1.60   thorpej static void
    768  1.50      kent ac97_restore_shadow(struct ac97_codec_if *self)
    769  1.14   thorpej {
    770  1.50      kent 	struct ac97_softc *as;
    771  1.50      kent 	const struct ac97_source_info *si;
    772  1.14   thorpej 	int idx;
    773  1.59      kent 	uint16_t val;
    774  1.14   thorpej 
    775  1.50      kent 	as = (struct ac97_softc *) self;
    776  1.59      kent 
    777  1.59      kent 	/* make sure chip is fully operational */
    778  1.59      kent #define	AC97_POWER_ALL	(AC97_POWER_REF | AC97_POWER_ANL | AC97_POWER_DAC \
    779  1.59      kent 			| AC97_POWER_ADC)
    780  1.64      kent 	for (idx = 50000; idx >= 0; idx--) {
    781  1.59      kent 		ac97_read(as, AC97_REG_POWER, &val);
    782  1.59      kent 		if ((val & AC97_POWER_ALL) == AC97_POWER_ALL)
    783  1.59      kent 		       break;
    784  1.64      kent 		DELAY(10);
    785  1.59      kent 	}
    786  1.59      kent #undef AC97_POWER_ALL
    787  1.59      kent 
    788  1.64      kent        /*
    789  1.64      kent 	* actually try changing a value!
    790  1.64      kent 	* The default value of AC97_REG_MASTER_VOLUME is 0x8000.
    791  1.64      kent 	*/
    792  1.64      kent        for (idx = 50000; idx >= 0; idx--) {
    793  1.64      kent 	       ac97_write(as, AC97_REG_MASTER_VOLUME, 0x1010);
    794  1.64      kent 	       ac97_read(as, AC97_REG_MASTER_VOLUME, &val);
    795  1.64      kent 	       if (val == 0x1010)
    796  1.64      kent 		       break;
    797  1.64      kent 	       DELAY(10);
    798  1.64      kent        }
    799  1.64      kent 
    800  1.64      kent        for (idx = 0; idx < SOURCE_INFO_SIZE; idx++) {
    801  1.14   thorpej 		si = &source_info[idx];
    802  1.59      kent 		/* don't "restore" to the reset reg! */
    803  1.59      kent 		if (si->reg != AC97_REG_RESET)
    804  1.59      kent 			ac97_write(as, si->reg, as->shadow_reg[si->reg >> 1]);
    805  1.42       scw 	}
    806  1.42       scw 
    807  1.42       scw 	if (as->ext_id & (AC97_EXT_AUDIO_VRA | AC97_EXT_AUDIO_DRA
    808  1.42       scw 			  | AC97_EXT_AUDIO_SPDIF | AC97_EXT_AUDIO_VRM
    809  1.42       scw 			  | AC97_EXT_AUDIO_CDAC | AC97_EXT_AUDIO_SDAC
    810  1.42       scw 			  | AC97_EXT_AUDIO_LDAC)) {
    811  1.42       scw 		ac97_write(as, AC97_REG_EXT_AUDIO_CTRL,
    812  1.42       scw 		    as->shadow_reg[AC97_REG_EXT_AUDIO_CTRL >> 1]);
    813  1.14   thorpej 	}
    814  1.14   thorpej }
    815   1.1  augustss 
    816  1.60   thorpej static int
    817  1.50      kent ac97_str_equal(const char *a, const char *b)
    818   1.1  augustss {
    819  1.50      kent 	return (a == b) || (a && b && (!strcmp(a, b)));
    820   1.1  augustss }
    821   1.1  augustss 
    822  1.60   thorpej static int
    823  1.33      kent ac97_check_capability(struct ac97_softc *as, int check)
    824  1.33      kent {
    825  1.33      kent 	switch (check) {
    826  1.33      kent 	case CHECK_NONE:
    827  1.33      kent 		return 1;
    828  1.33      kent 	case CHECK_SURROUND:
    829  1.33      kent 		return as->ext_id & AC97_EXT_AUDIO_SDAC;
    830  1.33      kent 	case CHECK_CENTER:
    831  1.33      kent 		return as->ext_id & AC97_EXT_AUDIO_CDAC;
    832  1.33      kent 	case CHECK_LFE:
    833  1.33      kent 		return as->ext_id & AC97_EXT_AUDIO_LDAC;
    834  1.33      kent 	case CHECK_HEADPHONES:
    835  1.33      kent 		return as->caps & AC97_CAPS_HEADPHONES;
    836  1.33      kent 	case CHECK_TONE:
    837  1.33      kent 		return as->caps & AC97_CAPS_TONECTRL;
    838  1.33      kent 	case CHECK_MIC:
    839  1.33      kent 		return as->caps & AC97_CAPS_MICIN;
    840  1.33      kent 	case CHECK_LOUDNESS:
    841  1.33      kent 		return as->caps & AC97_CAPS_LOUDNESS;
    842  1.33      kent 	case CHECK_3D:
    843  1.33      kent 		return AC97_CAPS_ENHANCEMENT(as->caps) != 0;
    844  1.33      kent 	default:
    845  1.33      kent 		printf("%s: internal error: feature=%d\n", __func__, check);
    846  1.33      kent 		return 0;
    847  1.33      kent 	}
    848  1.33      kent }
    849  1.33      kent 
    850  1.60   thorpej static void
    851  1.50      kent ac97_setup_source_info(struct ac97_softc *as)
    852   1.1  augustss {
    853   1.1  augustss 	int idx, ouridx;
    854  1.33      kent 	struct ac97_source_info *si, *si2;
    855   1.1  augustss 
    856   1.1  augustss 	for (idx = 0, ouridx = 0; idx < SOURCE_INFO_SIZE; idx++) {
    857   1.1  augustss 		si = &as->source_info[ouridx];
    858   1.1  augustss 
    859  1.33      kent 		if (!ac97_check_capability(as, source_info[idx].req_feature))
    860  1.33      kent 			continue;
    861  1.33      kent 
    862  1.20   thorpej 		memcpy(si, &source_info[idx], sizeof(*si));
    863   1.1  augustss 
    864   1.1  augustss 		switch (si->type) {
    865   1.1  augustss 		case AUDIO_MIXER_CLASS:
    866  1.36      kent 			si->mixer_class = ouridx;
    867   1.1  augustss 			ouridx++;
    868   1.1  augustss 			break;
    869   1.1  augustss 		case AUDIO_MIXER_VALUE:
    870   1.1  augustss 			/* Todo - Test to see if it works */
    871   1.1  augustss 			ouridx++;
    872   1.1  augustss 
    873   1.1  augustss 			/* Add an entry for mute, if necessary */
    874   1.1  augustss 			if (si->mute) {
    875   1.1  augustss 				si = &as->source_info[ouridx];
    876  1.20   thorpej 				memcpy(si, &source_info[idx], sizeof(*si));
    877   1.1  augustss 				si->qualifier = AudioNmute;
    878   1.1  augustss 				si->type = AUDIO_MIXER_ENUM;
    879   1.1  augustss 				si->info = &ac97_on_off;
    880   1.1  augustss 				si->info_size = sizeof(ac97_on_off);
    881   1.1  augustss 				si->bits = 1;
    882   1.1  augustss 				si->ofs = 15;
    883   1.1  augustss 				si->mute = 0;
    884   1.1  augustss 				si->polarity = 0;
    885   1.1  augustss 				ouridx++;
    886   1.1  augustss 			}
    887   1.1  augustss 			break;
    888   1.1  augustss 		case AUDIO_MIXER_ENUM:
    889   1.1  augustss 			/* Todo - Test to see if it works */
    890   1.1  augustss 			ouridx++;
    891   1.1  augustss 			break;
    892   1.1  augustss 		default:
    893  1.40   thorpej 			aprint_error ("ac97: shouldn't get here\n");
    894   1.1  augustss 			break;
    895   1.1  augustss 		}
    896   1.1  augustss 	}
    897   1.1  augustss 
    898   1.1  augustss 	as->num_source_info = ouridx;
    899   1.1  augustss 
    900   1.1  augustss 	for (idx = 0; idx < as->num_source_info; idx++) {
    901   1.1  augustss 		int idx2, previdx;
    902   1.1  augustss 
    903   1.1  augustss 		si = &as->source_info[idx];
    904   1.1  augustss 
    905   1.1  augustss 		/* Find mixer class */
    906   1.1  augustss 		for (idx2 = 0; idx2 < as->num_source_info; idx2++) {
    907   1.1  augustss 			si2 = &as->source_info[idx2];
    908   1.1  augustss 
    909  1.36      kent 			if (si2->type == AUDIO_MIXER_CLASS &&
    910   1.1  augustss 			    ac97_str_equal(si->class,
    911   1.1  augustss 					   si2->class)) {
    912   1.1  augustss 				si->mixer_class = idx2;
    913   1.1  augustss 			}
    914   1.1  augustss 		}
    915   1.1  augustss 
    916   1.1  augustss 
    917   1.1  augustss 		/* Setup prev and next pointers */
    918   1.1  augustss 		if (si->prev != 0)
    919   1.1  augustss 			continue;
    920   1.1  augustss 
    921   1.1  augustss 		if (si->qualifier)
    922   1.1  augustss 			continue;
    923   1.1  augustss 
    924   1.1  augustss 		si->prev = AUDIO_MIXER_LAST;
    925   1.1  augustss 		previdx = idx;
    926   1.1  augustss 
    927   1.1  augustss 		for (idx2 = 0; idx2 < as->num_source_info; idx2++) {
    928   1.1  augustss 			if (idx2 == idx)
    929   1.1  augustss 				continue;
    930   1.1  augustss 
    931   1.1  augustss 			si2 = &as->source_info[idx2];
    932   1.1  augustss 
    933   1.1  augustss 			if (!si2->prev &&
    934   1.1  augustss 			    ac97_str_equal(si->class, si2->class) &&
    935   1.1  augustss 			    ac97_str_equal(si->device, si2->device)) {
    936   1.1  augustss 				as->source_info[previdx].next = idx2;
    937   1.1  augustss 				as->source_info[idx2].prev = previdx;
    938  1.36      kent 
    939   1.1  augustss 				previdx = idx2;
    940   1.1  augustss 			}
    941   1.1  augustss 		}
    942   1.1  augustss 
    943   1.1  augustss 		as->source_info[previdx].next = AUDIO_MIXER_LAST;
    944   1.1  augustss 	}
    945   1.1  augustss }
    946   1.1  augustss 
    947  1.28      kent int
    948  1.50      kent ac97_attach(struct ac97_host_if *host_if)
    949   1.1  augustss {
    950   1.1  augustss 	struct ac97_softc *as;
    951  1.50      kent 	struct device *sc_dev;
    952   1.1  augustss 	int error, i, j;
    953  1.59      kent 	uint32_t id;
    954  1.59      kent 	uint16_t id1, id2;
    955  1.59      kent 	uint16_t extstat, rate;
    956  1.59      kent 	uint16_t val;
    957   1.8  augustss 	mixer_ctrl_t ctl;
    958  1.33      kent 	void (*initfunc)(struct ac97_softc *);
    959  1.44      kent #define FLAGBUFLEN	140
    960  1.44      kent 	char flagbuf[FLAGBUFLEN];
    961  1.28      kent 
    962  1.50      kent 	sc_dev = (struct device *)host_if->arg;
    963  1.33      kent 	initfunc = NULL;
    964  1.23   tsutsui 	as = malloc(sizeof(struct ac97_softc), M_DEVBUF, M_WAITOK|M_ZERO);
    965   1.1  augustss 
    966   1.2     soren 	if (as == NULL)
    967  1.50      kent 		return ENOMEM;
    968  1.14   thorpej 
    969  1.14   thorpej 	as->codec_if.vtbl = &ac97civ;
    970  1.14   thorpej 	as->host_if = host_if;
    971   1.1  augustss 
    972  1.14   thorpej 	if ((error = host_if->attach(host_if->arg, &as->codec_if))) {
    973  1.33      kent 		free(as, M_DEVBUF);
    974  1.50      kent 		return error;
    975   1.1  augustss 	}
    976   1.1  augustss 
    977  1.62      kent 	if ((error = host_if->reset(host_if->arg))) {
    978  1.62      kent 		free(as, M_DEVBUF);
    979  1.62      kent 		return error;
    980  1.62      kent 	}
    981   1.1  augustss 
    982  1.63      kent 	host_if->write(host_if->arg, AC97_REG_RESET, 0);
    983  1.14   thorpej 	host_if->write(host_if->arg, AC97_REG_POWER, 0);
    984   1.1  augustss 
    985  1.14   thorpej 	if (host_if->flags)
    986  1.14   thorpej 		as->host_flags = host_if->flags(host_if->arg);
    987   1.1  augustss 
    988  1.59      kent #define	AC97_POWER_ALL	(AC97_POWER_REF | AC97_POWER_ANL | AC97_POWER_DAC \
    989  1.59      kent 			| AC97_POWER_ADC)
    990  1.59      kent 	for (i = 500000; i >= 0; i--) {
    991  1.59      kent 		ac97_read(as, AC97_REG_POWER, &val);
    992  1.59      kent 		if ((val & AC97_POWER_ALL) == AC97_POWER_ALL)
    993  1.59      kent 		       break;
    994  1.59      kent 		DELAY(1);
    995  1.59      kent 	}
    996  1.59      kent #undef AC97_POWER_ALL
    997  1.59      kent 
    998  1.14   thorpej 	ac97_setup_defaults(as);
    999  1.52      kent 	ac97_read(as, AC97_REG_RESET, &as->caps);
   1000  1.15   thorpej 	ac97_read(as, AC97_REG_VENDOR_ID1, &id1);
   1001  1.15   thorpej 	ac97_read(as, AC97_REG_VENDOR_ID2, &id2);
   1002   1.1  augustss 
   1003   1.1  augustss 	id = (id1 << 16) | id2;
   1004   1.1  augustss 
   1005  1.44      kent 	aprint_normal("%s: ac97: ", sc_dev->dv_xname);
   1006   1.2     soren 
   1007   1.2     soren 	for (i = 0; ; i++) {
   1008   1.2     soren 		if (ac97codecid[i].id == 0) {
   1009   1.4  augustss 			char pnp[4];
   1010   1.7   thorpej 
   1011   1.7   thorpej 			AC97_GET_CODEC_ID(id, pnp);
   1012   1.4  augustss #define ISASCII(c) ((c) >= ' ' && (c) < 0x7f)
   1013   1.4  augustss 			if (ISASCII(pnp[0]) && ISASCII(pnp[1]) &&
   1014   1.4  augustss 			    ISASCII(pnp[2]))
   1015  1.40   thorpej 				aprint_normal("%c%c%c%d",
   1016  1.40   thorpej 				    pnp[0], pnp[1], pnp[2], pnp[3]);
   1017   1.4  augustss 			else
   1018  1.40   thorpej 				aprint_normal("unknown (0x%08x)", id);
   1019  1.11     soren 			break;
   1020  1.11     soren 		}
   1021  1.31      kent 		if (ac97codecid[i].id == (id & ac97codecid[i].mask)) {
   1022  1.40   thorpej 			aprint_normal("%s", ac97codecid[i].name);
   1023  1.33      kent 			if (ac97codecid[i].mask == AC97_VENDOR_ID_MASK) {
   1024  1.40   thorpej 				aprint_normal(" (0x%08x)", id);
   1025  1.33      kent 			}
   1026  1.33      kent 			initfunc = ac97codecid[i].init;
   1027   1.2     soren 			break;
   1028   1.2     soren 		}
   1029   1.1  augustss 	}
   1030  1.40   thorpej 	aprint_normal(" codec; ");
   1031   1.1  augustss 	for (i = j = 0; i < 10; i++) {
   1032  1.28      kent 		if (as->caps & (1 << i)) {
   1033  1.50      kent 			aprint_normal("%s%s", j ? ", " : "", ac97feature[i]);
   1034   1.1  augustss 			j++;
   1035   1.1  augustss 		}
   1036   1.1  augustss 	}
   1037  1.40   thorpej 	aprint_normal("%s%s\n", j ? ", " : "",
   1038  1.33      kent 	       ac97enhancement[AC97_CAPS_ENHANCEMENT(as->caps)]);
   1039   1.1  augustss 
   1040  1.28      kent 	as->ac97_clock = AC97_STANDARD_CLOCK;
   1041  1.28      kent 	ac97_read(as, AC97_REG_EXT_AUDIO_ID, &as->ext_id);
   1042  1.44      kent 	if (as->ext_id != 0) {
   1043  1.44      kent 		/* Print capabilities */
   1044  1.44      kent 		bitmask_snprintf(as->ext_id, "\20\20SECONDARY10\17SECONDARY01"
   1045  1.44      kent 				 "\14AC97_23\13AC97_22\12AMAP\11LDAC\10SDAC"
   1046  1.44      kent 				 "\7CDAC\4VRM\3SPDIF\2DRA\1VRA",
   1047  1.44      kent 				 flagbuf, FLAGBUFLEN);
   1048  1.44      kent 		aprint_normal("%s: ac97: ext id %s\n", sc_dev->dv_xname, flagbuf);
   1049  1.44      kent 
   1050  1.44      kent 		/* Print unusual settings */
   1051  1.44      kent 		if (as->ext_id & AC97_EXT_AUDIO_DSA_MASK) {
   1052  1.44      kent 			aprint_normal("%s: ac97: Slot assignment: ",
   1053  1.44      kent 				      sc_dev->dv_xname);
   1054  1.44      kent 			switch (as->ext_id & AC97_EXT_AUDIO_DSA_MASK) {
   1055  1.44      kent 			case AC97_EXT_AUDIO_DSA01:
   1056  1.44      kent 				aprint_normal("7&8, 6&9, 10&11.\n");
   1057  1.44      kent 				break;
   1058  1.44      kent 			case AC97_EXT_AUDIO_DSA10:
   1059  1.44      kent 				aprint_normal("6&9, 10&11, 3&4.\n");
   1060  1.44      kent 				break;
   1061  1.44      kent 			case AC97_EXT_AUDIO_DSA11:
   1062  1.44      kent 				aprint_normal("10&11, 3&4, 7&8.\n");
   1063  1.44      kent 				break;
   1064  1.44      kent 			}
   1065  1.44      kent 		}
   1066  1.27      kent 
   1067  1.44      kent 		/* Enable and disable features */
   1068  1.33      kent 		ac97_read(as, AC97_REG_EXT_AUDIO_CTRL, &extstat);
   1069  1.33      kent 		extstat &= ~AC97_EXT_AUDIO_DRA;
   1070  1.44      kent 		if (as->ext_id & AC97_EXT_AUDIO_LDAC)
   1071  1.44      kent 			extstat |= AC97_EXT_AUDIO_LDAC;
   1072  1.44      kent 		if (as->ext_id & AC97_EXT_AUDIO_SDAC)
   1073  1.44      kent 			extstat |= AC97_EXT_AUDIO_SDAC;
   1074  1.44      kent 		if (as->ext_id & AC97_EXT_AUDIO_CDAC)
   1075  1.44      kent 			extstat |= AC97_EXT_AUDIO_CDAC;
   1076  1.44      kent 		if (as->ext_id & AC97_EXT_AUDIO_VRM)
   1077  1.44      kent 			extstat |= AC97_EXT_AUDIO_VRM;
   1078  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_SPDIF) {
   1079  1.44      kent 			/* Output the same data as DAC to SPDIF output */
   1080  1.44      kent 			extstat &= ~AC97_EXT_AUDIO_SPSA_MASK;
   1081  1.44      kent 			extstat |= AC97_EXT_AUDIO_SPSA34;
   1082  1.27      kent 		}
   1083  1.44      kent 		if (as->ext_id & AC97_EXT_AUDIO_VRA)
   1084  1.44      kent 			extstat |= AC97_EXT_AUDIO_VRA;
   1085  1.33      kent 		ac97_write(as, AC97_REG_EXT_AUDIO_CTRL, extstat);
   1086  1.33      kent 		if (as->ext_id & AC97_EXT_AUDIO_VRA) {
   1087  1.33      kent 			/* VRA should be enabled. */
   1088  1.28      kent 			/* so it claims to do variable rate, let's make sure */
   1089  1.28      kent 			ac97_write(as, AC97_REG_PCM_FRONT_DAC_RATE, 44100);
   1090  1.28      kent 			ac97_read(as, AC97_REG_PCM_FRONT_DAC_RATE, &rate);
   1091  1.28      kent 			if (rate != 44100) {
   1092  1.28      kent 				/* We can't believe ext_id */
   1093  1.28      kent 				as->ext_id = 0;
   1094  1.40   thorpej 				aprint_normal(
   1095  1.40   thorpej 				    "%s: Ignore these capabilities.\n",
   1096  1.40   thorpej 				    sc_dev->dv_xname);
   1097  1.28      kent 			}
   1098  1.32      kent 			/* restore the default value */
   1099  1.32      kent 			ac97_write(as, AC97_REG_PCM_FRONT_DAC_RATE,
   1100  1.32      kent 				   AC97_SINGLE_RATE);
   1101  1.28      kent 		}
   1102  1.27      kent 	}
   1103   1.1  augustss 
   1104   1.1  augustss 	ac97_setup_source_info(as);
   1105   1.8  augustss 
   1106   1.8  augustss 	memset(&ctl, 0, sizeof(ctl));
   1107  1.33      kent 	/* disable mutes */
   1108  1.33      kent 	for (i = 0; i < 11; i++) {
   1109  1.33      kent 		static struct {
   1110  1.33      kent 			char *class, *device;
   1111  1.33      kent 		} d[11] = {
   1112  1.33      kent 			{ AudioCoutputs, AudioNmaster},
   1113  1.33      kent 			{ AudioCoutputs, AudioNheadphone},
   1114  1.33      kent 			{ AudioCoutputs, AudioNsurround},
   1115  1.33      kent 			{ AudioCoutputs, AudioNcenter},
   1116  1.33      kent 			{ AudioCoutputs, AudioNlfe},
   1117  1.33      kent 			{ AudioCinputs, AudioNdac},
   1118  1.33      kent 			{ AudioCinputs, AudioNcd},
   1119  1.33      kent 			{ AudioCinputs, AudioNline},
   1120  1.33      kent 			{ AudioCinputs, AudioNaux},
   1121  1.33      kent 			{ AudioCinputs, AudioNvideo},
   1122  1.33      kent 			{ AudioCrecord, AudioNvolume},
   1123  1.33      kent 		};
   1124  1.33      kent 
   1125  1.33      kent 		ctl.type = AUDIO_MIXER_ENUM;
   1126  1.33      kent 		ctl.un.ord = 0;
   1127  1.33      kent 
   1128  1.33      kent 		ctl.dev = ac97_get_portnum_by_name(&as->codec_if,
   1129  1.33      kent 			d[i].class, d[i].device, AudioNmute);
   1130  1.33      kent 		ac97_mixer_set_port(&as->codec_if, &ctl);
   1131  1.33      kent 	}
   1132   1.8  augustss 	ctl.type = AUDIO_MIXER_ENUM;
   1133  1.33      kent 	ctl.un.ord = 0;
   1134  1.33      kent 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCrecord,
   1135  1.33      kent 					   AudioNsource, NULL);
   1136  1.33      kent 	ac97_mixer_set_port(&as->codec_if, &ctl);
   1137  1.33      kent 
   1138  1.33      kent 	/* set a reasonable default volume */
   1139  1.33      kent 	ctl.type = AUDIO_MIXER_VALUE;
   1140  1.33      kent 	ctl.un.value.num_channels = 2;
   1141  1.33      kent 	ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = \
   1142  1.33      kent 	ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 127;
   1143  1.14   thorpej 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
   1144  1.33      kent 					   AudioNmaster, NULL);
   1145  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
   1146  1.33      kent 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
   1147  1.33      kent 					   AudioNsurround, NULL);
   1148  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
   1149  1.33      kent 	ctl.un.value.num_channels = 1;
   1150  1.33      kent 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
   1151  1.33      kent 					   AudioNcenter, NULL);
   1152  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
   1153  1.33      kent 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
   1154  1.33      kent 					   AudioNlfe, NULL);
   1155  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
   1156   1.1  augustss 
   1157  1.33      kent 	if (initfunc != NULL)
   1158  1.33      kent 		initfunc(as);
   1159  1.50      kent 	return 0;
   1160   1.1  augustss }
   1161   1.1  augustss 
   1162  1.65      kent static void
   1163  1.65      kent ac97_detach(struct ac97_codec_if *codec_if)
   1164  1.65      kent {
   1165  1.65      kent 	struct ac97_softc *as;
   1166  1.65      kent 
   1167  1.65      kent 	as = (struct ac97_softc *)codec_if;
   1168  1.65      kent 	ac97_write(as, AC97_REG_POWER, AC97_POWER_IN | AC97_POWER_OUT
   1169  1.65      kent 		   | AC97_POWER_MIXER | AC97_POWER_MIXER_VREF
   1170  1.65      kent 		   | AC97_POWER_ACLINK | AC97_POWER_CLK | AC97_POWER_AUX
   1171  1.65      kent 		   | AC97_POWER_EAMP);
   1172  1.65      kent 	free(as, M_DEVBUF);
   1173  1.65      kent }
   1174   1.1  augustss 
   1175  1.60   thorpej static int
   1176  1.50      kent ac97_query_devinfo(struct ac97_codec_if *codec_if, mixer_devinfo_t *dip)
   1177   1.1  augustss {
   1178  1.50      kent 	struct ac97_softc *as;
   1179  1.50      kent 	struct ac97_source_info *si;
   1180  1.50      kent 	const char *name;
   1181   1.1  augustss 
   1182  1.50      kent 	as = (struct ac97_softc *)codec_if;
   1183   1.1  augustss 	if (dip->index < as->num_source_info) {
   1184  1.50      kent 		si = &as->source_info[dip->index];
   1185   1.1  augustss 		dip->type = si->type;
   1186   1.1  augustss 		dip->mixer_class = si->mixer_class;
   1187   1.1  augustss 		dip->prev = si->prev;
   1188   1.1  augustss 		dip->next = si->next;
   1189  1.36      kent 
   1190   1.1  augustss 		if (si->qualifier)
   1191   1.1  augustss 			name = si->qualifier;
   1192   1.1  augustss 		else if (si->device)
   1193   1.1  augustss 			name = si->device;
   1194   1.1  augustss 		else if (si->class)
   1195   1.1  augustss 			name = si->class;
   1196   1.6  augustss 		else
   1197   1.6  augustss 			name = 0;
   1198  1.36      kent 
   1199   1.1  augustss 		if (name)
   1200   1.1  augustss 			strcpy(dip->label.name, name);
   1201   1.1  augustss 
   1202  1.20   thorpej 		memcpy(&dip->un, si->info, si->info_size);
   1203  1.19       erh 
   1204  1.19       erh 		/* Set the delta for volume sources */
   1205  1.19       erh 		if (dip->type == AUDIO_MIXER_VALUE)
   1206  1.19       erh 			dip->un.v.delta = 1 << (8 - si->bits);
   1207  1.19       erh 
   1208  1.50      kent 		return 0;
   1209   1.1  augustss 	}
   1210   1.1  augustss 
   1211  1.50      kent 	return ENXIO;
   1212   1.1  augustss }
   1213   1.1  augustss 
   1214   1.1  augustss 
   1215   1.1  augustss 
   1216  1.60   thorpej static int
   1217  1.50      kent ac97_mixer_set_port(struct ac97_codec_if *codec_if, mixer_ctrl_t *cp)
   1218   1.1  augustss {
   1219  1.50      kent 	struct ac97_softc *as;
   1220  1.50      kent 	struct ac97_source_info *si;
   1221   1.1  augustss 	u_int16_t mask;
   1222   1.1  augustss 	u_int16_t val, newval;
   1223   1.1  augustss 	int error;
   1224   1.1  augustss 
   1225  1.50      kent 	as = (struct ac97_softc *)codec_if;
   1226  1.50      kent 	si = &as->source_info[cp->dev];
   1227   1.1  augustss 	if (cp->dev < 0 || cp->dev >= as->num_source_info)
   1228  1.50      kent 		return EINVAL;
   1229   1.1  augustss 
   1230   1.1  augustss 	if (cp->type != si->type)
   1231  1.50      kent 		return EINVAL;
   1232   1.1  augustss 
   1233  1.15   thorpej 	ac97_read(as, si->reg, &val);
   1234   1.1  augustss 
   1235   1.1  augustss 	DPRINTFN(5, ("read(%x) = %x\n", si->reg, val));
   1236   1.1  augustss 
   1237   1.1  augustss 	mask = (1 << si->bits) - 1;
   1238   1.1  augustss 
   1239   1.1  augustss 	switch (cp->type) {
   1240   1.1  augustss 	case AUDIO_MIXER_ENUM:
   1241   1.1  augustss 		if (cp->un.ord > mask || cp->un.ord < 0)
   1242  1.50      kent 			return EINVAL;
   1243   1.1  augustss 
   1244   1.1  augustss 		newval = (cp->un.ord << si->ofs);
   1245   1.1  augustss 		if (si->reg == AC97_REG_RECORD_SELECT) {
   1246   1.1  augustss 			newval |= (newval << (8 + si->ofs));
   1247   1.1  augustss 			mask |= (mask << 8);
   1248  1.33      kent 			mask = mask << si->ofs;
   1249  1.33      kent 		} else if (si->reg == AC97_REG_SURR_MASTER) {
   1250  1.33      kent 			newval = cp->un.ord ? 0x8080 : 0x0000;
   1251  1.33      kent 			mask = 0x8080;
   1252  1.33      kent 		} else
   1253  1.33      kent 			mask = mask << si->ofs;
   1254   1.1  augustss 		break;
   1255   1.1  augustss 	case AUDIO_MIXER_VALUE:
   1256   1.1  augustss 	{
   1257  1.18  jdolecek 		const struct audio_mixer_value *value = si->info;
   1258  1.49      cube 		u_int16_t  l, r, ol, or;
   1259  1.49      cube 		int deltal, deltar;
   1260   1.1  augustss 
   1261   1.1  augustss 		if ((cp->un.value.num_channels <= 0) ||
   1262  1.36      kent 		    (cp->un.value.num_channels > value->num_channels))
   1263  1.50      kent 			return EINVAL;
   1264   1.1  augustss 
   1265   1.1  augustss 		if (cp->un.value.num_channels == 1) {
   1266   1.1  augustss 			l = r = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1267   1.1  augustss 		} else {
   1268  1.17        rh 			if (!(as->host_flags & AC97_HOST_SWAPPED_CHANNELS)) {
   1269  1.17        rh 				l = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1270  1.17        rh 				r = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1271  1.17        rh 			} else {	/* left/right is reversed here */
   1272  1.17        rh 				r = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1273  1.17        rh 				l = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1274  1.17        rh 			}
   1275  1.17        rh 
   1276   1.1  augustss 		}
   1277   1.1  augustss 
   1278   1.1  augustss 		if (!si->polarity) {
   1279   1.1  augustss 			l = 255 - l;
   1280   1.1  augustss 			r = 255 - r;
   1281   1.1  augustss 		}
   1282  1.36      kent 
   1283  1.49      cube 		ol = (val >> (8+si->ofs)) & mask;
   1284  1.49      cube 		or = (val >> si->ofs) & mask;
   1285  1.49      cube 
   1286  1.49      cube 		deltal = (ol << (8 - si->bits)) - l;
   1287  1.49      cube 		deltar = (or << (8 - si->bits)) - r;
   1288  1.49      cube 
   1289   1.1  augustss 		l = l >> (8 - si->bits);
   1290   1.1  augustss 		r = r >> (8 - si->bits);
   1291   1.1  augustss 
   1292  1.49      cube 		if (deltal && ol == l)
   1293  1.49      cube 			l += (deltal > 0) ? (l ? -1 : 0) : (l < mask ? 1 : 0);
   1294  1.49      cube 		if (deltar && or == r)
   1295  1.49      cube 			r += (deltar > 0) ? (r ? -1 : 0) : (r < mask ? 1 : 0);
   1296  1.49      cube 
   1297  1.49      cube 		newval = ((r & mask) << si->ofs);
   1298   1.1  augustss 		if (value->num_channels == 2) {
   1299  1.49      cube 			newval = newval | ((l & mask) << (si->ofs+8));
   1300   1.1  augustss 			mask |= (mask << 8);
   1301   1.1  augustss 		}
   1302  1.33      kent 		mask = mask << si->ofs;
   1303   1.1  augustss 		break;
   1304   1.1  augustss 	}
   1305   1.1  augustss 	default:
   1306  1.50      kent 		return EINVAL;
   1307   1.1  augustss 	}
   1308   1.1  augustss 
   1309  1.15   thorpej 	error = ac97_write(as, si->reg, (val & ~mask) | newval);
   1310   1.1  augustss 	if (error)
   1311  1.50      kent 		return error;
   1312   1.1  augustss 
   1313  1.50      kent 	return 0;
   1314   1.1  augustss }
   1315   1.1  augustss 
   1316  1.60   thorpej static int
   1317  1.50      kent ac97_get_portnum_by_name(struct ac97_codec_if *codec_if, const char *class,
   1318  1.50      kent 			 const char *device, const char *qualifier)
   1319   1.1  augustss {
   1320  1.50      kent 	struct ac97_softc *as;
   1321   1.1  augustss 	int idx;
   1322   1.1  augustss 
   1323  1.50      kent 	as = (struct ac97_softc *)codec_if;
   1324   1.1  augustss 	for (idx = 0; idx < as->num_source_info; idx++) {
   1325   1.1  augustss 		struct ac97_source_info *si = &as->source_info[idx];
   1326   1.1  augustss 		if (ac97_str_equal(class, si->class) &&
   1327   1.1  augustss 		    ac97_str_equal(device, si->device) &&
   1328   1.1  augustss 		    ac97_str_equal(qualifier, si->qualifier))
   1329  1.50      kent 			return idx;
   1330   1.1  augustss 	}
   1331   1.1  augustss 
   1332  1.50      kent 	return -1;
   1333   1.1  augustss }
   1334   1.1  augustss 
   1335  1.60   thorpej static int
   1336  1.50      kent ac97_mixer_get_port(struct ac97_codec_if *codec_if, mixer_ctrl_t *cp)
   1337   1.1  augustss {
   1338  1.50      kent 	struct ac97_softc *as;
   1339  1.50      kent 	struct ac97_source_info *si;
   1340   1.1  augustss 	u_int16_t mask;
   1341   1.1  augustss 	u_int16_t val;
   1342   1.1  augustss 
   1343  1.50      kent 	as = (struct ac97_softc *)codec_if;
   1344  1.50      kent 	si = &as->source_info[cp->dev];
   1345   1.1  augustss 	if (cp->dev < 0 || cp->dev >= as->num_source_info)
   1346  1.50      kent 		return EINVAL;
   1347   1.1  augustss 
   1348   1.1  augustss 	if (cp->type != si->type)
   1349  1.50      kent 		return EINVAL;
   1350   1.1  augustss 
   1351  1.15   thorpej 	ac97_read(as, si->reg, &val);
   1352   1.1  augustss 
   1353   1.1  augustss 	DPRINTFN(5, ("read(%x) = %x\n", si->reg, val));
   1354   1.1  augustss 
   1355   1.1  augustss 	mask = (1 << si->bits) - 1;
   1356   1.1  augustss 
   1357   1.1  augustss 	switch (cp->type) {
   1358   1.1  augustss 	case AUDIO_MIXER_ENUM:
   1359   1.1  augustss 		cp->un.ord = (val >> si->ofs) & mask;
   1360  1.36      kent 		DPRINTFN(4, ("AUDIO_MIXER_ENUM: %x %d %x %d\n",
   1361  1.36      kent 			     val, si->ofs, mask, cp->un.ord));
   1362   1.1  augustss 		break;
   1363   1.1  augustss 	case AUDIO_MIXER_VALUE:
   1364   1.1  augustss 	{
   1365  1.18  jdolecek 		const struct audio_mixer_value *value = si->info;
   1366   1.1  augustss 		u_int16_t  l, r;
   1367   1.1  augustss 
   1368   1.1  augustss 		if ((cp->un.value.num_channels <= 0) ||
   1369  1.36      kent 		    (cp->un.value.num_channels > value->num_channels))
   1370  1.50      kent 			return EINVAL;
   1371   1.1  augustss 
   1372   1.1  augustss 		if (value->num_channels == 1) {
   1373   1.1  augustss 			l = r = (val >> si->ofs) & mask;
   1374   1.1  augustss 		} else {
   1375  1.17        rh 			if (!(as->host_flags & AC97_HOST_SWAPPED_CHANNELS)) {
   1376  1.37      kent 				l = (val >> (si->ofs + 8)) & mask;
   1377  1.37      kent 				r = (val >> si->ofs) & mask;
   1378  1.37      kent 			} else {	/* host has reversed channels */
   1379  1.37      kent 				r = (val >> (si->ofs + 8)) & mask;
   1380  1.17        rh 				l = (val >> si->ofs) & mask;
   1381  1.17        rh 			}
   1382   1.1  augustss 		}
   1383   1.1  augustss 
   1384   1.1  augustss 		l = (l << (8 - si->bits));
   1385   1.1  augustss 		r = (r << (8 - si->bits));
   1386   1.1  augustss 		if (!si->polarity) {
   1387   1.1  augustss 			l = 255 - l;
   1388   1.1  augustss 			r = 255 - r;
   1389   1.1  augustss 		}
   1390   1.1  augustss 
   1391   1.1  augustss 		/* The EAP driver averages l and r for stereo
   1392   1.1  augustss 		   channels that are requested in MONO mode. Does this
   1393   1.1  augustss 		   make sense? */
   1394   1.1  augustss 		if (cp->un.value.num_channels == 1) {
   1395   1.1  augustss 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = l;
   1396   1.1  augustss 		} else if (cp->un.value.num_channels == 2) {
   1397   1.1  augustss 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
   1398   1.1  augustss 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1399   1.1  augustss 		}
   1400   1.1  augustss 
   1401   1.1  augustss 		break;
   1402   1.1  augustss 	}
   1403   1.1  augustss 	default:
   1404  1.50      kent 		return EINVAL;
   1405   1.1  augustss 	}
   1406   1.1  augustss 
   1407  1.50      kent 	return 0;
   1408   1.1  augustss }
   1409   1.1  augustss 
   1410  1.28      kent 
   1411  1.60   thorpej static int
   1412  1.28      kent ac97_set_rate(struct ac97_codec_if *codec_if, int target, u_long *rate)
   1413  1.28      kent {
   1414  1.28      kent 	struct ac97_softc *as;
   1415  1.28      kent 	u_long value;
   1416  1.28      kent 	u_int16_t ext_stat;
   1417  1.28      kent 	u_int16_t actual;
   1418  1.28      kent 	u_int16_t power;
   1419  1.28      kent 	u_int16_t power_bit;
   1420  1.28      kent 
   1421  1.28      kent 	as = (struct ac97_softc *)codec_if;
   1422  1.29      kent 	if (target == AC97_REG_PCM_MIC_ADC_RATE) {
   1423  1.29      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRM)) {
   1424  1.29      kent 			*rate = AC97_SINGLE_RATE;
   1425  1.29      kent 			return 0;
   1426  1.29      kent 		}
   1427  1.29      kent 	} else {
   1428  1.29      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRA)) {
   1429  1.29      kent 			*rate = AC97_SINGLE_RATE;
   1430  1.29      kent 			return 0;
   1431  1.29      kent 		}
   1432  1.29      kent 	}
   1433  1.28      kent 	value = *rate * AC97_STANDARD_CLOCK / as->ac97_clock;
   1434  1.28      kent 	ext_stat = 0;
   1435  1.28      kent 	/*
   1436  1.28      kent 	 * PCM_FRONT_DAC_RATE/PCM_SURR_DAC_RATE/PCM_LFE_DAC_RATE
   1437  1.28      kent 	 *	Check VRA, DRA
   1438  1.28      kent 	 * PCM_LR_ADC_RATE
   1439  1.28      kent 	 *	Check VRA
   1440  1.28      kent 	 * PCM_MIC_ADC_RATE
   1441  1.28      kent 	 *	Check VRM
   1442  1.28      kent 	 */
   1443  1.28      kent 	switch (target) {
   1444  1.28      kent 	case AC97_REG_PCM_FRONT_DAC_RATE:
   1445  1.28      kent 	case AC97_REG_PCM_SURR_DAC_RATE:
   1446  1.28      kent 	case AC97_REG_PCM_LFE_DAC_RATE:
   1447  1.28      kent 		power_bit = AC97_POWER_OUT;
   1448  1.28      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRA)) {
   1449  1.28      kent 			*rate = AC97_SINGLE_RATE;
   1450  1.28      kent 			return 0;
   1451  1.28      kent 		}
   1452  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_DRA) {
   1453  1.28      kent 			ac97_read(as, AC97_REG_EXT_AUDIO_CTRL, &ext_stat);
   1454  1.28      kent 			if (value > 0x1ffff) {
   1455  1.28      kent 				return EINVAL;
   1456  1.28      kent 			} else if (value > 0xffff) {
   1457  1.28      kent 				/* Enable DRA */
   1458  1.28      kent 				ext_stat |= AC97_EXT_AUDIO_DRA;
   1459  1.28      kent 				ac97_write(as, AC97_REG_EXT_AUDIO_CTRL, ext_stat);
   1460  1.28      kent 				value /= 2;
   1461  1.28      kent 			} else {
   1462  1.28      kent 				/* Disable DRA */
   1463  1.28      kent 				ext_stat &= ~AC97_EXT_AUDIO_DRA;
   1464  1.28      kent 				ac97_write(as, AC97_REG_EXT_AUDIO_CTRL, ext_stat);
   1465  1.28      kent 			}
   1466  1.28      kent 		} else {
   1467  1.28      kent 			if (value > 0xffff)
   1468  1.28      kent 				return EINVAL;
   1469  1.28      kent 		}
   1470  1.28      kent 		break;
   1471  1.28      kent 	case AC97_REG_PCM_LR_ADC_RATE:
   1472  1.28      kent 		power_bit = AC97_POWER_IN;
   1473  1.28      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRA)) {
   1474  1.28      kent 			*rate = AC97_SINGLE_RATE;
   1475  1.28      kent 			return 0;
   1476  1.28      kent 		}
   1477  1.28      kent 		if (value > 0xffff)
   1478  1.28      kent 			return EINVAL;
   1479  1.28      kent 		break;
   1480  1.28      kent 	case AC97_REG_PCM_MIC_ADC_RATE:
   1481  1.28      kent 		power_bit = AC97_POWER_IN;
   1482  1.28      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRM)) {
   1483  1.28      kent 			*rate = AC97_SINGLE_RATE;
   1484  1.28      kent 			return 0;
   1485  1.28      kent 		}
   1486  1.28      kent 		if (value > 0xffff)
   1487  1.28      kent 			return EINVAL;
   1488  1.28      kent 		break;
   1489  1.28      kent 	default:
   1490  1.28      kent 		printf("%s: Unknown register: 0x%x\n", __func__, target);
   1491  1.28      kent 		return EINVAL;
   1492  1.28      kent 	}
   1493  1.28      kent 
   1494  1.28      kent 	ac97_read(as, AC97_REG_POWER, &power);
   1495  1.28      kent 	ac97_write(as, AC97_REG_POWER, power | power_bit);
   1496  1.28      kent 
   1497  1.28      kent 	ac97_write(as, target, (u_int16_t)value);
   1498  1.28      kent 	ac97_read(as, target, &actual);
   1499  1.28      kent 	actual = (u_int32_t)actual * as->ac97_clock / AC97_STANDARD_CLOCK;
   1500  1.28      kent 
   1501  1.28      kent 	ac97_write(as, AC97_REG_POWER, power);
   1502  1.28      kent 	if (ext_stat & AC97_EXT_AUDIO_DRA) {
   1503  1.28      kent 		*rate = actual * 2;
   1504  1.28      kent 	} else {
   1505  1.28      kent 		*rate = actual;
   1506  1.28      kent 	}
   1507  1.28      kent 	return 0;
   1508  1.28      kent }
   1509  1.28      kent 
   1510  1.60   thorpej static void
   1511  1.28      kent ac97_set_clock(struct ac97_codec_if *codec_if, unsigned int clock)
   1512  1.28      kent {
   1513  1.28      kent 	struct ac97_softc *as;
   1514  1.28      kent 
   1515  1.28      kent 	as = (struct ac97_softc *)codec_if;
   1516  1.28      kent 	as->ac97_clock = clock;
   1517  1.28      kent }
   1518  1.28      kent 
   1519  1.60   thorpej static u_int16_t
   1520  1.28      kent ac97_get_extcaps(struct ac97_codec_if *codec_if)
   1521  1.28      kent {
   1522  1.28      kent 	struct ac97_softc *as;
   1523  1.28      kent 
   1524  1.28      kent 	as = (struct ac97_softc *)codec_if;
   1525  1.28      kent 	return as->ext_id;
   1526  1.33      kent }
   1527  1.33      kent 
   1528  1.60   thorpej static int
   1529  1.33      kent ac97_add_port(struct ac97_softc *as, const struct ac97_source_info *src)
   1530  1.33      kent {
   1531  1.33      kent 	struct ac97_source_info *si;
   1532  1.33      kent 	int ouridx, idx;
   1533  1.33      kent 
   1534  1.33      kent 	if (as->num_source_info >= MAX_SOURCES) {
   1535  1.33      kent 		printf("%s: internal error: increase MAX_SOURCES in %s\n",
   1536  1.33      kent 		       __func__, __FILE__);
   1537  1.33      kent 		return -1;
   1538  1.33      kent 	}
   1539  1.33      kent 	if (!ac97_check_capability(as, src->req_feature))
   1540  1.33      kent 		return -1;
   1541  1.33      kent 	ouridx = as->num_source_info;
   1542  1.33      kent 	si = &as->source_info[ouridx];
   1543  1.33      kent 	memcpy(si, src, sizeof(*si));
   1544  1.33      kent 
   1545  1.33      kent 	switch (si->type) {
   1546  1.33      kent 	case AUDIO_MIXER_CLASS:
   1547  1.33      kent 	case AUDIO_MIXER_VALUE:
   1548  1.33      kent 		printf("%s: adding class/value is not supported yet.\n",
   1549  1.33      kent 		       __func__);
   1550  1.33      kent 		return -1;
   1551  1.33      kent 	case AUDIO_MIXER_ENUM:
   1552  1.33      kent 		break;
   1553  1.33      kent 	default:
   1554  1.33      kent 		printf("%s: unknown type: %d\n", __func__, si->type);
   1555  1.33      kent 		return -1;
   1556  1.33      kent 	}
   1557  1.33      kent 	as->num_source_info++;
   1558  1.33      kent 
   1559  1.33      kent 	si->mixer_class = ac97_get_portnum_by_name(&as->codec_if, si->class,
   1560  1.33      kent 						   NULL, NULL);
   1561  1.33      kent 	/* Find the root of the device */
   1562  1.33      kent 	idx = ac97_get_portnum_by_name(&as->codec_if, si->class,
   1563  1.33      kent 				       si->device, NULL);
   1564  1.33      kent 	/* Find the last item */
   1565  1.33      kent 	while (as->source_info[idx].next != AUDIO_MIXER_LAST)
   1566  1.33      kent 		idx = as->source_info[idx].next;
   1567  1.33      kent 	/* Append */
   1568  1.33      kent 	as->source_info[idx].next = ouridx;
   1569  1.33      kent 	si->prev = idx;
   1570  1.33      kent 	si->next = AUDIO_MIXER_LAST;
   1571  1.33      kent 
   1572  1.33      kent 	return 0;
   1573  1.43      kent }
   1574  1.43      kent 
   1575  1.43      kent /**
   1576  1.43      kent  * Codec-dependent initialization
   1577  1.43      kent  */
   1578  1.43      kent 
   1579  1.46      kent #define	AD1980_REG_MISC	0x76
   1580  1.55      kent #define		AD1980_MISC_MBG0	0x0001	/* 0 1888/1980/1981 /1985 */
   1581  1.55      kent #define		AD1980_MISC_MBG1	0x0002	/* 1 1888/1980/1981 /1985 */
   1582  1.55      kent #define		AD1980_MISC_VREFD	0x0004	/* 2 1888/1980/1981 /1985 */
   1583  1.55      kent #define		AD1980_MISC_VREFH	0x0008	/* 3 1888/1980/1981 /1985 */
   1584  1.55      kent #define		AD1980_MISC_SRU		0x0010	/* 4 1888/1980      /1985 */
   1585  1.55      kent #define		AD1980_MISC_LOSEL	0x0020	/* 5 1888/1980/1981 /1985 */
   1586  1.55      kent #define		AD1980_MISC_2CMIC	0x0040	/* 6      1980/1981B/1985 */
   1587  1.55      kent #define		AD1980_MISC_SPRD	0x0080	/* 7 1888/1980      /1985 */
   1588  1.55      kent #define		AD1980_MISC_DMIX0	0x0100	/* 8 1888/1980      /1985 */
   1589  1.55      kent #define		AD1980_MISC_DMIX1	0x0200	/* 9 1888/1980      /1985 */
   1590  1.55      kent #define		AD1980_MISC_HPSEL	0x0400	/*10 1888/1980      /1985 */
   1591  1.55      kent #define		AD1980_MISC_CLDIS	0x0800	/*11 1888/1980      /1985 */
   1592  1.55      kent #define		AD1980_MISC_LODIS	0x1000	/*12 1888/1980/1981 /1985 */
   1593  1.55      kent #define		AD1980_MISC_MSPLT	0x2000	/*13 1888/1980/1981 /1985 */
   1594  1.55      kent #define		AD1980_MISC_AC97NC	0x4000	/*14 1888/1980      /1985 */
   1595  1.55      kent #define		AD1980_MISC_DACZ	0x8000	/*15 1888/1980/1981 /1985 */
   1596  1.46      kent #define	AD1981_REG_MISC	0x76
   1597  1.55      kent #define		AD1981_MISC_MADST	0x0010  /* 4 */
   1598  1.55      kent #define		AD1981A_MISC_MADPD	0x0040  /* 6 */
   1599  1.55      kent #define		AD1981B_MISC_MADPD	0x0080  /* 7 */
   1600  1.55      kent #define		AD1981_MISC_FMXE	0x0200  /* 9 */
   1601  1.55      kent #define		AD1981_MISC_DAM		0x0800  /*11 */
   1602  1.43      kent static void
   1603  1.51      kent ac97_ad198x_init(struct ac97_softc *as)
   1604  1.43      kent {
   1605  1.47      jmmv 	int i;
   1606  1.55      kent 	uint16_t misc;
   1607  1.43      kent 
   1608  1.46      kent 	ac97_read(as, AD1980_REG_MISC, &misc);
   1609  1.46      kent 	ac97_write(as, AD1980_REG_MISC,
   1610  1.46      kent 		   misc | AD1980_MISC_LOSEL | AD1980_MISC_HPSEL);
   1611  1.47      jmmv 
   1612  1.47      jmmv 	for (i = 0; i < as->num_source_info; i++) {
   1613  1.47      jmmv 		if (as->source_info[i].type != AUDIO_MIXER_VALUE)
   1614  1.47      jmmv 			continue;
   1615  1.47      jmmv 
   1616  1.47      jmmv 		if (as->source_info[i].reg == AC97_REG_MASTER_VOLUME)
   1617  1.47      jmmv 			as->source_info[i].reg = AC97_REG_SURR_MASTER;
   1618  1.47      jmmv 		else if (as->source_info[i].reg == AC97_REG_SURR_MASTER)
   1619  1.47      jmmv 			as->source_info[i].reg = AC97_REG_MASTER_VOLUME;
   1620  1.47      jmmv 	}
   1621  1.33      kent }
   1622  1.33      kent 
   1623  1.33      kent #define ALC650_REG_MULTI_CHANNEL_CONTROL	0x6a
   1624  1.33      kent #define		ALC650_MCC_SLOT_MODIFY_MASK		0xc000
   1625  1.35      kent #define		ALC650_MCC_FRONTDAC_FROM_SPDIFIN	0x2000 /* 13 */
   1626  1.35      kent #define		ALC650_MCC_SPDIFOUT_FROM_ADC		0x1000 /* 12 */
   1627  1.35      kent #define		ALC650_MCC_PCM_FROM_SPDIFIN		0x0800 /* 11 */
   1628  1.35      kent #define		ALC650_MCC_MIC_OR_CENTERLFE		0x0400 /* 10 */
   1629  1.35      kent #define		ALC650_MCC_LINEIN_OR_SURROUND		0x0200 /* 9 */
   1630  1.35      kent #define		ALC650_MCC_INDEPENDENT_MASTER_L		0x0080 /* 7 */
   1631  1.35      kent #define		ALC650_MCC_INDEPENDENT_MASTER_R		0x0040 /* 6 */
   1632  1.35      kent #define		ALC650_MCC_ANALOG_TO_CENTERLFE		0x0020 /* 5 */
   1633  1.35      kent #define		ALC650_MCC_ANALOG_TO_SURROUND		0x0010 /* 4 */
   1634  1.35      kent #define		ALC650_MCC_EXCHANGE_CENTERLFE		0x0008 /* 3 */
   1635  1.35      kent #define		ALC650_MCC_CENTERLFE_DOWNMIX		0x0004 /* 2 */
   1636  1.35      kent #define		ALC650_MCC_SURROUND_DOWNMIX		0x0002 /* 1 */
   1637  1.35      kent #define		ALC650_MCC_LINEOUT_TO_SURROUND		0x0001 /* 0 */
   1638  1.33      kent static void
   1639  1.33      kent ac97_alc650_init(struct ac97_softc *as)
   1640  1.33      kent {
   1641  1.35      kent 	static const struct ac97_source_info sources[6] = {
   1642  1.33      kent 		{ AudioCoutputs, AudioNsurround, "lineinjack",
   1643  1.33      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1644  1.33      kent 		  ALC650_REG_MULTI_CHANNEL_CONTROL,
   1645  1.33      kent 		  0x0000, 1, 9, 0, 0, CHECK_SURROUND },
   1646  1.35      kent 		{ AudioCoutputs, AudioNsurround, "mixtofront",
   1647  1.35      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1648  1.35      kent 		  ALC650_REG_MULTI_CHANNEL_CONTROL,
   1649  1.35      kent 		  0x0000, 1, 1, 0, 0, CHECK_SURROUND },
   1650  1.33      kent 		{ AudioCoutputs, AudioNcenter, "micjack",
   1651  1.33      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1652  1.33      kent 		  ALC650_REG_MULTI_CHANNEL_CONTROL,
   1653  1.33      kent 		  0x0000, 1, 10, 0, 0, CHECK_CENTER },
   1654  1.33      kent 		{ AudioCoutputs, AudioNlfe, "micjack",
   1655  1.33      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1656  1.33      kent 		  ALC650_REG_MULTI_CHANNEL_CONTROL,
   1657  1.35      kent 		  0x0000, 1, 10, 0, 0, CHECK_LFE },
   1658  1.35      kent 		{ AudioCoutputs, AudioNcenter, "mixtofront",
   1659  1.35      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1660  1.35      kent 		  ALC650_REG_MULTI_CHANNEL_CONTROL,
   1661  1.35      kent 		  0x0000, 1, 2, 0, 0, CHECK_CENTER },
   1662  1.35      kent 		{ AudioCoutputs, AudioNlfe, "mixtofront",
   1663  1.35      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1664  1.35      kent 		  ALC650_REG_MULTI_CHANNEL_CONTROL,
   1665  1.35      kent 		  0x0000, 1, 2, 0, 0, CHECK_LFE },
   1666  1.35      kent 	};
   1667  1.35      kent 
   1668  1.35      kent 	ac97_add_port(as, &sources[0]);
   1669  1.35      kent 	ac97_add_port(as, &sources[1]);
   1670  1.35      kent 	ac97_add_port(as, &sources[2]);
   1671  1.35      kent 	ac97_add_port(as, &sources[3]);
   1672  1.35      kent 	ac97_add_port(as, &sources[4]);
   1673  1.35      kent 	ac97_add_port(as, &sources[5]);
   1674  1.35      kent }
   1675  1.35      kent 
   1676  1.35      kent #define VT1616_REG_IO_CONTROL	0x5a
   1677  1.35      kent #define		VT1616_IC_LVL			(1 << 15)
   1678  1.35      kent #define		VT1616_IC_LFECENTER_TO_FRONT	(1 << 12)
   1679  1.35      kent #define		VT1616_IC_SURROUND_TO_FRONT	(1 << 11)
   1680  1.35      kent #define		VT1616_IC_BPDC			(1 << 10)
   1681  1.35      kent #define		VT1616_IC_DC			(1 << 9)
   1682  1.35      kent #define		VT1616_IC_IB_MASK		0x000c
   1683  1.35      kent static void
   1684  1.35      kent ac97_vt1616_init(struct ac97_softc *as)
   1685  1.35      kent {
   1686  1.35      kent 	static const struct ac97_source_info sources[3] = {
   1687  1.35      kent 		{ AudioCoutputs, AudioNsurround, "mixtofront",
   1688  1.35      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1689  1.35      kent 		  VT1616_REG_IO_CONTROL,
   1690  1.35      kent 		  0x0000, 1, 11, 0, 0, CHECK_SURROUND },
   1691  1.35      kent 		{ AudioCoutputs, AudioNcenter, "mixtofront",
   1692  1.35      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1693  1.35      kent 		  VT1616_REG_IO_CONTROL,
   1694  1.35      kent 		  0x0000, 1, 12, 0, 0, CHECK_CENTER },
   1695  1.35      kent 		{ AudioCoutputs, AudioNlfe, "mixtofront",
   1696  1.35      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1697  1.35      kent 		  VT1616_REG_IO_CONTROL,
   1698  1.35      kent 		  0x0000, 1, 12, 0, 0, CHECK_LFE },
   1699  1.35      kent 	};
   1700  1.33      kent 
   1701  1.33      kent 	ac97_add_port(as, &sources[0]);
   1702  1.33      kent 	ac97_add_port(as, &sources[1]);
   1703  1.33      kent 	ac97_add_port(as, &sources[2]);
   1704  1.28      kent }
   1705