linux/sound/pci/emu10k1/emupcm.c
<<
>>
Prefs
   1/*
   2 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
   3 *                   Creative Labs, Inc.
   4 *  Routines for control of EMU10K1 chips / PCM routines
   5 *  Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
   6 *
   7 *  BUGS:
   8 *    --
   9 *
  10 *  TODO:
  11 *    --
  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  26 *
  27 */
  28
  29#include <linux/pci.h>
  30#include <linux/delay.h>
  31#include <linux/slab.h>
  32#include <linux/time.h>
  33#include <linux/init.h>
  34#include <sound/core.h>
  35#include <sound/emu10k1.h>
  36
  37static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
  38                                      struct snd_emu10k1_voice *voice)
  39{
  40        struct snd_emu10k1_pcm *epcm;
  41
  42        if ((epcm = voice->epcm) == NULL)
  43                return;
  44        if (epcm->substream == NULL)
  45                return;
  46#if 0
  47        dev_dbg(emu->card->dev,
  48                "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
  49                        epcm->substream->runtime->hw->pointer(emu, epcm->substream),
  50                        snd_pcm_lib_period_bytes(epcm->substream),
  51                        snd_pcm_lib_buffer_bytes(epcm->substream));
  52#endif
  53        snd_pcm_period_elapsed(epcm->substream);
  54}
  55
  56static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
  57                                              unsigned int status)
  58{
  59#if 0
  60        if (status & IPR_ADCBUFHALFFULL) {
  61                if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
  62                        return;
  63        }
  64#endif
  65        snd_pcm_period_elapsed(emu->pcm_capture_substream);
  66}
  67
  68static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
  69                                              unsigned int status)
  70{
  71#if 0
  72        if (status & IPR_MICBUFHALFFULL) {
  73                if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
  74                        return;
  75        }
  76#endif
  77        snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
  78}
  79
  80static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
  81                                          unsigned int status)
  82{
  83#if 0
  84        if (status & IPR_EFXBUFHALFFULL) {
  85                if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
  86                        return;
  87        }
  88#endif
  89        snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
  90}        
  91
  92static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
  93{
  94        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
  95        struct snd_pcm_runtime *runtime = substream->runtime;
  96        struct snd_emu10k1_pcm *epcm = runtime->private_data;
  97        unsigned int ptr;
  98
  99        if (!epcm->running)
 100                return 0;
 101        ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
 102        ptr += runtime->buffer_size;
 103        ptr -= epcm->ccca_start_addr;
 104        ptr %= runtime->buffer_size;
 105
 106        return ptr;
 107}
 108
 109static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
 110{
 111        int err, i;
 112
 113        if (epcm->voices[1] != NULL && voices < 2) {
 114                snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
 115                epcm->voices[1] = NULL;
 116        }
 117        for (i = 0; i < voices; i++) {
 118                if (epcm->voices[i] == NULL)
 119                        break;
 120        }
 121        if (i == voices)
 122                return 0; /* already allocated */
 123
 124        for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
 125                if (epcm->voices[i]) {
 126                        snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
 127                        epcm->voices[i] = NULL;
 128                }
 129        }
 130        err = snd_emu10k1_voice_alloc(epcm->emu,
 131                                      epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
 132                                      voices,
 133                                      &epcm->voices[0]);
 134        
 135        if (err < 0)
 136                return err;
 137        epcm->voices[0]->epcm = epcm;
 138        if (voices > 1) {
 139                for (i = 1; i < voices; i++) {
 140                        epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
 141                        epcm->voices[i]->epcm = epcm;
 142                }
 143        }
 144        if (epcm->extra == NULL) {
 145                err = snd_emu10k1_voice_alloc(epcm->emu,
 146                                              epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
 147                                              1,
 148                                              &epcm->extra);
 149                if (err < 0) {
 150                        /*
 151                        dev_dbg(emu->card->dev, "pcm_channel_alloc: "
 152                               "failed extra: voices=%d, frame=%d\n",
 153                               voices, frame);
 154                        */
 155                        for (i = 0; i < voices; i++) {
 156                                snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
 157                                epcm->voices[i] = NULL;
 158                        }
 159                        return err;
 160                }
 161                epcm->extra->epcm = epcm;
 162                epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
 163        }
 164        return 0;
 165}
 166
 167static const unsigned int capture_period_sizes[31] = {
 168        384,    448,    512,    640,
 169        384*2,  448*2,  512*2,  640*2,
 170        384*4,  448*4,  512*4,  640*4,
 171        384*8,  448*8,  512*8,  640*8,
 172        384*16, 448*16, 512*16, 640*16,
 173        384*32, 448*32, 512*32, 640*32,
 174        384*64, 448*64, 512*64, 640*64,
 175        384*128,448*128,512*128
 176};
 177
 178static const struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
 179        .count = 31,
 180        .list = capture_period_sizes,
 181        .mask = 0
 182};
 183
 184static const unsigned int capture_rates[8] = {
 185        8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
 186};
 187
 188static const struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
 189        .count = 8,
 190        .list = capture_rates,
 191        .mask = 0
 192};
 193
 194static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
 195{
 196        switch (rate) {
 197        case 8000:      return ADCCR_SAMPLERATE_8;
 198        case 11025:     return ADCCR_SAMPLERATE_11;
 199        case 16000:     return ADCCR_SAMPLERATE_16;
 200        case 22050:     return ADCCR_SAMPLERATE_22;
 201        case 24000:     return ADCCR_SAMPLERATE_24;
 202        case 32000:     return ADCCR_SAMPLERATE_32;
 203        case 44100:     return ADCCR_SAMPLERATE_44;
 204        case 48000:     return ADCCR_SAMPLERATE_48;
 205        default:
 206                        snd_BUG();
 207                        return ADCCR_SAMPLERATE_8;
 208        }
 209}
 210
 211static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
 212{
 213        switch (rate) {
 214        case 8000:      return A_ADCCR_SAMPLERATE_8;
 215        case 11025:     return A_ADCCR_SAMPLERATE_11;
 216        case 12000:     return A_ADCCR_SAMPLERATE_12; /* really supported? */
 217        case 16000:     return ADCCR_SAMPLERATE_16;
 218        case 22050:     return ADCCR_SAMPLERATE_22;
 219        case 24000:     return ADCCR_SAMPLERATE_24;
 220        case 32000:     return ADCCR_SAMPLERATE_32;
 221        case 44100:     return ADCCR_SAMPLERATE_44;
 222        case 48000:     return ADCCR_SAMPLERATE_48;
 223        default:
 224                        snd_BUG();
 225                        return A_ADCCR_SAMPLERATE_8;
 226        }
 227}
 228
 229static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
 230{
 231        unsigned int pitch_target;
 232
 233        pitch_target = (rate << 8) / 375;
 234        pitch_target = (pitch_target >> 1) + (pitch_target & 1);
 235        return pitch_target;
 236}
 237
 238#define PITCH_48000 0x00004000
 239#define PITCH_96000 0x00008000
 240#define PITCH_85000 0x00007155
 241#define PITCH_80726 0x00006ba2
 242#define PITCH_67882 0x00005a82
 243#define PITCH_57081 0x00004c1c
 244
 245static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
 246{
 247        if (pitch_target == PITCH_48000)
 248                return CCCA_INTERPROM_0;
 249        else if (pitch_target < PITCH_48000)
 250                return CCCA_INTERPROM_1;
 251        else if (pitch_target >= PITCH_96000)
 252                return CCCA_INTERPROM_0;
 253        else if (pitch_target >= PITCH_85000)
 254                return CCCA_INTERPROM_6;
 255        else if (pitch_target >= PITCH_80726)
 256                return CCCA_INTERPROM_5;
 257        else if (pitch_target >= PITCH_67882)
 258                return CCCA_INTERPROM_4;
 259        else if (pitch_target >= PITCH_57081)
 260                return CCCA_INTERPROM_3;
 261        else  
 262                return CCCA_INTERPROM_2;
 263}
 264
 265/*
 266 * calculate cache invalidate size 
 267 *
 268 * stereo: channel is stereo
 269 * w_16: using 16bit samples
 270 *
 271 * returns: cache invalidate size in samples
 272 */
 273static inline int emu10k1_ccis(int stereo, int w_16)
 274{
 275        if (w_16) {
 276                return stereo ? 24 : 26;
 277        } else {
 278                return stereo ? 24*2 : 26*2;
 279        }
 280}
 281
 282static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
 283                                       int master, int extra,
 284                                       struct snd_emu10k1_voice *evoice,
 285                                       unsigned int start_addr,
 286                                       unsigned int end_addr,
 287                                       struct snd_emu10k1_pcm_mixer *mix)
 288{
 289        struct snd_pcm_substream *substream = evoice->epcm->substream;
 290        struct snd_pcm_runtime *runtime = substream->runtime;
 291        unsigned int silent_page, tmp;
 292        int voice, stereo, w_16;
 293        unsigned char attn, send_amount[8];
 294        unsigned char send_routing[8];
 295        unsigned long flags;
 296        unsigned int pitch_target;
 297        unsigned int ccis;
 298
 299        voice = evoice->number;
 300        stereo = runtime->channels == 2;
 301        w_16 = snd_pcm_format_width(runtime->format) == 16;
 302
 303        if (!extra && stereo) {
 304                start_addr >>= 1;
 305                end_addr >>= 1;
 306        }
 307        if (w_16) {
 308                start_addr >>= 1;
 309                end_addr >>= 1;
 310        }
 311
 312        spin_lock_irqsave(&emu->reg_lock, flags);
 313
 314        /* volume parameters */
 315        if (extra) {
 316                attn = 0;
 317                memset(send_routing, 0, sizeof(send_routing));
 318                send_routing[0] = 0;
 319                send_routing[1] = 1;
 320                send_routing[2] = 2;
 321                send_routing[3] = 3;
 322                memset(send_amount, 0, sizeof(send_amount));
 323        } else {
 324                /* mono, left, right (master voice = left) */
 325                tmp = stereo ? (master ? 1 : 2) : 0;
 326                memcpy(send_routing, &mix->send_routing[tmp][0], 8);
 327                memcpy(send_amount, &mix->send_volume[tmp][0], 8);
 328        }
 329
 330        ccis = emu10k1_ccis(stereo, w_16);
 331        
 332        if (master) {
 333                evoice->epcm->ccca_start_addr = start_addr + ccis;
 334                if (extra) {
 335                        start_addr += ccis;
 336                        end_addr += ccis + emu->delay_pcm_irq;
 337                }
 338                if (stereo && !extra) {
 339                        snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
 340                        snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
 341                } else {
 342                        snd_emu10k1_ptr_write(emu, CPF, voice, 0);
 343                }
 344        }
 345
 346        /* setup routing */
 347        if (emu->audigy) {
 348                snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
 349                                      snd_emu10k1_compose_audigy_fxrt1(send_routing));
 350                snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
 351                                      snd_emu10k1_compose_audigy_fxrt2(send_routing));
 352                snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
 353                                      ((unsigned int)send_amount[4] << 24) |
 354                                      ((unsigned int)send_amount[5] << 16) |
 355                                      ((unsigned int)send_amount[6] << 8) |
 356                                      (unsigned int)send_amount[7]);
 357        } else
 358                snd_emu10k1_ptr_write(emu, FXRT, voice,
 359                                      snd_emu10k1_compose_send_routing(send_routing));
 360        /* Stop CA */
 361        /* Assumption that PT is already 0 so no harm overwriting */
 362        snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
 363        snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
 364        snd_emu10k1_ptr_write(emu, PSST, voice,
 365                        (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
 366                        (send_amount[2] << 24));
 367        if (emu->card_capabilities->emu_model)
 368                pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
 369        else 
 370                pitch_target = emu10k1_calc_pitch_target(runtime->rate);
 371        if (extra)
 372                snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
 373                              emu10k1_select_interprom(pitch_target) |
 374                              (w_16 ? 0 : CCCA_8BITSELECT));
 375        else
 376                snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
 377                              emu10k1_select_interprom(pitch_target) |
 378                              (w_16 ? 0 : CCCA_8BITSELECT));
 379        /* Clear filter delay memory */
 380        snd_emu10k1_ptr_write(emu, Z1, voice, 0);
 381        snd_emu10k1_ptr_write(emu, Z2, voice, 0);
 382        /* invalidate maps */
 383        silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) | (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
 384        snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
 385        snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
 386        /* modulation envelope */
 387        snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
 388        snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
 389        snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
 390        snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
 391        snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
 392        snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
 393        snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
 394        snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
 395        snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
 396        snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
 397        /* volume envelope */
 398        snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
 399        snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
 400        /* filter envelope */
 401        snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
 402        /* pitch envelope */
 403        snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
 404
 405        spin_unlock_irqrestore(&emu->reg_lock, flags);
 406}
 407
 408static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
 409                                          struct snd_pcm_hw_params *hw_params)
 410{
 411        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 412        struct snd_pcm_runtime *runtime = substream->runtime;
 413        struct snd_emu10k1_pcm *epcm = runtime->private_data;
 414        size_t alloc_size;
 415        int err;
 416
 417        if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
 418                return err;
 419
 420        alloc_size = params_buffer_bytes(hw_params);
 421        if (emu->iommu_workaround)
 422                alloc_size += EMUPAGESIZE;
 423        err = snd_pcm_lib_malloc_pages(substream, alloc_size);
 424        if (err < 0)
 425                return err;
 426        if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE)
 427                runtime->dma_bytes -= EMUPAGESIZE;
 428        if (err > 0) {  /* change */
 429                int mapped;
 430                if (epcm->memblk != NULL)
 431                        snd_emu10k1_free_pages(emu, epcm->memblk);
 432                epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
 433                epcm->start_addr = 0;
 434                if (! epcm->memblk)
 435                        return -ENOMEM;
 436                mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
 437                if (mapped < 0)
 438                        return -ENOMEM;
 439                epcm->start_addr = mapped << PAGE_SHIFT;
 440        }
 441        return 0;
 442}
 443
 444static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
 445{
 446        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 447        struct snd_pcm_runtime *runtime = substream->runtime;
 448        struct snd_emu10k1_pcm *epcm;
 449
 450        if (runtime->private_data == NULL)
 451                return 0;
 452        epcm = runtime->private_data;
 453        if (epcm->extra) {
 454                snd_emu10k1_voice_free(epcm->emu, epcm->extra);
 455                epcm->extra = NULL;
 456        }
 457        if (epcm->voices[1]) {
 458                snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
 459                epcm->voices[1] = NULL;
 460        }
 461        if (epcm->voices[0]) {
 462                snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
 463                epcm->voices[0] = NULL;
 464        }
 465        if (epcm->memblk) {
 466                snd_emu10k1_free_pages(emu, epcm->memblk);
 467                epcm->memblk = NULL;
 468                epcm->start_addr = 0;
 469        }
 470        snd_pcm_lib_free_pages(substream);
 471        return 0;
 472}
 473
 474static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
 475{
 476        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 477        struct snd_pcm_runtime *runtime = substream->runtime;
 478        struct snd_emu10k1_pcm *epcm;
 479        int i;
 480
 481        if (runtime->private_data == NULL)
 482                return 0;
 483        epcm = runtime->private_data;
 484        if (epcm->extra) {
 485                snd_emu10k1_voice_free(epcm->emu, epcm->extra);
 486                epcm->extra = NULL;
 487        }
 488        for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
 489                if (epcm->voices[i]) {
 490                        snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
 491                        epcm->voices[i] = NULL;
 492                }
 493        }
 494        if (epcm->memblk) {
 495                snd_emu10k1_free_pages(emu, epcm->memblk);
 496                epcm->memblk = NULL;
 497                epcm->start_addr = 0;
 498        }
 499        snd_pcm_lib_free_pages(substream);
 500        return 0;
 501}
 502
 503static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
 504{
 505        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 506        struct snd_pcm_runtime *runtime = substream->runtime;
 507        struct snd_emu10k1_pcm *epcm = runtime->private_data;
 508        unsigned int start_addr, end_addr;
 509
 510        start_addr = epcm->start_addr;
 511        end_addr = snd_pcm_lib_period_bytes(substream);
 512        if (runtime->channels == 2) {
 513                start_addr >>= 1;
 514                end_addr >>= 1;
 515        }
 516        end_addr += start_addr;
 517        snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
 518                                   start_addr, end_addr, NULL);
 519        start_addr = epcm->start_addr;
 520        end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
 521        snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
 522                                   start_addr, end_addr,
 523                                   &emu->pcm_mixer[substream->number]);
 524        if (epcm->voices[1])
 525                snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
 526                                           start_addr, end_addr,
 527                                           &emu->pcm_mixer[substream->number]);
 528        return 0;
 529}
 530
 531static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
 532{
 533        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 534        struct snd_pcm_runtime *runtime = substream->runtime;
 535        struct snd_emu10k1_pcm *epcm = runtime->private_data;
 536        unsigned int start_addr, end_addr;
 537        unsigned int channel_size;
 538        int i;
 539
 540        start_addr = epcm->start_addr;
 541        end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
 542
 543        /*
 544         * the kX driver leaves some space between voices
 545         */
 546        channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
 547
 548        snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
 549                                   start_addr, start_addr + (channel_size / 2), NULL);
 550
 551        /* only difference with the master voice is we use it for the pointer */
 552        snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
 553                                   start_addr, start_addr + channel_size,
 554                                   &emu->efx_pcm_mixer[0]);
 555
 556        start_addr += channel_size;
 557        for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
 558                snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
 559                                           start_addr, start_addr + channel_size,
 560                                           &emu->efx_pcm_mixer[i]);
 561                start_addr += channel_size;
 562        }
 563
 564        return 0;
 565}
 566
 567static const struct snd_pcm_hardware snd_emu10k1_efx_playback =
 568{
 569        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
 570                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
 571                                 SNDRV_PCM_INFO_RESUME |
 572                                 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
 573        .formats =              SNDRV_PCM_FMTBIT_S16_LE,
 574        .rates =                SNDRV_PCM_RATE_48000,
 575        .rate_min =             48000,
 576        .rate_max =             48000,
 577        .channels_min =         NUM_EFX_PLAYBACK,
 578        .channels_max =         NUM_EFX_PLAYBACK,
 579        .buffer_bytes_max =     (64*1024),
 580        .period_bytes_min =     64,
 581        .period_bytes_max =     (64*1024),
 582        .periods_min =          2,
 583        .periods_max =          2,
 584        .fifo_size =            0,
 585};
 586
 587static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
 588                                         struct snd_pcm_hw_params *hw_params)
 589{
 590        return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
 591}
 592
 593static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
 594{
 595        return snd_pcm_lib_free_pages(substream);
 596}
 597
 598static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
 599{
 600        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 601        struct snd_pcm_runtime *runtime = substream->runtime;
 602        struct snd_emu10k1_pcm *epcm = runtime->private_data;
 603        int idx;
 604
 605        /* zeroing the buffer size will stop capture */
 606        snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
 607        switch (epcm->type) {
 608        case CAPTURE_AC97ADC:
 609                snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
 610                break;
 611        case CAPTURE_EFX:
 612                if (emu->audigy) {
 613                        snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
 614                        snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
 615                } else
 616                        snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
 617                break;
 618        default:
 619                break;
 620        }       
 621        snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
 622        epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
 623        epcm->capture_bs_val = 0;
 624        for (idx = 0; idx < 31; idx++) {
 625                if (capture_period_sizes[idx] == epcm->capture_bufsize) {
 626                        epcm->capture_bs_val = idx + 1;
 627                        break;
 628                }
 629        }
 630        if (epcm->capture_bs_val == 0) {
 631                snd_BUG();
 632                epcm->capture_bs_val++;
 633        }
 634        if (epcm->type == CAPTURE_AC97ADC) {
 635                epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
 636                if (runtime->channels > 1)
 637                        epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
 638                epcm->capture_cr_val |= emu->audigy ?
 639                        snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
 640                        snd_emu10k1_capture_rate_reg(runtime->rate);
 641        }
 642        return 0;
 643}
 644
 645static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
 646{
 647        struct snd_pcm_runtime *runtime;
 648        unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
 649
 650        if (evoice == NULL)
 651                return;
 652        runtime = evoice->epcm->substream->runtime;
 653        voice = evoice->number;
 654        stereo = (!extra && runtime->channels == 2);
 655        sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
 656        ccis = emu10k1_ccis(stereo, sample == 0);
 657        /* set cs to 2 * number of cache registers beside the invalidated */
 658        cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
 659        if (cs > 16) cs = 16;
 660        for (i = 0; i < cs; i++) {
 661                snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
 662                if (stereo) {
 663                        snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
 664                }
 665        }
 666        /* reset cache */
 667        snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
 668        snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
 669        if (stereo) {
 670                snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
 671                snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
 672        }
 673        /* fill cache */
 674        snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
 675        if (stereo) {
 676                snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
 677        }
 678}
 679
 680static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
 681                                               int master, int extra,
 682                                               struct snd_emu10k1_pcm_mixer *mix)
 683{
 684        struct snd_pcm_substream *substream;
 685        struct snd_pcm_runtime *runtime;
 686        unsigned int attn, vattn;
 687        unsigned int voice, tmp;
 688
 689        if (evoice == NULL)     /* skip second voice for mono */
 690                return;
 691        substream = evoice->epcm->substream;
 692        runtime = substream->runtime;
 693        voice = evoice->number;
 694
 695        attn = extra ? 0 : 0x00ff;
 696        tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
 697        vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
 698        snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
 699        snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
 700        snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
 701        snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
 702        snd_emu10k1_voice_clear_loop_stop(emu, voice);
 703}       
 704
 705static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
 706{
 707        struct snd_pcm_substream *substream;
 708        struct snd_pcm_runtime *runtime;
 709        unsigned int voice, pitch, pitch_target;
 710
 711        if (evoice == NULL)     /* skip second voice for mono */
 712                return;
 713        substream = evoice->epcm->substream;
 714        runtime = substream->runtime;
 715        voice = evoice->number;
 716
 717        pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
 718        if (emu->card_capabilities->emu_model)
 719                pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
 720        else 
 721                pitch_target = emu10k1_calc_pitch_target(runtime->rate);
 722        snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
 723        if (master || evoice->epcm->type == PLAYBACK_EFX)
 724                snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
 725        snd_emu10k1_ptr_write(emu, IP, voice, pitch);
 726        if (extra)
 727                snd_emu10k1_voice_intr_enable(emu, voice);
 728}
 729
 730static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
 731{
 732        unsigned int voice;
 733
 734        if (evoice == NULL)
 735                return;
 736        voice = evoice->number;
 737        snd_emu10k1_voice_intr_disable(emu, voice);
 738        snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
 739        snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
 740        snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
 741        snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
 742        snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
 743        snd_emu10k1_ptr_write(emu, IP, voice, 0);
 744}
 745
 746static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
 747                struct snd_emu10k1_pcm *epcm,
 748                struct snd_pcm_substream *substream,
 749                struct snd_pcm_runtime *runtime)
 750{
 751        unsigned int ptr, period_pos;
 752
 753        /* try to sychronize the current position for the interrupt
 754           source voice */
 755        period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
 756        period_pos %= runtime->period_size;
 757        ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
 758        ptr &= ~0x00ffffff;
 759        ptr |= epcm->ccca_start_addr + period_pos;
 760        snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
 761}
 762
 763static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
 764                                        int cmd)
 765{
 766        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 767        struct snd_pcm_runtime *runtime = substream->runtime;
 768        struct snd_emu10k1_pcm *epcm = runtime->private_data;
 769        struct snd_emu10k1_pcm_mixer *mix;
 770        int result = 0;
 771
 772        /*
 773        dev_dbg(emu->card->dev,
 774                "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
 775               (int)emu, cmd, substream->ops->pointer(substream))
 776        */
 777        spin_lock(&emu->reg_lock);
 778        switch (cmd) {
 779        case SNDRV_PCM_TRIGGER_START:
 780                snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);     /* do we need this? */
 781                snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
 782                /* follow thru */
 783        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 784        case SNDRV_PCM_TRIGGER_RESUME:
 785                if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
 786                        snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
 787                mix = &emu->pcm_mixer[substream->number];
 788                snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
 789                snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
 790                snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
 791                snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
 792                snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
 793                snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
 794                epcm->running = 1;
 795                break;
 796        case SNDRV_PCM_TRIGGER_STOP:
 797        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 798        case SNDRV_PCM_TRIGGER_SUSPEND:
 799                epcm->running = 0;
 800                snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
 801                snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
 802                snd_emu10k1_playback_stop_voice(emu, epcm->extra);
 803                break;
 804        default:
 805                result = -EINVAL;
 806                break;
 807        }
 808        spin_unlock(&emu->reg_lock);
 809        return result;
 810}
 811
 812static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
 813                                       int cmd)
 814{
 815        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 816        struct snd_pcm_runtime *runtime = substream->runtime;
 817        struct snd_emu10k1_pcm *epcm = runtime->private_data;
 818        int result = 0;
 819
 820        spin_lock(&emu->reg_lock);
 821        switch (cmd) {
 822        case SNDRV_PCM_TRIGGER_START:
 823        case SNDRV_PCM_TRIGGER_RESUME:
 824                /* hmm this should cause full and half full interrupt to be raised? */
 825                outl(epcm->capture_ipr, emu->port + IPR);
 826                snd_emu10k1_intr_enable(emu, epcm->capture_inte);
 827                /*
 828                dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
 829                       epcm->adccr, epcm->adcbs);
 830                */
 831                switch (epcm->type) {
 832                case CAPTURE_AC97ADC:
 833                        snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
 834                        break;
 835                case CAPTURE_EFX:
 836                        if (emu->audigy) {
 837                                snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
 838                                snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
 839                                dev_dbg(emu->card->dev,
 840                                        "cr_val=0x%x, cr_val2=0x%x\n",
 841                                        epcm->capture_cr_val,
 842                                        epcm->capture_cr_val2);
 843                        } else
 844                                snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
 845                        break;
 846                default:        
 847                        break;
 848                }
 849                snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
 850                epcm->running = 1;
 851                epcm->first_ptr = 1;
 852                break;
 853        case SNDRV_PCM_TRIGGER_STOP:
 854        case SNDRV_PCM_TRIGGER_SUSPEND:
 855                epcm->running = 0;
 856                snd_emu10k1_intr_disable(emu, epcm->capture_inte);
 857                outl(epcm->capture_ipr, emu->port + IPR);
 858                snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
 859                switch (epcm->type) {
 860                case CAPTURE_AC97ADC:
 861                        snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
 862                        break;
 863                case CAPTURE_EFX:
 864                        if (emu->audigy) {
 865                                snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
 866                                snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
 867                        } else
 868                                snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
 869                        break;
 870                default:
 871                        break;
 872                }
 873                break;
 874        default:
 875                result = -EINVAL;
 876        }
 877        spin_unlock(&emu->reg_lock);
 878        return result;
 879}
 880
 881static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
 882{
 883        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 884        struct snd_pcm_runtime *runtime = substream->runtime;
 885        struct snd_emu10k1_pcm *epcm = runtime->private_data;
 886        unsigned int ptr;
 887
 888        if (!epcm->running)
 889                return 0;
 890        ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
 891#if 0   /* Perex's code */
 892        ptr += runtime->buffer_size;
 893        ptr -= epcm->ccca_start_addr;
 894        ptr %= runtime->buffer_size;
 895#else   /* EMU10K1 Open Source code from Creative */
 896        if (ptr < epcm->ccca_start_addr)
 897                ptr += runtime->buffer_size - epcm->ccca_start_addr;
 898        else {
 899                ptr -= epcm->ccca_start_addr;
 900                if (ptr >= runtime->buffer_size)
 901                        ptr -= runtime->buffer_size;
 902        }
 903#endif
 904        /*
 905        dev_dbg(emu->card->dev,
 906               "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
 907               (long)ptr, (long)runtime->buffer_size,
 908               (long)runtime->period_size);
 909        */
 910        return ptr;
 911}
 912
 913
 914static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
 915                                        int cmd)
 916{
 917        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 918        struct snd_pcm_runtime *runtime = substream->runtime;
 919        struct snd_emu10k1_pcm *epcm = runtime->private_data;
 920        int i;
 921        int result = 0;
 922
 923        spin_lock(&emu->reg_lock);
 924        switch (cmd) {
 925        case SNDRV_PCM_TRIGGER_START:
 926                /* prepare voices */
 927                for (i = 0; i < NUM_EFX_PLAYBACK; i++) {        
 928                        snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
 929                }
 930                snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
 931
 932                /* follow thru */
 933        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 934        case SNDRV_PCM_TRIGGER_RESUME:
 935                snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
 936                snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
 937                                                   &emu->efx_pcm_mixer[0]);
 938                for (i = 1; i < NUM_EFX_PLAYBACK; i++)
 939                        snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
 940                                                           &emu->efx_pcm_mixer[i]);
 941                snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
 942                snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
 943                for (i = 1; i < NUM_EFX_PLAYBACK; i++)
 944                        snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
 945                epcm->running = 1;
 946                break;
 947        case SNDRV_PCM_TRIGGER_SUSPEND:
 948        case SNDRV_PCM_TRIGGER_STOP:
 949        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 950                epcm->running = 0;
 951                for (i = 0; i < NUM_EFX_PLAYBACK; i++) {        
 952                        snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
 953                }
 954                snd_emu10k1_playback_stop_voice(emu, epcm->extra);
 955                break;
 956        default:
 957                result = -EINVAL;
 958                break;
 959        }
 960        spin_unlock(&emu->reg_lock);
 961        return result;
 962}
 963
 964
 965static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
 966{
 967        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
 968        struct snd_pcm_runtime *runtime = substream->runtime;
 969        struct snd_emu10k1_pcm *epcm = runtime->private_data;
 970        unsigned int ptr;
 971
 972        if (!epcm->running)
 973                return 0;
 974        if (epcm->first_ptr) {
 975                udelay(50);     /* hack, it takes awhile until capture is started */
 976                epcm->first_ptr = 0;
 977        }
 978        ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
 979        return bytes_to_frames(runtime, ptr);
 980}
 981
 982/*
 983 *  Playback support device description
 984 */
 985
 986static const struct snd_pcm_hardware snd_emu10k1_playback =
 987{
 988        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
 989                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
 990                                 SNDRV_PCM_INFO_RESUME |
 991                                 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
 992        .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
 993        .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
 994        .rate_min =             4000,
 995        .rate_max =             96000,
 996        .channels_min =         1,
 997        .channels_max =         2,
 998        .buffer_bytes_max =     (128*1024),
 999        .period_bytes_min =     64,
1000        .period_bytes_max =     (128*1024),
1001        .periods_min =          1,
1002        .periods_max =          1024,
1003        .fifo_size =            0,
1004};
1005
1006/*
1007 *  Capture support device description
1008 */
1009
1010static const struct snd_pcm_hardware snd_emu10k1_capture =
1011{
1012        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1013                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1014                                 SNDRV_PCM_INFO_RESUME |
1015                                 SNDRV_PCM_INFO_MMAP_VALID),
1016        .formats =              SNDRV_PCM_FMTBIT_S16_LE,
1017        .rates =                SNDRV_PCM_RATE_8000_48000,
1018        .rate_min =             8000,
1019        .rate_max =             48000,
1020        .channels_min =         1,
1021        .channels_max =         2,
1022        .buffer_bytes_max =     (64*1024),
1023        .period_bytes_min =     384,
1024        .period_bytes_max =     (64*1024),
1025        .periods_min =          2,
1026        .periods_max =          2,
1027        .fifo_size =            0,
1028};
1029
1030static const struct snd_pcm_hardware snd_emu10k1_capture_efx =
1031{
1032        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1033                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1034                                 SNDRV_PCM_INFO_RESUME |
1035                                 SNDRV_PCM_INFO_MMAP_VALID),
1036        .formats =              SNDRV_PCM_FMTBIT_S16_LE,
1037        .rates =                SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | 
1038                                 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | 
1039                                 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1040        .rate_min =             44100,
1041        .rate_max =             192000,
1042        .channels_min =         8,
1043        .channels_max =         8,
1044        .buffer_bytes_max =     (64*1024),
1045        .period_bytes_min =     384,
1046        .period_bytes_max =     (64*1024),
1047        .periods_min =          2,
1048        .periods_max =          2,
1049        .fifo_size =            0,
1050};
1051
1052/*
1053 *
1054 */
1055
1056static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1057{
1058        struct snd_ctl_elem_id id;
1059
1060        if (! kctl)
1061                return;
1062        if (activate)
1063                kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1064        else
1065                kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1066        snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1067                       SNDRV_CTL_EVENT_MASK_INFO,
1068                       snd_ctl_build_ioff(&id, kctl, idx));
1069}
1070
1071static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1072{
1073        snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1074        snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1075        snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1076}
1077
1078static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1079{
1080        snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1081        snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1082        snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1083}
1084
1085static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1086{
1087        kfree(runtime->private_data);
1088}
1089
1090static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1091{
1092        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1093        struct snd_emu10k1_pcm_mixer *mix;
1094        int i;
1095
1096        for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1097                mix = &emu->efx_pcm_mixer[i];
1098                mix->epcm = NULL;
1099                snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1100        }
1101        return 0;
1102}
1103
1104static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1105{
1106        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1107        struct snd_emu10k1_pcm *epcm;
1108        struct snd_emu10k1_pcm_mixer *mix;
1109        struct snd_pcm_runtime *runtime = substream->runtime;
1110        int i;
1111
1112        epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1113        if (epcm == NULL)
1114                return -ENOMEM;
1115        epcm->emu = emu;
1116        epcm->type = PLAYBACK_EFX;
1117        epcm->substream = substream;
1118        
1119        emu->pcm_playback_efx_substream = substream;
1120
1121        runtime->private_data = epcm;
1122        runtime->private_free = snd_emu10k1_pcm_free_substream;
1123        runtime->hw = snd_emu10k1_efx_playback;
1124        
1125        for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1126                mix = &emu->efx_pcm_mixer[i];
1127                mix->send_routing[0][0] = i;
1128                memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1129                mix->send_volume[0][0] = 255;
1130                mix->attn[0] = 0xffff;
1131                mix->epcm = epcm;
1132                snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1133        }
1134        return 0;
1135}
1136
1137static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1138{
1139        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1140        struct snd_emu10k1_pcm *epcm;
1141        struct snd_emu10k1_pcm_mixer *mix;
1142        struct snd_pcm_runtime *runtime = substream->runtime;
1143        int i, err, sample_rate;
1144
1145        epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1146        if (epcm == NULL)
1147                return -ENOMEM;
1148        epcm->emu = emu;
1149        epcm->type = PLAYBACK_EMUVOICE;
1150        epcm->substream = substream;
1151        runtime->private_data = epcm;
1152        runtime->private_free = snd_emu10k1_pcm_free_substream;
1153        runtime->hw = snd_emu10k1_playback;
1154        if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1155                kfree(epcm);
1156                return err;
1157        }
1158        if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1159                kfree(epcm);
1160                return err;
1161        }
1162        if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
1163                sample_rate = 44100;
1164        else
1165                sample_rate = 48000;
1166        err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1167        if (err < 0) {
1168                kfree(epcm);
1169                return err;
1170        }
1171        mix = &emu->pcm_mixer[substream->number];
1172        for (i = 0; i < 4; i++)
1173                mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1174        memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1175        mix->send_volume[0][0] = mix->send_volume[0][1] =
1176        mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1177        mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1178        mix->epcm = epcm;
1179        snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1180        return 0;
1181}
1182
1183static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1184{
1185        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1186        struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1187
1188        mix->epcm = NULL;
1189        snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1190        return 0;
1191}
1192
1193static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1194{
1195        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1196        struct snd_pcm_runtime *runtime = substream->runtime;
1197        struct snd_emu10k1_pcm *epcm;
1198
1199        epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1200        if (epcm == NULL)
1201                return -ENOMEM;
1202        epcm->emu = emu;
1203        epcm->type = CAPTURE_AC97ADC;
1204        epcm->substream = substream;
1205        epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1206        epcm->capture_inte = INTE_ADCBUFENABLE;
1207        epcm->capture_ba_reg = ADCBA;
1208        epcm->capture_bs_reg = ADCBS;
1209        epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1210        runtime->private_data = epcm;
1211        runtime->private_free = snd_emu10k1_pcm_free_substream;
1212        runtime->hw = snd_emu10k1_capture;
1213        emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1214        emu->pcm_capture_substream = substream;
1215        snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1216        snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1217        return 0;
1218}
1219
1220static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1221{
1222        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1223
1224        emu->capture_interrupt = NULL;
1225        emu->pcm_capture_substream = NULL;
1226        return 0;
1227}
1228
1229static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1230{
1231        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1232        struct snd_emu10k1_pcm *epcm;
1233        struct snd_pcm_runtime *runtime = substream->runtime;
1234
1235        epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1236        if (epcm == NULL)
1237                return -ENOMEM;
1238        epcm->emu = emu;
1239        epcm->type = CAPTURE_AC97MIC;
1240        epcm->substream = substream;
1241        epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1242        epcm->capture_inte = INTE_MICBUFENABLE;
1243        epcm->capture_ba_reg = MICBA;
1244        epcm->capture_bs_reg = MICBS;
1245        epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1246        substream->runtime->private_data = epcm;
1247        substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1248        runtime->hw = snd_emu10k1_capture;
1249        runtime->hw.rates = SNDRV_PCM_RATE_8000;
1250        runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1251        runtime->hw.channels_min = 1;
1252        emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1253        emu->pcm_capture_mic_substream = substream;
1254        snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1255        return 0;
1256}
1257
1258static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1259{
1260        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1261
1262        emu->capture_interrupt = NULL;
1263        emu->pcm_capture_mic_substream = NULL;
1264        return 0;
1265}
1266
1267static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1268{
1269        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1270        struct snd_emu10k1_pcm *epcm;
1271        struct snd_pcm_runtime *runtime = substream->runtime;
1272        int nefx = emu->audigy ? 64 : 32;
1273        int idx;
1274
1275        epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1276        if (epcm == NULL)
1277                return -ENOMEM;
1278        epcm->emu = emu;
1279        epcm->type = CAPTURE_EFX;
1280        epcm->substream = substream;
1281        epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1282        epcm->capture_inte = INTE_EFXBUFENABLE;
1283        epcm->capture_ba_reg = FXBA;
1284        epcm->capture_bs_reg = FXBS;
1285        epcm->capture_idx_reg = FXIDX;
1286        substream->runtime->private_data = epcm;
1287        substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1288        runtime->hw = snd_emu10k1_capture_efx;
1289        runtime->hw.rates = SNDRV_PCM_RATE_48000;
1290        runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1291        spin_lock_irq(&emu->reg_lock);
1292        if (emu->card_capabilities->emu_model) {
1293                /*  Nb. of channels has been increased to 16 */
1294                /* TODO
1295                 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1296                 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1297                 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1298                 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1299                 * rate_min = 44100,
1300                 * rate_max = 192000,
1301                 * channels_min = 16,
1302                 * channels_max = 16,
1303                 * Need to add mixer control to fix sample rate
1304                 *                 
1305                 * There are 32 mono channels of 16bits each.
1306                 * 24bit Audio uses 2x channels over 16bit
1307                 * 96kHz uses 2x channels over 48kHz
1308                 * 192kHz uses 4x channels over 48kHz
1309                 * So, for 48kHz 24bit, one has 16 channels
1310                 * for 96kHz 24bit, one has 8 channels
1311                 * for 192kHz 24bit, one has 4 channels
1312                 *
1313                 */
1314#if 1
1315                switch (emu->emu1010.internal_clock) {
1316                case 0:
1317                        /* For 44.1kHz */
1318                        runtime->hw.rates = SNDRV_PCM_RATE_44100;
1319                        runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1320                        runtime->hw.channels_min =
1321                                runtime->hw.channels_max = 16;
1322                        break;
1323                case 1:
1324                        /* For 48kHz */
1325                        runtime->hw.rates = SNDRV_PCM_RATE_48000;
1326                        runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1327                        runtime->hw.channels_min =
1328                                runtime->hw.channels_max = 16;
1329                        break;
1330                }
1331#endif
1332#if 0
1333                /* For 96kHz */
1334                runtime->hw.rates = SNDRV_PCM_RATE_96000;
1335                runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1336                runtime->hw.channels_min = runtime->hw.channels_max = 4;
1337#endif
1338#if 0
1339                /* For 192kHz */
1340                runtime->hw.rates = SNDRV_PCM_RATE_192000;
1341                runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1342                runtime->hw.channels_min = runtime->hw.channels_max = 2;
1343#endif
1344                runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1345                /* efx_voices_mask[0] is expected to be zero
1346                 * efx_voices_mask[1] is expected to have 32bits set
1347                 */
1348        } else {
1349                runtime->hw.channels_min = runtime->hw.channels_max = 0;
1350                for (idx = 0; idx < nefx; idx++) {
1351                        if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1352                                runtime->hw.channels_min++;
1353                                runtime->hw.channels_max++;
1354                        }
1355                }
1356        }
1357        epcm->capture_cr_val = emu->efx_voices_mask[0];
1358        epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1359        spin_unlock_irq(&emu->reg_lock);
1360        emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1361        emu->pcm_capture_efx_substream = substream;
1362        snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1363        return 0;
1364}
1365
1366static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1367{
1368        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1369
1370        emu->capture_interrupt = NULL;
1371        emu->pcm_capture_efx_substream = NULL;
1372        return 0;
1373}
1374
1375static const struct snd_pcm_ops snd_emu10k1_playback_ops = {
1376        .open =                 snd_emu10k1_playback_open,
1377        .close =                snd_emu10k1_playback_close,
1378        .ioctl =                snd_pcm_lib_ioctl,
1379        .hw_params =            snd_emu10k1_playback_hw_params,
1380        .hw_free =              snd_emu10k1_playback_hw_free,
1381        .prepare =              snd_emu10k1_playback_prepare,
1382        .trigger =              snd_emu10k1_playback_trigger,
1383        .pointer =              snd_emu10k1_playback_pointer,
1384        .page =                 snd_pcm_sgbuf_ops_page,
1385};
1386
1387static const struct snd_pcm_ops snd_emu10k1_capture_ops = {
1388        .open =                 snd_emu10k1_capture_open,
1389        .close =                snd_emu10k1_capture_close,
1390        .ioctl =                snd_pcm_lib_ioctl,
1391        .hw_params =            snd_emu10k1_capture_hw_params,
1392        .hw_free =              snd_emu10k1_capture_hw_free,
1393        .prepare =              snd_emu10k1_capture_prepare,
1394        .trigger =              snd_emu10k1_capture_trigger,
1395        .pointer =              snd_emu10k1_capture_pointer,
1396};
1397
1398/* EFX playback */
1399static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1400        .open =                 snd_emu10k1_efx_playback_open,
1401        .close =                snd_emu10k1_efx_playback_close,
1402        .ioctl =                snd_pcm_lib_ioctl,
1403        .hw_params =            snd_emu10k1_playback_hw_params,
1404        .hw_free =              snd_emu10k1_efx_playback_hw_free,
1405        .prepare =              snd_emu10k1_efx_playback_prepare,
1406        .trigger =              snd_emu10k1_efx_playback_trigger,
1407        .pointer =              snd_emu10k1_efx_playback_pointer,
1408        .page =                 snd_pcm_sgbuf_ops_page,
1409};
1410
1411int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
1412{
1413        struct snd_pcm *pcm;
1414        struct snd_pcm_substream *substream;
1415        int err;
1416
1417        if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1418                return err;
1419
1420        pcm->private_data = emu;
1421
1422        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1423        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1424
1425        pcm->info_flags = 0;
1426        pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1427        strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1428        emu->pcm = pcm;
1429
1430        for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1431                if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1432                        return err;
1433
1434        for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1435                snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1436
1437        return 0;
1438}
1439
1440int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
1441{
1442        struct snd_pcm *pcm;
1443        struct snd_pcm_substream *substream;
1444        int err;
1445
1446        if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1447                return err;
1448
1449        pcm->private_data = emu;
1450
1451        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1452
1453        pcm->info_flags = 0;
1454        pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1455        strcpy(pcm->name, "Multichannel Playback");
1456        emu->pcm_multi = pcm;
1457
1458        for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1459                if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1460                        return err;
1461
1462        return 0;
1463}
1464
1465
1466static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1467        .open =                 snd_emu10k1_capture_mic_open,
1468        .close =                snd_emu10k1_capture_mic_close,
1469        .ioctl =                snd_pcm_lib_ioctl,
1470        .hw_params =            snd_emu10k1_capture_hw_params,
1471        .hw_free =              snd_emu10k1_capture_hw_free,
1472        .prepare =              snd_emu10k1_capture_prepare,
1473        .trigger =              snd_emu10k1_capture_trigger,
1474        .pointer =              snd_emu10k1_capture_pointer,
1475};
1476
1477int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
1478{
1479        struct snd_pcm *pcm;
1480        int err;
1481
1482        if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1483                return err;
1484
1485        pcm->private_data = emu;
1486
1487        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1488
1489        pcm->info_flags = 0;
1490        strcpy(pcm->name, "Mic Capture");
1491        emu->pcm_mic = pcm;
1492
1493        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1494
1495        return 0;
1496}
1497
1498static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1499{
1500        struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1501        int nefx = emu->audigy ? 64 : 32;
1502        uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1503        uinfo->count = nefx;
1504        uinfo->value.integer.min = 0;
1505        uinfo->value.integer.max = 1;
1506        return 0;
1507}
1508
1509static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1510{
1511        struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1512        int nefx = emu->audigy ? 64 : 32;
1513        int idx;
1514        
1515        spin_lock_irq(&emu->reg_lock);
1516        for (idx = 0; idx < nefx; idx++)
1517                ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1518        spin_unlock_irq(&emu->reg_lock);
1519        return 0;
1520}
1521
1522static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1523{
1524        struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1525        unsigned int nval[2], bits;
1526        int nefx = emu->audigy ? 64 : 32;
1527        int nefxb = emu->audigy ? 7 : 6;
1528        int change, idx;
1529        
1530        nval[0] = nval[1] = 0;
1531        for (idx = 0, bits = 0; idx < nefx; idx++)
1532                if (ucontrol->value.integer.value[idx]) {
1533                        nval[idx / 32] |= 1 << (idx % 32);
1534                        bits++;
1535                }
1536                
1537        for (idx = 0; idx < nefxb; idx++)
1538                if (1 << idx == bits)
1539                        break;
1540        
1541        if (idx >= nefxb)
1542                return -EINVAL;
1543
1544        spin_lock_irq(&emu->reg_lock);
1545        change = (nval[0] != emu->efx_voices_mask[0]) ||
1546                (nval[1] != emu->efx_voices_mask[1]);
1547        emu->efx_voices_mask[0] = nval[0];
1548        emu->efx_voices_mask[1] = nval[1];
1549        spin_unlock_irq(&emu->reg_lock);
1550        return change;
1551}
1552
1553static const struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1554        .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1555        .name = "Captured FX8010 Outputs",
1556        .info = snd_emu10k1_pcm_efx_voices_mask_info,
1557        .get = snd_emu10k1_pcm_efx_voices_mask_get,
1558        .put = snd_emu10k1_pcm_efx_voices_mask_put
1559};
1560
1561static const struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1562        .open =                 snd_emu10k1_capture_efx_open,
1563        .close =                snd_emu10k1_capture_efx_close,
1564        .ioctl =                snd_pcm_lib_ioctl,
1565        .hw_params =            snd_emu10k1_capture_hw_params,
1566        .hw_free =              snd_emu10k1_capture_hw_free,
1567        .prepare =              snd_emu10k1_capture_prepare,
1568        .trigger =              snd_emu10k1_capture_trigger,
1569        .pointer =              snd_emu10k1_capture_pointer,
1570};
1571
1572
1573/* EFX playback */
1574
1575#define INITIAL_TRAM_SHIFT     14
1576#define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1577
1578static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1579{
1580        struct snd_pcm_substream *substream = private_data;
1581        snd_pcm_period_elapsed(substream);
1582}
1583
1584static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1585                                                   unsigned short *dst_right,
1586                                                   unsigned short *src,
1587                                                   unsigned int count,
1588                                                   unsigned int tram_shift)
1589{
1590        /*
1591        dev_dbg(emu->card->dev,
1592                "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1593               "src = 0x%p, count = 0x%x\n",
1594               dst_left, dst_right, src, count);
1595        */
1596        if ((tram_shift & 1) == 0) {
1597                while (count--) {
1598                        *dst_left-- = *src++;
1599                        *dst_right-- = *src++;
1600                }
1601        } else {
1602                while (count--) {
1603                        *dst_right-- = *src++;
1604                        *dst_left-- = *src++;
1605                }
1606        }
1607}
1608
1609static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1610                                 struct snd_pcm_indirect *rec, size_t bytes)
1611{
1612        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1613        struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1614        unsigned int tram_size = pcm->buffer_size;
1615        unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1616        unsigned int frames = bytes >> 2, count;
1617        unsigned int tram_pos = pcm->tram_pos;
1618        unsigned int tram_shift = pcm->tram_shift;
1619
1620        while (frames > tram_pos) {
1621                count = tram_pos + 1;
1622                snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1623                                                       (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1624                                                       src, count, tram_shift);
1625                src += count * 2;
1626                frames -= count;
1627                tram_pos = (tram_size / 2) - 1;
1628                tram_shift++;
1629        }
1630        snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1631                                               (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1632                                               src, frames, tram_shift);
1633        tram_pos -= frames;
1634        pcm->tram_pos = tram_pos;
1635        pcm->tram_shift = tram_shift;
1636}
1637
1638static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1639{
1640        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1641        struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1642
1643        return snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec,
1644                                                  fx8010_pb_trans_copy);
1645}
1646
1647static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1648                                                 struct snd_pcm_hw_params *hw_params)
1649{
1650        return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1651}
1652
1653static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1654{
1655        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1656        struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1657        unsigned int i;
1658
1659        for (i = 0; i < pcm->channels; i++)
1660                snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1661        snd_pcm_lib_free_pages(substream);
1662        return 0;
1663}
1664
1665static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1666{
1667        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1668        struct snd_pcm_runtime *runtime = substream->runtime;
1669        struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1670        unsigned int i;
1671        
1672        /*
1673        dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1674               "buffer_size = 0x%x (0x%x)\n",
1675               emu->fx8010.etram_pages, runtime->dma_area,
1676               runtime->buffer_size, runtime->buffer_size << 2);
1677        */
1678        memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1679        pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1680        pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1681        pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1682        pcm->tram_shift = 0;
1683        snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0);     /* reset */
1684        snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);     /* reset */
1685        snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1686        snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0);         /* reset ptr number */
1687        snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1688        snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1689        for (i = 0; i < pcm->channels; i++)
1690                snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1691        return 0;
1692}
1693
1694static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1695{
1696        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1697        struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1698        int result = 0;
1699
1700        spin_lock(&emu->reg_lock);
1701        switch (cmd) {
1702        case SNDRV_PCM_TRIGGER_START:
1703                /* follow thru */
1704        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1705        case SNDRV_PCM_TRIGGER_RESUME:
1706#ifdef EMU10K1_SET_AC3_IEC958
1707        {
1708                int i;
1709                for (i = 0; i < 3; i++) {
1710                        unsigned int bits;
1711                        bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1712                               SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1713                               0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1714                        snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1715                }
1716        }
1717#endif
1718                result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1719                if (result < 0)
1720                        goto __err;
1721                snd_emu10k1_fx8010_playback_transfer(substream);        /* roll the ball */
1722                snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1723                break;
1724        case SNDRV_PCM_TRIGGER_STOP:
1725        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1726        case SNDRV_PCM_TRIGGER_SUSPEND:
1727                snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1728                snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1729                pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1730                pcm->tram_shift = 0;
1731                break;
1732        default:
1733                result = -EINVAL;
1734                break;
1735        }
1736      __err:
1737        spin_unlock(&emu->reg_lock);
1738        return result;
1739}
1740
1741static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1742{
1743        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1744        struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1745        size_t ptr; /* byte pointer */
1746
1747        if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1748                return 0;
1749        ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1750        return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1751}
1752
1753static const struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1754{
1755        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1756                                 SNDRV_PCM_INFO_RESUME |
1757                                 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1758        .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1759        .rates =                SNDRV_PCM_RATE_48000,
1760        .rate_min =             48000,
1761        .rate_max =             48000,
1762        .channels_min =         1,
1763        .channels_max =         1,
1764        .buffer_bytes_max =     (128*1024),
1765        .period_bytes_min =     1024,
1766        .period_bytes_max =     (128*1024),
1767        .periods_min =          2,
1768        .periods_max =          1024,
1769        .fifo_size =            0,
1770};
1771
1772static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1773{
1774        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1775        struct snd_pcm_runtime *runtime = substream->runtime;
1776        struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1777
1778        runtime->hw = snd_emu10k1_fx8010_playback;
1779        runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1780        runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1781        spin_lock_irq(&emu->reg_lock);
1782        if (pcm->valid == 0) {
1783                spin_unlock_irq(&emu->reg_lock);
1784                return -ENODEV;
1785        }
1786        pcm->opened = 1;
1787        spin_unlock_irq(&emu->reg_lock);
1788        return 0;
1789}
1790
1791static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1792{
1793        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1794        struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1795
1796        spin_lock_irq(&emu->reg_lock);
1797        pcm->opened = 0;
1798        spin_unlock_irq(&emu->reg_lock);
1799        return 0;
1800}
1801
1802static const struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1803        .open =                 snd_emu10k1_fx8010_playback_open,
1804        .close =                snd_emu10k1_fx8010_playback_close,
1805        .ioctl =                snd_pcm_lib_ioctl,
1806        .hw_params =            snd_emu10k1_fx8010_playback_hw_params,
1807        .hw_free =              snd_emu10k1_fx8010_playback_hw_free,
1808        .prepare =              snd_emu10k1_fx8010_playback_prepare,
1809        .trigger =              snd_emu10k1_fx8010_playback_trigger,
1810        .pointer =              snd_emu10k1_fx8010_playback_pointer,
1811        .ack =                  snd_emu10k1_fx8010_playback_transfer,
1812};
1813
1814int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device)
1815{
1816        struct snd_pcm *pcm;
1817        struct snd_kcontrol *kctl;
1818        int err;
1819
1820        if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1821                return err;
1822
1823        pcm->private_data = emu;
1824
1825        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1826        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1827
1828        pcm->info_flags = 0;
1829        strcpy(pcm->name, "Multichannel Capture/PT Playback");
1830        emu->pcm_efx = pcm;
1831
1832        /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs 
1833         * to these
1834         */     
1835        
1836        /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1837        if (emu->audigy) {
1838                emu->efx_voices_mask[0] = 0;
1839                if (emu->card_capabilities->emu_model)
1840                        /* Pavel Hofman - 32 voices will be used for
1841                         * capture (write mode) -
1842                         * each bit = corresponding voice
1843                         */
1844                        emu->efx_voices_mask[1] = 0xffffffff;
1845                else
1846                        emu->efx_voices_mask[1] = 0xffff;
1847        } else {
1848                emu->efx_voices_mask[0] = 0xffff0000;
1849                emu->efx_voices_mask[1] = 0;
1850        }
1851        /* For emu1010, the control has to set 32 upper bits (voices)
1852         * out of the 64 bits (voices) to true for the 16-channels capture
1853         * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1854         * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1855         * control can override this register's value.
1856         */
1857        kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1858        if (!kctl)
1859                return -ENOMEM;
1860        kctl->id.device = device;
1861        snd_ctl_add(emu->card, kctl);
1862
1863        snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1864
1865        return 0;
1866}
1867