linux-old/drivers/sound/cs46xx.c
<<
>>
Prefs
   1/*
   2 *      Crystal SoundFusion CS46xx driver
   3 *
   4 *      Copyright 1998-2001 Cirrus Logic Corporation <pcaudio@crystal.cirrus.com>
   5 *                                              <twoller@crystal.cirrus.com>
   6 *      Copyright 1999-2000 Jaroslav Kysela <perex@suse.cz>
   7 *      Copyright 2000 Alan Cox <alan@redhat.com>
   8 *
   9 *      The core of this code is taken from the ALSA project driver by 
  10 *      Jaroslav. Please send Jaroslav the credit for the driver and 
  11 *      report bugs in this port to <alan@redhat.com>
  12 *
  13 *      This program is free software; you can redistribute it and/or modify
  14 *      it under the terms of the GNU General Public License as published by
  15 *      the Free Software Foundation; either version 2 of the License, or
  16 *      (at your option) any later version.
  17 *
  18 *      This program is distributed in the hope that it will be useful,
  19 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 *      GNU General Public License for more details.
  22 *
  23 *      You should have received a copy of the GNU General Public License
  24 *      along with this program; if not, write to the Free Software
  25 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26 *      Current maintainers:
  27 *              Cirrus Logic Corporation, Thomas Woller (tw)
  28 *                      <twoller@crystal.cirrus.com>
  29 *              Nils Faerber (nf)
  30 *                      <nils@kernelconcepts.de>
  31 *              Thanks to David Pollard for testing.
  32 *
  33 *      Changes:
  34 *      20000909-nf     Changed cs_read, cs_write and drain_dac
  35 *      20001025-tw     Separate Playback/Capture structs and buffers.
  36 *                      Added Scatter/Gather support for Playback.
  37 *                      Added Capture.
  38 *      20001027-nf     Port to kernel 2.4.0-test9, some clean-ups
  39 *                      Start of powermanagement support (CS46XX_PM).
  40 *      20001128-tw     Add module parm for default buffer order.
  41 *                      added DMA_GFP flag to kmalloc dma buffer allocs.
  42 *                      backfill silence to eliminate stuttering on
  43 *                      underruns.
  44 *      20001201-tw     add resyncing of swptr on underruns.
  45 *      20001205-tw-nf  fixed GETOSPACE ioctl() after open()
  46 *      20010113-tw     patch from Hans Grobler general cleanup.
  47 *      20010117-tw     2.4.0 pci cleanup, wrapper code for 2.2.16-2.4.0
  48 *      20010118-tw     basic PM support for 2.2.16+ and 2.4.0/2.4.2.
  49 *      20010228-dh     patch from David Huggins - cs_update_ptr recursion.
  50 *      20010409-tw     add hercules game theatre XP amp code.
  51 *      20010420-tw     cleanup powerdown/up code.
  52 *      20010521-tw     eliminate pops, and fixes for powerdown.
  53 *      20010525-tw     added fixes for thinkpads with powerdown logic.
  54 *      20010723-sh     patch from Horms (Simon Horman) -
  55 *                      SOUND_PCM_READ_BITS returns bits as set in driver
  56 *                      rather than a logical or of the possible values.
  57 *                      Various ioctls handle the case where the device
  58 *                      is open for reading or writing but not both better.
  59 *
  60 *      Status:
  61 *      Playback/Capture supported from 8k-48k.
  62 *      16Bit Signed LE & 8Bit Unsigned, with Mono or Stereo supported.
  63 *
  64 *      APM/PM - 2.2.x APM is enabled and functioning fine. APM can also
  65 *      be enabled for 2.4.x by modifying the CS46XX_ACPI_SUPPORT macro
  66 *      definition.
  67 *
  68 *      Hercules Game Theatre XP - the EGPIO2 pin controls the external Amp,
  69 *      so, use the drain/polarity to enable.  
  70 *      hercules_egpio_disable set to 1, will force a 0 to EGPIODR.
  71 *
  72 *      VTB Santa Cruz - the GPIO7/GPIO8 on the Secondary Codec control
  73 *      the external amplifier for the "back" speakers, since we do not
  74 *      support the secondary codec then this external amp is also not
  75 *      turned on.
  76 */
  77 
  78#include <linux/list.h>
  79#include <linux/version.h>
  80#include <linux/module.h>
  81#include <linux/string.h>
  82#include <linux/ioport.h>
  83#include <linux/sched.h>
  84#include <linux/delay.h>
  85#include <linux/sound.h>
  86#include <linux/slab.h>
  87#include <linux/soundcard.h>
  88#include <linux/pci.h>
  89#include <linux/bitops.h>
  90#include <asm/io.h>
  91#include <asm/dma.h>
  92#include <linux/init.h>
  93#include <linux/poll.h>
  94#include <linux/smp_lock.h>
  95#include <linux/wrapper.h>
  96#include <asm/uaccess.h>
  97#include <asm/hardirq.h>
  98#include <linux/ac97_codec.h>
  99#include "cs46xxpm-24.h"
 100#include "cs46xx_wrapper-24.h"
 101
 102#include "cs461x.h"
 103
 104/* MIDI buffer sizes */
 105#define CS_MIDIINBUF  500
 106#define CS_MIDIOUTBUF 500
 107
 108#define ADC_RUNNING     1
 109#define DAC_RUNNING     2
 110
 111#define CS_FMT_16BIT    1               /* These are fixed in fact */
 112#define CS_FMT_STEREO   2
 113#define CS_FMT_MASK     3
 114
 115#define CS_TYPE_ADC     1
 116#define CS_TYPE_DAC     2
 117
 118#define CS_TRUE         1
 119#define CS_FALSE        0
 120
 121#define CS_INC_USE_COUNT(m) (atomic_inc(m))
 122#define CS_DEC_USE_COUNT(m) (atomic_dec(m))
 123#define CS_DEC_AND_TEST(m) (atomic_dec_and_test(m))
 124#define CS_IN_USE(m) (atomic_read(m) != 0)
 125
 126#define CS_DBGBREAKPOINT {__asm__("INT $3");}
 127/*
 128 *      CS461x definitions
 129 */
 130 
 131#define CS461X_BA0_SIZE         0x2000
 132#define CS461X_BA1_DATA0_SIZE   0x3000
 133#define CS461X_BA1_DATA1_SIZE   0x3800
 134#define CS461X_BA1_PRG_SIZE     0x7000
 135#define CS461X_BA1_REG_SIZE     0x0100
 136
 137#define GOF_PER_SEC     200
 138
 139#define CSDEBUG_INTERFACE 1
 140#define CSDEBUG 1
 141/*
 142 * Turn on/off debugging compilation by using 1/0 respectively for CSDEBUG
 143 *
 144 *
 145 * CSDEBUG is usual mode is set to 1, then use the
 146 * cs_debuglevel and cs_debugmask to turn on or off debugging.
 147 * Debug level of 1 has been defined to be kernel errors and info
 148 * that should be printed on any released driver.
 149 */
 150#if CSDEBUG
 151#define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask)) {x;} 
 152#else
 153#define CS_DBGOUT(mask,level,x) 
 154#endif
 155/*
 156 * cs_debugmask areas
 157 */
 158#define CS_INIT         0x00000001              /* initialization and probe functions */
 159#define CS_ERROR        0x00000002              /* tmp debugging bit placeholder */
 160#define CS_INTERRUPT    0x00000004              /* interrupt handler (separate from all other) */
 161#define CS_FUNCTION     0x00000008              /* enter/leave functions */
 162#define CS_WAVE_WRITE   0x00000010              /* write information for wave */
 163#define CS_WAVE_READ    0x00000020              /* read information for wave */
 164#define CS_MIDI_WRITE   0x00000040              /* write information for midi */
 165#define CS_MIDI_READ    0x00000080              /* read information for midi */
 166#define CS_MPU401_WRITE 0x00000100              /* write information for mpu401 */
 167#define CS_MPU401_READ  0x00000200              /* read information for mpu401 */
 168#define CS_OPEN         0x00000400              /* all open functions in the driver */
 169#define CS_RELEASE      0x00000800              /* all release functions in the driver */
 170#define CS_PARMS        0x00001000              /* functional and operational parameters */
 171#define CS_IOCTL        0x00002000              /* ioctl (non-mixer) */
 172#define CS_PM           0x00004000              /* PM */
 173#define CS_TMP          0x10000000              /* tmp debug mask bit */
 174
 175#define CS_IOCTL_CMD_SUSPEND    0x1     // suspend
 176#define CS_IOCTL_CMD_RESUME     0x2     // resume
 177
 178#if CSDEBUG
 179static unsigned long cs_debuglevel=1;                   /* levels range from 1-9 */
 180MODULE_PARM(cs_debuglevel, "i");
 181static unsigned long cs_debugmask=CS_INIT | CS_ERROR;   /* use CS_DBGOUT with various mask values */
 182MODULE_PARM(cs_debugmask, "i");
 183#endif
 184static unsigned long hercules_egpio_disable=0;  /* if non-zero set all EGPIO to 0 */
 185MODULE_PARM(hercules_egpio_disable, "i");
 186static unsigned long initdelay=700;  /* PM delay in millisecs */
 187MODULE_PARM(initdelay, "i");
 188static unsigned long powerdown=-1;  /* turn on/off powerdown processing in driver */
 189MODULE_PARM(powerdown, "i");
 190#define DMABUF_DEFAULTORDER 3
 191static unsigned long defaultorder=DMABUF_DEFAULTORDER;
 192MODULE_PARM(defaultorder, "i");
 193
 194static int external_amp;
 195MODULE_PARM(external_amp, "i");
 196static int thinkpad;
 197MODULE_PARM(thinkpad, "i");
 198
 199/*
 200* set the powerdown module parm to 0 to disable all 
 201* powerdown. also set thinkpad to 1 to disable powerdown, 
 202* but also to enable the clkrun functionality.
 203*/
 204static unsigned cs_powerdown=1;
 205static unsigned cs_laptop_wait=1;
 206
 207/* An instance of the 4610 channel */
 208struct cs_channel 
 209{
 210        int used;
 211        int num;
 212        void *state;
 213};
 214
 215#define CS46XX_MAJOR_VERSION "1"
 216#define CS46XX_MINOR_VERSION "28"
 217
 218#ifdef __ia64__
 219#define CS46XX_ARCH             "64"    //architecture key
 220#else
 221#define CS46XX_ARCH             "32"    //architecture key
 222#endif
 223
 224struct list_head cs46xx_devs = { &cs46xx_devs, &cs46xx_devs };
 225
 226/* magic numbers to protect our data structures */
 227#define CS_CARD_MAGIC           0x43525553 /* "CRUS" */
 228#define CS_STATE_MAGIC          0x4c4f4749 /* "LOGI" */
 229#define NR_HW_CH                3
 230
 231/* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
 232#define NR_AC97         2
 233
 234static const unsigned sample_size[] = { 1, 2, 2, 4 };
 235static const unsigned sample_shift[] = { 0, 1, 1, 2 };
 236
 237/* "software" or virtual channel, an instance of opened /dev/dsp */
 238struct cs_state {
 239        unsigned int magic;
 240        struct cs_card *card;   /* Card info */
 241
 242        /* single open lock mechanism, only used for recording */
 243        struct semaphore open_sem;
 244        wait_queue_head_t open_wait;
 245
 246        /* file mode */
 247        mode_t open_mode;
 248
 249        /* virtual channel number */
 250        int virt;
 251        
 252        struct dmabuf {
 253                /* wave sample stuff */
 254                unsigned int rate;
 255                unsigned char fmt, enable;
 256
 257                /* hardware channel */
 258                struct cs_channel *channel;
 259                int pringbuf;           /* Software ring slot */
 260                void *pbuf;             /* 4K hardware DMA buffer */
 261
 262                /* OSS buffer management stuff */
 263                void *rawbuf;
 264                dma_addr_t dma_handle;
 265                unsigned buforder;
 266                unsigned numfrag;
 267                unsigned fragshift;
 268                unsigned divisor;
 269                unsigned type;
 270                void *tmpbuff;                  /* tmp buffer for sample conversions */
 271                dma_addr_t dmaaddr;
 272                dma_addr_t dmaaddr_tmpbuff;
 273                unsigned buforder_tmpbuff;      /* Log base 2 of size in bytes.. */
 274
 275                /* our buffer acts like a circular ring */
 276                unsigned hwptr;         /* where dma last started, updated by update_ptr */
 277                unsigned swptr;         /* where driver last clear/filled, updated by read/write */
 278                int count;              /* bytes to be comsumed or been generated by dma machine */
 279                unsigned total_bytes;   /* total bytes dmaed by hardware */
 280                unsigned blocks;        /* total blocks */
 281
 282                unsigned error;         /* number of over/underruns */
 283                unsigned underrun;      /* underrun pending before next write has occurred */
 284                wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
 285
 286                /* redundant, but makes calculations easier */
 287                unsigned fragsize;
 288                unsigned dmasize;
 289                unsigned fragsamples;
 290
 291                /* OSS stuff */
 292                unsigned mapped:1;
 293                unsigned ready:1;
 294                unsigned endcleared:1;
 295                unsigned SGok:1;
 296                unsigned update_flag;
 297                unsigned ossfragshift;
 298                int ossmaxfrags;
 299                unsigned subdivision;
 300        } dmabuf;
 301        /* Guard against mmap/write/read races */
 302        struct semaphore sem;
 303};
 304
 305struct cs_card {
 306        struct cs_channel channel[2];
 307        unsigned int magic;
 308
 309        /* We keep cs461x cards in a linked list */
 310        struct cs_card *next;
 311
 312        /* The cs461x has a certain amount of cross channel interaction
 313           so we use a single per card lock */
 314        spinlock_t lock;
 315        
 316        /* Keep AC97 sane */
 317        spinlock_t ac97_lock;
 318
 319        /* mixer use count */
 320        atomic_t mixer_use_cnt;
 321
 322        /* PCI device stuff */
 323        struct pci_dev * pci_dev;
 324        struct list_head list;
 325
 326        unsigned int pctl, cctl;        /* Hardware DMA flag sets */
 327
 328        /* soundcore stuff */
 329        int dev_audio;
 330        int dev_midi;
 331
 332        /* structures for abstraction of hardware facilities, codecs, banks and channels*/
 333        struct ac97_codec *ac97_codec[NR_AC97];
 334        struct cs_state *states[2];
 335
 336        u16 ac97_features;
 337        
 338        int amplifier;                  /* Amplifier control */
 339        void (*amplifier_ctrl)(struct cs_card *, int);
 340        void (*amp_init)(struct cs_card *);
 341        
 342        int active;                     /* Active clocking */
 343        void (*active_ctrl)(struct cs_card *, int);
 344        
 345        /* hardware resources */
 346        unsigned long ba0_addr;
 347        unsigned long ba1_addr;
 348        u32 irq;
 349        
 350        /* mappings */
 351        void *ba0;
 352        union
 353        {
 354                struct
 355                {
 356                        u8 *data0;
 357                        u8 *data1;
 358                        u8 *pmem;
 359                        u8 *reg;
 360                } name;
 361                u8 *idx[4];
 362        } ba1;
 363        
 364        /* Function support */
 365        struct cs_channel *(*alloc_pcm_channel)(struct cs_card *);
 366        struct cs_channel *(*alloc_rec_pcm_channel)(struct cs_card *);
 367        void (*free_pcm_channel)(struct cs_card *, int chan);
 368
 369        /* /dev/midi stuff */
 370        struct {
 371                unsigned ird, iwr, icnt;
 372                unsigned ord, owr, ocnt;
 373                wait_queue_head_t open_wait;
 374                wait_queue_head_t iwait;
 375                wait_queue_head_t owait;
 376                spinlock_t lock;
 377                unsigned char ibuf[CS_MIDIINBUF];
 378                unsigned char obuf[CS_MIDIOUTBUF];
 379                mode_t open_mode;
 380                struct semaphore open_sem;
 381        } midi;
 382        struct cs46xx_pm pm;
 383};
 384
 385static int cs_open_mixdev(struct inode *inode, struct file *file);
 386static int cs_release_mixdev(struct inode *inode, struct file *file);
 387static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
 388                                unsigned long arg);
 389static int cs_hardware_init(struct cs_card *card);
 390static int cs46xx_powerup(struct cs_card *card, unsigned int type);
 391static int cs461x_powerdown(struct cs_card *card, unsigned int type, int suspendflag);
 392static void cs461x_clear_serial_FIFOs(struct cs_card *card, int type);
 393static int cs46xx_suspend_tbl(struct pci_dev *pcidev, u32 state);
 394static int cs46xx_resume_tbl(struct pci_dev *pcidev);
 395
 396static inline unsigned ld2(unsigned int x)
 397{
 398        unsigned r = 0;
 399        
 400        if (x >= 0x10000) {
 401                x >>= 16;
 402                r += 16;
 403        }
 404        if (x >= 0x100) {
 405                x >>= 8;
 406                r += 8;
 407        }
 408        if (x >= 0x10) {
 409                x >>= 4;
 410                r += 4;
 411        }
 412        if (x >= 4) {
 413                x >>= 2;
 414                r += 2;
 415        }
 416        if (x >= 2)
 417                r++;
 418        return r;
 419}
 420
 421#if CSDEBUG
 422
 423/* DEBUG ROUTINES */
 424
 425#define SOUND_MIXER_CS_GETDBGLEVEL      _SIOWR('M',120, int)
 426#define SOUND_MIXER_CS_SETDBGLEVEL      _SIOWR('M',121, int)
 427#define SOUND_MIXER_CS_GETDBGMASK       _SIOWR('M',122, int)
 428#define SOUND_MIXER_CS_SETDBGMASK       _SIOWR('M',123, int)
 429#define SOUND_MIXER_CS_APM              _SIOWR('M',124, int)
 430
 431void printioctl(unsigned int x)
 432{
 433    unsigned int i;
 434    unsigned char vidx;
 435        /* these values are incorrect for the ac97 driver, fix.
 436         * Index of mixtable1[] member is Device ID 
 437         * and must be <= SOUND_MIXER_NRDEVICES.
 438         * Value of array member is index into s->mix.vol[]
 439         */
 440        static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
 441                [SOUND_MIXER_PCM]     = 1,   /* voice */
 442                [SOUND_MIXER_LINE1]   = 2,   /* AUX */
 443                [SOUND_MIXER_CD]      = 3,   /* CD */
 444                [SOUND_MIXER_LINE]    = 4,   /* Line */
 445                [SOUND_MIXER_SYNTH]   = 5,   /* FM */
 446                [SOUND_MIXER_MIC]     = 6,   /* Mic */
 447                [SOUND_MIXER_SPEAKER] = 7,   /* Speaker */
 448                [SOUND_MIXER_RECLEV]  = 8,   /* Recording level */
 449                [SOUND_MIXER_VOLUME]  = 9    /* Master Volume */
 450        };
 451        
 452    switch(x) 
 453    {
 454        case SOUND_MIXER_CS_GETDBGMASK:
 455                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGMASK: ") );
 456                break;
 457        case SOUND_MIXER_CS_GETDBGLEVEL:
 458                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGLEVEL: ") );
 459                break;
 460        case SOUND_MIXER_CS_SETDBGMASK:
 461                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGMASK: ") );
 462                break;
 463        case SOUND_MIXER_CS_SETDBGLEVEL:
 464                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGLEVEL: ") );
 465                break;
 466        case OSS_GETVERSION:
 467                CS_DBGOUT(CS_IOCTL, 4, printk("OSS_GETVERSION: ") );
 468                break;
 469        case SNDCTL_DSP_SYNC:
 470                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SYNC: ") );
 471                break;
 472        case SNDCTL_DSP_SETDUPLEX:
 473                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETDUPLEX: ") );
 474                break;
 475        case SNDCTL_DSP_GETCAPS:
 476                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETCAPS: ") );
 477                break;
 478        case SNDCTL_DSP_RESET:
 479                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_RESET: ") );
 480                break;
 481        case SNDCTL_DSP_SPEED:
 482                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SPEED: ") );
 483                break;
 484        case SNDCTL_DSP_STEREO:
 485                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_STEREO: ") );
 486                break;
 487        case SNDCTL_DSP_CHANNELS:
 488                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CHANNELS: ") );
 489                break;
 490        case SNDCTL_DSP_GETFMTS: 
 491                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETFMTS: ") );
 492                break;
 493        case SNDCTL_DSP_SETFMT: 
 494                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFMT: ") );
 495                break;
 496        case SNDCTL_DSP_POST:
 497                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_POST: ") );
 498                break;
 499        case SNDCTL_DSP_GETTRIGGER:
 500                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETTRIGGER: ") );
 501                break;
 502        case SNDCTL_DSP_SETTRIGGER:
 503                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETTRIGGER: ") );
 504                break;
 505        case SNDCTL_DSP_GETOSPACE:
 506                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOSPACE: ") );
 507                break;
 508        case SNDCTL_DSP_GETISPACE:
 509                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETISPACE: ") );
 510                break;
 511        case SNDCTL_DSP_NONBLOCK:
 512                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_NONBLOCK: ") );
 513                break;
 514        case SNDCTL_DSP_GETODELAY:
 515                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETODELAY: ") );
 516                break;
 517        case SNDCTL_DSP_GETIPTR:
 518                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETIPTR: ") );
 519                break;
 520        case SNDCTL_DSP_GETOPTR:
 521                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOPTR: ") );
 522                break;
 523        case SNDCTL_DSP_GETBLKSIZE:
 524                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETBLKSIZE: ") );
 525                break;
 526        case SNDCTL_DSP_SETFRAGMENT:
 527                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFRAGMENT: ") );
 528                break;
 529        case SNDCTL_DSP_SUBDIVIDE:
 530                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SUBDIVIDE: ") );
 531                break;
 532        case SOUND_PCM_READ_RATE:
 533                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_RATE: ") );
 534                break;
 535        case SOUND_PCM_READ_CHANNELS:
 536                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_CHANNELS: ") );
 537                break;
 538        case SOUND_PCM_READ_BITS:
 539                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_BITS: ") );
 540                break;
 541        case SOUND_PCM_WRITE_FILTER:
 542                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_WRITE_FILTER: ") );
 543                break;
 544        case SNDCTL_DSP_SETSYNCRO:
 545                CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETSYNCRO: ") );
 546                break;
 547        case SOUND_PCM_READ_FILTER:
 548                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_FILTER: ") );
 549                break;
 550
 551        case SOUND_MIXER_PRIVATE1:
 552                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE1: ") );
 553                break;
 554        case SOUND_MIXER_PRIVATE2:
 555                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE2: ") );
 556                break;
 557        case SOUND_MIXER_PRIVATE3:
 558                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE3: ") );
 559                break;
 560        case SOUND_MIXER_PRIVATE4:
 561                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE4: ") );
 562                break;
 563        case SOUND_MIXER_PRIVATE5:
 564                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE5: ") );
 565                break;
 566        case SOUND_MIXER_INFO:
 567                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_INFO: ") );
 568                break;
 569        case SOUND_OLD_MIXER_INFO:
 570                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_OLD_MIXER_INFO: ") );
 571                break;
 572
 573        default:
 574                switch (_IOC_NR(x)) 
 575                {
 576                        case SOUND_MIXER_VOLUME:
 577                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_VOLUME: ") );
 578                                break;
 579                        case SOUND_MIXER_SPEAKER:
 580                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SPEAKER: ") );
 581                                break;
 582                        case SOUND_MIXER_RECLEV:
 583                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECLEV: ") );
 584                                break;
 585                        case SOUND_MIXER_MIC:
 586                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_MIC: ") );
 587                                break;
 588                        case SOUND_MIXER_SYNTH:
 589                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SYNTH: ") );
 590                                break;
 591                        case SOUND_MIXER_RECSRC: 
 592                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECSRC: ") );
 593                                break;
 594                        case SOUND_MIXER_DEVMASK:
 595                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_DEVMASK: ") );
 596                                break;
 597                        case SOUND_MIXER_RECMASK:
 598                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECMASK: ") );
 599                                break;
 600                        case SOUND_MIXER_STEREODEVS: 
 601                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_STEREODEVS: ") );
 602                                break;
 603                        case SOUND_MIXER_CAPS:
 604                                CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CAPS:") );
 605                                break;
 606                        default:
 607                                i = _IOC_NR(x);
 608                                if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
 609                                {
 610                                        CS_DBGOUT(CS_IOCTL, 4, printk("UNKNOWN IOCTL: 0x%.8x NR=%d ",x,i) );
 611                                }
 612                                else
 613                                {
 614                                        CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d ",
 615                                                        x,i) );
 616                                }
 617                                break;
 618                }
 619    }
 620    CS_DBGOUT(CS_IOCTL, 4, printk("command = 0x%x IOC_NR=%d\n",x, _IOC_NR(x)) );
 621}
 622#endif
 623
 624/*
 625 *  common I/O routines
 626 */
 627
 628static void cs461x_poke(struct cs_card *codec, unsigned long reg, unsigned int val)
 629{
 630        writel(val, codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
 631}
 632
 633static unsigned int cs461x_peek(struct cs_card *codec, unsigned long reg)
 634{
 635        return readl(codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
 636}
 637
 638static void cs461x_pokeBA0(struct cs_card *codec, unsigned long reg, unsigned int val)
 639{
 640        writel(val, codec->ba0+reg);
 641}
 642
 643static unsigned int cs461x_peekBA0(struct cs_card *codec, unsigned long reg)
 644{
 645        return readl(codec->ba0+reg);
 646}
 647
 648
 649static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg);
 650static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
 651
 652static struct cs_channel *cs_alloc_pcm_channel(struct cs_card *card)
 653{
 654        if(card->channel[1].used==1)
 655                return NULL;
 656        card->channel[1].used=1;
 657        card->channel[1].num=1;
 658        return &card->channel[1];
 659}
 660
 661static struct cs_channel *cs_alloc_rec_pcm_channel(struct cs_card *card)
 662{
 663        if(card->channel[0].used==1)
 664                return NULL;
 665        card->channel[0].used=1;
 666        card->channel[0].num=0;
 667        return &card->channel[0];
 668}
 669
 670static void cs_free_pcm_channel(struct cs_card *card, int channel)
 671{
 672        card->channel[channel].state = NULL;
 673        card->channel[channel].used=0;
 674}
 675
 676/*
 677 * setup a divisor value to help with conversion from
 678 * 16bit Stereo, down to 8bit stereo/mono or 16bit mono.
 679 * assign a divisor of 1 if using 16bit Stereo as that is
 680 * the only format that the static image will capture.
 681 */
 682static void cs_set_divisor(struct dmabuf *dmabuf)
 683{
 684        if(dmabuf->type == CS_TYPE_DAC)
 685                dmabuf->divisor = 1;
 686        else if( !(dmabuf->fmt & CS_FMT_STEREO) && 
 687            (dmabuf->fmt & CS_FMT_16BIT))
 688                dmabuf->divisor = 2;
 689        else if( (dmabuf->fmt & CS_FMT_STEREO) && 
 690            !(dmabuf->fmt & CS_FMT_16BIT))
 691                dmabuf->divisor = 2;
 692        else if( !(dmabuf->fmt & CS_FMT_STEREO) && 
 693            !(dmabuf->fmt & CS_FMT_16BIT))
 694                dmabuf->divisor = 4;
 695        else
 696                dmabuf->divisor = 1;
 697
 698        CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8, printk(
 699                "cs46xx: cs_set_divisor()- %s %d\n",
 700                        (dmabuf->type == CS_TYPE_ADC) ? "ADC" : "DAC", 
 701                        dmabuf->divisor) );
 702}
 703
 704/*
 705* mute some of the more prevalent registers to avoid popping.
 706*/
 707static void cs_mute(struct cs_card *card, int state) 
 708{
 709        struct ac97_codec *dev=card->ac97_codec[0];
 710
 711        CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()+ %s\n",
 712                (state == CS_TRUE) ? "Muting" : "UnMuting") );
 713
 714        if(state == CS_TRUE)
 715        {
 716        /*
 717        * fix pops when powering up on thinkpads
 718        */
 719                card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev, 
 720                                (u8)BA0_AC97_MASTER_VOLUME); 
 721                card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev, 
 722                                (u8)BA0_AC97_HEADPHONE_VOLUME); 
 723                card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev, 
 724                                (u8)BA0_AC97_MASTER_VOLUME_MONO); 
 725                card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev, 
 726                                (u8)BA0_AC97_PCM_OUT_VOLUME);
 727                        
 728                cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
 729                cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
 730                cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
 731                cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);
 732        }
 733        else
 734        {
 735                cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, card->pm.u32AC97_master_volume);
 736                cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, card->pm.u32AC97_headphone_volume);
 737                cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, card->pm.u32AC97_master_volume_mono);
 738                cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, card->pm.u32AC97_pcm_out_volume);
 739        }
 740        CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()-\n"));
 741}
 742
 743/* set playback sample rate */
 744static unsigned int cs_set_dac_rate(struct cs_state * state, unsigned int rate)
 745{       
 746        struct dmabuf *dmabuf = &state->dmabuf;
 747        unsigned int tmp1, tmp2;
 748        unsigned int phiIncr;
 749        unsigned int correctionPerGOF, correctionPerSec;
 750        unsigned long flags;
 751
 752        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()+ %d\n",rate) );
 753
 754        /*
 755         *  Compute the values used to drive the actual sample rate conversion.
 756         *  The following formulas are being computed, using inline assembly
 757         *  since we need to use 64 bit arithmetic to compute the values:
 758         *
 759         *  phiIncr = floor((Fs,in * 2^26) / Fs,out)
 760         *  correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
 761         *                                   GOF_PER_SEC)
 762         *  ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
 763         *                       GOF_PER_SEC * correctionPerGOF
 764         *
 765         *  i.e.
 766         *
 767         *  phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
 768         *  correctionPerGOF:correctionPerSec =
 769         *      dividend:remainder(ulOther / GOF_PER_SEC)
 770         */
 771        tmp1 = rate << 16;
 772        phiIncr = tmp1 / 48000;
 773        tmp1 -= phiIncr * 48000;
 774        tmp1 <<= 10;
 775        phiIncr <<= 10;
 776        tmp2 = tmp1 / 48000;
 777        phiIncr += tmp2;
 778        tmp1 -= tmp2 * 48000;
 779        correctionPerGOF = tmp1 / GOF_PER_SEC;
 780        tmp1 -= correctionPerGOF * GOF_PER_SEC;
 781        correctionPerSec = tmp1;
 782
 783        /*
 784         *  Fill in the SampleRateConverter control block.
 785         */
 786         
 787        spin_lock_irqsave(&state->card->lock, flags);
 788        cs461x_poke(state->card, BA1_PSRC,
 789          ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
 790        cs461x_poke(state->card, BA1_PPI, phiIncr);
 791        spin_unlock_irqrestore(&state->card->lock, flags);
 792        dmabuf->rate = rate;
 793        
 794        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()- %d\n",rate) );
 795        return rate;
 796}
 797
 798/* set recording sample rate */
 799static unsigned int cs_set_adc_rate(struct cs_state * state, unsigned int rate)
 800{
 801        struct dmabuf *dmabuf = &state->dmabuf;
 802        struct cs_card *card = state->card;
 803        unsigned int phiIncr, coeffIncr, tmp1, tmp2;
 804        unsigned int correctionPerGOF, correctionPerSec, initialDelay;
 805        unsigned int frameGroupLength, cnt;
 806        unsigned long flags;
 807        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()+ %d\n",rate) );
 808
 809        /*
 810         *  We can only decimate by up to a factor of 1/9th the hardware rate.
 811         *  Correct the value if an attempt is made to stray outside that limit.
 812         */
 813        if ((rate * 9) < 48000)
 814                rate = 48000 / 9;
 815
 816        /*
 817         *  We can not capture at at rate greater than the Input Rate (48000).
 818         *  Return an error if an attempt is made to stray outside that limit.
 819         */
 820        if (rate > 48000)
 821                rate = 48000;
 822
 823        /*
 824         *  Compute the values used to drive the actual sample rate conversion.
 825         *  The following formulas are being computed, using inline assembly
 826         *  since we need to use 64 bit arithmetic to compute the values:
 827         *
 828         *     coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
 829         *     phiIncr = floor((Fs,in * 2^26) / Fs,out)
 830         *     correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
 831         *                                GOF_PER_SEC)
 832         *     correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
 833         *                          GOF_PER_SEC * correctionPerGOF
 834         *     initialDelay = ceil((24 * Fs,in) / Fs,out)
 835         *
 836         * i.e.
 837         *
 838         *     coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
 839         *     phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
 840         *     correctionPerGOF:correctionPerSec =
 841         *          dividend:remainder(ulOther / GOF_PER_SEC)
 842         *     initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
 843         */
 844
 845        tmp1 = rate << 16;
 846        coeffIncr = tmp1 / 48000;
 847        tmp1 -= coeffIncr * 48000;
 848        tmp1 <<= 7;
 849        coeffIncr <<= 7;
 850        coeffIncr += tmp1 / 48000;
 851        coeffIncr ^= 0xFFFFFFFF;
 852        coeffIncr++;
 853        tmp1 = 48000 << 16;
 854        phiIncr = tmp1 / rate;
 855        tmp1 -= phiIncr * rate;
 856        tmp1 <<= 10;
 857        phiIncr <<= 10;
 858        tmp2 = tmp1 / rate;
 859        phiIncr += tmp2;
 860        tmp1 -= tmp2 * rate;
 861        correctionPerGOF = tmp1 / GOF_PER_SEC;
 862        tmp1 -= correctionPerGOF * GOF_PER_SEC;
 863        correctionPerSec = tmp1;
 864        initialDelay = ((48000 * 24) + rate - 1) / rate;
 865
 866        /*
 867         *  Fill in the VariDecimate control block.
 868         */
 869        spin_lock_irqsave(&card->lock, flags);
 870        cs461x_poke(card, BA1_CSRC,
 871                ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
 872        cs461x_poke(card, BA1_CCI, coeffIncr);
 873        cs461x_poke(card, BA1_CD,
 874                (((BA1_VARIDEC_BUF_1 + (initialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
 875        cs461x_poke(card, BA1_CPI, phiIncr);
 876        spin_unlock_irqrestore(&card->lock, flags);
 877
 878        /*
 879         *  Figure out the frame group length for the write back task.  Basically,
 880         *  this is just the factors of 24000 (2^6*3*5^3) that are not present in
 881         *  the output sample rate.
 882         */
 883        frameGroupLength = 1;
 884        for (cnt = 2; cnt <= 64; cnt *= 2) {
 885                if (((rate / cnt) * cnt) != rate)
 886                        frameGroupLength *= 2;
 887        }
 888        if (((rate / 3) * 3) != rate) {
 889                frameGroupLength *= 3;
 890        }
 891        for (cnt = 5; cnt <= 125; cnt *= 5) {
 892                if (((rate / cnt) * cnt) != rate) 
 893                        frameGroupLength *= 5;
 894        }
 895
 896        /*
 897         * Fill in the WriteBack control block.
 898         */
 899        spin_lock_irqsave(&card->lock, flags);
 900        cs461x_poke(card, BA1_CFG1, frameGroupLength);
 901        cs461x_poke(card, BA1_CFG2, (0x00800000 | frameGroupLength));
 902        cs461x_poke(card, BA1_CCST, 0x0000FFFF);
 903        cs461x_poke(card, BA1_CSPB, ((65536 * rate) / 24000));
 904        cs461x_poke(card, (BA1_CSPB + 4), 0x0000FFFF);
 905        spin_unlock_irqrestore(&card->lock, flags);
 906        dmabuf->rate = rate;
 907        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()- %d\n",rate) );
 908        return rate;
 909}
 910
 911/* prepare channel attributes for playback */ 
 912static void cs_play_setup(struct cs_state *state)
 913{
 914        struct dmabuf *dmabuf = &state->dmabuf;
 915        struct cs_card *card = state->card;
 916        unsigned int tmp, Count, playFormat;
 917
 918        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()+\n") );
 919        cs461x_poke(card, BA1_PVOL, 0x80008000);
 920        if(!dmabuf->SGok)
 921               cs461x_poke(card, BA1_PBA, virt_to_bus(dmabuf->pbuf));
 922    
 923        Count = 4;                                                          
 924        playFormat=cs461x_peek(card, BA1_PFIE);                             
 925        if ((dmabuf->fmt & CS_FMT_STEREO)) {                                
 926                playFormat &= ~DMA_RQ_C2_AC_MONO_TO_STEREO;                 
 927                Count *= 2;                                                 
 928        }                                                                   
 929        else                                                                
 930                playFormat |= DMA_RQ_C2_AC_MONO_TO_STEREO;                  
 931                                                                            
 932        if ((dmabuf->fmt & CS_FMT_16BIT)) {                                 
 933                playFormat &= ~(DMA_RQ_C2_AC_8_TO_16_BIT                    
 934                           | DMA_RQ_C2_AC_SIGNED_CONVERT);                  
 935                Count *= 2;                                                 
 936        }                                                                   
 937        else                                                                
 938                playFormat |= (DMA_RQ_C2_AC_8_TO_16_BIT                     
 939                           | DMA_RQ_C2_AC_SIGNED_CONVERT);                  
 940                                                                            
 941        cs461x_poke(card, BA1_PFIE, playFormat);                            
 942                                                                            
 943        tmp = cs461x_peek(card, BA1_PDTC);                                  
 944        tmp &= 0xfffffe00;                                                  
 945        cs461x_poke(card, BA1_PDTC, tmp | --Count);                         
 946
 947        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()-\n") );
 948
 949}
 950
 951struct InitStruct
 952{
 953    u32 off;
 954    u32 val;
 955} InitArray[] = { {0x00000040, 0x3fc0000f},
 956                  {0x0000004c, 0x04800000},
 957
 958                  {0x000000b3, 0x00000780},
 959                  {0x000000b7, 0x00000000},
 960                  {0x000000bc, 0x07800000},
 961
 962                  {0x000000cd, 0x00800000},
 963                };
 964
 965/*
 966 * "SetCaptureSPValues()" -- Initialize record task values before each
 967 *      capture startup.  
 968 */
 969void SetCaptureSPValues(struct cs_card *card)
 970{
 971        unsigned i, offset;
 972        CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()+\n") );
 973        for(i=0; i<sizeof(InitArray)/sizeof(struct InitStruct); i++)
 974        {
 975                offset = InitArray[i].off*4; /* 8bit to 32bit offset value */
 976                cs461x_poke(card, offset, InitArray[i].val );
 977        }
 978        CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()-\n") );
 979}
 980
 981/* prepare channel attributes for recording */
 982static void cs_rec_setup(struct cs_state *state)
 983{
 984        struct cs_card *card = state->card;
 985        struct dmabuf *dmabuf = &state->dmabuf;
 986        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()+\n") );
 987
 988        SetCaptureSPValues(card);
 989
 990        /*
 991         * set the attenuation to 0dB 
 992         */
 993        cs461x_poke(card, BA1_CVOL, 0x80008000);
 994
 995        /*
 996         * set the physical address of the capture buffer into the SP
 997         */
 998        cs461x_poke(card, BA1_CBA, virt_to_bus(dmabuf->rawbuf));
 999
1000        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()-\n") );
1001}
1002
1003
1004/* get current playback/recording dma buffer pointer (byte offset from LBA),
1005   called with spinlock held! */
1006   
1007static inline unsigned cs_get_dma_addr(struct cs_state *state)
1008{
1009        struct dmabuf *dmabuf = &state->dmabuf;
1010        u32 offset;
1011        
1012        if ( (!(dmabuf->enable & DAC_RUNNING)) &&
1013             (!(dmabuf->enable & ADC_RUNNING) ) )
1014        {
1015                CS_DBGOUT(CS_ERROR, 2, printk(
1016                        "cs46xx: ERROR cs_get_dma_addr(): not enabled \n") );
1017                return 0;
1018        }
1019                
1020        /*
1021         * ganularity is byte boundry, good part.
1022         */
1023        if(dmabuf->enable & DAC_RUNNING)
1024        {
1025                offset = cs461x_peek(state->card, BA1_PBA);                                  
1026        }
1027        else /* ADC_RUNNING must be set */
1028        {
1029                offset = cs461x_peek(state->card, BA1_CBA);                                  
1030        }
1031        CS_DBGOUT(CS_PARMS | CS_FUNCTION, 9, 
1032                printk("cs46xx: cs_get_dma_addr() %d\n",offset) );
1033        offset = (u32)bus_to_virt((unsigned long)offset) - (u32)dmabuf->rawbuf;
1034        CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8, 
1035                printk("cs46xx: cs_get_dma_addr()- %d\n",offset) );
1036        return offset;
1037}
1038
1039static void resync_dma_ptrs(struct cs_state *state)
1040{
1041        struct dmabuf *dmabuf;
1042        
1043        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()+ \n") );
1044        if(state)
1045        {
1046                dmabuf = &state->dmabuf;
1047                dmabuf->hwptr=dmabuf->swptr = 0;
1048                dmabuf->pringbuf = 0;
1049        }
1050        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()- \n") );
1051}
1052        
1053/* Stop recording (lock held) */
1054static inline void __stop_adc(struct cs_state *state)
1055{
1056        struct dmabuf *dmabuf = &state->dmabuf;
1057        struct cs_card *card = state->card;
1058        unsigned int tmp;
1059        
1060        dmabuf->enable &= ~ADC_RUNNING;
1061        
1062        tmp = cs461x_peek(card, BA1_CCTL);
1063        tmp &= 0xFFFF0000;
1064        cs461x_poke(card, BA1_CCTL, tmp );
1065}
1066
1067static void stop_adc(struct cs_state *state)
1068{
1069        unsigned long flags;
1070
1071        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()+ \n") );
1072        spin_lock_irqsave(&state->card->lock, flags);
1073        __stop_adc(state);
1074        spin_unlock_irqrestore(&state->card->lock, flags);
1075        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()- \n") );
1076}
1077
1078static void start_adc(struct cs_state *state)
1079{
1080        struct dmabuf *dmabuf = &state->dmabuf;
1081        struct cs_card *card = state->card;
1082        unsigned long flags;
1083        unsigned int tmp;
1084
1085        spin_lock_irqsave(&card->lock, flags);
1086        if (!(dmabuf->enable & ADC_RUNNING) && 
1087             ((dmabuf->mapped || dmabuf->count < (signed)dmabuf->dmasize) 
1088               && dmabuf->ready) && 
1089               ((card->pm.flags & CS46XX_PM_IDLE) || 
1090                (card->pm.flags & CS46XX_PM_RESUMED)) )
1091        {
1092                dmabuf->enable |= ADC_RUNNING;
1093                cs_set_divisor(dmabuf);
1094                tmp = cs461x_peek(card, BA1_CCTL);
1095                tmp &= 0xFFFF0000;
1096                tmp |= card->cctl;
1097                CS_DBGOUT(CS_FUNCTION, 2, printk(
1098                        "cs46xx: start_adc() poke 0x%x \n",tmp) );
1099                cs461x_poke(card, BA1_CCTL, tmp);
1100        }
1101        spin_unlock_irqrestore(&card->lock, flags);
1102}
1103
1104/* stop playback (lock held) */
1105static inline void __stop_dac(struct cs_state *state)
1106{
1107        struct dmabuf *dmabuf = &state->dmabuf;
1108        struct cs_card *card = state->card;
1109        unsigned int tmp;
1110
1111        dmabuf->enable &= ~DAC_RUNNING;
1112        
1113        tmp=cs461x_peek(card, BA1_PCTL);
1114        tmp&=0xFFFF;
1115        cs461x_poke(card, BA1_PCTL, tmp);
1116}
1117
1118static void stop_dac(struct cs_state *state)
1119{
1120        unsigned long flags;
1121
1122        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()+ \n") );
1123        spin_lock_irqsave(&state->card->lock, flags);
1124        __stop_dac(state);
1125        spin_unlock_irqrestore(&state->card->lock, flags);
1126        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()- \n") );
1127}       
1128
1129static void start_dac(struct cs_state *state)
1130{
1131        struct dmabuf *dmabuf = &state->dmabuf;
1132        struct cs_card *card = state->card;
1133        unsigned long flags;
1134        int tmp;
1135
1136        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()+ \n") );
1137        spin_lock_irqsave(&card->lock, flags);
1138        if (!(dmabuf->enable & DAC_RUNNING) && 
1139            ((dmabuf->mapped || dmabuf->count > 0) && dmabuf->ready) &&
1140               ((card->pm.flags & CS46XX_PM_IDLE) || 
1141                (card->pm.flags & CS46XX_PM_RESUMED)) )
1142        {
1143                dmabuf->enable |= DAC_RUNNING;
1144                tmp = cs461x_peek(card, BA1_PCTL);
1145                tmp &= 0xFFFF;
1146                tmp |= card->pctl;
1147                CS_DBGOUT(CS_PARMS, 6, printk(
1148                    "cs46xx: start_dac() poke card=0x%.08x tmp=0x%.08x addr=0x%.08x \n",
1149                    (unsigned)card, (unsigned)tmp, 
1150                    (unsigned)card->ba1.idx[(BA1_PCTL >> 16) & 3]+(BA1_PCTL&0xffff) ) );
1151                cs461x_poke(card, BA1_PCTL, tmp);
1152        }
1153        spin_unlock_irqrestore(&card->lock, flags);
1154        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()- \n") );
1155}
1156
1157#define DMABUF_MINORDER 1
1158
1159/*
1160 * allocate DMA buffer, playback and recording buffers are separate.
1161 */
1162static int alloc_dmabuf(struct cs_state *state)
1163{
1164
1165        struct cs_card *card=state->card;
1166        struct dmabuf *dmabuf = &state->dmabuf;
1167        void *rawbuf = NULL;
1168        void *tmpbuff = NULL;
1169        int order;
1170        struct page *map, *mapend;
1171        unsigned long df;
1172        
1173        dmabuf->ready  = dmabuf->mapped = 0;
1174        dmabuf->SGok = 0;
1175/*
1176* check for order within limits, but do not overwrite value.
1177*/
1178        if((defaultorder > 1) && (defaultorder < 12))
1179                df = defaultorder;
1180        else
1181                df = 2; 
1182
1183        for (order = df; order >= DMABUF_MINORDER; order--)
1184                if ( (rawbuf = (void *) pci_alloc_consistent(
1185                        card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr)))
1186                            break;
1187        if (!rawbuf) {
1188                CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
1189                        "cs46xx: alloc_dmabuf(): unable to allocate rawbuf\n"));
1190                return -ENOMEM;
1191        }
1192        dmabuf->buforder = order;
1193        dmabuf->rawbuf = rawbuf;
1194        // Now mark the pages as reserved; otherwise the 
1195        // remap_page_range() in cs46xx_mmap doesn't work.
1196        // 1. get index to last page in mem_map array for rawbuf.
1197        mapend = virt_to_page(dmabuf->rawbuf + 
1198                (PAGE_SIZE << dmabuf->buforder) - 1);
1199
1200        // 2. mark each physical page in range as 'reserved'.
1201        for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
1202                cs4x_mem_map_reserve(map);
1203
1204        CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: alloc_dmabuf(): allocated %ld (order = %d) bytes at %p\n",
1205               PAGE_SIZE << order, order, rawbuf) );
1206
1207/*
1208*  only allocate the conversion buffer for the ADC
1209*/
1210        if(dmabuf->type == CS_TYPE_DAC)
1211        {
1212                dmabuf->tmpbuff = NULL;
1213                dmabuf->buforder_tmpbuff = 0;
1214                return 0;
1215        }
1216/*
1217 * now the temp buffer for 16/8 conversions
1218 */
1219
1220        tmpbuff = (void *) pci_alloc_consistent(
1221                card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr_tmpbuff);
1222
1223        if (!tmpbuff)
1224                return -ENOMEM;
1225        CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: allocated %ld (order = %d) bytes at %p\n",
1226               PAGE_SIZE << order, order, tmpbuff) );
1227
1228        dmabuf->tmpbuff = tmpbuff;
1229        dmabuf->buforder_tmpbuff = order;
1230        
1231        // Now mark the pages as reserved; otherwise the 
1232        // remap_page_range() in cs46xx_mmap doesn't work.
1233        // 1. get index to last page in mem_map array for rawbuf.
1234        mapend = virt_to_page(dmabuf->tmpbuff + 
1235                (PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);
1236
1237        // 2. mark each physical page in range as 'reserved'.
1238        for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
1239                cs4x_mem_map_reserve(map);
1240        return 0;
1241}
1242
1243/* free DMA buffer */
1244static void dealloc_dmabuf(struct cs_state *state)
1245{
1246        struct dmabuf *dmabuf = &state->dmabuf;
1247        struct page *map, *mapend;
1248
1249        if (dmabuf->rawbuf) {
1250                // Undo prog_dmabuf()'s marking the pages as reserved 
1251                mapend = virt_to_page(dmabuf->rawbuf + 
1252                                (PAGE_SIZE << dmabuf->buforder) - 1);
1253                for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
1254                        cs4x_mem_map_unreserve(map);
1255                free_dmabuf(state->card, dmabuf);
1256        }
1257
1258        if (dmabuf->tmpbuff) {
1259                // Undo prog_dmabuf()'s marking the pages as reserved 
1260                mapend = virt_to_page(dmabuf->tmpbuff +
1261                                (PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);
1262                for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
1263                        cs4x_mem_map_unreserve(map);
1264                free_dmabuf2(state->card, dmabuf);
1265        }
1266
1267        dmabuf->rawbuf = NULL;
1268        dmabuf->tmpbuff = NULL;
1269        dmabuf->mapped = dmabuf->ready = 0;
1270        dmabuf->SGok = 0;
1271}
1272
1273static int __prog_dmabuf(struct cs_state *state)
1274{
1275        struct dmabuf *dmabuf = &state->dmabuf;
1276        unsigned long flags;
1277        unsigned long allocated_pages, allocated_bytes;                     
1278        unsigned long tmp1, tmp2, fmt=0;                                           
1279        unsigned long *ptmp = (unsigned long *) dmabuf->pbuf;               
1280        unsigned long SGarray[9], nSGpages=0;                               
1281        int ret;
1282
1283        CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()+ \n"));
1284/*
1285 * check for CAPTURE and use only non-sg for initial release
1286 */
1287        if(dmabuf->type == CS_TYPE_ADC)
1288        {
1289                CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() ADC\n"));
1290                /* 
1291                 * add in non-sg support for capture.
1292                 */
1293                spin_lock_irqsave(&state->card->lock, flags);
1294        /* add code to reset the rawbuf memory. TRW */
1295                resync_dma_ptrs(state);
1296                dmabuf->total_bytes = dmabuf->blocks = 0;
1297                dmabuf->count = dmabuf->error = dmabuf->underrun = 0;
1298
1299                dmabuf->SGok = 0;                                                   
1300
1301                spin_unlock_irqrestore(&state->card->lock, flags);
1302
1303                /* allocate DMA buffer if not allocated yet */
1304                if (!dmabuf->rawbuf || !dmabuf->tmpbuff)
1305                        if ((ret = alloc_dmabuf(state)))
1306                                return ret; 
1307        /*
1308         * static image only supports 16Bit signed, stereo - hard code fmt
1309         */
1310                fmt = CS_FMT_16BIT | CS_FMT_STEREO;
1311
1312                dmabuf->numfrag = 2;                                        
1313                dmabuf->fragsize = 2048;                                    
1314                dmabuf->fragsamples = 2048 >> sample_shift[fmt];    
1315                dmabuf->dmasize = 4096;                                     
1316                dmabuf->fragshift = 11;                                     
1317
1318                memset(dmabuf->rawbuf, (fmt & CS_FMT_16BIT) ? 0 : 0x80,
1319                       dmabuf->dmasize);
1320                memset(dmabuf->tmpbuff, (fmt & CS_FMT_16BIT) ? 0 : 0x80, 
1321                        PAGE_SIZE<<dmabuf->buforder_tmpbuff);      
1322
1323                /*
1324                 *      Now set up the ring
1325                 */
1326
1327                spin_lock_irqsave(&state->card->lock, flags);
1328                cs_rec_setup(state);
1329                spin_unlock_irqrestore(&state->card->lock, flags);
1330
1331                /* set the ready flag for the dma buffer */
1332                dmabuf->ready = 1;
1333
1334                CS_DBGOUT(CS_PARMS, 4, printk(
1335                        "cs46xx: prog_dmabuf(): CAPTURE rate=%d fmt=0x%x numfrag=%d "
1336                        "fragsize=%d dmasize=%d\n",
1337                            dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1338                            dmabuf->fragsize, dmabuf->dmasize) );
1339
1340                CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- 0 \n"));
1341                return 0;
1342        }
1343        else if (dmabuf->type == CS_TYPE_DAC)
1344        {
1345        /*
1346         * Must be DAC
1347         */
1348                CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() DAC\n"));
1349                spin_lock_irqsave(&state->card->lock, flags);
1350                resync_dma_ptrs(state);
1351                dmabuf->total_bytes = dmabuf->blocks = 0;
1352                dmabuf->count = dmabuf->error = dmabuf->underrun = 0;
1353
1354                dmabuf->SGok = 0;                                                   
1355
1356                spin_unlock_irqrestore(&state->card->lock, flags);
1357
1358                /* allocate DMA buffer if not allocated yet */
1359                if (!dmabuf->rawbuf)
1360                        if ((ret = alloc_dmabuf(state)))
1361                                return ret;
1362
1363                allocated_pages = 1 << dmabuf->buforder;                            
1364                allocated_bytes = allocated_pages*PAGE_SIZE;                        
1365                                                                                    
1366                if(allocated_pages < 2)                                             
1367                {
1368                        CS_DBGOUT(CS_FUNCTION, 4, printk(
1369                            "cs46xx: prog_dmabuf() Error: allocated_pages too small (%d)\n",
1370                                (unsigned)allocated_pages));
1371                        return -ENOMEM;
1372                }
1373                                                                                    
1374                /* Use all the pages allocated, fragsize 4k. */
1375                /* Use 'pbuf' for S/G page map table. */
1376                dmabuf->SGok = 1;           /* Use S/G. */
1377
1378                nSGpages = allocated_bytes/4096;    /* S/G pages always 4k. */
1379                                                                                    
1380                     /* Set up S/G variables. */
1381                *ptmp = virt_to_bus(dmabuf->rawbuf);                                
1382                *(ptmp+1) = 0x00000008;                                             
1383                for(tmp1= 1; tmp1 < nSGpages; tmp1++) {                             
1384                        *(ptmp+2*tmp1) = virt_to_bus( (dmabuf->rawbuf)+4096*tmp1);  
1385                        if( tmp1 == nSGpages-1)                                     
1386                                tmp2 = 0xbfff0000;
1387                        else                                                        
1388                                tmp2 = 0x80000000+8*(tmp1+1);                       
1389                        *(ptmp+2*tmp1+1) = tmp2;                                    
1390                }                                                                   
1391                SGarray[0] = 0x82c0200d;                                            
1392                SGarray[1] = 0xffff0000;                                            
1393                SGarray[2] = *ptmp;                                                 
1394                SGarray[3] = 0x00010600;                                            
1395                SGarray[4] = *(ptmp+2);                                             
1396                SGarray[5] = 0x80000010;                                            
1397                SGarray[6] = *ptmp;                                                 
1398                SGarray[7] = *(ptmp+2);                                             
1399                SGarray[8] = (virt_to_bus(dmabuf->pbuf) & 0xffff000) | 0x10;        
1400
1401                if (dmabuf->SGok) {                                                 
1402                        dmabuf->numfrag = nSGpages;                                 
1403                        dmabuf->fragsize = 4096;                                    
1404                        dmabuf->fragsamples = 4096 >> sample_shift[dmabuf->fmt];    
1405                        dmabuf->fragshift = 12;                                     
1406                        dmabuf->dmasize = dmabuf->numfrag*4096;                     
1407                }                                                                   
1408                else {                                                              
1409                        SGarray[0] = 0xf2c0000f;                                    
1410                        SGarray[1] = 0x00000200;                                    
1411                        SGarray[2] = 0;                                             
1412                        SGarray[3] = 0x00010600;                                    
1413                        SGarray[4]=SGarray[5]=SGarray[6]=SGarray[7]=SGarray[8] = 0; 
1414                        dmabuf->numfrag = 2;                                        
1415                        dmabuf->fragsize = 2048;                                    
1416                        dmabuf->fragsamples = 2048 >> sample_shift[dmabuf->fmt];    
1417                        dmabuf->dmasize = 4096;                                     
1418                        dmabuf->fragshift = 11;                                     
1419                }
1420                for(tmp1 = 0; tmp1 < sizeof(SGarray)/4; tmp1++)                     
1421                        cs461x_poke( state->card, BA1_PDTC+tmp1*4, SGarray[tmp1]);  
1422
1423                memset(dmabuf->rawbuf, (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1424                       dmabuf->dmasize);
1425
1426                /*
1427                 *      Now set up the ring
1428                 */
1429
1430                spin_lock_irqsave(&state->card->lock, flags);
1431                cs_play_setup(state);
1432                spin_unlock_irqrestore(&state->card->lock, flags);
1433
1434                /* set the ready flag for the dma buffer */
1435                dmabuf->ready = 1;
1436
1437                CS_DBGOUT(CS_PARMS, 4, printk(
1438                        "cs46xx: prog_dmabuf(): PLAYBACK rate=%d fmt=0x%x numfrag=%d "
1439                        "fragsize=%d dmasize=%d\n",
1440                            dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1441                            dmabuf->fragsize, dmabuf->dmasize) );
1442
1443                CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- \n"));
1444                return 0;
1445        }
1446        else
1447        {
1448                CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- Invalid Type %d\n",
1449                        dmabuf->type));
1450        }
1451        return 1;
1452}
1453
1454static int prog_dmabuf(struct cs_state *state)
1455{
1456        int ret;
1457        
1458        down(&state->sem);
1459        ret = __prog_dmabuf(state);
1460        up(&state->sem);
1461        
1462        return ret;
1463}
1464
1465static void cs_clear_tail(struct cs_state *state)
1466{
1467}
1468
1469static int drain_dac(struct cs_state *state, int nonblock)
1470{
1471        DECLARE_WAITQUEUE(wait, current);
1472        struct dmabuf *dmabuf = &state->dmabuf;
1473        struct cs_card *card=state->card;
1474        unsigned long flags;
1475        unsigned long tmo;
1476        int count;
1477
1478        CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()+ \n"));
1479        if (dmabuf->mapped || !dmabuf->ready)
1480        {
1481                CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0, not ready\n"));
1482                return 0;
1483        }
1484
1485        add_wait_queue(&dmabuf->wait, &wait);
1486        for (;;) {
1487                /* It seems that we have to set the current state to TASK_INTERRUPTIBLE
1488                   every time to make the process really go to sleep */
1489                current->state = TASK_INTERRUPTIBLE;
1490
1491                spin_lock_irqsave(&state->card->lock, flags);
1492                count = dmabuf->count;
1493                spin_unlock_irqrestore(&state->card->lock, flags);
1494
1495                if (count <= 0)
1496                        break;
1497
1498                if (signal_pending(current))
1499                        break;
1500
1501                if (nonblock) {
1502                        remove_wait_queue(&dmabuf->wait, &wait);
1503                        current->state = TASK_RUNNING;
1504                        return -EBUSY;
1505                }
1506
1507                tmo = (dmabuf->dmasize * HZ) / dmabuf->rate;
1508                tmo >>= sample_shift[dmabuf->fmt];
1509                tmo += (2048*HZ)/dmabuf->rate;
1510                
1511                if (!schedule_timeout(tmo ? tmo : 1) && tmo){
1512                        printk(KERN_ERR "cs46xx: drain_dac, dma timeout? %d\n", count);
1513                        break;
1514                }
1515        }
1516        remove_wait_queue(&dmabuf->wait, &wait);
1517        current->state = TASK_RUNNING;
1518        if (signal_pending(current))
1519        {
1520                CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- -ERESTARTSYS\n"));
1521                /*
1522                * set to silence and let that clear the fifos.
1523                */
1524                cs461x_clear_serial_FIFOs(card, CS_TYPE_DAC);
1525                return -ERESTARTSYS;
1526        }
1527
1528        CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0\n"));
1529        return 0;
1530}
1531
1532
1533/* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1534static void cs_update_ptr(struct cs_card *card, int wake)
1535{
1536        struct cs_state *state;
1537        struct dmabuf *dmabuf;
1538        unsigned hwptr;
1539        int diff;
1540
1541        /* error handling and process wake up for ADC */
1542        state = card->states[0];
1543        if(state)
1544        {
1545                dmabuf = &state->dmabuf;
1546                if (dmabuf->enable & ADC_RUNNING) {
1547                        /* update hardware pointer */
1548                        hwptr = cs_get_dma_addr(state);
1549
1550                        diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1551                        CS_DBGOUT(CS_PARMS, 9, printk(
1552                                "cs46xx: cs_update_ptr()+ ADC hwptr=%d diff=%d\n", 
1553                                hwptr,diff) );
1554                        dmabuf->hwptr = hwptr;
1555                        dmabuf->total_bytes += diff;
1556                        dmabuf->count += diff;
1557                        if (dmabuf->count > dmabuf->dmasize)
1558                                dmabuf->count = dmabuf->dmasize;
1559
1560                        if(dmabuf->mapped)
1561                        {
1562                                if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
1563                                        wake_up(&dmabuf->wait);
1564                        } else 
1565                        {
1566                                if (wake && dmabuf->count > 0)
1567                                        wake_up(&dmabuf->wait);
1568                        }
1569                }
1570        }
1571
1572/*
1573 * Now the DAC
1574 */
1575        state = card->states[1];
1576        if(state)
1577        {
1578                dmabuf = &state->dmabuf;
1579                /* error handling and process wake up for DAC */
1580                if (dmabuf->enable & DAC_RUNNING) {
1581                        /* update hardware pointer */
1582                        hwptr = cs_get_dma_addr(state);
1583
1584                        diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1585                        CS_DBGOUT(CS_PARMS, 9, printk(
1586                                "cs46xx: cs_update_ptr()+ DAC hwptr=%d diff=%d\n", 
1587                                hwptr,diff) );
1588                        dmabuf->hwptr = hwptr;
1589                        dmabuf->total_bytes += diff;
1590                        if (dmabuf->mapped) {
1591                                dmabuf->count += diff;
1592                                if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
1593                                        wake_up(&dmabuf->wait);
1594                                /*
1595                                 * other drivers use fragsize, but don't see any sense
1596                                 * in that, since dmasize is the buffer asked for
1597                                 * via mmap.
1598                                 */
1599                                if( dmabuf->count > dmabuf->dmasize)
1600                                        dmabuf->count &= dmabuf->dmasize-1;
1601                        } else {
1602                                dmabuf->count -= diff;
1603                                /*
1604                                 * backfill with silence and clear out the last 
1605                                 * "diff" number of bytes.
1606                                 */
1607                                if(hwptr >= diff)
1608                                {
1609                                        memset(dmabuf->rawbuf + hwptr - diff, 
1610                                                (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80, diff);
1611                                }
1612                                else
1613                                {
1614                                        memset(dmabuf->rawbuf, 
1615                                                (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1616                                                (unsigned)hwptr);
1617                                        memset((void *)((unsigned)dmabuf->rawbuf + 
1618                                                        dmabuf->dmasize + hwptr - diff),
1619                                                (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80, 
1620                                                diff - hwptr); 
1621                                }
1622
1623                                if (dmabuf->count < 0 || dmabuf->count > dmabuf->dmasize) {
1624                                        CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO
1625                                          "cs46xx: ERROR DAC count<0 or count > dmasize (%d)\n",
1626                                                dmabuf->count));
1627                                        /* 
1628                                        * buffer underrun or buffer overrun, reset the
1629                                        * count of bytes written back to 0.
1630                                        */
1631                                        if(dmabuf->count < 0)
1632                                                dmabuf->underrun=1;
1633                                        dmabuf->count = 0;
1634                                        dmabuf->error++;
1635                                }
1636                                if (wake && dmabuf->count < (signed)dmabuf->dmasize/2)
1637                                        wake_up(&dmabuf->wait);
1638                        }
1639                }
1640        }
1641}
1642
1643
1644/* hold spinlock for the following! */
1645static void cs_handle_midi(struct cs_card *card)
1646{
1647        unsigned char ch;
1648        int wake;
1649        unsigned temp1;
1650
1651        wake = 0;
1652        while (!(cs461x_peekBA0(card,  BA0_MIDSR) & MIDSR_RBE)) {
1653                ch = cs461x_peekBA0(card, BA0_MIDRP);
1654                if (card->midi.icnt < CS_MIDIINBUF) {
1655                        card->midi.ibuf[card->midi.iwr] = ch;
1656                        card->midi.iwr = (card->midi.iwr + 1) % CS_MIDIINBUF;
1657                        card->midi.icnt++;
1658                }
1659                wake = 1;
1660        }
1661        if (wake)
1662                wake_up(&card->midi.iwait);
1663        wake = 0;
1664        while (!(cs461x_peekBA0(card,  BA0_MIDSR) & MIDSR_TBF) && card->midi.ocnt > 0) {
1665                temp1 = ( card->midi.obuf[card->midi.ord] ) & 0x000000ff;
1666                cs461x_pokeBA0(card, BA0_MIDWP,temp1);
1667                card->midi.ord = (card->midi.ord + 1) % CS_MIDIOUTBUF;
1668                card->midi.ocnt--;
1669                if (card->midi.ocnt < CS_MIDIOUTBUF-16)
1670                        wake = 1;
1671        }
1672        if (wake)
1673                wake_up(&card->midi.owait);
1674}
1675
1676static void cs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1677{
1678        struct cs_card *card = (struct cs_card *)dev_id;
1679        /* Single channel card */
1680        struct cs_state *recstate = card->channel[0].state;
1681        struct cs_state *playstate = card->channel[1].state;
1682        u32 status;
1683
1684        CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()+ \n"));
1685
1686        spin_lock(&card->lock);
1687
1688        status = cs461x_peekBA0(card, BA0_HISR);
1689        
1690        if ((status & 0x7fffffff) == 0)
1691        {
1692                cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
1693                spin_unlock(&card->lock);
1694                return;
1695        }
1696        
1697        /*
1698         * check for playback or capture interrupt only
1699         */
1700        if( ((status & HISR_VC0) && playstate && playstate->dmabuf.ready) || 
1701            (((status & HISR_VC1) && recstate && recstate->dmabuf.ready)) )
1702        {
1703                CS_DBGOUT(CS_INTERRUPT, 8, printk(
1704                        "cs46xx: cs_interrupt() interrupt bit(s) set (0x%x)\n",status));
1705                cs_update_ptr(card, CS_TRUE);
1706        }
1707
1708        if( status & HISR_MIDI )
1709                cs_handle_midi(card);
1710        
1711        /* clear 'em */
1712        cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
1713        spin_unlock(&card->lock);
1714        CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()- \n"));
1715}
1716
1717
1718/**********************************************************************/
1719
1720static ssize_t cs_midi_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
1721{
1722        struct cs_card *card = (struct cs_card *)file->private_data;
1723        ssize_t ret;
1724        unsigned long flags;
1725        unsigned ptr;
1726        int cnt;
1727
1728        if (ppos != &file->f_pos)
1729                return -ESPIPE;
1730        if (!access_ok(VERIFY_WRITE, buffer, count))
1731                return -EFAULT;
1732        ret = 0;
1733        while (count > 0) {
1734                spin_lock_irqsave(&card->lock, flags);
1735                ptr = card->midi.ird;
1736                cnt = CS_MIDIINBUF - ptr;
1737                if (card->midi.icnt < cnt)
1738                        cnt = card->midi.icnt;
1739                spin_unlock_irqrestore(&card->lock, flags);
1740                if (cnt > count)
1741                        cnt = count;
1742                if (cnt <= 0) {
1743                        if (file->f_flags & O_NONBLOCK)
1744                                return ret ? ret : -EAGAIN;
1745                        interruptible_sleep_on(&card->midi.iwait);
1746                        if (signal_pending(current))
1747                                return ret ? ret : -ERESTARTSYS;
1748                        continue;
1749                }
1750                if (copy_to_user(buffer, card->midi.ibuf + ptr, cnt))
1751                        return ret ? ret : -EFAULT;
1752                ptr = (ptr + cnt) % CS_MIDIINBUF;
1753                spin_lock_irqsave(&card->lock, flags);
1754                card->midi.ird = ptr;
1755                card->midi.icnt -= cnt;
1756                spin_unlock_irqrestore(&card->lock, flags);
1757                count -= cnt;
1758                buffer += cnt;
1759                ret += cnt;
1760        }
1761        return ret;
1762}
1763
1764
1765static ssize_t cs_midi_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
1766{
1767        struct cs_card *card = (struct cs_card *)file->private_data;
1768        ssize_t ret;
1769        unsigned long flags;
1770        unsigned ptr;
1771        int cnt;
1772
1773        if (ppos != &file->f_pos)
1774                return -ESPIPE;
1775        if (!access_ok(VERIFY_READ, buffer, count))
1776                return -EFAULT;
1777        ret = 0;
1778        while (count > 0) {
1779                spin_lock_irqsave(&card->lock, flags);
1780                ptr = card->midi.owr;
1781                cnt = CS_MIDIOUTBUF - ptr;
1782                if (card->midi.ocnt + cnt > CS_MIDIOUTBUF)
1783                        cnt = CS_MIDIOUTBUF - card->midi.ocnt;
1784                if (cnt <= 0)
1785                        cs_handle_midi(card);
1786                spin_unlock_irqrestore(&card->lock, flags);
1787                if (cnt > count)
1788                        cnt = count;
1789                if (cnt <= 0) {
1790                        if (file->f_flags & O_NONBLOCK)
1791                                return ret ? ret : -EAGAIN;
1792                        interruptible_sleep_on(&card->midi.owait);
1793                        if (signal_pending(current))
1794                                return ret ? ret : -ERESTARTSYS;
1795                        continue;
1796                }
1797                if (copy_from_user(card->midi.obuf + ptr, buffer, cnt))
1798                        return ret ? ret : -EFAULT;
1799                ptr = (ptr + cnt) % CS_MIDIOUTBUF;
1800                spin_lock_irqsave(&card->lock, flags);
1801                card->midi.owr = ptr;
1802                card->midi.ocnt += cnt;
1803                spin_unlock_irqrestore(&card->lock, flags);
1804                count -= cnt;
1805                buffer += cnt;
1806                ret += cnt;
1807                spin_lock_irqsave(&card->lock, flags);
1808                cs_handle_midi(card);
1809                spin_unlock_irqrestore(&card->lock, flags);
1810        }
1811        return ret;
1812}
1813
1814
1815static unsigned int cs_midi_poll(struct file *file, struct poll_table_struct *wait)
1816{
1817        struct cs_card *card = (struct cs_card *)file->private_data;
1818        unsigned long flags;
1819        unsigned int mask = 0;
1820
1821        if (file->f_flags & FMODE_WRITE)
1822                poll_wait(file, &card->midi.owait, wait);
1823        if (file->f_flags & FMODE_READ)
1824                poll_wait(file, &card->midi.iwait, wait);
1825        spin_lock_irqsave(&card->lock, flags);
1826        if (file->f_flags & FMODE_READ) {
1827                if (card->midi.icnt > 0)
1828                        mask |= POLLIN | POLLRDNORM;
1829        }
1830        if (file->f_flags & FMODE_WRITE) {
1831                if (card->midi.ocnt < CS_MIDIOUTBUF)
1832                        mask |= POLLOUT | POLLWRNORM;
1833        }
1834        spin_unlock_irqrestore(&card->lock, flags);
1835        return mask;
1836}
1837
1838
1839static int cs_midi_open(struct inode *inode, struct file *file)
1840{
1841        int minor = MINOR(inode->i_rdev);
1842        struct cs_card *card=NULL;
1843        unsigned long flags;
1844        struct list_head *entry;
1845
1846        list_for_each(entry, &cs46xx_devs)
1847        {
1848                card = list_entry(entry, struct cs_card, list);
1849                if (card->dev_midi == minor)
1850                        break;
1851        }
1852
1853        if (entry == &cs46xx_devs)
1854                return -ENODEV;
1855        if (!card)
1856        {
1857                CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
1858                        "cs46xx: cs46xx_midi_open(): Error - unable to find card struct\n"));
1859                return -ENODEV;
1860        }
1861
1862        file->private_data = card;
1863        /* wait for device to become free */
1864        down(&card->midi.open_sem);
1865        while (card->midi.open_mode & file->f_mode) {
1866                if (file->f_flags & O_NONBLOCK) {
1867                        up(&card->midi.open_sem);
1868                        return -EBUSY;
1869                }
1870                up(&card->midi.open_sem);
1871                interruptible_sleep_on(&card->midi.open_wait);
1872                if (signal_pending(current))
1873                        return -ERESTARTSYS;
1874                down(&card->midi.open_sem);
1875        }
1876        spin_lock_irqsave(&card->midi.lock, flags);
1877        if (!(card->midi.open_mode & (FMODE_READ | FMODE_WRITE))) {
1878                card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1879                card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
1880                card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1881                cs461x_pokeBA0(card, BA0_MIDCR, 0x0000000f);            /* Enable xmit, rcv. */
1882                cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM);   /* Enable interrupts */
1883        }
1884        if (file->f_mode & FMODE_READ) {
1885                card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1886        }
1887        if (file->f_mode & FMODE_WRITE) {
1888                card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
1889        }
1890        spin_unlock_irqrestore(&card->midi.lock, flags);
1891        card->midi.open_mode |= (file->f_mode & (FMODE_READ | FMODE_WRITE));
1892        up(&card->midi.open_sem);
1893        MOD_INC_USE_COUNT; /* for 2.2 */
1894        return 0;
1895}
1896
1897
1898static int cs_midi_release(struct inode *inode, struct file *file)
1899{
1900        struct cs_card *card = (struct cs_card *)file->private_data;
1901        DECLARE_WAITQUEUE(wait, current);
1902        unsigned long flags;
1903        unsigned count, tmo;
1904
1905        if (file->f_mode & FMODE_WRITE) {
1906                current->state = TASK_INTERRUPTIBLE;
1907                add_wait_queue(&card->midi.owait, &wait);
1908                for (;;) {
1909                        spin_lock_irqsave(&card->midi.lock, flags);
1910                        count = card->midi.ocnt;
1911                        spin_unlock_irqrestore(&card->midi.lock, flags);
1912                        if (count <= 0)
1913                                break;
1914                        if (signal_pending(current))
1915                                break;
1916                        if (file->f_flags & O_NONBLOCK)
1917                                break;
1918                        tmo = (count * HZ) / 3100;
1919                        if (!schedule_timeout(tmo ? : 1) && tmo)
1920                                printk(KERN_DEBUG "cs46xx: midi timed out??\n");
1921                }
1922                remove_wait_queue(&card->midi.owait, &wait);
1923                current->state = TASK_RUNNING;
1924        }
1925        down(&card->midi.open_sem);
1926        card->midi.open_mode &= (~(file->f_mode & (FMODE_READ | FMODE_WRITE)));
1927        up(&card->midi.open_sem);
1928        wake_up(&card->midi.open_wait);
1929        MOD_DEC_USE_COUNT; /* for 2.2 */
1930        return 0;
1931}
1932
1933/*
1934 *   Midi file operations struct.
1935 */
1936static /*const*/ struct file_operations cs_midi_fops = {
1937        CS_OWNER        CS_THIS_MODULE
1938        llseek:         no_llseek,
1939        read:           cs_midi_read,
1940        write:          cs_midi_write,
1941        poll:           cs_midi_poll,
1942        open:           cs_midi_open,
1943        release:        cs_midi_release,
1944};
1945
1946/*
1947 *
1948 * CopySamples copies 16-bit stereo signed samples from the source to the
1949 * destination, possibly converting down to unsigned 8-bit and/or mono.
1950 * count specifies the number of output bytes to write.
1951 *
1952 *  Arguments:
1953 *
1954 *  dst             - Pointer to a destination buffer.
1955 *  src             - Pointer to a source buffer
1956 *  count           - The number of bytes to copy into the destination buffer.
1957 *  fmt             - CS_FMT_16BIT and/or CS_FMT_STEREO bits
1958 *  dmabuf          - pointer to the dma buffer structure
1959 *
1960 * NOTES: only call this routine if the output desired is not 16 Signed Stereo
1961 *      
1962 *
1963 */
1964static void CopySamples(char *dst, char *src, int count, unsigned fmt, 
1965                struct dmabuf *dmabuf)
1966{
1967
1968    s32 s32AudioSample;
1969    s16 *psSrc=(s16 *)src;
1970    s16 *psDst=(s16 *)dst;
1971    u8 *pucDst=(u8 *)dst;
1972
1973    CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: CopySamples()+ ") );
1974    CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
1975        " dst=0x%x src=0x%x count=%d fmt=0x%x\n",
1976        (unsigned)dst,(unsigned)src,(unsigned)count,(unsigned)fmt) );
1977
1978    /*
1979     * See if the data should be output as 8-bit unsigned stereo.
1980     */
1981    if((fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
1982    {
1983        /*
1984         * Convert each 16-bit signed stereo sample to 8-bit unsigned 
1985         * stereo using rounding.
1986         */
1987        psSrc = (s16 *)src;
1988        count = count/2;
1989        while(count--)
1990        {
1991            *(pucDst++) = (u8)(((s16)(*psSrc++) + (s16)0x8000) >> 8);
1992        }
1993    }
1994    /*
1995     * See if the data should be output at 8-bit unsigned mono.
1996     */
1997    else if(!(fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
1998    {
1999        /*
2000         * Convert each 16-bit signed stereo sample to 8-bit unsigned 
2001         * mono using averaging and rounding.
2002         */
2003        psSrc = (s16 *)src;
2004        count = count/2;
2005        while(count--)
2006        {
2007            s32AudioSample = ((*psSrc)+(*(psSrc + 1)))/2 + (s32)0x80;
2008            if(s32AudioSample > 0x7fff)
2009                s32AudioSample = 0x7fff;
2010            *(pucDst++) = (u8)(((s16)s32AudioSample + (s16)0x8000) >> 8);
2011            psSrc += 2;
2012        }
2013    }
2014    /*
2015     * See if the data should be output at 16-bit signed mono.
2016     */
2017    else if(!(fmt & CS_FMT_STEREO) && (fmt & CS_FMT_16BIT))
2018    {
2019        /*
2020         * Convert each 16-bit signed stereo sample to 16-bit signed 
2021         * mono using averaging.
2022         */
2023        psSrc = (s16 *)src;
2024        count = count/2;
2025        while(count--)
2026        {
2027            *(psDst++) = (s16)((*psSrc)+(*(psSrc + 1)))/2;
2028            psSrc += 2;
2029        }
2030    }
2031}
2032
2033/*
2034 * cs_copy_to_user()
2035 * replacement for the standard copy_to_user, to allow for a conversion from
2036 * 16 bit to 8 bit and from stereo to mono, if the record conversion is active.  
2037 * The current CS46xx/CS4280 static image only records in 16bit unsigned Stereo, 
2038 * so we convert from any of the other format combinations.
2039 */
2040static unsigned cs_copy_to_user(
2041        struct cs_state *s, 
2042        void *dest, 
2043        void *hwsrc, 
2044        unsigned cnt, 
2045        unsigned *copied)
2046{
2047        struct dmabuf *dmabuf = &s->dmabuf;
2048        void *src = hwsrc;  /* default to the standard destination buffer addr */
2049
2050        CS_DBGOUT(CS_FUNCTION, 6, printk(KERN_INFO 
2051                "cs_copy_to_user()+ fmt=0x%x cnt=%d dest=0x%.8x\n",
2052                dmabuf->fmt,(unsigned)cnt,(unsigned)dest) );
2053
2054        if(cnt > dmabuf->dmasize)
2055        {
2056                cnt = dmabuf->dmasize;
2057        }
2058        if(!cnt)
2059        {
2060                *copied = 0;
2061                return 0;
2062        }
2063        if(dmabuf->divisor != 1)
2064        {
2065                if(!dmabuf->tmpbuff)
2066                {
2067                        *copied = cnt/dmabuf->divisor;
2068                        return 0;
2069                }
2070
2071                CopySamples((char *)dmabuf->tmpbuff, (char *)hwsrc, cnt, 
2072                        dmabuf->fmt, dmabuf);
2073                src = dmabuf->tmpbuff;
2074                cnt = cnt/dmabuf->divisor;
2075        }
2076        if (copy_to_user(dest, src, cnt))
2077        {
2078                CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_ERR 
2079                        "cs46xx: cs_copy_to_user()- fault dest=0x%x src=0x%x cnt=%d\n",
2080                                (unsigned)dest,(unsigned)src,cnt) );
2081                *copied = 0;
2082                return -EFAULT;
2083        }
2084        *copied = cnt;
2085        CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO 
2086                "cs46xx: cs_copy_to_user()- copied bytes is %d \n",cnt) );
2087        return 0;
2088}
2089
2090/* in this loop, dmabuf.count signifies the amount of data that is waiting to be copied to
2091   the user's buffer.  it is filled by the dma machine and drained by this loop. */
2092static ssize_t cs_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
2093{
2094        struct cs_card *card = (struct cs_card *) file->private_data;
2095        struct cs_state *state;
2096        DECLARE_WAITQUEUE(wait, current);
2097        struct dmabuf *dmabuf;
2098        ssize_t ret = 0;
2099        unsigned long flags;
2100        unsigned swptr;
2101        int cnt;
2102        unsigned copied=0;
2103
2104        CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4, 
2105                printk("cs46xx: cs_read()+ %d\n",count) );
2106        state = (struct cs_state *)card->states[0];
2107        if(!state)
2108                return -ENODEV;
2109        dmabuf = &state->dmabuf;
2110
2111        if (ppos != &file->f_pos)
2112                return -ESPIPE;
2113        if (dmabuf->mapped)
2114                return -ENXIO;
2115        if (!access_ok(VERIFY_WRITE, buffer, count))
2116                return -EFAULT;
2117        
2118        down(&state->sem);
2119        if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
2120                goto out2;
2121
2122        add_wait_queue(&state->dmabuf.wait, &wait);
2123        while (count > 0) {
2124                while(!(card->pm.flags & CS46XX_PM_IDLE))
2125                {
2126                        schedule();
2127                        if (signal_pending(current)) {
2128                                if(!ret) ret = -ERESTARTSYS;
2129                                goto out;
2130                        }
2131                }
2132                spin_lock_irqsave(&state->card->lock, flags);
2133                swptr = dmabuf->swptr;
2134                cnt = dmabuf->dmasize - swptr;
2135                if (dmabuf->count < cnt)
2136                        cnt = dmabuf->count;
2137                if (cnt <= 0)
2138                        __set_current_state(TASK_INTERRUPTIBLE);
2139                spin_unlock_irqrestore(&state->card->lock, flags);
2140
2141                if (cnt > (count * dmabuf->divisor))
2142                        cnt = count * dmabuf->divisor;
2143                if (cnt <= 0) {
2144                        /* buffer is empty, start the dma machine and wait for data to be
2145                           recorded */
2146                        start_adc(state);
2147                        if (file->f_flags & O_NONBLOCK) {
2148                                if (!ret) ret = -EAGAIN;
2149                                goto out;
2150                        }
2151                        up(&state->sem);
2152                        schedule();
2153                        if (signal_pending(current)) {
2154                                if(!ret) ret = -ERESTARTSYS;
2155                                goto out;
2156                        }
2157                        down(&state->sem);
2158                        if (dmabuf->mapped) 
2159                        {
2160                                if(!ret)
2161                                        ret = -ENXIO;
2162                                goto out;
2163                        }
2164                        continue;
2165                }
2166
2167                CS_DBGOUT(CS_WAVE_READ, 2, printk(KERN_INFO 
2168                        "_read() copy_to cnt=%d count=%d ", cnt,count) );
2169                CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO 
2170                        " .dmasize=%d .count=%d buffer=0x%.8x ret=%d\n",
2171                        dmabuf->dmasize,dmabuf->count,(unsigned)buffer,ret) );
2172
2173                if (cs_copy_to_user(state, buffer, 
2174                        (void *)((unsigned)dmabuf->rawbuf + swptr), cnt, &copied))
2175                {
2176                        if (!ret) ret = -EFAULT;
2177                        goto out;
2178                }
2179                swptr = (swptr + cnt) % dmabuf->dmasize;
2180                spin_lock_irqsave(&card->lock, flags);
2181                dmabuf->swptr = swptr;
2182                dmabuf->count -= cnt;
2183                spin_unlock_irqrestore(&card->lock, flags);
2184                count -= copied;
2185                buffer += copied;
2186                ret += copied;
2187                start_adc(state);
2188        }
2189out:
2190        remove_wait_queue(&state->dmabuf.wait, &wait);
2191out2:
2192        up(&state->sem);
2193        set_current_state(TASK_RUNNING);
2194        CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4, 
2195                printk("cs46xx: cs_read()- %d\n",ret) );
2196        return ret;
2197}
2198
2199/* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
2200   the soundcard.  it is drained by the dma machine and filled by this loop. */
2201static ssize_t cs_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
2202{
2203        struct cs_card *card = (struct cs_card *) file->private_data;
2204        struct cs_state *state;
2205        DECLARE_WAITQUEUE(wait, current);
2206        struct dmabuf *dmabuf;
2207        ssize_t ret;
2208        unsigned long flags;
2209        unsigned swptr;
2210        int cnt;
2211
2212        CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 4,
2213                printk("cs46xx: cs_write called, count = %d\n", count) );
2214        state = (struct cs_state *)card->states[1];
2215        if(!state)
2216                return -ENODEV;
2217        if (!access_ok(VERIFY_READ, buffer, count))
2218                return -EFAULT;
2219        dmabuf = &state->dmabuf;
2220
2221        if (ppos != &file->f_pos)
2222                return -ESPIPE;
2223
2224        down(&state->sem);
2225        if (dmabuf->mapped)
2226        {
2227                ret = -ENXIO;
2228                goto out;
2229        }
2230
2231        if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
2232                goto out;
2233        add_wait_queue(&state->dmabuf.wait, &wait);
2234        ret = 0;
2235/*
2236* Start the loop to read from the user's buffer and write to the dma buffer.
2237* check for PM events and underrun/overrun in the loop.
2238*/
2239        while (count > 0) {
2240                while(!(card->pm.flags & CS46XX_PM_IDLE))
2241                {
2242                        schedule();
2243                        if (signal_pending(current)) {
2244                                if(!ret) ret = -ERESTARTSYS;
2245                                goto out;
2246                        }
2247                }
2248                spin_lock_irqsave(&state->card->lock, flags);
2249                if (dmabuf->count < 0) {
2250                        /* buffer underrun, we are recovering from sleep_on_timeout,
2251                           resync hwptr and swptr */
2252                        dmabuf->count = 0;
2253                        dmabuf->swptr = dmabuf->hwptr;
2254                }
2255                if (dmabuf->underrun)
2256                {
2257                        dmabuf->underrun = 0;
2258                        dmabuf->hwptr = cs_get_dma_addr(state);
2259                        dmabuf->swptr = dmabuf->hwptr;
2260                }
2261
2262                swptr = dmabuf->swptr;
2263                cnt = dmabuf->dmasize - swptr;
2264                if (dmabuf->count + cnt > dmabuf->dmasize)
2265                        cnt = dmabuf->dmasize - dmabuf->count;
2266                if (cnt <= 0)
2267                        __set_current_state(TASK_INTERRUPTIBLE);
2268                spin_unlock_irqrestore(&state->card->lock, flags);
2269
2270                if (cnt > count)
2271                        cnt = count;
2272                if (cnt <= 0) {
2273                        /* buffer is full, start the dma machine and wait for data to be
2274                           played */
2275                        start_dac(state);
2276                        if (file->f_flags & O_NONBLOCK) {
2277                                if (!ret) ret = -EAGAIN;
2278                                goto out;
2279                        }
2280                        up(&state->sem);
2281                        schedule();
2282                        if (signal_pending(current)) {
2283                                if(!ret) ret = -ERESTARTSYS;
2284                                goto out;
2285                        }
2286                        down(&state->sem);
2287                        if (dmabuf->mapped)
2288                        {
2289                                if(!ret)
2290                                        ret = -ENXIO;
2291                                goto out;
2292                        }
2293                        continue;
2294                }
2295                if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
2296                        if (!ret) ret = -EFAULT;
2297                        goto out;
2298                }
2299                spin_lock_irqsave(&state->card->lock, flags);
2300                swptr = (swptr + cnt) % dmabuf->dmasize;
2301                dmabuf->swptr = swptr;
2302                dmabuf->count += cnt;
2303                if(dmabuf->count > dmabuf->dmasize)
2304                {
2305                        CS_DBGOUT(CS_WAVE_WRITE | CS_ERROR, 2, printk(
2306                            "cs46xx: cs_write() d->count > dmasize - resetting\n"));
2307                        dmabuf->count = dmabuf->dmasize;
2308                }
2309                dmabuf->endcleared = 0;
2310                spin_unlock_irqrestore(&state->card->lock, flags);
2311
2312                count -= cnt;
2313                buffer += cnt;
2314                ret += cnt;
2315                start_dac(state);
2316        }
2317out:
2318        up(&state->sem);
2319        remove_wait_queue(&state->dmabuf.wait, &wait);
2320        set_current_state(TASK_RUNNING);
2321
2322        CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 2, 
2323                printk("cs46xx: cs_write()- ret=0x%x\n", ret) );
2324        return ret;
2325}
2326
2327static unsigned int cs_poll(struct file *file, struct poll_table_struct *wait)
2328{
2329        struct cs_card *card = (struct cs_card *)file->private_data;
2330        struct dmabuf *dmabuf;
2331        struct cs_state *state;
2332
2333        unsigned long flags;
2334        unsigned int mask = 0;
2335
2336        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()+ \n"));
2337        if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
2338        {
2339                return -EINVAL;
2340        }
2341        if (file->f_mode & FMODE_WRITE)
2342        {
2343                state = card->states[1];
2344                if(state)
2345                {
2346                        dmabuf = &state->dmabuf;
2347                        poll_wait(file, &dmabuf->wait, wait);
2348                }
2349        }
2350        if (file->f_mode & FMODE_READ)
2351        {
2352                state = card->states[0];
2353                if(state)
2354                {
2355                        dmabuf = &state->dmabuf;
2356                        poll_wait(file, &dmabuf->wait, wait);
2357                }
2358        }
2359
2360        spin_lock_irqsave(&card->lock, flags);
2361        cs_update_ptr(card, CS_FALSE);
2362        if (file->f_mode & FMODE_READ) {
2363                state = card->states[0];
2364                if(state)
2365                {
2366                        dmabuf = &state->dmabuf;
2367                        if (dmabuf->count >= (signed)dmabuf->fragsize)
2368                                mask |= POLLIN | POLLRDNORM;
2369                }
2370        }
2371        if (file->f_mode & FMODE_WRITE) {
2372                state = card->states[1];
2373                if(state)
2374                {
2375                        dmabuf = &state->dmabuf;
2376                        if (dmabuf->mapped) {
2377                                if (dmabuf->count >= (signed)dmabuf->fragsize)
2378                                    mask |= POLLOUT | POLLWRNORM;
2379                        } else {
2380                                if ((signed)dmabuf->dmasize >= dmabuf->count 
2381                                        + (signed)dmabuf->fragsize)
2382                                    mask |= POLLOUT | POLLWRNORM;
2383                        }
2384                }
2385        }
2386        spin_unlock_irqrestore(&card->lock, flags);
2387
2388        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()- (0x%x) \n",
2389                mask));
2390        return mask;
2391}
2392
2393/*
2394 *      We let users mmap the ring buffer. Its not the real DMA buffer but
2395 *      that side of the code is hidden in the IRQ handling. We do a software
2396 *      emulation of DMA from a 64K or so buffer into a 2K FIFO. 
2397 *      (the hardware probably deserves a moan here but Crystal send me nice
2398 *      toys ;)).
2399 */
2400 
2401static int cs_mmap(struct file *file, struct vm_area_struct *vma)
2402{
2403        struct cs_card *card = (struct cs_card *)file->private_data;
2404        struct cs_state *state;
2405        struct dmabuf *dmabuf;
2406        int ret = 0;
2407        unsigned long size;
2408
2409        CS_DBGOUT(CS_FUNCTION | CS_PARMS, 2, printk("cs46xx: cs_mmap()+ file=0x%x %s %s\n", 
2410                (unsigned)file, vma->vm_flags & VM_WRITE ? "VM_WRITE" : "",
2411                vma->vm_flags & VM_READ ? "VM_READ" : "") );
2412
2413        if (vma->vm_flags & VM_WRITE) {
2414                state = card->states[1];
2415                if(state)
2416                {
2417                        CS_DBGOUT(CS_OPEN, 2, printk(
2418                          "cs46xx: cs_mmap() VM_WRITE - state TRUE prog_dmabuf DAC\n") );
2419                        if ((ret = prog_dmabuf(state)) != 0)
2420                                return ret;
2421                }
2422        } else if (vma->vm_flags & VM_READ) {
2423                state = card->states[0];
2424                if(state)
2425                {
2426                        CS_DBGOUT(CS_OPEN, 2, printk(
2427                          "cs46xx: cs_mmap() VM_READ - state TRUE prog_dmabuf ADC\n") );
2428                        if ((ret = prog_dmabuf(state)) != 0)
2429                                return ret;
2430                }
2431        } else {
2432                CS_DBGOUT(CS_ERROR, 2, printk(
2433                  "cs46xx: cs_mmap() return -EINVAL\n") );
2434                return -EINVAL;
2435        }
2436
2437/*
2438 * For now ONLY support playback, but seems like the only way to use
2439 * mmap() is to open an FD with RDWR, just read or just write access
2440 * does not function, get an error back from the kernel.
2441 * Also, QuakeIII opens with RDWR!  So, there must be something
2442 * to needing read/write access mapping.  So, allow read/write but 
2443 * use the DAC only.
2444 */
2445        state = card->states[1];  
2446        if(!(unsigned)state)
2447        {
2448                ret = -EINVAL;
2449                goto out;
2450        }
2451
2452        down(&state->sem);      
2453        dmabuf = &state->dmabuf;
2454        if (cs4x_pgoff(vma) != 0)
2455        {
2456                ret = -EINVAL;
2457                goto out;
2458        }
2459        size = vma->vm_end - vma->vm_start;
2460
2461        CS_DBGOUT(CS_PARMS, 2, printk("cs46xx: cs_mmap(): size=%d\n",(unsigned)size) );
2462
2463        if (size > (PAGE_SIZE << dmabuf->buforder))
2464        {
2465                ret = -EINVAL;
2466                goto out;
2467        }
2468        if (remap_page_range(vma->vm_start, virt_to_phys(dmabuf->rawbuf),
2469                             size, vma->vm_page_prot))
2470        {
2471                ret = -EAGAIN;
2472                goto out;
2473        }
2474        dmabuf->mapped = 1;
2475
2476        CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_mmap()-\n") );
2477out:
2478        up(&state->sem);
2479        return ret;     
2480}
2481
2482static int cs_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2483{
2484        struct cs_card *card = (struct cs_card *)file->private_data;
2485        struct cs_state *state;
2486        struct dmabuf *dmabuf=0;
2487        unsigned long flags;
2488        audio_buf_info abinfo;
2489        count_info cinfo;
2490        int val, valsave, mapped, ret;
2491
2492        state = (struct cs_state *)card->states[0];
2493        if(state)
2494        {
2495                dmabuf = &state->dmabuf;
2496                mapped = (file->f_mode & FMODE_READ) && dmabuf->mapped;
2497        }
2498        state = (struct cs_state *)card->states[1];
2499        if(state)
2500        {
2501                dmabuf = &state->dmabuf;
2502                mapped |= (file->f_mode & FMODE_WRITE) && dmabuf->mapped;
2503        }
2504                
2505#if CSDEBUG
2506        printioctl(cmd);
2507#endif
2508
2509        switch (cmd) 
2510        {
2511        case OSS_GETVERSION:
2512                return put_user(SOUND_VERSION, (int *)arg);
2513
2514        case SNDCTL_DSP_RESET:
2515                /* FIXME: spin_lock ? */
2516                if (file->f_mode & FMODE_WRITE) {
2517                        state = (struct cs_state *)card->states[1];
2518                        if(state)
2519                        {
2520                                dmabuf = &state->dmabuf;
2521                                stop_dac(state);
2522                                synchronize_irq();
2523                                dmabuf->ready = 0;
2524                                resync_dma_ptrs(state);
2525                                dmabuf->swptr = dmabuf->hwptr = 0;
2526                                dmabuf->count = dmabuf->total_bytes = 0;
2527                                dmabuf->blocks = 0;
2528                                dmabuf->SGok = 0;
2529                        }
2530                }
2531                if (file->f_mode & FMODE_READ) {
2532                        state = (struct cs_state *)card->states[0];
2533                        if(state)
2534                        {
2535                                dmabuf = &state->dmabuf;
2536                                stop_adc(state);
2537                                synchronize_irq();
2538                                resync_dma_ptrs(state);
2539                                dmabuf->ready = 0;
2540                                dmabuf->swptr = dmabuf->hwptr = 0;
2541                                dmabuf->count = dmabuf->total_bytes = 0;
2542                                dmabuf->blocks = 0;
2543                                dmabuf->SGok = 0;
2544                        }
2545                }
2546                CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_RESET()-\n") );
2547                return 0;
2548
2549        case SNDCTL_DSP_SYNC:
2550                if (file->f_mode & FMODE_WRITE)
2551                        return drain_dac(state, file->f_flags & O_NONBLOCK);
2552                return 0;
2553
2554        case SNDCTL_DSP_SPEED: /* set sample rate */
2555                if (get_user(val, (int *)arg))
2556                        return -EFAULT;
2557                if (val >= 0) {
2558                        if (file->f_mode & FMODE_READ) {
2559                                state = (struct cs_state *)card->states[0];
2560                                if(state)
2561                                {
2562                                        dmabuf = &state->dmabuf;
2563                                        stop_adc(state);
2564                                        dmabuf->ready = 0;
2565                                        dmabuf->SGok = 0;
2566                                        cs_set_adc_rate(state, val);
2567                                        cs_set_divisor(dmabuf);
2568                                }
2569                        }
2570                        if (file->f_mode & FMODE_WRITE) {
2571                                state = (struct cs_state *)card->states[1];
2572                                if(state)
2573                                {
2574                                        dmabuf = &state->dmabuf;
2575                                        stop_dac(state);
2576                                        dmabuf->ready = 0;
2577                                        dmabuf->SGok = 0;
2578                                        cs_set_dac_rate(state, val);
2579                                        cs_set_divisor(dmabuf);
2580                                }
2581                        }
2582                        CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2583                            "cs46xx: cs_ioctl() DSP_SPEED %s %s %d\n",
2584                                file->f_mode & FMODE_WRITE ? "DAC" : "",
2585                                file->f_mode & FMODE_READ ? "ADC" : "",
2586                                dmabuf->rate ) );
2587                        return put_user(dmabuf->rate, (int *)arg);
2588                }
2589                return put_user(0, (int *)arg);
2590
2591        case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
2592                if (get_user(val, (int *)arg))
2593                        return -EFAULT;
2594                if (file->f_mode & FMODE_WRITE) {
2595                        state = (struct cs_state *)card->states[1];
2596                        if(state)
2597                        {
2598                                dmabuf = &state->dmabuf;
2599                                stop_dac(state);
2600                                dmabuf->ready = 0;
2601                                dmabuf->SGok = 0;
2602                                if(val)
2603                                        dmabuf->fmt |= CS_FMT_STEREO;
2604                                else
2605                                        dmabuf->fmt &= ~CS_FMT_STEREO;
2606                                cs_set_divisor(dmabuf);
2607                                CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2608                                    "cs46xx: DSP_STEREO() DAC %s\n",
2609                                    (dmabuf->fmt & CS_FMT_STEREO) ?
2610                                        "STEREO":"MONO") );
2611                        }
2612                }
2613                if (file->f_mode & FMODE_READ) {
2614                        state = (struct cs_state *)card->states[0];
2615                        if(state)
2616                        {
2617                                dmabuf = &state->dmabuf;
2618                                stop_adc(state);
2619                                dmabuf->ready = 0;
2620                                dmabuf->SGok = 0;
2621                                if(val)
2622                                        dmabuf->fmt |= CS_FMT_STEREO;
2623                                else
2624                                        dmabuf->fmt &= ~CS_FMT_STEREO;
2625                                cs_set_divisor(dmabuf);
2626                                CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2627                                    "cs46xx: DSP_STEREO() ADC %s\n",
2628                                    (dmabuf->fmt & CS_FMT_STEREO) ?
2629                                        "STEREO":"MONO") );
2630                        }
2631                }
2632                return 0;
2633
2634        case SNDCTL_DSP_GETBLKSIZE:
2635                if (file->f_mode & FMODE_WRITE) {
2636                        state = (struct cs_state *)card->states[1];
2637                        if(state)
2638                        {
2639                                dmabuf = &state->dmabuf;
2640                                if ((val = prog_dmabuf(state)))
2641                                        return val;
2642                                return put_user(dmabuf->fragsize, (int *)arg);
2643                        }
2644                }
2645                if (file->f_mode & FMODE_READ) {
2646                        state = (struct cs_state *)card->states[0];
2647                        if(state)
2648                        {
2649                                dmabuf = &state->dmabuf;
2650                                if ((val = prog_dmabuf(state)))
2651                                        return val;
2652                                return put_user(dmabuf->fragsize/dmabuf->divisor, 
2653                                                (int *)arg);
2654                        }
2655                }
2656                return put_user(0, (int *)arg);
2657
2658        case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
2659                return put_user(AFMT_S16_LE | AFMT_U8, (int *)arg);
2660
2661        case SNDCTL_DSP_SETFMT: /* Select sample format */
2662                if (get_user(val, (int *)arg))
2663                        return -EFAULT;
2664                CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2665                    "cs46xx: cs_ioctl() DSP_SETFMT %s %s %s %s\n",
2666                        file->f_mode & FMODE_WRITE ? "DAC" : "",
2667                        file->f_mode & FMODE_READ ? "ADC" : "",
2668                        val == AFMT_S16_LE ? "16Bit Signed" : "",
2669                        val == AFMT_U8 ? "8Bit Unsigned" : "") );
2670                valsave = val;
2671                if (val != AFMT_QUERY) {
2672                        if(val==AFMT_S16_LE || val==AFMT_U8)
2673                        {
2674                                if (file->f_mode & FMODE_WRITE) {
2675                                        state = (struct cs_state *)card->states[1];
2676                                        if(state)
2677                                        {
2678                                                dmabuf = &state->dmabuf;
2679                                                stop_dac(state);
2680                                                dmabuf->ready = 0;
2681                                                dmabuf->SGok = 0;
2682                                                if(val==AFMT_S16_LE)
2683                                                        dmabuf->fmt |= CS_FMT_16BIT;
2684                                                else
2685                                                        dmabuf->fmt &= ~CS_FMT_16BIT;
2686                                                cs_set_divisor(dmabuf);
2687                                                if((ret = prog_dmabuf(state)))
2688                                                        return ret;
2689                                        }
2690                                }
2691                                if (file->f_mode & FMODE_READ) {
2692                                        val = valsave;
2693                                        state = (struct cs_state *)card->states[0];
2694                                        if(state)
2695                                        {
2696                                                dmabuf = &state->dmabuf;
2697                                                stop_adc(state);
2698                                                dmabuf->ready = 0;
2699                                                dmabuf->SGok = 0;
2700                                                if(val==AFMT_S16_LE)
2701                                                        dmabuf->fmt |= CS_FMT_16BIT;
2702                                                else
2703                                                        dmabuf->fmt &= ~CS_FMT_16BIT;
2704                                                cs_set_divisor(dmabuf);
2705                                                if((ret = prog_dmabuf(state)))
2706                                                        return ret;
2707                                        }
2708                                }
2709                        }
2710                        else
2711                        {
2712                                CS_DBGOUT(CS_IOCTL | CS_ERROR, 2, printk(
2713                                    "cs46xx: DSP_SETFMT() Unsupported format (0x%x)\n",
2714                                        valsave) );
2715                        }
2716                }
2717                else
2718                {
2719                        if(file->f_mode & FMODE_WRITE)
2720                        {
2721                                state = (struct cs_state *)card->states[1];
2722                                if(state)
2723                                        dmabuf = &state->dmabuf;
2724                        }
2725                        else if(file->f_mode & FMODE_READ)
2726                        {
2727                                state = (struct cs_state *)card->states[0];
2728                                if(state)
2729                                        dmabuf = &state->dmabuf;
2730                        }
2731                }
2732                if(dmabuf)
2733                {
2734                        if(dmabuf->fmt & CS_FMT_16BIT)
2735                                return put_user(AFMT_S16_LE, (int *)arg);
2736                        else
2737                                return put_user(AFMT_U8, (int *)arg);
2738                }
2739                return put_user(0, (int *)arg);
2740
2741        case SNDCTL_DSP_CHANNELS:
2742                if (get_user(val, (int *)arg))
2743                        return -EFAULT;
2744                if (val != 0) {
2745                        if (file->f_mode & FMODE_WRITE) {
2746                                state = (struct cs_state *)card->states[1];
2747                                if(state)
2748                                {
2749                                        dmabuf = &state->dmabuf;
2750                                        stop_dac(state);
2751                                        dmabuf->ready = 0;
2752                                        dmabuf->SGok = 0;
2753                                        if(val>1)
2754                                                dmabuf->fmt |= CS_FMT_STEREO;
2755                                        else
2756                                                dmabuf->fmt &= ~CS_FMT_STEREO;
2757                                        cs_set_divisor(dmabuf);
2758                                        if (prog_dmabuf(state))
2759                                                return 0;
2760                                }
2761                        }
2762                        if (file->f_mode & FMODE_READ) {
2763                                state = (struct cs_state *)card->states[0];
2764                                if(state)
2765                                {
2766                                        dmabuf = &state->dmabuf;
2767                                        stop_adc(state);
2768                                        dmabuf->ready = 0;
2769                                        dmabuf->SGok = 0;
2770                                        if(val>1)
2771                                                dmabuf->fmt |= CS_FMT_STEREO;
2772                                        else
2773                                                dmabuf->fmt &= ~CS_FMT_STEREO;
2774                                        cs_set_divisor(dmabuf);
2775                                        if (prog_dmabuf(state))
2776                                                return 0;
2777                                }
2778                        }
2779                }
2780                return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
2781                                (int *)arg);
2782
2783        case SNDCTL_DSP_POST:
2784                /*
2785                 * There will be a longer than normal pause in the data.
2786                 * so... do nothing, because there is nothing that we can do.
2787                 */
2788                return 0;
2789
2790        case SNDCTL_DSP_SUBDIVIDE:
2791                if (file->f_mode & FMODE_WRITE) {
2792                        state = (struct cs_state *)card->states[1];
2793                        if(state)
2794                        {
2795                                dmabuf = &state->dmabuf;
2796                                if (dmabuf->subdivision)
2797                                        return -EINVAL;
2798                                if (get_user(val, (int *)arg))
2799                                        return -EFAULT;
2800                                if (val != 1 && val != 2)
2801                                        return -EINVAL;
2802                                dmabuf->subdivision = val;
2803                        }
2804                }
2805                if (file->f_mode & FMODE_READ) {
2806                        state = (struct cs_state *)card->states[0];
2807                        if(state)
2808                        {
2809                                dmabuf = &state->dmabuf;
2810                                if (dmabuf->subdivision)
2811                                        return -EINVAL;
2812                                if (get_user(val, (int *)arg))
2813                                        return -EFAULT;
2814                                if (val != 1 && val != 2)
2815                                        return -EINVAL;
2816                                dmabuf->subdivision = val;
2817                        }
2818                }
2819                return 0;
2820
2821        case SNDCTL_DSP_SETFRAGMENT:
2822                if (get_user(val, (int *)arg))
2823                        return -EFAULT;
2824
2825                if (file->f_mode & FMODE_WRITE) {
2826                        state = (struct cs_state *)card->states[1];
2827                        if(state)
2828                        {
2829                                dmabuf = &state->dmabuf;
2830                                dmabuf->ossfragshift = val & 0xffff;
2831                                dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2832                        }
2833                }
2834                if (file->f_mode & FMODE_READ) {
2835                        state = (struct cs_state *)card->states[0];
2836                        if(state)
2837                        {
2838                                dmabuf = &state->dmabuf;
2839                                dmabuf->ossfragshift = val & 0xffff;
2840                                dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2841                        }
2842                }
2843                return 0;
2844
2845        case SNDCTL_DSP_GETOSPACE:
2846                if (!(file->f_mode & FMODE_WRITE))
2847                        return -EINVAL;
2848                state = (struct cs_state *)card->states[1];
2849                if(state)
2850                {
2851                        dmabuf = &state->dmabuf;
2852                        spin_lock_irqsave(&state->card->lock, flags);
2853                        cs_update_ptr(card, CS_TRUE);
2854                        abinfo.fragsize = dmabuf->fragsize;
2855                        abinfo.fragstotal = dmabuf->numfrag;
2856                /*
2857                 * for mmap we always have total space available
2858                 */
2859                        if (dmabuf->mapped)
2860                                abinfo.bytes = dmabuf->dmasize;
2861                        else
2862                                abinfo.bytes = dmabuf->dmasize - dmabuf->count;
2863
2864                        abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2865                        spin_unlock_irqrestore(&state->card->lock, flags);
2866                        return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2867                }
2868                return -ENODEV;
2869
2870        case SNDCTL_DSP_GETISPACE:
2871                if (!(file->f_mode & FMODE_READ))
2872                        return -EINVAL;
2873                state = (struct cs_state *)card->states[0];
2874                if(state)
2875                {
2876                        dmabuf = &state->dmabuf;
2877                        spin_lock_irqsave(&state->card->lock, flags);
2878                        cs_update_ptr(card, CS_TRUE);
2879                        abinfo.fragsize = dmabuf->fragsize/dmabuf->divisor;
2880                        abinfo.bytes = dmabuf->count/dmabuf->divisor;
2881                        abinfo.fragstotal = dmabuf->numfrag;
2882                        abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2883                        spin_unlock_irqrestore(&state->card->lock, flags);
2884                        return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2885                }
2886                return -ENODEV;
2887
2888        case SNDCTL_DSP_NONBLOCK:
2889                file->f_flags |= O_NONBLOCK;
2890                return 0;
2891
2892        case SNDCTL_DSP_GETCAPS:
2893                return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP,
2894                            (int *)arg);
2895
2896        case SNDCTL_DSP_GETTRIGGER:
2897                val = 0;
2898                CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()+\n") );
2899                if (file->f_mode & FMODE_WRITE)
2900                {
2901                        state = (struct cs_state *)card->states[1];
2902                        if(state)
2903                        {
2904                                dmabuf = &state->dmabuf;
2905                                if(dmabuf->enable & DAC_RUNNING)
2906                                        val |= PCM_ENABLE_INPUT;
2907                        }
2908                }
2909                if (file->f_mode & FMODE_READ)
2910                {
2911                        if(state)
2912                        {
2913                                state = (struct cs_state *)card->states[0];
2914                                dmabuf = &state->dmabuf;
2915                                if(dmabuf->enable & ADC_RUNNING)
2916                                        val |= PCM_ENABLE_OUTPUT;
2917                        }
2918                }
2919                CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()- val=0x%x\n",val) );
2920                return put_user(val, (int *)arg);
2921
2922        case SNDCTL_DSP_SETTRIGGER:
2923                if (get_user(val, (int *)arg))
2924                        return -EFAULT;
2925                if (file->f_mode & FMODE_READ) {
2926                        state = (struct cs_state *)card->states[0];
2927                        if(state)
2928                        {
2929                                dmabuf = &state->dmabuf;
2930                                if (val & PCM_ENABLE_INPUT) {
2931                                        if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2932                                                return ret;
2933                                        start_adc(state);
2934                                } else
2935                                        stop_adc(state);
2936                        }
2937                }
2938                if (file->f_mode & FMODE_WRITE) {
2939                        state = (struct cs_state *)card->states[1];
2940                        if(state)
2941                        {
2942                                dmabuf = &state->dmabuf;
2943                                if (val & PCM_ENABLE_OUTPUT) {
2944                                        if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2945                                                return ret;
2946                                        start_dac(state);
2947                                } else
2948                                        stop_dac(state);
2949                        }
2950                }
2951                return 0;
2952
2953        case SNDCTL_DSP_GETIPTR:
2954                if (!(file->f_mode & FMODE_READ))
2955                        return -EINVAL;
2956                state = (struct cs_state *)card->states[0];
2957                if(state)
2958                {
2959                        dmabuf = &state->dmabuf;
2960                        spin_lock_irqsave(&state->card->lock, flags);
2961                        cs_update_ptr(card, CS_TRUE);
2962                        cinfo.bytes = dmabuf->total_bytes/dmabuf->divisor;
2963                        cinfo.blocks = dmabuf->count/dmabuf->divisor >> dmabuf->fragshift;
2964                        cinfo.ptr = dmabuf->hwptr/dmabuf->divisor;
2965                        spin_unlock_irqrestore(&state->card->lock, flags);
2966                        return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
2967                }
2968                return -ENODEV;
2969
2970        case SNDCTL_DSP_GETOPTR:
2971                if (!(file->f_mode & FMODE_WRITE))
2972                        return -EINVAL;
2973                state = (struct cs_state *)card->states[1];
2974                if(state)
2975                {
2976                        dmabuf = &state->dmabuf;
2977                        spin_lock_irqsave(&state->card->lock, flags);
2978                        cs_update_ptr(card, CS_TRUE);
2979                        cinfo.bytes = dmabuf->total_bytes;
2980                        if (dmabuf->mapped)
2981                        {
2982                                cinfo.blocks = (cinfo.bytes >> dmabuf->fragshift) 
2983                                                        - dmabuf->blocks;
2984                                CS_DBGOUT(CS_PARMS, 8, 
2985                                        printk("total_bytes=%d blocks=%d dmabuf->blocks=%d\n", 
2986                                        cinfo.bytes,cinfo.blocks,dmabuf->blocks) );
2987                                dmabuf->blocks = cinfo.bytes >> dmabuf->fragshift;
2988                        }
2989                        else
2990                        {
2991                                cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
2992                        }
2993                        cinfo.ptr = dmabuf->hwptr;
2994
2995                        CS_DBGOUT(CS_PARMS, 4, printk(
2996                            "cs46xx: GETOPTR bytes=%d blocks=%d ptr=%d\n",
2997                                cinfo.bytes,cinfo.blocks,cinfo.ptr) );
2998                        spin_unlock_irqrestore(&state->card->lock, flags);
2999                        return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
3000                }
3001                return -ENODEV;
3002
3003        case SNDCTL_DSP_SETDUPLEX:
3004                return 0;
3005
3006        case SNDCTL_DSP_GETODELAY:
3007                if (!(file->f_mode & FMODE_WRITE))
3008                        return -EINVAL;
3009                state = (struct cs_state *)card->states[1];
3010                if(state)
3011                {
3012                        dmabuf = &state->dmabuf;
3013                        spin_lock_irqsave(&state->card->lock, flags);
3014                        cs_update_ptr(card, CS_TRUE);
3015                        val = dmabuf->count;
3016                        spin_unlock_irqrestore(&state->card->lock, flags);
3017                }
3018                else
3019                        val = 0;
3020                return put_user(val, (int *)arg);
3021
3022        case SOUND_PCM_READ_RATE:
3023                if(file->f_mode & FMODE_READ)
3024                        state = (struct cs_state *)card->states[0];
3025                else 
3026                        state = (struct cs_state *)card->states[1];
3027                if(state)
3028                {
3029                        dmabuf = &state->dmabuf;
3030                        return put_user(dmabuf->rate, (int *)arg);
3031                }
3032                return put_user(0, (int *)arg);
3033                
3034
3035        case SOUND_PCM_READ_CHANNELS:
3036                if(file->f_mode & FMODE_READ)
3037                        state = (struct cs_state *)card->states[0];
3038                else 
3039                        state = (struct cs_state *)card->states[1];
3040                if(state)
3041                {
3042                        dmabuf = &state->dmabuf;
3043                        return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
3044                                (int *)arg);
3045                }
3046                return put_user(0, (int *)arg);
3047
3048        case SOUND_PCM_READ_BITS:
3049                if(file->f_mode & FMODE_READ)
3050                        state = (struct cs_state *)card->states[0];
3051                else 
3052                        state = (struct cs_state *)card->states[1];
3053                if(state)
3054                {
3055                        dmabuf = &state->dmabuf;
3056                        return put_user((dmabuf->fmt & CS_FMT_16BIT) ? 
3057                                AFMT_S16_LE : AFMT_U8, (int *)arg);
3058
3059                }
3060                return put_user(0, (int *)arg);
3061
3062        case SNDCTL_DSP_MAPINBUF:
3063        case SNDCTL_DSP_MAPOUTBUF:
3064        case SNDCTL_DSP_SETSYNCRO:
3065        case SOUND_PCM_WRITE_FILTER:
3066        case SOUND_PCM_READ_FILTER:
3067                return -EINVAL;
3068        }
3069        return -EINVAL;
3070}
3071
3072
3073/*
3074 *      AMP control - null AMP
3075 */
3076 
3077static void amp_none(struct cs_card *card, int change)
3078{       
3079}
3080
3081/*
3082 *      Crystal EAPD mode
3083 */
3084 
3085static void amp_voyetra(struct cs_card *card, int change)
3086{
3087        /* Manage the EAPD bit on the Crystal 4297 
3088           and the Analog AD1885 */
3089           
3090        int old=card->amplifier;
3091        
3092        card->amplifier+=change;
3093        if(card->amplifier && !old)
3094        {
3095                /* Turn the EAPD amp on */
3096                cs_ac97_set(card->ac97_codec[0],  AC97_POWER_CONTROL, 
3097                        cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) |
3098                                0x8000);
3099        }
3100        else if(old && !card->amplifier)
3101        {
3102                /* Turn the EAPD amp off */
3103                cs_ac97_set(card->ac97_codec[0],  AC97_POWER_CONTROL, 
3104                        cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
3105                                ~0x8000);
3106        }
3107}
3108
3109                       
3110/*
3111 *      Game Theatre XP card - EGPIO[2] is used to enable the external amp.
3112 */
3113 
3114static void amp_hercules(struct cs_card *card, int change)
3115{
3116        int old=card->amplifier;
3117        if(!card)
3118        {
3119                CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO 
3120                        "cs46xx: amp_hercules() called before initialized.\n"));
3121                return;
3122        }
3123        card->amplifier+=change;
3124        if( (card->amplifier && !old) && !(hercules_egpio_disable))
3125        {
3126                CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO 
3127                        "cs46xx: amp_hercules() external amp enabled\n"));
3128                cs461x_pokeBA0(card, BA0_EGPIODR, 
3129                        EGPIODR_GPOE2);     /* enable EGPIO2 output */
3130                cs461x_pokeBA0(card, BA0_EGPIOPTR, 
3131                        EGPIOPTR_GPPT2);   /* open-drain on output */
3132        }
3133        else if(old && !card->amplifier)
3134        {
3135                CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO 
3136                        "cs46xx: amp_hercules() external amp disabled\n"));
3137                cs461x_pokeBA0(card, BA0_EGPIODR, 0); /* disable */
3138                cs461x_pokeBA0(card, BA0_EGPIOPTR, 0); /* disable */
3139        }
3140}
3141
3142/*
3143 *      Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
3144 *      whenever we need to beat on the chip.
3145 *
3146 *      The original idea and code for this hack comes from David Kaiser at
3147 *      Linuxcare. Perhaps one day Crystal will document their chips well
3148 *      enough to make them useful.
3149 */
3150 
3151static void clkrun_hack(struct cs_card *card, int change)
3152{
3153        struct pci_dev *acpi_dev;
3154        u16 control;
3155        u8 pp;
3156        unsigned long port;
3157        int old=card->active;
3158        
3159        card->active+=change;
3160        
3161        acpi_dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3162        if(acpi_dev == NULL)
3163                return;         /* Not a thinkpad thats for sure */
3164
3165        /* Find the control port */             
3166        pci_read_config_byte(acpi_dev, 0x41, &pp);
3167        port=pp<<8;
3168
3169        /* Read ACPI port */    
3170        control=inw(port+0x10);
3171
3172        /* Flip CLKRUN off while running */
3173        if(!card->active && old)
3174        {
3175                CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
3176                        "cs46xx: clkrun() enable clkrun - change=%d active=%d\n",
3177                                change,card->active));
3178                outw(control|0x2000, port+0x10);
3179        }
3180        else 
3181        {
3182        /*
3183        * sometimes on a resume the bit is set, so always reset the bit.
3184        */
3185                CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
3186                        "cs46xx: clkrun() disable clkrun - change=%d active=%d\n",
3187                                change,card->active));
3188                outw(control&~0x2000, port+0x10);
3189        }
3190}
3191
3192        
3193static int cs_open(struct inode *inode, struct file *file)
3194{
3195        struct cs_card *card = (struct cs_card *)file->private_data;
3196        struct cs_state *state = NULL;
3197        struct dmabuf *dmabuf = NULL;
3198        struct list_head *entry;
3199        int minor = MINOR(inode->i_rdev);
3200        int ret=0;
3201        unsigned int tmp;
3202
3203        CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()+ file=0x%x %s %s\n",
3204                (unsigned)file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
3205                file->f_mode & FMODE_READ ? "FMODE_READ" : "") );
3206
3207        list_for_each(entry, &cs46xx_devs)
3208        {
3209                card = list_entry(entry, struct cs_card, list);
3210
3211                if (!((card->dev_audio ^ minor) & ~0xf))
3212                        break;
3213        }
3214        if (entry == &cs46xx_devs)
3215                return -ENODEV;
3216        if (!card) {
3217                CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
3218                        "cs46xx: cs_open(): Error - unable to find audio card struct\n"));
3219                return -ENODEV;
3220        }
3221
3222        /*
3223         * hardcode state[0] for capture, [1] for playback
3224         */
3225        if(file->f_mode & FMODE_READ)
3226        {
3227                CS_DBGOUT(CS_WAVE_READ, 2, printk("cs46xx: cs_open() FMODE_READ\n") );
3228                if (card->states[0] == NULL) {
3229                        state = card->states[0] = (struct cs_state *)
3230                                kmalloc(sizeof(struct cs_state), GFP_KERNEL);
3231                        if (state == NULL)
3232                                return -ENOMEM;
3233                        memset(state, 0, sizeof(struct cs_state));
3234                        init_MUTEX(&state->sem);
3235                        dmabuf = &state->dmabuf;
3236                        dmabuf->pbuf = (void *)get_free_page(GFP_KERNEL | GFP_DMA);
3237                        if(dmabuf->pbuf==NULL)
3238                        {
3239                                kfree(state);
3240                                card->states[0]=NULL;
3241                                return -ENOMEM;
3242                        }
3243                }
3244                else
3245                {
3246                        state = card->states[0];
3247                        if(state->open_mode & FMODE_READ)
3248                                return -EBUSY;
3249                }
3250                dmabuf->channel = card->alloc_rec_pcm_channel(card);
3251                        
3252                if (dmabuf->channel == NULL) {
3253                        kfree (card->states[0]);
3254                        card->states[0] = NULL;;
3255                        return -ENODEV;
3256                }
3257
3258                /* Now turn on external AMP if needed */
3259                state->card = card;
3260                state->card->active_ctrl(state->card,1);
3261                state->card->amplifier_ctrl(state->card,1);
3262                
3263                if( (tmp = cs46xx_powerup(card, CS_POWER_ADC)) )
3264                {
3265                        CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
3266                                "cs46xx: cs46xx_powerup of ADC failed (0x%x)\n",tmp) );
3267                        return -EIO;
3268                }
3269
3270                dmabuf->channel->state = state;
3271                /* initialize the virtual channel */
3272                state->virt = 0;
3273                state->magic = CS_STATE_MAGIC;
3274                init_waitqueue_head(&dmabuf->wait);
3275                init_MUTEX(&state->open_sem);
3276                file->private_data = card;
3277
3278                down(&state->open_sem);
3279
3280                /* set default sample format. According to OSS Programmer's Guide  /dev/dsp
3281                   should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3282                   /dev/dspW will accept 16-bits sample */
3283
3284                /* Default input is 8bit mono */
3285                dmabuf->fmt &= ~CS_FMT_MASK;
3286                dmabuf->type = CS_TYPE_ADC;
3287                dmabuf->ossfragshift = 0;
3288                dmabuf->ossmaxfrags  = 0;
3289                dmabuf->subdivision  = 0;
3290                cs_set_adc_rate(state, 8000);
3291                cs_set_divisor(dmabuf);
3292
3293                state->open_mode |= FMODE_READ;
3294                up(&state->open_sem);
3295        }
3296        if(file->f_mode & FMODE_WRITE)
3297        {
3298                CS_DBGOUT(CS_OPEN, 2, printk("cs46xx: cs_open() FMODE_WRITE\n") );
3299                if (card->states[1] == NULL) {
3300                        state = card->states[1] = (struct cs_state *)
3301                                kmalloc(sizeof(struct cs_state), GFP_KERNEL);
3302                        if (state == NULL)
3303                                return -ENOMEM;
3304                        memset(state, 0, sizeof(struct cs_state));
3305                        init_MUTEX(&state->sem);
3306                        dmabuf = &state->dmabuf;
3307                        dmabuf->pbuf = (void *)get_free_page(GFP_KERNEL | GFP_DMA);
3308                        if(dmabuf->pbuf==NULL)
3309                        {
3310                                kfree(state);
3311                                card->states[1]=NULL;
3312                                return -ENOMEM;
3313                        }
3314                }
3315                else
3316                {
3317                        state = card->states[1];
3318                        if(state->open_mode & FMODE_WRITE)
3319                                return -EBUSY;
3320                }
3321                dmabuf->channel = card->alloc_pcm_channel(card);
3322                        
3323                if (dmabuf->channel == NULL) {
3324                        kfree (card->states[1]);
3325                        card->states[1] = NULL;;
3326                        return -ENODEV;
3327                }
3328
3329                /* Now turn on external AMP if needed */
3330                state->card = card;
3331                state->card->active_ctrl(state->card,1);
3332                state->card->amplifier_ctrl(state->card,1);
3333
3334                if( (tmp = cs46xx_powerup(card, CS_POWER_DAC)) )
3335                {
3336                        CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
3337                                "cs46xx: cs46xx_powerup of DAC failed (0x%x)\n",tmp) );
3338                        return -EIO;
3339                }
3340                
3341                dmabuf->channel->state = state;
3342                /* initialize the virtual channel */
3343                state->virt = 1;
3344                state->magic = CS_STATE_MAGIC;
3345                init_waitqueue_head(&dmabuf->wait);
3346                init_MUTEX(&state->open_sem);
3347                file->private_data = card;
3348
3349                down(&state->open_sem);
3350
3351                /* set default sample format. According to OSS Programmer's Guide  /dev/dsp
3352                   should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3353                   /dev/dspW will accept 16-bits sample */
3354
3355                /* Default output is 8bit mono. */
3356                dmabuf->fmt &= ~CS_FMT_MASK;
3357                dmabuf->type = CS_TYPE_DAC;
3358                dmabuf->ossfragshift = 0;
3359                dmabuf->ossmaxfrags  = 0;
3360                dmabuf->subdivision  = 0;
3361                cs_set_dac_rate(state, 8000);
3362                cs_set_divisor(dmabuf);
3363
3364                state->open_mode |= FMODE_WRITE;
3365                up(&state->open_sem);
3366                if((ret = prog_dmabuf(state)))
3367                        return ret;
3368        }
3369        MOD_INC_USE_COUNT;      /* for 2.2 */
3370        CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()- 0\n") );
3371        return 0;
3372}
3373
3374static int cs_release(struct inode *inode, struct file *file)
3375{
3376        struct cs_card *card = (struct cs_card *)file->private_data;
3377        struct dmabuf *dmabuf;
3378        struct cs_state *state;
3379        unsigned int tmp;
3380        CS_DBGOUT(CS_RELEASE | CS_FUNCTION, 2, printk("cs46xx: cs_release()+ file=0x%x %s %s\n",
3381                (unsigned)file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
3382                file->f_mode & FMODE_READ ? "FMODE_READ" : "") );
3383
3384        if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
3385        {
3386                return -EINVAL;
3387        }
3388        state = card->states[1];
3389        if(state)
3390        {
3391                if ( (state->open_mode & FMODE_WRITE) & (file->f_mode & FMODE_WRITE) )
3392                {
3393                        CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_WRITE\n") );
3394                        dmabuf = &state->dmabuf;
3395                        cs_clear_tail(state);
3396                        drain_dac(state, file->f_flags & O_NONBLOCK);
3397                        /* stop DMA state machine and free DMA buffers/channels */
3398                        down(&state->open_sem);
3399                        stop_dac(state);
3400                        dealloc_dmabuf(state);
3401                        state->card->free_pcm_channel(state->card, dmabuf->channel->num);
3402                        free_page((unsigned long)state->dmabuf.pbuf);
3403
3404                        /* we're covered by the open_sem */
3405                        up(&state->open_sem);
3406                        state->card->states[state->virt] = NULL;
3407                        state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
3408
3409                        if( (tmp = cs461x_powerdown(card, CS_POWER_DAC, CS_FALSE )) )
3410                        {
3411                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO 
3412                                        "cs46xx: cs_release_mixdev() powerdown DAC failure (0x%x)\n",tmp) );
3413                        }
3414
3415                        /* Now turn off external AMP if needed */
3416                        state->card->amplifier_ctrl(state->card, -1);
3417                        state->card->active_ctrl(state->card, -1);
3418
3419                        kfree(state);
3420                }
3421        }
3422
3423        state = card->states[0];
3424        if(state)
3425        {
3426                if ( (state->open_mode & FMODE_READ) & (file->f_mode & FMODE_READ) )
3427                {
3428                        CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_READ\n") );
3429                        dmabuf = &state->dmabuf;
3430                        down(&state->open_sem);
3431                        stop_adc(state);
3432                        dealloc_dmabuf(state);
3433                        state->card->free_pcm_channel(state->card, dmabuf->channel->num);
3434                        free_page((unsigned long)state->dmabuf.pbuf);
3435
3436                        /* we're covered by the open_sem */
3437                        up(&state->open_sem);
3438                        state->card->states[state->virt] = NULL;
3439                        state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
3440
3441                        if( (tmp = cs461x_powerdown(card, CS_POWER_ADC, CS_FALSE )) )
3442                        {
3443                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO 
3444                                        "cs46xx: cs_release_mixdev() powerdown ADC failure (0x%x)\n",tmp) );
3445                        }
3446
3447                        /* Now turn off external AMP if needed */
3448                        state->card->amplifier_ctrl(state->card, -1);
3449                        state->card->active_ctrl(state->card, -1);
3450
3451                        kfree(state);
3452                }
3453        }
3454
3455        CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk("cs46xx: cs_release()- 0\n") );
3456        MOD_DEC_USE_COUNT;      /* For 2.2 */
3457        return 0;
3458}
3459
3460static void printpm(struct cs_card *s)
3461{
3462        CS_DBGOUT(CS_PM, 9, printk("pm struct:\n"));
3463        CS_DBGOUT(CS_PM, 9, printk("flags:0x%x u32CLKCR1_SAVE: 0%x u32SSPMValue: 0x%x\n",
3464                (unsigned)s->pm.flags,s->pm.u32CLKCR1_SAVE,s->pm.u32SSPMValue));
3465        CS_DBGOUT(CS_PM, 9, printk("u32PPLVCvalue: 0x%x u32PPRVCvalue: 0x%x\n",
3466                s->pm.u32PPLVCvalue,s->pm.u32PPRVCvalue));
3467        CS_DBGOUT(CS_PM, 9, printk("u32FMLVCvalue: 0x%x u32FMRVCvalue: 0x%x\n",
3468                s->pm.u32FMLVCvalue,s->pm.u32FMRVCvalue));
3469        CS_DBGOUT(CS_PM, 9, printk("u32GPIORvalue: 0x%x u32JSCTLvalue: 0x%x\n",
3470                s->pm.u32GPIORvalue,s->pm.u32JSCTLvalue));
3471        CS_DBGOUT(CS_PM, 9, printk("u32SSCR: 0x%x u32SRCSA: 0x%x\n",
3472                s->pm.u32SSCR,s->pm.u32SRCSA));
3473        CS_DBGOUT(CS_PM, 9, printk("u32DacASR: 0x%x u32AdcASR: 0x%x\n",
3474                s->pm.u32DacASR,s->pm.u32AdcASR));
3475        CS_DBGOUT(CS_PM, 9, printk("u32DacSR: 0x%x u32AdcSR: 0x%x\n",
3476                s->pm.u32DacSR,s->pm.u32AdcSR));
3477        CS_DBGOUT(CS_PM, 9, printk("u32MIDCR_Save: 0x%x\n",
3478                s->pm.u32MIDCR_Save));
3479        CS_DBGOUT(CS_PM, 9, printk("u32AC97_powerdown: 0x%x _general_purpose 0x%x\n",
3480                s->pm.u32AC97_powerdown,s->pm.u32AC97_general_purpose));
3481        CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume: 0x%x\n",
3482                s->pm.u32AC97_master_volume));
3483        CS_DBGOUT(CS_PM, 9, printk("u32AC97_headphone_volume: 0x%x\n",
3484                s->pm.u32AC97_headphone_volume));
3485        CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume_mono: 0x%x\n",
3486                s->pm.u32AC97_master_volume_mono));
3487        CS_DBGOUT(CS_PM, 9, printk("u32AC97_pcm_out_volume: 0x%x\n",
3488                s->pm.u32AC97_pcm_out_volume));
3489        CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_play: 0x%x dmabuf_count_play: %d\n",
3490                s->pm.dmabuf_swptr_play,s->pm.dmabuf_count_play));
3491        CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_capture: 0x%x dmabuf_count_capture: %d\n",
3492                s->pm.dmabuf_swptr_capture,s->pm.dmabuf_count_capture));
3493
3494}
3495
3496/****************************************************************************
3497*
3498*  Suspend - save the ac97 regs, mute the outputs and power down the part.  
3499*
3500****************************************************************************/
3501void cs46xx_ac97_suspend(struct cs_card *card)
3502{
3503        int Count,i;
3504        struct ac97_codec *dev=card->ac97_codec[0];
3505        unsigned int tmp;
3506
3507        CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()+\n"));
3508
3509        if(card->states[1])
3510        {
3511                stop_dac(card->states[1]);
3512                resync_dma_ptrs(card->states[1]);
3513        }
3514        if(card->states[0])
3515        {
3516                stop_adc(card->states[0]);
3517                resync_dma_ptrs(card->states[0]);
3518        }
3519
3520        for(Count = 0x2, i=0; (Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
3521                        && (i < CS46XX_AC97_NUMBER_RESTORE_REGS); 
3522                Count += 2, i++)
3523        {
3524                card->pm.ac97[i] = cs_ac97_get(dev, BA0_AC97_RESET + Count);
3525        }
3526/*
3527* Save the ac97 volume registers as well as the current powerdown state.
3528* Now, mute the all the outputs (master, headphone, and mono), as well
3529* as the PCM volume, in preparation for powering down the entire part.
3530        card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev, 
3531                        (u8)BA0_AC97_MASTER_VOLUME); 
3532        card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev, 
3533                        (u8)BA0_AC97_HEADPHONE_VOLUME); 
3534        card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev, 
3535                        (u8)BA0_AC97_MASTER_VOLUME_MONO); 
3536        card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev, 
3537                        (u8)BA0_AC97_PCM_OUT_VOLUME);
3538*/ 
3539/*
3540* mute the outputs
3541*/
3542        cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
3543        cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
3544        cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
3545        cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);
3546
3547/*
3548* save the registers that cause pops
3549*/
3550        card->pm.u32AC97_powerdown = (u32)cs_ac97_get(dev, (u8)AC97_POWER_CONTROL); 
3551        card->pm.u32AC97_general_purpose = (u32)cs_ac97_get(dev, (u8)BA0_AC97_GENERAL_PURPOSE); 
3552/*
3553* And power down everything on the AC97 codec.
3554* well, for now, only power down the DAC/ADC and MIXER VREFON components. 
3555* trouble with removing VREF.
3556*/
3557        if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
3558                        CS_POWER_MIXVON, CS_TRUE )) )
3559        {
3560                CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
3561                        "cs46xx: cs46xx_ac97_suspend() failure (0x%x)\n",tmp) );
3562        }
3563
3564        CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()-\n"));
3565}
3566
3567/****************************************************************************
3568*
3569*  Resume - power up the part and restore its registers..  
3570*
3571****************************************************************************/
3572void cs46xx_ac97_resume(struct cs_card *card)
3573{
3574        int Count,i;
3575        struct ac97_codec *dev=card->ac97_codec[0];
3576
3577        CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()+\n"));
3578
3579/*
3580* First, we restore the state of the general purpose register.  This
3581* contains the mic select (mic1 or mic2) and if we restore this after
3582* we restore the mic volume/boost state and mic2 was selected at
3583* suspend time, we will end up with a brief period of time where mic1
3584* is selected with the volume/boost settings for mic2, causing
3585* acoustic feedback.  So we restore the general purpose register
3586* first, thereby getting the correct mic selected before we restore
3587* the mic volume/boost.
3588*/
3589        cs_ac97_set(dev, (u8)BA0_AC97_GENERAL_PURPOSE, 
3590                (u16)card->pm.u32AC97_general_purpose);
3591/*
3592* Now, while the outputs are still muted, restore the state of power
3593* on the AC97 part.
3594*/
3595        cs_ac97_set(dev, (u8)BA0_AC97_POWERDOWN, (u16)card->pm.u32AC97_powerdown);
3596        mdelay(5 * cs_laptop_wait);
3597/*
3598* Restore just the first set of registers, from register number
3599* 0x02 to the register number that ulHighestRegToRestore specifies.
3600*/
3601        for(    Count = 0x2, i=0; 
3602                (Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
3603                        && (i < CS46XX_AC97_NUMBER_RESTORE_REGS); 
3604                Count += 2, i++)
3605        {
3606                cs_ac97_set(dev, (u8)(BA0_AC97_RESET + Count), (u16)card->pm.ac97[i]);
3607        }
3608
3609        /* Check if we have to init the amplifier */
3610        if(card->amp_init)
3611                card->amp_init(card);
3612        
3613        CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()-\n"));
3614}
3615
3616
3617static int cs46xx_restart_part(struct cs_card *card)
3618{
3619        struct dmabuf *dmabuf;
3620        CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3621                printk( "cs46xx: cs46xx_restart_part()+\n"));
3622        if(card->states[1])
3623        {
3624                dmabuf = &card->states[1]->dmabuf;
3625                dmabuf->ready = 0;
3626                resync_dma_ptrs(card->states[1]);
3627                cs_set_divisor(dmabuf);
3628                if(__prog_dmabuf(card->states[1]))
3629                {
3630                        CS_DBGOUT(CS_PM | CS_ERROR, 1, 
3631                                printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() dac error\n"));
3632                        return -1;
3633                }
3634                cs_set_dac_rate(card->states[1], dmabuf->rate);
3635        }
3636        if(card->states[0])
3637        {
3638                dmabuf = &card->states[0]->dmabuf;
3639                dmabuf->ready = 0;
3640                resync_dma_ptrs(card->states[0]);
3641                cs_set_divisor(dmabuf);
3642                if(__prog_dmabuf(card->states[0]))
3643                {
3644                        CS_DBGOUT(CS_PM | CS_ERROR, 1, 
3645                                printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() adc error\n"));
3646                        return -1;
3647                }
3648                cs_set_adc_rate(card->states[0], dmabuf->rate);
3649        }
3650        card->pm.flags |= CS46XX_PM_RESUMED;
3651        if(card->states[0])
3652                start_adc(card->states[0]);
3653        if(card->states[1])
3654                start_dac(card->states[1]);
3655
3656        card->pm.flags |= CS46XX_PM_IDLE;
3657        card->pm.flags &= ~(CS46XX_PM_SUSPENDING | CS46XX_PM_SUSPENDED 
3658                        | CS46XX_PM_RESUMING | CS46XX_PM_RESUMED);
3659        if(card->states[0])
3660                wake_up(&card->states[0]->dmabuf.wait);
3661        if(card->states[1])
3662                wake_up(&card->states[1]->dmabuf.wait);
3663
3664        CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3665                printk( "cs46xx: cs46xx_restart_part()-\n"));
3666        return 0;
3667}
3668
3669
3670static void cs461x_reset(struct cs_card *card);
3671static void cs461x_proc_stop(struct cs_card *card);
3672static int cs46xx_suspend(struct cs_card *card, u32 state)
3673{
3674        unsigned int tmp;
3675        CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3676                printk("cs46xx: cs46xx_suspend()+ flags=0x%x s=0x%x\n",
3677                        (unsigned)card->pm.flags,(unsigned)card));
3678/*
3679* check the current state, only suspend if IDLE
3680*/
3681        if(!(card->pm.flags & CS46XX_PM_IDLE))
3682        {
3683                CS_DBGOUT(CS_PM | CS_ERROR, 2, 
3684                        printk("cs46xx: cs46xx_suspend() unable to suspend, not IDLE\n"));
3685                return 1;
3686        }
3687        card->pm.flags &= ~CS46XX_PM_IDLE;
3688        card->pm.flags |= CS46XX_PM_SUSPENDING;
3689
3690        card->active_ctrl(card,1);
3691        
3692        tmp = cs461x_peek(card, BA1_PFIE);
3693        tmp &= ~0x0000f03f;
3694        tmp |=  0x00000010;
3695        cs461x_poke(card, BA1_PFIE, tmp);       /* playback interrupt disable */
3696
3697        tmp = cs461x_peek(card, BA1_CIE);
3698        tmp &= ~0x0000003f;
3699        tmp |=  0x00000011;
3700        cs461x_poke(card, BA1_CIE, tmp);        /* capture interrupt disable */
3701
3702        /*
3703         *  Stop playback DMA.
3704         */
3705        tmp = cs461x_peek(card, BA1_PCTL);
3706        cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
3707
3708        /*
3709         *  Stop capture DMA.
3710         */
3711        tmp = cs461x_peek(card, BA1_CCTL);
3712        cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
3713
3714        if(card->states[1])
3715        {
3716                card->pm.dmabuf_swptr_play = card->states[1]->dmabuf.swptr;
3717                card->pm.dmabuf_count_play = card->states[1]->dmabuf.count;
3718        }
3719        if(card->states[0])
3720        {
3721                card->pm.dmabuf_swptr_capture = card->states[0]->dmabuf.swptr;
3722                card->pm.dmabuf_count_capture = card->states[0]->dmabuf.count;
3723        }
3724
3725        cs46xx_ac97_suspend(card);
3726
3727        /*
3728         *  Reset the processor.
3729         */
3730        cs461x_reset(card);
3731
3732        cs461x_proc_stop(card);
3733
3734        /*
3735         *  Power down the DAC and ADC.  For now leave the other areas on.
3736         */
3737        cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, 0x0300);
3738
3739        /*
3740         *  Power down the PLL.
3741         */
3742        cs461x_pokeBA0(card, BA0_CLKCR1, 0);
3743
3744        /*
3745         *  Turn off the Processor by turning off the software clock enable flag in 
3746         *  the clock control register.
3747         */
3748        tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE;
3749        cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
3750
3751        card->active_ctrl(card,-1);
3752
3753        card->pm.flags &= ~CS46XX_PM_SUSPENDING;
3754        card->pm.flags |= CS46XX_PM_SUSPENDED;
3755
3756        printpm(card);
3757
3758        CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3759                printk("cs46xx: cs46xx_suspend()- flags=0x%x\n",
3760                        (unsigned)card->pm.flags));
3761        return 0;
3762}
3763
3764static int cs46xx_resume(struct cs_card *card)
3765{
3766        int i;
3767
3768        CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3769                printk( "cs46xx: cs46xx_resume()+ flags=0x%x\n",
3770                        (unsigned)card->pm.flags));
3771        if(!(card->pm.flags & CS46XX_PM_SUSPENDED))
3772        {
3773                CS_DBGOUT(CS_PM | CS_ERROR, 2, 
3774                        printk("cs46xx: cs46xx_resume() unable to resume, not SUSPENDED\n"));
3775                return 1;
3776        }
3777        card->pm.flags |= CS46XX_PM_RESUMING;
3778        card->pm.flags &= ~CS46XX_PM_SUSPENDED;
3779        printpm(card);
3780        card->active_ctrl(card, 1);
3781
3782        for(i=0;i<5;i++)
3783        {
3784                if (cs_hardware_init(card) != 0)
3785                {
3786                        CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
3787                                "cs46xx: cs46xx_resume()- ERROR in cs_hardware_init()\n"));
3788                        mdelay(10 * cs_laptop_wait);
3789                        cs461x_reset(card);
3790                        continue;
3791                }
3792                break;
3793        }
3794        if(i>=4)
3795        {
3796                CS_DBGOUT(CS_PM | CS_ERROR, 1, printk(
3797                        "cs46xx: cs46xx_resume()- cs_hardware_init() failed, retried %d times.\n",i));
3798                return 0;
3799        }
3800
3801        if(cs46xx_restart_part(card))
3802        {
3803                CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
3804                        "cs46xx: cs46xx_resume(): cs46xx_restart_part() returned error\n"));
3805        }
3806
3807        card->active_ctrl(card, -1);
3808
3809        CS_DBGOUT(CS_PM | CS_FUNCTION, 4, printk("cs46xx: cs46xx_resume()- flags=0x%x\n",
3810                (unsigned)card->pm.flags));
3811        return 0;
3812}
3813
3814static /*const*/ struct file_operations cs461x_fops = {
3815        CS_OWNER        CS_THIS_MODULE
3816        llseek:         no_llseek,
3817        read:           cs_read,
3818        write:          cs_write,
3819        poll:           cs_poll,
3820        ioctl:          cs_ioctl,
3821        mmap:           cs_mmap,
3822        open:           cs_open,
3823        release:        cs_release,
3824};
3825
3826/* Write AC97 codec registers */
3827
3828
3829static u16 _cs_ac97_get(struct ac97_codec *dev, u8 reg)
3830{
3831        struct cs_card *card = dev->private_data;
3832        int count,loopcnt;
3833        unsigned int tmp;
3834        u16 ret;
3835        
3836        /*
3837         *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3838         *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97 
3839         *  3. Write ACCTL = Control Register = 460h for initiating the write
3840         *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
3841         *  5. if DCV not cleared, break and return error
3842         *  6. Read ACSTS = Status Register = 464h, check VSTS bit
3843         */
3844
3845        cs461x_peekBA0(card, BA0_ACSDA);
3846
3847        /*
3848         *  Setup the AC97 control registers on the CS461x to send the
3849         *  appropriate command to the AC97 to perform the read.
3850         *  ACCAD = Command Address Register = 46Ch
3851         *  ACCDA = Command Data Register = 470h
3852         *  ACCTL = Control Register = 460h
3853         *  set DCV - will clear when process completed
3854         *  set CRW - Read command
3855         *  set VFRM - valid frame enabled
3856         *  set ESYN - ASYNC generation enabled
3857         *  set RSTN - ARST# inactive, AC97 codec not reset
3858         */
3859
3860        cs461x_pokeBA0(card, BA0_ACCAD, reg);
3861        cs461x_pokeBA0(card, BA0_ACCDA, 0);
3862        cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW |
3863                                             ACCTL_VFRM | ACCTL_ESYN |
3864                                             ACCTL_RSTN);
3865
3866
3867        /*
3868         *  Wait for the read to occur.
3869         */
3870        if(!(card->pm.flags & CS46XX_PM_IDLE))
3871                loopcnt = 2000;
3872        else
3873                loopcnt = 500 * cs_laptop_wait;
3874        loopcnt *= cs_laptop_wait;
3875        for (count = 0; count < loopcnt; count++) {
3876                /*
3877                 *  First, we want to wait for a short time.
3878                 */
3879                udelay(10 * cs_laptop_wait);
3880                /*
3881                 *  Now, check to see if the read has completed.
3882                 *  ACCTL = 460h, DCV should be reset by now and 460h = 17h
3883                 */
3884                if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
3885                        break;
3886        }
3887
3888        /*
3889         *  Make sure the read completed.
3890         */
3891        if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV) {
3892                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
3893                        "cs46xx: AC'97 read problem (ACCTL_DCV), reg = 0x%x returning 0xffff\n", reg));
3894                return 0xffff;
3895        }
3896
3897        /*
3898         *  Wait for the valid status bit to go active.
3899         */
3900
3901        if(!(card->pm.flags & CS46XX_PM_IDLE))
3902                loopcnt = 2000;
3903        else
3904                loopcnt = 1000;
3905        loopcnt *= cs_laptop_wait;
3906        for (count = 0; count < loopcnt; count++) {
3907                /*
3908                 *  Read the AC97 status register.
3909                 *  ACSTS = Status Register = 464h
3910                 *  VSTS - Valid Status
3911                 */
3912                if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_VSTS)
3913                        break;
3914                udelay(10 * cs_laptop_wait);
3915        }
3916        
3917        /*
3918         *  Make sure we got valid status.
3919         */
3920        if (!( (tmp=cs461x_peekBA0(card, BA0_ACSTS)) & ACSTS_VSTS)) {
3921                CS_DBGOUT(CS_ERROR, 2, printk(KERN_WARNING 
3922                        "cs46xx: AC'97 read problem (ACSTS_VSTS), reg = 0x%x val=0x%x 0xffff \n", 
3923                                reg, tmp));
3924                return 0xffff;
3925        }
3926
3927        /*
3928         *  Read the data returned from the AC97 register.
3929         *  ACSDA = Status Data Register = 474h
3930         */
3931        CS_DBGOUT(CS_FUNCTION, 9, printk(KERN_INFO
3932                "cs46xx: cs_ac97_get() reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n", 
3933                        reg, cs461x_peekBA0(card, BA0_ACSDA),
3934                        cs461x_peekBA0(card, BA0_ACCAD)));
3935        ret = cs461x_peekBA0(card, BA0_ACSDA);
3936        return ret;
3937}
3938
3939static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg)
3940{
3941        u16 ret;
3942        struct cs_card *card = dev->private_data;
3943        
3944        spin_lock(&card->ac97_lock);
3945        ret = _cs_ac97_get(dev, reg);
3946        spin_unlock(&card->ac97_lock);
3947        return ret;
3948}
3949
3950static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 val)
3951{
3952        struct cs_card *card = dev->private_data;
3953        int count;
3954        int val2 = 0;
3955        
3956        spin_lock(&card->ac97_lock);
3957        
3958        if(reg == AC97_CD_VOL)
3959        {
3960                val2 = _cs_ac97_get(dev, AC97_CD_VOL);
3961        }
3962        
3963        
3964        /*
3965         *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3966         *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97
3967         *  3. Write ACCTL = Control Register = 460h for initiating the write
3968         *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
3969         *  5. if DCV not cleared, break and return error
3970         */
3971
3972        /*
3973         *  Setup the AC97 control registers on the CS461x to send the
3974         *  appropriate command to the AC97 to perform the read.
3975         *  ACCAD = Command Address Register = 46Ch
3976         *  ACCDA = Command Data Register = 470h
3977         *  ACCTL = Control Register = 460h
3978         *  set DCV - will clear when process completed
3979         *  reset CRW - Write command
3980         *  set VFRM - valid frame enabled
3981         *  set ESYN - ASYNC generation enabled
3982         *  set RSTN - ARST# inactive, AC97 codec not reset
3983         */
3984        cs461x_pokeBA0(card, BA0_ACCAD, reg);
3985        cs461x_pokeBA0(card, BA0_ACCDA, val);
3986        cs461x_peekBA0(card, BA0_ACCTL);
3987        cs461x_pokeBA0(card, BA0_ACCTL, 0 | ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
3988        cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM |
3989                                             ACCTL_ESYN | ACCTL_RSTN);
3990        for (count = 0; count < 1000; count++) {
3991                /*
3992                 *  First, we want to wait for a short time.
3993                 */
3994                udelay(10 * cs_laptop_wait);
3995                /*
3996                 *  Now, check to see if the write has completed.
3997                 *  ACCTL = 460h, DCV should be reset by now and 460h = 07h
3998                 */
3999                if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
4000                        break;
4001        }
4002        /*
4003         *  Make sure the write completed.
4004         */
4005        if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV)
4006        {
4007                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4008                        "cs46xx: AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg, val));
4009        }
4010
4011        spin_unlock(&card->ac97_lock);
4012
4013        /*
4014         *      Adjust power if the mixer is selected/deselected according
4015         *      to the CD.
4016         *
4017         *      IF the CD is a valid input source (mixer or direct) AND
4018         *              the CD is not muted THEN power is needed
4019         *
4020         *      We do two things. When record select changes the input to
4021         *      add/remove the CD we adjust the power count if the CD is
4022         *      unmuted.
4023         *
4024         *      When the CD mute changes we adjust the power level if the
4025         *      CD was a valid input.
4026         *
4027         *      We also check for CD volume != 0, as the CD mute isn't
4028         *      normally tweaked from userspace.
4029         */
4030         
4031        /* CD mute change ? */
4032        
4033        if(reg==AC97_CD_VOL)
4034        {
4035                /* Mute bit change ? */
4036                if((val2^val)&0x8000 || ((val2 == 0x1f1f || val == 0x1f1f) && val2 != val))
4037                {
4038                        /* This is a hack but its cleaner than the alternatives.
4039                           Right now card->ac97_codec[0] might be NULL as we are
4040                           still doing codec setup. This does an early assignment
4041                           to avoid the problem if it occurs */
4042                           
4043                        if(card->ac97_codec[0]==NULL)
4044                                card->ac97_codec[0]=dev;
4045                                
4046                        /* Mute on */
4047                        if(val&0x8000 || val == 0x1f1f)
4048                                card->amplifier_ctrl(card, -1);
4049                        else /* Mute off power on */
4050                        {
4051                                if(card->amp_init)
4052                                        card->amp_init(card);
4053                                card->amplifier_ctrl(card, 1);
4054                        }
4055                }
4056        }
4057}
4058
4059
4060/* OSS /dev/mixer file operation methods */
4061
4062static int cs_open_mixdev(struct inode *inode, struct file *file)
4063{
4064        int i=0;
4065        int minor = MINOR(inode->i_rdev);
4066        struct cs_card *card=NULL;
4067        struct list_head *entry;
4068        unsigned int tmp;
4069
4070        CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
4071                  printk(KERN_INFO "cs46xx: cs_open_mixdev()+\n"));
4072
4073        list_for_each(entry, &cs46xx_devs)
4074        {
4075                card = list_entry(entry, struct cs_card, list);
4076                for (i = 0; i < NR_AC97; i++)
4077                        if (card->ac97_codec[i] != NULL &&
4078                            card->ac97_codec[i]->dev_mixer == minor)
4079                                goto match;
4080        }
4081        if (!card)
4082        {
4083                CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
4084                        printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
4085                return -ENODEV;
4086        }
4087 match:
4088        if(!card->ac97_codec[i])
4089                return -ENODEV;
4090        file->private_data = card->ac97_codec[i];
4091
4092        card->active_ctrl(card,1);
4093        if(!CS_IN_USE(&card->mixer_use_cnt))
4094        {
4095                if( (tmp = cs46xx_powerup(card, CS_POWER_MIXVON )) )
4096                {
4097                        CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
4098                                "cs46xx: cs_open_mixdev() powerup failure (0x%x)\n",tmp) );
4099                        return -EIO;
4100                }
4101        }
4102        card->amplifier_ctrl(card, 1);
4103        CS_INC_USE_COUNT(&card->mixer_use_cnt);
4104        MOD_INC_USE_COUNT; /* for 2.2 */
4105        CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
4106                  printk(KERN_INFO "cs46xx: cs_open_mixdev()- 0\n"));
4107        return 0;
4108}
4109
4110static int cs_release_mixdev(struct inode *inode, struct file *file)
4111{
4112        int minor = MINOR(inode->i_rdev);
4113        struct cs_card *card=NULL;
4114        struct list_head *entry;
4115        int i;
4116        unsigned int tmp;
4117
4118        CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4119                  printk(KERN_INFO "cs46xx: cs_release_mixdev()+\n"));
4120        list_for_each(entry, &cs46xx_devs)
4121        {
4122                card = list_entry(entry, struct cs_card, list);
4123                for (i = 0; i < NR_AC97; i++)
4124                        if (card->ac97_codec[i] != NULL &&
4125                            card->ac97_codec[i]->dev_mixer == minor)
4126                                goto match;
4127        }
4128        if (!card)
4129        {
4130                CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
4131                        printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
4132                return -ENODEV;
4133        }
4134match:
4135        MOD_DEC_USE_COUNT; /* for 2.2 */
4136        if(!CS_DEC_AND_TEST(&card->mixer_use_cnt))
4137        {
4138                CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4139                          printk(KERN_INFO "cs46xx: cs_release_mixdev()- no powerdown, usecnt>0\n"));
4140                card->active_ctrl(card, -1);
4141                card->amplifier_ctrl(card, -1);
4142                return 0;
4143        }
4144/*
4145* ok, no outstanding mixer opens, so powerdown.
4146*/
4147        if( (tmp = cs461x_powerdown(card, CS_POWER_MIXVON, CS_FALSE )) )
4148        {
4149                CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
4150                        "cs46xx: cs_release_mixdev() powerdown MIXVON failure (0x%x)\n",tmp) );
4151                card->active_ctrl(card, -1);
4152                card->amplifier_ctrl(card, -1);
4153                return -EIO;
4154        }
4155        card->active_ctrl(card, -1);
4156        card->amplifier_ctrl(card, -1);
4157        CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4158                  printk(KERN_INFO "cs46xx: cs_release_mixdev()- 0\n"));
4159        return 0;
4160}
4161
4162void __exit cs46xx_cleanup_module(void);
4163static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
4164                                unsigned long arg)
4165{
4166        struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
4167        struct cs_card *card=NULL;
4168        struct list_head *entry;
4169
4170#if CSDEBUG_INTERFACE
4171        int val;
4172
4173        if(     (cmd == SOUND_MIXER_CS_GETDBGMASK) || 
4174                (cmd == SOUND_MIXER_CS_SETDBGMASK) ||
4175                (cmd == SOUND_MIXER_CS_GETDBGLEVEL) ||
4176                (cmd == SOUND_MIXER_CS_SETDBGLEVEL) ||
4177                (cmd == SOUND_MIXER_CS_APM))
4178        {
4179            switch(cmd)
4180            {
4181
4182                case SOUND_MIXER_CS_GETDBGMASK:
4183                        return put_user(cs_debugmask, (unsigned long *)arg);
4184                
4185                case SOUND_MIXER_CS_GETDBGLEVEL:
4186                        return put_user(cs_debuglevel, (unsigned long *)arg);
4187
4188                case SOUND_MIXER_CS_SETDBGMASK:
4189                        if (get_user(val, (unsigned long *)arg))
4190                                return -EFAULT;
4191                        cs_debugmask = val;
4192                        return 0;
4193
4194                case SOUND_MIXER_CS_SETDBGLEVEL:
4195                        if (get_user(val, (unsigned long *)arg))
4196                                return -EFAULT;
4197                        cs_debuglevel = val;
4198                        return 0;
4199
4200                case SOUND_MIXER_CS_APM:
4201                        if (get_user(val, (unsigned long *) arg))
4202                                return -EFAULT;
4203                        if(val == CS_IOCTL_CMD_SUSPEND) 
4204                        {
4205                                list_for_each(entry, &cs46xx_devs)
4206                                {
4207                                        card = list_entry(entry, struct cs_card, list);
4208                                        cs46xx_suspend(card, 0);
4209                                }
4210
4211                        }
4212                        else if(val == CS_IOCTL_CMD_RESUME)
4213                        {
4214                                list_for_each(entry, &cs46xx_devs)
4215                                {
4216                                        card = list_entry(entry, struct cs_card, list);
4217                                        cs46xx_resume(card);
4218                                }
4219                        }
4220                        else
4221                        {
4222                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
4223                                    "cs46xx: mixer_ioctl(): invalid APM cmd (%d)\n",
4224                                        val));
4225                        }
4226                        return 0;
4227
4228                default:
4229                        CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO 
4230                                "cs46xx: mixer_ioctl(): ERROR unknown debug cmd\n") );
4231                        return 0;
4232            }
4233        }
4234#endif
4235        return codec->mixer_ioctl(codec, cmd, arg);
4236}
4237
4238static /*const*/ struct file_operations cs_mixer_fops = {
4239        CS_OWNER        CS_THIS_MODULE
4240        llseek:         no_llseek,
4241        ioctl:          cs_ioctl_mixdev,
4242        open:           cs_open_mixdev,
4243        release:        cs_release_mixdev,
4244};
4245
4246/* AC97 codec initialisation. */
4247static int __init cs_ac97_init(struct cs_card *card)
4248{
4249        int num_ac97 = 0;
4250        int ready_2nd = 0;
4251        struct ac97_codec *codec;
4252        u16 eid;
4253
4254        CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4255                "cs46xx: cs_ac97_init()+\n") );
4256
4257        for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
4258                if ((codec = ac97_alloc_codec()) == NULL)
4259                        return -ENOMEM;
4260
4261                /* initialize some basic codec information, other fields will be filled
4262                   in ac97_probe_codec */
4263                codec->private_data = card;
4264                codec->id = num_ac97;
4265
4266                codec->codec_read = cs_ac97_get;
4267                codec->codec_write = cs_ac97_set;
4268        
4269                if (ac97_probe_codec(codec) == 0)
4270                {
4271                        CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4272                                "cs46xx: cs_ac97_init()- codec number %d not found\n",
4273                                        num_ac97) );
4274                        card->ac97_codec[num_ac97] = 0;
4275                        break;
4276                }
4277                CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4278                        "cs46xx: cs_ac97_init() found codec %d\n",num_ac97) );
4279
4280                eid = cs_ac97_get(codec, AC97_EXTENDED_ID);
4281                
4282                if(eid==0xFFFF)
4283                {
4284                        printk(KERN_WARNING "cs46xx: codec %d not present\n",num_ac97);
4285                        ac97_release_codec(codec);
4286                        break;
4287                }
4288                
4289                card->ac97_features = eid;
4290                        
4291                if ((codec->dev_mixer = register_sound_mixer(&cs_mixer_fops, -1)) < 0) {
4292                        printk(KERN_ERR "cs46xx: couldn't register mixer!\n");
4293                        ac97_release_codec(codec);
4294                        break;
4295                }
4296                card->ac97_codec[num_ac97] = codec;
4297
4298                CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4299                        "cs46xx: cs_ac97_init() ac97_codec[%d] set to 0x%x\n",
4300                                (unsigned int)num_ac97,
4301                                (unsigned int)codec));
4302                /* if there is no secondary codec at all, don't probe any more */
4303                if (!ready_2nd)
4304                {
4305                        num_ac97 += 1;
4306                        break;
4307                }
4308        }
4309        CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4310                "cs46xx: cs_ac97_init()- %d\n", (unsigned int)num_ac97));
4311        return num_ac97;
4312}
4313
4314/*
4315 * load the static image into the DSP
4316 */
4317#include "cs461x_image.h"
4318static void cs461x_download_image(struct cs_card *card)
4319{
4320    unsigned i, j, temp1, temp2, offset, count;
4321    unsigned char *pBA1 = ioremap(card->ba1_addr, 0x40000);
4322    for( i=0; i < CLEAR__COUNT; i++)
4323    {
4324        offset = ClrStat[i].BA1__DestByteOffset;
4325        count  = ClrStat[i].BA1__SourceSize;
4326        for(  temp1 = offset; temp1<(offset+count); temp1+=4 )
4327              writel(0, pBA1+temp1);
4328    }
4329
4330    for(i=0; i<FILL__COUNT; i++)
4331    {
4332        temp2 = FillStat[i].Offset;
4333        for(j=0; j<(FillStat[i].Size)/4; j++)
4334        {
4335            temp1 = (FillStat[i]).pFill[j];
4336            writel(temp1, pBA1+temp2+j*4);
4337        }
4338    }
4339    iounmap(pBA1);
4340}
4341
4342
4343/*
4344 *  Chip reset
4345 */
4346
4347static void cs461x_reset(struct cs_card *card)
4348{
4349        int idx;
4350
4351        /*
4352         *  Write the reset bit of the SP control register.
4353         */
4354        cs461x_poke(card, BA1_SPCR, SPCR_RSTSP);
4355
4356        /*
4357         *  Write the control register.
4358         */
4359        cs461x_poke(card, BA1_SPCR, SPCR_DRQEN);
4360
4361        /*
4362         *  Clear the trap registers.
4363         */
4364        for (idx = 0; idx < 8; idx++) {
4365                cs461x_poke(card, BA1_DREG, DREG_REGID_TRAP_SELECT + idx);
4366                cs461x_poke(card, BA1_TWPR, 0xFFFF);
4367        }
4368        cs461x_poke(card, BA1_DREG, 0);
4369
4370        /*
4371         *  Set the frame timer to reflect the number of cycles per frame.
4372         */
4373        cs461x_poke(card, BA1_FRMT, 0xadf);
4374}
4375
4376static void cs461x_clear_serial_FIFOs(struct cs_card *card, int type)
4377{
4378        int idx, loop, startfifo=0, endfifo=0, powerdown1 = 0;
4379        unsigned int tmp;
4380
4381        /*
4382         *  See if the devices are powered down.  If so, we must power them up first
4383         *  or they will not respond.
4384         */
4385        if (!((tmp = cs461x_peekBA0(card, BA0_CLKCR1)) & CLKCR1_SWCE)) {
4386                cs461x_pokeBA0(card, BA0_CLKCR1, tmp | CLKCR1_SWCE);
4387                powerdown1 = 1;
4388        }
4389
4390        /*
4391         *  We want to clear out the serial port FIFOs so we don't end up playing
4392         *  whatever random garbage happens to be in them.  We fill the sample FIFOS
4393         *  with zero (silence).
4394         */
4395        cs461x_pokeBA0(card, BA0_SERBWP, 0);
4396
4397        /*
4398        * Check for which FIFO locations to clear, if we are currently
4399        * playing or capturing then we don't want to put in 128 bytes of
4400        * "noise".
4401         */
4402        if(type & CS_TYPE_DAC)
4403        {
4404                startfifo = 128;
4405                endfifo = 256;
4406        }
4407        if(type & CS_TYPE_ADC)
4408        {
4409                startfifo = 0;
4410                if(!endfifo)
4411                        endfifo = 128;
4412        }
4413        /*
4414         *  Fill sample FIFO locations (256 locations total).
4415         */
4416        for (idx = startfifo; idx < endfifo; idx++) {
4417                /*
4418                 *  Make sure the previous FIFO write operation has completed.
4419                 */
4420                for (loop = 0; loop < 5; loop++) {
4421                        udelay(50);
4422                        if (!(cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY))
4423                                break;
4424                }
4425                if (cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY) {
4426                        if (powerdown1)
4427                                cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4428                }
4429                /*
4430                 *  Write the serial port FIFO index.
4431                 */
4432                cs461x_pokeBA0(card, BA0_SERBAD, idx);
4433                /*
4434                 *  Tell the serial port to load the new value into the FIFO location.
4435                 */
4436                cs461x_pokeBA0(card, BA0_SERBCM, SERBCM_WRC);
4437        }
4438        /*
4439         *  Now, if we powered up the devices, then power them back down again.
4440         *  This is kinda ugly, but should never happen.
4441         */
4442        if (powerdown1)
4443                cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4444}
4445
4446
4447static int cs461x_powerdown(struct cs_card *card, unsigned int type, int suspendflag)
4448{
4449        int count;
4450        unsigned int tmp=0,muted=0;
4451
4452        CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO 
4453                "cs46xx: cs461x_powerdown()+ type=0x%x\n",type));
4454        if(!cs_powerdown && !suspendflag)
4455        {
4456                CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
4457                        "cs46xx: cs461x_powerdown() DISABLED exiting\n"));
4458                return 0;
4459        }
4460        tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4461        CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
4462                "cs46xx: cs461x_powerdown() powerdown reg=0x%x\n",tmp));
4463/*
4464* if powering down only the VREF, and not powering down the DAC/ADC,
4465* then do not power down the VREF, UNLESS both the DAC and ADC are not
4466* currently powered down.  If powering down DAC and ADC, then
4467* it is possible to power down the VREF (ON).
4468*/
4469        if (    ((type & CS_POWER_MIXVON) && 
4470                 (!(type & CS_POWER_ADC) || (!(type & CS_POWER_DAC))) )
4471              && 
4472                ((tmp & CS_AC97_POWER_CONTROL_ADC_ON) ||
4473                 (tmp & CS_AC97_POWER_CONTROL_DAC_ON) ) )
4474        {
4475                CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
4476                        "cs46xx: cs461x_powerdown()- 0  unable to powerdown. tmp=0x%x\n",tmp));
4477                return 0;
4478        }
4479/*
4480* for now, always keep power to the mixer block.
4481* not sure why it's a problem but it seems to be if we power off.
4482*/
4483        type &= ~CS_POWER_MIXVON;
4484        type &= ~CS_POWER_MIXVOFF;
4485
4486        /*
4487         *  Power down indicated areas.
4488         */
4489        if(type & CS_POWER_MIXVOFF)
4490        {
4491
4492                CS_DBGOUT(CS_FUNCTION, 4, 
4493                        printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVOFF\n"));
4494                /*
4495                 *  Power down the MIXER (VREF ON) on the AC97 card.  
4496                 */
4497                tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4498                if (tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4499                {
4500                        if(!muted)
4501                        {
4502                                cs_mute(card, CS_TRUE);
4503                                muted=1;
4504                        }
4505                        tmp |= CS_AC97_POWER_CONTROL_MIXVOFF;
4506                        cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4507                        /*
4508                         *  Now, we wait until we sample a ready state.
4509                         */
4510                        for (count = 0; count < 32; count++) {
4511                                /*
4512                                 *  First, lets wait a short while to let things settle out a
4513                                 *  bit, and to prevent retrying the read too quickly.
4514                                 */
4515                                udelay(500);
4516
4517                                /*
4518                                 *  Read the current state of the power control register.
4519                                 */
4520                                if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4521                                        CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4522                                        break;
4523                        }
4524                        
4525                        /*
4526                         *  Check the status..
4527                         */
4528                        if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4529                                CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4530                        {
4531                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4532                                        "cs46xx: powerdown MIXVOFF failed\n"));
4533                                return 1;
4534                        }
4535                }
4536        }
4537        if(type & CS_POWER_MIXVON)
4538        {
4539
4540                CS_DBGOUT(CS_FUNCTION, 4, 
4541                        printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVON\n"));
4542                /*
4543                 *  Power down the MIXER (VREF ON) on the AC97 card.  
4544                 */
4545                tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4546                if (tmp & CS_AC97_POWER_CONTROL_MIXVON_ON)
4547                {
4548                        if(!muted)
4549                        {
4550                                cs_mute(card, CS_TRUE);
4551                                muted=1;
4552                        }
4553                        tmp |= CS_AC97_POWER_CONTROL_MIXVON;
4554                        cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4555                        /*
4556                         *  Now, we wait until we sample a ready state.
4557                         */
4558                        for (count = 0; count < 32; count++) {
4559                                /*
4560                                 *  First, lets wait a short while to let things settle out a
4561                                 *  bit, and to prevent retrying the read too quickly.
4562                                 */
4563                                udelay(500);
4564
4565                                /*
4566                                 *  Read the current state of the power control register.
4567                                 */
4568                                if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4569                                        CS_AC97_POWER_CONTROL_MIXVON_ON))
4570                                        break;
4571                        }
4572                        
4573                        /*
4574                         *  Check the status..
4575                         */
4576                        if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4577                                CS_AC97_POWER_CONTROL_MIXVON_ON)
4578                        {
4579                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4580                                        "cs46xx: powerdown MIXVON failed\n"));
4581                                return 1;
4582                        }
4583                }
4584        }
4585        if(type & CS_POWER_ADC)
4586        {
4587                /*
4588                 *  Power down the ADC on the AC97 card.  
4589                 */
4590                CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs461x_powerdown()+ ADC\n"));
4591                tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4592                if (tmp & CS_AC97_POWER_CONTROL_ADC_ON)
4593                {
4594                        if(!muted)
4595                        {
4596                                cs_mute(card, CS_TRUE);
4597                                muted=1;
4598                        }
4599                        tmp |= CS_AC97_POWER_CONTROL_ADC;
4600                        cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4601
4602                        /*
4603                         *  Now, we wait until we sample a ready state.
4604                         */
4605                        for (count = 0; count < 32; count++) {
4606                                /*
4607                                 *  First, lets wait a short while to let things settle out a
4608                                 *  bit, and to prevent retrying the read too quickly.
4609                                 */
4610                                udelay(500);
4611
4612                                /*
4613                                 *  Read the current state of the power control register.
4614                                 */
4615                                if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4616                                        CS_AC97_POWER_CONTROL_ADC_ON))
4617                                        break;
4618                        }
4619
4620                        /*
4621                         *  Check the status..
4622                         */
4623                        if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4624                                CS_AC97_POWER_CONTROL_ADC_ON)
4625                        {
4626                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4627                                        "cs46xx: powerdown ADC failed\n"));
4628                                return 1;
4629                        }
4630                }
4631        }
4632        if(type & CS_POWER_DAC)
4633        {
4634                /*
4635                 *  Power down the DAC on the AC97 card.  
4636                 */
4637
4638                CS_DBGOUT(CS_FUNCTION, 4, 
4639                        printk(KERN_INFO "cs46xx: cs461x_powerdown()+ DAC\n"));
4640                tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4641                if (tmp & CS_AC97_POWER_CONTROL_DAC_ON)
4642                {
4643                        if(!muted)
4644                        {
4645                                cs_mute(card, CS_TRUE);
4646                                muted=1;
4647                        }
4648                        tmp |= CS_AC97_POWER_CONTROL_DAC;
4649                        cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4650                        /*
4651                         *  Now, we wait until we sample a ready state.
4652                         */
4653                        for (count = 0; count < 32; count++) {
4654                                /*
4655                                 *  First, lets wait a short while to let things settle out a
4656                                 *  bit, and to prevent retrying the read too quickly.
4657                                 */
4658                                udelay(500);
4659
4660                                /*
4661                                 *  Read the current state of the power control register.
4662                                 */
4663                                if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4664                                        CS_AC97_POWER_CONTROL_DAC_ON))
4665                                        break;
4666                        }
4667                        
4668                        /*
4669                         *  Check the status..
4670                         */
4671                        if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4672                                CS_AC97_POWER_CONTROL_DAC_ON)
4673                        {
4674                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4675                                        "cs46xx: powerdown DAC failed\n"));
4676                                return 1;
4677                        }
4678                }
4679        }
4680        tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4681        if(muted)
4682                cs_mute(card, CS_FALSE);
4683        CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO 
4684                "cs46xx: cs461x_powerdown()- 0 tmp=0x%x\n",tmp));
4685        return 0;
4686}
4687
4688static int cs46xx_powerup(struct cs_card *card, unsigned int type)
4689{
4690        int count;
4691        unsigned int tmp=0,muted=0;
4692
4693        CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
4694                "cs46xx: cs46xx_powerup()+ type=0x%x\n",type));
4695        /*
4696        * check for VREF and powerup if need to.
4697        */
4698        if(type & CS_POWER_MIXVON)
4699                type |= CS_POWER_MIXVOFF;
4700        if(type & (CS_POWER_DAC | CS_POWER_ADC))
4701                type |= CS_POWER_MIXVON | CS_POWER_MIXVOFF;
4702
4703        /*
4704         *  Power up indicated areas.
4705         */
4706        if(type & CS_POWER_MIXVOFF)
4707        {
4708
4709                CS_DBGOUT(CS_FUNCTION, 4, 
4710                        printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVOFF\n"));
4711                /*
4712                 *  Power up the MIXER (VREF ON) on the AC97 card.  
4713                 */
4714                tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4715                if (!(tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4716                {
4717                        if(!muted)
4718                        {
4719                                cs_mute(card, CS_TRUE);
4720                                muted=1;
4721                        }
4722                        tmp &= ~CS_AC97_POWER_CONTROL_MIXVOFF;
4723                        cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4724                        /*
4725                         *  Now, we wait until we sample a ready state.
4726                         */
4727                        for (count = 0; count < 32; count++) {
4728                                /*
4729                                 *  First, lets wait a short while to let things settle out a
4730                                 *  bit, and to prevent retrying the read too quickly.
4731                                 */
4732                                udelay(500);
4733
4734                                /*
4735                                 *  Read the current state of the power control register.
4736                                 */
4737                                if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4738                                        CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4739                                        break;
4740                        }
4741                        
4742                        /*
4743                         *  Check the status..
4744                         */
4745                        if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4746                                CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4747                        {
4748                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4749                                        "cs46xx: powerup MIXVOFF failed\n"));
4750                                return 1;
4751                        }
4752                }
4753        }
4754        if(type & CS_POWER_MIXVON)
4755        {
4756
4757                CS_DBGOUT(CS_FUNCTION, 4, 
4758                        printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVON\n"));
4759                /*
4760                 *  Power up the MIXER (VREF ON) on the AC97 card.  
4761                 */
4762                tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4763                if (!(tmp & CS_AC97_POWER_CONTROL_MIXVON_ON))
4764                {
4765                        if(!muted)
4766                        {
4767                                cs_mute(card, CS_TRUE);
4768                                muted=1;
4769                        }
4770                        tmp &= ~CS_AC97_POWER_CONTROL_MIXVON;
4771                        cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4772                        /*
4773                         *  Now, we wait until we sample a ready state.
4774                         */
4775                        for (count = 0; count < 32; count++) {
4776                                /*
4777                                 *  First, lets wait a short while to let things settle out a
4778                                 *  bit, and to prevent retrying the read too quickly.
4779                                 */
4780                                udelay(500);
4781
4782                                /*
4783                                 *  Read the current state of the power control register.
4784                                 */
4785                                if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4786                                        CS_AC97_POWER_CONTROL_MIXVON_ON)
4787                                        break;
4788                        }
4789                        
4790                        /*
4791                         *  Check the status..
4792                         */
4793                        if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4794                                CS_AC97_POWER_CONTROL_MIXVON_ON))
4795                        {
4796                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4797                                        "cs46xx: powerup MIXVON failed\n"));
4798                                return 1;
4799                        }
4800                }
4801        }
4802        if(type & CS_POWER_ADC)
4803        {
4804                /*
4805                 *  Power up the ADC on the AC97 card.  
4806                 */
4807                CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs46xx_powerup()+ ADC\n"));
4808                tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4809                if (!(tmp & CS_AC97_POWER_CONTROL_ADC_ON))
4810                {
4811                        if(!muted)
4812                        {
4813                                cs_mute(card, CS_TRUE);
4814                                muted=1;
4815                        }
4816                        tmp &= ~CS_AC97_POWER_CONTROL_ADC;
4817                        cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4818
4819                        /*
4820                         *  Now, we wait until we sample a ready state.
4821                         */
4822                        for (count = 0; count < 32; count++) {
4823                                /*
4824                                 *  First, lets wait a short while to let things settle out a
4825                                 *  bit, and to prevent retrying the read too quickly.
4826                                 */
4827                                udelay(500);
4828
4829                                /*
4830                                 *  Read the current state of the power control register.
4831                                 */
4832                                if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4833                                        CS_AC97_POWER_CONTROL_ADC_ON)
4834                                        break;
4835                        }
4836
4837                        /*
4838                         *  Check the status..
4839                         */
4840                        if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4841                                CS_AC97_POWER_CONTROL_ADC_ON))
4842                        {
4843                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4844                                        "cs46xx: powerup ADC failed\n"));
4845                                return 1;
4846                        }
4847                }
4848        }
4849        if(type & CS_POWER_DAC)
4850        {
4851                /*
4852                 *  Power up the DAC on the AC97 card.  
4853                 */
4854
4855                CS_DBGOUT(CS_FUNCTION, 4, 
4856                        printk(KERN_INFO "cs46xx: cs46xx_powerup()+ DAC\n"));
4857                tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4858                if (!(tmp & CS_AC97_POWER_CONTROL_DAC_ON))
4859                {
4860                        if(!muted)
4861                        {
4862                                cs_mute(card, CS_TRUE);
4863                                muted=1;
4864                        }
4865                        tmp &= ~CS_AC97_POWER_CONTROL_DAC;
4866                        cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4867                        /*
4868                         *  Now, we wait until we sample a ready state.
4869                         */
4870                        for (count = 0; count < 32; count++) {
4871                                /*
4872                                 *  First, lets wait a short while to let things settle out a
4873                                 *  bit, and to prevent retrying the read too quickly.
4874                                 */
4875                                udelay(500);
4876
4877                                /*
4878                                 *  Read the current state of the power control register.
4879                                 */
4880                                if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4881                                        CS_AC97_POWER_CONTROL_DAC_ON)
4882                                        break;
4883                        }
4884                        
4885                        /*
4886                         *  Check the status..
4887                         */
4888                        if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4889                                CS_AC97_POWER_CONTROL_DAC_ON))
4890                        {
4891                                CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4892                                        "cs46xx: powerup DAC failed\n"));
4893                                return 1;
4894                        }
4895                }
4896        }
4897        tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4898        if(muted)
4899                cs_mute(card, CS_FALSE);
4900        CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO 
4901                "cs46xx: cs46xx_powerup()- 0 tmp=0x%x\n",tmp));
4902        return 0;
4903}
4904
4905
4906static void cs461x_proc_start(struct cs_card *card)
4907{
4908        int cnt;
4909
4910        /*
4911         *  Set the frame timer to reflect the number of cycles per frame.
4912         */
4913        cs461x_poke(card, BA1_FRMT, 0xadf);
4914        /*
4915         *  Turn on the run, run at frame, and DMA enable bits in the local copy of
4916         *  the SP control register.
4917         */
4918        cs461x_poke(card, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
4919        /*
4920         *  Wait until the run at frame bit resets itself in the SP control
4921         *  register.
4922         */
4923        for (cnt = 0; cnt < 25; cnt++) {
4924                udelay(50);
4925                if (!(cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR))
4926                        break;
4927        }
4928
4929        if (cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR)
4930                printk(KERN_WARNING "cs46xx: SPCR_RUNFR never reset\n");
4931}
4932
4933static void cs461x_proc_stop(struct cs_card *card)
4934{
4935        /*
4936         *  Turn off the run, run at frame, and DMA enable bits in the local copy of
4937         *  the SP control register.
4938         */
4939        cs461x_poke(card, BA1_SPCR, 0);
4940}
4941
4942static int cs_hardware_init(struct cs_card *card)
4943{
4944        unsigned long end_time;
4945        unsigned int tmp,count;
4946        
4947        CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4948                "cs46xx: cs_hardware_init()+\n") );
4949        /* 
4950         *  First, blast the clock control register to zero so that the PLL starts
4951         *  out in a known state, and blast the master serial port control register
4952         *  to zero so that the serial ports also start out in a known state.
4953         */
4954        cs461x_pokeBA0(card, BA0_CLKCR1, 0);
4955        cs461x_pokeBA0(card, BA0_SERMC1, 0);
4956
4957        /*
4958         *  If we are in AC97 mode, then we must set the part to a host controlled
4959         *  AC-link.  Otherwise, we won't be able to bring up the link.
4960         */        
4961        cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_1_03);  /* 1.03 card */
4962        /* cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_2_0); */ /* 2.00 card */
4963
4964        /*
4965         *  Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
4966         *  spec) and then drive it high.  This is done for non AC97 modes since
4967         *  there might be logic external to the CS461x that uses the ARST# line
4968         *  for a reset.
4969         */
4970        cs461x_pokeBA0(card, BA0_ACCTL, 1);
4971        udelay(50);
4972        cs461x_pokeBA0(card, BA0_ACCTL, 0);
4973        udelay(50);
4974        cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_RSTN);
4975
4976        /*
4977         *  The first thing we do here is to enable sync generation.  As soon
4978         *  as we start receiving bit clock, we'll start producing the SYNC
4979         *  signal.
4980         */
4981        cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
4982
4983        /*
4984         *  Now wait for a short while to allow the AC97 part to start
4985         *  generating bit clock (so we don't try to start the PLL without an
4986         *  input clock).
4987         */
4988        mdelay(5 * cs_laptop_wait);             /* 1 should be enough ?? (and pigs might fly) */
4989
4990        /*
4991         *  Set the serial port timing configuration, so that
4992         *  the clock control circuit gets its clock from the correct place.
4993         */
4994        cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97);
4995
4996        /*
4997        * The part seems to not be ready for a while after a resume.
4998        * so, if we are resuming, then wait for 700 mils.  Note that 600 mils
4999        * is not enough for some platforms! tested on an IBM Thinkpads and 
5000        * reference cards.
5001        */
5002        if(!(card->pm.flags & CS46XX_PM_IDLE))
5003                mdelay(initdelay);
5004        /*
5005         *  Write the selected clock control setup to the hardware.  Do not turn on
5006         *  SWCE yet (if requested), so that the devices clocked by the output of
5007         *  PLL are not clocked until the PLL is stable.
5008         */
5009        cs461x_pokeBA0(card, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
5010        cs461x_pokeBA0(card, BA0_PLLM, 0x3a);
5011        cs461x_pokeBA0(card, BA0_CLKCR2, CLKCR2_PDIVS_8);
5012
5013        /*
5014         *  Power up the PLL.
5015         */
5016        cs461x_pokeBA0(card, BA0_CLKCR1, CLKCR1_PLLP);
5017
5018        /*
5019         *  Wait until the PLL has stabilized.
5020         */
5021        mdelay(5 * cs_laptop_wait);             /* Again 1 should be enough ?? */
5022
5023        /*
5024         *  Turn on clocking of the core so that we can setup the serial ports.
5025         */
5026        tmp = cs461x_peekBA0(card, BA0_CLKCR1) | CLKCR1_SWCE;
5027        cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
5028
5029        /*
5030         *  Fill the serial port FIFOs with silence.
5031         */
5032        cs461x_clear_serial_FIFOs(card,CS_TYPE_DAC | CS_TYPE_ADC);
5033
5034        /*
5035         *  Set the serial port FIFO pointer to the first sample in the FIFO.
5036         */
5037        /* cs461x_pokeBA0(card, BA0_SERBSP, 0); */
5038
5039        /*
5040         *  Write the serial port configuration to the part.  The master
5041         *  enable bit is not set until all other values have been written.
5042         */
5043        cs461x_pokeBA0(card, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
5044        cs461x_pokeBA0(card, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
5045        cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
5046
5047
5048        mdelay(5 * cs_laptop_wait);             /* Shouldnt be needed ?? */
5049        
5050/*
5051* If we are resuming under 2.2.x then we can not schedule a timeout.
5052* so, just spin the CPU.
5053*/
5054        if(card->pm.flags & CS46XX_PM_IDLE)
5055        {
5056        /*
5057         * Wait for the card ready signal from the AC97 card.
5058         */
5059                end_time = jiffies + 3 * (HZ >> 2);
5060                do {
5061                /*
5062                 *  Read the AC97 status register to see if we've seen a CODEC READY
5063                 *  signal from the AC97 card.
5064                 */
5065                        if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
5066                                break;
5067                        current->state = TASK_UNINTERRUPTIBLE;
5068                        schedule_timeout(1);
5069                } while (time_before(jiffies, end_time));
5070        }
5071        else
5072        {
5073                for (count = 0; count < 100; count++) {
5074                // First, we want to wait for a short time.
5075                        udelay(25 * cs_laptop_wait);
5076
5077                        if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
5078                                break;
5079                }
5080        }
5081
5082        /*
5083         *  Make sure CODEC is READY.
5084         */
5085        if (!(cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)) {
5086                CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING  
5087                        "cs46xx: create - never read card ready from AC'97\n"));
5088                CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING  
5089                        "cs46xx: probably not a bug, try using the CS4232 driver,\n"));
5090                CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING  
5091                        "cs46xx: or turn off any automatic Power Management support in the BIOS.\n"));
5092                return -EIO;
5093        }
5094
5095        /*
5096         *  Assert the vaid frame signal so that we can start sending commands
5097         *  to the AC97 card.
5098         */
5099        cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
5100
5101        if(card->pm.flags & CS46XX_PM_IDLE)
5102        {
5103        /*
5104         *  Wait until we've sampled input slots 3 and 4 as valid, meaning that
5105         *  the card is pumping ADC data across the AC-link.
5106         */
5107                end_time = jiffies + 3 * (HZ >> 2);
5108                do {
5109                        /*
5110                         *  Read the input slot valid register and see if input slots 3 and
5111                         *  4 are valid yet.
5112                         */
5113                        if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
5114                                break;
5115                        current->state = TASK_UNINTERRUPTIBLE;
5116                        schedule_timeout(1);
5117                } while (time_before(jiffies, end_time));
5118        }
5119        else
5120        {
5121                for (count = 0; count < 100; count++) {
5122                // First, we want to wait for a short time.
5123                        udelay(25 * cs_laptop_wait);
5124
5125                        if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
5126                                break;
5127                }
5128        }
5129        /*
5130         *  Make sure input slots 3 and 4 are valid.  If not, then return
5131         *  an error.
5132         */
5133        if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) != (ACISV_ISV3 | ACISV_ISV4)) {
5134                printk(KERN_WARNING "cs46xx: create - never read ISV3 & ISV4 from AC'97\n");
5135                return -EIO;
5136        }
5137
5138        /*
5139         *  Now, assert valid frame and the slot 3 and 4 valid bits.  This will
5140         *  commense the transfer of digital audio data to the AC97 card.
5141         */
5142        cs461x_pokeBA0(card, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
5143
5144        /*
5145         *  Turn off the Processor by turning off the software clock enable flag in 
5146         *  the clock control register.
5147         */
5148        /* tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE; */
5149        /* cs461x_pokeBA0(card, BA0_CLKCR1, tmp); */
5150
5151        /*
5152         *  Reset the processor.
5153         */
5154        cs461x_reset(card);
5155
5156        /*
5157         *  Download the image to the processor.
5158         */
5159        
5160        cs461x_download_image(card);
5161
5162        /*
5163         *  Stop playback DMA.
5164         */
5165        tmp = cs461x_peek(card, BA1_PCTL);
5166        card->pctl = tmp & 0xffff0000;
5167        cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
5168
5169        /*
5170         *  Stop capture DMA.
5171         */
5172        tmp = cs461x_peek(card, BA1_CCTL);
5173        card->cctl = tmp & 0x0000ffff;
5174        cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
5175
5176        /* initialize AC97 codec and register /dev/mixer */
5177        if(card->pm.flags & CS46XX_PM_IDLE)
5178        {
5179                if (cs_ac97_init(card) <= 0)
5180                {
5181                        CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
5182                                "cs46xx: cs_ac97_init() failure\n") );
5183                        return -EIO;
5184                }
5185        }
5186        else
5187        {
5188                cs46xx_ac97_resume(card);
5189        }
5190        
5191        cs461x_proc_start(card);
5192
5193        /*
5194         *  Enable interrupts on the part.
5195         */
5196        cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM);
5197
5198        tmp = cs461x_peek(card, BA1_PFIE);
5199        tmp &= ~0x0000f03f;
5200        cs461x_poke(card, BA1_PFIE, tmp);       /* playback interrupt enable */
5201
5202        tmp = cs461x_peek(card, BA1_CIE);
5203        tmp &= ~0x0000003f;
5204        tmp |=  0x00000001;
5205        cs461x_poke(card, BA1_CIE, tmp);        /* capture interrupt enable */  
5206
5207        /*
5208         *  If IDLE then Power down the part.  We will power components up 
5209         *  when we need them.  
5210         */
5211        if(card->pm.flags & CS46XX_PM_IDLE)
5212        {
5213                if(!cs_powerdown)
5214                {
5215                        if( (tmp = cs46xx_powerup(card, CS_POWER_DAC | CS_POWER_ADC |
5216                                        CS_POWER_MIXVON )) )
5217                        {
5218                                CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
5219                                        "cs46xx: cs461x_powerup() failure (0x%x)\n",tmp) );
5220                                return -EIO;
5221                        }
5222                }
5223                else
5224                {
5225                        if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
5226                                        CS_POWER_MIXVON, CS_F