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

index 12d02d7..a54b9b2 100644 (file)
@@ -193,11 +193,9 @@ struct nm256 {
        struct snd_card *card;
 
        void __iomem *cport;            /* control port */
-       struct resource *res_cport;     /* its resource */
        unsigned long cport_addr;       /* physical address */
 
        void __iomem *buffer;           /* buffer */
-       struct resource *res_buffer;    /* its resource */
        unsigned long buffer_addr;      /* buffer phyiscal address */
 
        u32 buffer_start;               /* start offset from pci resource 0 */
@@ -1313,8 +1311,9 @@ snd_nm256_mixer(struct nm256 *chip)
                .read = snd_nm256_ac97_read,
        };
 
-       chip->ac97_regs = kcalloc(ARRAY_SIZE(nm256_ac97_init_val),
-                                 sizeof(short), GFP_KERNEL);
+       chip->ac97_regs = devm_kcalloc(chip->card->dev,
+                                      ARRAY_SIZE(nm256_ac97_init_val),
+                                      sizeof(short), GFP_KERNEL);
        if (! chip->ac97_regs)
                return -ENOMEM;
 
@@ -1437,56 +1436,27 @@ static SIMPLE_DEV_PM_OPS(nm256_pm, nm256_suspend, nm256_resume);
 #define NM256_PM_OPS   NULL
 #endif /* CONFIG_PM_SLEEP */
 
-static int snd_nm256_free(struct nm256 *chip)
+static void snd_nm256_free(struct snd_card *card)
 {
+       struct nm256 *chip = card->private_data;
+
        if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
                snd_nm256_playback_stop(chip);
        if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
                snd_nm256_capture_stop(chip);
-
-       if (chip->irq >= 0)
-               free_irq(chip->irq, chip);
-
-       iounmap(chip->cport);
-       iounmap(chip->buffer);
-       release_and_free_resource(chip->res_cport);
-       release_and_free_resource(chip->res_buffer);
-
-       pci_disable_device(chip->pci);
-       kfree(chip->ac97_regs);
-       kfree(chip);
-       return 0;
-}
-
-static int snd_nm256_dev_free(struct snd_device *device)
-{
-       struct nm256 *chip = device->device_data;
-       return snd_nm256_free(chip);
 }
 
 static int
-snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
-                struct nm256 **chip_ret)
+snd_nm256_create(struct snd_card *card, struct pci_dev *pci)
 {
-       struct nm256 *chip;
+       struct nm256 *chip = card->private_data;
        int err, pval;
-       static const struct snd_device_ops ops = {
-               .dev_free =     snd_nm256_dev_free,
-       };
        u32 addr;
 
-       *chip_ret = NULL;
-
-       err = pci_enable_device(pci);
+       err = pcim_enable_device(pci);
        if (err < 0)
                return err;
 
-       chip = kzalloc(sizeof(*chip), GFP_KERNEL);
-       if (chip == NULL) {
-               pci_disable_device(pci);
-               return -ENOMEM;
-       }
-
        chip->card = card;
        chip->pci = pci;
        chip->use_cache = use_cache;
@@ -1508,22 +1478,16 @@ snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
        chip->buffer_addr = pci_resource_start(pci, 0);
        chip->cport_addr = pci_resource_start(pci, 1);
 
+       if (pci_request_regions(pci, card->driver))
+               return err;
+
        /* Init the memory port info.  */
        /* remap control port (#2) */
-       chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE,
-                                            card->driver);
-       if (chip->res_cport == NULL) {
-               dev_err(card->dev, "memory region 0x%lx (size 0x%x) busy\n",
-                          chip->cport_addr, NM_PORT2_SIZE);
-               err = -EBUSY;
-               goto __error;
-       }
-       chip->cport = ioremap(chip->cport_addr, NM_PORT2_SIZE);
-       if (chip->cport == NULL) {
+       chip->cport = devm_ioremap(&pci->dev, chip->cport_addr, NM_PORT2_SIZE);
+       if (!chip->cport) {
                dev_err(card->dev, "unable to map control port %lx\n",
                        chip->cport_addr);
-               err = -ENOMEM;
-               goto __error;
+               return -ENOMEM;
        }
 
        if (!strcmp(card->driver, "NM256AV")) {
@@ -1539,8 +1503,7 @@ snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
                                        " force_ac97=1\n");
                                dev_err(card->dev,
                                        "or try sb16, opl3sa2, or cs423x drivers instead.\n");
-                               err = -ENXIO;
-                               goto __error;
+                               return -ENXIO;
                        }
                }
                chip->buffer_end = 2560 * 1024;
