Home | History | Annotate | Line # | Download | only in ic
ac97.c revision 1.33
      1  1.33      kent /*      $NetBSD: ac97.c,v 1.33 2002/10/14 08:48:15 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.33      kent __KERNEL_RCSID(0, "$NetBSD: ac97.c,v 1.33 2002/10/14 08:48:15 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.33      kent #define Ac97Ntone	"tone"
     81  1.33      kent #define Ac97Nphone	"phone"
     82  1.33      kent 
     83  1.18  jdolecek static const struct audio_mixer_enum ac97_on_off = { 2,
     84   1.1  augustss 					       { { { AudioNoff } , 0 },
     85  1.33      kent 						 { { AudioNon }  , 1 } }};
     86   1.1  augustss 
     87  1.18  jdolecek static const struct audio_mixer_enum ac97_mic_select = { 2,
     88  1.33      kent 					       { { { AudioNmicrophone "0" },
     89   1.1  augustss 						   0 },
     90  1.33      kent 						 { { AudioNmicrophone "1" },
     91   1.1  augustss 						   1 } }};
     92   1.1  augustss 
     93  1.18  jdolecek static const struct audio_mixer_enum ac97_mono_select = { 2,
     94   1.1  augustss 					       { { { AudioNmixerout },
     95   1.1  augustss 						   0 },
     96  1.33      kent 						 { { AudioNmicrophone },
     97   1.1  augustss 						   1 } }};
     98   1.1  augustss 
     99  1.18  jdolecek static const struct audio_mixer_enum ac97_source = { 8,
    100   1.1  augustss 					       { { { AudioNmicrophone } , 0 },
    101   1.1  augustss 						 { { AudioNcd }, 1 },
    102  1.33      kent 						 { { AudioNvideo }, 2 },
    103   1.1  augustss 						 { { AudioNaux }, 3 },
    104   1.1  augustss 						 { { AudioNline }, 4 },
    105   1.1  augustss 						 { { AudioNmixerout }, 5 },
    106   1.1  augustss 						 { { AudioNmixerout AudioNmono }, 6 },
    107  1.33      kent 						 { { Ac97Nphone }, 7 }}};
    108   1.1  augustss 
    109  1.19       erh /*
    110  1.19       erh  * Due to different values for each source that uses these structures,
    111  1.19       erh  * the ac97_query_devinfo function sets delta in mixer_devinfo_t using
    112  1.19       erh  * ac97_source_info.bits.
    113  1.19       erh  */
    114  1.18  jdolecek static const struct audio_mixer_value ac97_volume_stereo = { { AudioNvolume },
    115   1.1  augustss 						       2 };
    116   1.1  augustss 
    117  1.18  jdolecek static const struct audio_mixer_value ac97_volume_mono = { { AudioNvolume },
    118   1.1  augustss 						     1 };
    119   1.1  augustss 
    120   1.1  augustss #define WRAP(a)  &a, sizeof(a)
    121   1.1  augustss 
    122  1.18  jdolecek const struct ac97_source_info {
    123  1.18  jdolecek 	const char *class;
    124  1.18  jdolecek 	const char *device;
    125  1.18  jdolecek 	const char *qualifier;
    126  1.33      kent 
    127   1.1  augustss 	int  type;
    128  1.18  jdolecek 	const void *info;
    129   1.1  augustss 	int  info_size;
    130   1.1  augustss 
    131   1.1  augustss 	u_int8_t  reg;
    132  1.14   thorpej 	u_int16_t default_value;
    133   1.1  augustss 	u_int8_t  bits:3;
    134   1.1  augustss 	u_int8_t  ofs:4;
    135   1.1  augustss 	u_int8_t  mute:1;
    136   1.1  augustss 	u_int8_t  polarity:1;   /* Does 0 == MAX or MIN */
    137  1.33      kent 	enum {
    138  1.33      kent 		CHECK_NONE = 0,
    139  1.33      kent 		CHECK_SURROUND,
    140  1.33      kent 		CHECK_CENTER,
    141  1.33      kent 		CHECK_LFE,
    142  1.33      kent 		CHECK_HEADPHONES,
    143  1.33      kent 		CHECK_TONE,
    144  1.33      kent 		CHECK_MIC,
    145  1.33      kent 		CHECK_LOUDNESS,
    146  1.33      kent 		CHECK_3D
    147  1.33      kent 	} req_feature;
    148   1.1  augustss 
    149   1.1  augustss 	int  prev;
    150  1.33      kent 	int  next;
    151   1.1  augustss 	int  mixer_class;
    152   1.1  augustss } source_info[] = {
    153  1.33      kent 	{ AudioCinputs,		NULL,		NULL,
    154  1.33      kent 	  AUDIO_MIXER_CLASS, },
    155  1.33      kent 	{ AudioCoutputs,	NULL,		NULL,
    156  1.33      kent 	  AUDIO_MIXER_CLASS, },
    157  1.33      kent 	{ AudioCrecord,		NULL,		NULL,
    158  1.33      kent 	  AUDIO_MIXER_CLASS, },
    159   1.1  augustss 	/* Stereo master volume*/
    160  1.33      kent 	{ AudioCoutputs,	AudioNmaster,	NULL,
    161  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    162  1.14   thorpej 	  AC97_REG_MASTER_VOLUME, 0x8000, 5, 0, 1,
    163   1.1  augustss 	},
    164   1.1  augustss 	/* Mono volume */
    165  1.33      kent 	{ AudioCoutputs,	AudioNmono,	NULL,
    166  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    167  1.14   thorpej 	  AC97_REG_MASTER_VOLUME_MONO, 0x8000, 6, 0, 1,
    168   1.1  augustss 	},
    169  1.33      kent 	{ AudioCoutputs,	AudioNmono,	AudioNsource,
    170  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_mono_select),
    171  1.14   thorpej 	  AC97_REG_GP, 0x0000, 1, 9, 0,
    172   1.1  augustss 	},
    173   1.1  augustss 	/* Headphone volume */
    174  1.33      kent 	{ AudioCoutputs,	AudioNheadphone, NULL,
    175  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    176  1.33      kent 	  AC97_REG_HEADPHONE_VOLUME, 0x8000, 6, 0, 1, 0, CHECK_HEADPHONES
    177  1.33      kent 	},
    178  1.33      kent 	/* Surround volume - logic hard coded for mute */
    179  1.33      kent 	{ AudioCoutputs,	AudioNsurround,	NULL,
    180  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    181  1.33      kent 	  AC97_REG_SURR_MASTER, 0x8080, 5, 0, 1, 0, CHECK_SURROUND
    182  1.33      kent 	},
    183  1.33      kent 	/* Center volume*/
    184  1.33      kent 	{ AudioCoutputs,	AudioNcenter,	NULL,
    185  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    186  1.33      kent 	  AC97_REG_CENTER_LFE_MASTER, 0x8080, 5, 0, 0, 0, CHECK_CENTER
    187  1.33      kent 	},
    188  1.33      kent 	{ AudioCoutputs,	AudioNcenter,	AudioNmute,
    189  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    190  1.33      kent 	  AC97_REG_CENTER_LFE_MASTER, 0x8080, 1, 7, 0, 0, CHECK_CENTER
    191  1.33      kent 	},
    192  1.33      kent 	/* LFE volume*/
    193  1.33      kent 	{ AudioCoutputs,	AudioNlfe,	NULL,
    194  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    195  1.33      kent 	  AC97_REG_CENTER_LFE_MASTER, 0x8080, 5, 8, 0, 0, CHECK_LFE
    196  1.33      kent 	},
    197  1.33      kent 	{ AudioCoutputs,	AudioNlfe,	AudioNmute,
    198  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    199  1.33      kent 	  AC97_REG_CENTER_LFE_MASTER, 0x8080, 1, 15, 0, 0, CHECK_LFE
    200   1.1  augustss 	},
    201   1.1  augustss 	/* Tone */
    202  1.33      kent 	{ AudioCoutputs,	Ac97Ntone,	NULL,
    203  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    204  1.33      kent 	  AC97_REG_MASTER_TONE, 0x0f0f, 4, 0, 0, 0, CHECK_TONE
    205   1.1  augustss 	},
    206   1.1  augustss 	/* PC Beep Volume */
    207  1.33      kent 	{ AudioCinputs,		AudioNspeaker,	NULL,
    208  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    209  1.14   thorpej 	  AC97_REG_PCBEEP_VOLUME, 0x0000, 4, 1, 1,
    210   1.1  augustss 	},
    211  1.33      kent 
    212   1.1  augustss 	/* Phone */
    213  1.33      kent 	{ AudioCinputs,		Ac97Nphone,	NULL,
    214  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    215  1.14   thorpej 	  AC97_REG_PHONE_VOLUME, 0x8008, 5, 0, 1,
    216   1.1  augustss 	},
    217   1.1  augustss 	/* Mic Volume */
    218  1.33      kent 	{ AudioCinputs,		AudioNmicrophone, NULL,
    219  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    220  1.14   thorpej 	  AC97_REG_MIC_VOLUME, 0x8008, 5, 0, 1,
    221   1.1  augustss 	},
    222  1.33      kent 	{ AudioCinputs,		AudioNmicrophone, AudioNpreamp,
    223  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    224  1.14   thorpej 	  AC97_REG_MIC_VOLUME, 0x8008, 1, 6, 0,
    225   1.1  augustss 	},
    226  1.33      kent 	{ AudioCinputs,		AudioNmicrophone, AudioNsource,
    227  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_mic_select),
    228  1.14   thorpej 	  AC97_REG_GP, 0x0000, 1, 8, 0,
    229   1.1  augustss 	},
    230   1.1  augustss 	/* Line in Volume */
    231  1.33      kent 	{ AudioCinputs,		AudioNline,	NULL,
    232  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    233  1.14   thorpej 	  AC97_REG_LINEIN_VOLUME, 0x8808, 5, 0, 1,
    234   1.1  augustss 	},
    235   1.1  augustss 	/* CD Volume */
    236  1.33      kent 	{ AudioCinputs,		AudioNcd,	NULL,
    237  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    238  1.14   thorpej 	  AC97_REG_CD_VOLUME, 0x8808, 5, 0, 1,
    239   1.1  augustss 	},
    240   1.1  augustss 	/* Video Volume */
    241  1.33      kent 	{ AudioCinputs,		AudioNvideo,	NULL,
    242  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    243  1.14   thorpej 	  AC97_REG_VIDEO_VOLUME, 0x8808, 5, 0, 1,
    244   1.1  augustss 	},
    245   1.1  augustss 	/* AUX volume */
    246  1.33      kent 	{ AudioCinputs,		AudioNaux,	NULL,
    247  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    248  1.14   thorpej 	  AC97_REG_AUX_VOLUME, 0x8808, 5, 0, 1,
    249   1.1  augustss 	},
    250   1.1  augustss 	/* PCM out volume */
    251  1.33      kent 	{ AudioCinputs,		AudioNdac,	NULL,
    252  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    253  1.14   thorpej 	  AC97_REG_PCMOUT_VOLUME, 0x8808, 5, 0, 1,
    254   1.1  augustss 	},
    255   1.1  augustss 	/* Record Source - some logic for this is hard coded - see below */
    256  1.33      kent 	{ AudioCrecord,		AudioNsource,	NULL,
    257  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_source),
    258  1.14   thorpej 	  AC97_REG_RECORD_SELECT, 0x0000, 3, 0, 0,
    259   1.1  augustss 	},
    260   1.1  augustss 	/* Record Gain */
    261  1.33      kent 	{ AudioCrecord,		AudioNvolume,	NULL,
    262  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_stereo),
    263  1.14   thorpej 	  AC97_REG_RECORD_GAIN, 0x8000, 4, 0, 1,
    264   1.1  augustss 	},
    265   1.1  augustss 	/* Record Gain mic */
    266  1.33      kent 	{ AudioCrecord,		AudioNmicrophone, NULL,
    267  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    268  1.33      kent 	  AC97_REG_RECORD_GAIN_MIC, 0x8000, 4, 0, 1, 1, CHECK_MIC
    269   1.1  augustss 	},
    270   1.1  augustss 	/* */
    271  1.33      kent 	{ AudioCoutputs,	AudioNloudness,	NULL,
    272  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    273  1.33      kent 	  AC97_REG_GP, 0x0000, 1, 12, 0, 0, CHECK_LOUDNESS
    274  1.33      kent 	},
    275  1.33      kent 	{ AudioCoutputs,	AudioNspatial,	NULL,
    276  1.33      kent 	  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
    277  1.33      kent 	  AC97_REG_GP, 0x0000, 1, 13, 0, 1, CHECK_3D
    278  1.33      kent 	},
    279  1.33      kent 	{ AudioCoutputs,	AudioNspatial,	"center",
    280  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    281  1.33      kent 	  AC97_REG_3D_CONTROL, 0x0000, 4, 8, 0, 1, CHECK_3D
    282  1.33      kent 	},
    283  1.33      kent 	{ AudioCoutputs,	AudioNspatial,	"depth",
    284  1.33      kent 	  AUDIO_MIXER_VALUE, WRAP(ac97_volume_mono),
    285  1.33      kent 	  AC97_REG_3D_CONTROL, 0x0000, 4, 0, 0, 1, CHECK_3D
    286   1.1  augustss 	},
    287   1.1  augustss 
    288   1.1  augustss 	/* Missing features: Simulated Stereo, POP, Loopback mode */
    289   1.1  augustss } ;
    290   1.1  augustss 
    291   1.1  augustss #define SOURCE_INFO_SIZE (sizeof(source_info)/sizeof(source_info[0]))
    292   1.1  augustss 
    293   1.1  augustss /*
    294   1.1  augustss  * Check out http://developer.intel.com/pc-supp/platform/ac97/ for
    295   1.1  augustss  * information on AC-97
    296   1.1  augustss  */
    297   1.1  augustss 
    298   1.1  augustss struct ac97_softc {
    299  1.28      kent 	/* ac97_codec_if must be at the first of ac97_softc. */
    300  1.14   thorpej 	struct ac97_codec_if codec_if;
    301   1.1  augustss 
    302  1.14   thorpej 	struct ac97_host_if *host_if;
    303   1.1  augustss 
    304  1.33      kent #define MAX_SOURCES	(2 * SOURCE_INFO_SIZE)
    305  1.33      kent 	struct ac97_source_info source_info[MAX_SOURCES];
    306   1.1  augustss 	int num_source_info;
    307  1.14   thorpej 
    308  1.14   thorpej 	enum ac97_host_flags host_flags;
    309  1.28      kent 	unsigned int ac97_clock; /* usually 48000 */
    310  1.28      kent #define AC97_STANDARD_CLOCK	48000U
    311  1.28      kent 	u_int16_t caps;		/* -> AC97_REG_RESET */
    312  1.28      kent 	u_int16_t ext_id;	/* -> AC97_REG_EXT_AUDIO_ID */
    313  1.14   thorpej 	u_int16_t shadow_reg[128];
    314   1.1  augustss };
    315   1.1  augustss 
    316   1.1  augustss int ac97_mixer_get_port __P((struct ac97_codec_if *self, mixer_ctrl_t *cp));
    317   1.1  augustss int ac97_mixer_set_port __P((struct ac97_codec_if *self, mixer_ctrl_t *));
    318   1.1  augustss int ac97_query_devinfo __P((struct ac97_codec_if *self, mixer_devinfo_t *));
    319  1.33      kent int ac97_get_portnum_by_name __P((struct ac97_codec_if *, const char *,
    320  1.33      kent 				  const char *, const char *));
    321  1.14   thorpej void ac97_restore_shadow __P((struct ac97_codec_if *self));
    322  1.28      kent int ac97_set_rate(struct ac97_codec_if *codec_if, int target, u_long *rate);
    323  1.28      kent void ac97_set_clock(struct ac97_codec_if *codec_if, unsigned int clock);
    324  1.28      kent u_int16_t ac97_get_extcaps(struct ac97_codec_if *codec_if);
    325  1.33      kent int ac97_add_port(struct ac97_softc *as, const struct ac97_source_info *src);
    326  1.33      kent 
    327  1.33      kent static void ac97_alc650_init(struct ac97_softc *);
    328   1.1  augustss 
    329   1.1  augustss struct ac97_codec_if_vtbl ac97civ = {
    330  1.28      kent 	ac97_mixer_get_port,
    331   1.1  augustss 	ac97_mixer_set_port,
    332   1.1  augustss 	ac97_query_devinfo,
    333  1.14   thorpej 	ac97_get_portnum_by_name,
    334  1.14   thorpej 	ac97_restore_shadow,
    335  1.28      kent 	ac97_get_extcaps,
    336  1.28      kent 	ac97_set_rate,
    337  1.28      kent 	ac97_set_clock,
    338   1.1  augustss };
    339   1.7   thorpej 
    340   1.7   thorpej static const struct ac97_codecid {
    341   1.1  augustss 	u_int32_t id;
    342  1.31      kent 	u_int32_t mask;
    343   1.7   thorpej 	const char *name;
    344  1.33      kent 	void (*init)(struct ac97_softc *);
    345   1.1  augustss } ac97codecid[] = {
    346  1.31      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 3),
    347  1.31      kent 	  0xffffffff,			"Analog Devices AD1819B" },
    348  1.31      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 64),
    349  1.31      kent 	  0xffffffff,			"Analog Devices AD1881" },
    350  1.31      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 72),
    351  1.31      kent 	  0xffffffff,			"Analog Devices AD1881A" },
    352  1.31      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 96),
    353  1.31      kent 	  0xffffffff,			"Analog Devices AD1885" },
    354  1.31      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 99),
    355  1.31      kent 	  0xffffffff,			"Analog Devices AD1886A" },
    356  1.33      kent 	{ AC97_CODEC_ID('A', 'D', 'S', 0),
    357  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Analog Devices unknown" },
    358  1.31      kent 
    359  1.33      kent 	/*
    360  1.33      kent 	 * Datasheets:
    361  1.33      kent 	 *	http://www.asahi-kasei.co.jp/akm/usa/product/ak4541/ek4541.pdf
    362  1.33      kent 	 *	http://www.asahi-kasei.co.jp/akm/usa/product/ak4543/ek4543.pdf
    363  1.33      kent 	 *	http://www.asahi-kasei.co.jp/akm/usa/product/ak4544a/ek4544a.pdf
    364  1.33      kent 	 *	http://www.asahi-kasei.co.jp/akm/usa/product/ak4545/ek4545.pdf
    365  1.33      kent 	 */
    366  1.31      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 0),
    367  1.31      kent 	  0xffffffff,			"Asahi Kasei AK4540"	},
    368  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 1),
    369  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4542"	},
    370  1.31      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 2),
    371  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4541/AK4543" },
    372  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 5),
    373  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4544" },
    374  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 6),
    375  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4544A" },
    376  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 7),
    377  1.33      kent 	  0xffffffff,			"Asahi Kasei AK4545" },
    378  1.33      kent 	{ AC97_CODEC_ID('A', 'K', 'M', 0),
    379  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Asahi Kasei unknown" },
    380  1.31      kent 
    381  1.33      kent 	/*
    382  1.33      kent 	 * Realtek & Avance Logic
    383  1.33      kent 	 *	http://www.realtek.com.tw/downloads/downloads1-3.aspx?lineid=5&famid=All&series=All&Spec=True
    384  1.33      kent 	 */
    385  1.33      kent 	{ AC97_CODEC_ID('A', 'L', 'C', 0x20),
    386  1.33      kent 	  0xffffffff,			"Realtek RL5383/RL5522/ALC100"	},
    387  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x10),
    388  1.33      kent 	  0xffffffff,			"Avance Logic ALC200/ALC201"	},
    389  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x20),
    390  1.33      kent 	  0xffffffff,			"Avance Logic ALC650", ac97_alc650_init },
    391  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x30),
    392  1.33      kent 	  0xffffffff,			"Avance Logic ALC101"	},
    393  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x40),
    394  1.33      kent 	  0xffffffff,			"Avance Logic ALC202"	},
    395  1.31      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0x50),
    396  1.33      kent 	  0xffffffff,			"Avance Logic ALC250"	},
    397  1.33      kent 	{ AC97_CODEC_ID('A', 'L', 'C', 0),
    398  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Realtek unknown"	},
    399  1.33      kent 	{ AC97_CODEC_ID('A', 'L', 'G', 0),
    400  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Avance Logic unknown"	},
    401  1.31      kent 
    402  1.31      kent 	/* Cirrus Logic, Crystal series:
    403  1.31      kent 	 *  'C' 'R' 'Y' 0x0[0-7]  - CS4297
    404  1.31      kent 	 *              0x1[0-7]  - CS4297A
    405  1.31      kent 	 *              0x2[0-7]  - CS4298
    406  1.31      kent 	 *              0x2[8-f]  - CS4294
    407  1.31      kent 	 *              0x3[0-7]  - CS4299
    408  1.31      kent 	 *              0x4[8-f]  - CS4201
    409  1.31      kent 	 *              0x5[8-f]  - CS4205
    410  1.31      kent 	 *              0x6[0-7]  - CS4291
    411  1.31      kent 	 *              0x7[0-7]  - CS4202
    412  1.31      kent 	 * Datasheets:
    413  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4297A-5.pdf?DocumentID=593
    414  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4294.pdf?DocumentID=32
    415  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4299-5.pdf?DocumentID=594
    416  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4201-2.pdf?DocumentID=492
    417  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4205-2.pdf?DocumentID=492
    418  1.31      kent 	 *	http://www.cirrus.com/pubs/cs4202-1.pdf?DocumentID=852
    419  1.31      kent 	 */
    420  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x00),
    421  1.31      kent 	  0xfffffff8,			"Crystal CS4297",	},
    422  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x10),
    423  1.31      kent 	  0xfffffff8,			"Crystal CS4297A",	},
    424  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x20),
    425  1.31      kent 	  0xfffffff8,			"Crystal CS4298",	},
    426  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x28),
    427  1.31      kent 	  0xfffffff8,			"Crystal CS4294",	},
    428  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x30),
    429  1.31      kent 	  0xfffffff8,			"Crystal CS4299",	},
    430  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x48),
    431  1.31      kent 	  0xfffffff8,			"Crystal CS4201",	},
    432  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x58),
    433  1.31      kent 	  0xfffffff8,			"Crystal CS4205",	},
    434  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x60),
    435  1.31      kent 	  0xfffffff8,			"Crystal CS4291",	},
    436  1.31      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0x70),
    437  1.31      kent 	  0xfffffff8,			"Crystal CS4202",	},
    438  1.33      kent 	{ AC97_CODEC_ID('C', 'R', 'Y', 0),
    439  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Cirrus Logic unknown",	},
    440  1.33      kent 
    441  1.33      kent 	{ 0x45838308, 0xffffffff,	"ESS Technology ES1921", },
    442  1.33      kent 	{ 0x45838300, AC97_VENDOR_ID_MASK, "ESS Technology unknown", },
    443  1.31      kent 
    444  1.31      kent 	{ AC97_CODEC_ID('N', 'S', 'C', 49),
    445  1.31      kent 	  0xffffffff,			"National Semiconductor LM4549", },
    446  1.33      kent 	{ AC97_CODEC_ID('N', 'S', 'C', 0),
    447  1.33      kent 	  AC97_VENDOR_ID_MASK,		"National Semiconductor unknown", },
    448  1.33      kent 
    449  1.31      kent 	{ AC97_CODEC_ID('S', 'I', 'L', 34),
    450  1.31      kent 	  0xffffffff,			"Silicon Laboratory Si3036", },
    451  1.31      kent 	{ AC97_CODEC_ID('S', 'I', 'L', 35),
    452  1.31      kent 	  0xffffffff,			"Silicon Laboratory Si3038", },
    453  1.33      kent 	{ AC97_CODEC_ID('S', 'I', 'L', 0),
    454  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Silicon Laboratory unknown", },
    455  1.33      kent 
    456  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 2),
    457  1.31      kent 	  0xffffffff,			"TriTech TR28022",	},
    458  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 3),
    459  1.31      kent 	  0xffffffff,			"TriTech TR28023",	},
    460  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 6),
    461  1.31      kent 	  0xffffffff,			"TriTech TR28026",	},
    462  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 8),
    463  1.31      kent 	  0xffffffff,			"TriTech TR28028",	},
    464  1.31      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 35),
    465  1.31      kent 	  0xffffffff,			"TriTech TR28602",	},
    466  1.33      kent 	{ AC97_CODEC_ID('T', 'R', 'A', 0),
    467  1.33      kent 	  AC97_VENDOR_ID_MASK,		"TriTech unknown",	},
    468  1.33      kent 
    469  1.33      kent 	/*
    470  1.33      kent 	 * http://www.wolfsonmicro.com/product_list.asp?cid=64
    471  1.33      kent 	 */
    472  1.31      kent 	{ AC97_CODEC_ID('W', 'M', 'L', 0),
    473  1.31      kent 	  0xffffffff,			"Wolfson WM9704",	},
    474  1.31      kent 	{ AC97_CODEC_ID('W', 'M', 'L', 3),
    475  1.31      kent 	  0xffffffff,			"Wolfson WM9707",	},
    476  1.33      kent 	{ AC97_CODEC_ID('W', 'M', 'L', 0),
    477  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Wolfson unknown",	},
    478  1.33      kent 
    479  1.33      kent 	/*
    480  1.33      kent 	 * http://www.yamaha.co.jp/english/product/lsi/us/products/pcaudio.html
    481  1.33      kent 	 * Datasheets:
    482  1.33      kent 	 *	http://www.yamaha.co.jp/english/product/lsi/us/products/pdf/4MF743A20.pdf
    483  1.33      kent 	 *	http://www.yamaha.co.jp/english/product/lsi/us/products/pdf/4MF753A20.pdf
    484  1.33      kent 	 */
    485  1.31      kent 	{ AC97_CODEC_ID('Y', 'M', 'H', 0),
    486  1.31      kent 	  0xffffffff,			"Yamaha YMF743-S",	},
    487  1.31      kent 	{ AC97_CODEC_ID('Y', 'M', 'H', 3),
    488  1.31      kent 	  0xffffffff,			"Yamaha YMF753-S",	},
    489  1.33      kent 	{ AC97_CODEC_ID('Y', 'M', 'H', 0),
    490  1.33      kent 	  AC97_VENDOR_ID_MASK,		"Yamaha unknown",	},
    491  1.33      kent 
    492  1.33      kent 	/*
    493  1.33      kent 	 * http://www.sigmatel.com/products.htm
    494  1.33      kent 	 */
    495  1.31      kent 	{ 0x83847600, 0xffffffff,	"SigmaTel STAC9700",	},
    496  1.31      kent 	{ 0x83847604, 0xffffffff,	"SigmaTel STAC9701/3/4/5", },
    497  1.31      kent 	{ 0x83847605, 0xffffffff,	"SigmaTel STAC9704",	},
    498  1.31      kent 	{ 0x83847608, 0xffffffff,	"SigmaTel STAC9708",	},
    499  1.31      kent 	{ 0x83847609, 0xffffffff,	"SigmaTel STAC9721/23",	},
    500  1.31      kent 	{ 0x83847644, 0xffffffff,	"SigmaTel STAC9744/45",	},
    501  1.31      kent 	{ 0x83847684, 0xffffffff,	"SigmaTel STAC9783/84",	},
    502  1.33      kent 	{ 0x83847600, AC97_VENDOR_ID_MASK, "SigmaTel unknown",	},
    503  1.33      kent 
    504  1.31      kent 	{ 0,
    505  1.31      kent 	  0,			NULL,			}
    506   1.1  augustss };
    507   1.1  augustss 
    508  1.18  jdolecek static const char * const ac97enhancement[] = {
    509   1.2     soren 	"no 3D stereo",
    510   1.1  augustss 	"Analog Devices Phat Stereo",
    511  1.24    ichiro 	"Creative",
    512   1.1  augustss 	"National Semi 3D",
    513   1.1  augustss 	"Yamaha Ymersion",
    514   1.1  augustss 	"BBE 3D",
    515  1.24    ichiro 	"Crystal Semi 3D",
    516   1.1  augustss 	"Qsound QXpander",
    517   1.1  augustss 	"Spatializer 3D",
    518   1.1  augustss 	"SRS 3D",
    519   1.1  augustss 	"Platform Tech 3D",
    520   1.1  augustss 	"AKM 3D",
    521   1.1  augustss 	"Aureal",
    522   1.1  augustss 	"AZTECH 3D",
    523   1.1  augustss 	"Binaura 3D",
    524   1.1  augustss 	"ESS Technology",
    525   1.1  augustss 	"Harman International VMAx",
    526   1.1  augustss 	"Nvidea 3D",
    527   1.1  augustss 	"Philips Incredible Sound",
    528   1.1  augustss 	"Texas Instruments' 3D",
    529   1.1  augustss 	"VLSI Technology 3D",
    530   1.1  augustss 	"TriTech 3D",
    531   1.1  augustss 	"Realtek 3D",
    532   1.1  augustss 	"Samsung 3D",
    533   1.1  augustss 	"Wolfson Microelectronics 3D",
    534   1.1  augustss 	"Delta Integration 3D",
    535   1.1  augustss 	"SigmaTel 3D",
    536  1.33      kent 	"KS Waves 3D",
    537   1.1  augustss 	"Rockwell 3D",
    538   1.1  augustss 	"Unknown 3D",
    539   1.1  augustss 	"Unknown 3D",
    540   1.1  augustss 	"Unknown 3D",
    541   1.1  augustss };
    542   1.1  augustss 
    543  1.18  jdolecek static const char * const ac97feature[] = {
    544  1.27      kent 	"dedicated mic channel",
    545   1.1  augustss 	"reserved",
    546   1.1  augustss 	"tone",
    547   1.1  augustss 	"simulated stereo",
    548   1.1  augustss 	"headphone",
    549   1.1  augustss 	"bass boost",
    550   1.1  augustss 	"18 bit DAC",
    551   1.1  augustss 	"20 bit DAC",
    552   1.1  augustss 	"18 bit ADC",
    553   1.1  augustss 	"20 bit ADC"
    554   1.1  augustss };
    555   1.1  augustss 
    556   1.1  augustss 
    557  1.18  jdolecek int ac97_str_equal __P((const char *, const char *));
    558  1.33      kent int ac97_check_capability(struct ac97_softc *, int);
    559   1.1  augustss void ac97_setup_source_info __P((struct ac97_softc *));
    560  1.14   thorpej void ac97_read __P((struct ac97_softc *, u_int8_t, u_int16_t *));
    561  1.14   thorpej void ac97_setup_defaults __P((struct ac97_softc *));
    562  1.14   thorpej int ac97_write __P((struct ac97_softc *, u_int8_t, u_int16_t));
    563   1.1  augustss 
    564   1.1  augustss /* #define AC97_DEBUG 10 */
    565   1.1  augustss 
    566   1.1  augustss #ifdef AUDIO_DEBUG
    567   1.1  augustss #define DPRINTF(x)	if (ac97debug) printf x
    568   1.1  augustss #define DPRINTFN(n,x)	if (ac97debug>(n)) printf x
    569   1.1  augustss #ifdef AC97_DEBUG
    570   1.1  augustss int	ac97debug = AC97_DEBUG;
    571   1.1  augustss #else
    572   1.1  augustss int	ac97debug = 0;
    573   1.1  augustss #endif
    574   1.1  augustss #else
    575   1.1  augustss #define DPRINTF(x)
    576   1.1  augustss #define DPRINTFN(n,x)
    577   1.1  augustss #endif
    578   1.1  augustss 
    579  1.14   thorpej void
    580  1.14   thorpej ac97_read(as, reg, val)
    581  1.14   thorpej 	struct ac97_softc *as;
    582  1.14   thorpej 	u_int8_t reg;
    583  1.14   thorpej 	u_int16_t *val;
    584  1.14   thorpej {
    585  1.14   thorpej 	int error;
    586  1.14   thorpej 
    587  1.14   thorpej 	if (as->host_flags & AC97_HOST_DONT_READ &&
    588  1.14   thorpej 	    (reg != AC97_REG_VENDOR_ID1 && reg != AC97_REG_VENDOR_ID2 &&
    589  1.14   thorpej 	     reg != AC97_REG_RESET)) {
    590  1.14   thorpej 		*val = as->shadow_reg[reg >> 1];
    591  1.14   thorpej 		return;
    592  1.14   thorpej 	}
    593  1.14   thorpej 
    594  1.14   thorpej 	if ((error = as->host_if->read(as->host_if->arg, reg, val))) {
    595  1.14   thorpej 		*val = as->shadow_reg[reg >> 1];
    596  1.14   thorpej 	}
    597  1.14   thorpej }
    598  1.14   thorpej 
    599  1.14   thorpej int
    600  1.14   thorpej ac97_write(as, reg, val)
    601  1.14   thorpej 	struct ac97_softc *as;
    602  1.14   thorpej 	u_int8_t reg;
    603  1.14   thorpej 	u_int16_t val;
    604  1.14   thorpej {
    605  1.14   thorpej 
    606  1.14   thorpej 	as->shadow_reg[reg >> 1] = val;
    607  1.14   thorpej 
    608  1.14   thorpej 	return (as->host_if->write(as->host_if->arg, reg, val));
    609  1.14   thorpej }
    610  1.14   thorpej 
    611  1.14   thorpej void
    612  1.14   thorpej ac97_setup_defaults(as)
    613  1.14   thorpej 	struct ac97_softc *as;
    614  1.14   thorpej {
    615  1.14   thorpej 	int idx;
    616  1.18  jdolecek 	const struct ac97_source_info *si;
    617  1.14   thorpej 
    618  1.14   thorpej 	memset(as->shadow_reg, 0, sizeof(as->shadow_reg));
    619  1.14   thorpej 
    620  1.14   thorpej 	for (idx = 0; idx < SOURCE_INFO_SIZE; idx++) {
    621  1.14   thorpej 		si = &source_info[idx];
    622  1.14   thorpej 		ac97_write(as, si->reg, si->default_value);
    623  1.14   thorpej 	}
    624  1.14   thorpej }
    625  1.14   thorpej 
    626  1.14   thorpej void
    627  1.14   thorpej ac97_restore_shadow(self)
    628  1.14   thorpej 	struct ac97_codec_if *self;
    629  1.14   thorpej {
    630  1.14   thorpej 	struct ac97_softc *as = (struct ac97_softc *) self;
    631  1.14   thorpej 	int idx;
    632  1.18  jdolecek 	const struct ac97_source_info *si;
    633  1.14   thorpej 
    634  1.14   thorpej 	for (idx = 0; idx < SOURCE_INFO_SIZE; idx++) {
    635  1.14   thorpej 		si = &source_info[idx];
    636  1.14   thorpej 		ac97_write(as, si->reg, as->shadow_reg[si->reg >> 1]);
    637  1.14   thorpej 	}
    638  1.14   thorpej }
    639   1.1  augustss 
    640   1.1  augustss int
    641   1.1  augustss ac97_str_equal(a, b)
    642  1.18  jdolecek 	const char *a, *b;
    643   1.1  augustss {
    644   1.1  augustss 	return ((a == b) || (a && b && (!strcmp(a, b))));
    645   1.1  augustss }
    646   1.1  augustss 
    647  1.33      kent int
    648  1.33      kent ac97_check_capability(struct ac97_softc *as, int check)
    649  1.33      kent {
    650  1.33      kent 	switch (check) {
    651  1.33      kent 	case CHECK_NONE:
    652  1.33      kent 		return 1;
    653  1.33      kent 	case CHECK_SURROUND:
    654  1.33      kent 		return as->ext_id & AC97_EXT_AUDIO_SDAC;
    655  1.33      kent 	case CHECK_CENTER:
    656  1.33      kent 		return as->ext_id & AC97_EXT_AUDIO_CDAC;
    657  1.33      kent 	case CHECK_LFE:
    658  1.33      kent 		return as->ext_id & AC97_EXT_AUDIO_LDAC;
    659  1.33      kent 	case CHECK_HEADPHONES:
    660  1.33      kent 		return as->caps & AC97_CAPS_HEADPHONES;
    661  1.33      kent 	case CHECK_TONE:
    662  1.33      kent 		return as->caps & AC97_CAPS_TONECTRL;
    663  1.33      kent 	case CHECK_MIC:
    664  1.33      kent 		return as->caps & AC97_CAPS_MICIN;
    665  1.33      kent 	case CHECK_LOUDNESS:
    666  1.33      kent 		return as->caps & AC97_CAPS_LOUDNESS;
    667  1.33      kent 	case CHECK_3D:
    668  1.33      kent 		return AC97_CAPS_ENHANCEMENT(as->caps) != 0;
    669  1.33      kent 	default:
    670  1.33      kent 		printf("%s: internal error: feature=%d\n", __func__, check);
    671  1.33      kent 		return 0;
    672  1.33      kent 	}
    673  1.33      kent }
    674  1.33      kent 
    675   1.1  augustss void
    676   1.1  augustss ac97_setup_source_info(as)
    677   1.1  augustss 	struct ac97_softc *as;
    678   1.1  augustss {
    679   1.1  augustss 	int idx, ouridx;
    680  1.33      kent 	struct ac97_source_info *si, *si2;
    681   1.1  augustss 
    682   1.1  augustss 	for (idx = 0, ouridx = 0; idx < SOURCE_INFO_SIZE; idx++) {
    683   1.1  augustss 		si = &as->source_info[ouridx];
    684   1.1  augustss 
    685  1.33      kent 		if (!ac97_check_capability(as, source_info[idx].req_feature))
    686  1.33      kent 			continue;
    687  1.33      kent 
    688  1.20   thorpej 		memcpy(si, &source_info[idx], sizeof(*si));
    689   1.1  augustss 
    690   1.1  augustss 		switch (si->type) {
    691   1.1  augustss 		case AUDIO_MIXER_CLASS:
    692   1.1  augustss 		        si->mixer_class = ouridx;
    693   1.1  augustss 			ouridx++;
    694   1.1  augustss 			break;
    695   1.1  augustss 		case AUDIO_MIXER_VALUE:
    696   1.1  augustss 			/* Todo - Test to see if it works */
    697   1.1  augustss 			ouridx++;
    698   1.1  augustss 
    699   1.1  augustss 			/* Add an entry for mute, if necessary */
    700   1.1  augustss 			if (si->mute) {
    701   1.1  augustss 				si = &as->source_info[ouridx];
    702  1.20   thorpej 				memcpy(si, &source_info[idx], sizeof(*si));
    703   1.1  augustss 				si->qualifier = AudioNmute;
    704   1.1  augustss 				si->type = AUDIO_MIXER_ENUM;
    705   1.1  augustss 				si->info = &ac97_on_off;
    706   1.1  augustss 				si->info_size = sizeof(ac97_on_off);
    707   1.1  augustss 				si->bits = 1;
    708   1.1  augustss 				si->ofs = 15;
    709   1.1  augustss 				si->mute = 0;
    710   1.1  augustss 				si->polarity = 0;
    711   1.1  augustss 				ouridx++;
    712   1.1  augustss 			}
    713   1.1  augustss 			break;
    714   1.1  augustss 		case AUDIO_MIXER_ENUM:
    715   1.1  augustss 			/* Todo - Test to see if it works */
    716   1.1  augustss 			ouridx++;
    717   1.1  augustss 			break;
    718   1.1  augustss 		default:
    719   1.1  augustss 			printf ("ac97: shouldn't get here\n");
    720   1.1  augustss 			break;
    721   1.1  augustss 		}
    722   1.1  augustss 	}
    723   1.1  augustss 
    724   1.1  augustss 	as->num_source_info = ouridx;
    725   1.1  augustss 
    726   1.1  augustss 	for (idx = 0; idx < as->num_source_info; idx++) {
    727   1.1  augustss 		int idx2, previdx;
    728   1.1  augustss 
    729   1.1  augustss 		si = &as->source_info[idx];
    730   1.1  augustss 
    731   1.1  augustss 		/* Find mixer class */
    732   1.1  augustss 		for (idx2 = 0; idx2 < as->num_source_info; idx2++) {
    733   1.1  augustss 			si2 = &as->source_info[idx2];
    734   1.1  augustss 
    735   1.1  augustss 			if (si2->type == AUDIO_MIXER_CLASS &&
    736   1.1  augustss 			    ac97_str_equal(si->class,
    737   1.1  augustss 					   si2->class)) {
    738   1.1  augustss 				si->mixer_class = idx2;
    739   1.1  augustss 			}
    740   1.1  augustss 		}
    741   1.1  augustss 
    742   1.1  augustss 
    743   1.1  augustss 		/* Setup prev and next pointers */
    744   1.1  augustss 		if (si->prev != 0)
    745   1.1  augustss 			continue;
    746   1.1  augustss 
    747   1.1  augustss 		if (si->qualifier)
    748   1.1  augustss 			continue;
    749   1.1  augustss 
    750   1.1  augustss 		si->prev = AUDIO_MIXER_LAST;
    751   1.1  augustss 		previdx = idx;
    752   1.1  augustss 
    753   1.1  augustss 		for (idx2 = 0; idx2 < as->num_source_info; idx2++) {
    754   1.1  augustss 			if (idx2 == idx)
    755   1.1  augustss 				continue;
    756   1.1  augustss 
    757   1.1  augustss 			si2 = &as->source_info[idx2];
    758   1.1  augustss 
    759   1.1  augustss 			if (!si2->prev &&
    760   1.1  augustss 			    ac97_str_equal(si->class, si2->class) &&
    761   1.1  augustss 			    ac97_str_equal(si->device, si2->device)) {
    762   1.1  augustss 				as->source_info[previdx].next = idx2;
    763   1.1  augustss 				as->source_info[idx2].prev = previdx;
    764   1.1  augustss 
    765   1.1  augustss 				previdx = idx2;
    766   1.1  augustss 			}
    767   1.1  augustss 		}
    768   1.1  augustss 
    769   1.1  augustss 		as->source_info[previdx].next = AUDIO_MIXER_LAST;
    770   1.1  augustss 	}
    771   1.1  augustss }
    772   1.1  augustss 
    773  1.28      kent int
    774  1.14   thorpej ac97_attach(host_if)
    775  1.14   thorpej 	struct ac97_host_if *host_if;
    776   1.1  augustss {
    777   1.1  augustss 	struct ac97_softc *as;
    778  1.14   thorpej 	struct device *sc_dev = (struct device *)host_if->arg;
    779   1.1  augustss 	int error, i, j;
    780   1.1  augustss 	u_int32_t id;
    781  1.28      kent 	u_int16_t id1, id2;
    782  1.28      kent 	u_int16_t extstat, rate;
    783   1.8  augustss 	mixer_ctrl_t ctl;
    784  1.27      kent 	const char *delim;
    785  1.33      kent 	void (*initfunc)(struct ac97_softc *);
    786  1.28      kent 
    787  1.33      kent 	initfunc = NULL;
    788  1.23   tsutsui 	as = malloc(sizeof(struct ac97_softc), M_DEVBUF, M_WAITOK|M_ZERO);
    789   1.1  augustss 
    790   1.2     soren 	if (as == NULL)
    791   1.2     soren 		return (ENOMEM);
    792  1.14   thorpej 
    793  1.14   thorpej 	as->codec_if.vtbl = &ac97civ;
    794  1.14   thorpej 	as->host_if = host_if;
    795   1.1  augustss 
    796  1.14   thorpej 	if ((error = host_if->attach(host_if->arg, &as->codec_if))) {
    797  1.33      kent 		free(as, M_DEVBUF);
    798   1.1  augustss 		return (error);
    799   1.1  augustss 	}
    800   1.1  augustss 
    801  1.14   thorpej 	host_if->reset(host_if->arg);
    802   1.1  augustss 
    803  1.14   thorpej 	host_if->write(host_if->arg, AC97_REG_POWER, 0);
    804  1.14   thorpej 	host_if->write(host_if->arg, AC97_REG_RESET, 0);
    805   1.1  augustss 
    806  1.14   thorpej 	if (host_if->flags)
    807  1.14   thorpej 		as->host_flags = host_if->flags(host_if->arg);
    808   1.1  augustss 
    809  1.14   thorpej 	ac97_setup_defaults(as);
    810  1.15   thorpej 	ac97_read(as, AC97_REG_VENDOR_ID1, &id1);
    811  1.15   thorpej 	ac97_read(as, AC97_REG_VENDOR_ID2, &id2);
    812  1.28      kent 	ac97_read(as, AC97_REG_RESET, &as->caps);
    813   1.1  augustss 
    814   1.1  augustss 	id = (id1 << 16) | id2;
    815   1.1  augustss 
    816   1.2     soren 	printf("%s: ", sc_dev->dv_xname);
    817   1.2     soren 
    818   1.2     soren 	for (i = 0; ; i++) {
    819   1.2     soren 		if (ac97codecid[i].id == 0) {
    820   1.4  augustss 			char pnp[4];
    821   1.7   thorpej 
    822   1.7   thorpej 			AC97_GET_CODEC_ID(id, pnp);
    823   1.4  augustss #define ISASCII(c) ((c) >= ' ' && (c) < 0x7f)
    824   1.4  augustss 			if (ISASCII(pnp[0]) && ISASCII(pnp[1]) &&
    825   1.4  augustss 			    ISASCII(pnp[2]))
    826   1.7   thorpej 				printf("%c%c%c%d", pnp[0], pnp[1], pnp[2],
    827   1.7   thorpej 				    pnp[3]);
    828   1.4  augustss 			else
    829  1.12     soren 				printf("unknown (0x%08x)", id);
    830  1.11     soren 			break;
    831  1.11     soren 		}
    832  1.31      kent 		if (ac97codecid[i].id == (id & ac97codecid[i].mask)) {
    833  1.11     soren 			printf("%s", ac97codecid[i].name);
    834  1.33      kent 			if (ac97codecid[i].mask == AC97_VENDOR_ID_MASK) {
    835  1.33      kent 				printf(" (0x%08x)", id);
    836  1.33      kent 			}
    837  1.33      kent 			initfunc = ac97codecid[i].init;
    838   1.2     soren 			break;
    839   1.2     soren 		}
    840   1.1  augustss 	}
    841   1.2     soren 	printf(" codec; ");
    842   1.1  augustss 	for (i = j = 0; i < 10; i++) {
    843  1.28      kent 		if (as->caps & (1 << i)) {
    844   1.1  augustss 			printf("%s%s", j? ", " : "", ac97feature[i]);
    845   1.1  augustss 			j++;
    846   1.1  augustss 		}
    847   1.1  augustss 	}
    848  1.28      kent 	printf("%s%s\n", j ? ", " : "",
    849  1.33      kent 	       ac97enhancement[AC97_CAPS_ENHANCEMENT(as->caps)]);
    850   1.1  augustss 
    851  1.28      kent 	as->ac97_clock = AC97_STANDARD_CLOCK;
    852  1.28      kent 	ac97_read(as, AC97_REG_EXT_AUDIO_ID, &as->ext_id);
    853  1.28      kent 	if (as->ext_id & (AC97_EXT_AUDIO_VRA | AC97_EXT_AUDIO_DRA
    854  1.28      kent 			  | AC97_EXT_AUDIO_SPDIF | AC97_EXT_AUDIO_VRM
    855  1.28      kent 			  | AC97_EXT_AUDIO_CDAC | AC97_EXT_AUDIO_SDAC
    856  1.28      kent 			  | AC97_EXT_AUDIO_LDAC)) {
    857  1.27      kent 		printf("%s:", sc_dev->dv_xname);
    858  1.27      kent 		delim = "";
    859  1.27      kent 
    860  1.33      kent 		ac97_read(as, AC97_REG_EXT_AUDIO_CTRL, &extstat);
    861  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_VRA) {
    862  1.27      kent 			printf("%s variable rate audio", delim);
    863  1.27      kent 			delim = ",";
    864  1.33      kent 			extstat |= AC97_EXT_AUDIO_VRA;
    865  1.27      kent 		}
    866  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_DRA) {
    867  1.27      kent 			printf("%s double rate output", delim);
    868  1.27      kent 			delim = ",";
    869  1.27      kent 		}
    870  1.33      kent 		extstat &= ~AC97_EXT_AUDIO_DRA;
    871  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_SPDIF) {
    872  1.27      kent 			printf("%s S/PDIF", delim);
    873  1.27      kent 			delim = ",";
    874  1.27      kent 		}
    875  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_VRM) {
    876  1.27      kent 			printf("%s variable rate dedicated mic", delim);
    877  1.27      kent 			delim = ",";
    878  1.33      kent 			extstat |= AC97_EXT_AUDIO_VRM;
    879  1.27      kent 		}
    880  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_CDAC) {
    881  1.27      kent 			printf("%s center DAC", delim);
    882  1.27      kent 			delim = ",";
    883  1.33      kent 			extstat |= AC97_EXT_AUDIO_CDAC;
    884  1.27      kent 		}
    885  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_SDAC) {
    886  1.27      kent 			printf("%s surround DAC", delim);
    887  1.27      kent 			delim = ",";
    888  1.33      kent 			extstat |= AC97_EXT_AUDIO_SDAC;
    889  1.27      kent 		}
    890  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_LDAC) {
    891  1.27      kent 			printf("%s LFE DAC", delim);
    892  1.33      kent 			extstat |= AC97_EXT_AUDIO_LDAC;
    893  1.27      kent 		}
    894  1.27      kent 		printf("\n");
    895  1.28      kent 
    896  1.33      kent 		ac97_write(as, AC97_REG_EXT_AUDIO_CTRL, extstat);
    897  1.33      kent 		if (as->ext_id & AC97_EXT_AUDIO_VRA) {
    898  1.33      kent 			/* VRA should be enabled. */
    899  1.28      kent 			/* so it claims to do variable rate, let's make sure */
    900  1.28      kent 			ac97_write(as, AC97_REG_PCM_FRONT_DAC_RATE, 44100);
    901  1.28      kent 			ac97_read(as, AC97_REG_PCM_FRONT_DAC_RATE, &rate);
    902  1.28      kent 			if (rate != 44100) {
    903  1.28      kent 				/* We can't believe ext_id */
    904  1.28      kent 				as->ext_id = 0;
    905  1.28      kent 				printf("%s: Ignore these capabilities.\n",
    906  1.28      kent 				       sc_dev->dv_xname);
    907  1.28      kent 			}
    908  1.32      kent 			/* restore the default value */
    909  1.32      kent 			ac97_write(as, AC97_REG_PCM_FRONT_DAC_RATE,
    910  1.32      kent 				   AC97_SINGLE_RATE);
    911  1.28      kent 		}
    912  1.27      kent 	}
    913   1.1  augustss 
    914   1.1  augustss 	ac97_setup_source_info(as);
    915   1.8  augustss 
    916  1.33      kent 	DELAY(900 * 1000);
    917   1.8  augustss 	memset(&ctl, 0, sizeof(ctl));
    918   1.8  augustss 
    919  1.33      kent 	/* disable mutes */
    920  1.33      kent 	for (i = 0; i < 11; i++) {
    921  1.33      kent 		static struct {
    922  1.33      kent 			char *class, *device;
    923  1.33      kent 		} d[11] = {
    924  1.33      kent 			{ AudioCoutputs, AudioNmaster},
    925  1.33      kent 			{ AudioCoutputs, AudioNheadphone},
    926  1.33      kent 			{ AudioCoutputs, AudioNsurround},
    927  1.33      kent 			{ AudioCoutputs, AudioNcenter},
    928  1.33      kent 			{ AudioCoutputs, AudioNlfe},
    929  1.33      kent 			{ AudioCinputs, AudioNdac},
    930  1.33      kent 			{ AudioCinputs, AudioNcd},
    931  1.33      kent 			{ AudioCinputs, AudioNline},
    932  1.33      kent 			{ AudioCinputs, AudioNaux},
    933  1.33      kent 			{ AudioCinputs, AudioNvideo},
    934  1.33      kent 			{ AudioCrecord, AudioNvolume},
    935  1.33      kent 		};
    936  1.33      kent 
    937  1.33      kent 		ctl.type = AUDIO_MIXER_ENUM;
    938  1.33      kent 		ctl.un.ord = 0;
    939  1.33      kent 
    940  1.33      kent 		ctl.dev = ac97_get_portnum_by_name(&as->codec_if,
    941  1.33      kent 			d[i].class, d[i].device, AudioNmute);
    942  1.33      kent 		ac97_mixer_set_port(&as->codec_if, &ctl);
    943  1.33      kent 	}
    944   1.8  augustss 	ctl.type = AUDIO_MIXER_ENUM;
    945  1.33      kent 	ctl.un.ord = 0;
    946  1.33      kent 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCrecord,
    947  1.33      kent 					   AudioNsource, NULL);
    948  1.33      kent 	ac97_mixer_set_port(&as->codec_if, &ctl);
    949  1.33      kent 
    950  1.33      kent 	/* set a reasonable default volume */
    951  1.33      kent 	ctl.type = AUDIO_MIXER_VALUE;
    952  1.33      kent 	ctl.un.value.num_channels = 2;
    953  1.33      kent 	ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = \
    954  1.33      kent 	ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 127;
    955  1.14   thorpej 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
    956  1.33      kent 					   AudioNmaster, NULL);
    957  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
    958  1.33      kent 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
    959  1.33      kent 					   AudioNsurround, NULL);
    960  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
    961  1.33      kent 	ctl.un.value.num_channels = 1;
    962  1.33      kent 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
    963  1.33      kent 					   AudioNcenter, NULL);
    964  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
    965  1.33      kent 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
    966  1.33      kent 					   AudioNlfe, NULL);
    967  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
    968   1.1  augustss 
    969  1.33      kent 	if (initfunc != NULL)
    970  1.33      kent 		initfunc(as);
    971   1.1  augustss 	return (0);
    972   1.1  augustss }
    973   1.1  augustss 
    974   1.1  augustss 
    975   1.1  augustss int
    976   1.1  augustss ac97_query_devinfo(codec_if, dip)
    977   1.1  augustss 	struct ac97_codec_if *codec_if;
    978   1.1  augustss 	mixer_devinfo_t *dip;
    979   1.1  augustss {
    980   1.1  augustss 	struct ac97_softc *as = (struct ac97_softc *)codec_if;
    981   1.1  augustss 
    982   1.1  augustss 	if (dip->index < as->num_source_info) {
    983   1.1  augustss 		struct ac97_source_info *si = &as->source_info[dip->index];
    984  1.18  jdolecek 		const char *name;
    985   1.1  augustss 
    986   1.1  augustss 		dip->type = si->type;
    987   1.1  augustss 		dip->mixer_class = si->mixer_class;
    988   1.1  augustss 		dip->prev = si->prev;
    989   1.1  augustss 		dip->next = si->next;
    990   1.1  augustss 
    991   1.1  augustss 		if (si->qualifier)
    992   1.1  augustss 			name = si->qualifier;
    993   1.1  augustss 		else if (si->device)
    994   1.1  augustss 			name = si->device;
    995   1.1  augustss 		else if (si->class)
    996   1.1  augustss 			name = si->class;
    997   1.6  augustss 		else
    998   1.6  augustss 			name = 0;
    999   1.1  augustss 
   1000   1.1  augustss 		if (name)
   1001   1.1  augustss 			strcpy(dip->label.name, name);
   1002   1.1  augustss 
   1003  1.20   thorpej 		memcpy(&dip->un, si->info, si->info_size);
   1004  1.19       erh 
   1005  1.19       erh 		/* Set the delta for volume sources */
   1006  1.19       erh 		if (dip->type == AUDIO_MIXER_VALUE)
   1007  1.19       erh 			dip->un.v.delta = 1 << (8 - si->bits);
   1008  1.19       erh 
   1009   1.1  augustss 		return (0);
   1010   1.1  augustss 	}
   1011   1.1  augustss 
   1012   1.1  augustss 	return (ENXIO);
   1013   1.1  augustss }
   1014   1.1  augustss 
   1015   1.1  augustss 
   1016   1.1  augustss 
   1017   1.1  augustss int
   1018   1.1  augustss ac97_mixer_set_port(codec_if, cp)
   1019   1.1  augustss 	struct ac97_codec_if *codec_if;
   1020   1.1  augustss 	mixer_ctrl_t *cp;
   1021   1.1  augustss {
   1022   1.1  augustss 	struct ac97_softc *as = (struct ac97_softc *)codec_if;
   1023   1.1  augustss 	struct ac97_source_info *si = &as->source_info[cp->dev];
   1024   1.1  augustss 	u_int16_t mask;
   1025   1.1  augustss 	u_int16_t val, newval;
   1026   1.1  augustss 	int error;
   1027   1.1  augustss 
   1028   1.1  augustss 	if (cp->dev < 0 || cp->dev >= as->num_source_info)
   1029   1.1  augustss 		return (EINVAL);
   1030   1.1  augustss 
   1031   1.1  augustss 	if (cp->type != si->type)
   1032   1.1  augustss 		return (EINVAL);
   1033   1.1  augustss 
   1034  1.15   thorpej 	ac97_read(as, si->reg, &val);
   1035   1.1  augustss 
   1036   1.1  augustss 	DPRINTFN(5, ("read(%x) = %x\n", si->reg, val));
   1037   1.1  augustss 
   1038   1.1  augustss 	mask = (1 << si->bits) - 1;
   1039   1.1  augustss 
   1040   1.1  augustss 	switch (cp->type) {
   1041   1.1  augustss 	case AUDIO_MIXER_ENUM:
   1042   1.1  augustss 		if (cp->un.ord > mask || cp->un.ord < 0)
   1043   1.1  augustss 			return (EINVAL);
   1044   1.1  augustss 
   1045   1.1  augustss 		newval = (cp->un.ord << si->ofs);
   1046   1.1  augustss 		if (si->reg == AC97_REG_RECORD_SELECT) {
   1047   1.1  augustss 			newval |= (newval << (8 + si->ofs));
   1048   1.1  augustss 			mask |= (mask << 8);
   1049  1.33      kent 			mask = mask << si->ofs;
   1050  1.33      kent 		} else if (si->reg == AC97_REG_SURR_MASTER) {
   1051  1.33      kent 			newval = cp->un.ord ? 0x8080 : 0x0000;
   1052  1.33      kent 			mask = 0x8080;
   1053  1.33      kent 		} else
   1054  1.33      kent 			mask = mask << si->ofs;
   1055   1.1  augustss 		break;
   1056   1.1  augustss 	case AUDIO_MIXER_VALUE:
   1057   1.1  augustss 	{
   1058  1.18  jdolecek 		const struct audio_mixer_value *value = si->info;
   1059   1.1  augustss 		u_int16_t  l, r;
   1060   1.1  augustss 
   1061   1.1  augustss 		if ((cp->un.value.num_channels <= 0) ||
   1062   1.1  augustss 		    (cp->un.value.num_channels > value->num_channels))
   1063   1.1  augustss 			return (EINVAL);
   1064   1.1  augustss 
   1065   1.1  augustss 		if (cp->un.value.num_channels == 1) {
   1066   1.1  augustss 			l = r = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1067   1.1  augustss 		} else {
   1068  1.17        rh 			if (!(as->host_flags & AC97_HOST_SWAPPED_CHANNELS)) {
   1069  1.17        rh 				l = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1070  1.17        rh 				r = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1071  1.17        rh 			} else {	/* left/right is reversed here */
   1072  1.17        rh 				r = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1073  1.17        rh 				l = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1074  1.17        rh 			}
   1075  1.17        rh 
   1076   1.1  augustss 		}
   1077   1.1  augustss 
   1078   1.1  augustss 		if (!si->polarity) {
   1079   1.1  augustss 			l = 255 - l;
   1080   1.1  augustss 			r = 255 - r;
   1081   1.1  augustss 		}
   1082   1.1  augustss 
   1083   1.1  augustss 		l = l >> (8 - si->bits);
   1084   1.1  augustss 		r = r >> (8 - si->bits);
   1085   1.1  augustss 
   1086   1.1  augustss 		newval = ((l & mask) << si->ofs);
   1087   1.1  augustss 		if (value->num_channels == 2) {
   1088   1.1  augustss 			newval |= ((r & mask) << (si->ofs + 8));
   1089   1.1  augustss 			mask |= (mask << 8);
   1090   1.1  augustss 		}
   1091  1.33      kent 		mask = mask << si->ofs;
   1092   1.1  augustss 		break;
   1093   1.1  augustss 	}
   1094   1.1  augustss 	default:
   1095   1.1  augustss 		return (EINVAL);
   1096   1.1  augustss 	}
   1097   1.1  augustss 
   1098  1.15   thorpej 	error = ac97_write(as, si->reg, (val & ~mask) | newval);
   1099   1.1  augustss 	if (error)
   1100   1.1  augustss 		return (error);
   1101   1.1  augustss 
   1102   1.1  augustss 	return (0);
   1103   1.1  augustss }
   1104   1.1  augustss 
   1105   1.1  augustss int
   1106   1.1  augustss ac97_get_portnum_by_name(codec_if, class, device, qualifier)
   1107   1.1  augustss 	struct ac97_codec_if *codec_if;
   1108  1.33      kent 	const char *class, *device, *qualifier;
   1109   1.1  augustss {
   1110   1.1  augustss 	struct ac97_softc *as = (struct ac97_softc *)codec_if;
   1111   1.1  augustss 	int idx;
   1112   1.1  augustss 
   1113   1.1  augustss 	for (idx = 0; idx < as->num_source_info; idx++) {
   1114   1.1  augustss 		struct ac97_source_info *si = &as->source_info[idx];
   1115   1.1  augustss 		if (ac97_str_equal(class, si->class) &&
   1116   1.1  augustss 		    ac97_str_equal(device, si->device) &&
   1117   1.1  augustss 		    ac97_str_equal(qualifier, si->qualifier))
   1118   1.1  augustss 			return (idx);
   1119   1.1  augustss 	}
   1120   1.1  augustss 
   1121   1.1  augustss 	return (-1);
   1122   1.1  augustss }
   1123   1.1  augustss 
   1124   1.1  augustss int
   1125   1.1  augustss ac97_mixer_get_port(codec_if, cp)
   1126   1.1  augustss 	struct ac97_codec_if *codec_if;
   1127   1.1  augustss 	mixer_ctrl_t *cp;
   1128   1.1  augustss {
   1129   1.1  augustss 	struct ac97_softc *as = (struct ac97_softc *)codec_if;
   1130   1.1  augustss 	struct ac97_source_info *si = &as->source_info[cp->dev];
   1131   1.1  augustss 	u_int16_t mask;
   1132   1.1  augustss 	u_int16_t val;
   1133   1.1  augustss 
   1134   1.1  augustss 	if (cp->dev < 0 || cp->dev >= as->num_source_info)
   1135   1.1  augustss 		return (EINVAL);
   1136   1.1  augustss 
   1137   1.1  augustss 	if (cp->type != si->type)
   1138   1.1  augustss 		return (EINVAL);
   1139   1.1  augustss 
   1140  1.15   thorpej 	ac97_read(as, si->reg, &val);
   1141   1.1  augustss 
   1142   1.1  augustss 	DPRINTFN(5, ("read(%x) = %x\n", si->reg, val));
   1143   1.1  augustss 
   1144   1.1  augustss 	mask = (1 << si->bits) - 1;
   1145   1.1  augustss 
   1146   1.1  augustss 	switch (cp->type) {
   1147   1.1  augustss 	case AUDIO_MIXER_ENUM:
   1148   1.1  augustss 		cp->un.ord = (val >> si->ofs) & mask;
   1149   1.1  augustss 		DPRINTFN(4, ("AUDIO_MIXER_ENUM: %x %d %x %d\n", val, si->ofs, mask, cp->un.ord));
   1150   1.1  augustss 		break;
   1151   1.1  augustss 	case AUDIO_MIXER_VALUE:
   1152   1.1  augustss 	{
   1153  1.18  jdolecek 		const struct audio_mixer_value *value = si->info;
   1154   1.1  augustss 		u_int16_t  l, r;
   1155   1.1  augustss 
   1156   1.1  augustss 		if ((cp->un.value.num_channels <= 0) ||
   1157   1.1  augustss 		    (cp->un.value.num_channels > value->num_channels))
   1158   1.1  augustss 			return (EINVAL);
   1159   1.1  augustss 
   1160   1.1  augustss 		if (value->num_channels == 1) {
   1161   1.1  augustss 			l = r = (val >> si->ofs) & mask;
   1162   1.1  augustss 		} else {
   1163  1.17        rh 			if (!(as->host_flags & AC97_HOST_SWAPPED_CHANNELS)) {
   1164  1.17        rh 				l = (val >> si->ofs) & mask;
   1165  1.17        rh 				r = (val >> (si->ofs + 8)) & mask;
   1166  1.17        rh 			} else {	/* host has reversed channels */
   1167  1.17        rh 				r = (val >> si->ofs) & mask;
   1168  1.17        rh 				l = (val >> (si->ofs + 8)) & mask;
   1169  1.17        rh 			}
   1170   1.1  augustss 		}
   1171   1.1  augustss 
   1172   1.1  augustss 		l = (l << (8 - si->bits));
   1173   1.1  augustss 		r = (r << (8 - si->bits));
   1174   1.1  augustss 		if (!si->polarity) {
   1175   1.1  augustss 			l = 255 - l;
   1176   1.1  augustss 			r = 255 - r;
   1177   1.1  augustss 		}
   1178   1.1  augustss 
   1179   1.1  augustss 		/* The EAP driver averages l and r for stereo
   1180   1.1  augustss 		   channels that are requested in MONO mode. Does this
   1181   1.1  augustss 		   make sense? */
   1182   1.1  augustss 		if (cp->un.value.num_channels == 1) {
   1183   1.1  augustss 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = l;
   1184   1.1  augustss 		} else if (cp->un.value.num_channels == 2) {
   1185   1.1  augustss 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
   1186   1.1  augustss 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1187   1.1  augustss 		}
   1188   1.1  augustss 
   1189   1.1  augustss 		break;
   1190   1.1  augustss 	}
   1191   1.1  augustss 	default:
   1192   1.1  augustss 		return (EINVAL);
   1193   1.1  augustss 	}
   1194   1.1  augustss 
   1195   1.1  augustss 	return (0);
   1196   1.1  augustss }
   1197   1.1  augustss 
   1198  1.28      kent 
   1199  1.28      kent int
   1200  1.28      kent ac97_set_rate(struct ac97_codec_if *codec_if, int target, u_long *rate)
   1201  1.28      kent {
   1202  1.28      kent 	struct ac97_softc *as;
   1203  1.28      kent 	u_long value;
   1204  1.28      kent 	u_int16_t ext_stat;
   1205  1.28      kent 	u_int16_t actual;
   1206  1.28      kent 	u_int16_t power;
   1207  1.28      kent 	u_int16_t power_bit;
   1208  1.28      kent 
   1209  1.28      kent 	as = (struct ac97_softc *)codec_if;
   1210  1.29      kent 	if (target == AC97_REG_PCM_MIC_ADC_RATE) {
   1211  1.29      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRM)) {
   1212  1.29      kent 			*rate = AC97_SINGLE_RATE;
   1213  1.29      kent 			return 0;
   1214  1.29      kent 		}
   1215  1.29      kent 	} else {
   1216  1.29      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRA)) {
   1217  1.29      kent 			*rate = AC97_SINGLE_RATE;
   1218  1.29      kent 			return 0;
   1219  1.29      kent 		}
   1220  1.29      kent 	}
   1221  1.28      kent 	value = *rate * AC97_STANDARD_CLOCK / as->ac97_clock;
   1222  1.28      kent 	ext_stat = 0;
   1223  1.28      kent 	/*
   1224  1.28      kent 	 * PCM_FRONT_DAC_RATE/PCM_SURR_DAC_RATE/PCM_LFE_DAC_RATE
   1225  1.28      kent 	 *	Check VRA, DRA
   1226  1.28      kent 	 * PCM_LR_ADC_RATE
   1227  1.28      kent 	 *	Check VRA
   1228  1.28      kent 	 * PCM_MIC_ADC_RATE
   1229  1.28      kent 	 *	Check VRM
   1230  1.28      kent 	 */
   1231  1.28      kent 	switch (target) {
   1232  1.28      kent 	case AC97_REG_PCM_FRONT_DAC_RATE:
   1233  1.28      kent 	case AC97_REG_PCM_SURR_DAC_RATE:
   1234  1.28      kent 	case AC97_REG_PCM_LFE_DAC_RATE:
   1235  1.28      kent 		power_bit = AC97_POWER_OUT;
   1236  1.28      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRA)) {
   1237  1.28      kent 			*rate = AC97_SINGLE_RATE;
   1238  1.28      kent 			return 0;
   1239  1.28      kent 		}
   1240  1.28      kent 		if (as->ext_id & AC97_EXT_AUDIO_DRA) {
   1241  1.28      kent 			ac97_read(as, AC97_REG_EXT_AUDIO_CTRL, &ext_stat);
   1242  1.28      kent 			if (value > 0x1ffff) {
   1243  1.28      kent 				return EINVAL;
   1244  1.28      kent 			} else if (value > 0xffff) {
   1245  1.28      kent 				/* Enable DRA */
   1246  1.28      kent 				ext_stat |= AC97_EXT_AUDIO_DRA;
   1247  1.28      kent 				ac97_write(as, AC97_REG_EXT_AUDIO_CTRL, ext_stat);
   1248  1.28      kent 				value /= 2;
   1249  1.28      kent 			} else {
   1250  1.28      kent 				/* Disable DRA */
   1251  1.28      kent 				ext_stat &= ~AC97_EXT_AUDIO_DRA;
   1252  1.28      kent 				ac97_write(as, AC97_REG_EXT_AUDIO_CTRL, ext_stat);
   1253  1.28      kent 			}
   1254  1.28      kent 		} else {
   1255  1.28      kent 			if (value > 0xffff)
   1256  1.28      kent 				return EINVAL;
   1257  1.28      kent 		}
   1258  1.28      kent 		break;
   1259  1.28      kent 	case AC97_REG_PCM_LR_ADC_RATE:
   1260  1.28      kent 		power_bit = AC97_POWER_IN;
   1261  1.28      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRA)) {
   1262  1.28      kent 			*rate = AC97_SINGLE_RATE;
   1263  1.28      kent 			return 0;
   1264  1.28      kent 		}
   1265  1.28      kent 		if (value > 0xffff)
   1266  1.28      kent 			return EINVAL;
   1267  1.28      kent 		break;
   1268  1.28      kent 	case AC97_REG_PCM_MIC_ADC_RATE:
   1269  1.28      kent 		power_bit = AC97_POWER_IN;
   1270  1.28      kent 		if (!(as->ext_id & AC97_EXT_AUDIO_VRM)) {
   1271  1.28      kent 			*rate = AC97_SINGLE_RATE;
   1272  1.28      kent 			return 0;
   1273  1.28      kent 		}
   1274  1.28      kent 		if (value > 0xffff)
   1275  1.28      kent 			return EINVAL;
   1276  1.28      kent 		break;
   1277  1.28      kent 	default:
   1278  1.28      kent 		printf("%s: Unknown register: 0x%x\n", __func__, target);
   1279  1.28      kent 		return EINVAL;
   1280  1.28      kent 	}
   1281  1.28      kent 
   1282  1.28      kent 	ac97_read(as, AC97_REG_POWER, &power);
   1283  1.28      kent 	ac97_write(as, AC97_REG_POWER, power | power_bit);
   1284  1.28      kent 
   1285  1.28      kent 	ac97_write(as, target, (u_int16_t)value);
   1286  1.28      kent 	ac97_read(as, target, &actual);
   1287  1.28      kent 	actual = (u_int32_t)actual * as->ac97_clock / AC97_STANDARD_CLOCK;
   1288  1.28      kent 
   1289  1.28      kent 	ac97_write(as, AC97_REG_POWER, power);
   1290  1.28      kent 	if (ext_stat & AC97_EXT_AUDIO_DRA) {
   1291  1.28      kent 		*rate = actual * 2;
   1292  1.28      kent 	} else {
   1293  1.28      kent 		*rate = actual;
   1294  1.28      kent 	}
   1295  1.28      kent 	return 0;
   1296  1.28      kent }
   1297  1.28      kent 
   1298  1.28      kent void
   1299  1.28      kent ac97_set_clock(struct ac97_codec_if *codec_if, unsigned int clock)
   1300  1.28      kent {
   1301  1.28      kent 	struct ac97_softc *as;
   1302  1.28      kent 
   1303  1.28      kent 	as = (struct ac97_softc *)codec_if;
   1304  1.28      kent 	as->ac97_clock = clock;
   1305  1.28      kent }
   1306  1.28      kent 
   1307  1.28      kent u_int16_t
   1308  1.28      kent ac97_get_extcaps(struct ac97_codec_if *codec_if)
   1309  1.28      kent {
   1310  1.28      kent 	struct ac97_softc *as;
   1311  1.28      kent 
   1312  1.28      kent 	as = (struct ac97_softc *)codec_if;
   1313  1.28      kent 	return as->ext_id;
   1314  1.33      kent }
   1315  1.33      kent 
   1316  1.33      kent int
   1317  1.33      kent ac97_add_port(struct ac97_softc *as, const struct ac97_source_info *src)
   1318  1.33      kent {
   1319  1.33      kent 	struct ac97_source_info *si;
   1320  1.33      kent 	int ouridx, idx;
   1321  1.33      kent 
   1322  1.33      kent 	if (as->num_source_info >= MAX_SOURCES) {
   1323  1.33      kent 		printf("%s: internal error: increase MAX_SOURCES in %s\n",
   1324  1.33      kent 		       __func__, __FILE__);
   1325  1.33      kent 		return -1;
   1326  1.33      kent 	}
   1327  1.33      kent 	if (!ac97_check_capability(as, src->req_feature))
   1328  1.33      kent 		return -1;
   1329  1.33      kent 	ouridx = as->num_source_info;
   1330  1.33      kent 	si = &as->source_info[ouridx];
   1331  1.33      kent 	memcpy(si, src, sizeof(*si));
   1332  1.33      kent 
   1333  1.33      kent 	switch (si->type) {
   1334  1.33      kent 	case AUDIO_MIXER_CLASS:
   1335  1.33      kent 	case AUDIO_MIXER_VALUE:
   1336  1.33      kent 		printf("%s: adding class/value is not supported yet.\n",
   1337  1.33      kent 		       __func__);
   1338  1.33      kent 		return -1;
   1339  1.33      kent 	case AUDIO_MIXER_ENUM:
   1340  1.33      kent 		break;
   1341  1.33      kent 	default:
   1342  1.33      kent 		printf("%s: unknown type: %d\n", __func__, si->type);
   1343  1.33      kent 		return -1;
   1344  1.33      kent 	}
   1345  1.33      kent 	as->num_source_info++;
   1346  1.33      kent 
   1347  1.33      kent 	si->mixer_class = ac97_get_portnum_by_name(&as->codec_if, si->class,
   1348  1.33      kent 						   NULL, NULL);
   1349  1.33      kent 	/* Find the root of the device */
   1350  1.33      kent 	idx = ac97_get_portnum_by_name(&as->codec_if, si->class,
   1351  1.33      kent 				       si->device, NULL);
   1352  1.33      kent 	/* Find the last item */
   1353  1.33      kent 	while (as->source_info[idx].next != AUDIO_MIXER_LAST)
   1354  1.33      kent 		idx = as->source_info[idx].next;
   1355  1.33      kent 	/* Append */
   1356  1.33      kent 	as->source_info[idx].next = ouridx;
   1357  1.33      kent 	si->prev = idx;
   1358  1.33      kent 	si->next = AUDIO_MIXER_LAST;
   1359  1.33      kent 
   1360  1.33      kent 	return 0;
   1361  1.33      kent }
   1362  1.33      kent 
   1363  1.33      kent #define ALC650_REG_MULTI_CHANNEL_CONTROL	0x6a
   1364  1.33      kent #define		ALC650_MCC_SLOT_MODIFY_MASK		0xc000
   1365  1.33      kent #define		ALC650_MCC_FRONTDAC_FROM_SPDIFIN	0x2000
   1366  1.33      kent #define		ALC650_MCC_SPDIFOUT_FROM_ADC		0x1000
   1367  1.33      kent #define		ALC650_MCC_PCM_FROM_SPDIFIN		0x0800
   1368  1.33      kent #define		ALC650_MCC_MIC_OR_CENTERLFE		0x0400
   1369  1.33      kent #define		ALC650_MCC_LINEIN_OR_SURROUND		0x0200
   1370  1.33      kent #define		ALC650_MCC_INDEPENDENT_MASTER_L		0x0080
   1371  1.33      kent #define		ALC650_MCC_INDEPENDENT_MASTER_R		0x0040
   1372  1.33      kent #define		ALC650_MCC_ANALOG_TO_CENTERLFE		0x0020
   1373  1.33      kent #define		ALC650_MCC_ANALOG_TO_SURROUND		0x0010
   1374  1.33      kent #define		ALC650_MCC_EXCHANGE_CENTERLFE		0x0008
   1375  1.33      kent #define		ALC650_MCC_CENTERLFE_DOWNMIX		0x0004
   1376  1.33      kent #define		ALC650_MCC_SURROUND_DOWNMIX		0x0002
   1377  1.33      kent #define		ALC650_MCC_LINEOUT_TO_SURROUND		0x0001
   1378  1.33      kent static void
   1379  1.33      kent ac97_alc650_init(struct ac97_softc *as)
   1380  1.33      kent {
   1381  1.33      kent 	static const struct ac97_source_info sources[3] = {
   1382  1.33      kent 		{ AudioCoutputs, AudioNsurround, "lineinjack",
   1383  1.33      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1384  1.33      kent 		  ALC650_REG_MULTI_CHANNEL_CONTROL,
   1385  1.33      kent 		  0x0000, 1, 9, 0, 0, CHECK_SURROUND },
   1386  1.33      kent 		{ AudioCoutputs, AudioNcenter, "micjack",
   1387  1.33      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1388  1.33      kent 		  ALC650_REG_MULTI_CHANNEL_CONTROL,
   1389  1.33      kent 		  0x0000, 1, 10, 0, 0, CHECK_CENTER },
   1390  1.33      kent 		{ AudioCoutputs, AudioNlfe, "micjack",
   1391  1.33      kent 		  AUDIO_MIXER_ENUM, WRAP(ac97_on_off),
   1392  1.33      kent 		  ALC650_REG_MULTI_CHANNEL_CONTROL,
   1393  1.33      kent 		  0x0000, 1, 10, 0, 0, CHECK_LFE }};
   1394  1.33      kent 
   1395  1.33      kent 	ac97_add_port(as, &sources[0]);
   1396  1.33      kent 	ac97_add_port(as, &sources[1]);
   1397  1.33      kent 	ac97_add_port(as, &sources[2]);
   1398  1.28      kent }
   1399