Ignore:
Timestamp:
Apr 19, 2025, 8:08:37 PM (4 months ago)
Author:
David Azarewicz
Message:

Merge in changes from 6.6-LTS branch.
Fixed additional 25+ problems.

Location:
GPL/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • GPL/trunk

  • GPL/trunk/alsa-kernel/pci/emu10k1/emupcm.c

    r703 r772  
    22/*
    33 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
     4 *                   Lee Revell <rlrevell@joe-job.com>
     5 *                   James Courtier-Dutton <James@superbug.co.uk>
     6 *                   Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
    47 *                   Creative Labs, Inc.
     8 *
    59 *  Routines for control of EMU10K1 chips / PCM routines
    6  *  Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
    7  *
    8  *  BUGS:
    9  *    --
    10  *
    11  *  TODO:
    12  *    --
    1310 */
    1411
     
    7774}       
    7875
    79 static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
    80 {
    81         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
    82         struct snd_pcm_runtime *runtime = substream->runtime;
    83         struct snd_emu10k1_pcm *epcm = runtime->private_data;
    84         unsigned int ptr;
    85 
    86         if (!epcm->running)
    87                 return 0;
    88         ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
    89         ptr += runtime->buffer_size;
    90         ptr -= epcm->ccca_start_addr;
    91         ptr %= runtime->buffer_size;
    92 
    93         return ptr;
    94 }
    95 
    96 static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
    97 {
    98         int err, i;
    99 
    100         if (epcm->voices[1] != NULL && voices < 2) {
    101                 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
    102                 epcm->voices[1] = NULL;
    103         }
    104         for (i = 0; i < voices; i++) {
    105                 if (epcm->voices[i] == NULL)
    106                         break;
    107         }
    108         if (i == voices)
    109                 return 0; /* already allocated */
    110 
    111         for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
     76static void snd_emu10k1_pcm_free_voices(struct snd_emu10k1_pcm *epcm)
     77{
     78        for (unsigned i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
    11279                if (epcm->voices[i]) {
    11380                        snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
     
    11582                }
    11683        }
     84}
     85
     86static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm *epcm,
     87                                         int type, int count, int channels)
     88{
     89        int err;
     90
     91        snd_emu10k1_pcm_free_voices(epcm);
     92
    11793        err = snd_emu10k1_voice_alloc(epcm->emu,
    118                                       epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
    119                                       voices,
    120                                       &epcm->voices[0]);
    121        
     94                                      type, count, channels,
     95                                      epcm, &epcm->voices[0]);
    12296        if (err < 0)
    12397                return err;
    124         epcm->voices[0]->epcm = epcm;
    125         if (voices > 1) {
    126                 for (i = 1; i < voices; i++) {
    127                         epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
    128                         epcm->voices[i]->epcm = epcm;
    129                 }
    130         }
     98
    13199        if (epcm->extra == NULL) {
     100                // The hardware supports only (half-)loop interrupts, so to support an
     101                // arbitrary number of periods per buffer, we use an extra voice with a
     102                // period-sized loop as the interrupt source. Additionally, the interrupt
     103                // timing of the hardware is "suboptimal" and needs some compensation.
    132104                err = snd_emu10k1_voice_alloc(epcm->emu,
    133                                               epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
    134                                               1,
    135                                               &epcm->extra);
     105                                              type + 1, 1, 1,
     106                                              epcm, &epcm->extra);
    136107                if (err < 0) {
    137108                        /*
     
    140111                               voices, frame);
    141112                        */
    142                         for (i = 0; i < voices; i++) {
    143                                 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
    144                                 epcm->voices[i] = NULL;
    145                         }
     113                        snd_emu10k1_pcm_free_voices(epcm);
    146114                        return err;
    147115                }
    148                 epcm->extra->epcm = epcm;
    149116                epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
    150117        }
    151         return 0;
    152 }
    153 
    154 static const unsigned int capture_period_sizes[31] = {
     118
     119        return 0;
     120}
     121
     122// Primes 2-7 and 2^n multiples thereof, up to 16.
     123static const unsigned int efx_capture_channels[] = {
     124        1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16
     125};
     126
     127static const struct snd_pcm_hw_constraint_list hw_constraints_efx_capture_channels = {
     128        .count = ARRAY_SIZE(efx_capture_channels),
     129        .list = efx_capture_channels,
     130        .mask = 0
     131};
     132
     133static const unsigned int capture_buffer_sizes[31] = {
    155134        384,    448,    512,    640,
    156135        384*2,  448*2,  512*2,  640*2,
     
    163142};
    164143
    165 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
     144static const struct snd_pcm_hw_constraint_list hw_constraints_capture_buffer_sizes = {
    166145        .count = 31,
    167         .list = capture_period_sizes,
     146        .list = capture_buffer_sizes,
    168147        .mask = 0
    169148};
     
    196175}
    197176
     177static const unsigned int audigy_capture_rates[9] = {
     178        8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
     179};
     180
     181static const struct snd_pcm_hw_constraint_list hw_constraints_audigy_capture_rates = {
     182        .count = 9,
     183        .list = audigy_capture_rates,
     184        .mask = 0
     185};
     186
    198187static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
    199188{
     
    201190        case 8000:      return A_ADCCR_SAMPLERATE_8;
    202191        case 11025:     return A_ADCCR_SAMPLERATE_11;
    203         case 12000:     return A_ADCCR_SAMPLERATE_12; /* really supported? */
     192        case 12000:     return A_ADCCR_SAMPLERATE_12;
    204193        case 16000:     return ADCCR_SAMPLERATE_16;
    205194        case 22050:     return ADCCR_SAMPLERATE_22;
     
    212201                        return A_ADCCR_SAMPLERATE_8;
    213202        }
     203}
     204
     205static void snd_emu10k1_constrain_capture_rates(struct snd_emu10k1 *emu,
     206                                                struct snd_pcm_runtime *runtime)
     207{
     208        if (emu->card_capabilities->emu_model &&
     209            emu->emu1010.word_clock == 44100) {
     210                // This also sets the rate constraint by deleting SNDRV_PCM_RATE_KNOT
     211                runtime->hw.rates = SNDRV_PCM_RATE_11025 | \
     212                                    SNDRV_PCM_RATE_22050 | \
     213                                    SNDRV_PCM_RATE_44100;
     214                runtime->hw.rate_min = 11025;
     215                runtime->hw.rate_max = 44100;
     216                return;
     217        }
     218        snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
     219                                   emu->audigy ? &hw_constraints_audigy_capture_rates :
     220                                                 &hw_constraints_capture_rates);
     221}
     222
     223static void snd_emu1010_constrain_efx_rate(struct snd_emu10k1 *emu,
     224                                           struct snd_pcm_runtime *runtime)
     225{
     226        int rate;
     227
     228        rate = emu->emu1010.word_clock;
     229        runtime->hw.rate_min = runtime->hw.rate_max = rate;
     230        runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
    214231}
    215232
     
    250267}
    251268
    252 /*
    253  * calculate cache invalidate size
    254  *
    255  * stereo: channel is stereo
    256  * w_16: using 16bit samples
    257  *
    258  * returns: cache invalidate size in samples
    259  */
    260 static inline int emu10k1_ccis(int stereo, int w_16)
    261 {
    262         if (w_16) {
    263                 return stereo ? 24 : 26;
    264         } else {
    265                 return stereo ? 24*2 : 26*2;
    266         }
     269static u16 emu10k1_send_target_from_amount(u8 amount)
     270{
     271        static const u8 shifts[8] = { 4, 4, 5, 6, 7, 8, 9, 10 };
     272        static const u16 offsets[8] = { 0, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000 };
     273        u8 exp;
     274
     275        if (amount == 0xff)
     276                return 0xffff;
     277        exp = amount >> 5;
     278        return ((amount & 0x1f) << shifts[exp]) + offsets[exp];
    267279}
    268280
    269281static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
    270                                        int master, int extra,
    271282                                       struct snd_emu10k1_voice *evoice,
     283                                       bool w_16, bool stereo,
    272284                                       unsigned int start_addr,
    273285                                       unsigned int end_addr,
    274                                        struct snd_emu10k1_pcm_mixer *mix)
    275 {
    276         struct snd_pcm_substream *substream = evoice->epcm->substream;
    277         struct snd_pcm_runtime *runtime = substream->runtime;
    278         unsigned int silent_page, tmp;
    279         int voice, stereo, w_16;
    280         unsigned char send_amount[8];
    281         unsigned char send_routing[8];
    282         unsigned long flags;
    283         unsigned int pitch_target;
    284         unsigned int ccis;
     286                                       const unsigned char *send_routing,
     287                                       const unsigned char *send_amount)
     288{
     289        unsigned int silent_page;
     290        int voice;
    285291
    286292        voice = evoice->number;
    287         stereo = runtime->channels == 2;
    288         w_16 = snd_pcm_format_width(runtime->format) == 16;
    289 
    290         if (!extra && stereo) {
    291                 start_addr >>= 1;
    292                 end_addr >>= 1;
    293         }
    294         if (w_16) {
    295                 start_addr >>= 1;
    296                 end_addr >>= 1;
    297         }
    298 
    299         spin_lock_irqsave(&emu->reg_lock, flags);
    300 
    301         /* volume parameters */
    302         if (extra) {
    303                 memset(send_routing, 0, sizeof(send_routing));
    304                 send_routing[0] = 0;
    305                 send_routing[1] = 1;
    306                 send_routing[2] = 2;
    307                 send_routing[3] = 3;
    308                 memset(send_amount, 0, sizeof(send_amount));
     293
     294        silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) |
     295                      (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
     296        snd_emu10k1_ptr_write_multiple(emu, voice,
     297                // Not really necessary for the slave, but it doesn't hurt
     298                CPF, stereo ? CPF_STEREO_MASK : 0,
     299                // Assumption that PT is already 0 so no harm overwriting
     300                PTRX, (send_amount[0] << 8) | send_amount[1],
     301                // Stereo slaves don't need to have the addresses set, but it doesn't hurt
     302                DSL, end_addr | (send_amount[3] << 24),
     303                PSST, start_addr | (send_amount[2] << 24),
     304                CCCA, emu10k1_select_interprom(evoice->epcm->pitch_target) |
     305                      (w_16 ? 0 : CCCA_8BITSELECT),
     306                // Clear filter delay memory
     307                Z1, 0,
     308                Z2, 0,
     309                // Invalidate maps
     310                MAPA, silent_page,
     311                MAPB, silent_page,
     312                // Disable filter (in conjunction with CCCA_RESONANCE == 0)
     313                VTFT, VTFT_FILTERTARGET_MASK,
     314                CVCF, CVCF_CURRENTFILTER_MASK,
     315                REGLIST_END);
     316        // Setup routing
     317        if (emu->audigy) {
     318                snd_emu10k1_ptr_write_multiple(emu, voice,
     319                        A_FXRT1, snd_emu10k1_compose_audigy_fxrt1(send_routing),
     320                        A_FXRT2, snd_emu10k1_compose_audigy_fxrt2(send_routing),
     321                        A_SENDAMOUNTS, snd_emu10k1_compose_audigy_sendamounts(send_amount),
     322                        REGLIST_END);
     323                for (int i = 0; i < 4; i++) {
     324                        u32 aml = emu10k1_send_target_from_amount(send_amount[2 * i]);
     325                        u32 amh = emu10k1_send_target_from_amount(send_amount[2 * i + 1]);
     326                        snd_emu10k1_ptr_write(emu, A_CSBA + i, voice, (amh << 16) | aml);
     327                }
    309328        } else {
    310                 /* mono, left, right (master voice = left) */
    311                 tmp = stereo ? (master ? 1 : 2) : 0;
    312                 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
    313                 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
    314         }
    315 
    316         ccis = emu10k1_ccis(stereo, w_16);
    317        
    318         if (master) {
    319                 evoice->epcm->ccca_start_addr = start_addr + ccis;
    320                 if (extra) {
    321                         start_addr += ccis;
    322                         end_addr += ccis + emu->delay_pcm_irq;
    323                 }
    324                 if (stereo && !extra) {
    325                         snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
    326                         snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
    327                 } else {
    328                         snd_emu10k1_ptr_write(emu, CPF, voice, 0);
    329                 }
    330         }
    331 
    332         /* setup routing */
    333         if (emu->audigy) {
    334                 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
    335                                       snd_emu10k1_compose_audigy_fxrt1(send_routing));
    336                 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
    337                                       snd_emu10k1_compose_audigy_fxrt2(send_routing));
    338                 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
    339                                       ((unsigned int)send_amount[4] << 24) |
    340                                       ((unsigned int)send_amount[5] << 16) |
    341                                       ((unsigned int)send_amount[6] << 8) |
    342                                       (unsigned int)send_amount[7]);
    343         } else
    344329                snd_emu10k1_ptr_write(emu, FXRT, voice,
    345330                                      snd_emu10k1_compose_send_routing(send_routing));
    346         /* Stop CA */
    347         /* Assumption that PT is already 0 so no harm overwriting */
    348         snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
    349         snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
    350         snd_emu10k1_ptr_write(emu, PSST, voice,
    351                         (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
    352                         (send_amount[2] << 24));
    353         if (emu->card_capabilities->emu_model)
    354                 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
    355         else
    356                 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
    357         if (extra)
    358                 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
    359                               emu10k1_select_interprom(pitch_target) |
    360                               (w_16 ? 0 : CCCA_8BITSELECT));
    361         else
    362                 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
    363                               emu10k1_select_interprom(pitch_target) |
    364                               (w_16 ? 0 : CCCA_8BITSELECT));
    365         /* Clear filter delay memory */
    366         snd_emu10k1_ptr_write(emu, Z1, voice, 0);
    367         snd_emu10k1_ptr_write(emu, Z2, voice, 0);
    368         /* invalidate maps */
    369         silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) | (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
    370         snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
    371         snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
    372         /* modulation envelope */
    373         snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
    374         snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
    375         snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
    376         snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
    377         snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
    378         snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
    379         snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
    380         snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
    381         snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
    382         snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
    383         /* volume envelope */
    384         snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
    385         snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
    386         /* filter envelope */
    387         snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
    388         /* pitch envelope */
    389         snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
    390 
    391         spin_unlock_irqrestore(&emu->reg_lock, flags);
     331        }
     332
     333        emu->voices[voice].dirty = 1;
     334}
     335
     336static void snd_emu10k1_pcm_init_voices(struct snd_emu10k1 *emu,
     337                                        struct snd_emu10k1_voice *evoice,
     338                                        bool w_16, bool stereo,
     339                                        unsigned int start_addr,
     340                                        unsigned int end_addr,
     341                                        struct snd_emu10k1_pcm_mixer *mix)
     342{
     343        spin_lock_irq(&emu->reg_lock);
     344        snd_emu10k1_pcm_init_voice(emu, evoice, w_16, stereo,
     345                                   start_addr, end_addr,
     346                                   &mix->send_routing[stereo][0],
     347                                   &mix->send_volume[stereo][0]);
     348        if (stereo)
     349                snd_emu10k1_pcm_init_voice(emu, evoice + 1, w_16, true,
     350                                           start_addr, end_addr,
     351                                           &mix->send_routing[2][0],
     352                                           &mix->send_volume[2][0]);
     353        spin_unlock_irq(&emu->reg_lock);
     354}
     355
     356static void snd_emu10k1_pcm_init_extra_voice(struct snd_emu10k1 *emu,
     357                                             struct snd_emu10k1_voice *evoice,
     358                                             bool w_16,
     359                                             unsigned int start_addr,
     360                                             unsigned int end_addr)
     361{
     362        static const unsigned char send_routing[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
     363        static const unsigned char send_amount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
     364
     365        snd_emu10k1_pcm_init_voice(emu, evoice, w_16, false,
     366                                   start_addr, end_addr,
     367                                   send_routing, send_amount);
    392368}
    393369
     
    399375        struct snd_emu10k1_pcm *epcm = runtime->private_data;
    400376        size_t alloc_size;
     377        int type, channels, count;
    401378        int err;
    402379
    403         err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params));
     380        if (epcm->type == PLAYBACK_EMUVOICE) {
     381                type = EMU10K1_PCM;
     382                channels = 1;
     383                count = params_channels(hw_params);
     384        } else {
     385                type = EMU10K1_EFX;
     386                channels = params_channels(hw_params);
     387                count = 1;
     388        }
     389        err = snd_emu10k1_pcm_channel_alloc(epcm, type, count, channels);
    404390        if (err < 0)
    405391                return err;
     
    442428                epcm->extra = NULL;
    443429        }
    444         if (epcm->voices[1]) {
    445                 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
    446                 epcm->voices[1] = NULL;
    447         }
    448         if (epcm->voices[0]) {
    449                 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
    450                 epcm->voices[0] = NULL;
    451         }
     430        snd_emu10k1_pcm_free_voices(epcm);
    452431        if (epcm->memblk) {
    453432                snd_emu10k1_free_pages(emu, epcm->memblk);
     
    459438}
    460439
    461 static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
    462 {
    463         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
    464         struct snd_pcm_runtime *runtime = substream->runtime;
    465         struct snd_emu10k1_pcm *epcm;
    466         int i;
    467 
    468         if (runtime->private_data == NULL)
    469                 return 0;
    470         epcm = runtime->private_data;
    471         if (epcm->extra) {
    472                 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
    473                 epcm->extra = NULL;
    474         }
    475         for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
    476                 if (epcm->voices[i]) {
    477                         snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
    478                         epcm->voices[i] = NULL;
    479                 }
    480         }
    481         if (epcm->memblk) {
    482                 snd_emu10k1_free_pages(emu, epcm->memblk);
    483                 epcm->memblk = NULL;
    484                 epcm->start_addr = 0;
    485         }
    486         snd_pcm_lib_free_pages(substream);
    487         return 0;
    488 }
    489 
    490440static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
    491441{
     
    493443        struct snd_pcm_runtime *runtime = substream->runtime;
    494444        struct snd_emu10k1_pcm *epcm = runtime->private_data;
     445        bool w_16 = snd_pcm_format_width(runtime->format) == 16;
     446        bool stereo = runtime->channels == 2;
    495447        unsigned int start_addr, end_addr;
    496 
    497         start_addr = epcm->start_addr;
    498         end_addr = snd_pcm_lib_period_bytes(substream);
    499         if (runtime->channels == 2) {
    500                 start_addr >>= 1;
    501                 end_addr >>= 1;
    502         }
    503         end_addr += start_addr;
    504         snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
    505                                    start_addr, end_addr, NULL);
    506         start_addr = epcm->start_addr;
    507         end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
    508         snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
    509                                    start_addr, end_addr,
    510                                    &emu->pcm_mixer[substream->number]);
    511         if (epcm->voices[1])
    512                 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
    513                                            start_addr, end_addr,
    514                                            &emu->pcm_mixer[substream->number]);
     448        unsigned int rate;
     449
     450        rate = runtime->rate;
     451        if (emu->card_capabilities->emu_model &&
     452            emu->emu1010.word_clock == 44100)
     453                rate = rate * 480 / 441;
     454        epcm->pitch_target = emu10k1_calc_pitch_target(rate);
     455
     456        start_addr = epcm->start_addr >> w_16;
     457        end_addr = start_addr + runtime->period_size;
     458        snd_emu10k1_pcm_init_extra_voice(emu, epcm->extra, w_16,
     459                                         start_addr, end_addr);
     460        start_addr >>= stereo;
     461        epcm->ccca_start_addr = start_addr;
     462        end_addr = start_addr + runtime->buffer_size;
     463        snd_emu10k1_pcm_init_voices(emu, epcm->voices[0], w_16, stereo,
     464                                    start_addr, end_addr,
     465                                    &emu->pcm_mixer[substream->number]);
     466
    515467        return 0;
    516468}
     
    521473        struct snd_pcm_runtime *runtime = substream->runtime;
    522474        struct snd_emu10k1_pcm *epcm = runtime->private_data;
    523         unsigned int start_addr, end_addr;
    524         unsigned int channel_size;
    525         int i;
    526 
    527         start_addr = epcm->start_addr;
    528         end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
    529 
    530         /*
    531          * the kX driver leaves some space between voices
    532          */
    533         channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
    534 
    535         snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
    536                                    start_addr, start_addr + (channel_size / 2), NULL);
    537 
    538         /* only difference with the master voice is we use it for the pointer */
    539         snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
    540                                    start_addr, start_addr + channel_size,
    541                                    &emu->efx_pcm_mixer[0]);
    542 
    543         start_addr += channel_size;
    544         for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
    545                 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
    546                                            start_addr, start_addr + channel_size,
    547                                            &emu->efx_pcm_mixer[i]);
     475        unsigned int start_addr;
     476        unsigned int extra_size, channel_size;
     477        unsigned int i;
     478
     479        epcm->pitch_target = PITCH_48000;
     480
     481        start_addr = epcm->start_addr >> 1;  // 16-bit voices
     482
     483        extra_size = runtime->period_size;
     484        channel_size = runtime->buffer_size;
     485
     486        snd_emu10k1_pcm_init_extra_voice(emu, epcm->extra, true,
     487                                         start_addr, start_addr + extra_size);
     488
     489        epcm->ccca_start_addr = start_addr;
     490        for (i = 0; i < runtime->channels; i++) {
     491                snd_emu10k1_pcm_init_voices(emu, epcm->voices[i], true, false,
     492                                            start_addr, start_addr + channel_size,
     493                                            &emu->efx_pcm_mixer[i]);
    548494                start_addr += channel_size;
    549495        }
     
    562508        .rate_min =             48000,
    563509        .rate_max =             48000,
    564         .channels_min =         NUM_EFX_PLAYBACK,
     510        .channels_min =         1,
    565511        .channels_max =         NUM_EFX_PLAYBACK,
    566         .buffer_bytes_max =     (64*1024),
    567         .period_bytes_min =     64,
    568         .period_bytes_max =     (64*1024),
     512        .buffer_bytes_max =     (128*1024),
     513        .period_bytes_max =     (128*1024),
    569514        .periods_min =          2,
    570         .periods_max =          2,
     515        .periods_max =          1024,
    571516        .fifo_size =            0,
    572517};
     
    586531                break;
    587532        case CAPTURE_EFX:
     533                if (emu->card_capabilities->emu_model) {
     534                        // The upper 32 16-bit capture voices, two for each of the 16 32-bit channels.
     535                        // The lower voices are occupied by A_EXTOUT_*_CAP*.
     536                        epcm->capture_cr_val = 0;
     537                        epcm->capture_cr_val2 = 0xffffffff >> (32 - runtime->channels * 2);
     538                }
    588539                if (emu->audigy) {
    589                         snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
    590                         snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
     540                        snd_emu10k1_ptr_write_multiple(emu, 0,
     541                                A_FXWC1, 0,
     542                                A_FXWC2, 0,
     543                                REGLIST_END);
    591544                } else
    592545                        snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
     
    599552        epcm->capture_bs_val = 0;
    600553        for (idx = 0; idx < 31; idx++) {
    601                 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
     554                if (capture_buffer_sizes[idx] == epcm->capture_bufsize) {
    602555                        epcm->capture_bs_val = idx + 1;
    603556                        break;
     
    609562        }
    610563        if (epcm->type == CAPTURE_AC97ADC) {
     564                unsigned rate = runtime->rate;
     565                if (!(runtime->hw.rates & SNDRV_PCM_RATE_48000))
     566                        rate = rate * 480 / 441;
     567
    611568                epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
    612569                if (runtime->channels > 1)
    613570                        epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
    614571                epcm->capture_cr_val |= emu->audigy ?
    615                         snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
    616                         snd_emu10k1_capture_rate_reg(runtime->rate);
    617         }
    618         return 0;
    619 }
    620 
    621 static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
    622 {
    623         struct snd_pcm_runtime *runtime;
    624         unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
    625 
    626         if (evoice == NULL)
    627                 return;
    628         runtime = evoice->epcm->substream->runtime;
     572                        snd_emu10k1_audigy_capture_rate_reg(rate) :
     573                        snd_emu10k1_capture_rate_reg(rate);
     574        }
     575        return 0;
     576}
     577
     578static void snd_emu10k1_playback_fill_cache(struct snd_emu10k1 *emu,
     579                                            unsigned voice,
     580                                            u32 sample, bool stereo)
     581{
     582        u32 ccr;
     583
     584        // We assume that the cache is resting at this point (i.e.,
     585        // CCR_CACHEINVALIDSIZE is very small).
     586
     587        // Clear leading frames. For simplicitly, this does too much,
     588        // except for 16-bit stereo. And the interpolator will actually
     589        // access them at all only when we're pitch-shifting.
     590        for (int i = 0; i < 3; i++)
     591                snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
     592
     593        // Fill cache
     594        ccr = (64 - 3) << REG_SHIFT(CCR_CACHEINVALIDSIZE);
     595        if (stereo) {
     596                // The engine goes haywire if CCR_READADDRESS is out of sync
     597                snd_emu10k1_ptr_write(emu, CCR, voice + 1, ccr);
     598        }
     599        snd_emu10k1_ptr_write(emu, CCR, voice, ccr);
     600}
     601
     602static void snd_emu10k1_playback_prepare_voices(struct snd_emu10k1 *emu,
     603                                                struct snd_emu10k1_pcm *epcm,
     604                                                bool w_16, bool stereo,
     605                                                int channels)
     606{
     607        struct snd_pcm_substream *substream = epcm->substream;
     608        struct snd_pcm_runtime *runtime = substream->runtime;
     609        unsigned eloop_start = epcm->start_addr >> w_16;
     610        unsigned loop_start = eloop_start >> stereo;
     611        unsigned eloop_size = runtime->period_size;
     612        unsigned loop_size = runtime->buffer_size;
     613        u32 sample = w_16 ? 0 : 0x80808080;
     614
     615        // To make the playback actually start at the 1st frame,
     616        // we need to compensate for two circumstances:
     617        // - The actual position is delayed by the cache size (64 frames)
     618        // - The interpolator is centered around the 4th frame
     619        loop_start += (epcm->resume_pos + 64 - 3) % loop_size;
     620        for (int i = 0; i < channels; i++) {
     621                unsigned voice = epcm->voices[i]->number;
     622                snd_emu10k1_ptr_write(emu, CCCA_CURRADDR, voice, loop_start);
     623                loop_start += loop_size;
     624                snd_emu10k1_playback_fill_cache(emu, voice, sample, stereo);
     625        }
     626
     627        // The interrupt is triggered when CCCA_CURRADDR (CA) wraps around,
     628        // which is ahead of the actual playback position, so the interrupt
     629        // source needs to be delayed.
     630        //
     631        // In principle, this wouldn't need to be the cache's entire size - in
     632        // practice, CCR_CACHEINVALIDSIZE (CIS) > `fetch threshold` has never
     633        // been observed, and assuming 40 _bytes_ should be safe.
     634        //
     635        // The cache fills are somewhat random, which makes it impossible to
     636        // align them with the interrupts. This makes a non-delayed interrupt
     637        // source not practical, as the interrupt handler would have to wait
     638        // for (CA - CIS) >= period_boundary for every channel in the stream.
     639        //
     640        // This is why all other (open) drivers for these chips use timer-based
     641        // interrupts.
     642        //
     643        eloop_start += (epcm->resume_pos + eloop_size - 3) % eloop_size;
     644        snd_emu10k1_ptr_write(emu, CCCA_CURRADDR, epcm->extra->number, eloop_start);
     645
     646        // It takes a moment until the cache fills complete,
     647        // but the unmuting takes long enough for that.
     648}
     649
     650static void snd_emu10k1_playback_commit_volume(struct snd_emu10k1 *emu,
     651                                               struct snd_emu10k1_voice *evoice,
     652                                               unsigned int vattn)
     653{
     654        snd_emu10k1_ptr_write_multiple(emu, evoice->number,
     655                VTFT, vattn | VTFT_FILTERTARGET_MASK,
     656                CVCF, vattn | CVCF_CURRENTFILTER_MASK,
     657                REGLIST_END);
     658}
     659
     660static void snd_emu10k1_playback_unmute_voice(struct snd_emu10k1 *emu,
     661                                              struct snd_emu10k1_voice *evoice,
     662                                              bool stereo, bool master,
     663                                              struct snd_emu10k1_pcm_mixer *mix)
     664{
     665        unsigned int vattn;
     666        unsigned int tmp;
     667
     668        tmp = stereo ? (master ? 1 : 2) : 0;
     669        vattn = mix->attn[tmp] << 16;
     670        snd_emu10k1_playback_commit_volume(emu, evoice, vattn);
     671}       
     672
     673static void snd_emu10k1_playback_unmute_voices(struct snd_emu10k1 *emu,
     674                                               struct snd_emu10k1_voice *evoice,
     675                                               bool stereo,
     676                                               struct snd_emu10k1_pcm_mixer *mix)
     677{
     678        snd_emu10k1_playback_unmute_voice(emu, evoice, stereo, true, mix);
     679        if (stereo)
     680                snd_emu10k1_playback_unmute_voice(emu, evoice + 1, true, false, mix);
     681}
     682
     683static void snd_emu10k1_playback_mute_voice(struct snd_emu10k1 *emu,
     684                                            struct snd_emu10k1_voice *evoice)
     685{
     686        snd_emu10k1_playback_commit_volume(emu, evoice, 0);
     687}
     688
     689static void snd_emu10k1_playback_mute_voices(struct snd_emu10k1 *emu,
     690                                             struct snd_emu10k1_voice *evoice,
     691                                             bool stereo)
     692{
     693        snd_emu10k1_playback_mute_voice(emu, evoice);
     694        if (stereo)
     695                snd_emu10k1_playback_mute_voice(emu, evoice + 1);
     696}
     697
     698static void snd_emu10k1_playback_commit_pitch(struct snd_emu10k1 *emu,
     699                                              u32 voice, u32 pitch_target)
     700{
     701        u32 ptrx = snd_emu10k1_ptr_read(emu, PTRX, voice);
     702        u32 cpf = snd_emu10k1_ptr_read(emu, CPF, voice);
     703        snd_emu10k1_ptr_write_multiple(emu, voice,
     704                PTRX, (ptrx & ~PTRX_PITCHTARGET_MASK) | pitch_target,
     705                CPF, (cpf & ~(CPF_CURRENTPITCH_MASK | CPF_FRACADDRESS_MASK)) | pitch_target,
     706                REGLIST_END);
     707}
     708
     709static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu,
     710                                               struct snd_emu10k1_voice *evoice)
     711{
     712        unsigned int voice;
     713
    629714        voice = evoice->number;
    630         stereo = (!extra && runtime->channels == 2);
    631         sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
    632         ccis = emu10k1_ccis(stereo, sample == 0);
    633         /* set cs to 2 * number of cache registers beside the invalidated */
    634         cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
    635         if (cs > 16) cs = 16;
    636         for (i = 0; i < cs; i++) {
    637                 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
    638                 if (stereo) {
    639                         snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
    640                 }
    641         }
    642         /* reset cache */
    643         snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
    644         snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
    645         if (stereo) {
    646                 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
    647                 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
    648         }
    649         /* fill cache */
    650         snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
    651         if (stereo) {
    652                 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
    653         }
    654 }
    655 
    656 static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
    657                                                int master, int extra,
    658                                                struct snd_emu10k1_pcm_mixer *mix)
    659 {
    660         struct snd_pcm_substream *substream;
    661         struct snd_pcm_runtime *runtime;
    662         unsigned int attn, vattn;
    663         unsigned int voice, tmp;
    664 
    665         if (evoice == NULL)     /* skip second voice for mono */
    666                 return;
    667         substream = evoice->epcm->substream;
    668         runtime = substream->runtime;
     715        snd_emu10k1_playback_commit_pitch(emu, voice, evoice->epcm->pitch_target << 16);
     716}
     717
     718static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu,
     719                                            struct snd_emu10k1_voice *evoice)
     720{
     721        unsigned int voice;
     722
    669723        voice = evoice->number;
    670 
    671         attn = extra ? 0 : 0x00ff;
    672         tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
    673         vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
    674         snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
    675         snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
    676         snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
    677         snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
    678         snd_emu10k1_voice_clear_loop_stop(emu, voice);
    679 }       
    680 
    681 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
    682 {
    683         struct snd_pcm_substream *substream;
    684         struct snd_pcm_runtime *runtime;
    685         unsigned int voice, pitch, pitch_target;
    686 
    687         if (evoice == NULL)     /* skip second voice for mono */
    688                 return;
    689         substream = evoice->epcm->substream;
    690         runtime = substream->runtime;
    691         voice = evoice->number;
    692 
    693         pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
    694         if (emu->card_capabilities->emu_model)
    695                 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
    696         else
    697                 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
    698         snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
    699         if (master || evoice->epcm->type == PLAYBACK_EFX)
    700                 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
    701         snd_emu10k1_ptr_write(emu, IP, voice, pitch);
    702         if (extra)
    703                 snd_emu10k1_voice_intr_enable(emu, voice);
    704 }
    705 
    706 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
    707 {
    708         unsigned int voice;
    709 
    710         if (evoice == NULL)
    711                 return;
    712         voice = evoice->number;
    713         snd_emu10k1_voice_intr_disable(emu, voice);
    714         snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
    715         snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
    716         snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
    717         snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
    718         snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
    719         snd_emu10k1_ptr_write(emu, IP, voice, 0);
    720 }
    721 
    722 static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
    723                 struct snd_emu10k1_pcm *epcm,
    724                 struct snd_pcm_substream *substream,
    725                 struct snd_pcm_runtime *runtime)
    726 {
    727         unsigned int ptr, period_pos;
    728 
    729         /* try to sychronize the current position for the interrupt
    730            source voice */
    731         period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
    732         period_pos %= runtime->period_size;
    733         ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
    734         ptr &= ~0x00ffffff;
    735         ptr |= epcm->ccca_start_addr + period_pos;
    736         snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
     724        snd_emu10k1_playback_commit_pitch(emu, voice, 0);
     725}
     726
     727static void snd_emu10k1_playback_set_running(struct snd_emu10k1 *emu,
     728                                             struct snd_emu10k1_pcm *epcm)
     729{
     730        epcm->running = 1;
     731        snd_emu10k1_voice_intr_enable(emu, epcm->extra->number);
     732}
     733
     734static void snd_emu10k1_playback_set_stopped(struct snd_emu10k1 *emu,
     735                                              struct snd_emu10k1_pcm *epcm)
     736{
     737        snd_emu10k1_voice_intr_disable(emu, epcm->extra->number);
     738        epcm->running = 0;
    737739}
    738740
     
    744746        struct snd_emu10k1_pcm *epcm = runtime->private_data;
    745747        struct snd_emu10k1_pcm_mixer *mix;
     748        bool w_16 = snd_pcm_format_width(runtime->format) == 16;
     749        bool stereo = runtime->channels == 2;
    746750        int result = 0;
    747751
     
    754758        switch (cmd) {
    755759        case SNDRV_PCM_TRIGGER_START:
    756                 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);     /* do we need this? */
    757                 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
     760                snd_emu10k1_playback_prepare_voices(emu, epcm, w_16, stereo, 1);
    758761                fallthrough;
    759762        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
    760763        case SNDRV_PCM_TRIGGER_RESUME:
    761                 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
    762                         snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
    763764                mix = &emu->pcm_mixer[substream->number];
    764                 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
    765                 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
    766                 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
    767                 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
    768                 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
    769                 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
    770                 epcm->running = 1;
     765                snd_emu10k1_playback_unmute_voices(emu, epcm->voices[0], stereo, mix);
     766                snd_emu10k1_playback_set_running(emu, epcm);
     767                snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0]);
     768                snd_emu10k1_playback_trigger_voice(emu, epcm->extra);
    771769                break;
    772770        case SNDRV_PCM_TRIGGER_STOP:
    773771        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
    774772        case SNDRV_PCM_TRIGGER_SUSPEND:
    775                 epcm->running = 0;
    776773                snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
    777                 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
    778774                snd_emu10k1_playback_stop_voice(emu, epcm->extra);
     775                snd_emu10k1_playback_set_stopped(emu, epcm);
     776                snd_emu10k1_playback_mute_voices(emu, epcm->voices[0], stereo);
    779777                break;
    780778        default:
     
    811809                case CAPTURE_EFX:
    812810                        if (emu->audigy) {
    813                                 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
    814                                 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
     811                                snd_emu10k1_ptr_write_multiple(emu, 0,
     812                                        A_FXWC1, epcm->capture_cr_val,
     813                                        A_FXWC2, epcm->capture_cr_val2,
     814                                        REGLIST_END);
    815815                                dev_dbg(emu->card->dev,
    816816                                        "cr_val=0x%x, cr_val2=0x%x\n",
     
    839839                case CAPTURE_EFX:
    840840                        if (emu->audigy) {
    841                                 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
    842                                 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
     841                                snd_emu10k1_ptr_write_multiple(emu, 0,
     842                                        A_FXWC1, 0,
     843                                        A_FXWC2, 0,
     844                                        REGLIST_END);
    843845                        } else
    844846                                snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
     
    860862        struct snd_pcm_runtime *runtime = substream->runtime;
    861863        struct snd_emu10k1_pcm *epcm = runtime->private_data;
    862         unsigned int ptr;
     864        int ptr;
    863865
    864866        if (!epcm->running)
    865867                return 0;
     868
    866869        ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
    867 #if 0   /* Perex's code */
    868         ptr += runtime->buffer_size;
    869870        ptr -= epcm->ccca_start_addr;
    870         ptr %= runtime->buffer_size;
    871 #else   /* EMU10K1 Open Source code from Creative */
    872         if (ptr < epcm->ccca_start_addr)
    873                 ptr += runtime->buffer_size - epcm->ccca_start_addr;
    874         else {
    875                 ptr -= epcm->ccca_start_addr;
    876                 if (ptr >= runtime->buffer_size)
    877                         ptr -= runtime->buffer_size;
    878         }
    879 #endif
     871
     872        // This is the size of the whole cache minus the interpolator read-ahead,
     873        // which leads us to the actual playback position.
     874        //
     875        // The cache is constantly kept mostly filled, so in principle we could
     876        // return a more advanced position representing how far the hardware has
     877        // already read the buffer, and set runtime->delay accordingly. However,
     878        // this would be slightly different for every channel (and remarkably slow
     879        // to obtain), so only a fixed worst-case value would be practical.
     880        //
     881        ptr -= 64 - 3;
     882        if (ptr < 0)
     883                ptr += runtime->buffer_size;
     884
    880885        /*
    881886        dev_dbg(emu->card->dev,
     
    887892}
    888893
     894static u64 snd_emu10k1_efx_playback_voice_mask(struct snd_emu10k1_pcm *epcm,
     895                                               int channels)
     896{
     897        u64 mask = 0;
     898
     899        for (int i = 0; i < channels; i++) {
     900                int voice = epcm->voices[i]->number;
     901                mask |= 1ULL << voice;
     902        }
     903        return mask;
     904}
     905
     906static void snd_emu10k1_efx_playback_freeze_voices(struct snd_emu10k1 *emu,
     907                                                   struct snd_emu10k1_pcm *epcm,
     908                                                   int channels)
     909{
     910        for (int i = 0; i < channels; i++) {
     911                int voice = epcm->voices[i]->number;
     912                snd_emu10k1_ptr_write(emu, CPF_STOP, voice, 1);
     913                snd_emu10k1_playback_commit_pitch(emu, voice, PITCH_48000 << 16);
     914        }
     915}
     916
     917static void snd_emu10k1_efx_playback_unmute_voices(struct snd_emu10k1 *emu,
     918                                                   struct snd_emu10k1_pcm *epcm,
     919                                                   int channels)
     920{
     921        for (int i = 0; i < channels; i++)
     922                snd_emu10k1_playback_unmute_voice(emu, epcm->voices[i], false, true,
     923                                                  &emu->efx_pcm_mixer[i]);
     924}
     925
     926static void snd_emu10k1_efx_playback_stop_voices(struct snd_emu10k1 *emu,
     927                                                 struct snd_emu10k1_pcm *epcm,
     928                                                 int channels)
     929{
     930        for (int i = 0; i < channels; i++)
     931                snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
     932        snd_emu10k1_playback_set_stopped(emu, epcm);
     933
     934        for (int i = 0; i < channels; i++)
     935                snd_emu10k1_playback_mute_voice(emu, epcm->voices[i]);
     936}
    889937
    890938static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
     
    894942        struct snd_pcm_runtime *runtime = substream->runtime;
    895943        struct snd_emu10k1_pcm *epcm = runtime->private_data;
    896         int i;
     944        u64 mask;
    897945        int result = 0;
    898946
     
    900948        switch (cmd) {
    901949        case SNDRV_PCM_TRIGGER_START:
    902                 /* prepare voices */
    903                 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {       
    904                         snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
    905                 }
    906                 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
    907                 fallthrough;
    908950        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
    909951        case SNDRV_PCM_TRIGGER_RESUME:
    910                 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
    911                 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
    912                                                    &emu->efx_pcm_mixer[0]);
    913                 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
    914                         snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
    915                                                            &emu->efx_pcm_mixer[i]);
    916                 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
    917                 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
    918                 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
    919                         snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
    920                 epcm->running = 1;
     952                mask = snd_emu10k1_efx_playback_voice_mask(
     953                                epcm, runtime->channels);
     954                for (int i = 0; i < 10; i++) {
     955                        // Note that the freeze is not interruptible, so we make no
     956                        // effort to reset the bits outside the error handling here.
     957                        snd_emu10k1_voice_set_loop_stop_multiple(emu, mask);
     958                        snd_emu10k1_efx_playback_freeze_voices(
     959                                        emu, epcm, runtime->channels);
     960                        snd_emu10k1_playback_prepare_voices(
     961                                        emu, epcm, true, false, runtime->channels);
     962
     963                        // It might seem to make more sense to unmute the voices only after
     964                        // they have been started, to potentially avoid torturing the speakers
     965                        // if something goes wrong. However, we cannot unmute atomically,
     966                        // which means that we'd get some mild artifacts in the regular case.
     967                        snd_emu10k1_efx_playback_unmute_voices(emu, epcm, runtime->channels);
     968
     969                        snd_emu10k1_playback_set_running(emu, epcm);
     970                        result = snd_emu10k1_voice_clear_loop_stop_multiple_atomic(emu, mask);
     971                        if (result == 0) {
     972                                // The extra voice is allowed to lag a bit
     973                                snd_emu10k1_playback_trigger_voice(emu, epcm->extra);
     974                                goto leave;
     975                        }
     976
     977                        snd_emu10k1_efx_playback_stop_voices(
     978                                        emu, epcm, runtime->channels);
     979
     980                        if (result != -EAGAIN)
     981                                break;
     982                        // The sync start can legitimately fail due to NMIs, etc.
     983                }
     984                snd_emu10k1_voice_clear_loop_stop_multiple(emu, mask);
    921985                break;
    922986        case SNDRV_PCM_TRIGGER_SUSPEND:
    923987        case SNDRV_PCM_TRIGGER_STOP:
    924988        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
    925                 epcm->running = 0;
    926                 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {       
    927                         snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
    928                 }
    929989                snd_emu10k1_playback_stop_voice(emu, epcm->extra);
     990                snd_emu10k1_efx_playback_stop_voices(
     991                                emu, epcm, runtime->channels);
     992
     993                epcm->resume_pos = snd_emu10k1_playback_pointer(substream);
    930994                break;
    931995        default:
     
    933997                break;
    934998        }
     999leave:
    9351000        spin_unlock(&emu->reg_lock);
    9361001        return result;
     
    9721037        .channels_max =         2,
    9731038        .buffer_bytes_max =     (128*1024),
    974         .period_bytes_min =     64,
    9751039        .period_bytes_max =     (128*1024),
    976         .periods_min =          1,
     1040        .periods_min =          2,
    9771041        .periods_max =          1024,
    9781042        .fifo_size =            0,
     
    9901054                                 SNDRV_PCM_INFO_MMAP_VALID),
    9911055        .formats =              SNDRV_PCM_FMTBIT_S16_LE,
    992         .rates =                SNDRV_PCM_RATE_8000_48000,
     1056        .rates =                SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_KNOT,
    9931057        .rate_min =             8000,
    9941058        .rate_max =             48000,
     
    10101074                                 SNDRV_PCM_INFO_MMAP_VALID),
    10111075        .formats =              SNDRV_PCM_FMTBIT_S16_LE,
    1012         .rates =                SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
    1013                                  SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
    1014                                  SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
    1015         .rate_min =             44100,
    1016         .rate_max =             192000,
    1017         .channels_min =         8,
    1018         .channels_max =         8,
     1076        .rates =                SNDRV_PCM_RATE_48000,
     1077        .rate_min =             48000,
     1078        .rate_max =             48000,
     1079        .channels_min =         1,
     1080        .channels_max =         16,
    10191081        .buffer_bytes_max =     (64*1024),
    10201082        .period_bytes_min =     384,
     
    10771139}
    10781140
     1141static int snd_emu10k1_playback_set_constraints(struct snd_pcm_runtime *runtime)
     1142{
     1143        int err;
     1144
     1145        // The buffer size must be a multiple of the period size, to avoid a
     1146        // mismatch between the extra voice and the regular voices.
     1147        err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
     1148        if (err < 0)
     1149                return err;
     1150        // The hardware is typically the cache's size of 64 frames ahead.
     1151        // Leave enough time for actually filling up the buffer.
     1152        err = snd_pcm_hw_constraint_minmax(
     1153                        runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 128, UINT_MAX);
     1154        return err;
     1155}
     1156
    10791157static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
    10801158{
     
    10831161        struct snd_emu10k1_pcm_mixer *mix;
    10841162        struct snd_pcm_runtime *runtime = substream->runtime;
    1085         int i;
     1163        int i, j, err;
    10861164
    10871165        epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
     
    10921170        epcm->substream = substream;
    10931171       
    1094         emu->pcm_playback_efx_substream = substream;
    1095 
    10961172        runtime->private_data = epcm;
    10971173        runtime->private_free = snd_emu10k1_pcm_free_substream;
    10981174        runtime->hw = snd_emu10k1_efx_playback;
    1099        
     1175        if (emu->card_capabilities->emu_model)
     1176                snd_emu1010_constrain_efx_rate(emu, runtime);
     1177        err = snd_emu10k1_playback_set_constraints(runtime);
     1178        if (err < 0) {
     1179                kfree(epcm);
     1180                return err;
     1181        }
     1182
    11001183        for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
    11011184                mix = &emu->efx_pcm_mixer[i];
    1102                 mix->send_routing[0][0] = i;
     1185                for (j = 0; j < 8; j++)
     1186                        mix->send_routing[0][j] = i + j;
    11031187                memset(&mix->send_volume, 0, sizeof(mix->send_volume));
    11041188                mix->send_volume[0][0] = 255;
    1105                 mix->attn[0] = 0xffff;
     1189                mix->attn[0] = 0x8000;
    11061190                mix->epcm = epcm;
    11071191                snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
     
    11271211        runtime->private_free = snd_emu10k1_pcm_free_substream;
    11281212        runtime->hw = snd_emu10k1_playback;
    1129         err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
     1213        err = snd_emu10k1_playback_set_constraints(runtime);
    11301214        if (err < 0) {
    11311215                kfree(epcm);
    11321216                return err;
    11331217        }
    1134         err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
    1135         if (err < 0) {
    1136                 kfree(epcm);
    1137                 return err;
    1138         }
    1139         if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
    1140                 sample_rate = 44100;
     1218        if (emu->card_capabilities->emu_model)
     1219                sample_rate = emu->emu1010.word_clock;
    11411220        else
    11421221                sample_rate = 48000;
     
    11471226        }
    11481227        mix = &emu->pcm_mixer[substream->number];
    1149         for (i = 0; i < 4; i++)
     1228        for (i = 0; i < 8; i++)
    11501229                mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
    11511230        memset(&mix->send_volume, 0, sizeof(mix->send_volume));
    11521231        mix->send_volume[0][0] = mix->send_volume[0][1] =
    11531232        mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
    1154         mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
     1233        mix->attn[0] = mix->attn[1] = mix->attn[2] = 0x8000;
    11551234        mix->epcm = epcm;
    11561235        snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
     
    11881267        runtime->private_free = snd_emu10k1_pcm_free_substream;
    11891268        runtime->hw = snd_emu10k1_capture;
     1269        snd_emu10k1_constrain_capture_rates(emu, runtime);
     1270        snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
     1271                                   &hw_constraints_capture_buffer_sizes);
    11901272        emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
    11911273        emu->pcm_capture_substream = substream;
    1192         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
    1193         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
    11941274        return 0;
    11951275}
     
    12261306        runtime->hw.rates = SNDRV_PCM_RATE_8000;
    12271307        runtime->hw.rate_min = runtime->hw.rate_max = 8000;
    1228         runtime->hw.channels_min = 1;
     1308        snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
     1309                                   &hw_constraints_capture_buffer_sizes);
    12291310        emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
    12301311        emu->pcm_capture_mic_substream = substream;
    1231         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
    12321312        return 0;
    12331313}
     
    12371317        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
    12381318
    1239         emu->capture_interrupt = NULL;
     1319        emu->capture_mic_interrupt = NULL;
    12401320        emu->pcm_capture_mic_substream = NULL;
    12411321        return 0;
     
    12481328        struct snd_pcm_runtime *runtime = substream->runtime;
    12491329        int nefx = emu->audigy ? 64 : 32;
    1250         int idx;
     1330        int idx, err;
    12511331
    12521332        epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
     
    12641344        substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
    12651345        runtime->hw = snd_emu10k1_capture_efx;
    1266         runtime->hw.rates = SNDRV_PCM_RATE_48000;
    1267         runtime->hw.rate_min = runtime->hw.rate_max = 48000;
    1268         spin_lock_irq(&emu->reg_lock);
    12691346        if (emu->card_capabilities->emu_model) {
    1270                 /*  Nb. of channels has been increased to 16 */
    1271                 /* TODO
    1272                  * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
    1273                  * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
    1274                  * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
    1275                  * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
    1276                  * rate_min = 44100,
    1277                  * rate_max = 192000,
    1278                  * channels_min = 16,
    1279                  * channels_max = 16,
    1280                  * Need to add mixer control to fix sample rate
    1281                  *                 
     1347                snd_emu1010_constrain_efx_rate(emu, runtime);
     1348                /*
    12821349                 * There are 32 mono channels of 16bits each.
    1283                  * 24bit Audio uses 2x channels over 16bit
    1284                  * 96kHz uses 2x channels over 48kHz
    1285                  * 192kHz uses 4x channels over 48kHz
    1286                  * So, for 48kHz 24bit, one has 16 channels
    1287                  * for 96kHz 24bit, one has 8 channels
    1288                  * for 192kHz 24bit, one has 4 channels
    1289                  *
     1350                 * 24bit Audio uses 2x channels over 16bit,
     1351                 * 96kHz uses 2x channels over 48kHz,
     1352                 * 192kHz uses 4x channels over 48kHz.
     1353                 * So, for 48kHz 24bit, one has 16 channels,
     1354                 * for 96kHz 24bit, one has 8 channels,
     1355                 * for 192kHz 24bit, one has 4 channels.
     1356                 * 1010rev2 and 1616(m) cards have double that,
     1357                 * but we don't exceed 16 channels anyway.
    12901358                 */
    1291 #if 1
    1292                 switch (emu->emu1010.internal_clock) {
    1293                 case 0:
    1294                         /* For 44.1kHz */
    1295                         runtime->hw.rates = SNDRV_PCM_RATE_44100;
    1296                         runtime->hw.rate_min = runtime->hw.rate_max = 44100;
    1297                         runtime->hw.channels_min =
    1298                                 runtime->hw.channels_max = 16;
    1299                         break;
    1300                 case 1:
    1301                         /* For 48kHz */
    1302                         runtime->hw.rates = SNDRV_PCM_RATE_48000;
    1303                         runtime->hw.rate_min = runtime->hw.rate_max = 48000;
    1304                         runtime->hw.channels_min =
    1305                                 runtime->hw.channels_max = 16;
    1306                         break;
    1307                 }
    1308 #endif
    13091359#if 0
    13101360                /* For 96kHz */
    1311                 runtime->hw.rates = SNDRV_PCM_RATE_96000;
    1312                 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
    13131361                runtime->hw.channels_min = runtime->hw.channels_max = 4;
    13141362#endif
    13151363#if 0
    13161364                /* For 192kHz */
    1317                 runtime->hw.rates = SNDRV_PCM_RATE_192000;
    1318                 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
    13191365                runtime->hw.channels_min = runtime->hw.channels_max = 2;
    13201366#endif
    13211367                runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
    1322                 /* efx_voices_mask[0] is expected to be zero
    1323                  * efx_voices_mask[1] is expected to have 32bits set
    1324                  */
    13251368        } else {
     1369                spin_lock_irq(&emu->reg_lock);
    13261370                runtime->hw.channels_min = runtime->hw.channels_max = 0;
    13271371                for (idx = 0; idx < nefx; idx++) {
     
    13311375                        }
    13321376                }
    1333         }
    1334         epcm->capture_cr_val = emu->efx_voices_mask[0];
    1335         epcm->capture_cr_val2 = emu->efx_voices_mask[1];
    1336         spin_unlock_irq(&emu->reg_lock);
     1377                epcm->capture_cr_val = emu->efx_voices_mask[0];
     1378                epcm->capture_cr_val2 = emu->efx_voices_mask[1];
     1379                spin_unlock_irq(&emu->reg_lock);
     1380        }
     1381        err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
     1382                                         &hw_constraints_efx_capture_channels);
     1383        if (err < 0) {
     1384                kfree(epcm);
     1385                return err;
     1386        }
     1387        snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
     1388                                   &hw_constraints_capture_buffer_sizes);
    13371389        emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
    13381390        emu->pcm_capture_efx_substream = substream;
    1339         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
    13401391        return 0;
    13411392}
     
    13451396        struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
    13461397
    1347         emu->capture_interrupt = NULL;
     1398        emu->capture_efx_interrupt = NULL;
    13481399        emu->pcm_capture_efx_substream = NULL;
    13491400        return 0;
     
    13731424        .close =                snd_emu10k1_efx_playback_close,
    13741425        .hw_params =            snd_emu10k1_playback_hw_params,
    1375         .hw_free =              snd_emu10k1_efx_playback_hw_free,
     1426        .hw_free =              snd_emu10k1_playback_hw_free,
    13761427        .prepare =              snd_emu10k1_efx_playback_prepare,
    13771428        .trigger =              snd_emu10k1_efx_playback_trigger,
    1378         .pointer =              snd_emu10k1_efx_playback_pointer,
     1429        .pointer =              snd_emu10k1_playback_pointer,
    13791430};
    13801431
     
    14881539        int idx;
    14891540       
    1490         spin_lock_irq(&emu->reg_lock);
    14911541        for (idx = 0; idx < nefx; idx++)
    14921542                ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
    1493         spin_unlock_irq(&emu->reg_lock);
    14941543        return 0;
    14951544}
     
    15001549        unsigned int nval[2], bits;
    15011550        int nefx = emu->audigy ? 64 : 32;
    1502         int nefxb = emu->audigy ? 7 : 6;
    15031551        int change, idx;
    15041552       
     
    15091557                        bits++;
    15101558                }
    1511                
    1512         for (idx = 0; idx < nefxb; idx++)
    1513                 if (1 << idx == bits)
    1514                         break;
    1515        
    1516         if (idx >= nefxb)
     1559
     1560        if (bits == 9 || bits == 11 || bits == 13 || bits == 15 || bits > 16)
    15171561                return -EINVAL;
    15181562
     
    16461690        pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
    16471691        pcm->tram_shift = 0;
    1648         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0);     /* reset */
    1649         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);     /* reset */
    1650         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
    1651         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0);         /* reset ptr number */
    1652         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
    1653         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
     1692        snd_emu10k1_ptr_write_multiple(emu, 0,
     1693                emu->gpr_base + pcm->gpr_running, 0,    /* reset */
     1694                emu->gpr_base + pcm->gpr_trigger, 0,    /* reset */
     1695                emu->gpr_base + pcm->gpr_size, runtime->buffer_size,
     1696                emu->gpr_base + pcm->gpr_ptr, 0,        /* reset ptr number */
     1697                emu->gpr_base + pcm->gpr_count, runtime->period_size,
     1698                emu->gpr_base + pcm->gpr_tmpcount, runtime->period_size,
     1699                REGLIST_END);
    16541700        for (i = 0; i < pcm->channels; i++)
    16551701                snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
     
    17821828        int err;
    17831829
    1784         err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm);
     1830        err = snd_pcm_new(emu->card, "emu10k1 efx", device, emu->audigy ? 0 : 8, 1, &pcm);
    17851831        if (err < 0)
    17861832                return err;
     
    17881834        pcm->private_data = emu;
    17891835
    1790         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
     1836        if (!emu->audigy)
     1837                snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
    17911838        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
    17921839
    17931840        pcm->info_flags = 0;
    1794         strcpy(pcm->name, "Multichannel Capture/PT Playback");
     1841        if (emu->audigy)
     1842                strcpy(pcm->name, "Multichannel Capture");
     1843        else
     1844                strcpy(pcm->name, "Multichannel Capture/PT Playback");
    17951845        emu->pcm_efx = pcm;
    17961846
    1797         /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
    1798          * to these
    1799          */     
    1800        
    1801         /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
    1802         if (emu->audigy) {
    1803                 emu->efx_voices_mask[0] = 0;
    1804                 if (emu->card_capabilities->emu_model)
    1805                         /* Pavel Hofman - 32 voices will be used for
    1806                          * capture (write mode) -
    1807                          * each bit = corresponding voice
    1808                          */
    1809                         emu->efx_voices_mask[1] = 0xffffffff;
    1810                 else
     1847        if (!emu->card_capabilities->emu_model) {
     1848                // On Sound Blasters, the DSP code copies the EXTINs to FXBUS2.
     1849                // The mask determines which of these and the EXTOUTs the multi-
     1850                // channel capture actually records (the channel order is fixed).
     1851                if (emu->audigy) {
     1852                        emu->efx_voices_mask[0] = 0;
    18111853                        emu->efx_voices_mask[1] = 0xffff;
     1854                } else {
     1855                        emu->efx_voices_mask[0] = 0xffff0000;
     1856                        emu->efx_voices_mask[1] = 0;
     1857                }
     1858                kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
     1859                if (!kctl)
     1860                        return -ENOMEM;
     1861                kctl->id.device = device;
     1862                err = snd_ctl_add(emu->card, kctl);
     1863                if (err < 0)
     1864                        return err;
    18121865        } else {
    1813                 emu->efx_voices_mask[0] = 0xffff0000;
    1814                 emu->efx_voices_mask[1] = 0;
    1815         }
    1816         /* For emu1010, the control has to set 32 upper bits (voices)
    1817          * out of the 64 bits (voices) to true for the 16-channels capture
    1818          * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
    1819          * is already defined but the snd_emu10k1_pcm_efx_voices_mask
    1820          * control can override this register's value.
    1821          */
    1822         kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
    1823         if (!kctl)
    1824                 return -ENOMEM;
    1825         kctl->id.device = device;
    1826         err = snd_ctl_add(emu->card, kctl);
    1827         if (err < 0)
    1828                 return err;
     1866                // On E-MU cards, the DSP code copies the P16VINs/EMU32INs to
     1867                // FXBUS2. These are already selected & routed by the FPGA,
     1868                // so there is no need to apply additional masking.
     1869        }
    18291870
    18301871        snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
Note: See TracChangeset for help on using the changeset viewer.