Commit 5eab6cb0 authored by Takashi Iwai's avatar Takashi Iwai
Browse files

ALSA: sb: Allocate resources with device-managed APIs

This patch converts the resource management in ISA sb drivers 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-55-tiwai@suse.de


Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent ea2bfa29
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -56,9 +56,6 @@ struct snd_emu8000 {
	unsigned long port1;	/* Port usually base+0 */
	unsigned long port2;	/* Port usually at base+0x400 */
	unsigned long port3;	/* Port usually at base+0x800 */
	struct resource *res_port1;
	struct resource *res_port2;
	struct resource *res_port3;
	unsigned short last_reg;/* Last register command */
	spinlock_t reg_lock;

+6 −42
Original line number Diff line number Diff line
@@ -1048,27 +1048,6 @@ snd_emu8000_create_mixer(struct snd_card *card, struct snd_emu8000 *emu)
	return err;
}


/*
 * free resources
 */
static int snd_emu8000_free(struct snd_emu8000 *hw)
{
	release_and_free_resource(hw->res_port1);
	release_and_free_resource(hw->res_port2);
	release_and_free_resource(hw->res_port3);
	kfree(hw);
	return 0;
}

/*
 */
static int snd_emu8000_dev_free(struct snd_device *device)
{
	struct snd_emu8000 *hw = device->device_data;
	return snd_emu8000_free(hw);
}

/*
 * initialize and register emu8000 synth device.
 */
@@ -1079,9 +1058,6 @@ snd_emu8000_new(struct snd_card *card, int index, long port, int seq_ports,
	struct snd_seq_device *awe;
	struct snd_emu8000 *hw;
	int err;
	static const struct snd_device_ops ops = {
		.dev_free = snd_emu8000_dev_free,
	};

	if (awe_ret)
		*awe_ret = NULL;
@@ -1089,7 +1065,7 @@ snd_emu8000_new(struct snd_card *card, int index, long port, int seq_ports,
	if (seq_ports <= 0)
		return 0;

	hw = kzalloc(sizeof(*hw), GFP_KERNEL);
	hw = devm_kzalloc(card->dev, sizeof(*hw), GFP_KERNEL);
	if (hw == NULL)
		return -ENOMEM;
	spin_lock_init(&hw->reg_lock);
@@ -1097,12 +1073,10 @@ snd_emu8000_new(struct snd_card *card, int index, long port, int seq_ports,
	hw->port1 = port;
	hw->port2 = port + 0x400;
	hw->port3 = port + 0x800;
	hw->res_port1 = request_region(hw->port1, 4, "Emu8000-1");
	hw->res_port2 = request_region(hw->port2, 4, "Emu8000-2");
	hw->res_port3 = request_region(hw->port3, 4, "Emu8000-3");
	if (!hw->res_port1 || !hw->res_port2 || !hw->res_port3) {
	if (!devm_request_region(card->dev, hw->port1, 4, "Emu8000-1") ||
	    !devm_request_region(card->dev, hw->port2, 4, "Emu8000-2") ||
	    !devm_request_region(card->dev, hw->port3, 4, "Emu8000-3")) {
		snd_printk(KERN_ERR "sbawe: can't grab ports 0x%lx, 0x%lx, 0x%lx\n", hw->port1, hw->port2, hw->port3);
		snd_emu8000_free(hw);
		return -EBUSY;
	}
	hw->mem_size = 0;
@@ -1115,23 +1089,13 @@ snd_emu8000_new(struct snd_card *card, int index, long port, int seq_ports,
	hw->fm_chorus_depth = 0;
	hw->fm_reverb_depth = 0;

	if (snd_emu8000_detect(hw) < 0) {
		snd_emu8000_free(hw);
	if (snd_emu8000_detect(hw) < 0)
		return -ENODEV;
	}

	snd_emu8000_init_hw(hw);
	err = snd_emu8000_create_mixer(card, hw);
	if (err < 0) {
		snd_emu8000_free(hw);
		return err;
	}
	
	err = snd_device_new(card, SNDRV_DEV_CODEC, hw, &ops);
	if (err < 0) {
		snd_emu8000_free(hw);
	if (err < 0)
		return err;
	}
#if IS_ENABLED(CONFIG_SND_SEQUENCER)
	if (snd_seq_device_new(card, index, SNDRV_SEQ_DEV_ID_EMU8000,
			       sizeof(struct snd_emu8000*), &awe) >= 0) {
+12 −27
Original line number Diff line number Diff line
@@ -226,7 +226,7 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)
	static const int possible_dmas16[] = {5, 7, -1};
	int err, xirq, xdma8, xdma16, xmpu_port, xmpu_irq;

	err = snd_card_new(devptr, index[dev], id[dev], THIS_MODULE,
	err = snd_devm_card_new(devptr, index[dev], id[dev], THIS_MODULE,
				sizeof(struct snd_card_jazz16), &card);
	if (err < 0)
		return err;
@@ -238,8 +238,7 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)
		xirq = snd_legacy_find_free_irq(possible_irqs);
		if (xirq < 0) {
			snd_printk(KERN_ERR "unable to find a free IRQ\n");
			err = -EBUSY;
			goto err_free;
			return -EBUSY;
		}
	}
	xdma8 = dma8[dev];
@@ -247,8 +246,7 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)
		xdma8 = snd_legacy_find_free_dma(possible_dmas8);
		if (xdma8 < 0) {
			snd_printk(KERN_ERR "unable to find a free DMA8\n");
			err = -EBUSY;
			goto err_free;
			return -EBUSY;
		}
	}
	xdma16 = dma16[dev];
@@ -256,8 +254,7 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)
		xdma16 = snd_legacy_find_free_dma(possible_dmas16);
		if (xdma16 < 0) {
			snd_printk(KERN_ERR "unable to find a free DMA16\n");
			err = -EBUSY;
			goto err_free;
			return -EBUSY;
		}
	}

