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