include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / media / video / cx88 / cx88-alsa.c
index 33dd4cb..33082c9 100644 (file)
@@ -7,7 +7,7 @@
  *    (c) 2005,2006 Ricardo Cerqueira <v4l@cerqueira.org>
  *    (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
  *    Based on a dummy cx88 module by Gerd Knorr <kraxel@bytesex.org>
- *    Based on dummy.c by Jaroslav Kysela <perex@suse.cz>
+ *    Based on dummy.c by Jaroslav Kysela <perex@perex.cz>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include <linux/init.h>
 #include <linux/device.h>
 #include <linux/interrupt.h>
+#include <linux/vmalloc.h>
 #include <linux/dma-mapping.h>
 #include <linux/pci.h>
+#include <linux/slab.h>
 
 #include <asm/delay.h>
-#include <sound/driver.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
 #include <sound/control.h>
 #include <sound/initval.h>
+#include <sound/tlv.h>
 
 #include "cx88.h"
 #include "cx88-reg.h"
        Data type declarations - Can be moded to a header file later
  ****************************************************************************/
 
-/* These can be replaced after done */
-#define MIXER_ADDR_LAST MAX_CX88_INPUT
-
 struct cx88_audio_dev {
        struct cx88_core           *core;
        struct cx88_dmaqueue       q;
-       u64 starttime;
 
        /* pci i/o */
        struct pci_dev             *pci;
@@ -75,10 +73,7 @@ struct cx88_audio_dev {
        unsigned int               period_size;
        unsigned int               num_periods;
 
-       struct videobuf_dmabuf     dma_risc;
-
-       int                        mixer_volume[MIXER_ADDR_LAST+1][2];
-       int                        capture_source[MIXER_ADDR_LAST+1][2];
+       struct videobuf_dmabuf     *dma_risc;
 
        struct cx88_buffer         *buf;
 
@@ -95,7 +90,6 @@ typedef struct cx88_audio_dev snd_cx88_card_t;
 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;     /* Index 0-MAX */
 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;      /* ID for this card */
 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
-static struct snd_card *snd_cx88_cards[SNDRV_CARDS];
 
 module_param_array(enable, bool, NULL, 0444);
 MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled.");
@@ -289,11 +283,12 @@ static int dsp_buffer_free(snd_cx88_card_t *chip)
        BUG_ON(!chip->dma_size);
 
        dprintk(2,"Freeing buffer\n");
-       videobuf_pci_dma_unmap(chip->pci, &chip->dma_risc);
-       videobuf_dma_free(&chip->dma_risc);
+       videobuf_sg_dma_unmap(&chip->pci->dev, chip->dma_risc);
+       videobuf_dma_free(chip->dma_risc);
        btcx_riscmem_free(chip->pci,&chip->buf->risc);
        kfree(chip->buf);
 
+       chip->dma_risc = NULL;
        chip->dma_size = 0;
 
        return 0;
@@ -337,6 +332,12 @@ static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
        struct snd_pcm_runtime *runtime = substream->runtime;
        int err;
 
+       if (!chip) {
+               printk(KERN_ERR "BUG: cx88 can't find device struct."
+                               " Can't proceed with open\n");
+               return -ENODEV;
+       }
+
        err = snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS);
        if (err < 0)
                goto _error;
@@ -373,6 +374,8 @@ static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
                              struct snd_pcm_hw_params * hw_params)
 {
        snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
+       struct videobuf_dmabuf *dma;
+
        struct cx88_buffer *buf;
        int ret;
 
@@ -388,7 +391,7 @@ static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
        BUG_ON(!chip->dma_size);
        BUG_ON(chip->num_periods & (chip->num_periods-1));
 
-       buf = kzalloc(sizeof(*buf), GFP_KERNEL);
+       buf = videobuf_sg_alloc(sizeof(*buf));
        if (NULL == buf)
                return -ENOMEM;
 
@@ -399,17 +402,18 @@ static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
        buf->vb.height = chip->num_periods;
        buf->vb.size   = chip->dma_size;
 
-       videobuf_dma_init(&buf->vb.dma);
-       ret = videobuf_dma_init_kernel(&buf->vb.dma, PCI_DMA_FROMDEVICE,
+       dma = videobuf_to_dma(&buf->vb);
+       videobuf_dma_init(dma);
+       ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
                        (PAGE_ALIGN(buf->vb.size) >> PAGE_SHIFT));
        if (ret < 0)
                goto error;
 
-       ret = videobuf_pci_dma_map(chip->pci,&buf->vb.dma);
+       ret = videobuf_sg_dma_map(&chip->pci->dev, dma);
        if (ret < 0)
                goto error;
 
-       ret = cx88_risc_databuffer(chip->pci, &buf->risc, buf->vb.dma.sglist,
+       ret = cx88_risc_databuffer(chip->pci, &buf->risc, dma->sglist,
                                   buf->vb.width, buf->vb.height, 1);
        if (ret < 0)
                goto error;
@@ -418,12 +422,14 @@ static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
        buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
        buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
 
-       buf->vb.state = STATE_PREPARED;
+       buf->vb.state = VIDEOBUF_PREPARED;
 
        chip->buf = buf;
-       chip->dma_risc = buf->vb.dma;
+       chip->dma_risc = dma;
 
-       substream->runtime->dma_area = chip->dma_risc.vmalloc;
+       substream->runtime->dma_area = chip->dma_risc->vmalloc;
+       substream->runtime->dma_bytes = chip->dma_size;
+       substream->runtime->dma_addr = 0;
        return 0;
 
 error:
@@ -494,13 +500,23 @@ static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
 
        count = atomic_read(&chip->count);
 
-//     dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __FUNCTION__,
+//     dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __func__,
 //             count, new, count & (runtime->periods-1),
 //             runtime->period_size * (count & (runtime->periods-1)));
        return runtime->period_size * (count & (runtime->periods-1));
 }
 
 /*
+ * page callback (needed for mmap)
+ */
+static struct page *snd_cx88_page(struct snd_pcm_substream *substream,
+                               unsigned long offset)
+{
+       void *pageptr = substream->runtime->dma_area + offset;
+       return vmalloc_to_page(pageptr);
+}
+
+/*
  * operators
  */
 static struct snd_pcm_ops snd_cx88_pcm_ops = {
@@ -512,6 +528,7 @@ static struct snd_pcm_ops snd_cx88_pcm_ops = {
        .prepare = snd_cx88_prepare,
        .trigger = snd_cx88_card_trigger,
        .pointer = snd_cx88_pointer,
+       .page = snd_cx88_page,
 };
 
 /*
@@ -535,58 +552,128 @@ static int __devinit snd_cx88_pcm(snd_cx88_card_t *chip, int device, char *name)
 /****************************************************************************
                                CONTROL INTERFACE
  ****************************************************************************/
-static int snd_cx88_capture_volume_info(struct snd_kcontrol *kcontrol,
-                                       struct snd_ctl_elem_info *info)
+static int snd_cx88_volume_info(struct snd_kcontrol *kcontrol,
+                               struct snd_ctl_elem_info *info)
 {
        info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
-       info->count = 1;
+       info->count = 2;
        info->value.integer.min = 0;
        info->value.integer.max = 0x3f;
 
        return 0;
 }
 
-/* OK - TODO: test it */
-static int snd_cx88_capture_volume_get(struct snd_kcontrol *kcontrol,
-                                      struct snd_ctl_elem_value *value)
+static int snd_cx88_volume_get(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *value)
 {
        snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
        struct cx88_core *core=chip->core;
+       int vol = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f),
+           bal = cx_read(AUD_BAL_CTL);
 
-       value->value.integer.value[0] = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f);
+       value->value.integer.value[(bal & 0x40) ? 0 : 1] = vol;
+       vol -= (bal & 0x3f);
+       value->value.integer.value[(bal & 0x40) ? 1 : 0] = vol < 0 ? 0 : vol;
 
        return 0;
 }
 
 /* OK - TODO: test it */
-static int snd_cx88_capture_volume_put(struct snd_kcontrol *kcontrol,
-                                      struct snd_ctl_elem_value *value)
+static int snd_cx88_volume_put(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *value)
 {
        snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
        struct cx88_core *core=chip->core;
-       int v;
-       u32 old_control;
-
+       int left, right, v, b;
+       int changed = 0;
+       u32 old;
+
+       left = value->value.integer.value[0] & 0x3f;
+       right = value->value.integer.value[1] & 0x3f;
+       b = right - left;
+       if (b < 0) {
+           v = 0x3f - left;
+           b = (-b) | 0x40;
+       } else {
+           v = 0x3f - right;
+       }
        /* Do we really know this will always be called with IRQs on? */
        spin_lock_irq(&chip->reg_lock);
+       old = cx_read(AUD_VOL_CTL);
+       if (v != (old & 0x3f)) {
+           cx_write(AUD_VOL_CTL, (old & ~0x3f) | v);
+           changed = 1;
+       }
+       if (cx_read(AUD_BAL_CTL) != b) {
+           cx_write(AUD_BAL_CTL, b);
+           changed = 1;
+       }
+       spin_unlock_irq(&chip->reg_lock);
 
-       old_control = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f);
-       v = 0x3f - (value->value.integer.value[0] & 0x3f);
-       cx_andor(AUD_VOL_CTL, 0x3f, v);
+       return changed;
+}
 
