linux/sound/pci/via82xx.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *   ALSA driver for VIA VT82xx (South Bridge)
   4 *
   5 *   VT82C686A/B/C, VT8233A/C, VT8235
   6 *
   7 *      Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
   8 *                         Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
   9 *                    2002 Takashi Iwai <tiwai@suse.de>
  10 */
  11
  12/*
  13 * Changes:
  14 *
  15 * Dec. 19, 2002        Takashi Iwai <tiwai@suse.de>
  16 *      - use the DSX channels for the first pcm playback.
  17 *        (on VIA8233, 8233C and 8235 only)
  18 *        this will allow you play simultaneously up to 4 streams.
  19 *        multi-channel playback is assigned to the second device
  20 *        on these chips.
  21 *      - support the secondary capture (on VIA8233/C,8235)
  22 *      - SPDIF support
  23 *        the DSX3 channel can be used for SPDIF output.
  24 *        on VIA8233A, this channel is assigned to the second pcm
  25 *        playback.
  26 *        the card config of alsa-lib will assign the correct
  27 *        device for applications.
  28 *      - clean up the code, separate low-level initialization
  29 *        routines for each chipset.
  30 *
  31 * Sep. 26, 2005        Karsten Wiese <annabellesgarden@yahoo.de>
  32 *      - Optimize position calculation for the 823x chips. 
  33 */
  34
  35#include <linux/io.h>
  36#include <linux/delay.h>
  37#include <linux/interrupt.h>
  38#include <linux/init.h>
  39#include <linux/pci.h>
  40#include <linux/slab.h>
  41#include <linux/gameport.h>
  42#include <linux/module.h>
  43#include <sound/core.h>
  44#include <sound/pcm.h>
  45#include <sound/pcm_params.h>
  46#include <sound/info.h>
  47#include <sound/tlv.h>
  48#include <sound/ac97_codec.h>
  49#include <sound/mpu401.h>
  50#include <sound/initval.h>
  51
  52#if 0
  53#define POINTER_DEBUG
  54#endif
  55
  56MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  57MODULE_DESCRIPTION("VIA VT82xx audio");
  58MODULE_LICENSE("GPL");
  59MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
  60
  61#if IS_REACHABLE(CONFIG_GAMEPORT)
  62#define SUPPORT_JOYSTICK 1
  63#endif
  64
  65static int index = SNDRV_DEFAULT_IDX1;  /* Index 0-MAX */
  66static char *id = SNDRV_DEFAULT_STR1;   /* ID for this card */
  67static long mpu_port;
  68#ifdef SUPPORT_JOYSTICK
  69static bool joystick;
  70#endif
  71static int ac97_clock = 48000;
  72static char *ac97_quirk;
  73static int dxs_support;
  74static int dxs_init_volume = 31;
  75static int nodelay;
  76
  77module_param(index, int, 0444);
  78MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
  79module_param(id, charp, 0444);
  80MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
  81module_param_hw(mpu_port, long, ioport, 0444);
  82MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
  83#ifdef SUPPORT_JOYSTICK
  84module_param(joystick, bool, 0444);
  85MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
  86#endif
  87module_param(ac97_clock, int, 0444);
  88MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  89module_param(ac97_quirk, charp, 0444);
  90MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
  91module_param(dxs_support, int, 0444);
  92MODULE_PARM_DESC(dxs_support, "Support for DXS channels (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA, 5 = enable any sample rate)");
  93module_param(dxs_init_volume, int, 0644);
  94MODULE_PARM_DESC(dxs_init_volume, "initial DXS volume (0-31)");
  95module_param(nodelay, int, 0444);
  96MODULE_PARM_DESC(nodelay, "Disable 500ms init delay");
  97
  98/* just for backward compatibility */
  99static bool enable;
 100module_param(enable, bool, 0444);
 101
 102
 103/* revision numbers for via686 */
 104#define VIA_REV_686_A           0x10
 105#define VIA_REV_686_B           0x11
 106#define VIA_REV_686_C           0x12
 107#define VIA_REV_686_D           0x13
 108#define VIA_REV_686_E           0x14
 109#define VIA_REV_686_H           0x20
 110
 111/* revision numbers for via8233 */
 112#define VIA_REV_PRE_8233        0x10    /* not in market */
 113#define VIA_REV_8233C           0x20    /* 2 rec, 4 pb, 1 multi-pb */
 114#define VIA_REV_8233            0x30    /* 2 rec, 4 pb, 1 multi-pb, spdif */
 115#define VIA_REV_8233A           0x40    /* 1 rec, 1 multi-pb, spdf */
 116#define VIA_REV_8235            0x50    /* 2 rec, 4 pb, 1 multi-pb, spdif */
 117#define VIA_REV_8237            0x60
 118#define VIA_REV_8251            0x70
 119
 120/*
 121 *  Direct registers
 122 */
 123
 124#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
 125#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
 126
 127/* common offsets */
 128#define VIA_REG_OFFSET_STATUS           0x00    /* byte - channel status */
 129#define   VIA_REG_STAT_ACTIVE           0x80    /* RO */
 130#define   VIA8233_SHADOW_STAT_ACTIVE    0x08    /* RO */
 131#define   VIA_REG_STAT_PAUSED           0x40    /* RO */
 132#define   VIA_REG_STAT_TRIGGER_QUEUED   0x08    /* RO */
 133#define   VIA_REG_STAT_STOPPED          0x04    /* RWC */
 134#define   VIA_REG_STAT_EOL              0x02    /* RWC */
 135#define   VIA_REG_STAT_FLAG             0x01    /* RWC */
 136#define VIA_REG_OFFSET_CONTROL          0x01    /* byte - channel control */
 137#define   VIA_REG_CTRL_START            0x80    /* WO */
 138#define   VIA_REG_CTRL_TERMINATE        0x40    /* WO */
 139#define   VIA_REG_CTRL_AUTOSTART        0x20
 140#define   VIA_REG_CTRL_PAUSE            0x08    /* RW */
 141#define   VIA_REG_CTRL_INT_STOP         0x04            
 142#define   VIA_REG_CTRL_INT_EOL          0x02
 143#define   VIA_REG_CTRL_INT_FLAG         0x01
 144#define   VIA_REG_CTRL_RESET            0x01    /* RW - probably reset? undocumented */
 145#define   VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
 146#define VIA_REG_OFFSET_TYPE             0x02    /* byte - channel type (686 only) */
 147#define   VIA_REG_TYPE_AUTOSTART        0x80    /* RW - autostart at EOL */
 148#define   VIA_REG_TYPE_16BIT            0x20    /* RW */
 149#define   VIA_REG_TYPE_STEREO           0x10    /* RW */
 150#define   VIA_REG_TYPE_INT_LLINE        0x00
 151#define   VIA_REG_TYPE_INT_LSAMPLE      0x04
 152#define   VIA_REG_TYPE_INT_LESSONE      0x08
 153#define   VIA_REG_TYPE_INT_MASK         0x0c
 154#define   VIA_REG_TYPE_INT_EOL          0x02
 155#define   VIA_REG_TYPE_INT_FLAG         0x01
 156#define VIA_REG_OFFSET_TABLE_PTR        0x04    /* dword - channel table pointer */
 157#define VIA_REG_OFFSET_CURR_PTR         0x04    /* dword - channel current pointer */
 158#define VIA_REG_OFFSET_STOP_IDX         0x08    /* dword - stop index, channel type, sample rate */
 159#define   VIA8233_REG_TYPE_16BIT        0x00200000      /* RW */
 160#define   VIA8233_REG_TYPE_STEREO       0x00100000      /* RW */
 161#define VIA_REG_OFFSET_CURR_COUNT       0x0c    /* dword - channel current count (24 bit) */
 162#define VIA_REG_OFFSET_CURR_INDEX       0x0f    /* byte - channel current index (for via8233 only) */
 163
 164#define DEFINE_VIA_REGSET(name,val) \
 165enum {\
 166        VIA_REG_##name##_STATUS         = (val),\
 167        VIA_REG_##name##_CONTROL        = (val) + 0x01,\
 168        VIA_REG_##name##_TYPE           = (val) + 0x02,\
 169        VIA_REG_##name##_TABLE_PTR      = (val) + 0x04,\
 170        VIA_REG_##name##_CURR_PTR       = (val) + 0x04,\
 171        VIA_REG_##name##_STOP_IDX       = (val) + 0x08,\
 172        VIA_REG_##name##_CURR_COUNT     = (val) + 0x0c,\
 173}
 174
 175/* playback block */
 176DEFINE_VIA_REGSET(PLAYBACK, 0x00);
 177DEFINE_VIA_REGSET(CAPTURE, 0x10);
 178DEFINE_VIA_REGSET(FM, 0x20);
 179
 180/* AC'97 */
 181#define VIA_REG_AC97                    0x80    /* dword */
 182#define   VIA_REG_AC97_CODEC_ID_MASK    (3<<30)
 183#define   VIA_REG_AC97_CODEC_ID_SHIFT   30
 184#define   VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
 185#define   VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
 186#define   VIA_REG_AC97_SECONDARY_VALID  (1<<27)
 187#define   VIA_REG_AC97_PRIMARY_VALID    (1<<25)
 188#define   VIA_REG_AC97_BUSY             (1<<24)
 189#define   VIA_REG_AC97_READ             (1<<23)
 190#define   VIA_REG_AC97_CMD_SHIFT        16
 191#define   VIA_REG_AC97_CMD_MASK         0x7e
 192#define   VIA_REG_AC97_DATA_SHIFT       0
 193#define   VIA_REG_AC97_DATA_MASK        0xffff
 194
 195#define VIA_REG_SGD_SHADOW              0x84    /* dword */
 196/* via686 */
 197#define   VIA_REG_SGD_STAT_PB_FLAG      (1<<0)
 198#define   VIA_REG_SGD_STAT_CP_FLAG      (1<<1)
 199#define   VIA_REG_SGD_STAT_FM_FLAG      (1<<2)
 200#define   VIA_REG_SGD_STAT_PB_EOL       (1<<4)
 201#define   VIA_REG_SGD_STAT_CP_EOL       (1<<5)
 202#define   VIA_REG_SGD_STAT_FM_EOL       (1<<6)
 203#define   VIA_REG_SGD_STAT_PB_STOP      (1<<8)
 204#define   VIA_REG_SGD_STAT_CP_STOP      (1<<9)
 205#define   VIA_REG_SGD_STAT_FM_STOP      (1<<10)
 206#define   VIA_REG_SGD_STAT_PB_ACTIVE    (1<<12)
 207#define   VIA_REG_SGD_STAT_CP_ACTIVE    (1<<13)
 208#define   VIA_REG_SGD_STAT_FM_ACTIVE    (1<<14)
 209/* via8233 */
 210#define   VIA8233_REG_SGD_STAT_FLAG     (1<<0)
 211#define   VIA8233_REG_SGD_STAT_EOL      (1<<1)
 212#define   VIA8233_REG_SGD_STAT_STOP     (1<<2)
 213#define   VIA8233_REG_SGD_STAT_ACTIVE   (1<<3)
 214#define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
 215#define   VIA8233_REG_SGD_CHAN_SDX      0
 216#define   VIA8233_REG_SGD_CHAN_MULTI    4
 217#define   VIA8233_REG_SGD_CHAN_REC      6
 218#define   VIA8233_REG_SGD_CHAN_REC1     7
 219
 220#define VIA_REG_GPI_STATUS              0x88
 221#define VIA_REG_GPI_INTR                0x8c
 222
 223/* multi-channel and capture registers for via8233 */
 224DEFINE_VIA_REGSET(MULTPLAY, 0x40);
 225DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
 226
 227/* via8233-specific registers */
 228#define VIA_REG_OFS_PLAYBACK_VOLUME_L   0x02    /* byte */
 229#define VIA_REG_OFS_PLAYBACK_VOLUME_R   0x03    /* byte */
 230#define VIA_REG_OFS_MULTPLAY_FORMAT     0x02    /* byte - format and channels */
 231#define   VIA_REG_MULTPLAY_FMT_8BIT     0x00
 232#define   VIA_REG_MULTPLAY_FMT_16BIT    0x80
 233#define   VIA_REG_MULTPLAY_FMT_CH_MASK  0x70    /* # channels << 4 (valid = 1,2,4,6) */
 234#define VIA_REG_OFS_CAPTURE_FIFO        0x02    /* byte - bit 6 = fifo  enable */
 235#define   VIA_REG_CAPTURE_FIFO_ENABLE   0x40
 236
 237#define VIA_DXS_MAX_VOLUME              31      /* max. volume (attenuation) of reg 0x32/33 */
 238
 239#define VIA_REG_CAPTURE_CHANNEL         0x63    /* byte - input select */
 240#define   VIA_REG_CAPTURE_CHANNEL_MIC   0x4
 241#define   VIA_REG_CAPTURE_CHANNEL_LINE  0
 242#define   VIA_REG_CAPTURE_SELECT_CODEC  0x03    /* recording source codec (0 = primary) */
 243
 244#define VIA_TBL_BIT_FLAG        0x40000000
 245#define VIA_TBL_BIT_EOL         0x80000000
 246
 247/* pci space */
 248#define VIA_ACLINK_STAT         0x40
 249#define  VIA_ACLINK_C11_READY   0x20
 250#define  VIA_ACLINK_C10_READY   0x10
 251#define  VIA_ACLINK_C01_READY   0x04 /* secondary codec ready */
 252#define  VIA_ACLINK_LOWPOWER    0x02 /* low-power state */
 253#define  VIA_ACLINK_C00_READY   0x01 /* primary codec ready */
 254#define VIA_ACLINK_CTRL         0x41
 255#define  VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
 256#define  VIA_ACLINK_CTRL_RESET  0x40 /* 0: assert, 1: de-assert */
 257#define  VIA_ACLINK_CTRL_SYNC   0x20 /* 0: release SYNC, 1: force SYNC hi */
 258#define  VIA_ACLINK_CTRL_SDO    0x10 /* 0: release SDO, 1: force SDO hi */
 259#define  VIA_ACLINK_CTRL_VRA    0x08 /* 0: disable VRA, 1: enable VRA */
 260#define  VIA_ACLINK_CTRL_PCM    0x04 /* 0: disable PCM, 1: enable PCM */
 261#define  VIA_ACLINK_CTRL_FM     0x02 /* via686 only */
 262#define  VIA_ACLINK_CTRL_SB     0x01 /* via686 only */
 263#define  VIA_ACLINK_CTRL_INIT   (VIA_ACLINK_CTRL_ENABLE|\
 264                                 VIA_ACLINK_CTRL_RESET|\
 265                                 VIA_ACLINK_CTRL_PCM|\
 266                                 VIA_ACLINK_CTRL_VRA)
 267#define VIA_FUNC_ENABLE         0x42
 268#define  VIA_FUNC_MIDI_PNP      0x80 /* FIXME: it's 0x40 in the datasheet! */
 269#define  VIA_FUNC_MIDI_IRQMASK  0x40 /* FIXME: not documented! */
 270#define  VIA_FUNC_RX2C_WRITE    0x20
 271#define  VIA_FUNC_SB_FIFO_EMPTY 0x10
 272#define  VIA_FUNC_ENABLE_GAME   0x08
 273#define  VIA_FUNC_ENABLE_FM     0x04
 274#define  VIA_FUNC_ENABLE_MIDI   0x02
 275#define  VIA_FUNC_ENABLE_SB     0x01
 276#define VIA_PNP_CONTROL         0x43
 277#define VIA_FM_NMI_CTRL         0x48
 278#define VIA8233_VOLCHG_CTRL     0x48
 279#define VIA8233_SPDIF_CTRL      0x49
 280#define  VIA8233_SPDIF_DX3      0x08
 281#define  VIA8233_SPDIF_SLOT_MASK        0x03
 282#define  VIA8233_SPDIF_SLOT_1011        0x00
 283#define  VIA8233_SPDIF_SLOT_34          0x01
 284#define  VIA8233_SPDIF_SLOT_78          0x02
 285#define  VIA8233_SPDIF_SLOT_69          0x03
 286
 287/*
 288 */
 289
 290#define VIA_DXS_AUTO    0
 291#define VIA_DXS_ENABLE  1
 292#define VIA_DXS_DISABLE 2
 293#define VIA_DXS_48K     3
 294#define VIA_DXS_NO_VRA  4
 295#define VIA_DXS_SRC     5
 296
 297
 298/*
 299 * pcm stream
 300 */
 301
 302struct snd_via_sg_table {
 303        unsigned int offset;
 304        unsigned int size;
 305} ;
 306
 307#define VIA_TABLE_SIZE  255
 308#define VIA_MAX_BUFSIZE (1<<24)
 309
 310struct viadev {
 311        unsigned int reg_offset;
 312        unsigned long port;
 313        int direction;  /* playback = 0, capture = 1 */
 314        struct snd_pcm_substream *substream;
 315        int running;
 316        unsigned int tbl_entries; /* # descriptors */
 317        struct snd_dma_buffer table;
 318        struct snd_via_sg_table *idx_table;
 319        /* for recovery from the unexpected pointer */
 320        unsigned int lastpos;
 321        unsigned int fragsize;
 322        unsigned int bufsize;
 323        unsigned int bufsize2;
 324        int hwptr_done;         /* processed frame position in the buffer */
 325        int in_interrupt;
 326        int shadow_shift;
 327};
 328
 329
 330enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
 331enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
 332
 333#define VIA_MAX_DEVS    7       /* 4 playback, 1 multi, 2 capture */
 334
 335struct via_rate_lock {
 336        spinlock_t lock;
 337        int rate;
 338        int used;
 339};
 340
 341struct via82xx {
 342        int irq;
 343
 344        unsigned long port;
 345        struct resource *mpu_res;
 346        int chip_type;
 347        unsigned char revision;
 348
 349        unsigned char old_legacy;
 350        unsigned char old_legacy_cfg;
 351#ifdef CONFIG_PM_SLEEP
 352        unsigned char legacy_saved;
 353        unsigned char legacy_cfg_saved;
 354        unsigned char spdif_ctrl_saved;
 355        unsigned char capture_src_saved[2];
 356        unsigned int mpu_port_saved;
 357#endif
 358
 359        unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
 360        unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
 361
 362        unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
 363
 364        struct pci_dev *pci;
 365        struct snd_card *card;
 366
 367        unsigned int num_devs;
 368        unsigned int playback_devno, multi_devno, capture_devno;
 369        struct viadev devs[VIA_MAX_DEVS];
 370        struct via_rate_lock rates[2]; /* playback and capture */
 371        unsigned int dxs_fixed: 1;      /* DXS channel accepts only 48kHz */
 372        unsigned int no_vra: 1;         /* no need to set VRA on DXS channels */
 373        unsigned int dxs_src: 1;        /* use full SRC capabilities of DXS */
 374        unsigned int spdif_on: 1;       /* only spdif rates work to external DACs */
 375
 376        struct snd_pcm *pcms[2];
 377        struct snd_rawmidi *rmidi;
 378        struct snd_kcontrol *dxs_controls[4];
 379
 380        struct snd_ac97_bus *ac97_bus;
 381        struct snd_ac97 *ac97;
 382        unsigned int ac97_clock;
 383        unsigned int ac97_secondary;    /* secondary AC'97 codec is present */
 384
 385        spinlock_t reg_lock;
 386        struct snd_info_entry *proc_entry;
 387
 388#ifdef SUPPORT_JOYSTICK
 389        struct gameport *gameport;
 390#endif
 391};
 392
 393static const struct pci_device_id snd_via82xx_ids[] = {
 394        /* 0x1106, 0x3058 */
 395        { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_82C686_5), TYPE_CARD_VIA686, },    /* 686A */
 396        /* 0x1106, 0x3059 */
 397        { PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_8233_5), TYPE_CARD_VIA8233, },     /* VT8233 */
 398        { 0, }
 399};
 400
 401MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
 402
 403/*
 404 */
 405
 406/*
 407 * allocate and initialize the descriptor buffers
 408 * periods = number of periods
 409 * fragsize = period size in bytes
 410 */
 411static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
 412                           struct pci_dev *pci,
 413                           unsigned int periods, unsigned int fragsize)
 414{
 415        unsigned int i, idx, ofs, rest;
 416        struct via82xx *chip = snd_pcm_substream_chip(substream);
 417
 418        if (dev->table.area == NULL) {
 419                /* the start of each lists must be aligned to 8 bytes,
 420                 * but the kernel pages are much bigger, so we don't care
 421                 */
 422                if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
 423                                        PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
 424                                        &dev->table) < 0)
 425                        return -ENOMEM;
 426        }
 427        if (! dev->idx_table) {
 428                dev->idx_table = kmalloc_array(VIA_TABLE_SIZE,
 429                                               sizeof(*dev->idx_table),
 430                                               GFP_KERNEL);
 431                if (! dev->idx_table)
 432                        return -ENOMEM;
 433        }
 434
 435        /* fill the entries */
 436        idx = 0;
 437        ofs = 0;
 438        for (i = 0; i < periods; i++) {
 439                rest = fragsize;
 440                /* fill descriptors for a period.
 441                 * a period can be split to several descriptors if it's
 442                 * over page boundary.
 443                 */
 444                do {
 445                        unsigned int r;
 446                        unsigned int flag;
 447                        unsigned int addr;
 448
 449                        if (idx >= VIA_TABLE_SIZE) {
 450                                dev_err(&pci->dev, "too much table size!\n");
 451                                return -EINVAL;
 452                        }
 453                        addr = snd_pcm_sgbuf_get_addr(substream, ofs);
 454                        ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32(addr);
 455                        r = snd_pcm_sgbuf_get_chunk_size(substream, ofs, rest);
 456                        rest -= r;
 457                        if (! rest) {
 458                                if (i == periods - 1)
 459                                        flag = VIA_TBL_BIT_EOL; /* buffer boundary */
 460                                else
 461                                        flag = VIA_TBL_BIT_FLAG; /* period boundary */
 462                        } else
 463                                flag = 0; /* period continues to the next */
 464                        /*
 465                        dev_dbg(&pci->dev,
 466                                "tbl %d: at %d  size %d (rest %d)\n",
 467                                idx, ofs, r, rest);
 468                        */
 469                        ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
 470                        dev->idx_table[idx].offset = ofs;
 471                        dev->idx_table[idx].size = r;
 472                        ofs += r;
 473                        idx++;
 474                } while (rest > 0);
 475        }
 476        dev->tbl_entries = idx;
 477        dev->bufsize = periods * fragsize;
 478        dev->bufsize2 = dev->bufsize / 2;
 479        dev->fragsize = fragsize;
 480        return 0;
 481}
 482
 483
 484static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
 485                           struct pci_dev *pci)
 486{
 487        if (dev->table.area) {
 488                snd_dma_free_pages(&dev->table);
 489                dev->table.area = NULL;
 490        }
 491        kfree(dev->idx_table);
 492        dev->idx_table = NULL;
 493        return 0;
 494}
 495
 496/*
 497 *  Basic I/O
 498 */
 499
 500static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
 501{
 502        return inl(VIAREG(chip, AC97));
 503}
 504 
 505static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
 506{
 507        outl(val, VIAREG(chip, AC97));
 508}
 509 
 510static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
 511{
 512        unsigned int timeout = 1000;    /* 1ms */
 513        unsigned int val;
 514        
 515        while (timeout-- > 0) {
 516                udelay(1);
 517                if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
 518                        return val & 0xffff;
 519        }
 520        dev_err(chip->card->dev, "codec_ready: codec %i is not ready [0x%x]\n",
 521                   secondary, snd_via82xx_codec_xread(chip));
 522        return -EIO;
 523}
 524 
 525static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
 526{
 527        unsigned int timeout = 1000;    /* 1ms */
 528        unsigned int val, val1;
 529        unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
 530                                         VIA_REG_AC97_SECONDARY_VALID;
 531        
 532        while (timeout-- > 0) {
 533                val = snd_via82xx_codec_xread(chip);
 534                val1 = val & (VIA_REG_AC97_BUSY | stat);
 535                if (val1 == stat)
 536                        return val & 0xffff;
 537                udelay(1);
 538        }
 539        return -EIO;
 540}
 541 
 542static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
 543{
 544        struct via82xx *chip = ac97->private_data;
 545        int err;
 546        err = snd_via82xx_codec_ready(chip, ac97->num);
 547        /* here we need to wait fairly for long time.. */
 548        if (!nodelay)
 549                msleep(500);
 550}
 551
 552static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
 553                                    unsigned short reg,
 554                                    unsigned short val)
 555{
 556        struct via82xx *chip = ac97->private_data;
 557        unsigned int xval;
 558
 559        xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
 560        xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
 561        xval |= reg << VIA_REG_AC97_CMD_SHIFT;
 562        xval |= val << VIA_REG_AC97_DATA_SHIFT;
 563        snd_via82xx_codec_xwrite(chip, xval);
 564        snd_via82xx_codec_ready(chip, ac97->num);
 565}
 566
 567static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
 568{
 569        struct via82xx *chip = ac97->private_data;
 570        unsigned int xval, val = 0xffff;
 571        int again = 0;
 572
 573        xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
 574        xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
 575        xval |= VIA_REG_AC97_READ;
 576        xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
 577        while (1) {
 578                if (again++ > 3) {
 579                        dev_err(chip->card->dev,
 580                                "codec_read: codec %i is not valid [0x%x]\n",
 581                                   ac97->num, snd_via82xx_codec_xread(chip));
 582                        return 0xffff;
 583                }
 584                snd_via82xx_codec_xwrite(chip, xval);
 585                udelay (20);
 586                if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
 587                        udelay(25);
 588                        val = snd_via82xx_codec_xread(chip);
 589                        break;
 590                }
 591        }
 592        return val & 0xffff;
 593}
 594
 595static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
 596{
 597        outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
 598             VIADEV_REG(viadev, OFFSET_CONTROL));
 599        inb(VIADEV_REG(viadev, OFFSET_CONTROL));
 600        udelay(50);
 601        /* disable interrupts */
 602        outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
 603        /* clear interrupts */
 604        outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
 605        outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
 606        // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
 607        viadev->lastpos = 0;
 608        viadev->hwptr_done = 0;
 609}
 610
 611
 612/*
 613 *  Interrupt handler
 614 *  Used for 686 and 8233A
 615 */
 616static irqreturn_t snd_via686_interrupt(int irq, void *dev_id)
 617{
 618        struct via82xx *chip = dev_id;
 619        unsigned int status;
 620        unsigned int i;
 621
 622        status = inl(VIAREG(chip, SGD_SHADOW));
 623        if (! (status & chip->intr_mask)) {
 624                if (chip->rmidi)
 625                        /* check mpu401 interrupt */
 626                        return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
 627                return IRQ_NONE;
 628        }
 629
 630        /* check status for each stream */
 631        spin_lock(&chip->reg_lock);
 632        for (i = 0; i < chip->num_devs; i++) {
 633                struct viadev *viadev = &chip->devs[i];
 634                unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
 635                if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
 636                        continue;
 637                if (viadev->substream && viadev->running) {
 638                        /*
 639                         * Update hwptr_done based on 'period elapsed'
 640                         * interrupts. We'll use it, when the chip returns 0 
 641                         * for OFFSET_CURR_COUNT.
 642                         */
 643                        if (c_status & VIA_REG_STAT_EOL)
 644                                viadev->hwptr_done = 0;
 645                        else
 646                                viadev->hwptr_done += viadev->fragsize;
 647                        viadev->in_interrupt = c_status;
 648                        spin_unlock(&chip->reg_lock);
 649                        snd_pcm_period_elapsed(viadev->substream);
 650                        spin_lock(&chip->reg_lock);
 651                        viadev->in_interrupt = 0;
 652                }
 653                outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
 654        }
 655        spin_unlock(&chip->reg_lock);
 656        return IRQ_HANDLED;
 657}
 658
 659/*
 660 *  Interrupt handler
 661 */
 662static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id)
 663{
 664        struct via82xx *chip = dev_id;
 665        unsigned int status;
 666        unsigned int i;
 667        int irqreturn = 0;
 668
 669        /* check status for each stream */
 670        spin_lock(&chip->reg_lock);
 671        status = inl(VIAREG(chip, SGD_SHADOW));
 672
 673        for (i = 0; i < chip->num_devs; i++) {
 674                struct viadev *viadev = &chip->devs[i];
 675                struct snd_pcm_substream *substream;
 676                unsigned char c_status, shadow_status;
 677
 678                shadow_status = (status >> viadev->shadow_shift) &
 679                        (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
 680                         VIA_REG_STAT_FLAG);
 681                c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
 682                if (!c_status)
 683                        continue;
 684
 685                substream = viadev->substream;
 686                if (substream && viadev->running) {
 687                        /*
 688                         * Update hwptr_done based on 'period elapsed'
 689                         * interrupts. We'll use it, when the chip returns 0 
 690                         * for OFFSET_CURR_COUNT.
 691                         */
 692                        if (c_status & VIA_REG_STAT_EOL)
 693                                viadev->hwptr_done = 0;
 694                        else
 695                                viadev->hwptr_done += viadev->fragsize;
 696                        viadev->in_interrupt = c_status;
 697                        if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
 698                                viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
 699                        spin_unlock(&chip->reg_lock);
 700
 701                        snd_pcm_period_elapsed(substream);
 702
 703                        spin_lock(&chip->reg_lock);
 704                        viadev->in_interrupt = 0;
 705                }
 706                outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
 707                irqreturn = 1;
 708        }
 709        spin_unlock(&chip->reg_lock);
 710        return IRQ_RETVAL(irqreturn);
 711}
 712
 713/*
 714 *  PCM callbacks
 715 */
 716
 717/*
 718 * trigger callback
 719 */
 720static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 721{
 722        struct via82xx *chip = snd_pcm_substream_chip(substream);
 723        struct viadev *viadev = substream->runtime->private_data;
 724        unsigned char val;
 725
 726        if (chip->chip_type != TYPE_VIA686)
 727                val = VIA_REG_CTRL_INT;
 728        else
 729                val = 0;
 730        switch (cmd) {
 731        case SNDRV_PCM_TRIGGER_START:
 732        case SNDRV_PCM_TRIGGER_RESUME:
 733                val |= VIA_REG_CTRL_START;
 734                viadev->running = 1;
 735                break;
 736        case SNDRV_PCM_TRIGGER_STOP:
 737        case SNDRV_PCM_TRIGGER_SUSPEND:
 738                val = VIA_REG_CTRL_TERMINATE;
 739                viadev->running = 0;
 740                break;
 741        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 742                val |= VIA_REG_CTRL_PAUSE;
 743                viadev->running = 0;
 744                break;
 745        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 746                viadev->running = 1;
 747                break;
 748        default:
 749                return -EINVAL;
 750        }
 751        outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
 752        if (cmd == SNDRV_PCM_TRIGGER_STOP)
 753                snd_via82xx_channel_reset(chip, viadev);
 754        return 0;
 755}
 756
 757
 758/*
 759 * pointer callbacks
 760 */
 761
 762/*
 763 * calculate the linear position at the given sg-buffer index and the rest count
 764 */
 765
 766#define check_invalid_pos(viadev,pos) \
 767        ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
 768                                     viadev->lastpos < viadev->bufsize2))
 769
 770static inline unsigned int calc_linear_pos(struct via82xx *chip,
 771                                           struct viadev *viadev,
 772                                           unsigned int idx,
 773                                           unsigned int count)
 774{
 775        unsigned int size, base, res;
 776
 777        size = viadev->idx_table[idx].size;
 778        base = viadev->idx_table[idx].offset;
 779        res = base + size - count;
 780        if (res >= viadev->bufsize)
 781                res -= viadev->bufsize;
 782
 783        /* check the validity of the calculated position */
 784        if (size < count) {
 785                dev_dbg(chip->card->dev,
 786                        "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
 787                           (int)size, (int)count);
 788                res = viadev->lastpos;
 789        } else {
 790                if (! count) {
 791                        /* Some mobos report count = 0 on the DMA boundary,
 792                         * i.e. count = size indeed.
 793                         * Let's check whether this step is above the expected size.
 794                         */
 795                        int delta = res - viadev->lastpos;
 796                        if (delta < 0)
 797                                delta += viadev->bufsize;
 798                        if ((unsigned int)delta > viadev->fragsize)
 799                                res = base;
 800                }
 801                if (check_invalid_pos(viadev, res)) {
 802#ifdef POINTER_DEBUG
 803                        dev_dbg(chip->card->dev,
 804                                "fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n",
 805                                idx, viadev->tbl_entries,
 806                               viadev->lastpos, viadev->bufsize2,
 807                               viadev->idx_table[idx].offset,
 808                               viadev->idx_table[idx].size, count);
 809#endif
 810                        /* count register returns full size when end of buffer is reached */
 811                        res = base + size;
 812                        if (check_invalid_pos(viadev, res)) {
 813                                dev_dbg(chip->card->dev,
 814                                        "invalid via82xx_cur_ptr (2), using last valid pointer\n");
 815                                res = viadev->lastpos;
 816                        }
 817                }
 818        }
 819        return res;
 820}
 821
 822/*
 823 * get the current pointer on via686
 824 */
 825static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
 826{
 827        struct via82xx *chip = snd_pcm_substream_chip(substream);
 828        struct viadev *viadev = substream->runtime->private_data;
 829        unsigned int idx, ptr, count, res;
 830
 831        if (snd_BUG_ON(!viadev->tbl_entries))
 832                return 0;
 833        if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
 834                return 0;
 835
 836        spin_lock(&chip->reg_lock);
 837        count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
 838        /* The via686a does not have the current index register,
 839         * so we need to calculate the index from CURR_PTR.
 840         */
 841        ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
 842        if (ptr <= (unsigned int)viadev->table.addr)
 843                idx = 0;
 844        else /* CURR_PTR holds the address + 8 */
 845                idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
 846        res = calc_linear_pos(chip, viadev, idx, count);
 847        viadev->lastpos = res; /* remember the last position */
 848        spin_unlock(&chip->reg_lock);
 849
 850        return bytes_to_frames(substream->runtime, res);
 851}
 852
 853/*
 854 * get the current pointer on via823x
 855 */
 856static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
 857{
 858        struct via82xx *chip = snd_pcm_substream_chip(substream);
 859        struct viadev *viadev = substream->runtime->private_data;
 860        unsigned int idx, count, res;
 861        int status;
 862        
 863        if (snd_BUG_ON(!viadev->tbl_entries))
 864                return 0;
 865
 866        spin_lock(&chip->reg_lock);
 867        count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
 868        status = viadev->in_interrupt;
 869        if (!status)
 870                status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
 871
 872        /* An apparent bug in the 8251 is worked around by sending a 
 873         * REG_CTRL_START. */
 874        if (chip->revision == VIA_REV_8251 && (status & VIA_REG_STAT_EOL))
 875                snd_via82xx_pcm_trigger(substream, SNDRV_PCM_TRIGGER_START);
 876
 877        if (!(status & VIA_REG_STAT_ACTIVE)) {
 878                res = 0;
 879                goto unlock;
 880        }
 881        if (count & 0xffffff) {
 882                idx = count >> 24;
 883                if (idx >= viadev->tbl_entries) {
 884#ifdef POINTER_DEBUG
 885                        dev_dbg(chip->card->dev,
 886                                "fail: invalid idx = %i/%i\n", idx,
 887                               viadev->tbl_entries);
 888#endif
 889                        res = viadev->lastpos;
 890                } else {
 891                        count &= 0xffffff;
 892                        res = calc_linear_pos(chip, viadev, idx, count);
 893                }
 894        } else {
 895                res = viadev->hwptr_done;
 896                if (!viadev->in_interrupt) {
 897                        if (status & VIA_REG_STAT_EOL) {
 898                                res = 0;
 899                        } else
 900                                if (status & VIA_REG_STAT_FLAG) {
 901                                        res += viadev->fragsize;
 902                                }
 903                }
 904        }                           
 905unlock:
 906        viadev->lastpos = res;
 907        spin_unlock(&chip->reg_lock);
 908
 909        return bytes_to_frames(substream->runtime, res);
 910}
 911
 912
 913/*
 914 * hw_params callback:
 915 * allocate the buffer and build up the buffer description table
 916 */
 917static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
 918                                 struct snd_pcm_hw_params *hw_params)
 919{
 920        struct via82xx *chip = snd_pcm_substream_chip(substream);
 921        struct viadev *viadev = substream->runtime->private_data;
 922        int err;
 923
 924        err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
 925        if (err < 0)
 926                return err;
 927        err = build_via_table(viadev, substream, chip->pci,
 928                              params_periods(hw_params),
 929                              params_period_bytes(hw_params));
 930        if (err < 0)
 931                return err;
 932
 933        return 0;
 934}
 935
 936/*
 937 * hw_free callback:
 938 * clean up the buffer description table and release the buffer
 939 */
 940static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
 941{
 942        struct via82xx *chip = snd_pcm_substream_chip(substream);
 943        struct viadev *viadev = substream->runtime->private_data;
 944
 945        clean_via_table(viadev, substream, chip->pci);
 946        snd_pcm_lib_free_pages(substream);
 947        return 0;
 948}
 949
 950
 951/*
 952 * set up the table pointer
 953 */
 954static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
 955{
 956        snd_via82xx_codec_ready(chip, 0);
 957        outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
 958        udelay(20);
 959        snd_via82xx_codec_ready(chip, 0);
 960}
 961
 962/*
 963 * prepare callback for playback and capture on via686
 964 */
 965static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
 966                                struct snd_pcm_runtime *runtime)
 967{
 968        snd_via82xx_channel_reset(chip, viadev);
 969        /* this must be set after channel_reset */
 970        snd_via82xx_set_table_ptr(chip, viadev);
 971        outb(VIA_REG_TYPE_AUTOSTART |
 972             (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
 973             (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
 974             ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
 975             VIA_REG_TYPE_INT_EOL |
 976             VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
 977}
 978
 979static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
 980{
 981        struct via82xx *chip = snd_pcm_substream_chip(substream);
 982        struct viadev *viadev = substream->runtime->private_data;
 983        struct snd_pcm_runtime *runtime = substream->runtime;
 984
 985        snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
 986        snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
 987        via686_setup_format(chip, viadev, runtime);
 988        return 0;
 989}
 990
 991static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
 992{
 993        struct via82xx *chip = snd_pcm_substream_chip(substream);
 994        struct viadev *viadev = substream->runtime->private_data;
 995        struct snd_pcm_runtime *runtime = substream->runtime;
 996
 997        snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
 998        via686_setup_format(chip, viadev, runtime);
 999        return 0;
1000}
1001
1002/*
1003 * lock the current rate
1004 */
1005static int via_lock_rate(struct via_rate_lock *rec, int rate)
1006{
1007        int changed = 0;
1008
1009        spin_lock_irq(&rec->lock);
1010        if (rec->rate != rate) {
1011                if (rec->rate && rec->used > 1) /* already set */
1012                        changed = -EINVAL;
1013                else {
1014                        rec->rate = rate;
1015                        changed = 1;
1016                }
1017        }
1018        spin_unlock_irq(&rec->lock);
1019        return changed;
1020}
1021
1022/*
1023 * prepare callback for DSX playback on via823x
1024 */
1025static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
1026{
1027        struct via82xx *chip = snd_pcm_substream_chip(substream);
1028        struct viadev *viadev = substream->runtime->private_data;
1029        struct snd_pcm_runtime *runtime = substream->runtime;
1030        int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
1031        int rate_changed;
1032        u32 rbits;
1033
1034        if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
1035                return rate_changed;
1036        if (rate_changed)
1037                snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1038                                  chip->no_vra ? 48000 : runtime->rate);
1039        if (chip->spdif_on && viadev->reg_offset == 0x30)
1040                snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1041
1042        if (runtime->rate == 48000)
1043                rbits = 0xfffff;
1044        else
1045                rbits = (0x100000 / 48000) * runtime->rate +
1046                        ((0x100000 % 48000) * runtime->rate) / 48000;
1047        snd_BUG_ON(rbits & ~0xfffff);
1048        snd_via82xx_channel_reset(chip, viadev);
1049        snd_via82xx_set_table_ptr(chip, viadev);
1050        outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
1051             VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1052        outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
1053             VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
1054        outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1055             (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1056             rbits | /* rate */
1057             0xff000000,    /* STOP index is never reached */
1058             VIADEV_REG(viadev, OFFSET_STOP_IDX));
1059        udelay(20);
1060        snd_via82xx_codec_ready(chip, 0);
1061        return 0;
1062}
1063
1064/*
1065 * prepare callback for multi-channel playback on via823x
1066 */
1067static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
1068{
1069        struct via82xx *chip = snd_pcm_substream_chip(substream);
1070        struct viadev *viadev = substream->runtime->private_data;
1071        struct snd_pcm_runtime *runtime = substream->runtime;
1072        unsigned int slots;
1073        int fmt;
1074
1075        if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1076                return -EINVAL;
1077        snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1078        snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1079        snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1080        snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1081        snd_via82xx_channel_reset(chip, viadev);
1082        snd_via82xx_set_table_ptr(chip, viadev);
1083
1084        fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
1085                VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
1086        fmt |= runtime->channels << 4;
1087        outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1088#if 0
1089        if (chip->revision == VIA_REV_8233A)
1090                slots = 0;
1091        else
1092#endif
1093        {
1094                /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1095                /* corresponding to FL, FR, RL, RR, C, LFE ?? */
1096                switch (runtime->channels) {
1097                case 1: slots = (1<<0) | (1<<4); break;
1098                case 2: slots = (1<<0) | (2<<4); break;
1099                case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1100                case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1101                case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1102                case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1103                default: slots = 0; break;
1104                }
1105        }
1106        /* STOP index is never reached */
1107        outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1108        udelay(20);
1109        snd_via82xx_codec_ready(chip, 0);
1110        return 0;
1111}
1112
1113/*
1114 * prepare callback for capture on via823x
1115 */
1116static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
1117{
1118        struct via82xx *chip = snd_pcm_substream_chip(substream);
1119        struct viadev *viadev = substream->runtime->private_data;
1120        struct snd_pcm_runtime *runtime = substream->runtime;
1121
1122        if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1123                return -EINVAL;
1124        snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1125        snd_via82xx_channel_reset(chip, viadev);
1126        snd_via82xx_set_table_ptr(chip, viadev);
1127        outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1128        outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1129             (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1130             0xff000000,    /* STOP index is never reached */
1131             VIADEV_REG(viadev, OFFSET_STOP_IDX));
1132        udelay(20);
1133        snd_via82xx_codec_ready(chip, 0);
1134        return 0;
1135}
1136
1137
1138/*
1139 * pcm hardware definition, identical for both playback and capture
1140 */
1141static const struct snd_pcm_hardware snd_via82xx_hw =
1142{
1143        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1144                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1145                                 SNDRV_PCM_INFO_MMAP_VALID |
1146                                 /* SNDRV_PCM_INFO_RESUME | */
1147                                 SNDRV_PCM_INFO_PAUSE),
1148        .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1149        .rates =                SNDRV_PCM_RATE_48000,
1150        .rate_min =             48000,
1151        .rate_max =             48000,
1152        .channels_min =         1,
1153        .channels_max =         2,
1154        .buffer_bytes_max =     VIA_MAX_BUFSIZE,
1155        .period_bytes_min =     32,
1156        .period_bytes_max =     VIA_MAX_BUFSIZE / 2,
1157        .periods_min =          2,
1158        .periods_max =          VIA_TABLE_SIZE / 2,
1159        .fifo_size =            0,
1160};
1161
1162
1163/*
1164 * open callback skeleton
1165 */
1166static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
1167                                struct snd_pcm_substream *substream)
1168{
1169        struct snd_pcm_runtime *runtime = substream->runtime;
1170        int err;
1171        struct via_rate_lock *ratep;
1172        bool use_src = false;
1173
1174        runtime->hw = snd_via82xx_hw;
1175        
1176        /* set the hw rate condition */
1177        ratep = &chip->rates[viadev->direction];
1178        spin_lock_irq(&ratep->lock);
1179        ratep->used++;
1180        if (chip->spdif_on && viadev->reg_offset == 0x30) {
1181                /* DXS#3 and spdif is on */
1182                runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1183                snd_pcm_limit_hw_rates(runtime);
1184        } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1185                /* fixed DXS playback rate */
1186                runtime->hw.rates = SNDRV_PCM_RATE_48000;
1187                runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1188        } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1189                /* use full SRC capabilities of DXS */
1190                runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1191                                     SNDRV_PCM_RATE_8000_48000);
1192                runtime->hw.rate_min = 8000;
1193                runtime->hw.rate_max = 48000;
1194                use_src = true;
1195        } else if (! ratep->rate) {
1196                int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1197                runtime->hw.rates = chip->ac97->rates[idx];
1198                snd_pcm_limit_hw_rates(runtime);
1199        } else {
1200                /* a fixed rate */
1201                runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1202                runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1203        }
1204        spin_unlock_irq(&ratep->lock);
1205
1206        /* we may remove following constaint when we modify table entries
1207           in interrupt */
1208        if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1209                return err;
1210
1211        if (use_src) {
1212                err = snd_pcm_hw_rule_noresample(runtime, 48000);
1213                if (err < 0)
1214                        return err;
1215        }
1216
1217        runtime->private_data = viadev;
1218        viadev->substream = substream;
1219
1220        return 0;
1221}
1222
1223
1224/*
1225 * open callback for playback on via686
1226 */
1227static int snd_via686_playback_open(struct snd_pcm_substream *substream)
1228{
1229        struct via82xx *chip = snd_pcm_substream_chip(substream);
1230        struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
1231        int err;
1232
1233        if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1234                return err;
1235        return 0;
1236}
1237
1238/*
1239 * open callback for playback on via823x DXS
1240 */
1241static int snd_via8233_playback_open(struct snd_pcm_substream *substream)
1242{
1243        struct via82xx *chip = snd_pcm_substream_chip(substream);
1244        struct viadev *viadev;
1245        unsigned int stream;
1246        int err;
1247
1248        viadev = &chip->devs[chip->playback_devno + substream->number];
1249        if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1250                return err;
1251        stream = viadev->reg_offset / 0x10;
1252        if (chip->dxs_controls[stream]) {
1253                chip->playback_volume[stream][0] =
1254                                VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
1255                chip->playback_volume[stream][1] =
1256                                VIA_DXS_MAX_VOLUME - (dxs_init_volume & 31);
1257                chip->dxs_controls[stream]->vd[0].access &=
1258                        ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1259                snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1260                               SNDRV_CTL_EVENT_MASK_INFO,
1261                               &chip->dxs_controls[stream]->id);
1262        }
1263        return 0;
1264}
1265
1266/*
1267 * open callback for playback on via823x multi-channel
1268 */
1269static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
1270{
1271        struct via82xx *chip = snd_pcm_substream_chip(substream);
1272        struct viadev *viadev = &chip->devs[chip->multi_devno];
1273        int err;
1274        /* channels constraint for VIA8233A
1275         * 3 and 5 channels are not supported
1276         */
1277        static const unsigned int channels[] = {
1278                1, 2, 4, 6
1279        };
1280        static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
1281                .count = ARRAY_SIZE(channels),
1282                .list = channels,
1283                .mask = 0,
1284        };
1285
1286        if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1287                return err;
1288        substream->runtime->hw.channels_max = 6;
1289        if (chip->revision == VIA_REV_8233A)
1290                snd_pcm_hw_constraint_list(substream->runtime, 0,
1291                                           SNDRV_PCM_HW_PARAM_CHANNELS,
1292                                           &hw_constraints_channels);
1293        return 0;
1294}
1295
1296/*
1297 * open callback for capture on via686 and via823x
1298 */
1299static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
1300{
1301        struct via82xx *chip = snd_pcm_substream_chip(substream);
1302        struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
1303
1304        return snd_via82xx_pcm_open(chip, viadev, substream);
1305}
1306
1307/*
1308 * close callback
1309 */
1310static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
1311{
1312        struct via82xx *chip = snd_pcm_substream_chip(substream);
1313        struct viadev *viadev = substream->runtime->private_data;
1314        struct via_rate_lock *ratep;
1315
1316        /* release the rate lock */
1317        ratep = &chip->rates[viadev->direction];
1318        spin_lock_irq(&ratep->lock);
1319        ratep->used--;
1320        if (! ratep->used)
1321                ratep->rate = 0;
1322        spin_unlock_irq(&ratep->lock);
1323        if (! ratep->rate) {
1324                if (! viadev->direction) {
1325                        snd_ac97_update_power(chip->ac97,
1326                                              AC97_PCM_FRONT_DAC_RATE, 0);
1327                        snd_ac97_update_power(chip->ac97,
1328                                              AC97_PCM_SURR_DAC_RATE, 0);
1329                        snd_ac97_update_power(chip->ac97,
1330                                              AC97_PCM_LFE_DAC_RATE, 0);
1331                } else
1332                        snd_ac97_update_power(chip->ac97,
1333                                              AC97_PCM_LR_ADC_RATE, 0);
1334        }
1335        viadev->substream = NULL;
1336        return 0;
1337}
1338
1339static int snd_via8233_playback_close(struct snd_pcm_substream *substream)
1340{
1341        struct via82xx *chip = snd_pcm_substream_chip(substream);
1342        struct viadev *viadev = substream->runtime->private_data;
1343        unsigned int stream;
1344
1345        stream = viadev->reg_offset / 0x10;
1346        if (chip->dxs_controls[stream]) {
1347                chip->dxs_controls[stream]->vd[0].access |=
1348                        SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1349                snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1350                               &chip->dxs_controls[stream]->id);
1351        }
1352        return snd_via82xx_pcm_close(substream);
1353}
1354
1355
1356/* via686 playback callbacks */
1357static const struct snd_pcm_ops snd_via686_playback_ops = {
1358        .open =         snd_via686_playback_open,
1359        .close =        snd_via82xx_pcm_close,
1360        .ioctl =        snd_pcm_lib_ioctl,
1361        .hw_params =    snd_via82xx_hw_params,
1362        .hw_free =      snd_via82xx_hw_free,
1363        .prepare =      snd_via686_playback_prepare,
1364        .trigger =      snd_via82xx_pcm_trigger,
1365        .pointer =      snd_via686_pcm_pointer,
1366        .page =         snd_pcm_sgbuf_ops_page,
1367};
1368
1369/* via686 capture callbacks */
1370static const struct snd_pcm_ops snd_via686_capture_ops = {
1371        .open =         snd_via82xx_capture_open,
1372        .close =        snd_via82xx_pcm_close,
1373        .ioctl =        snd_pcm_lib_ioctl,
1374        .hw_params =    snd_via82xx_hw_params,
1375        .hw_free =      snd_via82xx_hw_free,
1376        .prepare =      snd_via686_capture_prepare,
1377        .trigger =      snd_via82xx_pcm_trigger,
1378        .pointer =      snd_via686_pcm_pointer,
1379        .page =         snd_pcm_sgbuf_ops_page,
1380};
1381
1382/* via823x DSX playback callbacks */
1383static const struct snd_pcm_ops snd_via8233_playback_ops = {
1384        .open =         snd_via8233_playback_open,
1385        .close =        snd_via8233_playback_close,
1386        .ioctl =        snd_pcm_lib_ioctl,
1387        .hw_params =    snd_via82xx_hw_params,
1388        .hw_free =      snd_via82xx_hw_free,
1389        .prepare =      snd_via8233_playback_prepare,
1390        .trigger =      snd_via82xx_pcm_trigger,
1391        .pointer =      snd_via8233_pcm_pointer,
1392        .page =         snd_pcm_sgbuf_ops_page,
1393};
1394
1395/* via823x multi-channel playback callbacks */
1396static const struct snd_pcm_ops snd_via8233_multi_ops = {
1397        .open =         snd_via8233_multi_open,
1398        .close =        snd_via82xx_pcm_close,
1399        .ioctl =        snd_pcm_lib_ioctl,
1400        .hw_params =    snd_via82xx_hw_params,
1401        .hw_free =      snd_via82xx_hw_free,
1402        .prepare =      snd_via8233_multi_prepare,
1403        .trigger =      snd_via82xx_pcm_trigger,
1404        .pointer =      snd_via8233_pcm_pointer,
1405        .page =         snd_pcm_sgbuf_ops_page,
1406};
1407
1408/* via823x capture callbacks */
1409static const struct snd_pcm_ops snd_via8233_capture_ops = {
1410        .open =         snd_via82xx_capture_open,
1411        .close =        snd_via82xx_pcm_close,
1412        .ioctl =        snd_pcm_lib_ioctl,
1413        .hw_params =    snd_via82xx_hw_params,
1414        .hw_free =      snd_via82xx_hw_free,
1415        .prepare =      snd_via8233_capture_prepare,
1416        .trigger =      snd_via82xx_pcm_trigger,
1417        .pointer =      snd_via8233_pcm_pointer,
1418        .page =         snd_pcm_sgbuf_ops_page,
1419};
1420
1421
1422static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
1423                        int shadow_pos, int direction)
1424{
1425        chip->devs[idx].reg_offset = reg_offset;
1426        chip->devs[idx].shadow_shift = shadow_pos * 4;
1427        chip->devs[idx].direction = direction;
1428        chip->devs[idx].port = chip->port + reg_offset;
1429}
1430
1431/*
1432 * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1433 */
1434static int snd_via8233_pcm_new(struct via82xx *chip)
1435{
1436        struct snd_pcm *pcm;
1437        struct snd_pcm_chmap *chmap;
1438        int i, err;
1439
1440        chip->playback_devno = 0;       /* x 4 */
1441        chip->multi_devno = 4;          /* x 1 */
1442        chip->capture_devno = 5;        /* x 2 */
1443        chip->num_devs = 7;
1444        chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1445
1446        /* PCM #0:  4 DSX playbacks and 1 capture */
1447        err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1448        if (err < 0)
1449                return err;
1450        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1451        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1452        pcm->private_data = chip;
1453        strcpy(pcm->name, chip->card->shortname);
1454        chip->pcms[0] = pcm;
1455        /* set up playbacks */
1456        for (i = 0; i < 4; i++)
1457                init_viadev(chip, i, 0x10 * i, i, 0);
1458        /* capture */
1459        init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1460
1461        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1462                                              snd_dma_pci_data(chip->pci),
1463                                              64*1024, VIA_MAX_BUFSIZE);
1464
1465        err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1466                                     snd_pcm_std_chmaps, 2, 0,
1467                                     &chmap);
1468        if (err < 0)
1469                return err;
1470
1471        /* PCM #1:  multi-channel playback and 2nd capture */
1472        err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1473        if (err < 0)
1474                return err;
1475        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1476        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1477        pcm->private_data = chip;
1478        strcpy(pcm->name, chip->card->shortname);
1479        chip->pcms[1] = pcm;
1480        /* set up playback */
1481        init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1482        /* set up capture */
1483        init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
1484
1485        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1486                                              snd_dma_pci_data(chip->pci),
1487                                              64*1024, VIA_MAX_BUFSIZE);
1488
1489        err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1490                                     snd_pcm_alt_chmaps, 6, 0,
1491                                     &chmap);
1492        if (err < 0)
1493                return err;
1494        chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1495
1496        return 0;
1497}
1498
1499/*
1500 * create pcm instances for VIA8233A
1501 */
1502static int snd_via8233a_pcm_new(struct via82xx *chip)
1503{
1504        struct snd_pcm *pcm;
1505        struct snd_pcm_chmap *chmap;
1506        int err;
1507
1508        chip->multi_devno = 0;
1509        chip->playback_devno = 1;
1510        chip->capture_devno = 2;
1511        chip->num_devs = 3;
1512        chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1513
1514        /* PCM #0:  multi-channel playback and capture */
1515        err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1516        if (err < 0)
1517                return err;
1518        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1519        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1520        pcm->private_data = chip;
1521        strcpy(pcm->name, chip->card->shortname);
1522        chip->pcms[0] = pcm;
1523        /* set up playback */
1524        init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1525        /* capture */
1526        init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1527
1528        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1529                                              snd_dma_pci_data(chip->pci),
1530                                              64*1024, VIA_MAX_BUFSIZE);
1531
1532        err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1533                                     snd_pcm_alt_chmaps, 6, 0,
1534                                     &chmap);
1535        if (err < 0)
1536                return err;
1537        chip->ac97->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1538
1539        /* SPDIF supported? */
1540        if (! ac97_can_spdif(chip->ac97))
1541                return 0;
1542
1543        /* PCM #1:  DXS3 playback (for spdif) */
1544        err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1545        if (err < 0)
1546                return err;
1547        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1548        pcm->private_data = chip;
1549        strcpy(pcm->name, chip->card->shortname);
1550        chip->pcms[1] = pcm;
1551        /* set up playback */
1552        init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
1553
1554        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1555                                              snd_dma_pci_data(chip->pci),
1556                                              64*1024, VIA_MAX_BUFSIZE);
1557        return 0;
1558}
1559
1560/*
1561 * create a pcm instance for via686a/b
1562 */
1563static int snd_via686_pcm_new(struct via82xx *chip)
1564{
1565        struct snd_pcm *pcm;
1566        int err;
1567
1568        chip->playback_devno = 0;
1569        chip->capture_devno = 1;
1570        chip->num_devs = 2;
1571        chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1572
1573        err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1574        if (err < 0)
1575                return err;
1576        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1577        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1578        pcm->private_data = chip;
1579        strcpy(pcm->name, chip->card->shortname);
1580        chip->pcms[0] = pcm;
1581        init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1582        init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
1583
1584        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1585                                              snd_dma_pci_data(chip->pci),
1586                                              64*1024, VIA_MAX_BUFSIZE);
1587        return 0;
1588}
1589
1590
1591/*
1592 *  Mixer part
1593 */
1594
1595static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
1596                                           struct snd_ctl_elem_info *uinfo)
1597{
1598        /* formerly they were "Line" and "Mic", but it looks like that they
1599         * have nothing to do with the actual physical connections...
1600         */
1601        static const char * const texts[2] = {
1602                "Input1", "Input2"
1603        };
1604        return snd_ctl_enum_info(uinfo, 1, 2, texts);
1605}
1606
1607static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
1608                                          struct snd_ctl_elem_value *ucontrol)
1609{
1610        struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1611        unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1612        ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1613        return 0;
1614}
1615
1616static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
1617                                          struct snd_ctl_elem_value *ucontrol)
1618{
1619        struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1620        unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1621        u8 val, oval;
1622
1623        spin_lock_irq(&chip->reg_lock);
1624        oval = inb(port);
1625        val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1626        if (ucontrol->value.enumerated.item[0])
1627                val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1628        if (val != oval)
1629                outb(val, port);
1630        spin_unlock_irq(&chip->reg_lock);
1631        return val != oval;
1632}
1633
1634static struct snd_kcontrol_new snd_via8233_capture_source = {
1635        .name = "Input Source Select",
1636        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1637        .info = snd_via8233_capture_source_info,
1638        .get = snd_via8233_capture_source_get,
1639        .put = snd_via8233_capture_source_put,
1640};
1641
1642#define snd_via8233_dxs3_spdif_info     snd_ctl_boolean_mono_info
1643
1644static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
1645                                      struct snd_ctl_elem_value *ucontrol)
1646{
1647        struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1648        u8 val;
1649
1650        pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1651        ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1652        return 0;
1653}
1654
1655static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
1656                                      struct snd_ctl_elem_value *ucontrol)
1657{
1658        struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1659        u8 val, oval;
1660
1661        pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1662        val = oval & ~VIA8233_SPDIF_DX3;
1663        if (ucontrol->value.integer.value[0])
1664                val |= VIA8233_SPDIF_DX3;
1665        /* save the spdif flag for rate filtering */
1666        chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1667        if (val != oval) {
1668                pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1669                return 1;
1670        }
1671        return 0;
1672}
1673
1674static const struct snd_kcontrol_new snd_via8233_dxs3_spdif_control = {
1675        .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
1676        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1677        .info = snd_via8233_dxs3_spdif_info,
1678        .get = snd_via8233_dxs3_spdif_get,
1679        .put = snd_via8233_dxs3_spdif_put,
1680};
1681
1682static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
1683                                       struct snd_ctl_elem_info *uinfo)
1684{
1685        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1686        uinfo->count = 2;
1687        uinfo->value.integer.min = 0;
1688        uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1689        return 0;
1690}
1691
1692static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
1693                                      struct snd_ctl_elem_value *ucontrol)
1694{
1695        struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1696        unsigned int idx = kcontrol->id.subdevice;
1697
1698        ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1699        ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1700        return 0;
1701}
1702
1703static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
1704                                         struct snd_ctl_elem_value *ucontrol)
1705{
1706        struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1707        ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1708        ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
1709        return 0;
1710}
1711
1712static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
1713                                      struct snd_ctl_elem_value *ucontrol)
1714{
1715        struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1716        unsigned int idx = kcontrol->id.subdevice;
1717        unsigned long port = chip->port + 0x10 * idx;
1718        unsigned char val;
1719        int i, change = 0;
1720
1721        for (i = 0; i < 2; i++) {
1722                val = ucontrol->value.integer.value[i];
1723                if (val > VIA_DXS_MAX_VOLUME)
1724                        val = VIA_DXS_MAX_VOLUME;
1725                val = VIA_DXS_MAX_VOLUME - val;
1726                change |= val != chip->playback_volume[idx][i];
1727                if (change) {
1728                        chip->playback_volume[idx][i] = val;
1729                        outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1730                }
1731        }
1732        return change;
1733}
1734
1735static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
1736                                         struct snd_ctl_elem_value *ucontrol)
1737{
1738        struct via82xx *chip = snd_kcontrol_chip(kcontrol);
1739        unsigned int idx;
1740        unsigned char val;
1741        int i, change = 0;
1742
1743        for (i = 0; i < 2; i++) {
1744                val = ucontrol->value.integer.value[i];
1745                if (val > VIA_DXS_MAX_VOLUME)
1746                        val = VIA_DXS_MAX_VOLUME;
1747                val = VIA_DXS_MAX_VOLUME - val;
1748                if (val != chip->playback_volume_c[i]) {
1749                        change = 1;
1750                        chip->playback_volume_c[i] = val;
1751                        for (idx = 0; idx < 4; idx++) {
1752                                unsigned long port = chip->port + 0x10 * idx;
1753                                chip->playback_volume[idx][i] = val;
1754                                outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1755                        }
1756                }
1757        }
1758        return change;
1759}
1760
1761static const DECLARE_TLV_DB_SCALE(db_scale_dxs, -4650, 150, 1);
1762
1763static const struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control = {
1764        .name = "PCM Playback Volume",
1765        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1766        .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1767                   SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1768        .info = snd_via8233_dxs_volume_info,
1769        .get = snd_via8233_pcmdxs_volume_get,
1770        .put = snd_via8233_pcmdxs_volume_put,
1771        .tlv = { .p = db_scale_dxs }
1772};
1773
1774static const struct snd_kcontrol_new snd_via8233_dxs_volume_control = {
1775        .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1776        .device = 0,
1777        /* .subdevice set later */
1778        .name = "PCM Playback Volume",
1779        .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1780                  SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1781                  SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1782        .info = snd_via8233_dxs_volume_info,
1783        .get = snd_via8233_dxs_volume_get,
1784        .put = snd_via8233_dxs_volume_put,
1785        .tlv = { .p = db_scale_dxs }
1786};
1787
1788/*
1789 */
1790
1791static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1792{
1793        struct via82xx *chip = bus->private_data;
1794        chip->ac97_bus = NULL;
1795}
1796
1797static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
1798{
1799        struct via82xx *chip = ac97->private_data;
1800        chip->ac97 = NULL;
1801}
1802
1803static const struct ac97_quirk ac97_quirks[] = {
1804        {
1805                .subvendor = 0x1106,
1806                .subdevice = 0x4161,
1807                .codec_id = 0x56494161, /* VT1612A */
1808                .name = "Soltek SL-75DRV5",
1809                .type = AC97_TUNE_NONE
1810        },
1811        {       /* FIXME: which codec? */
1812                .subvendor = 0x1106,
1813                .subdevice = 0x4161,
1814                .name = "ASRock K7VT2",
1815                .type = AC97_TUNE_HP_ONLY
1816        },
1817        {
1818                .subvendor = 0x110a,
1819                .subdevice = 0x0079,
1820                .name = "Fujitsu Siemens D1289",
1821                .type = AC97_TUNE_HP_ONLY
1822        },
1823        {
1824                .subvendor = 0x1019,
1825                .subdevice = 0x0a81,
1826                .name = "ECS K7VTA3",
1827                .type = AC97_TUNE_HP_ONLY
1828        },
1829        {
1830                .subvendor = 0x1019,
1831                .subdevice = 0x0a85,
1832                .name = "ECS L7VMM2",
1833                .type = AC97_TUNE_HP_ONLY
1834        },
1835        {
1836                .subvendor = 0x1019,
1837                .subdevice = 0x1841,
1838                .name = "ECS K7VTA3",
1839                .type = AC97_TUNE_HP_ONLY
1840        },
1841        {
1842                .subvendor = 0x1849,
1843                .subdevice = 0x3059,
1844                .name = "ASRock K7VM2",
1845                .type = AC97_TUNE_HP_ONLY       /* VT1616 */
1846        },
1847        {
1848                .subvendor = 0x14cd,
1849                .subdevice = 0x7002,
1850                .name = "Unknown",
1851                .type = AC97_TUNE_ALC_JACK
1852        },
1853        {
1854                .subvendor = 0x1071,
1855                .subdevice = 0x8590,
1856                .name = "Mitac Mobo",
1857                .type = AC97_TUNE_ALC_JACK
1858        },
1859        {
1860                .subvendor = 0x161f,
1861                .subdevice = 0x202b,
1862                .name = "Arima Notebook",
1863                .type = AC97_TUNE_HP_ONLY,
1864        },
1865        {
1866                .subvendor = 0x161f,
1867                .subdevice = 0x2032,
1868                .name = "Targa Traveller 811",
1869                .type = AC97_TUNE_HP_ONLY,
1870        },
1871        {
1872                .subvendor = 0x161f,
1873                .subdevice = 0x2032,
1874                .name = "m680x",
1875                .type = AC97_TUNE_HP_ONLY, /* http://launchpad.net/bugs/38546 */
1876        },
1877        {
1878                .subvendor = 0x1297,
1879                .subdevice = 0xa232,
1880                .name = "Shuttle AK32VN",
1881                .type = AC97_TUNE_HP_ONLY
1882        },
1883        { } /* terminator */
1884};
1885
1886static int snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
1887{
1888        struct snd_ac97_template ac97;
1889        int err;
1890        static struct snd_ac97_bus_ops ops = {
1891                .write = snd_via82xx_codec_write,
1892                .read = snd_via82xx_codec_read,
1893                .wait = snd_via82xx_codec_wait,
1894        };
1895
1896        if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1897                return err;
1898        chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1899        chip->ac97_bus->clock = chip->ac97_clock;
1900
1901        memset(&ac97, 0, sizeof(ac97));
1902        ac97.private_data = chip;
1903        ac97.private_free = snd_via82xx_mixer_free_ac97;
1904        ac97.pci = chip->pci;
1905        ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
1906        if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1907                return err;
1908
1909        snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1910
1911        if (chip->chip_type != TYPE_VIA686) {
1912                /* use slot 10/11 */
1913                snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1914        }
1915
1916        return 0;
1917}
1918
1919#ifdef SUPPORT_JOYSTICK
1920#define JOYSTICK_ADDR   0x200
1921static int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
1922{
1923        struct gameport *gp;
1924        struct resource *r;
1925
1926        if (!joystick)
1927                return -ENODEV;
1928
1929        r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
1930        if (!r) {
1931                dev_warn(chip->card->dev, "cannot reserve joystick port %#x\n",
1932                       JOYSTICK_ADDR);
1933                return -EBUSY;
1934        }
1935
1936        chip->gameport = gp = gameport_allocate_port();
1937        if (!gp) {
1938                dev_err(chip->card->dev,
1939                        "cannot allocate memory for gameport\n");
1940                release_and_free_resource(r);
1941                return -ENOMEM;
1942        }
1943
1944        gameport_set_name(gp, "VIA686 Gameport");
1945        gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1946        gameport_set_dev_parent(gp, &chip->pci->dev);
1947        gp->io = JOYSTICK_ADDR;
1948        gameport_set_port_data(gp, r);
1949
1950        /* Enable legacy joystick port */
1951        *legacy |= VIA_FUNC_ENABLE_GAME;
1952        pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1953
1954        gameport_register_port(chip->gameport);
1955
1956        return 0;
1957}
1958
1959static void snd_via686_free_gameport(struct via82xx *chip)
1960{
1961        if (chip->gameport) {
1962                struct resource *r = gameport_get_port_data(chip->gameport);
1963
1964                gameport_unregister_port(chip->gameport);
1965                chip->gameport = NULL;
1966                release_and_free_resource(r);
1967        }
1968}
1969#else
1970static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
1971{
1972        return -ENOSYS;
1973}
1974static inline void snd_via686_free_gameport(struct via82xx *chip) { }
1975#endif
1976
1977
1978/*
1979 *
1980 */
1981
1982static int snd_via8233_init_misc(struct via82xx *chip)
1983{
1984        int i, err, caps;
1985        unsigned char val;
1986
1987        caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1988        for (i = 0; i < caps; i++) {
1989                snd_via8233_capture_source.index = i;
1990                err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1991                if (err < 0)
1992                        return err;
1993        }
1994        if (ac97_can_spdif(chip->ac97)) {
1995                err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1996                if (err < 0)
1997                        return err;
1998        }
1999        if (chip->chip_type != TYPE_VIA8233A) {
2000                /* when no h/w PCM volume control is found, use DXS volume control
2001                 * as the PCM vol control
2002                 */
2003                struct snd_ctl_elem_id sid;
2004                memset(&sid, 0, sizeof(sid));
2005                strcpy(sid.name, "PCM Playback Volume");
2006                sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2007                if (! snd_ctl_find_id(chip->card, &sid)) {
2008                        dev_info(chip->card->dev,
2009                                 "Using DXS as PCM Playback\n");
2010                        err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
2011                        if (err < 0)
2012                                return err;
2013                }
2014                else /* Using DXS when PCM emulation is enabled is really weird */
2015                {
2016                        for (i = 0; i < 4; ++i) {
2017                                struct snd_kcontrol *kctl;
2018
2019                                kctl = snd_ctl_new1(
2020                                        &snd_via8233_dxs_volume_control, chip);
2021                                if (!kctl)
2022                                        return -ENOMEM;
2023                                kctl->id.subdevice = i;
2024                                err = snd_ctl_add(chip->card, kctl);
2025                                if (err < 0)
2026                                        return err;
2027                                chip->dxs_controls[i] = kctl;
2028                        }
2029                }
2030        }
2031        /* select spdif data slot 10/11 */
2032        pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
2033        val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
2034        val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
2035        pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
2036
2037        return 0;
2038}
2039
2040static int snd_via686_init_misc(struct via82xx *chip)
2041{
2042        unsigned char legacy, legacy_cfg;
2043        int rev_h = 0;
2044
2045        legacy = chip->old_legacy;
2046        legacy_cfg = chip->old_legacy_cfg;
2047        legacy |= VIA_FUNC_MIDI_IRQMASK;        /* FIXME: correct? (disable MIDI) */
2048        legacy &= ~VIA_FUNC_ENABLE_GAME;        /* disable joystick */
2049        if (chip->revision >= VIA_REV_686_H) {
2050                rev_h = 1;
2051                if (mpu_port >= 0x200) {        /* force MIDI */
2052                        mpu_port &= 0xfffc;
2053                        pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
2054#ifdef CONFIG_PM_SLEEP
2055                        chip->mpu_port_saved = mpu_port;
2056#endif
2057                } else {
2058                        mpu_port = pci_resource_start(chip->pci, 2);
2059                }
2060        } else {
2061                switch (mpu_port) {     /* force MIDI */
2062                case 0x300:
2063                case 0x310:
2064                case 0x320:
2065                case 0x330:
2066                        legacy_cfg &= ~(3 << 2);
2067                        legacy_cfg |= (mpu_port & 0x0030) >> 2;
2068                        break;
2069                default:                        /* no, use BIOS settings */
2070                        if (legacy & VIA_FUNC_ENABLE_MIDI)
2071                                mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
2072                        break;
2073                }
2074        }
2075        if (mpu_port >= 0x200 &&
2076            (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
2077            != NULL) {
2078                if (rev_h)
2079                        legacy |= VIA_FUNC_MIDI_PNP;    /* enable PCI I/O 2 */
2080                legacy |= VIA_FUNC_ENABLE_MIDI;
2081        } else {
2082                if (rev_h)
2083                        legacy &= ~VIA_FUNC_MIDI_PNP;   /* disable PCI I/O 2 */
2084                legacy &= ~VIA_FUNC_ENABLE_MIDI;
2085                mpu_port = 0;
2086        }
2087
2088        pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2089        pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
2090        if (chip->mpu_res) {
2091                if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
2092                                        mpu_port, MPU401_INFO_INTEGRATED |
2093                                        MPU401_INFO_IRQ_HOOK, -1,
2094                                        &chip->rmidi) < 0) {
2095                        dev_warn(chip->card->dev,
2096                                 "unable to initialize MPU-401 at 0x%lx, skipping\n",
2097                                 mpu_port);
2098                        legacy &= ~VIA_FUNC_ENABLE_MIDI;
2099                } else {
2100                        legacy &= ~VIA_FUNC_MIDI_IRQMASK;       /* enable MIDI interrupt */
2101                }
2102                pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
2103        }
2104
2105        snd_via686_create_gameport(chip, &legacy);
2106
2107#ifdef CONFIG_PM_SLEEP
2108        chip->legacy_saved = legacy;
2109        chip->legacy_cfg_saved = legacy_cfg;
2110#endif
2111
2112        return 0;
2113}
2114
2115
2116/*
2117 * proc interface
2118 */
2119static void snd_via82xx_proc_read(struct snd_info_entry *entry,
2120                                  struct snd_info_buffer *buffer)
2121{
2122        struct via82xx *chip = entry->private_data;
2123        int i;
2124        
2125        snd_iprintf(buffer, "%s\n\n", chip->card->longname);
2126        for (i = 0; i < 0xa0; i += 4) {
2127                snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
2128        }
2129}
2130
2131static void snd_via82xx_proc_init(struct via82xx *chip)
2132{
2133        snd_card_ro_proc_new(chip->card, "via82xx", chip,
2134                             snd_via82xx_proc_read);
2135}
2136
2137/*
2138 *
2139 */
2140
2141static int snd_via82xx_chip_init(struct via82xx *chip)
2142{
2143        unsigned int val;
2144        unsigned long end_time;
2145        unsigned char pval;
2146
2147#if 0 /* broken on K7M? */
2148        if (chip->chip_type == TYPE_VIA686)
2149                /* disable all legacy ports */
2150                pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
2151#endif
2152        pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2153        if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
2154                /* deassert ACLink reset, force SYNC */
2155                pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2156                                      VIA_ACLINK_CTRL_ENABLE |
2157                                      VIA_ACLINK_CTRL_RESET |
2158                                      VIA_ACLINK_CTRL_SYNC);
2159                udelay(100);
2160#if 1 /* FIXME: should we do full reset here for all chip models? */
2161                pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2162                udelay(100);
2163#else
2164                /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2165                pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2166                                      VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2167                udelay(2);
2168#endif
2169                /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2170                /* note - FM data out has trouble with non VRA codecs !! */
2171                pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2172                udelay(100);
2173        }
2174        
2175        /* Make sure VRA is enabled, in case we didn't do a
2176         * complete codec reset, above */
2177        pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2178        if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2179                /* ACLink on, deassert ACLink reset, VSR, SGD data out */
2180                /* note - FM data out has trouble with non VRA codecs !! */
2181                pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2182                udelay(100);
2183        }
2184
2185        /* wait until codec ready */
2186        end_time = jiffies + msecs_to_jiffies(750);
2187        do {
2188                pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2189                if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2190                        break;
2191                schedule_timeout_uninterruptible(1);
2192        } while (time_before(jiffies, end_time));
2193
2194        if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
2195                dev_err(chip->card->dev,
2196                        "AC'97 codec is not ready [0x%x]\n", val);
2197
2198#if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2199        snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2200                                 VIA_REG_AC97_SECONDARY_VALID |
2201                                 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2202        end_time = jiffies + msecs_to_jiffies(750);
2203        snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2204                                 VIA_REG_AC97_SECONDARY_VALID |
2205                                 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2206        do {
2207                if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
2208                        chip->ac97_secondary = 1;
2209                        goto __ac97_ok2;
2210                }
2211                schedule_timeout_uninterruptible(1);
2212        } while (time_before(jiffies, end_time));
2213        /* This is ok, the most of motherboards have only one codec */
2214
2215      __ac97_ok2:
2216#endif
2217
2218        if (chip->chip_type == TYPE_VIA686) {
2219                /* route FM trap to IRQ, disable FM trap */
2220                pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2221                /* disable all GPI interrupts */
2222                outl(0, VIAREG(chip, GPI_INTR));
2223        }
2224
2225        if (chip->chip_type != TYPE_VIA686) {
2226                /* Workaround for Award BIOS bug:
2227                 * DXS channels don't work properly with VRA if MC97 is disabled.
2228                 */
2229                struct pci_dev *pci;
2230                pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
2231                if (pci) {
2232                        unsigned char data;
2233                        pci_read_config_byte(pci, 0x44, &data);
2234                        pci_write_config_byte(pci, 0x44, data | 0x40);
2235                        pci_dev_put(pci);
2236                }
2237        }
2238
2239        if (chip->chip_type != TYPE_VIA8233A) {
2240                int i, idx;
2241                for (idx = 0; idx < 4; idx++) {
2242                        unsigned long port = chip->port + 0x10 * idx;
2243                        for (i = 0; i < 2; i++) {
2244                                chip->playback_volume[idx][i]=chip->playback_volume_c[i];
2245                                outb(chip->playback_volume_c[i],
2246                                     port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
2247                        }
2248                }
2249        }
2250
2251        return 0;
2252}
2253
2254#ifdef CONFIG_PM_SLEEP
2255/*
2256 * power management
2257 */
2258static int snd_via82xx_suspend(struct device *dev)
2259{
2260        struct snd_card *card = dev_get_drvdata(dev);
2261        struct via82xx *chip = card->private_data;
2262        int i;
2263
2264        snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2265        for (i = 0; i < chip->num_devs; i++)
2266                snd_via82xx_channel_reset(chip, &chip->devs[i]);
2267        synchronize_irq(chip->irq);
2268        snd_ac97_suspend(chip->ac97);
2269
2270        /* save misc values */
2271        if (chip->chip_type != TYPE_VIA686) {
2272                pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2273                chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2274                chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2275        }
2276
2277        return 0;
2278}
2279
2280static int snd_via82xx_resume(struct device *dev)
2281{
2282        struct snd_card *card = dev_get_drvdata(dev);
2283        struct via82xx *chip = card->private_data;
2284        int i;
2285
2286        snd_via82xx_chip_init(chip);
2287
2288        if (chip->chip_type == TYPE_VIA686) {
2289                if (chip->mpu_port_saved)
2290                        pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2291                pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2292                pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2293        } else {
2294                pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2295                outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2296                outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2297        }
2298
2299        snd_ac97_resume(chip->ac97);
2300
2301        for (i = 0; i < chip->num_devs; i++)
2302                snd_via82xx_channel_reset(chip, &chip->devs[i]);
2303
2304        snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2305        return 0;
2306}
2307
2308static SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
2309#define SND_VIA82XX_PM_OPS      &snd_via82xx_pm
2310#else
2311#define SND_VIA82XX_PM_OPS      NULL
2312#endif /* CONFIG_PM_SLEEP */
2313
2314static int snd_via82xx_free(struct via82xx *chip)
2315{
2316        unsigned int i;
2317
2318        if (chip->irq < 0)
2319                goto __end_hw;
2320        /* disable interrupts */
2321        for (i = 0; i < chip->num_devs; i++)
2322                snd_via82xx_channel_reset(chip, &chip->devs[i]);
2323
2324        if (chip->irq >= 0)
2325                free_irq(chip->irq, chip);
2326 __end_hw:
2327        release_and_free_resource(chip->mpu_res);
2328        pci_release_regions(chip->pci);
2329
2330        if (chip->chip_type == TYPE_VIA686) {
2331                snd_via686_free_gameport(chip);
2332                pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2333                pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2334        }
2335        pci_disable_device(chip->pci);
2336        kfree(chip);
2337        return 0;
2338}
2339
2340static int snd_via82xx_dev_free(struct snd_device *device)
2341{
2342        struct via82xx *chip = device->device_data;
2343        return snd_via82xx_free(chip);
2344}
2345
2346static int snd_via82xx_create(struct snd_card *card,
2347                              struct pci_dev *pci,
2348                              int chip_type,
2349                              int revision,
2350                              unsigned int ac97_clock,
2351                              struct via82xx **r_via)
2352{
2353        struct via82xx *chip;
2354        int err;
2355        static struct snd_device_ops ops = {
2356                .dev_free =     snd_via82xx_dev_free,
2357        };
2358
2359        if ((err = pci_enable_device(pci)) < 0)
2360                return err;
2361
2362        if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
2363                pci_disable_device(pci);
2364                return -ENOMEM;
2365        }
2366
2367        chip->chip_type = chip_type;
2368        chip->revision = revision;
2369
2370        spin_lock_init(&chip->reg_lock);
2371        spin_lock_init(&chip->rates[0].lock);
2372        spin_lock_init(&chip->rates[1].lock);
2373        chip->card = card;
2374        chip->pci = pci;
2375        chip->irq = -1;
2376
2377        pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2378        pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2379        pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2380                              chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2381
2382        if ((err = pci_request_regions(pci, card->driver)) < 0) {
2383                kfree(chip);
2384                pci_disable_device(pci);
2385                return err;
2386        }
2387        chip->port = pci_resource_start(pci, 0);
2388        if (request_irq(pci->irq,
2389                        chip_type == TYPE_VIA8233 ?
2390                        snd_via8233_interrupt : snd_via686_interrupt,
2391                        IRQF_SHARED,
2392                        KBUILD_MODNAME, chip)) {
2393                dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
2394                snd_via82xx_free(chip);
2395                return -EBUSY;
2396        }
2397        chip->irq = pci->irq;
2398        if (ac97_clock >= 8000 && ac97_clock <= 48000)
2399                chip->ac97_clock = ac97_clock;
2400        synchronize_irq(chip->irq);
2401
2402        if ((err = snd_via82xx_chip_init(chip)) < 0) {
2403                snd_via82xx_free(chip);
2404                return err;
2405        }
2406
2407        if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2408                snd_via82xx_free(chip);
2409                return err;
2410        }
2411
2412        /* The 8233 ac97 controller does not implement the master bit
2413         * in the pci command register. IMHO this is a violation of the PCI spec.
2414         * We call pci_set_master here because it does not hurt. */
2415        pci_set_master(pci);
2416
2417        *r_via = chip;
2418        return 0;
2419}
2420
2421struct via823x_info {
2422        int revision;
2423        char *name;
2424        int type;
2425};
2426static struct via823x_info via823x_cards[] = {
2427        { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2428        { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2429        { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2430        { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2431        { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2432        { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
2433        { VIA_REV_8251, "VIA 8251", TYPE_VIA8233 },
2434};
2435
2436/*
2437 * auto detection of DXS channel supports.
2438 */
2439
2440static struct snd_pci_quirk dxs_whitelist[] = {
2441        SND_PCI_QUIRK(0x1005, 0x4710, "Avance Logic Mobo", VIA_DXS_ENABLE),
2442        SND_PCI_QUIRK(0x1019, 0x0996, "ESC Mobo", VIA_DXS_48K),
2443        SND_PCI_QUIRK(0x1019, 0x0a81, "ECS K7VTA3 v8.0", VIA_DXS_NO_VRA),
2444        SND_PCI_QUIRK(0x1019, 0x0a85, "ECS L7VMM2", VIA_DXS_NO_VRA),
2445        SND_PCI_QUIRK_VENDOR(0x1019, "ESC K8", VIA_DXS_SRC),
2446        SND_PCI_QUIRK(0x1019, 0xaa01, "ESC K8T890-A", VIA_DXS_SRC),
2447        SND_PCI_QUIRK(0x1025, 0x0033, "Acer Inspire 1353LM", VIA_DXS_NO_VRA),
2448        SND_PCI_QUIRK(0x1025, 0x0046, "Acer Aspire 1524 WLMi", VIA_DXS_SRC),
2449        SND_PCI_QUIRK_VENDOR(0x1043, "ASUS A7/A8", VIA_DXS_NO_VRA),
2450        SND_PCI_QUIRK_VENDOR(0x1071, "Diverse Notebook", VIA_DXS_NO_VRA),
2451        SND_PCI_QUIRK(0x10cf, 0x118e, "FSC Laptop", VIA_DXS_ENABLE),
2452        SND_PCI_QUIRK_VENDOR(0x1106, "ASRock", VIA_DXS_SRC),
2453        SND_PCI_QUIRK(0x1297, 0xa231, "Shuttle AK31v2", VIA_DXS_SRC),
2454        SND_PCI_QUIRK(0x1297, 0xa232, "Shuttle", VIA_DXS_SRC),
2455        SND_PCI_QUIRK(0x1297, 0xc160, "Shuttle Sk41G", VIA_DXS_SRC),
2456        SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte GA-7VAXP", VIA_DXS_ENABLE),
2457        SND_PCI_QUIRK(0x1462, 0x3800, "MSI KT266", VIA_DXS_ENABLE),
2458        SND_PCI_QUIRK(0x1462, 0x7120, "MSI KT4V", VIA_DXS_ENABLE),
2459        SND_PCI_QUIRK(0x1462, 0x7142, "MSI K8MM-V", VIA_DXS_ENABLE),
2460        SND_PCI_QUIRK_VENDOR(0x1462, "MSI Mobo", VIA_DXS_SRC),
2461        SND_PCI_QUIRK(0x147b, 0x1401, "ABIT KD7(-RAID)", VIA_DXS_ENABLE),
2462        SND_PCI_QUIRK(0x147b, 0x1411, "ABIT VA-20", VIA_DXS_ENABLE),
2463        SND_PCI_QUIRK(0x147b, 0x1413, "ABIT KV8 Pro", VIA_DXS_ENABLE),
2464        SND_PCI_QUIRK(0x147b, 0x1415, "ABIT AV8", VIA_DXS_NO_VRA),
2465        SND_PCI_QUIRK(0x14ff, 0x0403, "Twinhead mobo", VIA_DXS_ENABLE),
2466        SND_PCI_QUIRK(0x14ff, 0x0408, "Twinhead laptop", VIA_DXS_SRC),
2467        SND_PCI_QUIRK(0x1558, 0x4701, "Clevo D470", VIA_DXS_SRC),
2468        SND_PCI_QUIRK(0x1584, 0x8120, "Diverse Laptop", VIA_DXS_ENABLE),
2469        SND_PCI_QUIRK(0x1584, 0x8123, "Targa/Uniwill", VIA_DXS_NO_VRA),
2470        SND_PCI_QUIRK(0x161f, 0x202b, "Amira Notebook", VIA_DXS_NO_VRA),
2471        SND_PCI_QUIRK(0x161f, 0x2032, "m680x machines", VIA_DXS_48K),
2472        SND_PCI_QUIRK(0x1631, 0xe004, "PB EasyNote 3174", VIA_DXS_ENABLE),
2473        SND_PCI_QUIRK(0x1695, 0x3005, "EPoX EP-8K9A", VIA_DXS_ENABLE),
2474        SND_PCI_QUIRK_VENDOR(0x1695, "EPoX mobo", VIA_DXS_SRC),
2475        SND_PCI_QUIRK_VENDOR(0x16f3, "Jetway K8", VIA_DXS_SRC),
2476        SND_PCI_QUIRK_VENDOR(0x1734, "FSC Laptop", VIA_DXS_SRC),
2477        SND_PCI_QUIRK(0x1849, 0x3059, "ASRock K7VM2", VIA_DXS_NO_VRA),
2478        SND_PCI_QUIRK_VENDOR(0x1849, "ASRock mobo", VIA_DXS_SRC),
2479        SND_PCI_QUIRK(0x1919, 0x200a, "Soltek SL-K8",  VIA_DXS_NO_VRA),
2480        SND_PCI_QUIRK(0x4005, 0x4710, "MSI K7T266", VIA_DXS_SRC),
2481        { } /* terminator */
2482};
2483
2484static int check_dxs_list(struct pci_dev *pci, int revision)
2485{
2486        const struct snd_pci_quirk *w;
2487
2488        w = snd_pci_quirk_lookup(pci, dxs_whitelist);
2489        if (w) {
2490                dev_dbg(&pci->dev, "DXS white list for %s found\n",
2491                            snd_pci_quirk_name(w));
2492                return w->value;
2493        }
2494
2495        /* for newer revision, default to DXS_SRC */
2496        if (revision >= VIA_REV_8235)
2497                return VIA_DXS_SRC;
2498
2499        /*
2500         * not detected, try 48k rate only to be sure.
2501         */
2502        dev_info(&pci->dev, "Assuming DXS channels with 48k fixed sample rate.\n");
2503        dev_info(&pci->dev, "         Please try dxs_support=5 option\n");
2504        dev_info(&pci->dev, "         and report if it works on your machine.\n");
2505        dev_info(&pci->dev, "         For more details, read ALSA-Configuration.txt.\n");
2506        return VIA_DXS_48K;
2507};
2508
2509static int snd_via82xx_probe(struct pci_dev *pci,
2510                             const struct pci_device_id *pci_id)
2511{
2512        struct snd_card *card;
2513        struct via82xx *chip;
2514        int chip_type = 0, card_type;
2515        unsigned int i;
2516        int err;
2517
2518        err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
2519        if (err < 0)
2520                return err;
2521
2522        card_type = pci_id->driver_data;
2523        switch (card_type) {
2524        case TYPE_CARD_VIA686:
2525                strcpy(card->driver, "VIA686A");
2526                sprintf(card->shortname, "VIA 82C686A/B rev%x", pci->revision);
2527                chip_type = TYPE_VIA686;
2528                break;
2529        case TYPE_CARD_VIA8233:
2530                chip_type = TYPE_VIA8233;
2531                sprintf(card->shortname, "VIA 823x rev%x", pci->revision);
2532                for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2533                        if (pci->revision == via823x_cards[i].revision) {
2534                                chip_type = via823x_cards[i].type;
2535                                strcpy(card->shortname, via823x_cards[i].name);
2536                                break;
2537                        }
2538                }
2539                if (chip_type != TYPE_VIA8233A) {
2540                        if (dxs_support == VIA_DXS_AUTO)
2541                                dxs_support = check_dxs_list(pci, pci->revision);
2542                        /* force to use VIA8233 or 8233A model according to
2543                         * dxs_support module option
2544                         */
2545                        if (dxs_support == VIA_DXS_DISABLE)
2546                                chip_type = TYPE_VIA8233A;
2547                        else
2548                                chip_type = TYPE_VIA8233;
2549                }
2550                if (chip_type == TYPE_VIA8233A)
2551                        strcpy(card->driver, "VIA8233A");
2552                else if (pci->revision >= VIA_REV_8237)
2553                        strcpy(card->driver, "VIA8237"); /* no slog assignment */
2554                else
2555                        strcpy(card->driver, "VIA8233");
2556                break;
2557        default:
2558                dev_err(card->dev, "invalid card type %d\n", card_type);
2559                err = -EINVAL;
2560                goto __error;
2561        }
2562                
2563        if ((err = snd_via82xx_create(card, pci, chip_type, pci->revision,
2564                                      ac97_clock, &chip)) < 0)
2565                goto __error;
2566        card->private_data = chip;
2567        if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
2568                goto __error;
2569
2570        if (chip_type == TYPE_VIA686) {
2571                if ((err = snd_via686_pcm_new(chip)) < 0 ||
2572                    (err = snd_via686_init_misc(chip)) < 0)
2573                        goto __error;
2574        } else {
2575                if (chip_type == TYPE_VIA8233A) {
2576                        if ((err = snd_via8233a_pcm_new(chip)) < 0)
2577                                goto __error;
2578                        // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2579                } else {
2580                        if ((err = snd_via8233_pcm_new(chip)) < 0)
2581                                goto __error;
2582                        if (dxs_support == VIA_DXS_48K)
2583                                chip->dxs_fixed = 1;
2584                        else if (dxs_support == VIA_DXS_NO_VRA)
2585                                chip->no_vra = 1;
2586                        else if (dxs_support == VIA_DXS_SRC) {
2587                                chip->no_vra = 1;
2588                                chip->dxs_src = 1;
2589                        }
2590                }
2591                if ((err = snd_via8233_init_misc(chip)) < 0)
2592                        goto __error;
2593        }
2594
2595        /* disable interrupts */
2596        for (i = 0; i < chip->num_devs; i++)
2597                snd_via82xx_channel_reset(chip, &chip->devs[i]);
2598
2599        snprintf(card->longname, sizeof(card->longname),
2600                 "%s with %s at %#lx, irq %d", card->shortname,
2601                 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2602
2603        snd_via82xx_proc_init(chip);
2604
2605        if ((err = snd_card_register(card)) < 0) {
2606                snd_card_free(card);
2607                return err;
2608        }
2609        pci_set_drvdata(pci, card);
2610        return 0;
2611
2612 __error:
2613        snd_card_free(card);
2614        return err;
2615}
2616
2617static void snd_via82xx_remove(struct pci_dev *pci)
2618{
2619        snd_card_free(pci_get_drvdata(pci));
2620}
2621
2622static struct pci_driver via82xx_driver = {
2623        .name = KBUILD_MODNAME,
2624        .id_table = snd_via82xx_ids,
2625        .probe = snd_via82xx_probe,
2626        .remove = snd_via82xx_remove,
2627        .driver = {
2628                .pm = SND_VIA82XX_PM_OPS,
2629        },
2630};
2631
2632module_pci_driver(via82xx_driver);
2633