linux-old/drivers/sound/ymfpci.c
<<
>>
Prefs
   1/*
   2 *  Copyright 1999 Jaroslav Kysela <perex@suse.cz>
   3 *  Copyright 2000 Alan Cox <alan@redhat.com>
   4 *  Copyright 2001 Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de>
   5 *  Copyright 2002 Pete Zaitcev <zaitcev@yahoo.com>
   6 *
   7 *  Yamaha YMF7xx driver.
   8 *
   9 *  This code is a result of high-speed collision
  10 *  between ymfpci.c of ALSA and cs46xx.c of Linux.
  11 *  -- Pete Zaitcev <zaitcev@yahoo.com>; 2000/09/18
  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 *
  27 * TODO:
  28 *  - Use P44Slot for 44.1 playback (beware of idle buzzing in P44Slot).
  29 *  - 96KHz playback for DVD - use pitch of 2.0.
  30 *  - Retain DMA buffer on close, do not wait the end of frame.
  31 *  - Resolve XXX tagged questions.
  32 *  - Cannot play 5133Hz.
  33 *  - 2001/01/07 Consider if we can remove voice_lock, like so:
  34 *     : Allocate/deallocate voices in open/close under semafore.
  35 *     : We access voices in interrupt, that only for pcms that open.
  36 *    voice_lock around playback_prepare closes interrupts for insane duration.
  37 *  - Revisit the way voice_alloc is done - too confusing, overcomplicated.
  38 *    Should support various channel types, however.
  39 *  - Remove prog_dmabuf from read/write, leave it in open.
  40 *  - 2001/01/07 Replace the OPL3 part of CONFIG_SOUND_YMFPCI_LEGACY code with
  41 *    native synthesizer through a playback slot.
  42 *  - 2001/11/29 ac97_save_state
  43 *    Talk to Kai to remove ac97_save_state before it's too late!
  44 *  - Second AC97
  45 *  - Restore S/PDIF - Toshibas have it.
  46 *
  47 * Kai used pci_alloc_consistent for DMA buffer, which sounds a little
  48 * unconventional. However, given how small our fragments can be,
  49 * a little uncached access is perhaps better than endless flushing.
  50 * On i386 and other I/O-coherent architectures pci_alloc_consistent
  51 * is entirely harmless.
  52 */
  53
  54#include <linux/config.h>
  55#include <linux/module.h>
  56#include <linux/init.h>
  57#include <linux/ioport.h>
  58#include <linux/delay.h>
  59#include <linux/pci.h>
  60#include <linux/slab.h>
  61#include <linux/poll.h>
  62#include <linux/soundcard.h>
  63#include <linux/ac97_codec.h>
  64#include <linux/sound.h>
  65
  66#include <asm/io.h>
  67#include <asm/dma.h>
  68#include <asm/uaccess.h>
  69
  70#ifdef CONFIG_SOUND_YMFPCI_LEGACY
  71# include "sound_config.h"
  72# include "mpu401.h"
  73#endif
  74#include "ymfpci.h"
  75
  76/*
  77 * I do not believe in debug levels as I never can guess what
  78 * part of the code is going to be problematic in the future.
  79 * Don't forget to run your klogd with -c 8.
  80 *
  81 * Example (do not remove):
  82 * #define YMFDBG(fmt, arg...)  do{ printk(KERN_DEBUG fmt, ##arg); }while(0)
  83 */
  84#define YMFDBGW(fmt, arg...)  /* */     /* write counts */
  85#define YMFDBGI(fmt, arg...)  /* */     /* interrupts */
  86#define YMFDBGX(fmt, arg...)  /* */     /* ioctl */
  87
  88static int ymf_playback_trigger(ymfpci_t *unit, struct ymf_pcm *ypcm, int cmd);
  89static void ymf_capture_trigger(ymfpci_t *unit, struct ymf_pcm *ypcm, int cmd);
  90static void ymfpci_voice_free(ymfpci_t *unit, ymfpci_voice_t *pvoice);
  91static int ymf_capture_alloc(struct ymf_unit *unit, int *pbank);
  92static int ymf_playback_prepare(struct ymf_state *state);
  93static int ymf_capture_prepare(struct ymf_state *state);
  94static struct ymf_state *ymf_state_alloc(ymfpci_t *unit);
  95
  96static void ymfpci_aclink_reset(struct pci_dev * pci);
  97static void ymfpci_disable_dsp(ymfpci_t *unit);
  98static void ymfpci_download_image(ymfpci_t *codec);
  99static void ymf_memload(ymfpci_t *unit);
 100
 101static LIST_HEAD(ymf_devs);
 102
 103/*
 104 *  constants
 105 */
 106
 107static struct pci_device_id ymf_id_tbl[] __devinitdata = {
 108#define DEV(v, d, data) \
 109  { PCI_VENDOR_ID_##v, PCI_DEVICE_ID_##v##_##d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)data }
 110        DEV (YAMAHA, 724,  "YMF724"),
 111        DEV (YAMAHA, 724F, "YMF724F"),
 112        DEV (YAMAHA, 740,  "YMF740"),
 113        DEV (YAMAHA, 740C, "YMF740C"),
 114        DEV (YAMAHA, 744,  "YMF744"),
 115        DEV (YAMAHA, 754,  "YMF754"),
 116#undef DEV
 117        { }
 118};
 119MODULE_DEVICE_TABLE(pci, ymf_id_tbl);
 120
 121/*
 122 *  common I/O routines
 123 */
 124
 125static inline u8 ymfpci_readb(ymfpci_t *codec, u32 offset)
 126{
 127        return readb(codec->reg_area_virt + offset);
 128}
 129
 130static inline void ymfpci_writeb(ymfpci_t *codec, u32 offset, u8 val)
 131{
 132        writeb(val, codec->reg_area_virt + offset);
 133}
 134
 135static inline u16 ymfpci_readw(ymfpci_t *codec, u32 offset)
 136{
 137        return readw(codec->reg_area_virt + offset);
 138}
 139
 140static inline void ymfpci_writew(ymfpci_t *codec, u32 offset, u16 val)
 141{
 142        writew(val, codec->reg_area_virt + offset);
 143}
 144
 145static inline u32 ymfpci_readl(ymfpci_t *codec, u32 offset)
 146{
 147        return readl(codec->reg_area_virt + offset);
 148}
 149
 150static inline void ymfpci_writel(ymfpci_t *codec, u32 offset, u32 val)
 151{
 152        writel(val, codec->reg_area_virt + offset);
 153}
 154
 155static int ymfpci_codec_ready(ymfpci_t *unit, int secondary)
 156{
 157        enum { READY_STEP = 10 };
 158        u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
 159        int i;
 160
 161        for (i = 0; i < ((3*1000)/4) / READY_STEP; i++) {
 162                if ((ymfpci_readw(unit, reg) & 0x8000) == 0)
 163                        return 0;
 164                mdelay(READY_STEP);
 165        }
 166        printk(KERN_ERR "ymfpci_codec_ready: codec %i is not ready [0x%x]\n",
 167            secondary, ymfpci_readw(unit, reg));
 168        return -EBUSY;
 169}
 170
 171static void ymfpci_codec_write(struct ac97_codec *dev, u8 reg, u16 val)
 172{
 173        ymfpci_t *codec = dev->private_data;
 174        u32 cmd;
 175
 176        spin_lock(&codec->ac97_lock);
 177        /* XXX Do make use of dev->id */
 178        ymfpci_codec_ready(codec, 0);
 179        cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
 180        ymfpci_writel(codec, YDSXGR_AC97CMDDATA, cmd);
 181        spin_unlock(&codec->ac97_lock);
 182}
 183
 184static u16 ymfpci_codec_read(struct ac97_codec *dev, u8 reg)
 185{
 186        ymfpci_t *unit = dev->private_data;
 187        u16 ret;
 188        int i;
 189
 190        spin_lock(&unit->ac97_lock);
 191        if (ymfpci_codec_ready(unit, 0))
 192                goto out_err;
 193        ymfpci_writew(unit, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
 194        if (ymfpci_codec_ready(unit, 0))
 195                goto out_err;
 196        if (unit->pci->device == PCI_DEVICE_ID_YAMAHA_744 && unit->rev < 2) {
 197                for (i = 0; i < 600; i++)
 198                        ymfpci_readw(unit, YDSXGR_PRISTATUSDATA);
 199        }
 200        ret = ymfpci_readw(unit, YDSXGR_PRISTATUSDATA);
 201        spin_unlock(&unit->ac97_lock);
 202        return ret;
 203
 204 out_err:
 205        spin_unlock(&unit->ac97_lock);
 206        return ~0;
 207}
 208
 209/*
 210 *  Misc routines
 211 */
 212
 213/*
 214 * Calculate the actual sampling rate relatetively to the base clock (48kHz).
 215 */
 216static u32 ymfpci_calc_delta(u32 rate)
 217{
 218        switch (rate) {
 219        case 8000:      return 0x02aaab00;
 220        case 11025:     return 0x03accd00;
 221        case 16000:     return 0x05555500;
 222        case 22050:     return 0x07599a00;
 223        case 32000:     return 0x0aaaab00;
 224        case 44100:     return 0x0eb33300;
 225        default:        return ((rate << 16) / 48000) << 12;
 226        }
 227}
 228
 229static u32 def_rate[8] = {
 230        100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
 231};
 232
 233static u32 ymfpci_calc_lpfK(u32 rate)
 234{
 235        u32 i;
 236        static u32 val[8] = {
 237                0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
 238                0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
 239        };
 240        
 241        if (rate == 44100)
 242                return 0x40000000;      /* FIXME: What's the right value? */
 243        for (i = 0; i < 8; i++)
 244                if (rate <= def_rate[i])
 245                        return val[i];
 246        return val[0];
 247}
 248
 249static u32 ymfpci_calc_lpfQ(u32 rate)
 250{
 251        u32 i;
 252        static u32 val[8] = {
 253                0x35280000, 0x34A70000, 0x32020000, 0x31770000,
 254                0x31390000, 0x31C90000, 0x33D00000, 0x40000000
 255        };
 256        
 257        if (rate == 44100)
 258                return 0x370A0000;
 259        for (i = 0; i < 8; i++)
 260                if (rate <= def_rate[i])
 261                        return val[i];
 262        return val[0];
 263}
 264
 265static u32 ymf_calc_lend(u32 rate)
 266{
 267        return (rate * YMF_SAMPF) / 48000;
 268}
 269
 270/*
 271 * We ever allow only a few formats, but let's be generic, for smaller surprise.
 272 */
 273static int ymf_pcm_format_width(int format)
 274{
 275        static int mask16 = AFMT_S16_LE|AFMT_S16_BE|AFMT_U16_LE|AFMT_U16_BE;
 276
 277        if ((format & (format-1)) != 0) {
 278                printk(KERN_ERR "ymfpci: format 0x%x is not a power of 2\n", format);
 279                return 8;
 280        }
 281
 282        if (format == AFMT_IMA_ADPCM) return 4;
 283        if ((format & mask16) != 0) return 16;
 284        return 8;
 285}
 286
 287static void ymf_pcm_update_shift(struct ymf_pcm_format *f)
 288{
 289        f->shift = 0;
 290        if (f->voices == 2)
 291                f->shift++;
 292        if (ymf_pcm_format_width(f->format) == 16)
 293                f->shift++;
 294}
 295
 296/* Are you sure 32K is not too much? See if mpg123 skips on loaded systems. */
 297#define DMABUF_DEFAULTORDER (15-PAGE_SHIFT)
 298#define DMABUF_MINORDER 1
 299
 300/*
 301 * Allocate DMA buffer
 302 */
 303static int alloc_dmabuf(ymfpci_t *unit, struct ymf_dmabuf *dmabuf)
 304{
 305        void *rawbuf = NULL;
 306        dma_addr_t dma_addr;
 307        int order;
 308        struct page *map, *mapend;
 309
 310        /* alloc as big a chunk as we can */
 311        for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--) {
 312                rawbuf = pci_alloc_consistent(unit->pci, PAGE_SIZE << order, &dma_addr);
 313                if (rawbuf)
 314                        break;
 315        }
 316        if (!rawbuf)
 317                return -ENOMEM;
 318
 319#if 0
 320        printk(KERN_DEBUG "ymfpci: allocated %ld (order = %d) bytes at %p\n",
 321               PAGE_SIZE << order, order, rawbuf);
 322#endif
 323
 324        dmabuf->ready  = dmabuf->mapped = 0;
 325        dmabuf->rawbuf = rawbuf;
 326        dmabuf->dma_addr = dma_addr;
 327        dmabuf->buforder = order;
 328
 329        /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
 330        mapend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
 331        for (map = virt_to_page(rawbuf); map <= mapend; map++)
 332                set_bit(PG_reserved, &map->flags);
 333
 334        return 0;
 335}
 336
 337/*
 338 * Free DMA buffer
 339 */
 340static void dealloc_dmabuf(ymfpci_t *unit, struct ymf_dmabuf *dmabuf)
 341{
 342        struct page *map, *mapend;
 343
 344        if (dmabuf->rawbuf) {
 345                /* undo marking the pages as reserved */
 346                mapend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
 347                for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
 348                        clear_bit(PG_reserved, &map->flags);
 349
 350                pci_free_consistent(unit->pci, PAGE_SIZE << dmabuf->buforder,
 351                    dmabuf->rawbuf, dmabuf->dma_addr);
 352        }
 353        dmabuf->rawbuf = NULL;
 354        dmabuf->mapped = dmabuf->ready = 0;
 355}
 356
 357static int prog_dmabuf(struct ymf_state *state, int rec)
 358{
 359        struct ymf_dmabuf *dmabuf;
 360        int w_16;
 361        unsigned bufsize;
 362        unsigned long flags;
 363        int redzone, redfrags;
 364        int ret;
 365
 366        w_16 = ymf_pcm_format_width(state->format.format) == 16;
 367        dmabuf = rec ? &state->rpcm.dmabuf : &state->wpcm.dmabuf;
 368
 369        spin_lock_irqsave(&state->unit->reg_lock, flags);
 370        dmabuf->hwptr = dmabuf->swptr = 0;
 371        dmabuf->total_bytes = 0;
 372        dmabuf->count = 0;
 373        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
 374
 375        /* allocate DMA buffer if not allocated yet */
 376        if (!dmabuf->rawbuf)
 377                if ((ret = alloc_dmabuf(state->unit, dmabuf)))
 378                        return ret;
 379
 380        /*
 381         * Create fake fragment sizes and numbers for OSS ioctls.
 382         * Import what Doom might have set with SNDCTL_DSP_SETFRAGMENT.
 383         */
 384        bufsize = PAGE_SIZE << dmabuf->buforder;
 385        /* By default we give 4 big buffers. */
 386        dmabuf->fragshift = (dmabuf->buforder + PAGE_SHIFT - 2);
 387        if (dmabuf->ossfragshift > 3 &&
 388            dmabuf->ossfragshift < dmabuf->fragshift) {
 389                /* If OSS set smaller fragments, give more smaller buffers. */
 390                dmabuf->fragshift = dmabuf->ossfragshift;
 391        }
 392        dmabuf->fragsize = 1 << dmabuf->fragshift;
 393
 394        dmabuf->numfrag = bufsize >> dmabuf->fragshift;
 395        dmabuf->dmasize = dmabuf->numfrag << dmabuf->fragshift;
 396
 397        if (dmabuf->ossmaxfrags >= 2) {
 398                redzone = ymf_calc_lend(state->format.rate);
 399                redzone <<= state->format.shift;
 400                redzone *= 3;
 401                redfrags = (redzone + dmabuf->fragsize-1) >> dmabuf->fragshift;
 402
 403                if (dmabuf->ossmaxfrags + redfrags < dmabuf->numfrag) {
 404                        dmabuf->numfrag = dmabuf->ossmaxfrags + redfrags;
 405                        dmabuf->dmasize = dmabuf->numfrag << dmabuf->fragshift;
 406                }
 407        }
 408
 409        memset(dmabuf->rawbuf, w_16 ? 0 : 0x80, dmabuf->dmasize);
 410
 411        /*
 412         *      Now set up the ring 
 413         */
 414
 415        /* XXX   ret = rec? cap_pre(): pbk_pre();  */
 416        spin_lock_irqsave(&state->unit->voice_lock, flags);
 417        if (rec) {
 418                if ((ret = ymf_capture_prepare(state)) != 0) {
 419                        spin_unlock_irqrestore(&state->unit->voice_lock, flags);
 420                        return ret;
 421                }
 422        } else {
 423                if ((ret = ymf_playback_prepare(state)) != 0) {
 424                        spin_unlock_irqrestore(&state->unit->voice_lock, flags);
 425                        return ret;
 426                }
 427        }
 428        spin_unlock_irqrestore(&state->unit->voice_lock, flags);
 429
 430        /* set the ready flag for the dma buffer (this comment is not stupid) */
 431        dmabuf->ready = 1;
 432
 433#if 0
 434        printk(KERN_DEBUG "prog_dmabuf: rate %d format 0x%x,"
 435            " numfrag %d fragsize %d dmasize %d\n",
 436               state->format.rate, state->format.format, dmabuf->numfrag,
 437               dmabuf->fragsize, dmabuf->dmasize);
 438#endif
 439
 440        return 0;
 441}
 442
 443static void ymf_start_dac(struct ymf_state *state)
 444{
 445        ymf_playback_trigger(state->unit, &state->wpcm, 1);
 446}
 447
 448// static void ymf_start_adc(struct ymf_state *state)
 449// {
 450//      ymf_capture_trigger(state->unit, &state->rpcm, 1);
 451// }
 452
 453/*
 454 * Wait until output is drained.
 455 * This does not kill the hardware for the sake of ioctls.
 456 */
 457static void ymf_wait_dac(struct ymf_state *state)
 458{
 459        struct ymf_unit *unit = state->unit;
 460        struct ymf_pcm *ypcm = &state->wpcm;
 461        DECLARE_WAITQUEUE(waita, current);
 462        unsigned long flags;
 463
 464        add_wait_queue(&ypcm->dmabuf.wait, &waita);
 465
 466        spin_lock_irqsave(&unit->reg_lock, flags);
 467        if (ypcm->dmabuf.count != 0 && !ypcm->running) {
 468                ymf_playback_trigger(unit, ypcm, 1);
 469        }
 470
 471#if 0
 472        if (file->f_flags & O_NONBLOCK) {
 473                /*
 474                 * XXX Our  mistake is to attach DMA buffer to state
 475                 * rather than to some per-device structure.
 476                 * Cannot skip waiting, can only make it shorter.
 477                 */
 478        }
 479#endif
 480
 481        set_current_state(TASK_UNINTERRUPTIBLE);
 482        while (ypcm->running) {
 483                spin_unlock_irqrestore(&unit->reg_lock, flags);
 484                schedule();
 485                spin_lock_irqsave(&unit->reg_lock, flags);
 486                set_current_state(TASK_UNINTERRUPTIBLE);
 487        }
 488        spin_unlock_irqrestore(&unit->reg_lock, flags);
 489
 490        set_current_state(TASK_RUNNING);
 491        remove_wait_queue(&ypcm->dmabuf.wait, &waita);
 492
 493        /*
 494         * This function may take up to 4 seconds to reach this point
 495         * (32K circular buffer, 8000 Hz). User notices.
 496         */
 497}
 498
 499/* Can just stop, without wait. Or can we? */
 500static void ymf_stop_adc(struct ymf_state *state)
 501{
 502        struct ymf_unit *unit = state->unit;
 503        unsigned long flags;
 504
 505        spin_lock_irqsave(&unit->reg_lock, flags);
 506        ymf_capture_trigger(unit, &state->rpcm, 0);
 507        spin_unlock_irqrestore(&unit->reg_lock, flags);
 508}
 509
 510/*
 511 *  Hardware start management
 512 */
 513
 514static void ymfpci_hw_start(ymfpci_t *unit)
 515{
 516        unsigned long flags;
 517
 518        spin_lock_irqsave(&unit->reg_lock, flags);
 519        if (unit->start_count++ == 0) {
 520                ymfpci_writel(unit, YDSXGR_MODE,
 521                    ymfpci_readl(unit, YDSXGR_MODE) | 3);
 522                unit->active_bank = ymfpci_readl(unit, YDSXGR_CTRLSELECT) & 1;
 523        }
 524        spin_unlock_irqrestore(&unit->reg_lock, flags);
 525}
 526
 527static void ymfpci_hw_stop(ymfpci_t *unit)
 528{
 529        unsigned long flags;
 530        long timeout = 1000;
 531
 532        spin_lock_irqsave(&unit->reg_lock, flags);
 533        if (--unit->start_count == 0) {
 534                ymfpci_writel(unit, YDSXGR_MODE,
 535                    ymfpci_readl(unit, YDSXGR_MODE) & ~3);
 536                while (timeout-- > 0) {
 537                        if ((ymfpci_readl(unit, YDSXGR_STATUS) & 2) == 0)
 538                                break;
 539                }
 540        }
 541        spin_unlock_irqrestore(&unit->reg_lock, flags);
 542}
 543
 544/*
 545 *  Playback voice management
 546 */
 547
 548static int voice_alloc(ymfpci_t *codec, ymfpci_voice_type_t type, int pair, ymfpci_voice_t *rvoice[])
 549{
 550        ymfpci_voice_t *voice, *voice2;
 551        int idx;
 552
 553        for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
 554                voice = &codec->voices[idx];
 555                voice2 = pair ? &codec->voices[idx+1] : NULL;
 556                if (voice->use || (voice2 && voice2->use))
 557                        continue;
 558                voice->use = 1;
 559                if (voice2)
 560                        voice2->use = 1;
 561                switch (type) {
 562                case YMFPCI_PCM:
 563                        voice->pcm = 1;
 564                        if (voice2)
 565                                voice2->pcm = 1;
 566                        break;
 567                case YMFPCI_SYNTH:
 568                        voice->synth = 1;
 569                        break;
 570                case YMFPCI_MIDI:
 571                        voice->midi = 1;
 572                        break;
 573                }
 574                ymfpci_hw_start(codec);
 575                rvoice[0] = voice;
 576                if (voice2) {
 577                        ymfpci_hw_start(codec);
 578                        rvoice[1] = voice2;
 579                }
 580                return 0;
 581        }
 582        return -EBUSY;  /* Your audio channel is open by someone else. */
 583}
 584
 585static void ymfpci_voice_free(ymfpci_t *unit, ymfpci_voice_t *pvoice)
 586{
 587        ymfpci_hw_stop(unit);
 588        pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
 589        pvoice->ypcm = NULL;
 590}
 591
 592/*
 593 */
 594
 595static void ymf_pcm_interrupt(ymfpci_t *codec, ymfpci_voice_t *voice)
 596{
 597        struct ymf_pcm *ypcm;
 598        int redzone;
 599        int pos, delta, swptr;
 600        int played, distance;
 601        struct ymf_state *state;
 602        struct ymf_dmabuf *dmabuf;
 603        char silence;
 604
 605        if ((ypcm = voice->ypcm) == NULL) {
 606                return;
 607        }
 608        if ((state = ypcm->state) == NULL) {
 609                ypcm->running = 0;      // lock it
 610                return;
 611        }
 612        dmabuf = &ypcm->dmabuf;
 613        spin_lock(&codec->reg_lock);
 614        if (ypcm->running) {
 615                YMFDBGI("ymfpci: %d, intr bank %d count %d start 0x%x:%x\n",
 616                   voice->number, codec->active_bank, dmabuf->count,
 617                   le32_to_cpu(voice->bank[0].start),
 618                   le32_to_cpu(voice->bank[1].start));
 619                silence = (ymf_pcm_format_width(state->format.format) == 16) ?
 620                    0 : 0x80;
 621                /* We need actual left-hand-side redzone size here. */
 622                redzone = ymf_calc_lend(state->format.rate);
 623                redzone <<= (state->format.shift + 1);
 624                swptr = dmabuf->swptr;
 625
 626                pos = le32_to_cpu(voice->bank[codec->active_bank].start);
 627                pos <<= state->format.shift;
 628                if (pos < 0 || pos >= dmabuf->dmasize) {        /* ucode bug */
 629                        printk(KERN_ERR "ymfpci%d: runaway voice %d: hwptr %d=>%d dmasize %d\n",
 630                            codec->dev_audio, voice->number,
 631                            dmabuf->hwptr, pos, dmabuf->dmasize);
 632                        pos = 0;
 633                }
 634                if (pos < dmabuf->hwptr) {
 635                        delta = dmabuf->dmasize - dmabuf->hwptr;
 636                        memset(dmabuf->rawbuf + dmabuf->hwptr, silence, delta);
 637                        delta += pos;
 638                        memset(dmabuf->rawbuf, silence, pos);
 639                } else {
 640                        delta = pos - dmabuf->hwptr;
 641                        memset(dmabuf->rawbuf + dmabuf->hwptr, silence, delta);
 642                }
 643                dmabuf->hwptr = pos;
 644
 645                if (dmabuf->count == 0) {
 646                        printk(KERN_ERR "ymfpci%d: %d: strain: hwptr %d\n",
 647                            codec->dev_audio, voice->number, dmabuf->hwptr);
 648                        ymf_playback_trigger(codec, ypcm, 0);
 649                }
 650
 651                if (swptr <= pos) {
 652                        distance = pos - swptr;
 653                } else {
 654                        distance = dmabuf->dmasize - (swptr - pos);
 655                }
 656                if (distance < redzone) {
 657                        /*
 658                         * hwptr inside redzone => DMA ran out of samples.
 659                         */
 660                        if (delta < dmabuf->count) {
 661                                /*
 662                                 * Lost interrupt or other screwage.
 663                                 */
 664                                printk(KERN_ERR "ymfpci%d: %d: lost: delta %d"
 665                                    " hwptr %d swptr %d distance %d count %d\n",
 666                                    codec->dev_audio, voice->number, delta,
 667                                    dmabuf->hwptr, swptr, distance, dmabuf->count);
 668                        } else {
 669                                /*
 670                                 * Normal end of DMA.
 671                                 */
 672                                YMFDBGI("ymfpci%d: %d: done: delta %d"
 673                                    " hwptr %d swptr %d distance %d count %d\n",
 674                                    codec->dev_audio, voice->number, delta,
 675                                    dmabuf->hwptr, swptr, distance, dmabuf->count);
 676                        }
 677                        played = dmabuf->count;
 678                        if (ypcm->running) {
 679                                ymf_playback_trigger(codec, ypcm, 0);
 680                        }
 681                } else {
 682                        /*
 683                         * hwptr is chipping away towards a remote swptr.
 684                         * Calculate other distance and apply it to count.
 685                         */
 686                        if (swptr >= pos) {
 687                                distance = swptr - pos;
 688                        } else {
 689                                distance = dmabuf->dmasize - (pos - swptr);
 690                        }
 691                        if (distance < dmabuf->count) {
 692                                played = dmabuf->count - distance;
 693                        } else {
 694                                played = 0;
 695                        }
 696                }
 697
 698                dmabuf->total_bytes += played;
 699                dmabuf->count -= played;
 700                if (dmabuf->count < dmabuf->dmasize / 2) {
 701                        wake_up(&dmabuf->wait);
 702                }
 703        }
 704        spin_unlock(&codec->reg_lock);
 705}
 706
 707static void ymf_cap_interrupt(ymfpci_t *unit, struct ymf_capture *cap)
 708{
 709        struct ymf_pcm *ypcm;
 710        int redzone;
 711        struct ymf_state *state;
 712        struct ymf_dmabuf *dmabuf;
 713        int pos, delta;
 714        int cnt;
 715
 716        if ((ypcm = cap->ypcm) == NULL) {
 717                return;
 718        }
 719        if ((state = ypcm->state) == NULL) {
 720                ypcm->running = 0;      // lock it
 721                return;
 722        }
 723        dmabuf = &ypcm->dmabuf;
 724        spin_lock(&unit->reg_lock);
 725        if (ypcm->running) {
 726                redzone = ymf_calc_lend(state->format.rate);
 727                redzone <<= (state->format.shift + 1);
 728
 729                pos = le32_to_cpu(cap->bank[unit->active_bank].start);
 730                // pos <<= state->format.shift;
 731                if (pos < 0 || pos >= dmabuf->dmasize) {        /* ucode bug */
 732                        printk(KERN_ERR "ymfpci%d: runaway capture %d: hwptr %d=>%d dmasize %d\n",
 733                            unit->dev_audio, ypcm->capture_bank_number,
 734                            dmabuf->hwptr, pos, dmabuf->dmasize);
 735                        pos = 0;
 736                }
 737                if (pos < dmabuf->hwptr) {
 738                        delta = dmabuf->dmasize - dmabuf->hwptr;
 739                        delta += pos;
 740                } else {
 741                        delta = pos - dmabuf->hwptr;
 742                }
 743                dmabuf->hwptr = pos;
 744
 745                cnt = dmabuf->count;
 746                cnt += delta;
 747                if (cnt + redzone > dmabuf->dmasize) {
 748                        /* Overflow - bump swptr */
 749                        dmabuf->count = dmabuf->dmasize - redzone;
 750                        dmabuf->swptr = dmabuf->hwptr + redzone;
 751                        if (dmabuf->swptr >= dmabuf->dmasize) {
 752                                dmabuf->swptr -= dmabuf->dmasize;
 753                        }
 754                } else {
 755                        dmabuf->count = cnt;
 756                }
 757
 758                dmabuf->total_bytes += delta;
 759                if (dmabuf->count) {            /* && is_sleeping  XXX */
 760                        wake_up(&dmabuf->wait);
 761                }
 762        }
 763        spin_unlock(&unit->reg_lock);
 764}
 765
 766static int ymf_playback_trigger(ymfpci_t *codec, struct ymf_pcm *ypcm, int cmd)
 767{
 768
 769        if (ypcm->voices[0] == NULL) {
 770                return -EINVAL;
 771        }
 772        if (cmd != 0) {
 773                codec->ctrl_playback[ypcm->voices[0]->number + 1] =
 774                    cpu_to_le32(ypcm->voices[0]->bank_ba);
 775                if (ypcm->voices[1] != NULL)
 776                        codec->ctrl_playback[ypcm->voices[1]->number + 1] =
 777                            cpu_to_le32(ypcm->voices[1]->bank_ba);
 778                ypcm->running = 1;
 779        } else {
 780                codec->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
 781                if (ypcm->voices[1] != NULL)
 782                        codec->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
 783                ypcm->running = 0;
 784        }
 785        return 0;
 786}
 787
 788static void ymf_capture_trigger(ymfpci_t *codec, struct ymf_pcm *ypcm, int cmd)
 789{
 790        u32 tmp;
 791
 792        if (cmd != 0) {
 793                tmp = ymfpci_readl(codec, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
 794                ymfpci_writel(codec, YDSXGR_MAPOFREC, tmp);
 795                ypcm->running = 1;
 796        } else {
 797                tmp = ymfpci_readl(codec, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
 798                ymfpci_writel(codec, YDSXGR_MAPOFREC, tmp);
 799                ypcm->running = 0;
 800        }
 801}
 802
 803static int ymfpci_pcm_voice_alloc(struct ymf_pcm *ypcm, int voices)
 804{
 805        struct ymf_unit *unit;
 806        int err;
 807
 808        unit = ypcm->state->unit;
 809        if (ypcm->voices[1] != NULL && voices < 2) {
 810                ymfpci_voice_free(unit, ypcm->voices[1]);
 811                ypcm->voices[1] = NULL;
 812        }
 813        if (voices == 1 && ypcm->voices[0] != NULL)
 814                return 0;               /* already allocated */
 815        if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
 816                return 0;               /* already allocated */
 817        if (voices > 1) {
 818                if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
 819                        ymfpci_voice_free(unit, ypcm->voices[0]);
 820                        ypcm->voices[0] = NULL;
 821                }               
 822                if ((err = voice_alloc(unit, YMFPCI_PCM, 1, ypcm->voices)) < 0)
 823                        return err;
 824                ypcm->voices[0]->ypcm = ypcm;
 825                ypcm->voices[1]->ypcm = ypcm;
 826        } else {
 827                if ((err = voice_alloc(unit, YMFPCI_PCM, 0, ypcm->voices)) < 0)
 828                        return err;
 829                ypcm->voices[0]->ypcm = ypcm;
 830        }
 831        return 0;
 832}
 833
 834static void ymf_pcm_init_voice(ymfpci_voice_t *voice, int stereo,
 835    int rate, int w_16, unsigned long addr, unsigned int end, int spdif)
 836{
 837        u32 format;
 838        u32 delta = ymfpci_calc_delta(rate);
 839        u32 lpfQ = ymfpci_calc_lpfQ(rate);
 840        u32 lpfK = ymfpci_calc_lpfK(rate);
 841        ymfpci_playback_bank_t *bank;
 842        int nbank;
 843
 844        /*
 845         * The gain is a floating point number. According to the manual,
 846         * bit 31 indicates a sign bit, bit 30 indicates an integer part,
 847         * and bits [29:15] indicate a decimal fraction part. Thus,
 848         * for a gain of 1.0 the constant of 0x40000000 is loaded.
 849         */
 850        unsigned default_gain = cpu_to_le32(0x40000000);
 851
 852        format = (stereo ? 0x00010000 : 0) | (w_16 ? 0 : 0x80000000);
 853        if (stereo)
 854                end >>= 1;
 855        if (w_16)
 856                end >>= 1;
 857        for (nbank = 0; nbank < 2; nbank++) {
 858                bank = &voice->bank[nbank];
 859                bank->format = cpu_to_le32(format);
 860                bank->loop_default = 0; /* 0-loops forever, otherwise count */
 861                bank->base = cpu_to_le32(addr);
 862                bank->loop_start = 0;
 863                bank->loop_end = cpu_to_le32(end);
 864                bank->loop_frac = 0;
 865                bank->eg_gain_end = default_gain;
 866                bank->lpfQ = cpu_to_le32(lpfQ);
 867                bank->status = 0;
 868                bank->num_of_frames = 0;
 869                bank->loop_count = 0;
 870                bank->start = 0;
 871                bank->start_frac = 0;
 872                bank->delta =
 873                bank->delta_end = cpu_to_le32(delta);
 874                bank->lpfK =
 875                bank->lpfK_end = cpu_to_le32(lpfK);
 876                bank->eg_gain = default_gain;
 877                bank->lpfD1 =
 878                bank->lpfD2 = 0;
 879
 880                bank->left_gain = 
 881                bank->right_gain =
 882                bank->left_gain_end =
 883                bank->right_gain_end =
 884                bank->eff1_gain =
 885                bank->eff2_gain =
 886                bank->eff3_gain =
 887                bank->eff1_gain_end =
 888                bank->eff2_gain_end =
 889                bank->eff3_gain_end = 0;
 890
 891                if (!stereo) {
 892                        if (!spdif) {
 893                                bank->left_gain = 
 894                                bank->right_gain =
 895                                bank->left_gain_end =
 896                                bank->right_gain_end = default_gain;
 897                        } else {
 898                                bank->eff2_gain =
 899                                bank->eff2_gain_end =
 900                                bank->eff3_gain =
 901                                bank->eff3_gain_end = default_gain;
 902                        }
 903                } else {
 904                        if (!spdif) {
 905                                if ((voice->number & 1) == 0) {
 906                                        bank->left_gain =
 907                                        bank->left_gain_end = default_gain;
 908                                } else {
 909                                        bank->format |= cpu_to_le32(1);
 910                                        bank->right_gain =
 911                                        bank->right_gain_end = default_gain;
 912                                }
 913                        } else {
 914                                if ((voice->number & 1) == 0) {
 915                                        bank->eff2_gain =
 916                                        bank->eff2_gain_end = default_gain;
 917                                } else {
 918                                        bank->format |= cpu_to_le32(1);
 919                                        bank->eff3_gain =
 920                                        bank->eff3_gain_end = default_gain;
 921                                }
 922                        }
 923                }
 924        }
 925}
 926
 927/*
 928 * XXX Capture channel allocation is entirely fake at the moment.
 929 * We use only one channel and mark it busy as required.
 930 */
 931static int ymf_capture_alloc(struct ymf_unit *unit, int *pbank)
 932{
 933        struct ymf_capture *cap;
 934        int cbank;
 935
 936        cbank = 1;              /* Only ADC slot is used for now. */
 937        cap = &unit->capture[cbank];
 938        if (cap->use)
 939                return -EBUSY;
 940        cap->use = 1;
 941        *pbank = cbank;
 942        return 0;
 943}
 944
 945static int ymf_playback_prepare(struct ymf_state *state)
 946{
 947        struct ymf_pcm *ypcm = &state->wpcm;
 948        int err, nvoice;
 949
 950        if ((err = ymfpci_pcm_voice_alloc(ypcm, state->format.voices)) < 0) {
 951                /* Somebody started 32 mpg123's in parallel? */
 952                printk(KERN_INFO "ymfpci%d: cannot allocate voice\n",
 953                    state->unit->dev_audio);
 954                return err;
 955        }
 956
 957        for (nvoice = 0; nvoice < state->format.voices; nvoice++) {
 958                ymf_pcm_init_voice(ypcm->voices[nvoice],
 959                    state->format.voices == 2, state->format.rate,
 960                    ymf_pcm_format_width(state->format.format) == 16,
 961                    ypcm->dmabuf.dma_addr, ypcm->dmabuf.dmasize,
 962                    ypcm->spdif);
 963        }
 964        return 0;
 965}
 966
 967static int ymf_capture_prepare(struct ymf_state *state)
 968{
 969        ymfpci_t *unit = state->unit;
 970        struct ymf_pcm *ypcm = &state->rpcm;
 971        ymfpci_capture_bank_t * bank;
 972        /* XXX This is confusing, gotta rename one of them banks... */
 973        int nbank;              /* flip-flop bank */
 974        int cbank;              /* input [super-]bank */
 975        struct ymf_capture *cap;
 976        u32 rate, format;
 977
 978        if (ypcm->capture_bank_number == -1) {
 979                if (ymf_capture_alloc(unit, &cbank) != 0)
 980                        return -EBUSY;
 981
 982                ypcm->capture_bank_number = cbank;
 983
 984                cap = &unit->capture[cbank];
 985                cap->bank = unit->bank_capture[cbank][0];
 986                cap->ypcm = ypcm;
 987                ymfpci_hw_start(unit);
 988        }
 989
 990        // ypcm->frag_size = snd_pcm_lib_transfer_fragment(substream);
 991        // frag_size is replaced with nonfragged byte-aligned rolling buffer
 992        rate = ((48000 * 4096) / state->format.rate) - 1;
 993        format = 0;
 994        if (state->format.voices == 2)
 995                format |= 2;
 996        if (ymf_pcm_format_width(state->format.format) == 8)
 997                format |= 1;
 998        switch (ypcm->capture_bank_number) {
 999        case 0:
1000                ymfpci_writel(unit, YDSXGR_RECFORMAT, format);
1001                ymfpci_writel(unit, YDSXGR_RECSLOTSR, rate);
1002                break;
1003        case 1:
1004                ymfpci_writel(unit, YDSXGR_ADCFORMAT, format);
1005                ymfpci_writel(unit, YDSXGR_ADCSLOTSR, rate);
1006                break;
1007        }
1008        for (nbank = 0; nbank < 2; nbank++) {
1009                bank = unit->bank_capture[ypcm->capture_bank_number][nbank];
1010                bank->base = cpu_to_le32(ypcm->dmabuf.dma_addr);
1011                // bank->loop_end = ypcm->dmabuf.dmasize >> state->format.shift;
1012                bank->loop_end = cpu_to_le32(ypcm->dmabuf.dmasize);
1013                bank->start = 0;
1014                bank->num_of_loops = 0;
1015        }
1016#if 0 /* s/pdif */
1017        if (state->digital.dig_valid)
1018                /*state->digital.type == SND_PCM_DIG_AES_IEC958*/
1019                ymfpci_writew(codec, YDSXGR_SPDIFOUTSTATUS,
1020                    state->digital.dig_status[0] | (state->digital.dig_status[1] << 8));
1021#endif
1022        return 0;
1023}
1024
1025void ymf_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1026{
1027        ymfpci_t *codec = dev_id;
1028        u32 status, nvoice, mode;
1029        struct ymf_voice *voice;
1030        struct ymf_capture *cap;
1031
1032        status = ymfpci_readl(codec, YDSXGR_STATUS);
1033        if (status & 0x80000000) {
1034                codec->active_bank = ymfpci_readl(codec, YDSXGR_CTRLSELECT) & 1;
1035                spin_lock(&codec->voice_lock);
1036                for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
1037                        voice = &codec->voices[nvoice];
1038                        if (voice->use)
1039                                ymf_pcm_interrupt(codec, voice);
1040                }
1041                for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
1042                        cap = &codec->capture[nvoice];
1043                        if (cap->use)
1044                                ymf_cap_interrupt(codec, cap);
1045                }
1046                spin_unlock(&codec->voice_lock);
1047                spin_lock(&codec->reg_lock);
1048                ymfpci_writel(codec, YDSXGR_STATUS, 0x80000000);
1049                mode = ymfpci_readl(codec, YDSXGR_MODE) | 2;
1050                ymfpci_writel(codec, YDSXGR_MODE, mode);
1051                spin_unlock(&codec->reg_lock);
1052        }
1053
1054        status = ymfpci_readl(codec, YDSXGR_INTFLAG);
1055        if (status & 1) {
1056                /* timer handler */
1057                ymfpci_writel(codec, YDSXGR_INTFLAG, ~0);
1058        }
1059}
1060
1061static void ymf_pcm_free_substream(struct ymf_pcm *ypcm)
1062{
1063        unsigned long flags;
1064        struct ymf_unit *unit;
1065
1066        unit = ypcm->state->unit;
1067
1068        if (ypcm->type == PLAYBACK_VOICE) {
1069                spin_lock_irqsave(&unit->voice_lock, flags);
1070                if (ypcm->voices[1])
1071                        ymfpci_voice_free(unit, ypcm->voices[1]);
1072                if (ypcm->voices[0])
1073                        ymfpci_voice_free(unit, ypcm->voices[0]);
1074                spin_unlock_irqrestore(&unit->voice_lock, flags);
1075        } else {
1076                if (ypcm->capture_bank_number != -1) {
1077                        unit->capture[ypcm->capture_bank_number].use = 0;
1078                        ypcm->capture_bank_number = -1;
1079                        ymfpci_hw_stop(unit);
1080                }
1081        }
1082}
1083
1084static struct ymf_state *ymf_state_alloc(ymfpci_t *unit)
1085{
1086        struct ymf_pcm *ypcm;
1087        struct ymf_state *state;
1088
1089        if ((state = kmalloc(sizeof(struct ymf_state), GFP_KERNEL)) == NULL) {
1090                goto out0;
1091        }
1092        memset(state, 0, sizeof(struct ymf_state));
1093
1094        ypcm = &state->wpcm;
1095        ypcm->state = state;
1096        ypcm->type = PLAYBACK_VOICE;
1097        ypcm->capture_bank_number = -1;
1098        init_waitqueue_head(&ypcm->dmabuf.wait);
1099
1100        ypcm = &state->rpcm;
1101        ypcm->state = state;
1102        ypcm->type = CAPTURE_AC97;
1103        ypcm->capture_bank_number = -1;
1104        init_waitqueue_head(&ypcm->dmabuf.wait);
1105
1106        state->unit = unit;
1107
1108        state->format.format = AFMT_U8;
1109        state->format.rate = 8000;
1110        state->format.voices = 1;
1111        ymf_pcm_update_shift(&state->format);
1112
1113        return state;
1114
1115out0:
1116        return NULL;
1117}
1118
1119/* AES/IEC958 channel status bits */
1120#define SND_PCM_AES0_PROFESSIONAL       (1<<0)  /* 0 = consumer, 1 = professional */
1121#define SND_PCM_AES0_NONAUDIO           (1<<1)  /* 0 = audio, 1 = non-audio */
1122#define SND_PCM_AES0_PRO_EMPHASIS       (7<<2)  /* mask - emphasis */
1123#define SND_PCM_AES0_PRO_EMPHASIS_NOTID (0<<2)  /* emphasis not indicated */
1124#define SND_PCM_AES0_PRO_EMPHASIS_NONE  (1<<2)  /* none emphasis */
1125#define SND_PCM_AES0_PRO_EMPHASIS_5015  (3<<2)  /* 50/15us emphasis */
1126#define SND_PCM_AES0_PRO_EMPHASIS_CCITT (7<<2)  /* CCITT J.17 emphasis */
1127#define SND_PCM_AES0_PRO_FREQ_UNLOCKED  (1<<5)  /* source sample frequency: 0 = locked, 1 = unlocked */
1128#define SND_PCM_AES0_PRO_FS             (3<<6)  /* mask - sample frequency */
1129#define SND_PCM_AES0_PRO_FS_NOTID       (0<<6)  /* fs not indicated */
1130#define SND_PCM_AES0_PRO_FS_44100       (1<<6)  /* 44.1kHz */
1131#define SND_PCM_AES0_PRO_FS_48000       (2<<6)  /* 48kHz */
1132#define SND_PCM_AES0_PRO_FS_32000       (3<<6)  /* 32kHz */
1133#define SND_PCM_AES0_CON_NOT_COPYRIGHT  (1<<2)  /* 0 = copyright, 1 = not copyright */
1134#define SND_PCM_AES0_CON_EMPHASIS       (7<<3)  /* mask - emphasis */
1135#define SND_PCM_AES0_CON_EMPHASIS_NONE  (0<<3)  /* none emphasis */
1136#define SND_PCM_AES0_CON_EMPHASIS_5015  (1<<3)  /* 50/15us emphasis */
1137#define SND_PCM_AES0_CON_MODE           (3<<6)  /* mask - mode */
1138#define SND_PCM_AES1_PRO_MODE           (15<<0) /* mask - channel mode */
1139#define SND_PCM_AES1_PRO_MODE_NOTID     (0<<0)  /* not indicated */
1140#define SND_PCM_AES1_PRO_MODE_STEREOPHONIC (2<<0) /* stereophonic - ch A is left */
1141#define SND_PCM_AES1_PRO_MODE_SINGLE    (4<<0)  /* single channel */
1142#define SND_PCM_AES1_PRO_MODE_TWO       (8<<0)  /* two channels */
1143#define SND_PCM_AES1_PRO_MODE_PRIMARY   (12<<0) /* primary/secondary */
1144#define SND_PCM_AES1_PRO_MODE_BYTE3     (15<<0) /* vector to byte 3 */
1145#define SND_PCM_AES1_PRO_USERBITS       (15<<4) /* mask - user bits */
1146#define SND_PCM_AES1_PRO_USERBITS_NOTID (0<<4)  /* not indicated */
1147#define SND_PCM_AES1_PRO_USERBITS_192   (8<<4)  /* 192-bit structure */
1148#define SND_PCM_AES1_PRO_USERBITS_UDEF  (12<<4) /* user defined application */
1149#define SND_PCM_AES1_CON_CATEGORY       0x7f
1150#define SND_PCM_AES1_CON_GENERAL        0x00
1151#define SND_PCM_AES1_CON_EXPERIMENTAL   0x40
1152#define SND_PCM_AES1_CON_SOLIDMEM_MASK  0x0f
1153#define SND_PCM_AES1_CON_SOLIDMEM_ID    0x08
1154#define SND_PCM_AES1_CON_BROADCAST1_MASK 0x07
1155#define SND_PCM_AES1_CON_BROADCAST1_ID  0x04
1156#define SND_PCM_AES1_CON_DIGDIGCONV_MASK 0x07
1157#define SND_PCM_AES1_CON_DIGDIGCONV_ID  0x02
1158#define SND_PCM_AES1_CON_ADC_COPYRIGHT_MASK 0x1f
1159#define SND_PCM_AES1_CON_ADC_COPYRIGHT_ID 0x06
1160#define SND_PCM_AES1_CON_ADC_MASK       0x1f
1161#define SND_PCM_AES1_CON_ADC_ID         0x16
1162#define SND_PCM_AES1_CON_BROADCAST2_MASK 0x0f
1163#define SND_PCM_AES1_CON_BROADCAST2_ID  0x0e
1164#define SND_PCM_AES1_CON_LASEROPT_MASK  0x07
1165#define SND_PCM_AES1_CON_LASEROPT_ID    0x01
1166#define SND_PCM_AES1_CON_MUSICAL_MASK   0x07
1167#define SND_PCM_AES1_CON_MUSICAL_ID     0x05
1168#define SND_PCM_AES1_CON_MAGNETIC_MASK  0x07
1169#define SND_PCM_AES1_CON_MAGNETIC_ID    0x03
1170#define SND_PCM_AES1_CON_IEC908_CD      (SND_PCM_AES1_CON_LASEROPT_ID|0x00)
1171#define SND_PCM_AES1_CON_NON_IEC908_CD  (SND_PCM_AES1_CON_LASEROPT_ID|0x08)
1172#define SND_PCM_AES1_CON_PCM_CODER      (SND_PCM_AES1_CON_DIGDIGCONV_ID|0x00)
1173#define SND_PCM_AES1_CON_SAMPLER        (SND_PCM_AES1_CON_DIGDIGCONV_ID|0x20)
1174#define SND_PCM_AES1_CON_MIXER          (SND_PCM_AES1_CON_DIGDIGCONV_ID|0x10)
1175#define SND_PCM_AES1_CON_RATE_CONVERTER (SND_PCM_AES1_CON_DIGDIGCONV_ID|0x18)
1176#define SND_PCM_AES1_CON_SYNTHESIZER    (SND_PCM_AES1_CON_MUSICAL_ID|0x00)
1177#define SND_PCM_AES1_CON_MICROPHONE     (SND_PCM_AES1_CON_MUSICAL_ID|0x08)
1178#define SND_PCM_AES1_CON_DAT            (SND_PCM_AES1_CON_MAGNETIC_ID|0x00)
1179#define SND_PCM_AES1_CON_VCR            (SND_PCM_AES1_CON_MAGNETIC_ID|0x08)
1180#define SND_PCM_AES1_CON_ORIGINAL       (1<<7)  /* this bits depends on the category code */
1181#define SND_PCM_AES2_PRO_SBITS          (7<<0)  /* mask - sample bits */
1182#define SND_PCM_AES2_PRO_SBITS_20       (2<<0)  /* 20-bit - coordination */
1183#define SND_PCM_AES2_PRO_SBITS_24       (4<<0)  /* 24-bit - main audio */
1184#define SND_PCM_AES2_PRO_SBITS_UDEF     (6<<0)  /* user defined application */
1185#define SND_PCM_AES2_PRO_WORDLEN        (7<<3)  /* mask - source word length */
1186#define SND_PCM_AES2_PRO_WORDLEN_NOTID  (0<<3)  /* not indicated */
1187#define SND_PCM_AES2_PRO_WORDLEN_22_18  (2<<3)  /* 22-bit or 18-bit */
1188#define SND_PCM_AES2_PRO_WORDLEN_23_19  (4<<3)  /* 23-bit or 19-bit */
1189#define SND_PCM_AES2_PRO_WORDLEN_24_20  (5<<3)  /* 24-bit or 20-bit */
1190#define SND_PCM_AES2_PRO_WORDLEN_20_16  (6<<3)  /* 20-bit or 16-bit */
1191#define SND_PCM_AES2_CON_SOURCE         (15<<0) /* mask - source number */
1192#define SND_PCM_AES2_CON_SOURCE_UNSPEC  (0<<0)  /* unspecified */
1193#define SND_PCM_AES2_CON_CHANNEL        (15<<4) /* mask - channel number */
1194#define SND_PCM_AES2_CON_CHANNEL_UNSPEC (0<<4)  /* unspecified */
1195#define SND_PCM_AES3_CON_FS             (15<<0) /* mask - sample frequency */
1196#define SND_PCM_AES3_CON_FS_44100       (0<<0)  /* 44.1kHz */
1197#define SND_PCM_AES3_CON_FS_48000       (2<<0)  /* 48kHz */
1198#define SND_PCM_AES3_CON_FS_32000       (3<<0)  /* 32kHz */
1199#define SND_PCM_AES3_CON_CLOCK          (3<<4)  /* mask - clock accuracy */
1200#define SND_PCM_AES3_CON_CLOCK_1000PPM  (0<<4)  /* 1000 ppm */
1201#define SND_PCM_AES3_CON_CLOCK_50PPM    (1<<4)  /* 50 ppm */
1202#define SND_PCM_AES3_CON_CLOCK_VARIABLE (2<<4)  /* variable pitch */
1203
1204/*
1205 * User interface
1206 */
1207
1208/*
1209 * in this loop, dmabuf.count signifies the amount of data that is
1210 * waiting to be copied to the user's buffer.  it is filled by the dma
1211 * machine and drained by this loop.
1212 */
1213static ssize_t
1214ymf_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
1215{
1216        struct ymf_state *state = (struct ymf_state *)file->private_data;
1217        struct ymf_dmabuf *dmabuf = &state->rpcm.dmabuf;
1218        struct ymf_unit *unit = state->unit;
1219        DECLARE_WAITQUEUE(waita, current);
1220        ssize_t ret;
1221        unsigned long flags;
1222        unsigned int swptr;
1223        int cnt;                        /* This many to go in this revolution */
1224
1225        if (ppos != &file->f_pos)
1226                return -ESPIPE;
1227        if (dmabuf->mapped)
1228                return -ENXIO;
1229        if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
1230                return ret;
1231        ret = 0;
1232
1233        add_wait_queue(&dmabuf->wait, &waita);
1234        set_current_state(TASK_INTERRUPTIBLE);
1235        while (count > 0) {
1236                spin_lock_irqsave(&unit->reg_lock, flags);
1237                if (unit->suspended) {
1238                        spin_unlock_irqrestore(&unit->reg_lock, flags);
1239                        schedule();
1240                        set_current_state(TASK_INTERRUPTIBLE);
1241                        if (signal_pending(current)) {
1242                                if (!ret) ret = -EAGAIN;
1243                                break;
1244                        }
1245                        continue;
1246                }
1247                swptr = dmabuf->swptr;
1248                cnt = dmabuf->dmasize - swptr;
1249                if (dmabuf->count < cnt)
1250                        cnt = dmabuf->count;
1251                spin_unlock_irqrestore(&unit->reg_lock, flags);
1252
1253                if (cnt > count)
1254                        cnt = count;
1255                if (cnt <= 0) {
1256                        unsigned long tmo;
1257                        /* buffer is empty, start the dma machine and wait for data to be
1258                           recorded */
1259                        spin_lock_irqsave(&state->unit->reg_lock, flags);
1260                        if (!state->rpcm.running) {
1261                                ymf_capture_trigger(state->unit, &state->rpcm, 1);
1262                        }
1263                        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1264                        if (file->f_flags & O_NONBLOCK) {
1265                                if (!ret) ret = -EAGAIN;
1266                                break;
1267                        }
1268                        /* This isnt strictly right for the 810  but it'll do */
1269                        tmo = (dmabuf->dmasize * HZ) / (state->format.rate * 2);
1270                        tmo >>= state->format.shift;
1271                        /* There are two situations when sleep_on_timeout returns, one is when
1272                           the interrupt is serviced correctly and the process is waked up by
1273                           ISR ON TIME. Another is when timeout is expired, which means that
1274                           either interrupt is NOT serviced correctly (pending interrupt) or it
1275                           is TOO LATE for the process to be scheduled to run (scheduler latency)
1276                           which results in a (potential) buffer overrun. And worse, there is
1277                           NOTHING we can do to prevent it. */
1278                        tmo = schedule_timeout(tmo);
1279                        spin_lock_irqsave(&state->unit->reg_lock, flags);
1280                        set_current_state(TASK_INTERRUPTIBLE);
1281                        if (tmo == 0 && dmabuf->count == 0) {
1282                                printk(KERN_ERR "ymfpci%d: recording schedule timeout, "
1283                                    "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1284                                    state->unit->dev_audio,
1285                                    dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
1286                                    dmabuf->hwptr, dmabuf->swptr);
1287                        }
1288                        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1289                        if (signal_pending(current)) {
1290                                if (!ret) ret = -ERESTARTSYS;
1291                                break;
1292                        }
1293                        continue;
1294                }
1295
1296                if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
1297                        if (!ret) ret = -EFAULT;
1298                        break;
1299                }
1300
1301                swptr = (swptr + cnt) % dmabuf->dmasize;
1302
1303                spin_lock_irqsave(&unit->reg_lock, flags);
1304                if (unit->suspended) {
1305                        spin_unlock_irqrestore(&unit->reg_lock, flags);
1306                        continue;
1307                }
1308
1309                dmabuf->swptr = swptr;
1310                dmabuf->count -= cnt;
1311                // spin_unlock_irqrestore(&unit->reg_lock, flags);
1312
1313                count -= cnt;
1314                buffer += cnt;
1315                ret += cnt;
1316                // spin_lock_irqsave(&unit->reg_lock, flags);
1317                if (!state->rpcm.running) {
1318                        ymf_capture_trigger(unit, &state->rpcm, 1);
1319                }
1320                spin_unlock_irqrestore(&unit->reg_lock, flags);
1321        }
1322        set_current_state(TASK_RUNNING);
1323        remove_wait_queue(&dmabuf->wait, &waita);
1324
1325        return ret;
1326}
1327
1328static ssize_t
1329ymf_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
1330{
1331        struct ymf_state *state = (struct ymf_state *)file->private_data;
1332        struct ymf_dmabuf *dmabuf = &state->wpcm.dmabuf;
1333        struct ymf_unit *unit = state->unit;
1334        DECLARE_WAITQUEUE(waita, current);
1335        ssize_t ret;
1336        unsigned long flags;
1337        unsigned int swptr;
1338        int cnt;                        /* This many to go in this revolution */
1339        int redzone;
1340        int delay;
1341
1342        YMFDBGW("ymf_write: count %d\n", count);
1343
1344        if (ppos != &file->f_pos)
1345                return -ESPIPE;
1346        if (dmabuf->mapped)
1347                return -ENXIO;
1348        if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
1349                return ret;
1350        ret = 0;
1351
1352        /*
1353         * Alan's cs46xx works without a red zone - marvel of ingenuity.
1354         * We are not so brilliant... Red zone does two things:
1355         *  1. allows for safe start after a pause as we have no way
1356         *     to know what the actual, relentlessly advancing, hwptr is.
1357         *  2. makes computations in ymf_pcm_interrupt simpler.
1358         */
1359        redzone = ymf_calc_lend(state->format.rate) << state->format.shift;
1360        redzone *= 3;   /* 2 redzone + 1 possible uncertainty reserve. */
1361
1362        add_wait_queue(&dmabuf->wait, &waita);
1363        set_current_state(TASK_INTERRUPTIBLE);
1364        while (count > 0) {
1365                spin_lock_irqsave(&unit->reg_lock, flags);
1366                if (unit->suspended) {
1367                        spin_unlock_irqrestore(&unit->reg_lock, flags);
1368                        schedule();
1369                        set_current_state(TASK_INTERRUPTIBLE);
1370                        if (signal_pending(current)) {
1371                                if (!ret) ret = -EAGAIN;
1372                                break;
1373                        }
1374                        continue;
1375                }
1376                if (dmabuf->count < 0) {
1377                        printk(KERN_ERR
1378                           "ymf_write: count %d, was legal in cs46xx\n",
1379                            dmabuf->count);
1380                        dmabuf->count = 0;
1381                }
1382                if (dmabuf->count == 0) {
1383                        swptr = dmabuf->hwptr;
1384                        if (state->wpcm.running) {
1385                                /*
1386                                 * Add uncertainty reserve.
1387                                 */
1388                                cnt = ymf_calc_lend(state->format.rate);
1389                                cnt <<= state->format.shift;
1390                                if ((swptr += cnt) >= dmabuf->dmasize) {
1391                                        swptr -= dmabuf->dmasize;
1392                                }
1393                        }
1394                        dmabuf->swptr = swptr;
1395                } else {
1396                        /*
1397                         * XXX This is not right if dmabuf->count is small -
1398                         * about 2*x frame size or less. We cannot count on
1399                         * on appending and not causing an artefact.
1400                         * Should use a variation of the count==0 case above.
1401                         */
1402                        swptr = dmabuf->swptr;
1403                }
1404                cnt = dmabuf->dmasize - swptr;
1405                if (dmabuf->count + cnt > dmabuf->dmasize - redzone)
1406                        cnt = (dmabuf->dmasize - redzone) - dmabuf->count;
1407                spin_unlock_irqrestore(&unit->reg_lock, flags);
1408
1409                if (cnt > count)
1410                        cnt = count;
1411                if (cnt <= 0) {
1412                        YMFDBGW("ymf_write: full, count %d swptr %d\n",
1413                           dmabuf->count, dmabuf->swptr);
1414                        /*
1415                         * buffer is full, start the dma machine and
1416                         * wait for data to be played
1417                         */
1418                        spin_lock_irqsave(&unit->reg_lock, flags);
1419                        if (!state->wpcm.running) {
1420                                ymf_playback_trigger(unit, &state->wpcm, 1);
1421                        }
1422                        spin_unlock_irqrestore(&unit->reg_lock, flags);
1423                        if (file->f_flags & O_NONBLOCK) {
1424                                if (!ret) ret = -EAGAIN;
1425                                break;
1426                        }
1427                        schedule();
1428                        set_current_state(TASK_INTERRUPTIBLE);
1429                        if (signal_pending(current)) {
1430                                if (!ret) ret = -ERESTARTSYS;
1431                                break;
1432                        }
1433                        continue;
1434                }
1435                if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
1436                        if (!ret) ret = -EFAULT;
1437                        break;
1438                }
1439
1440                if ((swptr += cnt) >= dmabuf->dmasize) {
1441                        swptr -= dmabuf->dmasize;
1442                }
1443
1444                spin_lock_irqsave(&unit->reg_lock, flags);
1445                if (unit->suspended) {
1446                        spin_unlock_irqrestore(&unit->reg_lock, flags);
1447                        continue;
1448                }
1449                dmabuf->swptr = swptr;
1450                dmabuf->count += cnt;
1451
1452                /*
1453                 * Start here is a bad idea - may cause startup click
1454                 * in /bin/play when dmabuf is not full yet.
1455                 * However, some broken applications do not make
1456                 * any use of SNDCTL_DSP_SYNC (Doom is the worst).
1457                 * One frame is about 5.3ms, Doom write size is 46ms.
1458                 */
1459                delay = state->format.rate / 20;        /* 50ms */
1460                delay <<= state->format.shift;
1461                if (dmabuf->count >= delay && !state->wpcm.running) {
1462                        ymf_playback_trigger(unit, &state->wpcm, 1);
1463                }
1464
1465                spin_unlock_irqrestore(&unit->reg_lock, flags);
1466
1467                count -= cnt;
1468                buffer += cnt;
1469                ret += cnt;
1470        }
1471
1472        set_current_state(TASK_RUNNING);
1473        remove_wait_queue(&dmabuf->wait, &waita);
1474
1475        YMFDBGW("ymf_write: ret %d dmabuf.count %d\n", ret, dmabuf->count);
1476        return ret;
1477}
1478
1479static unsigned int ymf_poll(struct file *file, struct poll_table_struct *wait)
1480{
1481        struct ymf_state *state = (struct ymf_state *)file->private_data;
1482        struct ymf_dmabuf *dmabuf;
1483        int redzone;
1484        unsigned long flags;
1485        unsigned int mask = 0;
1486
1487        if (file->f_mode & FMODE_WRITE)
1488                poll_wait(file, &state->wpcm.dmabuf.wait, wait);
1489        if (file->f_mode & FMODE_READ)
1490                poll_wait(file, &state->rpcm.dmabuf.wait, wait);
1491
1492        spin_lock_irqsave(&state->unit->reg_lock, flags);
1493        if (file->f_mode & FMODE_READ) {
1494                dmabuf = &state->rpcm.dmabuf;
1495                if (dmabuf->count >= (signed)dmabuf->fragsize)
1496                        mask |= POLLIN | POLLRDNORM;
1497        }
1498        if (file->f_mode & FMODE_WRITE) {
1499                redzone = ymf_calc_lend(state->format.rate);
1500                redzone <<= state->format.shift;
1501                redzone *= 3;
1502
1503                dmabuf = &state->wpcm.dmabuf;
1504                if (dmabuf->mapped) {
1505                        if (dmabuf->count >= (signed)dmabuf->fragsize)
1506                                mask |= POLLOUT | POLLWRNORM;
1507                } else {
1508                        /*
1509                         * Don't select unless a full fragment is available.
1510                         * Otherwise artsd does GETOSPACE, sees 0, and loops.
1511                         */
1512                        if (dmabuf->count + redzone + dmabuf->fragsize
1513                             <= dmabuf->dmasize)
1514                                mask |= POLLOUT | POLLWRNORM;
1515                }
1516        }
1517        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1518
1519        return mask;
1520}
1521
1522static int ymf_mmap(struct file *file, struct vm_area_struct *vma)
1523{
1524        struct ymf_state *state = (struct ymf_state *)file->private_data;
1525        struct ymf_dmabuf *dmabuf = &state->wpcm.dmabuf;
1526        int ret;
1527        unsigned long size;
1528
1529        if (vma->vm_flags & VM_WRITE) {
1530                if ((ret = prog_dmabuf(state, 0)) != 0)
1531                        return ret;
1532        } else if (vma->vm_flags & VM_READ) {
1533                if ((ret = prog_dmabuf(state, 1)) != 0)
1534                        return ret;
1535        } else 
1536                return -EINVAL;
1537
1538        if (vma->vm_pgoff != 0)
1539                return -EINVAL;
1540        size = vma->vm_end - vma->vm_start;
1541        if (size > (PAGE_SIZE << dmabuf->buforder))
1542                return -EINVAL;
1543        if (remap_page_range(vma->vm_start, virt_to_phys(dmabuf->rawbuf),
1544                             size, vma->vm_page_prot))
1545                return -EAGAIN;
1546        dmabuf->mapped = 1;
1547
1548/* P3 */ printk(KERN_INFO "ymfpci: using memory mapped sound, untested!\n");
1549        return 0;
1550}
1551
1552static int ymf_ioctl(struct inode *inode, struct file *file,
1553    unsigned int cmd, unsigned long arg)
1554{
1555        struct ymf_state *state = (struct ymf_state *)file->private_data;
1556        struct ymf_dmabuf *dmabuf;
1557        unsigned long flags;
1558        audio_buf_info abinfo;
1559        count_info cinfo;
1560        int redzone;
1561        int val;
1562
1563        switch (cmd) {
1564        case OSS_GETVERSION:
1565                YMFDBGX("ymf_ioctl: cmd 0x%x(GETVER) arg 0x%lx\n", cmd, arg);
1566                return put_user(SOUND_VERSION, (int *)arg);
1567
1568        case SNDCTL_DSP_RESET:
1569                YMFDBGX("ymf_ioctl: cmd 0x%x(RESET)\n", cmd);
1570                if (file->f_mode & FMODE_WRITE) {
1571                        ymf_wait_dac(state);
1572                        dmabuf = &state->wpcm.dmabuf;
1573                        spin_lock_irqsave(&state->unit->reg_lock, flags);
1574                        dmabuf->ready = 0;
1575                        dmabuf->swptr = dmabuf->hwptr;
1576                        dmabuf->count = dmabuf->total_bytes = 0;
1577                        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1578                }
1579                if (file->f_mode & FMODE_READ) {
1580                        ymf_stop_adc(state);
1581                        dmabuf = &state->rpcm.dmabuf;
1582                        spin_lock_irqsave(&state->unit->reg_lock, flags);
1583                        dmabuf->ready = 0;
1584                        dmabuf->swptr = dmabuf->hwptr;
1585                        dmabuf->count = dmabuf->total_bytes = 0;
1586                        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1587                }
1588                return 0;
1589
1590        case SNDCTL_DSP_SYNC:
1591                YMFDBGX("ymf_ioctl: cmd 0x%x(SYNC)\n", cmd);
1592                if (file->f_mode & FMODE_WRITE) {
1593                        dmabuf = &state->wpcm.dmabuf;
1594                        if (file->f_flags & O_NONBLOCK) {
1595                                spin_lock_irqsave(&state->unit->reg_lock, flags);
1596                                if (dmabuf->count != 0 && !state->wpcm.running) {
1597                                        ymf_start_dac(state);
1598                                }
1599                                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1600                        } else {
1601                                ymf_wait_dac(state);
1602                        }
1603                }
1604                /* XXX What does this do for reading? dmabuf->count=0; ? */
1605                return 0;
1606
1607        case SNDCTL_DSP_SPEED: /* set smaple rate */
1608                if (get_user(val, (int *)arg))
1609                        return -EFAULT;
1610                YMFDBGX("ymf_ioctl: cmd 0x%x(SPEED) sp %d\n", cmd, val);
1611                if (val >= 8000 && val <= 48000) {
1612                        if (file->f_mode & FMODE_WRITE) {
1613                                ymf_wait_dac(state);
1614                                dmabuf = &state->wpcm.dmabuf;
1615                                spin_lock_irqsave(&state->unit->reg_lock, flags);
1616                                dmabuf->ready = 0;
1617                                state->format.rate = val;
1618                                ymf_pcm_update_shift(&state->format);
1619                                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1620                        }
1621                        if (file->f_mode & FMODE_READ) {
1622                                ymf_stop_adc(state);
1623                                dmabuf = &state->rpcm.dmabuf;
1624                                spin_lock_irqsave(&state->unit->reg_lock, flags);
1625                                dmabuf->ready = 0;
1626                                state->format.rate = val;
1627                                ymf_pcm_update_shift(&state->format);
1628                                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1629                        }
1630                }
1631                return put_user(state->format.rate, (int *)arg);
1632
1633        /*
1634         * OSS manual does not mention SNDCTL_DSP_STEREO at all.
1635         * All channels are mono and if you want stereo, you
1636         * play into two channels with SNDCTL_DSP_CHANNELS.
1637         * However, mpg123 calls it. I wonder, why Michael Hipp used it.
1638         */
1639        case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
1640                if (get_user(val, (int *)arg))
1641                        return -EFAULT;
1642                YMFDBGX("ymf_ioctl: cmd 0x%x(STEREO) st %d\n", cmd, val);
1643                if (file->f_mode & FMODE_WRITE) {
1644                        ymf_wait_dac(state); 
1645                        dmabuf = &state->wpcm.dmabuf;
1646                        spin_lock_irqsave(&state->unit->reg_lock, flags);
1647                        dmabuf->ready = 0;
1648                        state->format.voices = val ? 2 : 1;
1649                        ymf_pcm_update_shift(&state->format);
1650                        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1651                }
1652                if (file->f_mode & FMODE_READ) {
1653                        ymf_stop_adc(state);
1654                        dmabuf = &state->rpcm.dmabuf;
1655                        spin_lock_irqsave(&state->unit->reg_lock, flags);
1656                        dmabuf->ready = 0;
1657                        state->format.voices = val ? 2 : 1;
1658                        ymf_pcm_update_shift(&state->format);
1659                        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1660                }
1661                return 0;
1662
1663        case SNDCTL_DSP_GETBLKSIZE:
1664                YMFDBGX("ymf_ioctl: cmd 0x%x(GETBLK)\n", cmd);
1665                if (file->f_mode & FMODE_WRITE) {
1666                        if ((val = prog_dmabuf(state, 0)))
1667                                return val;
1668                        val = state->wpcm.dmabuf.fragsize;
1669                        YMFDBGX("ymf_ioctl: GETBLK w %d\n", val);
1670                        return put_user(val, (int *)arg);
1671                }
1672                if (file->f_mode & FMODE_READ) {
1673                        if ((val = prog_dmabuf(state, 1)))
1674                                return val;
1675                        val = state->rpcm.dmabuf.fragsize;
1676                        YMFDBGX("ymf_ioctl: GETBLK r %d\n", val);
1677                        return put_user(val, (int *)arg);
1678                }
1679                return -EINVAL;
1680
1681        case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
1682                YMFDBGX("ymf_ioctl: cmd 0x%x(GETFMTS)\n", cmd);
1683                return put_user(AFMT_S16_LE|AFMT_U8, (int *)arg);
1684
1685        case SNDCTL_DSP_SETFMT: /* Select sample format */
1686                if (get_user(val, (int *)arg))
1687                        return -EFAULT;
1688                YMFDBGX("ymf_ioctl: cmd 0x%x(SETFMT) fmt %d\n", cmd, val);
1689                if (val == AFMT_S16_LE || val == AFMT_U8) {
1690                        if (file->f_mode & FMODE_WRITE) {
1691                                ymf_wait_dac(state);
1692                                dmabuf = &state->wpcm.dmabuf;
1693                                spin_lock_irqsave(&state->unit->reg_lock, flags);
1694                                dmabuf->ready = 0;
1695                                state->format.format = val;
1696                                ymf_pcm_update_shift(&state->format);
1697                                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1698                        }
1699                        if (file->f_mode & FMODE_READ) {
1700                                ymf_stop_adc(state);
1701                                dmabuf = &state->rpcm.dmabuf;
1702                                spin_lock_irqsave(&state->unit->reg_lock, flags);
1703                                dmabuf->ready = 0;
1704                                state->format.format = val;
1705                                ymf_pcm_update_shift(&state->format);
1706                                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1707                        }
1708                }
1709                return put_user(state->format.format, (int *)arg);
1710
1711        case SNDCTL_DSP_CHANNELS:
1712                if (get_user(val, (int *)arg))
1713                        return -EFAULT;
1714                YMFDBGX("ymf_ioctl: cmd 0x%x(CHAN) ch %d\n", cmd, val);
1715                if (val != 0) {
1716                        if (file->f_mode & FMODE_WRITE) {
1717                                ymf_wait_dac(state);
1718                                if (val == 1 || val == 2) {
1719                                        spin_lock_irqsave(&state->unit->reg_lock, flags);
1720                                        dmabuf = &state->wpcm.dmabuf;
1721                                        dmabuf->ready = 0;
1722                                        state->format.voices = val;
1723                                        ymf_pcm_update_shift(&state->format);
1724                                        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1725                                }
1726                        }
1727                        if (file->f_mode & FMODE_READ) {
1728                                ymf_stop_adc(state);
1729                                if (val == 1 || val == 2) {
1730                                        spin_lock_irqsave(&state->unit->reg_lock, flags);
1731                                        dmabuf = &state->rpcm.dmabuf;
1732                                        dmabuf->ready = 0;
1733                                        state->format.voices = val;
1734                                        ymf_pcm_update_shift(&state->format);
1735                                        spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1736                                }
1737                        }
1738                }
1739                return put_user(state->format.voices, (int *)arg);
1740
1741        case SNDCTL_DSP_POST:
1742                YMFDBGX("ymf_ioctl: cmd 0x%x(POST)\n", cmd);
1743                /*
1744                 * Quoting OSS PG:
1745                 *    The ioctl SNDCTL_DSP_POST is a lightweight version of
1746                 *    SNDCTL_DSP_SYNC. It just tells to the driver that there
1747                 *    is likely to be a pause in the output. This makes it
1748                 *    possible for the device to handle the pause more
1749                 *    intelligently. This ioctl doesn't block the application.
1750                 *
1751                 * The paragraph above is a clumsy way to say "flush ioctl".
1752                 * This ioctl is used by mpg123.
1753                 */
1754                spin_lock_irqsave(&state->unit->reg_lock, flags);
1755                if (state->wpcm.dmabuf.count != 0 && !state->wpcm.running) {
1756                        ymf_start_dac(state);
1757                }
1758                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1759                return 0;
1760
1761        case SNDCTL_DSP_SETFRAGMENT:
1762                if (get_user(val, (int *)arg))
1763                        return -EFAULT;
1764                YMFDBGX("ymf_ioctl: cmd 0x%x(SETFRAG) fr 0x%04x:%04x(%d:%d)\n",
1765                    cmd,
1766                    (val >> 16) & 0xFFFF, val & 0xFFFF,
1767                    (val >> 16) & 0xFFFF, val & 0xFFFF);
1768                dmabuf = &state->wpcm.dmabuf;
1769                dmabuf->ossfragshift = val & 0xffff;
1770                dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
1771                if (dmabuf->ossfragshift < 4)
1772                        dmabuf->ossfragshift = 4;
1773                if (dmabuf->ossfragshift > 15)
1774                        dmabuf->ossfragshift = 15;
1775                return 0;
1776
1777        case SNDCTL_DSP_GETOSPACE:
1778                YMFDBGX("ymf_ioctl: cmd 0x%x(GETOSPACE)\n", cmd);
1779                if (!(file->f_mode & FMODE_WRITE))
1780                        return -EINVAL;
1781                dmabuf = &state->wpcm.dmabuf;
1782                if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
1783                        return val;
1784                redzone = ymf_calc_lend(state->format.rate);
1785                redzone <<= state->format.shift;
1786                redzone *= 3;
1787                spin_lock_irqsave(&state->unit->reg_lock, flags);
1788                abinfo.fragsize = dmabuf->fragsize;
1789                abinfo.bytes = dmabuf->dmasize - dmabuf->count - redzone;
1790                abinfo.fragstotal = dmabuf->numfrag;
1791                abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
1792                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1793                return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1794
1795        case SNDCTL_DSP_GETISPACE:
1796                YMFDBGX("ymf_ioctl: cmd 0x%x(GETISPACE)\n", cmd);
1797                if (!(file->f_mode & FMODE_READ))
1798                        return -EINVAL;
1799                dmabuf = &state->rpcm.dmabuf;
1800                if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
1801                        return val;
1802                spin_lock_irqsave(&state->unit->reg_lock, flags);
1803                abinfo.fragsize = dmabuf->fragsize;
1804                abinfo.bytes = dmabuf->count;
1805                abinfo.fragstotal = dmabuf->numfrag;
1806                abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
1807                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1808                return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1809
1810        case SNDCTL_DSP_NONBLOCK:
1811                YMFDBGX("ymf_ioctl: cmd 0x%x(NONBLOCK)\n", cmd);
1812                file->f_flags |= O_NONBLOCK;
1813                return 0;
1814
1815        case SNDCTL_DSP_GETCAPS:
1816                YMFDBGX("ymf_ioctl: cmd 0x%x(GETCAPS)\n", cmd);
1817                /* return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP,
1818                            (int *)arg); */
1819                return put_user(0, (int *)arg);
1820
1821        case SNDCTL_DSP_GETIPTR:
1822                YMFDBGX("ymf_ioctl: cmd 0x%x(GETIPTR)\n", cmd);
1823                if (!(file->f_mode & FMODE_READ))
1824                        return -EINVAL;
1825                dmabuf = &state->rpcm.dmabuf;
1826                spin_lock_irqsave(&state->unit->reg_lock, flags);
1827                cinfo.bytes = dmabuf->total_bytes;
1828                cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
1829                cinfo.ptr = dmabuf->hwptr;
1830                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1831                YMFDBGX("ymf_ioctl: GETIPTR ptr %d bytes %d\n",
1832                    cinfo.ptr, cinfo.bytes);
1833                return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1834
1835        case SNDCTL_DSP_GETOPTR:
1836                YMFDBGX("ymf_ioctl: cmd 0x%x(GETOPTR)\n", cmd);
1837                if (!(file->f_mode & FMODE_WRITE))
1838                        return -EINVAL;
1839                dmabuf = &state->wpcm.dmabuf;
1840                spin_lock_irqsave(&state->unit->reg_lock, flags);
1841                cinfo.bytes = dmabuf->total_bytes;
1842                cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
1843                cinfo.ptr = dmabuf->hwptr;
1844                spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1845                YMFDBGX("ymf_ioctl: GETOPTR ptr %d bytes %d\n",
1846                    cinfo.ptr, cinfo.bytes);
1847                return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1848
1849        case SNDCTL_DSP_SETDUPLEX:
1850                YMFDBGX("ymf_ioctl: cmd 0x%x(SETDUPLEX)\n", cmd);
1851                return 0;               /* Always duplex */
1852
1853        case SOUND_PCM_READ_RATE:
1854                YMFDBGX("ymf_ioctl: cmd 0x%x(READ_RATE)\n", cmd);
1855                return put_user(state->format.rate, (int *)arg);
1856
1857        case SOUND_PCM_READ_CHANNELS:
1858                YMFDBGX("ymf_ioctl: cmd 0x%x(READ_CH)\n", cmd);
1859                return put_user(state->format.voices, (int *)arg);
1860
1861        case SOUND_PCM_READ_BITS:
1862                YMFDBGX("ymf_ioctl: cmd 0x%x(READ_BITS)\n", cmd);
1863                return put_user(AFMT_S16_LE, (int *)arg);
1864
1865        case SNDCTL_DSP_MAPINBUF:
1866        case SNDCTL_DSP_MAPOUTBUF:
1867        case SNDCTL_DSP_SETSYNCRO:
1868        case SOUND_PCM_WRITE_FILTER:
1869        case SOUND_PCM_READ_FILTER:
1870                YMFDBGX("ymf_ioctl: cmd 0x%x unsupported\n", cmd);
1871                return -ENOTTY;
1872
1873        default:
1874                /*
1875                 * Some programs mix up audio devices and ioctls
1876                 * or perhaps they expect "universal" ioctls,
1877                 * for instance we get SNDCTL_TMR_CONTINUE here.
1878                 * (mpg123 -g 100 ends here too - to be fixed.)
1879                 */
1880                YMFDBGX("ymf_ioctl: cmd 0x%x unknown\n", cmd);
1881                break;
1882        }
1883        return -ENOTTY;
1884}
1885
1886/*
1887 * open(2)
1888 * We use upper part of the minor to distinguish between soundcards.
1889 * Channels are opened with a clone open.
1890 */
1891static int ymf_open(struct inode *inode, struct file *file)
1892{
1893        struct list_head *list;
1894        ymfpci_t *unit = NULL;
1895        int minor;
1896        struct ymf_state *state;
1897        int err;
1898
1899        minor = MINOR(inode->i_rdev);
1900        if ((minor & 0x0F) == 3) {      /* /dev/dspN */
1901                ;
1902        } else {
1903                return -ENXIO;
1904        }
1905
1906        unit = NULL;    /* gcc warns */
1907        list_for_each(list, &ymf_devs) {
1908                unit = list_entry(list, ymfpci_t, ymf_devs);
1909                if (((unit->dev_audio ^ minor) & ~0x0F) == 0)
1910                        break;
1911        }
1912        if (list == &ymf_devs)
1913                return -ENODEV;
1914
1915        down(&unit->open_sem);
1916
1917        if ((state = ymf_state_alloc(unit)) == NULL) {
1918                up(&unit->open_sem);
1919                return -ENOMEM;
1920        }
1921        list_add_tail(&state->chain, &unit->states);
1922
1923        file->private_data = state;
1924
1925        /*
1926         * ymf_read and ymf_write that we borrowed from cs46xx
1927         * allocate buffers with prog_dmabuf(). We call prog_dmabuf
1928         * here so that in case of DMA memory exhaustion open
1929         * fails rather than write.
1930         *
1931         * XXX prog_dmabuf allocates voice. Should allocate explicitly, above.
1932         */
1933        if (file->f_mode & FMODE_WRITE) {
1934                if (!state->wpcm.dmabuf.ready) {
1935                        if ((err = prog_dmabuf(state, 0)) != 0) {
1936                                goto out_nodma;
1937                        }
1938                }
1939        }
1940        if (file->f_mode & FMODE_READ) {
1941                if (!state->rpcm.dmabuf.ready) {
1942                        if ((err = prog_dmabuf(state, 1)) != 0) {
1943                                goto out_nodma;
1944                        }
1945                }
1946        }
1947
1948#if 0 /* test if interrupts work */
1949        ymfpci_writew(unit, YDSXGR_TIMERCOUNT, 0xfffe); /* ~ 680ms */
1950        ymfpci_writeb(unit, YDSXGR_TIMERCTRL,
1951            (YDSXGR_TIMERCTRL_TEN|YDSXGR_TIMERCTRL_TIEN));
1952#endif
1953        up(&unit->open_sem);
1954
1955        return 0;
1956
1957out_nodma:
1958        /*
1959         * XXX Broken custom: "goto out_xxx" in other place is
1960         * a nestable exception, but here it is not nestable due to semaphore.
1961         * XXX Doubtful technique of self-describing objects....
1962         */
1963        dealloc_dmabuf(unit, &state->wpcm.dmabuf);
1964        dealloc_dmabuf(unit, &state->rpcm.dmabuf);
1965        ymf_pcm_free_substream(&state->wpcm);
1966        ymf_pcm_free_substream(&state->rpcm);
1967
1968        list_del(&state->chain);
1969        kfree(state);
1970
1971        up(&unit->open_sem);
1972        return err;
1973}
1974
1975static int ymf_release(struct inode *inode, struct file *file)
1976{
1977        struct ymf_state *state = (struct ymf_state *)file->private_data;
1978        ymfpci_t *unit = state->unit;
1979
1980#if 0 /* test if interrupts work */
1981        ymfpci_writeb(unit, YDSXGR_TIMERCTRL, 0);
1982#endif
1983
1984        down(&unit->open_sem);
1985
1986        /*
1987         * XXX Solve the case of O_NONBLOCK close - don't deallocate here.
1988         * Deallocate when unloading the driver and we can wait.
1989         */
1990        ymf_wait_dac(state);
1991        ymf_stop_adc(state);            /* fortunately, it's immediate */
1992        dealloc_dmabuf(unit, &state->wpcm.dmabuf);
1993        dealloc_dmabuf(unit, &state->rpcm.dmabuf);
1994        ymf_pcm_free_substream(&state->wpcm);
1995        ymf_pcm_free_substream(&state->rpcm);
1996
1997        list_del(&state->chain);
1998        file->private_data = NULL;      /* Can you tell I programmed Solaris */
1999        kfree(state);
2000
2001        up(&unit->open_sem);
2002
2003        return 0;
2004}
2005
2006/*
2007 * Mixer operations are based on cs46xx.
2008 */
2009static int ymf_open_mixdev(struct inode *inode, struct file *file)
2010{
2011        int minor = MINOR(inode->i_rdev);
2012        struct list_head *list;
2013        ymfpci_t *unit;
2014        int i;
2015
2016        list_for_each(list, &ymf_devs) {
2017                unit = list_entry(list, ymfpci_t, ymf_devs);
2018                for (i = 0; i < NR_AC97; i++) {
2019                        if (unit->ac97_codec[i] != NULL &&
2020                            unit->ac97_codec[i]->dev_mixer == minor) {
2021                                goto match;
2022                        }
2023                }
2024        }
2025        return -ENODEV;
2026
2027 match:
2028        file->private_data = unit->ac97_codec[i];
2029
2030        return 0;
2031}
2032
2033static int ymf_ioctl_mixdev(struct inode *inode, struct file *file,
2034    unsigned int cmd, unsigned long arg)
2035{
2036        struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
2037
2038        return codec->mixer_ioctl(codec, cmd, arg);
2039}
2040
2041static int ymf_release_mixdev(struct inode *inode, struct file *file)
2042{
2043        return 0;
2044}
2045
2046static /*const*/ struct file_operations ymf_fops = {
2047        owner:          THIS_MODULE,
2048        llseek:         no_llseek,
2049        read:           ymf_read,
2050        write:          ymf_write,
2051        poll:           ymf_poll,
2052        ioctl:          ymf_ioctl,
2053        mmap:           ymf_mmap,
2054        open:           ymf_open,
2055        release:        ymf_release,
2056};
2057
2058static /*const*/ struct file_operations ymf_mixer_fops = {
2059        owner:          THIS_MODULE,
2060        llseek:         no_llseek,
2061        ioctl:          ymf_ioctl_mixdev,
2062        open:           ymf_open_mixdev,
2063        release:        ymf_release_mixdev,
2064};
2065
2066/*
2067 */
2068
2069static int ymf_suspend(struct pci_dev *pcidev, u32 unused)
2070{
2071        struct ymf_unit *unit = pci_get_drvdata(pcidev);
2072        unsigned long flags;
2073        struct ymf_dmabuf *dmabuf;
2074        struct list_head *p;
2075        struct ymf_state *state;
2076        struct ac97_codec *codec;
2077        int i;
2078
2079        spin_lock_irqsave(&unit->reg_lock, flags);
2080
2081        unit->suspended = 1;
2082
2083        for (i = 0; i < NR_AC97; i++) {
2084                if ((codec = unit->ac97_codec[i]) != NULL)
2085                        ac97_save_state(codec);
2086        }
2087
2088        list_for_each(p, &unit->states) {
2089                state = list_entry(p, struct ymf_state, chain);
2090
2091                dmabuf = &state->wpcm.dmabuf;
2092                dmabuf->hwptr = dmabuf->swptr = 0;
2093                dmabuf->total_bytes = 0;
2094                dmabuf->count = 0;
2095
2096                dmabuf = &state->rpcm.dmabuf;
2097                dmabuf->hwptr = dmabuf->swptr = 0;
2098                dmabuf->total_bytes = 0;
2099                dmabuf->count = 0;
2100        }
2101
2102        ymfpci_writel(unit, YDSXGR_NATIVEDACOUTVOL, 0);
2103        ymfpci_disable_dsp(unit);
2104
2105        spin_unlock_irqrestore(&unit->reg_lock, flags);
2106        
2107        return 0;
2108}
2109
2110static int ymf_resume(struct pci_dev *pcidev)
2111{
2112        struct ymf_unit *unit = pci_get_drvdata(pcidev);
2113        unsigned long flags;
2114        struct list_head *p;
2115        struct ymf_state *state;
2116        struct ac97_codec *codec;
2117        int i;
2118
2119        ymfpci_aclink_reset(unit->pci);
2120        ymfpci_codec_ready(unit, 0);            /* prints diag if not ready. */
2121
2122#ifdef CONFIG_SOUND_YMFPCI_LEGACY
2123        /* XXX At this time the legacy registers are probably deprogrammed. */
2124#endif
2125
2126        ymfpci_download_image(unit);
2127
2128        ymf_memload(unit);
2129
2130        spin_lock_irqsave(&unit->reg_lock, flags);
2131
2132        if (unit->start_count) {
2133                ymfpci_writel(unit, YDSXGR_MODE, 3);
2134                unit->active_bank = ymfpci_readl(unit, YDSXGR_CTRLSELECT) & 1;
2135        }
2136
2137        for (i = 0; i < NR_AC97; i++) {
2138                if ((codec = unit->ac97_codec[i]) != NULL)
2139                        ac97_restore_state(codec);
2140        }
2141
2142        unit->suspended = 0;
2143        list_for_each(p, &unit->states) {
2144                state = list_entry(p, struct ymf_state, chain);
2145                wake_up(&state->wpcm.dmabuf.wait);
2146                wake_up(&state->rpcm.dmabuf.wait);
2147        }
2148
2149        spin_unlock_irqrestore(&unit->reg_lock, flags);
2150        return 0;
2151}
2152
2153/*
2154 *  initialization routines
2155 */
2156
2157#ifdef CONFIG_SOUND_YMFPCI_LEGACY
2158
2159static int ymfpci_setup_legacy(ymfpci_t *unit, struct pci_dev *pcidev)
2160{
2161        int v;
2162        int mpuio = -1, oplio = -1;
2163
2164        switch (unit->iomidi) {
2165        case 0x330:
2166                mpuio = 0;
2167                break;
2168        case 0x300:
2169                mpuio = 1;
2170                break;
2171        case 0x332:
2172                mpuio = 2;
2173                break;
2174        case 0x334:
2175                mpuio = 3;
2176                break;
2177        default: ;
2178        }
2179
2180        switch (unit->iosynth) {
2181        case 0x388:
2182                oplio = 0;
2183                break;
2184        case 0x398:
2185                oplio = 1;
2186                break;
2187        case 0x3a0:
2188                oplio = 2;
2189                break;
2190        case 0x3a8:
2191                oplio = 3;
2192                break;
2193        default: ;
2194        }
2195
2196        if (mpuio >= 0 || oplio >= 0) {
2197                /* 0x0020: 1 - 10 bits of I/O address decoded, 0 - 16 bits. */
2198                v = 0x001e;
2199                pci_write_config_word(pcidev, PCIR_LEGCTRL, v);
2200
2201                switch (pcidev->device) {
2202                case PCI_DEVICE_ID_YAMAHA_724:
2203                case PCI_DEVICE_ID_YAMAHA_740:
2204                case PCI_DEVICE_ID_YAMAHA_724F:
2205                case PCI_DEVICE_ID_YAMAHA_740C:
2206                        v = 0x8800;
2207                        if (mpuio >= 0) { v |= mpuio<<4; }
2208                        if (oplio >= 0) { v |= oplio; }
2209                        pci_write_config_word(pcidev, PCIR_ELEGCTRL, v);
2210                        break;
2211
2212                case PCI_DEVICE_ID_YAMAHA_744:
2213                case PCI_DEVICE_ID_YAMAHA_754:
2214                        v = 0x8800;
2215                        pci_write_config_word(pcidev, PCIR_ELEGCTRL, v);
2216                        if (oplio >= 0) {
2217                                pci_write_config_word(pcidev, PCIR_OPLADR, unit->iosynth);
2218                        }
2219                        if (mpuio >= 0) {
2220                                pci_write_config_word(pcidev, PCIR_MPUADR, unit->iomidi);
2221                        }
2222                        break;
2223
2224                default:
2225                        printk(KERN_ERR "ymfpci: Unknown device ID: 0x%x\n",
2226                            pcidev->device);
2227                        return -EINVAL;
2228                }
2229        }
2230
2231        return 0;
2232}
2233#endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2234
2235static void ymfpci_aclink_reset(struct pci_dev * pci)
2236{
2237        u8 cmd;
2238
2239        /*
2240         * In the 744, 754 only 0x01 exists, 0x02 is undefined.
2241         * It does not seem to hurt to trip both regardless of revision.
2242         */
2243        pci_read_config_byte(pci, PCIR_DSXGCTRL, &cmd);
2244        pci_write_config_byte(pci, PCIR_DSXGCTRL, cmd & 0xfc);
2245        pci_write_config_byte(pci, PCIR_DSXGCTRL, cmd | 0x03);
2246        pci_write_config_byte(pci, PCIR_DSXGCTRL, cmd & 0xfc);
2247
2248        pci_write_config_word(pci, PCIR_DSXPWRCTRL1, 0);
2249        pci_write_config_word(pci, PCIR_DSXPWRCTRL2, 0);
2250}
2251
2252static void ymfpci_enable_dsp(ymfpci_t *codec)
2253{
2254        ymfpci_writel(codec, YDSXGR_CONFIG, 0x00000001);
2255}
2256
2257static void ymfpci_disable_dsp(ymfpci_t *codec)
2258{
2259        u32 val;
2260        int timeout = 1000;
2261
2262        val = ymfpci_readl(codec, YDSXGR_CONFIG);
2263        if (val)
2264                ymfpci_writel(codec, YDSXGR_CONFIG, 0x00000000);
2265        while (timeout-- > 0) {
2266                val = ymfpci_readl(codec, YDSXGR_STATUS);
2267                if ((val & 0x00000002) == 0)
2268                        break;
2269        }
2270}
2271
2272#include "ymfpci_image.h"
2273
2274static void ymfpci_download_image(ymfpci_t *codec)
2275{
2276        int i, ver_1e;
2277        u16 ctrl;
2278
2279        ymfpci_writel(codec, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2280        ymfpci_disable_dsp(codec);
2281        ymfpci_writel(codec, YDSXGR_MODE, 0x00010000);
2282        ymfpci_writel(codec, YDSXGR_MODE, 0x00000000);
2283        ymfpci_writel(codec, YDSXGR_MAPOFREC, 0x00000000);
2284        ymfpci_writel(codec, YDSXGR_MAPOFEFFECT, 0x00000000);
2285        ymfpci_writel(codec, YDSXGR_PLAYCTRLBASE, 0x00000000);
2286        ymfpci_writel(codec, YDSXGR_RECCTRLBASE, 0x00000000);
2287        ymfpci_writel(codec, YDSXGR_EFFCTRLBASE, 0x00000000);
2288        ctrl = ymfpci_readw(codec, YDSXGR_GLOBALCTRL);
2289        ymfpci_writew(codec, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2290
2291        /* setup DSP instruction code */
2292        for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2293                ymfpci_writel(codec, YDSXGR_DSPINSTRAM + (i << 2), DspInst[i]);
2294
2295        switch (codec->pci->device) {
2296        case PCI_DEVICE_ID_YAMAHA_724F:
2297        case PCI_DEVICE_ID_YAMAHA_740C:
2298        case PCI_DEVICE_ID_YAMAHA_744:
2299        case PCI_DEVICE_ID_YAMAHA_754:
2300                ver_1e = 1;
2301                break;
2302        default:
2303                ver_1e = 0;
2304        }
2305
2306        if (ver_1e) {
2307                /* setup control instruction code */
2308                for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2309                        ymfpci_writel(codec, YDSXGR_CTRLINSTRAM + (i << 2), CntrlInst1E[i]);
2310        } else {
2311                for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2312                        ymfpci_writel(codec, YDSXGR_CTRLINSTRAM + (i << 2), CntrlInst[i]);
2313        }
2314
2315        ymfpci_enable_dsp(codec);
2316
2317        /* 0.02s sounds not too bad, we may do schedule_timeout() later. */
2318        mdelay(20); /* seems we need some delay after downloading image.. */
2319}
2320
2321static int ymfpci_memalloc(ymfpci_t *codec)
2322{
2323        unsigned int playback_ctrl_size;
2324        unsigned int bank_size_playback;
2325        unsigned int bank_size_capture;
2326        unsigned int bank_size_effect;
2327        unsigned int size;
2328        unsigned int off;
2329        char *ptr;
2330        dma_addr_t pba;
2331        int voice, bank;
2332
2333        playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2334        bank_size_playback = ymfpci_readl(codec, YDSXGR_PLAYCTRLSIZE) << 2;
2335        bank_size_capture = ymfpci_readl(codec, YDSXGR_RECCTRLSIZE) << 2;
2336        bank_size_effect = ymfpci_readl(codec, YDSXGR_EFFCTRLSIZE) << 2;
2337        codec->work_size = YDSXG_DEFAULT_WORK_SIZE;
2338
2339        size = ((playback_ctrl_size + 0x00ff) & ~0x00ff) +
2340            ((bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES + 0xff) & ~0xff) +
2341            ((bank_size_capture * 2 * YDSXG_CAPTURE_VOICES + 0xff) & ~0xff) +
2342            ((bank_size_effect * 2 * YDSXG_EFFECT_VOICES + 0xff) & ~0xff) +
2343            codec->work_size;
2344
2345        ptr = pci_alloc_consistent(codec->pci, size + 0xff, &pba);
2346        if (ptr == NULL)
2347                return -ENOMEM;
2348        codec->dma_area_va = ptr;
2349        codec->dma_area_ba = pba;
2350        codec->dma_area_size = size + 0xff;
2351
2352        if ((off = ((uint) ptr) & 0xff) != 0) {
2353                ptr += 0x100 - off;
2354                pba += 0x100 - off;
2355        }
2356
2357        /*
2358         * Hardware requires only ptr[playback_ctrl_size] zeroed,
2359         * but in our judgement it is a wrong kind of savings, so clear it all.
2360         */
2361        memset(ptr, 0, size);
2362
2363        codec->ctrl_playback = (u32 *)ptr;
2364        codec->ctrl_playback_ba = pba;
2365        codec->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2366        ptr += (playback_ctrl_size + 0x00ff) & ~0x00ff;
2367        pba += (playback_ctrl_size + 0x00ff) & ~0x00ff;
2368
2369        off = 0;
2370        for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2371                codec->voices[voice].number = voice;
2372                codec->voices[voice].bank =
2373                    (ymfpci_playback_bank_t *) (ptr + off);
2374                codec->voices[voice].bank_ba = pba + off;
2375                off += 2 * bank_size_playback;          /* 2 banks */
2376        }
2377        off = (off + 0xff) & ~0xff;
2378        ptr += off;
2379        pba += off;
2380
2381        off = 0;
2382        codec->bank_base_capture = pba;
2383        for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2384                for (bank = 0; bank < 2; bank++) {
2385                        codec->bank_capture[voice][bank] =
2386                            (ymfpci_capture_bank_t *) (ptr + off);
2387                        off += bank_size_capture;
2388                }
2389        off = (off + 0xff) & ~0xff;
2390        ptr += off;
2391        pba += off;
2392
2393        off = 0;
2394        codec->bank_base_effect = pba;
2395        for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2396                for (bank = 0; bank < 2; bank++) {
2397                        codec->bank_effect[voice][bank] =
2398                            (ymfpci_effect_bank_t *) (ptr + off);
2399                        off += bank_size_effect;
2400                }
2401        off = (off + 0xff) & ~0xff;
2402        ptr += off;
2403        pba += off;
2404
2405        codec->work_base = pba;
2406
2407        return 0;
2408}
2409
2410static void ymfpci_memfree(ymfpci_t *codec)
2411{
2412        ymfpci_writel(codec, YDSXGR_PLAYCTRLBASE, 0);
2413        ymfpci_writel(codec, YDSXGR_RECCTRLBASE, 0);
2414        ymfpci_writel(codec, YDSXGR_EFFCTRLBASE, 0);
2415        ymfpci_writel(codec, YDSXGR_WORKBASE, 0);
2416        ymfpci_writel(codec, YDSXGR_WORKSIZE, 0);
2417        pci_free_consistent(codec->pci,
2418            codec->dma_area_size, codec->dma_area_va, codec->dma_area_ba);
2419}
2420
2421static void ymf_memload(ymfpci_t *unit)
2422{
2423
2424        ymfpci_writel(unit, YDSXGR_PLAYCTRLBASE, unit->ctrl_playback_ba);
2425        ymfpci_writel(unit, YDSXGR_RECCTRLBASE, unit->bank_base_capture);
2426        ymfpci_writel(unit, YDSXGR_EFFCTRLBASE, unit->bank_base_effect);
2427        ymfpci_writel(unit, YDSXGR_WORKBASE, unit->work_base);
2428        ymfpci_writel(unit, YDSXGR_WORKSIZE, unit->work_size >> 2);
2429
2430        /* S/PDIF output initialization */
2431        ymfpci_writew(unit, YDSXGR_SPDIFOUTCTRL, 0);
2432        ymfpci_writew(unit, YDSXGR_SPDIFOUTSTATUS,
2433                SND_PCM_AES0_CON_EMPHASIS_NONE |
2434                (SND_PCM_AES1_CON_ORIGINAL << 8) |
2435                (SND_PCM_AES1_CON_PCM_CODER << 8));
2436
2437        /* S/PDIF input initialization */
2438        ymfpci_writew(unit, YDSXGR_SPDIFINCTRL, 0);
2439
2440        /* move this volume setup to mixer */
2441        ymfpci_writel(unit, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2442        ymfpci_writel(unit, YDSXGR_BUF441OUTVOL, 0);
2443        ymfpci_writel(unit, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2444        ymfpci_writel(unit, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2445}
2446
2447static int ymf_ac97_init(ymfpci_t *unit, int num_ac97)
2448{
2449        struct ac97_codec *codec;
2450        u16 eid;
2451
2452        if ((codec = ac97_alloc_codec()) == NULL)
2453                return -ENOMEM;
2454
2455        /* initialize some basic codec information, other fields will be filled
2456           in ac97_probe_codec */
2457        codec->private_data = unit;
2458        codec->id = num_ac97;
2459
2460        codec->codec_read = ymfpci_codec_read;
2461        codec->codec_write = ymfpci_codec_write;
2462
2463        if (ac97_probe_codec(codec) == 0) {
2464                printk(KERN_ERR "ymfpci: ac97_probe_codec failed\n");
2465                goto out_kfree;
2466        }
2467
2468        eid = ymfpci_codec_read(codec, AC97_EXTENDED_ID);
2469        if (eid==0xFFFF) {
2470                printk(KERN_WARNING "ymfpci: no codec attached ?\n");
2471        }
2472
2473        unit->ac97_features = eid;
2474
2475        if ((codec->dev_mixer = register_sound_mixer(&ymf_mixer_fops, -1)) < 0) {
2476                printk(KERN_ERR "ymfpci: couldn't register mixer!\n");
2477                goto out_kfree;
2478        }
2479
2480        unit->ac97_codec[num_ac97] = codec;
2481
2482        return 0;
2483 out_kfree:
2484        ac97_release_codec(codec);
2485        return -ENODEV;
2486}
2487
2488#ifdef CONFIG_SOUND_YMFPCI_LEGACY
2489# ifdef MODULE
2490static int mpu_io     = 0;
2491static int synth_io   = 0;
2492MODULE_PARM(mpu_io, "i");
2493MODULE_PARM(synth_io, "i");
2494# else
2495static int mpu_io     = 0x330;
2496static int synth_io   = 0x388;
2497# endif
2498static int assigned;
2499#endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2500
2501static int __devinit ymf_probe_one(struct pci_dev *pcidev, const struct pci_device_id *ent)
2502{
2503        u16 ctrl;
2504        unsigned long base;
2505        ymfpci_t *codec;
2506
2507        int err;
2508
2509        if ((err = pci_enable_device(pcidev)) != 0) {
2510                printk(KERN_ERR "ymfpci: pci_enable_device failed\n");
2511                return err;
2512        }
2513        base = pci_resource_start(pcidev, 0);
2514
2515        if ((codec = kmalloc(sizeof(ymfpci_t), GFP_KERNEL)) == NULL) {
2516                printk(KERN_ERR "ymfpci: no core\n");
2517                return -ENOMEM;
2518        }
2519        memset(codec, 0, sizeof(*codec));
2520
2521        spin_lock_init(&codec->reg_lock);
2522        spin_lock_init(&codec->voice_lock);
2523        spin_lock_init(&codec->ac97_lock);
2524        init_MUTEX(&codec->open_sem);
2525        INIT_LIST_HEAD(&codec->states);
2526        codec->pci = pcidev;
2527
2528        pci_read_config_byte(pcidev, PCI_REVISION_ID, &codec->rev);
2529
2530        if (request_mem_region(base, 0x8000, "ymfpci") == NULL) {
2531                printk(KERN_ERR "ymfpci: unable to request mem region\n");
2532                goto out_free;
2533        }
2534
2535        if ((codec->reg_area_virt = ioremap(base, 0x8000)) == NULL) {
2536                printk(KERN_ERR "ymfpci: unable to map registers\n");
2537                goto out_release_region;
2538        }
2539
2540        pci_set_master(pcidev);
2541
2542        printk(KERN_INFO "ymfpci: %s at 0x%lx IRQ %d\n",
2543            (char *)ent->driver_data, base, pcidev->irq);
2544
2545        ymfpci_aclink_reset(pcidev);
2546        if (ymfpci_codec_ready(codec, 0) < 0)
2547                goto out_unmap;
2548
2549#ifdef CONFIG_SOUND_YMFPCI_LEGACY
2550        if (assigned == 0) {
2551                codec->iomidi = mpu_io;
2552                codec->iosynth = synth_io;
2553                if (ymfpci_setup_legacy(codec, pcidev) < 0)
2554                        goto out_unmap;
2555                assigned = 1;
2556        }
2557#endif
2558
2559        ymfpci_download_image(codec);
2560
2561        if (ymfpci_memalloc(codec) < 0)
2562                goto out_disable_dsp;
2563        ymf_memload(codec);
2564
2565        if (request_irq(pcidev->irq, ymf_interrupt, SA_SHIRQ, "ymfpci", codec) != 0) {
2566                printk(KERN_ERR "ymfpci: unable to request IRQ %d\n",
2567                    pcidev->irq);
2568                goto out_memfree;
2569        }
2570
2571        /* register /dev/dsp */
2572        if ((codec->dev_audio = register_sound_dsp(&ymf_fops, -1)) < 0) {
2573                printk(KERN_ERR "ymfpci: unable to register dsp\n");
2574                goto out_free_irq;
2575        }
2576
2577        /*
2578         * Poke just the primary for the moment.
2579         */
2580        if ((err = ymf_ac97_init(codec, 0)) != 0)
2581                goto out_unregister_sound_dsp;
2582
2583#ifdef CONFIG_SOUND_YMFPCI_LEGACY
2584        codec->opl3_data.name = "ymfpci";
2585        codec->mpu_data.name  = "ymfpci";
2586
2587        codec->opl3_data.io_base = codec->iosynth;
2588        codec->opl3_data.irq     = -1;
2589
2590        codec->mpu_data.io_base  = codec->iomidi;
2591        codec->mpu_data.irq      = -1;  /* May be different from our PCI IRQ. */
2592
2593        if (codec->iomidi) {
2594                if (!probe_uart401(&codec->mpu_data, THIS_MODULE)) {
2595                        codec->iomidi = 0;      /* XXX kludge */
2596                }
2597        }
2598#endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2599
2600        /* put it into driver list */
2601        list_add_tail(&codec->ymf_devs, &ymf_devs);
2602        pci_set_drvdata(pcidev, codec);
2603
2604        return 0;
2605
2606 out_unregister_sound_dsp:
2607        unregister_sound_dsp(codec->dev_audio);
2608 out_free_irq:
2609        free_irq(pcidev->irq, codec);
2610 out_memfree:
2611        ymfpci_memfree(codec);
2612 out_disable_dsp:
2613        ymfpci_disable_dsp(codec);
2614        ctrl = ymfpci_readw(codec, YDSXGR_GLOBALCTRL);
2615        ymfpci_writew(codec, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2616        ymfpci_writel(codec, YDSXGR_STATUS, ~0);
2617 out_unmap:
2618        iounmap(codec->reg_area_virt);
2619 out_release_region:
2620        release_mem_region(pci_resource_start(pcidev, 0), 0x8000);
2621 out_free:
2622        kfree(codec);
2623        return -ENODEV;
2624}
2625
2626static void __devexit ymf_remove_one(struct pci_dev *pcidev)
2627{
2628        __u16 ctrl;
2629        ymfpci_t *codec = pci_get_drvdata(pcidev);
2630
2631        /* remove from list of devices */
2632        list_del(&codec->ymf_devs);
2633
2634        unregister_sound_mixer(codec->ac97_codec[0]->dev_mixer);
2635        ac97_release_codec(codec->ac97_codec[0]);
2636        unregister_sound_dsp(codec->dev_audio);
2637        free_irq(pcidev->irq, codec);
2638        ymfpci_memfree(codec);
2639        ymfpci_writel(codec, YDSXGR_STATUS, ~0);
2640        ymfpci_disable_dsp(codec);
2641        ctrl = ymfpci_readw(codec, YDSXGR_GLOBALCTRL);
2642        ymfpci_writew(codec, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2643        iounmap(codec->reg_area_virt);
2644        release_mem_region(pci_resource_start(pcidev, 0), 0x8000);
2645#ifdef CONFIG_SOUND_YMFPCI_LEGACY
2646        if (codec->iomidi) {
2647                unload_uart401(&codec->mpu_data);
2648        }
2649#endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2650        kfree(codec);
2651}
2652
2653MODULE_AUTHOR("Jaroslav Kysela");
2654MODULE_DESCRIPTION("Yamaha YMF7xx PCI Audio");
2655MODULE_LICENSE("GPL");
2656
2657static struct pci_driver ymfpci_driver = {
2658        name:           "ymfpci",
2659        id_table:       ymf_id_tbl,
2660        probe:          ymf_probe_one,
2661        remove:         __devexit_p(ymf_remove_one),
2662        suspend:        ymf_suspend,
2663        resume:         ymf_resume
2664};
2665
2666static int __init ymf_init_module(void)
2667{
2668        return pci_module_init(&ymfpci_driver);
2669}
2670
2671static void __exit ymf_cleanup_module (void)
2672{
2673        pci_unregister_driver(&ymfpci_driver);
2674}
2675
2676module_init(ymf_init_module);
2677module_exit(ymf_cleanup_module);
2678