diff mbox series

[v2,14/79] ALSA: cs4281: Allocate resources with device-managed APIs

Message ID 20210715075941.23332-15-tiwai@suse.de (mailing list archive)
State New, archived
Headers show
Series ALSA: More devres usages | expand

Commit Message

Takashi Iwai July 15, 2021, 7:58 a.m. UTC
This patch converts the resource management in PCI cs4281 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.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/pci/cs4281.c | 112 ++++++++++-----------------------------------
 1 file changed, 24 insertions(+), 88 deletions(-)

Comments

Nathan Chancellor July 20, 2021, 7:46 p.m. UTC | #1
On Thu, Jul 15, 2021 at 09:58:36AM +0200, Takashi Iwai wrote:
> This patch converts the resource management in PCI cs4281 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.
> 
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> ---
>  sound/pci/cs4281.c | 112 ++++++++++-----------------------------------
>  1 file changed, 24 insertions(+), 88 deletions(-)
> 
> diff --git a/sound/pci/cs4281.c b/sound/pci/cs4281.c
> index e122a168c148..f338caf98354 100644
> --- a/sound/pci/cs4281.c
> +++ b/sound/pci/cs4281.c
> @@ -1268,8 +1268,10 @@ static inline int snd_cs4281_create_gameport(struct cs4281 *chip) { return -ENOS
>  static inline void snd_cs4281_free_gameport(struct cs4281 *chip) { }
>  #endif /* IS_REACHABLE(CONFIG_GAMEPORT) */
>  
> -static int snd_cs4281_free(struct cs4281 *chip)
> +static void snd_cs4281_free(struct snd_card *card)
>  {
> +	struct cs4281 *chip = card->private_data;
> +
>  	snd_cs4281_free_gameport(chip);
>  
>  	/* Mask interrupts */
> @@ -1278,49 +1280,20 @@ static int snd_cs4281_free(struct cs4281 *chip)
>  	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
>  	/* Sound System Power Management - Turn Everything OFF */
>  	snd_cs4281_pokeBA0(chip, BA0_SSPM, 0);
> -	/* PCI interface - D3 state */
> -	pci_set_power_state(chip->pci, PCI_D3hot);
> -
> -	if (chip->irq >= 0)
> -		free_irq(chip->irq, chip);
> -	iounmap(chip->ba0);
> -	iounmap(chip->ba1);
> -	pci_release_regions(chip->pci);
> -	pci_disable_device(chip->pci);
> -
> -	kfree(chip);
> -	return 0;
> -}
> -
> -static int snd_cs4281_dev_free(struct snd_device *device)
> -{
> -	struct cs4281 *chip = device->device_data;
> -	return snd_cs4281_free(chip);
>  }
>  
>  static int snd_cs4281_chip_init(struct cs4281 *chip); /* defined below */
>  
>  static int snd_cs4281_create(struct snd_card *card,
>  			     struct pci_dev *pci,
> -			     struct cs4281 **rchip,
>  			     int dual_codec)
>  {
>  	struct cs4281 *chip;
> -	unsigned int tmp;
>  	int err;
> -	static const struct snd_device_ops ops = {
> -		.dev_free =	snd_cs4281_dev_free,
> -	};
>  
> -	*rchip = 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;
> -	}
>  	spin_lock_init(&chip->reg_lock);
>  	chip->card = card;

clang warns:

sound/pci/cs4281.c:1298:2: error: variable 'chip' is uninitialized when used here [-Werror,-Wuninitialized]
        chip->card = card;
        ^~~~
sound/pci/cs4281.c:1291:21: note: initialize the variable 'chip' to silence this warning
        struct cs4281 *chip;
                           ^
                            = NULL
1 error generated.

>  	chip->pci = pci;
> @@ -1332,46 +1305,29 @@ static int snd_cs4281_create(struct snd_card *card,
>  	}
>  	chip->dual_codec = dual_codec;
>  
> -	err = pci_request_regions(pci, "CS4281");
> -	if (err < 0) {
> -		kfree(chip);
> -		pci_disable_device(pci);
> +	err = pcim_iomap_regions(pci, 0x03, "CS4281"); /* 2 BARs */
> +	if (err < 0)
>  		return err;
> -	}
>  	chip->ba0_addr = pci_resource_start(pci, 0);
>  	chip->ba1_addr = pci_resource_start(pci, 1);
>  
> -	chip->ba0 = pci_ioremap_bar(pci, 0);
> -	chip->ba1 = pci_ioremap_bar(pci, 1);
> -	if (!chip->ba0 || !chip->ba1) {
> -		snd_cs4281_free(chip);
> -		return -ENOMEM;
> -	}
> +	chip->ba0 = pcim_iomap_table(pci)[0];
> +	chip->ba1 = pcim_iomap_table(pci)[1];
>  	
> -	if (request_irq(pci->irq, snd_cs4281_interrupt, IRQF_SHARED,
> -			KBUILD_MODNAME, chip)) {
> +	if (devm_request_irq(&pci->dev, pci->irq, snd_cs4281_interrupt,
> +			     IRQF_SHARED, KBUILD_MODNAME, chip)) {
>  		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
> -		snd_cs4281_free(chip);
>  		return -ENOMEM;
>  	}
>  	chip->irq = pci->irq;
>  	card->sync_irq = chip->irq;
> +	card->private_free = snd_cs4281_free;
>  
> -	tmp = snd_cs4281_chip_init(chip);
> -	if (tmp) {
> -		snd_cs4281_free(chip);
> -		return tmp;
> -	}
> -
> -	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
> -	if (err < 0) {
> -		snd_cs4281_free(chip);
> +	err = snd_cs4281_chip_init(chip);
> +	if (err)
>  		return err;
> -	}
>  
>  	snd_cs4281_proc_init(chip);
> -
> -	*rchip = chip;
>  	return 0;
>  }
>  
> @@ -1887,46 +1843,34 @@ static int snd_cs4281_probe(struct pci_dev *pci,
>  		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;
> +	chip = card->private_data;
>  
> -	err = snd_cs4281_create(card, pci, &chip, dual_codec[dev]);
> -	if (err < 0) {
> -		snd_card_free(card);
> +	err = snd_cs4281_create(card, pci, dual_codec[dev]);
> +	if (err < 0)
>  		return err;
> -	}
> -	card->private_data = chip;
>  
>  	err = snd_cs4281_mixer(chip);
> -	if (err < 0) {
> -		snd_card_free(card);
> +	if (err < 0)
>  		return err;
> -	}
>  	err = snd_cs4281_pcm(chip, 0);
> -	if (err < 0) {
> -		snd_card_free(card);
> +	if (err < 0)
>  		return err;
> -	}
>  	err = snd_cs4281_midi(chip, 0);
> -	if (err < 0) {
> -		snd_card_free(card);
> +	if (err < 0)
>  		return err;
> -	}
>  	err = snd_opl3_new(card, OPL3_HW_OPL3_CS4281, &opl3);
> -	if (err < 0) {
> -		snd_card_free(card);
> +	if (err < 0)
>  		return err;
> -	}
>  	opl3->private_data = chip;
>  	opl3->command = snd_cs4281_opl3_command;
>  	snd_opl3_init(opl3);
>  	err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
> -	if (err < 0) {
> -		snd_card_free(card);
> +	if (err < 0)
>  		return err;
> -	}
>  	snd_cs4281_create_gameport(chip);
>  	strcpy(card->driver, "CS4281");
>  	strcpy(card->shortname, "Cirrus Logic CS4281");
> @@ -1936,21 +1880,14 @@ static int snd_cs4281_probe(struct pci_dev *pci,
>  		chip->irq);
>  
>  	err = snd_card_register(card);
> -	if (err < 0) {
> -		snd_card_free(card);
> +	if (err < 0)
>  		return err;
> -	}
>  
>  	pci_set_drvdata(pci, card);
>  	dev++;
>  	return 0;
>  }
>  
> -static void snd_cs4281_remove(struct pci_dev *pci)
> -{
> -	snd_card_free(pci_get_drvdata(pci));
> -}
> -
>  /*
>   * Power Management
>   */
> @@ -2054,7 +1991,6 @@ static struct pci_driver cs4281_driver = {
>  	.name = KBUILD_MODNAME,
>  	.id_table = snd_cs4281_ids,
>  	.probe = snd_cs4281_probe,
> -	.remove = snd_cs4281_remove,
>  	.driver = {
>  		.pm = CS4281_PM_OPS,
>  	},
> -- 
> 2.26.2
Takashi Iwai July 20, 2021, 10:17 p.m. UTC | #2
On Tue, 20 Jul 2021 21:46:47 +0200,
Nathan Chancellor wrote:
> 
> On Thu, Jul 15, 2021 at 09:58:36AM +0200, Takashi Iwai wrote:
> > This patch converts the resource management in PCI cs4281 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.
> > 
> > Signed-off-by: Takashi Iwai <tiwai@suse.de>
> > ---
> >  sound/pci/cs4281.c | 112 ++++++++++-----------------------------------
> >  1 file changed, 24 insertions(+), 88 deletions(-)
> > 
> > diff --git a/sound/pci/cs4281.c b/sound/pci/cs4281.c
> > index e122a168c148..f338caf98354 100644
> > --- a/sound/pci/cs4281.c
> > +++ b/sound/pci/cs4281.c
> > @@ -1268,8 +1268,10 @@ static inline int snd_cs4281_create_gameport(struct cs4281 *chip) { return -ENOS
> >  static inline void snd_cs4281_free_gameport(struct cs4281 *chip) { }
> >  #endif /* IS_REACHABLE(CONFIG_GAMEPORT) */
> >  
> > -static int snd_cs4281_free(struct cs4281 *chip)
> > +static void snd_cs4281_free(struct snd_card *card)
> >  {
> > +	struct cs4281 *chip = card->private_data;
> > +
> >  	snd_cs4281_free_gameport(chip);
> >  
> >  	/* Mask interrupts */
> > @@ -1278,49 +1280,20 @@ static int snd_cs4281_free(struct cs4281 *chip)
> >  	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
> >  	/* Sound System Power Management - Turn Everything OFF */
> >  	snd_cs4281_pokeBA0(chip, BA0_SSPM, 0);
> > -	/* PCI interface - D3 state */
> > -	pci_set_power_state(chip->pci, PCI_D3hot);
> > -
> > -	if (chip->irq >= 0)
> > -		free_irq(chip->irq, chip);
> > -	iounmap(chip->ba0);
> > -	iounmap(chip->ba1);
> > -	pci_release_regions(chip->pci);
> > -	pci_disable_device(chip->pci);
> > -
> > -	kfree(chip);
> > -	return 0;
> > -}
> > -
> > -static int snd_cs4281_dev_free(struct snd_device *device)
> > -{
> > -	struct cs4281 *chip = device->device_data;
> > -	return snd_cs4281_free(chip);
> >  }
> >  
> >  static int snd_cs4281_chip_init(struct cs4281 *chip); /* defined below */
> >  
> >  static int snd_cs4281_create(struct snd_card *card,
> >  			     struct pci_dev *pci,
> > -			     struct cs4281 **rchip,
> >  			     int dual_codec)
> >  {
> >  	struct cs4281 *chip;
> > -	unsigned int tmp;
> >  	int err;
> > -	static const struct snd_device_ops ops = {
> > -		.dev_free =	snd_cs4281_dev_free,
> > -	};
> >  
> > -	*rchip = 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;
> > -	}
> >  	spin_lock_init(&chip->reg_lock);
> >  	chip->card = card;
> 
> clang warns:
> 
> sound/pci/cs4281.c:1298:2: error: variable 'chip' is uninitialized when used here [-Werror,-Wuninitialized]
>         chip->card = card;
>         ^~~~
> sound/pci/cs4281.c:1291:21: note: initialize the variable 'chip' to silence this warning
>         struct cs4281 *chip;
>                            ^
>                             = NULL
> 1 error generated.

Thanks!  The fix patch is below.  I'll queue it up.


Takashi

-- 8< --
From: Takashi Iwai <tiwai@suse.de>
Subject: [PATCH] ALSA: cs4281: Fix missing chip initialization

The chip variable was forgotten to be initialized properly while
changing the object creation from the own malloc to
card->private_data.  This patch fixes it.

Fixes: 99041fea70d0 ("ALSA: cs4281: Allocate resources with device-managed APIs")
Reported-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/pci/cs4281.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/pci/cs4281.c b/sound/pci/cs4281.c
index f338caf98354..e7367402b84a 100644
--- a/sound/pci/cs4281.c
+++ b/sound/pci/cs4281.c
@@ -1288,7 +1288,7 @@ static int snd_cs4281_create(struct snd_card *card,
 			     struct pci_dev *pci,
 			     int dual_codec)
 {
-	struct cs4281 *chip;
+	struct cs4281 *chip = card->private_data;
 	int err;
 
 	err = pcim_enable_device(pci);
diff mbox series

Patch

diff --git a/sound/pci/cs4281.c b/sound/pci/cs4281.c
index e122a168c148..f338caf98354 100644
--- a/sound/pci/cs4281.c
+++ b/sound/pci/cs4281.c
@@ -1268,8 +1268,10 @@  static inline int snd_cs4281_create_gameport(struct cs4281 *chip) { return -ENOS
 static inline void snd_cs4281_free_gameport(struct cs4281 *chip) { }
 #endif /* IS_REACHABLE(CONFIG_GAMEPORT) */
 
-static int snd_cs4281_free(struct cs4281 *chip)
+static void snd_cs4281_free(struct snd_card *card)
 {
+	struct cs4281 *chip = card->private_data;
+
 	snd_cs4281_free_gameport(chip);
 
 	/* Mask interrupts */
@@ -1278,49 +1280,20 @@  static int snd_cs4281_free(struct cs4281 *chip)
 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
 	/* Sound System Power Management - Turn Everything OFF */
 	snd_cs4281_pokeBA0(chip, BA0_SSPM, 0);
-	/* PCI interface - D3 state */
-	pci_set_power_state(chip->pci, PCI_D3hot);
-
-	if (chip->irq >= 0)
-		free_irq(chip->irq, chip);
-	iounmap(chip->ba0);
-	iounmap(chip->ba1);
-	pci_release_regions(chip->pci);
-	pci_disable_device(chip->pci);
-
-	kfree(chip);
-	return 0;
-}
-
-static int snd_cs4281_dev_free(struct snd_device *device)
-{
-	struct cs4281 *chip = device->device_data;
-	return snd_cs4281_free(chip);
 }
 
 static int snd_cs4281_chip_init(struct cs4281 *chip); /* defined below */
 
 static int snd_cs4281_create(struct snd_card *card,
 			     struct pci_dev *pci,
-			     struct cs4281 **rchip,
 			     int dual_codec)
 {
 	struct cs4281 *chip;
-	unsigned int tmp;
 	int err;
-	static const struct snd_device_ops ops = {
-		.dev_free =	snd_cs4281_dev_free,
-	};
 
-	*rchip = 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;
-	}
 	spin_lock_init(&chip->reg_lock);
 	chip->card = card;
 	chip->pci = pci;
@@ -1332,46 +1305,29 @@  static int snd_cs4281_create(struct snd_card *card,
 	}
 	chip->dual_codec = dual_codec;
 
-	err = pci_request_regions(pci, "CS4281");
-	if (err < 0) {
-		kfree(chip);
-		pci_disable_device(pci);
+	err = pcim_iomap_regions(pci, 0x03, "CS4281"); /* 2 BARs */
+	if (err < 0)
 		return err;
-	}
 	chip->ba0_addr = pci_resource_start(pci, 0);
 	chip->ba1_addr = pci_resource_start(pci, 1);
 
-	chip->ba0 = pci_ioremap_bar(pci, 0);
-	chip->ba1 = pci_ioremap_bar(pci, 1);
-	if (!chip->ba0 || !chip->ba1) {
-		snd_cs4281_free(chip);
-		return -ENOMEM;
-	}
+	chip->ba0 = pcim_iomap_table(pci)[0];
+	chip->ba1 = pcim_iomap_table(pci)[1];
 	
-	if (request_irq(pci->irq, snd_cs4281_interrupt, IRQF_SHARED,
-			KBUILD_MODNAME, chip)) {
+	if (devm_request_irq(&pci->dev, pci->irq, snd_cs4281_interrupt,
+			     IRQF_SHARED, KBUILD_MODNAME, chip)) {
 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
-		snd_cs4281_free(chip);
 		return -ENOMEM;
 	}
 	chip->irq = pci->irq;
 	card->sync_irq = chip->irq;
+	card->private_free = snd_cs4281_free;
 
-	tmp = snd_cs4281_chip_init(chip);
-	if (tmp) {
-		snd_cs4281_free(chip);
-		return tmp;
-	}
-
-	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
-	if (err < 0) {
-		snd_cs4281_free(chip);
+	err = snd_cs4281_chip_init(chip);
+	if (err)
 		return err;
-	}
 
 	snd_cs4281_proc_init(chip);
-
-	*rchip = chip;
 	return 0;
 }
 
@@ -1887,46 +1843,34 @@  static int snd_cs4281_probe(struct pci_dev *pci,
 		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;
+	chip = card->private_data;
 
-	err = snd_cs4281_create(card, pci, &chip, dual_codec[dev]);
-	if (err < 0) {
-		snd_card_free(card);
+	err = snd_cs4281_create(card, pci, dual_codec[dev]);
+	if (err < 0)
 		return err;
-	}
-	card->private_data = chip;
 
 	err = snd_cs4281_mixer(chip);
-	if (err < 0) {
-		snd_card_free(card);
+	if (err < 0)
 		return err;
-	}
 	err = snd_cs4281_pcm(chip, 0);
-	if (err < 0) {
-		snd_card_free(card);
+	if (err < 0)
 		return err;
-	}
 	err = snd_cs4281_midi(chip, 0);
-	if (err < 0) {
-		snd_card_free(card);
+	if (err < 0)
 		return err;
-	}
 	err = snd_opl3_new(card, OPL3_HW_OPL3_CS4281, &opl3);
-	if (err < 0) {
-		snd_card_free(card);
+	if (err < 0)
 		return err;
-	}
 	opl3->private_data = chip;
 	opl3->command = snd_cs4281_opl3_command;
 	snd_opl3_init(opl3);
 	err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
-	if (err < 0) {
-		snd_card_free(card);
+	if (err < 0)
 		return err;
-	}
 	snd_cs4281_create_gameport(chip);
 	strcpy(card->driver, "CS4281");
 	strcpy(card->shortname, "Cirrus Logic CS4281");
@@ -1936,21 +1880,14 @@  static int snd_cs4281_probe(struct pci_dev *pci,
 		chip->irq);
 
 	err = snd_card_register(card);
-	if (err < 0) {
-		snd_card_free(card);
+	if (err < 0)
 		return err;
-	}
 
 	pci_set_drvdata(pci, card);
 	dev++;
 	return 0;
 }
 
-static void snd_cs4281_remove(struct pci_dev *pci)
-{
-	snd_card_free(pci_get_drvdata(pci));
-}
-
 /*
  * Power Management
  */
@@ -2054,7 +1991,6 @@  static struct pci_driver cs4281_driver = {
 	.name = KBUILD_MODNAME,
 	.id_table = snd_cs4281_ids,
 	.probe = snd_cs4281_probe,
-	.remove = snd_cs4281_remove,
 	.driver = {
 		.pm = CS4281_PM_OPS,
 	},