Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
0004  *  Driver EMU10K1X chips
0005  *
0006  *  Parts of this code were adapted from audigyls.c driver which is
0007  *  Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
0008  *
0009  *  BUGS:
0010  *    --
0011  *
0012  *  TODO:
0013  *
0014  *  Chips (SB0200 model):
0015  *    - EMU10K1X-DBQ
0016  *    - STAC 9708T
0017  */
0018 #include <linux/init.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/pci.h>
0021 #include <linux/dma-mapping.h>
0022 #include <linux/slab.h>
0023 #include <linux/module.h>
0024 #include <sound/core.h>
0025 #include <sound/initval.h>
0026 #include <sound/pcm.h>
0027 #include <sound/ac97_codec.h>
0028 #include <sound/info.h>
0029 #include <sound/rawmidi.h>
0030 
0031 MODULE_AUTHOR("Francisco Moraes <fmoraes@nc.rr.com>");
0032 MODULE_DESCRIPTION("EMU10K1X");
0033 MODULE_LICENSE("GPL");
0034 
0035 // module parameters (see "Module Parameters")
0036 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0037 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0038 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
0039 
0040 module_param_array(index, int, NULL, 0444);
0041 MODULE_PARM_DESC(index, "Index value for the EMU10K1X soundcard.");
0042 module_param_array(id, charp, NULL, 0444);
0043 MODULE_PARM_DESC(id, "ID string for the EMU10K1X soundcard.");
0044 module_param_array(enable, bool, NULL, 0444);
0045 MODULE_PARM_DESC(enable, "Enable the EMU10K1X soundcard.");
0046 
0047 
0048 // some definitions were borrowed from emu10k1 driver as they seem to be the same
0049 /************************************************************************************************/
0050 /* PCI function 0 registers, address = <val> + PCIBASE0                     */
0051 /************************************************************************************************/
0052 
0053 #define PTR         0x00        /* Indexed register set pointer register    */
0054                         /* NOTE: The CHANNELNUM and ADDRESS words can   */
0055                         /* be modified independently of each other. */
0056 
0057 #define DATA            0x04        /* Indexed register set data register       */
0058 
0059 #define IPR         0x08        /* Global interrupt pending register        */
0060                         /* Clear pending interrupts by writing a 1 to   */
0061                         /* the relevant bits and zero to the other bits */
0062 #define IPR_MIDITRANSBUFEMPTY   0x00000001  /* MIDI UART transmit buffer empty      */
0063 #define IPR_MIDIRECVBUFEMPTY    0x00000002  /* MIDI UART receive buffer empty       */
0064 #define IPR_CH_0_LOOP           0x00000800      /* Channel 0 loop                               */
0065 #define IPR_CH_0_HALF_LOOP      0x00000100      /* Channel 0 half loop                          */
0066 #define IPR_CAP_0_LOOP          0x00080000      /* Channel capture loop                         */
0067 #define IPR_CAP_0_HALF_LOOP     0x00010000      /* Channel capture half loop                    */
0068 
0069 #define INTE            0x0c        /* Interrupt enable register            */
0070 #define INTE_MIDITXENABLE       0x00000001  /* Enable MIDI transmit-buffer-empty interrupts */
0071 #define INTE_MIDIRXENABLE       0x00000002  /* Enable MIDI receive-buffer-empty interrupts  */
0072 #define INTE_CH_0_LOOP          0x00000800      /* Channel 0 loop                               */
0073 #define INTE_CH_0_HALF_LOOP     0x00000100      /* Channel 0 half loop                          */
0074 #define INTE_CAP_0_LOOP         0x00080000      /* Channel capture loop                         */
0075 #define INTE_CAP_0_HALF_LOOP    0x00010000      /* Channel capture half loop                    */
0076 
0077 #define HCFG            0x14        /* Hardware config register         */
0078 
0079 #define HCFG_LOCKSOUNDCACHE 0x00000008  /* 1 = Cancel bustmaster accesses to soundcache */
0080                         /* NOTE: This should generally never be used.   */
0081 #define HCFG_AUDIOENABLE    0x00000001  /* 0 = CODECs transmit zero-valued samples  */
0082                         /* Should be set to 1 when the EMU10K1 is   */
0083                         /* completely initialized.          */
0084 #define GPIO            0x18        /* Defaults: 00001080-Analog, 00001000-SPDIF.   */
0085 
0086 
0087 #define AC97DATA        0x1c        /* AC97 register set data register (16 bit) */
0088 
0089 #define AC97ADDRESS     0x1e        /* AC97 register set address register (8 bit)   */
0090 
0091 /********************************************************************************************************/
0092 /* Emu10k1x pointer-offset register set, accessed through the PTR and DATA registers            */
0093 /********************************************************************************************************/
0094 #define PLAYBACK_LIST_ADDR  0x00        /* Base DMA address of a list of pointers to each period/size */
0095                         /* One list entry: 4 bytes for DMA address, 
0096                          * 4 bytes for period_size << 16.
0097                          * One list entry is 8 bytes long.
0098                          * One list entry for each period in the buffer.
0099                          */
0100 #define PLAYBACK_LIST_SIZE  0x01        /* Size of list in bytes << 16. E.g. 8 periods -> 0x00380000  */
0101 #define PLAYBACK_LIST_PTR   0x02        /* Pointer to the current period being played */
0102 #define PLAYBACK_DMA_ADDR   0x04        /* Playback DMA address */
0103 #define PLAYBACK_PERIOD_SIZE    0x05        /* Playback period size */
0104 #define PLAYBACK_POINTER    0x06        /* Playback period pointer. Sample currently in DAC */
0105 #define PLAYBACK_UNKNOWN1       0x07
0106 #define PLAYBACK_UNKNOWN2       0x08
0107 
0108 /* Only one capture channel supported */
0109 #define CAPTURE_DMA_ADDR    0x10        /* Capture DMA address */
0110 #define CAPTURE_BUFFER_SIZE 0x11        /* Capture buffer size */
0111 #define CAPTURE_POINTER     0x12        /* Capture buffer pointer. Sample currently in ADC */
0112 #define CAPTURE_UNKNOWN         0x13
0113 
0114 /* From 0x20 - 0x3f, last samples played on each channel */
0115 
0116 #define TRIGGER_CHANNEL         0x40            /* Trigger channel playback                     */
0117 #define TRIGGER_CHANNEL_0       0x00000001      /* Trigger channel 0                            */
0118 #define TRIGGER_CHANNEL_1       0x00000002      /* Trigger channel 1                            */
0119 #define TRIGGER_CHANNEL_2       0x00000004      /* Trigger channel 2                            */
0120 #define TRIGGER_CAPTURE         0x00000100      /* Trigger capture channel                      */
0121 
0122 #define ROUTING                 0x41            /* Setup sound routing ?                        */
0123 #define ROUTING_FRONT_LEFT      0x00000001
0124 #define ROUTING_FRONT_RIGHT     0x00000002
0125 #define ROUTING_REAR_LEFT       0x00000004
0126 #define ROUTING_REAR_RIGHT      0x00000008
0127 #define ROUTING_CENTER_LFE      0x00010000
0128 
0129 #define SPCS0           0x42        /* SPDIF output Channel Status 0 register   */
0130 
0131 #define SPCS1           0x43        /* SPDIF output Channel Status 1 register   */
0132 
0133 #define SPCS2           0x44        /* SPDIF output Channel Status 2 register   */
0134 
0135 #define SPCS_CLKACCYMASK    0x30000000  /* Clock accuracy               */
0136 #define SPCS_CLKACCY_1000PPM    0x00000000  /* 1000 parts per million           */
0137 #define SPCS_CLKACCY_50PPM  0x10000000  /* 50 parts per million             */
0138 #define SPCS_CLKACCY_VARIABLE   0x20000000  /* Variable accuracy                */
0139 #define SPCS_SAMPLERATEMASK 0x0f000000  /* Sample rate                  */
0140 #define SPCS_SAMPLERATE_44  0x00000000  /* 44.1kHz sample rate              */
0141 #define SPCS_SAMPLERATE_48  0x02000000  /* 48kHz sample rate                */
0142 #define SPCS_SAMPLERATE_32  0x03000000  /* 32kHz sample rate                */
0143 #define SPCS_CHANNELNUMMASK 0x00f00000  /* Channel number               */
0144 #define SPCS_CHANNELNUM_UNSPEC  0x00000000  /* Unspecified channel number           */
0145 #define SPCS_CHANNELNUM_LEFT    0x00100000  /* Left channel                 */
0146 #define SPCS_CHANNELNUM_RIGHT   0x00200000  /* Right channel                */
0147 #define SPCS_SOURCENUMMASK  0x000f0000  /* Source number                */
0148 #define SPCS_SOURCENUM_UNSPEC   0x00000000  /* Unspecified source number            */
0149 #define SPCS_GENERATIONSTATUS   0x00008000  /* Originality flag (see IEC-958 spec)      */
0150 #define SPCS_CATEGORYCODEMASK   0x00007f00  /* Category code (see IEC-958 spec)     */
0151 #define SPCS_MODEMASK       0x000000c0  /* Mode (see IEC-958 spec)          */
0152 #define SPCS_EMPHASISMASK   0x00000038  /* Emphasis                 */
0153 #define SPCS_EMPHASIS_NONE  0x00000000  /* No emphasis                  */
0154 #define SPCS_EMPHASIS_50_15 0x00000008  /* 50/15 usec 2 channel             */
0155 #define SPCS_COPYRIGHT      0x00000004  /* Copyright asserted flag -- do not modify */
0156 #define SPCS_NOTAUDIODATA   0x00000002  /* 0 = Digital audio, 1 = not audio     */
0157 #define SPCS_PROFESSIONAL   0x00000001  /* 0 = Consumer (IEC-958), 1 = pro (AES3-1992)  */
0158 
0159 #define SPDIF_SELECT        0x45        /* Enables SPDIF or Analogue outputs 0-Analogue, 0x700-SPDIF */
0160 
0161 /* This is the MPU port on the card                                         */
0162 #define MUDATA      0x47
0163 #define MUCMD       0x48
0164 #define MUSTAT      MUCMD
0165 
0166 /* From 0x50 - 0x5f, last samples captured */
0167 
0168 /*
0169  * The hardware has 3 channels for playback and 1 for capture.
0170  *  - channel 0 is the front channel
0171  *  - channel 1 is the rear channel
0172  *  - channel 2 is the center/lfe channel
0173  * Volume is controlled by the AC97 for the front and rear channels by
0174  * the PCM Playback Volume, Sigmatel Surround Playback Volume and 
0175  * Surround Playback Volume. The Sigmatel 4-Speaker Stereo switch affects
0176  * the front/rear channel mixing in the REAR OUT jack. When using the
0177  * 4-Speaker Stereo, both front and rear channels will be mixed in the
0178  * REAR OUT.
0179  * The center/lfe channel has no volume control and cannot be muted during
0180  * playback.
0181  */
0182 
0183 struct emu10k1x_voice {
0184     struct emu10k1x *emu;
0185     int number;
0186     int use;
0187   
0188     struct emu10k1x_pcm *epcm;
0189 };
0190 
0191 struct emu10k1x_pcm {
0192     struct emu10k1x *emu;
0193     struct snd_pcm_substream *substream;
0194     struct emu10k1x_voice *voice;
0195     unsigned short running;
0196 };
0197 
0198 struct emu10k1x_midi {
0199     struct emu10k1x *emu;
0200     struct snd_rawmidi *rmidi;
0201     struct snd_rawmidi_substream *substream_input;
0202     struct snd_rawmidi_substream *substream_output;
0203     unsigned int midi_mode;
0204     spinlock_t input_lock;
0205     spinlock_t output_lock;
0206     spinlock_t open_lock;
0207     int tx_enable, rx_enable;
0208     int port;
0209     int ipr_tx, ipr_rx;
0210     void (*interrupt)(struct emu10k1x *emu, unsigned int status);
0211 };
0212 
0213 // definition of the chip-specific record
0214 struct emu10k1x {
0215     struct snd_card *card;
0216     struct pci_dev *pci;
0217 
0218     unsigned long port;
0219     int irq;
0220 
0221     unsigned char revision;     /* chip revision */
0222     unsigned int serial;            /* serial number */
0223     unsigned short model;       /* subsystem id */
0224 
0225     spinlock_t emu_lock;
0226     spinlock_t voice_lock;
0227 
0228     struct snd_ac97 *ac97;
0229     struct snd_pcm *pcm;
0230 
0231     struct emu10k1x_voice voices[3];
0232     struct emu10k1x_voice capture_voice;
0233     u32 spdif_bits[3]; // SPDIF out setup
0234 
0235     struct snd_dma_buffer *dma_buffer;
0236 
0237     struct emu10k1x_midi midi;
0238 };
0239 
0240 /* hardware definition */
0241 static const struct snd_pcm_hardware snd_emu10k1x_playback_hw = {
0242     .info =         (SNDRV_PCM_INFO_MMAP | 
0243                  SNDRV_PCM_INFO_INTERLEAVED |
0244                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
0245                  SNDRV_PCM_INFO_MMAP_VALID),
0246     .formats =      SNDRV_PCM_FMTBIT_S16_LE,
0247     .rates =        SNDRV_PCM_RATE_48000,
0248     .rate_min =     48000,
0249     .rate_max =     48000,
0250     .channels_min =     2,
0251     .channels_max =     2,
0252     .buffer_bytes_max = (32*1024),
0253     .period_bytes_min = 64,
0254     .period_bytes_max = (16*1024),
0255     .periods_min =      2,
0256     .periods_max =      8,
0257     .fifo_size =        0,
0258 };
0259 
0260 static const struct snd_pcm_hardware snd_emu10k1x_capture_hw = {
0261     .info =         (SNDRV_PCM_INFO_MMAP | 
0262                  SNDRV_PCM_INFO_INTERLEAVED |
0263                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
0264                  SNDRV_PCM_INFO_MMAP_VALID),
0265     .formats =      SNDRV_PCM_FMTBIT_S16_LE,
0266     .rates =        SNDRV_PCM_RATE_48000,
0267     .rate_min =     48000,
0268     .rate_max =     48000,
0269     .channels_min =     2,
0270     .channels_max =     2,
0271     .buffer_bytes_max = (32*1024),
0272     .period_bytes_min = 64,
0273     .period_bytes_max = (16*1024),
0274     .periods_min =      2,
0275     .periods_max =      2,
0276     .fifo_size =        0,
0277 };
0278 
0279 static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x * emu, 
0280                       unsigned int reg, 
0281                       unsigned int chn)
0282 {
0283     unsigned long flags;
0284     unsigned int regptr, val;
0285   
0286     regptr = (reg << 16) | chn;
0287 
0288     spin_lock_irqsave(&emu->emu_lock, flags);
0289     outl(regptr, emu->port + PTR);
0290     val = inl(emu->port + DATA);
0291     spin_unlock_irqrestore(&emu->emu_lock, flags);
0292     return val;
0293 }
0294 
0295 static void snd_emu10k1x_ptr_write(struct emu10k1x *emu, 
0296                    unsigned int reg, 
0297                    unsigned int chn, 
0298                    unsigned int data)
0299 {
0300     unsigned int regptr;
0301     unsigned long flags;
0302 
0303     regptr = (reg << 16) | chn;
0304 
0305     spin_lock_irqsave(&emu->emu_lock, flags);
0306     outl(regptr, emu->port + PTR);
0307     outl(data, emu->port + DATA);
0308     spin_unlock_irqrestore(&emu->emu_lock, flags);
0309 }
0310 
0311 static void snd_emu10k1x_intr_enable(struct emu10k1x *emu, unsigned int intrenb)
0312 {
0313     unsigned long flags;
0314     unsigned int intr_enable;
0315 
0316     spin_lock_irqsave(&emu->emu_lock, flags);
0317     intr_enable = inl(emu->port + INTE) | intrenb;
0318     outl(intr_enable, emu->port + INTE);
0319     spin_unlock_irqrestore(&emu->emu_lock, flags);
0320 }
0321 
0322 static void snd_emu10k1x_intr_disable(struct emu10k1x *emu, unsigned int intrenb)
0323 {
0324     unsigned long flags;
0325     unsigned int intr_enable;
0326 
0327     spin_lock_irqsave(&emu->emu_lock, flags);
0328     intr_enable = inl(emu->port + INTE) & ~intrenb;
0329     outl(intr_enable, emu->port + INTE);
0330     spin_unlock_irqrestore(&emu->emu_lock, flags);
0331 }
0332 
0333 static void snd_emu10k1x_gpio_write(struct emu10k1x *emu, unsigned int value)
0334 {
0335     unsigned long flags;
0336 
0337     spin_lock_irqsave(&emu->emu_lock, flags);
0338     outl(value, emu->port + GPIO);
0339     spin_unlock_irqrestore(&emu->emu_lock, flags);
0340 }
0341 
0342 static void snd_emu10k1x_pcm_free_substream(struct snd_pcm_runtime *runtime)
0343 {
0344     kfree(runtime->private_data);
0345 }
0346 
0347 static void snd_emu10k1x_pcm_interrupt(struct emu10k1x *emu, struct emu10k1x_voice *voice)
0348 {
0349     struct emu10k1x_pcm *epcm;
0350 
0351     epcm = voice->epcm;
0352     if (!epcm)
0353         return;
0354     if (epcm->substream == NULL)
0355         return;
0356 #if 0
0357     dev_info(emu->card->dev,
0358          "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
0359            epcm->substream->ops->pointer(epcm->substream),
0360            snd_pcm_lib_period_bytes(epcm->substream),
0361            snd_pcm_lib_buffer_bytes(epcm->substream));
0362 #endif
0363     snd_pcm_period_elapsed(epcm->substream);
0364 }
0365 
0366 /* open callback */
0367 static int snd_emu10k1x_playback_open(struct snd_pcm_substream *substream)
0368 {
0369     struct emu10k1x *chip = snd_pcm_substream_chip(substream);
0370     struct emu10k1x_pcm *epcm;
0371     struct snd_pcm_runtime *runtime = substream->runtime;
0372     int err;
0373 
0374     err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
0375     if (err < 0)
0376         return err;
0377     err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
0378     if (err < 0)
0379                 return err;
0380 
0381     epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
0382     if (epcm == NULL)
0383         return -ENOMEM;
0384     epcm->emu = chip;
0385     epcm->substream = substream;
0386   
0387     runtime->private_data = epcm;
0388     runtime->private_free = snd_emu10k1x_pcm_free_substream;
0389   
0390     runtime->hw = snd_emu10k1x_playback_hw;
0391 
0392     return 0;
0393 }
0394 
0395 /* close callback */
0396 static int snd_emu10k1x_playback_close(struct snd_pcm_substream *substream)
0397 {
0398     return 0;
0399 }
0400 
0401 /* hw_params callback */
0402 static int snd_emu10k1x_pcm_hw_params(struct snd_pcm_substream *substream,
0403                       struct snd_pcm_hw_params *hw_params)
0404 {
0405     struct snd_pcm_runtime *runtime = substream->runtime;
0406     struct emu10k1x_pcm *epcm = runtime->private_data;
0407 
0408     if (! epcm->voice) {
0409         epcm->voice = &epcm->emu->voices[substream->pcm->device];
0410         epcm->voice->use = 1;
0411         epcm->voice->epcm = epcm;
0412     }
0413 
0414     return 0;
0415 }
0416 
0417 /* hw_free callback */
0418 static int snd_emu10k1x_pcm_hw_free(struct snd_pcm_substream *substream)
0419 {
0420     struct snd_pcm_runtime *runtime = substream->runtime;
0421     struct emu10k1x_pcm *epcm;
0422 
0423     if (runtime->private_data == NULL)
0424         return 0;
0425     
0426     epcm = runtime->private_data;
0427 
0428     if (epcm->voice) {
0429         epcm->voice->use = 0;
0430         epcm->voice->epcm = NULL;
0431         epcm->voice = NULL;
0432     }
0433 
0434     return 0;
0435 }
0436 
0437 /* prepare callback */
0438 static int snd_emu10k1x_pcm_prepare(struct snd_pcm_substream *substream)
0439 {
0440     struct emu10k1x *emu = snd_pcm_substream_chip(substream);
0441     struct snd_pcm_runtime *runtime = substream->runtime;
0442     struct emu10k1x_pcm *epcm = runtime->private_data;
0443     int voice = epcm->voice->number;
0444     u32 *table_base = (u32 *)(emu->dma_buffer->area+1024*voice);
0445     u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
0446     int i;
0447     
0448     for(i = 0; i < runtime->periods; i++) {
0449         *table_base++=runtime->dma_addr+(i*period_size_bytes);
0450         *table_base++=period_size_bytes<<16;
0451     }
0452 
0453     snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer->addr+1024*voice);
0454     snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
0455     snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
0456     snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
0457     snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
0458     snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
0459     snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
0460 
0461     snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
0462 
0463     return 0;
0464 }
0465 
0466 /* trigger callback */
0467 static int snd_emu10k1x_pcm_trigger(struct snd_pcm_substream *substream,
0468                     int cmd)
0469 {
0470     struct emu10k1x *emu = snd_pcm_substream_chip(substream);
0471     struct snd_pcm_runtime *runtime = substream->runtime;
0472     struct emu10k1x_pcm *epcm = runtime->private_data;
0473     int channel = epcm->voice->number;
0474     int result = 0;
0475 
0476     /*
0477     dev_dbg(emu->card->dev,
0478         "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n",
0479         (int)emu, cmd, (int)substream->ops->pointer(substream));
0480     */
0481 
0482     switch (cmd) {
0483     case SNDRV_PCM_TRIGGER_START:
0484         if(runtime->periods == 2)
0485             snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
0486         else
0487             snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
0488         epcm->running = 1;
0489         snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
0490         break;
0491     case SNDRV_PCM_TRIGGER_STOP:
0492         epcm->running = 0;
0493         snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
0494         snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
0495         break;
0496     default:
0497         result = -EINVAL;
0498         break;
0499     }
0500     return result;
0501 }
0502 
0503 /* pointer callback */
0504 static snd_pcm_uframes_t
0505 snd_emu10k1x_pcm_pointer(struct snd_pcm_substream *substream)
0506 {
0507     struct emu10k1x *emu = snd_pcm_substream_chip(substream);
0508     struct snd_pcm_runtime *runtime = substream->runtime;
0509     struct emu10k1x_pcm *epcm = runtime->private_data;
0510     int channel = epcm->voice->number;
0511     snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
0512 
0513     if (!epcm->running)
0514         return 0;
0515 
0516     ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
0517     ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
0518     ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
0519 
0520     if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
0521         return 0;
0522     
0523     if (ptr3 != ptr4) 
0524         ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
0525     ptr2 = bytes_to_frames(runtime, ptr1);
0526     ptr2 += (ptr4 >> 3) * runtime->period_size;
0527     ptr = ptr2;
0528 
0529     if (ptr >= runtime->buffer_size)
0530         ptr -= runtime->buffer_size;
0531 
0532     return ptr;
0533 }
0534 
0535 /* operators */
0536 static const struct snd_pcm_ops snd_emu10k1x_playback_ops = {
0537     .open =        snd_emu10k1x_playback_open,
0538     .close =       snd_emu10k1x_playback_close,
0539     .hw_params =   snd_emu10k1x_pcm_hw_params,
0540     .hw_free =     snd_emu10k1x_pcm_hw_free,
0541     .prepare =     snd_emu10k1x_pcm_prepare,
0542     .trigger =     snd_emu10k1x_pcm_trigger,
0543     .pointer =     snd_emu10k1x_pcm_pointer,
0544 };
0545 
0546 /* open_capture callback */
0547 static int snd_emu10k1x_pcm_open_capture(struct snd_pcm_substream *substream)
0548 {
0549     struct emu10k1x *chip = snd_pcm_substream_chip(substream);
0550     struct emu10k1x_pcm *epcm;
0551     struct snd_pcm_runtime *runtime = substream->runtime;
0552     int err;
0553 
0554     err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
0555     if (err < 0)
0556         return err;
0557     err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
0558     if (err < 0)
0559         return err;
0560 
0561     epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
0562     if (epcm == NULL)
0563         return -ENOMEM;
0564 
0565     epcm->emu = chip;
0566     epcm->substream = substream;
0567 
0568     runtime->private_data = epcm;
0569     runtime->private_free = snd_emu10k1x_pcm_free_substream;
0570 
0571     runtime->hw = snd_emu10k1x_capture_hw;
0572 
0573     return 0;
0574 }
0575 
0576 /* close callback */
0577 static int snd_emu10k1x_pcm_close_capture(struct snd_pcm_substream *substream)
0578 {
0579     return 0;
0580 }
0581 
0582 /* hw_params callback */
0583 static int snd_emu10k1x_pcm_hw_params_capture(struct snd_pcm_substream *substream,
0584                           struct snd_pcm_hw_params *hw_params)
0585 {
0586     struct snd_pcm_runtime *runtime = substream->runtime;
0587     struct emu10k1x_pcm *epcm = runtime->private_data;
0588 
0589     if (! epcm->voice) {
0590         if (epcm->emu->capture_voice.use)
0591             return -EBUSY;
0592         epcm->voice = &epcm->emu->capture_voice;
0593         epcm->voice->epcm = epcm;
0594         epcm->voice->use = 1;
0595     }
0596 
0597     return 0;
0598 }
0599 
0600 /* hw_free callback */
0601 static int snd_emu10k1x_pcm_hw_free_capture(struct snd_pcm_substream *substream)
0602 {
0603     struct snd_pcm_runtime *runtime = substream->runtime;
0604 
0605     struct emu10k1x_pcm *epcm;
0606 
0607     if (runtime->private_data == NULL)
0608         return 0;
0609     epcm = runtime->private_data;
0610 
0611     if (epcm->voice) {
0612         epcm->voice->use = 0;
0613         epcm->voice->epcm = NULL;
0614         epcm->voice = NULL;
0615     }
0616 
0617     return 0;
0618 }
0619 
0620 /* prepare capture callback */
0621 static int snd_emu10k1x_pcm_prepare_capture(struct snd_pcm_substream *substream)
0622 {
0623     struct emu10k1x *emu = snd_pcm_substream_chip(substream);
0624     struct snd_pcm_runtime *runtime = substream->runtime;
0625 
0626     snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
0627     snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
0628     snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
0629     snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
0630 
0631     return 0;
0632 }
0633 
0634 /* trigger_capture callback */
0635 static int snd_emu10k1x_pcm_trigger_capture(struct snd_pcm_substream *substream,
0636                         int cmd)
0637 {
0638     struct emu10k1x *emu = snd_pcm_substream_chip(substream);
0639     struct snd_pcm_runtime *runtime = substream->runtime;
0640     struct emu10k1x_pcm *epcm = runtime->private_data;
0641     int result = 0;
0642 
0643     switch (cmd) {
0644     case SNDRV_PCM_TRIGGER_START:
0645         snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP | 
0646                      INTE_CAP_0_HALF_LOOP);
0647         snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
0648         epcm->running = 1;
0649         break;
0650     case SNDRV_PCM_TRIGGER_STOP:
0651         epcm->running = 0;
0652         snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP | 
0653                       INTE_CAP_0_HALF_LOOP);
0654         snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
0655         break;
0656     default:
0657         result = -EINVAL;
0658         break;
0659     }
0660     return result;
0661 }
0662 
0663 /* pointer_capture callback */
0664 static snd_pcm_uframes_t
0665 snd_emu10k1x_pcm_pointer_capture(struct snd_pcm_substream *substream)
0666 {
0667     struct emu10k1x *emu = snd_pcm_substream_chip(substream);
0668     struct snd_pcm_runtime *runtime = substream->runtime;
0669     struct emu10k1x_pcm *epcm = runtime->private_data;
0670     snd_pcm_uframes_t ptr;
0671 
0672     if (!epcm->running)
0673         return 0;
0674 
0675     ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
0676     if (ptr >= runtime->buffer_size)
0677         ptr -= runtime->buffer_size;
0678 
0679     return ptr;
0680 }
0681 
0682 static const struct snd_pcm_ops snd_emu10k1x_capture_ops = {
0683     .open =        snd_emu10k1x_pcm_open_capture,
0684     .close =       snd_emu10k1x_pcm_close_capture,
0685     .hw_params =   snd_emu10k1x_pcm_hw_params_capture,
0686     .hw_free =     snd_emu10k1x_pcm_hw_free_capture,
0687     .prepare =     snd_emu10k1x_pcm_prepare_capture,
0688     .trigger =     snd_emu10k1x_pcm_trigger_capture,
0689     .pointer =     snd_emu10k1x_pcm_pointer_capture,
0690 };
0691 
0692 static unsigned short snd_emu10k1x_ac97_read(struct snd_ac97 *ac97,
0693                          unsigned short reg)
0694 {
0695     struct emu10k1x *emu = ac97->private_data;
0696     unsigned long flags;
0697     unsigned short val;
0698   
0699     spin_lock_irqsave(&emu->emu_lock, flags);
0700     outb(reg, emu->port + AC97ADDRESS);
0701     val = inw(emu->port + AC97DATA);
0702     spin_unlock_irqrestore(&emu->emu_lock, flags);
0703     return val;
0704 }
0705 
0706 static void snd_emu10k1x_ac97_write(struct snd_ac97 *ac97,
0707                     unsigned short reg, unsigned short val)
0708 {
0709     struct emu10k1x *emu = ac97->private_data;
0710     unsigned long flags;
0711   
0712     spin_lock_irqsave(&emu->emu_lock, flags);
0713     outb(reg, emu->port + AC97ADDRESS);
0714     outw(val, emu->port + AC97DATA);
0715     spin_unlock_irqrestore(&emu->emu_lock, flags);
0716 }
0717 
0718 static int snd_emu10k1x_ac97(struct emu10k1x *chip)
0719 {
0720     struct snd_ac97_bus *pbus;
0721     struct snd_ac97_template ac97;
0722     int err;
0723     static const struct snd_ac97_bus_ops ops = {
0724         .write = snd_emu10k1x_ac97_write,
0725         .read = snd_emu10k1x_ac97_read,
0726     };
0727   
0728     err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus);
0729     if (err < 0)
0730         return err;
0731     pbus->no_vra = 1; /* we don't need VRA */
0732 
0733     memset(&ac97, 0, sizeof(ac97));
0734     ac97.private_data = chip;
0735     ac97.scaps = AC97_SCAP_NO_SPDIF;
0736     return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
0737 }
0738 
0739 static void snd_emu10k1x_free(struct snd_card *card)
0740 {
0741     struct emu10k1x *chip = card->private_data;
0742 
0743     snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
0744     // disable interrupts
0745     outl(0, chip->port + INTE);
0746     // disable audio
0747     outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
0748 }
0749 
0750 static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id)
0751 {
0752     unsigned int status;
0753 
0754     struct emu10k1x *chip = dev_id;
0755     struct emu10k1x_voice *pvoice = chip->voices;
0756     int i;
0757     int mask;
0758 
0759     status = inl(chip->port + IPR);
0760 
0761     if (! status)
0762         return IRQ_NONE;
0763 
0764     // capture interrupt
0765     if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
0766         struct emu10k1x_voice *cap_voice = &chip->capture_voice;
0767         if (cap_voice->use)
0768             snd_emu10k1x_pcm_interrupt(chip, cap_voice);
0769         else
0770             snd_emu10k1x_intr_disable(chip, 
0771                           INTE_CAP_0_LOOP |
0772                           INTE_CAP_0_HALF_LOOP);
0773     }
0774         
0775     mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
0776     for (i = 0; i < 3; i++) {
0777         if (status & mask) {
0778             if (pvoice->use)
0779                 snd_emu10k1x_pcm_interrupt(chip, pvoice);
0780             else 
0781                 snd_emu10k1x_intr_disable(chip, mask);
0782         }
0783         pvoice++;
0784         mask <<= 1;
0785     }
0786         
0787     if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
0788         if (chip->midi.interrupt)
0789             chip->midi.interrupt(chip, status);
0790         else
0791             snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
0792     }
0793         
0794     // acknowledge the interrupt if necessary
0795     outl(status, chip->port + IPR);
0796 
0797     /* dev_dbg(chip->card->dev, "interrupt %08x\n", status); */
0798     return IRQ_HANDLED;
0799 }
0800 
0801 static const struct snd_pcm_chmap_elem surround_map[] = {
0802     { .channels = 2,
0803       .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
0804     { }
0805 };
0806 
0807 static const struct snd_pcm_chmap_elem clfe_map[] = {
0808     { .channels = 2,
0809       .map = { SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
0810     { }
0811 };
0812 
0813 static int snd_emu10k1x_pcm(struct emu10k1x *emu, int device)
0814 {
0815     struct snd_pcm *pcm;
0816     const struct snd_pcm_chmap_elem *map = NULL;
0817     int err;
0818     int capture = 0;
0819   
0820     if (device == 0)
0821         capture = 1;
0822     
0823     err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm);
0824     if (err < 0)
0825         return err;
0826   
0827     pcm->private_data = emu;
0828     
0829     switch(device) {
0830     case 0:
0831         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
0832         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
0833         break;
0834     case 1:
0835     case 2:
0836         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
0837         break;
0838     }
0839 
0840     pcm->info_flags = 0;
0841     switch(device) {
0842     case 0:
0843         strcpy(pcm->name, "EMU10K1X Front");
0844         map = snd_pcm_std_chmaps;
0845         break;
0846     case 1:
0847         strcpy(pcm->name, "EMU10K1X Rear");
0848         map = surround_map;
0849         break;
0850     case 2:
0851         strcpy(pcm->name, "EMU10K1X Center/LFE");
0852         map = clfe_map;
0853         break;
0854     }
0855     emu->pcm = pcm;
0856 
0857     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
0858                        &emu->pci->dev, 32*1024, 32*1024);
0859   
0860     return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, map, 2,
0861                      1 << 2, NULL);
0862 }
0863 
0864 static int snd_emu10k1x_create(struct snd_card *card,
0865                    struct pci_dev *pci)
0866 {
0867     struct emu10k1x *chip = card->private_data;
0868     int err;
0869     int ch;
0870 
0871     err = pcim_enable_device(pci);
0872     if (err < 0)
0873         return err;
0874 
0875     if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(28)) < 0) {
0876         dev_err(card->dev, "error to set 28bit mask DMA\n");
0877         return -ENXIO;
0878     }
0879 
0880     chip->card = card;
0881     chip->pci = pci;
0882     chip->irq = -1;
0883 
0884     spin_lock_init(&chip->emu_lock);
0885     spin_lock_init(&chip->voice_lock);
0886   
0887     err = pci_request_regions(pci, "EMU10K1X");
0888     if (err < 0)
0889         return err;
0890     chip->port = pci_resource_start(pci, 0);
0891 
0892     if (devm_request_irq(&pci->dev, pci->irq, snd_emu10k1x_interrupt,
0893                  IRQF_SHARED, KBUILD_MODNAME, chip)) {
0894         dev_err(card->dev, "cannot grab irq %d\n", pci->irq);
0895         return -EBUSY;
0896     }
0897     chip->irq = pci->irq;
0898     card->sync_irq = chip->irq;
0899     card->private_free = snd_emu10k1x_free;
0900   
0901     chip->dma_buffer = snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV,
0902                         4 * 1024);
0903     if (!chip->dma_buffer)
0904         return -ENOMEM;
0905 
0906     pci_set_master(pci);
0907     /* read revision & serial */
0908     chip->revision = pci->revision;
0909     pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
0910     pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
0911     dev_info(card->dev, "Model %04x Rev %08x Serial %08x\n", chip->model,
0912            chip->revision, chip->serial);
0913 
0914     outl(0, chip->port + INTE); 
0915 
0916     for(ch = 0; ch < 3; ch++) {
0917         chip->voices[ch].emu = chip;
0918         chip->voices[ch].number = ch;
0919     }
0920 
0921     /*
0922      *  Init to 0x02109204 :
0923      *  Clock accuracy    = 0     (1000ppm)
0924      *  Sample Rate       = 2     (48kHz)
0925      *  Audio Channel     = 1     (Left of 2)
0926      *  Source Number     = 0     (Unspecified)
0927      *  Generation Status = 1     (Original for Cat Code 12)
0928      *  Cat Code          = 12    (Digital Signal Mixer)
0929      *  Mode              = 0     (Mode 0)
0930      *  Emphasis          = 0     (None)
0931      *  CP                = 1     (Copyright unasserted)
0932      *  AN                = 0     (Audio data)
0933      *  P                 = 0     (Consumer)
0934      */
0935     snd_emu10k1x_ptr_write(chip, SPCS0, 0,
0936                    chip->spdif_bits[0] = 
0937                    SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
0938                    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
0939                    SPCS_GENERATIONSTATUS | 0x00001200 |
0940                    0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
0941     snd_emu10k1x_ptr_write(chip, SPCS1, 0,
0942                    chip->spdif_bits[1] = 
0943                    SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
0944                    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
0945                    SPCS_GENERATIONSTATUS | 0x00001200 |
0946                    0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
0947     snd_emu10k1x_ptr_write(chip, SPCS2, 0,
0948                    chip->spdif_bits[2] = 
0949                    SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
0950                    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
0951                    SPCS_GENERATIONSTATUS | 0x00001200 |
0952                    0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
0953 
0954     snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
0955     snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
0956     snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
0957 
0958     outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
0959 
0960     return 0;
0961 }
0962 
0963 static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry, 
0964                        struct snd_info_buffer *buffer)
0965 {
0966     struct emu10k1x *emu = entry->private_data;
0967     unsigned long value,value1,value2;
0968     unsigned long flags;
0969     int i;
0970 
0971     snd_iprintf(buffer, "Registers:\n\n");
0972     for(i = 0; i < 0x20; i+=4) {
0973         spin_lock_irqsave(&emu->emu_lock, flags);
0974         value = inl(emu->port + i);
0975         spin_unlock_irqrestore(&emu->emu_lock, flags);
0976         snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
0977     }
0978     snd_iprintf(buffer, "\nRegisters\n\n");
0979     for(i = 0; i <= 0x48; i++) {
0980         value = snd_emu10k1x_ptr_read(emu, i, 0);
0981         if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
0982             value1 = snd_emu10k1x_ptr_read(emu, i, 1);
0983             value2 = snd_emu10k1x_ptr_read(emu, i, 2);
0984             snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
0985         } else {
0986             snd_iprintf(buffer, "%02X: %08lX\n", i, value);
0987         }
0988     }
0989 }
0990 
0991 static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry, 
0992                     struct snd_info_buffer *buffer)
0993 {
0994     struct emu10k1x *emu = entry->private_data;
0995     char line[64];
0996     unsigned int reg, channel_id , val;
0997 
0998     while (!snd_info_get_line(buffer, line, sizeof(line))) {
0999         if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
1000             continue;
1001 
1002         if (reg < 0x49 && channel_id <= 2)
1003             snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
1004     }
1005 }
1006 
1007 static int snd_emu10k1x_proc_init(struct emu10k1x *emu)
1008 {
1009     snd_card_rw_proc_new(emu->card, "emu10k1x_regs", emu,
1010                  snd_emu10k1x_proc_reg_read,
1011                  snd_emu10k1x_proc_reg_write);
1012     return 0;
1013 }
1014 
1015 #define snd_emu10k1x_shared_spdif_info  snd_ctl_boolean_mono_info
1016 
1017 static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol *kcontrol,
1018                      struct snd_ctl_elem_value *ucontrol)
1019 {
1020     struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1021 
1022     ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
1023 
1024     return 0;
1025 }
1026 
1027 static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol *kcontrol,
1028                      struct snd_ctl_elem_value *ucontrol)
1029 {
1030     struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1031     unsigned int val;
1032 
1033     val = ucontrol->value.integer.value[0] ;
1034 
1035     if (val) {
1036         // enable spdif output
1037         snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
1038         snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
1039         snd_emu10k1x_gpio_write(emu, 0x1000);
1040     } else {
1041         // disable spdif output
1042         snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
1043         snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
1044         snd_emu10k1x_gpio_write(emu, 0x1080);
1045     }
1046     return 0;
1047 }
1048 
1049 static const struct snd_kcontrol_new snd_emu10k1x_shared_spdif =
1050 {
1051     .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
1052     .name =     "Analog/Digital Output Jack",
1053     .info =     snd_emu10k1x_shared_spdif_info,
1054     .get =      snd_emu10k1x_shared_spdif_get,
1055     .put =      snd_emu10k1x_shared_spdif_put
1056 };
1057 
1058 static int snd_emu10k1x_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1059 {
1060     uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1061     uinfo->count = 1;
1062     return 0;
1063 }
1064 
1065 static int snd_emu10k1x_spdif_get(struct snd_kcontrol *kcontrol,
1066                   struct snd_ctl_elem_value *ucontrol)
1067 {
1068     struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1069     unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1070 
1071     ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
1072     ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
1073     ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
1074     ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
1075     return 0;
1076 }
1077 
1078 static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol *kcontrol,
1079                        struct snd_ctl_elem_value *ucontrol)
1080 {
1081     ucontrol->value.iec958.status[0] = 0xff;
1082     ucontrol->value.iec958.status[1] = 0xff;
1083     ucontrol->value.iec958.status[2] = 0xff;
1084     ucontrol->value.iec958.status[3] = 0xff;
1085     return 0;
1086 }
1087 
1088 static int snd_emu10k1x_spdif_put(struct snd_kcontrol *kcontrol,
1089                   struct snd_ctl_elem_value *ucontrol)
1090 {
1091     struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1092     unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1093     int change;
1094     unsigned int val;
1095 
1096     val = (ucontrol->value.iec958.status[0] << 0) |
1097         (ucontrol->value.iec958.status[1] << 8) |
1098         (ucontrol->value.iec958.status[2] << 16) |
1099         (ucontrol->value.iec958.status[3] << 24);
1100     change = val != emu->spdif_bits[idx];
1101     if (change) {
1102         snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
1103         emu->spdif_bits[idx] = val;
1104     }
1105     return change;
1106 }
1107 
1108 static const struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control =
1109 {
1110     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1111     .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1112     .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1113     .count =    3,
1114     .info =         snd_emu10k1x_spdif_info,
1115     .get =          snd_emu10k1x_spdif_get_mask
1116 };
1117 
1118 static const struct snd_kcontrol_new snd_emu10k1x_spdif_control =
1119 {
1120     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1121     .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1122     .count =    3,
1123     .info =         snd_emu10k1x_spdif_info,
1124     .get =          snd_emu10k1x_spdif_get,
1125     .put =          snd_emu10k1x_spdif_put
1126 };
1127 
1128 static int snd_emu10k1x_mixer(struct emu10k1x *emu)
1129 {
1130     int err;
1131     struct snd_kcontrol *kctl;
1132     struct snd_card *card = emu->card;
1133 
1134     kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu);
1135     if (!kctl)
1136         return -ENOMEM;
1137     err = snd_ctl_add(card, kctl);
1138     if (err)
1139         return err;
1140     kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu);
1141     if (!kctl)
1142         return -ENOMEM;
1143     err = snd_ctl_add(card, kctl);
1144     if (err)
1145         return err;
1146     kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu);
1147     if (!kctl)
1148         return -ENOMEM;
1149     err = snd_ctl_add(card, kctl);
1150     if (err)
1151         return err;
1152 
1153     return 0;
1154 }
1155 
1156 #define EMU10K1X_MIDI_MODE_INPUT    (1<<0)
1157 #define EMU10K1X_MIDI_MODE_OUTPUT   (1<<1)
1158 
1159 static inline unsigned char mpu401_read(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int idx)
1160 {
1161     return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
1162 }
1163 
1164 static inline void mpu401_write(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int data, int idx)
1165 {
1166     snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
1167 }
1168 
1169 #define mpu401_write_data(emu, mpu, data)   mpu401_write(emu, mpu, data, 0)
1170 #define mpu401_write_cmd(emu, mpu, data)    mpu401_write(emu, mpu, data, 1)
1171 #define mpu401_read_data(emu, mpu)      mpu401_read(emu, mpu, 0)
1172 #define mpu401_read_stat(emu, mpu)      mpu401_read(emu, mpu, 1)
1173 
1174 #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
1175 #define mpu401_output_ready(emu,mpu)    (!(mpu401_read_stat(emu,mpu) & 0x40))
1176 
1177 #define MPU401_RESET        0xff
1178 #define MPU401_ENTER_UART   0x3f
1179 #define MPU401_ACK      0xfe
1180 
1181 static void mpu401_clear_rx(struct emu10k1x *emu, struct emu10k1x_midi *mpu)
1182 {
1183     int timeout = 100000;
1184     for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
1185         mpu401_read_data(emu, mpu);
1186 #ifdef CONFIG_SND_DEBUG
1187     if (timeout <= 0)
1188         dev_err(emu->card->dev,
1189             "cmd: clear rx timeout (status = 0x%x)\n",
1190             mpu401_read_stat(emu, mpu));
1191 #endif
1192 }
1193 
1194 /*
1195 
1196  */
1197 
1198 static void do_emu10k1x_midi_interrupt(struct emu10k1x *emu,
1199                        struct emu10k1x_midi *midi, unsigned int status)
1200 {
1201     unsigned char byte;
1202 
1203     if (midi->rmidi == NULL) {
1204         snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
1205         return;
1206     }
1207 
1208     spin_lock(&midi->input_lock);
1209     if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
1210         if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1211             mpu401_clear_rx(emu, midi);
1212         } else {
1213             byte = mpu401_read_data(emu, midi);
1214             if (midi->substream_input)
1215                 snd_rawmidi_receive(midi->substream_input, &byte, 1);
1216         }
1217     }
1218     spin_unlock(&midi->input_lock);
1219 
1220     spin_lock(&midi->output_lock);
1221     if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
1222         if (midi->substream_output &&
1223             snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1224             mpu401_write_data(emu, midi, byte);
1225         } else {
1226             snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1227         }
1228     }
1229     spin_unlock(&midi->output_lock);
1230 }
1231 
1232 static void snd_emu10k1x_midi_interrupt(struct emu10k1x *emu, unsigned int status)
1233 {
1234     do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
1235 }
1236 
1237 static int snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
1238                   struct emu10k1x_midi *midi, unsigned char cmd, int ack)
1239 {
1240     unsigned long flags;
1241     int timeout, ok;
1242 
1243     spin_lock_irqsave(&midi->input_lock, flags);
1244     mpu401_write_data(emu, midi, 0x00);
1245     /* mpu401_clear_rx(emu, midi); */
1246 
1247     mpu401_write_cmd(emu, midi, cmd);
1248     if (ack) {
1249         ok = 0;
1250         timeout = 10000;
1251         while (!ok && timeout-- > 0) {
1252             if (mpu401_input_avail(emu, midi)) {
1253                 if (mpu401_read_data(emu, midi) == MPU401_ACK)
1254                     ok = 1;
1255             }
1256         }
1257         if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1258             ok = 1;
1259     } else {
1260         ok = 1;
1261     }
1262     spin_unlock_irqrestore(&midi->input_lock, flags);
1263     if (!ok) {
1264         dev_err(emu->card->dev,
1265             "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1266                cmd, emu->port,
1267                mpu401_read_stat(emu, midi),
1268                mpu401_read_data(emu, midi));
1269         return 1;
1270     }
1271     return 0;
1272 }
1273 
1274 static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream *substream)
1275 {
1276     struct emu10k1x *emu;
1277     struct emu10k1x_midi *midi = substream->rmidi->private_data;
1278     unsigned long flags;
1279     
1280     emu = midi->emu;
1281     if (snd_BUG_ON(!emu))
1282         return -ENXIO;
1283     spin_lock_irqsave(&midi->open_lock, flags);
1284     midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
1285     midi->substream_input = substream;
1286     if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1287         spin_unlock_irqrestore(&midi->open_lock, flags);
1288         if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1289             goto error_out;
1290         if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1291             goto error_out;
1292     } else {
1293         spin_unlock_irqrestore(&midi->open_lock, flags);
1294     }
1295     return 0;
1296 
1297 error_out:
1298     return -EIO;
1299 }
1300 
1301 static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream *substream)
1302 {
1303     struct emu10k1x *emu;
1304     struct emu10k1x_midi *midi = substream->rmidi->private_data;
1305     unsigned long flags;
1306 
1307     emu = midi->emu;
1308     if (snd_BUG_ON(!emu))
1309         return -ENXIO;
1310     spin_lock_irqsave(&midi->open_lock, flags);
1311     midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
1312     midi->substream_output = substream;
1313     if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1314         spin_unlock_irqrestore(&midi->open_lock, flags);
1315         if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1316             goto error_out;
1317         if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1318             goto error_out;
1319     } else {
1320         spin_unlock_irqrestore(&midi->open_lock, flags);
1321     }
1322     return 0;
1323 
1324 error_out:
1325     return -EIO;
1326 }
1327 
1328 static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream *substream)
1329 {
1330     struct emu10k1x *emu;
1331     struct emu10k1x_midi *midi = substream->rmidi->private_data;
1332     unsigned long flags;
1333     int err = 0;
1334 
1335     emu = midi->emu;
1336     if (snd_BUG_ON(!emu))
1337         return -ENXIO;
1338     spin_lock_irqsave(&midi->open_lock, flags);
1339     snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1340     midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
1341     midi->substream_input = NULL;
1342     if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1343         spin_unlock_irqrestore(&midi->open_lock, flags);
1344         err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1345     } else {
1346         spin_unlock_irqrestore(&midi->open_lock, flags);
1347     }
1348     return err;
1349 }
1350 
1351 static int snd_emu10k1x_midi_output_close(struct snd_rawmidi_substream *substream)
1352 {
1353     struct emu10k1x *emu;
1354     struct emu10k1x_midi *midi = substream->rmidi->private_data;
1355     unsigned long flags;
1356     int err = 0;
1357 
1358     emu = midi->emu;
1359     if (snd_BUG_ON(!emu))
1360         return -ENXIO;
1361     spin_lock_irqsave(&midi->open_lock, flags);
1362     snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1363     midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
1364     midi->substream_output = NULL;
1365     if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1366         spin_unlock_irqrestore(&midi->open_lock, flags);
1367         err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1368     } else {
1369         spin_unlock_irqrestore(&midi->open_lock, flags);
1370     }
1371     return err;
1372 }
1373 
1374 static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1375 {
1376     struct emu10k1x *emu;
1377     struct emu10k1x_midi *midi = substream->rmidi->private_data;
1378     emu = midi->emu;
1379     if (snd_BUG_ON(!emu))
1380         return;
1381 
1382     if (up)
1383         snd_emu10k1x_intr_enable(emu, midi->rx_enable);
1384     else
1385         snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1386 }
1387 
1388 static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1389 {
1390     struct emu10k1x *emu;
1391     struct emu10k1x_midi *midi = substream->rmidi->private_data;
1392     unsigned long flags;
1393 
1394     emu = midi->emu;
1395     if (snd_BUG_ON(!emu))
1396         return;
1397 
1398     if (up) {
1399         int max = 4;
1400         unsigned char byte;
1401     
1402         /* try to send some amount of bytes here before interrupts */
1403         spin_lock_irqsave(&midi->output_lock, flags);
1404         while (max > 0) {
1405             if (mpu401_output_ready(emu, midi)) {
1406                 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
1407                     snd_rawmidi_transmit(substream, &byte, 1) != 1) {
1408                     /* no more data */
1409                     spin_unlock_irqrestore(&midi->output_lock, flags);
1410                     return;
1411                 }
1412                 mpu401_write_data(emu, midi, byte);
1413                 max--;
1414             } else {
1415                 break;
1416             }
1417         }
1418         spin_unlock_irqrestore(&midi->output_lock, flags);
1419         snd_emu10k1x_intr_enable(emu, midi->tx_enable);
1420     } else {
1421         snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1422     }
1423 }
1424 
1425 /*
1426 
1427  */
1428 
1429 static const struct snd_rawmidi_ops snd_emu10k1x_midi_output =
1430 {
1431     .open =     snd_emu10k1x_midi_output_open,
1432     .close =    snd_emu10k1x_midi_output_close,
1433     .trigger =  snd_emu10k1x_midi_output_trigger,
1434 };
1435 
1436 static const struct snd_rawmidi_ops snd_emu10k1x_midi_input =
1437 {
1438     .open =     snd_emu10k1x_midi_input_open,
1439     .close =    snd_emu10k1x_midi_input_close,
1440     .trigger =  snd_emu10k1x_midi_input_trigger,
1441 };
1442 
1443 static void snd_emu10k1x_midi_free(struct snd_rawmidi *rmidi)
1444 {
1445     struct emu10k1x_midi *midi = rmidi->private_data;
1446     midi->interrupt = NULL;
1447     midi->rmidi = NULL;
1448 }
1449 
1450 static int emu10k1x_midi_init(struct emu10k1x *emu,
1451                   struct emu10k1x_midi *midi, int device,
1452                   char *name)
1453 {
1454     struct snd_rawmidi *rmidi;
1455     int err;
1456 
1457     err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi);
1458     if (err < 0)
1459         return err;
1460     midi->emu = emu;
1461     spin_lock_init(&midi->open_lock);
1462     spin_lock_init(&midi->input_lock);
1463     spin_lock_init(&midi->output_lock);
1464     strcpy(rmidi->name, name);
1465     snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
1466     snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
1467     rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1468                          SNDRV_RAWMIDI_INFO_INPUT |
1469                          SNDRV_RAWMIDI_INFO_DUPLEX;
1470     rmidi->private_data = midi;
1471     rmidi->private_free = snd_emu10k1x_midi_free;
1472     midi->rmidi = rmidi;
1473     return 0;
1474 }
1475 
1476 static int snd_emu10k1x_midi(struct emu10k1x *emu)
1477 {
1478     struct emu10k1x_midi *midi = &emu->midi;
1479     int err;
1480 
1481     err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)");
1482     if (err < 0)
1483         return err;
1484 
1485     midi->tx_enable = INTE_MIDITXENABLE;
1486     midi->rx_enable = INTE_MIDIRXENABLE;
1487     midi->port = MUDATA;
1488     midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
1489     midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
1490     midi->interrupt = snd_emu10k1x_midi_interrupt;
1491     return 0;
1492 }
1493 
1494 static int __snd_emu10k1x_probe(struct pci_dev *pci,
1495                 const struct pci_device_id *pci_id)
1496 {
1497     static int dev;
1498     struct snd_card *card;
1499     struct emu10k1x *chip;
1500     int err;
1501 
1502     if (dev >= SNDRV_CARDS)
1503         return -ENODEV;
1504     if (!enable[dev]) {
1505         dev++;
1506         return -ENOENT;
1507     }
1508 
1509     err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1510                 sizeof(*chip), &card);
1511     if (err < 0)
1512         return err;
1513     chip = card->private_data;
1514 
1515     err = snd_emu10k1x_create(card, pci);
1516     if (err < 0)
1517         return err;
1518 
1519     err = snd_emu10k1x_pcm(chip, 0);
1520     if (err < 0)
1521         return err;
1522     err = snd_emu10k1x_pcm(chip, 1);
1523     if (err < 0)
1524         return err;
1525     err = snd_emu10k1x_pcm(chip, 2);
1526     if (err < 0)
1527         return err;
1528 
1529     err = snd_emu10k1x_ac97(chip);
1530     if (err < 0)
1531         return err;
1532 
1533     err = snd_emu10k1x_mixer(chip);
1534     if (err < 0)
1535         return err;
1536     
1537     err = snd_emu10k1x_midi(chip);
1538     if (err < 0)
1539         return err;
1540 
1541     snd_emu10k1x_proc_init(chip);
1542 
1543     strcpy(card->driver, "EMU10K1X");
1544     strcpy(card->shortname, "Dell Sound Blaster Live!");
1545     sprintf(card->longname, "%s at 0x%lx irq %i",
1546         card->shortname, chip->port, chip->irq);
1547 
1548     err = snd_card_register(card);
1549     if (err < 0)
1550         return err;
1551 
1552     pci_set_drvdata(pci, card);
1553     dev++;
1554     return 0;
1555 }
1556 
1557 static int snd_emu10k1x_probe(struct pci_dev *pci,
1558                   const struct pci_device_id *pci_id)
1559 {
1560     return snd_card_free_on_error(&pci->dev, __snd_emu10k1x_probe(pci, pci_id));
1561 }
1562 
1563 // PCI IDs
1564 static const struct pci_device_id snd_emu10k1x_ids[] = {
1565     { PCI_VDEVICE(CREATIVE, 0x0006), 0 },   /* Dell OEM version (EMU10K1) */
1566     { 0, }
1567 };
1568 MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
1569 
1570 // pci_driver definition
1571 static struct pci_driver emu10k1x_driver = {
1572     .name = KBUILD_MODNAME,
1573     .id_table = snd_emu10k1x_ids,
1574     .probe = snd_emu10k1x_probe,
1575 };
1576 
1577 module_pci_driver(emu10k1x_driver);