linux-bk/sound/oss/ac97_codec.c
<<
>>
Prefs
   1/*
   2 * ac97_codec.c: Generic AC97 mixer/modem module
   3 *
   4 * Derived from ac97 mixer in maestro and trident driver.
   5 *
   6 * Copyright 2000 Silicon Integrated System Corporation
   7 *
   8 *      This program is free software; you can redistribute it and/or modify
   9 *      it under the terms of the GNU General Public License as published by
  10 *      the Free Software Foundation; either version 2 of the License, or
  11 *      (at your option) any later version.
  12 *
  13 *      This program is distributed in the hope that it will be useful,
  14 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 *      GNU General Public License for more details.
  17 *
  18 *      You should have received a copy of the GNU General Public License
  19 *      along with this program; if not, write to the Free Software
  20 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21 *
  22 **************************************************************************
  23 *
  24 * The Intel Audio Codec '97 specification is available at the Intel
  25 * audio homepage: http://developer.intel.com/ial/scalableplatforms/audio/
  26 *
  27 * The specification itself is currently available at:
  28 * ftp://download.intel.com/ial/scalableplatforms/ac97r22.pdf
  29 *
  30 **************************************************************************
  31 *
  32 * History
  33 * May 02, 2003 Liam Girdwood <liam.girdwood@wolfsonmicro.com>
  34 *      Removed non existant WM9700
  35 *      Added support for WM9705, WM9708, WM9709, WM9710, WM9711
  36 *      WM9712 and WM9717
  37 * Mar 28, 2002 Randolph Bentson <bentson@holmsjoen.com>
  38 *      corrections to support WM9707 in ViewPad 1000
  39 * v0.4 Mar 15 2000 Ollie Lho
  40 *      dual codecs support verified with 4 channels output
  41 * v0.3 Feb 22 2000 Ollie Lho
  42 *      bug fix for record mask setting
  43 * v0.2 Feb 10 2000 Ollie Lho
  44 *      add ac97_read_proc for /proc/driver/{vendor}/ac97
  45 * v0.1 Jan 14 2000 Ollie Lho <ollie@sis.com.tw> 
  46 *      Isolated from trident.c to support multiple ac97 codec
  47 */
  48#include <linux/module.h>
  49#include <linux/kernel.h>
  50#include <linux/slab.h>
  51#include <linux/string.h>
  52#include <linux/errno.h>
  53#include <linux/bitops.h>
  54#include <linux/delay.h>
  55#include <linux/pci.h>
  56#include <linux/ac97_codec.h>
  57#include <asm/uaccess.h>
  58
  59#define CODEC_ID_BUFSZ 14
  60
  61static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel);
  62static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel, 
  63                             unsigned int left, unsigned int right);
  64static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val );
  65static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask);
  66static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg);
  67
  68static int ac97_init_mixer(struct ac97_codec *codec);
  69
  70static int wolfson_init03(struct ac97_codec * codec);
  71static int wolfson_init04(struct ac97_codec * codec);
  72static int wolfson_init05(struct ac97_codec * codec);
  73static int wolfson_init11(struct ac97_codec * codec);
  74static int tritech_init(struct ac97_codec * codec);
  75static int tritech_maestro_init(struct ac97_codec * codec);
  76static int sigmatel_9708_init(struct ac97_codec *codec);
  77static int sigmatel_9721_init(struct ac97_codec *codec);
  78static int sigmatel_9744_init(struct ac97_codec *codec);
  79static int ad1886_init(struct ac97_codec *codec);
  80static int eapd_control(struct ac97_codec *codec, int);
  81static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
  82static int cmedia_init(struct ac97_codec * codec);
  83static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
  84static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
  85
  86
  87/*
  88 *      AC97 operations.
  89 *
  90 *      If you are adding a codec then you should be able to use
  91 *              eapd_ops - any codec that supports EAPD amp control (most)
  92 *              null_ops - any ancient codec that supports nothing
  93 *
  94 *      The three functions are
  95 *              init - used for non AC97 standard initialisation
  96 *              amplifier - used to do amplifier control (1=on 0=off)
  97 *              digital - switch to digital modes (0 = analog)
  98 *
  99 *      Not all codecs support all features, not all drivers use all the
 100 *      operations yet
 101 */
 102 
 103static struct ac97_ops null_ops = { NULL, NULL, NULL };
 104static struct ac97_ops default_ops = { NULL, eapd_control, NULL };
 105static struct ac97_ops default_digital_ops = { NULL, eapd_control, generic_digital_control};
 106static struct ac97_ops wolfson_ops03 = { wolfson_init03, NULL, NULL };
 107static struct ac97_ops wolfson_ops04 = { wolfson_init04, NULL, NULL };
 108static struct ac97_ops wolfson_ops05 = { wolfson_init05, NULL, NULL };
 109static struct ac97_ops wolfson_ops11 = { wolfson_init11, NULL, NULL };
 110static struct ac97_ops tritech_ops = { tritech_init, NULL, NULL };
 111static struct ac97_ops tritech_m_ops = { tritech_maestro_init, NULL, NULL };
 112static struct ac97_ops sigmatel_9708_ops = { sigmatel_9708_init, NULL, NULL };
 113static struct ac97_ops sigmatel_9721_ops = { sigmatel_9721_init, NULL, NULL };
 114static struct ac97_ops sigmatel_9744_ops = { sigmatel_9744_init, NULL, NULL };
 115static struct ac97_ops crystal_digital_ops = { NULL, eapd_control, crystal_digital_control };
 116static struct ac97_ops ad1886_ops = { ad1886_init, eapd_control, NULL };
 117static struct ac97_ops cmedia_ops = { NULL, eapd_control, NULL};
 118static struct ac97_ops cmedia_digital_ops = { cmedia_init, eapd_control, cmedia_digital_control};
 119
 120/* sorted by vendor/device id */
 121static const struct {
 122        u32 id;
 123        char *name;
 124        struct ac97_ops *ops;
 125        int flags;
 126} ac97_codec_ids[] = {
 127        {0x41445303, "Analog Devices AD1819",   &null_ops},
 128        {0x41445340, "Analog Devices AD1881",   &null_ops},
 129        {0x41445348, "Analog Devices AD1881A",  &null_ops},
 130        {0x41445360, "Analog Devices AD1885",   &default_ops},
 131        {0x41445361, "Analog Devices AD1886",   &ad1886_ops},
 132        {0x41445370, "Analog Devices AD1981",   &null_ops},
 133        {0x41445372, "Analog Devices AD1981A",  &null_ops},
 134        {0x41445374, "Analog Devices AD1981B",  &null_ops},
 135        {0x41445460, "Analog Devices AD1885",   &default_ops},
 136        {0x41445461, "Analog Devices AD1886",   &ad1886_ops},
 137        {0x414B4D00, "Asahi Kasei AK4540",      &null_ops},
 138        {0x414B4D01, "Asahi Kasei AK4542",      &null_ops},
 139        {0x414B4D02, "Asahi Kasei AK4543",      &null_ops},
 140        {0x414C4326, "ALC100P",                 &null_ops},
 141        {0x414C4710, "ALC200/200P",             &null_ops},
 142        {0x414C4720, "ALC650",                  &default_digital_ops},
 143        {0x434D4941, "CMedia",                  &cmedia_ops,            AC97_NO_PCM_VOLUME },
 144        {0x434D4942, "CMedia",                  &cmedia_ops,            AC97_NO_PCM_VOLUME },
 145        {0x434D4961, "CMedia",                  &cmedia_digital_ops,    AC97_NO_PCM_VOLUME },
 146        {0x43525900, "Cirrus Logic CS4297",     &default_ops},
 147        {0x43525903, "Cirrus Logic CS4297",     &default_ops},
 148        {0x43525913, "Cirrus Logic CS4297A rev A", &default_ops},
 149        {0x43525914, "Cirrus Logic CS4297A rev B", &default_ops},
 150        {0x43525923, "Cirrus Logic CS4298",     &null_ops},
 151        {0x4352592B, "Cirrus Logic CS4294",     &null_ops},
 152        {0x4352592D, "Cirrus Logic CS4294",     &null_ops},
 153        {0x43525931, "Cirrus Logic CS4299 rev A", &crystal_digital_ops},
 154        {0x43525933, "Cirrus Logic CS4299 rev C", &crystal_digital_ops},
 155        {0x43525934, "Cirrus Logic CS4299 rev D", &crystal_digital_ops},
 156        {0x43585442, "CXT66",                   &default_ops,           AC97_DELUDED_MODEM },
 157        {0x44543031, "Diamond Technology DT0893", &default_ops},
 158        {0x45838308, "ESS Allegro ES1988",      &null_ops},
 159        {0x49434511, "ICE1232",                 &null_ops}, /* I hope --jk */
 160        {0x4e534331, "National Semiconductor LM4549", &null_ops},
 161        {0x53494c22, "Silicon Laboratory Si3036", &null_ops},
 162        {0x53494c23, "Silicon Laboratory Si3038", &null_ops},
 163        {0x545200FF, "TriTech TR?????",         &tritech_m_ops},
 164        {0x54524102, "TriTech TR28022",         &null_ops},
 165        {0x54524103, "TriTech TR28023",         &null_ops},
 166        {0x54524106, "TriTech TR28026",         &null_ops},
 167        {0x54524108, "TriTech TR28028",         &tritech_ops},
 168        {0x54524123, "TriTech TR A5",           &null_ops},
 169        {0x574D4C03, "Wolfson WM9703/07/08/17", &wolfson_ops03},
 170        {0x574D4C04, "Wolfson WM9704M/WM9704Q", &wolfson_ops04},
 171        {0x574D4C05, "Wolfson WM9705/WM9710",   &wolfson_ops05},
 172        {0x574D4C09, "Wolfson WM9709",          &null_ops},
 173        {0x574D4C12, "Wolfson WM9711/9712",     &wolfson_ops11},
 174        {0x83847600, "SigmaTel STAC????",       &null_ops},
 175        {0x83847604, "SigmaTel STAC9701/3/4/5", &null_ops},
 176        {0x83847605, "SigmaTel STAC9704",       &null_ops},
 177        {0x83847608, "SigmaTel STAC9708",       &sigmatel_9708_ops},
 178        {0x83847609, "SigmaTel STAC9721/23",    &sigmatel_9721_ops},
 179        {0x83847644, "SigmaTel STAC9744/45",    &sigmatel_9744_ops},
 180        {0x83847652, "SigmaTel STAC9752/53",    &default_ops},
 181        {0x83847656, "SigmaTel STAC9756/57",    &sigmatel_9744_ops},
 182        {0x83847666, "SigmaTel STAC9750T",      &sigmatel_9744_ops},
 183        {0x83847684, "SigmaTel STAC9783/84?",   &null_ops},
 184        {0x57454301, "Winbond 83971D",          &null_ops},
 185};
 186
 187static const char *ac97_stereo_enhancements[] =
 188{
 189        /*   0 */ "No 3D Stereo Enhancement",
 190        /*   1 */ "Analog Devices Phat Stereo",
 191        /*   2 */ "Creative Stereo Enhancement",
 192        /*   3 */ "National Semi 3D Stereo Enhancement",
 193        /*   4 */ "YAMAHA Ymersion",
 194        /*   5 */ "BBE 3D Stereo Enhancement",
 195        /*   6 */ "Crystal Semi 3D Stereo Enhancement",
 196        /*   7 */ "Qsound QXpander",
 197        /*   8 */ "Spatializer 3D Stereo Enhancement",
 198        /*   9 */ "SRS 3D Stereo Enhancement",
 199        /*  10 */ "Platform Tech 3D Stereo Enhancement",
 200        /*  11 */ "AKM 3D Audio",
 201        /*  12 */ "Aureal Stereo Enhancement",
 202        /*  13 */ "Aztech 3D Enhancement",
 203        /*  14 */ "Binaura 3D Audio Enhancement",
 204        /*  15 */ "ESS Technology Stereo Enhancement",
 205        /*  16 */ "Harman International VMAx",
 206        /*  17 */ "Nvidea 3D Stereo Enhancement",
 207        /*  18 */ "Philips Incredible Sound",
 208        /*  19 */ "Texas Instruments 3D Stereo Enhancement",
 209        /*  20 */ "VLSI Technology 3D Stereo Enhancement",
 210        /*  21 */ "TriTech 3D Stereo Enhancement",
 211        /*  22 */ "Realtek 3D Stereo Enhancement",
 212        /*  23 */ "Samsung 3D Stereo Enhancement",
 213        /*  24 */ "Wolfson Microelectronics 3D Enhancement",
 214        /*  25 */ "Delta Integration 3D Enhancement",
 215        /*  26 */ "SigmaTel 3D Enhancement",
 216        /*  27 */ "Winbond 3D Stereo Enhancement",
 217        /*  28 */ "Rockwell 3D Stereo Enhancement",
 218        /*  29 */ "Reserved 29",
 219        /*  30 */ "Reserved 30",
 220        /*  31 */ "Reserved 31"
 221};
 222
 223/* this table has default mixer values for all OSS mixers. */
 224static struct mixer_defaults {
 225        int mixer;
 226        unsigned int value;
 227} mixer_defaults[SOUND_MIXER_NRDEVICES] = {
 228        /* all values 0 -> 100 in bytes */
 229        {SOUND_MIXER_VOLUME,    0x4343},
 230        {SOUND_MIXER_BASS,      0x4343},
 231        {SOUND_MIXER_TREBLE,    0x4343},
 232        {SOUND_MIXER_PCM,       0x4343},
 233        {SOUND_MIXER_SPEAKER,   0x4343},
 234        {SOUND_MIXER_LINE,      0x4343},
 235        {SOUND_MIXER_MIC,       0x0000},
 236        {SOUND_MIXER_CD,        0x4343},
 237        {SOUND_MIXER_ALTPCM,    0x4343},
 238        {SOUND_MIXER_IGAIN,     0x4343},
 239        {SOUND_MIXER_LINE1,     0x4343},
 240        {SOUND_MIXER_PHONEIN,   0x4343},
 241        {SOUND_MIXER_PHONEOUT,  0x4343},
 242        {SOUND_MIXER_VIDEO,     0x4343},
 243        {-1,0}
 244};
 245
 246/* table to scale scale from OSS mixer value to AC97 mixer register value */    
 247static struct ac97_mixer_hw {
 248        unsigned char offset;
 249        int scale;
 250} ac97_hw[SOUND_MIXER_NRDEVICES]= {
 251        [SOUND_MIXER_VOLUME]    =       {AC97_MASTER_VOL_STEREO,64},
 252        [SOUND_MIXER_BASS]      =       {AC97_MASTER_TONE,      16},
 253        [SOUND_MIXER_TREBLE]    =       {AC97_MASTER_TONE,      16},
 254        [SOUND_MIXER_PCM]       =       {AC97_PCMOUT_VOL,       32},
 255        [SOUND_MIXER_SPEAKER]   =       {AC97_PCBEEP_VOL,       16},
 256        [SOUND_MIXER_LINE]      =       {AC97_LINEIN_VOL,       32},
 257        [SOUND_MIXER_MIC]       =       {AC97_MIC_VOL,          32},
 258        [SOUND_MIXER_CD]        =       {AC97_CD_VOL,           32},
 259        [SOUND_MIXER_ALTPCM]    =       {AC97_HEADPHONE_VOL,    64},
 260        [SOUND_MIXER_IGAIN]     =       {AC97_RECORD_GAIN,      16},
 261        [SOUND_MIXER_LINE1]     =       {AC97_AUX_VOL,          32},
 262        [SOUND_MIXER_PHONEIN]   =       {AC97_PHONE_VOL,        32},
 263        [SOUND_MIXER_PHONEOUT]  =       {AC97_MASTER_VOL_MONO,  64},
 264        [SOUND_MIXER_VIDEO]     =       {AC97_VIDEO_VOL,        32},
 265};
 266
 267/* the following tables allow us to go from OSS <-> ac97 quickly. */
 268enum ac97_recsettings {
 269        AC97_REC_MIC=0,
 270        AC97_REC_CD,
 271        AC97_REC_VIDEO,
 272        AC97_REC_AUX,
 273        AC97_REC_LINE,
 274        AC97_REC_STEREO, /* combination of all enabled outputs..  */
 275        AC97_REC_MONO,        /*.. or the mono equivalent */
 276        AC97_REC_PHONE
 277};
 278
 279static const unsigned int ac97_rm2oss[] = {
 280        [AC97_REC_MIC]   = SOUND_MIXER_MIC,
 281        [AC97_REC_CD]    = SOUND_MIXER_CD,
 282        [AC97_REC_VIDEO] = SOUND_MIXER_VIDEO,
 283        [AC97_REC_AUX]   = SOUND_MIXER_LINE1,
 284        [AC97_REC_LINE]  = SOUND_MIXER_LINE,
 285        [AC97_REC_STEREO]= SOUND_MIXER_IGAIN,
 286        [AC97_REC_PHONE] = SOUND_MIXER_PHONEIN
 287};
 288
 289/* indexed by bit position */
 290static const unsigned int ac97_oss_rm[] = {
 291        [SOUND_MIXER_MIC]       = AC97_REC_MIC,
 292        [SOUND_MIXER_CD]        = AC97_REC_CD,
 293        [SOUND_MIXER_VIDEO]     = AC97_REC_VIDEO,
 294        [SOUND_MIXER_LINE1]     = AC97_REC_AUX,
 295        [SOUND_MIXER_LINE]      = AC97_REC_LINE,
 296        [SOUND_MIXER_IGAIN]     = AC97_REC_STEREO,
 297        [SOUND_MIXER_PHONEIN]   = AC97_REC_PHONE
 298};
 299
 300static LIST_HEAD(codecs);
 301static LIST_HEAD(codec_drivers);
 302static DECLARE_MUTEX(codec_sem);
 303
 304/* reads the given OSS mixer from the ac97 the caller must have insured that the ac97 knows
 305   about that given mixer, and should be holding a spinlock for the card */
 306static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel) 
 307{
 308        u16 val;
 309        int ret = 0;
 310        int scale;
 311        struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
 312
 313        val = codec->codec_read(codec , mh->offset);
 314
 315        if (val & AC97_MUTE) {
 316                ret = 0;
 317        } else if (AC97_STEREO_MASK & (1 << oss_channel)) {
 318                /* nice stereo mixers .. */
 319                int left,right;
 320
 321                left = (val >> 8)  & 0x7f;
 322                right = val  & 0x7f;
 323
 324                if (oss_channel == SOUND_MIXER_IGAIN) {
 325                        right = (right * 100) / mh->scale;
 326                        left = (left * 100) / mh->scale;
 327                } else {
 328                        /* these may have 5 or 6 bit resolution */
 329                        if(oss_channel == SOUND_MIXER_VOLUME || oss_channel == SOUND_MIXER_ALTPCM)
 330                                scale = (1 << codec->bit_resolution);
 331                        else
 332                                scale = mh->scale;
 333
 334                        right = 100 - ((right * 100) / scale);
 335                        left = 100 - ((left * 100) / scale);
 336                }
 337                ret = left | (right << 8);
 338        } else if (oss_channel == SOUND_MIXER_SPEAKER) {
 339                ret = 100 - ((((val & 0x1e)>>1) * 100) / mh->scale);
 340        } else if (oss_channel == SOUND_MIXER_PHONEIN) {
 341                ret = 100 - (((val & 0x1f) * 100) / mh->scale);
 342        } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
 343                scale = (1 << codec->bit_resolution);
 344                ret = 100 - (((val & 0x1f) * 100) / scale);
 345        } else if (oss_channel == SOUND_MIXER_MIC) {
 346                ret = 100 - (((val & 0x1f) * 100) / mh->scale);
 347                /*  the low bit is optional in the tone sliders and masking
 348                    it lets us avoid the 0xf 'bypass'.. */
 349        } else if (oss_channel == SOUND_MIXER_BASS) {
 350                ret = 100 - ((((val >> 8) & 0xe) * 100) / mh->scale);
 351        } else if (oss_channel == SOUND_MIXER_TREBLE) {
 352                ret = 100 - (((val & 0xe) * 100) / mh->scale);
 353        }
 354
 355#ifdef DEBUG
 356        printk("ac97_codec: read OSS mixer %2d (%s ac97 register 0x%02x), "
 357               "0x%04x -> 0x%04x\n",
 358               oss_channel, codec->id ? "Secondary" : "Primary",
 359               mh->offset, val, ret);
 360#endif
 361
 362        return ret;
 363}
 364
 365/* write the OSS encoded volume to the given OSS encoded mixer, again caller's job to
 366   make sure all is well in arg land, call with spinlock held */
 367static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
 368                      unsigned int left, unsigned int right)
 369{
 370        u16 val = 0;
 371        int scale;
 372        struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
 373
 374#ifdef DEBUG
 375        printk("ac97_codec: wrote OSS mixer %2d (%s ac97 register 0x%02x), "
 376               "left vol:%2d, right vol:%2d:",
 377               oss_channel, codec->id ? "Secondary" : "Primary",
 378               mh->offset, left, right);
 379#endif
 380
 381        if (AC97_STEREO_MASK & (1 << oss_channel)) {
 382                /* stereo mixers */
 383                if (left == 0 && right == 0) {
 384                        val = AC97_MUTE;
 385                } else {
 386                        if (oss_channel == SOUND_MIXER_IGAIN) {
 387                                right = (right * mh->scale) / 100;
 388                                left = (left * mh->scale) / 100;
 389                                if (right >= mh->scale)
 390                                        right = mh->scale-1;
 391                                if (left >= mh->scale)
 392                                        left = mh->scale-1;
 393                        } else {
 394                                /* these may have 5 or 6 bit resolution */
 395                                if (oss_channel == SOUND_MIXER_VOLUME ||
 396                                    oss_channel == SOUND_MIXER_ALTPCM)
 397                                        scale = (1 << codec->bit_resolution);
 398                                else
 399                                        scale = mh->scale;
 400
 401                                right = ((100 - right) * scale) / 100;
 402                                left = ((100 - left) * scale) / 100;
 403                                if (right >= scale)
 404                                        right = scale-1;
 405                                if (left >= scale)
 406                                        left = scale-1;
 407                        }
 408                        val = (left << 8) | right;
 409                }
 410        } else if (oss_channel == SOUND_MIXER_BASS) {
 411                val = codec->codec_read(codec , mh->offset) & ~0x0f00;
 412                left = ((100 - left) * mh->scale) / 100;
 413                if (left >= mh->scale)
 414                        left = mh->scale-1;
 415                val |= (left << 8) & 0x0e00;
 416        } else if (oss_channel == SOUND_MIXER_TREBLE) {
 417                val = codec->codec_read(codec , mh->offset) & ~0x000f;
 418                left = ((100 - left) * mh->scale) / 100;
 419                if (left >= mh->scale)
 420                        left = mh->scale-1;
 421                val |= left & 0x000e;
 422        } else if(left == 0) {
 423                val = AC97_MUTE;
 424        } else if (oss_channel == SOUND_MIXER_SPEAKER) {
 425                left = ((100 - left) * mh->scale) / 100;
 426                if (left >= mh->scale)
 427                        left = mh->scale-1;
 428                val = left << 1;
 429        } else if (oss_channel == SOUND_MIXER_PHONEIN) {
 430                left = ((100 - left) * mh->scale) / 100;
 431                if (left >= mh->scale)
 432                        left = mh->scale-1;
 433                val = left;
 434        } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
 435                scale = (1 << codec->bit_resolution);
 436                left = ((100 - left) * scale) / 100;
 437                if (left >= mh->scale)
 438                        left = mh->scale-1;
 439                val = left;
 440        } else if (oss_channel == SOUND_MIXER_MIC) {
 441                val = codec->codec_read(codec , mh->offset) & ~0x801f;
 442                left = ((100 - left) * mh->scale) / 100;
 443                if (left >= mh->scale)
 444                        left = mh->scale-1;
 445                val |= left;
 446                /*  the low bit is optional in the tone sliders and masking
 447                    it lets us avoid the 0xf 'bypass'.. */
 448        }
 449#ifdef DEBUG
 450        printk(" 0x%04x", val);
 451#endif
 452
 453        codec->codec_write(codec, mh->offset, val);
 454
 455#ifdef DEBUG
 456        val = codec->codec_read(codec, mh->offset);
 457        printk(" -> 0x%04x\n", val);
 458#endif
 459}
 460
 461/* a thin wrapper for write_mixer */
 462static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val ) 
 463{
 464        unsigned int left,right;
 465
 466        /* cleanse input a little */
 467        right = ((val >> 8)  & 0xff) ;
 468        left = (val  & 0xff) ;
 469
 470        if (right > 100) right = 100;
 471        if (left > 100) left = 100;
 472
 473        codec->mixer_state[oss_mixer] = (right << 8) | left;
 474        codec->write_mixer(codec, oss_mixer, left, right);
 475}
 476
 477/* read or write the recmask, the ac97 can really have left and right recording
 478   inputs independantly set, but OSS doesn't seem to want us to express that to
 479   the user. the caller guarantees that we have a supported bit set, and they
 480   must be holding the card's spinlock */
 481static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask) 
 482{
 483        unsigned int val;
 484
 485        if (rw) {
 486                /* read it from the card */
 487                val = codec->codec_read(codec, AC97_RECORD_SELECT);
 488#ifdef DEBUG
 489                printk("ac97_codec: ac97 recmask to set to 0x%04x\n", val);
 490#endif
 491                return (1 << ac97_rm2oss[val & 0x07]);
 492        }
 493
 494        /* else, write the first set in the mask as the
 495           output */    
 496        /* clear out current set value first (AC97 supports only 1 input!) */
 497        val = (1 << ac97_rm2oss[codec->codec_read(codec, AC97_RECORD_SELECT) & 0x07]);
 498        if (mask != val)
 499            mask &= ~val;
 500       
 501        val = ffs(mask); 
 502        val = ac97_oss_rm[val-1];
 503        val |= val << 8;  /* set both channels */
 504
 505#ifdef DEBUG
 506        printk("ac97_codec: setting ac97 recmask to 0x%04x\n", val);
 507#endif
 508
 509        codec->codec_write(codec, AC97_RECORD_SELECT, val);
 510
 511        return 0;
 512};
 513
 514static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg)
 515{
 516        int i, val = 0;
 517
 518        if (cmd == SOUND_MIXER_INFO) {
 519                mixer_info info;
 520                memset(&info, 0, sizeof(info));
 521                strlcpy(info.id, codec->name, sizeof(info.id));
 522                strlcpy(info.name, codec->name, sizeof(info.name));
 523                info.modify_counter = codec->modcnt;
 524                if (copy_to_user((void __user *)arg, &info, sizeof(info)))
 525                        return -EFAULT;
 526                return 0;
 527        }
 528        if (cmd == SOUND_OLD_MIXER_INFO) {
 529                _old_mixer_info info;
 530                memset(&info, 0, sizeof(info));
 531                strlcpy(info.id, codec->name, sizeof(info.id));
 532                strlcpy(info.name, codec->name, sizeof(info.name));
 533                if (copy_to_user((void __user *)arg, &info, sizeof(info)))
 534                        return -EFAULT;
 535                return 0;
 536        }
 537
 538        if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
 539                return -EINVAL;
 540
 541        if (cmd == OSS_GETVERSION)
 542                return put_user(SOUND_VERSION, (int __user *)arg);
 543
 544        if (_SIOC_DIR(cmd) == _SIOC_READ) {
 545                switch (_IOC_NR(cmd)) {
 546                case SOUND_MIXER_RECSRC: /* give them the current record source */
 547                        if (!codec->recmask_io) {
 548                                val = 0;
 549                        } else {
 550                                val = codec->recmask_io(codec, 1, 0);
 551                        }
 552                        break;
 553
 554                case SOUND_MIXER_DEVMASK: /* give them the supported mixers */
 555                        val = codec->supported_mixers;
 556                        break;
 557
 558                case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
 559                        val = codec->record_sources;
 560                        break;
 561
 562                case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
 563                        val = codec->stereo_mixers;
 564                        break;
 565
 566                case SOUND_MIXER_CAPS:
 567                        val = SOUND_CAP_EXCL_INPUT;
 568                        break;
 569
 570                default: /* read a specific mixer */
 571                        i = _IOC_NR(cmd);
 572
 573                        if (!supported_mixer(codec, i)) 
 574                                return -EINVAL;
 575
 576                        /* do we ever want to touch the hardware? */
 577                        /* val = codec->read_mixer(codec, i); */
 578                        val = codec->mixer_state[i];
 579                        break;
 580                }
 581                return put_user(val, (int __user *)arg);
 582        }
 583
 584        if (_SIOC_DIR(cmd) == (_SIOC_WRITE|_SIOC_READ)) {
 585                codec->modcnt++;
 586                if (get_user(val, (int __user *)arg))
 587                        return -EFAULT;
 588
 589                switch (_IOC_NR(cmd)) {
 590                case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
 591                        if (!codec->recmask_io) return -EINVAL;
 592                        if (!val) return 0;
 593                        if (!(val &= codec->record_sources)) return -EINVAL;
 594
 595                        codec->recmask_io(codec, 0, val);
 596
 597                        return 0;
 598                default: /* write a specific mixer */
 599                        i = _IOC_NR(cmd);
 600
 601                        if (!supported_mixer(codec, i)) 
 602                                return -EINVAL;
 603
 604                        ac97_set_mixer(codec, i, val);
 605
 606                        return 0;
 607                }
 608        }
 609        return -EINVAL;
 610}
 611
 612/* entry point for /proc/driver/controller_vendor/ac97/%d */
 613int ac97_read_proc (char *page, char **start, off_t off,
 614                    int count, int *eof, void *data)
 615{
 616        int len = 0, cap, extid, val, id1, id2;
 617        struct ac97_codec *codec;
 618        int is_ac97_20 = 0;
 619
 620        if ((codec = data) == NULL)
 621                return -ENODEV;
 622
 623        id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
 624        id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
 625        len += sprintf (page+len, "Vendor name      : %s\n", codec->name);
 626        len += sprintf (page+len, "Vendor id        : %04X %04X\n", id1, id2);
 627
 628        extid = codec->codec_read(codec, AC97_EXTENDED_ID);
 629        extid &= ~((1<<2)|(1<<4)|(1<<5)|(1<<10)|(1<<11)|(1<<12)|(1<<13));
 630        len += sprintf (page+len, "AC97 Version     : %s\n",
 631                        extid ? "2.0 or later" : "1.0");
 632        if (extid) is_ac97_20 = 1;
 633
 634        cap = codec->codec_read(codec, AC97_RESET);
 635        len += sprintf (page+len, "Capabilities     :%s%s%s%s%s%s\n",
 636                        cap & 0x0001 ? " -dedicated MIC PCM IN channel-" : "",
 637                        cap & 0x0002 ? " -reserved1-" : "",
 638                        cap & 0x0004 ? " -bass & treble-" : "",
 639                        cap & 0x0008 ? " -simulated stereo-" : "",
 640                        cap & 0x0010 ? " -headphone out-" : "",
 641                        cap & 0x0020 ? " -loudness-" : "");
 642        val = cap & 0x00c0;
 643        len += sprintf (page+len, "DAC resolutions  :%s%s%s\n",
 644                        " -16-bit-",
 645                        val & 0x0040 ? " -18-bit-" : "",
 646                        val & 0x0080 ? " -20-bit-" : "");
 647        val = cap & 0x0300;
 648        len += sprintf (page+len, "ADC resolutions  :%s%s%s\n",
 649                        " -16-bit-",
 650                        val & 0x0100 ? " -18-bit-" : "",
 651                        val & 0x0200 ? " -20-bit-" : "");
 652        len += sprintf (page+len, "3D enhancement   : %s\n",
 653                        ac97_stereo_enhancements[(cap >> 10) & 0x1f]);
 654
 655        val = codec->codec_read(codec, AC97_GENERAL_PURPOSE);
 656        len += sprintf (page+len, "POP path         : %s 3D\n"
 657                        "Sim. stereo      : %s\n"
 658                        "3D enhancement   : %s\n"
 659                        "Loudness         : %s\n"
 660                        "Mono output      : %s\n"
 661                        "MIC select       : %s\n"
 662                        "ADC/DAC loopback : %s\n",
 663                        val & 0x8000 ? "post" : "pre",
 664                        val & 0x4000 ? "on" : "off",
 665                        val & 0x2000 ? "on" : "off",
 666                        val & 0x1000 ? "on" : "off",
 667                        val & 0x0200 ? "MIC" : "MIX",
 668                        val & 0x0100 ? "MIC2" : "MIC1",
 669                        val & 0x0080 ? "on" : "off");
 670
 671        extid = codec->codec_read(codec, AC97_EXTENDED_ID);
 672        cap = extid;
 673        len += sprintf (page+len, "Ext Capabilities :%s%s%s%s%s%s%s\n",
 674                        cap & 0x0001 ? " -var rate PCM audio-" : "",
 675                        cap & 0x0002 ? " -2x PCM audio out-" : "",
 676                        cap & 0x0008 ? " -var rate MIC in-" : "",
 677                        cap & 0x0040 ? " -PCM center DAC-" : "",
 678                        cap & 0x0080 ? " -PCM surround DAC-" : "",
 679                        cap & 0x0100 ? " -PCM LFE DAC-" : "",
 680                        cap & 0x0200 ? " -slot/DAC mappings-" : "");
 681        if (is_ac97_20) {
 682                len += sprintf (page+len, "Front DAC rate   : %d\n",
 683                                codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE));
 684        }
 685
 686        return len;
 687}
 688
 689/**
 690 *      codec_id        -  Turn id1/id2 into a PnP string
 691 *      @id1: Vendor ID1
 692 *      @id2: Vendor ID2
 693 *      @buf: CODEC_ID_BUFSZ byte buffer
 694 *
 695 *      Fills buf with a zero terminated PnP ident string for the id1/id2
 696 *      pair. For convenience the return is the passed in buffer pointer.
 697 */
 698 
 699static char *codec_id(u16 id1, u16 id2, char *buf)
 700{
 701        if(id1&0x8080) {
 702                snprintf(buf, CODEC_ID_BUFSZ, "0x%04x:0x%04x", id1, id2);
 703        } else {
 704                buf[0] = (id1 >> 8);
 705                buf[1] = (id1 & 0xFF);
 706                buf[2] = (id2 >> 8);
 707                snprintf(buf+3, CODEC_ID_BUFSZ - 3, "%d", id2&0xFF);
 708        }
 709        return buf;
 710}
 711 
 712/**
 713 *      ac97_check_modem - Check if the Codec is a modem
 714 *      @codec: codec to check
 715 *
 716 *      Return true if the device is an AC97 1.0 or AC97 2.0 modem
 717 */
 718 
 719static int ac97_check_modem(struct ac97_codec *codec)
 720{
 721        /* Check for an AC97 1.0 soft modem (ID1) */
 722        if(codec->codec_read(codec, AC97_RESET) & 2)
 723                return 1;
 724        /* Check for an AC97 2.x soft modem */
 725        codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0L);
 726        if(codec->codec_read(codec, AC97_EXTENDED_MODEM_ID) & 1)
 727                return 1;
 728        return 0;
 729}
 730
 731
 732/**
 733 *      ac97_alloc_codec - Allocate an AC97 codec
 734 *
 735 *      Returns a new AC97 codec structure. AC97 codecs may become
 736 *      refcounted soon so this interface is needed. Returns with
 737 *      one reference taken.
 738 */
 739 
 740struct ac97_codec *ac97_alloc_codec(void)
 741{
 742        struct ac97_codec *codec = kmalloc(sizeof(struct ac97_codec), GFP_KERNEL);
 743        if(!codec)
 744                return NULL;
 745
 746        memset(codec, 0, sizeof(*codec));
 747        spin_lock_init(&codec->lock);
 748        INIT_LIST_HEAD(&codec->list);
 749        return codec;
 750}
 751
 752EXPORT_SYMBOL(ac97_alloc_codec);
 753
 754/**
 755 *      ac97_release_codec -    Release an AC97 codec
 756 *      @codec: codec to release
 757 *
 758 *      Release an allocated AC97 codec. This will be refcounted in
 759 *      time but for the moment is trivial. Calls the unregister
 760 *      handler if the codec is now defunct.
 761 */
 762 
 763void ac97_release_codec(struct ac97_codec *codec)
 764{
 765        /* Remove from the list first, we don't want to be
 766           "rediscovered" */
 767        down(&codec_sem);
 768        list_del(&codec->list);
 769        up(&codec_sem);
 770        /*
 771         *      The driver needs to deal with internal
 772         *      locking to avoid accidents here. 
 773         */
 774        if(codec->driver)
 775                codec->driver->remove(codec, codec->driver);
 776        kfree(codec);
 777}
 778
 779EXPORT_SYMBOL(ac97_release_codec);
 780
 781/**
 782 *      ac97_probe_codec - Initialize and setup AC97-compatible codec
 783 *      @codec: (in/out) Kernel info for a single AC97 codec
 784 *
 785 *      Reset the AC97 codec, then initialize the mixer and
 786 *      the rest of the @codec structure.
 787 *
 788 *      The codec_read and codec_write fields of @codec are
 789 *      required to be setup and working when this function
 790 *      is called.  All other fields are set by this function.
 791 *
 792 *      codec_wait field of @codec can optionally be provided
 793 *      when calling this function.  If codec_wait is not %NULL,
 794 *      this function will call codec_wait any time it is
 795 *      necessary to wait for the audio chip to reach the
 796 *      codec-ready state.  If codec_wait is %NULL, then
 797 *      the default behavior is to call schedule_timeout.
 798 *      Currently codec_wait is used to wait for AC97 codec
 799 *      reset to complete. 
 800 *
 801 *      Returns 1 (true) on success, or 0 (false) on failure.
 802 */
 803 
 804int ac97_probe_codec(struct ac97_codec *codec)
 805{
 806        u16 id1, id2;
 807        u16 audio;
 808        int i;
 809        char cidbuf[CODEC_ID_BUFSZ];
 810        u16 f;
 811        struct list_head *l;
 812        struct ac97_driver *d;
 813        
 814        /* probing AC97 codec, AC97 2.0 says that bit 15 of register 0x00 (reset) should 
 815         * be read zero.
 816         *
 817         * FIXME: is the following comment outdated?  -jgarzik 
 818         * Probing of AC97 in this way is not reliable, it is not even SAFE !!
 819         */
 820        codec->codec_write(codec, AC97_RESET, 0L);
 821
 822        /* also according to spec, we wait for codec-ready state */     
 823        if (codec->codec_wait)
 824                codec->codec_wait(codec);
 825        else
 826                udelay(10);
 827
 828        if ((audio = codec->codec_read(codec, AC97_RESET)) & 0x8000) {
 829                printk(KERN_ERR "ac97_codec: %s ac97 codec not present\n",
 830                       (codec->id & 0x2) ? (codec->id&1 ? "4th" : "Tertiary") 
 831                       : (codec->id&1 ? "Secondary":  "Primary"));
 832                return 0;
 833        }
 834
 835        /* probe for Modem Codec */
 836        codec->modem = ac97_check_modem(codec);
 837        codec->name = NULL;
 838        codec->codec_ops = &default_ops;
 839
 840        id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
 841        id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
 842        for (i = 0; i < ARRAY_SIZE(ac97_codec_ids); i++) {
 843                if (ac97_codec_ids[i].id == ((id1 << 16) | id2)) {
 844                        codec->type = ac97_codec_ids[i].id;
 845                        codec->name = ac97_codec_ids[i].name;
 846                        codec->codec_ops = ac97_codec_ids[i].ops;
 847                        codec->flags = ac97_codec_ids[i].flags;
 848                        break;
 849                }
 850        }
 851
 852        codec->model = (id1 << 16) | id2;
 853        
 854        f = codec->codec_read(codec, AC97_EXTENDED_STATUS);
 855        if(f & 4)
 856                codec->codec_ops = &default_digital_ops;
 857        
 858        /* A device which thinks its a modem but isnt */
 859        if(codec->flags & AC97_DELUDED_MODEM)
 860                codec->modem = 0;
 861                
 862        if (codec->name == NULL)
 863                codec->name = "Unknown";
 864        printk(KERN_INFO "ac97_codec: AC97 %s codec, id: %s (%s)\n", 
 865                codec->modem ? "Modem" : (audio ? "Audio" : ""),
 866               codec_id(id1, id2, cidbuf), codec->name);
 867
 868        if(!ac97_init_mixer(codec))
 869                return 0;
 870                
 871        /* 
 872         *      Attach last so the caller can override the mixer
 873         *      callbacks.
 874         */
 875         
 876        down(&codec_sem);
 877        list_add(&codec->list, &codecs);
 878
 879        list_for_each(l, &codec_drivers) {
 880                d = list_entry(l, struct ac97_driver, list);
 881                if ((codec->model ^ d->codec_id) & d->codec_mask)
 882                        continue;
 883                if(d->probe(codec, d) == 0)
 884                {
 885                        codec->driver = d;
 886                        break;
 887                }
 888        }
 889
 890        up(&codec_sem);
 891        return 1;
 892}
 893
 894static int ac97_init_mixer(struct ac97_codec *codec)
 895{
 896        u16 cap;
 897        int i;
 898
 899        cap = codec->codec_read(codec, AC97_RESET);
 900
 901        /* mixer masks */
 902        codec->supported_mixers = AC97_SUPPORTED_MASK;
 903        codec->stereo_mixers = AC97_STEREO_MASK;
 904        codec->record_sources = AC97_RECORD_MASK;
 905        if (!(cap & 0x04))
 906                codec->supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
 907        if (!(cap & 0x10))
 908                codec->supported_mixers &= ~SOUND_MASK_ALTPCM;
 909
 910
 911        /* detect bit resolution */
 912        codec->codec_write(codec, AC97_MASTER_VOL_STEREO, 0x2020);
 913        if(codec->codec_read(codec, AC97_MASTER_VOL_STEREO) == 0x2020)
 914                codec->bit_resolution = 6;
 915        else
 916                codec->bit_resolution = 5;
 917
 918        /* generic OSS to AC97 wrapper */
 919        codec->read_mixer = ac97_read_mixer;
 920        codec->write_mixer = ac97_write_mixer;
 921        codec->recmask_io = ac97_recmask_io;
 922        codec->mixer_ioctl = ac97_mixer_ioctl;
 923
 924        /* codec specific initialization for 4-6 channel output or secondary codec stuff */
 925        if (codec->codec_ops->init != NULL) {
 926                codec->codec_ops->init(codec);
 927        }
 928
 929        /* initialize mixer channel volumes */
 930        for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
 931                struct mixer_defaults *md = &mixer_defaults[i];
 932                if (md->mixer == -1) 
 933                        break;
 934                if (!supported_mixer(codec, md->mixer)) 
 935                        continue;
 936                ac97_set_mixer(codec, md->mixer, md->value);
 937        }
 938
 939        /*
 940         *      Volume is MUTE only on this device. We have to initialise
 941         *      it but its useless beyond that.
 942         */
 943        if(codec->flags & AC97_NO_PCM_VOLUME)
 944        {
 945                codec->supported_mixers &= ~SOUND_MASK_PCM;
 946                printk(KERN_WARNING "AC97 codec does not have proper volume support.\n");
 947        }
 948        return 1;
 949}
 950
 951#define AC97_SIGMATEL_ANALOG    0x6c    /* Analog Special */
 952#define AC97_SIGMATEL_DAC2INVERT 0x6e
 953#define AC97_SIGMATEL_BIAS1     0x70
 954#define AC97_SIGMATEL_BIAS2     0x72
 955#define AC97_SIGMATEL_MULTICHN  0x74    /* Multi-Channel programming */
 956#define AC97_SIGMATEL_CIC1      0x76
 957#define AC97_SIGMATEL_CIC2      0x78
 958
 959
 960static int sigmatel_9708_init(struct ac97_codec * codec)
 961{
 962        u16 codec72, codec6c;
 963
 964        codec72 = codec->codec_read(codec, AC97_SIGMATEL_BIAS2) & 0x8000;
 965        codec6c = codec->codec_read(codec, AC97_SIGMATEL_ANALOG);
 966
 967        if ((codec72==0) && (codec6c==0)) {
 968                codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
 969                codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1000);
 970                codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
 971                codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0007);
 972        } else if ((codec72==0x8000) && (codec6c==0)) {
 973                codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
 974                codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1001);
 975                codec->codec_write(codec, AC97_SIGMATEL_DAC2INVERT, 0x0008);
 976        } else if ((codec72==0x8000) && (codec6c==0x0080)) {
 977                /* nothing */
 978        }
 979        codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
 980        return 0;
 981}
 982
 983
 984static int sigmatel_9721_init(struct ac97_codec * codec)
 985{
 986        /* Only set up secondary codec */
 987        if (codec->id == 0)
 988                return 0;
 989
 990        codec->codec_write(codec, AC97_SURROUND_MASTER, 0L);
 991
 992        /* initialize SigmaTel STAC9721/23 as secondary codec, decoding AC link
 993           sloc 3,4 = 0x01, slot 7,8 = 0x00, */
 994        codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x00);
 995
 996        /* we don't have the crystal when we are on an AMR card, so use
 997           BIT_CLK as our clock source. Write the magic word ABBA and read
 998           back to enable register 0x78 */
 999        codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