@@ -267,7 +264,7 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)
	err = jazz16_detect_board(port[dev], xmpu_port);
	if (err < 0) {
		printk(KERN_ERR "Media Vision Jazz16 board not detected\n");
		goto err_free;
		return err;
	}
	err = snd_sbdsp_create(card, port[dev], irq[dev],
			       jazz16_interrupt,
@@ -275,7 +272,7 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)
			       SB_HW_JAZZ16,
			       &chip);
	if (err < 0)
		goto err_free;
		return err;

	xmpu_irq = mpu_irq[dev];
	if (xmpu_irq == SNDRV_AUTO_IRQ || mpu_port[dev] == SNDRV_AUTO_PORT)
@@ -283,7 +280,7 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)
	err = jazz16_configure_board(chip, xmpu_irq);
	if (err < 0) {
		printk(KERN_ERR "Media Vision Jazz16 configuration failed\n");
		goto err_free;
		return err;
	}

	jazz16->chip = chip;
@@ -296,10 +293,10 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)

	err = snd_sb8dsp_pcm(chip, 0);
	if (err < 0)
		goto err_free;
		return err;
	err = snd_sbmixer_new(chip);
	if (err < 0)
		goto err_free;
		return err;

	err = snd_opl3_create(card, chip->port, chip->port + 2,
			      OPL3_HW_AUTO, 1, &opl3);
@@ -309,7 +306,7 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)
	else {
		err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
		if (err < 0)
			goto err_free;
			return err;
	}
	if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
		if (mpu_irq[dev] == SNDRV_AUTO_IRQ)
@@ -326,21 +323,10 @@ static int snd_jazz16_probe(struct device *devptr, unsigned int dev)

	err = snd_card_register(card);
	if (err < 0)
		goto err_free;
		return err;

	dev_set_drvdata(devptr, card);
	return 0;

err_free:
	snd_card_free(card);
	return err;
}

static void snd_jazz16_remove(struct device *devptr, unsigned int dev)
{
	struct snd_card *card = dev_get_drvdata(devptr);

	snd_card_free(card);
}