@@ -1572,7 +1535,7 @@ snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
                /* get buffer end pointer from signature */
                err = snd_nm256_peek_for_sig(chip);
                if (err < 0)
-                       goto __error;
+                       return err;
        }
 
        chip->buffer_start = chip->buffer_end - chip->buffer_size;
@@ -1581,21 +1544,12 @@ snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
        dev_info(card->dev, "Mapping port 1 from 0x%x - 0x%x\n",
               chip->buffer_start, chip->buffer_end);
 
-       chip->res_buffer = request_mem_region(chip->buffer_addr,
-                                             chip->buffer_size,
-                                             card->driver);
-       if (chip->res_buffer == NULL) {
-               dev_err(card->dev, "buffer 0x%lx (size 0x%x) busy\n",
-                          chip->buffer_addr, chip->buffer_size);
-               err = -EBUSY;
-               goto __error;
-       }
-       chip->buffer = ioremap(chip->buffer_addr, chip->buffer_size);
-       if (chip->buffer == NULL) {
-               err = -ENOMEM;
+       chip->buffer = devm_ioremap(&pci->dev, chip->buffer_addr,
+                                   chip->buffer_size);
+       if (!chip->buffer) {
                dev_err(card->dev, "unable to map ring buffer at %lx\n",
                        chip->buffer_addr);
-               goto __error;
+               return -ENOMEM;
        }
 
        /* set offsets */
@@ -1618,19 +1572,10 @@ snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
        chip->coeffs_current = 0;
 
        snd_nm256_init_chip(chip);
+       card->private_free = snd_nm256_free;
 
        // pci_set_master(pci); /* needed? */
-       
-       err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
-       if (err < 0)
-               goto __error;
-
-       *chip_ret = chip;
        return 0;
-
-__error:
-       snd_nm256_free(chip);
-       return err;
 }
 
 
@@ -1673,9 +1618,11 @@ static int snd_nm256_probe(struct pci_dev *pci,
                }
        }
 
-       err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
+       err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
+                               sizeof(*chip), &card);
        if (err < 0)
                return err;
+       chip = card->private_data;
 
        switch (pci->device) {
        case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO:
@@ -1689,7 +1636,6 @@ static int snd_nm256_probe(struct pci_dev *pci,
                break;
        default:
                dev_err(&pci->dev, "invalid device id 0x%x\n", pci->device);
-               snd_card_free(card);
                return -EINVAL;
        }
 
@@ -1704,12 +1650,9 @@ static int snd_nm256_probe(struct pci_dev *pci,
                capture_bufsize = 4;
        if (capture_bufsize > 128)
                capture_bufsize = 128;
-       err = snd_nm256_create(card, pci, &chip);
-       if (err < 0) {
-               snd_card_free(card);
+       err = snd_nm256_create(card, pci);
+       if (err < 0)
                return err;
-       }
-       card->private_data = chip;
 
        if (reset_workaround) {
                dev_dbg(&pci->dev, "reset_workaround activated\n");
@@ -1722,15 +1665,11 @@ static int snd_nm256_probe(struct pci_dev *pci,
        }
 
        err = snd_nm256_pcm(chip, 0);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
        err = snd_nm256_mixer(chip);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
 
        sprintf(card->shortname, "NeoMagic %s", card->driver);
        sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d",
@@ -1738,26 +1677,17 @@ static int snd_nm256_probe(struct pci_dev *pci,
                chip->buffer_addr, chip->cport_addr, chip->irq);
 
        err = snd_card_register(card);
-       if (err < 0) {
-               snd_card_free(card);
+       if (err < 0)
                return err;
-       }
 
        pci_set_drvdata(pci, card);
        return 0;
 }
 
-static void snd_nm256_remove(struct pci_dev *pci)
-{
-       snd_card_free(pci_get_drvdata(pci));
-}
-
-
 static struct pci_driver nm256_driver = {
        .name = KBUILD_MODNAME,
        .id_table = snd_nm256_ids,
        .probe = snd_nm256_probe,
-       .remove = snd_nm256_remove,
        .driver = {
                .pm = NM256_PM_OPS,
        },