Skip to content

Commit

Permalink
ALSA: ymfpci: Allocate resources with device-managed APIs
Browse files Browse the repository at this point in the history
This patch converts the resource management in PCI ymfpci driver with
devres as a clean up.  Each manual resource management is converted
with the corresponding devres helper, the page allocations are done
with the 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-52-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
  • Loading branch information
tiwai committed Jul 19, 2021
1 parent 3bde335 commit c6e6bb5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 149 deletions.
66 changes: 25 additions & 41 deletions sound/pci/ymfpci/ymfpci.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ static int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
}

if (!r) {
r = request_region(io_port, 1, "YMFPCI gameport");
r = devm_request_region(&chip->pci->dev, io_port, 1,
"YMFPCI gameport");
if (!r) {
dev_err(chip->card->dev,
"joystick port %#x is in use.\n", io_port);
Expand All @@ -117,7 +118,6 @@ static int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
if (!gp) {
dev_err(chip->card->dev,
"cannot allocate memory for gameport\n");
release_and_free_resource(r);
return -ENOMEM;
}

Expand All @@ -126,7 +126,6 @@ static int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
gameport_set_dev_parent(gp, &chip->pci->dev);
gp->io = io_port;
gameport_set_port_data(gp, r);

if (chip->pci->device >= 0x0010) /* YMF 744/754 */
pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
Expand All @@ -142,12 +141,8 @@ static int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
{
if (chip->gameport) {
struct resource *r = gameport_get_port_data(chip->gameport);

gameport_unregister_port(chip->gameport);
chip->gameport = NULL;

release_and_free_resource(r);
}
}
#else
Expand Down Expand Up @@ -176,9 +171,10 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,
}

err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
0, &card);
sizeof(*chip), &card);
if (err < 0)
return err;
chip = card->private_data;

switch (pci_id->device) {
case 0x0004: str = "YMF724"; model = "DS-1"; break;
Expand All @@ -199,7 +195,8 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,
fm_port[dev] = pci_resource_start(pci, 1);
}
if (fm_port[dev] > 0)
fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3");
fm_res = devm_request_region(&pci->dev, fm_port[dev],
4, "YMFPCI OPL3");
if (fm_res) {
legacy_ctrl |= YMFPCI_LEGACY_FMEN;
pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
Expand All @@ -209,7 +206,8 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,
mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
}
if (mpu_port[dev] > 0)
mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401");
mpu_res = devm_request_region(&pci->dev, mpu_port[dev],
2, "YMFPCI MPU401");
if (mpu_res) {
legacy_ctrl |= YMFPCI_LEGACY_MEN;
pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
Expand All @@ -223,7 +221,8 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,
default: fm_port[dev] = 0; break;
}
if (fm_port[dev] > 0)
fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3");
fm_res = devm_request_region(&pci->dev, fm_port[dev],
4, "YMFPCI OPL3");
if (fm_res) {
legacy_ctrl |= YMFPCI_LEGACY_FMEN;
} else {
Expand All @@ -238,7 +237,8 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,
default: mpu_port[dev] = 0; break;
}
if (mpu_port[dev] > 0)
mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401");
mpu_res = devm_request_region(&pci->dev, mpu_port[dev],
2, "YMFPCI MPU401");
if (mpu_res) {
legacy_ctrl |= YMFPCI_LEGACY_MEN;
} else {
Expand All @@ -253,15 +253,9 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,
pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
err = snd_ymfpci_create(card, pci, old_legacy_ctrl, &chip);
if (err < 0) {
release_and_free_resource(mpu_res);
release_and_free_resource(fm_res);
goto free_card;
}
chip->fm_res = fm_res;
chip->mpu_res = mpu_res;
card->private_data = chip;
err = snd_ymfpci_create(card, pci, old_legacy_ctrl);
if (err < 0)
return err;

strcpy(card->driver, str);
sprintf(card->shortname, "Yamaha %s (%s)", model, str);
Expand All @@ -271,30 +265,30 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,
chip->irq);
err = snd_ymfpci_pcm(chip, 0);
if (err < 0)
goto free_card;
return err;

err = snd_ymfpci_pcm_spdif(chip, 1);
if (err < 0)
goto free_card;
return err;

err = snd_ymfpci_mixer(chip, rear_switch[dev]);
if (err < 0)
goto free_card;
return err;

if (chip->ac97->ext_id & AC97_EI_SDAC) {
err = snd_ymfpci_pcm_4ch(chip, 2);
if (err < 0)
goto free_card;
return err;

err = snd_ymfpci_pcm2(chip, 3);
if (err < 0)
goto free_card;
return err;
}
err = snd_ymfpci_timer(chip, 0);
if (err < 0)
goto free_card;
return err;

if (chip->mpu_res) {
if (mpu_res) {
err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
mpu_port[dev],
MPU401_INFO_INTEGRATED |
Expand All @@ -308,7 +302,7 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,
pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
}
}
if (chip->fm_res) {
if (fm_res) {
err = snd_opl3_create(card,
fm_port[dev],
fm_port[dev] + 2,
Expand All @@ -323,7 +317,7 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,
err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
if (err < 0) {
dev_err(card->dev, "cannot create opl3 hwdep\n");
goto free_card;
return err;
}
}
}
Expand All @@ -332,27 +326,17 @@ static int snd_card_ymfpci_probe(struct pci_dev *pci,

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

pci_set_drvdata(pci, card);
dev++;
return 0;

free_card:
snd_card_free(card);
return err;
}

static void snd_card_ymfpci_remove(struct pci_dev *pci)
{
snd_card_free(pci_get_drvdata(pci));
}

static struct pci_driver ymfpci_driver = {
.name = KBUILD_MODNAME,
.id_table = snd_ymfpci_ids,
.probe = snd_card_ymfpci_probe,
.remove = snd_card_ymfpci_remove,
#ifdef CONFIG_PM_SLEEP
.driver = {
.pm = &snd_ymfpci_pm,
Expand Down
8 changes: 2 additions & 6 deletions sound/pci/ymfpci/ymfpci.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,13 @@ struct snd_ymfpci {
unsigned char rev; /* PCI revision */
unsigned long reg_area_phys;
void __iomem *reg_area_virt;
struct resource *res_reg_area;
struct resource *fm_res;
struct resource *mpu_res;

unsigned short old_legacy_ctrl;
#ifdef SUPPORT_JOYSTICK
struct gameport *gameport;
#endif

struct snd_dma_buffer work_ptr;
struct snd_dma_buffer *work_ptr;

unsigned int bank_size_playback;
unsigned int bank_size_capture;
Expand Down Expand Up @@ -358,8 +355,7 @@ struct snd_ymfpci {

int snd_ymfpci_create(struct snd_card *card,
struct pci_dev *pci,
unsigned short old_legacy_ctrl,
struct snd_ymfpci ** rcodec);
unsigned short old_legacy_ctrl);
void snd_ymfpci_free_gameport(struct snd_ymfpci *chip);

extern const struct dev_pm_ops snd_ymfpci_pm;
Expand Down
Loading

0 comments on commit c6e6bb5

Please sign in to comment.