-       spin_unlock_irq(&chip->reg_lock);
+static const DECLARE_TLV_DB_SCALE(snd_cx88_db_scale, -6300, 100, 0);
 
-       return v != old_control;
+static struct snd_kcontrol_new snd_cx88_volume = {
+       .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+       .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+                 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
+       .name = "Playback Volume",
+       .info = snd_cx88_volume_info,
+       .get = snd_cx88_volume_get,
+       .put = snd_cx88_volume_put,
+       .tlv.p = snd_cx88_db_scale,
+};
+
+static int snd_cx88_switch_get(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *value)
+{
+       snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
+       struct cx88_core *core = chip->core;
+       u32 bit = kcontrol->private_value;
+
+       value->value.integer.value[0] = !(cx_read(AUD_VOL_CTL) & bit);
+       return 0;
 }
 
-static struct snd_kcontrol_new snd_cx88_capture_volume = {
+static int snd_cx88_switch_put(struct snd_kcontrol *kcontrol,
+                                      struct snd_ctl_elem_value *value)
+{
+       snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
+       struct cx88_core *core = chip->core;
+       u32 bit = kcontrol->private_value;
+       int ret = 0;
+       u32 vol;
+
+       spin_lock_irq(&chip->reg_lock);
+       vol = cx_read(AUD_VOL_CTL);
+       if (value->value.integer.value[0] != !(vol & bit)) {
+               vol ^= bit;
+               cx_write(AUD_VOL_CTL, vol);
+               ret = 1;
+       }
+       spin_unlock_irq(&chip->reg_lock);
+       return ret;
+}
+
+static struct snd_kcontrol_new snd_cx88_dac_switch = {
        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
-       .name = "Capture Volume",
-       .info = snd_cx88_capture_volume_info,
-       .get = snd_cx88_capture_volume_get,
-       .put = snd_cx88_capture_volume_put,
+       .name = "Playback Switch",
+       .info = snd_ctl_boolean_mono_info,
+       .get = snd_cx88_switch_get,
+       .put = snd_cx88_switch_put,
+       .private_value = (1<<8),
 };
 
+static struct snd_kcontrol_new snd_cx88_source_switch = {
+       .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+       .name = "Capture Switch",
+       .info = snd_ctl_boolean_mono_info,
+       .get = snd_cx88_switch_get,
+       .put = snd_cx88_switch_put,
+       .private_value = (1<<6),
+};
 
 /****************************************************************************
                        Basic Flow for Sound Devices
@@ -611,10 +698,8 @@ MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
 static int snd_cx88_free(snd_cx88_card_t *chip)
 {
 
-       if (chip->irq >= 0){
-               synchronize_irq(chip->irq);
+       if (chip->irq >= 0)
                free_irq(chip->irq, chip);
-       }
 
        cx88_core_put(chip->core,chip->pci);
 
@@ -660,11 +745,10 @@ static int __devinit snd_cx88_create(struct snd_card *card,
        core = cx88_core_get(pci);
        if (NULL == core) {
                err = -EINVAL;
-               kfree (chip);
                return err;
        }
 
-       if (!pci_dma_supported(pci,DMA_32BIT_MASK)) {
+       if (!pci_dma_supported(pci,DMA_BIT_MASK(32))) {
                dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
                err = -EIO;
                cx88_core_put(core,pci);
@@ -722,28 +806,30 @@ static int __devinit cx88_audio_initdev(struct pci_dev *pci,
                return (-ENOENT);
        }
 
-       card = snd_card_new(index[devno], id[devno], THIS_MODULE, sizeof(snd_cx88_card_t));
-       if (!card)
-               return (-ENOMEM);
+       err = snd_card_create(index[devno], id[devno], THIS_MODULE,
+                             sizeof(snd_cx88_card_t), &card);
+       if (err < 0)
+               return err;
 
        card->private_free = snd_cx88_dev_free;
 
        err = snd_cx88_create(card, pci, &chip);
        if (err < 0)
-               return (err);
+               goto error;
 
        err = snd_cx88_pcm(chip, 0, "CX88 Digital");
+       if (err < 0)
+               goto error;
 
-       if (err < 0) {
-               snd_card_free(card);
-               return (err);
-       }
-
-       err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_capture_volume, chip));
-       if (err < 0) {
-               snd_card_free(card);
-               return (err);
-       }
+       err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_volume, chip));
+       if (err < 0)
+               goto error;
+       err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_dac_switch, chip));
+       if (err < 0)
+               goto error;
+       err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_source_switch, chip));
+       if (err < 0)
+               goto error;
 
        strcpy (card->driver, "CX88x");
        sprintf(card->shortname, "Conexant CX%x", pci->device);
@@ -755,16 +841,16 @@ static int __devinit cx88_audio_initdev(struct pci_dev *pci,
               card->driver,devno);
 
        err = snd_card_register(card);
-       if (err < 0) {
-               snd_card_free(card);
-               return (err);
-       }
-       snd_cx88_cards[devno] = card;
-
+       if (err < 0)
+               goto error;
        pci_set_drvdata(pci,card);
 
        devno++;
        return 0;
+
+error:
+       snd_card_free(card);
+       return err;
 }
 /*
  * ALSA destructor
@@ -788,7 +874,7 @@ static struct pci_driver cx88_audio_pci_driver = {
        .name     = "cx88_audio",
        .id_table = cx88_audio_pci_tbl,
        .probe    = cx88_audio_initdev,
-       .remove   = cx88_audio_finidev,
+       .remove   = __devexit_p(cx88_audio_finidev),
 };
 
 /****************************************************************************
@@ -798,7 +884,7 @@ static struct pci_driver cx88_audio_pci_driver = {
 /*
  * module init
  */
-static int cx88_audio_init(void)
+static int __init cx88_audio_init(void)
 {
        printk(KERN_INFO "cx2388x alsa driver version %d.%d.%d loaded\n",
               (CX88_VERSION_CODE >> 16) & 0xff,
@@ -814,9 +900,8 @@ static int cx88_audio_init(void)
 /*
  * module remove
  */
-static void cx88_audio_fini(void)
+static void __exit cx88_audio_fini(void)
 {
-
        pci_unregister_driver(&cx88_audio_pci_driver);
 }