Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Driver for SiS7019 Audio Accelerator
0004  *
0005  *  Copyright (C) 2004-2007, David Dillow
0006  *  Written by David Dillow <dave@thedillows.org>
0007  *  Inspired by the Trident 4D-WaveDX/NX driver.
0008  *
0009  *  All rights reserved.
0010  */
0011 
0012 #include <linux/init.h>
0013 #include <linux/pci.h>
0014 #include <linux/time.h>
0015 #include <linux/slab.h>
0016 #include <linux/module.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/delay.h>
0019 #include <sound/core.h>
0020 #include <sound/ac97_codec.h>
0021 #include <sound/initval.h>
0022 #include "sis7019.h"
0023 
0024 MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
0025 MODULE_DESCRIPTION("SiS7019");
0026 MODULE_LICENSE("GPL");
0027 
0028 static int index = SNDRV_DEFAULT_IDX1;  /* Index 0-MAX */
0029 static char *id = SNDRV_DEFAULT_STR1;   /* ID for this card */
0030 static bool enable = 1;
0031 static int codecs = 1;
0032 
0033 module_param(index, int, 0444);
0034 MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
0035 module_param(id, charp, 0444);
0036 MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
0037 module_param(enable, bool, 0444);
0038 MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
0039 module_param(codecs, int, 0444);
0040 MODULE_PARM_DESC(codecs, "Set bit to indicate that codec number is expected to be present (default 1)");
0041 
0042 static const struct pci_device_id snd_sis7019_ids[] = {
0043     { PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
0044     { 0, }
0045 };
0046 
0047 MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
0048 
0049 /* There are three timing modes for the voices.
0050  *
0051  * For both playback and capture, when the buffer is one or two periods long,
0052  * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt
0053  * to let us know when the periods have ended.
0054  *
0055  * When performing playback with more than two periods per buffer, we set
0056  * the "Stop Sample Offset" and tell the hardware to interrupt us when we
0057  * reach it. We then update the offset and continue on until we are
0058  * interrupted for the next period.
0059  *
0060  * Capture channels do not have a SSO, so we allocate a playback channel to
0061  * use as a timer for the capture periods. We use the SSO on the playback
0062  * channel to clock out virtual periods, and adjust the virtual period length
0063  * to maintain synchronization. This algorithm came from the Trident driver.
0064  *
0065  * FIXME: It'd be nice to make use of some of the synth features in the
0066  * hardware, but a woeful lack of documentation is a significant roadblock.
0067  */
0068 struct voice {
0069     u16 flags;
0070 #define     VOICE_IN_USE        1
0071 #define     VOICE_CAPTURE       2
0072 #define     VOICE_SSO_TIMING    4
0073 #define     VOICE_SYNC_TIMING   8
0074     u16 sync_cso;
0075     u16 period_size;
0076     u16 buffer_size;
0077     u16 sync_period_size;
0078     u16 sync_buffer_size;
0079     u32 sso;
0080     u32 vperiod;
0081     struct snd_pcm_substream *substream;
0082     struct voice *timing;
0083     void __iomem *ctrl_base;
0084     void __iomem *wave_base;
0085     void __iomem *sync_base;
0086     int num;
0087 };
0088 
0089 /* We need four pages to store our wave parameters during a suspend. If
0090  * we're not doing power management, we still need to allocate a page
0091  * for the silence buffer.
0092  */
0093 #ifdef CONFIG_PM_SLEEP
0094 #define SIS_SUSPEND_PAGES   4
0095 #else
0096 #define SIS_SUSPEND_PAGES   1
0097 #endif
0098 
0099 struct sis7019 {
0100     unsigned long ioport;
0101     void __iomem *ioaddr;
0102     int irq;
0103     int codecs_present;
0104 
0105     struct pci_dev *pci;
0106     struct snd_pcm *pcm;
0107     struct snd_card *card;
0108     struct snd_ac97 *ac97[3];
0109 
0110     /* Protect against more than one thread hitting the AC97
0111      * registers (in a more polite manner than pounding the hardware
0112      * semaphore)
0113      */
0114     struct mutex ac97_mutex;
0115 
0116     /* voice_lock protects allocation/freeing of the voice descriptions
0117      */
0118     spinlock_t voice_lock;
0119 
0120     struct voice voices[64];
0121     struct voice capture_voice;
0122 
0123     /* Allocate pages to store the internal wave state during
0124      * suspends. When we're operating, this can be used as a silence
0125      * buffer for a timing channel.
0126      */
0127     void *suspend_state[SIS_SUSPEND_PAGES];
0128 
0129     int silence_users;
0130     dma_addr_t silence_dma_addr;
0131 };
0132 
0133 /* These values are also used by the module param 'codecs' to indicate
0134  * which codecs should be present.
0135  */
0136 #define SIS_PRIMARY_CODEC_PRESENT   0x0001
0137 #define SIS_SECONDARY_CODEC_PRESENT 0x0002
0138 #define SIS_TERTIARY_CODEC_PRESENT  0x0004
0139 
0140 /* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
0141  * documented range of 8-0xfff8 samples. Given that they are 0-based,
0142  * that places our period/buffer range at 9-0xfff9 samples. That makes the
0143  * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
0144  * max samples / min samples gives us the max periods in a buffer.
0145  *
0146  * We'll add a constraint upon open that limits the period and buffer sample
0147  * size to values that are legal for the hardware.
0148  */
0149 static const struct snd_pcm_hardware sis_playback_hw_info = {
0150     .info = (SNDRV_PCM_INFO_MMAP |
0151          SNDRV_PCM_INFO_MMAP_VALID |
0152          SNDRV_PCM_INFO_INTERLEAVED |
0153          SNDRV_PCM_INFO_BLOCK_TRANSFER |
0154          SNDRV_PCM_INFO_SYNC_START |
0155          SNDRV_PCM_INFO_RESUME),
0156     .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
0157             SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
0158     .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
0159     .rate_min = 4000,
0160     .rate_max = 48000,
0161     .channels_min = 1,
0162     .channels_max = 2,
0163     .buffer_bytes_max = (0xfff9 * 4),
0164     .period_bytes_min = 9,
0165     .period_bytes_max = (0xfff9 * 4),
0166     .periods_min = 1,
0167     .periods_max = (0xfff9 / 9),
0168 };
0169 
0170 static const struct snd_pcm_hardware sis_capture_hw_info = {
0171     .info = (SNDRV_PCM_INFO_MMAP |
0172          SNDRV_PCM_INFO_MMAP_VALID |
0173          SNDRV_PCM_INFO_INTERLEAVED |
0174          SNDRV_PCM_INFO_BLOCK_TRANSFER |
0175          SNDRV_PCM_INFO_SYNC_START |
0176          SNDRV_PCM_INFO_RESUME),
0177     .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
0178             SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
0179     .rates = SNDRV_PCM_RATE_48000,
0180     .rate_min = 4000,
0181     .rate_max = 48000,
0182     .channels_min = 1,
0183     .channels_max = 2,
0184     .buffer_bytes_max = (0xfff9 * 4),
0185     .period_bytes_min = 9,
0186     .period_bytes_max = (0xfff9 * 4),
0187     .periods_min = 1,
0188     .periods_max = (0xfff9 / 9),
0189 };
0190 
0191 static void sis_update_sso(struct voice *voice, u16 period)
0192 {
0193     void __iomem *base = voice->ctrl_base;
0194 
0195     voice->sso += period;
0196     if (voice->sso >= voice->buffer_size)
0197         voice->sso -= voice->buffer_size;
0198 
0199     /* Enforce the documented hardware minimum offset */
0200     if (voice->sso < 8)
0201         voice->sso = 8;
0202 
0203     /* The SSO is in the upper 16 bits of the register. */
0204     writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
0205 }
0206 
0207 static void sis_update_voice(struct voice *voice)
0208 {
0209     if (voice->flags & VOICE_SSO_TIMING) {
0210         sis_update_sso(voice, voice->period_size);
0211     } else if (voice->flags & VOICE_SYNC_TIMING) {
0212         int sync;
0213 
0214         /* If we've not hit the end of the virtual period, update
0215          * our records and keep going.
0216          */
0217         if (voice->vperiod > voice->period_size) {
0218             voice->vperiod -= voice->period_size;
0219             if (voice->vperiod < voice->period_size)
0220                 sis_update_sso(voice, voice->vperiod);
0221             else
0222                 sis_update_sso(voice, voice->period_size);
0223             return;
0224         }
0225 
0226         /* Calculate our relative offset between the target and
0227          * the actual CSO value. Since we're operating in a loop,
0228          * if the value is more than half way around, we can
0229          * consider ourselves wrapped.
0230          */
0231         sync = voice->sync_cso;
0232         sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO);
0233         if (sync > (voice->sync_buffer_size / 2))
0234             sync -= voice->sync_buffer_size;
0235 
0236         /* If sync is positive, then we interrupted too early, and
0237          * we'll need to come back in a few samples and try again.
0238          * There's a minimum wait, as it takes some time for the DMA
0239          * engine to startup, etc...
0240          */
0241         if (sync > 0) {
0242             if (sync < 16)
0243                 sync = 16;
0244             sis_update_sso(voice, sync);
0245             return;
0246         }
0247 
0248         /* Ok, we interrupted right on time, or (hopefully) just
0249          * a bit late. We'll adjst our next waiting period based
0250          * on how close we got.
0251          *
0252          * We need to stay just behind the actual channel to ensure
0253          * it really is past a period when we get our interrupt --
0254          * otherwise we'll fall into the early code above and have
0255          * a minimum wait time, which makes us quite late here,
0256          * eating into the user's time to refresh the buffer, esp.
0257          * if using small periods.
0258          *
0259          * If we're less than 9 samples behind, we're on target.
0260          * Otherwise, shorten the next vperiod by the amount we've
0261          * been delayed.
0262          */
0263         if (sync > -9)
0264             voice->vperiod = voice->sync_period_size + 1;
0265         else
0266             voice->vperiod = voice->sync_period_size + sync + 10;
0267 
0268         if (voice->vperiod < voice->buffer_size) {
0269             sis_update_sso(voice, voice->vperiod);
0270             voice->vperiod = 0;
0271         } else
0272             sis_update_sso(voice, voice->period_size);
0273 
0274         sync = voice->sync_cso + voice->sync_period_size;
0275         if (sync >= voice->sync_buffer_size)
0276             sync -= voice->sync_buffer_size;
0277         voice->sync_cso = sync;
0278     }
0279 
0280     snd_pcm_period_elapsed(voice->substream);
0281 }
0282 
0283 static void sis_voice_irq(u32 status, struct voice *voice)
0284 {
0285     int bit;
0286 
0287     while (status) {
0288         bit = __ffs(status);
0289         status >>= bit + 1;
0290         voice += bit;
0291         sis_update_voice(voice);
0292         voice++;
0293     }
0294 }
0295 
0296 static irqreturn_t sis_interrupt(int irq, void *dev)
0297 {
0298     struct sis7019 *sis = dev;
0299     unsigned long io = sis->ioport;
0300     struct voice *voice;
0301     u32 intr, status;
0302 
0303     /* We only use the DMA interrupts, and we don't enable any other
0304      * source of interrupts. But, it is possible to see an interrupt
0305      * status that didn't actually interrupt us, so eliminate anything
0306      * we're not expecting to avoid falsely claiming an IRQ, and an
0307      * ensuing endless loop.
0308      */
0309     intr = inl(io + SIS_GISR);
0310     intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
0311         SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
0312     if (!intr)
0313         return IRQ_NONE;
0314 
0315     do {
0316         status = inl(io + SIS_PISR_A);
0317         if (status) {
0318             sis_voice_irq(status, sis->voices);
0319             outl(status, io + SIS_PISR_A);
0320         }
0321 
0322         status = inl(io + SIS_PISR_B);
0323         if (status) {
0324             sis_voice_irq(status, &sis->voices[32]);
0325             outl(status, io + SIS_PISR_B);
0326         }
0327 
0328         status = inl(io + SIS_RISR);
0329         if (status) {
0330             voice = &sis->capture_voice;
0331             if (!voice->timing)
0332                 snd_pcm_period_elapsed(voice->substream);
0333 
0334             outl(status, io + SIS_RISR);
0335         }
0336 
0337         outl(intr, io + SIS_GISR);
0338         intr = inl(io + SIS_GISR);
0339         intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
0340             SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
0341     } while (intr);
0342 
0343     return IRQ_HANDLED;
0344 }
0345 
0346 static u32 sis_rate_to_delta(unsigned int rate)
0347 {
0348     u32 delta;
0349 
0350     /* This was copied from the trident driver, but it seems its gotten
0351      * around a bit... nevertheless, it works well.
0352      *
0353      * We special case 44100 and 8000 since rounding with the equation
0354      * does not give us an accurate enough value. For 11025 and 22050
0355      * the equation gives us the best answer. All other frequencies will
0356      * also use the equation. JDW
0357      */
0358     if (rate == 44100)
0359         delta = 0xeb3;
0360     else if (rate == 8000)
0361         delta = 0x2ab;
0362     else if (rate == 48000)
0363         delta = 0x1000;
0364     else
0365         delta = DIV_ROUND_CLOSEST(rate << 12, 48000) & 0x0000ffff;
0366     return delta;
0367 }
0368 
0369 static void __sis_map_silence(struct sis7019 *sis)
0370 {
0371     /* Helper function: must hold sis->voice_lock on entry */
0372     if (!sis->silence_users)
0373         sis->silence_dma_addr = dma_map_single(&sis->pci->dev,
0374                         sis->suspend_state[0],
0375                         4096, DMA_TO_DEVICE);
0376     sis->silence_users++;
0377 }
0378 
0379 static void __sis_unmap_silence(struct sis7019 *sis)
0380 {
0381     /* Helper function: must hold sis->voice_lock on entry */
0382     sis->silence_users--;
0383     if (!sis->silence_users)
0384         dma_unmap_single(&sis->pci->dev, sis->silence_dma_addr, 4096,
0385                     DMA_TO_DEVICE);
0386 }
0387 
0388 static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
0389 {
0390     unsigned long flags;
0391 
0392     spin_lock_irqsave(&sis->voice_lock, flags);
0393     if (voice->timing) {
0394         __sis_unmap_silence(sis);
0395         voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
0396                         VOICE_SYNC_TIMING);
0397         voice->timing = NULL;
0398     }
0399     voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
0400     spin_unlock_irqrestore(&sis->voice_lock, flags);
0401 }
0402 
0403 static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
0404 {
0405     /* Must hold the voice_lock on entry */
0406     struct voice *voice;
0407     int i;
0408 
0409     for (i = 0; i < 64; i++) {
0410         voice = &sis->voices[i];
0411         if (voice->flags & VOICE_IN_USE)
0412             continue;
0413         voice->flags |= VOICE_IN_USE;
0414         goto found_one;
0415     }
0416     voice = NULL;
0417 
0418 found_one:
0419     return voice;
0420 }
0421 
0422 static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
0423 {
0424     struct voice *voice;
0425     unsigned long flags;
0426 
0427     spin_lock_irqsave(&sis->voice_lock, flags);
0428     voice = __sis_alloc_playback_voice(sis);
0429     spin_unlock_irqrestore(&sis->voice_lock, flags);
0430 
0431     return voice;
0432 }
0433 
0434 static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
0435                     struct snd_pcm_hw_params *hw_params)
0436 {
0437     struct sis7019 *sis = snd_pcm_substream_chip(substream);
0438     struct snd_pcm_runtime *runtime = substream->runtime;
0439     struct voice *voice = runtime->private_data;
0440     unsigned int period_size, buffer_size;
0441     unsigned long flags;
0442     int needed;
0443 
0444     /* If there are one or two periods per buffer, we don't need a
0445      * timing voice, as we can use the capture channel's interrupts
0446      * to clock out the periods.
0447      */
0448     period_size = params_period_size(hw_params);
0449     buffer_size = params_buffer_size(hw_params);
0450     needed = (period_size != buffer_size &&
0451             period_size != (buffer_size / 2));
0452 
0453     if (needed && !voice->timing) {
0454         spin_lock_irqsave(&sis->voice_lock, flags);
0455         voice->timing = __sis_alloc_playback_voice(sis);
0456         if (voice->timing)
0457             __sis_map_silence(sis);
0458         spin_unlock_irqrestore(&sis->voice_lock, flags);
0459         if (!voice->timing)
0460             return -ENOMEM;
0461         voice->timing->substream = substream;
0462     } else if (!needed && voice->timing) {
0463         sis_free_voice(sis, voice);
0464         voice->timing = NULL;
0465     }
0466 
0467     return 0;
0468 }
0469 
0470 static int sis_playback_open(struct snd_pcm_substream *substream)
0471 {
0472     struct sis7019 *sis = snd_pcm_substream_chip(substream);
0473     struct snd_pcm_runtime *runtime = substream->runtime;
0474     struct voice *voice;
0475 
0476     voice = sis_alloc_playback_voice(sis);
0477     if (!voice)
0478         return -EAGAIN;
0479 
0480     voice->substream = substream;
0481     runtime->private_data = voice;
0482     runtime->hw = sis_playback_hw_info;
0483     snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
0484                         9, 0xfff9);
0485     snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
0486                         9, 0xfff9);
0487     snd_pcm_set_sync(substream);
0488     return 0;
0489 }
0490 
0491 static int sis_substream_close(struct snd_pcm_substream *substream)
0492 {
0493     struct sis7019 *sis = snd_pcm_substream_chip(substream);
0494     struct snd_pcm_runtime *runtime = substream->runtime;
0495     struct voice *voice = runtime->private_data;
0496 
0497     sis_free_voice(sis, voice);
0498     return 0;
0499 }
0500 
0501 static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
0502 {
0503     struct snd_pcm_runtime *runtime = substream->runtime;
0504     struct voice *voice = runtime->private_data;
0505     void __iomem *ctrl_base = voice->ctrl_base;
0506     void __iomem *wave_base = voice->wave_base;
0507     u32 format, dma_addr, control, sso_eso, delta, reg;
0508     u16 leo;
0509 
0510     /* We rely on the PCM core to ensure that the parameters for this
0511      * substream do not change on us while we're programming the HW.
0512      */
0513     format = 0;
0514     if (snd_pcm_format_width(runtime->format) == 8)
0515         format |= SIS_PLAY_DMA_FORMAT_8BIT;
0516     if (!snd_pcm_format_signed(runtime->format))
0517         format |= SIS_PLAY_DMA_FORMAT_UNSIGNED;
0518     if (runtime->channels == 1)
0519         format |= SIS_PLAY_DMA_FORMAT_MONO;
0520 
0521     /* The baseline setup is for a single period per buffer, and
0522      * we add bells and whistles as needed from there.
0523      */
0524     dma_addr = runtime->dma_addr;
0525     leo = runtime->buffer_size - 1;
0526     control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO;
0527     sso_eso = leo;
0528 
0529     if (runtime->period_size == (runtime->buffer_size / 2)) {
0530         control |= SIS_PLAY_DMA_INTR_AT_MLP;
0531     } else if (runtime->period_size != runtime->buffer_size) {
0532         voice->flags |= VOICE_SSO_TIMING;
0533         voice->sso = runtime->period_size - 1;
0534         voice->period_size = runtime->period_size;
0535         voice->buffer_size = runtime->buffer_size;
0536 
0537         control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
0538         control |= SIS_PLAY_DMA_INTR_AT_SSO;
0539         sso_eso |= (runtime->period_size - 1) << 16;
0540     }
0541 
0542     delta = sis_rate_to_delta(runtime->rate);
0543 
0544     /* Ok, we're ready to go, set up the channel.
0545      */
0546     writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
0547     writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE);
0548     writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL);
0549     writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO);
0550 
0551     for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
0552         writel(0, wave_base + reg);
0553 
0554     writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
0555     writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
0556     writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
0557             SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
0558             SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
0559             wave_base + SIS_WAVE_CHANNEL_CONTROL);
0560 
0561     /* Force PCI writes to post. */
0562     readl(ctrl_base);
0563 
0564     return 0;
0565 }
0566 
0567 static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
0568 {
0569     struct sis7019 *sis = snd_pcm_substream_chip(substream);
0570     unsigned long io = sis->ioport;
0571     struct snd_pcm_substream *s;
0572     struct voice *voice;
0573     void *chip;
0574     int starting;
0575     u32 record = 0;
0576     u32 play[2] = { 0, 0 };
0577 
0578     /* No locks needed, as the PCM core will hold the locks on the
0579      * substreams, and the HW will only start/stop the indicated voices
0580      * without changing the state of the others.
0581      */
0582     switch (cmd) {
0583     case SNDRV_PCM_TRIGGER_START:
0584     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0585     case SNDRV_PCM_TRIGGER_RESUME:
0586         starting = 1;
0587         break;
0588     case SNDRV_PCM_TRIGGER_STOP:
0589     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0590     case SNDRV_PCM_TRIGGER_SUSPEND:
0591         starting = 0;
0592         break;
0593     default:
0594         return -EINVAL;
0595     }
0596 
0597     snd_pcm_group_for_each_entry(s, substream) {
0598         /* Make sure it is for us... */
0599         chip = snd_pcm_substream_chip(s);
0600         if (chip != sis)
0601             continue;
0602 
0603         voice = s->runtime->private_data;
0604         if (voice->flags & VOICE_CAPTURE) {
0605             record |= 1 << voice->num;
0606             voice = voice->timing;
0607         }
0608 
0609         /* voice could be NULL if this a recording stream, and it
0610          * doesn't have an external timing channel.
0611          */
0612         if (voice)
0613             play[voice->num / 32] |= 1 << (voice->num & 0x1f);
0614 
0615         snd_pcm_trigger_done(s, substream);
0616     }
0617 
0618     if (starting) {
0619         if (record)
0620             outl(record, io + SIS_RECORD_START_REG);
0621         if (play[0])
0622             outl(play[0], io + SIS_PLAY_START_A_REG);
0623         if (play[1])
0624             outl(play[1], io + SIS_PLAY_START_B_REG);
0625     } else {
0626         if (record)
0627             outl(record, io + SIS_RECORD_STOP_REG);
0628         if (play[0])
0629             outl(play[0], io + SIS_PLAY_STOP_A_REG);
0630         if (play[1])
0631             outl(play[1], io + SIS_PLAY_STOP_B_REG);
0632     }
0633     return 0;
0634 }
0635 
0636 static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
0637 {
0638     struct snd_pcm_runtime *runtime = substream->runtime;
0639     struct voice *voice = runtime->private_data;
0640     u32 cso;
0641 
0642     cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
0643     cso &= 0xffff;
0644     return cso;
0645 }
0646 
0647 static int sis_capture_open(struct snd_pcm_substream *substream)
0648 {
0649     struct sis7019 *sis = snd_pcm_substream_chip(substream);
0650     struct snd_pcm_runtime *runtime = substream->runtime;
0651     struct voice *voice = &sis->capture_voice;
0652     unsigned long flags;
0653 
0654     /* FIXME: The driver only supports recording from one channel
0655      * at the moment, but it could support more.
0656      */
0657     spin_lock_irqsave(&sis->voice_lock, flags);
0658     if (voice->flags & VOICE_IN_USE)
0659         voice = NULL;
0660     else
0661         voice->flags |= VOICE_IN_USE;
0662     spin_unlock_irqrestore(&sis->voice_lock, flags);
0663 
0664     if (!voice)
0665         return -EAGAIN;
0666 
0667     voice->substream = substream;
0668     runtime->private_data = voice;
0669     runtime->hw = sis_capture_hw_info;
0670     runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC];
0671     snd_pcm_limit_hw_rates(runtime);
0672     snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
0673                         9, 0xfff9);
0674     snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
0675                         9, 0xfff9);
0676     snd_pcm_set_sync(substream);
0677     return 0;
0678 }
0679 
0680 static int sis_capture_hw_params(struct snd_pcm_substream *substream,
0681                     struct snd_pcm_hw_params *hw_params)
0682 {
0683     struct sis7019 *sis = snd_pcm_substream_chip(substream);
0684     int rc;
0685 
0686     rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
0687                         params_rate(hw_params));
0688     if (rc)
0689         goto out;
0690 
0691     rc = sis_alloc_timing_voice(substream, hw_params);
0692 
0693 out:
0694     return rc;
0695 }
0696 
0697 static void sis_prepare_timing_voice(struct voice *voice,
0698                     struct snd_pcm_substream *substream)
0699 {
0700     struct sis7019 *sis = snd_pcm_substream_chip(substream);
0701     struct snd_pcm_runtime *runtime = substream->runtime;
0702     struct voice *timing = voice->timing;
0703     void __iomem *play_base = timing->ctrl_base;
0704     void __iomem *wave_base = timing->wave_base;
0705     u16 buffer_size, period_size;
0706     u32 format, control, sso_eso, delta;
0707     u32 vperiod, sso, reg;
0708 
0709     /* Set our initial buffer and period as large as we can given a
0710      * single page of silence.
0711      */
0712     buffer_size = 4096 / runtime->channels;
0713     buffer_size /= snd_pcm_format_size(runtime->format, 1);
0714     period_size = buffer_size;
0715 
0716     /* Initially, we want to interrupt just a bit behind the end of
0717      * the period we're clocking out. 12 samples seems to give a good
0718      * delay.
0719      *
0720      * We want to spread our interrupts throughout the virtual period,
0721      * so that we don't end up with two interrupts back to back at the
0722      * end -- this helps minimize the effects of any jitter. Adjust our
0723      * clocking period size so that the last period is at least a fourth
0724      * of a full period.
0725      *
0726      * This is all moot if we don't need to use virtual periods.
0727      */
0728     vperiod = runtime->period_size + 12;
0729     if (vperiod > period_size) {
0730         u16 tail = vperiod % period_size;
0731         u16 quarter_period = period_size / 4;
0732 
0733         if (tail && tail < quarter_period) {
0734             u16 loops = vperiod / period_size;
0735 
0736             tail = quarter_period - tail;
0737             tail += loops - 1;
0738             tail /= loops;
0739             period_size -= tail;
0740         }
0741 
0742         sso = period_size - 1;
0743     } else {
0744         /* The initial period will fit inside the buffer, so we
0745          * don't need to use virtual periods -- disable them.
0746          */
0747         period_size = runtime->period_size;
0748         sso = vperiod - 1;
0749         vperiod = 0;
0750     }
0751 
0752     /* The interrupt handler implements the timing synchronization, so
0753      * setup its state.
0754      */
0755     timing->flags |= VOICE_SYNC_TIMING;
0756     timing->sync_base = voice->ctrl_base;
0757     timing->sync_cso = runtime->period_size;
0758     timing->sync_period_size = runtime->period_size;
0759     timing->sync_buffer_size = runtime->buffer_size;
0760     timing->period_size = period_size;
0761     timing->buffer_size = buffer_size;
0762     timing->sso = sso;
0763     timing->vperiod = vperiod;
0764 
0765     /* Using unsigned samples with the all-zero silence buffer
0766      * forces the output to the lower rail, killing playback.
0767      * So ignore unsigned vs signed -- it doesn't change the timing.
0768      */
0769     format = 0;
0770     if (snd_pcm_format_width(runtime->format) == 8)
0771         format = SIS_CAPTURE_DMA_FORMAT_8BIT;
0772     if (runtime->channels == 1)
0773         format |= SIS_CAPTURE_DMA_FORMAT_MONO;
0774 
0775     control = timing->buffer_size - 1;
0776     control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO;
0777     sso_eso = timing->buffer_size - 1;
0778     sso_eso |= timing->sso << 16;
0779 
0780     delta = sis_rate_to_delta(runtime->rate);
0781 
0782     /* We've done the math, now configure the channel.
0783      */
0784     writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO);
0785     writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE);
0786     writel(control, play_base + SIS_PLAY_DMA_CONTROL);
0787     writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO);
0788 
0789     for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
0790         writel(0, wave_base + reg);
0791 
0792     writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
0793     writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
0794     writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
0795             SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
0796             SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
0797             wave_base + SIS_WAVE_CHANNEL_CONTROL);
0798 }
0799 
0800 static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
0801 {
0802     struct snd_pcm_runtime *runtime = substream->runtime;
0803     struct voice *voice = runtime->private_data;
0804     void __iomem *rec_base = voice->ctrl_base;
0805     u32 format, dma_addr, control;
0806     u16 leo;
0807 
0808     /* We rely on the PCM core to ensure that the parameters for this
0809      * substream do not change on us while we're programming the HW.
0810      */
0811     format = 0;
0812     if (snd_pcm_format_width(runtime->format) == 8)
0813         format = SIS_CAPTURE_DMA_FORMAT_8BIT;
0814     if (!snd_pcm_format_signed(runtime->format))
0815         format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED;
0816     if (runtime->channels == 1)
0817         format |= SIS_CAPTURE_DMA_FORMAT_MONO;
0818 
0819     dma_addr = runtime->dma_addr;
0820     leo = runtime->buffer_size - 1;
0821     control = leo | SIS_CAPTURE_DMA_LOOP;
0822 
0823     /* If we've got more than two periods per buffer, then we have
0824      * use a timing voice to clock out the periods. Otherwise, we can
0825      * use the capture channel's interrupts.
0826      */
0827     if (voice->timing) {
0828         sis_prepare_timing_voice(voice, substream);
0829     } else {
0830         control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
0831         if (runtime->period_size != runtime->buffer_size)
0832             control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
0833     }
0834 
0835     writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO);
0836     writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE);
0837     writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL);
0838 
0839     /* Force the writes to post. */
0840     readl(rec_base);
0841 
0842     return 0;
0843 }
0844 
0845 static const struct snd_pcm_ops sis_playback_ops = {
0846     .open = sis_playback_open,
0847     .close = sis_substream_close,
0848     .prepare = sis_pcm_playback_prepare,
0849     .trigger = sis_pcm_trigger,
0850     .pointer = sis_pcm_pointer,
0851 };
0852 
0853 static const struct snd_pcm_ops sis_capture_ops = {
0854     .open = sis_capture_open,
0855     .close = sis_substream_close,
0856     .hw_params = sis_capture_hw_params,
0857     .prepare = sis_pcm_capture_prepare,
0858     .trigger = sis_pcm_trigger,
0859     .pointer = sis_pcm_pointer,
0860 };
0861 
0862 static int sis_pcm_create(struct sis7019 *sis)
0863 {
0864     struct snd_pcm *pcm;
0865     int rc;
0866 
0867     /* We have 64 voices, and the driver currently records from
0868      * only one channel, though that could change in the future.
0869      */
0870     rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
0871     if (rc)
0872         return rc;
0873 
0874     pcm->private_data = sis;
0875     strcpy(pcm->name, "SiS7019");
0876     sis->pcm = pcm;
0877 
0878     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops);
0879     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops);
0880 
0881     /* Try to preallocate some memory, but it's not the end of the
0882      * world if this fails.
0883      */
0884     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
0885                        &sis->pci->dev, 64*1024, 128*1024);
0886 
0887     return 0;
0888 }
0889 
0890 static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
0891 {
0892     unsigned long io = sis->ioport;
0893     unsigned short val = 0xffff;
0894     u16 status;
0895     u16 rdy;
0896     int count;
0897     static const u16 codec_ready[3] = {
0898         SIS_AC97_STATUS_CODEC_READY,
0899         SIS_AC97_STATUS_CODEC2_READY,
0900         SIS_AC97_STATUS_CODEC3_READY,
0901     };
0902 
0903     rdy = codec_ready[codec];
0904 
0905 
0906     /* Get the AC97 semaphore -- software first, so we don't spin
0907      * pounding out IO reads on the hardware semaphore...
0908      */
0909     mutex_lock(&sis->ac97_mutex);
0910 
0911     count = 0xffff;
0912     while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
0913         udelay(1);
0914 
0915     if (!count)
0916         goto timeout;
0917 
0918     /* ... and wait for any outstanding commands to complete ...
0919      */
0920     count = 0xffff;
0921     do {
0922         status = inw(io + SIS_AC97_STATUS);
0923         if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
0924             break;
0925 
0926         udelay(1);
0927     } while (--count);
0928 
0929     if (!count)
0930         goto timeout_sema;
0931 
0932     /* ... before sending our command and waiting for it to finish ...
0933      */
0934     outl(cmd, io + SIS_AC97_CMD);
0935     udelay(10);
0936 
0937     count = 0xffff;
0938     while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
0939         udelay(1);
0940 
0941     /* ... and reading the results (if any).
0942      */
0943     val = inl(io + SIS_AC97_CMD) >> 16;
0944 
0945 timeout_sema:
0946     outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
0947 timeout:
0948     mutex_unlock(&sis->ac97_mutex);
0949 
0950     if (!count) {
0951         dev_err(&sis->pci->dev, "ac97 codec %d timeout cmd 0x%08x\n",
0952                     codec, cmd);
0953     }
0954 
0955     return val;
0956 }
0957 
0958 static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
0959                 unsigned short val)
0960 {
0961     static const u32 cmd[3] = {
0962         SIS_AC97_CMD_CODEC_WRITE,
0963         SIS_AC97_CMD_CODEC2_WRITE,
0964         SIS_AC97_CMD_CODEC3_WRITE,
0965     };
0966     sis_ac97_rw(ac97->private_data, ac97->num,
0967             (val << 16) | (reg << 8) | cmd[ac97->num]);
0968 }
0969 
0970 static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
0971 {
0972     static const u32 cmd[3] = {
0973         SIS_AC97_CMD_CODEC_READ,
0974         SIS_AC97_CMD_CODEC2_READ,
0975         SIS_AC97_CMD_CODEC3_READ,
0976     };
0977     return sis_ac97_rw(ac97->private_data, ac97->num,
0978                     (reg << 8) | cmd[ac97->num]);
0979 }
0980 
0981 static int sis_mixer_create(struct sis7019 *sis)
0982 {
0983     struct snd_ac97_bus *bus;
0984     struct snd_ac97_template ac97;
0985     static const struct snd_ac97_bus_ops ops = {
0986         .write = sis_ac97_write,
0987         .read = sis_ac97_read,
0988     };
0989     int rc;
0990 
0991     memset(&ac97, 0, sizeof(ac97));
0992     ac97.private_data = sis;
0993 
0994     rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus);
0995     if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
0996         rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]);
0997     ac97.num = 1;
0998     if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
0999         rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
1000     ac97.num = 2;
1001     if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
1002         rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
1003 
1004     /* If we return an error here, then snd_card_free() should
1005      * free up any ac97 codecs that got created, as well as the bus.
1006      */
1007     return rc;
1008 }
1009 
1010 static void sis_chip_free(struct snd_card *card)
1011 {
1012     struct sis7019 *sis = card->private_data;
1013 
1014     /* Reset the chip, and disable all interrputs.
1015      */
1016     outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
1017     udelay(25);
1018     outl(0, sis->ioport + SIS_GCR);
1019     outl(0, sis->ioport + SIS_GIER);
1020 
1021     /* Now, free everything we allocated.
1022      */
1023     if (sis->irq >= 0)
1024         free_irq(sis->irq, sis);
1025 }
1026 
1027 static int sis_chip_init(struct sis7019 *sis)
1028 {
1029     unsigned long io = sis->ioport;
1030     void __iomem *ioaddr = sis->ioaddr;
1031     unsigned long timeout;
1032     u16 status;
1033     int count;
1034     int i;
1035 
1036     /* Reset the audio controller
1037      */
1038     outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
1039     udelay(25);
1040     outl(0, io + SIS_GCR);
1041 
1042     /* Get the AC-link semaphore, and reset the codecs
1043      */
1044     count = 0xffff;
1045     while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
1046         udelay(1);
1047 
1048     if (!count)
1049         return -EIO;
1050 
1051     outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
1052     udelay(250);
1053 
1054     count = 0xffff;
1055     while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
1056         udelay(1);
1057 
1058     /* Command complete, we can let go of the semaphore now.
1059      */
1060     outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
1061     if (!count)
1062         return -EIO;
1063 
1064     /* Now that we've finished the reset, find out what's attached.
1065      * There are some codec/board combinations that take an extremely
1066      * long time to come up. 350+ ms has been observed in the field,
1067      * so we'll give them up to 500ms.
1068      */
1069     sis->codecs_present = 0;
1070     timeout = msecs_to_jiffies(500) + jiffies;
1071     while (time_before_eq(jiffies, timeout)) {
1072         status = inl(io + SIS_AC97_STATUS);
1073         if (status & SIS_AC97_STATUS_CODEC_READY)
1074             sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
1075         if (status & SIS_AC97_STATUS_CODEC2_READY)
1076             sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
1077         if (status & SIS_AC97_STATUS_CODEC3_READY)
1078             sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
1079 
1080         if (sis->codecs_present == codecs)
1081             break;
1082 
1083         msleep(1);
1084     }
1085 
1086     /* All done, check for errors.
1087      */
1088     if (!sis->codecs_present) {
1089         dev_err(&sis->pci->dev, "could not find any codecs\n");
1090         return -EIO;
1091     }
1092 
1093     if (sis->codecs_present != codecs) {
1094         dev_warn(&sis->pci->dev, "missing codecs, found %0x, expected %0x\n",
1095                      sis->codecs_present, codecs);
1096     }
1097 
1098     /* Let the hardware know that the audio driver is alive,
1099      * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
1100      * record channels. We're going to want to use Variable Rate Audio
1101      * for recording, to avoid needlessly resampling from 48kHZ.
1102      */
1103     outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
1104     outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
1105         SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
1106         SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
1107         SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
1108 
1109     /* All AC97 PCM slots should be sourced from sub-mixer 0.
1110      */
1111     outl(0, io + SIS_AC97_PSR);
1112 
1113     /* There is only one valid DMA setup for a PCI environment.
1114      */
1115     outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
1116 
1117     /* Reset the synchronization groups for all of the channels
1118      * to be asynchronous. If we start doing SPDIF or 5.1 sound, etc.
1119      * we'll need to change how we handle these. Until then, we just
1120      * assign sub-mixer 0 to all playback channels, and avoid any
1121      * attenuation on the audio.
1122      */
1123     outl(0, io + SIS_PLAY_SYNC_GROUP_A);
1124     outl(0, io + SIS_PLAY_SYNC_GROUP_B);
1125     outl(0, io + SIS_PLAY_SYNC_GROUP_C);
1126     outl(0, io + SIS_PLAY_SYNC_GROUP_D);
1127     outl(0, io + SIS_MIXER_SYNC_GROUP);
1128 
1129     for (i = 0; i < 64; i++) {
1130         writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
1131         writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
1132                 SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
1133     }
1134 
1135     /* Don't attenuate any audio set for the wave amplifier.
1136      *
1137      * FIXME: Maximum attenuation is set for the music amp, which will
1138      * need to change if we start using the synth engine.
1139      */
1140     outl(0xffff0000, io + SIS_WEVCR);
1141 
1142     /* Ensure that the wave engine is in normal operating mode.
1143      */
1144     outl(0, io + SIS_WECCR);
1145 
1146     /* Go ahead and enable the DMA interrupts. They won't go live
1147      * until we start a channel.
1148      */
1149     outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
1150         SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
1151 
1152     return 0;
1153 }
1154 
1155 #ifdef CONFIG_PM_SLEEP
1156 static int sis_suspend(struct device *dev)
1157 {
1158     struct snd_card *card = dev_get_drvdata(dev);
1159     struct sis7019 *sis = card->private_data;
1160     void __iomem *ioaddr = sis->ioaddr;
1161     int i;
1162 
1163     snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1164     if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1165         snd_ac97_suspend(sis->ac97[0]);
1166     if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1167         snd_ac97_suspend(sis->ac97[1]);
1168     if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1169         snd_ac97_suspend(sis->ac97[2]);
1170 
1171     /* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
1172      */
1173     if (sis->irq >= 0) {
1174         free_irq(sis->irq, sis);
1175         sis->irq = -1;
1176     }
1177 
1178     /* Save the internal state away
1179      */
1180     for (i = 0; i < 4; i++) {
1181         memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
1182         ioaddr += 4096;
1183     }
1184 
1185     return 0;
1186 }
1187 
1188 static int sis_resume(struct device *dev)
1189 {
1190     struct pci_dev *pci = to_pci_dev(dev);
1191     struct snd_card *card = dev_get_drvdata(dev);
1192     struct sis7019 *sis = card->private_data;
1193     void __iomem *ioaddr = sis->ioaddr;
1194     int i;
1195 
1196     if (sis_chip_init(sis)) {
1197         dev_err(&pci->dev, "unable to re-init controller\n");
1198         goto error;
1199     }
1200 
1201     if (request_irq(pci->irq, sis_interrupt, IRQF_SHARED,
1202             KBUILD_MODNAME, sis)) {
1203         dev_err(&pci->dev, "unable to regain IRQ %d\n", pci->irq);
1204         goto error;
1205     }
1206 
1207     /* Restore saved state, then clear out the page we use for the
1208      * silence buffer.
1209      */
1210     for (i = 0; i < 4; i++) {
1211         memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
1212         ioaddr += 4096;
1213     }
1214 
1215     memset(sis->suspend_state[0], 0, 4096);
1216 
1217     sis->irq = pci->irq;
1218 
1219     if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1220         snd_ac97_resume(sis->ac97[0]);
1221     if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1222         snd_ac97_resume(sis->ac97[1]);
1223     if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1224         snd_ac97_resume(sis->ac97[2]);
1225 
1226     snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1227     return 0;
1228 
1229 error:
1230     snd_card_disconnect(card);
1231     return -EIO;
1232 }
1233 
1234 static SIMPLE_DEV_PM_OPS(sis_pm, sis_suspend, sis_resume);
1235 #define SIS_PM_OPS  &sis_pm
1236 #else
1237 #define SIS_PM_OPS  NULL
1238 #endif /* CONFIG_PM_SLEEP */
1239 
1240 static int sis_alloc_suspend(struct sis7019 *sis)
1241 {
1242     int i;
1243 
1244     /* We need 16K to store the internal wave engine state during a
1245      * suspend, but we don't need it to be contiguous, so play nice
1246      * with the memory system. We'll also use this area for a silence
1247      * buffer.
1248      */
1249     for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
1250         sis->suspend_state[i] = devm_kmalloc(&sis->pci->dev, 4096,
1251                              GFP_KERNEL);
1252         if (!sis->suspend_state[i])
1253             return -ENOMEM;
1254     }
1255     memset(sis->suspend_state[0], 0, 4096);
1256 
1257     return 0;
1258 }
1259 
1260 static int sis_chip_create(struct snd_card *card,
1261                struct pci_dev *pci)
1262 {
1263     struct sis7019 *sis = card->private_data;
1264     struct voice *voice;
1265     int rc;
1266     int i;
1267 
1268     rc = pcim_enable_device(pci);
1269     if (rc)
1270         return rc;
1271 
1272     rc = dma_set_mask(&pci->dev, DMA_BIT_MASK(30));
1273     if (rc < 0) {
1274         dev_err(&pci->dev, "architecture does not support 30-bit PCI busmaster DMA");
1275         return -ENXIO;
1276     }
1277 
1278     mutex_init(&sis->ac97_mutex);
1279     spin_lock_init(&sis->voice_lock);
1280     sis->card = card;
1281     sis->pci = pci;
1282     sis->irq = -1;
1283     sis->ioport = pci_resource_start(pci, 0);
1284 
1285     rc = pci_request_regions(pci, "SiS7019");
1286     if (rc) {
1287         dev_err(&pci->dev, "unable request regions\n");
1288         return rc;
1289     }
1290 
1291     sis->ioaddr = devm_ioremap(&pci->dev, pci_resource_start(pci, 1), 0x4000);
1292     if (!sis->ioaddr) {
1293         dev_err(&pci->dev, "unable to remap MMIO, aborting\n");
1294         return -EIO;
1295     }
1296 
1297     rc = sis_alloc_suspend(sis);
1298     if (rc < 0) {
1299         dev_err(&pci->dev, "unable to allocate state storage\n");
1300         return rc;
1301     }
1302 
1303     rc = sis_chip_init(sis);
1304     if (rc)
1305         return rc;
1306     card->private_free = sis_chip_free;
1307 
1308     rc = request_irq(pci->irq, sis_interrupt, IRQF_SHARED, KBUILD_MODNAME,
1309              sis);
1310     if (rc) {
1311         dev_err(&pci->dev, "unable to allocate irq %d\n", sis->irq);
1312         return rc;
1313     }
1314 
1315     sis->irq = pci->irq;
1316     card->sync_irq = sis->irq;
1317     pci_set_master(pci);
1318 
1319     for (i = 0; i < 64; i++) {
1320         voice = &sis->voices[i];
1321         voice->num = i;
1322         voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
1323         voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
1324     }
1325 
1326     voice = &sis->capture_voice;
1327     voice->flags = VOICE_CAPTURE;
1328     voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
1329     voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
1330 
1331     return 0;
1332 }
1333 
1334 static int __snd_sis7019_probe(struct pci_dev *pci,
1335                    const struct pci_device_id *pci_id)
1336 {
1337     struct snd_card *card;
1338     struct sis7019 *sis;
1339     int rc;
1340 
1341     if (!enable)
1342         return -ENOENT;
1343 
1344     /* The user can specify which codecs should be present so that we
1345      * can wait for them to show up if they are slow to recover from
1346      * the AC97 cold reset. We default to a single codec, the primary.
1347      *
1348      * We assume that SIS_PRIMARY_*_PRESENT matches bits 0-2.
1349      */
1350     codecs &= SIS_PRIMARY_CODEC_PRESENT | SIS_SECONDARY_CODEC_PRESENT |
1351           SIS_TERTIARY_CODEC_PRESENT;
1352     if (!codecs)
1353         codecs = SIS_PRIMARY_CODEC_PRESENT;
1354 
1355     rc = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
1356                    sizeof(*sis), &card);
1357     if (rc < 0)
1358         return rc;
1359 
1360     strcpy(card->driver, "SiS7019");
1361     strcpy(card->shortname, "SiS7019");
1362     rc = sis_chip_create(card, pci);
1363     if (rc)
1364         return rc;
1365 
1366     sis = card->private_data;
1367 
1368     rc = sis_mixer_create(sis);
1369     if (rc)
1370         return rc;
1371 
1372     rc = sis_pcm_create(sis);
1373     if (rc)
1374         return rc;
1375 
1376     snprintf(card->longname, sizeof(card->longname),
1377             "%s Audio Accelerator with %s at 0x%lx, irq %d",
1378             card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
1379             sis->ioport, sis->irq);
1380 
1381     rc = snd_card_register(card);
1382     if (rc)
1383         return rc;
1384 
1385     pci_set_drvdata(pci, card);
1386     return 0;
1387 }
1388 
1389 static int snd_sis7019_probe(struct pci_dev *pci,
1390                  const struct pci_device_id *pci_id)
1391 {
1392     return snd_card_free_on_error(&pci->dev, __snd_sis7019_probe(pci, pci_id));
1393 }
1394 
1395 static struct pci_driver sis7019_driver = {
1396     .name = KBUILD_MODNAME,
1397     .id_table = snd_sis7019_ids,
1398     .probe = snd_sis7019_probe,
1399     .driver = {
1400         .pm = SIS_PM_OPS,
1401     },
1402 };
1403 
1404 module_pci_driver(sis7019_driver);