Ignore:
Timestamp:
Oct 23, 2006, 11:07:11 PM (19 years ago)
Author:
vladest
Message:

SB code update
HDA code update
Some other updates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GPL/trunk/alsa-kernel/pci/emu10k1/emu10k1x.c

    r34 r84  
    6767
    6868#define PTR                     0x00            /* Indexed register set pointer register        */
    69                                                 /* NOTE: The CHANNELNUM and ADDRESS words can   */
    70                                                 /* be modified independently of each other.     */
     69/* NOTE: The CHANNELNUM and ADDRESS words can   */
     70/* be modified independently of each other.     */
    7171
    7272#define DATA                    0x04            /* Indexed register set data register           */
    7373
    7474#define IPR                     0x08            /* Global interrupt pending register            */
    75                                                 /* Clear pending interrupts by writing a 1 to   */
    76                                                 /* the relevant bits and zero to the other bits */
     75/* Clear pending interrupts by writing a 1 to   */
     76/* the relevant bits and zero to the other bits */
    7777#define IPR_MIDITRANSBUFEMPTY   0x00000001      /* MIDI UART transmit buffer empty              */
    7878#define IPR_MIDIRECVBUFEMPTY    0x00000002      /* MIDI UART receive buffer empty               */
     
    9393
    9494#define HCFG_LOCKSOUNDCACHE     0x00000008      /* 1 = Cancel bustmaster accesses to soundcache */
    95                                                 /* NOTE: This should generally never be used.   */
     95/* NOTE: This should generally never be used.   */
    9696#define HCFG_AUDIOENABLE        0x00000001      /* 0 = CODECs transmit zero-valued samples      */
    97                                                 /* Should be set to 1 when the EMU10K1 is       */
    98                                                 /* completely initialized.                      */
     97/* Should be set to 1 when the EMU10K1 is       */
     98/* completely initialized.                      */
    9999#define GPIO                    0x18            /* Defaults: 00001080-Analog, 00001000-SPDIF.   */
    100100
     
    108108/********************************************************************************************************/
    109109#define PLAYBACK_LIST_ADDR      0x00            /* Base DMA address of a list of pointers to each period/size */
    110                                                 /* One list entry: 4 bytes for DMA address,
    111                                                 * 4 bytes for period_size << 16.
    112                                                 * One list entry is 8 bytes long.
    113                                                 * One list entry for each period in the buffer.
    114                                                 */
     110/* One list entry: 4 bytes for DMA address,
     111 * 4 bytes for period_size << 16.
     112 * One list entry is 8 bytes long.
     113 * One list entry for each period in the buffer.
     114 */
    115115#define PLAYBACK_LIST_SIZE      0x01            /* Size of list in bytes << 16. E.g. 8 periods -> 0x00380000  */
    116116#define PLAYBACK_LIST_PTR       0x02            /* Pointer to the current period being played */
     
    187187 *  - channel 2 is the center/lfe chanel
    188188 * Volume is controlled by the AC97 for the front and rear channels by
    189  * the PCM Playback Volume, Sigmatel Surround Playback Volume and 
     189 * the PCM Playback Volume, Sigmatel Surround Playback Volume and
    190190 * Surround Playback Volume. The Sigmatel 4-Speaker Stereo switch affects
    191191 * the front/rear channel mixing in the REAR OUT jack. When using the
     
    197197
    198198struct emu10k1x_voice {
    199         struct emu10k1x *emu;
    200         int number;
    201         int use;
    202  
    203         struct emu10k1x_pcm *epcm;
     199    struct emu10k1x *emu;
     200    int number;
     201    int use;
     202
     203    struct emu10k1x_pcm *epcm;
    204204};
    205205
    206206struct emu10k1x_pcm {
    207         struct emu10k1x *emu;
    208         struct snd_pcm_substream *substream;
    209         struct emu10k1x_voice *voice;
    210         unsigned short running;
     207    struct emu10k1x *emu;
     208    struct snd_pcm_substream *substream;
     209    struct emu10k1x_voice *voice;
     210    unsigned short running;
    211211};
    212212
    213213struct emu10k1x_midi {
    214         struct emu10k1x *emu;
    215         struct snd_rawmidi *rmidi;
    216         struct snd_rawmidi_substream *substream_input;
    217         struct snd_rawmidi_substream *substream_output;
    218         unsigned int midi_mode;
    219         spinlock_t input_lock;
    220         spinlock_t output_lock;
    221         spinlock_t open_lock;
    222         int tx_enable, rx_enable;
    223         int port;
    224         int ipr_tx, ipr_rx;
    225         void (*interrupt)(struct emu10k1x *emu, unsigned int status);
     214    struct emu10k1x *emu;
     215    struct snd_rawmidi *rmidi;
     216    struct snd_rawmidi_substream *substream_input;
     217    struct snd_rawmidi_substream *substream_output;
     218    unsigned int midi_mode;
     219    spinlock_t input_lock;
     220    spinlock_t output_lock;
     221    spinlock_t open_lock;
     222    int tx_enable, rx_enable;
     223    int port;
     224    int ipr_tx, ipr_rx;
     225    void (*interrupt)(struct emu10k1x *emu, unsigned int status);
    226226};
    227227
    228228// definition of the chip-specific record
    229229struct emu10k1x {
    230         struct snd_card *card;
    231         struct pci_dev *pci;
    232 
    233         unsigned long port;
    234         struct resource *res_port;
    235         int irq;
    236 
    237         unsigned int revision;          /* chip revision */
    238         unsigned int serial;            /* serial number */
    239         unsigned short model;           /* subsystem id */
    240 
    241         spinlock_t emu_lock;
    242         spinlock_t voice_lock;
    243 
    244         struct snd_ac97 *ac97;
    245         struct snd_pcm *pcm;
    246 
    247         struct emu10k1x_voice voices[3];
    248         struct emu10k1x_voice capture_voice;
    249         u32 spdif_bits[3]; // SPDIF out setup
    250 
    251         struct snd_dma_buffer dma_buffer;
    252 
    253         struct emu10k1x_midi midi;
     230    struct snd_card *card;
     231    struct pci_dev *pci;
     232
     233    unsigned long port;
     234    struct resource *res_port;
     235    int irq;
     236
     237    unsigned int revision;              /* chip revision */
     238    unsigned int serial;            /* serial number */
     239    unsigned short model;               /* subsystem id */
     240
     241    spinlock_t emu_lock;
     242    spinlock_t voice_lock;
     243
     244    struct snd_ac97 *ac97;
     245    struct snd_pcm *pcm;
     246
     247    struct emu10k1x_voice voices[3];
     248    struct emu10k1x_voice capture_voice;
     249    u32 spdif_bits[3]; // SPDIF out setup
     250
     251    struct snd_dma_buffer dma_buffer;
     252
     253    struct emu10k1x_midi midi;
    254254};
    255255
    256256/* hardware definition */
    257257static struct snd_pcm_hardware snd_emu10k1x_playback_hw = {
    258         .info =                 (SNDRV_PCM_INFO_MMAP |
    259                                 SNDRV_PCM_INFO_INTERLEAVED |
    260                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
    261                                 SNDRV_PCM_INFO_MMAP_VALID),
    262         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
    263         .rates =                SNDRV_PCM_RATE_48000,
    264         .rate_min =             48000,
    265         .rate_max =             48000,
    266         .channels_min =         2,
    267         .channels_max =         2,
    268         .buffer_bytes_max =     (32*1024),
    269         .period_bytes_min =     64,
    270         .period_bytes_max =     (16*1024),
    271         .periods_min =          2,
    272         .periods_max =          8,
    273         .fifo_size =            0,
     258    .info =                     (SNDRV_PCM_INFO_MMAP |
     259                                SNDRV_PCM_INFO_INTERLEAVED |
     260                                SNDRV_PCM_INFO_BLOCK_TRANSFER |
     261                                SNDRV_PCM_INFO_MMAP_VALID),
     262                                 .formats =             SNDRV_PCM_FMTBIT_S16_LE,
     263                                 .rates =               SNDRV_PCM_RATE_48000,
     264                                 .rate_min =            48000,
     265                                 .rate_max =            48000,
     266                                 .channels_min =                2,
     267                                 .channels_max =                2,
     268                                 .buffer_bytes_max =    (32*1024),
     269                                 .period_bytes_min =    64,
     270                                 .period_bytes_max =    (16*1024),
     271                                 .periods_min =         2,
     272                                 .periods_max =         8,
     273                                 .fifo_size =           0,
    274274};
    275275
    276276static struct snd_pcm_hardware snd_emu10k1x_capture_hw = {
    277         .info =                 (SNDRV_PCM_INFO_MMAP |
    278                                 SNDRV_PCM_INFO_INTERLEAVED |
    279                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
    280                                 SNDRV_PCM_INFO_MMAP_VALID),
    281         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
    282         .rates =                SNDRV_PCM_RATE_48000,
    283         .rate_min =             48000,
    284         .rate_max =             48000,
    285         .channels_min =         2,
    286         .channels_max =         2,
    287         .buffer_bytes_max =     (32*1024),
    288         .period_bytes_min =     64,
    289         .period_bytes_max =     (16*1024),
    290         .periods_min =          2,
    291         .periods_max =          2,
    292         .fifo_size =            0,
     277    .info =                     (SNDRV_PCM_INFO_MMAP |
     278                                SNDRV_PCM_INFO_INTERLEAVED |
     279                                SNDRV_PCM_INFO_BLOCK_TRANSFER |
     280                                SNDRV_PCM_INFO_MMAP_VALID),
     281                                 .formats =             SNDRV_PCM_FMTBIT_S16_LE,
     282                                 .rates =               SNDRV_PCM_RATE_48000,
     283                                 .rate_min =            48000,
     284                                 .rate_max =            48000,
     285                                 .channels_min =                2,
     286                                 .channels_max =                2,
     287                                 .buffer_bytes_max =    (32*1024),
     288                                 .period_bytes_min =    64,
     289                                 .period_bytes_max =    (16*1024),
     290                                 .periods_min =         2,
     291                                 .periods_max =         2,
     292                                 .fifo_size =           0,
    293293};
    294294
    295 static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x * emu, 
    296                                           unsigned int reg,
    297                                           unsigned int chn)
    298 {
    299         unsigned long flags;
    300         unsigned int regptr, val;
    301  
    302         regptr = (reg << 16) | chn;
    303 
    304         spin_lock_irqsave(&emu->emu_lock, flags);
    305         outl(regptr, emu->port + PTR);
    306         val = inl(emu->port + DATA);
    307         spin_unlock_irqrestore(&emu->emu_lock, flags);
    308         return val;
    309 }
    310 
    311 static void snd_emu10k1x_ptr_write(struct emu10k1x *emu, 
    312                                    unsigned int reg,
    313                                    unsigned int chn,
    314                                    unsigned int data)
    315 {
    316         unsigned int regptr;
    317         unsigned long flags;
    318 
    319         regptr = (reg << 16) | chn;
    320 
    321         spin_lock_irqsave(&emu->emu_lock, flags);
    322         outl(regptr, emu->port + PTR);
    323         outl(data, emu->port + DATA);
    324         spin_unlock_irqrestore(&emu->emu_lock, flags);
     295static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x * emu,
     296                                          unsigned int reg,
     297                                          unsigned int chn)
     298{
     299    unsigned long flags;
     300    unsigned int regptr, val;
     301
     302    regptr = (reg << 16) | chn;
     303
     304    spin_lock_irqsave(&emu->emu_lock, flags);
     305    outl(regptr, emu->port + PTR);
     306    val = inl(emu->port + DATA);
     307    spin_unlock_irqrestore(&emu->emu_lock, flags);
     308    return val;
     309}
     310
     311static void snd_emu10k1x_ptr_write(struct emu10k1x *emu,
     312                                   unsigned int reg,
     313                                   unsigned int chn,
     314                                   unsigned int data)
     315{
     316    unsigned int regptr;
     317    unsigned long flags;
     318
     319    regptr = (reg << 16) | chn;
     320
     321    spin_lock_irqsave(&emu->emu_lock, flags);
     322    outl(regptr, emu->port + PTR);
     323    outl(data, emu->port + DATA);
     324    spin_unlock_irqrestore(&emu->emu_lock, flags);
    325325}
    326326
    327327static void snd_emu10k1x_intr_enable(struct emu10k1x *emu, unsigned int intrenb)
    328328{
    329         unsigned long flags;
    330         unsigned int enable;
    331  
    332         spin_lock_irqsave(&emu->emu_lock, flags);
    333         enable = inl(emu->port + INTE) | intrenb;
    334         outl(enable, emu->port + INTE);
    335         spin_unlock_irqrestore(&emu->emu_lock, flags);
     329    unsigned long flags;
     330    unsigned int enable;
     331
     332    spin_lock_irqsave(&emu->emu_lock, flags);
     333    enable = inl(emu->port + INTE) | intrenb;
     334    outl(enable, emu->port + INTE);
     335    spin_unlock_irqrestore(&emu->emu_lock, flags);
    336336}
    337337
    338338static void snd_emu10k1x_intr_disable(struct emu10k1x *emu, unsigned int intrenb)
    339339{
    340         unsigned long flags;
    341         unsigned int enable;
    342  
    343         spin_lock_irqsave(&emu->emu_lock, flags);
    344         enable = inl(emu->port + INTE) & ~intrenb;
    345         outl(enable, emu->port + INTE);
    346         spin_unlock_irqrestore(&emu->emu_lock, flags);
     340    unsigned long flags;
     341    unsigned int enable;
     342
     343    spin_lock_irqsave(&emu->emu_lock, flags);
     344    enable = inl(emu->port + INTE) & ~intrenb;
     345    outl(enable, emu->port + INTE);
     346    spin_unlock_irqrestore(&emu->emu_lock, flags);
    347347}
    348348
    349349static void snd_emu10k1x_gpio_write(struct emu10k1x *emu, unsigned int value)
    350350{
    351         unsigned long flags;
    352 
    353         spin_lock_irqsave(&emu->emu_lock, flags);
    354         outl(value, emu->port + GPIO);
    355         spin_unlock_irqrestore(&emu->emu_lock, flags);
     351    unsigned long flags;
     352
     353    spin_lock_irqsave(&emu->emu_lock, flags);
     354    outl(value, emu->port + GPIO);
     355    spin_unlock_irqrestore(&emu->emu_lock, flags);
    356356}
    357357
    358358static void snd_emu10k1x_pcm_free_substream(struct snd_pcm_runtime *runtime)
    359359{
    360         kfree(runtime->private_data);
     360    kfree(runtime->private_data);
    361361}
    362362
    363363static void snd_emu10k1x_pcm_interrupt(struct emu10k1x *emu, struct emu10k1x_voice *voice)
    364364{
    365         struct emu10k1x_pcm *epcm;
    366 
    367         if ((epcm = voice->epcm) == NULL)
    368                 return;
    369         if (epcm->substream == NULL)
    370                 return;
     365    struct emu10k1x_pcm *epcm;
     366
     367    if ((epcm = voice->epcm) == NULL)
     368        return;
     369    if (epcm->substream == NULL)
     370        return;
    371371#if 0
    372         snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
    373                    epcm->substream->ops->pointer(epcm->substream),
    374                    snd_pcm_lib_period_bytes(epcm->substream),
    375                    snd_pcm_lib_buffer_bytes(epcm->substream));
     372    snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
     373               epcm->substream->ops->pointer(epcm->substream),
     374               snd_pcm_lib_period_bytes(epcm->substream),
     375               snd_pcm_lib_buffer_bytes(epcm->substream));
    376376#endif
    377         snd_pcm_period_elapsed(epcm->substream);
     377    snd_pcm_period_elapsed(epcm->substream);
    378378}
    379379
     
    381381static int snd_emu10k1x_playback_open(struct snd_pcm_substream *substream)
    382382{
    383         struct emu10k1x *chip = snd_pcm_substream_chip(substream);
    384         struct emu10k1x_pcm *epcm;
    385         struct snd_pcm_runtime *runtime = substream->runtime;
    386         int err;
    387 
    388         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
    389                 return err;
    390         }
    391         if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
    392                 return err;
    393 
    394         epcm = (struct emu10k1x_pcm *)kzalloc(sizeof(*epcm), GFP_KERNEL);
    395         if (epcm == NULL)
    396                 return -ENOMEM;
    397         epcm->emu = chip;
    398         epcm->substream = substream;
    399  
    400         runtime->private_data = epcm;
    401         runtime->private_free = snd_emu10k1x_pcm_free_substream;
    402  
    403         runtime->hw = snd_emu10k1x_playback_hw;
    404 
    405         return 0;
     383    struct emu10k1x *chip = snd_pcm_substream_chip(substream);
     384    struct emu10k1x_pcm *epcm;
     385    struct snd_pcm_runtime *runtime = substream->runtime;
     386    int err;
     387
     388    if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
     389        return err;
     390    }
     391    if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
     392        return err;
     393
     394    epcm = (struct emu10k1x_pcm *)kzalloc(sizeof(*epcm), GFP_KERNEL);
     395    if (epcm == NULL)
     396        return -ENOMEM;
     397    epcm->emu = chip;
     398    epcm->substream = substream;
     399
     400    runtime->private_data = epcm;
     401    runtime->private_free = snd_emu10k1x_pcm_free_substream;
     402
     403    runtime->hw = snd_emu10k1x_playback_hw;
     404
     405    return 0;
    406406}
    407407
     
    409409static int snd_emu10k1x_playback_close(struct snd_pcm_substream *substream)
    410410{
    411         return 0;
     411    return 0;
    412412}
    413413
    414414/* hw_params callback */
    415415static int snd_emu10k1x_pcm_hw_params(struct snd_pcm_substream *substream,
    416                                       struct snd_pcm_hw_params *hw_params)
    417 {
    418         struct snd_pcm_runtime *runtime = substream->runtime;
    419         struct emu10k1x_pcm *epcm = runtime->private_data;
    420 
    421         if (! epcm->voice) {
    422                 epcm->voice = &epcm->emu->voices[substream->pcm->device];
    423                 epcm->voice->use = 1;
    424                 epcm->voice->epcm = epcm;
    425         }
    426 
    427         return snd_pcm_lib_malloc_pages(substream,
    428                                         params_buffer_bytes(hw_params));
     416                                      struct snd_pcm_hw_params *hw_params)
     417{
     418    struct snd_pcm_runtime *runtime = substream->runtime;
     419    struct emu10k1x_pcm *epcm = runtime->private_data;
     420
     421    if (! epcm->voice) {
     422        epcm->voice = &epcm->emu->voices[substream->pcm->device];
     423        epcm->voice->use = 1;
     424        epcm->voice->epcm = epcm;
     425    }
     426
     427    return snd_pcm_lib_malloc_pages(substream,
     428                                    params_buffer_bytes(hw_params));
    429429}
    430430
     
    432432static int snd_emu10k1x_pcm_hw_free(struct snd_pcm_substream *substream)
    433433{
    434         struct snd_pcm_runtime *runtime = substream->runtime;
    435         struct emu10k1x_pcm *epcm;
    436 
    437         if (runtime->private_data == NULL)
    438                 return 0;
    439        
    440         epcm = runtime->private_data;
    441 
    442         if (epcm->voice) {
    443                 epcm->voice->use = 0;
    444                 epcm->voice->epcm = NULL;
    445                 epcm->voice = NULL;
    446         }
    447 
    448         return snd_pcm_lib_free_pages(substream);
     434    struct snd_pcm_runtime *runtime = substream->runtime;
     435    struct emu10k1x_pcm *epcm;
     436
     437    if (runtime->private_data == NULL)
     438        return 0;
     439
     440    epcm = runtime->private_data;
     441
     442    if (epcm->voice) {
     443        epcm->voice->use = 0;
     444        epcm->voice->epcm = NULL;
     445        epcm->voice = NULL;
     446    }
     447
     448    return snd_pcm_lib_free_pages(substream);
    449449}
    450450
     
    452452static int snd_emu10k1x_pcm_prepare(struct snd_pcm_substream *substream)
    453453{
    454         struct emu10k1x *emu = snd_pcm_substream_chip(substream);
    455         struct snd_pcm_runtime *runtime = substream->runtime;
    456         struct emu10k1x_pcm *epcm = runtime->private_data;
    457         int voice = epcm->voice->number;
    458         u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
    459         u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
    460         int i;
    461        
    462         for(i=0; i < runtime->periods; i++) {
    463                 *table_base++=runtime->dma_addr+(i*period_size_bytes);
    464                 *table_base++=period_size_bytes<<16;
    465         }
    466 
    467         snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
    468         snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
    469         snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
    470         snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
    471         snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
    472         snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
    473         snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
    474 
    475         snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
    476 
    477         return 0;
     454    struct emu10k1x *emu = snd_pcm_substream_chip(substream);
     455    struct snd_pcm_runtime *runtime = substream->runtime;
     456    struct emu10k1x_pcm *epcm = runtime->private_data;
     457    int voice = epcm->voice->number;
     458    u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
     459    u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
     460    int i;
     461
     462    for(i=0; i < runtime->periods; i++) {
     463        *table_base++=runtime->dma_addr+(i*period_size_bytes);
     464        *table_base++=period_size_bytes<<16;
     465    }
     466
     467    snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
     468    snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
     469    snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
     470    snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
     471    snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
     472    snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
     473    snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
     474
     475    snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
     476
     477    return 0;
    478478}
    479479
    480480/* trigger callback */
    481481static int snd_emu10k1x_pcm_trigger(struct snd_pcm_substream *substream,
    482                                     int cmd)
    483 {
    484         struct emu10k1x *emu = snd_pcm_substream_chip(substream);
    485         struct snd_pcm_runtime *runtime = substream->runtime;
    486         struct emu10k1x_pcm *epcm = runtime->private_data;
    487         int channel = epcm->voice->number;
    488         int result = 0;
    489 
    490 //      snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
    491 
    492         switch (cmd) {
    493         case SNDRV_PCM_TRIGGER_START:
    494                 if(runtime->periods == 2)
    495                         snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
    496                 else
    497                         snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
    498                 epcm->running = 1;
    499                 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
    500                 break;
    501         case SNDRV_PCM_TRIGGER_STOP:
    502                 epcm->running = 0;
    503                 snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
    504                 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
    505                 break;
    506         default:
    507                 result = -EINVAL;
    508                 break;
    509         }
    510         return result;
     482                                    int cmd)
     483{
     484    struct emu10k1x *emu = snd_pcm_substream_chip(substream);
     485    struct snd_pcm_runtime *runtime = substream->runtime;
     486    struct emu10k1x_pcm *epcm = runtime->private_data;
     487    int channel = epcm->voice->number;
     488    int result = 0;
     489
     490    //  snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
     491
     492    switch (cmd) {
     493    case SNDRV_PCM_TRIGGER_START:
     494        if(runtime->periods == 2)
     495            snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
     496        else
     497            snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
     498        epcm->running = 1;
     499        snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
     500        break;
     501    case SNDRV_PCM_TRIGGER_STOP:
     502        epcm->running = 0;
     503        snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
     504        snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
     505        break;
     506    default:
     507        result = -EINVAL;
     508        break;
     509    }
     510    return result;
    511511}
    512512
     
    515515snd_emu10k1x_pcm_pointer(struct snd_pcm_substream *substream)
    516516{
    517         struct emu10k1x *emu = snd_pcm_substream_chip(substream);
    518         struct snd_pcm_runtime *runtime = substream->runtime;
    519         struct emu10k1x_pcm *epcm = runtime->private_data;
    520         int channel = epcm->voice->number;
    521         snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
    522 
    523         if (!epcm->running)
    524                 return 0;
    525 
    526         ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
    527         ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
    528         ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
    529 
    530         if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
    531                 return 0;
    532        
    533         if (ptr3 != ptr4)
    534                 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
    535         ptr2 = bytes_to_frames(runtime, ptr1);
    536         ptr2 += (ptr4 >> 3) * runtime->period_size;
    537         ptr = ptr2;
    538 
    539         if (ptr >= runtime->buffer_size)
    540                 ptr -= runtime->buffer_size;
    541 
    542         return ptr;
     517    struct emu10k1x *emu = snd_pcm_substream_chip(substream);
     518    struct snd_pcm_runtime *runtime = substream->runtime;
     519    struct emu10k1x_pcm *epcm = runtime->private_data;
     520    int channel = epcm->voice->number;
     521    snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
     522
     523    if (!epcm->running)
     524        return 0;
     525
     526    ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
     527    ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
     528    ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
     529
     530    if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
     531        return 0;
     532
     533    if (ptr3 != ptr4)
     534        ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
     535    ptr2 = bytes_to_frames(runtime, ptr1);
     536    ptr2 += (ptr4 >> 3) * runtime->period_size;
     537    ptr = ptr2;
     538
     539    if (ptr >= runtime->buffer_size)
     540        ptr -= runtime->buffer_size;
     541
     542    return ptr;
    543543}
    544544
    545545/* operators */
    546546static struct snd_pcm_ops snd_emu10k1x_playback_ops = {
    547         .open =        snd_emu10k1x_playback_open,
    548         .close =       snd_emu10k1x_playback_close,
    549         .ioctl =       snd_pcm_lib_ioctl,
    550         .hw_params =   snd_emu10k1x_pcm_hw_params,
    551         .hw_free =     snd_emu10k1x_pcm_hw_free,
    552         .prepare =     snd_emu10k1x_pcm_prepare,
    553         .trigger =     snd_emu10k1x_pcm_trigger,
    554         .pointer =     snd_emu10k1x_pcm_pointer,
     547    .open =        snd_emu10k1x_playback_open,
     548    .close =       snd_emu10k1x_playback_close,
     549    .ioctl =       snd_pcm_lib_ioctl,
     550    .hw_params =   snd_emu10k1x_pcm_hw_params,
     551    .hw_free =     snd_emu10k1x_pcm_hw_free,
     552    .prepare =     snd_emu10k1x_pcm_prepare,
     553    .trigger =     snd_emu10k1x_pcm_trigger,
     554    .pointer =     snd_emu10k1x_pcm_pointer,
    555555};
    556556
     
    558558static int snd_emu10k1x_pcm_open_capture(struct snd_pcm_substream *substream)
    559559{
    560         struct emu10k1x *chip = snd_pcm_substream_chip(substream);
    561         struct emu10k1x_pcm *epcm;
    562         struct snd_pcm_runtime *runtime = substream->runtime;
    563         int err;
    564 
    565         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
    566                 return err;
    567         if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
    568                 return err;
    569 
    570         epcm = (struct emu10k1x_pcm *)kzalloc(sizeof(*epcm), GFP_KERNEL);
    571         if (epcm == NULL)
    572                 return -ENOMEM;
    573 
    574         epcm->emu = chip;
    575         epcm->substream = substream;
    576 
    577         runtime->private_data = epcm;
    578         runtime->private_free = snd_emu10k1x_pcm_free_substream;
    579 
    580         runtime->hw = snd_emu10k1x_capture_hw;
    581 
    582         return 0;
     560    struct emu10k1x *chip = snd_pcm_substream_chip(substream);
     561    struct emu10k1x_pcm *epcm;
     562    struct snd_pcm_runtime *runtime = substream->runtime;
     563    int err;
     564
     565    if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
     566        return err;
     567    if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
     568        return err;
     569
     570    epcm = (struct emu10k1x_pcm *)kzalloc(sizeof(*epcm), GFP_KERNEL);
     571    if (epcm == NULL)
     572        return -ENOMEM;
     573
     574    epcm->emu = chip;
     575    epcm->substream = substream;
     576
     577    runtime->private_data = epcm;
     578    runtime->private_free = snd_emu10k1x_pcm_free_substream;
     579
     580    runtime->hw = snd_emu10k1x_capture_hw;
     581
     582    return 0;
    583583}
    584584
     
    586586static int snd_emu10k1x_pcm_close_capture(struct snd_pcm_substream *substream)
    587587{
    588         return 0;
     588    return 0;
    589589}
    590590
    591591/* hw_params callback */
    592592static int snd_emu10k1x_pcm_hw_params_capture(struct snd_pcm_substream *substream,
    593                                               struct snd_pcm_hw_params *hw_params)
    594 {
    595         struct snd_pcm_runtime *runtime = substream->runtime;
    596         struct emu10k1x_pcm *epcm = runtime->private_data;
    597 
    598         if (! epcm->voice) {
    599                 if (epcm->emu->capture_voice.use)
    600                         return -EBUSY;
    601                 epcm->voice = &epcm->emu->capture_voice;
    602                 epcm->voice->epcm = epcm;
    603                 epcm->voice->use = 1;
    604         }
    605 
    606         return snd_pcm_lib_malloc_pages(substream,
    607                                         params_buffer_bytes(hw_params));
     593                                              struct snd_pcm_hw_params *hw_params)
     594{
     595    struct snd_pcm_runtime *runtime = substream->runtime;
     596    struct emu10k1x_pcm *epcm = runtime->private_data;
     597
     598    if (! epcm->voice) {
     599        if (epcm->emu->capture_voice.use)
     600            return -EBUSY;
     601        epcm->voice = &epcm->emu->capture_voice;
     602        epcm->voice->epcm = epcm;
     603        epcm->voice->use = 1;
     604    }
     605
     606    return snd_pcm_lib_malloc_pages(substream,
     607                                    params_buffer_bytes(hw_params));
    608608}
    609609
     
    611611static int snd_emu10k1x_pcm_hw_free_capture(struct snd_pcm_substream *substream)
    612612{
    613         struct snd_pcm_runtime *runtime = substream->runtime;
    614 
    615         struct emu10k1x_pcm *epcm;
    616 
    617         if (runtime->private_data == NULL)
    618                 return 0;
    619         epcm = runtime->private_data;
    620 
    621         if (epcm->voice) {
    622                 epcm->voice->use = 0;
    623                 epcm->voice->epcm = NULL;
    624                 epcm->voice = NULL;
    625         }
    626 
    627         return snd_pcm_lib_free_pages(substream);
     613    struct snd_pcm_runtime *runtime = substream->runtime;
     614
     615    struct emu10k1x_pcm *epcm;
     616
     617    if (runtime->private_data == NULL)
     618        return 0;
     619    epcm = runtime->private_data;
     620
     621    if (epcm->voice) {
     622        epcm->voice->use = 0;
     623        epcm->voice->epcm = NULL;
     624        epcm->voice = NULL;
     625    }
     626
     627    return snd_pcm_lib_free_pages(substream);
    628628}
    629629
     
    631631static int snd_emu10k1x_pcm_prepare_capture(struct snd_pcm_substream *substream)
    632632{
    633         struct emu10k1x *emu = snd_pcm_substream_chip(substream);
    634         struct snd_pcm_runtime *runtime = substream->runtime;
    635 
    636         snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
    637         snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
    638         snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
    639         snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
    640 
    641         return 0;
     633    struct emu10k1x *emu = snd_pcm_substream_chip(substream);
     634    struct snd_pcm_runtime *runtime = substream->runtime;
     635
     636    snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
     637    snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
     638    snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
     639    snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
     640
     641    return 0;
    642642}
    643643
    644644/* trigger_capture callback */
    645645static int snd_emu10k1x_pcm_trigger_capture(struct snd_pcm_substream *substream,
    646                                             int cmd)
    647 {
    648         struct emu10k1x *emu = snd_pcm_substream_chip(substream);
    649         struct snd_pcm_runtime *runtime = substream->runtime;
    650         struct emu10k1x_pcm *epcm = runtime->private_data;
    651         int result = 0;
    652 
    653         switch (cmd) {
    654         case SNDRV_PCM_TRIGGER_START:
    655                 snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
    656                                         INTE_CAP_0_HALF_LOOP);
    657                 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
    658                 epcm->running = 1;
    659                 break;
    660         case SNDRV_PCM_TRIGGER_STOP:
    661                 epcm->running = 0;
    662                 snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
    663                                           INTE_CAP_0_HALF_LOOP);
    664                 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
    665                 break;
    666         default:
    667                 result = -EINVAL;
    668                 break;
    669         }
    670         return result;
     646                                            int cmd)
     647{
     648    struct emu10k1x *emu = snd_pcm_substream_chip(substream);
     649    struct snd_pcm_runtime *runtime = substream->runtime;
     650    struct emu10k1x_pcm *epcm = runtime->private_data;
     651    int result = 0;
     652
     653    switch (cmd) {
     654    case SNDRV_PCM_TRIGGER_START:
     655        snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
     656                                INTE_CAP_0_HALF_LOOP);
     657        snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
     658        epcm->running = 1;
     659        break;
     660    case SNDRV_PCM_TRIGGER_STOP:
     661        epcm->running = 0;
     662        snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
     663                                  INTE_CAP_0_HALF_LOOP);
     664        snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
     665        break;
     666    default:
     667        result = -EINVAL;
     668        break;
     669    }
     670    return result;
    671671}
    672672
     
    675675snd_emu10k1x_pcm_pointer_capture(struct snd_pcm_substream *substream)
    676676{
    677         struct emu10k1x *emu = snd_pcm_substream_chip(substream);
    678         struct snd_pcm_runtime *runtime = substream->runtime;
    679         struct emu10k1x_pcm *epcm = runtime->private_data;
    680         snd_pcm_uframes_t ptr;
    681 
    682         if (!epcm->running)
    683                 return 0;
    684 
    685         ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
    686         if (ptr >= runtime->buffer_size)
    687                 ptr -= runtime->buffer_size;
    688 
    689         return ptr;
     677    struct emu10k1x *emu = snd_pcm_substream_chip(substream);
     678    struct snd_pcm_runtime *runtime = substream->runtime;
     679    struct emu10k1x_pcm *epcm = runtime->private_data;
     680    snd_pcm_uframes_t ptr;
     681
     682    if (!epcm->running)
     683        return 0;
     684
     685    ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
     686    if (ptr >= runtime->buffer_size)
     687        ptr -= runtime->buffer_size;
     688
     689    return ptr;
    690690}
    691691
    692692static struct snd_pcm_ops snd_emu10k1x_capture_ops = {
    693         .open =        snd_emu10k1x_pcm_open_capture,
    694         .close =       snd_emu10k1x_pcm_close_capture,
    695         .ioctl =       snd_pcm_lib_ioctl,
    696         .hw_params =   snd_emu10k1x_pcm_hw_params_capture,
    697         .hw_free =     snd_emu10k1x_pcm_hw_free_capture,
    698         .prepare =     snd_emu10k1x_pcm_prepare_capture,
    699         .trigger =     snd_emu10k1x_pcm_trigger_capture,
    700         .pointer =     snd_emu10k1x_pcm_pointer_capture,
     693    .open =        snd_emu10k1x_pcm_open_capture,
     694    .close =       snd_emu10k1x_pcm_close_capture,
     695    .ioctl =       snd_pcm_lib_ioctl,
     696    .hw_params =   snd_emu10k1x_pcm_hw_params_capture,
     697    .hw_free =     snd_emu10k1x_pcm_hw_free_capture,
     698    .prepare =     snd_emu10k1x_pcm_prepare_capture,
     699    .trigger =     snd_emu10k1x_pcm_trigger_capture,
     700    .pointer =     snd_emu10k1x_pcm_pointer_capture,
    701701};
    702702
    703703static unsigned short snd_emu10k1x_ac97_read(struct snd_ac97 *ac97,
    704                                              unsigned short reg)
    705 {
    706         struct emu10k1x *emu = ac97->private_data;
    707         unsigned long flags;
    708         unsigned short val;
    709  
    710         spin_lock_irqsave(&emu->emu_lock, flags);
    711         outb(reg, emu->port + AC97ADDRESS);
    712         val = inw(emu->port + AC97DATA);
    713         spin_unlock_irqrestore(&emu->emu_lock, flags);
    714         return val;
     704                                             unsigned short reg)
     705{
     706    struct emu10k1x *emu = ac97->private_data;
     707    unsigned long flags;
     708    unsigned short val;
     709
     710    spin_lock_irqsave(&emu->emu_lock, flags);
     711    outb(reg, emu->port + AC97ADDRESS);
     712    val = inw(emu->port + AC97DATA);
     713    spin_unlock_irqrestore(&emu->emu_lock, flags);
     714    return val;
    715715}
    716716
    717717static void snd_emu10k1x_ac97_write(struct snd_ac97 *ac97,
    718                                     unsigned short reg, unsigned short val)
    719 {
    720         struct emu10k1x *emu = ac97->private_data;
    721         unsigned long flags;
    722  
    723         spin_lock_irqsave(&emu->emu_lock, flags);
    724         outb(reg, emu->port + AC97ADDRESS);
    725         outw(val, emu->port + AC97DATA);
    726         spin_unlock_irqrestore(&emu->emu_lock, flags);
     718                                    unsigned short reg, unsigned short val)
     719{
     720    struct emu10k1x *emu = ac97->private_data;
     721    unsigned long flags;
     722
     723    spin_lock_irqsave(&emu->emu_lock, flags);
     724    outb(reg, emu->port + AC97ADDRESS);
     725    outw(val, emu->port + AC97DATA);
     726    spin_unlock_irqrestore(&emu->emu_lock, flags);
    727727}
    728728
    729729static int snd_emu10k1x_ac97(struct emu10k1x *chip)
    730730{
    731         struct snd_ac97_bus *pbus;
    732         struct snd_ac97_template ac97;
    733         int err;
    734         static struct snd_ac97_bus_ops ops = {
    735                 .write = snd_emu10k1x_ac97_write,
    736                 .read = snd_emu10k1x_ac97_read,
    737         };
    738  
    739         if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
    740                 return err;
    741         pbus->no_vra = 1; /* we don't need VRA */
    742 
    743         memset(&ac97, 0, sizeof(ac97));
    744         ac97.private_data = chip;
    745         ac97.scaps = AC97_SCAP_NO_SPDIF;
    746         return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
     731    struct snd_ac97_bus *pbus;
     732    struct snd_ac97_template ac97;
     733    int err;
     734    static struct snd_ac97_bus_ops ops = {
     735        .write = snd_emu10k1x_ac97_write,
     736        .read = snd_emu10k1x_ac97_read,
     737    };
     738
     739    if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
     740        return err;
     741    pbus->no_vra = 1; /* we don't need VRA */
     742
     743    memset(&ac97, 0, sizeof(ac97));
     744    ac97.private_data = chip;
     745    ac97.scaps = AC97_SCAP_NO_SPDIF;
     746    return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
    747747}
    748748
    749749static int snd_emu10k1x_free(struct emu10k1x *chip)
    750750{
    751         snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
    752         // disable interrupts
    753         outl(0, chip->port + INTE);
    754         // disable audio
    755         outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
    756 
    757         // release the i/o port
    758         release_and_free_resource(chip->res_port);
    759 
    760         // release the irq
    761         if (chip->irq >= 0)
    762                 free_irq(chip->irq, (void *)chip);
    763 
    764         // release the DMA
    765         if (chip->dma_buffer.area) {
    766                 snd_dma_free_pages(&chip->dma_buffer);
    767         }
    768 
    769         pci_disable_device(chip->pci);
    770 
    771         // release the data
    772         kfree(chip);
    773         return 0;
     751    snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
     752    // disable interrupts
     753    outl(0, chip->port + INTE);
     754    // disable audio
     755    outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
     756
     757    // release the i/o port
     758    release_and_free_resource(chip->res_port);
     759
     760    // release the irq
     761    if (chip->irq >= 0)
     762        free_irq(chip->irq, (void *)chip);
     763
     764    // release the DMA
     765    if (chip->dma_buffer.area) {
     766        snd_dma_free_pages(&chip->dma_buffer);
     767    }
     768
     769    pci_disable_device(chip->pci);
     770
     771    // release the data
     772    kfree(chip);
     773    return 0;
    774774}
    775775
    776776static int snd_emu10k1x_dev_free(struct snd_device *device)
    777777{
    778         struct emu10k1x *chip = device->device_data;
    779         return snd_emu10k1x_free(chip);
    780 }
    781 
    782 static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id,
    783                                           struct pt_regs *regs)
    784 {
    785         unsigned int status;
    786 
    787         struct emu10k1x *chip = dev_id;
    788         struct emu10k1x_voice *pvoice = chip->voices;
    789         int i;
    790         int mask;
    791 
    792         status = inl(chip->port + IPR);
    793 
    794         if (! status)
    795                 return IRQ_NONE;
    796 
    797         // capture interrupt
    798         if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
    799                 struct emu10k1x_voice *pvoice = &chip->capture_voice;
    800                 if (pvoice->use)
    801                         snd_emu10k1x_pcm_interrupt(chip, pvoice);
    802                 else
    803                         snd_emu10k1x_intr_disable(chip,
    804                                                   INTE_CAP_0_LOOP |
    805                                                   INTE_CAP_0_HALF_LOOP);
    806         }
    807                
    808         mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
    809         for (i = 0; i < 3; i++) {
    810                 if (status & mask) {
    811                         if (pvoice->use)
    812                                 snd_emu10k1x_pcm_interrupt(chip, pvoice);
    813                         else
    814                                 snd_emu10k1x_intr_disable(chip, mask);
    815                 }
    816                 pvoice++;
    817                 mask <<= 1;
    818         }
    819                
    820         if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
    821                 if (chip->midi.interrupt)
    822                         chip->midi.interrupt(chip, status);
    823                 else
    824                         snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
    825         }
    826                
    827         // acknowledge the interrupt if necessary
    828         outl(status, chip->port + IPR);
    829 
    830         // snd_printk(KERN_INFO "interrupt %08x\n", status);
    831         return IRQ_HANDLED;
     778    struct emu10k1x *chip = device->device_data;
     779    return snd_emu10k1x_free(chip);
     780}
     781
     782static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id)
     783{
     784    unsigned int status;
     785
     786    struct emu10k1x *chip = dev_id;
     787    struct emu10k1x_voice *pvoice = chip->voices;
     788    int i;
     789    int mask;
     790
     791    status = inl(chip->port + IPR);
     792
     793    if (! status)
     794        return IRQ_NONE;
     795
     796    // capture interrupt
     797    if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
     798        struct emu10k1x_voice *pvoice = &chip->capture_voice;
     799        if (pvoice->use)
     800            snd_emu10k1x_pcm_interrupt(chip, pvoice);
     801        else
     802            snd_emu10k1x_intr_disable(chip,
     803                                      INTE_CAP_0_LOOP |
     804                                      INTE_CAP_0_HALF_LOOP);
     805    }
     806
     807    mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
     808    for (i = 0; i < 3; i++) {
     809        if (status & mask) {
     810            if (pvoice->use)
     811                snd_emu10k1x_pcm_interrupt(chip, pvoice);
     812            else
     813                snd_emu10k1x_intr_disable(chip, mask);
     814        }
     815        pvoice++;
     816        mask <<= 1;
     817    }
     818
     819    if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
     820        if (chip->midi.interrupt)
     821            chip->midi.interrupt(chip, status);
     822        else
     823            snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
     824    }
     825
     826    // acknowledge the interrupt if necessary
     827    outl(status, chip->port + IPR);
     828
     829    // snd_printk(KERN_INFO "interrupt %08x\n", status);
     830    return IRQ_HANDLED;
    832831}
    833832
    834833static int __devinit snd_emu10k1x_pcm(struct emu10k1x *emu, int device, struct snd_pcm **rpcm)
    835834{
    836         struct snd_pcm *pcm;
    837         int err;
    838         int capture = 0;
    839  
    840         if (rpcm)
    841                 *rpcm = NULL;
    842         if (device == 0)
    843                 capture = 1;
    844        
    845         if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
    846                 return err;
    847  
    848         pcm->private_data = emu;
    849        
    850         switch(device) {
    851         case 0:
    852                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
    853                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
    854                 break;
    855         case 1:
    856         case 2:
    857                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
    858                 break;
    859         }
    860 
    861         pcm->info_flags = 0;
    862         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
    863         switch(device) {
    864         case 0:
    865                 strcpy(pcm->name, "EMU10K1X Front");
    866                 break;
    867         case 1:
    868                 strcpy(pcm->name, "EMU10K1X Rear");
    869                 break;
    870         case 2:
    871                 strcpy(pcm->name, "EMU10K1X Center/LFE");
    872                 break;
    873         }
    874         emu->pcm = pcm;
    875 
    876         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
    877                                               snd_dma_pci_data(emu->pci),
    878                                               32*1024, 32*1024);
    879  
    880         if (rpcm)
    881                 *rpcm = pcm;
    882  
    883         return 0;
     835    struct snd_pcm *pcm;
     836    int err;
     837    int capture = 0;
     838
     839    if (rpcm)
     840        *rpcm = NULL;
     841    if (device == 0)
     842        capture = 1;
     843
     844    if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
     845        return err;
     846
     847    pcm->private_data = emu;
     848
     849    switch(device) {
     850    case 0:
     851        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
     852        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
     853        break;
     854    case 1:
     855    case 2:
     856        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
     857        break;
     858    }
     859
     860    pcm->info_flags = 0;
     861    pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
     862    switch(device) {
     863    case 0:
     864        strcpy(pcm->name, "EMU10K1X Front");
     865        break;
     866    case 1:
     867        strcpy(pcm->name, "EMU10K1X Rear");
     868        break;
     869    case 2:
     870        strcpy(pcm->name, "EMU10K1X Center/LFE");
     871        break;
     872    }
     873    emu->pcm = pcm;
     874
     875    snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
     876                                          snd_dma_pci_data(emu->pci),
     877                                          32*1024, 32*1024);
     878
     879    if (rpcm)
     880        *rpcm = pcm;
     881
     882    return 0;
    884883}
    885884
    886885static int __devinit snd_emu10k1x_create(struct snd_card *card,
    887                                         struct pci_dev *pci,
    888                                         struct emu10k1x **rchip)
    889 {
    890         struct emu10k1x *chip;
    891         int err;
    892         int ch;
    893         static struct snd_device_ops ops = {
    894                 .dev_free = snd_emu10k1x_dev_free,
    895         };
    896  
    897         *rchip = NULL;
    898  
    899         if ((err = pci_enable_device(pci)) < 0)
    900                 return err;
    901         pci_set_dma_mask(pci, 0x0fffffff);
    902  
    903         chip = (struct emu10k1x *)kzalloc(sizeof(*chip), GFP_KERNEL);
    904         if (chip == NULL) {
    905                 pci_disable_device(pci);
    906                 return -ENOMEM;
    907         }
    908  
    909         chip->card = card;
    910         chip->pci = pci;
    911         chip->irq = -1;
    912 
    913         spin_lock_init(&chip->emu_lock);
    914         spin_lock_init(&chip->voice_lock);
    915  
    916         chip->port = pci_resource_start(pci, 0);
    917         if ((chip->res_port = request_region(chip->port, 8,
    918                                              "EMU10K1X")) == NULL) {
    919                 snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
    920                 snd_emu10k1x_free(chip);
    921                 return -EBUSY;
    922         }
    923 
    924         if (request_irq(pci->irq, snd_emu10k1x_interrupt,
    925                         SA_INTERRUPT|SA_SHIRQ, "EMU10K1X",
    926                         (void *)chip)) {
    927                 snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
    928                 snd_emu10k1x_free(chip);
    929                 return -EBUSY;
    930         }
    931         chip->irq = pci->irq;
    932  
    933         if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
    934                                4 * 1024, &chip->dma_buffer) < 0) {
    935                 snd_emu10k1x_free(chip);
    936                 return -ENOMEM;
    937         }
    938 
    939         pci_set_master(pci);
    940         /* read revision & serial */
    941         pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
    942         pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
    943         pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
    944         snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
    945                    chip->revision, chip->serial);
    946 
    947         outl(0, chip->port + INTE);     
    948 
    949         for(ch = 0; ch < 3; ch++) {
    950                 chip->voices[ch].emu = chip;
    951                 chip->voices[ch].number = ch;
    952         }
    953 
    954         /*
    955         *  Init to 0x02109204 :
    956         *  Clock accuracy    = 0     (1000ppm)
    957         *  Sample Rate       = 2     (48kHz)
    958         *  Audio Channel     = 1     (Left of 2)
    959         *  Source Number     = 0     (Unspecified)
    960         *  Generation Status = 1     (Original for Cat Code 12)
    961         *  Cat Code          = 12    (Digital Signal Mixer)
    962         *  Mode              = 0     (Mode 0)
    963         *  Emphasis          = 0     (None)
    964         *  CP                = 1     (Copyright unasserted)
    965         *  AN                = 0     (Audio data)
    966         *  P                 = 0     (Consumer)
    967         */
    968         snd_emu10k1x_ptr_write(chip, SPCS0, 0,
    969                                chip->spdif_bits[0] =
    970                                SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
    971                                SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
    972                                SPCS_GENERATIONSTATUS | 0x00001200 |
    973                                0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
    974         snd_emu10k1x_ptr_write(chip, SPCS1, 0,
    975                                chip->spdif_bits[1] =
    976                                SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
    977                                SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
    978                                SPCS_GENERATIONSTATUS | 0x00001200 |
    979                                0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
    980         snd_emu10k1x_ptr_write(chip, SPCS2, 0,
    981                                chip->spdif_bits[2] =
    982                                SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
    983                                SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
    984                                SPCS_GENERATIONSTATUS | 0x00001200 |
    985                                0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
    986 
    987         snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
    988         snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
    989         snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
    990 
    991         outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
    992 
    993         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
    994                                   chip, &ops)) < 0) {
    995                 snd_emu10k1x_free(chip);
    996                 return err;
    997         }
    998         *rchip = chip;
    999         return 0;
    1000 }
    1001 
    1002 static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry, 
    1003                                        struct snd_info_buffer *buffer)
    1004 {
    1005         struct emu10k1x *emu = entry->private_data;
    1006         unsigned long value,value1,value2;
    1007         unsigned long flags;
    1008         int i;
    1009 
    1010         snd_iprintf(buffer, "Registers:\n\n");
    1011         for(i = 0; i < 0x20; i+=4) {
    1012                 spin_lock_irqsave(&emu->emu_lock, flags);
    1013                 value = inl(emu->port + i);
    1014                 spin_unlock_irqrestore(&emu->emu_lock, flags);
    1015                 snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
    1016         }
    1017         snd_iprintf(buffer, "\nRegisters\n\n");
    1018         for(i = 0; i <= 0x48; i++) {
    1019                 value = snd_emu10k1x_ptr_read(emu, i, 0);
    1020                 if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
    1021                         value1 = snd_emu10k1x_ptr_read(emu, i, 1);
    1022                         value2 = snd_emu10k1x_ptr_read(emu, i, 2);
    1023                         snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
    1024                 } else {
    1025                         snd_iprintf(buffer, "%02X: %08lX\n", i, value);
    1026                 }
    1027         }
    1028 }
    1029 
    1030 static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry, 
    1031                                         struct snd_info_buffer *buffer)
    1032 {
    1033         struct emu10k1x *emu = entry->private_data;
    1034         char line[64];
    1035         unsigned int reg, channel_id , val;
     886                                        struct pci_dev *pci,
     887                                        struct emu10k1x **rchip)
     888{
     889    struct emu10k1x *chip;
     890    int err;
     891    int ch;
     892    static struct snd_device_ops ops = {
     893        .dev_free = snd_emu10k1x_dev_free,
     894    };
     895
     896    *rchip = NULL;
     897
     898    if ((err = pci_enable_device(pci)) < 0)
     899        return err;
     900    pci_set_dma_mask(pci, 0x0fffffff);
     901
     902    chip = (struct emu10k1x *)kzalloc(sizeof(*chip), GFP_KERNEL);
     903    if (chip == NULL) {
     904        pci_disable_device(pci);
     905        return -ENOMEM;
     906    }
     907
     908    chip->card = card;
     909    chip->pci = pci;
     910    chip->irq = -1;
     911
     912    spin_lock_init(&chip->emu_lock);
     913    spin_lock_init(&chip->voice_lock);
     914
     915    chip->port = pci_resource_start(pci, 0);
     916    if ((chip->res_port = request_region(chip->port, 8,
     917                                         "EMU10K1X")) == NULL) {
     918        snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
     919        snd_emu10k1x_free(chip);
     920        return -EBUSY;
     921    }
     922
     923    if (request_irq(pci->irq, snd_emu10k1x_interrupt,
     924                    SA_INTERRUPT|SA_SHIRQ, "EMU10K1X",
     925                    (void *)chip)) {
     926        snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
     927        snd_emu10k1x_free(chip);
     928        return -EBUSY;
     929    }
     930    chip->irq = pci->irq;
     931
     932    if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
     933                           4 * 1024, &chip->dma_buffer) < 0) {
     934        snd_emu10k1x_free(chip);
     935        return -ENOMEM;
     936    }
     937
     938    pci_set_master(pci);
     939    /* read revision & serial */
     940    pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
     941    pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
     942    pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
     943    snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
     944               chip->revision, chip->serial);
     945
     946    outl(0, chip->port + INTE);
     947
     948    for(ch = 0; ch < 3; ch++) {
     949        chip->voices[ch].emu = chip;
     950        chip->voices[ch].number = ch;
     951    }
     952
     953    /*
     954    *  Init to 0x02109204 :
     955    *  Clock accuracy    = 0     (1000ppm)
     956    *  Sample Rate       = 2     (48kHz)
     957    *  Audio Channel     = 1     (Left of 2)
     958    *  Source Number     = 0     (Unspecified)
     959    *  Generation Status = 1     (Original for Cat Code 12)
     960    *  Cat Code          = 12    (Digital Signal Mixer)
     961    *  Mode              = 0     (Mode 0)
     962    *  Emphasis          = 0     (None)
     963    *  CP                = 1     (Copyright unasserted)
     964    *  AN                = 0     (Audio data)
     965    *  P                 = 0     (Consumer)
     966    */
     967    snd_emu10k1x_ptr_write(chip, SPCS0, 0,
     968                           chip->spdif_bits[0] =
     969                           SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
     970                           SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
     971                           SPCS_GENERATIONSTATUS | 0x00001200 |
     972                           0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
     973    snd_emu10k1x_ptr_write(chip, SPCS1, 0,
     974                           chip->spdif_bits[1] =
     975                           SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
     976                           SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
     977                           SPCS_GENERATIONSTATUS | 0x00001200 |
     978                           0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
     979    snd_emu10k1x_ptr_write(chip, SPCS2, 0,
     980                           chip->spdif_bits[2] =
     981                           SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
     982                           SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
     983                           SPCS_GENERATIONSTATUS | 0x00001200 |
     984                           0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
     985
     986    snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
     987    snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
     988    snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
     989
     990    outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
     991
     992    if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
     993                              chip, &ops)) < 0) {
     994        snd_emu10k1x_free(chip);
     995        return err;
     996    }
     997    *rchip = chip;
     998    return 0;
     999}
     1000
     1001static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry,
     1002                                       struct snd_info_buffer *buffer)
     1003{
     1004    struct emu10k1x *emu = entry->private_data;
     1005    unsigned long value,value1,value2;
     1006    unsigned long flags;
     1007    int i;
     1008
     1009    snd_iprintf(buffer, "Registers:\n\n");
     1010    for(i = 0; i < 0x20; i+=4) {
     1011        spin_lock_irqsave(&emu->emu_lock, flags);
     1012        value = inl(emu->port + i);
     1013        spin_unlock_irqrestore(&emu->emu_lock, flags);
     1014        snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
     1015    }
     1016    snd_iprintf(buffer, "\nRegisters\n\n");
     1017    for(i = 0; i <= 0x48; i++) {
     1018        value = snd_emu10k1x_ptr_read(emu, i, 0);
     1019        if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
     1020            value1 = snd_emu10k1x_ptr_read(emu, i, 1);
     1021            value2 = snd_emu10k1x_ptr_read(emu, i, 2);
     1022            snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
     1023        } else {
     1024            snd_iprintf(buffer, "%02X: %08lX\n", i, value);
     1025        }
     1026    }
     1027}
     1028
     1029static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry,
     1030                                        struct snd_info_buffer *buffer)
     1031{
     1032    struct emu10k1x *emu = entry->private_data;
     1033    char line[64];
     1034    unsigned int reg, channel_id , val;
    10361035#if 0
    1037         while (!snd_info_get_line(buffer, line, sizeof(line))) {
    1038                 if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
    1039                         continue;
    1040 
    1041                 if ((reg < 0x49) && (reg >=0) && (val <= 0xffffffff)
    1042                     && (channel_id >=0) && (channel_id <= 2) )
    1043                         snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
    1044         }
     1036    while (!snd_info_get_line(buffer, line, sizeof(line))) {
     1037        if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
     1038            continue;
     1039
     1040        if ((reg < 0x49) && (reg >=0) && (val <= 0xffffffff)
     1041            && (channel_id >=0) && (channel_id <= 2) )
     1042            snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
     1043    }
    10451044#endif
    10461045}
     
    10481047static int __devinit snd_emu10k1x_proc_init(struct emu10k1x * emu)
    10491048{
    1050         struct snd_info_entry *entry;
    1051        
    1052         if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
    1053                 snd_info_set_text_ops(entry, emu, 1024, snd_emu10k1x_proc_reg_read);
    1054                 entry->c.text.write_size = 64;
    1055                 entry->c.text.write = snd_emu10k1x_proc_reg_write;
    1056                 entry->mode |= S_IWUSR;
    1057                 entry->private_data = emu;
    1058         }
    1059        
    1060         return 0;
     1049    struct snd_info_entry *entry;
     1050
     1051    if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
     1052        snd_info_set_text_ops(entry, emu, 1024, snd_emu10k1x_proc_reg_read);
     1053        entry->c.text.write_size = 64;
     1054        entry->c.text.write = snd_emu10k1x_proc_reg_write;
     1055        entry->mode |= S_IWUSR;
     1056        entry->private_data = emu;
     1057    }
     1058
     1059    return 0;
    10611060}
    10621061
    10631062static int snd_emu10k1x_shared_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
    10641063{
    1065         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
    1066         uinfo->count = 1;
    1067         uinfo->value.integer.min = 0;
    1068         uinfo->value.integer.max = 1;
    1069         return 0;
     1064    uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
     1065    uinfo->count = 1;
     1066    uinfo->value.integer.min = 0;
     1067    uinfo->value.integer.max = 1;
     1068    return 0;
    10701069}
    10711070
    10721071static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol *kcontrol,
    1073                                         struct snd_ctl_elem_value *ucontrol)
    1074 {
    1075         struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
    1076 
    1077         ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
    1078 
    1079         return 0;
     1072                                        struct snd_ctl_elem_value *ucontrol)
     1073{
     1074    struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
     1075
     1076    ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
     1077
     1078    return 0;
    10801079}
    10811080
    10821081static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol *kcontrol,
    1083                                         struct snd_ctl_elem_value *ucontrol)
    1084 {
    1085         struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
    1086         unsigned int val;
    1087         int change = 0;
    1088 
    1089         val = ucontrol->value.integer.value[0] ;
    1090 
    1091         if (val) {
    1092                 // enable spdif output
    1093                 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
    1094                 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
    1095                 snd_emu10k1x_gpio_write(emu, 0x1000);
    1096         } else {
    1097                 // disable spdif output
    1098                 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
    1099                 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
    1100                 snd_emu10k1x_gpio_write(emu, 0x1080);
    1101         }
    1102         return change;
     1082                                        struct snd_ctl_elem_value *ucontrol)
     1083{
     1084    struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
     1085    unsigned int val;
     1086    int change = 0;
     1087
     1088    val = ucontrol->value.integer.value[0] ;
     1089
     1090    if (val) {
     1091        // enable spdif output
     1092        snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
     1093        snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
     1094        snd_emu10k1x_gpio_write(emu, 0x1000);
     1095    } else {
     1096        // disable spdif output
     1097        snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
     1098        snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
     1099        snd_emu10k1x_gpio_write(emu, 0x1080);
     1100    }
     1101    return change;
    11031102}
    11041103
    11051104static struct snd_kcontrol_new snd_emu10k1x_shared_spdif __devinitdata =
    11061105{
    1107         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
    1108         .name =         "Analog/Digital Output Jack",
    1109         .info =         snd_emu10k1x_shared_spdif_info,
    1110         .get =          snd_emu10k1x_shared_spdif_get,
    1111         .put =          snd_emu10k1x_shared_spdif_put
     1106    .iface =    SNDRV_CTL_ELEM_IFACE_MIXER,
     1107    .name =             "Analog/Digital Output Jack",
     1108    .info =             snd_emu10k1x_shared_spdif_info,
     1109    .get =              snd_emu10k1x_shared_spdif_get,
     1110    .put =              snd_emu10k1x_shared_spdif_put
    11121111};
    11131112
    11141113static int snd_emu10k1x_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
    11151114{
    1116         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
    1117         uinfo->count = 1;
    1118         return 0;
     1115    uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
     1116    uinfo->count = 1;
     1117    return 0;
    11191118}
    11201119
    11211120static int snd_emu10k1x_spdif_get(struct snd_kcontrol *kcontrol,
    1122                                   struct snd_ctl_elem_value *ucontrol)
    1123 {
    1124         struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
    1125         unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
    1126 
    1127         ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
    1128         ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
    1129         ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
    1130         ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
    1131         return 0;
     1121                                  struct snd_ctl_elem_value *ucontrol)
     1122{
     1123    struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
     1124    unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
     1125
     1126    ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
     1127    ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
     1128    ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
     1129    ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
     1130    return 0;
    11321131}
    11331132
    11341133static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol *kcontrol,
    1135                                        struct snd_ctl_elem_value *ucontrol)
    1136 {
    1137         ucontrol->value.iec958.status[0] = 0xff;
    1138         ucontrol->value.iec958.status[1] = 0xff;
    1139         ucontrol->value.iec958.status[2] = 0xff;
    1140         ucontrol->value.iec958.status[3] = 0xff;
    1141         return 0;
     1134                                       struct snd_ctl_elem_value *ucontrol)
     1135{
     1136    ucontrol->value.iec958.status[0] = 0xff;
     1137    ucontrol->value.iec958.status[1] = 0xff;
     1138    ucontrol->value.iec958.status[2] = 0xff;
     1139    ucontrol->value.iec958.status[3] = 0xff;
     1140    return 0;
    11421141}
    11431142
    11441143static int snd_emu10k1x_spdif_put(struct snd_kcontrol *kcontrol,
    1145                                   struct snd_ctl_elem_value *ucontrol)
    1146 {
    1147         struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
    1148         unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
    1149         int change;
    1150         unsigned int val;
    1151 
    1152         val = (ucontrol->value.iec958.status[0] << 0) |
    1153                 (ucontrol->value.iec958.status[1] << 8) |
    1154                 (ucontrol->value.iec958.status[2] << 16) |
    1155                 (ucontrol->value.iec958.status[3] << 24);
    1156         change = val != emu->spdif_bits[idx];
    1157         if (change) {
    1158                 snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
    1159                 emu->spdif_bits[idx] = val;
    1160         }
    1161         return change;
     1144                                  struct snd_ctl_elem_value *ucontrol)
     1145{
     1146    struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
     1147    unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
     1148    int change;
     1149    unsigned int val;
     1150
     1151    val = (ucontrol->value.iec958.status[0] << 0) |
     1152        (ucontrol->value.iec958.status[1] << 8) |
     1153        (ucontrol->value.iec958.status[2] << 16) |
     1154        (ucontrol->value.iec958.status[3] << 24);
     1155    change = val != emu->spdif_bits[idx];
     1156    if (change) {
     1157        snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
     1158        emu->spdif_bits[idx] = val;
     1159    }
     1160    return change;
    11621161}
    11631162
    11641163static struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control =
    11651164{
    1166         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
    1167         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
    1168         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
    1169         .count =        3,
    1170         .info =         snd_emu10k1x_spdif_info,
    1171         .get =          snd_emu10k1x_spdif_get_mask
     1165    .access =   SNDRV_CTL_ELEM_ACCESS_READ,
     1166    .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
     1167    .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
     1168    .count =    3,
     1169    .info =         snd_emu10k1x_spdif_info,
     1170    .get =          snd_emu10k1x_spdif_get_mask
    11721171};
    11731172
    11741173static struct snd_kcontrol_new snd_emu10k1x_spdif_control =
    11751174{
    1176         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
    1177         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
    1178         .count =        3,
    1179         .info =         snd_emu10k1x_spdif_info,
    1180         .get =          snd_emu10k1x_spdif_get,
    1181         .put =          snd_emu10k1x_spdif_put
     1175    .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
     1176    .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
     1177    .count =    3,
     1178    .info =         snd_emu10k1x_spdif_info,
     1179    .get =          snd_emu10k1x_spdif_get,
     1180    .put =          snd_emu10k1x_spdif_put
    11821181};
    11831182
    11841183static int __devinit snd_emu10k1x_mixer(struct emu10k1x *emu)
    11851184{
    1186         int err;
    1187         struct snd_kcontrol *kctl;
    1188         struct snd_card *card = emu->card;
    1189 
    1190         if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
    1191                 return -ENOMEM;
    1192         if ((err = snd_ctl_add(card, kctl)))
    1193                 return err;
    1194         if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
    1195                 return -ENOMEM;
    1196         if ((err = snd_ctl_add(card, kctl)))
    1197                 return err;
    1198         if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
    1199                 return -ENOMEM;
    1200         if ((err = snd_ctl_add(card, kctl)))
    1201                 return err;
    1202 
    1203         return 0;
     1185    int err;
     1186    struct snd_kcontrol *kctl;
     1187    struct snd_card *card = emu->card;
     1188
     1189    if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
     1190        return -ENOMEM;
     1191    if ((err = snd_ctl_add(card, kctl)))
     1192        return err;
     1193    if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
     1194        return -ENOMEM;
     1195    if ((err = snd_ctl_add(card, kctl)))
     1196        return err;
     1197    if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
     1198        return -ENOMEM;
     1199    if ((err = snd_ctl_add(card, kctl)))
     1200        return err;
     1201
     1202    return 0;
    12041203}
    12051204
     
    12091208static inline unsigned char mpu401_read(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int idx)
    12101209{
    1211         return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
     1210    return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
    12121211}
    12131212
    12141213static inline void mpu401_write(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int data, int idx)
    12151214{
    1216         snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
     1215    snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
    12171216}
    12181217
     
    12311230static void mpu401_clear_rx(struct emu10k1x *emu, struct emu10k1x_midi *mpu)
    12321231{
    1233         int timeout = 100000;
    1234         for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
    1235                 mpu401_read_data(emu, mpu);
     1232    int timeout = 100000;
     1233    for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
     1234        mpu401_read_data(emu, mpu);
    12361235#ifdef CONFIG_SND_DEBUG
    1237         if (timeout <= 0)
    1238                 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
     1236    if (timeout <= 0)
     1237        snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
    12391238#endif
    12401239}
     
    12421241/*
    12431242
    1244  */
     1243*/
    12451244
    12461245static void do_emu10k1x_midi_interrupt(struct emu10k1x *emu,
    1247                                        struct emu10k1x_midi *midi, unsigned int status)
    1248 {
    1249         unsigned char byte;
    1250 
    1251         if (midi->rmidi == NULL) {
    1252                 snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
    1253                 return;
    1254         }
    1255 
    1256         spin_lock(&midi->input_lock);
    1257         if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
    1258                 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
    1259                         mpu401_clear_rx(emu, midi);
    1260                 } else {
    1261                         byte = mpu401_read_data(emu, midi);
    1262                         if (midi->substream_input)
    1263                                 snd_rawmidi_receive(midi->substream_input, &byte, 1);
    1264                 }
    1265         }
    1266         spin_unlock(&midi->input_lock);
    1267 
    1268         spin_lock(&midi->output_lock);
    1269         if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
    1270                 if (midi->substream_output &&
    1271                     snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
    1272                         mpu401_write_data(emu, midi, byte);
    1273                 } else {
    1274                         snd_emu10k1x_intr_disable(emu, midi->tx_enable);
    1275                 }
    1276         }
    1277         spin_unlock(&midi->output_lock);
     1246                                       struct emu10k1x_midi *midi, unsigned int status)
     1247{
     1248    unsigned char byte;
     1249
     1250    if (midi->rmidi == NULL) {
     1251        snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
     1252        return;
     1253    }
     1254
     1255    spin_lock(&midi->input_lock);
     1256    if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
     1257        if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
     1258            mpu401_clear_rx(emu, midi);
     1259        } else {
     1260            byte = mpu401_read_data(emu, midi);
     1261            if (midi->substream_input)
     1262                snd_rawmidi_receive(midi->substream_input, &byte, 1);
     1263        }
     1264    }
     1265    spin_unlock(&midi->input_lock);
     1266
     1267    spin_lock(&midi->output_lock);
     1268    if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
     1269        if (midi->substream_output &&
     1270            snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
     1271            mpu401_write_data(emu, midi, byte);
     1272        } else {
     1273            snd_emu10k1x_intr_disable(emu, midi->tx_enable);
     1274        }
     1275    }
     1276    spin_unlock(&midi->output_lock);
    12781277}
    12791278
    12801279static void snd_emu10k1x_midi_interrupt(struct emu10k1x *emu, unsigned int status)
    12811280{
    1282         do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
    1283 }
    1284 
    1285 static void snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
    1286                                   struct emu10k1x_midi *midi, unsigned char cmd, int ack)
    1287 {
    1288         unsigned long flags;
    1289         int timeout, ok;
    1290 
    1291         spin_lock_irqsave(&midi->input_lock, flags);
    1292         mpu401_write_data(emu, midi, 0x00);
    1293         /* mpu401_clear_rx(emu, midi); */
    1294 
    1295         mpu401_write_cmd(emu, midi, cmd);
    1296         if (ack) {
    1297                 ok = 0;
    1298                 timeout = 10000;
    1299                 while (!ok && timeout-- > 0) {
    1300                         if (mpu401_input_avail(emu, midi)) {
    1301                                 if (mpu401_read_data(emu, midi) == MPU401_ACK)
    1302                                         ok = 1;
    1303                         }
    1304                 }
    1305                 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
    1306                         ok = 1;
    1307         } else {
    1308                 ok = 1;
    1309         }
    1310         spin_unlock_irqrestore(&midi->input_lock, flags);
    1311         if (!ok)
    1312                 snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
    1313                            cmd, emu->port,
    1314                            mpu401_read_stat(emu, midi),
    1315                            mpu401_read_data(emu, midi));
     1281    do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
     1282}
     1283
     1284static int snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
     1285                                 struct emu10k1x_midi *midi, unsigned char cmd, int ack)
     1286{
     1287    unsigned long flags;
     1288    int timeout, ok;
     1289
     1290    spin_lock_irqsave(&midi->input_lock, flags);
     1291    mpu401_write_data(emu, midi, 0x00);
     1292    /* mpu401_clear_rx(emu, midi); */
     1293
     1294    mpu401_write_cmd(emu, midi, cmd);
     1295    if (ack) {
     1296        ok = 0;
     1297        timeout = 10000;
     1298        while (!ok && timeout-- > 0) {
     1299            if (mpu401_input_avail(emu, midi)) {
     1300                if (mpu401_read_data(emu, midi) == MPU401_ACK)
     1301                    ok = 1;
     1302            }
     1303        }
     1304        if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
     1305            ok = 1;
     1306    } else {
     1307        ok = 1;
     1308    }
     1309    spin_unlock_irqrestore(&midi->input_lock, flags);
     1310    if (!ok) {
     1311        snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
     1312                   cmd, emu->port,
     1313                   mpu401_read_stat(emu, midi),
     1314                   mpu401_read_data(emu, midi));
     1315        return 1;
     1316    }
     1317    return 0;
    13161318}
    13171319
    13181320static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream *substream)
    13191321{
    1320         struct emu10k1x *emu;
    1321         struct emu10k1x_midi *midi = substream->rmidi->private_data;
    1322         unsigned long flags;
    1323        
    1324         emu = midi->emu;
    1325         snd_assert(emu, return -ENXIO);
    1326         spin_lock_irqsave(&midi->open_lock, flags);
    1327         midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
    1328         midi->substream_input = substream;
    1329         if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
    1330                 spin_unlock_irqrestore(&midi->open_lock, flags);
    1331                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
    1332                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
    1333         } else {
    1334                 spin_unlock_irqrestore(&midi->open_lock, flags);
    1335         }
    1336         return 0;
     1322    struct emu10k1x *emu;
     1323    struct emu10k1x_midi *midi = substream->rmidi->private_data;
     1324    unsigned long flags;
     1325
     1326    emu = midi->emu;
     1327    snd_assert(emu, return -ENXIO);
     1328    spin_lock_irqsave(&midi->open_lock, flags);
     1329    midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
     1330    midi->substream_input = substream;
     1331    if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
     1332        spin_unlock_irqrestore(&midi->open_lock, flags);
     1333        if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
     1334            goto error_out;
     1335        if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
     1336            goto error_out;
     1337    } else {
     1338        spin_unlock_irqrestore(&midi->open_lock, flags);
     1339    }
     1340    return 0;
     1341
     1342    error_out:
     1343        return -EIO;
    13371344}
    13381345
    13391346static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream *substream)
    13401347{
    1341         struct emu10k1x *emu;
    1342         struct emu10k1x_midi *midi = substream->rmidi->private_data;
    1343         unsigned long flags;
    1344 
    1345         emu = midi->emu;
    1346         snd_assert(emu, return -ENXIO);
    1347         spin_lock_irqsave(&midi->open_lock, flags);
    1348         midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
    1349         midi->substream_output = substream;
    1350         if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
    1351                 spin_unlock_irqrestore(&midi->open_lock, flags);
    1352                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
    1353                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
    1354         } else {
    1355                 spin_unlock_irqrestore(&midi->open_lock, flags);
    1356         }
    1357         return 0;
     1348    struct emu10k1x *emu;
     1349    struct emu10k1x_midi *midi = substream->rmidi->private_data;
     1350    unsigned long flags;
     1351
     1352    emu = midi->emu;
     1353    snd_assert(emu, return -ENXIO);
     1354    spin_lock_irqsave(&midi->open_lock, flags);
     1355    midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
     1356    midi->substream_output = substream;
     1357    if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
     1358        spin_unlock_irqrestore(&midi->open_lock, flags);
     1359        if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
     1360            goto error_out;
     1361        if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
     1362            goto error_out;
     1363    } else {
     1364        spin_unlock_irqrestore(&midi->open_lock, flags);
     1365    }
     1366    return 0;
     1367
     1368    error_out:
     1369        return -EIO;
    13581370}
    13591371
    13601372static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream *substream)
    13611373{
    1362         struct emu10k1x *emu;
    1363         struct emu10k1x_midi *midi = substream->rmidi->private_data;
    1364         unsigned long flags;
    1365 
    1366         emu = midi->emu;
    1367         snd_assert(emu, return -ENXIO);
    1368         spin_lock_irqsave(&midi->open_lock, flags);
    1369         snd_emu10k1x_intr_disable(emu, midi->rx_enable);
    1370         midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
    1371         midi->substream_input = NULL;
    1372         if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
    1373                 spin_unlock_irqrestore(&midi->open_lock, flags);
    1374                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
    1375         } else {
    1376                 spin_unlock_irqrestore(&midi->open_lock, flags);
    1377         }
    1378         return 0;
     1374    struct emu10k1x *emu;
     1375    struct emu10k1x_midi *midi = substream->rmidi->private_data;
     1376    unsigned long flags;
     1377    int err = 0;
     1378
     1379    emu = midi->emu;
     1380    snd_assert(emu, return -ENXIO);
     1381    spin_lock_irqsave(&midi->open_lock, flags);
     1382    snd_emu10k1x_intr_disable(emu, midi->rx_enable);
     1383    midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
     1384    midi->substream_input = NULL;
     1385    if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
     1386        spin_unlock_irqrestore(&midi->open_lock, flags);
     1387        err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
     1388    } else {
     1389        spin_unlock_irqrestore(&midi->open_lock, flags);
     1390    }
     1391    return err;
    13791392}
    13801393
    13811394static int snd_emu10k1x_midi_output_close(struct snd_rawmidi_substream *substream)
    13821395{
    1383         struct emu10k1x *emu;
    1384         struct emu10k1x_midi *midi = substream->rmidi->private_data;
    1385         unsigned long flags;
    1386 
    1387         emu = midi->emu;
    1388         snd_assert(emu, return -ENXIO);
    1389         spin_lock_irqsave(&midi->open_lock, flags);
    1390         snd_emu10k1x_intr_disable(emu, midi->tx_enable);
    1391         midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
    1392         midi->substream_output = NULL;
    1393         if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
    1394                 spin_unlock_irqrestore(&midi->open_lock, flags);
    1395                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
    1396         } else {
    1397                 spin_unlock_irqrestore(&midi->open_lock, flags);
    1398         }
    1399         return 0;
     1396    struct emu10k1x *emu;
     1397    struct emu10k1x_midi *midi = substream->rmidi->private_data;
     1398    unsigned long flags;
     1399    int err = 0;
     1400
     1401    emu = midi->emu;
     1402    snd_assert(emu, return -ENXIO);
     1403    spin_lock_irqsave(&midi->open_lock, flags);
     1404    snd_emu10k1x_intr_disable(emu, midi->tx_enable);
     1405    midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
     1406    midi->substream_output = NULL;
     1407    if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
     1408        spin_unlock_irqrestore(&midi->open_lock, flags);
     1409        err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
     1410    } else {
     1411        spin_unlock_irqrestore(&midi->open_lock, flags);
     1412    }
     1413    return err;
    14001414}
    14011415
    14021416static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
    14031417{
    1404         struct emu10k1x *emu;
    1405         struct emu10k1x_midi *midi = substream->rmidi->private_data;
    1406         emu = midi->emu;
    1407         snd_assert(emu, return);
    1408 
    1409         if (up)
    1410                 snd_emu10k1x_intr_enable(emu, midi->rx_enable);
    1411         else
    1412                 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
     1418    struct emu10k1x *emu;
     1419    struct emu10k1x_midi *midi = substream->rmidi->private_data;
     1420    emu = midi->emu;
     1421    snd_assert(emu, return);
     1422
     1423    if (up)
     1424        snd_emu10k1x_intr_enable(emu, midi->rx_enable);
     1425    else
     1426        snd_emu10k1x_intr_disable(emu, midi->rx_enable);
    14131427}
    14141428
    14151429static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
    14161430{
    1417         struct emu10k1x *emu;
    1418         struct emu10k1x_midi *midi = substream->rmidi->private_data;
    1419         unsigned long flags;
    1420 
    1421         emu = midi->emu;
    1422         snd_assert(emu, return);
    1423 
    1424         if (up) {
    1425                 int max = 4;
    1426                 unsigned char byte;
    1427        
    1428                 /* try to send some amount of bytes here before interrupts */
    1429                 spin_lock_irqsave(&midi->output_lock, flags);
    1430                 while (max > 0) {
    1431                         if (mpu401_output_ready(emu, midi)) {
    1432                                 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
    1433                                     snd_rawmidi_transmit(substream, &byte, 1) != 1) {
    1434                                         /* no more data */
    1435                                         spin_unlock_irqrestore(&midi->output_lock, flags);
    1436                                         return;
    1437                                 }
    1438                                 mpu401_write_data(emu, midi, byte);
    1439                                 max--;
    1440                         } else {
    1441                                 break;
    1442                         }
    1443                 }
    1444                 spin_unlock_irqrestore(&midi->output_lock, flags);
    1445                 snd_emu10k1x_intr_enable(emu, midi->tx_enable);
    1446         } else {
    1447                 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
    1448         }
     1431    struct emu10k1x *emu;
     1432    struct emu10k1x_midi *midi = substream->rmidi->private_data;
     1433    unsigned long flags;
     1434
     1435    emu = midi->emu;
     1436    snd_assert(emu, return);
     1437
     1438    if (up) {
     1439        int max = 4;
     1440        unsigned char byte;
     1441
     1442        /* try to send some amount of bytes here before interrupts */
     1443        spin_lock_irqsave(&midi->output_lock, flags);
     1444        while (max > 0) {
     1445            if (mpu401_output_ready(emu, midi)) {
     1446                if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
     1447                    snd_rawmidi_transmit(substream, &byte, 1) != 1) {
     1448                    /* no more data */
     1449                    spin_unlock_irqrestore(&midi->output_lock, flags);
     1450                    return;
     1451                }
     1452                mpu401_write_data(emu, midi, byte);
     1453                max--;
     1454            } else {
     1455                break;
     1456            }
     1457        }
     1458        spin_unlock_irqrestore(&midi->output_lock, flags);
     1459        snd_emu10k1x_intr_enable(emu, midi->tx_enable);
     1460    } else {
     1461        snd_emu10k1x_intr_disable(emu, midi->tx_enable);
     1462    }
    14491463}
    14501464
    14511465/*
    14521466
    1453  */
     1467*/
    14541468
    14551469static struct snd_rawmidi_ops snd_emu10k1x_midi_output =
    14561470{
    1457         .open =         snd_emu10k1x_midi_output_open,
    1458         .close =        snd_emu10k1x_midi_output_close,
    1459         .trigger =      snd_emu10k1x_midi_output_trigger,
     1471    .open =             snd_emu10k1x_midi_output_open,
     1472    .close =    snd_emu10k1x_midi_output_close,
     1473    .trigger =  snd_emu10k1x_midi_output_trigger,
    14601474};
    14611475
    14621476static struct snd_rawmidi_ops snd_emu10k1x_midi_input =
    14631477{
    1464         .open =         snd_emu10k1x_midi_input_open,
    1465         .close =        snd_emu10k1x_midi_input_close,
    1466         .trigger =      snd_emu10k1x_midi_input_trigger,
     1478    .open =             snd_emu10k1x_midi_input_open,
     1479    .close =    snd_emu10k1x_midi_input_close,
     1480    .trigger =  snd_emu10k1x_midi_input_trigger,
    14671481};
    14681482
    14691483static void snd_emu10k1x_midi_free(struct snd_rawmidi *rmidi)
    14701484{
    1471         struct emu10k1x_midi *midi = rmidi->private_data;
    1472         midi->interrupt = NULL;
    1473         midi->rmidi = NULL;
     1485    struct emu10k1x_midi *midi = rmidi->private_data;
     1486    midi->interrupt = NULL;
     1487    midi->rmidi = NULL;
    14741488}
    14751489
    14761490static int __devinit emu10k1x_midi_init(struct emu10k1x *emu,
    1477                                         struct emu10k1x_midi *midi, int device, char *name)
    1478 {
    1479         struct snd_rawmidi *rmidi;
    1480         int err;
    1481 
    1482         if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
    1483                 return err;
    1484         midi->emu = emu;
    1485         spin_lock_init(&midi->open_lock);
    1486         spin_lock_init(&midi->input_lock);
    1487         spin_lock_init(&midi->output_lock);
    1488         strcpy(rmidi->name, name);
    1489         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
    1490         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
    1491         rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
    1492                              SNDRV_RAWMIDI_INFO_INPUT |
    1493                              SNDRV_RAWMIDI_INFO_DUPLEX;
    1494         rmidi->private_data = midi;
    1495         rmidi->private_free = snd_emu10k1x_midi_free;
    1496         midi->rmidi = rmidi;
    1497         return 0;
     1491                                        struct emu10k1x_midi *midi, int device, char *name)
     1492{
     1493    struct snd_rawmidi *rmidi;
     1494    int err;
     1495
     1496    if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
     1497        return err;
     1498    midi->emu = emu;
     1499    spin_lock_init(&midi->open_lock);
     1500    spin_lock_init(&midi->input_lock);
     1501    spin_lock_init(&midi->output_lock);
     1502    strcpy(rmidi->name, name);
     1503    snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
     1504    snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
     1505    rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
     1506        SNDRV_RAWMIDI_INFO_INPUT |
     1507        SNDRV_RAWMIDI_INFO_DUPLEX;
     1508    rmidi->private_data = midi;
     1509    rmidi->private_free = snd_emu10k1x_midi_free;
     1510    midi->rmidi = rmidi;
     1511    return 0;
    14981512}
    14991513
    15001514static int __devinit snd_emu10k1x_midi(struct emu10k1x *emu)
    15011515{
    1502         struct emu10k1x_midi *midi = &emu->midi;
    1503         int err;
    1504 
    1505         if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
    1506                 return err;
    1507 
    1508         midi->tx_enable = INTE_MIDITXENABLE;
    1509         midi->rx_enable = INTE_MIDIRXENABLE;
    1510         midi->port = MUDATA;
    1511         midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
    1512         midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
    1513         midi->interrupt = snd_emu10k1x_midi_interrupt;
    1514         return 0;
     1516    struct emu10k1x_midi *midi = &emu->midi;
     1517    int err;
     1518
     1519    if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
     1520        return err;
     1521
     1522    midi->tx_enable = INTE_MIDITXENABLE;
     1523    midi->rx_enable = INTE_MIDIRXENABLE;
     1524    midi->port = MUDATA;
     1525    midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
     1526    midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
     1527    midi->interrupt = snd_emu10k1x_midi_interrupt;
     1528    return 0;
    15151529}
    15161530
    15171531static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
    1518                                         const struct pci_device_id *pci_id)
    1519 {
    1520         static int dev;
    1521         struct snd_card *card;
    1522         struct emu10k1x *chip;
    1523         int err;
    1524 
    1525         if (dev >= SNDRV_CARDS)
    1526                 return -ENODEV;
    1527         if (!enable[dev]) {
    1528                 dev++;
    1529                 return -ENOENT;
    1530         }
    1531 
    1532         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
    1533         if (card == NULL)
    1534                 return -ENOMEM;
    1535 
    1536         if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
    1537                 snd_card_free(card);
    1538                 return err;
    1539         }
    1540 
    1541         if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
    1542                 snd_card_free(card);
    1543                 return err;
    1544         }
    1545         if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
    1546                 snd_card_free(card);
    1547                 return err;
    1548         }
    1549         if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
    1550                 snd_card_free(card);
    1551                 return err;
    1552         }
    1553 
    1554         if ((err = snd_emu10k1x_ac97(chip)) < 0) {
    1555                 snd_card_free(card);
    1556                 return err;
    1557         }
    1558 
    1559         if ((err = snd_emu10k1x_mixer(chip)) < 0) {
    1560                 snd_card_free(card);
    1561                 return err;
    1562         }
    1563        
    1564         if ((err = snd_emu10k1x_midi(chip)) < 0) {
    1565                 snd_card_free(card);
    1566                 return err;
    1567         }
    1568 
    1569         snd_emu10k1x_proc_init(chip);
    1570 
    1571         strcpy(card->driver, "EMU10K1X");
    1572         strcpy(card->shortname, "Dell Sound Blaster Live!");
    1573         sprintf(card->longname, "%s at 0x%lx irq %i",
    1574                 card->shortname, chip->port, chip->irq);
    1575 
    1576         if ((err = snd_card_register(card)) < 0) {
    1577                 snd_card_free(card);
    1578                 return err;
    1579         }
    1580 
    1581         pci_set_drvdata(pci, card);
    1582         dev++;
    1583         return 0;
     1532                                        const struct pci_device_id *pci_id)
     1533{
     1534    static int dev;
     1535    struct snd_card *card;
     1536    struct emu10k1x *chip;
     1537    int err;
     1538
     1539    if (dev >= SNDRV_CARDS)
     1540        return -ENODEV;
     1541    if (!enable[dev]) {
     1542        dev++;
     1543        return -ENOENT;
     1544    }
     1545
     1546    card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
     1547    if (card == NULL)
     1548        return -ENOMEM;
     1549
     1550    if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
     1551        snd_card_free(card);
     1552        return err;
     1553    }
     1554
     1555    if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
     1556        snd_card_free(card);
     1557        return err;
     1558    }
     1559    if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
     1560        snd_card_free(card);
     1561        return err;
     1562    }
     1563    if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
     1564        snd_card_free(card);
     1565        return err;
     1566    }
     1567
     1568    if ((err = snd_emu10k1x_ac97(chip)) < 0) {
     1569        snd_card_free(card);
     1570        return err;
     1571    }
     1572
     1573    if ((err = snd_emu10k1x_mixer(chip)) < 0) {
     1574        snd_card_free(card);
     1575        return err;
     1576    }
     1577
     1578    if ((err = snd_emu10k1x_midi(chip)) < 0) {
     1579        snd_card_free(card);
     1580        return err;
     1581    }
     1582
     1583    snd_emu10k1x_proc_init(chip);
     1584
     1585    strcpy(card->driver, "EMU10K1X");
     1586    strcpy(card->shortname, "Dell Sound Blaster Live!");
     1587    sprintf(card->longname, "%s at 0x%lx irq %i",
     1588            card->shortname, chip->port, chip->irq);
     1589
     1590    if ((err = snd_card_register(card)) < 0) {
     1591        snd_card_free(card);
     1592        return err;
     1593    }
     1594
     1595    pci_set_drvdata(pci, card);
     1596    dev++;
     1597    return 0;
    15841598}
    15851599
    15861600static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
    15871601{
    1588         snd_card_free(pci_get_drvdata(pci));
    1589         pci_set_drvdata(pci, NULL);
     1602    snd_card_free(pci_get_drvdata(pci));
     1603    pci_set_drvdata(pci, NULL);
    15901604}
    15911605
    15921606// PCI IDs
    15931607static struct pci_device_id snd_emu10k1x_ids[] = {
    1594         { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },    /* Dell OEM version (EMU10K1) */
    1595         { 0, }
     1608    { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },        /* Dell OEM version (EMU10K1) */
     1609    { 0, }
    15961610};
    15971611MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
     
    15991613// pci_driver definition
    16001614static struct pci_driver driver = {
    1601         .name = "EMU10K1X",
    1602         .id_table = snd_emu10k1x_ids,
    1603         .probe = snd_emu10k1x_probe,
    1604         .remove = snd_emu10k1x_remove,
     1615    .name = "EMU10K1X",
     1616    .id_table = snd_emu10k1x_ids,
     1617    .probe = snd_emu10k1x_probe,
     1618    .remove = snd_emu10k1x_remove,
    16051619};
    16061620
     
    16081622static int __init alsa_card_emu10k1x_init(void)
    16091623{
    1610         int err;
    1611 
    1612         if ((err = pci_register_driver(&driver)) > 0)
    1613                 return err;
    1614 
    1615         return 0;
     1624    int err;
     1625
     1626    if ((err = pci_register_driver(&driver)) > 0)
     1627        return err;
     1628
     1629    return 0;
    16161630}
    16171631
     
    16191633static void __exit alsa_card_emu10k1x_exit(void)
    16201634{
    1621         pci_unregister_driver(&driver);
     1635    pci_unregister_driver(&driver);
    16221636}
    16231637
Note: See TracChangeset for help on using the changeset viewer.