ALSA: au88x0: Allocate resources with device-managed APIs
authorTakashi Iwai <tiwai@suse.de>
Thu, 15 Jul 2021 07:58:49 +0000 (09:58 +0200)
committerTakashi Iwai <tiwai@suse.de>
Mon, 19 Jul 2021 14:16:51 +0000 (16:16 +0200)
This patch converts the resource management in PCI au88x0 driver with
devres as a clean up.  Each manual resource management is converted
with the corresponding devres helper, and the card object release is
managed now via card->private_free instead of a lowlevel snd_device.

This should give no user-visible functional changes.

Link: https://lore.kernel.org/r/20210715075941.23332-28-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/pci/au88x0/au88x0.c

index 1f7fee4..342ef2a 100644 (file)
@@ -123,56 +123,35 @@ static void snd_vortex_workaround(struct pci_dev *vortex, int fix)
 
 // component-destructor
 // (see "Management of Cards and Components")
-static int snd_vortex_dev_free(struct snd_device *device)
+static void snd_vortex_free(struct snd_card *card)
 {
-       vortex_t *vortex = device->device_data;
+       vortex_t *vortex = card->private_data;
 
        vortex_gameport_unregister(vortex);
        vortex_core_shutdown(vortex);
-       // Take down PCI interface.
-       free_irq(vortex->irq, vortex);
-       iounmap(vortex->mmio);
-       pci_release_regions(vortex->pci_dev);
-       pci_disable_device(vortex->pci_dev);
-       kfree(vortex);
-
-       return 0;
 }
 
 // chip-specific constructor
 // (see "Management of Cards and Components")
 static int
-snd_vortex_create(struct snd_card *card, struct pci_dev *pci, vortex_t ** rchip)
+snd_vortex_create(struct snd_card *card, struct pci_dev *pci)
 {
-       vortex_t *chip;
+       vortex_t *chip = card->private_data;
        int err;
-       static const struct snd_device_ops ops = {
-               .dev_free = snd_vortex_dev_free,
-       };
-
-       *rchip = NULL;
 
        // check PCI availability (DMA).
-       err = pci_enable_device(pci);
+       err = pcim_enable_device(pci);
        if (err < 0)
                return err;
        if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32))) {
                dev_err(card->dev, "error to set DMA mask\n");
-               pci_disable_device(pci);
                return -ENXIO;
        }
 
-       chip = kzalloc(sizeof(*chip), GFP_KERNEL);
-       if (chip == NULL) {
-               pci_disable_device(pci);
-               return -ENOMEM;
-       }
-
        chip->card = card;
 
        // initialize the stuff
        chip->pci_dev = pci;
-       chip->io = pci_resource_start(pci, 0);
        chip->vendor = pci->vendor;
        chip->device = pci->device;
        chip->card = card;
@@ -181,16 +160,12 @@ snd_vortex_create(struct snd_card *card, struct pci_dev *pci, vortex_t ** rchip)
        // (1) PCI resource allocation
        // Get MMIO area
        //
-       err = pci_request_regions(pci, CARD_NAME_SHORT);
+       err = pcim_iomap_regions(pci, 1 << 0, CARD_NAME_SHORT);
        if (err)
-               goto regions_out;
+               return err;
 