#ifdef CONFIG_PM
@@ -372,7 +358,6 @@ static int snd_jazz16_resume(struct device *pdev, unsigned int n)
static struct isa_driver snd_jazz16_driver = {
	.match		= snd_jazz16_match,
	.probe		= snd_jazz16_probe,
	.remove		= snd_jazz16_remove,
#ifdef CONFIG_PM
	.suspend	= snd_jazz16_suspend,
	.resume		= snd_jazz16_resume,
+7 −35
Original line number Diff line number Diff line
@@ -285,15 +285,6 @@ static int snd_card_sb16_pnp(int dev, struct snd_card_sb16 *acard,

#endif /* CONFIG_PNP */

static void snd_sb16_free(struct snd_card *card)
{
	struct snd_card_sb16 *acard = card->private_data;
        
	if (acard == NULL)
		return;
	release_and_free_resource(acard->fm_res);
}

#ifdef CONFIG_PNP
#define is_isapnp_selected(dev)		isapnp[dev]
#else
@@ -306,11 +297,10 @@ static int snd_sb16_card_new(struct device *devptr, int dev,
	struct snd_card *card;
	int err;

	err = snd_card_new(devptr, index[dev], id[dev], THIS_MODULE,
	err = snd_devm_card_new(devptr, index[dev], id[dev], THIS_MODULE,
				sizeof(struct snd_card_sb16), &card);
	if (err < 0)
		return err;
	card->private_free = snd_sb16_free;
	*cardp = card;
	return 0;
}
@@ -482,17 +472,16 @@ static int snd_sb16_isa_probe1(int dev, struct device *pdev)
	/* non-PnP FM port address is hardwired with base port address */
	fm_port[dev] = port[dev];
	/* block the 0x388 port to avoid PnP conflicts */
	acard->fm_res = request_region(0x388, 4, "SoundBlaster FM");
	acard->fm_res = devm_request_region(card->dev, 0x388, 4,
					    "SoundBlaster FM");
#ifdef SNDRV_SBAWE_EMU8000
	/* non-PnP AWE port address is hardwired with base port address */
	awe_port[dev] = port[dev] + 0x400;
#endif

	err = snd_sb16_probe(card, dev);
	if (err < 0) {
		snd_card_free(card);
	if (err < 0)
		return err;
	}
	dev_set_drvdata(pdev, card);
	return 0;
}
@@ -547,11 +536,6 @@ static int snd_sb16_isa_probe(struct device *pdev, unsigned int dev)
	}
}

static void snd_sb16_isa_remove(struct device *pdev, unsigned int dev)
{
	snd_card_free(dev_get_drvdata(pdev));
}

#ifdef CONFIG_PM
static int snd_sb16_isa_suspend(struct device *dev, unsigned int n,
				pm_message_t state)
