Home | History | Annotate | Line # | Download | only in ic
ac97.c revision 1.20
      1  1.20   thorpej /*      $NetBSD: ac97.c,v 1.20 2001/07/07 15:56:07 thorpej 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.1  augustss 
     65   1.1  augustss #include <sys/param.h>
     66   1.1  augustss #include <sys/systm.h>
     67   1.1  augustss #include <sys/kernel.h>
     68   1.1  augustss #include <sys/malloc.h>
     69   1.1  augustss #include <sys/device.h>
     70   1.1  augustss 
     71   1.1  augustss #include <sys/audioio.h>
     72   1.1  augustss #include <dev/audio_if.h>
     73  1.10   thorpej 
     74  1.10   thorpej #include <dev/ic/ac97reg.h>
     75   1.9   thorpej #include <dev/ic/ac97var.h>
     76   1.1  augustss 
     77  1.18  jdolecek static const struct audio_mixer_enum ac97_on_off = { 2,
     78   1.1  augustss 					       { { { AudioNoff } , 0 },
     79   1.1  augustss 					         { { AudioNon }  , 1 } }};
     80   1.1  augustss 
     81   1.1  augustss 
     82  1.18  jdolecek static const struct audio_mixer_enum ac97_mic_select = { 2,
     83   1.1  augustss 					       { { { AudioNmicrophone "0" },
     84   1.1  augustss 						   0 },
     85   1.1  augustss 					         { { AudioNmicrophone "1" },
     86   1.1  augustss 						   1 } }};
     87   1.1  augustss 
     88  1.18  jdolecek static const struct audio_mixer_enum ac97_mono_select = { 2,
     89   1.1  augustss 					       { { { AudioNmixerout },
     90   1.1  augustss 						   0 },
     91   1.1  augustss 					         { { AudioNmicrophone },
     92   1.1  augustss 						   1 } }};
     93   1.1  augustss 
     94  1.18  jdolecek static const struct audio_mixer_enum ac97_source = { 8,
     95   1.1  augustss 					       { { { AudioNmicrophone } , 0 },
     96   1.1  augustss 						 { { AudioNcd }, 1 },
     97   1.1  augustss 						 { { "video" }, 2 },
     98   1.1  augustss 						 { { AudioNaux }, 3 },
     99   1.1  augustss 						 { { AudioNline }, 4 },
    100   1.1  augustss 						 { { AudioNmixerout }, 5 },
    101   1.1  augustss 						 { { AudioNmixerout AudioNmono }, 6 },
    102   1.1  augustss 						 { { "phone" }, 7 }}};
    103   1.1  augustss 
    104  1.19       erh /*
    105  1.19       erh  * Due to different values for each source that uses these structures,
    106  1.19       erh  * the ac97_query_devinfo function sets delta in mixer_devinfo_t using
    107  1.19       erh  * ac97_source_info.bits.
    108  1.19       erh  */
    109  1.18  jdolecek static const struct audio_mixer_value ac97_volume_stereo = { { AudioNvolume },
    110   1.1  augustss 						       2 };
    111   1.1  augustss 
    112  1.18  jdolecek static const struct audio_mixer_value ac97_volume_mono = { { AudioNvolume },
    113   1.1  augustss 						     1 };
    114   1.1  augustss 
    115   1.1  augustss #define WRAP(a)  &a, sizeof(a)
    116   1.1  augustss 
    117  1.18  jdolecek const struct ac97_source_info {
    118  1.18  jdolecek 	const char *class;
    119  1.18  jdolecek 	const char *device;
    120  1.18  jdolecek 	const char *qualifier;
    121   1.1  augustss 	int  type;
    122   1.1  augustss 
    123  1.18  jdolecek 	const void *info;
    124   1.1  augustss 	int  info_size;
    125   1.1  augustss 
    126   1.1  augustss 	u_int8_t  reg;
    127  1.14   thorpej 	u_int16_t default_value;
    128   1.1  augustss 	u_int8_t  bits:3;
    129   1.1  augustss 	u_int8_t  ofs:4;
    130   1.1  augustss 	u_int8_t  mute:1;
    131   1.1  augustss 	u_int8_t  polarity:1;   /* Does 0 == MAX or MIN */
    132   1.1  augustss 
    133   1.1  augustss 	int  prev;
    134   1.1  augustss 	int  next;
    135   1.1  augustss 	int  mixer_class;
    136   1.1  augustss } source_info[] = {
    137   1.1  augustss 	{ AudioCinputs ,            NULL,           NULL,    AUDIO_MIXER_CLASS,
    138   1.1  augustss 	},
    139   1.1  augustss 	{ AudioCoutputs,            NULL,           NULL,    AUDIO_MIXER_CLASS,
    140   1.1  augustss 	},
    141   1.1  augustss 	{ AudioCrecord ,            NULL,           NULL,    AUDIO_MIXER_CLASS,
    142   1.1  augustss 	},
    143   1.1  augustss 	/* Stereo master volume*/
    144   1.1  augustss 	{ AudioCoutputs,     AudioNmaster,        NULL,    AUDIO_MIXER_VALUE,
    145   1.1  augustss 	  WRAP(ac97_volume_stereo),
    146  1.14   thorpej 	  AC97_REG_MASTER_VOLUME, 0x8000, 5, 0, 1,
    147   1.1  augustss 	},
    148   1.1  augustss 	/* Mono volume */
    149   1.1  augustss 	{ AudioCoutputs,       AudioNmono,        NULL,    AUDIO_MIXER_VALUE,
    150   1.1  augustss 	  WRAP(ac97_volume_mono),
    151  1.14   thorpej 	  AC97_REG_MASTER_VOLUME_MONO, 0x8000, 6, 0, 1,
    152   1.1  augustss 	},
    153   1.1  augustss 	{ AudioCoutputs,       AudioNmono,AudioNsource,   AUDIO_MIXER_ENUM,
    154   1.1  augustss 	  WRAP(ac97_mono_select),
    155  1.14   thorpej 	  AC97_REG_GP, 0x0000, 1, 9, 0,
    156   1.1  augustss 	},
    157   1.1  augustss 	/* Headphone volume */
    158   1.1  augustss 	{ AudioCoutputs,  AudioNheadphone,        NULL,    AUDIO_MIXER_VALUE,
    159   1.1  augustss 	  WRAP(ac97_volume_stereo),
    160  1.14   thorpej 	  AC97_REG_HEADPHONE_VOLUME, 0x8000, 6, 0, 1,
    161   1.1  augustss 	},
    162   1.1  augustss 	/* Tone */
    163   1.1  augustss 	{ AudioCoutputs,           "tone",        NULL,    AUDIO_MIXER_VALUE,
    164   1.1  augustss 	  WRAP(ac97_volume_stereo),
    165  1.14   thorpej 	  AC97_REG_MASTER_TONE, 0x0f0f, 4, 0, 0,
    166   1.1  augustss 	},
    167   1.1  augustss 	/* PC Beep Volume */
    168   1.1  augustss 	{ AudioCinputs,     AudioNspeaker,        NULL,    AUDIO_MIXER_VALUE,
    169   1.1  augustss 	  WRAP(ac97_volume_mono),
    170  1.14   thorpej 	  AC97_REG_PCBEEP_VOLUME, 0x0000, 4, 1, 1,
    171   1.1  augustss 	},
    172   1.1  augustss 	/* Phone */
    173   1.1  augustss 	{ AudioCinputs,           "phone",        NULL,    AUDIO_MIXER_VALUE,
    174   1.1  augustss 	  WRAP(ac97_volume_mono),
    175  1.14   thorpej 	  AC97_REG_PHONE_VOLUME, 0x8008, 5, 0, 1,
    176   1.1  augustss 	},
    177   1.1  augustss 	/* Mic Volume */
    178   1.1  augustss 	{ AudioCinputs,  AudioNmicrophone,        NULL,    AUDIO_MIXER_VALUE,
    179   1.1  augustss 	  WRAP(ac97_volume_mono),
    180  1.14   thorpej 	  AC97_REG_MIC_VOLUME, 0x8008, 5, 0, 1,
    181   1.1  augustss 	},
    182   1.1  augustss 	{ AudioCinputs,  AudioNmicrophone, AudioNpreamp,   AUDIO_MIXER_ENUM,
    183   1.1  augustss 	  WRAP(ac97_on_off),
    184  1.14   thorpej 	  AC97_REG_MIC_VOLUME, 0x8008, 1, 6, 0,
    185   1.1  augustss 	},
    186   1.1  augustss 	{ AudioCinputs,  AudioNmicrophone, AudioNsource,   AUDIO_MIXER_ENUM,
    187   1.1  augustss 	  WRAP(ac97_mic_select),
    188  1.14   thorpej 	  AC97_REG_GP, 0x0000, 1, 8, 0,
    189   1.1  augustss 	},
    190   1.1  augustss 	/* Line in Volume */
    191   1.1  augustss 	{ AudioCinputs,        AudioNline,        NULL,    AUDIO_MIXER_VALUE,
    192   1.1  augustss 	  WRAP(ac97_volume_stereo),
    193  1.14   thorpej 	  AC97_REG_LINEIN_VOLUME, 0x8808, 5, 0, 1,
    194   1.1  augustss 	},
    195   1.1  augustss 	/* CD Volume */
    196   1.1  augustss 	{ AudioCinputs,          AudioNcd,        NULL,    AUDIO_MIXER_VALUE,
    197   1.1  augustss 	  WRAP(ac97_volume_stereo),
    198  1.14   thorpej 	  AC97_REG_CD_VOLUME, 0x8808, 5, 0, 1,
    199   1.1  augustss 	},
    200   1.1  augustss 	/* Video Volume */
    201   1.1  augustss 	{ AudioCinputs,           "video",        NULL,    AUDIO_MIXER_VALUE,
    202   1.1  augustss 	  WRAP(ac97_volume_stereo),
    203  1.14   thorpej 	  AC97_REG_VIDEO_VOLUME, 0x8808, 5, 0, 1,
    204   1.1  augustss 	},
    205   1.1  augustss 	/* AUX volume */
    206   1.1  augustss 	{ AudioCinputs,         AudioNaux,        NULL,    AUDIO_MIXER_VALUE,
    207   1.1  augustss 	  WRAP(ac97_volume_stereo),
    208  1.14   thorpej 	  AC97_REG_AUX_VOLUME, 0x8808, 5, 0, 1,
    209   1.1  augustss 	},
    210   1.1  augustss 	/* PCM out volume */
    211   1.1  augustss 	{ AudioCinputs,         AudioNdac,        NULL,    AUDIO_MIXER_VALUE,
    212   1.1  augustss 	  WRAP(ac97_volume_stereo),
    213  1.14   thorpej 	  AC97_REG_PCMOUT_VOLUME, 0x8808, 5, 0, 1,
    214   1.1  augustss 	},
    215   1.1  augustss 	/* Record Source - some logic for this is hard coded - see below */
    216   1.1  augustss 	{ AudioCrecord,      AudioNsource,        NULL,    AUDIO_MIXER_ENUM,
    217   1.1  augustss 	  WRAP(ac97_source),
    218  1.14   thorpej 	  AC97_REG_RECORD_SELECT, 0x0000, 3, 0, 0,
    219   1.1  augustss 	},
    220   1.1  augustss 	/* Record Gain */
    221   1.1  augustss 	{ AudioCrecord,      AudioNvolume,        NULL,    AUDIO_MIXER_VALUE,
    222   1.1  augustss 	  WRAP(ac97_volume_stereo),
    223  1.14   thorpej 	  AC97_REG_RECORD_GAIN, 0x8000, 4, 0, 1,
    224   1.1  augustss 	},
    225   1.1  augustss 	/* Record Gain mic */
    226   1.1  augustss 	{ AudioCrecord,  AudioNmicrophone,        NULL,    AUDIO_MIXER_VALUE,
    227   1.1  augustss 	  WRAP(ac97_volume_mono),
    228  1.14   thorpej 	  AC97_REG_RECORD_GAIN_MIC, 0x8000, 4, 0, 1, 1,
    229   1.1  augustss 	},
    230   1.1  augustss 	/* */
    231   1.1  augustss 	{ AudioCoutputs,   AudioNloudness,        NULL,    AUDIO_MIXER_ENUM,
    232   1.1  augustss 	  WRAP(ac97_on_off),
    233  1.14   thorpej 	  AC97_REG_GP, 0x0000, 1, 12, 0,
    234   1.1  augustss 	},
    235   1.1  augustss 	{ AudioCoutputs,    AudioNspatial,        NULL,    AUDIO_MIXER_ENUM,
    236   1.1  augustss 	  WRAP(ac97_on_off),
    237  1.14   thorpej 	  AC97_REG_GP, 0x0000, 1, 13, 0,
    238   1.1  augustss 	},
    239   1.1  augustss 	{ AudioCoutputs,    AudioNspatial,    "center",    AUDIO_MIXER_VALUE,
    240   1.1  augustss 	  WRAP(ac97_volume_mono),
    241  1.14   thorpej 	  AC97_REG_3D_CONTROL, 0x0000, 4, 8, 0, 1,
    242   1.1  augustss 	},
    243   1.1  augustss 	{ AudioCoutputs,    AudioNspatial,     "depth",    AUDIO_MIXER_VALUE,
    244   1.1  augustss 	  WRAP(ac97_volume_mono),
    245  1.14   thorpej 	  AC97_REG_3D_CONTROL, 0x0000, 4, 0, 0, 1,
    246   1.1  augustss 	},
    247   1.1  augustss 
    248   1.1  augustss 	/* Missing features: Simulated Stereo, POP, Loopback mode */
    249   1.1  augustss } ;
    250   1.1  augustss 
    251   1.1  augustss #define SOURCE_INFO_SIZE (sizeof(source_info)/sizeof(source_info[0]))
    252   1.1  augustss 
    253   1.1  augustss /*
    254   1.1  augustss  * Check out http://developer.intel.com/pc-supp/platform/ac97/ for
    255   1.1  augustss  * information on AC-97
    256   1.1  augustss  */
    257   1.1  augustss 
    258   1.1  augustss struct ac97_softc {
    259  1.14   thorpej 	struct ac97_codec_if codec_if;
    260   1.1  augustss 
    261  1.14   thorpej 	struct ac97_host_if *host_if;
    262   1.1  augustss 
    263   1.1  augustss 	struct ac97_source_info source_info[2 * SOURCE_INFO_SIZE];
    264   1.1  augustss 	int num_source_info;
    265  1.14   thorpej 
    266  1.14   thorpej 	enum ac97_host_flags host_flags;
    267  1.14   thorpej 
    268  1.14   thorpej 	u_int16_t shadow_reg[128];
    269   1.1  augustss };
    270   1.1  augustss 
    271   1.1  augustss int ac97_mixer_get_port __P((struct ac97_codec_if *self, mixer_ctrl_t *cp));
    272   1.1  augustss int ac97_mixer_set_port __P((struct ac97_codec_if *self, mixer_ctrl_t *));
    273   1.1  augustss int ac97_query_devinfo __P((struct ac97_codec_if *self, mixer_devinfo_t *));
    274   1.1  augustss int ac97_get_portnum_by_name __P((struct ac97_codec_if *, char *, char *,
    275   1.1  augustss 				  char *));
    276  1.14   thorpej void ac97_restore_shadow __P((struct ac97_codec_if *self));
    277   1.1  augustss 
    278   1.1  augustss struct ac97_codec_if_vtbl ac97civ = {
    279   1.1  augustss 	ac97_mixer_get_port,
    280   1.1  augustss 	ac97_mixer_set_port,
    281   1.1  augustss 	ac97_query_devinfo,
    282  1.14   thorpej 	ac97_get_portnum_by_name,
    283  1.14   thorpej 	ac97_restore_shadow,
    284   1.1  augustss };
    285   1.7   thorpej 
    286   1.7   thorpej static const struct ac97_codecid {
    287   1.1  augustss 	u_int32_t id;
    288   1.7   thorpej 	const char *name;
    289   1.1  augustss } ac97codecid[] = {
    290  1.13   thorpej 	{ AC97_CODEC_ID('A', 'D', 'S', 64),	"Analog Devices AD1881" },
    291   1.7   thorpej 	{ AC97_CODEC_ID('A', 'K', 'M', 0),	"Asahi Kasei AK4540"	},
    292  1.13   thorpej 	{ AC97_CODEC_ID('A', 'K', 'M', 2),	"Asahi Kasei AK4543"	},
    293   1.7   thorpej 	{ AC97_CODEC_ID('C', 'R', 'Y', 0),	"Crystal CS4297"	},
    294   1.7   thorpej 	{ AC97_CODEC_ID('C', 'R', 'Y', 3),	"Crystal CS4297"	},
    295   1.7   thorpej 	{ AC97_CODEC_ID('C', 'R', 'Y', 19),	"Crystal CS4297A"	},
    296  1.13   thorpej 	{ AC97_CODEC_ID('C', 'R', 'Y', 35),	"Crystal CS4298",	},
    297  1.13   thorpej 	{ AC97_CODEC_ID('C', 'R', 'Y', 43),	"Crystal CS4294",	},
    298  1.13   thorpej 	{ AC97_CODEC_ID('C', 'R', 'Y', 49),	"Crystal CS4299",	},
    299  1.13   thorpej 	{ AC97_CODEC_ID('C', 'R', 'Y', 51),	"Crystal CS4298A",	},
    300  1.16  augustss 	{ AC97_CODEC_ID('C', 'R', 'Y', 52),	"Crystal CS4299",	},
    301  1.16  augustss 	{ AC97_CODEC_ID('N', 'S', 'C', 49), "National Semiconductor LM4549", },
    302  1.13   thorpej 	{ AC97_CODEC_ID('S', 'I', 'L', 34),	"Silicon Laboratory Si3036", },
    303  1.13   thorpej 	{ AC97_CODEC_ID('S', 'I', 'L', 35),	"Silicon Laboratory Si3038", },
    304  1.13   thorpej 	{ AC97_CODEC_ID('T', 'R', 'A', 2),	"TriTech TR28022",	},
    305  1.13   thorpej 	{ AC97_CODEC_ID('T', 'R', 'A', 3),	"TriTech TR28023",	},
    306  1.16  augustss 	{ AC97_CODEC_ID('T', 'R', 'A', 6),	"TriTech TR28026",	},
    307  1.16  augustss 	{ AC97_CODEC_ID('T', 'R', 'A', 8),	"TriTech TR28028",	},
    308  1.13   thorpej 	{ AC97_CODEC_ID('T', 'R', 'A', 35),	"TriTech unknown",	},
    309  1.13   thorpej 	{ AC97_CODEC_ID('W', 'M', 'L', 0),	"Wolfson WM9704",	},
    310  1.13   thorpej 	{ AC97_CODEC_ID('W', 'M', 'L', 3),	"Wolfson WM9707",	},
    311  1.13   thorpej 	{ 0x83847600,				"SigmaTel STAC9700",	},
    312  1.13   thorpej 	{ 0x83847604,				"SigmaTel STAC9701/3/4/5", },
    313  1.13   thorpej 	{ 0x83847605,				"SigmaTel STAC9704", 	},
    314  1.13   thorpej 	{ 0x83847608,				"SigmaTel STAC9708", 	},
    315  1.13   thorpej 	{ 0x83847609,				"SigmaTel STAC9721/23",	},
    316  1.13   thorpej 	{ 0x83847644,				"SigmaTel STAC9744/45",	},
    317  1.13   thorpej 	{ 0x83847684,				"SigmaTel STAC9783/84",	},
    318  1.13   thorpej 	{ 0,					NULL,			}
    319   1.1  augustss };
    320   1.1  augustss 
    321  1.18  jdolecek static const char * const ac97enhancement[] = {
    322   1.2     soren 	"no 3D stereo",
    323   1.1  augustss 	"Analog Devices Phat Stereo",
    324   1.1  augustss 	"Creative"
    325   1.1  augustss 	"National Semi 3D",
    326   1.1  augustss 	"Yamaha Ymersion",
    327   1.1  augustss 	"BBE 3D",
    328   1.1  augustss 	"Crystal Semi 3D"
    329   1.1  augustss 	"Qsound QXpander",
    330   1.1  augustss 	"Spatializer 3D",
    331   1.1  augustss 	"SRS 3D",
    332   1.1  augustss 	"Platform Tech 3D",
    333   1.1  augustss 	"AKM 3D",
    334   1.1  augustss 	"Aureal",
    335   1.1  augustss 	"AZTECH 3D",
    336   1.1  augustss 	"Binaura 3D",
    337   1.1  augustss 	"ESS Technology",
    338   1.1  augustss 	"Harman International VMAx",
    339   1.1  augustss 	"Nvidea 3D",
    340   1.1  augustss 	"Philips Incredible Sound",
    341   1.1  augustss 	"Texas Instruments' 3D",
    342   1.1  augustss 	"VLSI Technology 3D",
    343   1.1  augustss 	"TriTech 3D",
    344   1.1  augustss 	"Realtek 3D",
    345   1.1  augustss 	"Samsung 3D",
    346   1.1  augustss 	"Wolfson Microelectronics 3D",
    347   1.1  augustss 	"Delta Integration 3D",
    348   1.1  augustss 	"SigmaTel 3D",
    349   1.1  augustss 	"Unknown 3D",
    350   1.1  augustss 	"Rockwell 3D",
    351   1.1  augustss 	"Unknown 3D",
    352   1.1  augustss 	"Unknown 3D",
    353   1.1  augustss 	"Unknown 3D",
    354   1.1  augustss };
    355   1.1  augustss 
    356  1.18  jdolecek static const char * const ac97feature[] = {
    357   1.1  augustss 	"mic channel",
    358   1.1  augustss 	"reserved",
    359   1.1  augustss 	"tone",
    360   1.1  augustss 	"simulated stereo",
    361   1.1  augustss 	"headphone",
    362   1.1  augustss 	"bass boost",
    363   1.1  augustss 	"18 bit DAC",
    364   1.1  augustss 	"20 bit DAC",
    365   1.1  augustss 	"18 bit ADC",
    366   1.1  augustss 	"20 bit ADC"
    367   1.1  augustss };
    368   1.1  augustss 
    369   1.1  augustss 
    370  1.18  jdolecek int ac97_str_equal __P((const char *, const char *));
    371   1.1  augustss void ac97_setup_source_info __P((struct ac97_softc *));
    372  1.14   thorpej void ac97_read __P((struct ac97_softc *, u_int8_t, u_int16_t *));
    373  1.14   thorpej void ac97_setup_defaults __P((struct ac97_softc *));
    374  1.14   thorpej int ac97_write __P((struct ac97_softc *, u_int8_t, u_int16_t));
    375   1.1  augustss 
    376   1.1  augustss /* #define AC97_DEBUG 10 */
    377   1.1  augustss 
    378   1.1  augustss #ifdef AUDIO_DEBUG
    379   1.1  augustss #define DPRINTF(x)	if (ac97debug) printf x
    380   1.1  augustss #define DPRINTFN(n,x)	if (ac97debug>(n)) printf x
    381   1.1  augustss #ifdef AC97_DEBUG
    382   1.1  augustss int	ac97debug = AC97_DEBUG;
    383   1.1  augustss #else
    384   1.1  augustss int	ac97debug = 0;
    385   1.1  augustss #endif
    386   1.1  augustss #else
    387   1.1  augustss #define DPRINTF(x)
    388   1.1  augustss #define DPRINTFN(n,x)
    389   1.1  augustss #endif
    390   1.1  augustss 
    391  1.14   thorpej void
    392  1.14   thorpej ac97_read(as, reg, val)
    393  1.14   thorpej 	struct ac97_softc *as;
    394  1.14   thorpej 	u_int8_t reg;
    395  1.14   thorpej 	u_int16_t *val;
    396  1.14   thorpej {
    397  1.14   thorpej 	int error;
    398  1.14   thorpej 
    399  1.14   thorpej 	if (as->host_flags & AC97_HOST_DONT_READ &&
    400  1.14   thorpej 	    (reg != AC97_REG_VENDOR_ID1 && reg != AC97_REG_VENDOR_ID2 &&
    401  1.14   thorpej 	     reg != AC97_REG_RESET)) {
    402  1.14   thorpej 		*val = as->shadow_reg[reg >> 1];
    403  1.14   thorpej 		return;
    404  1.14   thorpej 	}
    405  1.14   thorpej 
    406  1.14   thorpej 	if ((error = as->host_if->read(as->host_if->arg, reg, val))) {
    407  1.14   thorpej 		*val = as->shadow_reg[reg >> 1];
    408  1.14   thorpej 	}
    409  1.14   thorpej }
    410  1.14   thorpej 
    411  1.14   thorpej int
    412  1.14   thorpej ac97_write(as, reg, val)
    413  1.14   thorpej 	struct ac97_softc *as;
    414  1.14   thorpej 	u_int8_t reg;
    415  1.14   thorpej 	u_int16_t val;
    416  1.14   thorpej {
    417  1.14   thorpej 
    418  1.14   thorpej 	as->shadow_reg[reg >> 1] = val;
    419  1.14   thorpej 
    420  1.14   thorpej 	return (as->host_if->write(as->host_if->arg, reg, val));
    421  1.14   thorpej }
    422  1.14   thorpej 
    423  1.14   thorpej void
    424  1.14   thorpej ac97_setup_defaults(as)
    425  1.14   thorpej 	struct ac97_softc *as;
    426  1.14   thorpej {
    427  1.14   thorpej 	int idx;
    428  1.18  jdolecek 	const struct ac97_source_info *si;
    429  1.14   thorpej 
    430  1.14   thorpej 	memset(as->shadow_reg, 0, sizeof(as->shadow_reg));
    431  1.14   thorpej 
    432  1.14   thorpej 	for (idx = 0; idx < SOURCE_INFO_SIZE; idx++) {
    433  1.14   thorpej 		si = &source_info[idx];
    434  1.14   thorpej 		ac97_write(as, si->reg, si->default_value);
    435  1.14   thorpej 	}
    436  1.14   thorpej }
    437  1.14   thorpej 
    438  1.14   thorpej void
    439  1.14   thorpej ac97_restore_shadow(self)
    440  1.14   thorpej 	struct ac97_codec_if *self;
    441  1.14   thorpej {
    442  1.14   thorpej 	struct ac97_softc *as = (struct ac97_softc *) self;
    443  1.14   thorpej 	int idx;
    444  1.18  jdolecek 	const struct ac97_source_info *si;
    445  1.14   thorpej 
    446  1.14   thorpej 	for (idx = 0; idx < SOURCE_INFO_SIZE; idx++) {
    447  1.14   thorpej 		si = &source_info[idx];
    448  1.14   thorpej 		ac97_write(as, si->reg, as->shadow_reg[si->reg >> 1]);
    449  1.14   thorpej 	}
    450  1.14   thorpej }
    451   1.1  augustss 
    452   1.1  augustss int
    453   1.1  augustss ac97_str_equal(a, b)
    454  1.18  jdolecek 	const char *a, *b;
    455   1.1  augustss {
    456   1.1  augustss 	return ((a == b) || (a && b && (!strcmp(a, b))));
    457   1.1  augustss }
    458   1.1  augustss 
    459   1.1  augustss void
    460   1.1  augustss ac97_setup_source_info(as)
    461   1.1  augustss 	struct ac97_softc *as;
    462   1.1  augustss {
    463   1.1  augustss 	int idx, ouridx;
    464   1.1  augustss 	struct ac97_source_info *si, *si2;
    465   1.1  augustss 
    466   1.1  augustss 	for (idx = 0, ouridx = 0; idx < SOURCE_INFO_SIZE; idx++) {
    467   1.1  augustss 		si = &as->source_info[ouridx];
    468   1.1  augustss 
    469  1.20   thorpej 		memcpy(si, &source_info[idx], sizeof(*si));
    470   1.1  augustss 
    471   1.1  augustss 		switch (si->type) {
    472   1.1  augustss 		case AUDIO_MIXER_CLASS:
    473   1.1  augustss 		        si->mixer_class = ouridx;
    474   1.1  augustss 			ouridx++;
    475   1.1  augustss 			break;
    476   1.1  augustss 		case AUDIO_MIXER_VALUE:
    477   1.1  augustss 			/* Todo - Test to see if it works */
    478   1.1  augustss 			ouridx++;
    479   1.1  augustss 
    480   1.1  augustss 			/* Add an entry for mute, if necessary */
    481   1.1  augustss 			if (si->mute) {
    482   1.1  augustss 				si = &as->source_info[ouridx];
    483  1.20   thorpej 				memcpy(si, &source_info[idx], sizeof(*si));
    484   1.1  augustss 				si->qualifier = AudioNmute;
    485   1.1  augustss 				si->type = AUDIO_MIXER_ENUM;
    486   1.1  augustss 				si->info = &ac97_on_off;
    487   1.1  augustss 				si->info_size = sizeof(ac97_on_off);
    488   1.1  augustss 				si->bits = 1;
    489   1.1  augustss 				si->ofs = 15;
    490   1.1  augustss 				si->mute = 0;
    491   1.1  augustss 				si->polarity = 0;
    492   1.1  augustss 				ouridx++;
    493   1.1  augustss 			}
    494   1.1  augustss 			break;
    495   1.1  augustss 		case AUDIO_MIXER_ENUM:
    496   1.1  augustss 			/* Todo - Test to see if it works */
    497   1.1  augustss 			ouridx++;
    498   1.1  augustss 			break;
    499   1.1  augustss 		default:
    500   1.1  augustss 			printf ("ac97: shouldn't get here\n");
    501   1.1  augustss 			break;
    502   1.1  augustss 		}
    503   1.1  augustss 	}
    504   1.1  augustss 
    505   1.1  augustss 	as->num_source_info = ouridx;
    506   1.1  augustss 
    507   1.1  augustss 	for (idx = 0; idx < as->num_source_info; idx++) {
    508   1.1  augustss 		int idx2, previdx;
    509   1.1  augustss 
    510   1.1  augustss 		si = &as->source_info[idx];
    511   1.1  augustss 
    512   1.1  augustss 		/* Find mixer class */
    513   1.1  augustss 		for (idx2 = 0; idx2 < as->num_source_info; idx2++) {
    514   1.1  augustss 			si2 = &as->source_info[idx2];
    515   1.1  augustss 
    516   1.1  augustss 			if (si2->type == AUDIO_MIXER_CLASS &&
    517   1.1  augustss 			    ac97_str_equal(si->class,
    518   1.1  augustss 					   si2->class)) {
    519   1.1  augustss 				si->mixer_class = idx2;
    520   1.1  augustss 			}
    521   1.1  augustss 		}
    522   1.1  augustss 
    523   1.1  augustss 
    524   1.1  augustss 		/* Setup prev and next pointers */
    525   1.1  augustss 		if (si->prev != 0)
    526   1.1  augustss 			continue;
    527   1.1  augustss 
    528   1.1  augustss 		if (si->qualifier)
    529   1.1  augustss 			continue;
    530   1.1  augustss 
    531   1.1  augustss 		si->prev = AUDIO_MIXER_LAST;
    532   1.1  augustss 		previdx = idx;
    533   1.1  augustss 
    534   1.1  augustss 		for (idx2 = 0; idx2 < as->num_source_info; idx2++) {
    535   1.1  augustss 			if (idx2 == idx)
    536   1.1  augustss 				continue;
    537   1.1  augustss 
    538   1.1  augustss 			si2 = &as->source_info[idx2];
    539   1.1  augustss 
    540   1.1  augustss 			if (!si2->prev &&
    541   1.1  augustss 			    ac97_str_equal(si->class, si2->class) &&
    542   1.1  augustss 			    ac97_str_equal(si->device, si2->device)) {
    543   1.1  augustss 				as->source_info[previdx].next = idx2;
    544   1.1  augustss 				as->source_info[idx2].prev = previdx;
    545   1.1  augustss 
    546   1.1  augustss 				previdx = idx2;
    547   1.1  augustss 			}
    548   1.1  augustss 		}
    549   1.1  augustss 
    550   1.1  augustss 		as->source_info[previdx].next = AUDIO_MIXER_LAST;
    551   1.1  augustss 	}
    552   1.1  augustss }
    553   1.1  augustss 
    554   1.1  augustss int
    555  1.14   thorpej ac97_attach(host_if)
    556  1.14   thorpej 	struct ac97_host_if *host_if;
    557   1.1  augustss {
    558   1.1  augustss 	struct ac97_softc *as;
    559  1.14   thorpej 	struct device *sc_dev = (struct device *)host_if->arg;
    560   1.1  augustss 	int error, i, j;
    561   1.1  augustss 	u_int16_t id1, id2, caps;
    562   1.1  augustss 	u_int32_t id;
    563   1.8  augustss 	mixer_ctrl_t ctl;
    564   1.1  augustss 
    565   1.1  augustss 	as = malloc(sizeof(struct ac97_softc), M_DEVBUF, M_WAITOK);
    566   1.1  augustss 
    567   1.2     soren 	if (as == NULL)
    568   1.2     soren 		return (ENOMEM);
    569   1.1  augustss 
    570  1.14   thorpej 	memset(as, 0, sizeof(*as));
    571  1.14   thorpej 
    572  1.14   thorpej 	as->codec_if.vtbl = &ac97civ;
    573  1.14   thorpej 	as->host_if = host_if;
    574   1.1  augustss 
    575  1.14   thorpej 	if ((error = host_if->attach(host_if->arg, &as->codec_if))) {
    576   1.1  augustss 		free (as, M_DEVBUF);
    577   1.1  augustss 		return (error);
    578   1.1  augustss 	}
    579   1.1  augustss 
    580  1.14   thorpej 	host_if->reset(host_if->arg);
    581   1.1  augustss 
    582  1.14   thorpej 	host_if->write(host_if->arg, AC97_REG_POWER, 0);
    583  1.14   thorpej 	host_if->write(host_if->arg, AC97_REG_RESET, 0);
    584   1.1  augustss 
    585  1.14   thorpej 	if (host_if->flags)
    586  1.14   thorpej 		as->host_flags = host_if->flags(host_if->arg);
    587   1.1  augustss 
    588  1.14   thorpej 	ac97_setup_defaults(as);
    589  1.15   thorpej 	ac97_read(as, AC97_REG_VENDOR_ID1, &id1);
    590  1.15   thorpej 	ac97_read(as, AC97_REG_VENDOR_ID2, &id2);
    591  1.15   thorpej 	ac97_read(as, AC97_REG_RESET, &caps);
    592   1.1  augustss 
    593   1.1  augustss 	id = (id1 << 16) | id2;
    594   1.1  augustss 
    595   1.2     soren 	printf("%s: ", sc_dev->dv_xname);
    596   1.2     soren 
    597   1.2     soren 	for (i = 0; ; i++) {
    598   1.2     soren 		if (ac97codecid[i].id == 0) {
    599   1.4  augustss 			char pnp[4];
    600   1.7   thorpej 
    601   1.7   thorpej 			AC97_GET_CODEC_ID(id, pnp);
    602   1.4  augustss #define ISASCII(c) ((c) >= ' ' && (c) < 0x7f)
    603   1.4  augustss 			if (ISASCII(pnp[0]) && ISASCII(pnp[1]) &&
    604   1.4  augustss 			    ISASCII(pnp[2]))
    605   1.7   thorpej 				printf("%c%c%c%d", pnp[0], pnp[1], pnp[2],
    606   1.7   thorpej 				    pnp[3]);
    607   1.4  augustss 			else
    608  1.12     soren 				printf("unknown (0x%08x)", id);
    609  1.11     soren 			break;
    610  1.11     soren 		}
    611  1.11     soren 		if (ac97codecid[i].id == id) {
    612  1.11     soren 			printf("%s", ac97codecid[i].name);
    613   1.2     soren 			break;
    614   1.2     soren 		}
    615   1.1  augustss 	}
    616   1.2     soren 	printf(" codec; ");
    617   1.1  augustss 	for (i = j = 0; i < 10; i++) {
    618   1.1  augustss 		if (caps & (1 << i)) {
    619   1.1  augustss 			printf("%s%s", j? ", " : "", ac97feature[i]);
    620   1.1  augustss 			j++;
    621   1.1  augustss 		}
    622   1.1  augustss 	}
    623   1.1  augustss 
    624   1.1  augustss 	printf("%s%s\n", j? ", " : "", ac97enhancement[(caps >> 10) & 0x1f]);
    625   1.1  augustss 
    626   1.1  augustss 	ac97_setup_source_info(as);
    627   1.8  augustss 
    628   1.8  augustss 	/* Just enable the DAC and master volumes by default */
    629   1.8  augustss 	memset(&ctl, 0, sizeof(ctl));
    630   1.8  augustss 
    631   1.8  augustss 	ctl.type = AUDIO_MIXER_ENUM;
    632   1.8  augustss 	ctl.un.ord = 0;  /* off */
    633  1.14   thorpej 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCoutputs,
    634   1.8  augustss 					   AudioNmaster, AudioNmute);
    635  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
    636  1.14   thorpej 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCinputs,
    637   1.8  augustss 					   AudioNdac, AudioNmute);
    638   1.8  augustss 
    639  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
    640  1.14   thorpej 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCrecord,
    641   1.8  augustss 					   AudioNvolume, AudioNmute);
    642  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
    643   1.8  augustss 
    644  1.14   thorpej 	ctl.dev = ac97_get_portnum_by_name(&as->codec_if, AudioCrecord,
    645   1.8  augustss 					   AudioNsource, NULL);
    646   1.8  augustss 	ctl.type = AUDIO_MIXER_ENUM;
    647   1.8  augustss 	ctl.un.ord = 0;
    648  1.14   thorpej 	ac97_mixer_set_port(&as->codec_if, &ctl);
    649   1.1  augustss 
    650   1.1  augustss 	return (0);
    651   1.1  augustss }
    652   1.1  augustss 
    653   1.1  augustss 
    654   1.1  augustss int
    655   1.1  augustss ac97_query_devinfo(codec_if, dip)
    656   1.1  augustss 	struct ac97_codec_if *codec_if;
    657   1.1  augustss 	mixer_devinfo_t *dip;
    658   1.1  augustss {
    659   1.1  augustss 	struct ac97_softc *as = (struct ac97_softc *)codec_if;
    660   1.1  augustss 
    661   1.1  augustss 	if (dip->index < as->num_source_info) {
    662   1.1  augustss 		struct ac97_source_info *si = &as->source_info[dip->index];
    663  1.18  jdolecek 		const char *name;
    664   1.1  augustss 
    665   1.1  augustss 		dip->type = si->type;
    666   1.1  augustss 		dip->mixer_class = si->mixer_class;
    667   1.1  augustss 		dip->prev = si->prev;
    668   1.1  augustss 		dip->next = si->next;
    669   1.1  augustss 
    670   1.1  augustss 		if (si->qualifier)
    671   1.1  augustss 			name = si->qualifier;
    672   1.1  augustss 		else if (si->device)
    673   1.1  augustss 			name = si->device;
    674   1.1  augustss 		else if (si->class)
    675   1.1  augustss 			name = si->class;
    676   1.6  augustss 		else
    677   1.6  augustss 			name = 0;
    678   1.1  augustss 
    679   1.1  augustss 		if (name)
    680   1.1  augustss 			strcpy(dip->label.name, name);
    681   1.1  augustss 
    682  1.20   thorpej 		memcpy(&dip->un, si->info, si->info_size);
    683  1.19       erh 
    684  1.19       erh 		/* Set the delta for volume sources */
    685  1.19       erh 		if (dip->type == AUDIO_MIXER_VALUE)
    686  1.19       erh 			dip->un.v.delta = 1 << (8 - si->bits);
    687  1.19       erh 
    688   1.1  augustss 		return (0);
    689   1.1  augustss 	}
    690   1.1  augustss 
    691   1.1  augustss 	return (ENXIO);
    692   1.1  augustss }
    693   1.1  augustss 
    694   1.1  augustss 
    695   1.1  augustss 
    696   1.1  augustss int
    697   1.1  augustss ac97_mixer_set_port(codec_if, cp)
    698   1.1  augustss 	struct ac97_codec_if *codec_if;
    699   1.1  augustss 	mixer_ctrl_t *cp;
    700   1.1  augustss {
    701   1.1  augustss 	struct ac97_softc *as = (struct ac97_softc *)codec_if;
    702   1.1  augustss 	struct ac97_source_info *si = &as->source_info[cp->dev];
    703   1.1  augustss 	u_int16_t mask;
    704   1.1  augustss 	u_int16_t val, newval;
    705   1.1  augustss 	int error;
    706   1.1  augustss 
    707   1.1  augustss 	if (cp->dev < 0 || cp->dev >= as->num_source_info)
    708   1.1  augustss 		return (EINVAL);
    709   1.1  augustss 
    710   1.1  augustss 	if (cp->type != si->type)
    711   1.1  augustss 		return (EINVAL);
    712   1.1  augustss 
    713  1.15   thorpej 	ac97_read(as, si->reg, &val);
    714   1.1  augustss 
    715   1.1  augustss 	DPRINTFN(5, ("read(%x) = %x\n", si->reg, val));
    716   1.1  augustss 
    717   1.1  augustss 	mask = (1 << si->bits) - 1;
    718   1.1  augustss 
    719   1.1  augustss 	switch (cp->type) {
    720   1.1  augustss 	case AUDIO_MIXER_ENUM:
    721   1.1  augustss 		if (cp->un.ord > mask || cp->un.ord < 0)
    722   1.1  augustss 			return (EINVAL);
    723   1.1  augustss 
    724   1.1  augustss 		newval = (cp->un.ord << si->ofs);
    725   1.1  augustss 		if (si->reg == AC97_REG_RECORD_SELECT) {
    726   1.1  augustss 			newval |= (newval << (8 + si->ofs));
    727   1.1  augustss 			mask |= (mask << 8);
    728   1.1  augustss 		}
    729   1.1  augustss 		break;
    730   1.1  augustss 	case AUDIO_MIXER_VALUE:
    731   1.1  augustss 	{
    732  1.18  jdolecek 		const struct audio_mixer_value *value = si->info;
    733   1.1  augustss 		u_int16_t  l, r;
    734   1.1  augustss 
    735   1.1  augustss 		if ((cp->un.value.num_channels <= 0) ||
    736   1.1  augustss 		    (cp->un.value.num_channels > value->num_channels))
    737   1.1  augustss 			return (EINVAL);
    738   1.1  augustss 
    739   1.1  augustss 		if (cp->un.value.num_channels == 1) {
    740   1.1  augustss 			l = r = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    741   1.1  augustss 		} else {
    742  1.17        rh 			if (!(as->host_flags & AC97_HOST_SWAPPED_CHANNELS)) {
    743  1.17        rh 				l = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    744  1.17        rh 				r = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    745  1.17        rh 			} else {	/* left/right is reversed here */
    746  1.17        rh 				r = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    747  1.17        rh 				l = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    748  1.17        rh 			}
    749  1.17        rh 
    750   1.1  augustss 		}
    751   1.1  augustss 
    752   1.1  augustss 		if (!si->polarity) {
    753   1.1  augustss 			l = 255 - l;
    754   1.1  augustss 			r = 255 - r;
    755   1.1  augustss 		}
    756   1.1  augustss 
    757   1.1  augustss 		l = l >> (8 - si->bits);
    758   1.1  augustss 		r = r >> (8 - si->bits);
    759   1.1  augustss 
    760   1.1  augustss 		newval = ((l & mask) << si->ofs);
    761   1.1  augustss 		if (value->num_channels == 2) {
    762   1.1  augustss 			newval |= ((r & mask) << (si->ofs + 8));
    763   1.1  augustss 			mask |= (mask << 8);
    764   1.1  augustss 		}
    765   1.1  augustss 
    766   1.1  augustss 		break;
    767   1.1  augustss 	}
    768   1.1  augustss 	default:
    769   1.1  augustss 		return (EINVAL);
    770   1.1  augustss 	}
    771   1.1  augustss 
    772   1.1  augustss 	mask = mask << si->ofs;
    773  1.15   thorpej 	error = ac97_write(as, si->reg, (val & ~mask) | newval);
    774   1.1  augustss 	if (error)
    775   1.1  augustss 		return (error);
    776   1.1  augustss 
    777   1.1  augustss 	return (0);
    778   1.1  augustss }
    779   1.1  augustss 
    780   1.1  augustss int
    781   1.1  augustss ac97_get_portnum_by_name(codec_if, class, device, qualifier)
    782   1.1  augustss 	struct ac97_codec_if *codec_if;
    783   1.1  augustss 	char *class, *device, *qualifier;
    784   1.1  augustss {
    785   1.1  augustss 	struct ac97_softc *as = (struct ac97_softc *)codec_if;
    786   1.1  augustss 	int idx;
    787   1.1  augustss 
    788   1.1  augustss 	for (idx = 0; idx < as->num_source_info; idx++) {
    789   1.1  augustss 		struct ac97_source_info *si = &as->source_info[idx];
    790   1.1  augustss 		if (ac97_str_equal(class, si->class) &&
    791   1.1  augustss 		    ac97_str_equal(device, si->device) &&
    792   1.1  augustss 		    ac97_str_equal(qualifier, si->qualifier))
    793   1.1  augustss 			return (idx);
    794   1.1  augustss 	}
    795   1.1  augustss 
    796   1.1  augustss 	return (-1);
    797   1.1  augustss }
    798   1.1  augustss 
    799   1.1  augustss int
    800   1.1  augustss ac97_mixer_get_port(codec_if, cp)
    801   1.1  augustss 	struct ac97_codec_if *codec_if;
    802   1.1  augustss 	mixer_ctrl_t *cp;
    803   1.1  augustss {
    804   1.1  augustss 	struct ac97_softc *as = (struct ac97_softc *)codec_if;
    805   1.1  augustss 	struct ac97_source_info *si = &as->source_info[cp->dev];
    806   1.1  augustss 	u_int16_t mask;
    807   1.1  augustss 	u_int16_t val;
    808   1.1  augustss 
    809   1.1  augustss 	if (cp->dev < 0 || cp->dev >= as->num_source_info)
    810   1.1  augustss 		return (EINVAL);
    811   1.1  augustss 
    812   1.1  augustss 	if (cp->type != si->type)
    813   1.1  augustss 		return (EINVAL);
    814   1.1  augustss 
    815  1.15   thorpej 	ac97_read(as, si->reg, &val);
    816   1.1  augustss 
    817   1.1  augustss 	DPRINTFN(5, ("read(%x) = %x\n", si->reg, val));
    818   1.1  augustss 
    819   1.1  augustss 	mask = (1 << si->bits) - 1;
    820   1.1  augustss 
    821   1.1  augustss 	switch (cp->type) {
    822   1.1  augustss 	case AUDIO_MIXER_ENUM:
    823   1.1  augustss 		cp->un.ord = (val >> si->ofs) & mask;
    824   1.1  augustss 		DPRINTFN(4, ("AUDIO_MIXER_ENUM: %x %d %x %d\n", val, si->ofs, mask, cp->un.ord));
    825   1.1  augustss 		break;
    826   1.1  augustss 	case AUDIO_MIXER_VALUE:
    827   1.1  augustss 	{
    828  1.18  jdolecek 		const struct audio_mixer_value *value = si->info;
    829   1.1  augustss 		u_int16_t  l, r;
    830   1.1  augustss 
    831   1.1  augustss 		if ((cp->un.value.num_channels <= 0) ||
    832   1.1  augustss 		    (cp->un.value.num_channels > value->num_channels))
    833   1.1  augustss 			return (EINVAL);
    834   1.1  augustss 
    835   1.1  augustss 		if (value->num_channels == 1) {
    836   1.1  augustss 			l = r = (val >> si->ofs) & mask;
    837   1.1  augustss 		} else {
    838  1.17        rh 			if (!(as->host_flags & AC97_HOST_SWAPPED_CHANNELS)) {
    839  1.17        rh 				l = (val >> si->ofs) & mask;
    840  1.17        rh 				r = (val >> (si->ofs + 8)) & mask;
    841  1.17        rh 			} else {	/* host has reversed channels */
    842  1.17        rh 				r = (val >> si->ofs) & mask;
    843  1.17        rh 				l = (val >> (si->ofs + 8)) & mask;
    844  1.17        rh 			}
    845   1.1  augustss 		}
    846   1.1  augustss 
    847   1.1  augustss 		l = (l << (8 - si->bits));
    848   1.1  augustss 		r = (r << (8 - si->bits));
    849   1.1  augustss 		if (!si->polarity) {
    850   1.1  augustss 			l = 255 - l;
    851   1.1  augustss 			r = 255 - r;
    852   1.1  augustss 		}
    853   1.1  augustss 
    854   1.1  augustss 		/* The EAP driver averages l and r for stereo
    855   1.1  augustss 		   channels that are requested in MONO mode. Does this
    856   1.1  augustss 		   make sense? */
    857   1.1  augustss 		if (cp->un.value.num_channels == 1) {
    858   1.1  augustss 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = l;
    859   1.1  augustss 		} else if (cp->un.value.num_channels == 2) {
    860   1.1  augustss 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    861   1.1  augustss 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    862   1.1  augustss 		}
    863   1.1  augustss 
    864   1.1  augustss 		break;
    865   1.1  augustss 	}
    866   1.1  augustss 	default:
    867   1.1  augustss 		return (EINVAL);
    868   1.1  augustss 	}
    869   1.1  augustss 
    870   1.1  augustss 	return (0);
    871   1.1  augustss }
    872   1.1  augustss 
    873