Commit c19935f0 authored by Takashi Iwai's avatar Takashi Iwai

ALSA: nm256: Allocate resources with device-managed APIs

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