[PATCH] v4l: 975: apply saa7134-alsa fixes
authorRicardo Cerqueira <v4l@cerqueira.org>
Mon, 14 Nov 2005 00:08:10 +0000 (16:08 -0800)
committerLinus Torvalds <torvalds@g5.osdl.org>
Mon, 14 Nov 2005 02:14:20 +0000 (18:14 -0800)
Merged parts of a patch from Takashi Iwai for an older version of the module.

This patch was adapted and tested by Ricardo Cerqueira.

Signed-off-by: Ricardo Cerqueira <v4l@cerqueira.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@brturbo.com.br>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
drivers/media/video/saa7134/saa7134-alsa.c

index 289ca3a..5707c66 100644 (file)
@@ -30,6 +30,7 @@
 #include <sound/core.h>
 #include <sound/control.h>
 #include <sound/pcm.h>
+#include <sound/pcm_params.h>
 #include <sound/initval.h>
 #include <linux/interrupt.h>
 
@@ -168,8 +169,9 @@ void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status)
        if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
                dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->dmasound.read_count,
                        dev->dmasound.bufsize, dev->dmasound.blocks);
+               spin_unlock(&dev->slock);
                snd_pcm_stop(dev->dmasound.substream,SNDRV_PCM_STATE_XRUN);
-               goto done;
+               return;
        }
 
        /* next block addr */
@@ -252,7 +254,7 @@ static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
        struct saa7134_dev *dev=pcm->dev;
        int err = 0;
 
-       spin_lock_irq(&dev->slock);
+       spin_lock(&dev->slock);
        if (cmd == SNDRV_PCM_TRIGGER_START) {
                /* start dma */
                saa7134_dma_start(dev);
@@ -262,44 +264,11 @@ static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
        } else {
                err = -EINVAL;
        }
-       spin_unlock_irq(&dev->slock);
+       spin_unlock(&dev->slock);
 
        return err;
 }
 
-/*
- * DMA buffer config
- *
- *   Sets the values that will later be used as the size of the buffer,
- *  size of the fragments, and total number of fragments.
- *   Must be called during the preparation stage, before memory is
- *  allocated
- *
- *   - Copied verbatim from saa7134-oss.
- *
- */
-
-static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
-{
-       if (blksize < 0x100)
-               blksize = 0x100;
-       if (blksize > 0x10000)
-               blksize = 0x10000;
-
-       if (blocks < 2)
-               blocks = 2;
-       if ((blksize * blocks) > 1024*1024)
-               blocks = 1024*1024 / blksize;
-
-       dev->dmasound.blocks  = blocks;
-       dev->dmasound.blksize = blksize;
-       dev->dmasound.bufsize = blksize * blocks;
-
-       dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
-               blocks,blksize,blksize * blocks / 1024);
-       return 0;
-}
-
 /*
  * DMA buffer initialization
  *
@@ -325,6 +294,28 @@ static int dsp_buffer_init(struct saa7134_dev *dev)
        return 0;
 }
 
+/*
+ * DMA buffer release
+ *
+ *   Called after closing the device, during snd_card_saa7134_capture_close
+ *
+ */
+
+static int dsp_buffer_free(struct saa7134_dev *dev)
+{
+       if (!dev->dmasound.blksize)
+               BUG();
+
+       videobuf_dma_free(&dev->dmasound.dma);
+
+       dev->dmasound.blocks  = 0;
+       dev->dmasound.blksize = 0;
+       dev->dmasound.bufsize = 0;
+
+       return 0;
+}
+
+
 /*
  * ALSA PCM preparation
  *
@@ -340,74 +331,30 @@ static int dsp_buffer_init(struct saa7134_dev *dev)
 static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
 {
        snd_pcm_runtime_t *runtime = substream->runtime;
-       int err, bswap, sign;
+       int bswap, sign;
        u32 fmt, control;
        snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
        struct saa7134_dev *dev;
        snd_card_saa7134_pcm_t *pcm = runtime->private_data;
-       unsigned long size;
-       unsigned count;
-
-       size = snd_pcm_lib_buffer_bytes(substream);
-       count = snd_pcm_lib_period_bytes(substream);
 
        pcm->dev->dmasound.substream = substream;
 
-       dev=saa7134->dev;
-
-       dsp_buffer_conf(dev,count,(size/count));
-
-       err = dsp_buffer_init(dev);
-       if (0 != err)
-               return err;
-
-       /* prepare buffer */
-       if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->dmasound.dma)))
-               return err;
-       if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt)))
-               goto fail1;
-       if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
-                                             dev->dmasound.dma.sglist,
-                                             dev->dmasound.dma.sglen,
-                                             0)))
-               goto fail2;
-
-
+       dev = saa7134->dev;
 