-       chip->mmio = pci_ioremap_bar(pci, 0);
-       if (!chip->mmio) {
-               dev_err(card->dev, "MMIO area remap failed.\n");
-               err = -ENOMEM;
-               goto ioremap_out;
-       }
+       chip->io = pci_resource_start(pci, 0);
+       chip->mmio = pcim_iomap_table(pci)[0];
 
        /* Init audio core.
         * This must be done before we do request_irq otherwise we can get spurious
@@ -198,44 +173,22 @@ snd_vortex_create(struct snd_card *card, struct pci_dev *pci, vortex_t ** rchip)
        err = vortex_core_init(chip);
        if (err) {
                dev_err(card->dev, "hw core init failed\n");
-               goto core_out;
+               return err;
        }
 
-       err = request_irq(pci->irq, vortex_interrupt,
-                         IRQF_SHARED, KBUILD_MODNAME, chip);
+       err = devm_request_irq(&pci->dev, pci->irq, vortex_interrupt,
+                              IRQF_SHARED, KBUILD_MODNAME, chip);
        if (err) {
                dev_err(card->dev, "cannot grab irq\n");
-               goto irq_out;
+               return err;
        }
        chip->irq = pci->irq;
        card->sync_irq = chip->irq;
+       card->private_free = snd_vortex_free;
 
        pci_set_master(pci);
        // End of PCI setup.
-
-       // Register alsa root device.
-       err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
-       if (err < 0)
-               goto alloc_out;
-
-       *rchip = chip;
-
        return 0;
-
-      alloc_out:
-       free_irq(chip->irq, chip);
-      irq_out:
-       vortex_core_shutdown(chip);
-      core_out:
-       iounmap(chip->mmio);
-      ioremap_out:
-       pci_release_regions(chip->pci_dev);
-      regions_out:
-       pci_disable_device(chip->pci_dev);
-       //FIXME: this not the right place to unregister the gameport
-       vortex_gameport_unregister(chip);
-       kfree(chip);
-       return err;
 }
 
 // constructor -- see "Constructor" sub-section
@@ -255,17 +208,16 @@ snd_vortex_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
                return -ENOENT;
        }
        // (2)
-       err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
-                          0, &card);
+       err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
+                               sizeof(*chip), &card);
        if (err < 0)
                return err;
+       chip = card->private_data;
 
        // (3)
-       err = snd_vortex_create(card, pci, &chip);
-       if (err < 0) {
-               snd_card_free(card);
+       err = snd_vortex_create(card, pci);
+       if (err < 0)
                return err;
-       }
        snd_vortex_workaround(pci, pcifix[dev]);
 
        // Card details needed in snd_vortex_midi
@@ -276,50 +228,37 @@ snd_vortex_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
 
        // (4) Alloc components.
        err = snd_vortex_mixer(chip);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
        // ADB pcm.
        err = snd_vortex_new_pcm(chip, VORTEX_PCM_ADB, NR_PCM);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
 #ifndef CHIP_AU8820
        // ADB SPDIF
        err = snd_vortex_new_pcm(chip, VORTEX_PCM_SPDIF, 1);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
        // A3D
        err = snd_vortex_new_pcm(chip, VORTEX_PCM_A3D, NR_A3D);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
 #endif
        /*
           // ADB I2S
           if ((err = snd_vortex_new_pcm(chip, VORTEX_PCM_I2S, 1)) < 0) {
-          snd_card_free(card);
           return err;
           }
         */
 #ifndef CHIP_AU8810
        // WT pcm.
        err = snd_vortex_new_pcm(chip, VORTEX_PCM_WT, NR_WT);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
 #endif
        err = snd_vortex_midi(chip);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
 
        vortex_gameport_register(chip);
 
@@ -342,15 +281,11 @@ snd_vortex_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
 
        // (5)
        err = pci_read_config_word(pci, PCI_DEVICE_ID, &chip->device);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }       
        err = pci_read_config_word(pci, PCI_VENDOR_ID, &chip->vendor);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
        chip->rev = pci->revision;
 #ifdef CHIP_AU8830
        if ((chip->rev) != 0xfe && (chip->rev) != 0xfa) {
@@ -359,18 +294,14 @@ snd_vortex_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
                       chip->rev);
                dev_alert(card->dev,
                          "Please email the results of 'lspci -vv' to openvortex-dev@nongnu.org.\n");
-               snd_card_free(card);
-               err = -ENODEV;
-               return err;
+               return -ENODEV;
        }
 #endif
 
        // (6)
        err = snd_card_register(card);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
        // (7)
        pci_set_drvdata(pci, card);
        dev++;
@@ -379,18 +310,11 @@ snd_vortex_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
        return 0;
 }
 
-// destructor -- see "Destructor" sub-section
-static void snd_vortex_remove(struct pci_dev *pci)
-{
-       snd_card_free(pci_get_drvdata(pci));
-}
-
 // pci_driver definition
 static struct pci_driver vortex_driver = {
        .name = KBUILD_MODNAME,
        .id_table = snd_vortex_ids,
        .probe = snd_vortex_probe,
-       .remove = snd_vortex_remove,
 };
 
 module_pci_driver(vortex_driver);