@@ -574,7 +558,6 @@ static int snd_sb16_isa_resume(struct device *dev, unsigned int n)
static struct isa_driver snd_sb16_isa_driver = {
	.match		= snd_sb16_isa_match,
	.probe		= snd_sb16_isa_probe,
	.remove		= snd_sb16_isa_remove,
#ifdef CONFIG_PM
	.suspend	= snd_sb16_isa_suspend,
	.resume		= snd_sb16_isa_resume,
@@ -600,15 +583,11 @@ static int snd_sb16_pnp_detect(struct pnp_card_link *pcard,
		if (res < 0)
			return res;
		res = snd_card_sb16_pnp(dev, card->private_data, pcard, pid);
		if (res < 0) {
			snd_card_free(card);
		if (res < 0)
			return res;
		}
		res = snd_sb16_probe(card, dev);
		if (res < 0) {
			snd_card_free(card);
		if (res < 0)
			return res;
		}
		pnp_set_card_drvdata(pcard, card);
		dev++;
		return 0;
@@ -617,12 +596,6 @@ static int snd_sb16_pnp_detect(struct pnp_card_link *pcard,
	return -ENODEV;
}

static void snd_sb16_pnp_remove(struct pnp_card_link *pcard)
{
	snd_card_free(pnp_get_card_drvdata(pcard));
	pnp_set_card_drvdata(pcard, NULL);
}

#ifdef CONFIG_PM
static int snd_sb16_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
{
@@ -643,7 +616,6 @@ static struct pnp_card_driver sb16_pnpc_driver = {
#endif
	.id_table = snd_sb16_pnpids,
	.probe = snd_sb16_pnp_detect,
	.remove = snd_sb16_pnp_remove,
#ifdef CONFIG_PM
	.suspend = snd_sb16_pnp_suspend,
	.resume = snd_sb16_pnp_resume,
+13 −35
Original line number Diff line number Diff line
@@ -54,15 +54,6 @@ static irqreturn_t snd_sb8_interrupt(int irq, void *dev_id)
	}
}

static void snd_sb8_free(struct snd_card *card)
{
	struct snd_sb8 *acard = card->private_data;

	if (acard == NULL)
		return;
	release_and_free_resource(acard->fm_res);
}

static int snd_sb8_match(struct device *pdev, unsigned int dev)
{
	if (!enable[dev])
@@ -86,26 +77,26 @@ static int snd_sb8_probe(struct device *pdev, unsigned int dev)
	struct snd_opl3 *opl3;
	int err;

	err = snd_card_new(pdev, index[dev], id[dev], THIS_MODULE,
	err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
				sizeof(struct snd_sb8), &card);
	if (err < 0)
		return err;
	acard = card->private_data;
	card->private_free = snd_sb8_free;

	/*
	 * Block the 0x388 port to avoid PnP conflicts.
	 * No need to check this value after request_region,
	 * as we never do anything with it.
	 */
	acard->fm_res = request_region(0x388, 4, "SoundBlaster FM");
	acard->fm_res = devm_request_region(card->dev, 0x388, 4,
					    "SoundBlaster FM");

	if (port[dev] != SNDRV_AUTO_PORT) {
		err = snd_sbdsp_create(card, port[dev], irq[dev],
				       snd_sb8_interrupt, dma8[dev],
				       -1, SB_HW_AUTO, &chip);
		if (err < 0)
			goto _err;
			return err;
	} else {
		/* auto-probe legacy ports */
		static const unsigned long possible_ports[] = {
@@ -125,10 +116,8 @@ static int snd_sb8_probe(struct device *pdev, unsigned int dev)
				break;
			}
		}
		if (i >= ARRAY_SIZE(possible_ports)) {
			err = -EINVAL;
			goto _err;
		}
		if (i >= ARRAY_SIZE(possible_ports))
			return -EINVAL;
	}
	acard->chip = chip;
			
@@ -139,17 +128,16 @@ static int snd_sb8_probe(struct device *pdev, unsigned int dev)
		else
			snd_printk(KERN_WARNING "SB 16 chip detected at 0x%lx, try snd-sb16 module\n",
				   port[dev]);
		err = -ENODEV;
		goto _err;
		return -ENODEV;
	}

	err = snd_sb8dsp_pcm(chip, 0);
	if (err < 0)
		goto _err;
		return err;

	err = snd_sbmixer_new(chip);
	if (err < 0)
		goto _err;
		return err;

	if (chip->hardware == SB_HW_10 || chip->hardware == SB_HW_20) {
		err = snd_opl3_create(card, chip->port + 8, 0,
@@ -167,12 +155,12 @@ static int snd_sb8_probe(struct device *pdev, unsigned int dev)
	if (err >= 0) {
		err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
		if (err < 0)
			goto _err;
			return err;
	}

	err = snd_sb8dsp_midi(chip, 0);
	if (err < 0)
		goto _err;
		return err;

	strcpy(card->driver, chip->hardware == SB_HW_PRO ? "SB Pro" : "SB8");
	strcpy(card->shortname, chip->name);
@@ -183,19 +171,10 @@ static int snd_sb8_probe(struct device *pdev, unsigned int dev)

	err = snd_card_register(card);
	if (err < 0)
		goto _err;
		return err;

	dev_set_drvdata(pdev, card);
	return 0;

 _err:
	snd_card_free(card);
	return err;
}

static void snd_sb8_remove(struct device *pdev, unsigned int dev)
{
	snd_card_free(dev_get_drvdata(pdev));
}

#ifdef CONFIG_PM
@@ -229,7 +208,6 @@ static int snd_sb8_resume(struct device *dev, unsigned int n)
static struct isa_driver snd_sb8_driver = {
	.match		= snd_sb8_match,
	.probe		= snd_sb8_probe,
	.remove		= snd_sb8_remove,
#ifdef CONFIG_PM
	.suspend	= snd_sb8_suspend,
	.resume		= snd_sb8_resume,
Loading