ALSA: riptide: Allocate resources with device-managed APIs
authorTakashi Iwai <tiwai@suse.de>
Thu, 15 Jul 2021 07:59:06 +0000 (09:59 +0200)
committerTakashi Iwai <tiwai@suse.de>
Mon, 19 Jul 2021 14:17:05 +0000 (16:17 +0200)
This patch converts the resource management in PCI riptide 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-45-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/pci/riptide/riptide.c

index 709a1a2..5a987c6 100644 (file)
@@ -1798,13 +1798,11 @@ static int snd_riptide_initialize(struct snd_riptide *chip)
        return err;
 }
 
-static int snd_riptide_free(struct snd_riptide *chip)
+static void snd_riptide_free(struct snd_card *card)
 {
+       struct snd_riptide *chip = card->private_data;
        struct cmdif *cif;
 
-       if (!chip)
-               return 0;
-
        cif = chip->cif;
        if (cif) {
                SET_GRESET(cif->hwport);
@@ -1812,39 +1810,19 @@ static int snd_riptide_free(struct snd_riptide *chip)
                UNSET_GRESET(cif->hwport);
                kfree(chip->cif);
        }
-       if (chip->irq >= 0)
-               free_irq(chip->irq, chip);
        release_firmware(chip->fw_entry);
-       release_and_free_resource(chip->res_port);
-       kfree(chip);
-       return 0;
-}
-
-static int snd_riptide_dev_free(struct snd_device *device)
-{
-       struct snd_riptide *chip = device->device_data;
-
-       return snd_riptide_free(chip);
 }
 
 static int
-snd_riptide_create(struct snd_card *card, struct pci_dev *pci,
-                  struct snd_riptide **rchip)
+snd_riptide_create(struct snd_card *card, struct pci_dev *pci)
 {
-       struct snd_riptide *chip;
+       struct snd_riptide *chip = card->private_data;
        struct riptideport *hwport;
        int err;
-       static const struct snd_device_ops ops = {
-               .dev_free = snd_riptide_dev_free,
-       };
 
-       *rchip = NULL;
-       err = pci_enable_device(pci);
+       err = pcim_enable_device(pci);
        if (err < 0)
                return err;
-       chip = kzalloc(sizeof(struct snd_riptide), GFP_KERNEL);
-       if (!chip)
-               return -ENOMEM;
 
        spin_lock_init(&chip->lock);
        chip->card = card;
@@ -1855,24 +1833,20 @@ snd_riptide_create(struct snd_card *card, struct pci_dev *pci,
        chip->received_irqs = 0;
        chip->handled_irqs = 0;
        chip->cif = NULL;
+       card->private_free = snd_riptide_free;
 
-       chip->res_port = request_region(chip->port, 64, "RIPTIDE");
-       if (!chip->res_port) {
-               snd_printk(KERN_ERR
-                          "Riptide: unable to grab region 0x%lx-0x%lx\n",
-                          chip->port, chip->port + 64 - 1);
-               snd_riptide_free(chip);
-               return -EBUSY;
-       }
+       err = pci_request_regions(pci, "RIPTIDE");
+       if (err < 0)
+               return err;
        hwport = (struct riptideport *)chip->port;
        UNSET_AIE(hwport);
 
-       if (request_threaded_irq(pci->irq, snd_riptide_interrupt,
-                                riptide_handleirq, IRQF_SHARED,
-                                KBUILD_MODNAME, chip)) {
+       if (devm_request_threaded_irq(&pci->dev, pci->irq,
+                                     snd_riptide_interrupt,
+                                     riptide_handleirq, IRQF_SHARED,
+                                     KBUILD_MODNAME, chip)) {
                snd_printk(KERN_ERR "Riptide: unable to grab IRQ %d\n",
                           pci->irq);
-               snd_riptide_free(chip);
                return -EBUSY;
        }
        chip->irq = pci->irq;
@@ -1880,18 +1854,9 @@ snd_riptide_create(struct snd_card *card, struct pci_dev *pci,
        chip->device_id = pci->device;
        pci_set_master(pci);
        err = snd_riptide_initialize(chip);
-       if (err < 0) {
-               snd_riptide_free(chip);
-               return err;
-       }
-
-       err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
-       if (err < 0) {
-               snd_riptide_free(chip);
+       if (err < 0)
                return err;
-       }
 
-       *rchip = chip;
        return 0;
 }
 
@@ -2073,20 +2038,20 @@ snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
                return -ENOENT;
        }
 
-       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;
-       err = snd_riptide_create(card, pci, &chip);
+       chip = card->private_data;
+       err = snd_riptide_create(card, pci);
        if (err < 0)
-               goto error;
-       card->private_data = chip;
+               return err;
        err = snd_riptide_pcm(chip, 0);
        if (err < 0)
-               goto error;
+               return err;
        err = snd_riptide_mixer(chip);
        if (err < 0)
-               goto error;
+               return err;
 
        val = LEGACY_ENABLE_ALL;
        if (opl3_port[dev])
@@ -2153,26 +2118,16 @@ snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
        snd_riptide_proc_init(chip);
        err = snd_card_register(card);
        if (err < 0)
-               goto error;
+               return err;
        pci_set_drvdata(pci, card);
        dev++;
        return 0;
-
- error:
-       snd_card_free(card);
-       return err;
-}
-
-static void snd_card_riptide_remove(struct pci_dev *pci)
-{
-       snd_card_free(pci_get_drvdata(pci));
 }
 
 static struct pci_driver driver = {
        .name = KBUILD_MODNAME,
        .id_table = snd_riptide_ids,
        .probe = snd_card_riptide_probe,
-       .remove = snd_card_riptide_remove,
        .driver = {
                .pm = RIPTIDE_PM_OPS,
        },