-       switch (runtime->format) {
-         case SNDRV_PCM_FORMAT_U8:
-         case SNDRV_PCM_FORMAT_S8:
+       if (snd_pcm_format_width(runtime->format) == 8)
                fmt = 0x00;
-               break;
-         case SNDRV_PCM_FORMAT_U16_LE:
-         case SNDRV_PCM_FORMAT_U16_BE:
-         case SNDRV_PCM_FORMAT_S16_LE:
-         case SNDRV_PCM_FORMAT_S16_BE:
+       else
                fmt = 0x01;
-               break;
-         default:
-               err = -EINVAL;
-               return 1;
-       }
 
-       switch (runtime->format) {
-         case SNDRV_PCM_FORMAT_S8:
-         case SNDRV_PCM_FORMAT_S16_LE:
-         case SNDRV_PCM_FORMAT_S16_BE:
+       if (snd_pcm_format_signed(runtime->format))
                sign = 1;
-               break;
-         default:
+       else
                sign = 0;
-               break;
-       }
 
-       switch (runtime->format) {
-         case SNDRV_PCM_FORMAT_U16_BE:
-         case SNDRV_PCM_FORMAT_S16_BE:
-               bswap = 1; break;
-         default:
-               bswap = 0; break;
-       }
+       if (snd_pcm_format_big_endian(runtime->format))
+               bswap = 1;
+       else
+               bswap = 0;
 
        switch (dev->pci->device) {
          case PCI_DEVICE_ID_PHILIPS_SAA7134:
@@ -448,12 +395,6 @@ static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
        if (bswap)
                control |= SAA7134_RS_CONTROL_BSWAP;
 
-       /* I should be able to use runtime->dma_addr in the control
-          byte, but it doesn't work. So I allocate the DMA using the
-          V4L functions, and force ALSA to use that as the DMA area */
-
-       runtime->dma_area = dev->dmasound.dma.vmalloc;
-
        saa_writel(SAA7134_RS_BA1(6),0);
        saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
        saa_writel(SAA7134_RS_PITCH(6),0);
@@ -462,12 +403,6 @@ static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
        dev->dmasound.rate = runtime->rate;
 
        return 0;
- fail2:
-       saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
- fail1:
-       videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
-       return err;
-
 
 }
 
@@ -539,15 +474,76 @@ static void snd_card_saa7134_runtime_free(snd_pcm_runtime_t *runtime)
  *   - One of the ALSA capture callbacks.
  *
  *   Called on initialization, right before the PCM preparation
- *   Usually used in ALSA to allocate the DMA, but since we don't use the
- *  ALSA DMA it does nothing
  *
  */
 
 static int snd_card_saa7134_hw_params(snd_pcm_substream_t * substream,
                                    snd_pcm_hw_params_t * hw_params)
 {
-       return 0;
+       snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
+       struct saa7134_dev *dev;
+       unsigned int period_size, periods;
+       int err;
+
+       period_size = params_period_bytes(hw_params);
+       periods = params_periods(hw_params);
+
+       snd_assert(period_size >= 0x100 && period_size <= 0x10000,
+                  return -EINVAL);
+       snd_assert(periods >= 2, return -EINVAL);
+       snd_assert(period_size * periods <= 1024 * 1024, return -EINVAL);
+
+       dev = saa7134->dev;
+
+       if (dev->dmasound.blocks == periods &&
+           dev->dmasound.blksize == period_size)
+               return 0;
+
+       /* release the old buffer */
+       if (substream->runtime->dma_area) {
+               saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
+               videobuf_dma_pci_unmap(dev->pci, &dev->dmasound.dma);
+               dsp_buffer_free(dev);
+               substream->runtime->dma_area = NULL;
+       }
+       dev->dmasound.blocks  = periods;
+       dev->dmasound.blksize = period_size;
+       dev->dmasound.bufsize = period_size * periods;
+
+       err = dsp_buffer_init(dev);
+       if (0 != err) {
+               dev->dmasound.blocks  = 0;
+               dev->dmasound.blksize = 0;
+               dev->dmasound.bufsize = 0;
+               return err;
+       }
+
+       if (0 != (err = videobuf_dma_pci_map(dev->pci, &dev->dmasound.dma))) {
+               dsp_buffer_free(dev);
+               return err;
+       }
+       if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt))) {
+               videobuf_dma_pci_unmap(dev->pci, &dev->dmasound.dma);
+               dsp_buffer_free(dev);
+               return err;
+       }
+       if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
+                                               dev->dmasound.dma.sglist,
+                                               dev->dmasound.dma.sglen,
+                                               0))) {
+               saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
+               videobuf_dma_pci_unmap(dev->pci, &dev->dmasound.dma);
+               dsp_buffer_free(dev);
+               return err;
+       }
+
+       /* I should be able to use runtime->dma_addr in the control
+          byte, but it doesn't work. So I allocate the DMA using the
+          V4L functions, and force ALSA to use that as the DMA area */
+
+       substream->runtime->dma_area = dev->dmasound.dma.vmalloc;
+
+       return 1;
 
 }
 