1000        codec->codec_read(codec, AC97_SIGMATEL_CIC1);
1001
1002        /* sync all the clocks*/
1003        codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x3802);
1004
1005        return 0;
1006}
1007
1008
1009static int sigmatel_9744_init(struct ac97_codec * codec)
1010{
1011        // patch for SigmaTel
1012        codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
1013        codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x0000); // is this correct? --jk
1014        codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
1015        codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0002);
1016        codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
1017        return 0;
1018}
1019
1020static int cmedia_init(struct ac97_codec *codec)
1021{
1022        /* Initialise the CMedia 9739 */
1023        /*
1024                We could set various options here
1025                Register 0x20 bit 0x100 sets mic as center bass
1026                Also do multi_channel_ctrl &=~0x3000 |=0x1000
1027                
1028                For now we set up the GPIO and PC beep 
1029        */
1030        
1031        u16 v;
1032        
1033        /* MIC */
1034        codec->codec_write(codec, 0x64, 0x3000);
1035        v = codec->codec_read(codec, 0x64);
1036        v &= ~0x8000;
1037        codec->codec_write(codec, 0x64, v);
1038        codec->codec_write(codec, 0x70, 0x0100);
1039        codec->codec_write(codec, 0x72, 0x0020);
1040        return 0;
1041}
1042        
1043#define AC97_WM97XX_FMIXER_VOL 0x72
1044#define AC97_WM97XX_RMIXER_VOL 0x74
1045#define AC97_WM97XX_TEST 0x5a
1046#define AC97_WM9704_RPCM_VOL 0x70
1047#define AC97_WM9711_OUT3VOL 0x16
1048
1049static int wolfson_init03(struct ac97_codec * codec)
1050{
1051        /* this is known to work for the ViewSonic ViewPad 1000 */
1052        codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
1053        codec->codec_write(codec, AC97_GENERAL_PURPOSE, 0x8000);
1054        return 0;
1055}
1056
1057static int wolfson_init04(struct ac97_codec * codec)
1058{
1059        codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
1060        codec->codec_write(codec, AC97_WM97XX_RMIXER_VOL, 0x0808);
1061
1062        // patch for DVD noise
1063        codec->codec_write(codec, AC97_WM97XX_TEST, 0x0200);
1064
1065        // init vol as PCM vol
1066        codec->codec_write(codec, AC97_WM9704_RPCM_VOL,
1067                codec->codec_read(codec, AC97_PCMOUT_VOL));
1068
1069        /* set rear surround volume */
1070        codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
1071        return 0;
1072}
1073
1074/* WM9705, WM9710 */
1075static int wolfson_init05(struct ac97_codec * codec)
1076{
1077        /* set front mixer volume */
1078        codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
1079        return 0;
1080}
1081
1082/* WM9711, WM9712 */
1083static int wolfson_init11(struct ac97_codec * codec)
1084{
1085        /* stop pop's during suspend/resume */
1086        codec->codec_write(codec, AC97_WM97XX_TEST,
1087                codec->codec_read(codec, AC97_WM97XX_TEST) & 0xffbf);
1088
1089        /* set out3 volume */
1090        codec->codec_write(codec, AC97_WM9711_OUT3VOL, 0x0808);
1091        return 0;
1092}
1093
1094static int tritech_init(struct ac97_codec * codec)
1095{
1096        codec->codec_write(codec, 0x26, 0x0300);
1097        codec->codec_write(codec, 0x26, 0x0000);
1098        codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
1099        codec->codec_write(codec, AC97_RESERVED_3A, 0x0000);
1100        return 0;
1101}
1102
1103
1104/* copied from drivers/sound/maestro.c */
1105static int tritech_maestro_init(struct ac97_codec * codec)
1106{
1107        /* no idea what this does */
1108        codec->codec_write(codec, 0x2A, 0x0001);
1109        codec->codec_write(codec, 0x2C, 0x0000);
1110        codec->codec_write(codec, 0x2C, 0XFFFF);
1111        return 0;
1112}
1113
1114
1115
1116/* 
1117 *      Presario700 workaround 
1118 *      for Jack Sense/SPDIF Register mis-setting causing
1119 *      no audible output
1120 *      by Santiago Nullo 04/05/2002
1121 */
1122
1123#define AC97_AD1886_JACK_SENSE 0x72
1124
1125static int ad1886_init(struct ac97_codec * codec)
1126{
1127        /* from AD1886 Specs */
1128        codec->codec_write(codec, AC97_AD1886_JACK_SENSE, 0x0010);
1129        return 0;
1130}
1131
1132
1133
1134
1135/*
1136 *      This is basically standard AC97. It should work as a default for
1137 *      almost all modern codecs. Note that some cards wire EAPD *backwards*
1138 *      That side of it is up to the card driver not us to cope with.
1139 *
1140 */
1141
1142static int eapd_control(struct ac97_codec * codec, int on)
1143{
1144        if(on)
1145                codec->codec_write(codec, AC97_POWER_CONTROL,
1146                        codec->codec_read(codec, AC97_POWER_CONTROL)|0x8000);
1147        else
1148                codec->codec_write(codec, AC97_POWER_CONTROL,
1149                        codec->codec_read(codec, AC97_POWER_CONTROL)&~0x8000);
1150        return 0;
1151}
1152
1153static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1154{
1155        u16 reg;
1156        
1157        reg = codec->codec_read(codec, AC97_SPDIF_CONTROL);
1158        
1159        switch(rate)
1160        {
1161                /* Off by default */
1162                default:
1163                case 0:
1164                        reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1165                        codec->codec_write(codec, AC97_EXTENDED_STATUS, (reg & ~AC97_EA_SPDIF));
1166                        if(rate == 0)
1167                                return 0;
1168                        return -EINVAL;
1169                case 1:
1170                        reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K;
1171                        break;
1172                case 2:
1173                        reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K;
1174                        break;
1175                case 3:
1176                        reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K;
1177                        break;
1178        }
1179        
1180        reg &= ~AC97_SC_CC_MASK;
1181        reg |= (mode & AUDIO_CCMASK) << 6;
1182        
1183        if(mode & AUDIO_DIGITAL)
1184                reg |= 2;
1185        if(mode & AUDIO_PRO)
1186                reg |= 1;
1187        if(mode & AUDIO_DRS)
1188                reg |= 0x4000;
1189
1190        codec->codec_write(codec, AC97_SPDIF_CONTROL, reg);
1191
1192        reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1193        reg &= (AC97_EA_SLOT_MASK);
1194        reg |= AC97_EA_VRA | AC97_EA_SPDIF | slots;
1195        codec->codec_write(codec, AC97_EXTENDED_STATUS, reg);
1196        
1197        reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1198        if(!(reg & 0x0400))
1199        {
1200                codec->codec_write(codec, AC97_EXTENDED_STATUS, reg & ~ AC97_EA_SPDIF);
1201                return -EINVAL;
1202        }
1203        return 0;
1204}
1205
1206/*
1207 *      Crystal digital audio control (CS4299)
1208 */
1209 
1210static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1211{
1212        u16 cv;
1213
1214        if(mode & AUDIO_DIGITAL)
1215                return -EINVAL;
1216                
1217        switch(rate)
1218        {
1219                case 0: cv = 0x0; break;        /* SPEN off */
1220                case 48000: cv = 0x8004; break; /* 48KHz digital */
1221                case 44100: cv = 0x8104; break; /* 44.1KHz digital */
1222                case 32768:                     /* 32Khz */
1223                default:
1224                        return -EINVAL;
1225        }
1226        codec->codec_write(codec, 0x68, cv);
1227        return 0;
1228}
1229
1230/*
1231 *      CMedia digital audio control
1232 *      Needs more work.
1233 */
1234 
1235static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1236{
1237        u16 cv;
1238
1239        if(mode & AUDIO_DIGITAL)
1240                return -EINVAL;
1241                
1242        switch(rate)
1243        {
1244                case 0:         cv = 0x0001; break;     /* SPEN off */
1245                case 48000:     cv = 0x0009; break;     /* 48KHz digital */
1246                default:
1247                        return -EINVAL;
1248        }
1249        codec->codec_write(codec, 0x2A, 0x05c4);
1250        codec->codec_write(codec, 0x6C, cv);
1251        
1252        /* Switch on mix to surround */
1253        cv = codec->codec_read(codec, 0x64);
1254        cv &= ~0x0200;
1255        if(mode)
1256                cv |= 0x0200;
1257        codec->codec_write(codec, 0x64, cv);
1258        return 0;
1259}
1260
1261
1262/* copied from drivers/sound/maestro.c */
1263#if 0  /* there has been 1 person on the planet with a pt101 that we
1264        know of.  If they care, they can put this back in :) */
1265static int pt101_init(struct ac97_codec * codec)
1266{
1267        printk(KERN_INFO "ac97_codec: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
1268        /* who knows.. */
1269        codec->codec_write(codec, 0x2A, 0x0001);
1270        codec->codec_write(codec, 0x2C, 0x0000);
1271        codec->codec_write(codec, 0x2C, 0xFFFF);
1272        codec->codec_write(codec, 0x10, 0x9F1F);
1273        codec->codec_write(codec, 0x12, 0x0808);
1274        codec->codec_write(codec, 0x14, 0x9F1F);
1275        codec->codec_write(codec, 0x16, 0x9F1F);
1276        codec->codec_write(codec, 0x18, 0x0404);
1277        codec->codec_write(codec, 0x1A, 0x0000);
1278        codec->codec_write(codec, 0x1C, 0x0000);
1279        codec->codec_write(codec, 0x02, 0x0404);
1280        codec->codec_write(codec, 0x04, 0x0808);
1281        codec->codec_write(codec, 0x0C, 0x801F);
1282        codec->codec_write(codec, 0x0E, 0x801F);
1283        return 0;
1284}
1285#endif
1286        
1287
1288EXPORT_SYMBOL(ac97_read_proc);
1289EXPORT_SYMBOL(ac97_probe_codec);
1290
1291/*
1292 *      AC97 library support routines
1293 */     
1294 
1295/**
1296 *      ac97_set_dac_rate       -       set codec rate adaption
1297 *      @codec: ac97 code
1298 *      @rate: rate in hertz
1299 *
1300 *      Set the DAC rate. Assumes the codec supports VRA. The caller is
1301 *      expected to have checked this little detail.
1302 */
1303 
1304unsigned int ac97_set_dac_rate(struct ac97_codec *codec, unsigned int rate)
1305{
1306        unsigned int new_rate = rate;
1307        u32 dacp;
1308        u32 mast_vol, phone_vol, mono_vol, pcm_vol;
1309        u32 mute_vol = 0x8000;  /* The mute volume? */
1310
1311        if(rate != codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE))
1312        {
1313                /* Mute several registers */
1314                mast_vol = codec->codec_read(codec, AC97_MASTER_VOL_STEREO);
1315                mono_vol = codec->codec_read(codec, AC97_MASTER_VOL_MONO);
1316                phone_vol = codec->codec_read(codec, AC97_HEADPHONE_VOL);
1317                pcm_vol = codec->codec_read(codec, AC97_PCMOUT_VOL);
1318                codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mute_vol);
1319                codec->codec_write(codec, AC97_MASTER_VOL_MONO, mute_vol);
1320                codec->codec_write(codec, AC97_HEADPHONE_VOL, mute_vol);
1321                codec->codec_write(codec, AC97_PCMOUT_VOL, mute_vol);
1322                
1323                /* Power down the DAC */
1324                dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
1325                codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0200);
1326                /* Load the rate and read the effective rate */
1327                codec->codec_write(codec, AC97_PCM_FRONT_DAC_RATE, rate);
1328                new_rate=codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE);
1329                /* Power it back up */
1330                codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
1331
1332                /* Restore volumes */
1333                codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mast_vol);
1334                codec->codec_write(codec, AC97_MASTER_VOL_MONO, mono_vol);
1335                codec->codec_write(codec, AC97_HEADPHONE_VOL, phone_vol);
1336                codec->codec_write(codec, AC97_PCMOUT_VOL, pcm_vol);
1337        }
1338        return new_rate;
1339}
1340
1341EXPORT_SYMBOL(ac97_set_dac_rate);
1342
1343/**
1344 *      ac97_set_adc_rate       -       set codec rate adaption
1345 *      @codec: ac97 code
1346 *      @rate: rate in hertz
1347 *
1348 *      Set the ADC rate. Assumes the codec supports VRA. The caller is
1349 *      expected to have checked this little detail.
1350 */
1351
1352unsigned int ac97_set_adc_rate(struct ac97_codec *codec, unsigned int rate)
1353{
1354        unsigned int new_rate = rate;
1355        u32 dacp;
1356
1357        if(rate != codec->codec_read(codec, AC97_PCM_LR_ADC_RATE))
1358        {
1359                /* Power down the ADC */
1360                dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
1361                codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0100);
1362                /* Load the rate and read the effective rate */
1363                codec->codec_write(codec, AC97_PCM_LR_ADC_RATE, rate);
1364                new_rate=codec->codec_read(codec, AC97_PCM_LR_ADC_RATE);
1365                /* Power it back up */
1366                codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
1367        }
1368        return new_rate;
1369}
1370
1371EXPORT_SYMBOL(ac97_set_adc_rate);
1372
1373int ac97_save_state(struct ac97_codec *codec)
1374{
1375        return 0;       
1376}
1377
1378EXPORT_SYMBOL(ac97_save_state);
1379
1380int ac97_restore_state(struct ac97_codec *codec)
1381{
1382        int i;
1383        unsigned int left, right, val;
1384
1385        for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1386                if (!supported_mixer(codec, i)) 
1387                        continue;
1388
1389                val = codec->mixer_state[i];
1390                right = val >> 8;
1391                left = val  & 0xff;
1392                codec->write_mixer(codec, i, left, right);
1393        }
1394        return 0;
1395}
1396
1397EXPORT_SYMBOL(ac97_restore_state);
1398
1399/**
1400 *      ac97_register_driver    -       register a codec helper
1401 *      @driver: Driver handler
1402 *
1403 *      Register a handler for codecs matching the codec id. The handler
1404 *      attach function is called for all present codecs and will be 
1405 *      called when new codecs are discovered.
1406 */
1407 
1408int ac97_register_driver(struct ac97_driver *driver)
1409{
1410        struct list_head *l;
1411        struct ac97_codec *c;
1412        
1413        down(&codec_sem);
1414        INIT_LIST_HEAD(&driver->list);
1415        list_add(&driver->list, &codec_drivers);
1416        
1417        list_for_each(l, &codecs)
1418        {
1419                c = list_entry(l, struct ac97_codec, list);
1420                if(c->driver != NULL || ((c->model ^ driver->codec_id) & driver->codec_mask))
1421                        continue;
1422                if(driver->probe(c, driver))
1423                        continue;
1424                c->driver = driver;
1425        }
1426        up(&codec_sem);
1427        return 0;
1428}
1429
1430EXPORT_SYMBOL_GPL(ac97_register_driver);
1431
1432/**
1433 *      ac97_unregister_driver  -       unregister a codec helper
1434 *      @driver: Driver handler
1435 *
1436 *      Unregister a handler for codecs matching the codec id. The handler
1437 *      remove function is called for all matching codecs.
1438 */
1439 
1440void ac97_unregister_driver(struct ac97_driver *driver)
1441{
1442        struct list_head *l;
1443        struct ac97_codec *c;
1444        
1445        down(&codec_sem);
1446        list_del_init(&driver->list);
1447
1448        list_for_each(l, &codecs)
1449        {
1450                c = list_entry(l, struct ac97_codec, list);
1451                if (c->driver == driver) {
1452                        driver->remove(c, driver);
1453                        c->driver = NULL;
1454                }
1455        }
1456        
1457        up(&codec_sem);
1458}
1459
1460EXPORT_SYMBOL_GPL(ac97_unregister_driver);
1461
1462static int swap_headphone(int remove_master)
1463{
1464        struct list_head *l;
1465        struct ac97_codec *c;
1466        
1467        if (remove_master) {
1468                down(&codec_sem);
1469                list_for_each(l, &codecs)
1470                {
1471                        c = list_entry(l, struct ac97_codec, list);
1472                        if (supported_mixer(c, SOUND_MIXER_PHONEOUT))
1473                                c->supported_mixers &= ~SOUND_MASK_PHONEOUT;
1474                }
1475                up(&codec_sem);
1476        } else
1477                ac97_hw[SOUND_MIXER_PHONEOUT].offset = AC97_MASTER_VOL_STEREO;
1478
1479        /* Scale values already match */
1480        ac97_hw[SOUND_MIXER_VOLUME].offset = AC97_MASTER_VOL_MONO;
1481        return 0;
1482}
1483
1484static int apply_quirk(int quirk)
1485{
1486        switch (quirk) {
1487        case AC97_TUNE_NONE:
1488                return 0;
1489        case AC97_TUNE_HP_ONLY:
1490                return swap_headphone(1);
1491        case AC97_TUNE_SWAP_HP:
1492                return swap_headphone(0);
1493        case AC97_TUNE_SWAP_SURROUND:
1494                return -ENOSYS; /* not yet implemented */
1495        case AC97_TUNE_AD_SHARING:
1496                return -ENOSYS; /* not yet implemented */
1497        case AC97_TUNE_ALC_JACK:
1498                return -ENOSYS; /* not yet implemented */
1499        }
1500        return -EINVAL;
1501}
1502
1503/**
1504 *      ac97_tune_hardware - tune up the hardware
1505 *      @pdev: pci_dev pointer
1506 *      @quirk: quirk list
1507 *      @override: explicit quirk value (overrides if not AC97_TUNE_DEFAULT)
1508 *
1509 *      Do some workaround for each pci device, such as renaming of the
1510 *      headphone (true line-out) control as "Master".
1511 *      The quirk-list must be terminated with a zero-filled entry.
1512 *
1513 *      Returns zero if successful, or a negative error code on failure.
1514 */
1515
1516int ac97_tune_hardware(struct pci_dev *pdev, struct ac97_quirk *quirk, int override)
1517{
1518        int result;
1519
1520        if (!quirk)
1521                return -EINVAL;
1522
1523        if (override != AC97_TUNE_DEFAULT) {
1524                result = apply_quirk(override);
1525                if (result < 0)
1526                        printk(KERN_ERR "applying quirk type %d failed (%d)\n", override, result);
1527                return result;
1528        }
1529
1530        for (; quirk->vendor; quirk++) {
1531                if (quirk->vendor != pdev->subsystem_vendor)
1532                        continue;
1533                if ((! quirk->mask && quirk->device == pdev->subsystem_device) ||
1534                    quirk->device == (quirk->mask & pdev->subsystem_device)) {
1535#ifdef DEBUG
1536                        printk("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, pdev->subsystem_device);
1537#endif
1538                        result = apply_quirk(quirk->type);
1539                        if (result < 0)
1540                                printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
1541                        return result;
1542                }
1543        }
1544        return 0;
1545}
1546
1547EXPORT_SYMBOL_GPL(ac97_tune_hardware);
1548
1549MODULE_LICENSE("GPL");
1550
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.