@@ -557,33 +553,23 @@ static int snd_card_saa7134_hw_params(snd_pcm_substream_t * substream,
  *   - One of the ALSA capture callbacks.
  *
  *   Called after closing the device, but before snd_card_saa7134_capture_close
- *   Usually used in ALSA to free the DMA, but since we don't use the
- *  ALSA DMA it does nothing
+ *   It stops the DMA audio and releases the buffers.
  *
  */
 
 static int snd_card_saa7134_hw_free(snd_pcm_substream_t * substream)
 {
-       return 0;
-}
-
-/*
- * DMA buffer release
- *
- *   Called after closing the device, during snd_card_saa7134_capture_close
- *
- */
-
-static int dsp_buffer_free(struct saa7134_dev *dev)
-{
-       if (!dev->dmasound.blksize)
-               BUG();
+       snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
+       struct saa7134_dev *dev;
 
-       videobuf_dma_free(&dev->dmasound.dma);
+       dev = saa7134->dev;
 
-       dev->dmasound.blocks  = 0;
-       dev->dmasound.blksize = 0;
-       dev->dmasound.bufsize = 0;
+       if (substream->runtime->dma_area) {
+               saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
+               videobuf_dma_pci_unmap(dev->pci, &dev->dmasound.dma);
+               dsp_buffer_free(dev);
+               substream->runtime->dma_area = NULL;
+       }
 
        return 0;
 }
@@ -593,21 +579,12 @@ static int dsp_buffer_free(struct saa7134_dev *dev)
  *
  *   - One of the ALSA capture callbacks.
  *
- *   Called after closing the device. It stops the DMA audio and releases
- *  the buffers
+ *   Called after closing the device.
  *
  */
 
 static int snd_card_saa7134_capture_close(snd_pcm_substream_t * substream)
 {
-       snd_card_saa7134_t *chip = snd_pcm_substream_chip(substream);
-       struct saa7134_dev *dev = chip->dev;
-
-       /* unlock buffer */
-       saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
-       videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
-
-       dsp_buffer_free(dev);
        return 0;
 }
 
@@ -720,7 +697,6 @@ static int snd_saa7134_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_
 static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
 {
        snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
-       unsigned long flags;
        int change, addr = kcontrol->private_value;
        int left, right;
 
@@ -734,12 +710,12 @@ static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_
                right = 0;
        if (right > 20)
                right = 20;
-       spin_lock_irqsave(&chip->mixer_lock, flags);
+       spin_lock_irq(&chip->mixer_lock);
        change = chip->mixer_volume[addr][0] != left ||
                 chip->mixer_volume[addr][1] != right;
        chip->mixer_volume[addr][0] = left;
        chip->mixer_volume[addr][1] = right;
-       spin_unlock_irqrestore(&chip->mixer_lock, flags);
+       spin_unlock_irq(&chip->mixer_lock);
        return change;
 }
 
@@ -761,13 +737,12 @@ static int snd_saa7134_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_
 static int snd_saa7134_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
 {
        snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
-       unsigned long flags;
        int addr = kcontrol->private_value;
 
-       spin_lock_irqsave(&chip->mixer_lock, flags);
+       spin_lock_irq(&chip->mixer_lock);
        ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
        ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
-       spin_unlock_irqrestore(&chip->mixer_lock, flags);
+       spin_unlock_irq(&chip->mixer_lock);
 
        return 0;
 }
@@ -884,15 +859,10 @@ static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
 
 static void snd_saa7134_free(snd_card_t * card)
 {
-       return;
-}
-
-static int snd_saa7134_dev_free(snd_device_t *device)
-{
-       snd_card_saa7134_t *chip = device->device_data;
+       snd_card_saa7134_t *chip = card->private_data;
 
        if (chip->dev->dmasound.priv_data == NULL)
-               return 0;
+               return;
 
        if (chip->irq >= 0) {
                synchronize_irq(chip->irq);
@@ -901,7 +871,6 @@ static int snd_saa7134_dev_free(snd_device_t *device)
 
        chip->dev->dmasound.priv_data = NULL;
 
-       return 0;
 }
 
 /*
@@ -918,9 +887,6 @@ int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
        snd_card_t *card;
        snd_card_saa7134_t *chip;
        int err;
-       static snd_device_ops_t ops = {
-               .dev_free =     snd_saa7134_dev_free,
-       };
 
 
        if (devnum >= SNDRV_CARDS)
@@ -948,7 +914,6 @@ int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
        chip->card = card;
 
        chip->pci = dev->pci;
-       chip->irq = dev->pci->irq;
        chip->iobase = pci_resource_start(dev->pci, 0);
 
 
@@ -962,11 +927,9 @@ int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
                goto __nodev;
        }
 
-       init_MUTEX(&dev->dmasound.lock);
+       chip->irq = dev->pci->irq;
 
-       if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
-               goto __nodev;
-       }
+       init_MUTEX(&dev->dmasound.lock);
 
        if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
                goto __nodev;